blob: b293869cb23934546941b72010b051bc747ce3fb [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 Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_VERTSPLIT
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 Moolenaar071d4272004-06-13 20:20:40 +0000159#ifdef FEAT_VERTSPLIT
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
173#ifdef FEAT_VERTSPLIT
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
183#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
184/* 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 Moolenaar071d4272004-06-13 20:20:40 +0000414 * Changed something in the current window, at buffer line "lnum", that
415 * requires that line and possibly other lines to be redrawn.
416 * Used when entering/leaving Insert mode with the cursor on a folded line.
417 * Used to remove the "$" from a change command.
418 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
419 * may become invalid and the whole window will have to be redrawn.
420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100422redrawWinline(
423 linenr_T lnum,
424 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
426#ifdef FEAT_FOLDING
427 int i;
428#endif
429
430 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
431 curwin->w_redraw_top = lnum;
432 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
433 curwin->w_redraw_bot = lnum;
434 redraw_later(VALID);
435
436#ifdef FEAT_FOLDING
437 if (invalid)
438 {
439 /* A w_lines[] entry for this lnum has become invalid. */
440 i = find_wl_entry(curwin, lnum);
441 if (i >= 0)
442 curwin->w_lines[i].wl_valid = FALSE;
443 }
444#endif
445}
446
447/*
448 * update all windows that are editing the current buffer
449 */
450 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100451update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 redraw_curbuf_later(type);
454 update_screen(type);
455}
456
457/*
458 * update_screen()
459 *
460 * Based on the current value of curwin->w_topline, transfer a screenfull
461 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
462 */
463 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100464update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 win_T *wp;
467 static int did_intro = FALSE;
468#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
469 int did_one;
470#endif
471
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000472 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 if (!screen_valid(TRUE))
474 return;
475
476 if (must_redraw)
477 {
478 if (type < must_redraw) /* use maximal type */
479 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000480
481 /* must_redraw is reset here, so that when we run into some weird
482 * reason to redraw while busy redrawing (e.g., asynchronous
483 * scrolling), or update_topline() in win_update() will cause a
484 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 must_redraw = 0;
486 }
487
488 /* Need to update w_lines[]. */
489 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
490 type = NOT_VALID;
491
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000492 /* Postpone the redrawing when it's not needed and when being called
493 * recursively. */
494 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 redraw_later(type); /* remember type for next time */
497 must_redraw = type;
498 if (type > INVERTED_ALL)
499 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
500 return;
501 }
502
503 updating_screen = TRUE;
504#ifdef FEAT_SYN_HL
505 ++display_tick; /* let syntax code know we're in a next round of
506 * display updating */
507#endif
508
509 /*
510 * if the screen was scrolled up when displaying a message, scroll it down
511 */
512 if (msg_scrolled)
513 {
514 clear_cmdline = TRUE;
515 if (msg_scrolled > Rows - 5) /* clearing is faster */
516 type = CLEAR;
517 else if (type != CLEAR)
518 {
519 check_for_delay(FALSE);
520 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
521 type = CLEAR;
522 FOR_ALL_WINDOWS(wp)
523 {
524 if (W_WINROW(wp) < msg_scrolled)
525 {
526 if (W_WINROW(wp) + wp->w_height > msg_scrolled
527 && wp->w_redr_type < REDRAW_TOP
528 && wp->w_lines_valid > 0
529 && wp->w_topline == wp->w_lines[0].wl_lnum)
530 {
531 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
532 wp->w_redr_type = REDRAW_TOP;
533 }
534 else
535 {
536 wp->w_redr_type = NOT_VALID;
537#ifdef FEAT_WINDOWS
538 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
539 <= msg_scrolled)
540 wp->w_redr_status = TRUE;
541#endif
542 }
543 }
544 }
545 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000546#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000547 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550 msg_scrolled = 0;
551 need_wait_return = FALSE;
552 }
553
554 /* reset cmdline_row now (may have been changed temporarily) */
555 compute_cmdrow();
556
557 /* Check for changed highlighting */
558 if (need_highlight_changed)
559 highlight_changed();
560
561 if (type == CLEAR) /* first clear screen */
562 {
563 screenclear(); /* will reset clear_cmdline */
564 type = NOT_VALID;
565 }
566
567 if (clear_cmdline) /* going to clear cmdline (done below) */
568 check_for_delay(FALSE);
569
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000570#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200571 /* Force redraw when width of 'number' or 'relativenumber' column
572 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000573 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200574 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
575 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000576 curwin->w_redr_type = NOT_VALID;
577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 /*
580 * Only start redrawing if there is really something to do.
581 */
582 if (type == INVERTED)
583 update_curswant();
584 if (curwin->w_redr_type < type
585 && !((type == VALID
586 && curwin->w_lines[0].wl_valid
587#ifdef FEAT_DIFF
588 && curwin->w_topfill == curwin->w_old_topfill
589 && curwin->w_botfill == curwin->w_old_botfill
590#endif
591 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000593 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
595 && curwin->w_old_visual_mode == VIsual_mode
596 && (curwin->w_valid & VALID_VIRTCOL)
597 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 ))
599 curwin->w_redr_type = type;
600
Bram Moolenaar5a305422006-04-28 22:38:25 +0000601#ifdef FEAT_WINDOWS
602 /* Redraw the tab pages line if needed. */
603 if (redraw_tabline || type >= NOT_VALID)
604 draw_tabline();
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef FEAT_SYN_HL
608 /*
609 * Correct stored syntax highlighting info for changes in each displayed
610 * buffer. Each buffer must only be done once.
611 */
612 FOR_ALL_WINDOWS(wp)
613 {
614 if (wp->w_buffer->b_mod_set)
615 {
616# ifdef FEAT_WINDOWS
617 win_T *wwp;
618
619 /* Check if we already did this buffer. */
620 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
621 if (wwp->w_buffer == wp->w_buffer)
622 break;
623# endif
624 if (
625# ifdef FEAT_WINDOWS
626 wwp == wp &&
627# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200628 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 syn_stack_apply_changes(wp->w_buffer);
630 }
631 }
632#endif
633
634 /*
635 * Go from top to bottom through the windows, redrawing the ones that need
636 * it.
637 */
638#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
639 did_one = FALSE;
640#endif
641#ifdef FEAT_SEARCH_EXTRA
642 search_hl.rm.regprog = NULL;
643#endif
644 FOR_ALL_WINDOWS(wp)
645 {
646 if (wp->w_redr_type != 0)
647 {
648 cursor_off();
649#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
650 if (!did_one)
651 {
652 did_one = TRUE;
653# ifdef FEAT_SEARCH_EXTRA
654 start_search_hl();
655# endif
656# ifdef FEAT_CLIPBOARD
657 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200658 if (clip_star.available && clip_isautosel_star())
659 clip_update_selection(&clip_star);
660 if (clip_plus.available && clip_isautosel_plus())
661 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# endif
663#ifdef FEAT_GUI
664 /* Remove the cursor before starting to do anything, because
665 * scrolling may make it difficult to redraw the text under
666 * it. */
667 if (gui.in_use)
668 gui_undraw_cursor();
669#endif
670 }
671#endif
672 win_update(wp);
673 }
674
675#ifdef FEAT_WINDOWS
676 /* redraw status line after the window to minimize cursor movement */
677 if (wp->w_redr_status)
678 {
679 cursor_off();
680 win_redr_status(wp);
681 }
682#endif
683 }
684#if defined(FEAT_SEARCH_EXTRA)
685 end_search_hl();
686#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100687#ifdef FEAT_INS_EXPAND
688 /* May need to redraw the popup menu. */
689 if (pum_visible())
690 pum_redraw();
691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693#ifdef FEAT_WINDOWS
694 /* Reset b_mod_set flags. Going through all windows is probably faster
695 * than going through all buffers (there could be many buffers). */
696 for (wp = firstwin; wp != NULL; wp = wp->w_next)
697 wp->w_buffer->b_mod_set = FALSE;
698#else
699 curbuf->b_mod_set = FALSE;
700#endif
701
702 updating_screen = FALSE;
703#ifdef FEAT_GUI
704 gui_may_resize_shell();
705#endif
706
707 /* Clear or redraw the command line. Done last, because scrolling may
708 * mess up the command line. */
709 if (clear_cmdline || redraw_cmdline)
710 showmode();
711
712 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200713 if (!did_intro)
714 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 did_intro = TRUE;
716
717#ifdef FEAT_GUI
718 /* Redraw the cursor and update the scrollbars when all screen updating is
719 * done. */
720 if (gui.in_use)
721 {
722 out_flush(); /* required before updating the cursor */
723 if (did_one)
724 gui_update_cursor(FALSE, FALSE);
725 gui_update_scrollbars(FALSE);
726 }
727#endif
728}
729
Bram Moolenaar860cae12010-06-05 23:22:07 +0200730#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200731/*
732 * Return TRUE if the cursor line in window "wp" may be concealed, according
733 * to the 'concealcursor' option.
734 */
735 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100736conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200737{
738 int c;
739
740 if (*wp->w_p_cocu == NUL)
741 return FALSE;
742 if (get_real_state() & VISUAL)
743 c = 'v';
744 else if (State & INSERT)
745 c = 'i';
746 else if (State & NORMAL)
747 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200748 else if (State & CMDLINE)
749 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200750 else
751 return FALSE;
752 return vim_strchr(wp->w_p_cocu, c) != NULL;
753}
754
755/*
756 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
757 */
758 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100759conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200760{
761 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
762 {
763 need_cursor_line_redraw = TRUE;
764 /* Need to recompute cursor column, e.g., when starting Visual mode
765 * without concealing. */
766 curs_columns(TRUE);
767 }
768}
769
Bram Moolenaar860cae12010-06-05 23:22:07 +0200770 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100771update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200772{
773 int row;
774 int j;
775
776 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200777 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200778 {
779# ifdef FEAT_GUI
780 /* Remove the cursor before starting to do anything, because scrolling
781 * may make it difficult to redraw the text under it. */
782 if (gui.in_use)
783 gui_undraw_cursor();
784# endif
785 row = 0;
786 for (j = 0; j < wp->w_lines_valid; ++j)
787 {
788 if (lnum == wp->w_lines[j].wl_lnum)
789 {
790 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200791# ifdef FEAT_SEARCH_EXTRA
792 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200793 start_search_hl();
794 prepare_search_hl(wp, lnum);
795# endif
796 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
797# if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799# endif
800 break;
801 }
802 row += wp->w_lines[j].wl_size;
803 }
804# ifdef FEAT_GUI
805 /* Redraw the cursor */
806 if (gui.in_use)
807 {
808 out_flush(); /* required before updating the cursor */
809 gui_update_cursor(FALSE, FALSE);
810 }
811# endif
812 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200813 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200814}
815#endif
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100818static void update_prepare(void);
819static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821/*
822 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000823 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 */
825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100826update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 cursor_off();
829 updating_screen = TRUE;
830#ifdef FEAT_GUI
831 /* Remove the cursor before starting to do anything, because scrolling may
832 * make it difficult to redraw the text under it. */
833 if (gui.in_use)
834 gui_undraw_cursor();
835#endif
836#ifdef FEAT_SEARCH_EXTRA
837 start_search_hl();
838#endif
839}
840
841/*
842 * Finish updating one or more windows.
843 */
844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100845update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
847 if (redraw_cmdline)
848 showmode();
849
850# ifdef FEAT_SEARCH_EXTRA
851 end_search_hl();
852# endif
853
854 updating_screen = FALSE;
855
856# ifdef FEAT_GUI
857 gui_may_resize_shell();
858
859 /* Redraw the cursor and update the scrollbars when all screen updating is
860 * done. */
861 if (gui.in_use)
862 {
863 out_flush(); /* required before updating the cursor */
864 gui_update_cursor(FALSE, FALSE);
865 gui_update_scrollbars(FALSE);
866 }
867# endif
868}
869#endif
870
871#if defined(FEAT_SIGNS) || defined(PROTO)
872 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100873update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 win_T *wp;
876 int doit = FALSE;
877
878# ifdef FEAT_FOLDING
879 win_foldinfo.fi_level = 0;
880# endif
881
882 /* update/delete a specific mark */
883 FOR_ALL_WINDOWS(wp)
884 {
885 if (buf != NULL && lnum > 0)
886 {
887 if (wp->w_buffer == buf && lnum >= wp->w_topline
888 && lnum < wp->w_botline)
889 {
890 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
891 wp->w_redraw_top = lnum;
892 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
893 wp->w_redraw_bot = lnum;
894 redraw_win_later(wp, VALID);
895 }
896 }
897 else
898 redraw_win_later(wp, VALID);
899 if (wp->w_redr_type != 0)
900 doit = TRUE;
901 }
902
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100903 /* Return when there is nothing to do, screen updating is already
904 * happening (recursive call) or still starting up. */
905 if (!doit || updating_screen
906#ifdef FEAT_GUI
907 || gui.starting
908#endif
909 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 return;
911
912 /* update all windows that need updating */
913 update_prepare();
914
915# ifdef FEAT_WINDOWS
916 for (wp = firstwin; wp; wp = wp->w_next)
917 {
918 if (wp->w_redr_type != 0)
919 win_update(wp);
920 if (wp->w_redr_status)
921 win_redr_status(wp);
922 }
923# else
924 if (curwin->w_redr_type != 0)
925 win_update(curwin);
926# endif
927
928 update_finish();
929}
930#endif
931
932
933#if defined(FEAT_GUI) || defined(PROTO)
934/*
935 * Update a single window, its status line and maybe the command line msg.
936 * Used for the GUI scrollbar.
937 */
938 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100939updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000941 /* return if already busy updating */
942 if (updating_screen)
943 return;
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 update_prepare();
946
947#ifdef FEAT_CLIPBOARD
948 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200949 if (clip_star.available && clip_isautosel_star())
950 clip_update_selection(&clip_star);
951 if (clip_plus.available && clip_isautosel_plus())
952 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000958 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000959 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000960 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (wp->w_redr_status
963# ifdef FEAT_CMDL_INFO
964 || p_ru
965# endif
966# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000967 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968# endif
969 )
970 win_redr_status(wp);
971#endif
972
973 update_finish();
974}
975#endif
976
977/*
978 * Update a single window.
979 *
980 * This may cause the windows below it also to be redrawn (when clearing the
981 * screen or scrolling lines).
982 *
983 * How the window is redrawn depends on wp->w_redr_type. Each type also
984 * implies the one below it.
985 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000986 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
988 * INVERTED redraw the changed part of the Visual area
989 * INVERTED_ALL redraw the whole Visual area
990 * VALID 1. scroll up/down to adjust for a changed w_topline
991 * 2. update lines at the top when scrolled down
992 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000993 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * b_mod_top and b_mod_bot.
995 * - if wp->w_redraw_top non-zero, redraw lines between
996 * wp->w_redraw_top and wp->w_redr_bot.
997 * - continue redrawing when syntax status is invalid.
998 * 4. if scrolled up, update lines at the bottom.
999 * This results in three areas that may need updating:
1000 * top: from first row to top_end (when scrolled down)
1001 * mid: from mid_start to mid_end (update inversion or changed text)
1002 * bot: from bot_start to last row (when scrolled up)
1003 */
1004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001005win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 buf_T *buf = wp->w_buffer;
1008 int type;
1009 int top_end = 0; /* Below last row of the top area that needs
1010 updating. 0 when no top area updating. */
1011 int mid_start = 999;/* first row of the mid area that needs
1012 updating. 999 when no mid area updating. */
1013 int mid_end = 0; /* Below last row of the mid area that needs
1014 updating. 0 when no mid area updating. */
1015 int bot_start = 999;/* first row of the bot area that needs
1016 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int scrolled_down = FALSE; /* TRUE when scrolled down when
1018 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001020 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 int top_to_mod = FALSE; /* redraw above mod_top */
1022#endif
1023
1024 int row; /* current window row to display */
1025 linenr_T lnum; /* current buffer lnum to display */
1026 int idx; /* current index in w_lines[] */
1027 int srow; /* starting row of the current line */
1028
1029 int eof = FALSE; /* if TRUE, we hit the end of the file */
1030 int didline = FALSE; /* if TRUE, we finished the last line */
1031 int i;
1032 long j;
1033 static int recursive = FALSE; /* being called recursively */
1034 int old_botline = wp->w_botline;
1035#ifdef FEAT_FOLDING
1036 long fold_count;
1037#endif
1038#ifdef FEAT_SYN_HL
1039 /* remember what happened to the previous line, to know if
1040 * check_visual_highlight() can be used */
1041#define DID_NONE 1 /* didn't update a line */
1042#define DID_LINE 2 /* updated a normal line */
1043#define DID_FOLD 3 /* updated a folded line */
1044 int did_update = DID_NONE;
1045 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1046#endif
1047 linenr_T mod_top = 0;
1048 linenr_T mod_bot = 0;
1049#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1050 int save_got_int;
1051#endif
1052
1053 type = wp->w_redr_type;
1054
1055 if (type == NOT_VALID)
1056 {
1057#ifdef FEAT_WINDOWS
1058 wp->w_redr_status = TRUE;
1059#endif
1060 wp->w_lines_valid = 0;
1061 }
1062
1063 /* Window is zero-height: nothing to draw. */
1064 if (wp->w_height == 0)
1065 {
1066 wp->w_redr_type = 0;
1067 return;
1068 }
1069
1070#ifdef FEAT_VERTSPLIT
1071 /* Window is zero-width: Only need to draw the separator. */
1072 if (wp->w_width == 0)
1073 {
1074 /* draw the vertical separator right of this window */
1075 draw_vsep_win(wp, 0);
1076 wp->w_redr_type = 0;
1077 return;
1078 }
1079#endif
1080
1081#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001082 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
1084
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001085#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001086 /* Force redraw when width of 'number' or 'relativenumber' column
1087 * changes. */
1088 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001089 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001090 {
1091 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001092 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001093 }
1094 else
1095#endif
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1098 {
1099 /*
1100 * When there are both inserted/deleted lines and specific lines to be
1101 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1102 * everything (only happens when redrawing is off for while).
1103 */
1104 type = NOT_VALID;
1105 }
1106 else
1107 {
1108 /*
1109 * Set mod_top to the first line that needs displaying because of
1110 * changes. Set mod_bot to the first line after the changes.
1111 */
1112 mod_top = wp->w_redraw_top;
1113 if (wp->w_redraw_bot != 0)
1114 mod_bot = wp->w_redraw_bot + 1;
1115 else
1116 mod_bot = 0;
1117 wp->w_redraw_top = 0; /* reset for next time */
1118 wp->w_redraw_bot = 0;
1119 if (buf->b_mod_set)
1120 {
1121 if (mod_top == 0 || mod_top > buf->b_mod_top)
1122 {
1123 mod_top = buf->b_mod_top;
1124#ifdef FEAT_SYN_HL
1125 /* Need to redraw lines above the change that may be included
1126 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001127 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001129 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (mod_top < 1)
1131 mod_top = 1;
1132 }
1133#endif
1134 }
1135 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1136 mod_bot = buf->b_mod_bot;
1137
1138#ifdef FEAT_SEARCH_EXTRA
1139 /* When 'hlsearch' is on and using a multi-line search pattern, a
1140 * change in one line may make the Search highlighting in a
1141 * previous line invalid. Simple solution: redraw all visible
1142 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001143 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001145 if (search_hl.rm.regprog != NULL
1146 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001148 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001149 {
1150 cur = wp->w_match_head;
1151 while (cur != NULL)
1152 {
1153 if (cur->match.regprog != NULL
1154 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001155 {
1156 top_to_mod = TRUE;
1157 break;
1158 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001159 cur = cur->next;
1160 }
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163 }
1164#ifdef FEAT_FOLDING
1165 if (mod_top != 0 && hasAnyFolding(wp))
1166 {
1167 linenr_T lnumt, lnumb;
1168
1169 /*
1170 * A change in a line can cause lines above it to become folded or
1171 * unfolded. Find the top most buffer line that may be affected.
1172 * If the line was previously folded and displayed, get the first
1173 * line of that fold. If the line is folded now, get the first
1174 * folded line. Use the minimum of these two.
1175 */
1176
1177 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1178 * the line below it. If there is no valid entry, use w_topline.
1179 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1180 * to this line. If there is no valid entry, use MAXLNUM. */
1181 lnumt = wp->w_topline;
1182 lnumb = MAXLNUM;
1183 for (i = 0; i < wp->w_lines_valid; ++i)
1184 if (wp->w_lines[i].wl_valid)
1185 {
1186 if (wp->w_lines[i].wl_lastlnum < mod_top)
1187 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1188 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1189 {
1190 lnumb = wp->w_lines[i].wl_lnum;
1191 /* When there is a fold column it might need updating
1192 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001193 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 ++lnumb;
1195 }
1196 }
1197
1198 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1199 if (mod_top > lnumt)
1200 mod_top = lnumt;
1201
1202 /* Now do the same for the bottom line (one above mod_bot). */
1203 --mod_bot;
1204 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1205 ++mod_bot;
1206 if (mod_bot < lnumb)
1207 mod_bot = lnumb;
1208 }
1209#endif
1210
1211 /* When a change starts above w_topline and the end is below
1212 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001213 * If the end of the change is above w_topline: do like no change was
1214 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (mod_top != 0 && mod_top < wp->w_topline)
1216 {
1217 if (mod_bot > wp->w_topline)
1218 mod_top = wp->w_topline;
1219#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001220 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 top_end = 1;
1222#endif
1223 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224
1225 /* When line numbers are displayed need to redraw all lines below
1226 * inserted/deleted lines. */
1227 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1228 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * When only displaying the lines at the top, set top_end. Used when
1233 * window has scrolled down for msg_scrolled.
1234 */
1235 if (type == REDRAW_TOP)
1236 {
1237 j = 0;
1238 for (i = 0; i < wp->w_lines_valid; ++i)
1239 {
1240 j += wp->w_lines[i].wl_size;
1241 if (j >= wp->w_upd_rows)
1242 {
1243 top_end = j;
1244 break;
1245 }
1246 }
1247 if (top_end == 0)
1248 /* not found (cannot happen?): redraw everything */
1249 type = NOT_VALID;
1250 else
1251 /* top area defined, the rest is VALID */
1252 type = VALID;
1253 }
1254
Bram Moolenaar367329b2007-08-30 11:53:22 +00001255 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001256 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1257 * non-zero and thus not FALSE) will indicate that screenclear() was not
1258 * called. */
1259 if (screen_cleared)
1260 screen_cleared = MAYBE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /*
1263 * If there are no changes on the screen that require a complete redraw,
1264 * handle three cases:
1265 * 1: we are off the top of the screen by a few lines: scroll down
1266 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1267 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1268 * w_lines[] that needs updating.
1269 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001270 if ((type == VALID || type == SOME_VALID
1271 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272#ifdef FEAT_DIFF
1273 && !wp->w_botfill && !wp->w_old_botfill
1274#endif
1275 )
1276 {
1277 if (mod_top != 0 && wp->w_topline == mod_top)
1278 {
1279 /*
1280 * w_topline is the first changed line, the scrolling will be done
1281 * further down.
1282 */
1283 }
1284 else if (wp->w_lines[0].wl_valid
1285 && (wp->w_topline < wp->w_lines[0].wl_lnum
1286#ifdef FEAT_DIFF
1287 || (wp->w_topline == wp->w_lines[0].wl_lnum
1288 && wp->w_topfill > wp->w_old_topfill)
1289#endif
1290 ))
1291 {
1292 /*
1293 * New topline is above old topline: May scroll down.
1294 */
1295#ifdef FEAT_FOLDING
1296 if (hasAnyFolding(wp))
1297 {
1298 linenr_T ln;
1299
1300 /* count the number of lines we are off, counting a sequence
1301 * of folded lines as one */
1302 j = 0;
1303 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1304 {
1305 ++j;
1306 if (j >= wp->w_height - 2)
1307 break;
1308 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1309 }
1310 }
1311 else
1312#endif
1313 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1314 if (j < wp->w_height - 2) /* not too far off */
1315 {
1316 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1317#ifdef FEAT_DIFF
1318 /* insert extra lines for previously invisible filler lines */
1319 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1320 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1321 - wp->w_old_topfill;
1322#endif
1323 if (i < wp->w_height - 2) /* less than a screen off */
1324 {
1325 /*
1326 * Try to insert the correct number of lines.
1327 * If not the last window, delete the lines at the bottom.
1328 * win_ins_lines may fail when the terminal can't do it.
1329 */
1330 if (i > 0)
1331 check_for_delay(FALSE);
1332 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1333 {
1334 if (wp->w_lines_valid != 0)
1335 {
1336 /* Need to update rows that are new, stop at the
1337 * first one that scrolled down. */
1338 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 /* Move the entries that were scrolled, disable
1342 * the entries for the lines to be redrawn. */
1343 if ((wp->w_lines_valid += j) > wp->w_height)
1344 wp->w_lines_valid = wp->w_height;
1345 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1346 wp->w_lines[idx] = wp->w_lines[idx - j];
1347 while (idx >= 0)
1348 wp->w_lines[idx--].wl_valid = FALSE;
1349 }
1350 }
1351 else
1352 mid_start = 0; /* redraw all lines */
1353 }
1354 else
1355 mid_start = 0; /* redraw all lines */
1356 }
1357 else
1358 mid_start = 0; /* redraw all lines */
1359 }
1360 else
1361 {
1362 /*
1363 * New topline is at or below old topline: May scroll up.
1364 * When topline didn't change, find first entry in w_lines[] that
1365 * needs updating.
1366 */
1367
1368 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1369 j = -1;
1370 row = 0;
1371 for (i = 0; i < wp->w_lines_valid; i++)
1372 {
1373 if (wp->w_lines[i].wl_valid
1374 && wp->w_lines[i].wl_lnum == wp->w_topline)
1375 {
1376 j = i;
1377 break;
1378 }
1379 row += wp->w_lines[i].wl_size;
1380 }
1381 if (j == -1)
1382 {
1383 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1384 * lines */
1385 mid_start = 0;
1386 }
1387 else
1388 {
1389 /*
1390 * Try to delete the correct number of lines.
1391 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1392 */
1393#ifdef FEAT_DIFF
1394 /* If the topline didn't change, delete old filler lines,
1395 * otherwise delete filler lines of the new topline... */
1396 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1397 row += wp->w_old_topfill;
1398 else
1399 row += diff_check_fill(wp, wp->w_topline);
1400 /* ... but don't delete new filler lines. */
1401 row -= wp->w_topfill;
1402#endif
1403 if (row > 0)
1404 {
1405 check_for_delay(FALSE);
1406 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1407 bot_start = wp->w_height - row;
1408 else
1409 mid_start = 0; /* redraw all lines */
1410 }
1411 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1412 {
1413 /*
1414 * Skip the lines (below the deleted lines) that are still
1415 * valid and don't need redrawing. Copy their info
1416 * upwards, to compensate for the deleted lines. Set
1417 * bot_start to the first row that needs redrawing.
1418 */
1419 bot_start = 0;
1420 idx = 0;
1421 for (;;)
1422 {
1423 wp->w_lines[idx] = wp->w_lines[j];
1424 /* stop at line that didn't fit, unless it is still
1425 * valid (no lines deleted) */
1426 if (row > 0 && bot_start + row
1427 + (int)wp->w_lines[j].wl_size > wp->w_height)
1428 {
1429 wp->w_lines_valid = idx + 1;
1430 break;
1431 }
1432 bot_start += wp->w_lines[idx++].wl_size;
1433
1434 /* stop at the last valid entry in w_lines[].wl_size */
1435 if (++j >= wp->w_lines_valid)
1436 {
1437 wp->w_lines_valid = idx;
1438 break;
1439 }
1440 }
1441#ifdef FEAT_DIFF
1442 /* Correct the first entry for filler lines at the top
1443 * when it won't get updated below. */
1444 if (wp->w_p_diff && bot_start > 0)
1445 wp->w_lines[0].wl_size =
1446 plines_win_nofill(wp, wp->w_topline, TRUE)
1447 + wp->w_topfill;
1448#endif
1449 }
1450 }
1451 }
1452
1453 /* When starting redraw in the first line, redraw all lines. When
1454 * there is only one window it's probably faster to clear the screen
1455 * first. */
1456 if (mid_start == 0)
1457 {
1458 mid_end = wp->w_height;
1459 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001460 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001461 /* Clear the screen when it was not done by win_del_lines() or
1462 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1463 * then. */
1464 if (screen_cleared != TRUE)
1465 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001466#ifdef FEAT_WINDOWS
1467 /* The screen was cleared, redraw the tab pages line. */
1468 if (redraw_tabline)
1469 draw_tabline();
1470#endif
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473
1474 /* When win_del_lines() or win_ins_lines() caused the screen to be
1475 * cleared (only happens for the first window) or when screenclear()
1476 * was called directly above, "must_redraw" will have been set to
1477 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1478 if (screen_cleared == TRUE)
1479 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 else
1482 {
1483 /* Not VALID or INVERTED: redraw all lines. */
1484 mid_start = 0;
1485 mid_end = wp->w_height;
1486 }
1487
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001488 if (type == SOME_VALID)
1489 {
1490 /* SOME_VALID: redraw all lines. */
1491 mid_start = 0;
1492 mid_end = wp->w_height;
1493 type = NOT_VALID;
1494 }
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /* check if we are updating or removing the inverted part */
1497 if ((VIsual_active && buf == curwin->w_buffer)
1498 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1499 {
1500 linenr_T from, to;
1501
1502 if (VIsual_active)
1503 {
1504 if (VIsual_active
1505 && (VIsual_mode != wp->w_old_visual_mode
1506 || type == INVERTED_ALL))
1507 {
1508 /*
1509 * If the type of Visual selection changed, redraw the whole
1510 * selection. Also when the ownership of the X selection is
1511 * gained or lost.
1512 */
1513 if (curwin->w_cursor.lnum < VIsual.lnum)
1514 {
1515 from = curwin->w_cursor.lnum;
1516 to = VIsual.lnum;
1517 }
1518 else
1519 {
1520 from = VIsual.lnum;
1521 to = curwin->w_cursor.lnum;
1522 }
1523 /* redraw more when the cursor moved as well */
1524 if (wp->w_old_cursor_lnum < from)
1525 from = wp->w_old_cursor_lnum;
1526 if (wp->w_old_cursor_lnum > to)
1527 to = wp->w_old_cursor_lnum;
1528 if (wp->w_old_visual_lnum < from)
1529 from = wp->w_old_visual_lnum;
1530 if (wp->w_old_visual_lnum > to)
1531 to = wp->w_old_visual_lnum;
1532 }
1533 else
1534 {
1535 /*
1536 * Find the line numbers that need to be updated: The lines
1537 * between the old cursor position and the current cursor
1538 * position. Also check if the Visual position changed.
1539 */
1540 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1541 {
1542 from = curwin->w_cursor.lnum;
1543 to = wp->w_old_cursor_lnum;
1544 }
1545 else
1546 {
1547 from = wp->w_old_cursor_lnum;
1548 to = curwin->w_cursor.lnum;
1549 if (from == 0) /* Visual mode just started */
1550 from = to;
1551 }
1552
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001553 if (VIsual.lnum != wp->w_old_visual_lnum
1554 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 if (wp->w_old_visual_lnum < from
1557 && wp->w_old_visual_lnum != 0)
1558 from = wp->w_old_visual_lnum;
1559 if (wp->w_old_visual_lnum > to)
1560 to = wp->w_old_visual_lnum;
1561 if (VIsual.lnum < from)
1562 from = VIsual.lnum;
1563 if (VIsual.lnum > to)
1564 to = VIsual.lnum;
1565 }
1566 }
1567
1568 /*
1569 * If in block mode and changed column or curwin->w_curswant:
1570 * update all lines.
1571 * First compute the actual start and end column.
1572 */
1573 if (VIsual_mode == Ctrl_V)
1574 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001575 colnr_T fromc, toc;
1576#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1577 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar404406a2014-10-09 13:24:43 +02001579 if (curwin->w_p_lbr)
1580 ve_flags = VE_ALL;
1581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001583#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1584 ve_flags = save_ve_flags;
1585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 ++toc;
1587 if (curwin->w_curswant == MAXCOL)
1588 toc = MAXCOL;
1589
1590 if (fromc != wp->w_old_cursor_fcol
1591 || toc != wp->w_old_cursor_lcol)
1592 {
1593 if (from > VIsual.lnum)
1594 from = VIsual.lnum;
1595 if (to < VIsual.lnum)
1596 to = VIsual.lnum;
1597 }
1598 wp->w_old_cursor_fcol = fromc;
1599 wp->w_old_cursor_lcol = toc;
1600 }
1601 }
1602 else
1603 {
1604 /* Use the line numbers of the old Visual area. */
1605 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1606 {
1607 from = wp->w_old_cursor_lnum;
1608 to = wp->w_old_visual_lnum;
1609 }
1610 else
1611 {
1612 from = wp->w_old_visual_lnum;
1613 to = wp->w_old_cursor_lnum;
1614 }
1615 }
1616
1617 /*
1618 * There is no need to update lines above the top of the window.
1619 */
1620 if (from < wp->w_topline)
1621 from = wp->w_topline;
1622
1623 /*
1624 * If we know the value of w_botline, use it to restrict the update to
1625 * the lines that are visible in the window.
1626 */
1627 if (wp->w_valid & VALID_BOTLINE)
1628 {
1629 if (from >= wp->w_botline)
1630 from = wp->w_botline - 1;
1631 if (to >= wp->w_botline)
1632 to = wp->w_botline - 1;
1633 }
1634
1635 /*
1636 * Find the minimal part to be updated.
1637 * Watch out for scrolling that made entries in w_lines[] invalid.
1638 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1639 * top_end; need to redraw from top_end to the "to" line.
1640 * A middle mouse click with a Visual selection may change the text
1641 * above the Visual area and reset wl_valid, do count these for
1642 * mid_end (in srow).
1643 */
1644 if (mid_start > 0)
1645 {
1646 lnum = wp->w_topline;
1647 idx = 0;
1648 srow = 0;
1649 if (scrolled_down)
1650 mid_start = top_end;
1651 else
1652 mid_start = 0;
1653 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1654 {
1655 if (wp->w_lines[idx].wl_valid)
1656 mid_start += wp->w_lines[idx].wl_size;
1657 else if (!scrolled_down)
1658 srow += wp->w_lines[idx].wl_size;
1659 ++idx;
1660# ifdef FEAT_FOLDING
1661 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1662 lnum = wp->w_lines[idx].wl_lnum;
1663 else
1664# endif
1665 ++lnum;
1666 }
1667 srow += mid_start;
1668 mid_end = wp->w_height;
1669 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1670 {
1671 if (wp->w_lines[idx].wl_valid
1672 && wp->w_lines[idx].wl_lnum >= to + 1)
1673 {
1674 /* Only update until first row of this line */
1675 mid_end = srow;
1676 break;
1677 }
1678 srow += wp->w_lines[idx].wl_size;
1679 }
1680 }
1681 }
1682
1683 if (VIsual_active && buf == curwin->w_buffer)
1684 {
1685 wp->w_old_visual_mode = VIsual_mode;
1686 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1687 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001688 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 wp->w_old_curswant = curwin->w_curswant;
1690 }
1691 else
1692 {
1693 wp->w_old_visual_mode = 0;
1694 wp->w_old_cursor_lnum = 0;
1695 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001696 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1700 /* reset got_int, otherwise regexp won't work */
1701 save_got_int = got_int;
1702 got_int = 0;
1703#endif
1704#ifdef FEAT_FOLDING
1705 win_foldinfo.fi_level = 0;
1706#endif
1707
1708 /*
1709 * Update all the window rows.
1710 */
1711 idx = 0; /* first entry in w_lines[].wl_size */
1712 row = 0;
1713 srow = 0;
1714 lnum = wp->w_topline; /* first line shown in window */
1715 for (;;)
1716 {
1717 /* stop updating when reached the end of the window (check for _past_
1718 * the end of the window is at the end of the loop) */
1719 if (row == wp->w_height)
1720 {
1721 didline = TRUE;
1722 break;
1723 }
1724
1725 /* stop updating when hit the end of the file */
1726 if (lnum > buf->b_ml.ml_line_count)
1727 {
1728 eof = TRUE;
1729 break;
1730 }
1731
1732 /* Remember the starting row of the line that is going to be dealt
1733 * with. It is used further down when the line doesn't fit. */
1734 srow = row;
1735
1736 /*
1737 * Update a line when it is in an area that needs updating, when it
1738 * has changes or w_lines[idx] is invalid.
1739 * bot_start may be halfway a wrapped line after using
1740 * win_del_lines(), check if the current line includes it.
1741 * When syntax folding is being used, the saved syntax states will
1742 * already have been updated, we can't see where the syntax state is
1743 * the same again, just update until the end of the window.
1744 */
1745 if (row < top_end
1746 || (row >= mid_start && row < mid_end)
1747#ifdef FEAT_SEARCH_EXTRA
1748 || top_to_mod
1749#endif
1750 || idx >= wp->w_lines_valid
1751 || (row + wp->w_lines[idx].wl_size > bot_start)
1752 || (mod_top != 0
1753 && (lnum == mod_top
1754 || (lnum >= mod_top
1755 && (lnum < mod_bot
1756#ifdef FEAT_SYN_HL
1757 || did_update == DID_FOLD
1758 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001759 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 && (
1761# ifdef FEAT_FOLDING
1762 (foldmethodIsSyntax(wp)
1763 && hasAnyFolding(wp)) ||
1764# endif
1765 syntax_check_changed(lnum)))
1766#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001767#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001768 /* match in fixed position might need redraw
1769 * if lines were inserted or deleted */
1770 || (wp->w_match_head != NULL
1771 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 )))))
1774 {
1775#ifdef FEAT_SEARCH_EXTRA
1776 if (lnum == mod_top)
1777 top_to_mod = FALSE;
1778#endif
1779
1780 /*
1781 * When at start of changed lines: May scroll following lines
1782 * up or down to minimize redrawing.
1783 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001784 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 */
1786 if (lnum == mod_top
1787 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001788 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 int old_rows = 0;
1791 int new_rows = 0;
1792 int xtra_rows;
1793 linenr_T l;
1794
1795 /* Count the old number of window rows, using w_lines[], which
1796 * should still contain the sizes for the lines as they are
1797 * currently displayed. */
1798 for (i = idx; i < wp->w_lines_valid; ++i)
1799 {
1800 /* Only valid lines have a meaningful wl_lnum. Invalid
1801 * lines are part of the changed area. */
1802 if (wp->w_lines[i].wl_valid
1803 && wp->w_lines[i].wl_lnum == mod_bot)
1804 break;
1805 old_rows += wp->w_lines[i].wl_size;
1806#ifdef FEAT_FOLDING
1807 if (wp->w_lines[i].wl_valid
1808 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1809 {
1810 /* Must have found the last valid entry above mod_bot.
1811 * Add following invalid entries. */
1812 ++i;
1813 while (i < wp->w_lines_valid
1814 && !wp->w_lines[i].wl_valid)
1815 old_rows += wp->w_lines[i++].wl_size;
1816 break;
1817 }
1818#endif
1819 }
1820
1821 if (i >= wp->w_lines_valid)
1822 {
1823 /* We can't find a valid line below the changed lines,
1824 * need to redraw until the end of the window.
1825 * Inserting/deleting lines has no use. */
1826 bot_start = 0;
1827 }
1828 else
1829 {
1830 /* Able to count old number of rows: Count new window
1831 * rows, and may insert/delete lines */
1832 j = idx;
1833 for (l = lnum; l < mod_bot; ++l)
1834 {
1835#ifdef FEAT_FOLDING
1836 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1837 ++new_rows;
1838 else
1839#endif
1840#ifdef FEAT_DIFF
1841 if (l == wp->w_topline)
1842 new_rows += plines_win_nofill(wp, l, TRUE)
1843 + wp->w_topfill;
1844 else
1845#endif
1846 new_rows += plines_win(wp, l, TRUE);
1847 ++j;
1848 if (new_rows > wp->w_height - row - 2)
1849 {
1850 /* it's getting too much, must redraw the rest */
1851 new_rows = 9999;
1852 break;
1853 }
1854 }
1855 xtra_rows = new_rows - old_rows;
1856 if (xtra_rows < 0)
1857 {
1858 /* May scroll text up. If there is not enough
1859 * remaining text or scrolling fails, must redraw the
1860 * rest. If scrolling works, must redraw the text
1861 * below the scrolled text. */
1862 if (row - xtra_rows >= wp->w_height - 2)
1863 mod_bot = MAXLNUM;
1864 else
1865 {
1866 check_for_delay(FALSE);
1867 if (win_del_lines(wp, row,
1868 -xtra_rows, FALSE, FALSE) == FAIL)
1869 mod_bot = MAXLNUM;
1870 else
1871 bot_start = wp->w_height + xtra_rows;
1872 }
1873 }
1874 else if (xtra_rows > 0)
1875 {
1876 /* May scroll text down. If there is not enough
1877 * remaining text of scrolling fails, must redraw the
1878 * rest. */
1879 if (row + xtra_rows >= wp->w_height - 2)
1880 mod_bot = MAXLNUM;
1881 else
1882 {
1883 check_for_delay(FALSE);
1884 if (win_ins_lines(wp, row + old_rows,
1885 xtra_rows, FALSE, FALSE) == FAIL)
1886 mod_bot = MAXLNUM;
1887 else if (top_end > row + old_rows)
1888 /* Scrolled the part at the top that requires
1889 * updating down. */
1890 top_end += xtra_rows;
1891 }
1892 }
1893
1894 /* When not updating the rest, may need to move w_lines[]
1895 * entries. */
1896 if (mod_bot != MAXLNUM && i != j)
1897 {
1898 if (j < i)
1899 {
1900 int x = row + new_rows;
1901
1902 /* move entries in w_lines[] upwards */
1903 for (;;)
1904 {
1905 /* stop at last valid entry in w_lines[] */
1906 if (i >= wp->w_lines_valid)
1907 {
1908 wp->w_lines_valid = j;
1909 break;
1910 }
1911 wp->w_lines[j] = wp->w_lines[i];
1912 /* stop at a line that won't fit */
1913 if (x + (int)wp->w_lines[j].wl_size
1914 > wp->w_height)
1915 {
1916 wp->w_lines_valid = j + 1;
1917 break;
1918 }
1919 x += wp->w_lines[j++].wl_size;
1920 ++i;
1921 }
1922 if (bot_start > x)
1923 bot_start = x;
1924 }
1925 else /* j > i */
1926 {
1927 /* move entries in w_lines[] downwards */
1928 j -= i;
1929 wp->w_lines_valid += j;
1930 if (wp->w_lines_valid > wp->w_height)
1931 wp->w_lines_valid = wp->w_height;
1932 for (i = wp->w_lines_valid; i - j >= idx; --i)
1933 wp->w_lines[i] = wp->w_lines[i - j];
1934
1935 /* The w_lines[] entries for inserted lines are
1936 * now invalid, but wl_size may be used above.
1937 * Reset to zero. */
1938 while (i >= idx)
1939 {
1940 wp->w_lines[i].wl_size = 0;
1941 wp->w_lines[i--].wl_valid = FALSE;
1942 }
1943 }
1944 }
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947
1948#ifdef FEAT_FOLDING
1949 /*
1950 * When lines are folded, display one line for all of them.
1951 * Otherwise, display normally (can be several display lines when
1952 * 'wrap' is on).
1953 */
1954 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1955 if (fold_count != 0)
1956 {
1957 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1958 ++row;
1959 --fold_count;
1960 wp->w_lines[idx].wl_folded = TRUE;
1961 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1962# ifdef FEAT_SYN_HL
1963 did_update = DID_FOLD;
1964# endif
1965 }
1966 else
1967#endif
1968 if (idx < wp->w_lines_valid
1969 && wp->w_lines[idx].wl_valid
1970 && wp->w_lines[idx].wl_lnum == lnum
1971 && lnum > wp->w_topline
1972 && !(dy_flags & DY_LASTLINE)
1973 && srow + wp->w_lines[idx].wl_size > wp->w_height
1974#ifdef FEAT_DIFF
1975 && diff_check_fill(wp, lnum) == 0
1976#endif
1977 )
1978 {
1979 /* This line is not going to fit. Don't draw anything here,
1980 * will draw "@ " lines below. */
1981 row = wp->w_height + 1;
1982 }
1983 else
1984 {
1985#ifdef FEAT_SEARCH_EXTRA
1986 prepare_search_hl(wp, lnum);
1987#endif
1988#ifdef FEAT_SYN_HL
1989 /* Let the syntax stuff know we skipped a few lines. */
1990 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001991 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 syntax_end_parsing(syntax_last_parsed + 1);
1993#endif
1994
1995 /*
1996 * Display one line.
1997 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001998 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000#ifdef FEAT_FOLDING
2001 wp->w_lines[idx].wl_folded = FALSE;
2002 wp->w_lines[idx].wl_lastlnum = lnum;
2003#endif
2004#ifdef FEAT_SYN_HL
2005 did_update = DID_LINE;
2006 syntax_last_parsed = lnum;
2007#endif
2008 }
2009
2010 wp->w_lines[idx].wl_lnum = lnum;
2011 wp->w_lines[idx].wl_valid = TRUE;
2012 if (row > wp->w_height) /* past end of screen */
2013 {
2014 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002015 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2017 ++idx;
2018 break;
2019 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002020 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 wp->w_lines[idx].wl_size = row - srow;
2022 ++idx;
2023#ifdef FEAT_FOLDING
2024 lnum += fold_count + 1;
2025#else
2026 ++lnum;
2027#endif
2028 }
2029 else
2030 {
2031 /* This line does not need updating, advance to the next one */
2032 row += wp->w_lines[idx++].wl_size;
2033 if (row > wp->w_height) /* past end of screen */
2034 break;
2035#ifdef FEAT_FOLDING
2036 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2037#else
2038 ++lnum;
2039#endif
2040#ifdef FEAT_SYN_HL
2041 did_update = DID_NONE;
2042#endif
2043 }
2044
2045 if (lnum > buf->b_ml.ml_line_count)
2046 {
2047 eof = TRUE;
2048 break;
2049 }
2050 }
2051 /*
2052 * End of loop over all window lines.
2053 */
2054
2055
2056 if (idx > wp->w_lines_valid)
2057 wp->w_lines_valid = idx;
2058
2059#ifdef FEAT_SYN_HL
2060 /*
2061 * Let the syntax stuff know we stop parsing here.
2062 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002063 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 syntax_end_parsing(syntax_last_parsed + 1);
2065#endif
2066
2067 /*
2068 * If we didn't hit the end of the file, and we didn't finish the last
2069 * line we were working on, then the line didn't fit.
2070 */
2071 wp->w_empty_rows = 0;
2072#ifdef FEAT_DIFF
2073 wp->w_filler_rows = 0;
2074#endif
2075 if (!eof && !didline)
2076 {
2077 if (lnum == wp->w_topline)
2078 {
2079 /*
2080 * Single line that does not fit!
2081 * Don't overwrite it, it can be edited.
2082 */
2083 wp->w_botline = lnum + 1;
2084 }
2085#ifdef FEAT_DIFF
2086 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2087 {
2088 /* Window ends in filler lines. */
2089 wp->w_botline = lnum;
2090 wp->w_filler_rows = wp->w_height - srow;
2091 }
2092#endif
2093 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2094 {
2095 /*
2096 * Last line isn't finished: Display "@@@" at the end.
2097 */
2098 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2099 W_WINROW(wp) + wp->w_height,
2100 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2101 '@', '@', hl_attr(HLF_AT));
2102 set_empty_rows(wp, srow);
2103 wp->w_botline = lnum;
2104 }
2105 else
2106 {
2107 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2108 wp->w_botline = lnum;
2109 }
2110 }
2111 else
2112 {
2113#ifdef FEAT_VERTSPLIT
2114 draw_vsep_win(wp, row);
2115#endif
2116 if (eof) /* we hit the end of the file */
2117 {
2118 wp->w_botline = buf->b_ml.ml_line_count + 1;
2119#ifdef FEAT_DIFF
2120 j = diff_check_fill(wp, wp->w_botline);
2121 if (j > 0 && !wp->w_botfill)
2122 {
2123 /*
2124 * Display filler lines at the end of the file
2125 */
2126 if (char2cells(fill_diff) > 1)
2127 i = '-';
2128 else
2129 i = fill_diff;
2130 if (row + j > wp->w_height)
2131 j = wp->w_height - row;
2132 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2133 row += j;
2134 }
2135#endif
2136 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002137 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 wp->w_botline = lnum;
2139
2140 /* make sure the rest of the screen is blank */
2141 /* put '~'s on rows that aren't part of the file. */
2142 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2143 }
2144
2145 /* Reset the type of redrawing required, the window has been updated. */
2146 wp->w_redr_type = 0;
2147#ifdef FEAT_DIFF
2148 wp->w_old_topfill = wp->w_topfill;
2149 wp->w_old_botfill = wp->w_botfill;
2150#endif
2151
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 /*
2155 * There is a trick with w_botline. If we invalidate it on each
2156 * change that might modify it, this will cause a lot of expensive
2157 * calls to plines() in update_topline() each time. Therefore the
2158 * value of w_botline is often approximated, and this value is used to
2159 * compute the value of w_topline. If the value of w_botline was
2160 * wrong, check that the value of w_topline is correct (cursor is on
2161 * the visible part of the text). If it's not, we need to redraw
2162 * again. Mostly this just means scrolling up a few lines, so it
2163 * doesn't look too bad. Only do this for the current window (where
2164 * changes are relevant).
2165 */
2166 wp->w_valid |= VALID_BOTLINE;
2167 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2168 {
2169 recursive = TRUE;
2170 curwin->w_valid &= ~VALID_TOPLINE;
2171 update_topline(); /* may invalidate w_botline again */
2172 if (must_redraw != 0)
2173 {
2174 /* Don't update for changes in buffer again. */
2175 i = curbuf->b_mod_set;
2176 curbuf->b_mod_set = FALSE;
2177 win_update(curwin);
2178 must_redraw = 0;
2179 curbuf->b_mod_set = i;
2180 }
2181 recursive = FALSE;
2182 }
2183 }
2184
2185#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2186 /* restore got_int, unless CTRL-C was hit while redrawing */
2187 if (!got_int)
2188 got_int = save_got_int;
2189#endif
2190}
2191
2192#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002193static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195/*
2196 * Return TRUE when window "wp" has a column to draw signs in.
2197 */
2198 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002199draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 return (wp->w_buffer->b_signlist != NULL
2202# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002203 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204# endif
2205 );
2206}
2207#endif
2208
2209/*
2210 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2211 * as the filler character.
2212 */
2213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002214win_draw_end(
2215 win_T *wp,
2216 int c1,
2217 int c2,
2218 int row,
2219 int endrow,
2220 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2223 int n = 0;
2224# define FDC_OFF n
2225#else
2226# define FDC_OFF 0
2227#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002228#ifdef FEAT_FOLDING
2229 int fdc = compute_foldcolumn(wp, 0);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232#ifdef FEAT_RIGHTLEFT
2233 if (wp->w_p_rl)
2234 {
2235 /* No check for cmdline window: should never be right-left. */
2236# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002237 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 if (n > 0)
2240 {
2241 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002242 if (n > W_WIDTH(wp))
2243 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2245 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2246 ' ', ' ', hl_attr(HLF_FC));
2247 }
2248# endif
2249# ifdef FEAT_SIGNS
2250 if (draw_signcolumn(wp))
2251 {
2252 int nn = n + 2;
2253
2254 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002255 if (nn > W_WIDTH(wp))
2256 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2258 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2259 ' ', ' ', hl_attr(HLF_SC));
2260 n = nn;
2261 }
2262# endif
2263 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2264 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2265 c2, c2, hl_attr(hl));
2266 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2267 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2268 c1, c2, hl_attr(hl));
2269 }
2270 else
2271#endif
2272 {
2273#ifdef FEAT_CMDWIN
2274 if (cmdwin_type != 0 && wp == curwin)
2275 {
2276 /* draw the cmdline character in the leftmost column */
2277 n = 1;
2278 if (n > wp->w_width)
2279 n = wp->w_width;
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2282 cmdwin_type, ' ', hl_attr(HLF_AT));
2283 }
2284#endif
2285#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002286 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002288 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 /* draw the fold column at the left */
2291 if (nn > W_WIDTH(wp))
2292 nn = W_WIDTH(wp);
2293 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2294 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2295 ' ', ' ', hl_attr(HLF_FC));
2296 n = nn;
2297 }
2298#endif
2299#ifdef FEAT_SIGNS
2300 if (draw_signcolumn(wp))
2301 {
2302 int nn = n + 2;
2303
2304 /* draw the sign column after the fold column */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_SC));
2310 n = nn;
2311 }
2312#endif
2313 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2314 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2315 c1, c2, hl_attr(hl));
2316 }
2317 set_empty_rows(wp, row);
2318}
2319
Bram Moolenaar1a384422010-07-14 19:53:30 +02002320#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002321static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002322
2323/*
2324 * Advance **color_cols and return TRUE when there are columns to draw.
2325 */
2326 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002327advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002328{
2329 while (**color_cols >= 0 && vcol > **color_cols)
2330 ++*color_cols;
2331 return (**color_cols >= 0);
2332}
2333#endif
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#ifdef FEAT_FOLDING
2336/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002337 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2338 * space is available for window "wp", minus "col".
2339 */
2340 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002341compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002342{
2343 int fdc = wp->w_p_fdc;
2344 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2345 int wwidth = W_WIDTH(wp);
2346
2347 if (fdc > wwidth - (col + wmw))
2348 fdc = wwidth - (col + wmw);
2349 return fdc;
2350}
2351
2352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 * Display one folded line.
2354 */
2355 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002356fold_line(
2357 win_T *wp,
2358 long fold_count,
2359 foldinfo_T *foldinfo,
2360 linenr_T lnum,
2361 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 char_u buf[51];
2364 pos_T *top, *bot;
2365 linenr_T lnume = lnum + fold_count - 1;
2366 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002367 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 int col;
2370 int txtcol;
2371 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002372 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 /* Build the fold line:
2375 * 1. Add the cmdwin_type for the command-line window
2376 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002377 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * 4. Compose the text
2379 * 5. Add the text
2380 * 6. set highlighting for the Visual area an other text
2381 */
2382 col = 0;
2383
2384 /*
2385 * 1. Add the cmdwin_type for the command-line window
2386 * Ignores 'rightleft', this window is never right-left.
2387 */
2388#ifdef FEAT_CMDWIN
2389 if (cmdwin_type != 0 && wp == curwin)
2390 {
2391 ScreenLines[off] = cmdwin_type;
2392 ScreenAttrs[off] = hl_attr(HLF_AT);
2393#ifdef FEAT_MBYTE
2394 if (enc_utf8)
2395 ScreenLinesUC[off] = 0;
2396#endif
2397 ++col;
2398 }
2399#endif
2400
2401 /*
2402 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002403 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002405 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (fdc > 0)
2407 {
2408 fill_foldcolumn(buf, wp, TRUE, lnum);
2409#ifdef FEAT_RIGHTLEFT
2410 if (wp->w_p_rl)
2411 {
2412 int i;
2413
2414 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2415 hl_attr(HLF_FC));
2416 /* reverse the fold column */
2417 for (i = 0; i < fdc; ++i)
2418 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2419 }
2420 else
2421#endif
2422 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2423 col += fdc;
2424 }
2425
2426#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2428 for (ri = 0; ri < l; ++ri) \
2429 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2430 else \
2431 for (ri = 0; ri < l; ++ri) \
2432 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002434# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2435 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437
Bram Moolenaar64486672010-05-16 15:46:46 +02002438 /* Set all attributes of the 'number' or 'relativenumber' column and the
2439 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002440 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442#ifdef FEAT_SIGNS
2443 /* If signs are being displayed, add two spaces. */
2444 if (draw_signcolumn(wp))
2445 {
2446 len = W_WIDTH(wp) - col;
2447 if (len > 0)
2448 {
2449 if (len > 2)
2450 len = 2;
2451# ifdef FEAT_RIGHTLEFT
2452 if (wp->w_p_rl)
2453 /* the line number isn't reversed */
2454 copy_text_attr(off + W_WIDTH(wp) - len - col,
2455 (char_u *)" ", len, hl_attr(HLF_FL));
2456 else
2457# endif
2458 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2459 col += len;
2460 }
2461 }
2462#endif
2463
2464 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002465 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002467 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 len = W_WIDTH(wp) - col;
2470 if (len > 0)
2471 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002472 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002473 long num;
2474 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002475
2476 if (len > w + 1)
2477 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002478
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002479 if (wp->w_p_nu && !wp->w_p_rnu)
2480 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002481 num = (long)lnum;
2482 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002483 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002484 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002485 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002486 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002487 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002488 /* 'number' + 'relativenumber': cursor line shows absolute
2489 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002490 num = lnum;
2491 fmt = "%-*ld ";
2492 }
2493 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002494
Bram Moolenaar700e7342013-01-30 12:31:36 +01002495 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#ifdef FEAT_RIGHTLEFT
2497 if (wp->w_p_rl)
2498 /* the line number isn't reversed */
2499 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2500 hl_attr(HLF_FL));
2501 else
2502#endif
2503 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2504 col += len;
2505 }
2506 }
2507
2508 /*
2509 * 4. Compose the folded-line string with 'foldtext', if set.
2510 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002511 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 txtcol = col; /* remember where text starts */
2514
2515 /*
2516 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2517 * Right-left text is put in columns 0 - number-col, normal text is put
2518 * in columns number-col - window-width.
2519 */
2520#ifdef FEAT_MBYTE
2521 if (has_mbyte)
2522 {
2523 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002524 int u8c, u8cc[MAX_MCO];
2525 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 int idx;
2527 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002528 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529# ifdef FEAT_ARABIC
2530 int prev_c = 0; /* previous Arabic character */
2531 int prev_c1 = 0; /* first composing char for prev_c */
2532# endif
2533
2534# ifdef FEAT_RIGHTLEFT
2535 if (wp->w_p_rl)
2536 idx = off;
2537 else
2538# endif
2539 idx = off + col;
2540
2541 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2542 for (p = text; *p != NUL; )
2543 {
2544 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002545 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (col + cells > W_WIDTH(wp)
2547# ifdef FEAT_RIGHTLEFT
2548 - (wp->w_p_rl ? col : 0)
2549# endif
2550 )
2551 break;
2552 ScreenLines[idx] = *p;
2553 if (enc_utf8)
2554 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002555 u8c = utfc_ptr2char(p, u8cc);
2556 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 ScreenLinesUC[idx] = 0;
2559#ifdef FEAT_ARABIC
2560 prev_c = u8c;
2561#endif
2562 }
2563 else
2564 {
2565#ifdef FEAT_ARABIC
2566 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2567 {
2568 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002569 int pc, pc1, nc;
2570 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 int firstbyte = *p;
2572
2573 /* The idea of what is the previous and next
2574 * character depends on 'rightleft'. */
2575 if (wp->w_p_rl)
2576 {
2577 pc = prev_c;
2578 pc1 = prev_c1;
2579 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002580 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 else
2583 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002586 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 prev_c = u8c;
2589
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 pc, pc1, nc);
2592 ScreenLines[idx] = firstbyte;
2593 }
2594 else
2595 prev_c = u8c;
2596#endif
2597 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002598#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (u8c >= 0x10000)
2600 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2601 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 for (i = 0; i < Screen_mco; ++i)
2605 {
2606 ScreenLinesC[i][idx] = u8cc[i];
2607 if (u8cc[i] == 0)
2608 break;
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611 if (cells > 1)
2612 ScreenLines[idx + 1] = 0;
2613 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002614 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2615 /* double-byte single width character */
2616 ScreenLines2[idx] = p[1];
2617 else if (cells > 1)
2618 /* double-width character */
2619 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 col += cells;
2621 idx += cells;
2622 p += c_len;
2623 }
2624 }
2625 else
2626#endif
2627 {
2628 len = (int)STRLEN(text);
2629 if (len > W_WIDTH(wp) - col)
2630 len = W_WIDTH(wp) - col;
2631 if (len > 0)
2632 {
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 STRNCPY(current_ScreenLine, text, len);
2636 else
2637#endif
2638 STRNCPY(current_ScreenLine + col, text, len);
2639 col += len;
2640 }
2641 }
2642
2643 /* Fill the rest of the line with the fold filler */
2644#ifdef FEAT_RIGHTLEFT
2645 if (wp->w_p_rl)
2646 col -= txtcol;
2647#endif
2648 while (col < W_WIDTH(wp)
2649#ifdef FEAT_RIGHTLEFT
2650 - (wp->w_p_rl ? txtcol : 0)
2651#endif
2652 )
2653 {
2654#ifdef FEAT_MBYTE
2655 if (enc_utf8)
2656 {
2657 if (fill_fold >= 0x80)
2658 {
2659 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662 else
2663 ScreenLinesUC[off + col] = 0;
2664 }
2665#endif
2666 ScreenLines[off + col++] = fill_fold;
2667 }
2668
2669 if (text != buf)
2670 vim_free(text);
2671
2672 /*
2673 * 6. set highlighting for the Visual area an other text.
2674 * If all folded lines are in the Visual area, highlight the line.
2675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2677 {
2678 if (ltoreq(curwin->w_cursor, VIsual))
2679 {
2680 /* Visual is after curwin->w_cursor */
2681 top = &curwin->w_cursor;
2682 bot = &VIsual;
2683 }
2684 else
2685 {
2686 /* Visual is before curwin->w_cursor */
2687 top = &VIsual;
2688 bot = &curwin->w_cursor;
2689 }
2690 if (lnum >= top->lnum
2691 && lnume <= bot->lnum
2692 && (VIsual_mode != 'v'
2693 || ((lnum > top->lnum
2694 || (lnum == top->lnum
2695 && top->col == 0))
2696 && (lnume < bot->lnum
2697 || (lnume == bot->lnum
2698 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002699 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 if (VIsual_mode == Ctrl_V)
2702 {
2703 /* Visual block mode: highlight the chars part of the block */
2704 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2705 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002706 if (wp->w_old_cursor_lcol != MAXCOL
2707 && wp->w_old_cursor_lcol + txtcol
2708 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 len = wp->w_old_cursor_lcol;
2710 else
2711 len = W_WIDTH(wp) - txtcol;
2712 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002713 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715 }
2716 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002719 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002725 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2726 if (wp->w_p_cc_cols)
2727 {
2728 int i = 0;
2729 int j = wp->w_p_cc_cols[i];
2730 int old_txtcol = txtcol;
2731
2732 while (j > -1)
2733 {
2734 txtcol += j;
2735 if (wp->w_p_wrap)
2736 txtcol -= wp->w_skipcol;
2737 else
2738 txtcol -= wp->w_leftcol;
2739 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2740 ScreenAttrs[off + txtcol] = hl_combine_attr(
2741 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2742 txtcol = old_txtcol;
2743 j = wp->w_p_cc_cols[++i];
2744 }
2745 }
2746
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002747 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002748 if (wp->w_p_cuc)
2749 {
2750 txtcol += wp->w_virtcol;
2751 if (wp->w_p_wrap)
2752 txtcol -= wp->w_skipcol;
2753 else
2754 txtcol -= wp->w_leftcol;
2755 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2756 ScreenAttrs[off + txtcol] = hl_combine_attr(
2757 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2758 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2762 (int)W_WIDTH(wp), FALSE);
2763
2764 /*
2765 * Update w_cline_height and w_cline_folded if the cursor line was
2766 * updated (saves a call to plines() later).
2767 */
2768 if (wp == curwin
2769 && lnum <= curwin->w_cursor.lnum
2770 && lnume >= curwin->w_cursor.lnum)
2771 {
2772 curwin->w_cline_row = row;
2773 curwin->w_cline_height = 1;
2774 curwin->w_cline_folded = TRUE;
2775 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2776 }
2777}
2778
2779/*
2780 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2781 */
2782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783copy_text_attr(
2784 int off,
2785 char_u *buf,
2786 int len,
2787 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002789 int i;
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 mch_memmove(ScreenLines + off, buf, (size_t)len);
2792# ifdef FEAT_MBYTE
2793 if (enc_utf8)
2794 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2795# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002796 for (i = 0; i < len; ++i)
2797 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798}
2799
2800/*
2801 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002802 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002805fill_foldcolumn(
2806 char_u *p,
2807 win_T *wp,
2808 int closed, /* TRUE of FALSE */
2809 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 int i = 0;
2812 int level;
2813 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002814 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002815 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002818 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 level = win_foldinfo.fi_level;
2821 if (level > 0)
2822 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002823 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002824 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 /* If the column is too narrow, we start at the lowest level that
2827 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002828 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (first_level < 1)
2830 first_level = 1;
2831
Bram Moolenaar1c934292015-01-27 16:39:29 +01002832 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 if (win_foldinfo.fi_lnum == lnum
2835 && first_level + i >= win_foldinfo.fi_low_level)
2836 p[i] = '-';
2837 else if (first_level == 1)
2838 p[i] = '|';
2839 else if (first_level + i <= 9)
2840 p[i] = '0' + first_level + i;
2841 else
2842 p[i] = '>';
2843 if (first_level + i == level)
2844 break;
2845 }
2846 }
2847 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002848 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850#endif /* FEAT_FOLDING */
2851
2852/*
2853 * Display line "lnum" of window 'wp' on the screen.
2854 * Start at row "startrow", stop when "endrow" is reached.
2855 * wp->w_virtcol needs to be valid.
2856 *
2857 * Return the number of last row the line occupies.
2858 */
2859 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002860win_line(
2861 win_T *wp,
2862 linenr_T lnum,
2863 int startrow,
2864 int endrow,
2865 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 int col; /* visual column on screen */
2868 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2869 int c = 0; /* init for GCC */
2870 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002871#ifdef FEAT_LINEBREAK
2872 long vcol_sbr = -1; /* virtual column after showbreak */
2873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 long vcol_prev = -1; /* "vcol" of previous character */
2875 char_u *line; /* current line */
2876 char_u *ptr; /* current position in "line" */
2877 int row; /* row in the window, excl w_winrow */
2878 int screen_row; /* row on the screen, incl w_winrow */
2879
2880 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2881 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002882 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002883 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 int c_extra = NUL; /* extra chars, all the same */
2885 int extra_attr = 0; /* attributes when n_extra != 0 */
2886 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2887 displaying lcs_eol at end-of-line */
2888 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2889 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2890
2891 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2892 int saved_n_extra = 0;
2893 char_u *saved_p_extra = NULL;
2894 int saved_c_extra = 0;
2895 int saved_char_attr = 0;
2896
2897 int n_attr = 0; /* chars with special attr */
2898 int saved_attr2 = 0; /* char_attr saved for n_attr */
2899 int n_attr3 = 0; /* chars with overruling special attr */
2900 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2901
2902 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2903
2904 int fromcol, tocol; /* start/end of inverting */
2905 int fromcol_prev = -2; /* start of inverting after cursor */
2906 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002908 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 pos_T pos;
2910 long v;
2911
2912 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002913 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2915 in this line */
2916 int attr = 0; /* attributes for area highlighting */
2917 int area_attr = 0; /* attributes desired by highlighting */
2918 int search_attr = 0; /* attributes desired by 'hlsearch' */
2919#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 int syntax_attr = 0; /* attributes desired by syntax */
2922 int has_syntax = FALSE; /* this buffer has syntax highl. */
2923 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002924 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002925 int draw_color_col = FALSE; /* highlight colorcolumn */
2926 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002927#endif
2928#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002929 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002930# define SPWORDLEN 150
2931 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002932 int nextlinecol = 0; /* column where nextline[] starts */
2933 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002934 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002935 int spell_attr = 0; /* attributes desired by spelling */
2936 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002937 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2938 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002939 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002940 static int cap_col = -1; /* column to check for Cap word */
2941 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002942 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943#endif
2944 int extra_check; /* has syntax or linebreak */
2945#ifdef FEAT_MBYTE
2946 int multi_attr = 0; /* attributes desired by multibyte */
2947 int mb_l = 1; /* multi-byte byte length */
2948 int mb_c = 0; /* decoded multi-byte character */
2949 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002950 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#endif
2952#ifdef FEAT_DIFF
2953 int filler_lines; /* nr of filler lines to be drawn */
2954 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002955 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int change_start = MAXCOL; /* first col of changed area */
2957 int change_end = -1; /* last col of changed area */
2958#endif
2959 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2960#ifdef FEAT_LINEBREAK
2961 int need_showbreak = FALSE;
2962#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002963#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2964 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002966 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#endif
2968#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002969 matchitem_T *cur; /* points to the match list */
2970 match_T *shl; /* points to search_hl or a match */
2971 int shl_flag; /* flag to indicate whether search_hl
2972 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002973 int pos_inprogress; /* marks that position match search is
2974 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002975 int prevcol_hl_flag; /* flag to indicate whether prevcol
2976 equals startcol of search_hl or one
2977 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#endif
2979#ifdef FEAT_ARABIC
2980 int prev_c = 0; /* previous Arabic character */
2981 int prev_c1 = 0; /* first composing char for prev_c */
2982#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002983#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002984 int did_line_attr = 0;
2985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /* draw_state: items that are drawn in sequence: */
2988#define WL_START 0 /* nothing done yet */
2989#ifdef FEAT_CMDWIN
2990# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2991#else
2992# define WL_CMDLINE WL_START
2993#endif
2994#ifdef FEAT_FOLDING
2995# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2996#else
2997# define WL_FOLD WL_CMDLINE
2998#endif
2999#ifdef FEAT_SIGNS
3000# define WL_SIGN WL_FOLD + 1 /* column for signs */
3001#else
3002# define WL_SIGN WL_FOLD /* column for signs */
3003#endif
3004#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003005#ifdef FEAT_LINEBREAK
3006# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003008# define WL_BRI WL_NR
3009#endif
3010#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3011# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3012#else
3013# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#endif
3015#define WL_LINE WL_SBR + 1 /* text in the line */
3016 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003017#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 int feedback_col = 0;
3019 int feedback_old_attr = -1;
3020#endif
3021
Bram Moolenaar860cae12010-06-05 23:22:07 +02003022#ifdef FEAT_CONCEAL
3023 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003024 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003025 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003026 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003027 int is_concealing = FALSE;
3028 int boguscols = 0; /* nonexistent columns added to force
3029 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003030 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003031 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003032 int match_conc = FALSE; /* cchar for match functions */
3033 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003034 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003035# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003036# define FIX_FOR_BOGUSCOLS \
3037 { \
3038 n_extra += vcol_off; \
3039 vcol -= vcol_off; \
3040 vcol_off = 0; \
3041 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003042 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003043 boguscols = 0; \
3044 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003045#else
3046# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049 if (startrow > endrow) /* past the end already! */
3050 return startrow;
3051
3052 row = startrow;
3053 screen_row = row + W_WINROW(wp);
3054
3055 /*
3056 * To speed up the loop below, set extra_check when there is linebreak,
3057 * trailing white space and/or syntax processing to be done.
3058 */
3059#ifdef FEAT_LINEBREAK
3060 extra_check = wp->w_p_lbr;
3061#else
3062 extra_check = 0;
3063#endif
3064#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003065 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
3067 /* Prepare for syntax highlighting in this line. When there is an
3068 * error, stop syntax highlighting. */
3069 save_did_emsg = did_emsg;
3070 did_emsg = FALSE;
3071 syntax_start(wp, lnum);
3072 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003073 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 {
3076 did_emsg = save_did_emsg;
3077 has_syntax = TRUE;
3078 extra_check = TRUE;
3079 }
3080 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003081
3082 /* Check for columns to display for 'colorcolumn'. */
3083 color_cols = wp->w_p_cc_cols;
3084 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003085 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003086#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003087
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003088#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003089 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003090 && *wp->w_s->b_p_spl != NUL
3091 && wp->w_s->b_langp.ga_len > 0
3092 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003093 {
3094 /* Prepare for spell checking. */
3095 has_spell = TRUE;
3096 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097
3098 /* Get the start of the next line, so that words that wrap to the next
3099 * line are found too: "et<line-break>al.".
3100 * Trick: skip a few chars for C/shell/Vim comments */
3101 nextline[SPWORDLEN] = NUL;
3102 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3103 {
3104 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3105 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3106 }
3107
3108 /* When a word wrapped from the previous line the start of the current
3109 * line is valid. */
3110 if (lnum == checked_lnum)
3111 cur_checked_col = checked_col;
3112 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003113
3114 /* When there was a sentence end in the previous line may require a
3115 * word starting with capital in this line. In line 1 always check
3116 * the first word. */
3117 if (lnum != capcol_lnum)
3118 cap_col = -1;
3119 if (lnum == 1)
3120 cap_col = 0;
3121 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124
3125 /*
3126 * handle visual active in this window
3127 */
3128 fromcol = -10;
3129 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3131 {
3132 /* Visual is after curwin->w_cursor */
3133 if (ltoreq(curwin->w_cursor, VIsual))
3134 {
3135 top = &curwin->w_cursor;
3136 bot = &VIsual;
3137 }
3138 else /* Visual is before curwin->w_cursor */
3139 {
3140 top = &VIsual;
3141 bot = &curwin->w_cursor;
3142 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003143 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (VIsual_mode == Ctrl_V) /* block mode */
3145 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003146 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 fromcol = wp->w_old_cursor_fcol;
3149 tocol = wp->w_old_cursor_lcol;
3150 }
3151 }
3152 else /* non-block mode */
3153 {
3154 if (lnum > top->lnum && lnum <= bot->lnum)
3155 fromcol = 0;
3156 else if (lnum == top->lnum)
3157 {
3158 if (VIsual_mode == 'V') /* linewise */
3159 fromcol = 0;
3160 else
3161 {
3162 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3163 if (gchar_pos(top) == NUL)
3164 tocol = fromcol + 1;
3165 }
3166 }
3167 if (VIsual_mode != 'V' && lnum == bot->lnum)
3168 {
3169 if (*p_sel == 'e' && bot->col == 0
3170#ifdef FEAT_VIRTUALEDIT
3171 && bot->coladd == 0
3172#endif
3173 )
3174 {
3175 fromcol = -10;
3176 tocol = MAXCOL;
3177 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003178 else if (bot->col == MAXCOL)
3179 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181 {
3182 pos = *bot;
3183 if (*p_sel == 'e')
3184 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3185 else
3186 {
3187 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3188 ++tocol;
3189 }
3190 }
3191 }
3192 }
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /* Check if the character under the cursor should not be inverted */
3195 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003196#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 )
3200 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 /* if inverting in this line set area_highlighting */
3203 if (fromcol >= 0)
3204 {
3205 area_highlighting = TRUE;
3206 attr = hl_attr(HLF_V);
3207#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003208 if ((clip_star.available && !clip_star.owned
3209 && clip_isautosel_star())
3210 || (clip_plus.available && !clip_plus.owned
3211 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 attr = hl_attr(HLF_VNC);
3213#endif
3214 }
3215 }
3216
3217 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003218 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003220 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 && wp == curwin
3222 && lnum >= curwin->w_cursor.lnum
3223 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3224 {
3225 if (lnum == curwin->w_cursor.lnum)
3226 getvcol(curwin, &(curwin->w_cursor),
3227 (colnr_T *)&fromcol, NULL, NULL);
3228 else
3229 fromcol = 0;
3230 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3231 {
3232 pos.lnum = lnum;
3233 pos.col = search_match_endcol;
3234 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3235 }
3236 else
3237 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003238 /* do at least one character; happens when past end of line */
3239 if (fromcol == tocol)
3240 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 area_highlighting = TRUE;
3242 attr = hl_attr(HLF_I);
3243 }
3244
3245#ifdef FEAT_DIFF
3246 filler_lines = diff_check(wp, lnum);
3247 if (filler_lines < 0)
3248 {
3249 if (filler_lines == -1)
3250 {
3251 if (diff_find_change(wp, lnum, &change_start, &change_end))
3252 diff_hlf = HLF_ADD; /* added line */
3253 else if (change_start == 0)
3254 diff_hlf = HLF_TXD; /* changed text */
3255 else
3256 diff_hlf = HLF_CHD; /* changed line */
3257 }
3258 else
3259 diff_hlf = HLF_ADD; /* added line */
3260 filler_lines = 0;
3261 area_highlighting = TRUE;
3262 }
3263 if (lnum == wp->w_topline)
3264 filler_lines = wp->w_topfill;
3265 filler_todo = filler_lines;
3266#endif
3267
3268#ifdef LINE_ATTR
3269# ifdef FEAT_SIGNS
3270 /* If this line has a sign with line highlighting set line_attr. */
3271 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3272 if (v != 0)
3273 line_attr = sign_get_attr((int)v, TRUE);
3274# endif
3275# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3276 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003277 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 line_attr = hl_attr(HLF_L);
3279# endif
3280 if (line_attr != 0)
3281 area_highlighting = TRUE;
3282#endif
3283
3284 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3285 ptr = line;
3286
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003287#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003288 if (has_spell)
3289 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003290 /* For checking first word with a capital skip white space. */
3291 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003292 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003293
Bram Moolenaar30abd282005-06-22 22:35:10 +00003294 /* To be able to spell-check over line boundaries copy the end of the
3295 * current line into nextline[]. Above the start of the next line was
3296 * copied to nextline[SPWORDLEN]. */
3297 if (nextline[SPWORDLEN] == NUL)
3298 {
3299 /* No next line or it is empty. */
3300 nextlinecol = MAXCOL;
3301 nextline_idx = 0;
3302 }
3303 else
3304 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003306 if (v < SPWORDLEN)
3307 {
3308 /* Short line, use it completely and append the start of the
3309 * next line. */
3310 nextlinecol = 0;
3311 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003312 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003313 nextline_idx = v + 1;
3314 }
3315 else
3316 {
3317 /* Long line, use only the last SPWORDLEN bytes. */
3318 nextlinecol = v - SPWORDLEN;
3319 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3320 nextline_idx = SPWORDLEN + 1;
3321 }
3322 }
3323 }
3324#endif
3325
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003326 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003328 if (lcs_space || lcs_trail)
3329 extra_check = TRUE;
3330 /* find start of trailing whitespace */
3331 if (lcs_trail)
3332 {
3333 trailcol = (colnr_T)STRLEN(ptr);
3334 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3335 --trailcol;
3336 trailcol += (colnr_T) (ptr - line);
3337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339
3340 /*
3341 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3342 * first character to be displayed.
3343 */
3344 if (wp->w_p_wrap)
3345 v = wp->w_skipcol;
3346 else
3347 v = wp->w_leftcol;
3348 if (v > 0)
3349 {
3350#ifdef FEAT_MBYTE
3351 char_u *prev_ptr = ptr;
3352#endif
3353 while (vcol < v && *ptr != NUL)
3354 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003355 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 vcol += c;
3357#ifdef FEAT_MBYTE
3358 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003360 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003363 /* When:
3364 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003365 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003366 * - 'virtualedit' is set, or
3367 * - the visual mode is active,
3368 * the end of the line may be before the start of the displayed part.
3369 */
3370 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003371#ifdef FEAT_SYN_HL
3372 wp->w_p_cuc || draw_color_col ||
3373#endif
3374#ifdef FEAT_VIRTUALEDIT
3375 virtual_active() ||
3376#endif
3377 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
3382 /* Handle a character that's not completely on the screen: Put ptr at
3383 * that character but skip the first few screen characters. */
3384 if (vcol > v)
3385 {
3386 vcol -= c;
3387#ifdef FEAT_MBYTE
3388 ptr = prev_ptr;
3389#else
3390 --ptr;
3391#endif
3392 n_skip = v - vcol;
3393 }
3394
3395 /*
3396 * Adjust for when the inverted text is before the screen,
3397 * and when the start of the inverted text is before the screen.
3398 */
3399 if (tocol <= vcol)
3400 fromcol = 0;
3401 else if (fromcol >= 0 && fromcol < vcol)
3402 fromcol = vcol;
3403
3404#ifdef FEAT_LINEBREAK
3405 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3406 if (wp->w_p_wrap)
3407 need_showbreak = TRUE;
3408#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003409#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003410 /* When spell checking a word we need to figure out the start of the
3411 * word and if it's badly spelled or not. */
3412 if (has_spell)
3413 {
3414 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003415 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003416 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003417
3418 pos = wp->w_cursor;
3419 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003420 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003421 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003422
3423 /* spell_move_to() may call ml_get() and make "line" invalid */
3424 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3425 ptr = line + linecol;
3426
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003427 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003428 {
3429 /* no bad word found at line start, don't check until end of a
3430 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003431 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003432 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003433 }
3434 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003435 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003436 /* bad word found, use attributes until end of word */
3437 word_end = wp->w_cursor.col + len + 1;
3438
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003439 /* Turn index into actual attributes. */
3440 if (spell_hlf != HLF_COUNT)
3441 spell_attr = highlight_attr[spell_hlf];
3442 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003443 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003444
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003445# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003446 /* Need to restart syntax highlighting for this line. */
3447 if (has_syntax)
3448 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003449# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003450 }
3451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453
3454 /*
3455 * Correct highlighting for cursor that can't be disabled.
3456 * Avoids having to check this for each character.
3457 */
3458 if (fromcol >= 0)
3459 {
3460 if (noinvcur)
3461 {
3462 if ((colnr_T)fromcol == wp->w_virtcol)
3463 {
3464 /* highlighting starts at cursor, let it start just after the
3465 * cursor */
3466 fromcol_prev = fromcol;
3467 fromcol = -1;
3468 }
3469 else if ((colnr_T)fromcol < wp->w_virtcol)
3470 /* restart highlighting after the cursor */
3471 fromcol_prev = wp->w_virtcol;
3472 }
3473 if (fromcol >= tocol)
3474 fromcol = -1;
3475 }
3476
3477#ifdef FEAT_SEARCH_EXTRA
3478 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003479 * Handle highlighting the last used search pattern and matches.
3480 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003482 cur = wp->w_match_head;
3483 shl_flag = FALSE;
3484 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003486 if (shl_flag == FALSE)
3487 {
3488 shl = &search_hl;
3489 shl_flag = TRUE;
3490 }
3491 else
3492 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003493 shl->startcol = MAXCOL;
3494 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003496 v = (long)(ptr - line);
3497 if (cur != NULL)
3498 cur->pos.cur = 0;
3499 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3500
3501 /* Need to get the line again, a multi-line regexp may have made it
3502 * invalid. */
3503 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3504 ptr = line + v;
3505
3506 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003508 if (shl->lnum == lnum)
3509 shl->startcol = shl->rm.startpos[0].col;
3510 else
3511 shl->startcol = 0;
3512 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3513 - shl->rm.startpos[0].lnum)
3514 shl->endcol = shl->rm.endpos[0].col;
3515 else
3516 shl->endcol = MAXCOL;
3517 /* Highlight one character for an empty match. */
3518 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003521 if (has_mbyte && line[shl->endcol] != NUL)
3522 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003525 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003527 if ((long)shl->startcol < v) /* match at leftcol */
3528 {
3529 shl->attr_cur = shl->attr;
3530 search_attr = shl->attr;
3531 }
3532 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003534 if (shl != &search_hl && cur != NULL)
3535 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537#endif
3538
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003539#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003540 /* Cursor line highlighting for 'cursorline' in the current window. Not
3541 * when Visual mode is active, because it's not clear what is selected
3542 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003543 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003544 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003545 {
3546 line_attr = hl_attr(HLF_CUL);
3547 area_highlighting = TRUE;
3548 }
3549#endif
3550
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003551 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 col = 0;
3553#ifdef FEAT_RIGHTLEFT
3554 if (wp->w_p_rl)
3555 {
3556 /* Rightleft window: process the text in the normal direction, but put
3557 * it in current_ScreenLine[] from right to left. Start at the
3558 * rightmost column of the window. */
3559 col = W_WIDTH(wp) - 1;
3560 off += col;
3561 }
3562#endif
3563
3564 /*
3565 * Repeat for the whole displayed line.
3566 */
3567 for (;;)
3568 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003569#ifdef FEAT_CONCEAL
3570 has_match_conc = FALSE;
3571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 /* Skip this quickly when working on the text. */
3573 if (draw_state != WL_LINE)
3574 {
3575#ifdef FEAT_CMDWIN
3576 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3577 {
3578 draw_state = WL_CMDLINE;
3579 if (cmdwin_type != 0 && wp == curwin)
3580 {
3581 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003583 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 char_attr = hl_attr(HLF_AT);
3585 }
3586 }
3587#endif
3588
3589#ifdef FEAT_FOLDING
3590 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3591 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003592 int fdc = compute_foldcolumn(wp, 0);
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003595 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 /* Draw the 'foldcolumn'. */
3598 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003599 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003601 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 c_extra = NUL;
3603 char_attr = hl_attr(HLF_FC);
3604 }
3605 }
3606#endif
3607
3608#ifdef FEAT_SIGNS
3609 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3610 {
3611 draw_state = WL_SIGN;
3612 /* Show the sign column when there are any signs in this
3613 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003614 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003616 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003618 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619# endif
3620
3621 /* Draw two cells with the sign value or blank. */
3622 c_extra = ' ';
3623 char_attr = hl_attr(HLF_SC);
3624 n_extra = 2;
3625
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003626 if (row == startrow
3627#ifdef FEAT_DIFF
3628 + filler_lines && filler_todo <= 0
3629#endif
3630 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3633 SIGN_TEXT);
3634# ifdef FEAT_SIGN_ICONS
3635 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3636 SIGN_ICON);
3637 if (gui.in_use && icon_sign != 0)
3638 {
3639 /* Use the image in this position. */
3640 c_extra = SIGN_BYTE;
3641# ifdef FEAT_NETBEANS_INTG
3642 if (buf_signcount(wp->w_buffer, lnum) > 1)
3643 c_extra = MULTISIGN_BYTE;
3644# endif
3645 char_attr = icon_sign;
3646 }
3647 else
3648# endif
3649 if (text_sign != 0)
3650 {
3651 p_extra = sign_get_text(text_sign);
3652 if (p_extra != NULL)
3653 {
3654 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003655 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 char_attr = sign_get_attr(text_sign, FALSE);
3658 }
3659 }
3660 }
3661 }
3662#endif
3663
3664 if (draw_state == WL_NR - 1 && n_extra == 0)
3665 {
3666 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003667 /* Display the absolute or relative line number. After the
3668 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3669 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 && (row == startrow
3671#ifdef FEAT_DIFF
3672 + filler_lines
3673#endif
3674 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3675 {
3676 /* Draw the line number (empty space after wrapping). */
3677 if (row == startrow
3678#ifdef FEAT_DIFF
3679 + filler_lines
3680#endif
3681 )
3682 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003683 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003684 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003685
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003686 if (wp->w_p_nu && !wp->w_p_rnu)
3687 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003688 num = (long)lnum;
3689 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003690 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003691 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003692 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003693 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003694 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003695 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003696 num = lnum;
3697 fmt = "%-*ld ";
3698 }
3699 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003700
Bram Moolenaar700e7342013-01-30 12:31:36 +01003701 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003702 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (wp->w_skipcol > 0)
3704 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3705 *p_extra = '-';
3706#ifdef FEAT_RIGHTLEFT
3707 if (wp->w_p_rl) /* reverse line numbers */
3708 rl_mirror(extra);
3709#endif
3710 p_extra = extra;
3711 c_extra = NUL;
3712 }
3713 else
3714 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003715 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003717#ifdef FEAT_SYN_HL
3718 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003719 * the current line differently.
3720 * TODO: Can we use CursorLine instead of CursorLineNr
3721 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003722 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003723 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003724 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 }
3728
Bram Moolenaar597a4222014-06-25 14:39:50 +02003729#ifdef FEAT_LINEBREAK
3730 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3731 && n_extra == 0 && *p_sbr != NUL)
3732 /* draw indent after showbreak value */
3733 draw_state = WL_BRI;
3734 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3735 /* After the showbreak, draw the breakindent */
3736 draw_state = WL_BRI - 1;
3737
3738 /* draw 'breakindent': indent wrapped text accordingly */
3739 if (draw_state == WL_BRI - 1 && n_extra == 0)
3740 {
3741 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003742 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003743# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003744 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003745# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003746 )
3747 {
3748 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003749# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003750 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003751 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003752 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003753# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003754 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3755 char_attr = hl_combine_attr(char_attr,
3756 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003757# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003758 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003759# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003760 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003761 c_extra = ' ';
3762 n_extra = get_breakindent_win(wp,
3763 ml_get_buf(wp->w_buffer, lnum, FALSE));
3764 /* Correct end of highlighted area for 'breakindent',
3765 * required when 'linebreak' is also set. */
3766 if (tocol == vcol)
3767 tocol += n_extra;
3768 }
3769 }
3770#endif
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3773 if (draw_state == WL_SBR - 1 && n_extra == 0)
3774 {
3775 draw_state = WL_SBR;
3776# ifdef FEAT_DIFF
3777 if (filler_todo > 0)
3778 {
3779 /* Draw "deleted" diff line(s). */
3780 if (char2cells(fill_diff) > 1)
3781 c_extra = '-';
3782 else
3783 c_extra = fill_diff;
3784# ifdef FEAT_RIGHTLEFT
3785 if (wp->w_p_rl)
3786 n_extra = col + 1;
3787 else
3788# endif
3789 n_extra = W_WIDTH(wp) - col;
3790 char_attr = hl_attr(HLF_DED);
3791 }
3792# endif
3793# ifdef FEAT_LINEBREAK
3794 if (*p_sbr != NUL && need_showbreak)
3795 {
3796 /* Draw 'showbreak' at the start of each broken line. */
3797 p_extra = p_sbr;
3798 c_extra = NUL;
3799 n_extra = (int)STRLEN(p_sbr);
3800 char_attr = hl_attr(HLF_AT);
3801 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003802 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 /* Correct end of highlighted area for 'showbreak',
3804 * required when 'linebreak' is also set. */
3805 if (tocol == vcol)
3806 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003807#ifdef FEAT_SYN_HL
3808 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003809 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003810 char_attr = hl_combine_attr(char_attr,
3811 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814# endif
3815 }
3816#endif
3817
3818 if (draw_state == WL_LINE - 1 && n_extra == 0)
3819 {
3820 draw_state = WL_LINE;
3821 if (saved_n_extra)
3822 {
3823 /* Continue item from end of wrapped line. */
3824 n_extra = saved_n_extra;
3825 c_extra = saved_c_extra;
3826 p_extra = saved_p_extra;
3827 char_attr = saved_char_attr;
3828 }
3829 else
3830 char_attr = 0;
3831 }
3832 }
3833
3834 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003835 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#ifdef FEAT_DIFF
3838 && filler_todo <= 0
3839#endif
3840 )
3841 {
3842 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3843 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003844 /* Pretend we have finished updating the window. Except when
3845 * 'cursorcolumn' is set. */
3846#ifdef FEAT_SYN_HL
3847 if (wp->w_p_cuc)
3848 row = wp->w_cline_row + wp->w_cline_height;
3849 else
3850#endif
3851 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 break;
3853 }
3854
3855 if (draw_state == WL_LINE && area_highlighting)
3856 {
3857 /* handle Visual or match highlighting in this line */
3858 if (vcol == fromcol
3859#ifdef FEAT_MBYTE
3860 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3861 && (*mb_ptr2cells)(ptr) > 1)
3862#endif
3863 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003864 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 && vcol < tocol))
3866 area_attr = attr; /* start highlighting */
3867 else if (area_attr != 0
3868 && (vcol == tocol
3869 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872#ifdef FEAT_SEARCH_EXTRA
3873 if (!n_extra)
3874 {
3875 /*
3876 * Check for start/end of search pattern match.
3877 * After end, check for start/end of next match.
3878 * When another match, have to check for start again.
3879 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 * Do this for 'search_hl' and the match list (ordered by
3881 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003883 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003884 cur = wp->w_match_head;
3885 shl_flag = FALSE;
3886 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003888 if (shl_flag == FALSE
3889 && ((cur != NULL
3890 && cur->priority > SEARCH_HL_PRIORITY)
3891 || cur == NULL))
3892 {
3893 shl = &search_hl;
3894 shl_flag = TRUE;
3895 }
3896 else
3897 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003898 if (cur != NULL)
3899 cur->pos.cur = 0;
3900 pos_inprogress = TRUE;
3901 while (shl->rm.regprog != NULL
3902 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003904 if (shl->startcol != MAXCOL
3905 && v >= (long)shl->startcol
3906 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003908#ifdef FEAT_MBYTE
3909 int tmp_col = v + MB_PTR2LEN(ptr);
3910
3911 if (shl->endcol < tmp_col)
3912 shl->endcol = tmp_col;
3913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003915#ifdef FEAT_CONCEAL
3916 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3917 == cur->hlg_id)
3918 {
3919 has_match_conc = TRUE;
3920 match_conc = cur->conceal_char;
3921 }
3922 else
3923 has_match_conc = match_conc = FALSE;
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003926 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
3928 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003929#ifdef FEAT_CONCEAL
3930 prev_syntax_id = 0;
3931#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003932 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3933 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003934 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 /* Need to get the line again, a multi-line regexp
3937 * may have made it invalid. */
3938 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3939 ptr = line + v;
3940
3941 if (shl->lnum == lnum)
3942 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003943 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003945 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003947 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003949 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
3951 /* highlight empty match, try again after
3952 * it */
3953#ifdef FEAT_MBYTE
3954 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003955 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003956 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
3958#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003959 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961
3962 /* Loop to check if the match starts at the
3963 * current position */
3964 continue;
3965 }
3966 }
3967 break;
3968 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003969 if (shl != &search_hl && cur != NULL)
3970 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003972
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003973 /* Use attributes from match with highest priority among
3974 * 'search_hl' and the match list. */
3975 search_attr = search_hl.attr_cur;
3976 cur = wp->w_match_head;
3977 shl_flag = FALSE;
3978 while (cur != NULL || shl_flag == FALSE)
3979 {
3980 if (shl_flag == FALSE
3981 && ((cur != NULL
3982 && cur->priority > SEARCH_HL_PRIORITY)
3983 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003984 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003985 shl = &search_hl;
3986 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003987 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003988 else
3989 shl = &cur->hl;
3990 if (shl->attr_cur != 0)
3991 search_attr = shl->attr_cur;
3992 if (shl != &search_hl && cur != NULL)
3993 cur = cur->next;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996#endif
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003999 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004001 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4002 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004004 if (diff_hlf == HLF_TXD && ptr - line > change_end
4005 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004007 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004008 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4009 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004012
4013 /* Decide which of the highlight attributes to use. */
4014 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004015#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004016 if (area_attr != 0)
4017 char_attr = hl_combine_attr(line_attr, area_attr);
4018 else if (search_attr != 0)
4019 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004020 /* Use line_attr when not in the Visual or 'incsearch' area
4021 * (area_attr may be 0 when "noinvcur" is set). */
4022 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004023 || vcol < fromcol || vcol_prev < fromcol_prev
4024 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004025 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004026#else
4027 if (area_attr != 0)
4028 char_attr = area_attr;
4029 else if (search_attr != 0)
4030 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004031#endif
4032 else
4033 {
4034 attr_pri = FALSE;
4035#ifdef FEAT_SYN_HL
4036 if (has_syntax)
4037 char_attr = syntax_attr;
4038 else
4039#endif
4040 char_attr = 0;
4041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043
4044 /*
4045 * Get the next character to put on the screen.
4046 */
4047 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004048 * The "p_extra" points to the extra stuff that is inserted to
4049 * represent special characters (non-printable stuff) and other
4050 * things. When all characters are the same, c_extra is used.
4051 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4052 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4054 */
4055 if (n_extra > 0)
4056 {
4057 if (c_extra != NUL)
4058 {
4059 c = c_extra;
4060#ifdef FEAT_MBYTE
4061 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4062 if (enc_utf8 && (*mb_char2len)(c) > 1)
4063 {
4064 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004065 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 else
4069 mb_utf8 = FALSE;
4070#endif
4071 }
4072 else
4073 {
4074 c = *p_extra;
4075#ifdef FEAT_MBYTE
4076 if (has_mbyte)
4077 {
4078 mb_c = c;
4079 if (enc_utf8)
4080 {
4081 /* If the UTF-8 character is more than one byte:
4082 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004083 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 mb_utf8 = FALSE;
4085 if (mb_l > n_extra)
4086 mb_l = 1;
4087 else if (mb_l > 1)
4088 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004089 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004091 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 }
4094 else
4095 {
4096 /* if this is a DBCS character, put it in "mb_c" */
4097 mb_l = MB_BYTE2LEN(c);
4098 if (mb_l >= n_extra)
4099 mb_l = 1;
4100 else if (mb_l > 1)
4101 mb_c = (c << 8) + p_extra[1];
4102 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004103 if (mb_l == 0) /* at the NUL at end-of-line */
4104 mb_l = 1;
4105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 /* If a double-width char doesn't fit display a '>' in the
4107 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004108 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109# ifdef FEAT_RIGHTLEFT
4110 wp->w_p_rl ? (col <= 0) :
4111# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004112 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 && (*mb_char2cells)(mb_c) == 2)
4114 {
4115 c = '>';
4116 mb_c = c;
4117 mb_l = 1;
4118 mb_utf8 = FALSE;
4119 multi_attr = hl_attr(HLF_AT);
4120 /* put the pointer back to output the double-width
4121 * character at the start of the next line. */
4122 ++n_extra;
4123 --p_extra;
4124 }
4125 else
4126 {
4127 n_extra -= mb_l - 1;
4128 p_extra += mb_l - 1;
4129 }
4130 }
4131#endif
4132 ++p_extra;
4133 }
4134 --n_extra;
4135 }
4136 else
4137 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004138 if (p_extra_free != NULL)
4139 {
4140 vim_free(p_extra_free);
4141 p_extra_free = NULL;
4142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /*
4144 * Get a character from the line itself.
4145 */
4146 c = *ptr;
4147#ifdef FEAT_MBYTE
4148 if (has_mbyte)
4149 {
4150 mb_c = c;
4151 if (enc_utf8)
4152 {
4153 /* If the UTF-8 character is more than one byte: Decode it
4154 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004155 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 mb_utf8 = FALSE;
4157 if (mb_l > 1)
4158 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004159 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 /* Overlong encoded ASCII or ASCII with composing char
4161 * is displayed normally, except a NUL. */
4162 if (mb_c < 0x80)
4163 c = mb_c;
4164 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004165
4166 /* At start of the line we can have a composing char.
4167 * Draw it as a space with a composing char. */
4168 if (utf_iscomposing(mb_c))
4169 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004170 int i;
4171
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004172 for (i = Screen_mco - 1; i > 0; --i)
4173 u8cc[i] = u8cc[i - 1];
4174 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004175 mb_c = ' ';
4176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178
4179 if ((mb_l == 1 && c >= 0x80)
4180 || (mb_l >= 1 && mb_c == 0)
4181 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004182# ifdef UNICODE16
4183 || mb_c >= 0x10000
4184# endif
4185 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 /*
4188 * Illegal UTF-8 byte: display as <xx>.
4189 * Non-BMP character : display as ? or fullwidth ?.
4190 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004191# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
4195 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004196# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 if (wp->w_p_rl) /* reverse */
4198 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004201# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 else if (utf_char2cells(mb_c) != 2)
4203 STRCPY(extra, "?");
4204 else
4205 /* 0xff1f in UTF-8: full-width '?' */
4206 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208
4209 p_extra = extra;
4210 c = *p_extra;
4211 mb_c = mb_ptr2char_adv(&p_extra);
4212 mb_utf8 = (c >= 0x80);
4213 n_extra = (int)STRLEN(p_extra);
4214 c_extra = NUL;
4215 if (area_attr == 0 && search_attr == 0)
4216 {
4217 n_attr = n_extra + 1;
4218 extra_attr = hl_attr(HLF_8);
4219 saved_attr2 = char_attr; /* save current attr */
4220 }
4221 }
4222 else if (mb_l == 0) /* at the NUL at end-of-line */
4223 mb_l = 1;
4224#ifdef FEAT_ARABIC
4225 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4226 {
4227 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004228 int pc, pc1, nc;
4229 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 /* The idea of what is the previous and next
4232 * character depends on 'rightleft'. */
4233 if (wp->w_p_rl)
4234 {
4235 pc = prev_c;
4236 pc1 = prev_c1;
4237 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240 else
4241 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004242 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 prev_c = mb_c;
4247
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004248 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 else
4251 prev_c = mb_c;
4252#endif
4253 }
4254 else /* enc_dbcs */
4255 {
4256 mb_l = MB_BYTE2LEN(c);
4257 if (mb_l == 0) /* at the NUL at end-of-line */
4258 mb_l = 1;
4259 else if (mb_l > 1)
4260 {
4261 /* We assume a second byte below 32 is illegal.
4262 * Hopefully this is OK for all double-byte encodings!
4263 */
4264 if (ptr[1] >= 32)
4265 mb_c = (c << 8) + ptr[1];
4266 else
4267 {
4268 if (ptr[1] == NUL)
4269 {
4270 /* head byte at end of line */
4271 mb_l = 1;
4272 transchar_nonprint(extra, c);
4273 }
4274 else
4275 {
4276 /* illegal tail byte */
4277 mb_l = 2;
4278 STRCPY(extra, "XX");
4279 }
4280 p_extra = extra;
4281 n_extra = (int)STRLEN(extra) - 1;
4282 c_extra = NUL;
4283 c = *p_extra++;
4284 if (area_attr == 0 && search_attr == 0)
4285 {
4286 n_attr = n_extra + 1;
4287 extra_attr = hl_attr(HLF_8);
4288 saved_attr2 = char_attr; /* save current attr */
4289 }
4290 mb_c = c;
4291 }
4292 }
4293 }
4294 /* If a double-width char doesn't fit display a '>' in the
4295 * last column; the character is displayed at the start of the
4296 * next line. */
4297 if ((
4298# ifdef FEAT_RIGHTLEFT
4299 wp->w_p_rl ? (col <= 0) :
4300# endif
4301 (col >= W_WIDTH(wp) - 1))
4302 && (*mb_char2cells)(mb_c) == 2)
4303 {
4304 c = '>';
4305 mb_c = c;
4306 mb_utf8 = FALSE;
4307 mb_l = 1;
4308 multi_attr = hl_attr(HLF_AT);
4309 /* Put pointer back so that the character will be
4310 * displayed at the start of the next line. */
4311 --ptr;
4312 }
4313 else if (*ptr != NUL)
4314 ptr += mb_l - 1;
4315
4316 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004317 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004318 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004319 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004322 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 c = ' ';
4324 if (area_attr == 0 && search_attr == 0)
4325 {
4326 n_attr = n_extra + 1;
4327 extra_attr = hl_attr(HLF_AT);
4328 saved_attr2 = char_attr; /* save current attr */
4329 }
4330 mb_c = c;
4331 mb_utf8 = FALSE;
4332 mb_l = 1;
4333 }
4334
4335 }
4336#endif
4337 ++ptr;
4338
4339 if (extra_check)
4340 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004341#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004342 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004343#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004344
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004345#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /* Get syntax attribute, unless still at the start of the line
4347 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004348 v = (long)(ptr - line);
4349 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
4351 /* Get the syntax attribute for the character. If there
4352 * is an error, disable syntax highlighting. */
4353 save_did_emsg = did_emsg;
4354 did_emsg = FALSE;
4355
Bram Moolenaar217ad922005-03-20 22:37:15 +00004356 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004357# ifdef FEAT_SPELL
4358 has_spell ? &can_spell :
4359# endif
4360 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004363 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004364 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004365 has_syntax = FALSE;
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 else
4368 did_emsg = save_did_emsg;
4369
4370 /* Need to get the line again, a multi-line regexp may
4371 * have made it invalid. */
4372 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4373 ptr = line + v;
4374
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004375 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004377 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004378 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004379# ifdef FEAT_CONCEAL
4380 /* no concealing past the end of the line, it interferes
4381 * with line highlighting */
4382 if (c == NUL)
4383 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004384 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004385 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004388#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004389
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004390#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004391 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004392 * Only do this when there is no syntax highlighting, the
4393 * @Spell cluster is not used or the current syntax item
4394 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004395 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004396 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004397 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004398# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004399 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004400 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004401# endif
4402 if (c != 0 && (
4403# ifdef FEAT_SYN_HL
4404 !has_syntax ||
4405# endif
4406 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004407 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004408 char_u *prev_ptr, *p;
4409 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004410 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004411# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004412 if (has_mbyte)
4413 {
4414 prev_ptr = ptr - mb_l;
4415 v -= mb_l - 1;
4416 }
4417 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004418# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004419 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004420
4421 /* Use nextline[] if possible, it has the start of the
4422 * next line concatenated. */
4423 if ((prev_ptr - line) - nextlinecol >= 0)
4424 p = nextline + (prev_ptr - line) - nextlinecol;
4425 else
4426 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004427 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004428 len = spell_check(wp, p, &spell_hlf, &cap_col,
4429 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004430 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004431
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004432 /* In Insert mode only highlight a word that
4433 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004434 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004435 && (State & INSERT) != 0
4436 && wp->w_cursor.lnum == lnum
4437 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004438 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004439 && wp->w_cursor.col < (colnr_T)word_end)
4440 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004441 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004442 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004443 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004444
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004445 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004446 && (p - nextline) + len > nextline_idx)
4447 {
4448 /* Remember that the good word continues at the
4449 * start of the next line. */
4450 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004451 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004452 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004453
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004454 /* Turn index into actual attributes. */
4455 if (spell_hlf != HLF_COUNT)
4456 spell_attr = highlight_attr[spell_hlf];
4457
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004458 if (cap_col > 0)
4459 {
4460 if (p != prev_ptr
4461 && (p - nextline) + cap_col >= nextline_idx)
4462 {
4463 /* Remember that the word in the next line
4464 * must start with a capital. */
4465 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004466 cap_col = (int)((p - nextline) + cap_col
4467 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004468 }
4469 else
4470 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004471 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004472 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004473 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004474 }
4475 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004476 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004477 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004478 char_attr = hl_combine_attr(char_attr, spell_attr);
4479 else
4480 char_attr = hl_combine_attr(spell_attr, char_attr);
4481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#endif
4483#ifdef FEAT_LINEBREAK
4484 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004485 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004487 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004489# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004490 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004491# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004492 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004494 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004496 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004497
Bram Moolenaar597a4222014-06-25 14:39:50 +02004498 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004499 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004500 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004501 if (c == TAB && n_extra + col > W_WIDTH(wp))
4502 n_extra = (int)wp->w_buffer->b_p_ts
4503 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4504
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004505# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004506 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004507# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004511 {
4512#ifdef FEAT_CONCEAL
4513 if (c == TAB)
4514 /* See "Tab alignment" below. */
4515 FIX_FOR_BOGUSCOLS;
4516#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004517 if (!wp->w_p_list)
4518 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521#endif
4522
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004523 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4524 */
4525 if (wp->w_p_list
4526 && (((c == 160
4527#ifdef FEAT_MBYTE
4528 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4529#endif
4530 ) && lcs_nbsp)
4531 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4532 {
4533 c = (c == ' ') ? lcs_space : lcs_nbsp;
4534 if (area_attr == 0 && search_attr == 0)
4535 {
4536 n_attr = 1;
4537 extra_attr = hl_attr(HLF_8);
4538 saved_attr2 = char_attr; /* save current attr */
4539 }
4540#ifdef FEAT_MBYTE
4541 mb_c = c;
4542 if (enc_utf8 && (*mb_char2len)(c) > 1)
4543 {
4544 mb_utf8 = TRUE;
4545 u8cc[0] = 0;
4546 c = 0xc0;
4547 }
4548 else
4549 mb_utf8 = FALSE;
4550#endif
4551 }
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4554 {
4555 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004556 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 n_attr = 1;
4559 extra_attr = hl_attr(HLF_8);
4560 saved_attr2 = char_attr; /* save current attr */
4561 }
4562#ifdef FEAT_MBYTE
4563 mb_c = c;
4564 if (enc_utf8 && (*mb_char2len)(c) > 1)
4565 {
4566 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004567 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004568 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 else
4571 mb_utf8 = FALSE;
4572#endif
4573 }
4574 }
4575
4576 /*
4577 * Handling of non-printable characters.
4578 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004579 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581 /*
4582 * when getting a character from the file, we may have to
4583 * turn it into something else on the way to putting it
4584 * into "ScreenLines".
4585 */
4586 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4587 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004588 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004589 long vcol_adjusted = vcol; /* removed showbreak length */
4590#ifdef FEAT_LINEBREAK
4591 /* only adjust the tab_len, when at the first column
4592 * after the showbreak value was drawn */
4593 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4594 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004597 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004598 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4599
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004600#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004601 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004602#endif
4603 /* tab amount depends on current column */
4604 n_extra = tab_len;
4605#ifdef FEAT_LINEBREAK
4606 else
4607 {
4608 char_u *p;
4609 int len = n_extra;
4610 int i;
4611 int saved_nextra = n_extra;
4612
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004613#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004614 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004615 /* there are characters to conceal */
4616 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004617 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4618 */
4619 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4620 && n_extra > tab_len)
4621 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004622#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004623
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004624 /* if n_extra > 0, it gives the number of chars, to
4625 * use for a tab, else we need to calculate the width
4626 * for a tab */
4627#ifdef FEAT_MBYTE
4628 len = (tab_len * mb_char2len(lcs_tab2));
4629 if (n_extra > 0)
4630 len += n_extra - tab_len;
4631#endif
4632 c = lcs_tab1;
4633 p = alloc((unsigned)(len + 1));
4634 vim_memset(p, ' ', len);
4635 p[len] = NUL;
4636 p_extra_free = p;
4637 for (i = 0; i < tab_len; i++)
4638 {
4639#ifdef FEAT_MBYTE
4640 mb_char2bytes(lcs_tab2, p);
4641 p += mb_char2len(lcs_tab2);
4642 n_extra += mb_char2len(lcs_tab2)
4643 - (saved_nextra > 0 ? 1 : 0);
4644#else
4645 p[i] = lcs_tab2;
4646#endif
4647 }
4648 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004649#ifdef FEAT_CONCEAL
4650 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4651 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004652 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004653 n_extra -= vcol_off;
4654#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004655 }
4656#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004657#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004658 {
4659 int vc_saved = vcol_off;
4660
4661 /* Tab alignment should be identical regardless of
4662 * 'conceallevel' value. So tab compensates of all
4663 * previous concealed characters, and thus resets
4664 * vcol_off and boguscols accumulated so far in the
4665 * line. Note that the tab can be longer than
4666 * 'tabstop' when there are concealed characters. */
4667 FIX_FOR_BOGUSCOLS;
4668
4669 /* Make sure, the highlighting for the tab char will be
4670 * correctly set further below (effectively reverts the
4671 * FIX_FOR_BOGSUCOLS macro */
4672 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004673 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004674 tab_len += vc_saved;
4675 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#ifdef FEAT_MBYTE
4678 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4679#endif
4680 if (wp->w_p_list)
4681 {
4682 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004683#ifdef FEAT_LINEBREAK
4684 if (wp->w_p_lbr)
4685 c_extra = NUL; /* using p_extra from above */
4686 else
4687#endif
4688 c_extra = lcs_tab2;
4689 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 extra_attr = hl_attr(HLF_8);
4691 saved_attr2 = char_attr; /* save current attr */
4692#ifdef FEAT_MBYTE
4693 mb_c = c;
4694 if (enc_utf8 && (*mb_char2len)(c) > 1)
4695 {
4696 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004697 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004698 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700#endif
4701 }
4702 else
4703 {
4704 c_extra = ' ';
4705 c = ' ';
4706 }
4707 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004708 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004709 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004710 || ((fromcol >= 0 || fromcol_prev >= 0)
4711 && tocol > vcol
4712 && VIsual_mode != Ctrl_V
4713 && (
4714# ifdef FEAT_RIGHTLEFT
4715 wp->w_p_rl ? (col >= 0) :
4716# endif
4717 (col < W_WIDTH(wp)))
4718 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004719 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004720 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004721 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004723 /* Display a '$' after the line or highlight an extra
4724 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4726 /* For a diff line the highlighting continues after the
4727 * "$". */
4728 if (
4729# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004730 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731# ifdef LINE_ATTR
4732 &&
4733# endif
4734# endif
4735# ifdef LINE_ATTR
4736 line_attr == 0
4737# endif
4738 )
4739#endif
4740 {
4741#ifdef FEAT_VIRTUALEDIT
4742 /* In virtualedit, visual selections may extend
4743 * beyond end of line. */
4744 if (area_highlighting && virtual_active()
4745 && tocol != MAXCOL && vcol < tocol)
4746 n_extra = 0;
4747 else
4748#endif
4749 {
4750 p_extra = at_end_str;
4751 n_extra = 1;
4752 c_extra = NUL;
4753 }
4754 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004755 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004756 c = lcs_eol;
4757 else
4758 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 lcs_eol_one = -1;
4760 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004761 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 extra_attr = hl_attr(HLF_AT);
4764 n_attr = 1;
4765 }
4766#ifdef FEAT_MBYTE
4767 mb_c = c;
4768 if (enc_utf8 && (*mb_char2len)(c) > 1)
4769 {
4770 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004771 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004772 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774 else
4775 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4776#endif
4777 }
4778 else if (c != NUL)
4779 {
4780 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004781 if (n_extra == 0)
4782 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783#ifdef FEAT_RIGHTLEFT
4784 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4785 rl_mirror(p_extra); /* reverse "<12>" */
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004788#ifdef FEAT_LINEBREAK
4789 if (wp->w_p_lbr)
4790 {
4791 char_u *p;
4792
4793 c = *p_extra;
4794 p = alloc((unsigned)n_extra + 1);
4795 vim_memset(p, ' ', n_extra);
4796 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4797 p[n_extra] = NUL;
4798 p_extra_free = p_extra = p;
4799 }
4800 else
4801#endif
4802 {
4803 n_extra = byte2cells(c) - 1;
4804 c = *p_extra++;
4805 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004806 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 n_attr = n_extra + 1;
4809 extra_attr = hl_attr(HLF_8);
4810 saved_attr2 = char_attr; /* save current attr */
4811 }
4812#ifdef FEAT_MBYTE
4813 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4814#endif
4815 }
4816#ifdef FEAT_VIRTUALEDIT
4817 else if (VIsual_active
4818 && (VIsual_mode == Ctrl_V
4819 || VIsual_mode == 'v')
4820 && virtual_active()
4821 && tocol != MAXCOL
4822 && vcol < tocol
4823 && (
4824# ifdef FEAT_RIGHTLEFT
4825 wp->w_p_rl ? (col >= 0) :
4826# endif
4827 (col < W_WIDTH(wp))))
4828 {
4829 c = ' ';
4830 --ptr; /* put it back at the NUL */
4831 }
4832#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004833#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else if ((
4835# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004836 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 ) && (
4840# ifdef FEAT_RIGHTLEFT
4841 wp->w_p_rl ? (col >= 0) :
4842# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004843 (col
4844# ifdef FEAT_CONCEAL
4845 - boguscols
4846# endif
4847 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
4849 /* Highlight until the right side of the window */
4850 c = ' ';
4851 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004852
4853 /* Remember we do the char for line highlighting. */
4854 ++did_line_attr;
4855
4856 /* don't do search HL for the rest of the line */
4857 if (line_attr != 0 && char_attr == search_attr && col > 0)
4858 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859# ifdef FEAT_DIFF
4860 if (diff_hlf == HLF_TXD)
4861 {
4862 diff_hlf = HLF_CHD;
4863 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004866 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4867 char_attr = hl_combine_attr(char_attr,
4868 hl_attr(HLF_CUL));
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871# endif
4872 }
4873#endif
4874 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004875
4876#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004877 if ( wp->w_p_cole > 0
4878 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004879 conceal_cursor_line(wp) )
4880 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004881 && !(lnum_in_visual_area
4882 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004883 {
4884 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004885 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004886 && (syn_get_sub_char() != NUL || match_conc
4887 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004888 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004889 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004890 /* First time at this concealed item: display one
4891 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004892 if (match_conc)
4893 c = match_conc;
4894 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004895 c = syn_get_sub_char();
4896 else if (lcs_conceal != NUL)
4897 c = lcs_conceal;
4898 else
4899 c = ' ';
4900
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004901 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004902
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004903 if (n_extra > 0)
4904 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004905 vcol += n_extra;
4906 if (wp->w_p_wrap && n_extra > 0)
4907 {
4908# ifdef FEAT_RIGHTLEFT
4909 if (wp->w_p_rl)
4910 {
4911 col -= n_extra;
4912 boguscols -= n_extra;
4913 }
4914 else
4915# endif
4916 {
4917 boguscols += n_extra;
4918 col += n_extra;
4919 }
4920 }
4921 n_extra = 0;
4922 n_attr = 0;
4923 }
4924 else if (n_skip == 0)
4925 {
4926 is_concealing = TRUE;
4927 n_skip = 1;
4928 }
4929# ifdef FEAT_MBYTE
4930 mb_c = c;
4931 if (enc_utf8 && (*mb_char2len)(c) > 1)
4932 {
4933 mb_utf8 = TRUE;
4934 u8cc[0] = 0;
4935 c = 0xc0;
4936 }
4937 else
4938 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4939# endif
4940 }
4941 else
4942 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004943 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004944 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004945 }
4946#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004949#ifdef FEAT_CONCEAL
4950 /* In the cursor line and we may be concealing characters: correct
4951 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004952 if (!did_wcol && draw_state == WL_LINE
4953 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004954 && conceal_cursor_line(wp)
4955 && (int)wp->w_virtcol <= vcol + n_skip)
4956 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004957# ifdef FEAT_RIGHTLEFT
4958 if (wp->w_p_rl)
4959 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4960 else
4961# endif
4962 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004963 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004964 did_wcol = TRUE;
4965 }
4966#endif
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 /* Don't override visual selection highlighting. */
4969 if (n_attr > 0
4970 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004971 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 char_attr = extra_attr;
4973
Bram Moolenaar81695252004-12-29 20:58:21 +00004974#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 /* XIM don't send preedit_start and preedit_end, but they send
4976 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4977 * im_is_preediting() here. */
4978 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004979 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 && (State & INSERT)
4981 && !p_imdisable
4982 && im_is_preediting()
4983 && draw_state == WL_LINE)
4984 {
4985 colnr_T tcol;
4986
4987 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004988 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else
4990 tcol = preedit_end_col;
4991 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4992 {
4993 if (feedback_old_attr < 0)
4994 {
4995 feedback_col = 0;
4996 feedback_old_attr = char_attr;
4997 }
4998 char_attr = im_get_feedback_attr(feedback_col);
4999 if (char_attr < 0)
5000 char_attr = feedback_old_attr;
5001 feedback_col++;
5002 }
5003 else if (feedback_old_attr >= 0)
5004 {
5005 char_attr = feedback_old_attr;
5006 feedback_old_attr = -1;
5007 feedback_col = 0;
5008 }
5009 }
5010#endif
5011 /*
5012 * Handle the case where we are in column 0 but not on the first
5013 * character of the line and the user wants us to show us a
5014 * special character (via 'listchars' option "precedes:<char>".
5015 */
5016 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005017 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5019#ifdef FEAT_DIFF
5020 && filler_todo <= 0
5021#endif
5022 && draw_state > WL_NR
5023 && c != NUL)
5024 {
5025 c = lcs_prec;
5026 lcs_prec_todo = NUL;
5027#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005028 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5029 {
5030 /* Double-width character being overwritten by the "precedes"
5031 * character, need to fill up half the character. */
5032 c_extra = MB_FILLER_CHAR;
5033 n_extra = 1;
5034 n_attr = 2;
5035 extra_attr = hl_attr(HLF_AT);
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 mb_c = c;
5038 if (enc_utf8 && (*mb_char2len)(c) > 1)
5039 {
5040 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005041 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005042 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 else
5045 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5046#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005047 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 saved_attr3 = char_attr; /* save current attr */
5050 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5051 n_attr3 = 1;
5052 }
5053 }
5054
5055 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005056 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005058 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005059#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005060 || did_line_attr == 1
5061#endif
5062 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005064#ifdef FEAT_SEARCH_EXTRA
5065 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005066
5067 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005068 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005069 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005070#endif
5071
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005072 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * highlight match at end of line. If it's beyond the last
5074 * char on the screen, just overwrite that one (tricky!) Not
5075 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005076#ifdef FEAT_SEARCH_EXTRA
5077 prevcol_hl_flag = FALSE;
5078 if (prevcol == (long)search_hl.startcol)
5079 prevcol_hl_flag = TRUE;
5080 else
5081 {
5082 cur = wp->w_match_head;
5083 while (cur != NULL)
5084 {
5085 if (prevcol == (long)cur->hl.startcol)
5086 {
5087 prevcol_hl_flag = TRUE;
5088 break;
5089 }
5090 cur = cur->next;
5091 }
5092 }
5093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005095 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005096 && (VIsual_mode != Ctrl_V
5097 || lnum == VIsual.lnum
5098 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005099 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100#ifdef FEAT_SEARCH_EXTRA
5101 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005102 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005103# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005104 && did_line_attr <= 1
5105# endif
5106 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107#endif
5108 ))
5109 {
5110 int n = 0;
5111
5112#ifdef FEAT_RIGHTLEFT
5113 if (wp->w_p_rl)
5114 {
5115 if (col < 0)
5116 n = 1;
5117 }
5118 else
5119#endif
5120 {
5121 if (col >= W_WIDTH(wp))
5122 n = -1;
5123 }
5124 if (n != 0)
5125 {
5126 /* At the window boundary, highlight the last character
5127 * instead (better than nothing). */
5128 off += n;
5129 col += n;
5130 }
5131 else
5132 {
5133 /* Add a blank character to highlight. */
5134 ScreenLines[off] = ' ';
5135#ifdef FEAT_MBYTE
5136 if (enc_utf8)
5137 ScreenLinesUC[off] = 0;
5138#endif
5139 }
5140#ifdef FEAT_SEARCH_EXTRA
5141 if (area_attr == 0)
5142 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005143 /* Use attributes from match with highest priority among
5144 * 'search_hl' and the match list. */
5145 char_attr = search_hl.attr;
5146 cur = wp->w_match_head;
5147 shl_flag = FALSE;
5148 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005149 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005150 if (shl_flag == FALSE
5151 && ((cur != NULL
5152 && cur->priority > SEARCH_HL_PRIORITY)
5153 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005154 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005155 shl = &search_hl;
5156 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005157 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005158 else
5159 shl = &cur->hl;
5160 if ((ptr - line) - 1 == (long)shl->startcol)
5161 char_attr = shl->attr;
5162 if (shl != &search_hl && cur != NULL)
5163 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166#endif
5167 ScreenAttrs[off] = char_attr;
5168#ifdef FEAT_RIGHTLEFT
5169 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005172 --off;
5173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 else
5175#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005178 ++off;
5179 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005180 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005181#ifdef FEAT_SYN_HL
5182 eol_hl_off = 1;
5183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaar91170f82006-05-05 21:15:17 +00005187 /*
5188 * At end of the text line.
5189 */
5190 if (c == NUL)
5191 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005192#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005193 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5194 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005195 {
5196 /* highlight last char after line */
5197 --col;
5198 --off;
5199 --vcol;
5200 }
5201
Bram Moolenaar1a384422010-07-14 19:53:30 +02005202 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005203 if (wp->w_p_wrap)
5204 v = wp->w_skipcol;
5205 else
5206 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005207
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005208 /* check if line ends before left margin */
5209 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005210 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005211#ifdef FEAT_CONCEAL
5212 /* Get rid of the boguscols now, we want to draw until the right
5213 * edge for 'cursorcolumn'. */
5214 col -= boguscols;
5215 boguscols = 0;
5216#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005217
5218 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005219 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005220
5221 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005222 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5223 && (int)wp->w_virtcol <
5224 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005225 && lnum != wp->w_cursor.lnum)
5226 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005227# ifdef FEAT_RIGHTLEFT
5228 && !wp->w_p_rl
5229# endif
5230 )
5231 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005232 int rightmost_vcol = 0;
5233 int i;
5234
5235 if (wp->w_p_cuc)
5236 rightmost_vcol = wp->w_virtcol;
5237 if (draw_color_col)
5238 /* determine rightmost colorcolumn to possibly draw */
5239 for (i = 0; color_cols[i] >= 0; ++i)
5240 if (rightmost_vcol < color_cols[i])
5241 rightmost_vcol = color_cols[i];
5242
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005243 while (col < W_WIDTH(wp))
5244 {
5245 ScreenLines[off] = ' ';
5246#ifdef FEAT_MBYTE
5247 if (enc_utf8)
5248 ScreenLinesUC[off] = 0;
5249#endif
5250 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005251 if (draw_color_col)
5252 draw_color_col = advance_color_col(VCOL_HLC,
5253 &color_cols);
5254
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005255 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005256 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005257 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005258 ScreenAttrs[off++] = hl_attr(HLF_MC);
5259 else
5260 ScreenAttrs[off++] = 0;
5261
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005262 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005263 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005264
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005265 ++vcol;
5266 }
5267 }
5268#endif
5269
Bram Moolenaar860cae12010-06-05 23:22:07 +02005270 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5271 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 row++;
5273
5274 /*
5275 * Update w_cline_height and w_cline_folded if the cursor line was
5276 * updated (saves a call to plines() later).
5277 */
5278 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5279 {
5280 curwin->w_cline_row = startrow;
5281 curwin->w_cline_height = row - startrow;
5282#ifdef FEAT_FOLDING
5283 curwin->w_cline_folded = FALSE;
5284#endif
5285 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5286 }
5287
5288 break;
5289 }
5290
5291 /* line continues beyond line end */
5292 if (lcs_ext
5293 && !wp->w_p_wrap
5294#ifdef FEAT_DIFF
5295 && filler_todo <= 0
5296#endif
5297 && (
5298#ifdef FEAT_RIGHTLEFT
5299 wp->w_p_rl ? col == 0 :
5300#endif
5301 col == W_WIDTH(wp) - 1)
5302 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005303 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5305 {
5306 c = lcs_ext;
5307 char_attr = hl_attr(HLF_AT);
5308#ifdef FEAT_MBYTE
5309 mb_c = c;
5310 if (enc_utf8 && (*mb_char2len)(c) > 1)
5311 {
5312 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005313 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005314 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 else
5317 mb_utf8 = FALSE;
5318#endif
5319 }
5320
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005321#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005322 /* advance to the next 'colorcolumn' */
5323 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005324 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005325
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005326 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005327 * highlight the cursor position itself.
5328 * Also highlight the 'colorcolumn' if it is different than
5329 * 'cursorcolumn' */
5330 vcol_save_attr = -1;
5331 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005332 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005333 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005334 && lnum != wp->w_cursor.lnum)
5335 {
5336 vcol_save_attr = char_attr;
5337 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5338 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005339 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005340 {
5341 vcol_save_attr = char_attr;
5342 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5343 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005344 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005345#endif
5346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 /*
5348 * Store character to be displayed.
5349 * Skip characters that are left of the screen for 'nowrap'.
5350 */
5351 vcol_prev = vcol;
5352 if (draw_state < WL_LINE || n_skip <= 0)
5353 {
5354 /*
5355 * Store the character.
5356 */
5357#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5358 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5359 {
5360 /* A double-wide character is: put first halve in left cell. */
5361 --off;
5362 --col;
5363 }
5364#endif
5365 ScreenLines[off] = c;
5366#ifdef FEAT_MBYTE
5367 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005368 {
5369 if ((mb_c & 0xff00) == 0x8e00)
5370 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else if (enc_utf8)
5374 {
5375 if (mb_utf8)
5376 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005377 int i;
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005380 if ((c & 0xff) == 0)
5381 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005382 for (i = 0; i < Screen_mco; ++i)
5383 {
5384 ScreenLinesC[i][off] = u8cc[i];
5385 if (u8cc[i] == 0)
5386 break;
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 else
5390 ScreenLinesUC[off] = 0;
5391 }
5392 if (multi_attr)
5393 {
5394 ScreenAttrs[off] = multi_attr;
5395 multi_attr = 0;
5396 }
5397 else
5398#endif
5399 ScreenAttrs[off] = char_attr;
5400
5401#ifdef FEAT_MBYTE
5402 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5403 {
5404 /* Need to fill two screen columns. */
5405 ++off;
5406 ++col;
5407 if (enc_utf8)
5408 /* UTF-8: Put a 0 in the second screen char. */
5409 ScreenLines[off] = 0;
5410 else
5411 /* DBCS: Put second byte in the second screen char. */
5412 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005413 if (draw_state > WL_NR
5414#ifdef FEAT_DIFF
5415 && filler_todo <= 0
5416#endif
5417 )
5418 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 /* When "tocol" is halfway a character, set it to the end of
5420 * the character, otherwise highlighting won't stop. */
5421 if (tocol == vcol)
5422 ++tocol;
5423#ifdef FEAT_RIGHTLEFT
5424 if (wp->w_p_rl)
5425 {
5426 /* now it's time to backup one cell */
5427 --off;
5428 --col;
5429 }
5430#endif
5431 }
5432#endif
5433#ifdef FEAT_RIGHTLEFT
5434 if (wp->w_p_rl)
5435 {
5436 --off;
5437 --col;
5438 }
5439 else
5440#endif
5441 {
5442 ++off;
5443 ++col;
5444 }
5445 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005447 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005448 {
5449 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005450 ++vcol_off;
5451 if (n_extra > 0)
5452 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005453 if (wp->w_p_wrap)
5454 {
5455 /*
5456 * Special voodoo required if 'wrap' is on.
5457 *
5458 * Advance the column indicator to force the line
5459 * drawing to wrap early. This will make the line
5460 * take up the same screen space when parts are concealed,
5461 * so that cursor line computations aren't messed up.
5462 *
5463 * To avoid the fictitious advance of 'col' causing
5464 * trailing junk to be written out of the screen line
5465 * we are building, 'boguscols' keeps track of the number
5466 * of bad columns we have advanced.
5467 */
5468 if (n_extra > 0)
5469 {
5470 vcol += n_extra;
5471# ifdef FEAT_RIGHTLEFT
5472 if (wp->w_p_rl)
5473 {
5474 col -= n_extra;
5475 boguscols -= n_extra;
5476 }
5477 else
5478# endif
5479 {
5480 col += n_extra;
5481 boguscols += n_extra;
5482 }
5483 n_extra = 0;
5484 n_attr = 0;
5485 }
5486
5487
5488# ifdef FEAT_MBYTE
5489 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5490 {
5491 /* Need to fill two screen columns. */
5492# ifdef FEAT_RIGHTLEFT
5493 if (wp->w_p_rl)
5494 {
5495 --boguscols;
5496 --col;
5497 }
5498 else
5499# endif
5500 {
5501 ++boguscols;
5502 ++col;
5503 }
5504 }
5505# endif
5506
5507# ifdef FEAT_RIGHTLEFT
5508 if (wp->w_p_rl)
5509 {
5510 --boguscols;
5511 --col;
5512 }
5513 else
5514# endif
5515 {
5516 ++boguscols;
5517 ++col;
5518 }
5519 }
5520 else
5521 {
5522 if (n_extra > 0)
5523 {
5524 vcol += n_extra;
5525 n_extra = 0;
5526 n_attr = 0;
5527 }
5528 }
5529
5530 }
5531#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 else
5533 --n_skip;
5534
Bram Moolenaar64486672010-05-16 15:46:46 +02005535 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5536 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005537 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#ifdef FEAT_DIFF
5539 && filler_todo <= 0
5540#endif
5541 )
5542 ++vcol;
5543
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005544#ifdef FEAT_SYN_HL
5545 if (vcol_save_attr >= 0)
5546 char_attr = vcol_save_attr;
5547#endif
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 /* restore attributes after "predeces" in 'listchars' */
5550 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5551 char_attr = saved_attr3;
5552
5553 /* restore attributes after last 'listchars' or 'number' char */
5554 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5555 char_attr = saved_attr2;
5556
5557 /*
5558 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005559 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 */
5561 if ((
5562#ifdef FEAT_RIGHTLEFT
5563 wp->w_p_rl ? (col < 0) :
5564#endif
5565 (col >= W_WIDTH(wp)))
5566 && (*ptr != NUL
5567#ifdef FEAT_DIFF
5568 || filler_todo > 0
5569#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005570 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5572 )
5573 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005574#ifdef FEAT_CONCEAL
5575 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5576 (int)W_WIDTH(wp), wp->w_p_rl);
5577 boguscols = 0;
5578#else
5579 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5580 (int)W_WIDTH(wp), wp->w_p_rl);
5581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 ++row;
5583 ++screen_row;
5584
5585 /* When not wrapping and finished diff lines, or when displayed
5586 * '$' and highlighting until last column, break here. */
5587 if ((!wp->w_p_wrap
5588#ifdef FEAT_DIFF
5589 && filler_todo <= 0
5590#endif
5591 ) || lcs_eol_one == -1)
5592 break;
5593
5594 /* When the window is too narrow draw all "@" lines. */
5595 if (draw_state != WL_LINE
5596#ifdef FEAT_DIFF
5597 && filler_todo <= 0
5598#endif
5599 )
5600 {
5601 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5602#ifdef FEAT_VERTSPLIT
5603 draw_vsep_win(wp, row);
5604#endif
5605 row = endrow;
5606 }
5607
5608 /* When line got too long for screen break here. */
5609 if (row == endrow)
5610 {
5611 ++row;
5612 break;
5613 }
5614
5615 if (screen_cur_row == screen_row - 1
5616#ifdef FEAT_DIFF
5617 && filler_todo <= 0
5618#endif
5619 && W_WIDTH(wp) == Columns)
5620 {
5621 /* Remember that the line wraps, used for modeless copy. */
5622 LineWraps[screen_row - 1] = TRUE;
5623
5624 /*
5625 * Special trick to make copy/paste of wrapped lines work with
5626 * xterm/screen: write an extra character beyond the end of
5627 * the line. This will work with all terminal types
5628 * (regardless of the xn,am settings).
5629 * Only do this on a fast tty.
5630 * Only do this if the cursor is on the current line
5631 * (something has been written in it).
5632 * Don't do this for the GUI.
5633 * Don't do this for double-width characters.
5634 * Don't do this for a window not at the right screen border.
5635 */
5636 if (p_tf
5637#ifdef FEAT_GUI
5638 && !gui.in_use
5639#endif
5640#ifdef FEAT_MBYTE
5641 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005642 && ((*mb_off2cells)(LineOffset[screen_row],
5643 LineOffset[screen_row] + screen_Columns)
5644 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005646 + (int)Columns - 2,
5647 LineOffset[screen_row] + screen_Columns)
5648 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#endif
5650 )
5651 {
5652 /* First make sure we are at the end of the screen line,
5653 * then output the same character again to let the
5654 * terminal know about the wrap. If the terminal doesn't
5655 * auto-wrap, we overwrite the character. */
5656 if (screen_cur_col != W_WIDTH(wp))
5657 screen_char(LineOffset[screen_row - 1]
5658 + (unsigned)Columns - 1,
5659 screen_row - 1, (int)(Columns - 1));
5660
5661#ifdef FEAT_MBYTE
5662 /* When there is a multi-byte character, just output a
5663 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005664 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5665 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 out_char(' ');
5667 else
5668#endif
5669 out_char(ScreenLines[LineOffset[screen_row - 1]
5670 + (Columns - 1)]);
5671 /* force a redraw of the first char on the next line */
5672 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5673 screen_start(); /* don't know where cursor is now */
5674 }
5675 }
5676
5677 col = 0;
5678 off = (unsigned)(current_ScreenLine - ScreenLines);
5679#ifdef FEAT_RIGHTLEFT
5680 if (wp->w_p_rl)
5681 {
5682 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5683 off += col;
5684 }
5685#endif
5686
5687 /* reset the drawing state for the start of a wrapped line */
5688 draw_state = WL_START;
5689 saved_n_extra = n_extra;
5690 saved_p_extra = p_extra;
5691 saved_c_extra = c_extra;
5692 saved_char_attr = char_attr;
5693 n_extra = 0;
5694 lcs_prec_todo = lcs_prec;
5695#ifdef FEAT_LINEBREAK
5696# ifdef FEAT_DIFF
5697 if (filler_todo <= 0)
5698# endif
5699 need_showbreak = TRUE;
5700#endif
5701#ifdef FEAT_DIFF
5702 --filler_todo;
5703 /* When the filler lines are actually below the last line of the
5704 * file, don't draw the line itself, break here. */
5705 if (filler_todo == 0 && wp->w_botfill)
5706 break;
5707#endif
5708 }
5709
5710 } /* for every character in the line */
5711
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005712#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005713 /* After an empty line check first word for capital. */
5714 if (*skipwhite(line) == NUL)
5715 {
5716 capcol_lnum = lnum + 1;
5717 cap_col = 0;
5718 }
5719#endif
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 return row;
5722}
5723
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005724#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005725static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005726
5727/*
5728 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005729 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005730 */
5731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005732comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005733{
5734 int i;
5735
5736 for (i = 0; i < Screen_mco; ++i)
5737 {
5738 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5739 return TRUE;
5740 if (ScreenLinesC[i][off_from] == 0)
5741 break;
5742 }
5743 return FALSE;
5744}
5745#endif
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747/*
5748 * Check whether the given character needs redrawing:
5749 * - the (first byte of the) character is different
5750 * - the attributes are different
5751 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005752 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 */
5754 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005755char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 if (cols > 0
5758 && ((ScreenLines[off_from] != ScreenLines[off_to]
5759 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5760
5761#ifdef FEAT_MBYTE
5762 || (enc_dbcs != 0
5763 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5764 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5765 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5766 : (cols > 1 && ScreenLines[off_from + 1]
5767 != ScreenLines[off_to + 1])))
5768 || (enc_utf8
5769 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5770 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005771 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005772 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5773 && ScreenLines[off_from + 1]
5774 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#endif
5776 ))
5777 return TRUE;
5778 return FALSE;
5779}
5780
5781/*
5782 * Move one "cooked" screen line to the screen, but only the characters that
5783 * have actually changed. Handle insert/delete character.
5784 * "coloff" gives the first column on the screen for this line.
5785 * "endcol" gives the columns where valid characters are.
5786 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5787 * needs to be cleared, negative otherwise.
5788 * "rlflag" is TRUE in a rightleft window:
5789 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5790 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5791 */
5792 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005793screen_line(
5794 int row,
5795 int coloff,
5796 int endcol,
5797 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005799 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 unsigned off_from;
5804 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005805#ifdef FEAT_MBYTE
5806 unsigned max_off_from;
5807 unsigned max_off_to;
5808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 int col = 0;
5810#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5811 int hl;
5812#endif
5813 int force = FALSE; /* force update rest of the line */
5814 int redraw_this /* bool: does character need redraw? */
5815#ifdef FEAT_GUI
5816 = TRUE /* For GUI when while-loop empty */
5817#endif
5818 ;
5819 int redraw_next; /* redraw_this for next character */
5820#ifdef FEAT_MBYTE
5821 int clear_next = FALSE;
5822 int char_cells; /* 1: normal char */
5823 /* 2: occupies two display cells */
5824# define CHAR_CELLS char_cells
5825#else
5826# define CHAR_CELLS 1
5827#endif
5828
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005829 /* Check for illegal row and col, just in case. */
5830 if (row >= Rows)
5831 row = Rows - 1;
5832 if (endcol > Columns)
5833 endcol = Columns;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835# ifdef FEAT_CLIPBOARD
5836 clip_may_clear_selection(row, row);
5837# endif
5838
5839 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5840 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005841#ifdef FEAT_MBYTE
5842 max_off_from = off_from + screen_Columns;
5843 max_off_to = LineOffset[row] + screen_Columns;
5844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846#ifdef FEAT_RIGHTLEFT
5847 if (rlflag)
5848 {
5849 /* Clear rest first, because it's left of the text. */
5850 if (clear_width > 0)
5851 {
5852 while (col <= endcol && ScreenLines[off_to] == ' '
5853 && ScreenAttrs[off_to] == 0
5854# ifdef FEAT_MBYTE
5855 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5856# endif
5857 )
5858 {
5859 ++off_to;
5860 ++col;
5861 }
5862 if (col <= endcol)
5863 screen_fill(row, row + 1, col + coloff,
5864 endcol + coloff + 1, ' ', ' ', 0);
5865 }
5866 col = endcol + 1;
5867 off_to = LineOffset[row] + col + coloff;
5868 off_from += col;
5869 endcol = (clear_width > 0 ? clear_width : -clear_width);
5870 }
5871#endif /* FEAT_RIGHTLEFT */
5872
5873 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5874
5875 while (col < endcol)
5876 {
5877#ifdef FEAT_MBYTE
5878 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005879 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 else
5881 char_cells = 1;
5882#endif
5883
5884 redraw_this = redraw_next;
5885 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5886 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5887
5888#ifdef FEAT_GUI
5889 /* If the next character was bold, then redraw the current character to
5890 * remove any pixels that might have spilt over into us. This only
5891 * happens in the GUI.
5892 */
5893 if (redraw_next && gui.in_use)
5894 {
5895 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005896 if (hl > HL_ALL)
5897 hl = syn_attr2attr(hl);
5898 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 redraw_this = TRUE;
5900 }
5901#endif
5902
5903 if (redraw_this)
5904 {
5905 /*
5906 * Special handling when 'xs' termcap flag set (hpterm):
5907 * Attributes for characters are stored at the position where the
5908 * cursor is when writing the highlighting code. The
5909 * start-highlighting code must be written with the cursor on the
5910 * first highlighted character. The stop-highlighting code must
5911 * be written with the cursor just after the last highlighted
5912 * character.
5913 * Overwriting a character doesn't remove it's highlighting. Need
5914 * to clear the rest of the line, and force redrawing it
5915 * completely.
5916 */
5917 if ( p_wiv
5918 && !force
5919#ifdef FEAT_GUI
5920 && !gui.in_use
5921#endif
5922 && ScreenAttrs[off_to] != 0
5923 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5924 {
5925 /*
5926 * Need to remove highlighting attributes here.
5927 */
5928 windgoto(row, col + coloff);
5929 out_str(T_CE); /* clear rest of this screen line */
5930 screen_start(); /* don't know where cursor is now */
5931 force = TRUE; /* force redraw of rest of the line */
5932 redraw_next = TRUE; /* or else next char would miss out */
5933
5934 /*
5935 * If the previous character was highlighted, need to stop
5936 * highlighting at this character.
5937 */
5938 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5939 {
5940 screen_attr = ScreenAttrs[off_to - 1];
5941 term_windgoto(row, col + coloff);
5942 screen_stop_highlight();
5943 }
5944 else
5945 screen_attr = 0; /* highlighting has stopped */
5946 }
5947#ifdef FEAT_MBYTE
5948 if (enc_dbcs != 0)
5949 {
5950 /* Check if overwriting a double-byte with a single-byte or
5951 * the other way around requires another character to be
5952 * redrawn. For UTF-8 this isn't needed, because comparing
5953 * ScreenLinesUC[] is sufficient. */
5954 if (char_cells == 1
5955 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005956 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
5958 /* Writing a single-cell character over a double-cell
5959 * character: need to redraw the next cell. */
5960 ScreenLines[off_to + 1] = 0;
5961 redraw_next = TRUE;
5962 }
5963 else if (char_cells == 2
5964 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005965 && (*mb_off2cells)(off_to, max_off_to) == 1
5966 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 /* Writing the second half of a double-cell character over
5969 * a double-cell character: need to redraw the second
5970 * cell. */
5971 ScreenLines[off_to + 2] = 0;
5972 redraw_next = TRUE;
5973 }
5974
5975 if (enc_dbcs == DBCS_JPNU)
5976 ScreenLines2[off_to] = ScreenLines2[off_from];
5977 }
5978 /* When writing a single-width character over a double-width
5979 * character and at the end of the redrawn text, need to clear out
5980 * the right halve of the old character.
5981 * Also required when writing the right halve of a double-width
5982 * char over the left halve of an existing one. */
5983 if (has_mbyte && col + char_cells == endcol
5984 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005985 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005987 && (*mb_off2cells)(off_to, max_off_to) == 1
5988 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 clear_next = TRUE;
5990#endif
5991
5992 ScreenLines[off_to] = ScreenLines[off_from];
5993#ifdef FEAT_MBYTE
5994 if (enc_utf8)
5995 {
5996 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5997 if (ScreenLinesUC[off_from] != 0)
5998 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005999 int i;
6000
6001 for (i = 0; i < Screen_mco; ++i)
6002 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
6004 }
6005 if (char_cells == 2)
6006 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6007#endif
6008
6009#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006010 /* The bold trick makes a single column of pixels appear in the
6011 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 * character should be redrawn too. This happens for our own GUI
6013 * and for some xterms. */
6014 if (
6015# ifdef FEAT_GUI
6016 gui.in_use
6017# endif
6018# if defined(FEAT_GUI) && defined(UNIX)
6019 ||
6020# endif
6021# ifdef UNIX
6022 term_is_xterm
6023# endif
6024 )
6025 {
6026 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006027 if (hl > HL_ALL)
6028 hl = syn_attr2attr(hl);
6029 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 redraw_next = TRUE;
6031 }
6032#endif
6033 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6034#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006035 /* For simplicity set the attributes of second half of a
6036 * double-wide character equal to the first half. */
6037 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006039
6040 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 else
6043#endif
6044 screen_char(off_to, row, col + coloff);
6045 }
6046 else if ( p_wiv
6047#ifdef FEAT_GUI
6048 && !gui.in_use
6049#endif
6050 && col + coloff > 0)
6051 {
6052 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6053 {
6054 /*
6055 * Don't output stop-highlight when moving the cursor, it will
6056 * stop the highlighting when it should continue.
6057 */
6058 screen_attr = 0;
6059 }
6060 else if (screen_attr != 0)
6061 screen_stop_highlight();
6062 }
6063
6064 off_to += CHAR_CELLS;
6065 off_from += CHAR_CELLS;
6066 col += CHAR_CELLS;
6067 }
6068
6069#ifdef FEAT_MBYTE
6070 if (clear_next)
6071 {
6072 /* Clear the second half of a double-wide character of which the left
6073 * half was overwritten with a single-wide character. */
6074 ScreenLines[off_to] = ' ';
6075 if (enc_utf8)
6076 ScreenLinesUC[off_to] = 0;
6077 screen_char(off_to, row, col + coloff);
6078 }
6079#endif
6080
6081 if (clear_width > 0
6082#ifdef FEAT_RIGHTLEFT
6083 && !rlflag
6084#endif
6085 )
6086 {
6087#ifdef FEAT_GUI
6088 int startCol = col;
6089#endif
6090
6091 /* blank out the rest of the line */
6092 while (col < clear_width && ScreenLines[off_to] == ' '
6093 && ScreenAttrs[off_to] == 0
6094#ifdef FEAT_MBYTE
6095 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6096#endif
6097 )
6098 {
6099 ++off_to;
6100 ++col;
6101 }
6102 if (col < clear_width)
6103 {
6104#ifdef FEAT_GUI
6105 /*
6106 * In the GUI, clearing the rest of the line may leave pixels
6107 * behind if the first character cleared was bold. Some bold
6108 * fonts spill over the left. In this case we redraw the previous
6109 * character too. If we didn't skip any blanks above, then we
6110 * only redraw if the character wasn't already redrawn anyway.
6111 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006112 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 hl = ScreenAttrs[off_to];
6115 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006116 {
6117 int prev_cells = 1;
6118# ifdef FEAT_MBYTE
6119 if (enc_utf8)
6120 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6121 * that its width is 2. */
6122 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6123 else if (enc_dbcs != 0)
6124 {
6125 /* find previous character by counting from first
6126 * column and get its width. */
6127 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006128 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006129
6130 while (off < off_to)
6131 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006132 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006133 off += prev_cells;
6134 }
6135 }
6136
6137 if (enc_dbcs != 0 && prev_cells > 1)
6138 screen_char_2(off_to - prev_cells, row,
6139 col + coloff - prev_cells);
6140 else
6141# endif
6142 screen_char(off_to - prev_cells, row,
6143 col + coloff - prev_cells);
6144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146#endif
6147 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6148 ' ', ' ', 0);
6149#ifdef FEAT_VERTSPLIT
6150 off_to += clear_width - col;
6151 col = clear_width;
6152#endif
6153 }
6154 }
6155
6156 if (clear_width > 0)
6157 {
6158#ifdef FEAT_VERTSPLIT
6159 /* For a window that's left of another, draw the separator char. */
6160 if (col + coloff < Columns)
6161 {
6162 int c;
6163
6164 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006165 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006167 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6168 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169# endif
6170 || ScreenAttrs[off_to] != hl)
6171 {
6172 ScreenLines[off_to] = c;
6173 ScreenAttrs[off_to] = hl;
6174# ifdef FEAT_MBYTE
6175 if (enc_utf8)
6176 {
6177 if (c >= 0x80)
6178 {
6179 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006180 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182 else
6183 ScreenLinesUC[off_to] = 0;
6184 }
6185# endif
6186 screen_char(off_to, row, col + coloff);
6187 }
6188 }
6189 else
6190#endif
6191 LineWraps[row] = FALSE;
6192 }
6193}
6194
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006195#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006197 * Mirror text "str" for right-left displaying.
6198 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006200 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006201rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 char_u *p1, *p2;
6204 int t;
6205
6206 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6207 {
6208 t = *p1;
6209 *p1 = *p2;
6210 *p2 = t;
6211 }
6212}
6213#endif
6214
6215#if defined(FEAT_WINDOWS) || defined(PROTO)
6216/*
6217 * mark all status lines for redraw; used after first :cd
6218 */
6219 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006220status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 win_T *wp;
6223
6224 for (wp = firstwin; wp; wp = wp->w_next)
6225 if (wp->w_status_height)
6226 {
6227 wp->w_redr_status = TRUE;
6228 redraw_later(VALID);
6229 }
6230}
6231
6232/*
6233 * mark all status lines of the current buffer for redraw
6234 */
6235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006236status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 win_T *wp;
6239
6240 for (wp = firstwin; wp; wp = wp->w_next)
6241 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6242 {
6243 wp->w_redr_status = TRUE;
6244 redraw_later(VALID);
6245 }
6246}
6247
6248/*
6249 * Redraw all status lines that need to be redrawn.
6250 */
6251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006252redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 win_T *wp;
6255
6256 for (wp = firstwin; wp; wp = wp->w_next)
6257 if (wp->w_redr_status)
6258 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006259 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006260 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262#endif
6263
6264#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6265/*
6266 * Redraw all status lines at the bottom of frame "frp".
6267 */
6268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006269win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 if (frp->fr_layout == FR_LEAF)
6272 frp->fr_win->w_redr_status = TRUE;
6273 else if (frp->fr_layout == FR_ROW)
6274 {
6275 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6276 win_redraw_last_status(frp);
6277 }
6278 else /* frp->fr_layout == FR_COL */
6279 {
6280 frp = frp->fr_child;
6281 while (frp->fr_next != NULL)
6282 frp = frp->fr_next;
6283 win_redraw_last_status(frp);
6284 }
6285}
6286#endif
6287
6288#ifdef FEAT_VERTSPLIT
6289/*
6290 * Draw the verticap separator right of window "wp" starting with line "row".
6291 */
6292 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006293draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 int hl;
6296 int c;
6297
6298 if (wp->w_vsep_width)
6299 {
6300 /* draw the vertical separator right of this window */
6301 c = fillchar_vsep(&hl);
6302 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6303 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6304 c, ' ', hl);
6305 }
6306}
6307#endif
6308
6309#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006310static int status_match_len(expand_T *xp, char_u *s);
6311static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006314 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 */
6316 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006317status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 int len = 0;
6320
6321#ifdef FEAT_MENU
6322 int emenu = (xp->xp_context == EXPAND_MENUS
6323 || xp->xp_context == EXPAND_MENUNAMES);
6324
6325 /* Check for menu separators - replace with '|'. */
6326 if (emenu && menu_is_separator(s))
6327 return 1;
6328#endif
6329
6330 while (*s != NUL)
6331 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006332 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006333 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006334 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336
6337 return len;
6338}
6339
6340/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006341 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006342 * These are backslashes used for escaping. Do show backslashes in help tags.
6343 */
6344 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006345skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006346{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006347 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006348#ifdef FEAT_MENU
6349 || ((xp->xp_context == EXPAND_MENUS
6350 || xp->xp_context == EXPAND_MENUNAMES)
6351 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6352#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006353 )
6354 {
6355#ifndef BACKSLASH_IN_FILENAME
6356 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6357 return 2;
6358#endif
6359 return 1;
6360 }
6361 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006362}
6363
6364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 * Show wildchar matches in the status line.
6366 * Show at least the "match" item.
6367 * We start at item 'first_match' in the list and show all matches that fit.
6368 *
6369 * If inversion is possible we use it. Else '=' characters are used.
6370 */
6371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006372win_redr_status_matches(
6373 expand_T *xp,
6374 int num_matches,
6375 char_u **matches, /* list of matches */
6376 int match,
6377 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6380 int row;
6381 char_u *buf;
6382 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006383 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 int fillchar;
6385 int attr;
6386 int i;
6387 int highlight = TRUE;
6388 char_u *selstart = NULL;
6389 int selstart_col = 0;
6390 char_u *selend = NULL;
6391 static int first_match = 0;
6392 int add_left = FALSE;
6393 char_u *s;
6394#ifdef FEAT_MENU
6395 int emenu;
6396#endif
6397#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6398 int l;
6399#endif
6400
6401 if (matches == NULL) /* interrupted completion? */
6402 return;
6403
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006404#ifdef FEAT_MBYTE
6405 if (has_mbyte)
6406 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6407 else
6408#endif
6409 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (buf == NULL)
6411 return;
6412
6413 if (match == -1) /* don't show match but original text */
6414 {
6415 match = 0;
6416 highlight = FALSE;
6417 }
6418 /* count 1 for the ending ">" */
6419 clen = status_match_len(xp, L_MATCH(match)) + 3;
6420 if (match == 0)
6421 first_match = 0;
6422 else if (match < first_match)
6423 {
6424 /* jumping left, as far as we can go */
6425 first_match = match;
6426 add_left = TRUE;
6427 }
6428 else
6429 {
6430 /* check if match fits on the screen */
6431 for (i = first_match; i < match; ++i)
6432 clen += status_match_len(xp, L_MATCH(i)) + 2;
6433 if (first_match > 0)
6434 clen += 2;
6435 /* jumping right, put match at the left */
6436 if ((long)clen > Columns)
6437 {
6438 first_match = match;
6439 /* if showing the last match, we can add some on the left */
6440 clen = 2;
6441 for (i = match; i < num_matches; ++i)
6442 {
6443 clen += status_match_len(xp, L_MATCH(i)) + 2;
6444 if ((long)clen >= Columns)
6445 break;
6446 }
6447 if (i == num_matches)
6448 add_left = TRUE;
6449 }
6450 }
6451 if (add_left)
6452 while (first_match > 0)
6453 {
6454 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6455 if ((long)clen >= Columns)
6456 break;
6457 --first_match;
6458 }
6459
6460 fillchar = fillchar_status(&attr, TRUE);
6461
6462 if (first_match == 0)
6463 {
6464 *buf = NUL;
6465 len = 0;
6466 }
6467 else
6468 {
6469 STRCPY(buf, "< ");
6470 len = 2;
6471 }
6472 clen = len;
6473
6474 i = first_match;
6475 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6476 {
6477 if (i == match)
6478 {
6479 selstart = buf + len;
6480 selstart_col = clen;
6481 }
6482
6483 s = L_MATCH(i);
6484 /* Check for menu separators - replace with '|' */
6485#ifdef FEAT_MENU
6486 emenu = (xp->xp_context == EXPAND_MENUS
6487 || xp->xp_context == EXPAND_MENUNAMES);
6488 if (emenu && menu_is_separator(s))
6489 {
6490 STRCPY(buf + len, transchar('|'));
6491 l = (int)STRLEN(buf + len);
6492 len += l;
6493 clen += l;
6494 }
6495 else
6496#endif
6497 for ( ; *s != NUL; ++s)
6498 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006499 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 clen += ptr2cells(s);
6501#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006502 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
6504 STRNCPY(buf + len, s, l);
6505 s += l - 1;
6506 len += l;
6507 }
6508 else
6509#endif
6510 {
6511 STRCPY(buf + len, transchar_byte(*s));
6512 len += (int)STRLEN(buf + len);
6513 }
6514 }
6515 if (i == match)
6516 selend = buf + len;
6517
6518 *(buf + len++) = ' ';
6519 *(buf + len++) = ' ';
6520 clen += 2;
6521 if (++i == num_matches)
6522 break;
6523 }
6524
6525 if (i != num_matches)
6526 {
6527 *(buf + len++) = '>';
6528 ++clen;
6529 }
6530
6531 buf[len] = NUL;
6532
6533 row = cmdline_row - 1;
6534 if (row >= 0)
6535 {
6536 if (wild_menu_showing == 0)
6537 {
6538 if (msg_scrolled > 0)
6539 {
6540 /* Put the wildmenu just above the command line. If there is
6541 * no room, scroll the screen one line up. */
6542 if (cmdline_row == Rows - 1)
6543 {
6544 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6545 ++msg_scrolled;
6546 }
6547 else
6548 {
6549 ++cmdline_row;
6550 ++row;
6551 }
6552 wild_menu_showing = WM_SCROLLED;
6553 }
6554 else
6555 {
6556 /* Create status line if needed by setting 'laststatus' to 2.
6557 * Set 'winminheight' to zero to avoid that the window is
6558 * resized. */
6559 if (lastwin->w_status_height == 0)
6560 {
6561 save_p_ls = p_ls;
6562 save_p_wmh = p_wmh;
6563 p_ls = 2;
6564 p_wmh = 0;
6565 last_status(FALSE);
6566 }
6567 wild_menu_showing = WM_SHOWN;
6568 }
6569 }
6570
6571 screen_puts(buf, row, 0, attr);
6572 if (selstart != NULL && highlight)
6573 {
6574 *selend = NUL;
6575 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6576 }
6577
6578 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6579 }
6580
6581#ifdef FEAT_VERTSPLIT
6582 win_redraw_last_status(topframe);
6583#else
6584 lastwin->w_redr_status = TRUE;
6585#endif
6586 vim_free(buf);
6587}
6588#endif
6589
6590#if defined(FEAT_WINDOWS) || defined(PROTO)
6591/*
6592 * Redraw the status line of window wp.
6593 *
6594 * If inversion is possible we use it. Else '=' characters are used.
6595 */
6596 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006597win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 int row;
6600 char_u *p;
6601 int len;
6602 int fillchar;
6603 int attr;
6604 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006605 static int busy = FALSE;
6606
6607 /* It's possible to get here recursively when 'statusline' (indirectly)
6608 * invokes ":redrawstatus". Simply ignore the call then. */
6609 if (busy)
6610 return;
6611 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613 wp->w_redr_status = FALSE;
6614 if (wp->w_status_height == 0)
6615 {
6616 /* no status line, can only be last window */
6617 redraw_cmdline = TRUE;
6618 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006619 else if (!redrawing()
6620#ifdef FEAT_INS_EXPAND
6621 /* don't update status line when popup menu is visible and may be
6622 * drawn over it */
6623 || pum_visible()
6624#endif
6625 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 {
6627 /* Don't redraw right now, do it later. */
6628 wp->w_redr_status = TRUE;
6629 }
6630#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006631 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
6633 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006634 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
6636#endif
6637 else
6638 {
6639 fillchar = fillchar_status(&attr, wp == curwin);
6640
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006641 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 p = NameBuff;
6643 len = (int)STRLEN(p);
6644
6645 if (wp->w_buffer->b_help
6646#ifdef FEAT_QUICKFIX
6647 || wp->w_p_pvw
6648#endif
6649 || bufIsChanged(wp->w_buffer)
6650 || wp->w_buffer->b_p_ro)
6651 *(p + len++) = ' ';
6652 if (wp->w_buffer->b_help)
6653 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006654 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 len += (int)STRLEN(p + len);
6656 }
6657#ifdef FEAT_QUICKFIX
6658 if (wp->w_p_pvw)
6659 {
6660 STRCPY(p + len, _("[Preview]"));
6661 len += (int)STRLEN(p + len);
6662 }
6663#endif
6664 if (bufIsChanged(wp->w_buffer))
6665 {
6666 STRCPY(p + len, "[+]");
6667 len += 3;
6668 }
6669 if (wp->w_buffer->b_p_ro)
6670 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006671 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 len += 4;
6673 }
6674
6675#ifndef FEAT_VERTSPLIT
6676 this_ru_col = ru_col;
6677 if (this_ru_col < (Columns + 1) / 2)
6678 this_ru_col = (Columns + 1) / 2;
6679#else
6680 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6681 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6682 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6683 if (this_ru_col <= 1)
6684 {
6685 p = (char_u *)"<"; /* No room for file name! */
6686 len = 1;
6687 }
6688 else
6689#endif
6690#ifdef FEAT_MBYTE
6691 if (has_mbyte)
6692 {
6693 int clen = 0, i;
6694
6695 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006696 clen = mb_string2cells(p, -1);
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /* Find first character that will fit.
6699 * Going from start to end is much faster for DBCS. */
6700 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006701 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 clen -= (*mb_ptr2cells)(p + i);
6703 len = clen;
6704 if (i > 0)
6705 {
6706 p = p + i - 1;
6707 *p = '<';
6708 ++len;
6709 }
6710
6711 }
6712 else
6713#endif
6714 if (len > this_ru_col - 1)
6715 {
6716 p += len - (this_ru_col - 1);
6717 *p = '<';
6718 len = this_ru_col - 1;
6719 }
6720
6721 row = W_WINROW(wp) + wp->w_height;
6722 screen_puts(p, row, W_WINCOL(wp), attr);
6723 screen_fill(row, row + 1, len + W_WINCOL(wp),
6724 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6725
6726 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6727 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6728 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6729 - 1 + W_WINCOL(wp)), attr);
6730
6731#ifdef FEAT_CMDL_INFO
6732 win_redr_ruler(wp, TRUE);
6733#endif
6734 }
6735
6736#ifdef FEAT_VERTSPLIT
6737 /*
6738 * May need to draw the character below the vertical separator.
6739 */
6740 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6741 {
6742 if (stl_connected(wp))
6743 fillchar = fillchar_status(&attr, wp == curwin);
6744 else
6745 fillchar = fillchar_vsep(&attr);
6746 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6747 attr);
6748 }
6749#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006750 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751}
6752
Bram Moolenaar238a5642006-02-21 22:12:05 +00006753#ifdef FEAT_STL_OPT
6754/*
6755 * Redraw the status line according to 'statusline' and take care of any
6756 * errors encountered.
6757 */
6758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006759redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006760{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006761 static int entered = FALSE;
6762 int save_called_emsg = called_emsg;
6763
6764 /* When called recursively return. This can happen when the statusline
6765 * contains an expression that triggers a redraw. */
6766 if (entered)
6767 return;
6768 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006769
6770 called_emsg = FALSE;
6771 win_redr_custom(wp, FALSE);
6772 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006773 {
6774 /* When there is an error disable the statusline, otherwise the
6775 * display is messed up with errors and a redraw triggers the problem
6776 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006777 set_string_option_direct((char_u *)"statusline", -1,
6778 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006779 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006780 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006781 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006782 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006783}
6784#endif
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786# ifdef FEAT_VERTSPLIT
6787/*
6788 * Return TRUE if the status line of window "wp" is connected to the status
6789 * line of the window right of it. If not, then it's a vertical separator.
6790 * Only call if (wp->w_vsep_width != 0).
6791 */
6792 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006793stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 frame_T *fr;
6796
6797 fr = wp->w_frame;
6798 while (fr->fr_parent != NULL)
6799 {
6800 if (fr->fr_parent->fr_layout == FR_COL)
6801 {
6802 if (fr->fr_next != NULL)
6803 break;
6804 }
6805 else
6806 {
6807 if (fr->fr_next != NULL)
6808 return TRUE;
6809 }
6810 fr = fr->fr_parent;
6811 }
6812 return FALSE;
6813}
6814# endif
6815
6816#endif /* FEAT_WINDOWS */
6817
6818#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6819/*
6820 * Get the value to show for the language mappings, active 'keymap'.
6821 */
6822 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006823get_keymap_str(
6824 win_T *wp,
6825 char_u *buf, /* buffer for the result */
6826 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 char_u *p;
6829
6830 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6831 return FALSE;
6832
6833 {
6834#ifdef FEAT_EVAL
6835 buf_T *old_curbuf = curbuf;
6836 win_T *old_curwin = curwin;
6837 char_u *s;
6838
6839 curbuf = wp->w_buffer;
6840 curwin = wp;
6841 STRCPY(buf, "b:keymap_name"); /* must be writable */
6842 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006843 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 --emsg_skip;
6845 curbuf = old_curbuf;
6846 curwin = old_curwin;
6847 if (p == NULL || *p == NUL)
6848#endif
6849 {
6850#ifdef FEAT_KEYMAP
6851 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6852 p = wp->w_buffer->b_p_keymap;
6853 else
6854#endif
6855 p = (char_u *)"lang";
6856 }
6857 if ((int)(STRLEN(p) + 3) < len)
6858 sprintf((char *)buf, "<%s>", p);
6859 else
6860 buf[0] = NUL;
6861#ifdef FEAT_EVAL
6862 vim_free(s);
6863#endif
6864 }
6865 return buf[0] != NUL;
6866}
6867#endif
6868
6869#if defined(FEAT_STL_OPT) || defined(PROTO)
6870/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006871 * Redraw the status line or ruler of window "wp".
6872 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 */
6874 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006875win_redr_custom(
6876 win_T *wp,
6877 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006879 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 int attr;
6881 int curattr;
6882 int row;
6883 int col = 0;
6884 int maxwidth;
6885 int width;
6886 int n;
6887 int len;
6888 int fillchar;
6889 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006890 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006892 struct stl_hlrec hltab[STL_MAX_ITEM];
6893 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006894 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006895 win_T *ewp;
6896 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar1d633412013-12-11 15:52:01 +01006898 /* There is a tiny chance that this gets called recursively: When
6899 * redrawing a status line triggers redrawing the ruler or tabline.
6900 * Avoid trouble by not allowing recursion. */
6901 if (entered)
6902 return;
6903 entered = TRUE;
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006906 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006908 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006909 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006910 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006911 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006912 attr = hl_attr(HLF_TPF);
6913 maxwidth = Columns;
6914# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006915 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006918 else
6919 {
6920 row = W_WINROW(wp) + wp->w_height;
6921 fillchar = fillchar_status(&attr, wp == curwin);
6922 maxwidth = W_WIDTH(wp);
6923
6924 if (draw_ruler)
6925 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006926 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006927 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006928 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006929 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006930 if (*++stl == '-')
6931 stl++;
6932 if (atoi((char *)stl))
6933 while (VIM_ISDIGIT(*stl))
6934 stl++;
6935 if (*stl++ != '(')
6936 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006937 }
6938#ifdef FEAT_VERTSPLIT
6939 col = ru_col - (Columns - W_WIDTH(wp));
6940 if (col < (W_WIDTH(wp) + 1) / 2)
6941 col = (W_WIDTH(wp) + 1) / 2;
6942#else
6943 col = ru_col;
6944 if (col > (Columns + 1) / 2)
6945 col = (Columns + 1) / 2;
6946#endif
6947 maxwidth = W_WIDTH(wp) - col;
6948#ifdef FEAT_WINDOWS
6949 if (!wp->w_status_height)
6950#endif
6951 {
6952 row = Rows - 1;
6953 --maxwidth; /* writing in last column may cause scrolling */
6954 fillchar = ' ';
6955 attr = 0;
6956 }
6957
6958# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006959 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006960# endif
6961 }
6962 else
6963 {
6964 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006965 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006966 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006967 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006969 use_sandbox = was_set_insecurely((char_u *)"statusline",
6970 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006971# endif
6972 }
6973
6974#ifdef FEAT_VERTSPLIT
6975 col += W_WINCOL(wp);
6976#endif
6977 }
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006980 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
Bram Moolenaar61452852011-02-01 18:01:11 +01006982 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6983 * the cursor away and back. */
6984 ewp = wp == NULL ? curwin : wp;
6985 p_crb_save = ewp->w_p_crb;
6986 ewp->w_p_crb = FALSE;
6987
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 /* Make a copy, because the statusline may include a function call that
6989 * might change the option value and free the memory. */
6990 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006991 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006992 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006993 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006994 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006995 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006997 /* Make all characters printable. */
6998 p = transstr(buf);
6999 if (p != NULL)
7000 {
7001 vim_strncpy(buf, p, sizeof(buf) - 1);
7002 vim_free(p);
7003 }
7004
7005 /* fill up with "fillchar" */
7006 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007007 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
7009#ifdef FEAT_MBYTE
7010 len += (*mb_char2bytes)(fillchar, buf + len);
7011#else
7012 buf[len++] = fillchar;
7013#endif
7014 ++width;
7015 }
7016 buf[len] = NUL;
7017
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007018 /*
7019 * Draw each snippet with the specified highlighting.
7020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 curattr = attr;
7022 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007023 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007025 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 screen_puts_len(p, len, row, col, curattr);
7027 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007030 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007032 else if (hltab[n].userhl < 0)
7033 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007035 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007036 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#endif
7038 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007039 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007042
7043 if (wp == NULL)
7044 {
7045 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7046 col = 0;
7047 len = 0;
7048 p = buf;
7049 fillchar = 0;
7050 for (n = 0; tabtab[n].start != NULL; n++)
7051 {
7052 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7053 while (col < len)
7054 TabPageIdxs[col++] = fillchar;
7055 p = tabtab[n].start;
7056 fillchar = tabtab[n].userhl;
7057 }
7058 while (col < Columns)
7059 TabPageIdxs[col++] = fillchar;
7060 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007061
7062theend:
7063 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064}
7065
7066#endif /* FEAT_STL_OPT */
7067
7068/*
7069 * Output a single character directly to the screen and update ScreenLines.
7070 */
7071 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007072screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 char_u buf[MB_MAXBYTES + 1];
7075
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007076#ifdef FEAT_MBYTE
7077 if (has_mbyte)
7078 buf[(*mb_char2bytes)(c, buf)] = NUL;
7079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007081 {
7082 buf[0] = c;
7083 buf[1] = NUL;
7084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 screen_puts(buf, row, col, attr);
7086}
7087
7088/*
7089 * Get a single character directly from ScreenLines into "bytes[]".
7090 * Also return its attribute in *attrp;
7091 */
7092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007093screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
7095 unsigned off;
7096
7097 /* safety check */
7098 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7099 {
7100 off = LineOffset[row] + col;
7101 *attrp = ScreenAttrs[off];
7102 bytes[0] = ScreenLines[off];
7103 bytes[1] = NUL;
7104
7105#ifdef FEAT_MBYTE
7106 if (enc_utf8 && ScreenLinesUC[off] != 0)
7107 bytes[utfc_char2bytes(off, bytes)] = NUL;
7108 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7109 {
7110 bytes[0] = ScreenLines[off];
7111 bytes[1] = ScreenLines2[off];
7112 bytes[2] = NUL;
7113 }
7114 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7115 {
7116 bytes[1] = ScreenLines[off + 1];
7117 bytes[2] = NUL;
7118 }
7119#endif
7120 }
7121}
7122
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007123#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007124static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007125
7126/*
7127 * Return TRUE if composing characters for screen posn "off" differs from
7128 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007129 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007130 */
7131 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007132screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007133{
7134 int i;
7135
7136 for (i = 0; i < Screen_mco; ++i)
7137 {
7138 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7139 return TRUE;
7140 if (u8cc[i] == 0)
7141 break;
7142 }
7143 return FALSE;
7144}
7145#endif
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * Put string '*text' on the screen at position 'row' and 'col', with
7149 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7150 * Note: only outputs within one row, message is truncated at screen boundary!
7151 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7152 */
7153 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007154screen_puts(
7155 char_u *text,
7156 int row,
7157 int col,
7158 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160 screen_puts_len(text, -1, row, col, attr);
7161}
7162
7163/*
7164 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7165 * a NUL.
7166 */
7167 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007168screen_puts_len(
7169 char_u *text,
7170 int textlen,
7171 int row,
7172 int col,
7173 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 unsigned off;
7176 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007177 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 int c;
7179#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007180 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 int mbyte_blen = 1;
7182 int mbyte_cells = 1;
7183 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007184 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 int clear_next_cell = FALSE;
7186# ifdef FEAT_ARABIC
7187 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007188 int pc, nc, nc1;
7189 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190# endif
7191#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007192#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7193 int force_redraw_this;
7194 int force_redraw_next = FALSE;
7195#endif
7196 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7199 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007200 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaarc236c162008-07-13 17:41:49 +00007202#ifdef FEAT_MBYTE
7203 /* When drawing over the right halve of a double-wide char clear out the
7204 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007205 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007206# ifdef FEAT_GUI
7207 && !gui.in_use
7208# endif
7209 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007210 {
7211 ScreenLines[off - 1] = ' ';
7212 ScreenAttrs[off - 1] = 0;
7213 if (enc_utf8)
7214 {
7215 ScreenLinesUC[off - 1] = 0;
7216 ScreenLinesC[0][off - 1] = 0;
7217 }
7218 /* redraw the previous cell, make it empty */
7219 screen_char(off - 1, row, col - 1);
7220 /* force the cell at "col" to be redrawn */
7221 force_redraw_next = TRUE;
7222 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007223#endif
7224
Bram Moolenaar367329b2007-08-30 11:53:22 +00007225#ifdef FEAT_MBYTE
7226 max_off = LineOffset[row] + screen_Columns;
7227#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007228 while (col < screen_Columns
7229 && (len < 0 || (int)(ptr - text) < len)
7230 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 {
7232 c = *ptr;
7233#ifdef FEAT_MBYTE
7234 /* check if this is the first byte of a multibyte */
7235 if (has_mbyte)
7236 {
7237 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007238 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007240 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7242 mbyte_cells = 1;
7243 else if (enc_dbcs != 0)
7244 mbyte_cells = mbyte_blen;
7245 else /* enc_utf8 */
7246 {
7247 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007248 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 (int)((text + len) - ptr));
7250 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007251 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007253# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /* Non-BMP character: display as ? or fullwidth ?. */
7255 if (u8c >= 0x10000)
7256 {
7257 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7258 if (attr == 0)
7259 attr = hl_attr(HLF_8);
7260 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262# ifdef FEAT_ARABIC
7263 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7264 {
7265 /* Do Arabic shaping. */
7266 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7267 {
7268 /* Past end of string to be displayed. */
7269 nc = NUL;
7270 nc1 = NUL;
7271 }
7272 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007273 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007274 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7275 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007276 nc1 = pcc[0];
7277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 pc = prev_c;
7279 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007280 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 else
7283 prev_c = u8c;
7284# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007285 if (col + mbyte_cells > screen_Columns)
7286 {
7287 /* Only 1 cell left, but character requires 2 cells:
7288 * display a '>' in the last column to avoid wrapping. */
7289 c = '>';
7290 mbyte_cells = 1;
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293 }
7294#endif
7295
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007296#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7297 force_redraw_this = force_redraw_next;
7298 force_redraw_next = FALSE;
7299#endif
7300
7301 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302#ifdef FEAT_MBYTE
7303 || (mbyte_cells == 2
7304 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7305 || (enc_dbcs == DBCS_JPNU
7306 && c == 0x8e
7307 && ScreenLines2[off] != ptr[1])
7308 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007309 && (ScreenLinesUC[off] !=
7310 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7311 || (ScreenLinesUC[off] != 0
7312 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#endif
7314 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007315 || exmode_active;
7316
7317 if (need_redraw
7318#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7319 || force_redraw_this
7320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 )
7322 {
7323#if defined(FEAT_GUI) || defined(UNIX)
7324 /* The bold trick makes a single row of pixels appear in the next
7325 * character. When a bold character is removed, the next
7326 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007327 * and for some xterms. */
7328 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329# ifdef FEAT_GUI
7330 gui.in_use
7331# endif
7332# if defined(FEAT_GUI) && defined(UNIX)
7333 ||
7334# endif
7335# ifdef UNIX
7336 term_is_xterm
7337# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007338 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007340 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007342 if (n > HL_ALL)
7343 n = syn_attr2attr(n);
7344 if (n & HL_BOLD)
7345 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 }
7347#endif
7348#ifdef FEAT_MBYTE
7349 /* When at the end of the text and overwriting a two-cell
7350 * character with a one-cell character, need to clear the next
7351 * cell. Also when overwriting the left halve of a two-cell char
7352 * with the right halve of a two-cell char. Do this only once
7353 * (mb_off2cells() may return 2 on the right halve). */
7354 if (clear_next_cell)
7355 clear_next_cell = FALSE;
7356 else if (has_mbyte
7357 && (len < 0 ? ptr[mbyte_blen] == NUL
7358 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007359 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007361 && (*mb_off2cells)(off, max_off) == 1
7362 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 clear_next_cell = TRUE;
7364
7365 /* Make sure we never leave a second byte of a double-byte behind,
7366 * it confuses mb_off2cells(). */
7367 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007368 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007370 && (*mb_off2cells)(off, max_off) == 1
7371 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 ScreenLines[off + mbyte_blen] = 0;
7373#endif
7374 ScreenLines[off] = c;
7375 ScreenAttrs[off] = attr;
7376#ifdef FEAT_MBYTE
7377 if (enc_utf8)
7378 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007379 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 ScreenLinesUC[off] = 0;
7381 else
7382 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383 int i;
7384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007386 for (i = 0; i < Screen_mco; ++i)
7387 {
7388 ScreenLinesC[i][off] = u8cc[i];
7389 if (u8cc[i] == 0)
7390 break;
7391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
7393 if (mbyte_cells == 2)
7394 {
7395 ScreenLines[off + 1] = 0;
7396 ScreenAttrs[off + 1] = attr;
7397 }
7398 screen_char(off, row, col);
7399 }
7400 else if (mbyte_cells == 2)
7401 {
7402 ScreenLines[off + 1] = ptr[1];
7403 ScreenAttrs[off + 1] = attr;
7404 screen_char_2(off, row, col);
7405 }
7406 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7407 {
7408 ScreenLines2[off] = ptr[1];
7409 screen_char(off, row, col);
7410 }
7411 else
7412#endif
7413 screen_char(off, row, col);
7414 }
7415#ifdef FEAT_MBYTE
7416 if (has_mbyte)
7417 {
7418 off += mbyte_cells;
7419 col += mbyte_cells;
7420 ptr += mbyte_blen;
7421 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007422 {
7423 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007425 len = -1;
7426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 else
7429#endif
7430 {
7431 ++off;
7432 ++col;
7433 ++ptr;
7434 }
7435 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007436
7437#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7438 /* If we detected the next character needs to be redrawn, but the text
7439 * doesn't extend up to there, update the character here. */
7440 if (force_redraw_next && col < screen_Columns)
7441 {
7442# ifdef FEAT_MBYTE
7443 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7444 screen_char_2(off, row, col);
7445 else
7446# endif
7447 screen_char(off, row, col);
7448 }
7449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450}
7451
7452#ifdef FEAT_SEARCH_EXTRA
7453/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007454 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 */
7456 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007457start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 if (p_hls && !no_hlsearch)
7460 {
7461 last_pat_prog(&search_hl.rm);
7462 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007463# ifdef FEAT_RELTIME
7464 /* Set the time limit to 'redrawtime'. */
7465 profile_setlimit(p_rdt, &search_hl.tm);
7466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 }
7468}
7469
7470/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007471 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 */
7473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007474end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 if (search_hl.rm.regprog != NULL)
7477 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007478 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 search_hl.rm.regprog = NULL;
7480 }
7481}
7482
7483/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007484 * Init for calling prepare_search_hl().
7485 */
7486 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007487init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007488{
7489 matchitem_T *cur;
7490
7491 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7492 * match */
7493 cur = wp->w_match_head;
7494 while (cur != NULL)
7495 {
7496 cur->hl.rm = cur->match;
7497 if (cur->hlg_id == 0)
7498 cur->hl.attr = 0;
7499 else
7500 cur->hl.attr = syn_id2attr(cur->hlg_id);
7501 cur->hl.buf = wp->w_buffer;
7502 cur->hl.lnum = 0;
7503 cur->hl.first_lnum = 0;
7504# ifdef FEAT_RELTIME
7505 /* Set the time limit to 'redrawtime'. */
7506 profile_setlimit(p_rdt, &(cur->hl.tm));
7507# endif
7508 cur = cur->next;
7509 }
7510 search_hl.buf = wp->w_buffer;
7511 search_hl.lnum = 0;
7512 search_hl.first_lnum = 0;
7513 /* time limit is set at the toplevel, for all windows */
7514}
7515
7516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 * Advance to the match in window "wp" line "lnum" or past it.
7518 */
7519 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007520prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007522 matchitem_T *cur; /* points to the match list */
7523 match_T *shl; /* points to search_hl or a match */
7524 int shl_flag; /* flag to indicate whether search_hl
7525 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007526 int pos_inprogress; /* marks that position match search is
7527 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 int n;
7529
7530 /*
7531 * When using a multi-line pattern, start searching at the top
7532 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007533 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007535 cur = wp->w_match_head;
7536 shl_flag = FALSE;
7537 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007539 if (shl_flag == FALSE)
7540 {
7541 shl = &search_hl;
7542 shl_flag = TRUE;
7543 }
7544 else
7545 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (shl->rm.regprog != NULL
7547 && shl->lnum == 0
7548 && re_multiline(shl->rm.regprog))
7549 {
7550 if (shl->first_lnum == 0)
7551 {
7552# ifdef FEAT_FOLDING
7553 for (shl->first_lnum = lnum;
7554 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7555 if (hasFoldingWin(wp, shl->first_lnum - 1,
7556 NULL, NULL, TRUE, NULL))
7557 break;
7558# else
7559 shl->first_lnum = wp->w_topline;
7560# endif
7561 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007562 if (cur != NULL)
7563 cur->pos.cur = 0;
7564 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007566 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7567 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007569 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7570 pos_inprogress = cur == NULL || cur->pos.cur == 0
7571 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (shl->lnum != 0)
7573 {
7574 shl->first_lnum = shl->lnum
7575 + shl->rm.endpos[0].lnum
7576 - shl->rm.startpos[0].lnum;
7577 n = shl->rm.endpos[0].col;
7578 }
7579 else
7580 {
7581 ++shl->first_lnum;
7582 n = 0;
7583 }
7584 }
7585 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007586 if (shl != &search_hl && cur != NULL)
7587 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589}
7590
7591/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007592 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 * Uses shl->buf.
7594 * Sets shl->lnum and shl->rm contents.
7595 * Note: Assumes a previous match is always before "lnum", unless
7596 * shl->lnum is zero.
7597 * Careful: Any pointers for buffer lines will become invalid.
7598 */
7599 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007600next_search_hl(
7601 win_T *win,
7602 match_T *shl, /* points to search_hl or a match */
7603 linenr_T lnum,
7604 colnr_T mincol, /* minimal column for a match */
7605 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 linenr_T l;
7608 colnr_T matchcol;
7609 long nmatched;
7610
7611 if (shl->lnum != 0)
7612 {
7613 /* Check for three situations:
7614 * 1. If the "lnum" is below a previous match, start a new search.
7615 * 2. If the previous match includes "mincol", use it.
7616 * 3. Continue after the previous match.
7617 */
7618 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7619 if (lnum > l)
7620 shl->lnum = 0;
7621 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7622 return;
7623 }
7624
7625 /*
7626 * Repeat searching for a match until one is found that includes "mincol"
7627 * or none is found in this line.
7628 */
7629 called_emsg = FALSE;
7630 for (;;)
7631 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007632#ifdef FEAT_RELTIME
7633 /* Stop searching after passing the time limit. */
7634 if (profile_passed_limit(&(shl->tm)))
7635 {
7636 shl->lnum = 0; /* no match found in time */
7637 break;
7638 }
7639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 /* Three situations:
7641 * 1. No useful previous match: search from start of line.
7642 * 2. Not Vi compatible or empty match: continue at next character.
7643 * Break the loop if this is beyond the end of the line.
7644 * 3. Vi compatible searching: continue at end of previous match.
7645 */
7646 if (shl->lnum == 0)
7647 matchcol = 0;
7648 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7649 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007650 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007652 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007653
7654 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007655 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007656 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007658 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 shl->lnum = 0;
7660 break;
7661 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007662#ifdef FEAT_MBYTE
7663 if (has_mbyte)
7664 matchcol += mb_ptr2len(ml);
7665 else
7666#endif
7667 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 else
7670 matchcol = shl->rm.endpos[0].col;
7671
7672 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007673 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007675 /* Remember whether shl->rm is using a copy of the regprog in
7676 * cur->match. */
7677 int regprog_is_copy = (shl != &search_hl && cur != NULL
7678 && shl == &cur->hl
7679 && cur->match.regprog == cur->hl.rm.regprog);
7680
Bram Moolenaarb3414592014-06-17 17:48:32 +02007681 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7682 matchcol,
7683#ifdef FEAT_RELTIME
7684 &(shl->tm)
7685#else
7686 NULL
7687#endif
7688 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007689 /* Copy the regprog, in case it got freed and recompiled. */
7690 if (regprog_is_copy)
7691 cur->match.regprog = cur->hl.rm.regprog;
7692
Bram Moolenaarb3414592014-06-17 17:48:32 +02007693 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007694 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007695 /* Error while handling regexp: stop using this regexp. */
7696 if (shl == &search_hl)
7697 {
7698 /* don't free regprog in the match list, it's a copy */
7699 vim_regfree(shl->rm.regprog);
7700 SET_NO_HLSEARCH(TRUE);
7701 }
7702 shl->rm.regprog = NULL;
7703 shl->lnum = 0;
7704 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7705 message */
7706 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007707 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007708 }
7709 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007710 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007711 else
7712 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (nmatched == 0)
7714 {
7715 shl->lnum = 0; /* no match found */
7716 break;
7717 }
7718 if (shl->rm.startpos[0].lnum > 0
7719 || shl->rm.startpos[0].col >= mincol
7720 || nmatched > 1
7721 || shl->rm.endpos[0].col > mincol)
7722 {
7723 shl->lnum += shl->rm.startpos[0].lnum;
7724 break; /* useful match found */
7725 }
7726 }
7727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007730next_search_hl_pos(
7731 match_T *shl, /* points to a match */
7732 linenr_T lnum,
7733 posmatch_T *posmatch, /* match positions */
7734 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007735{
7736 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007737 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007738
7739 shl->lnum = 0;
7740 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7741 {
7742 if (posmatch->pos[i].lnum == 0)
7743 break;
7744 if (posmatch->pos[i].col < mincol)
7745 continue;
7746 if (posmatch->pos[i].lnum == lnum)
7747 {
7748 if (shl->lnum == lnum)
7749 {
7750 /* partially sort positions by column numbers
7751 * on the same line */
7752 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7753 {
7754 llpos_T tmp = posmatch->pos[i];
7755
7756 posmatch->pos[i] = posmatch->pos[bot];
7757 posmatch->pos[bot] = tmp;
7758 }
7759 }
7760 else
7761 {
7762 bot = i;
7763 shl->lnum = lnum;
7764 }
7765 }
7766 }
7767 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007768 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007769 {
7770 colnr_T start = posmatch->pos[bot].col == 0
7771 ? 0 : posmatch->pos[bot].col - 1;
7772 colnr_T end = posmatch->pos[bot].col == 0
7773 ? MAXCOL : start + posmatch->pos[bot].len;
7774
7775 shl->rm.startpos[0].lnum = 0;
7776 shl->rm.startpos[0].col = start;
7777 shl->rm.endpos[0].lnum = 0;
7778 shl->rm.endpos[0].col = end;
7779 posmatch->cur = bot + 1;
7780 return TRUE;
7781 }
7782 return FALSE;
7783}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007784#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007787screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 attrentry_T *aep = NULL;
7790
7791 screen_attr = attr;
7792 if (full_screen
7793#ifdef WIN3264
7794 && termcap_active
7795#endif
7796 )
7797 {
7798#ifdef FEAT_GUI
7799 if (gui.in_use)
7800 {
7801 char buf[20];
7802
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007803 /* The GUI handles this internally. */
7804 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 OUT_STR(buf);
7806 }
7807 else
7808#endif
7809 {
7810 if (attr > HL_ALL) /* special HL attr. */
7811 {
7812 if (t_colors > 1)
7813 aep = syn_cterm_attr2entry(attr);
7814 else
7815 aep = syn_term_attr2entry(attr);
7816 if (aep == NULL) /* did ":syntax clear" */
7817 attr = 0;
7818 else
7819 attr = aep->ae_attr;
7820 }
7821 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7822 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007823 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7824 && cterm_normal_fg_bold)
7825 /* If the Normal FG color has BOLD attribute and the new HL
7826 * has a FG color defined, clear BOLD. */
7827 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7829 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007830 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7831 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 out_str(T_US);
7833 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7834 out_str(T_CZH);
7835 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7836 out_str(T_MR);
7837
7838 /*
7839 * Output the color or start string after bold etc., in case the
7840 * bold etc. override the color setting.
7841 */
7842 if (aep != NULL)
7843 {
7844 if (t_colors > 1)
7845 {
7846 if (aep->ae_u.cterm.fg_color)
7847 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7848 if (aep->ae_u.cterm.bg_color)
7849 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7850 }
7851 else
7852 {
7853 if (aep->ae_u.term.start != NULL)
7854 out_str(aep->ae_u.term.start);
7855 }
7856 }
7857 }
7858 }
7859}
7860
7861 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007862screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
7864 int do_ME = FALSE; /* output T_ME code */
7865
7866 if (screen_attr != 0
7867#ifdef WIN3264
7868 && termcap_active
7869#endif
7870 )
7871 {
7872#ifdef FEAT_GUI
7873 if (gui.in_use)
7874 {
7875 char buf[20];
7876
7877 /* use internal GUI code */
7878 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7879 OUT_STR(buf);
7880 }
7881 else
7882#endif
7883 {
7884 if (screen_attr > HL_ALL) /* special HL attr. */
7885 {
7886 attrentry_T *aep;
7887
7888 if (t_colors > 1)
7889 {
7890 /*
7891 * Assume that t_me restores the original colors!
7892 */
7893 aep = syn_cterm_attr2entry(screen_attr);
7894 if (aep != NULL && (aep->ae_u.cterm.fg_color
7895 || aep->ae_u.cterm.bg_color))
7896 do_ME = TRUE;
7897 }
7898 else
7899 {
7900 aep = syn_term_attr2entry(screen_attr);
7901 if (aep != NULL && aep->ae_u.term.stop != NULL)
7902 {
7903 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7904 do_ME = TRUE;
7905 else
7906 out_str(aep->ae_u.term.stop);
7907 }
7908 }
7909 if (aep == NULL) /* did ":syntax clear" */
7910 screen_attr = 0;
7911 else
7912 screen_attr = aep->ae_attr;
7913 }
7914
7915 /*
7916 * Often all ending-codes are equal to T_ME. Avoid outputting the
7917 * same sequence several times.
7918 */
7919 if (screen_attr & HL_STANDOUT)
7920 {
7921 if (STRCMP(T_SE, T_ME) == 0)
7922 do_ME = TRUE;
7923 else
7924 out_str(T_SE);
7925 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007926 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
7928 if (STRCMP(T_UE, T_ME) == 0)
7929 do_ME = TRUE;
7930 else
7931 out_str(T_UE);
7932 }
7933 if (screen_attr & HL_ITALIC)
7934 {
7935 if (STRCMP(T_CZR, T_ME) == 0)
7936 do_ME = TRUE;
7937 else
7938 out_str(T_CZR);
7939 }
7940 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7941 out_str(T_ME);
7942
7943 if (t_colors > 1)
7944 {
7945 /* set Normal cterm colors */
7946 if (cterm_normal_fg_color != 0)
7947 term_fg_color(cterm_normal_fg_color - 1);
7948 if (cterm_normal_bg_color != 0)
7949 term_bg_color(cterm_normal_bg_color - 1);
7950 if (cterm_normal_fg_bold)
7951 out_str(T_MD);
7952 }
7953 }
7954 }
7955 screen_attr = 0;
7956}
7957
7958/*
7959 * Reset the colors for a cterm. Used when leaving Vim.
7960 * The machine specific code may override this again.
7961 */
7962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007963reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 if (t_colors > 1)
7966 {
7967 /* set Normal cterm colors */
7968 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7969 {
7970 out_str(T_OP);
7971 screen_attr = -1;
7972 }
7973 if (cterm_normal_fg_bold)
7974 {
7975 out_str(T_ME);
7976 screen_attr = -1;
7977 }
7978 }
7979}
7980
7981/*
7982 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7983 * using the attributes from ScreenAttrs["off"].
7984 */
7985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007986screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 int attr;
7989
7990 /* Check for illegal values, just in case (could happen just after
7991 * resizing). */
7992 if (row >= screen_Rows || col >= screen_Columns)
7993 return;
7994
Bram Moolenaar494838a2015-02-10 19:20:37 +01007995 /* Outputting a character in the last cell on the screen may scroll the
7996 * screen up. Only do it when the "xn" termcap property is set, otherwise
7997 * mark the character invalid (update it when scrolled up). */
7998 if (*T_XN == NUL
7999 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000#ifdef FEAT_RIGHTLEFT
8001 /* account for first command-line character in rightleft mode */
8002 && !cmdmsg_rl
8003#endif
8004 )
8005 {
8006 ScreenAttrs[off] = (sattr_T)-1;
8007 return;
8008 }
8009
8010 /*
8011 * Stop highlighting first, so it's easier to move the cursor.
8012 */
8013#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8014 if (screen_char_attr != 0)
8015 attr = screen_char_attr;
8016 else
8017#endif
8018 attr = ScreenAttrs[off];
8019 if (screen_attr != attr)
8020 screen_stop_highlight();
8021
8022 windgoto(row, col);
8023
8024 if (screen_attr != attr)
8025 screen_start_highlight(attr);
8026
8027#ifdef FEAT_MBYTE
8028 if (enc_utf8 && ScreenLinesUC[off] != 0)
8029 {
8030 char_u buf[MB_MAXBYTES + 1];
8031
8032 /* Convert UTF-8 character to bytes and write it. */
8033
8034 buf[utfc_char2bytes(off, buf)] = NUL;
8035
8036 out_str(buf);
8037 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8038 ++screen_cur_col;
8039 }
8040 else
8041#endif
8042 {
8043#ifdef FEAT_MBYTE
8044 out_flush_check();
8045#endif
8046 out_char(ScreenLines[off]);
8047#ifdef FEAT_MBYTE
8048 /* double-byte character in single-width cell */
8049 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8050 out_char(ScreenLines2[off]);
8051#endif
8052 }
8053
8054 screen_cur_col++;
8055}
8056
8057#ifdef FEAT_MBYTE
8058
8059/*
8060 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8061 * on the screen at position 'row' and 'col'.
8062 * The attributes of the first byte is used for all. This is required to
8063 * output the two bytes of a double-byte character with nothing in between.
8064 */
8065 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008066screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067{
8068 /* Check for illegal values (could be wrong when screen was resized). */
8069 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8070 return;
8071
8072 /* Outputting the last character on the screen may scrollup the screen.
8073 * Don't to it! Mark the character invalid (update it when scrolled up) */
8074 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8075 {
8076 ScreenAttrs[off] = (sattr_T)-1;
8077 return;
8078 }
8079
8080 /* Output the first byte normally (positions the cursor), then write the
8081 * second byte directly. */
8082 screen_char(off, row, col);
8083 out_char(ScreenLines[off + 1]);
8084 ++screen_cur_col;
8085}
8086#endif
8087
8088#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8089/*
8090 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8091 * This uses the contents of ScreenLines[] and doesn't change it.
8092 */
8093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008094screen_draw_rectangle(
8095 int row,
8096 int col,
8097 int height,
8098 int width,
8099 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 int r, c;
8102 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008103#ifdef FEAT_MBYTE
8104 int max_off;
8105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008107 /* Can't use ScreenLines unless initialized */
8108 if (ScreenLines == NULL)
8109 return;
8110
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (invert)
8112 screen_char_attr = HL_INVERSE;
8113 for (r = row; r < row + height; ++r)
8114 {
8115 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008116#ifdef FEAT_MBYTE
8117 max_off = off + screen_Columns;
8118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 for (c = col; c < col + width; ++c)
8120 {
8121#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008122 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
8124 screen_char_2(off + c, r, c);
8125 ++c;
8126 }
8127 else
8128#endif
8129 {
8130 screen_char(off + c, r, c);
8131#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008132 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 ++c;
8134#endif
8135 }
8136 }
8137 }
8138 screen_char_attr = 0;
8139}
8140#endif
8141
8142#ifdef FEAT_VERTSPLIT
8143/*
8144 * Redraw the characters for a vertically split window.
8145 */
8146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008147redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 int col;
8150 int width;
8151
8152# ifdef FEAT_CLIPBOARD
8153 clip_may_clear_selection(row, end - 1);
8154# endif
8155
8156 if (wp == NULL)
8157 {
8158 col = 0;
8159 width = Columns;
8160 }
8161 else
8162 {
8163 col = wp->w_wincol;
8164 width = wp->w_width;
8165 }
8166 screen_draw_rectangle(row, col, end - row, width, FALSE);
8167}
8168#endif
8169
8170/*
8171 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8172 * with character 'c1' in first column followed by 'c2' in the other columns.
8173 * Use attributes 'attr'.
8174 */
8175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008176screen_fill(
8177 int start_row,
8178 int end_row,
8179 int start_col,
8180 int end_col,
8181 int c1,
8182 int c2,
8183 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184{
8185 int row;
8186 int col;
8187 int off;
8188 int end_off;
8189 int did_delete;
8190 int c;
8191 int norm_term;
8192#if defined(FEAT_GUI) || defined(UNIX)
8193 int force_next = FALSE;
8194#endif
8195
8196 if (end_row > screen_Rows) /* safety check */
8197 end_row = screen_Rows;
8198 if (end_col > screen_Columns) /* safety check */
8199 end_col = screen_Columns;
8200 if (ScreenLines == NULL
8201 || start_row >= end_row
8202 || start_col >= end_col) /* nothing to do */
8203 return;
8204
8205 /* it's a "normal" terminal when not in a GUI or cterm */
8206 norm_term = (
8207#ifdef FEAT_GUI
8208 !gui.in_use &&
8209#endif
8210 t_colors <= 1);
8211 for (row = start_row; row < end_row; ++row)
8212 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008213#ifdef FEAT_MBYTE
8214 if (has_mbyte
8215# ifdef FEAT_GUI
8216 && !gui.in_use
8217# endif
8218 )
8219 {
8220 /* When drawing over the right halve of a double-wide char clear
8221 * out the left halve. When drawing over the left halve of a
8222 * double wide-char clear out the right halve. Only needed in a
8223 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008224 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008225 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008226 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008227 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008228 }
8229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 /*
8231 * Try to use delete-line termcap code, when no attributes or in a
8232 * "normal" terminal, where a bold/italic space is just a
8233 * space.
8234 */
8235 did_delete = FALSE;
8236 if (c2 == ' '
8237 && end_col == Columns
8238 && can_clear(T_CE)
8239 && (attr == 0
8240 || (norm_term
8241 && attr <= HL_ALL
8242 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8243 {
8244 /*
8245 * check if we really need to clear something
8246 */
8247 col = start_col;
8248 if (c1 != ' ') /* don't clear first char */
8249 ++col;
8250
8251 off = LineOffset[row] + col;
8252 end_off = LineOffset[row] + end_col;
8253
8254 /* skip blanks (used often, keep it fast!) */
8255#ifdef FEAT_MBYTE
8256 if (enc_utf8)
8257 while (off < end_off && ScreenLines[off] == ' '
8258 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8259 ++off;
8260 else
8261#endif
8262 while (off < end_off && ScreenLines[off] == ' '
8263 && ScreenAttrs[off] == 0)
8264 ++off;
8265 if (off < end_off) /* something to be cleared */
8266 {
8267 col = off - LineOffset[row];
8268 screen_stop_highlight();
8269 term_windgoto(row, col);/* clear rest of this screen line */
8270 out_str(T_CE);
8271 screen_start(); /* don't know where cursor is now */
8272 col = end_col - col;
8273 while (col--) /* clear chars in ScreenLines */
8274 {
8275 ScreenLines[off] = ' ';
8276#ifdef FEAT_MBYTE
8277 if (enc_utf8)
8278 ScreenLinesUC[off] = 0;
8279#endif
8280 ScreenAttrs[off] = 0;
8281 ++off;
8282 }
8283 }
8284 did_delete = TRUE; /* the chars are cleared now */
8285 }
8286
8287 off = LineOffset[row] + start_col;
8288 c = c1;
8289 for (col = start_col; col < end_col; ++col)
8290 {
8291 if (ScreenLines[off] != c
8292#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008293 || (enc_utf8 && (int)ScreenLinesUC[off]
8294 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295#endif
8296 || ScreenAttrs[off] != attr
8297#if defined(FEAT_GUI) || defined(UNIX)
8298 || force_next
8299#endif
8300 )
8301 {
8302#if defined(FEAT_GUI) || defined(UNIX)
8303 /* The bold trick may make a single row of pixels appear in
8304 * the next character. When a bold character is removed, the
8305 * next character should be redrawn too. This happens for our
8306 * own GUI and for some xterms. */
8307 if (
8308# ifdef FEAT_GUI
8309 gui.in_use
8310# endif
8311# if defined(FEAT_GUI) && defined(UNIX)
8312 ||
8313# endif
8314# ifdef UNIX
8315 term_is_xterm
8316# endif
8317 )
8318 {
8319 if (ScreenLines[off] != ' '
8320 && (ScreenAttrs[off] > HL_ALL
8321 || ScreenAttrs[off] & HL_BOLD))
8322 force_next = TRUE;
8323 else
8324 force_next = FALSE;
8325 }
8326#endif
8327 ScreenLines[off] = c;
8328#ifdef FEAT_MBYTE
8329 if (enc_utf8)
8330 {
8331 if (c >= 0x80)
8332 {
8333 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008334 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 else
8337 ScreenLinesUC[off] = 0;
8338 }
8339#endif
8340 ScreenAttrs[off] = attr;
8341 if (!did_delete || c != ' ')
8342 screen_char(off, row, col);
8343 }
8344 ++off;
8345 if (col == start_col)
8346 {
8347 if (did_delete)
8348 break;
8349 c = c2;
8350 }
8351 }
8352 if (end_col == Columns)
8353 LineWraps[row] = FALSE;
8354 if (row == Rows - 1) /* overwritten the command line */
8355 {
8356 redraw_cmdline = TRUE;
8357 if (c1 == ' ' && c2 == ' ')
8358 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008359 if (start_col == 0)
8360 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362 }
8363}
8364
8365/*
8366 * Check if there should be a delay. Used before clearing or redrawing the
8367 * screen or the command line.
8368 */
8369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008370check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8373 && !did_wait_return
8374 && emsg_silent == 0)
8375 {
8376 out_flush();
8377 ui_delay(1000L, TRUE);
8378 emsg_on_display = FALSE;
8379 if (check_msg_scroll)
8380 msg_scroll = FALSE;
8381 }
8382}
8383
8384/*
8385 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008386 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 * Returns TRUE if there is a valid screen to write to.
8388 * Returns FALSE when starting up and screen not initialized yet.
8389 */
8390 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008391screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008393 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 return (ScreenLines != NULL);
8395}
8396
8397/*
8398 * Resize the shell to Rows and Columns.
8399 * Allocate ScreenLines[] and associated items.
8400 *
8401 * There may be some time between setting Rows and Columns and (re)allocating
8402 * ScreenLines[]. This happens when starting up and when (manually) changing
8403 * the shell size. Always use screen_Rows and screen_Columns to access items
8404 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8405 * final size of the shell is needed.
8406 */
8407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008408screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 int new_row, old_row;
8411#ifdef FEAT_GUI
8412 int old_Rows;
8413#endif
8414 win_T *wp;
8415 int outofmem = FALSE;
8416 int len;
8417 schar_T *new_ScreenLines;
8418#ifdef FEAT_MBYTE
8419 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008420 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008422 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423#endif
8424 sattr_T *new_ScreenAttrs;
8425 unsigned *new_LineOffset;
8426 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008427#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008428 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008429 tabpage_T *tp;
8430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008432 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008433#ifdef FEAT_AUTOCMD
8434 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008436retry:
8437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 /*
8439 * Allocation of the screen buffers is done only when the size changes and
8440 * when Rows and Columns have been set and we have started doing full
8441 * screen stuff.
8442 */
8443 if ((ScreenLines != NULL
8444 && Rows == screen_Rows
8445 && Columns == screen_Columns
8446#ifdef FEAT_MBYTE
8447 && enc_utf8 == (ScreenLinesUC != NULL)
8448 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008449 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#endif
8451 )
8452 || Rows == 0
8453 || Columns == 0
8454 || (!full_screen && ScreenLines == NULL))
8455 return;
8456
8457 /*
8458 * It's possible that we produce an out-of-memory message below, which
8459 * will cause this function to be called again. To break the loop, just
8460 * return here.
8461 */
8462 if (entered)
8463 return;
8464 entered = TRUE;
8465
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008466 /*
8467 * Note that the window sizes are updated before reallocating the arrays,
8468 * thus we must not redraw here!
8469 */
8470 ++RedrawingDisabled;
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 win_new_shellsize(); /* fit the windows in the new sized shell */
8473
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 comp_col(); /* recompute columns for shown command and ruler */
8475
8476 /*
8477 * We're changing the size of the screen.
8478 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8479 * - Move lines from the old arrays into the new arrays, clear extra
8480 * lines (unless the screen is going to be cleared).
8481 * - Free the old arrays.
8482 *
8483 * If anything fails, make ScreenLines NULL, so we don't do anything!
8484 * Continuing with the old ScreenLines may result in a crash, because the
8485 * size is wrong.
8486 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008487 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008489#ifdef FEAT_AUTOCMD
8490 if (aucmd_win != NULL)
8491 win_free_lsize(aucmd_win);
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 new_ScreenLines = (schar_T *)lalloc((long_u)(
8495 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8496#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008497 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 if (enc_utf8)
8499 {
8500 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8501 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008502 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008503 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8505 }
8506 if (enc_dbcs == DBCS_JPNU)
8507 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8508 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8509#endif
8510 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8511 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8512 new_LineOffset = (unsigned *)lalloc((long_u)(
8513 Rows * sizeof(unsigned)), FALSE);
8514 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008515#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008516 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008519 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {
8521 if (win_alloc_lines(wp) == FAIL)
8522 {
8523 outofmem = TRUE;
8524#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008525 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526#endif
8527 }
8528 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008529#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008530 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8531 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008532 outofmem = TRUE;
8533#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008534#ifdef FEAT_WINDOWS
8535give_up:
8536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008538#ifdef FEAT_MBYTE
8539 for (i = 0; i < p_mco; ++i)
8540 if (new_ScreenLinesC[i] == NULL)
8541 break;
8542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (new_ScreenLines == NULL
8544#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008545 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8547#endif
8548 || new_ScreenAttrs == NULL
8549 || new_LineOffset == NULL
8550 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008551#ifdef FEAT_WINDOWS
8552 || new_TabPageIdxs == NULL
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 || outofmem)
8555 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008556 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008557 {
8558 /* guess the size */
8559 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8560
8561 /* Remember we did this to avoid getting outofmem messages over
8562 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008563 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 vim_free(new_ScreenLines);
8566 new_ScreenLines = NULL;
8567#ifdef FEAT_MBYTE
8568 vim_free(new_ScreenLinesUC);
8569 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008570 for (i = 0; i < p_mco; ++i)
8571 {
8572 vim_free(new_ScreenLinesC[i]);
8573 new_ScreenLinesC[i] = NULL;
8574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 vim_free(new_ScreenLines2);
8576 new_ScreenLines2 = NULL;
8577#endif
8578 vim_free(new_ScreenAttrs);
8579 new_ScreenAttrs = NULL;
8580 vim_free(new_LineOffset);
8581 new_LineOffset = NULL;
8582 vim_free(new_LineWraps);
8583 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008584#ifdef FEAT_WINDOWS
8585 vim_free(new_TabPageIdxs);
8586 new_TabPageIdxs = NULL;
8587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589 else
8590 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 for (new_row = 0; new_row < Rows; ++new_row)
8594 {
8595 new_LineOffset[new_row] = new_row * Columns;
8596 new_LineWraps[new_row] = FALSE;
8597
8598 /*
8599 * If the screen is not going to be cleared, copy as much as
8600 * possible from the old screen to the new one and clear the rest
8601 * (used when resizing the window at the "--more--" prompt or when
8602 * executing an external command, for the GUI).
8603 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008604 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {
8606 (void)vim_memset(new_ScreenLines + new_row * Columns,
8607 ' ', (size_t)Columns * sizeof(schar_T));
8608#ifdef FEAT_MBYTE
8609 if (enc_utf8)
8610 {
8611 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8612 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008613 for (i = 0; i < p_mco; ++i)
8614 (void)vim_memset(new_ScreenLinesC[i]
8615 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 0, (size_t)Columns * sizeof(u8char_T));
8617 }
8618 if (enc_dbcs == DBCS_JPNU)
8619 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8620 0, (size_t)Columns * sizeof(schar_T));
8621#endif
8622 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8623 0, (size_t)Columns * sizeof(sattr_T));
8624 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008625 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
8627 if (screen_Columns < Columns)
8628 len = screen_Columns;
8629 else
8630 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008631#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008632 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008633 * may be invalid now. Also when p_mco changes. */
8634 if (!(enc_utf8 && ScreenLinesUC == NULL)
8635 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008636#endif
8637 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8638 ScreenLines + LineOffset[old_row],
8639 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008641 if (enc_utf8 && ScreenLinesUC != NULL
8642 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
8644 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8645 ScreenLinesUC + LineOffset[old_row],
8646 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008647 for (i = 0; i < p_mco; ++i)
8648 mch_memmove(new_ScreenLinesC[i]
8649 + new_LineOffset[new_row],
8650 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 (size_t)len * sizeof(u8char_T));
8652 }
8653 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8654 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8655 ScreenLines2 + LineOffset[old_row],
8656 (size_t)len * sizeof(schar_T));
8657#endif
8658 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8659 ScreenAttrs + LineOffset[old_row],
8660 (size_t)len * sizeof(sattr_T));
8661 }
8662 }
8663 }
8664 /* Use the last line of the screen for the current line. */
8665 current_ScreenLine = new_ScreenLines + Rows * Columns;
8666 }
8667
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008668 free_screenlines();
8669
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 ScreenLines = new_ScreenLines;
8671#ifdef FEAT_MBYTE
8672 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008673 for (i = 0; i < p_mco; ++i)
8674 ScreenLinesC[i] = new_ScreenLinesC[i];
8675 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 ScreenLines2 = new_ScreenLines2;
8677#endif
8678 ScreenAttrs = new_ScreenAttrs;
8679 LineOffset = new_LineOffset;
8680 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008681#ifdef FEAT_WINDOWS
8682 TabPageIdxs = new_TabPageIdxs;
8683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 /* It's important that screen_Rows and screen_Columns reflect the actual
8686 * size of ScreenLines[]. Set them before calling anything. */
8687#ifdef FEAT_GUI
8688 old_Rows = screen_Rows;
8689#endif
8690 screen_Rows = Rows;
8691 screen_Columns = Columns;
8692
8693 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008694 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 screenclear2();
8696
8697#ifdef FEAT_GUI
8698 else if (gui.in_use
8699 && !gui.starting
8700 && ScreenLines != NULL
8701 && old_Rows != Rows)
8702 {
8703 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8704 /*
8705 * Adjust the position of the cursor, for when executing an external
8706 * command.
8707 */
8708 if (msg_row >= Rows) /* Rows got smaller */
8709 msg_row = Rows - 1; /* put cursor at last row */
8710 else if (Rows > old_Rows) /* Rows got bigger */
8711 msg_row += Rows - old_Rows; /* put cursor in same place */
8712 if (msg_col >= Columns) /* Columns got smaller */
8713 msg_col = Columns - 1; /* put cursor at last column */
8714 }
8715#endif
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008718 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008719
8720#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008721 /*
8722 * Do not apply autocommands more than 3 times to avoid an endless loop
8723 * in case applying autocommands always changes Rows or Columns.
8724 */
8725 if (starting == 0 && ++retry_count <= 3)
8726 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008727 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008728 /* In rare cases, autocommands may have altered Rows or Columns,
8729 * jump back to check if we need to allocate the screen again. */
8730 goto retry;
8731 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733}
8734
8735 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008736free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008737{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008738#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 int i;
8740
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008741 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008742 for (i = 0; i < Screen_mco; ++i)
8743 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008744 vim_free(ScreenLines2);
8745#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008746 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008747 vim_free(ScreenAttrs);
8748 vim_free(LineOffset);
8749 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008750#ifdef FEAT_WINDOWS
8751 vim_free(TabPageIdxs);
8752#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008753}
8754
8755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008756screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757{
8758 check_for_delay(FALSE);
8759 screenalloc(FALSE); /* allocate screen buffers if size changed */
8760 screenclear2(); /* clear the screen */
8761}
8762
8763 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008764screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 int i;
8767
8768 if (starting == NO_SCREEN || ScreenLines == NULL
8769#ifdef FEAT_GUI
8770 || (gui.in_use && gui.starting)
8771#endif
8772 )
8773 return;
8774
8775#ifdef FEAT_GUI
8776 if (!gui.in_use)
8777#endif
8778 screen_attr = -1; /* force setting the Normal colors */
8779 screen_stop_highlight(); /* don't want highlighting here */
8780
8781#ifdef FEAT_CLIPBOARD
8782 /* disable selection without redrawing it */
8783 clip_scroll_selection(9999);
8784#endif
8785
8786 /* blank out ScreenLines */
8787 for (i = 0; i < Rows; ++i)
8788 {
8789 lineclear(LineOffset[i], (int)Columns);
8790 LineWraps[i] = FALSE;
8791 }
8792
8793 if (can_clear(T_CL))
8794 {
8795 out_str(T_CL); /* clear the display */
8796 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008797 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
8799 else
8800 {
8801 /* can't clear the screen, mark all chars with invalid attributes */
8802 for (i = 0; i < Rows; ++i)
8803 lineinvalid(LineOffset[i], (int)Columns);
8804 clear_cmdline = TRUE;
8805 }
8806
8807 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8808
8809 win_rest_invalid(firstwin);
8810 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008811#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008812 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 if (must_redraw == CLEAR) /* no need to clear again */
8815 must_redraw = NOT_VALID;
8816 compute_cmdrow();
8817 msg_row = cmdline_row; /* put cursor on last line for messages */
8818 msg_col = 0;
8819 screen_start(); /* don't know where cursor is now */
8820 msg_scrolled = 0; /* can't scroll back */
8821 msg_didany = FALSE;
8822 msg_didout = FALSE;
8823}
8824
8825/*
8826 * Clear one line in ScreenLines.
8827 */
8828 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008829lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8832#ifdef FEAT_MBYTE
8833 if (enc_utf8)
8834 (void)vim_memset(ScreenLinesUC + off, 0,
8835 (size_t)width * sizeof(u8char_T));
8836#endif
8837 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8838}
8839
8840/*
8841 * Mark one line in ScreenLines invalid by setting the attributes to an
8842 * invalid value.
8843 */
8844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008845lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8848}
8849
8850#ifdef FEAT_VERTSPLIT
8851/*
8852 * Copy part of a Screenline for vertically split window "wp".
8853 */
8854 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008855linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 unsigned off_to = LineOffset[to] + wp->w_wincol;
8858 unsigned off_from = LineOffset[from] + wp->w_wincol;
8859
8860 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8861 wp->w_width * sizeof(schar_T));
8862# ifdef FEAT_MBYTE
8863 if (enc_utf8)
8864 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008865 int i;
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8868 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869 for (i = 0; i < p_mco; ++i)
8870 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8871 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 }
8873 if (enc_dbcs == DBCS_JPNU)
8874 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8875 wp->w_width * sizeof(schar_T));
8876# endif
8877 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8878 wp->w_width * sizeof(sattr_T));
8879}
8880#endif
8881
8882/*
8883 * Return TRUE if clearing with term string "p" would work.
8884 * It can't work when the string is empty or it won't set the right background.
8885 */
8886 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008887can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 return (*p != NUL && (t_colors <= 1
8890#ifdef FEAT_GUI
8891 || gui.in_use
8892#endif
8893 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8894}
8895
8896/*
8897 * Reset cursor position. Use whenever cursor was moved because of outputting
8898 * something directly to the screen (shell commands) or a terminal control
8899 * code.
8900 */
8901 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008902screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 screen_cur_row = screen_cur_col = 9999;
8905}
8906
8907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 * Move the cursor to position "row","col" in the screen.
8909 * This tries to find the most efficient way to move, minimizing the number of
8910 * characters sent to the terminal.
8911 */
8912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008913windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008915 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 int i;
8917 int plan;
8918 int cost;
8919 int wouldbe_col;
8920 int noinvcurs;
8921 char_u *bs;
8922 int goto_cost;
8923 int attr;
8924
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008925#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8927
8928#define PLAN_LE 1
8929#define PLAN_CR 2
8930#define PLAN_NL 3
8931#define PLAN_WRITE 4
8932 /* Can't use ScreenLines unless initialized */
8933 if (ScreenLines == NULL)
8934 return;
8935
8936 if (col != screen_cur_col || row != screen_cur_row)
8937 {
8938 /* Check for valid position. */
8939 if (row < 0) /* window without text lines? */
8940 row = 0;
8941 if (row >= screen_Rows)
8942 row = screen_Rows - 1;
8943 if (col >= screen_Columns)
8944 col = screen_Columns - 1;
8945
8946 /* check if no cursor movement is allowed in highlight mode */
8947 if (screen_attr && *T_MS == NUL)
8948 noinvcurs = HIGHL_COST;
8949 else
8950 noinvcurs = 0;
8951 goto_cost = GOTO_COST + noinvcurs;
8952
8953 /*
8954 * Plan how to do the positioning:
8955 * 1. Use CR to move it to column 0, same row.
8956 * 2. Use T_LE to move it a few columns to the left.
8957 * 3. Use NL to move a few lines down, column 0.
8958 * 4. Move a few columns to the right with T_ND or by writing chars.
8959 *
8960 * Don't do this if the cursor went beyond the last column, the cursor
8961 * position is unknown then (some terminals wrap, some don't )
8962 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008963 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 * characters to move the cursor to the right.
8965 */
8966 if (row >= screen_cur_row && screen_cur_col < Columns)
8967 {
8968 /*
8969 * If the cursor is in the same row, bigger col, we can use CR
8970 * or T_LE.
8971 */
8972 bs = NULL; /* init for GCC */
8973 attr = screen_attr;
8974 if (row == screen_cur_row && col < screen_cur_col)
8975 {
8976 /* "le" is preferred over "bc", because "bc" is obsolete */
8977 if (*T_LE)
8978 bs = T_LE; /* "cursor left" */
8979 else
8980 bs = T_BC; /* "backspace character (old) */
8981 if (*bs)
8982 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8983 else
8984 cost = 999;
8985 if (col + 1 < cost) /* using CR is less characters */
8986 {
8987 plan = PLAN_CR;
8988 wouldbe_col = 0;
8989 cost = 1; /* CR is just one character */
8990 }
8991 else
8992 {
8993 plan = PLAN_LE;
8994 wouldbe_col = col;
8995 }
8996 if (noinvcurs) /* will stop highlighting */
8997 {
8998 cost += noinvcurs;
8999 attr = 0;
9000 }
9001 }
9002
9003 /*
9004 * If the cursor is above where we want to be, we can use CR LF.
9005 */
9006 else if (row > screen_cur_row)
9007 {
9008 plan = PLAN_NL;
9009 wouldbe_col = 0;
9010 cost = (row - screen_cur_row) * 2; /* CR LF */
9011 if (noinvcurs) /* will stop highlighting */
9012 {
9013 cost += noinvcurs;
9014 attr = 0;
9015 }
9016 }
9017
9018 /*
9019 * If the cursor is in the same row, smaller col, just use write.
9020 */
9021 else
9022 {
9023 plan = PLAN_WRITE;
9024 wouldbe_col = screen_cur_col;
9025 cost = 0;
9026 }
9027
9028 /*
9029 * Check if any characters that need to be written have the
9030 * correct attributes. Also avoid UTF-8 characters.
9031 */
9032 i = col - wouldbe_col;
9033 if (i > 0)
9034 cost += i;
9035 if (cost < goto_cost && i > 0)
9036 {
9037 /*
9038 * Check if the attributes are correct without additionally
9039 * stopping highlighting.
9040 */
9041 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9042 while (i && *p++ == attr)
9043 --i;
9044 if (i != 0)
9045 {
9046 /*
9047 * Try if it works when highlighting is stopped here.
9048 */
9049 if (*--p == 0)
9050 {
9051 cost += noinvcurs;
9052 while (i && *p++ == 0)
9053 --i;
9054 }
9055 if (i != 0)
9056 cost = 999; /* different attributes, don't do it */
9057 }
9058#ifdef FEAT_MBYTE
9059 if (enc_utf8)
9060 {
9061 /* Don't use an UTF-8 char for positioning, it's slow. */
9062 for (i = wouldbe_col; i < col; ++i)
9063 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9064 {
9065 cost = 999;
9066 break;
9067 }
9068 }
9069#endif
9070 }
9071
9072 /*
9073 * We can do it without term_windgoto()!
9074 */
9075 if (cost < goto_cost)
9076 {
9077 if (plan == PLAN_LE)
9078 {
9079 if (noinvcurs)
9080 screen_stop_highlight();
9081 while (screen_cur_col > col)
9082 {
9083 out_str(bs);
9084 --screen_cur_col;
9085 }
9086 }
9087 else if (plan == PLAN_CR)
9088 {
9089 if (noinvcurs)
9090 screen_stop_highlight();
9091 out_char('\r');
9092 screen_cur_col = 0;
9093 }
9094 else if (plan == PLAN_NL)
9095 {
9096 if (noinvcurs)
9097 screen_stop_highlight();
9098 while (screen_cur_row < row)
9099 {
9100 out_char('\n');
9101 ++screen_cur_row;
9102 }
9103 screen_cur_col = 0;
9104 }
9105
9106 i = col - screen_cur_col;
9107 if (i > 0)
9108 {
9109 /*
9110 * Use cursor-right if it's one character only. Avoids
9111 * removing a line of pixels from the last bold char, when
9112 * using the bold trick in the GUI.
9113 */
9114 if (T_ND[0] != NUL && T_ND[1] == NUL)
9115 {
9116 while (i-- > 0)
9117 out_char(*T_ND);
9118 }
9119 else
9120 {
9121 int off;
9122
9123 off = LineOffset[row] + screen_cur_col;
9124 while (i-- > 0)
9125 {
9126 if (ScreenAttrs[off] != screen_attr)
9127 screen_stop_highlight();
9128#ifdef FEAT_MBYTE
9129 out_flush_check();
9130#endif
9131 out_char(ScreenLines[off]);
9132#ifdef FEAT_MBYTE
9133 if (enc_dbcs == DBCS_JPNU
9134 && ScreenLines[off] == 0x8e)
9135 out_char(ScreenLines2[off]);
9136#endif
9137 ++off;
9138 }
9139 }
9140 }
9141 }
9142 }
9143 else
9144 cost = 999;
9145
9146 if (cost >= goto_cost)
9147 {
9148 if (noinvcurs)
9149 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009150 if (row == screen_cur_row && (col > screen_cur_col)
9151 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 term_cursor_right(col - screen_cur_col);
9153 else
9154 term_windgoto(row, col);
9155 }
9156 screen_cur_row = row;
9157 screen_cur_col = col;
9158 }
9159}
9160
9161/*
9162 * Set cursor to its position in the current window.
9163 */
9164 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009165setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 if (redrawing())
9168 {
9169 validate_cursor();
9170 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9171 W_WINCOL(curwin) + (
9172#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009173 /* With 'rightleft' set and the cursor on a double-wide
9174 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9176# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009177 (has_mbyte
9178 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9179 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180# endif
9181 1)) :
9182#endif
9183 curwin->w_wcol));
9184 }
9185}
9186
9187
9188/*
9189 * insert 'line_count' lines at 'row' in window 'wp'
9190 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9191 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9192 * scrolling.
9193 * Returns FAIL if the lines are not inserted, OK for success.
9194 */
9195 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009196win_ins_lines(
9197 win_T *wp,
9198 int row,
9199 int line_count,
9200 int invalid,
9201 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int did_delete;
9204 int nextrow;
9205 int lastrow;
9206 int retval;
9207
9208 if (invalid)
9209 wp->w_lines_valid = 0;
9210
9211 if (wp->w_height < 5)
9212 return FAIL;
9213
9214 if (line_count > wp->w_height - row)
9215 line_count = wp->w_height - row;
9216
9217 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9218 if (retval != MAYBE)
9219 return retval;
9220
9221 /*
9222 * If there is a next window or a status line, we first try to delete the
9223 * lines at the bottom to avoid messing what is after the window.
9224 * If this fails and there are following windows, don't do anything to avoid
9225 * messing up those windows, better just redraw.
9226 */
9227 did_delete = FALSE;
9228#ifdef FEAT_WINDOWS
9229 if (wp->w_next != NULL || wp->w_status_height)
9230 {
9231 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9232 line_count, (int)Rows, FALSE, NULL) == OK)
9233 did_delete = TRUE;
9234 else if (wp->w_next)
9235 return FAIL;
9236 }
9237#endif
9238 /*
9239 * if no lines deleted, blank the lines that will end up below the window
9240 */
9241 if (!did_delete)
9242 {
9243#ifdef FEAT_WINDOWS
9244 wp->w_redr_status = TRUE;
9245#endif
9246 redraw_cmdline = TRUE;
9247 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9248 lastrow = nextrow + line_count;
9249 if (lastrow > Rows)
9250 lastrow = Rows;
9251 screen_fill(nextrow - line_count, lastrow - line_count,
9252 W_WINCOL(wp), (int)W_ENDCOL(wp),
9253 ' ', ' ', 0);
9254 }
9255
9256 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9257 == FAIL)
9258 {
9259 /* deletion will have messed up other windows */
9260 if (did_delete)
9261 {
9262#ifdef FEAT_WINDOWS
9263 wp->w_redr_status = TRUE;
9264#endif
9265 win_rest_invalid(W_NEXT(wp));
9266 }
9267 return FAIL;
9268 }
9269
9270 return OK;
9271}
9272
9273/*
9274 * delete "line_count" window lines at "row" in window "wp"
9275 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9276 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9277 * scrolling
9278 * Return OK for success, FAIL if the lines are not deleted.
9279 */
9280 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009281win_del_lines(
9282 win_T *wp,
9283 int row,
9284 int line_count,
9285 int invalid,
9286 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287{
9288 int retval;
9289
9290 if (invalid)
9291 wp->w_lines_valid = 0;
9292
9293 if (line_count > wp->w_height - row)
9294 line_count = wp->w_height - row;
9295
9296 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9297 if (retval != MAYBE)
9298 return retval;
9299
9300 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9301 (int)Rows, FALSE, NULL) == FAIL)
9302 return FAIL;
9303
9304#ifdef FEAT_WINDOWS
9305 /*
9306 * If there are windows or status lines below, try to put them at the
9307 * correct place. If we can't do that, they have to be redrawn.
9308 */
9309 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9310 {
9311 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9312 line_count, (int)Rows, NULL) == FAIL)
9313 {
9314 wp->w_redr_status = TRUE;
9315 win_rest_invalid(wp->w_next);
9316 }
9317 }
9318 /*
9319 * If this is the last window and there is no status line, redraw the
9320 * command line later.
9321 */
9322 else
9323#endif
9324 redraw_cmdline = TRUE;
9325 return OK;
9326}
9327
9328/*
9329 * Common code for win_ins_lines() and win_del_lines().
9330 * Returns OK or FAIL when the work has been done.
9331 * Returns MAYBE when not finished yet.
9332 */
9333 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009334win_do_lines(
9335 win_T *wp,
9336 int row,
9337 int line_count,
9338 int mayclear,
9339 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 int retval;
9342
9343 if (!redrawing() || line_count <= 0)
9344 return FAIL;
9345
9346 /* only a few lines left: redraw is faster */
9347 if (mayclear && Rows - line_count < 5
9348#ifdef FEAT_VERTSPLIT
9349 && wp->w_width == Columns
9350#endif
9351 )
9352 {
9353 screenclear(); /* will set wp->w_lines_valid to 0 */
9354 return FAIL;
9355 }
9356
9357 /*
9358 * Delete all remaining lines
9359 */
9360 if (row + line_count >= wp->w_height)
9361 {
9362 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9363 W_WINCOL(wp), (int)W_ENDCOL(wp),
9364 ' ', ' ', 0);
9365 return OK;
9366 }
9367
9368 /*
9369 * when scrolling, the message on the command line should be cleared,
9370 * otherwise it will stay there forever.
9371 */
9372 clear_cmdline = TRUE;
9373
9374 /*
9375 * If the terminal can set a scroll region, use that.
9376 * Always do this in a vertically split window. This will redraw from
9377 * ScreenLines[] when t_CV isn't defined. That's faster than using
9378 * win_line().
9379 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009380 * a character in the lower right corner of the scroll region may cause a
9381 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 */
9383 if (scroll_region
9384#ifdef FEAT_VERTSPLIT
9385 || W_WIDTH(wp) != Columns
9386#endif
9387 )
9388 {
9389#ifdef FEAT_VERTSPLIT
9390 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9391#endif
9392 scroll_region_set(wp, row);
9393 if (del)
9394 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9395 wp->w_height - row, FALSE, wp);
9396 else
9397 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9398 wp->w_height - row, wp);
9399#ifdef FEAT_VERTSPLIT
9400 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9401#endif
9402 scroll_region_reset();
9403 return retval;
9404 }
9405
9406#ifdef FEAT_WINDOWS
9407 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9408 return FAIL;
9409#endif
9410
9411 return MAYBE;
9412}
9413
9414/*
9415 * window 'wp' and everything after it is messed up, mark it for redraw
9416 */
9417 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009418win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420#ifdef FEAT_WINDOWS
9421 while (wp != NULL)
9422#else
9423 if (wp != NULL)
9424#endif
9425 {
9426 redraw_win_later(wp, NOT_VALID);
9427#ifdef FEAT_WINDOWS
9428 wp->w_redr_status = TRUE;
9429 wp = wp->w_next;
9430#endif
9431 }
9432 redraw_cmdline = TRUE;
9433}
9434
9435/*
9436 * The rest of the routines in this file perform screen manipulations. The
9437 * given operation is performed physically on the screen. The corresponding
9438 * change is also made to the internal screen image. In this way, the editor
9439 * anticipates the effect of editing changes on the appearance of the screen.
9440 * That way, when we call screenupdate a complete redraw isn't usually
9441 * necessary. Another advantage is that we can keep adding code to anticipate
9442 * screen changes, and in the meantime, everything still works.
9443 */
9444
9445/*
9446 * types for inserting or deleting lines
9447 */
9448#define USE_T_CAL 1
9449#define USE_T_CDL 2
9450#define USE_T_AL 3
9451#define USE_T_CE 4
9452#define USE_T_DL 5
9453#define USE_T_SR 6
9454#define USE_NL 7
9455#define USE_T_CD 8
9456#define USE_REDRAW 9
9457
9458/*
9459 * insert lines on the screen and update ScreenLines[]
9460 * 'end' is the line after the scrolled part. Normally it is Rows.
9461 * When scrolling region used 'off' is the offset from the top for the region.
9462 * 'row' and 'end' are relative to the start of the region.
9463 *
9464 * return FAIL for failure, OK for success.
9465 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009466 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009467screen_ins_lines(
9468 int off,
9469 int row,
9470 int line_count,
9471 int end,
9472 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
9474 int i;
9475 int j;
9476 unsigned temp;
9477 int cursor_row;
9478 int type;
9479 int result_empty;
9480 int can_ce = can_clear(T_CE);
9481
9482 /*
9483 * FAIL if
9484 * - there is no valid screen
9485 * - the screen has to be redrawn completely
9486 * - the line count is less than one
9487 * - the line count is more than 'ttyscroll'
9488 */
9489 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9490 return FAIL;
9491
9492 /*
9493 * There are seven ways to insert lines:
9494 * 0. When in a vertically split window and t_CV isn't set, redraw the
9495 * characters from ScreenLines[].
9496 * 1. Use T_CD (clear to end of display) if it exists and the result of
9497 * the insert is just empty lines
9498 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9499 * present or line_count > 1. It looks better if we do all the inserts
9500 * at once.
9501 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9502 * insert is just empty lines and T_CE is not present or line_count >
9503 * 1.
9504 * 4. Use T_AL (insert line) if it exists.
9505 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9506 * just empty lines.
9507 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9508 * just empty lines.
9509 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9510 * the 'da' flag is not set or we have clear line capability.
9511 * 8. redraw the characters from ScreenLines[].
9512 *
9513 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9514 * the scrollbar for the window. It does have insert line, use that if it
9515 * exists.
9516 */
9517 result_empty = (row + line_count >= end);
9518#ifdef FEAT_VERTSPLIT
9519 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9520 type = USE_REDRAW;
9521 else
9522#endif
9523 if (can_clear(T_CD) && result_empty)
9524 type = USE_T_CD;
9525 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9526 type = USE_T_CAL;
9527 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9528 type = USE_T_CDL;
9529 else if (*T_AL != NUL)
9530 type = USE_T_AL;
9531 else if (can_ce && result_empty)
9532 type = USE_T_CE;
9533 else if (*T_DL != NUL && result_empty)
9534 type = USE_T_DL;
9535 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9536 type = USE_T_SR;
9537 else
9538 return FAIL;
9539
9540 /*
9541 * For clearing the lines screen_del_lines() is used. This will also take
9542 * care of t_db if necessary.
9543 */
9544 if (type == USE_T_CD || type == USE_T_CDL ||
9545 type == USE_T_CE || type == USE_T_DL)
9546 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9547
9548 /*
9549 * If text is retained below the screen, first clear or delete as many
9550 * lines at the bottom of the window as are about to be inserted so that
9551 * the deleted lines won't later surface during a screen_del_lines.
9552 */
9553 if (*T_DB)
9554 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9555
9556#ifdef FEAT_CLIPBOARD
9557 /* Remove a modeless selection when inserting lines halfway the screen
9558 * or not the full width of the screen. */
9559 if (off + row > 0
9560# ifdef FEAT_VERTSPLIT
9561 || (wp != NULL && wp->w_width != Columns)
9562# endif
9563 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009564 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 else
9566 clip_scroll_selection(-line_count);
9567#endif
9568
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#ifdef FEAT_GUI
9570 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9571 * scrolling is actually carried out. */
9572 gui_dont_update_cursor();
9573#endif
9574
9575 if (*T_CCS != NUL) /* cursor relative to region */
9576 cursor_row = row;
9577 else
9578 cursor_row = row + off;
9579
9580 /*
9581 * Shift LineOffset[] line_count down to reflect the inserted lines.
9582 * Clear the inserted lines in ScreenLines[].
9583 */
9584 row += off;
9585 end += off;
9586 for (i = 0; i < line_count; ++i)
9587 {
9588#ifdef FEAT_VERTSPLIT
9589 if (wp != NULL && wp->w_width != Columns)
9590 {
9591 /* need to copy part of a line */
9592 j = end - 1 - i;
9593 while ((j -= line_count) >= row)
9594 linecopy(j + line_count, j, wp);
9595 j += line_count;
9596 if (can_clear((char_u *)" "))
9597 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9598 else
9599 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9600 LineWraps[j] = FALSE;
9601 }
9602 else
9603#endif
9604 {
9605 j = end - 1 - i;
9606 temp = LineOffset[j];
9607 while ((j -= line_count) >= row)
9608 {
9609 LineOffset[j + line_count] = LineOffset[j];
9610 LineWraps[j + line_count] = LineWraps[j];
9611 }
9612 LineOffset[j + line_count] = temp;
9613 LineWraps[j + line_count] = FALSE;
9614 if (can_clear((char_u *)" "))
9615 lineclear(temp, (int)Columns);
9616 else
9617 lineinvalid(temp, (int)Columns);
9618 }
9619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620
9621 screen_stop_highlight();
9622 windgoto(cursor_row, 0);
9623
9624#ifdef FEAT_VERTSPLIT
9625 /* redraw the characters */
9626 if (type == USE_REDRAW)
9627 redraw_block(row, end, wp);
9628 else
9629#endif
9630 if (type == USE_T_CAL)
9631 {
9632 term_append_lines(line_count);
9633 screen_start(); /* don't know where cursor is now */
9634 }
9635 else
9636 {
9637 for (i = 0; i < line_count; i++)
9638 {
9639 if (type == USE_T_AL)
9640 {
9641 if (i && cursor_row != 0)
9642 windgoto(cursor_row, 0);
9643 out_str(T_AL);
9644 }
9645 else /* type == USE_T_SR */
9646 out_str(T_SR);
9647 screen_start(); /* don't know where cursor is now */
9648 }
9649 }
9650
9651 /*
9652 * With scroll-reverse and 'da' flag set we need to clear the lines that
9653 * have been scrolled down into the region.
9654 */
9655 if (type == USE_T_SR && *T_DA)
9656 {
9657 for (i = 0; i < line_count; ++i)
9658 {
9659 windgoto(off + i, 0);
9660 out_str(T_CE);
9661 screen_start(); /* don't know where cursor is now */
9662 }
9663 }
9664
9665#ifdef FEAT_GUI
9666 gui_can_update_cursor();
9667 if (gui.in_use)
9668 out_flush(); /* always flush after a scroll */
9669#endif
9670 return OK;
9671}
9672
9673/*
9674 * delete lines on the screen and update ScreenLines[]
9675 * 'end' is the line after the scrolled part. Normally it is Rows.
9676 * When scrolling region used 'off' is the offset from the top for the region.
9677 * 'row' and 'end' are relative to the start of the region.
9678 *
9679 * Return OK for success, FAIL if the lines are not deleted.
9680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009682screen_del_lines(
9683 int off,
9684 int row,
9685 int line_count,
9686 int end,
9687 int force, /* even when line_count > p_ttyscroll */
9688 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 int j;
9691 int i;
9692 unsigned temp;
9693 int cursor_row;
9694 int cursor_end;
9695 int result_empty; /* result is empty until end of region */
9696 int can_delete; /* deleting line codes can be used */
9697 int type;
9698
9699 /*
9700 * FAIL if
9701 * - there is no valid screen
9702 * - the screen has to be redrawn completely
9703 * - the line count is less than one
9704 * - the line count is more than 'ttyscroll'
9705 */
9706 if (!screen_valid(TRUE) || line_count <= 0 ||
9707 (!force && line_count > p_ttyscroll))
9708 return FAIL;
9709
9710 /*
9711 * Check if the rest of the current region will become empty.
9712 */
9713 result_empty = row + line_count >= end;
9714
9715 /*
9716 * We can delete lines only when 'db' flag not set or when 'ce' option
9717 * available.
9718 */
9719 can_delete = (*T_DB == NUL || can_clear(T_CE));
9720
9721 /*
9722 * There are six ways to delete lines:
9723 * 0. When in a vertically split window and t_CV isn't set, redraw the
9724 * characters from ScreenLines[].
9725 * 1. Use T_CD if it exists and the result is empty.
9726 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9727 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9728 * none of the other ways work.
9729 * 4. Use T_CE (erase line) if the result is empty.
9730 * 5. Use T_DL (delete line) if it exists.
9731 * 6. redraw the characters from ScreenLines[].
9732 */
9733#ifdef FEAT_VERTSPLIT
9734 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9735 type = USE_REDRAW;
9736 else
9737#endif
9738 if (can_clear(T_CD) && result_empty)
9739 type = USE_T_CD;
9740#if defined(__BEOS__) && defined(BEOS_DR8)
9741 /*
9742 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9743 * its internal termcap... this works okay for tests which test *T_DB !=
9744 * NUL. It has the disadvantage that the user cannot use any :set t_*
9745 * command to get T_DB (back) to empty_option, only :set term=... will do
9746 * the trick...
9747 * Anyway, this hack will hopefully go away with the next OS release.
9748 * (Olaf Seibert)
9749 */
9750 else if (row == 0 && T_DB == empty_option
9751 && (line_count == 1 || *T_CDL == NUL))
9752#else
9753 else if (row == 0 && (
9754#ifndef AMIGA
9755 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9756 * up, so use delete-line command */
9757 line_count == 1 ||
9758#endif
9759 *T_CDL == NUL))
9760#endif
9761 type = USE_NL;
9762 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9763 type = USE_T_CDL;
9764 else if (can_clear(T_CE) && result_empty
9765#ifdef FEAT_VERTSPLIT
9766 && (wp == NULL || wp->w_width == Columns)
9767#endif
9768 )
9769 type = USE_T_CE;
9770 else if (*T_DL != NUL && can_delete)
9771 type = USE_T_DL;
9772 else if (*T_CDL != NUL && can_delete)
9773 type = USE_T_CDL;
9774 else
9775 return FAIL;
9776
9777#ifdef FEAT_CLIPBOARD
9778 /* Remove a modeless selection when deleting lines halfway the screen or
9779 * not the full width of the screen. */
9780 if (off + row > 0
9781# ifdef FEAT_VERTSPLIT
9782 || (wp != NULL && wp->w_width != Columns)
9783# endif
9784 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009785 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 else
9787 clip_scroll_selection(line_count);
9788#endif
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790#ifdef FEAT_GUI
9791 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9792 * scrolling is actually carried out. */
9793 gui_dont_update_cursor();
9794#endif
9795
9796 if (*T_CCS != NUL) /* cursor relative to region */
9797 {
9798 cursor_row = row;
9799 cursor_end = end;
9800 }
9801 else
9802 {
9803 cursor_row = row + off;
9804 cursor_end = end + off;
9805 }
9806
9807 /*
9808 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9809 * Clear the inserted lines in ScreenLines[].
9810 */
9811 row += off;
9812 end += off;
9813 for (i = 0; i < line_count; ++i)
9814 {
9815#ifdef FEAT_VERTSPLIT
9816 if (wp != NULL && wp->w_width != Columns)
9817 {
9818 /* need to copy part of a line */
9819 j = row + i;
9820 while ((j += line_count) <= end - 1)
9821 linecopy(j - line_count, j, wp);
9822 j -= line_count;
9823 if (can_clear((char_u *)" "))
9824 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9825 else
9826 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9827 LineWraps[j] = FALSE;
9828 }
9829 else
9830#endif
9831 {
9832 /* whole width, moving the line pointers is faster */
9833 j = row + i;
9834 temp = LineOffset[j];
9835 while ((j += line_count) <= end - 1)
9836 {
9837 LineOffset[j - line_count] = LineOffset[j];
9838 LineWraps[j - line_count] = LineWraps[j];
9839 }
9840 LineOffset[j - line_count] = temp;
9841 LineWraps[j - line_count] = FALSE;
9842 if (can_clear((char_u *)" "))
9843 lineclear(temp, (int)Columns);
9844 else
9845 lineinvalid(temp, (int)Columns);
9846 }
9847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848
9849 screen_stop_highlight();
9850
9851#ifdef FEAT_VERTSPLIT
9852 /* redraw the characters */
9853 if (type == USE_REDRAW)
9854 redraw_block(row, end, wp);
9855 else
9856#endif
9857 if (type == USE_T_CD) /* delete the lines */
9858 {
9859 windgoto(cursor_row, 0);
9860 out_str(T_CD);
9861 screen_start(); /* don't know where cursor is now */
9862 }
9863 else if (type == USE_T_CDL)
9864 {
9865 windgoto(cursor_row, 0);
9866 term_delete_lines(line_count);
9867 screen_start(); /* don't know where cursor is now */
9868 }
9869 /*
9870 * Deleting lines at top of the screen or scroll region: Just scroll
9871 * the whole screen (scroll region) up by outputting newlines on the
9872 * last line.
9873 */
9874 else if (type == USE_NL)
9875 {
9876 windgoto(cursor_end - 1, 0);
9877 for (i = line_count; --i >= 0; )
9878 out_char('\n'); /* cursor will remain on same line */
9879 }
9880 else
9881 {
9882 for (i = line_count; --i >= 0; )
9883 {
9884 if (type == USE_T_DL)
9885 {
9886 windgoto(cursor_row, 0);
9887 out_str(T_DL); /* delete a line */
9888 }
9889 else /* type == USE_T_CE */
9890 {
9891 windgoto(cursor_row + i, 0);
9892 out_str(T_CE); /* erase a line */
9893 }
9894 screen_start(); /* don't know where cursor is now */
9895 }
9896 }
9897
9898 /*
9899 * If the 'db' flag is set, we need to clear the lines that have been
9900 * scrolled up at the bottom of the region.
9901 */
9902 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9903 {
9904 for (i = line_count; i > 0; --i)
9905 {
9906 windgoto(cursor_end - i, 0);
9907 out_str(T_CE); /* erase a line */
9908 screen_start(); /* don't know where cursor is now */
9909 }
9910 }
9911
9912#ifdef FEAT_GUI
9913 gui_can_update_cursor();
9914 if (gui.in_use)
9915 out_flush(); /* always flush after a scroll */
9916#endif
9917
9918 return OK;
9919}
9920
9921/*
9922 * show the current mode and ruler
9923 *
9924 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9925 * If clear_cmdline is FALSE there may be a message there that needs to be
9926 * cleared only if a mode is shown.
9927 * Return the length of the message (0 if no message).
9928 */
9929 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009930showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931{
9932 int need_clear;
9933 int length = 0;
9934 int do_mode;
9935 int attr;
9936 int nwr_save;
9937#ifdef FEAT_INS_EXPAND
9938 int sub_attr;
9939#endif
9940
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009941 do_mode = ((p_smd && msg_silent == 0)
9942 && ((State & INSERT)
9943 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009944 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (do_mode || Recording)
9946 {
9947 /*
9948 * Don't show mode right now, when not redrawing or inside a mapping.
9949 * Call char_avail() only when we are going to show something, because
9950 * it takes a bit of time.
9951 */
9952 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9953 {
9954 redraw_cmdline = TRUE; /* show mode later */
9955 return 0;
9956 }
9957
9958 nwr_save = need_wait_return;
9959
9960 /* wait a bit before overwriting an important message */
9961 check_for_delay(FALSE);
9962
9963 /* if the cmdline is more than one line high, erase top lines */
9964 need_clear = clear_cmdline;
9965 if (clear_cmdline && cmdline_row < Rows - 1)
9966 msg_clr_cmdline(); /* will reset clear_cmdline */
9967
9968 /* Position on the last line in the window, column 0 */
9969 msg_pos_mode();
9970 cursor_off();
9971 attr = hl_attr(HLF_CM); /* Highlight mode */
9972 if (do_mode)
9973 {
9974 MSG_PUTS_ATTR("--", attr);
9975#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009976 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009977# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009978 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009979# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009980 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009981# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009982 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009983# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 MSG_PUTS_ATTR(" IM", attr);
9985# else
9986 MSG_PUTS_ATTR(" XIM", attr);
9987# endif
9988#endif
9989#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9990 if (gui.in_use)
9991 {
9992 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009993 {
9994 /* HANGUL */
9995 if (enc_utf8)
9996 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
9997 else
9998 MSG_PUTS_ATTR(" \307\321\261\333", attr);
9999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 }
10001#endif
10002#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010003 /* CTRL-X in Insert mode */
10004 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
10006 /* These messages can get long, avoid a wrap in a narrow
10007 * window. Prefer showing edit_submode_extra. */
10008 length = (Rows - msg_row) * Columns - 3;
10009 if (edit_submode_extra != NULL)
10010 length -= vim_strsize(edit_submode_extra);
10011 if (length > 0)
10012 {
10013 if (edit_submode_pre != NULL)
10014 length -= vim_strsize(edit_submode_pre);
10015 if (length - vim_strsize(edit_submode) > 0)
10016 {
10017 if (edit_submode_pre != NULL)
10018 msg_puts_attr(edit_submode_pre, attr);
10019 msg_puts_attr(edit_submode, attr);
10020 }
10021 if (edit_submode_extra != NULL)
10022 {
10023 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10024 if ((int)edit_submode_highl < (int)HLF_COUNT)
10025 sub_attr = hl_attr(edit_submode_highl);
10026 else
10027 sub_attr = attr;
10028 msg_puts_attr(edit_submode_extra, sub_attr);
10029 }
10030 }
10031 length = 0;
10032 }
10033 else
10034#endif
10035 {
10036#ifdef FEAT_VREPLACE
10037 if (State & VREPLACE_FLAG)
10038 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10039 else
10040#endif
10041 if (State & REPLACE_FLAG)
10042 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10043 else if (State & INSERT)
10044 {
10045#ifdef FEAT_RIGHTLEFT
10046 if (p_ri)
10047 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10048#endif
10049 MSG_PUTS_ATTR(_(" INSERT"), attr);
10050 }
10051 else if (restart_edit == 'I')
10052 MSG_PUTS_ATTR(_(" (insert)"), attr);
10053 else if (restart_edit == 'R')
10054 MSG_PUTS_ATTR(_(" (replace)"), attr);
10055 else if (restart_edit == 'V')
10056 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10057#ifdef FEAT_RIGHTLEFT
10058 if (p_hkmap)
10059 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10060# ifdef FEAT_FKMAP
10061 if (p_fkmap)
10062 MSG_PUTS_ATTR(farsi_text_5, attr);
10063# endif
10064#endif
10065#ifdef FEAT_KEYMAP
10066 if (State & LANGMAP)
10067 {
10068# ifdef FEAT_ARABIC
10069 if (curwin->w_p_arab)
10070 MSG_PUTS_ATTR(_(" Arabic"), attr);
10071 else
10072# endif
10073 MSG_PUTS_ATTR(_(" (lang)"), attr);
10074 }
10075#endif
10076 if ((State & INSERT) && p_paste)
10077 MSG_PUTS_ATTR(_(" (paste)"), attr);
10078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 if (VIsual_active)
10080 {
10081 char *p;
10082
10083 /* Don't concatenate separate words to avoid translation
10084 * problems. */
10085 switch ((VIsual_select ? 4 : 0)
10086 + (VIsual_mode == Ctrl_V) * 2
10087 + (VIsual_mode == 'V'))
10088 {
10089 case 0: p = N_(" VISUAL"); break;
10090 case 1: p = N_(" VISUAL LINE"); break;
10091 case 2: p = N_(" VISUAL BLOCK"); break;
10092 case 4: p = N_(" SELECT"); break;
10093 case 5: p = N_(" SELECT LINE"); break;
10094 default: p = N_(" SELECT BLOCK"); break;
10095 }
10096 MSG_PUTS_ATTR(_(p), attr);
10097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 MSG_PUTS_ATTR(" --", attr);
10099 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 need_clear = TRUE;
10102 }
10103 if (Recording
10104#ifdef FEAT_INS_EXPAND
10105 && edit_submode == NULL /* otherwise it gets too long */
10106#endif
10107 )
10108 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010109 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 need_clear = TRUE;
10111 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010112
10113 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 if (need_clear || clear_cmdline)
10115 msg_clr_eos();
10116 msg_didout = FALSE; /* overwrite this message */
10117 length = msg_col;
10118 msg_col = 0;
10119 need_wait_return = nwr_save; /* never ask for hit-return for this */
10120 }
10121 else if (clear_cmdline && msg_silent == 0)
10122 /* Clear the whole command line. Will reset "clear_cmdline". */
10123 msg_clr_cmdline();
10124
10125#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 /* In Visual mode the size of the selected area must be redrawn. */
10127 if (VIsual_active)
10128 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
10130 /* If the last window has no status line, the ruler is after the mode
10131 * message and must be redrawn */
10132 if (redrawing()
10133# ifdef FEAT_WINDOWS
10134 && lastwin->w_status_height == 0
10135# endif
10136 )
10137 win_redr_ruler(lastwin, TRUE);
10138#endif
10139 redraw_cmdline = FALSE;
10140 clear_cmdline = FALSE;
10141
10142 return length;
10143}
10144
10145/*
10146 * Position for a mode message.
10147 */
10148 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010149msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 msg_col = 0;
10152 msg_row = Rows - 1;
10153}
10154
10155/*
10156 * Delete mode message. Used when ESC is typed which is expected to end
10157 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010158 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 */
10160 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010161unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
10163 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010164 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 */
10166 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10167 redraw_cmdline = TRUE; /* delete mode later */
10168 else
10169 {
10170 msg_pos_mode();
10171 if (Recording)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010172 recording_mode(hl_attr(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 msg_clr_eos();
10174 }
10175}
10176
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010177 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010178recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010179{
10180 MSG_PUTS_ATTR(_("recording"), attr);
10181 if (!shortmess(SHM_RECORDING))
10182 {
10183 char_u s[4];
10184 sprintf((char *)s, " @%c", Recording);
10185 MSG_PUTS_ATTR(s, attr);
10186 }
10187}
10188
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010189#if defined(FEAT_WINDOWS)
10190/*
10191 * Draw the tab pages line at the top of the Vim window.
10192 */
10193 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010194draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010195{
10196 int tabcount = 0;
10197 tabpage_T *tp;
10198 int tabwidth;
10199 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010200 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010201 int attr;
10202 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010203 win_T *cwp;
10204 int wincount;
10205 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010206 int c;
10207 int len;
10208 int attr_sel = hl_attr(HLF_TPS);
10209 int attr_nosel = hl_attr(HLF_TP);
10210 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010211 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010212 int room;
10213 int use_sep_chars = (t_colors < 8
10214#ifdef FEAT_GUI
10215 && !gui.in_use
10216#endif
10217 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010218
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010219 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010220
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010221#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010222 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010223 if (gui_use_tabline())
10224 {
10225 gui_update_tabline();
10226 return;
10227 }
10228#endif
10229
10230 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010231 return;
10232
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010233#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010234
10235 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10236 for (scol = 0; scol < Columns; ++scol)
10237 TabPageIdxs[scol] = 0;
10238
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010239 /* Use the 'tabline' option if it's set. */
10240 if (*p_tal != NUL)
10241 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010242 int save_called_emsg = called_emsg;
10243
10244 /* Check for an error. If there is one we would loop in redrawing the
10245 * screen. Avoid that by making 'tabline' empty. */
10246 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010247 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010248 if (called_emsg)
10249 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010250 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010251 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010252 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010253 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010254#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010255 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010256 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10257 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010258
Bram Moolenaar238a5642006-02-21 22:12:05 +000010259 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10260 if (tabwidth < 6)
10261 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010262
Bram Moolenaar238a5642006-02-21 22:12:05 +000010263 attr = attr_nosel;
10264 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010265 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010266 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10267 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010268 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010269 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010270
Bram Moolenaar238a5642006-02-21 22:12:05 +000010271 if (tp->tp_topframe == topframe)
10272 attr = attr_sel;
10273 if (use_sep_chars && col > 0)
10274 screen_putchar('|', 0, col++, attr);
10275
10276 if (tp->tp_topframe != topframe)
10277 attr = attr_nosel;
10278
10279 screen_putchar(' ', 0, col++, attr);
10280
10281 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010282 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010283 cwp = curwin;
10284 wp = firstwin;
10285 }
10286 else
10287 {
10288 cwp = tp->tp_curwin;
10289 wp = tp->tp_firstwin;
10290 }
10291
10292 modified = FALSE;
10293 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10294 if (bufIsChanged(wp->w_buffer))
10295 modified = TRUE;
10296 if (modified || wincount > 1)
10297 {
10298 if (wincount > 1)
10299 {
10300 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010301 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010302 if (col + len >= Columns - 3)
10303 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010304 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010305#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010306 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010307#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010308 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010309#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010310 );
10311 col += len;
10312 }
10313 if (modified)
10314 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10315 screen_putchar(' ', 0, col++, attr);
10316 }
10317
10318 room = scol - col + tabwidth - 1;
10319 if (room > 0)
10320 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010321 /* Get buffer name in NameBuff[] */
10322 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010323 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010324 len = vim_strsize(NameBuff);
10325 p = NameBuff;
10326#ifdef FEAT_MBYTE
10327 if (has_mbyte)
10328 while (len > room)
10329 {
10330 len -= ptr2cells(p);
10331 mb_ptr_adv(p);
10332 }
10333 else
10334#endif
10335 if (len > room)
10336 {
10337 p += len - room;
10338 len = room;
10339 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010340 if (len > Columns - col - 1)
10341 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010343 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010344 col += len;
10345 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010346 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010347
10348 /* Store the tab page number in TabPageIdxs[], so that
10349 * jump_to_mouse() knows where each one is. */
10350 ++tabcount;
10351 while (scol < col)
10352 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010353 }
10354
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355 if (use_sep_chars)
10356 c = '_';
10357 else
10358 c = ' ';
10359 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010360
10361 /* Put an "X" for closing the current tab if there are several. */
10362 if (first_tabpage->tp_next != NULL)
10363 {
10364 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10365 TabPageIdxs[Columns - 1] = -999;
10366 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010367 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010368
10369 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10370 * set. */
10371 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010372}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010373
10374/*
10375 * Get buffer name for "buf" into NameBuff[].
10376 * Takes care of special buffer names and translates special characters.
10377 */
10378 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010379get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010380{
10381 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010382 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010383 else
10384 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10385 trans_characters(NameBuff, MAXPATHL);
10386}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387#endif
10388
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10390/*
10391 * Get the character to use in a status line. Get its attributes in "*attr".
10392 */
10393 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010394fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395{
10396 int fill;
10397 if (is_curwin)
10398 {
10399 *attr = hl_attr(HLF_S);
10400 fill = fill_stl;
10401 }
10402 else
10403 {
10404 *attr = hl_attr(HLF_SNC);
10405 fill = fill_stlnc;
10406 }
10407 /* Use fill when there is highlighting, and highlighting of current
10408 * window differs, or the fillchars differ, or this is not the
10409 * current window */
10410 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10411 || !is_curwin || firstwin == lastwin)
10412 || (fill_stl != fill_stlnc)))
10413 return fill;
10414 if (is_curwin)
10415 return '^';
10416 return '=';
10417}
10418#endif
10419
10420#ifdef FEAT_VERTSPLIT
10421/*
10422 * Get the character to use in a separator between vertically split windows.
10423 * Get its attributes in "*attr".
10424 */
10425 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010426fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428 *attr = hl_attr(HLF_C);
10429 if (*attr == 0 && fill_vert == ' ')
10430 return '|';
10431 else
10432 return fill_vert;
10433}
10434#endif
10435
10436/*
10437 * Return TRUE if redrawing should currently be done.
10438 */
10439 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010440redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 return (!RedrawingDisabled
10443 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10444}
10445
10446/*
10447 * Return TRUE if printing messages should currently be done.
10448 */
10449 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010450messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451{
10452 return (!(p_lz && char_avail() && !KeyTyped));
10453}
10454
10455/*
10456 * Show current status info in ruler and various other places
10457 * If always is FALSE, only show ruler if position has changed.
10458 */
10459 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010460showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461{
10462 if (!always && !redrawing())
10463 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010464#ifdef FEAT_INS_EXPAND
10465 if (pum_visible())
10466 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010467# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010468 /* Don't redraw right now, do it later. */
10469 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010470# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010471 return;
10472 }
10473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010475 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010476 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010477 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 else
10480#endif
10481#ifdef FEAT_CMDL_INFO
10482 win_redr_ruler(curwin, always);
10483#endif
10484
10485#ifdef FEAT_TITLE
10486 if (need_maketitle
10487# ifdef FEAT_STL_OPT
10488 || (p_icon && (stl_syntax & STL_IN_ICON))
10489 || (p_title && (stl_syntax & STL_IN_TITLE))
10490# endif
10491 )
10492 maketitle();
10493#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010494#ifdef FEAT_WINDOWS
10495 /* Redraw the tab pages line if needed. */
10496 if (redraw_tabline)
10497 draw_tabline();
10498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499}
10500
10501#ifdef FEAT_CMDL_INFO
10502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010503win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010505#define RULER_BUF_LEN 70
10506 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 int row;
10508 int fillchar;
10509 int attr;
10510 int empty_line = FALSE;
10511 colnr_T virtcol;
10512 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010513 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 int o;
10515#ifdef FEAT_VERTSPLIT
10516 int this_ru_col;
10517 int off = 0;
10518 int width = Columns;
10519# define WITH_OFF(x) x
10520# define WITH_WIDTH(x) x
10521#else
10522# define WITH_OFF(x) 0
10523# define WITH_WIDTH(x) Columns
10524# define this_ru_col ru_col
10525#endif
10526
10527 /* If 'ruler' off or redrawing disabled, don't do anything */
10528 if (!p_ru)
10529 return;
10530
10531 /*
10532 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10533 * after deleting lines, before cursor.lnum is corrected.
10534 */
10535 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10536 return;
10537
10538#ifdef FEAT_INS_EXPAND
10539 /* Don't draw the ruler while doing insert-completion, it might overwrite
10540 * the (long) mode message. */
10541# ifdef FEAT_WINDOWS
10542 if (wp == lastwin && lastwin->w_status_height == 0)
10543# endif
10544 if (edit_submode != NULL)
10545 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010546 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10547 if (pum_visible())
10548 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549#endif
10550
10551#ifdef FEAT_STL_OPT
10552 if (*p_ruf)
10553 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010554 int save_called_emsg = called_emsg;
10555
10556 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 if (called_emsg)
10559 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010560 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 return;
10563 }
10564#endif
10565
10566 /*
10567 * Check if not in Insert mode and the line is empty (will show "0-1").
10568 */
10569 if (!(State & INSERT)
10570 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10571 empty_line = TRUE;
10572
10573 /*
10574 * Only draw the ruler when something changed.
10575 */
10576 validate_virtcol_win(wp);
10577 if ( redraw_cmdline
10578 || always
10579 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10580 || wp->w_cursor.col != wp->w_ru_cursor.col
10581 || wp->w_virtcol != wp->w_ru_virtcol
10582#ifdef FEAT_VIRTUALEDIT
10583 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10584#endif
10585 || wp->w_topline != wp->w_ru_topline
10586 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10587#ifdef FEAT_DIFF
10588 || wp->w_topfill != wp->w_ru_topfill
10589#endif
10590 || empty_line != wp->w_ru_empty)
10591 {
10592 cursor_off();
10593#ifdef FEAT_WINDOWS
10594 if (wp->w_status_height)
10595 {
10596 row = W_WINROW(wp) + wp->w_height;
10597 fillchar = fillchar_status(&attr, wp == curwin);
10598# ifdef FEAT_VERTSPLIT
10599 off = W_WINCOL(wp);
10600 width = W_WIDTH(wp);
10601# endif
10602 }
10603 else
10604#endif
10605 {
10606 row = Rows - 1;
10607 fillchar = ' ';
10608 attr = 0;
10609#ifdef FEAT_VERTSPLIT
10610 width = Columns;
10611 off = 0;
10612#endif
10613 }
10614
10615 /* In list mode virtcol needs to be recomputed */
10616 virtcol = wp->w_virtcol;
10617 if (wp->w_p_list && lcs_tab1 == NUL)
10618 {
10619 wp->w_p_list = FALSE;
10620 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10621 wp->w_p_list = TRUE;
10622 }
10623
10624 /*
10625 * Some sprintfs return the length, some return a pointer.
10626 * To avoid portability problems we use strlen() here.
10627 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010628 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10630 ? 0L
10631 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010632 len = STRLEN(buffer);
10633 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10635 (int)virtcol + 1);
10636
10637 /*
10638 * Add a "50%" if there is room for it.
10639 * On the last line, don't print in the last column (scrolls the
10640 * screen up on some terminals).
10641 */
10642 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010643 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 o = i + vim_strsize(buffer + i + 1);
10645#ifdef FEAT_WINDOWS
10646 if (wp->w_status_height == 0) /* can't use last char of screen */
10647#endif
10648 ++o;
10649#ifdef FEAT_VERTSPLIT
10650 this_ru_col = ru_col - (Columns - width);
10651 if (this_ru_col < 0)
10652 this_ru_col = 0;
10653#endif
10654 /* Never use more than half the window/screen width, leave the other
10655 * half for the filename. */
10656 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10657 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10658 if (this_ru_col + o < WITH_WIDTH(width))
10659 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010660 /* need at least 3 chars left for get_rel_pos() + NUL */
10661 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 {
10663#ifdef FEAT_MBYTE
10664 if (has_mbyte)
10665 i += (*mb_char2bytes)(fillchar, buffer + i);
10666 else
10667#endif
10668 buffer[i++] = fillchar;
10669 ++o;
10670 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010671 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 }
10673 /* Truncate at window boundary. */
10674#ifdef FEAT_MBYTE
10675 if (has_mbyte)
10676 {
10677 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010678 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 {
10680 o += (*mb_ptr2cells)(buffer + i);
10681 if (this_ru_col + o > WITH_WIDTH(width))
10682 {
10683 buffer[i] = NUL;
10684 break;
10685 }
10686 }
10687 }
10688 else
10689#endif
10690 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10691 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10692
10693 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10694 i = redraw_cmdline;
10695 screen_fill(row, row + 1,
10696 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10697 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10698 fillchar, fillchar, attr);
10699 /* don't redraw the cmdline because of showing the ruler */
10700 redraw_cmdline = i;
10701 wp->w_ru_cursor = wp->w_cursor;
10702 wp->w_ru_virtcol = wp->w_virtcol;
10703 wp->w_ru_empty = empty_line;
10704 wp->w_ru_topline = wp->w_topline;
10705 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10706#ifdef FEAT_DIFF
10707 wp->w_ru_topfill = wp->w_topfill;
10708#endif
10709 }
10710}
10711#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010712
10713#if defined(FEAT_LINEBREAK) || defined(PROTO)
10714/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010715 * Return the width of the 'number' and 'relativenumber' column.
10716 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010717 * Otherwise it depends on 'numberwidth' and the line count.
10718 */
10719 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010720number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010721{
10722 int n;
10723 linenr_T lnum;
10724
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010725 if (wp->w_p_rnu && !wp->w_p_nu)
10726 /* cursor line shows "0" */
10727 lnum = wp->w_height;
10728 else
10729 /* cursor line shows absolute line number */
10730 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010731
Bram Moolenaar6b314672015-03-20 15:42:10 +010010732 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010733 return wp->w_nrwidth_width;
10734 wp->w_nrwidth_line_count = lnum;
10735
10736 n = 0;
10737 do
10738 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010739 lnum /= 10;
10740 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010741 } while (lnum > 0);
10742
10743 /* 'numberwidth' gives the minimal width plus one */
10744 if (n < wp->w_p_nuw - 1)
10745 n = wp->w_p_nuw - 1;
10746
10747 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010748 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010749 return n;
10750}
10751#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010752
10753/*
10754 * Return the current cursor column. This is the actual position on the
10755 * screen. First column is 0.
10756 */
10757 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010758screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010759{
10760 return screen_cur_col;
10761}
10762
10763/*
10764 * Return the current cursor row. This is the actual position on the screen.
10765 * First row is 0.
10766 */
10767 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010768screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010769{
10770 return screen_cur_row;
10771}