blob: 8044079b52ec9066b4f2b51916bd3c63fc3fe7f2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
419redraw_after_callback()
420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
425 else if ((State & NORMAL) || (State & INSERT))
426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
435 gui_update_cursor(TRUE, FALSE);
436 gui_mch_flush();
437 }
438#endif
439}
440
441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 * Changed something in the current window, at buffer line "lnum", that
443 * requires that line and possibly other lines to be redrawn.
444 * Used when entering/leaving Insert mode with the cursor on a folded line.
445 * Used to remove the "$" from a change command.
446 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
447 * may become invalid and the whole window will have to be redrawn.
448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100450redrawWinline(
451 linenr_T lnum,
452 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454#ifdef FEAT_FOLDING
455 int i;
456#endif
457
458 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
459 curwin->w_redraw_top = lnum;
460 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
461 curwin->w_redraw_bot = lnum;
462 redraw_later(VALID);
463
464#ifdef FEAT_FOLDING
465 if (invalid)
466 {
467 /* A w_lines[] entry for this lnum has become invalid. */
468 i = find_wl_entry(curwin, lnum);
469 if (i >= 0)
470 curwin->w_lines[i].wl_valid = FALSE;
471 }
472#endif
473}
474
475/*
476 * update all windows that are editing the current buffer
477 */
478 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100479update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 redraw_curbuf_later(type);
482 update_screen(type);
483}
484
485/*
486 * update_screen()
487 *
488 * Based on the current value of curwin->w_topline, transfer a screenfull
489 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
490 */
491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 win_T *wp;
495 static int did_intro = FALSE;
496#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
497 int did_one;
498#endif
499
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000500 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (!screen_valid(TRUE))
502 return;
503
504 if (must_redraw)
505 {
506 if (type < must_redraw) /* use maximal type */
507 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000508
509 /* must_redraw is reset here, so that when we run into some weird
510 * reason to redraw while busy redrawing (e.g., asynchronous
511 * scrolling), or update_topline() in win_update() will cause a
512 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 must_redraw = 0;
514 }
515
516 /* Need to update w_lines[]. */
517 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
518 type = NOT_VALID;
519
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000520 /* Postpone the redrawing when it's not needed and when being called
521 * recursively. */
522 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 redraw_later(type); /* remember type for next time */
525 must_redraw = type;
526 if (type > INVERTED_ALL)
527 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
528 return;
529 }
530
531 updating_screen = TRUE;
532#ifdef FEAT_SYN_HL
533 ++display_tick; /* let syntax code know we're in a next round of
534 * display updating */
535#endif
536
537 /*
538 * if the screen was scrolled up when displaying a message, scroll it down
539 */
540 if (msg_scrolled)
541 {
542 clear_cmdline = TRUE;
543 if (msg_scrolled > Rows - 5) /* clearing is faster */
544 type = CLEAR;
545 else if (type != CLEAR)
546 {
547 check_for_delay(FALSE);
548 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
549 type = CLEAR;
550 FOR_ALL_WINDOWS(wp)
551 {
552 if (W_WINROW(wp) < msg_scrolled)
553 {
554 if (W_WINROW(wp) + wp->w_height > msg_scrolled
555 && wp->w_redr_type < REDRAW_TOP
556 && wp->w_lines_valid > 0
557 && wp->w_topline == wp->w_lines[0].wl_lnum)
558 {
559 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
560 wp->w_redr_type = REDRAW_TOP;
561 }
562 else
563 {
564 wp->w_redr_type = NOT_VALID;
565#ifdef FEAT_WINDOWS
566 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
567 <= msg_scrolled)
568 wp->w_redr_status = TRUE;
569#endif
570 }
571 }
572 }
573 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000574#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000575 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578 msg_scrolled = 0;
579 need_wait_return = FALSE;
580 }
581
582 /* reset cmdline_row now (may have been changed temporarily) */
583 compute_cmdrow();
584
585 /* Check for changed highlighting */
586 if (need_highlight_changed)
587 highlight_changed();
588
589 if (type == CLEAR) /* first clear screen */
590 {
591 screenclear(); /* will reset clear_cmdline */
592 type = NOT_VALID;
593 }
594
595 if (clear_cmdline) /* going to clear cmdline (done below) */
596 check_for_delay(FALSE);
597
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000598#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200599 /* Force redraw when width of 'number' or 'relativenumber' column
600 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000601 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200602 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
603 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604 curwin->w_redr_type = NOT_VALID;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 /*
608 * Only start redrawing if there is really something to do.
609 */
610 if (type == INVERTED)
611 update_curswant();
612 if (curwin->w_redr_type < type
613 && !((type == VALID
614 && curwin->w_lines[0].wl_valid
615#ifdef FEAT_DIFF
616 && curwin->w_topfill == curwin->w_old_topfill
617 && curwin->w_botfill == curwin->w_old_botfill
618#endif
619 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000621 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
623 && curwin->w_old_visual_mode == VIsual_mode
624 && (curwin->w_valid & VALID_VIRTCOL)
625 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ))
627 curwin->w_redr_type = type;
628
Bram Moolenaar5a305422006-04-28 22:38:25 +0000629#ifdef FEAT_WINDOWS
630 /* Redraw the tab pages line if needed. */
631 if (redraw_tabline || type >= NOT_VALID)
632 draw_tabline();
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_SYN_HL
636 /*
637 * Correct stored syntax highlighting info for changes in each displayed
638 * buffer. Each buffer must only be done once.
639 */
640 FOR_ALL_WINDOWS(wp)
641 {
642 if (wp->w_buffer->b_mod_set)
643 {
644# ifdef FEAT_WINDOWS
645 win_T *wwp;
646
647 /* Check if we already did this buffer. */
648 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
649 if (wwp->w_buffer == wp->w_buffer)
650 break;
651# endif
652 if (
653# ifdef FEAT_WINDOWS
654 wwp == wp &&
655# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200656 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 syn_stack_apply_changes(wp->w_buffer);
658 }
659 }
660#endif
661
662 /*
663 * Go from top to bottom through the windows, redrawing the ones that need
664 * it.
665 */
666#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
667 did_one = FALSE;
668#endif
669#ifdef FEAT_SEARCH_EXTRA
670 search_hl.rm.regprog = NULL;
671#endif
672 FOR_ALL_WINDOWS(wp)
673 {
674 if (wp->w_redr_type != 0)
675 {
676 cursor_off();
677#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
678 if (!did_one)
679 {
680 did_one = TRUE;
681# ifdef FEAT_SEARCH_EXTRA
682 start_search_hl();
683# endif
684# ifdef FEAT_CLIPBOARD
685 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200686 if (clip_star.available && clip_isautosel_star())
687 clip_update_selection(&clip_star);
688 if (clip_plus.available && clip_isautosel_plus())
689 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
691#ifdef FEAT_GUI
692 /* Remove the cursor before starting to do anything, because
693 * scrolling may make it difficult to redraw the text under
694 * it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698 }
699#endif
700 win_update(wp);
701 }
702
703#ifdef FEAT_WINDOWS
704 /* redraw status line after the window to minimize cursor movement */
705 if (wp->w_redr_status)
706 {
707 cursor_off();
708 win_redr_status(wp);
709 }
710#endif
711 }
712#if defined(FEAT_SEARCH_EXTRA)
713 end_search_hl();
714#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100715#ifdef FEAT_INS_EXPAND
716 /* May need to redraw the popup menu. */
717 if (pum_visible())
718 pum_redraw();
719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721#ifdef FEAT_WINDOWS
722 /* Reset b_mod_set flags. Going through all windows is probably faster
723 * than going through all buffers (there could be many buffers). */
724 for (wp = firstwin; wp != NULL; wp = wp->w_next)
725 wp->w_buffer->b_mod_set = FALSE;
726#else
727 curbuf->b_mod_set = FALSE;
728#endif
729
730 updating_screen = FALSE;
731#ifdef FEAT_GUI
732 gui_may_resize_shell();
733#endif
734
735 /* Clear or redraw the command line. Done last, because scrolling may
736 * mess up the command line. */
737 if (clear_cmdline || redraw_cmdline)
738 showmode();
739
740 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200741 if (!did_intro)
742 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 did_intro = TRUE;
744
745#ifdef FEAT_GUI
746 /* Redraw the cursor and update the scrollbars when all screen updating is
747 * done. */
748 if (gui.in_use)
749 {
750 out_flush(); /* required before updating the cursor */
751 if (did_one)
752 gui_update_cursor(FALSE, FALSE);
753 gui_update_scrollbars(FALSE);
754 }
755#endif
756}
757
Bram Moolenaar860cae12010-06-05 23:22:07 +0200758#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200759/*
760 * Return TRUE if the cursor line in window "wp" may be concealed, according
761 * to the 'concealcursor' option.
762 */
763 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100764conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200765{
766 int c;
767
768 if (*wp->w_p_cocu == NUL)
769 return FALSE;
770 if (get_real_state() & VISUAL)
771 c = 'v';
772 else if (State & INSERT)
773 c = 'i';
774 else if (State & NORMAL)
775 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200776 else if (State & CMDLINE)
777 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200778 else
779 return FALSE;
780 return vim_strchr(wp->w_p_cocu, c) != NULL;
781}
782
783/*
784 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
785 */
786 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100787conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200788{
789 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
790 {
791 need_cursor_line_redraw = TRUE;
792 /* Need to recompute cursor column, e.g., when starting Visual mode
793 * without concealing. */
794 curs_columns(TRUE);
795 }
796}
797
Bram Moolenaar860cae12010-06-05 23:22:07 +0200798 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200800{
801 int row;
802 int j;
803
804 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200805 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200806 {
807# ifdef FEAT_GUI
808 /* Remove the cursor before starting to do anything, because scrolling
809 * may make it difficult to redraw the text under it. */
810 if (gui.in_use)
811 gui_undraw_cursor();
812# endif
813 row = 0;
814 for (j = 0; j < wp->w_lines_valid; ++j)
815 {
816 if (lnum == wp->w_lines[j].wl_lnum)
817 {
818 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200819# ifdef FEAT_SEARCH_EXTRA
820 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200821 start_search_hl();
822 prepare_search_hl(wp, lnum);
823# endif
824 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
825# if defined(FEAT_SEARCH_EXTRA)
826 end_search_hl();
827# endif
828 break;
829 }
830 row += wp->w_lines[j].wl_size;
831 }
832# ifdef FEAT_GUI
833 /* Redraw the cursor */
834 if (gui.in_use)
835 {
836 out_flush(); /* required before updating the cursor */
837 gui_update_cursor(FALSE, FALSE);
838 }
839# endif
840 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200841 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200842}
843#endif
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100846static void update_prepare(void);
847static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849/*
850 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000851 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 */
853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100854update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 cursor_off();
857 updating_screen = TRUE;
858#ifdef FEAT_GUI
859 /* Remove the cursor before starting to do anything, because scrolling may
860 * make it difficult to redraw the text under it. */
861 if (gui.in_use)
862 gui_undraw_cursor();
863#endif
864#ifdef FEAT_SEARCH_EXTRA
865 start_search_hl();
866#endif
867}
868
869/*
870 * Finish updating one or more windows.
871 */
872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100873update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 if (redraw_cmdline)
876 showmode();
877
878# ifdef FEAT_SEARCH_EXTRA
879 end_search_hl();
880# endif
881
882 updating_screen = FALSE;
883
884# ifdef FEAT_GUI
885 gui_may_resize_shell();
886
887 /* Redraw the cursor and update the scrollbars when all screen updating is
888 * done. */
889 if (gui.in_use)
890 {
891 out_flush(); /* required before updating the cursor */
892 gui_update_cursor(FALSE, FALSE);
893 gui_update_scrollbars(FALSE);
894 }
895# endif
896}
897#endif
898
899#if defined(FEAT_SIGNS) || defined(PROTO)
900 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100901update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902{
903 win_T *wp;
904 int doit = FALSE;
905
906# ifdef FEAT_FOLDING
907 win_foldinfo.fi_level = 0;
908# endif
909
910 /* update/delete a specific mark */
911 FOR_ALL_WINDOWS(wp)
912 {
913 if (buf != NULL && lnum > 0)
914 {
915 if (wp->w_buffer == buf && lnum >= wp->w_topline
916 && lnum < wp->w_botline)
917 {
918 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
919 wp->w_redraw_top = lnum;
920 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
921 wp->w_redraw_bot = lnum;
922 redraw_win_later(wp, VALID);
923 }
924 }
925 else
926 redraw_win_later(wp, VALID);
927 if (wp->w_redr_type != 0)
928 doit = TRUE;
929 }
930
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100931 /* Return when there is nothing to do, screen updating is already
932 * happening (recursive call) or still starting up. */
933 if (!doit || updating_screen
934#ifdef FEAT_GUI
935 || gui.starting
936#endif
937 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 return;
939
940 /* update all windows that need updating */
941 update_prepare();
942
943# ifdef FEAT_WINDOWS
944 for (wp = firstwin; wp; wp = wp->w_next)
945 {
946 if (wp->w_redr_type != 0)
947 win_update(wp);
948 if (wp->w_redr_status)
949 win_redr_status(wp);
950 }
951# else
952 if (curwin->w_redr_type != 0)
953 win_update(curwin);
954# endif
955
956 update_finish();
957}
958#endif
959
960
961#if defined(FEAT_GUI) || defined(PROTO)
962/*
963 * Update a single window, its status line and maybe the command line msg.
964 * Used for the GUI scrollbar.
965 */
966 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100967updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000969 /* return if already busy updating */
970 if (updating_screen)
971 return;
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 update_prepare();
974
975#ifdef FEAT_CLIPBOARD
976 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200977 if (clip_star.available && clip_isautosel_star())
978 clip_update_selection(&clip_star);
979 if (clip_plus.available && clip_isautosel_plus())
980 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000986 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000987 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000988 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (wp->w_redr_status
991# ifdef FEAT_CMDL_INFO
992 || p_ru
993# endif
994# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000995 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996# endif
997 )
998 win_redr_status(wp);
999#endif
1000
1001 update_finish();
1002}
1003#endif
1004
1005/*
1006 * Update a single window.
1007 *
1008 * This may cause the windows below it also to be redrawn (when clearing the
1009 * screen or scrolling lines).
1010 *
1011 * How the window is redrawn depends on wp->w_redr_type. Each type also
1012 * implies the one below it.
1013 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001014 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1016 * INVERTED redraw the changed part of the Visual area
1017 * INVERTED_ALL redraw the whole Visual area
1018 * VALID 1. scroll up/down to adjust for a changed w_topline
1019 * 2. update lines at the top when scrolled down
1020 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001021 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * b_mod_top and b_mod_bot.
1023 * - if wp->w_redraw_top non-zero, redraw lines between
1024 * wp->w_redraw_top and wp->w_redr_bot.
1025 * - continue redrawing when syntax status is invalid.
1026 * 4. if scrolled up, update lines at the bottom.
1027 * This results in three areas that may need updating:
1028 * top: from first row to top_end (when scrolled down)
1029 * mid: from mid_start to mid_end (update inversion or changed text)
1030 * bot: from bot_start to last row (when scrolled up)
1031 */
1032 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001033win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
1035 buf_T *buf = wp->w_buffer;
1036 int type;
1037 int top_end = 0; /* Below last row of the top area that needs
1038 updating. 0 when no top area updating. */
1039 int mid_start = 999;/* first row of the mid area that needs
1040 updating. 999 when no mid area updating. */
1041 int mid_end = 0; /* Below last row of the mid area that needs
1042 updating. 0 when no mid area updating. */
1043 int bot_start = 999;/* first row of the bot area that needs
1044 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 int scrolled_down = FALSE; /* TRUE when scrolled down when
1046 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001048 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 int top_to_mod = FALSE; /* redraw above mod_top */
1050#endif
1051
1052 int row; /* current window row to display */
1053 linenr_T lnum; /* current buffer lnum to display */
1054 int idx; /* current index in w_lines[] */
1055 int srow; /* starting row of the current line */
1056
1057 int eof = FALSE; /* if TRUE, we hit the end of the file */
1058 int didline = FALSE; /* if TRUE, we finished the last line */
1059 int i;
1060 long j;
1061 static int recursive = FALSE; /* being called recursively */
1062 int old_botline = wp->w_botline;
1063#ifdef FEAT_FOLDING
1064 long fold_count;
1065#endif
1066#ifdef FEAT_SYN_HL
1067 /* remember what happened to the previous line, to know if
1068 * check_visual_highlight() can be used */
1069#define DID_NONE 1 /* didn't update a line */
1070#define DID_LINE 2 /* updated a normal line */
1071#define DID_FOLD 3 /* updated a folded line */
1072 int did_update = DID_NONE;
1073 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1074#endif
1075 linenr_T mod_top = 0;
1076 linenr_T mod_bot = 0;
1077#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1078 int save_got_int;
1079#endif
1080
1081 type = wp->w_redr_type;
1082
1083 if (type == NOT_VALID)
1084 {
1085#ifdef FEAT_WINDOWS
1086 wp->w_redr_status = TRUE;
1087#endif
1088 wp->w_lines_valid = 0;
1089 }
1090
1091 /* Window is zero-height: nothing to draw. */
1092 if (wp->w_height == 0)
1093 {
1094 wp->w_redr_type = 0;
1095 return;
1096 }
1097
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001098#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 /* Window is zero-width: Only need to draw the separator. */
1100 if (wp->w_width == 0)
1101 {
1102 /* draw the vertical separator right of this window */
1103 draw_vsep_win(wp, 0);
1104 wp->w_redr_type = 0;
1105 return;
1106 }
1107#endif
1108
1109#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001110 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001113#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001114 /* Force redraw when width of 'number' or 'relativenumber' column
1115 * changes. */
1116 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001117 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001118 {
1119 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001120 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001121 }
1122 else
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1126 {
1127 /*
1128 * When there are both inserted/deleted lines and specific lines to be
1129 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1130 * everything (only happens when redrawing is off for while).
1131 */
1132 type = NOT_VALID;
1133 }
1134 else
1135 {
1136 /*
1137 * Set mod_top to the first line that needs displaying because of
1138 * changes. Set mod_bot to the first line after the changes.
1139 */
1140 mod_top = wp->w_redraw_top;
1141 if (wp->w_redraw_bot != 0)
1142 mod_bot = wp->w_redraw_bot + 1;
1143 else
1144 mod_bot = 0;
1145 wp->w_redraw_top = 0; /* reset for next time */
1146 wp->w_redraw_bot = 0;
1147 if (buf->b_mod_set)
1148 {
1149 if (mod_top == 0 || mod_top > buf->b_mod_top)
1150 {
1151 mod_top = buf->b_mod_top;
1152#ifdef FEAT_SYN_HL
1153 /* Need to redraw lines above the change that may be included
1154 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001155 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001157 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (mod_top < 1)
1159 mod_top = 1;
1160 }
1161#endif
1162 }
1163 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1164 mod_bot = buf->b_mod_bot;
1165
1166#ifdef FEAT_SEARCH_EXTRA
1167 /* When 'hlsearch' is on and using a multi-line search pattern, a
1168 * change in one line may make the Search highlighting in a
1169 * previous line invalid. Simple solution: redraw all visible
1170 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001171 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001173 if (search_hl.rm.regprog != NULL
1174 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001176 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001177 {
1178 cur = wp->w_match_head;
1179 while (cur != NULL)
1180 {
1181 if (cur->match.regprog != NULL
1182 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001183 {
1184 top_to_mod = TRUE;
1185 break;
1186 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001187 cur = cur->next;
1188 }
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#endif
1191 }
1192#ifdef FEAT_FOLDING
1193 if (mod_top != 0 && hasAnyFolding(wp))
1194 {
1195 linenr_T lnumt, lnumb;
1196
1197 /*
1198 * A change in a line can cause lines above it to become folded or
1199 * unfolded. Find the top most buffer line that may be affected.
1200 * If the line was previously folded and displayed, get the first
1201 * line of that fold. If the line is folded now, get the first
1202 * folded line. Use the minimum of these two.
1203 */
1204
1205 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1206 * the line below it. If there is no valid entry, use w_topline.
1207 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1208 * to this line. If there is no valid entry, use MAXLNUM. */
1209 lnumt = wp->w_topline;
1210 lnumb = MAXLNUM;
1211 for (i = 0; i < wp->w_lines_valid; ++i)
1212 if (wp->w_lines[i].wl_valid)
1213 {
1214 if (wp->w_lines[i].wl_lastlnum < mod_top)
1215 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1216 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1217 {
1218 lnumb = wp->w_lines[i].wl_lnum;
1219 /* When there is a fold column it might need updating
1220 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001221 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 ++lnumb;
1223 }
1224 }
1225
1226 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1227 if (mod_top > lnumt)
1228 mod_top = lnumt;
1229
1230 /* Now do the same for the bottom line (one above mod_bot). */
1231 --mod_bot;
1232 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1233 ++mod_bot;
1234 if (mod_bot < lnumb)
1235 mod_bot = lnumb;
1236 }
1237#endif
1238
1239 /* When a change starts above w_topline and the end is below
1240 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001241 * If the end of the change is above w_topline: do like no change was
1242 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (mod_top != 0 && mod_top < wp->w_topline)
1244 {
1245 if (mod_bot > wp->w_topline)
1246 mod_top = wp->w_topline;
1247#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001248 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 top_end = 1;
1250#endif
1251 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001252
1253 /* When line numbers are displayed need to redraw all lines below
1254 * inserted/deleted lines. */
1255 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1256 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259 /*
1260 * When only displaying the lines at the top, set top_end. Used when
1261 * window has scrolled down for msg_scrolled.
1262 */
1263 if (type == REDRAW_TOP)
1264 {
1265 j = 0;
1266 for (i = 0; i < wp->w_lines_valid; ++i)
1267 {
1268 j += wp->w_lines[i].wl_size;
1269 if (j >= wp->w_upd_rows)
1270 {
1271 top_end = j;
1272 break;
1273 }
1274 }
1275 if (top_end == 0)
1276 /* not found (cannot happen?): redraw everything */
1277 type = NOT_VALID;
1278 else
1279 /* top area defined, the rest is VALID */
1280 type = VALID;
1281 }
1282
Bram Moolenaar367329b2007-08-30 11:53:22 +00001283 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001284 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1285 * non-zero and thus not FALSE) will indicate that screenclear() was not
1286 * called. */
1287 if (screen_cleared)
1288 screen_cleared = MAYBE;
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 /*
1291 * If there are no changes on the screen that require a complete redraw,
1292 * handle three cases:
1293 * 1: we are off the top of the screen by a few lines: scroll down
1294 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1295 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1296 * w_lines[] that needs updating.
1297 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001298 if ((type == VALID || type == SOME_VALID
1299 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef FEAT_DIFF
1301 && !wp->w_botfill && !wp->w_old_botfill
1302#endif
1303 )
1304 {
1305 if (mod_top != 0 && wp->w_topline == mod_top)
1306 {
1307 /*
1308 * w_topline is the first changed line, the scrolling will be done
1309 * further down.
1310 */
1311 }
1312 else if (wp->w_lines[0].wl_valid
1313 && (wp->w_topline < wp->w_lines[0].wl_lnum
1314#ifdef FEAT_DIFF
1315 || (wp->w_topline == wp->w_lines[0].wl_lnum
1316 && wp->w_topfill > wp->w_old_topfill)
1317#endif
1318 ))
1319 {
1320 /*
1321 * New topline is above old topline: May scroll down.
1322 */
1323#ifdef FEAT_FOLDING
1324 if (hasAnyFolding(wp))
1325 {
1326 linenr_T ln;
1327
1328 /* count the number of lines we are off, counting a sequence
1329 * of folded lines as one */
1330 j = 0;
1331 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1332 {
1333 ++j;
1334 if (j >= wp->w_height - 2)
1335 break;
1336 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1337 }
1338 }
1339 else
1340#endif
1341 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1342 if (j < wp->w_height - 2) /* not too far off */
1343 {
1344 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1345#ifdef FEAT_DIFF
1346 /* insert extra lines for previously invisible filler lines */
1347 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1348 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1349 - wp->w_old_topfill;
1350#endif
1351 if (i < wp->w_height - 2) /* less than a screen off */
1352 {
1353 /*
1354 * Try to insert the correct number of lines.
1355 * If not the last window, delete the lines at the bottom.
1356 * win_ins_lines may fail when the terminal can't do it.
1357 */
1358 if (i > 0)
1359 check_for_delay(FALSE);
1360 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1361 {
1362 if (wp->w_lines_valid != 0)
1363 {
1364 /* Need to update rows that are new, stop at the
1365 * first one that scrolled down. */
1366 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /* Move the entries that were scrolled, disable
1370 * the entries for the lines to be redrawn. */
1371 if ((wp->w_lines_valid += j) > wp->w_height)
1372 wp->w_lines_valid = wp->w_height;
1373 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1374 wp->w_lines[idx] = wp->w_lines[idx - j];
1375 while (idx >= 0)
1376 wp->w_lines[idx--].wl_valid = FALSE;
1377 }
1378 }
1379 else
1380 mid_start = 0; /* redraw all lines */
1381 }
1382 else
1383 mid_start = 0; /* redraw all lines */
1384 }
1385 else
1386 mid_start = 0; /* redraw all lines */
1387 }
1388 else
1389 {
1390 /*
1391 * New topline is at or below old topline: May scroll up.
1392 * When topline didn't change, find first entry in w_lines[] that
1393 * needs updating.
1394 */
1395
1396 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1397 j = -1;
1398 row = 0;
1399 for (i = 0; i < wp->w_lines_valid; i++)
1400 {
1401 if (wp->w_lines[i].wl_valid
1402 && wp->w_lines[i].wl_lnum == wp->w_topline)
1403 {
1404 j = i;
1405 break;
1406 }
1407 row += wp->w_lines[i].wl_size;
1408 }
1409 if (j == -1)
1410 {
1411 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1412 * lines */
1413 mid_start = 0;
1414 }
1415 else
1416 {
1417 /*
1418 * Try to delete the correct number of lines.
1419 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1420 */
1421#ifdef FEAT_DIFF
1422 /* If the topline didn't change, delete old filler lines,
1423 * otherwise delete filler lines of the new topline... */
1424 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1425 row += wp->w_old_topfill;
1426 else
1427 row += diff_check_fill(wp, wp->w_topline);
1428 /* ... but don't delete new filler lines. */
1429 row -= wp->w_topfill;
1430#endif
1431 if (row > 0)
1432 {
1433 check_for_delay(FALSE);
1434 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1435 bot_start = wp->w_height - row;
1436 else
1437 mid_start = 0; /* redraw all lines */
1438 }
1439 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1440 {
1441 /*
1442 * Skip the lines (below the deleted lines) that are still
1443 * valid and don't need redrawing. Copy their info
1444 * upwards, to compensate for the deleted lines. Set
1445 * bot_start to the first row that needs redrawing.
1446 */
1447 bot_start = 0;
1448 idx = 0;
1449 for (;;)
1450 {
1451 wp->w_lines[idx] = wp->w_lines[j];
1452 /* stop at line that didn't fit, unless it is still
1453 * valid (no lines deleted) */
1454 if (row > 0 && bot_start + row
1455 + (int)wp->w_lines[j].wl_size > wp->w_height)
1456 {
1457 wp->w_lines_valid = idx + 1;
1458 break;
1459 }
1460 bot_start += wp->w_lines[idx++].wl_size;
1461
1462 /* stop at the last valid entry in w_lines[].wl_size */
1463 if (++j >= wp->w_lines_valid)
1464 {
1465 wp->w_lines_valid = idx;
1466 break;
1467 }
1468 }
1469#ifdef FEAT_DIFF
1470 /* Correct the first entry for filler lines at the top
1471 * when it won't get updated below. */
1472 if (wp->w_p_diff && bot_start > 0)
1473 wp->w_lines[0].wl_size =
1474 plines_win_nofill(wp, wp->w_topline, TRUE)
1475 + wp->w_topfill;
1476#endif
1477 }
1478 }
1479 }
1480
1481 /* When starting redraw in the first line, redraw all lines. When
1482 * there is only one window it's probably faster to clear the screen
1483 * first. */
1484 if (mid_start == 0)
1485 {
1486 mid_end = wp->w_height;
1487 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001488 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001489 /* Clear the screen when it was not done by win_del_lines() or
1490 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1491 * then. */
1492 if (screen_cleared != TRUE)
1493 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001494#ifdef FEAT_WINDOWS
1495 /* The screen was cleared, redraw the tab pages line. */
1496 if (redraw_tabline)
1497 draw_tabline();
1498#endif
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001501
1502 /* When win_del_lines() or win_ins_lines() caused the screen to be
1503 * cleared (only happens for the first window) or when screenclear()
1504 * was called directly above, "must_redraw" will have been set to
1505 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1506 if (screen_cleared == TRUE)
1507 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 else
1510 {
1511 /* Not VALID or INVERTED: redraw all lines. */
1512 mid_start = 0;
1513 mid_end = wp->w_height;
1514 }
1515
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001516 if (type == SOME_VALID)
1517 {
1518 /* SOME_VALID: redraw all lines. */
1519 mid_start = 0;
1520 mid_end = wp->w_height;
1521 type = NOT_VALID;
1522 }
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 /* check if we are updating or removing the inverted part */
1525 if ((VIsual_active && buf == curwin->w_buffer)
1526 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1527 {
1528 linenr_T from, to;
1529
1530 if (VIsual_active)
1531 {
1532 if (VIsual_active
1533 && (VIsual_mode != wp->w_old_visual_mode
1534 || type == INVERTED_ALL))
1535 {
1536 /*
1537 * If the type of Visual selection changed, redraw the whole
1538 * selection. Also when the ownership of the X selection is
1539 * gained or lost.
1540 */
1541 if (curwin->w_cursor.lnum < VIsual.lnum)
1542 {
1543 from = curwin->w_cursor.lnum;
1544 to = VIsual.lnum;
1545 }
1546 else
1547 {
1548 from = VIsual.lnum;
1549 to = curwin->w_cursor.lnum;
1550 }
1551 /* redraw more when the cursor moved as well */
1552 if (wp->w_old_cursor_lnum < from)
1553 from = wp->w_old_cursor_lnum;
1554 if (wp->w_old_cursor_lnum > to)
1555 to = wp->w_old_cursor_lnum;
1556 if (wp->w_old_visual_lnum < from)
1557 from = wp->w_old_visual_lnum;
1558 if (wp->w_old_visual_lnum > to)
1559 to = wp->w_old_visual_lnum;
1560 }
1561 else
1562 {
1563 /*
1564 * Find the line numbers that need to be updated: The lines
1565 * between the old cursor position and the current cursor
1566 * position. Also check if the Visual position changed.
1567 */
1568 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1569 {
1570 from = curwin->w_cursor.lnum;
1571 to = wp->w_old_cursor_lnum;
1572 }
1573 else
1574 {
1575 from = wp->w_old_cursor_lnum;
1576 to = curwin->w_cursor.lnum;
1577 if (from == 0) /* Visual mode just started */
1578 from = to;
1579 }
1580
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001581 if (VIsual.lnum != wp->w_old_visual_lnum
1582 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 if (wp->w_old_visual_lnum < from
1585 && wp->w_old_visual_lnum != 0)
1586 from = wp->w_old_visual_lnum;
1587 if (wp->w_old_visual_lnum > to)
1588 to = wp->w_old_visual_lnum;
1589 if (VIsual.lnum < from)
1590 from = VIsual.lnum;
1591 if (VIsual.lnum > to)
1592 to = VIsual.lnum;
1593 }
1594 }
1595
1596 /*
1597 * If in block mode and changed column or curwin->w_curswant:
1598 * update all lines.
1599 * First compute the actual start and end column.
1600 */
1601 if (VIsual_mode == Ctrl_V)
1602 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001603 colnr_T fromc, toc;
1604#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1605 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaar404406a2014-10-09 13:24:43 +02001607 if (curwin->w_p_lbr)
1608 ve_flags = VE_ALL;
1609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001611#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1612 ve_flags = save_ve_flags;
1613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 ++toc;
1615 if (curwin->w_curswant == MAXCOL)
1616 toc = MAXCOL;
1617
1618 if (fromc != wp->w_old_cursor_fcol
1619 || toc != wp->w_old_cursor_lcol)
1620 {
1621 if (from > VIsual.lnum)
1622 from = VIsual.lnum;
1623 if (to < VIsual.lnum)
1624 to = VIsual.lnum;
1625 }
1626 wp->w_old_cursor_fcol = fromc;
1627 wp->w_old_cursor_lcol = toc;
1628 }
1629 }
1630 else
1631 {
1632 /* Use the line numbers of the old Visual area. */
1633 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1634 {
1635 from = wp->w_old_cursor_lnum;
1636 to = wp->w_old_visual_lnum;
1637 }
1638 else
1639 {
1640 from = wp->w_old_visual_lnum;
1641 to = wp->w_old_cursor_lnum;
1642 }
1643 }
1644
1645 /*
1646 * There is no need to update lines above the top of the window.
1647 */
1648 if (from < wp->w_topline)
1649 from = wp->w_topline;
1650
1651 /*
1652 * If we know the value of w_botline, use it to restrict the update to
1653 * the lines that are visible in the window.
1654 */
1655 if (wp->w_valid & VALID_BOTLINE)
1656 {
1657 if (from >= wp->w_botline)
1658 from = wp->w_botline - 1;
1659 if (to >= wp->w_botline)
1660 to = wp->w_botline - 1;
1661 }
1662
1663 /*
1664 * Find the minimal part to be updated.
1665 * Watch out for scrolling that made entries in w_lines[] invalid.
1666 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1667 * top_end; need to redraw from top_end to the "to" line.
1668 * A middle mouse click with a Visual selection may change the text
1669 * above the Visual area and reset wl_valid, do count these for
1670 * mid_end (in srow).
1671 */
1672 if (mid_start > 0)
1673 {
1674 lnum = wp->w_topline;
1675 idx = 0;
1676 srow = 0;
1677 if (scrolled_down)
1678 mid_start = top_end;
1679 else
1680 mid_start = 0;
1681 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1682 {
1683 if (wp->w_lines[idx].wl_valid)
1684 mid_start += wp->w_lines[idx].wl_size;
1685 else if (!scrolled_down)
1686 srow += wp->w_lines[idx].wl_size;
1687 ++idx;
1688# ifdef FEAT_FOLDING
1689 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1690 lnum = wp->w_lines[idx].wl_lnum;
1691 else
1692# endif
1693 ++lnum;
1694 }
1695 srow += mid_start;
1696 mid_end = wp->w_height;
1697 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1698 {
1699 if (wp->w_lines[idx].wl_valid
1700 && wp->w_lines[idx].wl_lnum >= to + 1)
1701 {
1702 /* Only update until first row of this line */
1703 mid_end = srow;
1704 break;
1705 }
1706 srow += wp->w_lines[idx].wl_size;
1707 }
1708 }
1709 }
1710
1711 if (VIsual_active && buf == curwin->w_buffer)
1712 {
1713 wp->w_old_visual_mode = VIsual_mode;
1714 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1715 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001716 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 wp->w_old_curswant = curwin->w_curswant;
1718 }
1719 else
1720 {
1721 wp->w_old_visual_mode = 0;
1722 wp->w_old_cursor_lnum = 0;
1723 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001724 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1728 /* reset got_int, otherwise regexp won't work */
1729 save_got_int = got_int;
1730 got_int = 0;
1731#endif
1732#ifdef FEAT_FOLDING
1733 win_foldinfo.fi_level = 0;
1734#endif
1735
1736 /*
1737 * Update all the window rows.
1738 */
1739 idx = 0; /* first entry in w_lines[].wl_size */
1740 row = 0;
1741 srow = 0;
1742 lnum = wp->w_topline; /* first line shown in window */
1743 for (;;)
1744 {
1745 /* stop updating when reached the end of the window (check for _past_
1746 * the end of the window is at the end of the loop) */
1747 if (row == wp->w_height)
1748 {
1749 didline = TRUE;
1750 break;
1751 }
1752
1753 /* stop updating when hit the end of the file */
1754 if (lnum > buf->b_ml.ml_line_count)
1755 {
1756 eof = TRUE;
1757 break;
1758 }
1759
1760 /* Remember the starting row of the line that is going to be dealt
1761 * with. It is used further down when the line doesn't fit. */
1762 srow = row;
1763
1764 /*
1765 * Update a line when it is in an area that needs updating, when it
1766 * has changes or w_lines[idx] is invalid.
1767 * bot_start may be halfway a wrapped line after using
1768 * win_del_lines(), check if the current line includes it.
1769 * When syntax folding is being used, the saved syntax states will
1770 * already have been updated, we can't see where the syntax state is
1771 * the same again, just update until the end of the window.
1772 */
1773 if (row < top_end
1774 || (row >= mid_start && row < mid_end)
1775#ifdef FEAT_SEARCH_EXTRA
1776 || top_to_mod
1777#endif
1778 || idx >= wp->w_lines_valid
1779 || (row + wp->w_lines[idx].wl_size > bot_start)
1780 || (mod_top != 0
1781 && (lnum == mod_top
1782 || (lnum >= mod_top
1783 && (lnum < mod_bot
1784#ifdef FEAT_SYN_HL
1785 || did_update == DID_FOLD
1786 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001787 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 && (
1789# ifdef FEAT_FOLDING
1790 (foldmethodIsSyntax(wp)
1791 && hasAnyFolding(wp)) ||
1792# endif
1793 syntax_check_changed(lnum)))
1794#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001795#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001796 /* match in fixed position might need redraw
1797 * if lines were inserted or deleted */
1798 || (wp->w_match_head != NULL
1799 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 )))))
1802 {
1803#ifdef FEAT_SEARCH_EXTRA
1804 if (lnum == mod_top)
1805 top_to_mod = FALSE;
1806#endif
1807
1808 /*
1809 * When at start of changed lines: May scroll following lines
1810 * up or down to minimize redrawing.
1811 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001812 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
1814 if (lnum == mod_top
1815 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001816 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 int old_rows = 0;
1819 int new_rows = 0;
1820 int xtra_rows;
1821 linenr_T l;
1822
1823 /* Count the old number of window rows, using w_lines[], which
1824 * should still contain the sizes for the lines as they are
1825 * currently displayed. */
1826 for (i = idx; i < wp->w_lines_valid; ++i)
1827 {
1828 /* Only valid lines have a meaningful wl_lnum. Invalid
1829 * lines are part of the changed area. */
1830 if (wp->w_lines[i].wl_valid
1831 && wp->w_lines[i].wl_lnum == mod_bot)
1832 break;
1833 old_rows += wp->w_lines[i].wl_size;
1834#ifdef FEAT_FOLDING
1835 if (wp->w_lines[i].wl_valid
1836 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1837 {
1838 /* Must have found the last valid entry above mod_bot.
1839 * Add following invalid entries. */
1840 ++i;
1841 while (i < wp->w_lines_valid
1842 && !wp->w_lines[i].wl_valid)
1843 old_rows += wp->w_lines[i++].wl_size;
1844 break;
1845 }
1846#endif
1847 }
1848
1849 if (i >= wp->w_lines_valid)
1850 {
1851 /* We can't find a valid line below the changed lines,
1852 * need to redraw until the end of the window.
1853 * Inserting/deleting lines has no use. */
1854 bot_start = 0;
1855 }
1856 else
1857 {
1858 /* Able to count old number of rows: Count new window
1859 * rows, and may insert/delete lines */
1860 j = idx;
1861 for (l = lnum; l < mod_bot; ++l)
1862 {
1863#ifdef FEAT_FOLDING
1864 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1865 ++new_rows;
1866 else
1867#endif
1868#ifdef FEAT_DIFF
1869 if (l == wp->w_topline)
1870 new_rows += plines_win_nofill(wp, l, TRUE)
1871 + wp->w_topfill;
1872 else
1873#endif
1874 new_rows += plines_win(wp, l, TRUE);
1875 ++j;
1876 if (new_rows > wp->w_height - row - 2)
1877 {
1878 /* it's getting too much, must redraw the rest */
1879 new_rows = 9999;
1880 break;
1881 }
1882 }
1883 xtra_rows = new_rows - old_rows;
1884 if (xtra_rows < 0)
1885 {
1886 /* May scroll text up. If there is not enough
1887 * remaining text or scrolling fails, must redraw the
1888 * rest. If scrolling works, must redraw the text
1889 * below the scrolled text. */
1890 if (row - xtra_rows >= wp->w_height - 2)
1891 mod_bot = MAXLNUM;
1892 else
1893 {
1894 check_for_delay(FALSE);
1895 if (win_del_lines(wp, row,
1896 -xtra_rows, FALSE, FALSE) == FAIL)
1897 mod_bot = MAXLNUM;
1898 else
1899 bot_start = wp->w_height + xtra_rows;
1900 }
1901 }
1902 else if (xtra_rows > 0)
1903 {
1904 /* May scroll text down. If there is not enough
1905 * remaining text of scrolling fails, must redraw the
1906 * rest. */
1907 if (row + xtra_rows >= wp->w_height - 2)
1908 mod_bot = MAXLNUM;
1909 else
1910 {
1911 check_for_delay(FALSE);
1912 if (win_ins_lines(wp, row + old_rows,
1913 xtra_rows, FALSE, FALSE) == FAIL)
1914 mod_bot = MAXLNUM;
1915 else if (top_end > row + old_rows)
1916 /* Scrolled the part at the top that requires
1917 * updating down. */
1918 top_end += xtra_rows;
1919 }
1920 }
1921
1922 /* When not updating the rest, may need to move w_lines[]
1923 * entries. */
1924 if (mod_bot != MAXLNUM && i != j)
1925 {
1926 if (j < i)
1927 {
1928 int x = row + new_rows;
1929
1930 /* move entries in w_lines[] upwards */
1931 for (;;)
1932 {
1933 /* stop at last valid entry in w_lines[] */
1934 if (i >= wp->w_lines_valid)
1935 {
1936 wp->w_lines_valid = j;
1937 break;
1938 }
1939 wp->w_lines[j] = wp->w_lines[i];
1940 /* stop at a line that won't fit */
1941 if (x + (int)wp->w_lines[j].wl_size
1942 > wp->w_height)
1943 {
1944 wp->w_lines_valid = j + 1;
1945 break;
1946 }
1947 x += wp->w_lines[j++].wl_size;
1948 ++i;
1949 }
1950 if (bot_start > x)
1951 bot_start = x;
1952 }
1953 else /* j > i */
1954 {
1955 /* move entries in w_lines[] downwards */
1956 j -= i;
1957 wp->w_lines_valid += j;
1958 if (wp->w_lines_valid > wp->w_height)
1959 wp->w_lines_valid = wp->w_height;
1960 for (i = wp->w_lines_valid; i - j >= idx; --i)
1961 wp->w_lines[i] = wp->w_lines[i - j];
1962
1963 /* The w_lines[] entries for inserted lines are
1964 * now invalid, but wl_size may be used above.
1965 * Reset to zero. */
1966 while (i >= idx)
1967 {
1968 wp->w_lines[i].wl_size = 0;
1969 wp->w_lines[i--].wl_valid = FALSE;
1970 }
1971 }
1972 }
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
1976#ifdef FEAT_FOLDING
1977 /*
1978 * When lines are folded, display one line for all of them.
1979 * Otherwise, display normally (can be several display lines when
1980 * 'wrap' is on).
1981 */
1982 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1983 if (fold_count != 0)
1984 {
1985 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1986 ++row;
1987 --fold_count;
1988 wp->w_lines[idx].wl_folded = TRUE;
1989 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1990# ifdef FEAT_SYN_HL
1991 did_update = DID_FOLD;
1992# endif
1993 }
1994 else
1995#endif
1996 if (idx < wp->w_lines_valid
1997 && wp->w_lines[idx].wl_valid
1998 && wp->w_lines[idx].wl_lnum == lnum
1999 && lnum > wp->w_topline
2000 && !(dy_flags & DY_LASTLINE)
2001 && srow + wp->w_lines[idx].wl_size > wp->w_height
2002#ifdef FEAT_DIFF
2003 && diff_check_fill(wp, lnum) == 0
2004#endif
2005 )
2006 {
2007 /* This line is not going to fit. Don't draw anything here,
2008 * will draw "@ " lines below. */
2009 row = wp->w_height + 1;
2010 }
2011 else
2012 {
2013#ifdef FEAT_SEARCH_EXTRA
2014 prepare_search_hl(wp, lnum);
2015#endif
2016#ifdef FEAT_SYN_HL
2017 /* Let the syntax stuff know we skipped a few lines. */
2018 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002019 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 syntax_end_parsing(syntax_last_parsed + 1);
2021#endif
2022
2023 /*
2024 * Display one line.
2025 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002026 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028#ifdef FEAT_FOLDING
2029 wp->w_lines[idx].wl_folded = FALSE;
2030 wp->w_lines[idx].wl_lastlnum = lnum;
2031#endif
2032#ifdef FEAT_SYN_HL
2033 did_update = DID_LINE;
2034 syntax_last_parsed = lnum;
2035#endif
2036 }
2037
2038 wp->w_lines[idx].wl_lnum = lnum;
2039 wp->w_lines[idx].wl_valid = TRUE;
2040 if (row > wp->w_height) /* past end of screen */
2041 {
2042 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002043 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2045 ++idx;
2046 break;
2047 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002048 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 wp->w_lines[idx].wl_size = row - srow;
2050 ++idx;
2051#ifdef FEAT_FOLDING
2052 lnum += fold_count + 1;
2053#else
2054 ++lnum;
2055#endif
2056 }
2057 else
2058 {
2059 /* This line does not need updating, advance to the next one */
2060 row += wp->w_lines[idx++].wl_size;
2061 if (row > wp->w_height) /* past end of screen */
2062 break;
2063#ifdef FEAT_FOLDING
2064 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2065#else
2066 ++lnum;
2067#endif
2068#ifdef FEAT_SYN_HL
2069 did_update = DID_NONE;
2070#endif
2071 }
2072
2073 if (lnum > buf->b_ml.ml_line_count)
2074 {
2075 eof = TRUE;
2076 break;
2077 }
2078 }
2079 /*
2080 * End of loop over all window lines.
2081 */
2082
2083
2084 if (idx > wp->w_lines_valid)
2085 wp->w_lines_valid = idx;
2086
2087#ifdef FEAT_SYN_HL
2088 /*
2089 * Let the syntax stuff know we stop parsing here.
2090 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002091 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 syntax_end_parsing(syntax_last_parsed + 1);
2093#endif
2094
2095 /*
2096 * If we didn't hit the end of the file, and we didn't finish the last
2097 * line we were working on, then the line didn't fit.
2098 */
2099 wp->w_empty_rows = 0;
2100#ifdef FEAT_DIFF
2101 wp->w_filler_rows = 0;
2102#endif
2103 if (!eof && !didline)
2104 {
2105 if (lnum == wp->w_topline)
2106 {
2107 /*
2108 * Single line that does not fit!
2109 * Don't overwrite it, it can be edited.
2110 */
2111 wp->w_botline = lnum + 1;
2112 }
2113#ifdef FEAT_DIFF
2114 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2115 {
2116 /* Window ends in filler lines. */
2117 wp->w_botline = lnum;
2118 wp->w_filler_rows = wp->w_height - srow;
2119 }
2120#endif
2121 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2122 {
2123 /*
2124 * Last line isn't finished: Display "@@@" at the end.
2125 */
2126 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2127 W_WINROW(wp) + wp->w_height,
2128 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2129 '@', '@', hl_attr(HLF_AT));
2130 set_empty_rows(wp, srow);
2131 wp->w_botline = lnum;
2132 }
2133 else
2134 {
2135 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2136 wp->w_botline = lnum;
2137 }
2138 }
2139 else
2140 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002141#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 draw_vsep_win(wp, row);
2143#endif
2144 if (eof) /* we hit the end of the file */
2145 {
2146 wp->w_botline = buf->b_ml.ml_line_count + 1;
2147#ifdef FEAT_DIFF
2148 j = diff_check_fill(wp, wp->w_botline);
2149 if (j > 0 && !wp->w_botfill)
2150 {
2151 /*
2152 * Display filler lines at the end of the file
2153 */
2154 if (char2cells(fill_diff) > 1)
2155 i = '-';
2156 else
2157 i = fill_diff;
2158 if (row + j > wp->w_height)
2159 j = wp->w_height - row;
2160 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2161 row += j;
2162 }
2163#endif
2164 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 wp->w_botline = lnum;
2167
2168 /* make sure the rest of the screen is blank */
2169 /* put '~'s on rows that aren't part of the file. */
2170 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2171 }
2172
2173 /* Reset the type of redrawing required, the window has been updated. */
2174 wp->w_redr_type = 0;
2175#ifdef FEAT_DIFF
2176 wp->w_old_topfill = wp->w_topfill;
2177 wp->w_old_botfill = wp->w_botfill;
2178#endif
2179
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002180 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 /*
2183 * There is a trick with w_botline. If we invalidate it on each
2184 * change that might modify it, this will cause a lot of expensive
2185 * calls to plines() in update_topline() each time. Therefore the
2186 * value of w_botline is often approximated, and this value is used to
2187 * compute the value of w_topline. If the value of w_botline was
2188 * wrong, check that the value of w_topline is correct (cursor is on
2189 * the visible part of the text). If it's not, we need to redraw
2190 * again. Mostly this just means scrolling up a few lines, so it
2191 * doesn't look too bad. Only do this for the current window (where
2192 * changes are relevant).
2193 */
2194 wp->w_valid |= VALID_BOTLINE;
2195 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2196 {
2197 recursive = TRUE;
2198 curwin->w_valid &= ~VALID_TOPLINE;
2199 update_topline(); /* may invalidate w_botline again */
2200 if (must_redraw != 0)
2201 {
2202 /* Don't update for changes in buffer again. */
2203 i = curbuf->b_mod_set;
2204 curbuf->b_mod_set = FALSE;
2205 win_update(curwin);
2206 must_redraw = 0;
2207 curbuf->b_mod_set = i;
2208 }
2209 recursive = FALSE;
2210 }
2211 }
2212
2213#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2214 /* restore got_int, unless CTRL-C was hit while redrawing */
2215 if (!got_int)
2216 got_int = save_got_int;
2217#endif
2218}
2219
2220#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002221static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223/*
2224 * Return TRUE when window "wp" has a column to draw signs in.
2225 */
2226 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002227draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 return (wp->w_buffer->b_signlist != NULL
2230# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002231 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232# endif
2233 );
2234}
2235#endif
2236
2237/*
2238 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2239 * as the filler character.
2240 */
2241 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002242win_draw_end(
2243 win_T *wp,
2244 int c1,
2245 int c2,
2246 int row,
2247 int endrow,
2248 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2251 int n = 0;
2252# define FDC_OFF n
2253#else
2254# define FDC_OFF 0
2255#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002256#ifdef FEAT_FOLDING
2257 int fdc = compute_foldcolumn(wp, 0);
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260#ifdef FEAT_RIGHTLEFT
2261 if (wp->w_p_rl)
2262 {
2263 /* No check for cmdline window: should never be right-left. */
2264# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002265 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 if (n > 0)
2268 {
2269 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002270 if (n > W_WIDTH(wp))
2271 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2273 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2274 ' ', ' ', hl_attr(HLF_FC));
2275 }
2276# endif
2277# ifdef FEAT_SIGNS
2278 if (draw_signcolumn(wp))
2279 {
2280 int nn = n + 2;
2281
2282 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002283 if (nn > W_WIDTH(wp))
2284 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2286 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2287 ' ', ' ', hl_attr(HLF_SC));
2288 n = nn;
2289 }
2290# endif
2291 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2292 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2293 c2, c2, hl_attr(hl));
2294 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2295 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2296 c1, c2, hl_attr(hl));
2297 }
2298 else
2299#endif
2300 {
2301#ifdef FEAT_CMDWIN
2302 if (cmdwin_type != 0 && wp == curwin)
2303 {
2304 /* draw the cmdline character in the leftmost column */
2305 n = 1;
2306 if (n > wp->w_width)
2307 n = wp->w_width;
2308 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2309 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2310 cmdwin_type, ' ', hl_attr(HLF_AT));
2311 }
2312#endif
2313#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002314 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002316 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /* draw the fold column at the left */
2319 if (nn > W_WIDTH(wp))
2320 nn = W_WIDTH(wp);
2321 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2322 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2323 ' ', ' ', hl_attr(HLF_FC));
2324 n = nn;
2325 }
2326#endif
2327#ifdef FEAT_SIGNS
2328 if (draw_signcolumn(wp))
2329 {
2330 int nn = n + 2;
2331
2332 /* draw the sign column after the fold column */
2333 if (nn > W_WIDTH(wp))
2334 nn = W_WIDTH(wp);
2335 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2336 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2337 ' ', ' ', hl_attr(HLF_SC));
2338 n = nn;
2339 }
2340#endif
2341 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2342 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2343 c1, c2, hl_attr(hl));
2344 }
2345 set_empty_rows(wp, row);
2346}
2347
Bram Moolenaar1a384422010-07-14 19:53:30 +02002348#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002349static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002350
2351/*
2352 * Advance **color_cols and return TRUE when there are columns to draw.
2353 */
2354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002355advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002356{
2357 while (**color_cols >= 0 && vcol > **color_cols)
2358 ++*color_cols;
2359 return (**color_cols >= 0);
2360}
2361#endif
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_FOLDING
2364/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002365 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2366 * space is available for window "wp", minus "col".
2367 */
2368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002370{
2371 int fdc = wp->w_p_fdc;
2372 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2373 int wwidth = W_WIDTH(wp);
2374
2375 if (fdc > wwidth - (col + wmw))
2376 fdc = wwidth - (col + wmw);
2377 return fdc;
2378}
2379
2380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * Display one folded line.
2382 */
2383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002384fold_line(
2385 win_T *wp,
2386 long fold_count,
2387 foldinfo_T *foldinfo,
2388 linenr_T lnum,
2389 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 char_u buf[51];
2392 pos_T *top, *bot;
2393 linenr_T lnume = lnum + fold_count - 1;
2394 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002395 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 int col;
2398 int txtcol;
2399 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002400 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402 /* Build the fold line:
2403 * 1. Add the cmdwin_type for the command-line window
2404 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002405 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 * 4. Compose the text
2407 * 5. Add the text
2408 * 6. set highlighting for the Visual area an other text
2409 */
2410 col = 0;
2411
2412 /*
2413 * 1. Add the cmdwin_type for the command-line window
2414 * Ignores 'rightleft', this window is never right-left.
2415 */
2416#ifdef FEAT_CMDWIN
2417 if (cmdwin_type != 0 && wp == curwin)
2418 {
2419 ScreenLines[off] = cmdwin_type;
2420 ScreenAttrs[off] = hl_attr(HLF_AT);
2421#ifdef FEAT_MBYTE
2422 if (enc_utf8)
2423 ScreenLinesUC[off] = 0;
2424#endif
2425 ++col;
2426 }
2427#endif
2428
2429 /*
2430 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002431 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002433 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (fdc > 0)
2435 {
2436 fill_foldcolumn(buf, wp, TRUE, lnum);
2437#ifdef FEAT_RIGHTLEFT
2438 if (wp->w_p_rl)
2439 {
2440 int i;
2441
2442 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2443 hl_attr(HLF_FC));
2444 /* reverse the fold column */
2445 for (i = 0; i < fdc; ++i)
2446 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2447 }
2448 else
2449#endif
2450 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2451 col += fdc;
2452 }
2453
2454#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002455# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2456 for (ri = 0; ri < l; ++ri) \
2457 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2458 else \
2459 for (ri = 0; ri < l; ++ri) \
2460 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002462# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2463 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#endif
2465
Bram Moolenaar64486672010-05-16 15:46:46 +02002466 /* Set all attributes of the 'number' or 'relativenumber' column and the
2467 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002468 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470#ifdef FEAT_SIGNS
2471 /* If signs are being displayed, add two spaces. */
2472 if (draw_signcolumn(wp))
2473 {
2474 len = W_WIDTH(wp) - col;
2475 if (len > 0)
2476 {
2477 if (len > 2)
2478 len = 2;
2479# ifdef FEAT_RIGHTLEFT
2480 if (wp->w_p_rl)
2481 /* the line number isn't reversed */
2482 copy_text_attr(off + W_WIDTH(wp) - len - col,
2483 (char_u *)" ", len, hl_attr(HLF_FL));
2484 else
2485# endif
2486 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2487 col += len;
2488 }
2489 }
2490#endif
2491
2492 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002493 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002495 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 len = W_WIDTH(wp) - col;
2498 if (len > 0)
2499 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002500 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002501 long num;
2502 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002503
2504 if (len > w + 1)
2505 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002506
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002507 if (wp->w_p_nu && !wp->w_p_rnu)
2508 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002509 num = (long)lnum;
2510 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002511 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002512 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002513 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002514 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002515 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002516 /* 'number' + 'relativenumber': cursor line shows absolute
2517 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002518 num = lnum;
2519 fmt = "%-*ld ";
2520 }
2521 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002522
Bram Moolenaar700e7342013-01-30 12:31:36 +01002523 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524#ifdef FEAT_RIGHTLEFT
2525 if (wp->w_p_rl)
2526 /* the line number isn't reversed */
2527 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2528 hl_attr(HLF_FL));
2529 else
2530#endif
2531 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2532 col += len;
2533 }
2534 }
2535
2536 /*
2537 * 4. Compose the folded-line string with 'foldtext', if set.
2538 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002539 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
2541 txtcol = col; /* remember where text starts */
2542
2543 /*
2544 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2545 * Right-left text is put in columns 0 - number-col, normal text is put
2546 * in columns number-col - window-width.
2547 */
2548#ifdef FEAT_MBYTE
2549 if (has_mbyte)
2550 {
2551 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 int u8c, u8cc[MAX_MCO];
2553 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 int idx;
2555 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002556 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557# ifdef FEAT_ARABIC
2558 int prev_c = 0; /* previous Arabic character */
2559 int prev_c1 = 0; /* first composing char for prev_c */
2560# endif
2561
2562# ifdef FEAT_RIGHTLEFT
2563 if (wp->w_p_rl)
2564 idx = off;
2565 else
2566# endif
2567 idx = off + col;
2568
2569 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2570 for (p = text; *p != NUL; )
2571 {
2572 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002573 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (col + cells > W_WIDTH(wp)
2575# ifdef FEAT_RIGHTLEFT
2576 - (wp->w_p_rl ? col : 0)
2577# endif
2578 )
2579 break;
2580 ScreenLines[idx] = *p;
2581 if (enc_utf8)
2582 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 u8c = utfc_ptr2char(p, u8cc);
2584 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 ScreenLinesUC[idx] = 0;
2587#ifdef FEAT_ARABIC
2588 prev_c = u8c;
2589#endif
2590 }
2591 else
2592 {
2593#ifdef FEAT_ARABIC
2594 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2595 {
2596 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002597 int pc, pc1, nc;
2598 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 int firstbyte = *p;
2600
2601 /* The idea of what is the previous and next
2602 * character depends on 'rightleft'. */
2603 if (wp->w_p_rl)
2604 {
2605 pc = prev_c;
2606 pc1 = prev_c1;
2607 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002608 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610 else
2611 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002612 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002614 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 prev_c = u8c;
2617
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 pc, pc1, nc);
2620 ScreenLines[idx] = firstbyte;
2621 }
2622 else
2623 prev_c = u8c;
2624#endif
2625 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002626#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (u8c >= 0x10000)
2628 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2629 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002632 for (i = 0; i < Screen_mco; ++i)
2633 {
2634 ScreenLinesC[i][idx] = u8cc[i];
2635 if (u8cc[i] == 0)
2636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 if (cells > 1)
2640 ScreenLines[idx + 1] = 0;
2641 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002642 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2643 /* double-byte single width character */
2644 ScreenLines2[idx] = p[1];
2645 else if (cells > 1)
2646 /* double-width character */
2647 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 col += cells;
2649 idx += cells;
2650 p += c_len;
2651 }
2652 }
2653 else
2654#endif
2655 {
2656 len = (int)STRLEN(text);
2657 if (len > W_WIDTH(wp) - col)
2658 len = W_WIDTH(wp) - col;
2659 if (len > 0)
2660 {
2661#ifdef FEAT_RIGHTLEFT
2662 if (wp->w_p_rl)
2663 STRNCPY(current_ScreenLine, text, len);
2664 else
2665#endif
2666 STRNCPY(current_ScreenLine + col, text, len);
2667 col += len;
2668 }
2669 }
2670
2671 /* Fill the rest of the line with the fold filler */
2672#ifdef FEAT_RIGHTLEFT
2673 if (wp->w_p_rl)
2674 col -= txtcol;
2675#endif
2676 while (col < W_WIDTH(wp)
2677#ifdef FEAT_RIGHTLEFT
2678 - (wp->w_p_rl ? txtcol : 0)
2679#endif
2680 )
2681 {
2682#ifdef FEAT_MBYTE
2683 if (enc_utf8)
2684 {
2685 if (fill_fold >= 0x80)
2686 {
2687 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002688 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 else
2691 ScreenLinesUC[off + col] = 0;
2692 }
2693#endif
2694 ScreenLines[off + col++] = fill_fold;
2695 }
2696
2697 if (text != buf)
2698 vim_free(text);
2699
2700 /*
2701 * 6. set highlighting for the Visual area an other text.
2702 * If all folded lines are in the Visual area, highlight the line.
2703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2705 {
2706 if (ltoreq(curwin->w_cursor, VIsual))
2707 {
2708 /* Visual is after curwin->w_cursor */
2709 top = &curwin->w_cursor;
2710 bot = &VIsual;
2711 }
2712 else
2713 {
2714 /* Visual is before curwin->w_cursor */
2715 top = &VIsual;
2716 bot = &curwin->w_cursor;
2717 }
2718 if (lnum >= top->lnum
2719 && lnume <= bot->lnum
2720 && (VIsual_mode != 'v'
2721 || ((lnum > top->lnum
2722 || (lnum == top->lnum
2723 && top->col == 0))
2724 && (lnume < bot->lnum
2725 || (lnume == bot->lnum
2726 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002727 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 if (VIsual_mode == Ctrl_V)
2730 {
2731 /* Visual block mode: highlight the chars part of the block */
2732 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2733 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002734 if (wp->w_old_cursor_lcol != MAXCOL
2735 && wp->w_old_cursor_lcol + txtcol
2736 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 len = wp->w_old_cursor_lcol;
2738 else
2739 len = W_WIDTH(wp) - txtcol;
2740 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002741 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 }
2744 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002747 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002752#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002753 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2754 if (wp->w_p_cc_cols)
2755 {
2756 int i = 0;
2757 int j = wp->w_p_cc_cols[i];
2758 int old_txtcol = txtcol;
2759
2760 while (j > -1)
2761 {
2762 txtcol += j;
2763 if (wp->w_p_wrap)
2764 txtcol -= wp->w_skipcol;
2765 else
2766 txtcol -= wp->w_leftcol;
2767 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2768 ScreenAttrs[off + txtcol] = hl_combine_attr(
2769 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2770 txtcol = old_txtcol;
2771 j = wp->w_p_cc_cols[++i];
2772 }
2773 }
2774
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002775 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002776 if (wp->w_p_cuc)
2777 {
2778 txtcol += wp->w_virtcol;
2779 if (wp->w_p_wrap)
2780 txtcol -= wp->w_skipcol;
2781 else
2782 txtcol -= wp->w_leftcol;
2783 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2784 ScreenAttrs[off + txtcol] = hl_combine_attr(
2785 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2786 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2790 (int)W_WIDTH(wp), FALSE);
2791
2792 /*
2793 * Update w_cline_height and w_cline_folded if the cursor line was
2794 * updated (saves a call to plines() later).
2795 */
2796 if (wp == curwin
2797 && lnum <= curwin->w_cursor.lnum
2798 && lnume >= curwin->w_cursor.lnum)
2799 {
2800 curwin->w_cline_row = row;
2801 curwin->w_cline_height = 1;
2802 curwin->w_cline_folded = TRUE;
2803 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2804 }
2805}
2806
2807/*
2808 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2809 */
2810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002811copy_text_attr(
2812 int off,
2813 char_u *buf,
2814 int len,
2815 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002817 int i;
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 mch_memmove(ScreenLines + off, buf, (size_t)len);
2820# ifdef FEAT_MBYTE
2821 if (enc_utf8)
2822 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2823# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002824 for (i = 0; i < len; ++i)
2825 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828/*
2829 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002830 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 */
2832 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002833fill_foldcolumn(
2834 char_u *p,
2835 win_T *wp,
2836 int closed, /* TRUE of FALSE */
2837 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 int i = 0;
2840 int level;
2841 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002842 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002843 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002846 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 level = win_foldinfo.fi_level;
2849 if (level > 0)
2850 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002851 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002852 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /* If the column is too narrow, we start at the lowest level that
2855 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002856 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (first_level < 1)
2858 first_level = 1;
2859
Bram Moolenaar1c934292015-01-27 16:39:29 +01002860 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 if (win_foldinfo.fi_lnum == lnum
2863 && first_level + i >= win_foldinfo.fi_low_level)
2864 p[i] = '-';
2865 else if (first_level == 1)
2866 p[i] = '|';
2867 else if (first_level + i <= 9)
2868 p[i] = '0' + first_level + i;
2869 else
2870 p[i] = '>';
2871 if (first_level + i == level)
2872 break;
2873 }
2874 }
2875 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002876 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877}
2878#endif /* FEAT_FOLDING */
2879
2880/*
2881 * Display line "lnum" of window 'wp' on the screen.
2882 * Start at row "startrow", stop when "endrow" is reached.
2883 * wp->w_virtcol needs to be valid.
2884 *
2885 * Return the number of last row the line occupies.
2886 */
2887 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002888win_line(
2889 win_T *wp,
2890 linenr_T lnum,
2891 int startrow,
2892 int endrow,
2893 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 int col; /* visual column on screen */
2896 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2897 int c = 0; /* init for GCC */
2898 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002899#ifdef FEAT_LINEBREAK
2900 long vcol_sbr = -1; /* virtual column after showbreak */
2901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 long vcol_prev = -1; /* "vcol" of previous character */
2903 char_u *line; /* current line */
2904 char_u *ptr; /* current position in "line" */
2905 int row; /* row in the window, excl w_winrow */
2906 int screen_row; /* row on the screen, incl w_winrow */
2907
2908 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2909 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002910 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002911 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 int c_extra = NUL; /* extra chars, all the same */
2913 int extra_attr = 0; /* attributes when n_extra != 0 */
2914 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2915 displaying lcs_eol at end-of-line */
2916 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2917 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2918
2919 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2920 int saved_n_extra = 0;
2921 char_u *saved_p_extra = NULL;
2922 int saved_c_extra = 0;
2923 int saved_char_attr = 0;
2924
2925 int n_attr = 0; /* chars with special attr */
2926 int saved_attr2 = 0; /* char_attr saved for n_attr */
2927 int n_attr3 = 0; /* chars with overruling special attr */
2928 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2929
2930 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2931
2932 int fromcol, tocol; /* start/end of inverting */
2933 int fromcol_prev = -2; /* start of inverting after cursor */
2934 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002936 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 pos_T pos;
2938 long v;
2939
2940 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002941 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2943 in this line */
2944 int attr = 0; /* attributes for area highlighting */
2945 int area_attr = 0; /* attributes desired by highlighting */
2946 int search_attr = 0; /* attributes desired by 'hlsearch' */
2947#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002948 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int syntax_attr = 0; /* attributes desired by syntax */
2950 int has_syntax = FALSE; /* this buffer has syntax highl. */
2951 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002952 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002953 int draw_color_col = FALSE; /* highlight colorcolumn */
2954 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002955#endif
2956#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002957 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002958# define SPWORDLEN 150
2959 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002960 int nextlinecol = 0; /* column where nextline[] starts */
2961 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002962 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002963 int spell_attr = 0; /* attributes desired by spelling */
2964 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002965 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2966 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002967 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002968 static int cap_col = -1; /* column to check for Cap word */
2969 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002970 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#endif
2972 int extra_check; /* has syntax or linebreak */
2973#ifdef FEAT_MBYTE
2974 int multi_attr = 0; /* attributes desired by multibyte */
2975 int mb_l = 1; /* multi-byte byte length */
2976 int mb_c = 0; /* decoded multi-byte character */
2977 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002978 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979#endif
2980#ifdef FEAT_DIFF
2981 int filler_lines; /* nr of filler lines to be drawn */
2982 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002983 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 int change_start = MAXCOL; /* first col of changed area */
2985 int change_end = -1; /* last col of changed area */
2986#endif
2987 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2988#ifdef FEAT_LINEBREAK
2989 int need_showbreak = FALSE;
2990#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002991#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2992 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002994 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995#endif
2996#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002997 matchitem_T *cur; /* points to the match list */
2998 match_T *shl; /* points to search_hl or a match */
2999 int shl_flag; /* flag to indicate whether search_hl
3000 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003001 int pos_inprogress; /* marks that position match search is
3002 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003003 int prevcol_hl_flag; /* flag to indicate whether prevcol
3004 equals startcol of search_hl or one
3005 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#endif
3007#ifdef FEAT_ARABIC
3008 int prev_c = 0; /* previous Arabic character */
3009 int prev_c1 = 0; /* first composing char for prev_c */
3010#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003011#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003012 int did_line_attr = 0;
3013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /* draw_state: items that are drawn in sequence: */
3016#define WL_START 0 /* nothing done yet */
3017#ifdef FEAT_CMDWIN
3018# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3019#else
3020# define WL_CMDLINE WL_START
3021#endif
3022#ifdef FEAT_FOLDING
3023# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3024#else
3025# define WL_FOLD WL_CMDLINE
3026#endif
3027#ifdef FEAT_SIGNS
3028# define WL_SIGN WL_FOLD + 1 /* column for signs */
3029#else
3030# define WL_SIGN WL_FOLD /* column for signs */
3031#endif
3032#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003033#ifdef FEAT_LINEBREAK
3034# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003036# define WL_BRI WL_NR
3037#endif
3038#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3039# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3040#else
3041# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#endif
3043#define WL_LINE WL_SBR + 1 /* text in the line */
3044 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003045#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 int feedback_col = 0;
3047 int feedback_old_attr = -1;
3048#endif
3049
Bram Moolenaar860cae12010-06-05 23:22:07 +02003050#ifdef FEAT_CONCEAL
3051 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003052 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003053 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003054 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003055 int is_concealing = FALSE;
3056 int boguscols = 0; /* nonexistent columns added to force
3057 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003058 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003059 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003060 int match_conc = FALSE; /* cchar for match functions */
3061 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003062 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003063# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003064# define FIX_FOR_BOGUSCOLS \
3065 { \
3066 n_extra += vcol_off; \
3067 vcol -= vcol_off; \
3068 vcol_off = 0; \
3069 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003070 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003071 boguscols = 0; \
3072 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003073#else
3074# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 if (startrow > endrow) /* past the end already! */
3078 return startrow;
3079
3080 row = startrow;
3081 screen_row = row + W_WINROW(wp);
3082
3083 /*
3084 * To speed up the loop below, set extra_check when there is linebreak,
3085 * trailing white space and/or syntax processing to be done.
3086 */
3087#ifdef FEAT_LINEBREAK
3088 extra_check = wp->w_p_lbr;
3089#else
3090 extra_check = 0;
3091#endif
3092#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003093 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
3095 /* Prepare for syntax highlighting in this line. When there is an
3096 * error, stop syntax highlighting. */
3097 save_did_emsg = did_emsg;
3098 did_emsg = FALSE;
3099 syntax_start(wp, lnum);
3100 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003101 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else
3103 {
3104 did_emsg = save_did_emsg;
3105 has_syntax = TRUE;
3106 extra_check = TRUE;
3107 }
3108 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003109
3110 /* Check for columns to display for 'colorcolumn'. */
3111 color_cols = wp->w_p_cc_cols;
3112 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003113 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003114#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003115
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003116#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003117 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003118 && *wp->w_s->b_p_spl != NUL
3119 && wp->w_s->b_langp.ga_len > 0
3120 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003121 {
3122 /* Prepare for spell checking. */
3123 has_spell = TRUE;
3124 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003125
3126 /* Get the start of the next line, so that words that wrap to the next
3127 * line are found too: "et<line-break>al.".
3128 * Trick: skip a few chars for C/shell/Vim comments */
3129 nextline[SPWORDLEN] = NUL;
3130 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3131 {
3132 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3133 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3134 }
3135
3136 /* When a word wrapped from the previous line the start of the current
3137 * line is valid. */
3138 if (lnum == checked_lnum)
3139 cur_checked_col = checked_col;
3140 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003141
3142 /* When there was a sentence end in the previous line may require a
3143 * word starting with capital in this line. In line 1 always check
3144 * the first word. */
3145 if (lnum != capcol_lnum)
3146 cap_col = -1;
3147 if (lnum == 1)
3148 cap_col = 0;
3149 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
3152
3153 /*
3154 * handle visual active in this window
3155 */
3156 fromcol = -10;
3157 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3159 {
3160 /* Visual is after curwin->w_cursor */
3161 if (ltoreq(curwin->w_cursor, VIsual))
3162 {
3163 top = &curwin->w_cursor;
3164 bot = &VIsual;
3165 }
3166 else /* Visual is before curwin->w_cursor */
3167 {
3168 top = &VIsual;
3169 bot = &curwin->w_cursor;
3170 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003171 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (VIsual_mode == Ctrl_V) /* block mode */
3173 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003174 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 fromcol = wp->w_old_cursor_fcol;
3177 tocol = wp->w_old_cursor_lcol;
3178 }
3179 }
3180 else /* non-block mode */
3181 {
3182 if (lnum > top->lnum && lnum <= bot->lnum)
3183 fromcol = 0;
3184 else if (lnum == top->lnum)
3185 {
3186 if (VIsual_mode == 'V') /* linewise */
3187 fromcol = 0;
3188 else
3189 {
3190 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3191 if (gchar_pos(top) == NUL)
3192 tocol = fromcol + 1;
3193 }
3194 }
3195 if (VIsual_mode != 'V' && lnum == bot->lnum)
3196 {
3197 if (*p_sel == 'e' && bot->col == 0
3198#ifdef FEAT_VIRTUALEDIT
3199 && bot->coladd == 0
3200#endif
3201 )
3202 {
3203 fromcol = -10;
3204 tocol = MAXCOL;
3205 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003206 else if (bot->col == MAXCOL)
3207 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209 {
3210 pos = *bot;
3211 if (*p_sel == 'e')
3212 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3213 else
3214 {
3215 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3216 ++tocol;
3217 }
3218 }
3219 }
3220 }
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /* Check if the character under the cursor should not be inverted */
3223 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003224#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 )
3228 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 /* if inverting in this line set area_highlighting */
3231 if (fromcol >= 0)
3232 {
3233 area_highlighting = TRUE;
3234 attr = hl_attr(HLF_V);
3235#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003236 if ((clip_star.available && !clip_star.owned
3237 && clip_isautosel_star())
3238 || (clip_plus.available && !clip_plus.owned
3239 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 attr = hl_attr(HLF_VNC);
3241#endif
3242 }
3243 }
3244
3245 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003246 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003248 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 && wp == curwin
3250 && lnum >= curwin->w_cursor.lnum
3251 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3252 {
3253 if (lnum == curwin->w_cursor.lnum)
3254 getvcol(curwin, &(curwin->w_cursor),
3255 (colnr_T *)&fromcol, NULL, NULL);
3256 else
3257 fromcol = 0;
3258 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3259 {
3260 pos.lnum = lnum;
3261 pos.col = search_match_endcol;
3262 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3263 }
3264 else
3265 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003266 /* do at least one character; happens when past end of line */
3267 if (fromcol == tocol)
3268 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 area_highlighting = TRUE;
3270 attr = hl_attr(HLF_I);
3271 }
3272
3273#ifdef FEAT_DIFF
3274 filler_lines = diff_check(wp, lnum);
3275 if (filler_lines < 0)
3276 {
3277 if (filler_lines == -1)
3278 {
3279 if (diff_find_change(wp, lnum, &change_start, &change_end))
3280 diff_hlf = HLF_ADD; /* added line */
3281 else if (change_start == 0)
3282 diff_hlf = HLF_TXD; /* changed text */
3283 else
3284 diff_hlf = HLF_CHD; /* changed line */
3285 }
3286 else
3287 diff_hlf = HLF_ADD; /* added line */
3288 filler_lines = 0;
3289 area_highlighting = TRUE;
3290 }
3291 if (lnum == wp->w_topline)
3292 filler_lines = wp->w_topfill;
3293 filler_todo = filler_lines;
3294#endif
3295
3296#ifdef LINE_ATTR
3297# ifdef FEAT_SIGNS
3298 /* If this line has a sign with line highlighting set line_attr. */
3299 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3300 if (v != 0)
3301 line_attr = sign_get_attr((int)v, TRUE);
3302# endif
3303# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3304 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003305 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 line_attr = hl_attr(HLF_L);
3307# endif
3308 if (line_attr != 0)
3309 area_highlighting = TRUE;
3310#endif
3311
3312 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3313 ptr = line;
3314
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003315#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316 if (has_spell)
3317 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003318 /* For checking first word with a capital skip white space. */
3319 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003320 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003321
Bram Moolenaar30abd282005-06-22 22:35:10 +00003322 /* To be able to spell-check over line boundaries copy the end of the
3323 * current line into nextline[]. Above the start of the next line was
3324 * copied to nextline[SPWORDLEN]. */
3325 if (nextline[SPWORDLEN] == NUL)
3326 {
3327 /* No next line or it is empty. */
3328 nextlinecol = MAXCOL;
3329 nextline_idx = 0;
3330 }
3331 else
3332 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003333 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003334 if (v < SPWORDLEN)
3335 {
3336 /* Short line, use it completely and append the start of the
3337 * next line. */
3338 nextlinecol = 0;
3339 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003340 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003341 nextline_idx = v + 1;
3342 }
3343 else
3344 {
3345 /* Long line, use only the last SPWORDLEN bytes. */
3346 nextlinecol = v - SPWORDLEN;
3347 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3348 nextline_idx = SPWORDLEN + 1;
3349 }
3350 }
3351 }
3352#endif
3353
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003354 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003356 if (lcs_space || lcs_trail)
3357 extra_check = TRUE;
3358 /* find start of trailing whitespace */
3359 if (lcs_trail)
3360 {
3361 trailcol = (colnr_T)STRLEN(ptr);
3362 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3363 --trailcol;
3364 trailcol += (colnr_T) (ptr - line);
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367
3368 /*
3369 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3370 * first character to be displayed.
3371 */
3372 if (wp->w_p_wrap)
3373 v = wp->w_skipcol;
3374 else
3375 v = wp->w_leftcol;
3376 if (v > 0)
3377 {
3378#ifdef FEAT_MBYTE
3379 char_u *prev_ptr = ptr;
3380#endif
3381 while (vcol < v && *ptr != NUL)
3382 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003383 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 vcol += c;
3385#ifdef FEAT_MBYTE
3386 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003388 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003391 /* When:
3392 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003393 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003394 * - 'virtualedit' is set, or
3395 * - the visual mode is active,
3396 * the end of the line may be before the start of the displayed part.
3397 */
3398 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003399#ifdef FEAT_SYN_HL
3400 wp->w_p_cuc || draw_color_col ||
3401#endif
3402#ifdef FEAT_VIRTUALEDIT
3403 virtual_active() ||
3404#endif
3405 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 /* Handle a character that's not completely on the screen: Put ptr at
3411 * that character but skip the first few screen characters. */
3412 if (vcol > v)
3413 {
3414 vcol -= c;
3415#ifdef FEAT_MBYTE
3416 ptr = prev_ptr;
3417#else
3418 --ptr;
3419#endif
3420 n_skip = v - vcol;
3421 }
3422
3423 /*
3424 * Adjust for when the inverted text is before the screen,
3425 * and when the start of the inverted text is before the screen.
3426 */
3427 if (tocol <= vcol)
3428 fromcol = 0;
3429 else if (fromcol >= 0 && fromcol < vcol)
3430 fromcol = vcol;
3431
3432#ifdef FEAT_LINEBREAK
3433 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3434 if (wp->w_p_wrap)
3435 need_showbreak = TRUE;
3436#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003437#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003438 /* When spell checking a word we need to figure out the start of the
3439 * word and if it's badly spelled or not. */
3440 if (has_spell)
3441 {
3442 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003443 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003444 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003445
3446 pos = wp->w_cursor;
3447 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003448 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003449 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003450
3451 /* spell_move_to() may call ml_get() and make "line" invalid */
3452 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3453 ptr = line + linecol;
3454
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003455 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003456 {
3457 /* no bad word found at line start, don't check until end of a
3458 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003459 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003460 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003461 }
3462 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003463 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003464 /* bad word found, use attributes until end of word */
3465 word_end = wp->w_cursor.col + len + 1;
3466
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003467 /* Turn index into actual attributes. */
3468 if (spell_hlf != HLF_COUNT)
3469 spell_attr = highlight_attr[spell_hlf];
3470 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003471 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003472
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003473# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003474 /* Need to restart syntax highlighting for this line. */
3475 if (has_syntax)
3476 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003477# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003478 }
3479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481
3482 /*
3483 * Correct highlighting for cursor that can't be disabled.
3484 * Avoids having to check this for each character.
3485 */
3486 if (fromcol >= 0)
3487 {
3488 if (noinvcur)
3489 {
3490 if ((colnr_T)fromcol == wp->w_virtcol)
3491 {
3492 /* highlighting starts at cursor, let it start just after the
3493 * cursor */
3494 fromcol_prev = fromcol;
3495 fromcol = -1;
3496 }
3497 else if ((colnr_T)fromcol < wp->w_virtcol)
3498 /* restart highlighting after the cursor */
3499 fromcol_prev = wp->w_virtcol;
3500 }
3501 if (fromcol >= tocol)
3502 fromcol = -1;
3503 }
3504
3505#ifdef FEAT_SEARCH_EXTRA
3506 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003507 * Handle highlighting the last used search pattern and matches.
3508 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003510 cur = wp->w_match_head;
3511 shl_flag = FALSE;
3512 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003514 if (shl_flag == FALSE)
3515 {
3516 shl = &search_hl;
3517 shl_flag = TRUE;
3518 }
3519 else
3520 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003521 shl->startcol = MAXCOL;
3522 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003524 v = (long)(ptr - line);
3525 if (cur != NULL)
3526 cur->pos.cur = 0;
3527 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3528
3529 /* Need to get the line again, a multi-line regexp may have made it
3530 * invalid. */
3531 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3532 ptr = line + v;
3533
3534 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003536 if (shl->lnum == lnum)
3537 shl->startcol = shl->rm.startpos[0].col;
3538 else
3539 shl->startcol = 0;
3540 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3541 - shl->rm.startpos[0].lnum)
3542 shl->endcol = shl->rm.endpos[0].col;
3543 else
3544 shl->endcol = MAXCOL;
3545 /* Highlight one character for an empty match. */
3546 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003549 if (has_mbyte && line[shl->endcol] != NUL)
3550 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003553 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003555 if ((long)shl->startcol < v) /* match at leftcol */
3556 {
3557 shl->attr_cur = shl->attr;
3558 search_attr = shl->attr;
3559 }
3560 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003562 if (shl != &search_hl && cur != NULL)
3563 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565#endif
3566
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003567#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003568 /* Cursor line highlighting for 'cursorline' in the current window. Not
3569 * when Visual mode is active, because it's not clear what is selected
3570 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003571 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003572 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003573 {
3574 line_attr = hl_attr(HLF_CUL);
3575 area_highlighting = TRUE;
3576 }
3577#endif
3578
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003579 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 col = 0;
3581#ifdef FEAT_RIGHTLEFT
3582 if (wp->w_p_rl)
3583 {
3584 /* Rightleft window: process the text in the normal direction, but put
3585 * it in current_ScreenLine[] from right to left. Start at the
3586 * rightmost column of the window. */
3587 col = W_WIDTH(wp) - 1;
3588 off += col;
3589 }
3590#endif
3591
3592 /*
3593 * Repeat for the whole displayed line.
3594 */
3595 for (;;)
3596 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003597#ifdef FEAT_CONCEAL
3598 has_match_conc = FALSE;
3599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 /* Skip this quickly when working on the text. */
3601 if (draw_state != WL_LINE)
3602 {
3603#ifdef FEAT_CMDWIN
3604 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3605 {
3606 draw_state = WL_CMDLINE;
3607 if (cmdwin_type != 0 && wp == curwin)
3608 {
3609 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003611 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_attr = hl_attr(HLF_AT);
3613 }
3614 }
3615#endif
3616
3617#ifdef FEAT_FOLDING
3618 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3619 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003620 int fdc = compute_foldcolumn(wp, 0);
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003623 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 /* Draw the 'foldcolumn'. */
3626 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003627 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003629 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 c_extra = NUL;
3631 char_attr = hl_attr(HLF_FC);
3632 }
3633 }
3634#endif
3635
3636#ifdef FEAT_SIGNS
3637 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3638 {
3639 draw_state = WL_SIGN;
3640 /* Show the sign column when there are any signs in this
3641 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003642 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003644 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003646 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647# endif
3648
3649 /* Draw two cells with the sign value or blank. */
3650 c_extra = ' ';
3651 char_attr = hl_attr(HLF_SC);
3652 n_extra = 2;
3653
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003654 if (row == startrow
3655#ifdef FEAT_DIFF
3656 + filler_lines && filler_todo <= 0
3657#endif
3658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3661 SIGN_TEXT);
3662# ifdef FEAT_SIGN_ICONS
3663 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3664 SIGN_ICON);
3665 if (gui.in_use && icon_sign != 0)
3666 {
3667 /* Use the image in this position. */
3668 c_extra = SIGN_BYTE;
3669# ifdef FEAT_NETBEANS_INTG
3670 if (buf_signcount(wp->w_buffer, lnum) > 1)
3671 c_extra = MULTISIGN_BYTE;
3672# endif
3673 char_attr = icon_sign;
3674 }
3675 else
3676# endif
3677 if (text_sign != 0)
3678 {
3679 p_extra = sign_get_text(text_sign);
3680 if (p_extra != NULL)
3681 {
3682 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003683 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685 char_attr = sign_get_attr(text_sign, FALSE);
3686 }
3687 }
3688 }
3689 }
3690#endif
3691
3692 if (draw_state == WL_NR - 1 && n_extra == 0)
3693 {
3694 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003695 /* Display the absolute or relative line number. After the
3696 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3697 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 && (row == startrow
3699#ifdef FEAT_DIFF
3700 + filler_lines
3701#endif
3702 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3703 {
3704 /* Draw the line number (empty space after wrapping). */
3705 if (row == startrow
3706#ifdef FEAT_DIFF
3707 + filler_lines
3708#endif
3709 )
3710 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003711 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003713
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003714 if (wp->w_p_nu && !wp->w_p_rnu)
3715 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003716 num = (long)lnum;
3717 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003718 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003719 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003720 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003721 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003722 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003723 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003724 num = lnum;
3725 fmt = "%-*ld ";
3726 }
3727 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003728
Bram Moolenaar700e7342013-01-30 12:31:36 +01003729 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003730 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (wp->w_skipcol > 0)
3732 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3733 *p_extra = '-';
3734#ifdef FEAT_RIGHTLEFT
3735 if (wp->w_p_rl) /* reverse line numbers */
3736 rl_mirror(extra);
3737#endif
3738 p_extra = extra;
3739 c_extra = NUL;
3740 }
3741 else
3742 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003743 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003745#ifdef FEAT_SYN_HL
3746 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003747 * the current line differently.
3748 * TODO: Can we use CursorLine instead of CursorLineNr
3749 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003750 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003751 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003752 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 }
3756
Bram Moolenaar597a4222014-06-25 14:39:50 +02003757#ifdef FEAT_LINEBREAK
3758 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3759 && n_extra == 0 && *p_sbr != NUL)
3760 /* draw indent after showbreak value */
3761 draw_state = WL_BRI;
3762 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3763 /* After the showbreak, draw the breakindent */
3764 draw_state = WL_BRI - 1;
3765
3766 /* draw 'breakindent': indent wrapped text accordingly */
3767 if (draw_state == WL_BRI - 1 && n_extra == 0)
3768 {
3769 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003771# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003772 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003773# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003774 )
3775 {
3776 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003777# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003778 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003779 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003780 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003781# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003782 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3783 char_attr = hl_combine_attr(char_attr,
3784 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003785# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003786 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003787# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003788 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003789 c_extra = ' ';
3790 n_extra = get_breakindent_win(wp,
3791 ml_get_buf(wp->w_buffer, lnum, FALSE));
3792 /* Correct end of highlighted area for 'breakindent',
3793 * required when 'linebreak' is also set. */
3794 if (tocol == vcol)
3795 tocol += n_extra;
3796 }
3797 }
3798#endif
3799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3801 if (draw_state == WL_SBR - 1 && n_extra == 0)
3802 {
3803 draw_state = WL_SBR;
3804# ifdef FEAT_DIFF
3805 if (filler_todo > 0)
3806 {
3807 /* Draw "deleted" diff line(s). */
3808 if (char2cells(fill_diff) > 1)
3809 c_extra = '-';
3810 else
3811 c_extra = fill_diff;
3812# ifdef FEAT_RIGHTLEFT
3813 if (wp->w_p_rl)
3814 n_extra = col + 1;
3815 else
3816# endif
3817 n_extra = W_WIDTH(wp) - col;
3818 char_attr = hl_attr(HLF_DED);
3819 }
3820# endif
3821# ifdef FEAT_LINEBREAK
3822 if (*p_sbr != NUL && need_showbreak)
3823 {
3824 /* Draw 'showbreak' at the start of each broken line. */
3825 p_extra = p_sbr;
3826 c_extra = NUL;
3827 n_extra = (int)STRLEN(p_sbr);
3828 char_attr = hl_attr(HLF_AT);
3829 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003830 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 /* Correct end of highlighted area for 'showbreak',
3832 * required when 'linebreak' is also set. */
3833 if (tocol == vcol)
3834 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003835#ifdef FEAT_SYN_HL
3836 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003837 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003838 char_attr = hl_combine_attr(char_attr,
3839 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842# endif
3843 }
3844#endif
3845
3846 if (draw_state == WL_LINE - 1 && n_extra == 0)
3847 {
3848 draw_state = WL_LINE;
3849 if (saved_n_extra)
3850 {
3851 /* Continue item from end of wrapped line. */
3852 n_extra = saved_n_extra;
3853 c_extra = saved_c_extra;
3854 p_extra = saved_p_extra;
3855 char_attr = saved_char_attr;
3856 }
3857 else
3858 char_attr = 0;
3859 }
3860 }
3861
3862 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003863 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003864 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865#ifdef FEAT_DIFF
3866 && filler_todo <= 0
3867#endif
3868 )
3869 {
3870 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3871 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003872 /* Pretend we have finished updating the window. Except when
3873 * 'cursorcolumn' is set. */
3874#ifdef FEAT_SYN_HL
3875 if (wp->w_p_cuc)
3876 row = wp->w_cline_row + wp->w_cline_height;
3877 else
3878#endif
3879 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 break;
3881 }
3882
3883 if (draw_state == WL_LINE && area_highlighting)
3884 {
3885 /* handle Visual or match highlighting in this line */
3886 if (vcol == fromcol
3887#ifdef FEAT_MBYTE
3888 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3889 && (*mb_ptr2cells)(ptr) > 1)
3890#endif
3891 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003892 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 && vcol < tocol))
3894 area_attr = attr; /* start highlighting */
3895 else if (area_attr != 0
3896 && (vcol == tocol
3897 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
3900#ifdef FEAT_SEARCH_EXTRA
3901 if (!n_extra)
3902 {
3903 /*
3904 * Check for start/end of search pattern match.
3905 * After end, check for start/end of next match.
3906 * When another match, have to check for start again.
3907 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003908 * Do this for 'search_hl' and the match list (ordered by
3909 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003911 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003912 cur = wp->w_match_head;
3913 shl_flag = FALSE;
3914 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003916 if (shl_flag == FALSE
3917 && ((cur != NULL
3918 && cur->priority > SEARCH_HL_PRIORITY)
3919 || cur == NULL))
3920 {
3921 shl = &search_hl;
3922 shl_flag = TRUE;
3923 }
3924 else
3925 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003926 if (cur != NULL)
3927 cur->pos.cur = 0;
3928 pos_inprogress = TRUE;
3929 while (shl->rm.regprog != NULL
3930 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003932 if (shl->startcol != MAXCOL
3933 && v >= (long)shl->startcol
3934 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003936#ifdef FEAT_MBYTE
3937 int tmp_col = v + MB_PTR2LEN(ptr);
3938
3939 if (shl->endcol < tmp_col)
3940 shl->endcol = tmp_col;
3941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003943#ifdef FEAT_CONCEAL
3944 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3945 == cur->hlg_id)
3946 {
3947 has_match_conc = TRUE;
3948 match_conc = cur->conceal_char;
3949 }
3950 else
3951 has_match_conc = match_conc = FALSE;
3952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003954 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
3956 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003957#ifdef FEAT_CONCEAL
3958 prev_syntax_id = 0;
3959#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003960 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3961 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003962 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 /* Need to get the line again, a multi-line regexp
3965 * may have made it invalid. */
3966 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3967 ptr = line + v;
3968
3969 if (shl->lnum == lnum)
3970 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003971 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003973 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003975 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003977 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
3979 /* highlight empty match, try again after
3980 * it */
3981#ifdef FEAT_MBYTE
3982 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003983 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003984 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 else
3986#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003987 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989
3990 /* Loop to check if the match starts at the
3991 * current position */
3992 continue;
3993 }
3994 }
3995 break;
3996 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003997 if (shl != &search_hl && cur != NULL)
3998 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004000
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004001 /* Use attributes from match with highest priority among
4002 * 'search_hl' and the match list. */
4003 search_attr = search_hl.attr_cur;
4004 cur = wp->w_match_head;
4005 shl_flag = FALSE;
4006 while (cur != NULL || shl_flag == FALSE)
4007 {
4008 if (shl_flag == FALSE
4009 && ((cur != NULL
4010 && cur->priority > SEARCH_HL_PRIORITY)
4011 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004012 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004013 shl = &search_hl;
4014 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004015 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004016 else
4017 shl = &cur->hl;
4018 if (shl->attr_cur != 0)
4019 search_attr = shl->attr_cur;
4020 if (shl != &search_hl && cur != NULL)
4021 cur = cur->next;
4022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024#endif
4025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004027 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004029 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4030 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004032 if (diff_hlf == HLF_TXD && ptr - line > change_end
4033 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004036 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4037 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004040
4041 /* Decide which of the highlight attributes to use. */
4042 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004043#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004044 if (area_attr != 0)
4045 char_attr = hl_combine_attr(line_attr, area_attr);
4046 else if (search_attr != 0)
4047 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004048 /* Use line_attr when not in the Visual or 'incsearch' area
4049 * (area_attr may be 0 when "noinvcur" is set). */
4050 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004051 || vcol < fromcol || vcol_prev < fromcol_prev
4052 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004053 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004054#else
4055 if (area_attr != 0)
4056 char_attr = area_attr;
4057 else if (search_attr != 0)
4058 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004059#endif
4060 else
4061 {
4062 attr_pri = FALSE;
4063#ifdef FEAT_SYN_HL
4064 if (has_syntax)
4065 char_attr = syntax_attr;
4066 else
4067#endif
4068 char_attr = 0;
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071
4072 /*
4073 * Get the next character to put on the screen.
4074 */
4075 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004076 * The "p_extra" points to the extra stuff that is inserted to
4077 * represent special characters (non-printable stuff) and other
4078 * things. When all characters are the same, c_extra is used.
4079 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4080 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4082 */
4083 if (n_extra > 0)
4084 {
4085 if (c_extra != NUL)
4086 {
4087 c = c_extra;
4088#ifdef FEAT_MBYTE
4089 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4090 if (enc_utf8 && (*mb_char2len)(c) > 1)
4091 {
4092 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004093 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004094 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096 else
4097 mb_utf8 = FALSE;
4098#endif
4099 }
4100 else
4101 {
4102 c = *p_extra;
4103#ifdef FEAT_MBYTE
4104 if (has_mbyte)
4105 {
4106 mb_c = c;
4107 if (enc_utf8)
4108 {
4109 /* If the UTF-8 character is more than one byte:
4110 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004111 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 mb_utf8 = FALSE;
4113 if (mb_l > n_extra)
4114 mb_l = 1;
4115 else if (mb_l > 1)
4116 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004117 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121 }
4122 else
4123 {
4124 /* if this is a DBCS character, put it in "mb_c" */
4125 mb_l = MB_BYTE2LEN(c);
4126 if (mb_l >= n_extra)
4127 mb_l = 1;
4128 else if (mb_l > 1)
4129 mb_c = (c << 8) + p_extra[1];
4130 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004131 if (mb_l == 0) /* at the NUL at end-of-line */
4132 mb_l = 1;
4133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /* If a double-width char doesn't fit display a '>' in the
4135 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004136 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137# ifdef FEAT_RIGHTLEFT
4138 wp->w_p_rl ? (col <= 0) :
4139# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004140 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 && (*mb_char2cells)(mb_c) == 2)
4142 {
4143 c = '>';
4144 mb_c = c;
4145 mb_l = 1;
4146 mb_utf8 = FALSE;
4147 multi_attr = hl_attr(HLF_AT);
4148 /* put the pointer back to output the double-width
4149 * character at the start of the next line. */
4150 ++n_extra;
4151 --p_extra;
4152 }
4153 else
4154 {
4155 n_extra -= mb_l - 1;
4156 p_extra += mb_l - 1;
4157 }
4158 }
4159#endif
4160 ++p_extra;
4161 }
4162 --n_extra;
4163 }
4164 else
4165 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004166 if (p_extra_free != NULL)
4167 {
4168 vim_free(p_extra_free);
4169 p_extra_free = NULL;
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 /*
4172 * Get a character from the line itself.
4173 */
4174 c = *ptr;
4175#ifdef FEAT_MBYTE
4176 if (has_mbyte)
4177 {
4178 mb_c = c;
4179 if (enc_utf8)
4180 {
4181 /* If the UTF-8 character is more than one byte: Decode it
4182 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004183 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 mb_utf8 = FALSE;
4185 if (mb_l > 1)
4186 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004187 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 /* Overlong encoded ASCII or ASCII with composing char
4189 * is displayed normally, except a NUL. */
4190 if (mb_c < 0x80)
4191 c = mb_c;
4192 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004193
4194 /* At start of the line we can have a composing char.
4195 * Draw it as a space with a composing char. */
4196 if (utf_iscomposing(mb_c))
4197 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004198 int i;
4199
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004200 for (i = Screen_mco - 1; i > 0; --i)
4201 u8cc[i] = u8cc[i - 1];
4202 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004203 mb_c = ' ';
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206
4207 if ((mb_l == 1 && c >= 0x80)
4208 || (mb_l >= 1 && mb_c == 0)
4209 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004210# ifdef UNICODE16
4211 || mb_c >= 0x10000
4212# endif
4213 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
4215 /*
4216 * Illegal UTF-8 byte: display as <xx>.
4217 * Non-BMP character : display as ? or fullwidth ?.
4218 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004219# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004224# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (wp->w_p_rl) /* reverse */
4226 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004229# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 else if (utf_char2cells(mb_c) != 2)
4231 STRCPY(extra, "?");
4232 else
4233 /* 0xff1f in UTF-8: full-width '?' */
4234 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 p_extra = extra;
4238 c = *p_extra;
4239 mb_c = mb_ptr2char_adv(&p_extra);
4240 mb_utf8 = (c >= 0x80);
4241 n_extra = (int)STRLEN(p_extra);
4242 c_extra = NUL;
4243 if (area_attr == 0 && search_attr == 0)
4244 {
4245 n_attr = n_extra + 1;
4246 extra_attr = hl_attr(HLF_8);
4247 saved_attr2 = char_attr; /* save current attr */
4248 }
4249 }
4250 else if (mb_l == 0) /* at the NUL at end-of-line */
4251 mb_l = 1;
4252#ifdef FEAT_ARABIC
4253 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4254 {
4255 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 int pc, pc1, nc;
4257 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 /* The idea of what is the previous and next
4260 * character depends on 'rightleft'. */
4261 if (wp->w_p_rl)
4262 {
4263 pc = prev_c;
4264 pc1 = prev_c1;
4265 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 else
4269 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004270 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004272 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 prev_c = mb_c;
4275
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004276 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 else
4279 prev_c = mb_c;
4280#endif
4281 }
4282 else /* enc_dbcs */
4283 {
4284 mb_l = MB_BYTE2LEN(c);
4285 if (mb_l == 0) /* at the NUL at end-of-line */
4286 mb_l = 1;
4287 else if (mb_l > 1)
4288 {
4289 /* We assume a second byte below 32 is illegal.
4290 * Hopefully this is OK for all double-byte encodings!
4291 */
4292 if (ptr[1] >= 32)
4293 mb_c = (c << 8) + ptr[1];
4294 else
4295 {
4296 if (ptr[1] == NUL)
4297 {
4298 /* head byte at end of line */
4299 mb_l = 1;
4300 transchar_nonprint(extra, c);
4301 }
4302 else
4303 {
4304 /* illegal tail byte */
4305 mb_l = 2;
4306 STRCPY(extra, "XX");
4307 }
4308 p_extra = extra;
4309 n_extra = (int)STRLEN(extra) - 1;
4310 c_extra = NUL;
4311 c = *p_extra++;
4312 if (area_attr == 0 && search_attr == 0)
4313 {
4314 n_attr = n_extra + 1;
4315 extra_attr = hl_attr(HLF_8);
4316 saved_attr2 = char_attr; /* save current attr */
4317 }
4318 mb_c = c;
4319 }
4320 }
4321 }
4322 /* If a double-width char doesn't fit display a '>' in the
4323 * last column; the character is displayed at the start of the
4324 * next line. */
4325 if ((
4326# ifdef FEAT_RIGHTLEFT
4327 wp->w_p_rl ? (col <= 0) :
4328# endif
4329 (col >= W_WIDTH(wp) - 1))
4330 && (*mb_char2cells)(mb_c) == 2)
4331 {
4332 c = '>';
4333 mb_c = c;
4334 mb_utf8 = FALSE;
4335 mb_l = 1;
4336 multi_attr = hl_attr(HLF_AT);
4337 /* Put pointer back so that the character will be
4338 * displayed at the start of the next line. */
4339 --ptr;
4340 }
4341 else if (*ptr != NUL)
4342 ptr += mb_l - 1;
4343
4344 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004345 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004346 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004347 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004350 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 c = ' ';
4352 if (area_attr == 0 && search_attr == 0)
4353 {
4354 n_attr = n_extra + 1;
4355 extra_attr = hl_attr(HLF_AT);
4356 saved_attr2 = char_attr; /* save current attr */
4357 }
4358 mb_c = c;
4359 mb_utf8 = FALSE;
4360 mb_l = 1;
4361 }
4362
4363 }
4364#endif
4365 ++ptr;
4366
4367 if (extra_check)
4368 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004369#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004370 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004371#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004372
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004373#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 /* Get syntax attribute, unless still at the start of the line
4375 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004376 v = (long)(ptr - line);
4377 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 {
4379 /* Get the syntax attribute for the character. If there
4380 * is an error, disable syntax highlighting. */
4381 save_did_emsg = did_emsg;
4382 did_emsg = FALSE;
4383
Bram Moolenaar217ad922005-03-20 22:37:15 +00004384 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004385# ifdef FEAT_SPELL
4386 has_spell ? &can_spell :
4387# endif
4388 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004391 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004392 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004393 has_syntax = FALSE;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 else
4396 did_emsg = save_did_emsg;
4397
4398 /* Need to get the line again, a multi-line regexp may
4399 * have made it invalid. */
4400 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4401 ptr = line + v;
4402
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004403 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004405 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004406 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004407# ifdef FEAT_CONCEAL
4408 /* no concealing past the end of the line, it interferes
4409 * with line highlighting */
4410 if (c == NUL)
4411 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004412 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004413 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004416#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004417
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004418#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004419 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004420 * Only do this when there is no syntax highlighting, the
4421 * @Spell cluster is not used or the current syntax item
4422 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004423 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004424 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004425 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004426# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004427 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004428 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004429# endif
4430 if (c != 0 && (
4431# ifdef FEAT_SYN_HL
4432 !has_syntax ||
4433# endif
4434 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004435 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004436 char_u *prev_ptr, *p;
4437 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004438 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004439# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004440 if (has_mbyte)
4441 {
4442 prev_ptr = ptr - mb_l;
4443 v -= mb_l - 1;
4444 }
4445 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004446# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004447 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004448
4449 /* Use nextline[] if possible, it has the start of the
4450 * next line concatenated. */
4451 if ((prev_ptr - line) - nextlinecol >= 0)
4452 p = nextline + (prev_ptr - line) - nextlinecol;
4453 else
4454 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004455 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004456 len = spell_check(wp, p, &spell_hlf, &cap_col,
4457 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004458 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004459
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004460 /* In Insert mode only highlight a word that
4461 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004462 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004463 && (State & INSERT) != 0
4464 && wp->w_cursor.lnum == lnum
4465 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004466 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004467 && wp->w_cursor.col < (colnr_T)word_end)
4468 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004469 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004470 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004471 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004472
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004473 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004474 && (p - nextline) + len > nextline_idx)
4475 {
4476 /* Remember that the good word continues at the
4477 * start of the next line. */
4478 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004479 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004480 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004481
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004482 /* Turn index into actual attributes. */
4483 if (spell_hlf != HLF_COUNT)
4484 spell_attr = highlight_attr[spell_hlf];
4485
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004486 if (cap_col > 0)
4487 {
4488 if (p != prev_ptr
4489 && (p - nextline) + cap_col >= nextline_idx)
4490 {
4491 /* Remember that the word in the next line
4492 * must start with a capital. */
4493 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004494 cap_col = (int)((p - nextline) + cap_col
4495 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004496 }
4497 else
4498 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004499 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004500 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004501 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004502 }
4503 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004504 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004505 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004506 char_attr = hl_combine_attr(char_attr, spell_attr);
4507 else
4508 char_attr = hl_combine_attr(spell_attr, char_attr);
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510#endif
4511#ifdef FEAT_LINEBREAK
4512 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004513 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004515 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004517# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004518 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004519# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004520 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004522 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004524 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004525
Bram Moolenaar597a4222014-06-25 14:39:50 +02004526 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004527 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004528 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004529 if (c == TAB && n_extra + col > W_WIDTH(wp))
4530 n_extra = (int)wp->w_buffer->b_p_ts
4531 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4532
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004533# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004534 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004535# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004537# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004539 {
4540#ifdef FEAT_CONCEAL
4541 if (c == TAB)
4542 /* See "Tab alignment" below. */
4543 FIX_FOR_BOGUSCOLS;
4544#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004545 if (!wp->w_p_list)
4546 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549#endif
4550
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004551 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4552 */
4553 if (wp->w_p_list
4554 && (((c == 160
4555#ifdef FEAT_MBYTE
4556 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4557#endif
4558 ) && lcs_nbsp)
4559 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4560 {
4561 c = (c == ' ') ? lcs_space : lcs_nbsp;
4562 if (area_attr == 0 && search_attr == 0)
4563 {
4564 n_attr = 1;
4565 extra_attr = hl_attr(HLF_8);
4566 saved_attr2 = char_attr; /* save current attr */
4567 }
4568#ifdef FEAT_MBYTE
4569 mb_c = c;
4570 if (enc_utf8 && (*mb_char2len)(c) > 1)
4571 {
4572 mb_utf8 = TRUE;
4573 u8cc[0] = 0;
4574 c = 0xc0;
4575 }
4576 else
4577 mb_utf8 = FALSE;
4578#endif
4579 }
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4582 {
4583 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004584 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 n_attr = 1;
4587 extra_attr = hl_attr(HLF_8);
4588 saved_attr2 = char_attr; /* save current attr */
4589 }
4590#ifdef FEAT_MBYTE
4591 mb_c = c;
4592 if (enc_utf8 && (*mb_char2len)(c) > 1)
4593 {
4594 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004595 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004596 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 else
4599 mb_utf8 = FALSE;
4600#endif
4601 }
4602 }
4603
4604 /*
4605 * Handling of non-printable characters.
4606 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004607 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 /*
4610 * when getting a character from the file, we may have to
4611 * turn it into something else on the way to putting it
4612 * into "ScreenLines".
4613 */
4614 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4615 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004616 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004617 long vcol_adjusted = vcol; /* removed showbreak length */
4618#ifdef FEAT_LINEBREAK
4619 /* only adjust the tab_len, when at the first column
4620 * after the showbreak value was drawn */
4621 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4622 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004625 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004626 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4627
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004628#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004629 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004630#endif
4631 /* tab amount depends on current column */
4632 n_extra = tab_len;
4633#ifdef FEAT_LINEBREAK
4634 else
4635 {
4636 char_u *p;
4637 int len = n_extra;
4638 int i;
4639 int saved_nextra = n_extra;
4640
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004641#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004642 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004643 /* there are characters to conceal */
4644 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004645 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4646 */
4647 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4648 && n_extra > tab_len)
4649 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004650#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004651
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004652 /* if n_extra > 0, it gives the number of chars, to
4653 * use for a tab, else we need to calculate the width
4654 * for a tab */
4655#ifdef FEAT_MBYTE
4656 len = (tab_len * mb_char2len(lcs_tab2));
4657 if (n_extra > 0)
4658 len += n_extra - tab_len;
4659#endif
4660 c = lcs_tab1;
4661 p = alloc((unsigned)(len + 1));
4662 vim_memset(p, ' ', len);
4663 p[len] = NUL;
4664 p_extra_free = p;
4665 for (i = 0; i < tab_len; i++)
4666 {
4667#ifdef FEAT_MBYTE
4668 mb_char2bytes(lcs_tab2, p);
4669 p += mb_char2len(lcs_tab2);
4670 n_extra += mb_char2len(lcs_tab2)
4671 - (saved_nextra > 0 ? 1 : 0);
4672#else
4673 p[i] = lcs_tab2;
4674#endif
4675 }
4676 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004677#ifdef FEAT_CONCEAL
4678 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4679 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004680 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004681 n_extra -= vcol_off;
4682#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004683 }
4684#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004685#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004686 {
4687 int vc_saved = vcol_off;
4688
4689 /* Tab alignment should be identical regardless of
4690 * 'conceallevel' value. So tab compensates of all
4691 * previous concealed characters, and thus resets
4692 * vcol_off and boguscols accumulated so far in the
4693 * line. Note that the tab can be longer than
4694 * 'tabstop' when there are concealed characters. */
4695 FIX_FOR_BOGUSCOLS;
4696
4697 /* Make sure, the highlighting for the tab char will be
4698 * correctly set further below (effectively reverts the
4699 * FIX_FOR_BOGSUCOLS macro */
4700 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004701 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004702 tab_len += vc_saved;
4703 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#ifdef FEAT_MBYTE
4706 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4707#endif
4708 if (wp->w_p_list)
4709 {
4710 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004711#ifdef FEAT_LINEBREAK
4712 if (wp->w_p_lbr)
4713 c_extra = NUL; /* using p_extra from above */
4714 else
4715#endif
4716 c_extra = lcs_tab2;
4717 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 extra_attr = hl_attr(HLF_8);
4719 saved_attr2 = char_attr; /* save current attr */
4720#ifdef FEAT_MBYTE
4721 mb_c = c;
4722 if (enc_utf8 && (*mb_char2len)(c) > 1)
4723 {
4724 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004725 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004726 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728#endif
4729 }
4730 else
4731 {
4732 c_extra = ' ';
4733 c = ' ';
4734 }
4735 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004736 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004737 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004738 || ((fromcol >= 0 || fromcol_prev >= 0)
4739 && tocol > vcol
4740 && VIsual_mode != Ctrl_V
4741 && (
4742# ifdef FEAT_RIGHTLEFT
4743 wp->w_p_rl ? (col >= 0) :
4744# endif
4745 (col < W_WIDTH(wp)))
4746 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004747 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004748 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004749 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004751 /* Display a '$' after the line or highlight an extra
4752 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4754 /* For a diff line the highlighting continues after the
4755 * "$". */
4756 if (
4757# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004758 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759# ifdef LINE_ATTR
4760 &&
4761# endif
4762# endif
4763# ifdef LINE_ATTR
4764 line_attr == 0
4765# endif
4766 )
4767#endif
4768 {
4769#ifdef FEAT_VIRTUALEDIT
4770 /* In virtualedit, visual selections may extend
4771 * beyond end of line. */
4772 if (area_highlighting && virtual_active()
4773 && tocol != MAXCOL && vcol < tocol)
4774 n_extra = 0;
4775 else
4776#endif
4777 {
4778 p_extra = at_end_str;
4779 n_extra = 1;
4780 c_extra = NUL;
4781 }
4782 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004783 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004784 c = lcs_eol;
4785 else
4786 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 lcs_eol_one = -1;
4788 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004789 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
4791 extra_attr = hl_attr(HLF_AT);
4792 n_attr = 1;
4793 }
4794#ifdef FEAT_MBYTE
4795 mb_c = c;
4796 if (enc_utf8 && (*mb_char2len)(c) > 1)
4797 {
4798 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004799 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004800 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802 else
4803 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4804#endif
4805 }
4806 else if (c != NUL)
4807 {
4808 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004809 if (n_extra == 0)
4810 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#ifdef FEAT_RIGHTLEFT
4812 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4813 rl_mirror(p_extra); /* reverse "<12>" */
4814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004816#ifdef FEAT_LINEBREAK
4817 if (wp->w_p_lbr)
4818 {
4819 char_u *p;
4820
4821 c = *p_extra;
4822 p = alloc((unsigned)n_extra + 1);
4823 vim_memset(p, ' ', n_extra);
4824 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4825 p[n_extra] = NUL;
4826 p_extra_free = p_extra = p;
4827 }
4828 else
4829#endif
4830 {
4831 n_extra = byte2cells(c) - 1;
4832 c = *p_extra++;
4833 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004834 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
4836 n_attr = n_extra + 1;
4837 extra_attr = hl_attr(HLF_8);
4838 saved_attr2 = char_attr; /* save current attr */
4839 }
4840#ifdef FEAT_MBYTE
4841 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4842#endif
4843 }
4844#ifdef FEAT_VIRTUALEDIT
4845 else if (VIsual_active
4846 && (VIsual_mode == Ctrl_V
4847 || VIsual_mode == 'v')
4848 && virtual_active()
4849 && tocol != MAXCOL
4850 && vcol < tocol
4851 && (
4852# ifdef FEAT_RIGHTLEFT
4853 wp->w_p_rl ? (col >= 0) :
4854# endif
4855 (col < W_WIDTH(wp))))
4856 {
4857 c = ' ';
4858 --ptr; /* put it back at the NUL */
4859 }
4860#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004861#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else if ((
4863# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004864 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 ) && (
4868# ifdef FEAT_RIGHTLEFT
4869 wp->w_p_rl ? (col >= 0) :
4870# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004871 (col
4872# ifdef FEAT_CONCEAL
4873 - boguscols
4874# endif
4875 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 /* Highlight until the right side of the window */
4878 c = ' ';
4879 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004880
4881 /* Remember we do the char for line highlighting. */
4882 ++did_line_attr;
4883
4884 /* don't do search HL for the rest of the line */
4885 if (line_attr != 0 && char_attr == search_attr && col > 0)
4886 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887# ifdef FEAT_DIFF
4888 if (diff_hlf == HLF_TXD)
4889 {
4890 diff_hlf = HLF_CHD;
4891 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004894 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4895 char_attr = hl_combine_attr(char_attr,
4896 hl_attr(HLF_CUL));
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899# endif
4900 }
4901#endif
4902 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004903
4904#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004905 if ( wp->w_p_cole > 0
4906 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004907 conceal_cursor_line(wp) )
4908 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004909 && !(lnum_in_visual_area
4910 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004911 {
4912 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004913 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004914 && (syn_get_sub_char() != NUL || match_conc
4915 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004916 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004917 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004918 /* First time at this concealed item: display one
4919 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004920 if (match_conc)
4921 c = match_conc;
4922 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004923 c = syn_get_sub_char();
4924 else if (lcs_conceal != NUL)
4925 c = lcs_conceal;
4926 else
4927 c = ' ';
4928
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004929 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004930
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004931 if (n_extra > 0)
4932 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004933 vcol += n_extra;
4934 if (wp->w_p_wrap && n_extra > 0)
4935 {
4936# ifdef FEAT_RIGHTLEFT
4937 if (wp->w_p_rl)
4938 {
4939 col -= n_extra;
4940 boguscols -= n_extra;
4941 }
4942 else
4943# endif
4944 {
4945 boguscols += n_extra;
4946 col += n_extra;
4947 }
4948 }
4949 n_extra = 0;
4950 n_attr = 0;
4951 }
4952 else if (n_skip == 0)
4953 {
4954 is_concealing = TRUE;
4955 n_skip = 1;
4956 }
4957# ifdef FEAT_MBYTE
4958 mb_c = c;
4959 if (enc_utf8 && (*mb_char2len)(c) > 1)
4960 {
4961 mb_utf8 = TRUE;
4962 u8cc[0] = 0;
4963 c = 0xc0;
4964 }
4965 else
4966 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4967# endif
4968 }
4969 else
4970 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004971 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004972 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004973 }
4974#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004977#ifdef FEAT_CONCEAL
4978 /* In the cursor line and we may be concealing characters: correct
4979 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004980 if (!did_wcol && draw_state == WL_LINE
4981 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004982 && conceal_cursor_line(wp)
4983 && (int)wp->w_virtcol <= vcol + n_skip)
4984 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004985# ifdef FEAT_RIGHTLEFT
4986 if (wp->w_p_rl)
4987 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4988 else
4989# endif
4990 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004991 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004992 did_wcol = TRUE;
4993 }
4994#endif
4995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /* Don't override visual selection highlighting. */
4997 if (n_attr > 0
4998 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004999 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 char_attr = extra_attr;
5001
Bram Moolenaar81695252004-12-29 20:58:21 +00005002#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 /* XIM don't send preedit_start and preedit_end, but they send
5004 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5005 * im_is_preediting() here. */
5006 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005007 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 && (State & INSERT)
5009 && !p_imdisable
5010 && im_is_preediting()
5011 && draw_state == WL_LINE)
5012 {
5013 colnr_T tcol;
5014
5015 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005016 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
5018 tcol = preedit_end_col;
5019 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5020 {
5021 if (feedback_old_attr < 0)
5022 {
5023 feedback_col = 0;
5024 feedback_old_attr = char_attr;
5025 }
5026 char_attr = im_get_feedback_attr(feedback_col);
5027 if (char_attr < 0)
5028 char_attr = feedback_old_attr;
5029 feedback_col++;
5030 }
5031 else if (feedback_old_attr >= 0)
5032 {
5033 char_attr = feedback_old_attr;
5034 feedback_old_attr = -1;
5035 feedback_col = 0;
5036 }
5037 }
5038#endif
5039 /*
5040 * Handle the case where we are in column 0 but not on the first
5041 * character of the line and the user wants us to show us a
5042 * special character (via 'listchars' option "precedes:<char>".
5043 */
5044 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005045 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5047#ifdef FEAT_DIFF
5048 && filler_todo <= 0
5049#endif
5050 && draw_state > WL_NR
5051 && c != NUL)
5052 {
5053 c = lcs_prec;
5054 lcs_prec_todo = NUL;
5055#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005056 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5057 {
5058 /* Double-width character being overwritten by the "precedes"
5059 * character, need to fill up half the character. */
5060 c_extra = MB_FILLER_CHAR;
5061 n_extra = 1;
5062 n_attr = 2;
5063 extra_attr = hl_attr(HLF_AT);
5064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 mb_c = c;
5066 if (enc_utf8 && (*mb_char2len)(c) > 1)
5067 {
5068 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005069 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005070 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 else
5073 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5074#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005075 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 saved_attr3 = char_attr; /* save current attr */
5078 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5079 n_attr3 = 1;
5080 }
5081 }
5082
5083 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005084 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005086 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005087#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005088 || did_line_attr == 1
5089#endif
5090 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005092#ifdef FEAT_SEARCH_EXTRA
5093 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005094
5095 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005096 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005097 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005098#endif
5099
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005100 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 * highlight match at end of line. If it's beyond the last
5102 * char on the screen, just overwrite that one (tricky!) Not
5103 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005104#ifdef FEAT_SEARCH_EXTRA
5105 prevcol_hl_flag = FALSE;
5106 if (prevcol == (long)search_hl.startcol)
5107 prevcol_hl_flag = TRUE;
5108 else
5109 {
5110 cur = wp->w_match_head;
5111 while (cur != NULL)
5112 {
5113 if (prevcol == (long)cur->hl.startcol)
5114 {
5115 prevcol_hl_flag = TRUE;
5116 break;
5117 }
5118 cur = cur->next;
5119 }
5120 }
5121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005123 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005124 && (VIsual_mode != Ctrl_V
5125 || lnum == VIsual.lnum
5126 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005127 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#ifdef FEAT_SEARCH_EXTRA
5129 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005130 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005131# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005132 && did_line_attr <= 1
5133# endif
5134 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135#endif
5136 ))
5137 {
5138 int n = 0;
5139
5140#ifdef FEAT_RIGHTLEFT
5141 if (wp->w_p_rl)
5142 {
5143 if (col < 0)
5144 n = 1;
5145 }
5146 else
5147#endif
5148 {
5149 if (col >= W_WIDTH(wp))
5150 n = -1;
5151 }
5152 if (n != 0)
5153 {
5154 /* At the window boundary, highlight the last character
5155 * instead (better than nothing). */
5156 off += n;
5157 col += n;
5158 }
5159 else
5160 {
5161 /* Add a blank character to highlight. */
5162 ScreenLines[off] = ' ';
5163#ifdef FEAT_MBYTE
5164 if (enc_utf8)
5165 ScreenLinesUC[off] = 0;
5166#endif
5167 }
5168#ifdef FEAT_SEARCH_EXTRA
5169 if (area_attr == 0)
5170 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005171 /* Use attributes from match with highest priority among
5172 * 'search_hl' and the match list. */
5173 char_attr = search_hl.attr;
5174 cur = wp->w_match_head;
5175 shl_flag = FALSE;
5176 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005177 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005178 if (shl_flag == FALSE
5179 && ((cur != NULL
5180 && cur->priority > SEARCH_HL_PRIORITY)
5181 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005182 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005183 shl = &search_hl;
5184 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005185 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005186 else
5187 shl = &cur->hl;
5188 if ((ptr - line) - 1 == (long)shl->startcol)
5189 char_attr = shl->attr;
5190 if (shl != &search_hl && cur != NULL)
5191 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194#endif
5195 ScreenAttrs[off] = char_attr;
5196#ifdef FEAT_RIGHTLEFT
5197 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005200 --off;
5201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 else
5203#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005206 ++off;
5207 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005208 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005209#ifdef FEAT_SYN_HL
5210 eol_hl_off = 1;
5211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar91170f82006-05-05 21:15:17 +00005215 /*
5216 * At end of the text line.
5217 */
5218 if (c == NUL)
5219 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005220#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005221 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5222 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005223 {
5224 /* highlight last char after line */
5225 --col;
5226 --off;
5227 --vcol;
5228 }
5229
Bram Moolenaar1a384422010-07-14 19:53:30 +02005230 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005231 if (wp->w_p_wrap)
5232 v = wp->w_skipcol;
5233 else
5234 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005235
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005236 /* check if line ends before left margin */
5237 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005238 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005239#ifdef FEAT_CONCEAL
5240 /* Get rid of the boguscols now, we want to draw until the right
5241 * edge for 'cursorcolumn'. */
5242 col -= boguscols;
5243 boguscols = 0;
5244#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005245
5246 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005247 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005248
5249 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005250 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5251 && (int)wp->w_virtcol <
5252 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005253 && lnum != wp->w_cursor.lnum)
5254 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005255# ifdef FEAT_RIGHTLEFT
5256 && !wp->w_p_rl
5257# endif
5258 )
5259 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005260 int rightmost_vcol = 0;
5261 int i;
5262
5263 if (wp->w_p_cuc)
5264 rightmost_vcol = wp->w_virtcol;
5265 if (draw_color_col)
5266 /* determine rightmost colorcolumn to possibly draw */
5267 for (i = 0; color_cols[i] >= 0; ++i)
5268 if (rightmost_vcol < color_cols[i])
5269 rightmost_vcol = color_cols[i];
5270
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005271 while (col < W_WIDTH(wp))
5272 {
5273 ScreenLines[off] = ' ';
5274#ifdef FEAT_MBYTE
5275 if (enc_utf8)
5276 ScreenLinesUC[off] = 0;
5277#endif
5278 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005279 if (draw_color_col)
5280 draw_color_col = advance_color_col(VCOL_HLC,
5281 &color_cols);
5282
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005283 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005284 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005285 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005286 ScreenAttrs[off++] = hl_attr(HLF_MC);
5287 else
5288 ScreenAttrs[off++] = 0;
5289
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005290 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005291 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005292
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005293 ++vcol;
5294 }
5295 }
5296#endif
5297
Bram Moolenaar860cae12010-06-05 23:22:07 +02005298 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5299 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 row++;
5301
5302 /*
5303 * Update w_cline_height and w_cline_folded if the cursor line was
5304 * updated (saves a call to plines() later).
5305 */
5306 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5307 {
5308 curwin->w_cline_row = startrow;
5309 curwin->w_cline_height = row - startrow;
5310#ifdef FEAT_FOLDING
5311 curwin->w_cline_folded = FALSE;
5312#endif
5313 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5314 }
5315
5316 break;
5317 }
5318
5319 /* line continues beyond line end */
5320 if (lcs_ext
5321 && !wp->w_p_wrap
5322#ifdef FEAT_DIFF
5323 && filler_todo <= 0
5324#endif
5325 && (
5326#ifdef FEAT_RIGHTLEFT
5327 wp->w_p_rl ? col == 0 :
5328#endif
5329 col == W_WIDTH(wp) - 1)
5330 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005331 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5333 {
5334 c = lcs_ext;
5335 char_attr = hl_attr(HLF_AT);
5336#ifdef FEAT_MBYTE
5337 mb_c = c;
5338 if (enc_utf8 && (*mb_char2len)(c) > 1)
5339 {
5340 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005341 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005342 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344 else
5345 mb_utf8 = FALSE;
5346#endif
5347 }
5348
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005349#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005350 /* advance to the next 'colorcolumn' */
5351 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005352 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005353
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005354 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005355 * highlight the cursor position itself.
5356 * Also highlight the 'colorcolumn' if it is different than
5357 * 'cursorcolumn' */
5358 vcol_save_attr = -1;
5359 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005360 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005361 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005362 && lnum != wp->w_cursor.lnum)
5363 {
5364 vcol_save_attr = char_attr;
5365 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5366 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005367 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005368 {
5369 vcol_save_attr = char_attr;
5370 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5371 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005372 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005373#endif
5374
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 /*
5376 * Store character to be displayed.
5377 * Skip characters that are left of the screen for 'nowrap'.
5378 */
5379 vcol_prev = vcol;
5380 if (draw_state < WL_LINE || n_skip <= 0)
5381 {
5382 /*
5383 * Store the character.
5384 */
5385#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5386 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5387 {
5388 /* A double-wide character is: put first halve in left cell. */
5389 --off;
5390 --col;
5391 }
5392#endif
5393 ScreenLines[off] = c;
5394#ifdef FEAT_MBYTE
5395 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005396 {
5397 if ((mb_c & 0xff00) == 0x8e00)
5398 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 else if (enc_utf8)
5402 {
5403 if (mb_utf8)
5404 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005405 int i;
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005408 if ((c & 0xff) == 0)
5409 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005410 for (i = 0; i < Screen_mco; ++i)
5411 {
5412 ScreenLinesC[i][off] = u8cc[i];
5413 if (u8cc[i] == 0)
5414 break;
5415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
5417 else
5418 ScreenLinesUC[off] = 0;
5419 }
5420 if (multi_attr)
5421 {
5422 ScreenAttrs[off] = multi_attr;
5423 multi_attr = 0;
5424 }
5425 else
5426#endif
5427 ScreenAttrs[off] = char_attr;
5428
5429#ifdef FEAT_MBYTE
5430 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5431 {
5432 /* Need to fill two screen columns. */
5433 ++off;
5434 ++col;
5435 if (enc_utf8)
5436 /* UTF-8: Put a 0 in the second screen char. */
5437 ScreenLines[off] = 0;
5438 else
5439 /* DBCS: Put second byte in the second screen char. */
5440 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005441 if (draw_state > WL_NR
5442#ifdef FEAT_DIFF
5443 && filler_todo <= 0
5444#endif
5445 )
5446 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 /* When "tocol" is halfway a character, set it to the end of
5448 * the character, otherwise highlighting won't stop. */
5449 if (tocol == vcol)
5450 ++tocol;
5451#ifdef FEAT_RIGHTLEFT
5452 if (wp->w_p_rl)
5453 {
5454 /* now it's time to backup one cell */
5455 --off;
5456 --col;
5457 }
5458#endif
5459 }
5460#endif
5461#ifdef FEAT_RIGHTLEFT
5462 if (wp->w_p_rl)
5463 {
5464 --off;
5465 --col;
5466 }
5467 else
5468#endif
5469 {
5470 ++off;
5471 ++col;
5472 }
5473 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005475 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005476 {
5477 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005478 ++vcol_off;
5479 if (n_extra > 0)
5480 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005481 if (wp->w_p_wrap)
5482 {
5483 /*
5484 * Special voodoo required if 'wrap' is on.
5485 *
5486 * Advance the column indicator to force the line
5487 * drawing to wrap early. This will make the line
5488 * take up the same screen space when parts are concealed,
5489 * so that cursor line computations aren't messed up.
5490 *
5491 * To avoid the fictitious advance of 'col' causing
5492 * trailing junk to be written out of the screen line
5493 * we are building, 'boguscols' keeps track of the number
5494 * of bad columns we have advanced.
5495 */
5496 if (n_extra > 0)
5497 {
5498 vcol += n_extra;
5499# ifdef FEAT_RIGHTLEFT
5500 if (wp->w_p_rl)
5501 {
5502 col -= n_extra;
5503 boguscols -= n_extra;
5504 }
5505 else
5506# endif
5507 {
5508 col += n_extra;
5509 boguscols += n_extra;
5510 }
5511 n_extra = 0;
5512 n_attr = 0;
5513 }
5514
5515
5516# ifdef FEAT_MBYTE
5517 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5518 {
5519 /* Need to fill two screen columns. */
5520# ifdef FEAT_RIGHTLEFT
5521 if (wp->w_p_rl)
5522 {
5523 --boguscols;
5524 --col;
5525 }
5526 else
5527# endif
5528 {
5529 ++boguscols;
5530 ++col;
5531 }
5532 }
5533# endif
5534
5535# ifdef FEAT_RIGHTLEFT
5536 if (wp->w_p_rl)
5537 {
5538 --boguscols;
5539 --col;
5540 }
5541 else
5542# endif
5543 {
5544 ++boguscols;
5545 ++col;
5546 }
5547 }
5548 else
5549 {
5550 if (n_extra > 0)
5551 {
5552 vcol += n_extra;
5553 n_extra = 0;
5554 n_attr = 0;
5555 }
5556 }
5557
5558 }
5559#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 else
5561 --n_skip;
5562
Bram Moolenaar64486672010-05-16 15:46:46 +02005563 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5564 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005565 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566#ifdef FEAT_DIFF
5567 && filler_todo <= 0
5568#endif
5569 )
5570 ++vcol;
5571
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005572#ifdef FEAT_SYN_HL
5573 if (vcol_save_attr >= 0)
5574 char_attr = vcol_save_attr;
5575#endif
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 /* restore attributes after "predeces" in 'listchars' */
5578 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5579 char_attr = saved_attr3;
5580
5581 /* restore attributes after last 'listchars' or 'number' char */
5582 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5583 char_attr = saved_attr2;
5584
5585 /*
5586 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005587 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 */
5589 if ((
5590#ifdef FEAT_RIGHTLEFT
5591 wp->w_p_rl ? (col < 0) :
5592#endif
5593 (col >= W_WIDTH(wp)))
5594 && (*ptr != NUL
5595#ifdef FEAT_DIFF
5596 || filler_todo > 0
5597#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005598 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5600 )
5601 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005602#ifdef FEAT_CONCEAL
5603 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5604 (int)W_WIDTH(wp), wp->w_p_rl);
5605 boguscols = 0;
5606#else
5607 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5608 (int)W_WIDTH(wp), wp->w_p_rl);
5609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 ++row;
5611 ++screen_row;
5612
5613 /* When not wrapping and finished diff lines, or when displayed
5614 * '$' and highlighting until last column, break here. */
5615 if ((!wp->w_p_wrap
5616#ifdef FEAT_DIFF
5617 && filler_todo <= 0
5618#endif
5619 ) || lcs_eol_one == -1)
5620 break;
5621
5622 /* When the window is too narrow draw all "@" lines. */
5623 if (draw_state != WL_LINE
5624#ifdef FEAT_DIFF
5625 && filler_todo <= 0
5626#endif
5627 )
5628 {
5629 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005630#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 draw_vsep_win(wp, row);
5632#endif
5633 row = endrow;
5634 }
5635
5636 /* When line got too long for screen break here. */
5637 if (row == endrow)
5638 {
5639 ++row;
5640 break;
5641 }
5642
5643 if (screen_cur_row == screen_row - 1
5644#ifdef FEAT_DIFF
5645 && filler_todo <= 0
5646#endif
5647 && W_WIDTH(wp) == Columns)
5648 {
5649 /* Remember that the line wraps, used for modeless copy. */
5650 LineWraps[screen_row - 1] = TRUE;
5651
5652 /*
5653 * Special trick to make copy/paste of wrapped lines work with
5654 * xterm/screen: write an extra character beyond the end of
5655 * the line. This will work with all terminal types
5656 * (regardless of the xn,am settings).
5657 * Only do this on a fast tty.
5658 * Only do this if the cursor is on the current line
5659 * (something has been written in it).
5660 * Don't do this for the GUI.
5661 * Don't do this for double-width characters.
5662 * Don't do this for a window not at the right screen border.
5663 */
5664 if (p_tf
5665#ifdef FEAT_GUI
5666 && !gui.in_use
5667#endif
5668#ifdef FEAT_MBYTE
5669 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005670 && ((*mb_off2cells)(LineOffset[screen_row],
5671 LineOffset[screen_row] + screen_Columns)
5672 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005674 + (int)Columns - 2,
5675 LineOffset[screen_row] + screen_Columns)
5676 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#endif
5678 )
5679 {
5680 /* First make sure we are at the end of the screen line,
5681 * then output the same character again to let the
5682 * terminal know about the wrap. If the terminal doesn't
5683 * auto-wrap, we overwrite the character. */
5684 if (screen_cur_col != W_WIDTH(wp))
5685 screen_char(LineOffset[screen_row - 1]
5686 + (unsigned)Columns - 1,
5687 screen_row - 1, (int)(Columns - 1));
5688
5689#ifdef FEAT_MBYTE
5690 /* When there is a multi-byte character, just output a
5691 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005692 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5693 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 out_char(' ');
5695 else
5696#endif
5697 out_char(ScreenLines[LineOffset[screen_row - 1]
5698 + (Columns - 1)]);
5699 /* force a redraw of the first char on the next line */
5700 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5701 screen_start(); /* don't know where cursor is now */
5702 }
5703 }
5704
5705 col = 0;
5706 off = (unsigned)(current_ScreenLine - ScreenLines);
5707#ifdef FEAT_RIGHTLEFT
5708 if (wp->w_p_rl)
5709 {
5710 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5711 off += col;
5712 }
5713#endif
5714
5715 /* reset the drawing state for the start of a wrapped line */
5716 draw_state = WL_START;
5717 saved_n_extra = n_extra;
5718 saved_p_extra = p_extra;
5719 saved_c_extra = c_extra;
5720 saved_char_attr = char_attr;
5721 n_extra = 0;
5722 lcs_prec_todo = lcs_prec;
5723#ifdef FEAT_LINEBREAK
5724# ifdef FEAT_DIFF
5725 if (filler_todo <= 0)
5726# endif
5727 need_showbreak = TRUE;
5728#endif
5729#ifdef FEAT_DIFF
5730 --filler_todo;
5731 /* When the filler lines are actually below the last line of the
5732 * file, don't draw the line itself, break here. */
5733 if (filler_todo == 0 && wp->w_botfill)
5734 break;
5735#endif
5736 }
5737
5738 } /* for every character in the line */
5739
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005740#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005741 /* After an empty line check first word for capital. */
5742 if (*skipwhite(line) == NUL)
5743 {
5744 capcol_lnum = lnum + 1;
5745 cap_col = 0;
5746 }
5747#endif
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 return row;
5750}
5751
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005752#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005753static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005754
5755/*
5756 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005757 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005758 */
5759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005760comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005761{
5762 int i;
5763
5764 for (i = 0; i < Screen_mco; ++i)
5765 {
5766 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5767 return TRUE;
5768 if (ScreenLinesC[i][off_from] == 0)
5769 break;
5770 }
5771 return FALSE;
5772}
5773#endif
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775/*
5776 * Check whether the given character needs redrawing:
5777 * - the (first byte of the) character is different
5778 * - the attributes are different
5779 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005780 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 */
5782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005783char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 if (cols > 0
5786 && ((ScreenLines[off_from] != ScreenLines[off_to]
5787 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5788
5789#ifdef FEAT_MBYTE
5790 || (enc_dbcs != 0
5791 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5792 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5793 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5794 : (cols > 1 && ScreenLines[off_from + 1]
5795 != ScreenLines[off_to + 1])))
5796 || (enc_utf8
5797 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5798 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005799 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005800 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5801 && ScreenLines[off_from + 1]
5802 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803#endif
5804 ))
5805 return TRUE;
5806 return FALSE;
5807}
5808
5809/*
5810 * Move one "cooked" screen line to the screen, but only the characters that
5811 * have actually changed. Handle insert/delete character.
5812 * "coloff" gives the first column on the screen for this line.
5813 * "endcol" gives the columns where valid characters are.
5814 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5815 * needs to be cleared, negative otherwise.
5816 * "rlflag" is TRUE in a rightleft window:
5817 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5818 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5819 */
5820 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005821screen_line(
5822 int row,
5823 int coloff,
5824 int endcol,
5825 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005827 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005829 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 unsigned off_from;
5832 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005833#ifdef FEAT_MBYTE
5834 unsigned max_off_from;
5835 unsigned max_off_to;
5836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005838#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 int hl;
5840#endif
5841 int force = FALSE; /* force update rest of the line */
5842 int redraw_this /* bool: does character need redraw? */
5843#ifdef FEAT_GUI
5844 = TRUE /* For GUI when while-loop empty */
5845#endif
5846 ;
5847 int redraw_next; /* redraw_this for next character */
5848#ifdef FEAT_MBYTE
5849 int clear_next = FALSE;
5850 int char_cells; /* 1: normal char */
5851 /* 2: occupies two display cells */
5852# define CHAR_CELLS char_cells
5853#else
5854# define CHAR_CELLS 1
5855#endif
5856
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005857 /* Check for illegal row and col, just in case. */
5858 if (row >= Rows)
5859 row = Rows - 1;
5860 if (endcol > Columns)
5861 endcol = Columns;
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863# ifdef FEAT_CLIPBOARD
5864 clip_may_clear_selection(row, row);
5865# endif
5866
5867 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5868 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005869#ifdef FEAT_MBYTE
5870 max_off_from = off_from + screen_Columns;
5871 max_off_to = LineOffset[row] + screen_Columns;
5872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874#ifdef FEAT_RIGHTLEFT
5875 if (rlflag)
5876 {
5877 /* Clear rest first, because it's left of the text. */
5878 if (clear_width > 0)
5879 {
5880 while (col <= endcol && ScreenLines[off_to] == ' '
5881 && ScreenAttrs[off_to] == 0
5882# ifdef FEAT_MBYTE
5883 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5884# endif
5885 )
5886 {
5887 ++off_to;
5888 ++col;
5889 }
5890 if (col <= endcol)
5891 screen_fill(row, row + 1, col + coloff,
5892 endcol + coloff + 1, ' ', ' ', 0);
5893 }
5894 col = endcol + 1;
5895 off_to = LineOffset[row] + col + coloff;
5896 off_from += col;
5897 endcol = (clear_width > 0 ? clear_width : -clear_width);
5898 }
5899#endif /* FEAT_RIGHTLEFT */
5900
5901 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5902
5903 while (col < endcol)
5904 {
5905#ifdef FEAT_MBYTE
5906 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005907 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 else
5909 char_cells = 1;
5910#endif
5911
5912 redraw_this = redraw_next;
5913 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5914 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5915
5916#ifdef FEAT_GUI
5917 /* If the next character was bold, then redraw the current character to
5918 * remove any pixels that might have spilt over into us. This only
5919 * happens in the GUI.
5920 */
5921 if (redraw_next && gui.in_use)
5922 {
5923 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005924 if (hl > HL_ALL)
5925 hl = syn_attr2attr(hl);
5926 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 redraw_this = TRUE;
5928 }
5929#endif
5930
5931 if (redraw_this)
5932 {
5933 /*
5934 * Special handling when 'xs' termcap flag set (hpterm):
5935 * Attributes for characters are stored at the position where the
5936 * cursor is when writing the highlighting code. The
5937 * start-highlighting code must be written with the cursor on the
5938 * first highlighted character. The stop-highlighting code must
5939 * be written with the cursor just after the last highlighted
5940 * character.
5941 * Overwriting a character doesn't remove it's highlighting. Need
5942 * to clear the rest of the line, and force redrawing it
5943 * completely.
5944 */
5945 if ( p_wiv
5946 && !force
5947#ifdef FEAT_GUI
5948 && !gui.in_use
5949#endif
5950 && ScreenAttrs[off_to] != 0
5951 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5952 {
5953 /*
5954 * Need to remove highlighting attributes here.
5955 */
5956 windgoto(row, col + coloff);
5957 out_str(T_CE); /* clear rest of this screen line */
5958 screen_start(); /* don't know where cursor is now */
5959 force = TRUE; /* force redraw of rest of the line */
5960 redraw_next = TRUE; /* or else next char would miss out */
5961
5962 /*
5963 * If the previous character was highlighted, need to stop
5964 * highlighting at this character.
5965 */
5966 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5967 {
5968 screen_attr = ScreenAttrs[off_to - 1];
5969 term_windgoto(row, col + coloff);
5970 screen_stop_highlight();
5971 }
5972 else
5973 screen_attr = 0; /* highlighting has stopped */
5974 }
5975#ifdef FEAT_MBYTE
5976 if (enc_dbcs != 0)
5977 {
5978 /* Check if overwriting a double-byte with a single-byte or
5979 * the other way around requires another character to be
5980 * redrawn. For UTF-8 this isn't needed, because comparing
5981 * ScreenLinesUC[] is sufficient. */
5982 if (char_cells == 1
5983 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005984 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 /* Writing a single-cell character over a double-cell
5987 * character: need to redraw the next cell. */
5988 ScreenLines[off_to + 1] = 0;
5989 redraw_next = TRUE;
5990 }
5991 else if (char_cells == 2
5992 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005993 && (*mb_off2cells)(off_to, max_off_to) == 1
5994 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 /* Writing the second half of a double-cell character over
5997 * a double-cell character: need to redraw the second
5998 * cell. */
5999 ScreenLines[off_to + 2] = 0;
6000 redraw_next = TRUE;
6001 }
6002
6003 if (enc_dbcs == DBCS_JPNU)
6004 ScreenLines2[off_to] = ScreenLines2[off_from];
6005 }
6006 /* When writing a single-width character over a double-width
6007 * character and at the end of the redrawn text, need to clear out
6008 * the right halve of the old character.
6009 * Also required when writing the right halve of a double-width
6010 * char over the left halve of an existing one. */
6011 if (has_mbyte && col + char_cells == endcol
6012 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006013 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006015 && (*mb_off2cells)(off_to, max_off_to) == 1
6016 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 clear_next = TRUE;
6018#endif
6019
6020 ScreenLines[off_to] = ScreenLines[off_from];
6021#ifdef FEAT_MBYTE
6022 if (enc_utf8)
6023 {
6024 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6025 if (ScreenLinesUC[off_from] != 0)
6026 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 int i;
6028
6029 for (i = 0; i < Screen_mco; ++i)
6030 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 }
6033 if (char_cells == 2)
6034 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6035#endif
6036
6037#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006038 /* The bold trick makes a single column of pixels appear in the
6039 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 * character should be redrawn too. This happens for our own GUI
6041 * and for some xterms. */
6042 if (
6043# ifdef FEAT_GUI
6044 gui.in_use
6045# endif
6046# if defined(FEAT_GUI) && defined(UNIX)
6047 ||
6048# endif
6049# ifdef UNIX
6050 term_is_xterm
6051# endif
6052 )
6053 {
6054 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006055 if (hl > HL_ALL)
6056 hl = syn_attr2attr(hl);
6057 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 redraw_next = TRUE;
6059 }
6060#endif
6061 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6062#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006063 /* For simplicity set the attributes of second half of a
6064 * double-wide character equal to the first half. */
6065 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006067
6068 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 else
6071#endif
6072 screen_char(off_to, row, col + coloff);
6073 }
6074 else if ( p_wiv
6075#ifdef FEAT_GUI
6076 && !gui.in_use
6077#endif
6078 && col + coloff > 0)
6079 {
6080 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6081 {
6082 /*
6083 * Don't output stop-highlight when moving the cursor, it will
6084 * stop the highlighting when it should continue.
6085 */
6086 screen_attr = 0;
6087 }
6088 else if (screen_attr != 0)
6089 screen_stop_highlight();
6090 }
6091
6092 off_to += CHAR_CELLS;
6093 off_from += CHAR_CELLS;
6094 col += CHAR_CELLS;
6095 }
6096
6097#ifdef FEAT_MBYTE
6098 if (clear_next)
6099 {
6100 /* Clear the second half of a double-wide character of which the left
6101 * half was overwritten with a single-wide character. */
6102 ScreenLines[off_to] = ' ';
6103 if (enc_utf8)
6104 ScreenLinesUC[off_to] = 0;
6105 screen_char(off_to, row, col + coloff);
6106 }
6107#endif
6108
6109 if (clear_width > 0
6110#ifdef FEAT_RIGHTLEFT
6111 && !rlflag
6112#endif
6113 )
6114 {
6115#ifdef FEAT_GUI
6116 int startCol = col;
6117#endif
6118
6119 /* blank out the rest of the line */
6120 while (col < clear_width && ScreenLines[off_to] == ' '
6121 && ScreenAttrs[off_to] == 0
6122#ifdef FEAT_MBYTE
6123 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6124#endif
6125 )
6126 {
6127 ++off_to;
6128 ++col;
6129 }
6130 if (col < clear_width)
6131 {
6132#ifdef FEAT_GUI
6133 /*
6134 * In the GUI, clearing the rest of the line may leave pixels
6135 * behind if the first character cleared was bold. Some bold
6136 * fonts spill over the left. In this case we redraw the previous
6137 * character too. If we didn't skip any blanks above, then we
6138 * only redraw if the character wasn't already redrawn anyway.
6139 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006140 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 hl = ScreenAttrs[off_to];
6143 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006144 {
6145 int prev_cells = 1;
6146# ifdef FEAT_MBYTE
6147 if (enc_utf8)
6148 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6149 * that its width is 2. */
6150 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6151 else if (enc_dbcs != 0)
6152 {
6153 /* find previous character by counting from first
6154 * column and get its width. */
6155 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006156 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006157
6158 while (off < off_to)
6159 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006160 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006161 off += prev_cells;
6162 }
6163 }
6164
6165 if (enc_dbcs != 0 && prev_cells > 1)
6166 screen_char_2(off_to - prev_cells, row,
6167 col + coloff - prev_cells);
6168 else
6169# endif
6170 screen_char(off_to - prev_cells, row,
6171 col + coloff - prev_cells);
6172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174#endif
6175 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6176 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006177#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 off_to += clear_width - col;
6179 col = clear_width;
6180#endif
6181 }
6182 }
6183
6184 if (clear_width > 0)
6185 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006186#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 /* For a window that's left of another, draw the separator char. */
6188 if (col + coloff < Columns)
6189 {
6190 int c;
6191
6192 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006193 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006195 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6196 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197# endif
6198 || ScreenAttrs[off_to] != hl)
6199 {
6200 ScreenLines[off_to] = c;
6201 ScreenAttrs[off_to] = hl;
6202# ifdef FEAT_MBYTE
6203 if (enc_utf8)
6204 {
6205 if (c >= 0x80)
6206 {
6207 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006208 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 else
6211 ScreenLinesUC[off_to] = 0;
6212 }
6213# endif
6214 screen_char(off_to, row, col + coloff);
6215 }
6216 }
6217 else
6218#endif
6219 LineWraps[row] = FALSE;
6220 }
6221}
6222
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006223#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006225 * Mirror text "str" for right-left displaying.
6226 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006228 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006229rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 char_u *p1, *p2;
6232 int t;
6233
6234 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6235 {
6236 t = *p1;
6237 *p1 = *p2;
6238 *p2 = t;
6239 }
6240}
6241#endif
6242
6243#if defined(FEAT_WINDOWS) || defined(PROTO)
6244/*
6245 * mark all status lines for redraw; used after first :cd
6246 */
6247 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006248status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 win_T *wp;
6251
6252 for (wp = firstwin; wp; wp = wp->w_next)
6253 if (wp->w_status_height)
6254 {
6255 wp->w_redr_status = TRUE;
6256 redraw_later(VALID);
6257 }
6258}
6259
6260/*
6261 * mark all status lines of the current buffer for redraw
6262 */
6263 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006264status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 win_T *wp;
6267
6268 for (wp = firstwin; wp; wp = wp->w_next)
6269 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6270 {
6271 wp->w_redr_status = TRUE;
6272 redraw_later(VALID);
6273 }
6274}
6275
6276/*
6277 * Redraw all status lines that need to be redrawn.
6278 */
6279 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006280redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
6282 win_T *wp;
6283
6284 for (wp = firstwin; wp; wp = wp->w_next)
6285 if (wp->w_redr_status)
6286 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006287 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006288 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289}
6290#endif
6291
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006292#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293/*
6294 * Redraw all status lines at the bottom of frame "frp".
6295 */
6296 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006297win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 if (frp->fr_layout == FR_LEAF)
6300 frp->fr_win->w_redr_status = TRUE;
6301 else if (frp->fr_layout == FR_ROW)
6302 {
6303 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6304 win_redraw_last_status(frp);
6305 }
6306 else /* frp->fr_layout == FR_COL */
6307 {
6308 frp = frp->fr_child;
6309 while (frp->fr_next != NULL)
6310 frp = frp->fr_next;
6311 win_redraw_last_status(frp);
6312 }
6313}
6314#endif
6315
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006316#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317/*
6318 * Draw the verticap separator right of window "wp" starting with line "row".
6319 */
6320 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006321draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 int hl;
6324 int c;
6325
6326 if (wp->w_vsep_width)
6327 {
6328 /* draw the vertical separator right of this window */
6329 c = fillchar_vsep(&hl);
6330 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6331 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6332 c, ' ', hl);
6333 }
6334}
6335#endif
6336
6337#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006338static int status_match_len(expand_T *xp, char_u *s);
6339static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340
6341/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006342 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 */
6344 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006345status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 int len = 0;
6348
6349#ifdef FEAT_MENU
6350 int emenu = (xp->xp_context == EXPAND_MENUS
6351 || xp->xp_context == EXPAND_MENUNAMES);
6352
6353 /* Check for menu separators - replace with '|'. */
6354 if (emenu && menu_is_separator(s))
6355 return 1;
6356#endif
6357
6358 while (*s != NUL)
6359 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006360 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006361 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006362 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364
6365 return len;
6366}
6367
6368/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006369 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006370 * These are backslashes used for escaping. Do show backslashes in help tags.
6371 */
6372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006373skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006374{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006375 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006376#ifdef FEAT_MENU
6377 || ((xp->xp_context == EXPAND_MENUS
6378 || xp->xp_context == EXPAND_MENUNAMES)
6379 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6380#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006381 )
6382 {
6383#ifndef BACKSLASH_IN_FILENAME
6384 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6385 return 2;
6386#endif
6387 return 1;
6388 }
6389 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006390}
6391
6392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Show wildchar matches in the status line.
6394 * Show at least the "match" item.
6395 * We start at item 'first_match' in the list and show all matches that fit.
6396 *
6397 * If inversion is possible we use it. Else '=' characters are used.
6398 */
6399 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006400win_redr_status_matches(
6401 expand_T *xp,
6402 int num_matches,
6403 char_u **matches, /* list of matches */
6404 int match,
6405 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6408 int row;
6409 char_u *buf;
6410 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006411 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 int fillchar;
6413 int attr;
6414 int i;
6415 int highlight = TRUE;
6416 char_u *selstart = NULL;
6417 int selstart_col = 0;
6418 char_u *selend = NULL;
6419 static int first_match = 0;
6420 int add_left = FALSE;
6421 char_u *s;
6422#ifdef FEAT_MENU
6423 int emenu;
6424#endif
6425#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6426 int l;
6427#endif
6428
6429 if (matches == NULL) /* interrupted completion? */
6430 return;
6431
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006432#ifdef FEAT_MBYTE
6433 if (has_mbyte)
6434 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6435 else
6436#endif
6437 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (buf == NULL)
6439 return;
6440
6441 if (match == -1) /* don't show match but original text */
6442 {
6443 match = 0;
6444 highlight = FALSE;
6445 }
6446 /* count 1 for the ending ">" */
6447 clen = status_match_len(xp, L_MATCH(match)) + 3;
6448 if (match == 0)
6449 first_match = 0;
6450 else if (match < first_match)
6451 {
6452 /* jumping left, as far as we can go */
6453 first_match = match;
6454 add_left = TRUE;
6455 }
6456 else
6457 {
6458 /* check if match fits on the screen */
6459 for (i = first_match; i < match; ++i)
6460 clen += status_match_len(xp, L_MATCH(i)) + 2;
6461 if (first_match > 0)
6462 clen += 2;
6463 /* jumping right, put match at the left */
6464 if ((long)clen > Columns)
6465 {
6466 first_match = match;
6467 /* if showing the last match, we can add some on the left */
6468 clen = 2;
6469 for (i = match; i < num_matches; ++i)
6470 {
6471 clen += status_match_len(xp, L_MATCH(i)) + 2;
6472 if ((long)clen >= Columns)
6473 break;
6474 }
6475 if (i == num_matches)
6476 add_left = TRUE;
6477 }
6478 }
6479 if (add_left)
6480 while (first_match > 0)
6481 {
6482 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6483 if ((long)clen >= Columns)
6484 break;
6485 --first_match;
6486 }
6487
6488 fillchar = fillchar_status(&attr, TRUE);
6489
6490 if (first_match == 0)
6491 {
6492 *buf = NUL;
6493 len = 0;
6494 }
6495 else
6496 {
6497 STRCPY(buf, "< ");
6498 len = 2;
6499 }
6500 clen = len;
6501
6502 i = first_match;
6503 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6504 {
6505 if (i == match)
6506 {
6507 selstart = buf + len;
6508 selstart_col = clen;
6509 }
6510
6511 s = L_MATCH(i);
6512 /* Check for menu separators - replace with '|' */
6513#ifdef FEAT_MENU
6514 emenu = (xp->xp_context == EXPAND_MENUS
6515 || xp->xp_context == EXPAND_MENUNAMES);
6516 if (emenu && menu_is_separator(s))
6517 {
6518 STRCPY(buf + len, transchar('|'));
6519 l = (int)STRLEN(buf + len);
6520 len += l;
6521 clen += l;
6522 }
6523 else
6524#endif
6525 for ( ; *s != NUL; ++s)
6526 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006527 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 clen += ptr2cells(s);
6529#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006530 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
6532 STRNCPY(buf + len, s, l);
6533 s += l - 1;
6534 len += l;
6535 }
6536 else
6537#endif
6538 {
6539 STRCPY(buf + len, transchar_byte(*s));
6540 len += (int)STRLEN(buf + len);
6541 }
6542 }
6543 if (i == match)
6544 selend = buf + len;
6545
6546 *(buf + len++) = ' ';
6547 *(buf + len++) = ' ';
6548 clen += 2;
6549 if (++i == num_matches)
6550 break;
6551 }
6552
6553 if (i != num_matches)
6554 {
6555 *(buf + len++) = '>';
6556 ++clen;
6557 }
6558
6559 buf[len] = NUL;
6560
6561 row = cmdline_row - 1;
6562 if (row >= 0)
6563 {
6564 if (wild_menu_showing == 0)
6565 {
6566 if (msg_scrolled > 0)
6567 {
6568 /* Put the wildmenu just above the command line. If there is
6569 * no room, scroll the screen one line up. */
6570 if (cmdline_row == Rows - 1)
6571 {
6572 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6573 ++msg_scrolled;
6574 }
6575 else
6576 {
6577 ++cmdline_row;
6578 ++row;
6579 }
6580 wild_menu_showing = WM_SCROLLED;
6581 }
6582 else
6583 {
6584 /* Create status line if needed by setting 'laststatus' to 2.
6585 * Set 'winminheight' to zero to avoid that the window is
6586 * resized. */
6587 if (lastwin->w_status_height == 0)
6588 {
6589 save_p_ls = p_ls;
6590 save_p_wmh = p_wmh;
6591 p_ls = 2;
6592 p_wmh = 0;
6593 last_status(FALSE);
6594 }
6595 wild_menu_showing = WM_SHOWN;
6596 }
6597 }
6598
6599 screen_puts(buf, row, 0, attr);
6600 if (selstart != NULL && highlight)
6601 {
6602 *selend = NUL;
6603 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6604 }
6605
6606 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6607 }
6608
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006609#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 win_redraw_last_status(topframe);
6611#else
6612 lastwin->w_redr_status = TRUE;
6613#endif
6614 vim_free(buf);
6615}
6616#endif
6617
6618#if defined(FEAT_WINDOWS) || defined(PROTO)
6619/*
6620 * Redraw the status line of window wp.
6621 *
6622 * If inversion is possible we use it. Else '=' characters are used.
6623 */
6624 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006625win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 int row;
6628 char_u *p;
6629 int len;
6630 int fillchar;
6631 int attr;
6632 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006633 static int busy = FALSE;
6634
6635 /* It's possible to get here recursively when 'statusline' (indirectly)
6636 * invokes ":redrawstatus". Simply ignore the call then. */
6637 if (busy)
6638 return;
6639 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641 wp->w_redr_status = FALSE;
6642 if (wp->w_status_height == 0)
6643 {
6644 /* no status line, can only be last window */
6645 redraw_cmdline = TRUE;
6646 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006647 else if (!redrawing()
6648#ifdef FEAT_INS_EXPAND
6649 /* don't update status line when popup menu is visible and may be
6650 * drawn over it */
6651 || pum_visible()
6652#endif
6653 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
6655 /* Don't redraw right now, do it later. */
6656 wp->w_redr_status = TRUE;
6657 }
6658#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006659 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006662 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
6664#endif
6665 else
6666 {
6667 fillchar = fillchar_status(&attr, wp == curwin);
6668
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006669 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 p = NameBuff;
6671 len = (int)STRLEN(p);
6672
6673 if (wp->w_buffer->b_help
6674#ifdef FEAT_QUICKFIX
6675 || wp->w_p_pvw
6676#endif
6677 || bufIsChanged(wp->w_buffer)
6678 || wp->w_buffer->b_p_ro)
6679 *(p + len++) = ' ';
6680 if (wp->w_buffer->b_help)
6681 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006682 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 len += (int)STRLEN(p + len);
6684 }
6685#ifdef FEAT_QUICKFIX
6686 if (wp->w_p_pvw)
6687 {
6688 STRCPY(p + len, _("[Preview]"));
6689 len += (int)STRLEN(p + len);
6690 }
6691#endif
6692 if (bufIsChanged(wp->w_buffer))
6693 {
6694 STRCPY(p + len, "[+]");
6695 len += 3;
6696 }
6697 if (wp->w_buffer->b_p_ro)
6698 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006699 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 len += 4;
6701 }
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6704 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6705 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6706 if (this_ru_col <= 1)
6707 {
6708 p = (char_u *)"<"; /* No room for file name! */
6709 len = 1;
6710 }
6711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712#ifdef FEAT_MBYTE
6713 if (has_mbyte)
6714 {
6715 int clen = 0, i;
6716
6717 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006718 clen = mb_string2cells(p, -1);
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 /* Find first character that will fit.
6721 * Going from start to end is much faster for DBCS. */
6722 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006723 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 clen -= (*mb_ptr2cells)(p + i);
6725 len = clen;
6726 if (i > 0)
6727 {
6728 p = p + i - 1;
6729 *p = '<';
6730 ++len;
6731 }
6732
6733 }
6734 else
6735#endif
6736 if (len > this_ru_col - 1)
6737 {
6738 p += len - (this_ru_col - 1);
6739 *p = '<';
6740 len = this_ru_col - 1;
6741 }
6742
6743 row = W_WINROW(wp) + wp->w_height;
6744 screen_puts(p, row, W_WINCOL(wp), attr);
6745 screen_fill(row, row + 1, len + W_WINCOL(wp),
6746 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6747
6748 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6749 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6750 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6751 - 1 + W_WINCOL(wp)), attr);
6752
6753#ifdef FEAT_CMDL_INFO
6754 win_redr_ruler(wp, TRUE);
6755#endif
6756 }
6757
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 /*
6759 * May need to draw the character below the vertical separator.
6760 */
6761 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6762 {
6763 if (stl_connected(wp))
6764 fillchar = fillchar_status(&attr, wp == curwin);
6765 else
6766 fillchar = fillchar_vsep(&attr);
6767 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6768 attr);
6769 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006770 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
Bram Moolenaar238a5642006-02-21 22:12:05 +00006773#ifdef FEAT_STL_OPT
6774/*
6775 * Redraw the status line according to 'statusline' and take care of any
6776 * errors encountered.
6777 */
6778 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006779redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006780{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006781 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006782 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006783
6784 /* When called recursively return. This can happen when the statusline
6785 * contains an expression that triggers a redraw. */
6786 if (entered)
6787 return;
6788 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006789
Bram Moolenaara742e082016-04-05 21:10:38 +02006790 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006791 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006792 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006793 {
6794 /* When there is an error disable the statusline, otherwise the
6795 * display is messed up with errors and a redraw triggers the problem
6796 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006797 set_string_option_direct((char_u *)"statusline", -1,
6798 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006799 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006800 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006801 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006802 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006803}
6804#endif
6805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806/*
6807 * Return TRUE if the status line of window "wp" is connected to the status
6808 * line of the window right of it. If not, then it's a vertical separator.
6809 * Only call if (wp->w_vsep_width != 0).
6810 */
6811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006812stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814 frame_T *fr;
6815
6816 fr = wp->w_frame;
6817 while (fr->fr_parent != NULL)
6818 {
6819 if (fr->fr_parent->fr_layout == FR_COL)
6820 {
6821 if (fr->fr_next != NULL)
6822 break;
6823 }
6824 else
6825 {
6826 if (fr->fr_next != NULL)
6827 return TRUE;
6828 }
6829 fr = fr->fr_parent;
6830 }
6831 return FALSE;
6832}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
6834#endif /* FEAT_WINDOWS */
6835
6836#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6837/*
6838 * Get the value to show for the language mappings, active 'keymap'.
6839 */
6840 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006841get_keymap_str(
6842 win_T *wp,
6843 char_u *buf, /* buffer for the result */
6844 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 char_u *p;
6847
6848 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6849 return FALSE;
6850
6851 {
6852#ifdef FEAT_EVAL
6853 buf_T *old_curbuf = curbuf;
6854 win_T *old_curwin = curwin;
6855 char_u *s;
6856
6857 curbuf = wp->w_buffer;
6858 curwin = wp;
6859 STRCPY(buf, "b:keymap_name"); /* must be writable */
6860 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006861 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 --emsg_skip;
6863 curbuf = old_curbuf;
6864 curwin = old_curwin;
6865 if (p == NULL || *p == NUL)
6866#endif
6867 {
6868#ifdef FEAT_KEYMAP
6869 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6870 p = wp->w_buffer->b_p_keymap;
6871 else
6872#endif
6873 p = (char_u *)"lang";
6874 }
6875 if ((int)(STRLEN(p) + 3) < len)
6876 sprintf((char *)buf, "<%s>", p);
6877 else
6878 buf[0] = NUL;
6879#ifdef FEAT_EVAL
6880 vim_free(s);
6881#endif
6882 }
6883 return buf[0] != NUL;
6884}
6885#endif
6886
6887#if defined(FEAT_STL_OPT) || defined(PROTO)
6888/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006889 * Redraw the status line or ruler of window "wp".
6890 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 */
6892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006893win_redr_custom(
6894 win_T *wp,
6895 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006897 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 int attr;
6899 int curattr;
6900 int row;
6901 int col = 0;
6902 int maxwidth;
6903 int width;
6904 int n;
6905 int len;
6906 int fillchar;
6907 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006908 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006910 struct stl_hlrec hltab[STL_MAX_ITEM];
6911 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006912 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006913 win_T *ewp;
6914 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
Bram Moolenaar1d633412013-12-11 15:52:01 +01006916 /* There is a tiny chance that this gets called recursively: When
6917 * redrawing a status line triggers redrawing the ruler or tabline.
6918 * Avoid trouble by not allowing recursion. */
6919 if (entered)
6920 return;
6921 entered = TRUE;
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006924 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006926 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006927 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006928 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006929 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006930 attr = hl_attr(HLF_TPF);
6931 maxwidth = Columns;
6932# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006933 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006934# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006936 else
6937 {
6938 row = W_WINROW(wp) + wp->w_height;
6939 fillchar = fillchar_status(&attr, wp == curwin);
6940 maxwidth = W_WIDTH(wp);
6941
6942 if (draw_ruler)
6943 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006944 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006945 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006946 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006947 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006948 if (*++stl == '-')
6949 stl++;
6950 if (atoi((char *)stl))
6951 while (VIM_ISDIGIT(*stl))
6952 stl++;
6953 if (*stl++ != '(')
6954 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006955 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006956#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006957 col = ru_col - (Columns - W_WIDTH(wp));
6958 if (col < (W_WIDTH(wp) + 1) / 2)
6959 col = (W_WIDTH(wp) + 1) / 2;
6960#else
6961 col = ru_col;
6962 if (col > (Columns + 1) / 2)
6963 col = (Columns + 1) / 2;
6964#endif
6965 maxwidth = W_WIDTH(wp) - col;
6966#ifdef FEAT_WINDOWS
6967 if (!wp->w_status_height)
6968#endif
6969 {
6970 row = Rows - 1;
6971 --maxwidth; /* writing in last column may cause scrolling */
6972 fillchar = ' ';
6973 attr = 0;
6974 }
6975
6976# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006977 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006978# endif
6979 }
6980 else
6981 {
6982 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006983 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006984 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006985 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006986# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006987 use_sandbox = was_set_insecurely((char_u *)"statusline",
6988 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006989# endif
6990 }
6991
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006992#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006993 col += W_WINCOL(wp);
6994#endif
6995 }
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006998 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
Bram Moolenaar61452852011-02-01 18:01:11 +01007000 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7001 * the cursor away and back. */
7002 ewp = wp == NULL ? curwin : wp;
7003 p_crb_save = ewp->w_p_crb;
7004 ewp->w_p_crb = FALSE;
7005
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 /* Make a copy, because the statusline may include a function call that
7007 * might change the option value and free the memory. */
7008 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007009 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007011 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007013 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007015 /* Make all characters printable. */
7016 p = transstr(buf);
7017 if (p != NULL)
7018 {
7019 vim_strncpy(buf, p, sizeof(buf) - 1);
7020 vim_free(p);
7021 }
7022
7023 /* fill up with "fillchar" */
7024 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007025 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
7027#ifdef FEAT_MBYTE
7028 len += (*mb_char2bytes)(fillchar, buf + len);
7029#else
7030 buf[len++] = fillchar;
7031#endif
7032 ++width;
7033 }
7034 buf[len] = NUL;
7035
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007036 /*
7037 * Draw each snippet with the specified highlighting.
7038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 curattr = attr;
7040 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007041 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007043 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 screen_puts_len(p, len, row, col, curattr);
7045 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007046 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007048 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007050 else if (hltab[n].userhl < 0)
7051 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007053 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007054 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#endif
7056 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007057 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 }
7059 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007060
7061 if (wp == NULL)
7062 {
7063 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7064 col = 0;
7065 len = 0;
7066 p = buf;
7067 fillchar = 0;
7068 for (n = 0; tabtab[n].start != NULL; n++)
7069 {
7070 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7071 while (col < len)
7072 TabPageIdxs[col++] = fillchar;
7073 p = tabtab[n].start;
7074 fillchar = tabtab[n].userhl;
7075 }
7076 while (col < Columns)
7077 TabPageIdxs[col++] = fillchar;
7078 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007079
7080theend:
7081 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082}
7083
7084#endif /* FEAT_STL_OPT */
7085
7086/*
7087 * Output a single character directly to the screen and update ScreenLines.
7088 */
7089 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007090screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 char_u buf[MB_MAXBYTES + 1];
7093
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007094#ifdef FEAT_MBYTE
7095 if (has_mbyte)
7096 buf[(*mb_char2bytes)(c, buf)] = NUL;
7097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007099 {
7100 buf[0] = c;
7101 buf[1] = NUL;
7102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 screen_puts(buf, row, col, attr);
7104}
7105
7106/*
7107 * Get a single character directly from ScreenLines into "bytes[]".
7108 * Also return its attribute in *attrp;
7109 */
7110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007111screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112{
7113 unsigned off;
7114
7115 /* safety check */
7116 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7117 {
7118 off = LineOffset[row] + col;
7119 *attrp = ScreenAttrs[off];
7120 bytes[0] = ScreenLines[off];
7121 bytes[1] = NUL;
7122
7123#ifdef FEAT_MBYTE
7124 if (enc_utf8 && ScreenLinesUC[off] != 0)
7125 bytes[utfc_char2bytes(off, bytes)] = NUL;
7126 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7127 {
7128 bytes[0] = ScreenLines[off];
7129 bytes[1] = ScreenLines2[off];
7130 bytes[2] = NUL;
7131 }
7132 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7133 {
7134 bytes[1] = ScreenLines[off + 1];
7135 bytes[2] = NUL;
7136 }
7137#endif
7138 }
7139}
7140
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007141#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007142static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007143
7144/*
7145 * Return TRUE if composing characters for screen posn "off" differs from
7146 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007147 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007148 */
7149 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007150screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007151{
7152 int i;
7153
7154 for (i = 0; i < Screen_mco; ++i)
7155 {
7156 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7157 return TRUE;
7158 if (u8cc[i] == 0)
7159 break;
7160 }
7161 return FALSE;
7162}
7163#endif
7164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165/*
7166 * Put string '*text' on the screen at position 'row' and 'col', with
7167 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7168 * Note: only outputs within one row, message is truncated at screen boundary!
7169 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7170 */
7171 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007172screen_puts(
7173 char_u *text,
7174 int row,
7175 int col,
7176 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 screen_puts_len(text, -1, row, col, attr);
7179}
7180
7181/*
7182 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7183 * a NUL.
7184 */
7185 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007186screen_puts_len(
7187 char_u *text,
7188 int textlen,
7189 int row,
7190 int col,
7191 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 unsigned off;
7194 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007195 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int c;
7197#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007198 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 int mbyte_blen = 1;
7200 int mbyte_cells = 1;
7201 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007202 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 int clear_next_cell = FALSE;
7204# ifdef FEAT_ARABIC
7205 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007206 int pc, nc, nc1;
7207 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208# endif
7209#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007210#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7211 int force_redraw_this;
7212 int force_redraw_next = FALSE;
7213#endif
7214 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7217 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007218 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaarc236c162008-07-13 17:41:49 +00007220#ifdef FEAT_MBYTE
7221 /* When drawing over the right halve of a double-wide char clear out the
7222 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007223 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007224# ifdef FEAT_GUI
7225 && !gui.in_use
7226# endif
7227 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007228 {
7229 ScreenLines[off - 1] = ' ';
7230 ScreenAttrs[off - 1] = 0;
7231 if (enc_utf8)
7232 {
7233 ScreenLinesUC[off - 1] = 0;
7234 ScreenLinesC[0][off - 1] = 0;
7235 }
7236 /* redraw the previous cell, make it empty */
7237 screen_char(off - 1, row, col - 1);
7238 /* force the cell at "col" to be redrawn */
7239 force_redraw_next = TRUE;
7240 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007241#endif
7242
Bram Moolenaar367329b2007-08-30 11:53:22 +00007243#ifdef FEAT_MBYTE
7244 max_off = LineOffset[row] + screen_Columns;
7245#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007246 while (col < screen_Columns
7247 && (len < 0 || (int)(ptr - text) < len)
7248 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
7250 c = *ptr;
7251#ifdef FEAT_MBYTE
7252 /* check if this is the first byte of a multibyte */
7253 if (has_mbyte)
7254 {
7255 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007256 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007258 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7260 mbyte_cells = 1;
7261 else if (enc_dbcs != 0)
7262 mbyte_cells = mbyte_blen;
7263 else /* enc_utf8 */
7264 {
7265 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007266 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 (int)((text + len) - ptr));
7268 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007269 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007271# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* Non-BMP character: display as ? or fullwidth ?. */
7273 if (u8c >= 0x10000)
7274 {
7275 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7276 if (attr == 0)
7277 attr = hl_attr(HLF_8);
7278 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280# ifdef FEAT_ARABIC
7281 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7282 {
7283 /* Do Arabic shaping. */
7284 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7285 {
7286 /* Past end of string to be displayed. */
7287 nc = NUL;
7288 nc1 = NUL;
7289 }
7290 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007291 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007292 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7293 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 nc1 = pcc[0];
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 pc = prev_c;
7297 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007298 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 }
7300 else
7301 prev_c = u8c;
7302# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007303 if (col + mbyte_cells > screen_Columns)
7304 {
7305 /* Only 1 cell left, but character requires 2 cells:
7306 * display a '>' in the last column to avoid wrapping. */
7307 c = '>';
7308 mbyte_cells = 1;
7309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 }
7311 }
7312#endif
7313
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007314#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7315 force_redraw_this = force_redraw_next;
7316 force_redraw_next = FALSE;
7317#endif
7318
7319 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320#ifdef FEAT_MBYTE
7321 || (mbyte_cells == 2
7322 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7323 || (enc_dbcs == DBCS_JPNU
7324 && c == 0x8e
7325 && ScreenLines2[off] != ptr[1])
7326 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007327 && (ScreenLinesUC[off] !=
7328 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7329 || (ScreenLinesUC[off] != 0
7330 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331#endif
7332 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007333 || exmode_active;
7334
7335 if (need_redraw
7336#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7337 || force_redraw_this
7338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 )
7340 {
7341#if defined(FEAT_GUI) || defined(UNIX)
7342 /* The bold trick makes a single row of pixels appear in the next
7343 * character. When a bold character is removed, the next
7344 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007345 * and for some xterms. */
7346 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347# ifdef FEAT_GUI
7348 gui.in_use
7349# endif
7350# if defined(FEAT_GUI) && defined(UNIX)
7351 ||
7352# endif
7353# ifdef UNIX
7354 term_is_xterm
7355# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007356 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007358 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007360 if (n > HL_ALL)
7361 n = syn_attr2attr(n);
7362 if (n & HL_BOLD)
7363 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365#endif
7366#ifdef FEAT_MBYTE
7367 /* When at the end of the text and overwriting a two-cell
7368 * character with a one-cell character, need to clear the next
7369 * cell. Also when overwriting the left halve of a two-cell char
7370 * with the right halve of a two-cell char. Do this only once
7371 * (mb_off2cells() may return 2 on the right halve). */
7372 if (clear_next_cell)
7373 clear_next_cell = FALSE;
7374 else if (has_mbyte
7375 && (len < 0 ? ptr[mbyte_blen] == NUL
7376 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007377 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007379 && (*mb_off2cells)(off, max_off) == 1
7380 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 clear_next_cell = TRUE;
7382
7383 /* Make sure we never leave a second byte of a double-byte behind,
7384 * it confuses mb_off2cells(). */
7385 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007386 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007388 && (*mb_off2cells)(off, max_off) == 1
7389 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 ScreenLines[off + mbyte_blen] = 0;
7391#endif
7392 ScreenLines[off] = c;
7393 ScreenAttrs[off] = attr;
7394#ifdef FEAT_MBYTE
7395 if (enc_utf8)
7396 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007397 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 ScreenLinesUC[off] = 0;
7399 else
7400 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007401 int i;
7402
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404 for (i = 0; i < Screen_mco; ++i)
7405 {
7406 ScreenLinesC[i][off] = u8cc[i];
7407 if (u8cc[i] == 0)
7408 break;
7409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 if (mbyte_cells == 2)
7412 {
7413 ScreenLines[off + 1] = 0;
7414 ScreenAttrs[off + 1] = attr;
7415 }
7416 screen_char(off, row, col);
7417 }
7418 else if (mbyte_cells == 2)
7419 {
7420 ScreenLines[off + 1] = ptr[1];
7421 ScreenAttrs[off + 1] = attr;
7422 screen_char_2(off, row, col);
7423 }
7424 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7425 {
7426 ScreenLines2[off] = ptr[1];
7427 screen_char(off, row, col);
7428 }
7429 else
7430#endif
7431 screen_char(off, row, col);
7432 }
7433#ifdef FEAT_MBYTE
7434 if (has_mbyte)
7435 {
7436 off += mbyte_cells;
7437 col += mbyte_cells;
7438 ptr += mbyte_blen;
7439 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007440 {
7441 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007443 len = -1;
7444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446 else
7447#endif
7448 {
7449 ++off;
7450 ++col;
7451 ++ptr;
7452 }
7453 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007454
7455#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7456 /* If we detected the next character needs to be redrawn, but the text
7457 * doesn't extend up to there, update the character here. */
7458 if (force_redraw_next && col < screen_Columns)
7459 {
7460# ifdef FEAT_MBYTE
7461 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7462 screen_char_2(off, row, col);
7463 else
7464# endif
7465 screen_char(off, row, col);
7466 }
7467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468}
7469
7470#ifdef FEAT_SEARCH_EXTRA
7471/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007472 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 */
7474 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007475start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476{
7477 if (p_hls && !no_hlsearch)
7478 {
7479 last_pat_prog(&search_hl.rm);
7480 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007481# ifdef FEAT_RELTIME
7482 /* Set the time limit to 'redrawtime'. */
7483 profile_setlimit(p_rdt, &search_hl.tm);
7484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486}
7487
7488/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007489 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 */
7491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007492end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 if (search_hl.rm.regprog != NULL)
7495 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007496 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 search_hl.rm.regprog = NULL;
7498 }
7499}
7500
7501/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007502 * Init for calling prepare_search_hl().
7503 */
7504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007505init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007506{
7507 matchitem_T *cur;
7508
7509 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7510 * match */
7511 cur = wp->w_match_head;
7512 while (cur != NULL)
7513 {
7514 cur->hl.rm = cur->match;
7515 if (cur->hlg_id == 0)
7516 cur->hl.attr = 0;
7517 else
7518 cur->hl.attr = syn_id2attr(cur->hlg_id);
7519 cur->hl.buf = wp->w_buffer;
7520 cur->hl.lnum = 0;
7521 cur->hl.first_lnum = 0;
7522# ifdef FEAT_RELTIME
7523 /* Set the time limit to 'redrawtime'. */
7524 profile_setlimit(p_rdt, &(cur->hl.tm));
7525# endif
7526 cur = cur->next;
7527 }
7528 search_hl.buf = wp->w_buffer;
7529 search_hl.lnum = 0;
7530 search_hl.first_lnum = 0;
7531 /* time limit is set at the toplevel, for all windows */
7532}
7533
7534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 * Advance to the match in window "wp" line "lnum" or past it.
7536 */
7537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007538prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007540 matchitem_T *cur; /* points to the match list */
7541 match_T *shl; /* points to search_hl or a match */
7542 int shl_flag; /* flag to indicate whether search_hl
7543 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007544 int pos_inprogress; /* marks that position match search is
7545 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 int n;
7547
7548 /*
7549 * When using a multi-line pattern, start searching at the top
7550 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007551 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007553 cur = wp->w_match_head;
7554 shl_flag = FALSE;
7555 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007557 if (shl_flag == FALSE)
7558 {
7559 shl = &search_hl;
7560 shl_flag = TRUE;
7561 }
7562 else
7563 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (shl->rm.regprog != NULL
7565 && shl->lnum == 0
7566 && re_multiline(shl->rm.regprog))
7567 {
7568 if (shl->first_lnum == 0)
7569 {
7570# ifdef FEAT_FOLDING
7571 for (shl->first_lnum = lnum;
7572 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7573 if (hasFoldingWin(wp, shl->first_lnum - 1,
7574 NULL, NULL, TRUE, NULL))
7575 break;
7576# else
7577 shl->first_lnum = wp->w_topline;
7578# endif
7579 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007580 if (cur != NULL)
7581 cur->pos.cur = 0;
7582 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007584 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7585 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007587 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7588 pos_inprogress = cur == NULL || cur->pos.cur == 0
7589 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 if (shl->lnum != 0)
7591 {
7592 shl->first_lnum = shl->lnum
7593 + shl->rm.endpos[0].lnum
7594 - shl->rm.startpos[0].lnum;
7595 n = shl->rm.endpos[0].col;
7596 }
7597 else
7598 {
7599 ++shl->first_lnum;
7600 n = 0;
7601 }
7602 }
7603 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007604 if (shl != &search_hl && cur != NULL)
7605 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607}
7608
7609/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007610 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 * Uses shl->buf.
7612 * Sets shl->lnum and shl->rm contents.
7613 * Note: Assumes a previous match is always before "lnum", unless
7614 * shl->lnum is zero.
7615 * Careful: Any pointers for buffer lines will become invalid.
7616 */
7617 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007618next_search_hl(
7619 win_T *win,
7620 match_T *shl, /* points to search_hl or a match */
7621 linenr_T lnum,
7622 colnr_T mincol, /* minimal column for a match */
7623 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625 linenr_T l;
7626 colnr_T matchcol;
7627 long nmatched;
7628
7629 if (shl->lnum != 0)
7630 {
7631 /* Check for three situations:
7632 * 1. If the "lnum" is below a previous match, start a new search.
7633 * 2. If the previous match includes "mincol", use it.
7634 * 3. Continue after the previous match.
7635 */
7636 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7637 if (lnum > l)
7638 shl->lnum = 0;
7639 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7640 return;
7641 }
7642
7643 /*
7644 * Repeat searching for a match until one is found that includes "mincol"
7645 * or none is found in this line.
7646 */
7647 called_emsg = FALSE;
7648 for (;;)
7649 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007650#ifdef FEAT_RELTIME
7651 /* Stop searching after passing the time limit. */
7652 if (profile_passed_limit(&(shl->tm)))
7653 {
7654 shl->lnum = 0; /* no match found in time */
7655 break;
7656 }
7657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 /* Three situations:
7659 * 1. No useful previous match: search from start of line.
7660 * 2. Not Vi compatible or empty match: continue at next character.
7661 * Break the loop if this is beyond the end of the line.
7662 * 3. Vi compatible searching: continue at end of previous match.
7663 */
7664 if (shl->lnum == 0)
7665 matchcol = 0;
7666 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7667 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007668 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007670 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007671
7672 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007673 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007674 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007676 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 shl->lnum = 0;
7678 break;
7679 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007680#ifdef FEAT_MBYTE
7681 if (has_mbyte)
7682 matchcol += mb_ptr2len(ml);
7683 else
7684#endif
7685 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 else
7688 matchcol = shl->rm.endpos[0].col;
7689
7690 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007691 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007693 /* Remember whether shl->rm is using a copy of the regprog in
7694 * cur->match. */
7695 int regprog_is_copy = (shl != &search_hl && cur != NULL
7696 && shl == &cur->hl
7697 && cur->match.regprog == cur->hl.rm.regprog);
7698
Bram Moolenaarb3414592014-06-17 17:48:32 +02007699 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7700 matchcol,
7701#ifdef FEAT_RELTIME
7702 &(shl->tm)
7703#else
7704 NULL
7705#endif
7706 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007707 /* Copy the regprog, in case it got freed and recompiled. */
7708 if (regprog_is_copy)
7709 cur->match.regprog = cur->hl.rm.regprog;
7710
Bram Moolenaarb3414592014-06-17 17:48:32 +02007711 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007712 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007713 /* Error while handling regexp: stop using this regexp. */
7714 if (shl == &search_hl)
7715 {
7716 /* don't free regprog in the match list, it's a copy */
7717 vim_regfree(shl->rm.regprog);
7718 SET_NO_HLSEARCH(TRUE);
7719 }
7720 shl->rm.regprog = NULL;
7721 shl->lnum = 0;
7722 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7723 message */
7724 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007725 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007726 }
7727 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007728 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007729 else
7730 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 if (nmatched == 0)
7732 {
7733 shl->lnum = 0; /* no match found */
7734 break;
7735 }
7736 if (shl->rm.startpos[0].lnum > 0
7737 || shl->rm.startpos[0].col >= mincol
7738 || nmatched > 1
7739 || shl->rm.endpos[0].col > mincol)
7740 {
7741 shl->lnum += shl->rm.startpos[0].lnum;
7742 break; /* useful match found */
7743 }
7744 }
7745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaarb3414592014-06-17 17:48:32 +02007747 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007748next_search_hl_pos(
7749 match_T *shl, /* points to a match */
7750 linenr_T lnum,
7751 posmatch_T *posmatch, /* match positions */
7752 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007753{
7754 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007755 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007756
7757 shl->lnum = 0;
7758 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7759 {
7760 if (posmatch->pos[i].lnum == 0)
7761 break;
7762 if (posmatch->pos[i].col < mincol)
7763 continue;
7764 if (posmatch->pos[i].lnum == lnum)
7765 {
7766 if (shl->lnum == lnum)
7767 {
7768 /* partially sort positions by column numbers
7769 * on the same line */
7770 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7771 {
7772 llpos_T tmp = posmatch->pos[i];
7773
7774 posmatch->pos[i] = posmatch->pos[bot];
7775 posmatch->pos[bot] = tmp;
7776 }
7777 }
7778 else
7779 {
7780 bot = i;
7781 shl->lnum = lnum;
7782 }
7783 }
7784 }
7785 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007786 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007787 {
7788 colnr_T start = posmatch->pos[bot].col == 0
7789 ? 0 : posmatch->pos[bot].col - 1;
7790 colnr_T end = posmatch->pos[bot].col == 0
7791 ? MAXCOL : start + posmatch->pos[bot].len;
7792
7793 shl->rm.startpos[0].lnum = 0;
7794 shl->rm.startpos[0].col = start;
7795 shl->rm.endpos[0].lnum = 0;
7796 shl->rm.endpos[0].col = end;
7797 posmatch->cur = bot + 1;
7798 return TRUE;
7799 }
7800 return FALSE;
7801}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007802#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007805screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 attrentry_T *aep = NULL;
7808
7809 screen_attr = attr;
7810 if (full_screen
7811#ifdef WIN3264
7812 && termcap_active
7813#endif
7814 )
7815 {
7816#ifdef FEAT_GUI
7817 if (gui.in_use)
7818 {
7819 char buf[20];
7820
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007821 /* The GUI handles this internally. */
7822 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 OUT_STR(buf);
7824 }
7825 else
7826#endif
7827 {
7828 if (attr > HL_ALL) /* special HL attr. */
7829 {
7830 if (t_colors > 1)
7831 aep = syn_cterm_attr2entry(attr);
7832 else
7833 aep = syn_term_attr2entry(attr);
7834 if (aep == NULL) /* did ":syntax clear" */
7835 attr = 0;
7836 else
7837 attr = aep->ae_attr;
7838 }
7839 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7840 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007841 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7842 && cterm_normal_fg_bold)
7843 /* If the Normal FG color has BOLD attribute and the new HL
7844 * has a FG color defined, clear BOLD. */
7845 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7847 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007848 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7849 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 out_str(T_US);
7851 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7852 out_str(T_CZH);
7853 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7854 out_str(T_MR);
7855
7856 /*
7857 * Output the color or start string after bold etc., in case the
7858 * bold etc. override the color setting.
7859 */
7860 if (aep != NULL)
7861 {
7862 if (t_colors > 1)
7863 {
7864 if (aep->ae_u.cterm.fg_color)
7865 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7866 if (aep->ae_u.cterm.bg_color)
7867 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7868 }
7869 else
7870 {
7871 if (aep->ae_u.term.start != NULL)
7872 out_str(aep->ae_u.term.start);
7873 }
7874 }
7875 }
7876 }
7877}
7878
7879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 int do_ME = FALSE; /* output T_ME code */
7883
7884 if (screen_attr != 0
7885#ifdef WIN3264
7886 && termcap_active
7887#endif
7888 )
7889 {
7890#ifdef FEAT_GUI
7891 if (gui.in_use)
7892 {
7893 char buf[20];
7894
7895 /* use internal GUI code */
7896 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7897 OUT_STR(buf);
7898 }
7899 else
7900#endif
7901 {
7902 if (screen_attr > HL_ALL) /* special HL attr. */
7903 {
7904 attrentry_T *aep;
7905
7906 if (t_colors > 1)
7907 {
7908 /*
7909 * Assume that t_me restores the original colors!
7910 */
7911 aep = syn_cterm_attr2entry(screen_attr);
7912 if (aep != NULL && (aep->ae_u.cterm.fg_color
7913 || aep->ae_u.cterm.bg_color))
7914 do_ME = TRUE;
7915 }
7916 else
7917 {
7918 aep = syn_term_attr2entry(screen_attr);
7919 if (aep != NULL && aep->ae_u.term.stop != NULL)
7920 {
7921 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7922 do_ME = TRUE;
7923 else
7924 out_str(aep->ae_u.term.stop);
7925 }
7926 }
7927 if (aep == NULL) /* did ":syntax clear" */
7928 screen_attr = 0;
7929 else
7930 screen_attr = aep->ae_attr;
7931 }
7932
7933 /*
7934 * Often all ending-codes are equal to T_ME. Avoid outputting the
7935 * same sequence several times.
7936 */
7937 if (screen_attr & HL_STANDOUT)
7938 {
7939 if (STRCMP(T_SE, T_ME) == 0)
7940 do_ME = TRUE;
7941 else
7942 out_str(T_SE);
7943 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007944 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
7946 if (STRCMP(T_UE, T_ME) == 0)
7947 do_ME = TRUE;
7948 else
7949 out_str(T_UE);
7950 }
7951 if (screen_attr & HL_ITALIC)
7952 {
7953 if (STRCMP(T_CZR, T_ME) == 0)
7954 do_ME = TRUE;
7955 else
7956 out_str(T_CZR);
7957 }
7958 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7959 out_str(T_ME);
7960
7961 if (t_colors > 1)
7962 {
7963 /* set Normal cterm colors */
7964 if (cterm_normal_fg_color != 0)
7965 term_fg_color(cterm_normal_fg_color - 1);
7966 if (cterm_normal_bg_color != 0)
7967 term_bg_color(cterm_normal_bg_color - 1);
7968 if (cterm_normal_fg_bold)
7969 out_str(T_MD);
7970 }
7971 }
7972 }
7973 screen_attr = 0;
7974}
7975
7976/*
7977 * Reset the colors for a cterm. Used when leaving Vim.
7978 * The machine specific code may override this again.
7979 */
7980 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007981reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983 if (t_colors > 1)
7984 {
7985 /* set Normal cterm colors */
7986 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7987 {
7988 out_str(T_OP);
7989 screen_attr = -1;
7990 }
7991 if (cterm_normal_fg_bold)
7992 {
7993 out_str(T_ME);
7994 screen_attr = -1;
7995 }
7996 }
7997}
7998
7999/*
8000 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8001 * using the attributes from ScreenAttrs["off"].
8002 */
8003 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008004screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 int attr;
8007
8008 /* Check for illegal values, just in case (could happen just after
8009 * resizing). */
8010 if (row >= screen_Rows || col >= screen_Columns)
8011 return;
8012
Bram Moolenaar494838a2015-02-10 19:20:37 +01008013 /* Outputting a character in the last cell on the screen may scroll the
8014 * screen up. Only do it when the "xn" termcap property is set, otherwise
8015 * mark the character invalid (update it when scrolled up). */
8016 if (*T_XN == NUL
8017 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018#ifdef FEAT_RIGHTLEFT
8019 /* account for first command-line character in rightleft mode */
8020 && !cmdmsg_rl
8021#endif
8022 )
8023 {
8024 ScreenAttrs[off] = (sattr_T)-1;
8025 return;
8026 }
8027
8028 /*
8029 * Stop highlighting first, so it's easier to move the cursor.
8030 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008031#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (screen_char_attr != 0)
8033 attr = screen_char_attr;
8034 else
8035#endif
8036 attr = ScreenAttrs[off];
8037 if (screen_attr != attr)
8038 screen_stop_highlight();
8039
8040 windgoto(row, col);
8041
8042 if (screen_attr != attr)
8043 screen_start_highlight(attr);
8044
8045#ifdef FEAT_MBYTE
8046 if (enc_utf8 && ScreenLinesUC[off] != 0)
8047 {
8048 char_u buf[MB_MAXBYTES + 1];
8049
8050 /* Convert UTF-8 character to bytes and write it. */
8051
8052 buf[utfc_char2bytes(off, buf)] = NUL;
8053
8054 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008055 if (utf_ambiguous_width(ScreenLinesUC[off]))
8056 screen_cur_col = 9999;
8057 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 ++screen_cur_col;
8059 }
8060 else
8061#endif
8062 {
8063#ifdef FEAT_MBYTE
8064 out_flush_check();
8065#endif
8066 out_char(ScreenLines[off]);
8067#ifdef FEAT_MBYTE
8068 /* double-byte character in single-width cell */
8069 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8070 out_char(ScreenLines2[off]);
8071#endif
8072 }
8073
8074 screen_cur_col++;
8075}
8076
8077#ifdef FEAT_MBYTE
8078
8079/*
8080 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8081 * on the screen at position 'row' and 'col'.
8082 * The attributes of the first byte is used for all. This is required to
8083 * output the two bytes of a double-byte character with nothing in between.
8084 */
8085 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008086screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 /* Check for illegal values (could be wrong when screen was resized). */
8089 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8090 return;
8091
8092 /* Outputting the last character on the screen may scrollup the screen.
8093 * Don't to it! Mark the character invalid (update it when scrolled up) */
8094 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8095 {
8096 ScreenAttrs[off] = (sattr_T)-1;
8097 return;
8098 }
8099
8100 /* Output the first byte normally (positions the cursor), then write the
8101 * second byte directly. */
8102 screen_char(off, row, col);
8103 out_char(ScreenLines[off + 1]);
8104 ++screen_cur_col;
8105}
8106#endif
8107
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008108#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109/*
8110 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8111 * This uses the contents of ScreenLines[] and doesn't change it.
8112 */
8113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008114screen_draw_rectangle(
8115 int row,
8116 int col,
8117 int height,
8118 int width,
8119 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 int r, c;
8122 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008123#ifdef FEAT_MBYTE
8124 int max_off;
8125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008127 /* Can't use ScreenLines unless initialized */
8128 if (ScreenLines == NULL)
8129 return;
8130
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (invert)
8132 screen_char_attr = HL_INVERSE;
8133 for (r = row; r < row + height; ++r)
8134 {
8135 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008136#ifdef FEAT_MBYTE
8137 max_off = off + screen_Columns;
8138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 for (c = col; c < col + width; ++c)
8140 {
8141#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008142 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
8144 screen_char_2(off + c, r, c);
8145 ++c;
8146 }
8147 else
8148#endif
8149 {
8150 screen_char(off + c, r, c);
8151#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008152 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 ++c;
8154#endif
8155 }
8156 }
8157 }
8158 screen_char_attr = 0;
8159}
8160#endif
8161
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008162#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163/*
8164 * Redraw the characters for a vertically split window.
8165 */
8166 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008167redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 int col;
8170 int width;
8171
8172# ifdef FEAT_CLIPBOARD
8173 clip_may_clear_selection(row, end - 1);
8174# endif
8175
8176 if (wp == NULL)
8177 {
8178 col = 0;
8179 width = Columns;
8180 }
8181 else
8182 {
8183 col = wp->w_wincol;
8184 width = wp->w_width;
8185 }
8186 screen_draw_rectangle(row, col, end - row, width, FALSE);
8187}
8188#endif
8189
8190/*
8191 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8192 * with character 'c1' in first column followed by 'c2' in the other columns.
8193 * Use attributes 'attr'.
8194 */
8195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008196screen_fill(
8197 int start_row,
8198 int end_row,
8199 int start_col,
8200 int end_col,
8201 int c1,
8202 int c2,
8203 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 int row;
8206 int col;
8207 int off;
8208 int end_off;
8209 int did_delete;
8210 int c;
8211 int norm_term;
8212#if defined(FEAT_GUI) || defined(UNIX)
8213 int force_next = FALSE;
8214#endif
8215
8216 if (end_row > screen_Rows) /* safety check */
8217 end_row = screen_Rows;
8218 if (end_col > screen_Columns) /* safety check */
8219 end_col = screen_Columns;
8220 if (ScreenLines == NULL
8221 || start_row >= end_row
8222 || start_col >= end_col) /* nothing to do */
8223 return;
8224
8225 /* it's a "normal" terminal when not in a GUI or cterm */
8226 norm_term = (
8227#ifdef FEAT_GUI
8228 !gui.in_use &&
8229#endif
8230 t_colors <= 1);
8231 for (row = start_row; row < end_row; ++row)
8232 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008233#ifdef FEAT_MBYTE
8234 if (has_mbyte
8235# ifdef FEAT_GUI
8236 && !gui.in_use
8237# endif
8238 )
8239 {
8240 /* When drawing over the right halve of a double-wide char clear
8241 * out the left halve. When drawing over the left halve of a
8242 * double wide-char clear out the right halve. Only needed in a
8243 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008244 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008245 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008246 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008247 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008248 }
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 /*
8251 * Try to use delete-line termcap code, when no attributes or in a
8252 * "normal" terminal, where a bold/italic space is just a
8253 * space.
8254 */
8255 did_delete = FALSE;
8256 if (c2 == ' '
8257 && end_col == Columns
8258 && can_clear(T_CE)
8259 && (attr == 0
8260 || (norm_term
8261 && attr <= HL_ALL
8262 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8263 {
8264 /*
8265 * check if we really need to clear something
8266 */
8267 col = start_col;
8268 if (c1 != ' ') /* don't clear first char */
8269 ++col;
8270
8271 off = LineOffset[row] + col;
8272 end_off = LineOffset[row] + end_col;
8273
8274 /* skip blanks (used often, keep it fast!) */
8275#ifdef FEAT_MBYTE
8276 if (enc_utf8)
8277 while (off < end_off && ScreenLines[off] == ' '
8278 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8279 ++off;
8280 else
8281#endif
8282 while (off < end_off && ScreenLines[off] == ' '
8283 && ScreenAttrs[off] == 0)
8284 ++off;
8285 if (off < end_off) /* something to be cleared */
8286 {
8287 col = off - LineOffset[row];
8288 screen_stop_highlight();
8289 term_windgoto(row, col);/* clear rest of this screen line */
8290 out_str(T_CE);
8291 screen_start(); /* don't know where cursor is now */
8292 col = end_col - col;
8293 while (col--) /* clear chars in ScreenLines */
8294 {
8295 ScreenLines[off] = ' ';
8296#ifdef FEAT_MBYTE
8297 if (enc_utf8)
8298 ScreenLinesUC[off] = 0;
8299#endif
8300 ScreenAttrs[off] = 0;
8301 ++off;
8302 }
8303 }
8304 did_delete = TRUE; /* the chars are cleared now */
8305 }
8306
8307 off = LineOffset[row] + start_col;
8308 c = c1;
8309 for (col = start_col; col < end_col; ++col)
8310 {
8311 if (ScreenLines[off] != c
8312#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008313 || (enc_utf8 && (int)ScreenLinesUC[off]
8314 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315#endif
8316 || ScreenAttrs[off] != attr
8317#if defined(FEAT_GUI) || defined(UNIX)
8318 || force_next
8319#endif
8320 )
8321 {
8322#if defined(FEAT_GUI) || defined(UNIX)
8323 /* The bold trick may make a single row of pixels appear in
8324 * the next character. When a bold character is removed, the
8325 * next character should be redrawn too. This happens for our
8326 * own GUI and for some xterms. */
8327 if (
8328# ifdef FEAT_GUI
8329 gui.in_use
8330# endif
8331# if defined(FEAT_GUI) && defined(UNIX)
8332 ||
8333# endif
8334# ifdef UNIX
8335 term_is_xterm
8336# endif
8337 )
8338 {
8339 if (ScreenLines[off] != ' '
8340 && (ScreenAttrs[off] > HL_ALL
8341 || ScreenAttrs[off] & HL_BOLD))
8342 force_next = TRUE;
8343 else
8344 force_next = FALSE;
8345 }
8346#endif
8347 ScreenLines[off] = c;
8348#ifdef FEAT_MBYTE
8349 if (enc_utf8)
8350 {
8351 if (c >= 0x80)
8352 {
8353 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008354 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356 else
8357 ScreenLinesUC[off] = 0;
8358 }
8359#endif
8360 ScreenAttrs[off] = attr;
8361 if (!did_delete || c != ' ')
8362 screen_char(off, row, col);
8363 }
8364 ++off;
8365 if (col == start_col)
8366 {
8367 if (did_delete)
8368 break;
8369 c = c2;
8370 }
8371 }
8372 if (end_col == Columns)
8373 LineWraps[row] = FALSE;
8374 if (row == Rows - 1) /* overwritten the command line */
8375 {
8376 redraw_cmdline = TRUE;
8377 if (c1 == ' ' && c2 == ' ')
8378 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008379 if (start_col == 0)
8380 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382 }
8383}
8384
8385/*
8386 * Check if there should be a delay. Used before clearing or redrawing the
8387 * screen or the command line.
8388 */
8389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008390check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391{
8392 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8393 && !did_wait_return
8394 && emsg_silent == 0)
8395 {
8396 out_flush();
8397 ui_delay(1000L, TRUE);
8398 emsg_on_display = FALSE;
8399 if (check_msg_scroll)
8400 msg_scroll = FALSE;
8401 }
8402}
8403
8404/*
8405 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008406 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * Returns TRUE if there is a valid screen to write to.
8408 * Returns FALSE when starting up and screen not initialized yet.
8409 */
8410 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008411screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008413 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 return (ScreenLines != NULL);
8415}
8416
8417/*
8418 * Resize the shell to Rows and Columns.
8419 * Allocate ScreenLines[] and associated items.
8420 *
8421 * There may be some time between setting Rows and Columns and (re)allocating
8422 * ScreenLines[]. This happens when starting up and when (manually) changing
8423 * the shell size. Always use screen_Rows and screen_Columns to access items
8424 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8425 * final size of the shell is needed.
8426 */
8427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008428screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 int new_row, old_row;
8431#ifdef FEAT_GUI
8432 int old_Rows;
8433#endif
8434 win_T *wp;
8435 int outofmem = FALSE;
8436 int len;
8437 schar_T *new_ScreenLines;
8438#ifdef FEAT_MBYTE
8439 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008440 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008442 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443#endif
8444 sattr_T *new_ScreenAttrs;
8445 unsigned *new_LineOffset;
8446 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008447#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008448 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008449 tabpage_T *tp;
8450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008452 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008453#ifdef FEAT_AUTOCMD
8454 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008456retry:
8457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 /*
8459 * Allocation of the screen buffers is done only when the size changes and
8460 * when Rows and Columns have been set and we have started doing full
8461 * screen stuff.
8462 */
8463 if ((ScreenLines != NULL
8464 && Rows == screen_Rows
8465 && Columns == screen_Columns
8466#ifdef FEAT_MBYTE
8467 && enc_utf8 == (ScreenLinesUC != NULL)
8468 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008469 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#endif
8471 )
8472 || Rows == 0
8473 || Columns == 0
8474 || (!full_screen && ScreenLines == NULL))
8475 return;
8476
8477 /*
8478 * It's possible that we produce an out-of-memory message below, which
8479 * will cause this function to be called again. To break the loop, just
8480 * return here.
8481 */
8482 if (entered)
8483 return;
8484 entered = TRUE;
8485
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008486 /*
8487 * Note that the window sizes are updated before reallocating the arrays,
8488 * thus we must not redraw here!
8489 */
8490 ++RedrawingDisabled;
8491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 win_new_shellsize(); /* fit the windows in the new sized shell */
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 comp_col(); /* recompute columns for shown command and ruler */
8495
8496 /*
8497 * We're changing the size of the screen.
8498 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8499 * - Move lines from the old arrays into the new arrays, clear extra
8500 * lines (unless the screen is going to be cleared).
8501 * - Free the old arrays.
8502 *
8503 * If anything fails, make ScreenLines NULL, so we don't do anything!
8504 * Continuing with the old ScreenLines may result in a crash, because the
8505 * size is wrong.
8506 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008507 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008509#ifdef FEAT_AUTOCMD
8510 if (aucmd_win != NULL)
8511 win_free_lsize(aucmd_win);
8512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
8514 new_ScreenLines = (schar_T *)lalloc((long_u)(
8515 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8516#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008517 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (enc_utf8)
8519 {
8520 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8521 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008522 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008523 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8525 }
8526 if (enc_dbcs == DBCS_JPNU)
8527 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8528 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8529#endif
8530 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8531 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8532 new_LineOffset = (unsigned *)lalloc((long_u)(
8533 Rows * sizeof(unsigned)), FALSE);
8534 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008535#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008536 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008539 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 if (win_alloc_lines(wp) == FAIL)
8542 {
8543 outofmem = TRUE;
8544#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008545 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#endif
8547 }
8548 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008549#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008550 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8551 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008552 outofmem = TRUE;
8553#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008554#ifdef FEAT_WINDOWS
8555give_up:
8556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008558#ifdef FEAT_MBYTE
8559 for (i = 0; i < p_mco; ++i)
8560 if (new_ScreenLinesC[i] == NULL)
8561 break;
8562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 if (new_ScreenLines == NULL
8564#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008565 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8567#endif
8568 || new_ScreenAttrs == NULL
8569 || new_LineOffset == NULL
8570 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008571#ifdef FEAT_WINDOWS
8572 || new_TabPageIdxs == NULL
8573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 || outofmem)
8575 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008576 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008577 {
8578 /* guess the size */
8579 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8580
8581 /* Remember we did this to avoid getting outofmem messages over
8582 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008583 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 vim_free(new_ScreenLines);
8586 new_ScreenLines = NULL;
8587#ifdef FEAT_MBYTE
8588 vim_free(new_ScreenLinesUC);
8589 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590 for (i = 0; i < p_mco; ++i)
8591 {
8592 vim_free(new_ScreenLinesC[i]);
8593 new_ScreenLinesC[i] = NULL;
8594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 vim_free(new_ScreenLines2);
8596 new_ScreenLines2 = NULL;
8597#endif
8598 vim_free(new_ScreenAttrs);
8599 new_ScreenAttrs = NULL;
8600 vim_free(new_LineOffset);
8601 new_LineOffset = NULL;
8602 vim_free(new_LineWraps);
8603 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008604#ifdef FEAT_WINDOWS
8605 vim_free(new_TabPageIdxs);
8606 new_TabPageIdxs = NULL;
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609 else
8610 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008611 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 for (new_row = 0; new_row < Rows; ++new_row)
8614 {
8615 new_LineOffset[new_row] = new_row * Columns;
8616 new_LineWraps[new_row] = FALSE;
8617
8618 /*
8619 * If the screen is not going to be cleared, copy as much as
8620 * possible from the old screen to the new one and clear the rest
8621 * (used when resizing the window at the "--more--" prompt or when
8622 * executing an external command, for the GUI).
8623 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008624 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {
8626 (void)vim_memset(new_ScreenLines + new_row * Columns,
8627 ' ', (size_t)Columns * sizeof(schar_T));
8628#ifdef FEAT_MBYTE
8629 if (enc_utf8)
8630 {
8631 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8632 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008633 for (i = 0; i < p_mco; ++i)
8634 (void)vim_memset(new_ScreenLinesC[i]
8635 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 0, (size_t)Columns * sizeof(u8char_T));
8637 }
8638 if (enc_dbcs == DBCS_JPNU)
8639 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8640 0, (size_t)Columns * sizeof(schar_T));
8641#endif
8642 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8643 0, (size_t)Columns * sizeof(sattr_T));
8644 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008645 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 {
8647 if (screen_Columns < Columns)
8648 len = screen_Columns;
8649 else
8650 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008651#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008652 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008653 * may be invalid now. Also when p_mco changes. */
8654 if (!(enc_utf8 && ScreenLinesUC == NULL)
8655 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008656#endif
8657 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8658 ScreenLines + LineOffset[old_row],
8659 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008661 if (enc_utf8 && ScreenLinesUC != NULL
8662 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {
8664 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8665 ScreenLinesUC + LineOffset[old_row],
8666 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008667 for (i = 0; i < p_mco; ++i)
8668 mch_memmove(new_ScreenLinesC[i]
8669 + new_LineOffset[new_row],
8670 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 (size_t)len * sizeof(u8char_T));
8672 }
8673 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8674 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8675 ScreenLines2 + LineOffset[old_row],
8676 (size_t)len * sizeof(schar_T));
8677#endif
8678 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8679 ScreenAttrs + LineOffset[old_row],
8680 (size_t)len * sizeof(sattr_T));
8681 }
8682 }
8683 }
8684 /* Use the last line of the screen for the current line. */
8685 current_ScreenLine = new_ScreenLines + Rows * Columns;
8686 }
8687
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008688 free_screenlines();
8689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 ScreenLines = new_ScreenLines;
8691#ifdef FEAT_MBYTE
8692 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008693 for (i = 0; i < p_mco; ++i)
8694 ScreenLinesC[i] = new_ScreenLinesC[i];
8695 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 ScreenLines2 = new_ScreenLines2;
8697#endif
8698 ScreenAttrs = new_ScreenAttrs;
8699 LineOffset = new_LineOffset;
8700 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008701#ifdef FEAT_WINDOWS
8702 TabPageIdxs = new_TabPageIdxs;
8703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704
8705 /* It's important that screen_Rows and screen_Columns reflect the actual
8706 * size of ScreenLines[]. Set them before calling anything. */
8707#ifdef FEAT_GUI
8708 old_Rows = screen_Rows;
8709#endif
8710 screen_Rows = Rows;
8711 screen_Columns = Columns;
8712
8713 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008714 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 screenclear2();
8716
8717#ifdef FEAT_GUI
8718 else if (gui.in_use
8719 && !gui.starting
8720 && ScreenLines != NULL
8721 && old_Rows != Rows)
8722 {
8723 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8724 /*
8725 * Adjust the position of the cursor, for when executing an external
8726 * command.
8727 */
8728 if (msg_row >= Rows) /* Rows got smaller */
8729 msg_row = Rows - 1; /* put cursor at last row */
8730 else if (Rows > old_Rows) /* Rows got bigger */
8731 msg_row += Rows - old_Rows; /* put cursor in same place */
8732 if (msg_col >= Columns) /* Columns got smaller */
8733 msg_col = Columns - 1; /* put cursor at last column */
8734 }
8735#endif
8736
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008738 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008739
8740#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008741 /*
8742 * Do not apply autocommands more than 3 times to avoid an endless loop
8743 * in case applying autocommands always changes Rows or Columns.
8744 */
8745 if (starting == 0 && ++retry_count <= 3)
8746 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008747 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008748 /* In rare cases, autocommands may have altered Rows or Columns,
8749 * jump back to check if we need to allocate the screen again. */
8750 goto retry;
8751 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
8755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008756free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008757{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008758#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008759 int i;
8760
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008761 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008762 for (i = 0; i < Screen_mco; ++i)
8763 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008764 vim_free(ScreenLines2);
8765#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008766 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008767 vim_free(ScreenAttrs);
8768 vim_free(LineOffset);
8769 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008770#ifdef FEAT_WINDOWS
8771 vim_free(TabPageIdxs);
8772#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008773}
8774
8775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008776screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 check_for_delay(FALSE);
8779 screenalloc(FALSE); /* allocate screen buffers if size changed */
8780 screenclear2(); /* clear the screen */
8781}
8782
8783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008784screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785{
8786 int i;
8787
8788 if (starting == NO_SCREEN || ScreenLines == NULL
8789#ifdef FEAT_GUI
8790 || (gui.in_use && gui.starting)
8791#endif
8792 )
8793 return;
8794
8795#ifdef FEAT_GUI
8796 if (!gui.in_use)
8797#endif
8798 screen_attr = -1; /* force setting the Normal colors */
8799 screen_stop_highlight(); /* don't want highlighting here */
8800
8801#ifdef FEAT_CLIPBOARD
8802 /* disable selection without redrawing it */
8803 clip_scroll_selection(9999);
8804#endif
8805
8806 /* blank out ScreenLines */
8807 for (i = 0; i < Rows; ++i)
8808 {
8809 lineclear(LineOffset[i], (int)Columns);
8810 LineWraps[i] = FALSE;
8811 }
8812
8813 if (can_clear(T_CL))
8814 {
8815 out_str(T_CL); /* clear the display */
8816 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008817 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 }
8819 else
8820 {
8821 /* can't clear the screen, mark all chars with invalid attributes */
8822 for (i = 0; i < Rows; ++i)
8823 lineinvalid(LineOffset[i], (int)Columns);
8824 clear_cmdline = TRUE;
8825 }
8826
8827 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8828
8829 win_rest_invalid(firstwin);
8830 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008831#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008832 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (must_redraw == CLEAR) /* no need to clear again */
8835 must_redraw = NOT_VALID;
8836 compute_cmdrow();
8837 msg_row = cmdline_row; /* put cursor on last line for messages */
8838 msg_col = 0;
8839 screen_start(); /* don't know where cursor is now */
8840 msg_scrolled = 0; /* can't scroll back */
8841 msg_didany = FALSE;
8842 msg_didout = FALSE;
8843}
8844
8845/*
8846 * Clear one line in ScreenLines.
8847 */
8848 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008849lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8852#ifdef FEAT_MBYTE
8853 if (enc_utf8)
8854 (void)vim_memset(ScreenLinesUC + off, 0,
8855 (size_t)width * sizeof(u8char_T));
8856#endif
8857 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8858}
8859
8860/*
8861 * Mark one line in ScreenLines invalid by setting the attributes to an
8862 * invalid value.
8863 */
8864 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008865lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8868}
8869
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008870#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871/*
8872 * Copy part of a Screenline for vertically split window "wp".
8873 */
8874 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008875linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 unsigned off_to = LineOffset[to] + wp->w_wincol;
8878 unsigned off_from = LineOffset[from] + wp->w_wincol;
8879
8880 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8881 wp->w_width * sizeof(schar_T));
8882# ifdef FEAT_MBYTE
8883 if (enc_utf8)
8884 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008885 int i;
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8888 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008889 for (i = 0; i < p_mco; ++i)
8890 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8891 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 if (enc_dbcs == DBCS_JPNU)
8894 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8895 wp->w_width * sizeof(schar_T));
8896# endif
8897 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8898 wp->w_width * sizeof(sattr_T));
8899}
8900#endif
8901
8902/*
8903 * Return TRUE if clearing with term string "p" would work.
8904 * It can't work when the string is empty or it won't set the right background.
8905 */
8906 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008907can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 return (*p != NUL && (t_colors <= 1
8910#ifdef FEAT_GUI
8911 || gui.in_use
8912#endif
8913 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8914}
8915
8916/*
8917 * Reset cursor position. Use whenever cursor was moved because of outputting
8918 * something directly to the screen (shell commands) or a terminal control
8919 * code.
8920 */
8921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008922screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 screen_cur_row = screen_cur_col = 9999;
8925}
8926
8927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 * Move the cursor to position "row","col" in the screen.
8929 * This tries to find the most efficient way to move, minimizing the number of
8930 * characters sent to the terminal.
8931 */
8932 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008933windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008935 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 int i;
8937 int plan;
8938 int cost;
8939 int wouldbe_col;
8940 int noinvcurs;
8941 char_u *bs;
8942 int goto_cost;
8943 int attr;
8944
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008945#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8947
8948#define PLAN_LE 1
8949#define PLAN_CR 2
8950#define PLAN_NL 3
8951#define PLAN_WRITE 4
8952 /* Can't use ScreenLines unless initialized */
8953 if (ScreenLines == NULL)
8954 return;
8955
8956 if (col != screen_cur_col || row != screen_cur_row)
8957 {
8958 /* Check for valid position. */
8959 if (row < 0) /* window without text lines? */
8960 row = 0;
8961 if (row >= screen_Rows)
8962 row = screen_Rows - 1;
8963 if (col >= screen_Columns)
8964 col = screen_Columns - 1;
8965
8966 /* check if no cursor movement is allowed in highlight mode */
8967 if (screen_attr && *T_MS == NUL)
8968 noinvcurs = HIGHL_COST;
8969 else
8970 noinvcurs = 0;
8971 goto_cost = GOTO_COST + noinvcurs;
8972
8973 /*
8974 * Plan how to do the positioning:
8975 * 1. Use CR to move it to column 0, same row.
8976 * 2. Use T_LE to move it a few columns to the left.
8977 * 3. Use NL to move a few lines down, column 0.
8978 * 4. Move a few columns to the right with T_ND or by writing chars.
8979 *
8980 * Don't do this if the cursor went beyond the last column, the cursor
8981 * position is unknown then (some terminals wrap, some don't )
8982 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008983 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 * characters to move the cursor to the right.
8985 */
8986 if (row >= screen_cur_row && screen_cur_col < Columns)
8987 {
8988 /*
8989 * If the cursor is in the same row, bigger col, we can use CR
8990 * or T_LE.
8991 */
8992 bs = NULL; /* init for GCC */
8993 attr = screen_attr;
8994 if (row == screen_cur_row && col < screen_cur_col)
8995 {
8996 /* "le" is preferred over "bc", because "bc" is obsolete */
8997 if (*T_LE)
8998 bs = T_LE; /* "cursor left" */
8999 else
9000 bs = T_BC; /* "backspace character (old) */
9001 if (*bs)
9002 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9003 else
9004 cost = 999;
9005 if (col + 1 < cost) /* using CR is less characters */
9006 {
9007 plan = PLAN_CR;
9008 wouldbe_col = 0;
9009 cost = 1; /* CR is just one character */
9010 }
9011 else
9012 {
9013 plan = PLAN_LE;
9014 wouldbe_col = col;
9015 }
9016 if (noinvcurs) /* will stop highlighting */
9017 {
9018 cost += noinvcurs;
9019 attr = 0;
9020 }
9021 }
9022
9023 /*
9024 * If the cursor is above where we want to be, we can use CR LF.
9025 */
9026 else if (row > screen_cur_row)
9027 {
9028 plan = PLAN_NL;
9029 wouldbe_col = 0;
9030 cost = (row - screen_cur_row) * 2; /* CR LF */
9031 if (noinvcurs) /* will stop highlighting */
9032 {
9033 cost += noinvcurs;
9034 attr = 0;
9035 }
9036 }
9037
9038 /*
9039 * If the cursor is in the same row, smaller col, just use write.
9040 */
9041 else
9042 {
9043 plan = PLAN_WRITE;
9044 wouldbe_col = screen_cur_col;
9045 cost = 0;
9046 }
9047
9048 /*
9049 * Check if any characters that need to be written have the
9050 * correct attributes. Also avoid UTF-8 characters.
9051 */
9052 i = col - wouldbe_col;
9053 if (i > 0)
9054 cost += i;
9055 if (cost < goto_cost && i > 0)
9056 {
9057 /*
9058 * Check if the attributes are correct without additionally
9059 * stopping highlighting.
9060 */
9061 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9062 while (i && *p++ == attr)
9063 --i;
9064 if (i != 0)
9065 {
9066 /*
9067 * Try if it works when highlighting is stopped here.
9068 */
9069 if (*--p == 0)
9070 {
9071 cost += noinvcurs;
9072 while (i && *p++ == 0)
9073 --i;
9074 }
9075 if (i != 0)
9076 cost = 999; /* different attributes, don't do it */
9077 }
9078#ifdef FEAT_MBYTE
9079 if (enc_utf8)
9080 {
9081 /* Don't use an UTF-8 char for positioning, it's slow. */
9082 for (i = wouldbe_col; i < col; ++i)
9083 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9084 {
9085 cost = 999;
9086 break;
9087 }
9088 }
9089#endif
9090 }
9091
9092 /*
9093 * We can do it without term_windgoto()!
9094 */
9095 if (cost < goto_cost)
9096 {
9097 if (plan == PLAN_LE)
9098 {
9099 if (noinvcurs)
9100 screen_stop_highlight();
9101 while (screen_cur_col > col)
9102 {
9103 out_str(bs);
9104 --screen_cur_col;
9105 }
9106 }
9107 else if (plan == PLAN_CR)
9108 {
9109 if (noinvcurs)
9110 screen_stop_highlight();
9111 out_char('\r');
9112 screen_cur_col = 0;
9113 }
9114 else if (plan == PLAN_NL)
9115 {
9116 if (noinvcurs)
9117 screen_stop_highlight();
9118 while (screen_cur_row < row)
9119 {
9120 out_char('\n');
9121 ++screen_cur_row;
9122 }
9123 screen_cur_col = 0;
9124 }
9125
9126 i = col - screen_cur_col;
9127 if (i > 0)
9128 {
9129 /*
9130 * Use cursor-right if it's one character only. Avoids
9131 * removing a line of pixels from the last bold char, when
9132 * using the bold trick in the GUI.
9133 */
9134 if (T_ND[0] != NUL && T_ND[1] == NUL)
9135 {
9136 while (i-- > 0)
9137 out_char(*T_ND);
9138 }
9139 else
9140 {
9141 int off;
9142
9143 off = LineOffset[row] + screen_cur_col;
9144 while (i-- > 0)
9145 {
9146 if (ScreenAttrs[off] != screen_attr)
9147 screen_stop_highlight();
9148#ifdef FEAT_MBYTE
9149 out_flush_check();
9150#endif
9151 out_char(ScreenLines[off]);
9152#ifdef FEAT_MBYTE
9153 if (enc_dbcs == DBCS_JPNU
9154 && ScreenLines[off] == 0x8e)
9155 out_char(ScreenLines2[off]);
9156#endif
9157 ++off;
9158 }
9159 }
9160 }
9161 }
9162 }
9163 else
9164 cost = 999;
9165
9166 if (cost >= goto_cost)
9167 {
9168 if (noinvcurs)
9169 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009170 if (row == screen_cur_row && (col > screen_cur_col)
9171 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 term_cursor_right(col - screen_cur_col);
9173 else
9174 term_windgoto(row, col);
9175 }
9176 screen_cur_row = row;
9177 screen_cur_col = col;
9178 }
9179}
9180
9181/*
9182 * Set cursor to its position in the current window.
9183 */
9184 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009185setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 if (redrawing())
9188 {
9189 validate_cursor();
9190 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9191 W_WINCOL(curwin) + (
9192#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009193 /* With 'rightleft' set and the cursor on a double-wide
9194 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9196# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009197 (has_mbyte
9198 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9199 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200# endif
9201 1)) :
9202#endif
9203 curwin->w_wcol));
9204 }
9205}
9206
9207
9208/*
9209 * insert 'line_count' lines at 'row' in window 'wp'
9210 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9211 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9212 * scrolling.
9213 * Returns FAIL if the lines are not inserted, OK for success.
9214 */
9215 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009216win_ins_lines(
9217 win_T *wp,
9218 int row,
9219 int line_count,
9220 int invalid,
9221 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 int did_delete;
9224 int nextrow;
9225 int lastrow;
9226 int retval;
9227
9228 if (invalid)
9229 wp->w_lines_valid = 0;
9230
9231 if (wp->w_height < 5)
9232 return FAIL;
9233
9234 if (line_count > wp->w_height - row)
9235 line_count = wp->w_height - row;
9236
9237 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9238 if (retval != MAYBE)
9239 return retval;
9240
9241 /*
9242 * If there is a next window or a status line, we first try to delete the
9243 * lines at the bottom to avoid messing what is after the window.
9244 * If this fails and there are following windows, don't do anything to avoid
9245 * messing up those windows, better just redraw.
9246 */
9247 did_delete = FALSE;
9248#ifdef FEAT_WINDOWS
9249 if (wp->w_next != NULL || wp->w_status_height)
9250 {
9251 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9252 line_count, (int)Rows, FALSE, NULL) == OK)
9253 did_delete = TRUE;
9254 else if (wp->w_next)
9255 return FAIL;
9256 }
9257#endif
9258 /*
9259 * if no lines deleted, blank the lines that will end up below the window
9260 */
9261 if (!did_delete)
9262 {
9263#ifdef FEAT_WINDOWS
9264 wp->w_redr_status = TRUE;
9265#endif
9266 redraw_cmdline = TRUE;
9267 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9268 lastrow = nextrow + line_count;
9269 if (lastrow > Rows)
9270 lastrow = Rows;
9271 screen_fill(nextrow - line_count, lastrow - line_count,
9272 W_WINCOL(wp), (int)W_ENDCOL(wp),
9273 ' ', ' ', 0);
9274 }
9275
9276 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9277 == FAIL)
9278 {
9279 /* deletion will have messed up other windows */
9280 if (did_delete)
9281 {
9282#ifdef FEAT_WINDOWS
9283 wp->w_redr_status = TRUE;
9284#endif
9285 win_rest_invalid(W_NEXT(wp));
9286 }
9287 return FAIL;
9288 }
9289
9290 return OK;
9291}
9292
9293/*
9294 * delete "line_count" window lines at "row" in window "wp"
9295 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9296 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9297 * scrolling
9298 * Return OK for success, FAIL if the lines are not deleted.
9299 */
9300 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009301win_del_lines(
9302 win_T *wp,
9303 int row,
9304 int line_count,
9305 int invalid,
9306 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int retval;
9309
9310 if (invalid)
9311 wp->w_lines_valid = 0;
9312
9313 if (line_count > wp->w_height - row)
9314 line_count = wp->w_height - row;
9315
9316 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9317 if (retval != MAYBE)
9318 return retval;
9319
9320 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9321 (int)Rows, FALSE, NULL) == FAIL)
9322 return FAIL;
9323
9324#ifdef FEAT_WINDOWS
9325 /*
9326 * If there are windows or status lines below, try to put them at the
9327 * correct place. If we can't do that, they have to be redrawn.
9328 */
9329 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9330 {
9331 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9332 line_count, (int)Rows, NULL) == FAIL)
9333 {
9334 wp->w_redr_status = TRUE;
9335 win_rest_invalid(wp->w_next);
9336 }
9337 }
9338 /*
9339 * If this is the last window and there is no status line, redraw the
9340 * command line later.
9341 */
9342 else
9343#endif
9344 redraw_cmdline = TRUE;
9345 return OK;
9346}
9347
9348/*
9349 * Common code for win_ins_lines() and win_del_lines().
9350 * Returns OK or FAIL when the work has been done.
9351 * Returns MAYBE when not finished yet.
9352 */
9353 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009354win_do_lines(
9355 win_T *wp,
9356 int row,
9357 int line_count,
9358 int mayclear,
9359 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361 int retval;
9362
9363 if (!redrawing() || line_count <= 0)
9364 return FAIL;
9365
9366 /* only a few lines left: redraw is faster */
9367 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009368#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 && wp->w_width == Columns
9370#endif
9371 )
9372 {
9373 screenclear(); /* will set wp->w_lines_valid to 0 */
9374 return FAIL;
9375 }
9376
9377 /*
9378 * Delete all remaining lines
9379 */
9380 if (row + line_count >= wp->w_height)
9381 {
9382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9383 W_WINCOL(wp), (int)W_ENDCOL(wp),
9384 ' ', ' ', 0);
9385 return OK;
9386 }
9387
9388 /*
9389 * when scrolling, the message on the command line should be cleared,
9390 * otherwise it will stay there forever.
9391 */
9392 clear_cmdline = TRUE;
9393
9394 /*
9395 * If the terminal can set a scroll region, use that.
9396 * Always do this in a vertically split window. This will redraw from
9397 * ScreenLines[] when t_CV isn't defined. That's faster than using
9398 * win_line().
9399 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009400 * a character in the lower right corner of the scroll region may cause a
9401 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 */
9403 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009404#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 || W_WIDTH(wp) != Columns
9406#endif
9407 )
9408 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009409#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9411#endif
9412 scroll_region_set(wp, row);
9413 if (del)
9414 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9415 wp->w_height - row, FALSE, wp);
9416 else
9417 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9418 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009419#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9421#endif
9422 scroll_region_reset();
9423 return retval;
9424 }
9425
9426#ifdef FEAT_WINDOWS
9427 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9428 return FAIL;
9429#endif
9430
9431 return MAYBE;
9432}
9433
9434/*
9435 * window 'wp' and everything after it is messed up, mark it for redraw
9436 */
9437 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009438win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440#ifdef FEAT_WINDOWS
9441 while (wp != NULL)
9442#else
9443 if (wp != NULL)
9444#endif
9445 {
9446 redraw_win_later(wp, NOT_VALID);
9447#ifdef FEAT_WINDOWS
9448 wp->w_redr_status = TRUE;
9449 wp = wp->w_next;
9450#endif
9451 }
9452 redraw_cmdline = TRUE;
9453}
9454
9455/*
9456 * The rest of the routines in this file perform screen manipulations. The
9457 * given operation is performed physically on the screen. The corresponding
9458 * change is also made to the internal screen image. In this way, the editor
9459 * anticipates the effect of editing changes on the appearance of the screen.
9460 * That way, when we call screenupdate a complete redraw isn't usually
9461 * necessary. Another advantage is that we can keep adding code to anticipate
9462 * screen changes, and in the meantime, everything still works.
9463 */
9464
9465/*
9466 * types for inserting or deleting lines
9467 */
9468#define USE_T_CAL 1
9469#define USE_T_CDL 2
9470#define USE_T_AL 3
9471#define USE_T_CE 4
9472#define USE_T_DL 5
9473#define USE_T_SR 6
9474#define USE_NL 7
9475#define USE_T_CD 8
9476#define USE_REDRAW 9
9477
9478/*
9479 * insert lines on the screen and update ScreenLines[]
9480 * 'end' is the line after the scrolled part. Normally it is Rows.
9481 * When scrolling region used 'off' is the offset from the top for the region.
9482 * 'row' and 'end' are relative to the start of the region.
9483 *
9484 * return FAIL for failure, OK for success.
9485 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009486 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009487screen_ins_lines(
9488 int off,
9489 int row,
9490 int line_count,
9491 int end,
9492 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 int i;
9495 int j;
9496 unsigned temp;
9497 int cursor_row;
9498 int type;
9499 int result_empty;
9500 int can_ce = can_clear(T_CE);
9501
9502 /*
9503 * FAIL if
9504 * - there is no valid screen
9505 * - the screen has to be redrawn completely
9506 * - the line count is less than one
9507 * - the line count is more than 'ttyscroll'
9508 */
9509 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9510 return FAIL;
9511
9512 /*
9513 * There are seven ways to insert lines:
9514 * 0. When in a vertically split window and t_CV isn't set, redraw the
9515 * characters from ScreenLines[].
9516 * 1. Use T_CD (clear to end of display) if it exists and the result of
9517 * the insert is just empty lines
9518 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9519 * present or line_count > 1. It looks better if we do all the inserts
9520 * at once.
9521 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9522 * insert is just empty lines and T_CE is not present or line_count >
9523 * 1.
9524 * 4. Use T_AL (insert line) if it exists.
9525 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9526 * just empty lines.
9527 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9528 * just empty lines.
9529 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9530 * the 'da' flag is not set or we have clear line capability.
9531 * 8. redraw the characters from ScreenLines[].
9532 *
9533 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9534 * the scrollbar for the window. It does have insert line, use that if it
9535 * exists.
9536 */
9537 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009538#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9540 type = USE_REDRAW;
9541 else
9542#endif
9543 if (can_clear(T_CD) && result_empty)
9544 type = USE_T_CD;
9545 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9546 type = USE_T_CAL;
9547 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9548 type = USE_T_CDL;
9549 else if (*T_AL != NUL)
9550 type = USE_T_AL;
9551 else if (can_ce && result_empty)
9552 type = USE_T_CE;
9553 else if (*T_DL != NUL && result_empty)
9554 type = USE_T_DL;
9555 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9556 type = USE_T_SR;
9557 else
9558 return FAIL;
9559
9560 /*
9561 * For clearing the lines screen_del_lines() is used. This will also take
9562 * care of t_db if necessary.
9563 */
9564 if (type == USE_T_CD || type == USE_T_CDL ||
9565 type == USE_T_CE || type == USE_T_DL)
9566 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9567
9568 /*
9569 * If text is retained below the screen, first clear or delete as many
9570 * lines at the bottom of the window as are about to be inserted so that
9571 * the deleted lines won't later surface during a screen_del_lines.
9572 */
9573 if (*T_DB)
9574 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9575
9576#ifdef FEAT_CLIPBOARD
9577 /* Remove a modeless selection when inserting lines halfway the screen
9578 * or not the full width of the screen. */
9579 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009580# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 || (wp != NULL && wp->w_width != Columns)
9582# endif
9583 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009584 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 else
9586 clip_scroll_selection(-line_count);
9587#endif
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589#ifdef FEAT_GUI
9590 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9591 * scrolling is actually carried out. */
9592 gui_dont_update_cursor();
9593#endif
9594
9595 if (*T_CCS != NUL) /* cursor relative to region */
9596 cursor_row = row;
9597 else
9598 cursor_row = row + off;
9599
9600 /*
9601 * Shift LineOffset[] line_count down to reflect the inserted lines.
9602 * Clear the inserted lines in ScreenLines[].
9603 */
9604 row += off;
9605 end += off;
9606 for (i = 0; i < line_count; ++i)
9607 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009608#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 if (wp != NULL && wp->w_width != Columns)
9610 {
9611 /* need to copy part of a line */
9612 j = end - 1 - i;
9613 while ((j -= line_count) >= row)
9614 linecopy(j + line_count, j, wp);
9615 j += line_count;
9616 if (can_clear((char_u *)" "))
9617 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9618 else
9619 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9620 LineWraps[j] = FALSE;
9621 }
9622 else
9623#endif
9624 {
9625 j = end - 1 - i;
9626 temp = LineOffset[j];
9627 while ((j -= line_count) >= row)
9628 {
9629 LineOffset[j + line_count] = LineOffset[j];
9630 LineWraps[j + line_count] = LineWraps[j];
9631 }
9632 LineOffset[j + line_count] = temp;
9633 LineWraps[j + line_count] = FALSE;
9634 if (can_clear((char_u *)" "))
9635 lineclear(temp, (int)Columns);
9636 else
9637 lineinvalid(temp, (int)Columns);
9638 }
9639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640
9641 screen_stop_highlight();
9642 windgoto(cursor_row, 0);
9643
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009644#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 /* redraw the characters */
9646 if (type == USE_REDRAW)
9647 redraw_block(row, end, wp);
9648 else
9649#endif
9650 if (type == USE_T_CAL)
9651 {
9652 term_append_lines(line_count);
9653 screen_start(); /* don't know where cursor is now */
9654 }
9655 else
9656 {
9657 for (i = 0; i < line_count; i++)
9658 {
9659 if (type == USE_T_AL)
9660 {
9661 if (i && cursor_row != 0)
9662 windgoto(cursor_row, 0);
9663 out_str(T_AL);
9664 }
9665 else /* type == USE_T_SR */
9666 out_str(T_SR);
9667 screen_start(); /* don't know where cursor is now */
9668 }
9669 }
9670
9671 /*
9672 * With scroll-reverse and 'da' flag set we need to clear the lines that
9673 * have been scrolled down into the region.
9674 */
9675 if (type == USE_T_SR && *T_DA)
9676 {
9677 for (i = 0; i < line_count; ++i)
9678 {
9679 windgoto(off + i, 0);
9680 out_str(T_CE);
9681 screen_start(); /* don't know where cursor is now */
9682 }
9683 }
9684
9685#ifdef FEAT_GUI
9686 gui_can_update_cursor();
9687 if (gui.in_use)
9688 out_flush(); /* always flush after a scroll */
9689#endif
9690 return OK;
9691}
9692
9693/*
9694 * delete lines on the screen and update ScreenLines[]
9695 * 'end' is the line after the scrolled part. Normally it is Rows.
9696 * When scrolling region used 'off' is the offset from the top for the region.
9697 * 'row' and 'end' are relative to the start of the region.
9698 *
9699 * Return OK for success, FAIL if the lines are not deleted.
9700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009702screen_del_lines(
9703 int off,
9704 int row,
9705 int line_count,
9706 int end,
9707 int force, /* even when line_count > p_ttyscroll */
9708 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 int j;
9711 int i;
9712 unsigned temp;
9713 int cursor_row;
9714 int cursor_end;
9715 int result_empty; /* result is empty until end of region */
9716 int can_delete; /* deleting line codes can be used */
9717 int type;
9718
9719 /*
9720 * FAIL if
9721 * - there is no valid screen
9722 * - the screen has to be redrawn completely
9723 * - the line count is less than one
9724 * - the line count is more than 'ttyscroll'
9725 */
9726 if (!screen_valid(TRUE) || line_count <= 0 ||
9727 (!force && line_count > p_ttyscroll))
9728 return FAIL;
9729
9730 /*
9731 * Check if the rest of the current region will become empty.
9732 */
9733 result_empty = row + line_count >= end;
9734
9735 /*
9736 * We can delete lines only when 'db' flag not set or when 'ce' option
9737 * available.
9738 */
9739 can_delete = (*T_DB == NUL || can_clear(T_CE));
9740
9741 /*
9742 * There are six ways to delete lines:
9743 * 0. When in a vertically split window and t_CV isn't set, redraw the
9744 * characters from ScreenLines[].
9745 * 1. Use T_CD if it exists and the result is empty.
9746 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9747 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9748 * none of the other ways work.
9749 * 4. Use T_CE (erase line) if the result is empty.
9750 * 5. Use T_DL (delete line) if it exists.
9751 * 6. redraw the characters from ScreenLines[].
9752 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009753#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9755 type = USE_REDRAW;
9756 else
9757#endif
9758 if (can_clear(T_CD) && result_empty)
9759 type = USE_T_CD;
9760#if defined(__BEOS__) && defined(BEOS_DR8)
9761 /*
9762 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9763 * its internal termcap... this works okay for tests which test *T_DB !=
9764 * NUL. It has the disadvantage that the user cannot use any :set t_*
9765 * command to get T_DB (back) to empty_option, only :set term=... will do
9766 * the trick...
9767 * Anyway, this hack will hopefully go away with the next OS release.
9768 * (Olaf Seibert)
9769 */
9770 else if (row == 0 && T_DB == empty_option
9771 && (line_count == 1 || *T_CDL == NUL))
9772#else
9773 else if (row == 0 && (
9774#ifndef AMIGA
9775 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9776 * up, so use delete-line command */
9777 line_count == 1 ||
9778#endif
9779 *T_CDL == NUL))
9780#endif
9781 type = USE_NL;
9782 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9783 type = USE_T_CDL;
9784 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009785#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 && (wp == NULL || wp->w_width == Columns)
9787#endif
9788 )
9789 type = USE_T_CE;
9790 else if (*T_DL != NUL && can_delete)
9791 type = USE_T_DL;
9792 else if (*T_CDL != NUL && can_delete)
9793 type = USE_T_CDL;
9794 else
9795 return FAIL;
9796
9797#ifdef FEAT_CLIPBOARD
9798 /* Remove a modeless selection when deleting lines halfway the screen or
9799 * not the full width of the screen. */
9800 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009801# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 || (wp != NULL && wp->w_width != Columns)
9803# endif
9804 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009805 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else
9807 clip_scroll_selection(line_count);
9808#endif
9809
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#ifdef FEAT_GUI
9811 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9812 * scrolling is actually carried out. */
9813 gui_dont_update_cursor();
9814#endif
9815
9816 if (*T_CCS != NUL) /* cursor relative to region */
9817 {
9818 cursor_row = row;
9819 cursor_end = end;
9820 }
9821 else
9822 {
9823 cursor_row = row + off;
9824 cursor_end = end + off;
9825 }
9826
9827 /*
9828 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9829 * Clear the inserted lines in ScreenLines[].
9830 */
9831 row += off;
9832 end += off;
9833 for (i = 0; i < line_count; ++i)
9834 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009835#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 if (wp != NULL && wp->w_width != Columns)
9837 {
9838 /* need to copy part of a line */
9839 j = row + i;
9840 while ((j += line_count) <= end - 1)
9841 linecopy(j - line_count, j, wp);
9842 j -= line_count;
9843 if (can_clear((char_u *)" "))
9844 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9845 else
9846 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9847 LineWraps[j] = FALSE;
9848 }
9849 else
9850#endif
9851 {
9852 /* whole width, moving the line pointers is faster */
9853 j = row + i;
9854 temp = LineOffset[j];
9855 while ((j += line_count) <= end - 1)
9856 {
9857 LineOffset[j - line_count] = LineOffset[j];
9858 LineWraps[j - line_count] = LineWraps[j];
9859 }
9860 LineOffset[j - line_count] = temp;
9861 LineWraps[j - line_count] = FALSE;
9862 if (can_clear((char_u *)" "))
9863 lineclear(temp, (int)Columns);
9864 else
9865 lineinvalid(temp, (int)Columns);
9866 }
9867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
9869 screen_stop_highlight();
9870
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009871#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 /* redraw the characters */
9873 if (type == USE_REDRAW)
9874 redraw_block(row, end, wp);
9875 else
9876#endif
9877 if (type == USE_T_CD) /* delete the lines */
9878 {
9879 windgoto(cursor_row, 0);
9880 out_str(T_CD);
9881 screen_start(); /* don't know where cursor is now */
9882 }
9883 else if (type == USE_T_CDL)
9884 {
9885 windgoto(cursor_row, 0);
9886 term_delete_lines(line_count);
9887 screen_start(); /* don't know where cursor is now */
9888 }
9889 /*
9890 * Deleting lines at top of the screen or scroll region: Just scroll
9891 * the whole screen (scroll region) up by outputting newlines on the
9892 * last line.
9893 */
9894 else if (type == USE_NL)
9895 {
9896 windgoto(cursor_end - 1, 0);
9897 for (i = line_count; --i >= 0; )
9898 out_char('\n'); /* cursor will remain on same line */
9899 }
9900 else
9901 {
9902 for (i = line_count; --i >= 0; )
9903 {
9904 if (type == USE_T_DL)
9905 {
9906 windgoto(cursor_row, 0);
9907 out_str(T_DL); /* delete a line */
9908 }
9909 else /* type == USE_T_CE */
9910 {
9911 windgoto(cursor_row + i, 0);
9912 out_str(T_CE); /* erase a line */
9913 }
9914 screen_start(); /* don't know where cursor is now */
9915 }
9916 }
9917
9918 /*
9919 * If the 'db' flag is set, we need to clear the lines that have been
9920 * scrolled up at the bottom of the region.
9921 */
9922 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9923 {
9924 for (i = line_count; i > 0; --i)
9925 {
9926 windgoto(cursor_end - i, 0);
9927 out_str(T_CE); /* erase a line */
9928 screen_start(); /* don't know where cursor is now */
9929 }
9930 }
9931
9932#ifdef FEAT_GUI
9933 gui_can_update_cursor();
9934 if (gui.in_use)
9935 out_flush(); /* always flush after a scroll */
9936#endif
9937
9938 return OK;
9939}
9940
9941/*
9942 * show the current mode and ruler
9943 *
9944 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9945 * If clear_cmdline is FALSE there may be a message there that needs to be
9946 * cleared only if a mode is shown.
9947 * Return the length of the message (0 if no message).
9948 */
9949 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009950showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 int need_clear;
9953 int length = 0;
9954 int do_mode;
9955 int attr;
9956 int nwr_save;
9957#ifdef FEAT_INS_EXPAND
9958 int sub_attr;
9959#endif
9960
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009961 do_mode = ((p_smd && msg_silent == 0)
9962 && ((State & INSERT)
9963 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009964 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 if (do_mode || Recording)
9966 {
9967 /*
9968 * Don't show mode right now, when not redrawing or inside a mapping.
9969 * Call char_avail() only when we are going to show something, because
9970 * it takes a bit of time.
9971 */
9972 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9973 {
9974 redraw_cmdline = TRUE; /* show mode later */
9975 return 0;
9976 }
9977
9978 nwr_save = need_wait_return;
9979
9980 /* wait a bit before overwriting an important message */
9981 check_for_delay(FALSE);
9982
9983 /* if the cmdline is more than one line high, erase top lines */
9984 need_clear = clear_cmdline;
9985 if (clear_cmdline && cmdline_row < Rows - 1)
9986 msg_clr_cmdline(); /* will reset clear_cmdline */
9987
9988 /* Position on the last line in the window, column 0 */
9989 msg_pos_mode();
9990 cursor_off();
9991 attr = hl_attr(HLF_CM); /* Highlight mode */
9992 if (do_mode)
9993 {
9994 MSG_PUTS_ATTR("--", attr);
9995#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009996 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009997# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009998 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009999# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010000 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010001# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010002 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010003# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 MSG_PUTS_ATTR(" IM", attr);
10005# else
10006 MSG_PUTS_ATTR(" XIM", attr);
10007# endif
10008#endif
10009#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10010 if (gui.in_use)
10011 {
10012 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010013 {
10014 /* HANGUL */
10015 if (enc_utf8)
10016 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10017 else
10018 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 }
10021#endif
10022#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010023 /* CTRL-X in Insert mode */
10024 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 {
10026 /* These messages can get long, avoid a wrap in a narrow
10027 * window. Prefer showing edit_submode_extra. */
10028 length = (Rows - msg_row) * Columns - 3;
10029 if (edit_submode_extra != NULL)
10030 length -= vim_strsize(edit_submode_extra);
10031 if (length > 0)
10032 {
10033 if (edit_submode_pre != NULL)
10034 length -= vim_strsize(edit_submode_pre);
10035 if (length - vim_strsize(edit_submode) > 0)
10036 {
10037 if (edit_submode_pre != NULL)
10038 msg_puts_attr(edit_submode_pre, attr);
10039 msg_puts_attr(edit_submode, attr);
10040 }
10041 if (edit_submode_extra != NULL)
10042 {
10043 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10044 if ((int)edit_submode_highl < (int)HLF_COUNT)
10045 sub_attr = hl_attr(edit_submode_highl);
10046 else
10047 sub_attr = attr;
10048 msg_puts_attr(edit_submode_extra, sub_attr);
10049 }
10050 }
10051 length = 0;
10052 }
10053 else
10054#endif
10055 {
10056#ifdef FEAT_VREPLACE
10057 if (State & VREPLACE_FLAG)
10058 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10059 else
10060#endif
10061 if (State & REPLACE_FLAG)
10062 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10063 else if (State & INSERT)
10064 {
10065#ifdef FEAT_RIGHTLEFT
10066 if (p_ri)
10067 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10068#endif
10069 MSG_PUTS_ATTR(_(" INSERT"), attr);
10070 }
10071 else if (restart_edit == 'I')
10072 MSG_PUTS_ATTR(_(" (insert)"), attr);
10073 else if (restart_edit == 'R')
10074 MSG_PUTS_ATTR(_(" (replace)"), attr);
10075 else if (restart_edit == 'V')
10076 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10077#ifdef FEAT_RIGHTLEFT
10078 if (p_hkmap)
10079 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10080# ifdef FEAT_FKMAP
10081 if (p_fkmap)
10082 MSG_PUTS_ATTR(farsi_text_5, attr);
10083# endif
10084#endif
10085#ifdef FEAT_KEYMAP
10086 if (State & LANGMAP)
10087 {
10088# ifdef FEAT_ARABIC
10089 if (curwin->w_p_arab)
10090 MSG_PUTS_ATTR(_(" Arabic"), attr);
10091 else
10092# endif
10093 MSG_PUTS_ATTR(_(" (lang)"), attr);
10094 }
10095#endif
10096 if ((State & INSERT) && p_paste)
10097 MSG_PUTS_ATTR(_(" (paste)"), attr);
10098
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 if (VIsual_active)
10100 {
10101 char *p;
10102
10103 /* Don't concatenate separate words to avoid translation
10104 * problems. */
10105 switch ((VIsual_select ? 4 : 0)
10106 + (VIsual_mode == Ctrl_V) * 2
10107 + (VIsual_mode == 'V'))
10108 {
10109 case 0: p = N_(" VISUAL"); break;
10110 case 1: p = N_(" VISUAL LINE"); break;
10111 case 2: p = N_(" VISUAL BLOCK"); break;
10112 case 4: p = N_(" SELECT"); break;
10113 case 5: p = N_(" SELECT LINE"); break;
10114 default: p = N_(" SELECT BLOCK"); break;
10115 }
10116 MSG_PUTS_ATTR(_(p), attr);
10117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 MSG_PUTS_ATTR(" --", attr);
10119 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010120
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 need_clear = TRUE;
10122 }
10123 if (Recording
10124#ifdef FEAT_INS_EXPAND
10125 && edit_submode == NULL /* otherwise it gets too long */
10126#endif
10127 )
10128 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010129 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 need_clear = TRUE;
10131 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010132
10133 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 if (need_clear || clear_cmdline)
10135 msg_clr_eos();
10136 msg_didout = FALSE; /* overwrite this message */
10137 length = msg_col;
10138 msg_col = 0;
10139 need_wait_return = nwr_save; /* never ask for hit-return for this */
10140 }
10141 else if (clear_cmdline && msg_silent == 0)
10142 /* Clear the whole command line. Will reset "clear_cmdline". */
10143 msg_clr_cmdline();
10144
10145#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 /* In Visual mode the size of the selected area must be redrawn. */
10147 if (VIsual_active)
10148 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149
10150 /* If the last window has no status line, the ruler is after the mode
10151 * message and must be redrawn */
10152 if (redrawing()
10153# ifdef FEAT_WINDOWS
10154 && lastwin->w_status_height == 0
10155# endif
10156 )
10157 win_redr_ruler(lastwin, TRUE);
10158#endif
10159 redraw_cmdline = FALSE;
10160 clear_cmdline = FALSE;
10161
10162 return length;
10163}
10164
10165/*
10166 * Position for a mode message.
10167 */
10168 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010169msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 msg_col = 0;
10172 msg_row = Rows - 1;
10173}
10174
10175/*
10176 * Delete mode message. Used when ESC is typed which is expected to end
10177 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010178 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 */
10180 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010181unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010184 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 */
10186 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10187 redraw_cmdline = TRUE; /* delete mode later */
10188 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010189 clearmode();
10190}
10191
10192/*
10193 * Clear the mode message.
10194 */
10195 void
10196clearmode()
10197{
10198 msg_pos_mode();
10199 if (Recording)
10200 recording_mode(hl_attr(HLF_CM));
10201 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202}
10203
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010204 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010205recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010206{
10207 MSG_PUTS_ATTR(_("recording"), attr);
10208 if (!shortmess(SHM_RECORDING))
10209 {
10210 char_u s[4];
10211 sprintf((char *)s, " @%c", Recording);
10212 MSG_PUTS_ATTR(s, attr);
10213 }
10214}
10215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010216#if defined(FEAT_WINDOWS)
10217/*
10218 * Draw the tab pages line at the top of the Vim window.
10219 */
10220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010221draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010222{
10223 int tabcount = 0;
10224 tabpage_T *tp;
10225 int tabwidth;
10226 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010227 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010228 int attr;
10229 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010230 win_T *cwp;
10231 int wincount;
10232 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010233 int c;
10234 int len;
10235 int attr_sel = hl_attr(HLF_TPS);
10236 int attr_nosel = hl_attr(HLF_TP);
10237 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010238 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010239 int room;
10240 int use_sep_chars = (t_colors < 8
10241#ifdef FEAT_GUI
10242 && !gui.in_use
10243#endif
10244 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010245
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010246 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010247
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010248#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010249 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010250 if (gui_use_tabline())
10251 {
10252 gui_update_tabline();
10253 return;
10254 }
10255#endif
10256
10257 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010258 return;
10259
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010260#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010261
10262 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10263 for (scol = 0; scol < Columns; ++scol)
10264 TabPageIdxs[scol] = 0;
10265
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010266 /* Use the 'tabline' option if it's set. */
10267 if (*p_tal != NUL)
10268 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010269 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010270
10271 /* Check for an error. If there is one we would loop in redrawing the
10272 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010273 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010274 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010275 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010276 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010277 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010278 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010279 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010280 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010281#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010282 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010283 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10284 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010285
Bram Moolenaar238a5642006-02-21 22:12:05 +000010286 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10287 if (tabwidth < 6)
10288 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010289
Bram Moolenaar238a5642006-02-21 22:12:05 +000010290 attr = attr_nosel;
10291 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010292 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010293 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10294 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010295 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010296 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010297
Bram Moolenaar238a5642006-02-21 22:12:05 +000010298 if (tp->tp_topframe == topframe)
10299 attr = attr_sel;
10300 if (use_sep_chars && col > 0)
10301 screen_putchar('|', 0, col++, attr);
10302
10303 if (tp->tp_topframe != topframe)
10304 attr = attr_nosel;
10305
10306 screen_putchar(' ', 0, col++, attr);
10307
10308 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010309 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010310 cwp = curwin;
10311 wp = firstwin;
10312 }
10313 else
10314 {
10315 cwp = tp->tp_curwin;
10316 wp = tp->tp_firstwin;
10317 }
10318
10319 modified = FALSE;
10320 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10321 if (bufIsChanged(wp->w_buffer))
10322 modified = TRUE;
10323 if (modified || wincount > 1)
10324 {
10325 if (wincount > 1)
10326 {
10327 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010328 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010329 if (col + len >= Columns - 3)
10330 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010332#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010333 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010334#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010335 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010336#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010337 );
10338 col += len;
10339 }
10340 if (modified)
10341 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10342 screen_putchar(' ', 0, col++, attr);
10343 }
10344
10345 room = scol - col + tabwidth - 1;
10346 if (room > 0)
10347 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010348 /* Get buffer name in NameBuff[] */
10349 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010350 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010351 len = vim_strsize(NameBuff);
10352 p = NameBuff;
10353#ifdef FEAT_MBYTE
10354 if (has_mbyte)
10355 while (len > room)
10356 {
10357 len -= ptr2cells(p);
10358 mb_ptr_adv(p);
10359 }
10360 else
10361#endif
10362 if (len > room)
10363 {
10364 p += len - room;
10365 len = room;
10366 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010367 if (len > Columns - col - 1)
10368 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010370 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010371 col += len;
10372 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010373 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010374
10375 /* Store the tab page number in TabPageIdxs[], so that
10376 * jump_to_mouse() knows where each one is. */
10377 ++tabcount;
10378 while (scol < col)
10379 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010380 }
10381
Bram Moolenaar238a5642006-02-21 22:12:05 +000010382 if (use_sep_chars)
10383 c = '_';
10384 else
10385 c = ' ';
10386 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010387
10388 /* Put an "X" for closing the current tab if there are several. */
10389 if (first_tabpage->tp_next != NULL)
10390 {
10391 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10392 TabPageIdxs[Columns - 1] = -999;
10393 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010394 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010395
10396 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10397 * set. */
10398 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010399}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010400
10401/*
10402 * Get buffer name for "buf" into NameBuff[].
10403 * Takes care of special buffer names and translates special characters.
10404 */
10405 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010406get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010407{
10408 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010409 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010410 else
10411 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10412 trans_characters(NameBuff, MAXPATHL);
10413}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010414#endif
10415
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10417/*
10418 * Get the character to use in a status line. Get its attributes in "*attr".
10419 */
10420 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010421fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422{
10423 int fill;
10424 if (is_curwin)
10425 {
10426 *attr = hl_attr(HLF_S);
10427 fill = fill_stl;
10428 }
10429 else
10430 {
10431 *attr = hl_attr(HLF_SNC);
10432 fill = fill_stlnc;
10433 }
10434 /* Use fill when there is highlighting, and highlighting of current
10435 * window differs, or the fillchars differ, or this is not the
10436 * current window */
10437 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10438 || !is_curwin || firstwin == lastwin)
10439 || (fill_stl != fill_stlnc)))
10440 return fill;
10441 if (is_curwin)
10442 return '^';
10443 return '=';
10444}
10445#endif
10446
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010447#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448/*
10449 * Get the character to use in a separator between vertically split windows.
10450 * Get its attributes in "*attr".
10451 */
10452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010453fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 *attr = hl_attr(HLF_C);
10456 if (*attr == 0 && fill_vert == ' ')
10457 return '|';
10458 else
10459 return fill_vert;
10460}
10461#endif
10462
10463/*
10464 * Return TRUE if redrawing should currently be done.
10465 */
10466 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010467redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468{
10469 return (!RedrawingDisabled
10470 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10471}
10472
10473/*
10474 * Return TRUE if printing messages should currently be done.
10475 */
10476 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010477messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478{
10479 return (!(p_lz && char_avail() && !KeyTyped));
10480}
10481
10482/*
10483 * Show current status info in ruler and various other places
10484 * If always is FALSE, only show ruler if position has changed.
10485 */
10486 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010487showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 if (!always && !redrawing())
10490 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010491#ifdef FEAT_INS_EXPAND
10492 if (pum_visible())
10493 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010494# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010495 /* Don't redraw right now, do it later. */
10496 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010497# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010498 return;
10499 }
10500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010502 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010503 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010504 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 else
10507#endif
10508#ifdef FEAT_CMDL_INFO
10509 win_redr_ruler(curwin, always);
10510#endif
10511
10512#ifdef FEAT_TITLE
10513 if (need_maketitle
10514# ifdef FEAT_STL_OPT
10515 || (p_icon && (stl_syntax & STL_IN_ICON))
10516 || (p_title && (stl_syntax & STL_IN_TITLE))
10517# endif
10518 )
10519 maketitle();
10520#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010521#ifdef FEAT_WINDOWS
10522 /* Redraw the tab pages line if needed. */
10523 if (redraw_tabline)
10524 draw_tabline();
10525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526}
10527
10528#ifdef FEAT_CMDL_INFO
10529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010530win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010532#define RULER_BUF_LEN 70
10533 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 int row;
10535 int fillchar;
10536 int attr;
10537 int empty_line = FALSE;
10538 colnr_T virtcol;
10539 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010540 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010542#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 int this_ru_col;
10544 int off = 0;
10545 int width = Columns;
10546# define WITH_OFF(x) x
10547# define WITH_WIDTH(x) x
10548#else
10549# define WITH_OFF(x) 0
10550# define WITH_WIDTH(x) Columns
10551# define this_ru_col ru_col
10552#endif
10553
10554 /* If 'ruler' off or redrawing disabled, don't do anything */
10555 if (!p_ru)
10556 return;
10557
10558 /*
10559 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10560 * after deleting lines, before cursor.lnum is corrected.
10561 */
10562 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10563 return;
10564
10565#ifdef FEAT_INS_EXPAND
10566 /* Don't draw the ruler while doing insert-completion, it might overwrite
10567 * the (long) mode message. */
10568# ifdef FEAT_WINDOWS
10569 if (wp == lastwin && lastwin->w_status_height == 0)
10570# endif
10571 if (edit_submode != NULL)
10572 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010573 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10574 if (pum_visible())
10575 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576#endif
10577
10578#ifdef FEAT_STL_OPT
10579 if (*p_ruf)
10580 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010581 int save_called_emsg = called_emsg;
10582
10583 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 if (called_emsg)
10586 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010587 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010588 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 return;
10590 }
10591#endif
10592
10593 /*
10594 * Check if not in Insert mode and the line is empty (will show "0-1").
10595 */
10596 if (!(State & INSERT)
10597 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10598 empty_line = TRUE;
10599
10600 /*
10601 * Only draw the ruler when something changed.
10602 */
10603 validate_virtcol_win(wp);
10604 if ( redraw_cmdline
10605 || always
10606 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10607 || wp->w_cursor.col != wp->w_ru_cursor.col
10608 || wp->w_virtcol != wp->w_ru_virtcol
10609#ifdef FEAT_VIRTUALEDIT
10610 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10611#endif
10612 || wp->w_topline != wp->w_ru_topline
10613 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10614#ifdef FEAT_DIFF
10615 || wp->w_topfill != wp->w_ru_topfill
10616#endif
10617 || empty_line != wp->w_ru_empty)
10618 {
10619 cursor_off();
10620#ifdef FEAT_WINDOWS
10621 if (wp->w_status_height)
10622 {
10623 row = W_WINROW(wp) + wp->w_height;
10624 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 off = W_WINCOL(wp);
10626 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 }
10628 else
10629#endif
10630 {
10631 row = Rows - 1;
10632 fillchar = ' ';
10633 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010634#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 width = Columns;
10636 off = 0;
10637#endif
10638 }
10639
10640 /* In list mode virtcol needs to be recomputed */
10641 virtcol = wp->w_virtcol;
10642 if (wp->w_p_list && lcs_tab1 == NUL)
10643 {
10644 wp->w_p_list = FALSE;
10645 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10646 wp->w_p_list = TRUE;
10647 }
10648
10649 /*
10650 * Some sprintfs return the length, some return a pointer.
10651 * To avoid portability problems we use strlen() here.
10652 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010653 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10655 ? 0L
10656 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010657 len = STRLEN(buffer);
10658 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10660 (int)virtcol + 1);
10661
10662 /*
10663 * Add a "50%" if there is room for it.
10664 * On the last line, don't print in the last column (scrolls the
10665 * screen up on some terminals).
10666 */
10667 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010668 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 o = i + vim_strsize(buffer + i + 1);
10670#ifdef FEAT_WINDOWS
10671 if (wp->w_status_height == 0) /* can't use last char of screen */
10672#endif
10673 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010674#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 this_ru_col = ru_col - (Columns - width);
10676 if (this_ru_col < 0)
10677 this_ru_col = 0;
10678#endif
10679 /* Never use more than half the window/screen width, leave the other
10680 * half for the filename. */
10681 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10682 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10683 if (this_ru_col + o < WITH_WIDTH(width))
10684 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010685 /* need at least 3 chars left for get_rel_pos() + NUL */
10686 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 {
10688#ifdef FEAT_MBYTE
10689 if (has_mbyte)
10690 i += (*mb_char2bytes)(fillchar, buffer + i);
10691 else
10692#endif
10693 buffer[i++] = fillchar;
10694 ++o;
10695 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010696 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 }
10698 /* Truncate at window boundary. */
10699#ifdef FEAT_MBYTE
10700 if (has_mbyte)
10701 {
10702 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010703 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
10705 o += (*mb_ptr2cells)(buffer + i);
10706 if (this_ru_col + o > WITH_WIDTH(width))
10707 {
10708 buffer[i] = NUL;
10709 break;
10710 }
10711 }
10712 }
10713 else
10714#endif
10715 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10716 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10717
10718 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10719 i = redraw_cmdline;
10720 screen_fill(row, row + 1,
10721 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10722 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10723 fillchar, fillchar, attr);
10724 /* don't redraw the cmdline because of showing the ruler */
10725 redraw_cmdline = i;
10726 wp->w_ru_cursor = wp->w_cursor;
10727 wp->w_ru_virtcol = wp->w_virtcol;
10728 wp->w_ru_empty = empty_line;
10729 wp->w_ru_topline = wp->w_topline;
10730 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10731#ifdef FEAT_DIFF
10732 wp->w_ru_topfill = wp->w_topfill;
10733#endif
10734 }
10735}
10736#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010737
10738#if defined(FEAT_LINEBREAK) || defined(PROTO)
10739/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010740 * Return the width of the 'number' and 'relativenumber' column.
10741 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010742 * Otherwise it depends on 'numberwidth' and the line count.
10743 */
10744 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010745number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010746{
10747 int n;
10748 linenr_T lnum;
10749
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010750 if (wp->w_p_rnu && !wp->w_p_nu)
10751 /* cursor line shows "0" */
10752 lnum = wp->w_height;
10753 else
10754 /* cursor line shows absolute line number */
10755 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010756
Bram Moolenaar6b314672015-03-20 15:42:10 +010010757 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010758 return wp->w_nrwidth_width;
10759 wp->w_nrwidth_line_count = lnum;
10760
10761 n = 0;
10762 do
10763 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010764 lnum /= 10;
10765 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010766 } while (lnum > 0);
10767
10768 /* 'numberwidth' gives the minimal width plus one */
10769 if (n < wp->w_p_nuw - 1)
10770 n = wp->w_p_nuw - 1;
10771
10772 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010773 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010774 return n;
10775}
10776#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010777
10778/*
10779 * Return the current cursor column. This is the actual position on the
10780 * screen. First column is 0.
10781 */
10782 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010783screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010784{
10785 return screen_cur_col;
10786}
10787
10788/*
10789 * Return the current cursor row. This is the actual position on the screen.
10790 * First row is 0.
10791 */
10792 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010793screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010794{
10795 return screen_cur_row;
10796}