blob: b580aee78a8e09ef6873bb205dcbf4cfb4298573 [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
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200425 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * Based on the current value of curwin->w_topline, transfer a screenfull
490 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
491 */
492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 win_T *wp;
496 static int did_intro = FALSE;
497#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
498 int did_one;
499#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200500#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200501 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200502 int gui_cursor_col;
503 int gui_cursor_row;
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000506 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (!screen_valid(TRUE))
508 return;
509
510 if (must_redraw)
511 {
512 if (type < must_redraw) /* use maximal type */
513 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000514
515 /* must_redraw is reset here, so that when we run into some weird
516 * reason to redraw while busy redrawing (e.g., asynchronous
517 * scrolling), or update_topline() in win_update() will cause a
518 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 must_redraw = 0;
520 }
521
522 /* Need to update w_lines[]. */
523 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
524 type = NOT_VALID;
525
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000526 /* Postpone the redrawing when it's not needed and when being called
527 * recursively. */
528 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 redraw_later(type); /* remember type for next time */
531 must_redraw = type;
532 if (type > INVERTED_ALL)
533 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
534 return;
535 }
536
537 updating_screen = TRUE;
538#ifdef FEAT_SYN_HL
539 ++display_tick; /* let syntax code know we're in a next round of
540 * display updating */
541#endif
542
543 /*
544 * if the screen was scrolled up when displaying a message, scroll it down
545 */
546 if (msg_scrolled)
547 {
548 clear_cmdline = TRUE;
549 if (msg_scrolled > Rows - 5) /* clearing is faster */
550 type = CLEAR;
551 else if (type != CLEAR)
552 {
553 check_for_delay(FALSE);
554 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
555 type = CLEAR;
556 FOR_ALL_WINDOWS(wp)
557 {
558 if (W_WINROW(wp) < msg_scrolled)
559 {
560 if (W_WINROW(wp) + wp->w_height > msg_scrolled
561 && wp->w_redr_type < REDRAW_TOP
562 && wp->w_lines_valid > 0
563 && wp->w_topline == wp->w_lines[0].wl_lnum)
564 {
565 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
566 wp->w_redr_type = REDRAW_TOP;
567 }
568 else
569 {
570 wp->w_redr_type = NOT_VALID;
571#ifdef FEAT_WINDOWS
572 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
573 <= msg_scrolled)
574 wp->w_redr_status = TRUE;
575#endif
576 }
577 }
578 }
579 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000580#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000581 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584 msg_scrolled = 0;
585 need_wait_return = FALSE;
586 }
587
588 /* reset cmdline_row now (may have been changed temporarily) */
589 compute_cmdrow();
590
591 /* Check for changed highlighting */
592 if (need_highlight_changed)
593 highlight_changed();
594
595 if (type == CLEAR) /* first clear screen */
596 {
597 screenclear(); /* will reset clear_cmdline */
598 type = NOT_VALID;
599 }
600
601 if (clear_cmdline) /* going to clear cmdline (done below) */
602 check_for_delay(FALSE);
603
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200605 /* Force redraw when width of 'number' or 'relativenumber' column
606 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000607 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200608 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
609 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000610 curwin->w_redr_type = NOT_VALID;
611#endif
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /*
614 * Only start redrawing if there is really something to do.
615 */
616 if (type == INVERTED)
617 update_curswant();
618 if (curwin->w_redr_type < type
619 && !((type == VALID
620 && curwin->w_lines[0].wl_valid
621#ifdef FEAT_DIFF
622 && curwin->w_topfill == curwin->w_old_topfill
623 && curwin->w_botfill == curwin->w_old_botfill
624#endif
625 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000627 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
629 && curwin->w_old_visual_mode == VIsual_mode
630 && (curwin->w_valid & VALID_VIRTCOL)
631 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ))
633 curwin->w_redr_type = type;
634
Bram Moolenaar5a305422006-04-28 22:38:25 +0000635#ifdef FEAT_WINDOWS
636 /* Redraw the tab pages line if needed. */
637 if (redraw_tabline || type >= NOT_VALID)
638 draw_tabline();
639#endif
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
642 /*
643 * Correct stored syntax highlighting info for changes in each displayed
644 * buffer. Each buffer must only be done once.
645 */
646 FOR_ALL_WINDOWS(wp)
647 {
648 if (wp->w_buffer->b_mod_set)
649 {
650# ifdef FEAT_WINDOWS
651 win_T *wwp;
652
653 /* Check if we already did this buffer. */
654 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
655 if (wwp->w_buffer == wp->w_buffer)
656 break;
657# endif
658 if (
659# ifdef FEAT_WINDOWS
660 wwp == wp &&
661# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200662 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 syn_stack_apply_changes(wp->w_buffer);
664 }
665 }
666#endif
667
668 /*
669 * Go from top to bottom through the windows, redrawing the ones that need
670 * it.
671 */
672#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
673 did_one = FALSE;
674#endif
675#ifdef FEAT_SEARCH_EXTRA
676 search_hl.rm.regprog = NULL;
677#endif
678 FOR_ALL_WINDOWS(wp)
679 {
680 if (wp->w_redr_type != 0)
681 {
682 cursor_off();
683#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
684 if (!did_one)
685 {
686 did_one = TRUE;
687# ifdef FEAT_SEARCH_EXTRA
688 start_search_hl();
689# endif
690# ifdef FEAT_CLIPBOARD
691 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200692 if (clip_star.available && clip_isautosel_star())
693 clip_update_selection(&clip_star);
694 if (clip_plus.available && clip_isautosel_plus())
695 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696# endif
697#ifdef FEAT_GUI
698 /* Remove the cursor before starting to do anything, because
699 * scrolling may make it difficult to redraw the text under
700 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200701 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200702 {
703 gui_cursor_col = gui.cursor_col;
704 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200706 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
709 }
710#endif
711 win_update(wp);
712 }
713
714#ifdef FEAT_WINDOWS
715 /* redraw status line after the window to minimize cursor movement */
716 if (wp->w_redr_status)
717 {
718 cursor_off();
719 win_redr_status(wp);
720 }
721#endif
722 }
723#if defined(FEAT_SEARCH_EXTRA)
724 end_search_hl();
725#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100726#ifdef FEAT_INS_EXPAND
727 /* May need to redraw the popup menu. */
728 if (pum_visible())
729 pum_redraw();
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732#ifdef FEAT_WINDOWS
733 /* Reset b_mod_set flags. Going through all windows is probably faster
734 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200735 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 wp->w_buffer->b_mod_set = FALSE;
737#else
738 curbuf->b_mod_set = FALSE;
739#endif
740
741 updating_screen = FALSE;
742#ifdef FEAT_GUI
743 gui_may_resize_shell();
744#endif
745
746 /* Clear or redraw the command line. Done last, because scrolling may
747 * mess up the command line. */
748 if (clear_cmdline || redraw_cmdline)
749 showmode();
750
751 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200752 if (!did_intro)
753 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 did_intro = TRUE;
755
756#ifdef FEAT_GUI
757 /* Redraw the cursor and update the scrollbars when all screen updating is
758 * done. */
759 if (gui.in_use)
760 {
761 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200762 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200763 {
764 /* Put the GUI position where the cursor was, gui_update_cursor()
765 * uses that. */
766 gui.col = gui_cursor_col;
767 gui.row = gui_cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200769 screen_cur_col = gui.col;
770 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 gui_update_scrollbars(FALSE);
773 }
774#endif
775}
776
Bram Moolenaar860cae12010-06-05 23:22:07 +0200777#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200778/*
779 * Return TRUE if the cursor line in window "wp" may be concealed, according
780 * to the 'concealcursor' option.
781 */
782 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100783conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200784{
785 int c;
786
787 if (*wp->w_p_cocu == NUL)
788 return FALSE;
789 if (get_real_state() & VISUAL)
790 c = 'v';
791 else if (State & INSERT)
792 c = 'i';
793 else if (State & NORMAL)
794 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200795 else if (State & CMDLINE)
796 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200797 else
798 return FALSE;
799 return vim_strchr(wp->w_p_cocu, c) != NULL;
800}
801
802/*
803 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
804 */
805 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100806conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200807{
808 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
809 {
810 need_cursor_line_redraw = TRUE;
811 /* Need to recompute cursor column, e.g., when starting Visual mode
812 * without concealing. */
813 curs_columns(TRUE);
814 }
815}
816
Bram Moolenaar860cae12010-06-05 23:22:07 +0200817 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100818update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200819{
820 int row;
821 int j;
822
Bram Moolenaar908be432016-05-24 10:51:30 +0200823 /* Don't do anything if the screen structures are (not yet) valid. */
824 if (!screen_valid(TRUE))
825 return;
826
Bram Moolenaar860cae12010-06-05 23:22:07 +0200827 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200828 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200829 {
830# ifdef FEAT_GUI
831 /* Remove the cursor before starting to do anything, because scrolling
832 * may make it difficult to redraw the text under it. */
833 if (gui.in_use)
834 gui_undraw_cursor();
835# endif
836 row = 0;
837 for (j = 0; j < wp->w_lines_valid; ++j)
838 {
839 if (lnum == wp->w_lines[j].wl_lnum)
840 {
841 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200842# ifdef FEAT_SEARCH_EXTRA
843 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200844 start_search_hl();
845 prepare_search_hl(wp, lnum);
846# endif
847 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
848# if defined(FEAT_SEARCH_EXTRA)
849 end_search_hl();
850# endif
851 break;
852 }
853 row += wp->w_lines[j].wl_size;
854 }
855# ifdef FEAT_GUI
856 /* Redraw the cursor */
857 if (gui.in_use)
858 {
859 out_flush(); /* required before updating the cursor */
860 gui_update_cursor(FALSE, FALSE);
861 }
862# endif
863 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200864 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200865}
866#endif
867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100869static void update_prepare(void);
870static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872/*
873 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000874 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 */
876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100877update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 cursor_off();
880 updating_screen = TRUE;
881#ifdef FEAT_GUI
882 /* Remove the cursor before starting to do anything, because scrolling may
883 * make it difficult to redraw the text under it. */
884 if (gui.in_use)
885 gui_undraw_cursor();
886#endif
887#ifdef FEAT_SEARCH_EXTRA
888 start_search_hl();
889#endif
890}
891
892/*
893 * Finish updating one or more windows.
894 */
895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100896update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897{
898 if (redraw_cmdline)
899 showmode();
900
901# ifdef FEAT_SEARCH_EXTRA
902 end_search_hl();
903# endif
904
905 updating_screen = FALSE;
906
907# ifdef FEAT_GUI
908 gui_may_resize_shell();
909
910 /* Redraw the cursor and update the scrollbars when all screen updating is
911 * done. */
912 if (gui.in_use)
913 {
914 out_flush(); /* required before updating the cursor */
915 gui_update_cursor(FALSE, FALSE);
916 gui_update_scrollbars(FALSE);
917 }
918# endif
919}
920#endif
921
922#if defined(FEAT_SIGNS) || defined(PROTO)
923 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100924update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
926 win_T *wp;
927 int doit = FALSE;
928
929# ifdef FEAT_FOLDING
930 win_foldinfo.fi_level = 0;
931# endif
932
933 /* update/delete a specific mark */
934 FOR_ALL_WINDOWS(wp)
935 {
936 if (buf != NULL && lnum > 0)
937 {
938 if (wp->w_buffer == buf && lnum >= wp->w_topline
939 && lnum < wp->w_botline)
940 {
941 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
942 wp->w_redraw_top = lnum;
943 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
944 wp->w_redraw_bot = lnum;
945 redraw_win_later(wp, VALID);
946 }
947 }
948 else
949 redraw_win_later(wp, VALID);
950 if (wp->w_redr_type != 0)
951 doit = TRUE;
952 }
953
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100954 /* Return when there is nothing to do, screen updating is already
955 * happening (recursive call) or still starting up. */
956 if (!doit || updating_screen
957#ifdef FEAT_GUI
958 || gui.starting
959#endif
960 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return;
962
963 /* update all windows that need updating */
964 update_prepare();
965
966# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +0200967 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 if (wp->w_redr_type != 0)
970 win_update(wp);
971 if (wp->w_redr_status)
972 win_redr_status(wp);
973 }
974# else
975 if (curwin->w_redr_type != 0)
976 win_update(curwin);
977# endif
978
979 update_finish();
980}
981#endif
982
983
984#if defined(FEAT_GUI) || defined(PROTO)
985/*
986 * Update a single window, its status line and maybe the command line msg.
987 * Used for the GUI scrollbar.
988 */
989 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100990updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000992 /* return if already busy updating */
993 if (updating_screen)
994 return;
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 update_prepare();
997
998#ifdef FEAT_CLIPBOARD
999 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001000 if (clip_star.available && clip_isautosel_star())
1001 clip_update_selection(&clip_star);
1002 if (clip_plus.available && clip_isautosel_plus())
1003 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001009 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001010 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001011 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (wp->w_redr_status
1014# ifdef FEAT_CMDL_INFO
1015 || p_ru
1016# endif
1017# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001018 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019# endif
1020 )
1021 win_redr_status(wp);
1022#endif
1023
1024 update_finish();
1025}
1026#endif
1027
1028/*
1029 * Update a single window.
1030 *
1031 * This may cause the windows below it also to be redrawn (when clearing the
1032 * screen or scrolling lines).
1033 *
1034 * How the window is redrawn depends on wp->w_redr_type. Each type also
1035 * implies the one below it.
1036 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001037 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1039 * INVERTED redraw the changed part of the Visual area
1040 * INVERTED_ALL redraw the whole Visual area
1041 * VALID 1. scroll up/down to adjust for a changed w_topline
1042 * 2. update lines at the top when scrolled down
1043 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001044 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 * b_mod_top and b_mod_bot.
1046 * - if wp->w_redraw_top non-zero, redraw lines between
1047 * wp->w_redraw_top and wp->w_redr_bot.
1048 * - continue redrawing when syntax status is invalid.
1049 * 4. if scrolled up, update lines at the bottom.
1050 * This results in three areas that may need updating:
1051 * top: from first row to top_end (when scrolled down)
1052 * mid: from mid_start to mid_end (update inversion or changed text)
1053 * bot: from bot_start to last row (when scrolled up)
1054 */
1055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001056win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 buf_T *buf = wp->w_buffer;
1059 int type;
1060 int top_end = 0; /* Below last row of the top area that needs
1061 updating. 0 when no top area updating. */
1062 int mid_start = 999;/* first row of the mid area that needs
1063 updating. 999 when no mid area updating. */
1064 int mid_end = 0; /* Below last row of the mid area that needs
1065 updating. 0 when no mid area updating. */
1066 int bot_start = 999;/* first row of the bot area that needs
1067 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 int scrolled_down = FALSE; /* TRUE when scrolled down when
1069 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001071 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 int top_to_mod = FALSE; /* redraw above mod_top */
1073#endif
1074
1075 int row; /* current window row to display */
1076 linenr_T lnum; /* current buffer lnum to display */
1077 int idx; /* current index in w_lines[] */
1078 int srow; /* starting row of the current line */
1079
1080 int eof = FALSE; /* if TRUE, we hit the end of the file */
1081 int didline = FALSE; /* if TRUE, we finished the last line */
1082 int i;
1083 long j;
1084 static int recursive = FALSE; /* being called recursively */
1085 int old_botline = wp->w_botline;
1086#ifdef FEAT_FOLDING
1087 long fold_count;
1088#endif
1089#ifdef FEAT_SYN_HL
1090 /* remember what happened to the previous line, to know if
1091 * check_visual_highlight() can be used */
1092#define DID_NONE 1 /* didn't update a line */
1093#define DID_LINE 2 /* updated a normal line */
1094#define DID_FOLD 3 /* updated a folded line */
1095 int did_update = DID_NONE;
1096 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1097#endif
1098 linenr_T mod_top = 0;
1099 linenr_T mod_bot = 0;
1100#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1101 int save_got_int;
1102#endif
1103
1104 type = wp->w_redr_type;
1105
1106 if (type == NOT_VALID)
1107 {
1108#ifdef FEAT_WINDOWS
1109 wp->w_redr_status = TRUE;
1110#endif
1111 wp->w_lines_valid = 0;
1112 }
1113
1114 /* Window is zero-height: nothing to draw. */
1115 if (wp->w_height == 0)
1116 {
1117 wp->w_redr_type = 0;
1118 return;
1119 }
1120
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001121#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 /* Window is zero-width: Only need to draw the separator. */
1123 if (wp->w_width == 0)
1124 {
1125 /* draw the vertical separator right of this window */
1126 draw_vsep_win(wp, 0);
1127 wp->w_redr_type = 0;
1128 return;
1129 }
1130#endif
1131
1132#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001133 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001136#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001137 /* Force redraw when width of 'number' or 'relativenumber' column
1138 * changes. */
1139 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001140 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001141 {
1142 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001143 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001144 }
1145 else
1146#endif
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1149 {
1150 /*
1151 * When there are both inserted/deleted lines and specific lines to be
1152 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1153 * everything (only happens when redrawing is off for while).
1154 */
1155 type = NOT_VALID;
1156 }
1157 else
1158 {
1159 /*
1160 * Set mod_top to the first line that needs displaying because of
1161 * changes. Set mod_bot to the first line after the changes.
1162 */
1163 mod_top = wp->w_redraw_top;
1164 if (wp->w_redraw_bot != 0)
1165 mod_bot = wp->w_redraw_bot + 1;
1166 else
1167 mod_bot = 0;
1168 wp->w_redraw_top = 0; /* reset for next time */
1169 wp->w_redraw_bot = 0;
1170 if (buf->b_mod_set)
1171 {
1172 if (mod_top == 0 || mod_top > buf->b_mod_top)
1173 {
1174 mod_top = buf->b_mod_top;
1175#ifdef FEAT_SYN_HL
1176 /* Need to redraw lines above the change that may be included
1177 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (mod_top < 1)
1182 mod_top = 1;
1183 }
1184#endif
1185 }
1186 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1187 mod_bot = buf->b_mod_bot;
1188
1189#ifdef FEAT_SEARCH_EXTRA
1190 /* When 'hlsearch' is on and using a multi-line search pattern, a
1191 * change in one line may make the Search highlighting in a
1192 * previous line invalid. Simple solution: redraw all visible
1193 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001194 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001196 if (search_hl.rm.regprog != NULL
1197 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001199 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001200 {
1201 cur = wp->w_match_head;
1202 while (cur != NULL)
1203 {
1204 if (cur->match.regprog != NULL
1205 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001206 {
1207 top_to_mod = TRUE;
1208 break;
1209 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001210 cur = cur->next;
1211 }
1212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
1214 }
1215#ifdef FEAT_FOLDING
1216 if (mod_top != 0 && hasAnyFolding(wp))
1217 {
1218 linenr_T lnumt, lnumb;
1219
1220 /*
1221 * A change in a line can cause lines above it to become folded or
1222 * unfolded. Find the top most buffer line that may be affected.
1223 * If the line was previously folded and displayed, get the first
1224 * line of that fold. If the line is folded now, get the first
1225 * folded line. Use the minimum of these two.
1226 */
1227
1228 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1229 * the line below it. If there is no valid entry, use w_topline.
1230 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1231 * to this line. If there is no valid entry, use MAXLNUM. */
1232 lnumt = wp->w_topline;
1233 lnumb = MAXLNUM;
1234 for (i = 0; i < wp->w_lines_valid; ++i)
1235 if (wp->w_lines[i].wl_valid)
1236 {
1237 if (wp->w_lines[i].wl_lastlnum < mod_top)
1238 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1239 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1240 {
1241 lnumb = wp->w_lines[i].wl_lnum;
1242 /* When there is a fold column it might need updating
1243 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001244 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ++lnumb;
1246 }
1247 }
1248
1249 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1250 if (mod_top > lnumt)
1251 mod_top = lnumt;
1252
1253 /* Now do the same for the bottom line (one above mod_bot). */
1254 --mod_bot;
1255 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1256 ++mod_bot;
1257 if (mod_bot < lnumb)
1258 mod_bot = lnumb;
1259 }
1260#endif
1261
1262 /* When a change starts above w_topline and the end is below
1263 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001264 * If the end of the change is above w_topline: do like no change was
1265 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (mod_top != 0 && mod_top < wp->w_topline)
1267 {
1268 if (mod_bot > wp->w_topline)
1269 mod_top = wp->w_topline;
1270#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001271 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 top_end = 1;
1273#endif
1274 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001275
1276 /* When line numbers are displayed need to redraw all lines below
1277 * inserted/deleted lines. */
1278 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1279 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281
1282 /*
1283 * When only displaying the lines at the top, set top_end. Used when
1284 * window has scrolled down for msg_scrolled.
1285 */
1286 if (type == REDRAW_TOP)
1287 {
1288 j = 0;
1289 for (i = 0; i < wp->w_lines_valid; ++i)
1290 {
1291 j += wp->w_lines[i].wl_size;
1292 if (j >= wp->w_upd_rows)
1293 {
1294 top_end = j;
1295 break;
1296 }
1297 }
1298 if (top_end == 0)
1299 /* not found (cannot happen?): redraw everything */
1300 type = NOT_VALID;
1301 else
1302 /* top area defined, the rest is VALID */
1303 type = VALID;
1304 }
1305
Bram Moolenaar367329b2007-08-30 11:53:22 +00001306 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001307 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1308 * non-zero and thus not FALSE) will indicate that screenclear() was not
1309 * called. */
1310 if (screen_cleared)
1311 screen_cleared = MAYBE;
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 /*
1314 * If there are no changes on the screen that require a complete redraw,
1315 * handle three cases:
1316 * 1: we are off the top of the screen by a few lines: scroll down
1317 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1318 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1319 * w_lines[] that needs updating.
1320 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001321 if ((type == VALID || type == SOME_VALID
1322 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef FEAT_DIFF
1324 && !wp->w_botfill && !wp->w_old_botfill
1325#endif
1326 )
1327 {
1328 if (mod_top != 0 && wp->w_topline == mod_top)
1329 {
1330 /*
1331 * w_topline is the first changed line, the scrolling will be done
1332 * further down.
1333 */
1334 }
1335 else if (wp->w_lines[0].wl_valid
1336 && (wp->w_topline < wp->w_lines[0].wl_lnum
1337#ifdef FEAT_DIFF
1338 || (wp->w_topline == wp->w_lines[0].wl_lnum
1339 && wp->w_topfill > wp->w_old_topfill)
1340#endif
1341 ))
1342 {
1343 /*
1344 * New topline is above old topline: May scroll down.
1345 */
1346#ifdef FEAT_FOLDING
1347 if (hasAnyFolding(wp))
1348 {
1349 linenr_T ln;
1350
1351 /* count the number of lines we are off, counting a sequence
1352 * of folded lines as one */
1353 j = 0;
1354 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1355 {
1356 ++j;
1357 if (j >= wp->w_height - 2)
1358 break;
1359 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1360 }
1361 }
1362 else
1363#endif
1364 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1365 if (j < wp->w_height - 2) /* not too far off */
1366 {
1367 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1368#ifdef FEAT_DIFF
1369 /* insert extra lines for previously invisible filler lines */
1370 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1371 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1372 - wp->w_old_topfill;
1373#endif
1374 if (i < wp->w_height - 2) /* less than a screen off */
1375 {
1376 /*
1377 * Try to insert the correct number of lines.
1378 * If not the last window, delete the lines at the bottom.
1379 * win_ins_lines may fail when the terminal can't do it.
1380 */
1381 if (i > 0)
1382 check_for_delay(FALSE);
1383 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1384 {
1385 if (wp->w_lines_valid != 0)
1386 {
1387 /* Need to update rows that are new, stop at the
1388 * first one that scrolled down. */
1389 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 /* Move the entries that were scrolled, disable
1393 * the entries for the lines to be redrawn. */
1394 if ((wp->w_lines_valid += j) > wp->w_height)
1395 wp->w_lines_valid = wp->w_height;
1396 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1397 wp->w_lines[idx] = wp->w_lines[idx - j];
1398 while (idx >= 0)
1399 wp->w_lines[idx--].wl_valid = FALSE;
1400 }
1401 }
1402 else
1403 mid_start = 0; /* redraw all lines */
1404 }
1405 else
1406 mid_start = 0; /* redraw all lines */
1407 }
1408 else
1409 mid_start = 0; /* redraw all lines */
1410 }
1411 else
1412 {
1413 /*
1414 * New topline is at or below old topline: May scroll up.
1415 * When topline didn't change, find first entry in w_lines[] that
1416 * needs updating.
1417 */
1418
1419 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1420 j = -1;
1421 row = 0;
1422 for (i = 0; i < wp->w_lines_valid; i++)
1423 {
1424 if (wp->w_lines[i].wl_valid
1425 && wp->w_lines[i].wl_lnum == wp->w_topline)
1426 {
1427 j = i;
1428 break;
1429 }
1430 row += wp->w_lines[i].wl_size;
1431 }
1432 if (j == -1)
1433 {
1434 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1435 * lines */
1436 mid_start = 0;
1437 }
1438 else
1439 {
1440 /*
1441 * Try to delete the correct number of lines.
1442 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1443 */
1444#ifdef FEAT_DIFF
1445 /* If the topline didn't change, delete old filler lines,
1446 * otherwise delete filler lines of the new topline... */
1447 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1448 row += wp->w_old_topfill;
1449 else
1450 row += diff_check_fill(wp, wp->w_topline);
1451 /* ... but don't delete new filler lines. */
1452 row -= wp->w_topfill;
1453#endif
1454 if (row > 0)
1455 {
1456 check_for_delay(FALSE);
1457 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1458 bot_start = wp->w_height - row;
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1463 {
1464 /*
1465 * Skip the lines (below the deleted lines) that are still
1466 * valid and don't need redrawing. Copy their info
1467 * upwards, to compensate for the deleted lines. Set
1468 * bot_start to the first row that needs redrawing.
1469 */
1470 bot_start = 0;
1471 idx = 0;
1472 for (;;)
1473 {
1474 wp->w_lines[idx] = wp->w_lines[j];
1475 /* stop at line that didn't fit, unless it is still
1476 * valid (no lines deleted) */
1477 if (row > 0 && bot_start + row
1478 + (int)wp->w_lines[j].wl_size > wp->w_height)
1479 {
1480 wp->w_lines_valid = idx + 1;
1481 break;
1482 }
1483 bot_start += wp->w_lines[idx++].wl_size;
1484
1485 /* stop at the last valid entry in w_lines[].wl_size */
1486 if (++j >= wp->w_lines_valid)
1487 {
1488 wp->w_lines_valid = idx;
1489 break;
1490 }
1491 }
1492#ifdef FEAT_DIFF
1493 /* Correct the first entry for filler lines at the top
1494 * when it won't get updated below. */
1495 if (wp->w_p_diff && bot_start > 0)
1496 wp->w_lines[0].wl_size =
1497 plines_win_nofill(wp, wp->w_topline, TRUE)
1498 + wp->w_topfill;
1499#endif
1500 }
1501 }
1502 }
1503
1504 /* When starting redraw in the first line, redraw all lines. When
1505 * there is only one window it's probably faster to clear the screen
1506 * first. */
1507 if (mid_start == 0)
1508 {
1509 mid_end = wp->w_height;
1510 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001511 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001512 /* Clear the screen when it was not done by win_del_lines() or
1513 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1514 * then. */
1515 if (screen_cleared != TRUE)
1516 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001517#ifdef FEAT_WINDOWS
1518 /* The screen was cleared, redraw the tab pages line. */
1519 if (redraw_tabline)
1520 draw_tabline();
1521#endif
1522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001524
1525 /* When win_del_lines() or win_ins_lines() caused the screen to be
1526 * cleared (only happens for the first window) or when screenclear()
1527 * was called directly above, "must_redraw" will have been set to
1528 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1529 if (screen_cleared == TRUE)
1530 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 else
1533 {
1534 /* Not VALID or INVERTED: redraw all lines. */
1535 mid_start = 0;
1536 mid_end = wp->w_height;
1537 }
1538
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001539 if (type == SOME_VALID)
1540 {
1541 /* SOME_VALID: redraw all lines. */
1542 mid_start = 0;
1543 mid_end = wp->w_height;
1544 type = NOT_VALID;
1545 }
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /* check if we are updating or removing the inverted part */
1548 if ((VIsual_active && buf == curwin->w_buffer)
1549 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1550 {
1551 linenr_T from, to;
1552
1553 if (VIsual_active)
1554 {
1555 if (VIsual_active
1556 && (VIsual_mode != wp->w_old_visual_mode
1557 || type == INVERTED_ALL))
1558 {
1559 /*
1560 * If the type of Visual selection changed, redraw the whole
1561 * selection. Also when the ownership of the X selection is
1562 * gained or lost.
1563 */
1564 if (curwin->w_cursor.lnum < VIsual.lnum)
1565 {
1566 from = curwin->w_cursor.lnum;
1567 to = VIsual.lnum;
1568 }
1569 else
1570 {
1571 from = VIsual.lnum;
1572 to = curwin->w_cursor.lnum;
1573 }
1574 /* redraw more when the cursor moved as well */
1575 if (wp->w_old_cursor_lnum < from)
1576 from = wp->w_old_cursor_lnum;
1577 if (wp->w_old_cursor_lnum > to)
1578 to = wp->w_old_cursor_lnum;
1579 if (wp->w_old_visual_lnum < from)
1580 from = wp->w_old_visual_lnum;
1581 if (wp->w_old_visual_lnum > to)
1582 to = wp->w_old_visual_lnum;
1583 }
1584 else
1585 {
1586 /*
1587 * Find the line numbers that need to be updated: The lines
1588 * between the old cursor position and the current cursor
1589 * position. Also check if the Visual position changed.
1590 */
1591 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1592 {
1593 from = curwin->w_cursor.lnum;
1594 to = wp->w_old_cursor_lnum;
1595 }
1596 else
1597 {
1598 from = wp->w_old_cursor_lnum;
1599 to = curwin->w_cursor.lnum;
1600 if (from == 0) /* Visual mode just started */
1601 from = to;
1602 }
1603
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001604 if (VIsual.lnum != wp->w_old_visual_lnum
1605 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 if (wp->w_old_visual_lnum < from
1608 && wp->w_old_visual_lnum != 0)
1609 from = wp->w_old_visual_lnum;
1610 if (wp->w_old_visual_lnum > to)
1611 to = wp->w_old_visual_lnum;
1612 if (VIsual.lnum < from)
1613 from = VIsual.lnum;
1614 if (VIsual.lnum > to)
1615 to = VIsual.lnum;
1616 }
1617 }
1618
1619 /*
1620 * If in block mode and changed column or curwin->w_curswant:
1621 * update all lines.
1622 * First compute the actual start and end column.
1623 */
1624 if (VIsual_mode == Ctrl_V)
1625 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001626 colnr_T fromc, toc;
1627#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1628 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaar404406a2014-10-09 13:24:43 +02001630 if (curwin->w_p_lbr)
1631 ve_flags = VE_ALL;
1632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001634#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1635 ve_flags = save_ve_flags;
1636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 ++toc;
1638 if (curwin->w_curswant == MAXCOL)
1639 toc = MAXCOL;
1640
1641 if (fromc != wp->w_old_cursor_fcol
1642 || toc != wp->w_old_cursor_lcol)
1643 {
1644 if (from > VIsual.lnum)
1645 from = VIsual.lnum;
1646 if (to < VIsual.lnum)
1647 to = VIsual.lnum;
1648 }
1649 wp->w_old_cursor_fcol = fromc;
1650 wp->w_old_cursor_lcol = toc;
1651 }
1652 }
1653 else
1654 {
1655 /* Use the line numbers of the old Visual area. */
1656 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1657 {
1658 from = wp->w_old_cursor_lnum;
1659 to = wp->w_old_visual_lnum;
1660 }
1661 else
1662 {
1663 from = wp->w_old_visual_lnum;
1664 to = wp->w_old_cursor_lnum;
1665 }
1666 }
1667
1668 /*
1669 * There is no need to update lines above the top of the window.
1670 */
1671 if (from < wp->w_topline)
1672 from = wp->w_topline;
1673
1674 /*
1675 * If we know the value of w_botline, use it to restrict the update to
1676 * the lines that are visible in the window.
1677 */
1678 if (wp->w_valid & VALID_BOTLINE)
1679 {
1680 if (from >= wp->w_botline)
1681 from = wp->w_botline - 1;
1682 if (to >= wp->w_botline)
1683 to = wp->w_botline - 1;
1684 }
1685
1686 /*
1687 * Find the minimal part to be updated.
1688 * Watch out for scrolling that made entries in w_lines[] invalid.
1689 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1690 * top_end; need to redraw from top_end to the "to" line.
1691 * A middle mouse click with a Visual selection may change the text
1692 * above the Visual area and reset wl_valid, do count these for
1693 * mid_end (in srow).
1694 */
1695 if (mid_start > 0)
1696 {
1697 lnum = wp->w_topline;
1698 idx = 0;
1699 srow = 0;
1700 if (scrolled_down)
1701 mid_start = top_end;
1702 else
1703 mid_start = 0;
1704 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1705 {
1706 if (wp->w_lines[idx].wl_valid)
1707 mid_start += wp->w_lines[idx].wl_size;
1708 else if (!scrolled_down)
1709 srow += wp->w_lines[idx].wl_size;
1710 ++idx;
1711# ifdef FEAT_FOLDING
1712 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1713 lnum = wp->w_lines[idx].wl_lnum;
1714 else
1715# endif
1716 ++lnum;
1717 }
1718 srow += mid_start;
1719 mid_end = wp->w_height;
1720 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1721 {
1722 if (wp->w_lines[idx].wl_valid
1723 && wp->w_lines[idx].wl_lnum >= to + 1)
1724 {
1725 /* Only update until first row of this line */
1726 mid_end = srow;
1727 break;
1728 }
1729 srow += wp->w_lines[idx].wl_size;
1730 }
1731 }
1732 }
1733
1734 if (VIsual_active && buf == curwin->w_buffer)
1735 {
1736 wp->w_old_visual_mode = VIsual_mode;
1737 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1738 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001739 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 wp->w_old_curswant = curwin->w_curswant;
1741 }
1742 else
1743 {
1744 wp->w_old_visual_mode = 0;
1745 wp->w_old_cursor_lnum = 0;
1746 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001747 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1751 /* reset got_int, otherwise regexp won't work */
1752 save_got_int = got_int;
1753 got_int = 0;
1754#endif
1755#ifdef FEAT_FOLDING
1756 win_foldinfo.fi_level = 0;
1757#endif
1758
1759 /*
1760 * Update all the window rows.
1761 */
1762 idx = 0; /* first entry in w_lines[].wl_size */
1763 row = 0;
1764 srow = 0;
1765 lnum = wp->w_topline; /* first line shown in window */
1766 for (;;)
1767 {
1768 /* stop updating when reached the end of the window (check for _past_
1769 * the end of the window is at the end of the loop) */
1770 if (row == wp->w_height)
1771 {
1772 didline = TRUE;
1773 break;
1774 }
1775
1776 /* stop updating when hit the end of the file */
1777 if (lnum > buf->b_ml.ml_line_count)
1778 {
1779 eof = TRUE;
1780 break;
1781 }
1782
1783 /* Remember the starting row of the line that is going to be dealt
1784 * with. It is used further down when the line doesn't fit. */
1785 srow = row;
1786
1787 /*
1788 * Update a line when it is in an area that needs updating, when it
1789 * has changes or w_lines[idx] is invalid.
1790 * bot_start may be halfway a wrapped line after using
1791 * win_del_lines(), check if the current line includes it.
1792 * When syntax folding is being used, the saved syntax states will
1793 * already have been updated, we can't see where the syntax state is
1794 * the same again, just update until the end of the window.
1795 */
1796 if (row < top_end
1797 || (row >= mid_start && row < mid_end)
1798#ifdef FEAT_SEARCH_EXTRA
1799 || top_to_mod
1800#endif
1801 || idx >= wp->w_lines_valid
1802 || (row + wp->w_lines[idx].wl_size > bot_start)
1803 || (mod_top != 0
1804 && (lnum == mod_top
1805 || (lnum >= mod_top
1806 && (lnum < mod_bot
1807#ifdef FEAT_SYN_HL
1808 || did_update == DID_FOLD
1809 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001810 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 && (
1812# ifdef FEAT_FOLDING
1813 (foldmethodIsSyntax(wp)
1814 && hasAnyFolding(wp)) ||
1815# endif
1816 syntax_check_changed(lnum)))
1817#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001818#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001819 /* match in fixed position might need redraw
1820 * if lines were inserted or deleted */
1821 || (wp->w_match_head != NULL
1822 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 )))))
1825 {
1826#ifdef FEAT_SEARCH_EXTRA
1827 if (lnum == mod_top)
1828 top_to_mod = FALSE;
1829#endif
1830
1831 /*
1832 * When at start of changed lines: May scroll following lines
1833 * up or down to minimize redrawing.
1834 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001835 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 if (lnum == mod_top
1838 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001839 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 int old_rows = 0;
1842 int new_rows = 0;
1843 int xtra_rows;
1844 linenr_T l;
1845
1846 /* Count the old number of window rows, using w_lines[], which
1847 * should still contain the sizes for the lines as they are
1848 * currently displayed. */
1849 for (i = idx; i < wp->w_lines_valid; ++i)
1850 {
1851 /* Only valid lines have a meaningful wl_lnum. Invalid
1852 * lines are part of the changed area. */
1853 if (wp->w_lines[i].wl_valid
1854 && wp->w_lines[i].wl_lnum == mod_bot)
1855 break;
1856 old_rows += wp->w_lines[i].wl_size;
1857#ifdef FEAT_FOLDING
1858 if (wp->w_lines[i].wl_valid
1859 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1860 {
1861 /* Must have found the last valid entry above mod_bot.
1862 * Add following invalid entries. */
1863 ++i;
1864 while (i < wp->w_lines_valid
1865 && !wp->w_lines[i].wl_valid)
1866 old_rows += wp->w_lines[i++].wl_size;
1867 break;
1868 }
1869#endif
1870 }
1871
1872 if (i >= wp->w_lines_valid)
1873 {
1874 /* We can't find a valid line below the changed lines,
1875 * need to redraw until the end of the window.
1876 * Inserting/deleting lines has no use. */
1877 bot_start = 0;
1878 }
1879 else
1880 {
1881 /* Able to count old number of rows: Count new window
1882 * rows, and may insert/delete lines */
1883 j = idx;
1884 for (l = lnum; l < mod_bot; ++l)
1885 {
1886#ifdef FEAT_FOLDING
1887 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1888 ++new_rows;
1889 else
1890#endif
1891#ifdef FEAT_DIFF
1892 if (l == wp->w_topline)
1893 new_rows += plines_win_nofill(wp, l, TRUE)
1894 + wp->w_topfill;
1895 else
1896#endif
1897 new_rows += plines_win(wp, l, TRUE);
1898 ++j;
1899 if (new_rows > wp->w_height - row - 2)
1900 {
1901 /* it's getting too much, must redraw the rest */
1902 new_rows = 9999;
1903 break;
1904 }
1905 }
1906 xtra_rows = new_rows - old_rows;
1907 if (xtra_rows < 0)
1908 {
1909 /* May scroll text up. If there is not enough
1910 * remaining text or scrolling fails, must redraw the
1911 * rest. If scrolling works, must redraw the text
1912 * below the scrolled text. */
1913 if (row - xtra_rows >= wp->w_height - 2)
1914 mod_bot = MAXLNUM;
1915 else
1916 {
1917 check_for_delay(FALSE);
1918 if (win_del_lines(wp, row,
1919 -xtra_rows, FALSE, FALSE) == FAIL)
1920 mod_bot = MAXLNUM;
1921 else
1922 bot_start = wp->w_height + xtra_rows;
1923 }
1924 }
1925 else if (xtra_rows > 0)
1926 {
1927 /* May scroll text down. If there is not enough
1928 * remaining text of scrolling fails, must redraw the
1929 * rest. */
1930 if (row + xtra_rows >= wp->w_height - 2)
1931 mod_bot = MAXLNUM;
1932 else
1933 {
1934 check_for_delay(FALSE);
1935 if (win_ins_lines(wp, row + old_rows,
1936 xtra_rows, FALSE, FALSE) == FAIL)
1937 mod_bot = MAXLNUM;
1938 else if (top_end > row + old_rows)
1939 /* Scrolled the part at the top that requires
1940 * updating down. */
1941 top_end += xtra_rows;
1942 }
1943 }
1944
1945 /* When not updating the rest, may need to move w_lines[]
1946 * entries. */
1947 if (mod_bot != MAXLNUM && i != j)
1948 {
1949 if (j < i)
1950 {
1951 int x = row + new_rows;
1952
1953 /* move entries in w_lines[] upwards */
1954 for (;;)
1955 {
1956 /* stop at last valid entry in w_lines[] */
1957 if (i >= wp->w_lines_valid)
1958 {
1959 wp->w_lines_valid = j;
1960 break;
1961 }
1962 wp->w_lines[j] = wp->w_lines[i];
1963 /* stop at a line that won't fit */
1964 if (x + (int)wp->w_lines[j].wl_size
1965 > wp->w_height)
1966 {
1967 wp->w_lines_valid = j + 1;
1968 break;
1969 }
1970 x += wp->w_lines[j++].wl_size;
1971 ++i;
1972 }
1973 if (bot_start > x)
1974 bot_start = x;
1975 }
1976 else /* j > i */
1977 {
1978 /* move entries in w_lines[] downwards */
1979 j -= i;
1980 wp->w_lines_valid += j;
1981 if (wp->w_lines_valid > wp->w_height)
1982 wp->w_lines_valid = wp->w_height;
1983 for (i = wp->w_lines_valid; i - j >= idx; --i)
1984 wp->w_lines[i] = wp->w_lines[i - j];
1985
1986 /* The w_lines[] entries for inserted lines are
1987 * now invalid, but wl_size may be used above.
1988 * Reset to zero. */
1989 while (i >= idx)
1990 {
1991 wp->w_lines[i].wl_size = 0;
1992 wp->w_lines[i--].wl_valid = FALSE;
1993 }
1994 }
1995 }
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998
1999#ifdef FEAT_FOLDING
2000 /*
2001 * When lines are folded, display one line for all of them.
2002 * Otherwise, display normally (can be several display lines when
2003 * 'wrap' is on).
2004 */
2005 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2006 if (fold_count != 0)
2007 {
2008 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2009 ++row;
2010 --fold_count;
2011 wp->w_lines[idx].wl_folded = TRUE;
2012 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2013# ifdef FEAT_SYN_HL
2014 did_update = DID_FOLD;
2015# endif
2016 }
2017 else
2018#endif
2019 if (idx < wp->w_lines_valid
2020 && wp->w_lines[idx].wl_valid
2021 && wp->w_lines[idx].wl_lnum == lnum
2022 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002023 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 && srow + wp->w_lines[idx].wl_size > wp->w_height
2025#ifdef FEAT_DIFF
2026 && diff_check_fill(wp, lnum) == 0
2027#endif
2028 )
2029 {
2030 /* This line is not going to fit. Don't draw anything here,
2031 * will draw "@ " lines below. */
2032 row = wp->w_height + 1;
2033 }
2034 else
2035 {
2036#ifdef FEAT_SEARCH_EXTRA
2037 prepare_search_hl(wp, lnum);
2038#endif
2039#ifdef FEAT_SYN_HL
2040 /* Let the syntax stuff know we skipped a few lines. */
2041 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002042 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 syntax_end_parsing(syntax_last_parsed + 1);
2044#endif
2045
2046 /*
2047 * Display one line.
2048 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002049 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051#ifdef FEAT_FOLDING
2052 wp->w_lines[idx].wl_folded = FALSE;
2053 wp->w_lines[idx].wl_lastlnum = lnum;
2054#endif
2055#ifdef FEAT_SYN_HL
2056 did_update = DID_LINE;
2057 syntax_last_parsed = lnum;
2058#endif
2059 }
2060
2061 wp->w_lines[idx].wl_lnum = lnum;
2062 wp->w_lines[idx].wl_valid = TRUE;
2063 if (row > wp->w_height) /* past end of screen */
2064 {
2065 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002066 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2068 ++idx;
2069 break;
2070 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002071 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 wp->w_lines[idx].wl_size = row - srow;
2073 ++idx;
2074#ifdef FEAT_FOLDING
2075 lnum += fold_count + 1;
2076#else
2077 ++lnum;
2078#endif
2079 }
2080 else
2081 {
2082 /* This line does not need updating, advance to the next one */
2083 row += wp->w_lines[idx++].wl_size;
2084 if (row > wp->w_height) /* past end of screen */
2085 break;
2086#ifdef FEAT_FOLDING
2087 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2088#else
2089 ++lnum;
2090#endif
2091#ifdef FEAT_SYN_HL
2092 did_update = DID_NONE;
2093#endif
2094 }
2095
2096 if (lnum > buf->b_ml.ml_line_count)
2097 {
2098 eof = TRUE;
2099 break;
2100 }
2101 }
2102 /*
2103 * End of loop over all window lines.
2104 */
2105
2106
2107 if (idx > wp->w_lines_valid)
2108 wp->w_lines_valid = idx;
2109
2110#ifdef FEAT_SYN_HL
2111 /*
2112 * Let the syntax stuff know we stop parsing here.
2113 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002114 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 syntax_end_parsing(syntax_last_parsed + 1);
2116#endif
2117
2118 /*
2119 * If we didn't hit the end of the file, and we didn't finish the last
2120 * line we were working on, then the line didn't fit.
2121 */
2122 wp->w_empty_rows = 0;
2123#ifdef FEAT_DIFF
2124 wp->w_filler_rows = 0;
2125#endif
2126 if (!eof && !didline)
2127 {
2128 if (lnum == wp->w_topline)
2129 {
2130 /*
2131 * Single line that does not fit!
2132 * Don't overwrite it, it can be edited.
2133 */
2134 wp->w_botline = lnum + 1;
2135 }
2136#ifdef FEAT_DIFF
2137 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2138 {
2139 /* Window ends in filler lines. */
2140 wp->w_botline = lnum;
2141 wp->w_filler_rows = wp->w_height - srow;
2142 }
2143#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002144 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2145 {
2146 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2147
2148 /*
2149 * Last line isn't finished: Display "@@@" in the last screen line.
2150 */
2151 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
2152 hl_attr(HLF_AT));
2153 screen_fill(scr_row, scr_row + 1,
2154 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
2155 '@', ' ', hl_attr(HLF_AT));
2156 set_empty_rows(wp, srow);
2157 wp->w_botline = lnum;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2160 {
2161 /*
2162 * Last line isn't finished: Display "@@@" at the end.
2163 */
2164 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2165 W_WINROW(wp) + wp->w_height,
2166 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2167 '@', '@', hl_attr(HLF_AT));
2168 set_empty_rows(wp, srow);
2169 wp->w_botline = lnum;
2170 }
2171 else
2172 {
2173 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2174 wp->w_botline = lnum;
2175 }
2176 }
2177 else
2178 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002179#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 draw_vsep_win(wp, row);
2181#endif
2182 if (eof) /* we hit the end of the file */
2183 {
2184 wp->w_botline = buf->b_ml.ml_line_count + 1;
2185#ifdef FEAT_DIFF
2186 j = diff_check_fill(wp, wp->w_botline);
2187 if (j > 0 && !wp->w_botfill)
2188 {
2189 /*
2190 * Display filler lines at the end of the file
2191 */
2192 if (char2cells(fill_diff) > 1)
2193 i = '-';
2194 else
2195 i = fill_diff;
2196 if (row + j > wp->w_height)
2197 j = wp->w_height - row;
2198 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2199 row += j;
2200 }
2201#endif
2202 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002203 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 wp->w_botline = lnum;
2205
2206 /* make sure the rest of the screen is blank */
2207 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002208 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210
2211 /* Reset the type of redrawing required, the window has been updated. */
2212 wp->w_redr_type = 0;
2213#ifdef FEAT_DIFF
2214 wp->w_old_topfill = wp->w_topfill;
2215 wp->w_old_botfill = wp->w_botfill;
2216#endif
2217
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002218 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 /*
2221 * There is a trick with w_botline. If we invalidate it on each
2222 * change that might modify it, this will cause a lot of expensive
2223 * calls to plines() in update_topline() each time. Therefore the
2224 * value of w_botline is often approximated, and this value is used to
2225 * compute the value of w_topline. If the value of w_botline was
2226 * wrong, check that the value of w_topline is correct (cursor is on
2227 * the visible part of the text). If it's not, we need to redraw
2228 * again. Mostly this just means scrolling up a few lines, so it
2229 * doesn't look too bad. Only do this for the current window (where
2230 * changes are relevant).
2231 */
2232 wp->w_valid |= VALID_BOTLINE;
2233 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2234 {
2235 recursive = TRUE;
2236 curwin->w_valid &= ~VALID_TOPLINE;
2237 update_topline(); /* may invalidate w_botline again */
2238 if (must_redraw != 0)
2239 {
2240 /* Don't update for changes in buffer again. */
2241 i = curbuf->b_mod_set;
2242 curbuf->b_mod_set = FALSE;
2243 win_update(curwin);
2244 must_redraw = 0;
2245 curbuf->b_mod_set = i;
2246 }
2247 recursive = FALSE;
2248 }
2249 }
2250
2251#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2252 /* restore got_int, unless CTRL-C was hit while redrawing */
2253 if (!got_int)
2254 got_int = save_got_int;
2255#endif
2256}
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258/*
2259 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2260 * as the filler character.
2261 */
2262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002263win_draw_end(
2264 win_T *wp,
2265 int c1,
2266 int c2,
2267 int row,
2268 int endrow,
2269 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
2271#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2272 int n = 0;
2273# define FDC_OFF n
2274#else
2275# define FDC_OFF 0
2276#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002277#ifdef FEAT_FOLDING
2278 int fdc = compute_foldcolumn(wp, 0);
2279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280
2281#ifdef FEAT_RIGHTLEFT
2282 if (wp->w_p_rl)
2283 {
2284 /* No check for cmdline window: should never be right-left. */
2285# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002286 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 if (n > 0)
2289 {
2290 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002291 if (n > W_WIDTH(wp))
2292 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2294 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2295 ' ', ' ', hl_attr(HLF_FC));
2296 }
2297# endif
2298# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002299 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 int nn = n + 2;
2302
2303 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002304 if (nn > W_WIDTH(wp))
2305 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2307 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2308 ' ', ' ', hl_attr(HLF_SC));
2309 n = nn;
2310 }
2311# endif
2312 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2313 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2314 c2, c2, hl_attr(hl));
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2317 c1, c2, hl_attr(hl));
2318 }
2319 else
2320#endif
2321 {
2322#ifdef FEAT_CMDWIN
2323 if (cmdwin_type != 0 && wp == curwin)
2324 {
2325 /* draw the cmdline character in the leftmost column */
2326 n = 1;
2327 if (n > wp->w_width)
2328 n = wp->w_width;
2329 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2330 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2331 cmdwin_type, ' ', hl_attr(HLF_AT));
2332 }
2333#endif
2334#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002335 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002337 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 /* draw the fold column at the left */
2340 if (nn > W_WIDTH(wp))
2341 nn = W_WIDTH(wp);
2342 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2343 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2344 ' ', ' ', hl_attr(HLF_FC));
2345 n = nn;
2346 }
2347#endif
2348#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002349 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 int nn = n + 2;
2352
2353 /* draw the sign column after the fold column */
2354 if (nn > W_WIDTH(wp))
2355 nn = W_WIDTH(wp);
2356 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2357 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2358 ' ', ' ', hl_attr(HLF_SC));
2359 n = nn;
2360 }
2361#endif
2362 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2363 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2364 c1, c2, hl_attr(hl));
2365 }
2366 set_empty_rows(wp, row);
2367}
2368
Bram Moolenaar1a384422010-07-14 19:53:30 +02002369#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002370static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002371
2372/*
2373 * Advance **color_cols and return TRUE when there are columns to draw.
2374 */
2375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002376advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002377{
2378 while (**color_cols >= 0 && vcol > **color_cols)
2379 ++*color_cols;
2380 return (**color_cols >= 0);
2381}
2382#endif
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#ifdef FEAT_FOLDING
2385/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002386 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2387 * space is available for window "wp", minus "col".
2388 */
2389 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002390compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002391{
2392 int fdc = wp->w_p_fdc;
2393 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2394 int wwidth = W_WIDTH(wp);
2395
2396 if (fdc > wwidth - (col + wmw))
2397 fdc = wwidth - (col + wmw);
2398 return fdc;
2399}
2400
2401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 * Display one folded line.
2403 */
2404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002405fold_line(
2406 win_T *wp,
2407 long fold_count,
2408 foldinfo_T *foldinfo,
2409 linenr_T lnum,
2410 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002412 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 pos_T *top, *bot;
2414 linenr_T lnume = lnum + fold_count - 1;
2415 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002416 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 int col;
2419 int txtcol;
2420 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002421 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /* Build the fold line:
2424 * 1. Add the cmdwin_type for the command-line window
2425 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002426 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 * 4. Compose the text
2428 * 5. Add the text
2429 * 6. set highlighting for the Visual area an other text
2430 */
2431 col = 0;
2432
2433 /*
2434 * 1. Add the cmdwin_type for the command-line window
2435 * Ignores 'rightleft', this window is never right-left.
2436 */
2437#ifdef FEAT_CMDWIN
2438 if (cmdwin_type != 0 && wp == curwin)
2439 {
2440 ScreenLines[off] = cmdwin_type;
2441 ScreenAttrs[off] = hl_attr(HLF_AT);
2442#ifdef FEAT_MBYTE
2443 if (enc_utf8)
2444 ScreenLinesUC[off] = 0;
2445#endif
2446 ++col;
2447 }
2448#endif
2449
2450 /*
2451 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002452 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002454 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (fdc > 0)
2456 {
2457 fill_foldcolumn(buf, wp, TRUE, lnum);
2458#ifdef FEAT_RIGHTLEFT
2459 if (wp->w_p_rl)
2460 {
2461 int i;
2462
2463 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2464 hl_attr(HLF_FC));
2465 /* reverse the fold column */
2466 for (i = 0; i < fdc; ++i)
2467 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2468 }
2469 else
2470#endif
2471 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2472 col += fdc;
2473 }
2474
2475#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002476# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2477 for (ri = 0; ri < l; ++ri) \
2478 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2479 else \
2480 for (ri = 0; ri < l; ++ri) \
2481 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002483# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2484 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#endif
2486
Bram Moolenaar64486672010-05-16 15:46:46 +02002487 /* Set all attributes of the 'number' or 'relativenumber' column and the
2488 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002489 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491#ifdef FEAT_SIGNS
2492 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002493 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 len = W_WIDTH(wp) - col;
2496 if (len > 0)
2497 {
2498 if (len > 2)
2499 len = 2;
2500# ifdef FEAT_RIGHTLEFT
2501 if (wp->w_p_rl)
2502 /* the line number isn't reversed */
2503 copy_text_attr(off + W_WIDTH(wp) - len - col,
2504 (char_u *)" ", len, hl_attr(HLF_FL));
2505 else
2506# endif
2507 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2508 col += len;
2509 }
2510 }
2511#endif
2512
2513 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002514 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002516 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 len = W_WIDTH(wp) - col;
2519 if (len > 0)
2520 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002521 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002522 long num;
2523 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002524
2525 if (len > w + 1)
2526 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002527
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002528 if (wp->w_p_nu && !wp->w_p_rnu)
2529 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002530 num = (long)lnum;
2531 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002532 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002533 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002534 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002535 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002536 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002537 /* 'number' + 'relativenumber': cursor line shows absolute
2538 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002539 num = lnum;
2540 fmt = "%-*ld ";
2541 }
2542 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002543
Bram Moolenaar700e7342013-01-30 12:31:36 +01002544 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545#ifdef FEAT_RIGHTLEFT
2546 if (wp->w_p_rl)
2547 /* the line number isn't reversed */
2548 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2549 hl_attr(HLF_FL));
2550 else
2551#endif
2552 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2553 col += len;
2554 }
2555 }
2556
2557 /*
2558 * 4. Compose the folded-line string with 'foldtext', if set.
2559 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002560 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 txtcol = col; /* remember where text starts */
2563
2564 /*
2565 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2566 * Right-left text is put in columns 0 - number-col, normal text is put
2567 * in columns number-col - window-width.
2568 */
2569#ifdef FEAT_MBYTE
2570 if (has_mbyte)
2571 {
2572 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 int u8c, u8cc[MAX_MCO];
2574 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 int idx;
2576 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002577 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578# ifdef FEAT_ARABIC
2579 int prev_c = 0; /* previous Arabic character */
2580 int prev_c1 = 0; /* first composing char for prev_c */
2581# endif
2582
2583# ifdef FEAT_RIGHTLEFT
2584 if (wp->w_p_rl)
2585 idx = off;
2586 else
2587# endif
2588 idx = off + col;
2589
2590 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2591 for (p = text; *p != NUL; )
2592 {
2593 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002594 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (col + cells > W_WIDTH(wp)
2596# ifdef FEAT_RIGHTLEFT
2597 - (wp->w_p_rl ? col : 0)
2598# endif
2599 )
2600 break;
2601 ScreenLines[idx] = *p;
2602 if (enc_utf8)
2603 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 u8c = utfc_ptr2char(p, u8cc);
2605 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {
2607 ScreenLinesUC[idx] = 0;
2608#ifdef FEAT_ARABIC
2609 prev_c = u8c;
2610#endif
2611 }
2612 else
2613 {
2614#ifdef FEAT_ARABIC
2615 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2616 {
2617 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 int pc, pc1, nc;
2619 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 int firstbyte = *p;
2621
2622 /* The idea of what is the previous and next
2623 * character depends on 'rightleft'. */
2624 if (wp->w_p_rl)
2625 {
2626 pc = prev_c;
2627 pc1 = prev_c1;
2628 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002629 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else
2632 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002633 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002635 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 prev_c = u8c;
2638
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002639 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 pc, pc1, nc);
2641 ScreenLines[idx] = firstbyte;
2642 }
2643 else
2644 prev_c = u8c;
2645#endif
2646 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002647#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (u8c >= 0x10000)
2649 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2650 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002653 for (i = 0; i < Screen_mco; ++i)
2654 {
2655 ScreenLinesC[i][idx] = u8cc[i];
2656 if (u8cc[i] == 0)
2657 break;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660 if (cells > 1)
2661 ScreenLines[idx + 1] = 0;
2662 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002663 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2664 /* double-byte single width character */
2665 ScreenLines2[idx] = p[1];
2666 else if (cells > 1)
2667 /* double-width character */
2668 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 col += cells;
2670 idx += cells;
2671 p += c_len;
2672 }
2673 }
2674 else
2675#endif
2676 {
2677 len = (int)STRLEN(text);
2678 if (len > W_WIDTH(wp) - col)
2679 len = W_WIDTH(wp) - col;
2680 if (len > 0)
2681 {
2682#ifdef FEAT_RIGHTLEFT
2683 if (wp->w_p_rl)
2684 STRNCPY(current_ScreenLine, text, len);
2685 else
2686#endif
2687 STRNCPY(current_ScreenLine + col, text, len);
2688 col += len;
2689 }
2690 }
2691
2692 /* Fill the rest of the line with the fold filler */
2693#ifdef FEAT_RIGHTLEFT
2694 if (wp->w_p_rl)
2695 col -= txtcol;
2696#endif
2697 while (col < W_WIDTH(wp)
2698#ifdef FEAT_RIGHTLEFT
2699 - (wp->w_p_rl ? txtcol : 0)
2700#endif
2701 )
2702 {
2703#ifdef FEAT_MBYTE
2704 if (enc_utf8)
2705 {
2706 if (fill_fold >= 0x80)
2707 {
2708 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002709 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 else
2712 ScreenLinesUC[off + col] = 0;
2713 }
2714#endif
2715 ScreenLines[off + col++] = fill_fold;
2716 }
2717
2718 if (text != buf)
2719 vim_free(text);
2720
2721 /*
2722 * 6. set highlighting for the Visual area an other text.
2723 * If all folded lines are in the Visual area, highlight the line.
2724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2726 {
2727 if (ltoreq(curwin->w_cursor, VIsual))
2728 {
2729 /* Visual is after curwin->w_cursor */
2730 top = &curwin->w_cursor;
2731 bot = &VIsual;
2732 }
2733 else
2734 {
2735 /* Visual is before curwin->w_cursor */
2736 top = &VIsual;
2737 bot = &curwin->w_cursor;
2738 }
2739 if (lnum >= top->lnum
2740 && lnume <= bot->lnum
2741 && (VIsual_mode != 'v'
2742 || ((lnum > top->lnum
2743 || (lnum == top->lnum
2744 && top->col == 0))
2745 && (lnume < bot->lnum
2746 || (lnume == bot->lnum
2747 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002748 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 if (VIsual_mode == Ctrl_V)
2751 {
2752 /* Visual block mode: highlight the chars part of the block */
2753 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2754 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002755 if (wp->w_old_cursor_lcol != MAXCOL
2756 && wp->w_old_cursor_lcol + txtcol
2757 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 len = wp->w_old_cursor_lcol;
2759 else
2760 len = W_WIDTH(wp) - txtcol;
2761 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002762 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 }
2765 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002768 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002773#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002774 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2775 if (wp->w_p_cc_cols)
2776 {
2777 int i = 0;
2778 int j = wp->w_p_cc_cols[i];
2779 int old_txtcol = txtcol;
2780
2781 while (j > -1)
2782 {
2783 txtcol += j;
2784 if (wp->w_p_wrap)
2785 txtcol -= wp->w_skipcol;
2786 else
2787 txtcol -= wp->w_leftcol;
2788 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2789 ScreenAttrs[off + txtcol] = hl_combine_attr(
2790 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2791 txtcol = old_txtcol;
2792 j = wp->w_p_cc_cols[++i];
2793 }
2794 }
2795
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002796 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002797 if (wp->w_p_cuc)
2798 {
2799 txtcol += wp->w_virtcol;
2800 if (wp->w_p_wrap)
2801 txtcol -= wp->w_skipcol;
2802 else
2803 txtcol -= wp->w_leftcol;
2804 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2805 ScreenAttrs[off + txtcol] = hl_combine_attr(
2806 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2807 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2811 (int)W_WIDTH(wp), FALSE);
2812
2813 /*
2814 * Update w_cline_height and w_cline_folded if the cursor line was
2815 * updated (saves a call to plines() later).
2816 */
2817 if (wp == curwin
2818 && lnum <= curwin->w_cursor.lnum
2819 && lnume >= curwin->w_cursor.lnum)
2820 {
2821 curwin->w_cline_row = row;
2822 curwin->w_cline_height = 1;
2823 curwin->w_cline_folded = TRUE;
2824 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2825 }
2826}
2827
2828/*
2829 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2830 */
2831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002832copy_text_attr(
2833 int off,
2834 char_u *buf,
2835 int len,
2836 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002838 int i;
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 mch_memmove(ScreenLines + off, buf, (size_t)len);
2841# ifdef FEAT_MBYTE
2842 if (enc_utf8)
2843 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2844# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002845 for (i = 0; i < len; ++i)
2846 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847}
2848
2849/*
2850 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002851 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 */
2853 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002854fill_foldcolumn(
2855 char_u *p,
2856 win_T *wp,
2857 int closed, /* TRUE of FALSE */
2858 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 int i = 0;
2861 int level;
2862 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002863 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002864 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002867 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 level = win_foldinfo.fi_level;
2870 if (level > 0)
2871 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002872 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002873 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 /* If the column is too narrow, we start at the lowest level that
2876 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002877 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 if (first_level < 1)
2879 first_level = 1;
2880
Bram Moolenaar1c934292015-01-27 16:39:29 +01002881 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 if (win_foldinfo.fi_lnum == lnum
2884 && first_level + i >= win_foldinfo.fi_low_level)
2885 p[i] = '-';
2886 else if (first_level == 1)
2887 p[i] = '|';
2888 else if (first_level + i <= 9)
2889 p[i] = '0' + first_level + i;
2890 else
2891 p[i] = '>';
2892 if (first_level + i == level)
2893 break;
2894 }
2895 }
2896 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002897 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898}
2899#endif /* FEAT_FOLDING */
2900
2901/*
2902 * Display line "lnum" of window 'wp' on the screen.
2903 * Start at row "startrow", stop when "endrow" is reached.
2904 * wp->w_virtcol needs to be valid.
2905 *
2906 * Return the number of last row the line occupies.
2907 */
2908 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002909win_line(
2910 win_T *wp,
2911 linenr_T lnum,
2912 int startrow,
2913 int endrow,
2914 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 int col; /* visual column on screen */
2917 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2918 int c = 0; /* init for GCC */
2919 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002920#ifdef FEAT_LINEBREAK
2921 long vcol_sbr = -1; /* virtual column after showbreak */
2922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 long vcol_prev = -1; /* "vcol" of previous character */
2924 char_u *line; /* current line */
2925 char_u *ptr; /* current position in "line" */
2926 int row; /* row in the window, excl w_winrow */
2927 int screen_row; /* row on the screen, incl w_winrow */
2928
2929 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2930 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002931 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002932 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 int c_extra = NUL; /* extra chars, all the same */
2934 int extra_attr = 0; /* attributes when n_extra != 0 */
2935 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2936 displaying lcs_eol at end-of-line */
2937 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2938 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2939
2940 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2941 int saved_n_extra = 0;
2942 char_u *saved_p_extra = NULL;
2943 int saved_c_extra = 0;
2944 int saved_char_attr = 0;
2945
2946 int n_attr = 0; /* chars with special attr */
2947 int saved_attr2 = 0; /* char_attr saved for n_attr */
2948 int n_attr3 = 0; /* chars with overruling special attr */
2949 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2950
2951 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2952
2953 int fromcol, tocol; /* start/end of inverting */
2954 int fromcol_prev = -2; /* start of inverting after cursor */
2955 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002957 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 pos_T pos;
2959 long v;
2960
2961 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002962 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2964 in this line */
2965 int attr = 0; /* attributes for area highlighting */
2966 int area_attr = 0; /* attributes desired by highlighting */
2967 int search_attr = 0; /* attributes desired by 'hlsearch' */
2968#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002969 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 int syntax_attr = 0; /* attributes desired by syntax */
2971 int has_syntax = FALSE; /* this buffer has syntax highl. */
2972 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002973 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002974 int draw_color_col = FALSE; /* highlight colorcolumn */
2975 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002976#endif
2977#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002978 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002979# define SPWORDLEN 150
2980 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002981 int nextlinecol = 0; /* column where nextline[] starts */
2982 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002983 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002984 int spell_attr = 0; /* attributes desired by spelling */
2985 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002986 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2987 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002988 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002989 static int cap_col = -1; /* column to check for Cap word */
2990 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002991 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992#endif
2993 int extra_check; /* has syntax or linebreak */
2994#ifdef FEAT_MBYTE
2995 int multi_attr = 0; /* attributes desired by multibyte */
2996 int mb_l = 1; /* multi-byte byte length */
2997 int mb_c = 0; /* decoded multi-byte character */
2998 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002999 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#endif
3001#ifdef FEAT_DIFF
3002 int filler_lines; /* nr of filler lines to be drawn */
3003 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003004 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 int change_start = MAXCOL; /* first col of changed area */
3006 int change_end = -1; /* last col of changed area */
3007#endif
3008 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3009#ifdef FEAT_LINEBREAK
3010 int need_showbreak = FALSE;
3011#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003012#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3013 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003015 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#endif
3017#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003018 matchitem_T *cur; /* points to the match list */
3019 match_T *shl; /* points to search_hl or a match */
3020 int shl_flag; /* flag to indicate whether search_hl
3021 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003022 int pos_inprogress; /* marks that position match search is
3023 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003024 int prevcol_hl_flag; /* flag to indicate whether prevcol
3025 equals startcol of search_hl or one
3026 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#endif
3028#ifdef FEAT_ARABIC
3029 int prev_c = 0; /* previous Arabic character */
3030 int prev_c1 = 0; /* first composing char for prev_c */
3031#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003032#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003033 int did_line_attr = 0;
3034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 /* draw_state: items that are drawn in sequence: */
3037#define WL_START 0 /* nothing done yet */
3038#ifdef FEAT_CMDWIN
3039# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3040#else
3041# define WL_CMDLINE WL_START
3042#endif
3043#ifdef FEAT_FOLDING
3044# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3045#else
3046# define WL_FOLD WL_CMDLINE
3047#endif
3048#ifdef FEAT_SIGNS
3049# define WL_SIGN WL_FOLD + 1 /* column for signs */
3050#else
3051# define WL_SIGN WL_FOLD /* column for signs */
3052#endif
3053#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003054#ifdef FEAT_LINEBREAK
3055# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003057# define WL_BRI WL_NR
3058#endif
3059#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3060# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3061#else
3062# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#endif
3064#define WL_LINE WL_SBR + 1 /* text in the line */
3065 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003066#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 int feedback_col = 0;
3068 int feedback_old_attr = -1;
3069#endif
3070
Bram Moolenaar860cae12010-06-05 23:22:07 +02003071#ifdef FEAT_CONCEAL
3072 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003073 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003074 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003075 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003076 int is_concealing = FALSE;
3077 int boguscols = 0; /* nonexistent columns added to force
3078 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003079 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003080 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003081 int match_conc = 0; /* cchar for match functions */
3082 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003083 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003084# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003085# define FIX_FOR_BOGUSCOLS \
3086 { \
3087 n_extra += vcol_off; \
3088 vcol -= vcol_off; \
3089 vcol_off = 0; \
3090 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003091 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003092 boguscols = 0; \
3093 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003094#else
3095# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 if (startrow > endrow) /* past the end already! */
3099 return startrow;
3100
3101 row = startrow;
3102 screen_row = row + W_WINROW(wp);
3103
3104 /*
3105 * To speed up the loop below, set extra_check when there is linebreak,
3106 * trailing white space and/or syntax processing to be done.
3107 */
3108#ifdef FEAT_LINEBREAK
3109 extra_check = wp->w_p_lbr;
3110#else
3111 extra_check = 0;
3112#endif
3113#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003114 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
3116 /* Prepare for syntax highlighting in this line. When there is an
3117 * error, stop syntax highlighting. */
3118 save_did_emsg = did_emsg;
3119 did_emsg = FALSE;
3120 syntax_start(wp, lnum);
3121 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003122 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 else
3124 {
3125 did_emsg = save_did_emsg;
3126 has_syntax = TRUE;
3127 extra_check = TRUE;
3128 }
3129 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003130
3131 /* Check for columns to display for 'colorcolumn'. */
3132 color_cols = wp->w_p_cc_cols;
3133 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003134 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003135#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003136
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003137#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003138 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003139 && *wp->w_s->b_p_spl != NUL
3140 && wp->w_s->b_langp.ga_len > 0
3141 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003142 {
3143 /* Prepare for spell checking. */
3144 has_spell = TRUE;
3145 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003146
3147 /* Get the start of the next line, so that words that wrap to the next
3148 * line are found too: "et<line-break>al.".
3149 * Trick: skip a few chars for C/shell/Vim comments */
3150 nextline[SPWORDLEN] = NUL;
3151 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3152 {
3153 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3154 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3155 }
3156
3157 /* When a word wrapped from the previous line the start of the current
3158 * line is valid. */
3159 if (lnum == checked_lnum)
3160 cur_checked_col = checked_col;
3161 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003162
3163 /* When there was a sentence end in the previous line may require a
3164 * word starting with capital in this line. In line 1 always check
3165 * the first word. */
3166 if (lnum != capcol_lnum)
3167 cap_col = -1;
3168 if (lnum == 1)
3169 cap_col = 0;
3170 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173
3174 /*
3175 * handle visual active in this window
3176 */
3177 fromcol = -10;
3178 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3180 {
3181 /* Visual is after curwin->w_cursor */
3182 if (ltoreq(curwin->w_cursor, VIsual))
3183 {
3184 top = &curwin->w_cursor;
3185 bot = &VIsual;
3186 }
3187 else /* Visual is before curwin->w_cursor */
3188 {
3189 top = &VIsual;
3190 bot = &curwin->w_cursor;
3191 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003192 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (VIsual_mode == Ctrl_V) /* block mode */
3194 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003195 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 fromcol = wp->w_old_cursor_fcol;
3198 tocol = wp->w_old_cursor_lcol;
3199 }
3200 }
3201 else /* non-block mode */
3202 {
3203 if (lnum > top->lnum && lnum <= bot->lnum)
3204 fromcol = 0;
3205 else if (lnum == top->lnum)
3206 {
3207 if (VIsual_mode == 'V') /* linewise */
3208 fromcol = 0;
3209 else
3210 {
3211 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3212 if (gchar_pos(top) == NUL)
3213 tocol = fromcol + 1;
3214 }
3215 }
3216 if (VIsual_mode != 'V' && lnum == bot->lnum)
3217 {
3218 if (*p_sel == 'e' && bot->col == 0
3219#ifdef FEAT_VIRTUALEDIT
3220 && bot->coladd == 0
3221#endif
3222 )
3223 {
3224 fromcol = -10;
3225 tocol = MAXCOL;
3226 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003227 else if (bot->col == MAXCOL)
3228 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 else
3230 {
3231 pos = *bot;
3232 if (*p_sel == 'e')
3233 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3234 else
3235 {
3236 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3237 ++tocol;
3238 }
3239 }
3240 }
3241 }
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 /* Check if the character under the cursor should not be inverted */
3244 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003245#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 )
3249 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 /* if inverting in this line set area_highlighting */
3252 if (fromcol >= 0)
3253 {
3254 area_highlighting = TRUE;
3255 attr = hl_attr(HLF_V);
3256#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003257 if ((clip_star.available && !clip_star.owned
3258 && clip_isautosel_star())
3259 || (clip_plus.available && !clip_plus.owned
3260 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 attr = hl_attr(HLF_VNC);
3262#endif
3263 }
3264 }
3265
3266 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003267 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003269 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 && wp == curwin
3271 && lnum >= curwin->w_cursor.lnum
3272 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3273 {
3274 if (lnum == curwin->w_cursor.lnum)
3275 getvcol(curwin, &(curwin->w_cursor),
3276 (colnr_T *)&fromcol, NULL, NULL);
3277 else
3278 fromcol = 0;
3279 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3280 {
3281 pos.lnum = lnum;
3282 pos.col = search_match_endcol;
3283 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3284 }
3285 else
3286 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003287 /* do at least one character; happens when past end of line */
3288 if (fromcol == tocol)
3289 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 area_highlighting = TRUE;
3291 attr = hl_attr(HLF_I);
3292 }
3293
3294#ifdef FEAT_DIFF
3295 filler_lines = diff_check(wp, lnum);
3296 if (filler_lines < 0)
3297 {
3298 if (filler_lines == -1)
3299 {
3300 if (diff_find_change(wp, lnum, &change_start, &change_end))
3301 diff_hlf = HLF_ADD; /* added line */
3302 else if (change_start == 0)
3303 diff_hlf = HLF_TXD; /* changed text */
3304 else
3305 diff_hlf = HLF_CHD; /* changed line */
3306 }
3307 else
3308 diff_hlf = HLF_ADD; /* added line */
3309 filler_lines = 0;
3310 area_highlighting = TRUE;
3311 }
3312 if (lnum == wp->w_topline)
3313 filler_lines = wp->w_topfill;
3314 filler_todo = filler_lines;
3315#endif
3316
3317#ifdef LINE_ATTR
3318# ifdef FEAT_SIGNS
3319 /* If this line has a sign with line highlighting set line_attr. */
3320 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3321 if (v != 0)
3322 line_attr = sign_get_attr((int)v, TRUE);
3323# endif
3324# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3325 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003326 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 line_attr = hl_attr(HLF_L);
3328# endif
3329 if (line_attr != 0)
3330 area_highlighting = TRUE;
3331#endif
3332
3333 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3334 ptr = line;
3335
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003336#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003337 if (has_spell)
3338 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003339 /* For checking first word with a capital skip white space. */
3340 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003341 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003342
Bram Moolenaar30abd282005-06-22 22:35:10 +00003343 /* To be able to spell-check over line boundaries copy the end of the
3344 * current line into nextline[]. Above the start of the next line was
3345 * copied to nextline[SPWORDLEN]. */
3346 if (nextline[SPWORDLEN] == NUL)
3347 {
3348 /* No next line or it is empty. */
3349 nextlinecol = MAXCOL;
3350 nextline_idx = 0;
3351 }
3352 else
3353 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003354 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003355 if (v < SPWORDLEN)
3356 {
3357 /* Short line, use it completely and append the start of the
3358 * next line. */
3359 nextlinecol = 0;
3360 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003361 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003362 nextline_idx = v + 1;
3363 }
3364 else
3365 {
3366 /* Long line, use only the last SPWORDLEN bytes. */
3367 nextlinecol = v - SPWORDLEN;
3368 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3369 nextline_idx = SPWORDLEN + 1;
3370 }
3371 }
3372 }
3373#endif
3374
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003375 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003377 if (lcs_space || lcs_trail)
3378 extra_check = TRUE;
3379 /* find start of trailing whitespace */
3380 if (lcs_trail)
3381 {
3382 trailcol = (colnr_T)STRLEN(ptr);
3383 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3384 --trailcol;
3385 trailcol += (colnr_T) (ptr - line);
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388
3389 /*
3390 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3391 * first character to be displayed.
3392 */
3393 if (wp->w_p_wrap)
3394 v = wp->w_skipcol;
3395 else
3396 v = wp->w_leftcol;
3397 if (v > 0)
3398 {
3399#ifdef FEAT_MBYTE
3400 char_u *prev_ptr = ptr;
3401#endif
3402 while (vcol < v && *ptr != NUL)
3403 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003404 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 vcol += c;
3406#ifdef FEAT_MBYTE
3407 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003409 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003412 /* When:
3413 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003414 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003415 * - 'virtualedit' is set, or
3416 * - the visual mode is active,
3417 * the end of the line may be before the start of the displayed part.
3418 */
3419 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003420#ifdef FEAT_SYN_HL
3421 wp->w_p_cuc || draw_color_col ||
3422#endif
3423#ifdef FEAT_VIRTUALEDIT
3424 virtual_active() ||
3425#endif
3426 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 /* Handle a character that's not completely on the screen: Put ptr at
3432 * that character but skip the first few screen characters. */
3433 if (vcol > v)
3434 {
3435 vcol -= c;
3436#ifdef FEAT_MBYTE
3437 ptr = prev_ptr;
3438#else
3439 --ptr;
3440#endif
3441 n_skip = v - vcol;
3442 }
3443
3444 /*
3445 * Adjust for when the inverted text is before the screen,
3446 * and when the start of the inverted text is before the screen.
3447 */
3448 if (tocol <= vcol)
3449 fromcol = 0;
3450 else if (fromcol >= 0 && fromcol < vcol)
3451 fromcol = vcol;
3452
3453#ifdef FEAT_LINEBREAK
3454 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3455 if (wp->w_p_wrap)
3456 need_showbreak = TRUE;
3457#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003458#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003459 /* When spell checking a word we need to figure out the start of the
3460 * word and if it's badly spelled or not. */
3461 if (has_spell)
3462 {
3463 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003464 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003465 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003466
3467 pos = wp->w_cursor;
3468 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003469 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003470 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003471
3472 /* spell_move_to() may call ml_get() and make "line" invalid */
3473 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3474 ptr = line + linecol;
3475
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003476 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003477 {
3478 /* no bad word found at line start, don't check until end of a
3479 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003480 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003481 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003482 }
3483 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003484 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003485 /* bad word found, use attributes until end of word */
3486 word_end = wp->w_cursor.col + len + 1;
3487
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003488 /* Turn index into actual attributes. */
3489 if (spell_hlf != HLF_COUNT)
3490 spell_attr = highlight_attr[spell_hlf];
3491 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003492 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003493
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003494# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003495 /* Need to restart syntax highlighting for this line. */
3496 if (has_syntax)
3497 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003498# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003499 }
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
3503 /*
3504 * Correct highlighting for cursor that can't be disabled.
3505 * Avoids having to check this for each character.
3506 */
3507 if (fromcol >= 0)
3508 {
3509 if (noinvcur)
3510 {
3511 if ((colnr_T)fromcol == wp->w_virtcol)
3512 {
3513 /* highlighting starts at cursor, let it start just after the
3514 * cursor */
3515 fromcol_prev = fromcol;
3516 fromcol = -1;
3517 }
3518 else if ((colnr_T)fromcol < wp->w_virtcol)
3519 /* restart highlighting after the cursor */
3520 fromcol_prev = wp->w_virtcol;
3521 }
3522 if (fromcol >= tocol)
3523 fromcol = -1;
3524 }
3525
3526#ifdef FEAT_SEARCH_EXTRA
3527 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003528 * Handle highlighting the last used search pattern and matches.
3529 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003531 cur = wp->w_match_head;
3532 shl_flag = FALSE;
3533 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003535 if (shl_flag == FALSE)
3536 {
3537 shl = &search_hl;
3538 shl_flag = TRUE;
3539 }
3540 else
3541 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003542 shl->startcol = MAXCOL;
3543 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003545 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003546 v = (long)(ptr - line);
3547 if (cur != NULL)
3548 cur->pos.cur = 0;
3549 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3550
3551 /* Need to get the line again, a multi-line regexp may have made it
3552 * invalid. */
3553 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3554 ptr = line + v;
3555
3556 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003558 if (shl->lnum == lnum)
3559 shl->startcol = shl->rm.startpos[0].col;
3560 else
3561 shl->startcol = 0;
3562 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3563 - shl->rm.startpos[0].lnum)
3564 shl->endcol = shl->rm.endpos[0].col;
3565 else
3566 shl->endcol = MAXCOL;
3567 /* Highlight one character for an empty match. */
3568 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003571 if (has_mbyte && line[shl->endcol] != NUL)
3572 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003575 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003577 if ((long)shl->startcol < v) /* match at leftcol */
3578 {
3579 shl->attr_cur = shl->attr;
3580 search_attr = shl->attr;
3581 }
3582 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003584 if (shl != &search_hl && cur != NULL)
3585 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587#endif
3588
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003589#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003590 /* Cursor line highlighting for 'cursorline' in the current window. Not
3591 * when Visual mode is active, because it's not clear what is selected
3592 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003593 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003594 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003595 {
3596 line_attr = hl_attr(HLF_CUL);
3597 area_highlighting = TRUE;
3598 }
3599#endif
3600
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003601 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 col = 0;
3603#ifdef FEAT_RIGHTLEFT
3604 if (wp->w_p_rl)
3605 {
3606 /* Rightleft window: process the text in the normal direction, but put
3607 * it in current_ScreenLine[] from right to left. Start at the
3608 * rightmost column of the window. */
3609 col = W_WIDTH(wp) - 1;
3610 off += col;
3611 }
3612#endif
3613
3614 /*
3615 * Repeat for the whole displayed line.
3616 */
3617 for (;;)
3618 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003619#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003620 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 /* Skip this quickly when working on the text. */
3623 if (draw_state != WL_LINE)
3624 {
3625#ifdef FEAT_CMDWIN
3626 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3627 {
3628 draw_state = WL_CMDLINE;
3629 if (cmdwin_type != 0 && wp == curwin)
3630 {
3631 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003633 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 char_attr = hl_attr(HLF_AT);
3635 }
3636 }
3637#endif
3638
3639#ifdef FEAT_FOLDING
3640 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3641 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003642 int fdc = compute_foldcolumn(wp, 0);
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003645 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 /* Draw the 'foldcolumn'. */
3648 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003649 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003651 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 c_extra = NUL;
3653 char_attr = hl_attr(HLF_FC);
3654 }
3655 }
3656#endif
3657
3658#ifdef FEAT_SIGNS
3659 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3660 {
3661 draw_state = WL_SIGN;
3662 /* Show the sign column when there are any signs in this
3663 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003664 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003666 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003668 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669# endif
3670
3671 /* Draw two cells with the sign value or blank. */
3672 c_extra = ' ';
3673 char_attr = hl_attr(HLF_SC);
3674 n_extra = 2;
3675
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003676 if (row == startrow
3677#ifdef FEAT_DIFF
3678 + filler_lines && filler_todo <= 0
3679#endif
3680 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3683 SIGN_TEXT);
3684# ifdef FEAT_SIGN_ICONS
3685 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3686 SIGN_ICON);
3687 if (gui.in_use && icon_sign != 0)
3688 {
3689 /* Use the image in this position. */
3690 c_extra = SIGN_BYTE;
3691# ifdef FEAT_NETBEANS_INTG
3692 if (buf_signcount(wp->w_buffer, lnum) > 1)
3693 c_extra = MULTISIGN_BYTE;
3694# endif
3695 char_attr = icon_sign;
3696 }
3697 else
3698# endif
3699 if (text_sign != 0)
3700 {
3701 p_extra = sign_get_text(text_sign);
3702 if (p_extra != NULL)
3703 {
3704 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003705 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707 char_attr = sign_get_attr(text_sign, FALSE);
3708 }
3709 }
3710 }
3711 }
3712#endif
3713
3714 if (draw_state == WL_NR - 1 && n_extra == 0)
3715 {
3716 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003717 /* Display the absolute or relative line number. After the
3718 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3719 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 && (row == startrow
3721#ifdef FEAT_DIFF
3722 + filler_lines
3723#endif
3724 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3725 {
3726 /* Draw the line number (empty space after wrapping). */
3727 if (row == startrow
3728#ifdef FEAT_DIFF
3729 + filler_lines
3730#endif
3731 )
3732 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003733 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003734 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003735
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003736 if (wp->w_p_nu && !wp->w_p_rnu)
3737 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003738 num = (long)lnum;
3739 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003740 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003741 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003742 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003743 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003744 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003745 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003746 num = lnum;
3747 fmt = "%-*ld ";
3748 }
3749 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003750
Bram Moolenaar700e7342013-01-30 12:31:36 +01003751 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003752 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (wp->w_skipcol > 0)
3754 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3755 *p_extra = '-';
3756#ifdef FEAT_RIGHTLEFT
3757 if (wp->w_p_rl) /* reverse line numbers */
3758 rl_mirror(extra);
3759#endif
3760 p_extra = extra;
3761 c_extra = NUL;
3762 }
3763 else
3764 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003765 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003767#ifdef FEAT_SYN_HL
3768 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003769 * the current line differently.
3770 * TODO: Can we use CursorLine instead of CursorLineNr
3771 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003772 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003773 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003774 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777 }
3778
Bram Moolenaar597a4222014-06-25 14:39:50 +02003779#ifdef FEAT_LINEBREAK
3780 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3781 && n_extra == 0 && *p_sbr != NUL)
3782 /* draw indent after showbreak value */
3783 draw_state = WL_BRI;
3784 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3785 /* After the showbreak, draw the breakindent */
3786 draw_state = WL_BRI - 1;
3787
3788 /* draw 'breakindent': indent wrapped text accordingly */
3789 if (draw_state == WL_BRI - 1 && n_extra == 0)
3790 {
3791 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003792 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003793# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003794 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003795# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003796 )
3797 {
3798 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003799# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003800 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003801 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003802 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003803# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003804 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3805 char_attr = hl_combine_attr(char_attr,
3806 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003807# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003808 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003809# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003810 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003811 c_extra = ' ';
3812 n_extra = get_breakindent_win(wp,
3813 ml_get_buf(wp->w_buffer, lnum, FALSE));
3814 /* Correct end of highlighted area for 'breakindent',
3815 * required when 'linebreak' is also set. */
3816 if (tocol == vcol)
3817 tocol += n_extra;
3818 }
3819 }
3820#endif
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3823 if (draw_state == WL_SBR - 1 && n_extra == 0)
3824 {
3825 draw_state = WL_SBR;
3826# ifdef FEAT_DIFF
3827 if (filler_todo > 0)
3828 {
3829 /* Draw "deleted" diff line(s). */
3830 if (char2cells(fill_diff) > 1)
3831 c_extra = '-';
3832 else
3833 c_extra = fill_diff;
3834# ifdef FEAT_RIGHTLEFT
3835 if (wp->w_p_rl)
3836 n_extra = col + 1;
3837 else
3838# endif
3839 n_extra = W_WIDTH(wp) - col;
3840 char_attr = hl_attr(HLF_DED);
3841 }
3842# endif
3843# ifdef FEAT_LINEBREAK
3844 if (*p_sbr != NUL && need_showbreak)
3845 {
3846 /* Draw 'showbreak' at the start of each broken line. */
3847 p_extra = p_sbr;
3848 c_extra = NUL;
3849 n_extra = (int)STRLEN(p_sbr);
3850 char_attr = hl_attr(HLF_AT);
3851 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003852 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 /* Correct end of highlighted area for 'showbreak',
3854 * required when 'linebreak' is also set. */
3855 if (tocol == vcol)
3856 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003857#ifdef FEAT_SYN_HL
3858 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003859 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003860 char_attr = hl_combine_attr(char_attr,
3861 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864# endif
3865 }
3866#endif
3867
3868 if (draw_state == WL_LINE - 1 && n_extra == 0)
3869 {
3870 draw_state = WL_LINE;
3871 if (saved_n_extra)
3872 {
3873 /* Continue item from end of wrapped line. */
3874 n_extra = saved_n_extra;
3875 c_extra = saved_c_extra;
3876 p_extra = saved_p_extra;
3877 char_attr = saved_char_attr;
3878 }
3879 else
3880 char_attr = 0;
3881 }
3882 }
3883
3884 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003885 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003886 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#ifdef FEAT_DIFF
3888 && filler_todo <= 0
3889#endif
3890 )
3891 {
3892 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3893 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003894 /* Pretend we have finished updating the window. Except when
3895 * 'cursorcolumn' is set. */
3896#ifdef FEAT_SYN_HL
3897 if (wp->w_p_cuc)
3898 row = wp->w_cline_row + wp->w_cline_height;
3899 else
3900#endif
3901 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 break;
3903 }
3904
3905 if (draw_state == WL_LINE && area_highlighting)
3906 {
3907 /* handle Visual or match highlighting in this line */
3908 if (vcol == fromcol
3909#ifdef FEAT_MBYTE
3910 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3911 && (*mb_ptr2cells)(ptr) > 1)
3912#endif
3913 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003914 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 && vcol < tocol))
3916 area_attr = attr; /* start highlighting */
3917 else if (area_attr != 0
3918 && (vcol == tocol
3919 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922#ifdef FEAT_SEARCH_EXTRA
3923 if (!n_extra)
3924 {
3925 /*
3926 * Check for start/end of search pattern match.
3927 * After end, check for start/end of next match.
3928 * When another match, have to check for start again.
3929 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003930 * Do this for 'search_hl' and the match list (ordered by
3931 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003933 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003934 cur = wp->w_match_head;
3935 shl_flag = FALSE;
3936 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003938 if (shl_flag == FALSE
3939 && ((cur != NULL
3940 && cur->priority > SEARCH_HL_PRIORITY)
3941 || cur == NULL))
3942 {
3943 shl = &search_hl;
3944 shl_flag = TRUE;
3945 }
3946 else
3947 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003948 if (cur != NULL)
3949 cur->pos.cur = 0;
3950 pos_inprogress = TRUE;
3951 while (shl->rm.regprog != NULL
3952 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003954 if (shl->startcol != MAXCOL
3955 && v >= (long)shl->startcol
3956 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003958#ifdef FEAT_MBYTE
3959 int tmp_col = v + MB_PTR2LEN(ptr);
3960
3961 if (shl->endcol < tmp_col)
3962 shl->endcol = tmp_col;
3963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003965#ifdef FEAT_CONCEAL
3966 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3967 == cur->hlg_id)
3968 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003969 has_match_conc =
3970 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003971 match_conc = cur->conceal_char;
3972 }
3973 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003974 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003977 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
3979 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003980#ifdef FEAT_CONCEAL
3981 prev_syntax_id = 0;
3982#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003983 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3984 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003985 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 /* Need to get the line again, a multi-line regexp
3988 * may have made it invalid. */
3989 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3990 ptr = line + v;
3991
3992 if (shl->lnum == lnum)
3993 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003994 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003996 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003998 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004000 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 /* highlight empty match, try again after
4003 * it */
4004#ifdef FEAT_MBYTE
4005 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004006 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004007 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 else
4009#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004010 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012
4013 /* Loop to check if the match starts at the
4014 * current position */
4015 continue;
4016 }
4017 }
4018 break;
4019 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004020 if (shl != &search_hl && cur != NULL)
4021 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004023
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004024 /* Use attributes from match with highest priority among
4025 * 'search_hl' and the match list. */
4026 search_attr = search_hl.attr_cur;
4027 cur = wp->w_match_head;
4028 shl_flag = FALSE;
4029 while (cur != NULL || shl_flag == FALSE)
4030 {
4031 if (shl_flag == FALSE
4032 && ((cur != NULL
4033 && cur->priority > SEARCH_HL_PRIORITY)
4034 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004035 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004036 shl = &search_hl;
4037 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004038 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004039 else
4040 shl = &cur->hl;
4041 if (shl->attr_cur != 0)
4042 search_attr = shl->attr_cur;
4043 if (shl != &search_hl && cur != NULL)
4044 cur = cur->next;
4045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047#endif
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004050 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004052 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4053 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004055 if (diff_hlf == HLF_TXD && ptr - line > change_end
4056 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004058 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004059 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4060 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004063
4064 /* Decide which of the highlight attributes to use. */
4065 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004066#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004067 if (area_attr != 0)
4068 char_attr = hl_combine_attr(line_attr, area_attr);
4069 else if (search_attr != 0)
4070 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004071 /* Use line_attr when not in the Visual or 'incsearch' area
4072 * (area_attr may be 0 when "noinvcur" is set). */
4073 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004074 || vcol < fromcol || vcol_prev < fromcol_prev
4075 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004076 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004077#else
4078 if (area_attr != 0)
4079 char_attr = area_attr;
4080 else if (search_attr != 0)
4081 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004082#endif
4083 else
4084 {
4085 attr_pri = FALSE;
4086#ifdef FEAT_SYN_HL
4087 if (has_syntax)
4088 char_attr = syntax_attr;
4089 else
4090#endif
4091 char_attr = 0;
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094
4095 /*
4096 * Get the next character to put on the screen.
4097 */
4098 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004099 * The "p_extra" points to the extra stuff that is inserted to
4100 * represent special characters (non-printable stuff) and other
4101 * things. When all characters are the same, c_extra is used.
4102 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4103 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4105 */
4106 if (n_extra > 0)
4107 {
4108 if (c_extra != NUL)
4109 {
4110 c = c_extra;
4111#ifdef FEAT_MBYTE
4112 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4113 if (enc_utf8 && (*mb_char2len)(c) > 1)
4114 {
4115 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004116 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004117 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119 else
4120 mb_utf8 = FALSE;
4121#endif
4122 }
4123 else
4124 {
4125 c = *p_extra;
4126#ifdef FEAT_MBYTE
4127 if (has_mbyte)
4128 {
4129 mb_c = c;
4130 if (enc_utf8)
4131 {
4132 /* If the UTF-8 character is more than one byte:
4133 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004134 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 mb_utf8 = FALSE;
4136 if (mb_l > n_extra)
4137 mb_l = 1;
4138 else if (mb_l > 1)
4139 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004140 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004142 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145 else
4146 {
4147 /* if this is a DBCS character, put it in "mb_c" */
4148 mb_l = MB_BYTE2LEN(c);
4149 if (mb_l >= n_extra)
4150 mb_l = 1;
4151 else if (mb_l > 1)
4152 mb_c = (c << 8) + p_extra[1];
4153 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004154 if (mb_l == 0) /* at the NUL at end-of-line */
4155 mb_l = 1;
4156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 /* If a double-width char doesn't fit display a '>' in the
4158 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004159 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160# ifdef FEAT_RIGHTLEFT
4161 wp->w_p_rl ? (col <= 0) :
4162# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004163 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 && (*mb_char2cells)(mb_c) == 2)
4165 {
4166 c = '>';
4167 mb_c = c;
4168 mb_l = 1;
4169 mb_utf8 = FALSE;
4170 multi_attr = hl_attr(HLF_AT);
4171 /* put the pointer back to output the double-width
4172 * character at the start of the next line. */
4173 ++n_extra;
4174 --p_extra;
4175 }
4176 else
4177 {
4178 n_extra -= mb_l - 1;
4179 p_extra += mb_l - 1;
4180 }
4181 }
4182#endif
4183 ++p_extra;
4184 }
4185 --n_extra;
4186 }
4187 else
4188 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004189 if (p_extra_free != NULL)
4190 {
4191 vim_free(p_extra_free);
4192 p_extra_free = NULL;
4193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 /*
4195 * Get a character from the line itself.
4196 */
4197 c = *ptr;
4198#ifdef FEAT_MBYTE
4199 if (has_mbyte)
4200 {
4201 mb_c = c;
4202 if (enc_utf8)
4203 {
4204 /* If the UTF-8 character is more than one byte: Decode it
4205 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004206 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 mb_utf8 = FALSE;
4208 if (mb_l > 1)
4209 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004210 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 /* Overlong encoded ASCII or ASCII with composing char
4212 * is displayed normally, except a NUL. */
4213 if (mb_c < 0x80)
4214 c = mb_c;
4215 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004216
4217 /* At start of the line we can have a composing char.
4218 * Draw it as a space with a composing char. */
4219 if (utf_iscomposing(mb_c))
4220 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221 int i;
4222
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004223 for (i = Screen_mco - 1; i > 0; --i)
4224 u8cc[i] = u8cc[i - 1];
4225 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004226 mb_c = ' ';
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229
4230 if ((mb_l == 1 && c >= 0x80)
4231 || (mb_l >= 1 && mb_c == 0)
4232 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004233# ifdef UNICODE16
4234 || mb_c >= 0x10000
4235# endif
4236 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 /*
4239 * Illegal UTF-8 byte: display as <xx>.
4240 * Non-BMP character : display as ? or fullwidth ?.
4241 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004242# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004247# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (wp->w_p_rl) /* reverse */
4249 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004252# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 else if (utf_char2cells(mb_c) != 2)
4254 STRCPY(extra, "?");
4255 else
4256 /* 0xff1f in UTF-8: full-width '?' */
4257 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 p_extra = extra;
4261 c = *p_extra;
4262 mb_c = mb_ptr2char_adv(&p_extra);
4263 mb_utf8 = (c >= 0x80);
4264 n_extra = (int)STRLEN(p_extra);
4265 c_extra = NUL;
4266 if (area_attr == 0 && search_attr == 0)
4267 {
4268 n_attr = n_extra + 1;
4269 extra_attr = hl_attr(HLF_8);
4270 saved_attr2 = char_attr; /* save current attr */
4271 }
4272 }
4273 else if (mb_l == 0) /* at the NUL at end-of-line */
4274 mb_l = 1;
4275#ifdef FEAT_ARABIC
4276 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4277 {
4278 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004279 int pc, pc1, nc;
4280 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 /* The idea of what is the previous and next
4283 * character depends on 'rightleft'. */
4284 if (wp->w_p_rl)
4285 {
4286 pc = prev_c;
4287 pc1 = prev_c1;
4288 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004289 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 else
4292 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004295 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 prev_c = mb_c;
4298
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004299 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 else
4302 prev_c = mb_c;
4303#endif
4304 }
4305 else /* enc_dbcs */
4306 {
4307 mb_l = MB_BYTE2LEN(c);
4308 if (mb_l == 0) /* at the NUL at end-of-line */
4309 mb_l = 1;
4310 else if (mb_l > 1)
4311 {
4312 /* We assume a second byte below 32 is illegal.
4313 * Hopefully this is OK for all double-byte encodings!
4314 */
4315 if (ptr[1] >= 32)
4316 mb_c = (c << 8) + ptr[1];
4317 else
4318 {
4319 if (ptr[1] == NUL)
4320 {
4321 /* head byte at end of line */
4322 mb_l = 1;
4323 transchar_nonprint(extra, c);
4324 }
4325 else
4326 {
4327 /* illegal tail byte */
4328 mb_l = 2;
4329 STRCPY(extra, "XX");
4330 }
4331 p_extra = extra;
4332 n_extra = (int)STRLEN(extra) - 1;
4333 c_extra = NUL;
4334 c = *p_extra++;
4335 if (area_attr == 0 && search_attr == 0)
4336 {
4337 n_attr = n_extra + 1;
4338 extra_attr = hl_attr(HLF_8);
4339 saved_attr2 = char_attr; /* save current attr */
4340 }
4341 mb_c = c;
4342 }
4343 }
4344 }
4345 /* If a double-width char doesn't fit display a '>' in the
4346 * last column; the character is displayed at the start of the
4347 * next line. */
4348 if ((
4349# ifdef FEAT_RIGHTLEFT
4350 wp->w_p_rl ? (col <= 0) :
4351# endif
4352 (col >= W_WIDTH(wp) - 1))
4353 && (*mb_char2cells)(mb_c) == 2)
4354 {
4355 c = '>';
4356 mb_c = c;
4357 mb_utf8 = FALSE;
4358 mb_l = 1;
4359 multi_attr = hl_attr(HLF_AT);
4360 /* Put pointer back so that the character will be
4361 * displayed at the start of the next line. */
4362 --ptr;
4363 }
4364 else if (*ptr != NUL)
4365 ptr += mb_l - 1;
4366
4367 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004368 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004369 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004370 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004373 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 c = ' ';
4375 if (area_attr == 0 && search_attr == 0)
4376 {
4377 n_attr = n_extra + 1;
4378 extra_attr = hl_attr(HLF_AT);
4379 saved_attr2 = char_attr; /* save current attr */
4380 }
4381 mb_c = c;
4382 mb_utf8 = FALSE;
4383 mb_l = 1;
4384 }
4385
4386 }
4387#endif
4388 ++ptr;
4389
4390 if (extra_check)
4391 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004392#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004393 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004394#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004395
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004396#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /* Get syntax attribute, unless still at the start of the line
4398 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004399 v = (long)(ptr - line);
4400 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
4402 /* Get the syntax attribute for the character. If there
4403 * is an error, disable syntax highlighting. */
4404 save_did_emsg = did_emsg;
4405 did_emsg = FALSE;
4406
Bram Moolenaar217ad922005-03-20 22:37:15 +00004407 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004408# ifdef FEAT_SPELL
4409 has_spell ? &can_spell :
4410# endif
4411 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
4413 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004414 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004415 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004416 has_syntax = FALSE;
4417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 else
4419 did_emsg = save_did_emsg;
4420
4421 /* Need to get the line again, a multi-line regexp may
4422 * have made it invalid. */
4423 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4424 ptr = line + v;
4425
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004426 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004428 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004429 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004430# ifdef FEAT_CONCEAL
4431 /* no concealing past the end of the line, it interferes
4432 * with line highlighting */
4433 if (c == NUL)
4434 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004435 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004436 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004439#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004440
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004441#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004442 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004443 * Only do this when there is no syntax highlighting, the
4444 * @Spell cluster is not used or the current syntax item
4445 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004446 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004448 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004449# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004450 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004451 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004452# endif
4453 if (c != 0 && (
4454# ifdef FEAT_SYN_HL
4455 !has_syntax ||
4456# endif
4457 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004458 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004459 char_u *prev_ptr, *p;
4460 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004461 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004462# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004463 if (has_mbyte)
4464 {
4465 prev_ptr = ptr - mb_l;
4466 v -= mb_l - 1;
4467 }
4468 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004469# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004470 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004471
4472 /* Use nextline[] if possible, it has the start of the
4473 * next line concatenated. */
4474 if ((prev_ptr - line) - nextlinecol >= 0)
4475 p = nextline + (prev_ptr - line) - nextlinecol;
4476 else
4477 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004478 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004479 len = spell_check(wp, p, &spell_hlf, &cap_col,
4480 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004481 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004482
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004483 /* In Insert mode only highlight a word that
4484 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004485 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004486 && (State & INSERT) != 0
4487 && wp->w_cursor.lnum == lnum
4488 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004489 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004490 && wp->w_cursor.col < (colnr_T)word_end)
4491 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004492 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004493 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004494 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004495
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004496 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004497 && (p - nextline) + len > nextline_idx)
4498 {
4499 /* Remember that the good word continues at the
4500 * start of the next line. */
4501 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004502 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004503 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004504
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004505 /* Turn index into actual attributes. */
4506 if (spell_hlf != HLF_COUNT)
4507 spell_attr = highlight_attr[spell_hlf];
4508
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004509 if (cap_col > 0)
4510 {
4511 if (p != prev_ptr
4512 && (p - nextline) + cap_col >= nextline_idx)
4513 {
4514 /* Remember that the word in the next line
4515 * must start with a capital. */
4516 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004517 cap_col = (int)((p - nextline) + cap_col
4518 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004519 }
4520 else
4521 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004522 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004523 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004524 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004525 }
4526 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004527 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004528 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004529 char_attr = hl_combine_attr(char_attr, spell_attr);
4530 else
4531 char_attr = hl_combine_attr(spell_attr, char_attr);
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533#endif
4534#ifdef FEAT_LINEBREAK
4535 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004536 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004538 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004540# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004541 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004542# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004543 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004545 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004547 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004548
Bram Moolenaar597a4222014-06-25 14:39:50 +02004549 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004550 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004551 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004552 if (c == TAB && n_extra + col > W_WIDTH(wp))
4553 n_extra = (int)wp->w_buffer->b_p_ts
4554 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4555
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004556# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004557 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004558# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004562 {
4563#ifdef FEAT_CONCEAL
4564 if (c == TAB)
4565 /* See "Tab alignment" below. */
4566 FIX_FOR_BOGUSCOLS;
4567#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004568 if (!wp->w_p_list)
4569 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572#endif
4573
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004574 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4575 */
4576 if (wp->w_p_list
4577 && (((c == 160
4578#ifdef FEAT_MBYTE
4579 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4580#endif
4581 ) && lcs_nbsp)
4582 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4583 {
4584 c = (c == ' ') ? lcs_space : lcs_nbsp;
4585 if (area_attr == 0 && search_attr == 0)
4586 {
4587 n_attr = 1;
4588 extra_attr = hl_attr(HLF_8);
4589 saved_attr2 = char_attr; /* save current attr */
4590 }
4591#ifdef FEAT_MBYTE
4592 mb_c = c;
4593 if (enc_utf8 && (*mb_char2len)(c) > 1)
4594 {
4595 mb_utf8 = TRUE;
4596 u8cc[0] = 0;
4597 c = 0xc0;
4598 }
4599 else
4600 mb_utf8 = FALSE;
4601#endif
4602 }
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4605 {
4606 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004607 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609 n_attr = 1;
4610 extra_attr = hl_attr(HLF_8);
4611 saved_attr2 = char_attr; /* save current attr */
4612 }
4613#ifdef FEAT_MBYTE
4614 mb_c = c;
4615 if (enc_utf8 && (*mb_char2len)(c) > 1)
4616 {
4617 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004618 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004619 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 else
4622 mb_utf8 = FALSE;
4623#endif
4624 }
4625 }
4626
4627 /*
4628 * Handling of non-printable characters.
4629 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004630 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 /*
4633 * when getting a character from the file, we may have to
4634 * turn it into something else on the way to putting it
4635 * into "ScreenLines".
4636 */
4637 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4638 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004639 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004640 long vcol_adjusted = vcol; /* removed showbreak length */
4641#ifdef FEAT_LINEBREAK
4642 /* only adjust the tab_len, when at the first column
4643 * after the showbreak value was drawn */
4644 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4645 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004648 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004649 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4650
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004651#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004652 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004653#endif
4654 /* tab amount depends on current column */
4655 n_extra = tab_len;
4656#ifdef FEAT_LINEBREAK
4657 else
4658 {
4659 char_u *p;
4660 int len = n_extra;
4661 int i;
4662 int saved_nextra = n_extra;
4663
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004664#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004665 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004666 /* there are characters to conceal */
4667 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004668 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4669 */
4670 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4671 && n_extra > tab_len)
4672 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004673#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004674
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004675 /* if n_extra > 0, it gives the number of chars, to
4676 * use for a tab, else we need to calculate the width
4677 * for a tab */
4678#ifdef FEAT_MBYTE
4679 len = (tab_len * mb_char2len(lcs_tab2));
4680 if (n_extra > 0)
4681 len += n_extra - tab_len;
4682#endif
4683 c = lcs_tab1;
4684 p = alloc((unsigned)(len + 1));
4685 vim_memset(p, ' ', len);
4686 p[len] = NUL;
4687 p_extra_free = p;
4688 for (i = 0; i < tab_len; i++)
4689 {
4690#ifdef FEAT_MBYTE
4691 mb_char2bytes(lcs_tab2, p);
4692 p += mb_char2len(lcs_tab2);
4693 n_extra += mb_char2len(lcs_tab2)
4694 - (saved_nextra > 0 ? 1 : 0);
4695#else
4696 p[i] = lcs_tab2;
4697#endif
4698 }
4699 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004700#ifdef FEAT_CONCEAL
4701 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4702 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004703 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004704 n_extra -= vcol_off;
4705#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004706 }
4707#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004708#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004709 {
4710 int vc_saved = vcol_off;
4711
4712 /* Tab alignment should be identical regardless of
4713 * 'conceallevel' value. So tab compensates of all
4714 * previous concealed characters, and thus resets
4715 * vcol_off and boguscols accumulated so far in the
4716 * line. Note that the tab can be longer than
4717 * 'tabstop' when there are concealed characters. */
4718 FIX_FOR_BOGUSCOLS;
4719
4720 /* Make sure, the highlighting for the tab char will be
4721 * correctly set further below (effectively reverts the
4722 * FIX_FOR_BOGSUCOLS macro */
4723 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004724 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004725 tab_len += vc_saved;
4726 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#ifdef FEAT_MBYTE
4729 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4730#endif
4731 if (wp->w_p_list)
4732 {
4733 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004734#ifdef FEAT_LINEBREAK
4735 if (wp->w_p_lbr)
4736 c_extra = NUL; /* using p_extra from above */
4737 else
4738#endif
4739 c_extra = lcs_tab2;
4740 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 extra_attr = hl_attr(HLF_8);
4742 saved_attr2 = char_attr; /* save current attr */
4743#ifdef FEAT_MBYTE
4744 mb_c = c;
4745 if (enc_utf8 && (*mb_char2len)(c) > 1)
4746 {
4747 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004748 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004749 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751#endif
4752 }
4753 else
4754 {
4755 c_extra = ' ';
4756 c = ' ';
4757 }
4758 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004759 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004760 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004761 || ((fromcol >= 0 || fromcol_prev >= 0)
4762 && tocol > vcol
4763 && VIsual_mode != Ctrl_V
4764 && (
4765# ifdef FEAT_RIGHTLEFT
4766 wp->w_p_rl ? (col >= 0) :
4767# endif
4768 (col < W_WIDTH(wp)))
4769 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004770 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004771 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004772 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004774 /* Display a '$' after the line or highlight an extra
4775 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4777 /* For a diff line the highlighting continues after the
4778 * "$". */
4779 if (
4780# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004781 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782# ifdef LINE_ATTR
4783 &&
4784# endif
4785# endif
4786# ifdef LINE_ATTR
4787 line_attr == 0
4788# endif
4789 )
4790#endif
4791 {
4792#ifdef FEAT_VIRTUALEDIT
4793 /* In virtualedit, visual selections may extend
4794 * beyond end of line. */
4795 if (area_highlighting && virtual_active()
4796 && tocol != MAXCOL && vcol < tocol)
4797 n_extra = 0;
4798 else
4799#endif
4800 {
4801 p_extra = at_end_str;
4802 n_extra = 1;
4803 c_extra = NUL;
4804 }
4805 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004806 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004807 c = lcs_eol;
4808 else
4809 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 lcs_eol_one = -1;
4811 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004812 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 extra_attr = hl_attr(HLF_AT);
4815 n_attr = 1;
4816 }
4817#ifdef FEAT_MBYTE
4818 mb_c = c;
4819 if (enc_utf8 && (*mb_char2len)(c) > 1)
4820 {
4821 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004822 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004823 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 else
4826 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4827#endif
4828 }
4829 else if (c != NUL)
4830 {
4831 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004832 if (n_extra == 0)
4833 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#ifdef FEAT_RIGHTLEFT
4835 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4836 rl_mirror(p_extra); /* reverse "<12>" */
4837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004839#ifdef FEAT_LINEBREAK
4840 if (wp->w_p_lbr)
4841 {
4842 char_u *p;
4843
4844 c = *p_extra;
4845 p = alloc((unsigned)n_extra + 1);
4846 vim_memset(p, ' ', n_extra);
4847 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4848 p[n_extra] = NUL;
4849 p_extra_free = p_extra = p;
4850 }
4851 else
4852#endif
4853 {
4854 n_extra = byte2cells(c) - 1;
4855 c = *p_extra++;
4856 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004857 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
4859 n_attr = n_extra + 1;
4860 extra_attr = hl_attr(HLF_8);
4861 saved_attr2 = char_attr; /* save current attr */
4862 }
4863#ifdef FEAT_MBYTE
4864 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4865#endif
4866 }
4867#ifdef FEAT_VIRTUALEDIT
4868 else if (VIsual_active
4869 && (VIsual_mode == Ctrl_V
4870 || VIsual_mode == 'v')
4871 && virtual_active()
4872 && tocol != MAXCOL
4873 && vcol < tocol
4874 && (
4875# ifdef FEAT_RIGHTLEFT
4876 wp->w_p_rl ? (col >= 0) :
4877# endif
4878 (col < W_WIDTH(wp))))
4879 {
4880 c = ' ';
4881 --ptr; /* put it back at the NUL */
4882 }
4883#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004884#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 else if ((
4886# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004887 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 ) && (
4891# ifdef FEAT_RIGHTLEFT
4892 wp->w_p_rl ? (col >= 0) :
4893# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004894 (col
4895# ifdef FEAT_CONCEAL
4896 - boguscols
4897# endif
4898 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 /* Highlight until the right side of the window */
4901 c = ' ';
4902 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004903
4904 /* Remember we do the char for line highlighting. */
4905 ++did_line_attr;
4906
4907 /* don't do search HL for the rest of the line */
4908 if (line_attr != 0 && char_attr == search_attr && col > 0)
4909 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910# ifdef FEAT_DIFF
4911 if (diff_hlf == HLF_TXD)
4912 {
4913 diff_hlf = HLF_CHD;
4914 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004917 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4918 char_attr = hl_combine_attr(char_attr,
4919 hl_attr(HLF_CUL));
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922# endif
4923 }
4924#endif
4925 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004926
4927#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004928 if ( wp->w_p_cole > 0
4929 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004930 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004931 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004932 && !(lnum_in_visual_area
4933 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004934 {
4935 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004936 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004937 && (syn_get_sub_char() != NUL || match_conc
4938 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004939 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004940 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004941 /* First time at this concealed item: display one
4942 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004943 if (match_conc)
4944 c = match_conc;
4945 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004946 c = syn_get_sub_char();
4947 else if (lcs_conceal != NUL)
4948 c = lcs_conceal;
4949 else
4950 c = ' ';
4951
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004952 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004953
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004954 if (n_extra > 0)
4955 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004956 vcol += n_extra;
4957 if (wp->w_p_wrap && n_extra > 0)
4958 {
4959# ifdef FEAT_RIGHTLEFT
4960 if (wp->w_p_rl)
4961 {
4962 col -= n_extra;
4963 boguscols -= n_extra;
4964 }
4965 else
4966# endif
4967 {
4968 boguscols += n_extra;
4969 col += n_extra;
4970 }
4971 }
4972 n_extra = 0;
4973 n_attr = 0;
4974 }
4975 else if (n_skip == 0)
4976 {
4977 is_concealing = TRUE;
4978 n_skip = 1;
4979 }
4980# ifdef FEAT_MBYTE
4981 mb_c = c;
4982 if (enc_utf8 && (*mb_char2len)(c) > 1)
4983 {
4984 mb_utf8 = TRUE;
4985 u8cc[0] = 0;
4986 c = 0xc0;
4987 }
4988 else
4989 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4990# endif
4991 }
4992 else
4993 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004994 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004995 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004996 }
4997#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005000#ifdef FEAT_CONCEAL
5001 /* In the cursor line and we may be concealing characters: correct
5002 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005003 if (!did_wcol && draw_state == WL_LINE
5004 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005005 && conceal_cursor_line(wp)
5006 && (int)wp->w_virtcol <= vcol + n_skip)
5007 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005008# ifdef FEAT_RIGHTLEFT
5009 if (wp->w_p_rl)
5010 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5011 else
5012# endif
5013 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005014 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005015 did_wcol = TRUE;
5016 }
5017#endif
5018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /* Don't override visual selection highlighting. */
5020 if (n_attr > 0
5021 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005022 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 char_attr = extra_attr;
5024
Bram Moolenaar81695252004-12-29 20:58:21 +00005025#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 /* XIM don't send preedit_start and preedit_end, but they send
5027 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5028 * im_is_preediting() here. */
5029 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005030 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 && (State & INSERT)
5032 && !p_imdisable
5033 && im_is_preediting()
5034 && draw_state == WL_LINE)
5035 {
5036 colnr_T tcol;
5037
5038 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005039 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 else
5041 tcol = preedit_end_col;
5042 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5043 {
5044 if (feedback_old_attr < 0)
5045 {
5046 feedback_col = 0;
5047 feedback_old_attr = char_attr;
5048 }
5049 char_attr = im_get_feedback_attr(feedback_col);
5050 if (char_attr < 0)
5051 char_attr = feedback_old_attr;
5052 feedback_col++;
5053 }
5054 else if (feedback_old_attr >= 0)
5055 {
5056 char_attr = feedback_old_attr;
5057 feedback_old_attr = -1;
5058 feedback_col = 0;
5059 }
5060 }
5061#endif
5062 /*
5063 * Handle the case where we are in column 0 but not on the first
5064 * character of the line and the user wants us to show us a
5065 * special character (via 'listchars' option "precedes:<char>".
5066 */
5067 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005068 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5070#ifdef FEAT_DIFF
5071 && filler_todo <= 0
5072#endif
5073 && draw_state > WL_NR
5074 && c != NUL)
5075 {
5076 c = lcs_prec;
5077 lcs_prec_todo = NUL;
5078#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005079 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5080 {
5081 /* Double-width character being overwritten by the "precedes"
5082 * character, need to fill up half the character. */
5083 c_extra = MB_FILLER_CHAR;
5084 n_extra = 1;
5085 n_attr = 2;
5086 extra_attr = hl_attr(HLF_AT);
5087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 mb_c = c;
5089 if (enc_utf8 && (*mb_char2len)(c) > 1)
5090 {
5091 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005092 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 else
5096 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5097#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005098 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100 saved_attr3 = char_attr; /* save current attr */
5101 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5102 n_attr3 = 1;
5103 }
5104 }
5105
5106 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005107 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005109 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005110#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005111 || did_line_attr == 1
5112#endif
5113 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005115#ifdef FEAT_SEARCH_EXTRA
5116 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005117
5118 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005119 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005120 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005121#endif
5122
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005123 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 * highlight match at end of line. If it's beyond the last
5125 * char on the screen, just overwrite that one (tricky!) Not
5126 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005127#ifdef FEAT_SEARCH_EXTRA
5128 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005129 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005130 prevcol_hl_flag = TRUE;
5131 else
5132 {
5133 cur = wp->w_match_head;
5134 while (cur != NULL)
5135 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005136 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005137 {
5138 prevcol_hl_flag = TRUE;
5139 break;
5140 }
5141 cur = cur->next;
5142 }
5143 }
5144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005146 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005147 && (VIsual_mode != Ctrl_V
5148 || lnum == VIsual.lnum
5149 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005150 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#ifdef FEAT_SEARCH_EXTRA
5152 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005153 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005154# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005155 && did_line_attr <= 1
5156# endif
5157 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#endif
5159 ))
5160 {
5161 int n = 0;
5162
5163#ifdef FEAT_RIGHTLEFT
5164 if (wp->w_p_rl)
5165 {
5166 if (col < 0)
5167 n = 1;
5168 }
5169 else
5170#endif
5171 {
5172 if (col >= W_WIDTH(wp))
5173 n = -1;
5174 }
5175 if (n != 0)
5176 {
5177 /* At the window boundary, highlight the last character
5178 * instead (better than nothing). */
5179 off += n;
5180 col += n;
5181 }
5182 else
5183 {
5184 /* Add a blank character to highlight. */
5185 ScreenLines[off] = ' ';
5186#ifdef FEAT_MBYTE
5187 if (enc_utf8)
5188 ScreenLinesUC[off] = 0;
5189#endif
5190 }
5191#ifdef FEAT_SEARCH_EXTRA
5192 if (area_attr == 0)
5193 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005194 /* Use attributes from match with highest priority among
5195 * 'search_hl' and the match list. */
5196 char_attr = search_hl.attr;
5197 cur = wp->w_match_head;
5198 shl_flag = FALSE;
5199 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005200 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005201 if (shl_flag == FALSE
5202 && ((cur != NULL
5203 && cur->priority > SEARCH_HL_PRIORITY)
5204 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005205 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005206 shl = &search_hl;
5207 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005208 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005209 else
5210 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005211 if ((ptr - line) - 1 == (long)shl->startcol
5212 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005213 char_attr = shl->attr;
5214 if (shl != &search_hl && cur != NULL)
5215 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218#endif
5219 ScreenAttrs[off] = char_attr;
5220#ifdef FEAT_RIGHTLEFT
5221 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005224 --off;
5225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 else
5227#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005230 ++off;
5231 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005232 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005233#ifdef FEAT_SYN_HL
5234 eol_hl_off = 1;
5235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar91170f82006-05-05 21:15:17 +00005239 /*
5240 * At end of the text line.
5241 */
5242 if (c == NUL)
5243 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005244#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005245 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5246 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005247 {
5248 /* highlight last char after line */
5249 --col;
5250 --off;
5251 --vcol;
5252 }
5253
Bram Moolenaar1a384422010-07-14 19:53:30 +02005254 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005255 if (wp->w_p_wrap)
5256 v = wp->w_skipcol;
5257 else
5258 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005259
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005260 /* check if line ends before left margin */
5261 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005262 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005263#ifdef FEAT_CONCEAL
5264 /* Get rid of the boguscols now, we want to draw until the right
5265 * edge for 'cursorcolumn'. */
5266 col -= boguscols;
5267 boguscols = 0;
5268#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005269
5270 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005271 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005272
5273 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005274 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5275 && (int)wp->w_virtcol <
5276 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005277 && lnum != wp->w_cursor.lnum)
5278 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005279# ifdef FEAT_RIGHTLEFT
5280 && !wp->w_p_rl
5281# endif
5282 )
5283 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005284 int rightmost_vcol = 0;
5285 int i;
5286
5287 if (wp->w_p_cuc)
5288 rightmost_vcol = wp->w_virtcol;
5289 if (draw_color_col)
5290 /* determine rightmost colorcolumn to possibly draw */
5291 for (i = 0; color_cols[i] >= 0; ++i)
5292 if (rightmost_vcol < color_cols[i])
5293 rightmost_vcol = color_cols[i];
5294
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005295 while (col < W_WIDTH(wp))
5296 {
5297 ScreenLines[off] = ' ';
5298#ifdef FEAT_MBYTE
5299 if (enc_utf8)
5300 ScreenLinesUC[off] = 0;
5301#endif
5302 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005303 if (draw_color_col)
5304 draw_color_col = advance_color_col(VCOL_HLC,
5305 &color_cols);
5306
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005307 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005308 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005309 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005310 ScreenAttrs[off++] = hl_attr(HLF_MC);
5311 else
5312 ScreenAttrs[off++] = 0;
5313
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005314 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005315 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005316
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005317 ++vcol;
5318 }
5319 }
5320#endif
5321
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5323 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 row++;
5325
5326 /*
5327 * Update w_cline_height and w_cline_folded if the cursor line was
5328 * updated (saves a call to plines() later).
5329 */
5330 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5331 {
5332 curwin->w_cline_row = startrow;
5333 curwin->w_cline_height = row - startrow;
5334#ifdef FEAT_FOLDING
5335 curwin->w_cline_folded = FALSE;
5336#endif
5337 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5338 }
5339
5340 break;
5341 }
5342
5343 /* line continues beyond line end */
5344 if (lcs_ext
5345 && !wp->w_p_wrap
5346#ifdef FEAT_DIFF
5347 && filler_todo <= 0
5348#endif
5349 && (
5350#ifdef FEAT_RIGHTLEFT
5351 wp->w_p_rl ? col == 0 :
5352#endif
5353 col == W_WIDTH(wp) - 1)
5354 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005355 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5357 {
5358 c = lcs_ext;
5359 char_attr = hl_attr(HLF_AT);
5360#ifdef FEAT_MBYTE
5361 mb_c = c;
5362 if (enc_utf8 && (*mb_char2len)(c) > 1)
5363 {
5364 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005365 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005366 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368 else
5369 mb_utf8 = FALSE;
5370#endif
5371 }
5372
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005373#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005374 /* advance to the next 'colorcolumn' */
5375 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005376 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005377
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005378 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005379 * highlight the cursor position itself.
5380 * Also highlight the 'colorcolumn' if it is different than
5381 * 'cursorcolumn' */
5382 vcol_save_attr = -1;
5383 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005384 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005385 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005386 && lnum != wp->w_cursor.lnum)
5387 {
5388 vcol_save_attr = char_attr;
5389 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5390 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005391 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005392 {
5393 vcol_save_attr = char_attr;
5394 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5395 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005396 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005397#endif
5398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 /*
5400 * Store character to be displayed.
5401 * Skip characters that are left of the screen for 'nowrap'.
5402 */
5403 vcol_prev = vcol;
5404 if (draw_state < WL_LINE || n_skip <= 0)
5405 {
5406 /*
5407 * Store the character.
5408 */
5409#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5410 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5411 {
5412 /* A double-wide character is: put first halve in left cell. */
5413 --off;
5414 --col;
5415 }
5416#endif
5417 ScreenLines[off] = c;
5418#ifdef FEAT_MBYTE
5419 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005420 {
5421 if ((mb_c & 0xff00) == 0x8e00)
5422 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else if (enc_utf8)
5426 {
5427 if (mb_utf8)
5428 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005429 int i;
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005432 if ((c & 0xff) == 0)
5433 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005434 for (i = 0; i < Screen_mco; ++i)
5435 {
5436 ScreenLinesC[i][off] = u8cc[i];
5437 if (u8cc[i] == 0)
5438 break;
5439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 }
5441 else
5442 ScreenLinesUC[off] = 0;
5443 }
5444 if (multi_attr)
5445 {
5446 ScreenAttrs[off] = multi_attr;
5447 multi_attr = 0;
5448 }
5449 else
5450#endif
5451 ScreenAttrs[off] = char_attr;
5452
5453#ifdef FEAT_MBYTE
5454 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5455 {
5456 /* Need to fill two screen columns. */
5457 ++off;
5458 ++col;
5459 if (enc_utf8)
5460 /* UTF-8: Put a 0 in the second screen char. */
5461 ScreenLines[off] = 0;
5462 else
5463 /* DBCS: Put second byte in the second screen char. */
5464 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005465 if (draw_state > WL_NR
5466#ifdef FEAT_DIFF
5467 && filler_todo <= 0
5468#endif
5469 )
5470 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 /* When "tocol" is halfway a character, set it to the end of
5472 * the character, otherwise highlighting won't stop. */
5473 if (tocol == vcol)
5474 ++tocol;
5475#ifdef FEAT_RIGHTLEFT
5476 if (wp->w_p_rl)
5477 {
5478 /* now it's time to backup one cell */
5479 --off;
5480 --col;
5481 }
5482#endif
5483 }
5484#endif
5485#ifdef FEAT_RIGHTLEFT
5486 if (wp->w_p_rl)
5487 {
5488 --off;
5489 --col;
5490 }
5491 else
5492#endif
5493 {
5494 ++off;
5495 ++col;
5496 }
5497 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005498#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005499 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005500 {
5501 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005502 ++vcol_off;
5503 if (n_extra > 0)
5504 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005505 if (wp->w_p_wrap)
5506 {
5507 /*
5508 * Special voodoo required if 'wrap' is on.
5509 *
5510 * Advance the column indicator to force the line
5511 * drawing to wrap early. This will make the line
5512 * take up the same screen space when parts are concealed,
5513 * so that cursor line computations aren't messed up.
5514 *
5515 * To avoid the fictitious advance of 'col' causing
5516 * trailing junk to be written out of the screen line
5517 * we are building, 'boguscols' keeps track of the number
5518 * of bad columns we have advanced.
5519 */
5520 if (n_extra > 0)
5521 {
5522 vcol += n_extra;
5523# ifdef FEAT_RIGHTLEFT
5524 if (wp->w_p_rl)
5525 {
5526 col -= n_extra;
5527 boguscols -= n_extra;
5528 }
5529 else
5530# endif
5531 {
5532 col += n_extra;
5533 boguscols += n_extra;
5534 }
5535 n_extra = 0;
5536 n_attr = 0;
5537 }
5538
5539
5540# ifdef FEAT_MBYTE
5541 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5542 {
5543 /* Need to fill two screen columns. */
5544# ifdef FEAT_RIGHTLEFT
5545 if (wp->w_p_rl)
5546 {
5547 --boguscols;
5548 --col;
5549 }
5550 else
5551# endif
5552 {
5553 ++boguscols;
5554 ++col;
5555 }
5556 }
5557# endif
5558
5559# ifdef FEAT_RIGHTLEFT
5560 if (wp->w_p_rl)
5561 {
5562 --boguscols;
5563 --col;
5564 }
5565 else
5566# endif
5567 {
5568 ++boguscols;
5569 ++col;
5570 }
5571 }
5572 else
5573 {
5574 if (n_extra > 0)
5575 {
5576 vcol += n_extra;
5577 n_extra = 0;
5578 n_attr = 0;
5579 }
5580 }
5581
5582 }
5583#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 else
5585 --n_skip;
5586
Bram Moolenaar64486672010-05-16 15:46:46 +02005587 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5588 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005589 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590#ifdef FEAT_DIFF
5591 && filler_todo <= 0
5592#endif
5593 )
5594 ++vcol;
5595
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005596#ifdef FEAT_SYN_HL
5597 if (vcol_save_attr >= 0)
5598 char_attr = vcol_save_attr;
5599#endif
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 /* restore attributes after "predeces" in 'listchars' */
5602 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5603 char_attr = saved_attr3;
5604
5605 /* restore attributes after last 'listchars' or 'number' char */
5606 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5607 char_attr = saved_attr2;
5608
5609 /*
5610 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005611 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 */
5613 if ((
5614#ifdef FEAT_RIGHTLEFT
5615 wp->w_p_rl ? (col < 0) :
5616#endif
5617 (col >= W_WIDTH(wp)))
5618 && (*ptr != NUL
5619#ifdef FEAT_DIFF
5620 || filler_todo > 0
5621#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005622 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5624 )
5625 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005626#ifdef FEAT_CONCEAL
5627 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5628 (int)W_WIDTH(wp), wp->w_p_rl);
5629 boguscols = 0;
5630#else
5631 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5632 (int)W_WIDTH(wp), wp->w_p_rl);
5633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 ++row;
5635 ++screen_row;
5636
5637 /* When not wrapping and finished diff lines, or when displayed
5638 * '$' and highlighting until last column, break here. */
5639 if ((!wp->w_p_wrap
5640#ifdef FEAT_DIFF
5641 && filler_todo <= 0
5642#endif
5643 ) || lcs_eol_one == -1)
5644 break;
5645
5646 /* When the window is too narrow draw all "@" lines. */
5647 if (draw_state != WL_LINE
5648#ifdef FEAT_DIFF
5649 && filler_todo <= 0
5650#endif
5651 )
5652 {
5653 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005654#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 draw_vsep_win(wp, row);
5656#endif
5657 row = endrow;
5658 }
5659
5660 /* When line got too long for screen break here. */
5661 if (row == endrow)
5662 {
5663 ++row;
5664 break;
5665 }
5666
5667 if (screen_cur_row == screen_row - 1
5668#ifdef FEAT_DIFF
5669 && filler_todo <= 0
5670#endif
5671 && W_WIDTH(wp) == Columns)
5672 {
5673 /* Remember that the line wraps, used for modeless copy. */
5674 LineWraps[screen_row - 1] = TRUE;
5675
5676 /*
5677 * Special trick to make copy/paste of wrapped lines work with
5678 * xterm/screen: write an extra character beyond the end of
5679 * the line. This will work with all terminal types
5680 * (regardless of the xn,am settings).
5681 * Only do this on a fast tty.
5682 * Only do this if the cursor is on the current line
5683 * (something has been written in it).
5684 * Don't do this for the GUI.
5685 * Don't do this for double-width characters.
5686 * Don't do this for a window not at the right screen border.
5687 */
5688 if (p_tf
5689#ifdef FEAT_GUI
5690 && !gui.in_use
5691#endif
5692#ifdef FEAT_MBYTE
5693 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005694 && ((*mb_off2cells)(LineOffset[screen_row],
5695 LineOffset[screen_row] + screen_Columns)
5696 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005698 + (int)Columns - 2,
5699 LineOffset[screen_row] + screen_Columns)
5700 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#endif
5702 )
5703 {
5704 /* First make sure we are at the end of the screen line,
5705 * then output the same character again to let the
5706 * terminal know about the wrap. If the terminal doesn't
5707 * auto-wrap, we overwrite the character. */
5708 if (screen_cur_col != W_WIDTH(wp))
5709 screen_char(LineOffset[screen_row - 1]
5710 + (unsigned)Columns - 1,
5711 screen_row - 1, (int)(Columns - 1));
5712
5713#ifdef FEAT_MBYTE
5714 /* When there is a multi-byte character, just output a
5715 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005716 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5717 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 out_char(' ');
5719 else
5720#endif
5721 out_char(ScreenLines[LineOffset[screen_row - 1]
5722 + (Columns - 1)]);
5723 /* force a redraw of the first char on the next line */
5724 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5725 screen_start(); /* don't know where cursor is now */
5726 }
5727 }
5728
5729 col = 0;
5730 off = (unsigned)(current_ScreenLine - ScreenLines);
5731#ifdef FEAT_RIGHTLEFT
5732 if (wp->w_p_rl)
5733 {
5734 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5735 off += col;
5736 }
5737#endif
5738
5739 /* reset the drawing state for the start of a wrapped line */
5740 draw_state = WL_START;
5741 saved_n_extra = n_extra;
5742 saved_p_extra = p_extra;
5743 saved_c_extra = c_extra;
5744 saved_char_attr = char_attr;
5745 n_extra = 0;
5746 lcs_prec_todo = lcs_prec;
5747#ifdef FEAT_LINEBREAK
5748# ifdef FEAT_DIFF
5749 if (filler_todo <= 0)
5750# endif
5751 need_showbreak = TRUE;
5752#endif
5753#ifdef FEAT_DIFF
5754 --filler_todo;
5755 /* When the filler lines are actually below the last line of the
5756 * file, don't draw the line itself, break here. */
5757 if (filler_todo == 0 && wp->w_botfill)
5758 break;
5759#endif
5760 }
5761
5762 } /* for every character in the line */
5763
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005764#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005765 /* After an empty line check first word for capital. */
5766 if (*skipwhite(line) == NUL)
5767 {
5768 capcol_lnum = lnum + 1;
5769 cap_col = 0;
5770 }
5771#endif
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 return row;
5774}
5775
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005776#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005777static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005778
5779/*
5780 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005781 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005782 */
5783 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005784comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005785{
5786 int i;
5787
5788 for (i = 0; i < Screen_mco; ++i)
5789 {
5790 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5791 return TRUE;
5792 if (ScreenLinesC[i][off_from] == 0)
5793 break;
5794 }
5795 return FALSE;
5796}
5797#endif
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799/*
5800 * Check whether the given character needs redrawing:
5801 * - the (first byte of the) character is different
5802 * - the attributes are different
5803 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005804 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
5806 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005807char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 if (cols > 0
5810 && ((ScreenLines[off_from] != ScreenLines[off_to]
5811 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5812
5813#ifdef FEAT_MBYTE
5814 || (enc_dbcs != 0
5815 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5816 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5817 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5818 : (cols > 1 && ScreenLines[off_from + 1]
5819 != ScreenLines[off_to + 1])))
5820 || (enc_utf8
5821 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5822 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005823 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005824 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5825 && ScreenLines[off_from + 1]
5826 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
5828 ))
5829 return TRUE;
5830 return FALSE;
5831}
5832
5833/*
5834 * Move one "cooked" screen line to the screen, but only the characters that
5835 * have actually changed. Handle insert/delete character.
5836 * "coloff" gives the first column on the screen for this line.
5837 * "endcol" gives the columns where valid characters are.
5838 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5839 * needs to be cleared, negative otherwise.
5840 * "rlflag" is TRUE in a rightleft window:
5841 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5842 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5843 */
5844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005845screen_line(
5846 int row,
5847 int coloff,
5848 int endcol,
5849 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005851 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 unsigned off_from;
5856 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005857#ifdef FEAT_MBYTE
5858 unsigned max_off_from;
5859 unsigned max_off_to;
5860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005862#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 int hl;
5864#endif
5865 int force = FALSE; /* force update rest of the line */
5866 int redraw_this /* bool: does character need redraw? */
5867#ifdef FEAT_GUI
5868 = TRUE /* For GUI when while-loop empty */
5869#endif
5870 ;
5871 int redraw_next; /* redraw_this for next character */
5872#ifdef FEAT_MBYTE
5873 int clear_next = FALSE;
5874 int char_cells; /* 1: normal char */
5875 /* 2: occupies two display cells */
5876# define CHAR_CELLS char_cells
5877#else
5878# define CHAR_CELLS 1
5879#endif
5880
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005881 /* Check for illegal row and col, just in case. */
5882 if (row >= Rows)
5883 row = Rows - 1;
5884 if (endcol > Columns)
5885 endcol = Columns;
5886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887# ifdef FEAT_CLIPBOARD
5888 clip_may_clear_selection(row, row);
5889# endif
5890
5891 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5892 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005893#ifdef FEAT_MBYTE
5894 max_off_from = off_from + screen_Columns;
5895 max_off_to = LineOffset[row] + screen_Columns;
5896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897
5898#ifdef FEAT_RIGHTLEFT
5899 if (rlflag)
5900 {
5901 /* Clear rest first, because it's left of the text. */
5902 if (clear_width > 0)
5903 {
5904 while (col <= endcol && ScreenLines[off_to] == ' '
5905 && ScreenAttrs[off_to] == 0
5906# ifdef FEAT_MBYTE
5907 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5908# endif
5909 )
5910 {
5911 ++off_to;
5912 ++col;
5913 }
5914 if (col <= endcol)
5915 screen_fill(row, row + 1, col + coloff,
5916 endcol + coloff + 1, ' ', ' ', 0);
5917 }
5918 col = endcol + 1;
5919 off_to = LineOffset[row] + col + coloff;
5920 off_from += col;
5921 endcol = (clear_width > 0 ? clear_width : -clear_width);
5922 }
5923#endif /* FEAT_RIGHTLEFT */
5924
5925 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5926
5927 while (col < endcol)
5928 {
5929#ifdef FEAT_MBYTE
5930 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005931 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 else
5933 char_cells = 1;
5934#endif
5935
5936 redraw_this = redraw_next;
5937 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5938 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5939
5940#ifdef FEAT_GUI
5941 /* If the next character was bold, then redraw the current character to
5942 * remove any pixels that might have spilt over into us. This only
5943 * happens in the GUI.
5944 */
5945 if (redraw_next && gui.in_use)
5946 {
5947 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005948 if (hl > HL_ALL)
5949 hl = syn_attr2attr(hl);
5950 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 redraw_this = TRUE;
5952 }
5953#endif
5954
5955 if (redraw_this)
5956 {
5957 /*
5958 * Special handling when 'xs' termcap flag set (hpterm):
5959 * Attributes for characters are stored at the position where the
5960 * cursor is when writing the highlighting code. The
5961 * start-highlighting code must be written with the cursor on the
5962 * first highlighted character. The stop-highlighting code must
5963 * be written with the cursor just after the last highlighted
5964 * character.
5965 * Overwriting a character doesn't remove it's highlighting. Need
5966 * to clear the rest of the line, and force redrawing it
5967 * completely.
5968 */
5969 if ( p_wiv
5970 && !force
5971#ifdef FEAT_GUI
5972 && !gui.in_use
5973#endif
5974 && ScreenAttrs[off_to] != 0
5975 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5976 {
5977 /*
5978 * Need to remove highlighting attributes here.
5979 */
5980 windgoto(row, col + coloff);
5981 out_str(T_CE); /* clear rest of this screen line */
5982 screen_start(); /* don't know where cursor is now */
5983 force = TRUE; /* force redraw of rest of the line */
5984 redraw_next = TRUE; /* or else next char would miss out */
5985
5986 /*
5987 * If the previous character was highlighted, need to stop
5988 * highlighting at this character.
5989 */
5990 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5991 {
5992 screen_attr = ScreenAttrs[off_to - 1];
5993 term_windgoto(row, col + coloff);
5994 screen_stop_highlight();
5995 }
5996 else
5997 screen_attr = 0; /* highlighting has stopped */
5998 }
5999#ifdef FEAT_MBYTE
6000 if (enc_dbcs != 0)
6001 {
6002 /* Check if overwriting a double-byte with a single-byte or
6003 * the other way around requires another character to be
6004 * redrawn. For UTF-8 this isn't needed, because comparing
6005 * ScreenLinesUC[] is sufficient. */
6006 if (char_cells == 1
6007 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006008 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
6010 /* Writing a single-cell character over a double-cell
6011 * character: need to redraw the next cell. */
6012 ScreenLines[off_to + 1] = 0;
6013 redraw_next = TRUE;
6014 }
6015 else if (char_cells == 2
6016 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006017 && (*mb_off2cells)(off_to, max_off_to) == 1
6018 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
6020 /* Writing the second half of a double-cell character over
6021 * a double-cell character: need to redraw the second
6022 * cell. */
6023 ScreenLines[off_to + 2] = 0;
6024 redraw_next = TRUE;
6025 }
6026
6027 if (enc_dbcs == DBCS_JPNU)
6028 ScreenLines2[off_to] = ScreenLines2[off_from];
6029 }
6030 /* When writing a single-width character over a double-width
6031 * character and at the end of the redrawn text, need to clear out
6032 * the right halve of the old character.
6033 * Also required when writing the right halve of a double-width
6034 * char over the left halve of an existing one. */
6035 if (has_mbyte && col + char_cells == endcol
6036 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006037 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006039 && (*mb_off2cells)(off_to, max_off_to) == 1
6040 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 clear_next = TRUE;
6042#endif
6043
6044 ScreenLines[off_to] = ScreenLines[off_from];
6045#ifdef FEAT_MBYTE
6046 if (enc_utf8)
6047 {
6048 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6049 if (ScreenLinesUC[off_from] != 0)
6050 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006051 int i;
6052
6053 for (i = 0; i < Screen_mco; ++i)
6054 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 }
6056 }
6057 if (char_cells == 2)
6058 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6059#endif
6060
6061#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006062 /* The bold trick makes a single column of pixels appear in the
6063 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * character should be redrawn too. This happens for our own GUI
6065 * and for some xterms. */
6066 if (
6067# ifdef FEAT_GUI
6068 gui.in_use
6069# endif
6070# if defined(FEAT_GUI) && defined(UNIX)
6071 ||
6072# endif
6073# ifdef UNIX
6074 term_is_xterm
6075# endif
6076 )
6077 {
6078 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006079 if (hl > HL_ALL)
6080 hl = syn_attr2attr(hl);
6081 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 redraw_next = TRUE;
6083 }
6084#endif
6085 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6086#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006087 /* For simplicity set the attributes of second half of a
6088 * double-wide character equal to the first half. */
6089 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006091
6092 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 else
6095#endif
6096 screen_char(off_to, row, col + coloff);
6097 }
6098 else if ( p_wiv
6099#ifdef FEAT_GUI
6100 && !gui.in_use
6101#endif
6102 && col + coloff > 0)
6103 {
6104 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6105 {
6106 /*
6107 * Don't output stop-highlight when moving the cursor, it will
6108 * stop the highlighting when it should continue.
6109 */
6110 screen_attr = 0;
6111 }
6112 else if (screen_attr != 0)
6113 screen_stop_highlight();
6114 }
6115
6116 off_to += CHAR_CELLS;
6117 off_from += CHAR_CELLS;
6118 col += CHAR_CELLS;
6119 }
6120
6121#ifdef FEAT_MBYTE
6122 if (clear_next)
6123 {
6124 /* Clear the second half of a double-wide character of which the left
6125 * half was overwritten with a single-wide character. */
6126 ScreenLines[off_to] = ' ';
6127 if (enc_utf8)
6128 ScreenLinesUC[off_to] = 0;
6129 screen_char(off_to, row, col + coloff);
6130 }
6131#endif
6132
6133 if (clear_width > 0
6134#ifdef FEAT_RIGHTLEFT
6135 && !rlflag
6136#endif
6137 )
6138 {
6139#ifdef FEAT_GUI
6140 int startCol = col;
6141#endif
6142
6143 /* blank out the rest of the line */
6144 while (col < clear_width && ScreenLines[off_to] == ' '
6145 && ScreenAttrs[off_to] == 0
6146#ifdef FEAT_MBYTE
6147 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6148#endif
6149 )
6150 {
6151 ++off_to;
6152 ++col;
6153 }
6154 if (col < clear_width)
6155 {
6156#ifdef FEAT_GUI
6157 /*
6158 * In the GUI, clearing the rest of the line may leave pixels
6159 * behind if the first character cleared was bold. Some bold
6160 * fonts spill over the left. In this case we redraw the previous
6161 * character too. If we didn't skip any blanks above, then we
6162 * only redraw if the character wasn't already redrawn anyway.
6163 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006164 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
6166 hl = ScreenAttrs[off_to];
6167 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006168 {
6169 int prev_cells = 1;
6170# ifdef FEAT_MBYTE
6171 if (enc_utf8)
6172 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6173 * that its width is 2. */
6174 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6175 else if (enc_dbcs != 0)
6176 {
6177 /* find previous character by counting from first
6178 * column and get its width. */
6179 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006180 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006181
6182 while (off < off_to)
6183 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006184 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006185 off += prev_cells;
6186 }
6187 }
6188
6189 if (enc_dbcs != 0 && prev_cells > 1)
6190 screen_char_2(off_to - prev_cells, row,
6191 col + coloff - prev_cells);
6192 else
6193# endif
6194 screen_char(off_to - prev_cells, row,
6195 col + coloff - prev_cells);
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198#endif
6199 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6200 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006201#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 off_to += clear_width - col;
6203 col = clear_width;
6204#endif
6205 }
6206 }
6207
6208 if (clear_width > 0)
6209 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006210#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /* For a window that's left of another, draw the separator char. */
6212 if (col + coloff < Columns)
6213 {
6214 int c;
6215
6216 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006217 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006219 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6220 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221# endif
6222 || ScreenAttrs[off_to] != hl)
6223 {
6224 ScreenLines[off_to] = c;
6225 ScreenAttrs[off_to] = hl;
6226# ifdef FEAT_MBYTE
6227 if (enc_utf8)
6228 {
6229 if (c >= 0x80)
6230 {
6231 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006232 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
6234 else
6235 ScreenLinesUC[off_to] = 0;
6236 }
6237# endif
6238 screen_char(off_to, row, col + coloff);
6239 }
6240 }
6241 else
6242#endif
6243 LineWraps[row] = FALSE;
6244 }
6245}
6246
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006247#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006249 * Mirror text "str" for right-left displaying.
6250 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006253rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 char_u *p1, *p2;
6256 int t;
6257
6258 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6259 {
6260 t = *p1;
6261 *p1 = *p2;
6262 *p2 = t;
6263 }
6264}
6265#endif
6266
6267#if defined(FEAT_WINDOWS) || defined(PROTO)
6268/*
6269 * mark all status lines for redraw; used after first :cd
6270 */
6271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006272status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 win_T *wp;
6275
Bram Moolenaar29323592016-07-24 22:04:11 +02006276 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 if (wp->w_status_height)
6278 {
6279 wp->w_redr_status = TRUE;
6280 redraw_later(VALID);
6281 }
6282}
6283
6284/*
6285 * mark all status lines of the current buffer for redraw
6286 */
6287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006288status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 win_T *wp;
6291
Bram Moolenaar29323592016-07-24 22:04:11 +02006292 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6294 {
6295 wp->w_redr_status = TRUE;
6296 redraw_later(VALID);
6297 }
6298}
6299
6300/*
6301 * Redraw all status lines that need to be redrawn.
6302 */
6303 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006304redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 win_T *wp;
6307
Bram Moolenaar29323592016-07-24 22:04:11 +02006308 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (wp->w_redr_status)
6310 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006311 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006312 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313}
6314#endif
6315
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006316#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317/*
6318 * Redraw all status lines at the bottom of frame "frp".
6319 */
6320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006321win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 if (frp->fr_layout == FR_LEAF)
6324 frp->fr_win->w_redr_status = TRUE;
6325 else if (frp->fr_layout == FR_ROW)
6326 {
6327 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6328 win_redraw_last_status(frp);
6329 }
6330 else /* frp->fr_layout == FR_COL */
6331 {
6332 frp = frp->fr_child;
6333 while (frp->fr_next != NULL)
6334 frp = frp->fr_next;
6335 win_redraw_last_status(frp);
6336 }
6337}
6338#endif
6339
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006340#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341/*
6342 * Draw the verticap separator right of window "wp" starting with line "row".
6343 */
6344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006345draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 int hl;
6348 int c;
6349
6350 if (wp->w_vsep_width)
6351 {
6352 /* draw the vertical separator right of this window */
6353 c = fillchar_vsep(&hl);
6354 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6355 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6356 c, ' ', hl);
6357 }
6358}
6359#endif
6360
6361#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006362static int status_match_len(expand_T *xp, char_u *s);
6363static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006366 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 */
6368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006369status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 int len = 0;
6372
6373#ifdef FEAT_MENU
6374 int emenu = (xp->xp_context == EXPAND_MENUS
6375 || xp->xp_context == EXPAND_MENUNAMES);
6376
6377 /* Check for menu separators - replace with '|'. */
6378 if (emenu && menu_is_separator(s))
6379 return 1;
6380#endif
6381
6382 while (*s != NUL)
6383 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006384 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006385 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006386 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
6388
6389 return len;
6390}
6391
6392/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006393 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006394 * These are backslashes used for escaping. Do show backslashes in help tags.
6395 */
6396 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006397skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006398{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006399 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006400#ifdef FEAT_MENU
6401 || ((xp->xp_context == EXPAND_MENUS
6402 || xp->xp_context == EXPAND_MENUNAMES)
6403 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6404#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006405 )
6406 {
6407#ifndef BACKSLASH_IN_FILENAME
6408 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6409 return 2;
6410#endif
6411 return 1;
6412 }
6413 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006414}
6415
6416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 * Show wildchar matches in the status line.
6418 * Show at least the "match" item.
6419 * We start at item 'first_match' in the list and show all matches that fit.
6420 *
6421 * If inversion is possible we use it. Else '=' characters are used.
6422 */
6423 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006424win_redr_status_matches(
6425 expand_T *xp,
6426 int num_matches,
6427 char_u **matches, /* list of matches */
6428 int match,
6429 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6432 int row;
6433 char_u *buf;
6434 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 int fillchar;
6437 int attr;
6438 int i;
6439 int highlight = TRUE;
6440 char_u *selstart = NULL;
6441 int selstart_col = 0;
6442 char_u *selend = NULL;
6443 static int first_match = 0;
6444 int add_left = FALSE;
6445 char_u *s;
6446#ifdef FEAT_MENU
6447 int emenu;
6448#endif
6449#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6450 int l;
6451#endif
6452
6453 if (matches == NULL) /* interrupted completion? */
6454 return;
6455
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006456#ifdef FEAT_MBYTE
6457 if (has_mbyte)
6458 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6459 else
6460#endif
6461 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (buf == NULL)
6463 return;
6464
6465 if (match == -1) /* don't show match but original text */
6466 {
6467 match = 0;
6468 highlight = FALSE;
6469 }
6470 /* count 1 for the ending ">" */
6471 clen = status_match_len(xp, L_MATCH(match)) + 3;
6472 if (match == 0)
6473 first_match = 0;
6474 else if (match < first_match)
6475 {
6476 /* jumping left, as far as we can go */
6477 first_match = match;
6478 add_left = TRUE;
6479 }
6480 else
6481 {
6482 /* check if match fits on the screen */
6483 for (i = first_match; i < match; ++i)
6484 clen += status_match_len(xp, L_MATCH(i)) + 2;
6485 if (first_match > 0)
6486 clen += 2;
6487 /* jumping right, put match at the left */
6488 if ((long)clen > Columns)
6489 {
6490 first_match = match;
6491 /* if showing the last match, we can add some on the left */
6492 clen = 2;
6493 for (i = match; i < num_matches; ++i)
6494 {
6495 clen += status_match_len(xp, L_MATCH(i)) + 2;
6496 if ((long)clen >= Columns)
6497 break;
6498 }
6499 if (i == num_matches)
6500 add_left = TRUE;
6501 }
6502 }
6503 if (add_left)
6504 while (first_match > 0)
6505 {
6506 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6507 if ((long)clen >= Columns)
6508 break;
6509 --first_match;
6510 }
6511
6512 fillchar = fillchar_status(&attr, TRUE);
6513
6514 if (first_match == 0)
6515 {
6516 *buf = NUL;
6517 len = 0;
6518 }
6519 else
6520 {
6521 STRCPY(buf, "< ");
6522 len = 2;
6523 }
6524 clen = len;
6525
6526 i = first_match;
6527 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6528 {
6529 if (i == match)
6530 {
6531 selstart = buf + len;
6532 selstart_col = clen;
6533 }
6534
6535 s = L_MATCH(i);
6536 /* Check for menu separators - replace with '|' */
6537#ifdef FEAT_MENU
6538 emenu = (xp->xp_context == EXPAND_MENUS
6539 || xp->xp_context == EXPAND_MENUNAMES);
6540 if (emenu && menu_is_separator(s))
6541 {
6542 STRCPY(buf + len, transchar('|'));
6543 l = (int)STRLEN(buf + len);
6544 len += l;
6545 clen += l;
6546 }
6547 else
6548#endif
6549 for ( ; *s != NUL; ++s)
6550 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006551 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 clen += ptr2cells(s);
6553#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006554 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 STRNCPY(buf + len, s, l);
6557 s += l - 1;
6558 len += l;
6559 }
6560 else
6561#endif
6562 {
6563 STRCPY(buf + len, transchar_byte(*s));
6564 len += (int)STRLEN(buf + len);
6565 }
6566 }
6567 if (i == match)
6568 selend = buf + len;
6569
6570 *(buf + len++) = ' ';
6571 *(buf + len++) = ' ';
6572 clen += 2;
6573 if (++i == num_matches)
6574 break;
6575 }
6576
6577 if (i != num_matches)
6578 {
6579 *(buf + len++) = '>';
6580 ++clen;
6581 }
6582
6583 buf[len] = NUL;
6584
6585 row = cmdline_row - 1;
6586 if (row >= 0)
6587 {
6588 if (wild_menu_showing == 0)
6589 {
6590 if (msg_scrolled > 0)
6591 {
6592 /* Put the wildmenu just above the command line. If there is
6593 * no room, scroll the screen one line up. */
6594 if (cmdline_row == Rows - 1)
6595 {
6596 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6597 ++msg_scrolled;
6598 }
6599 else
6600 {
6601 ++cmdline_row;
6602 ++row;
6603 }
6604 wild_menu_showing = WM_SCROLLED;
6605 }
6606 else
6607 {
6608 /* Create status line if needed by setting 'laststatus' to 2.
6609 * Set 'winminheight' to zero to avoid that the window is
6610 * resized. */
6611 if (lastwin->w_status_height == 0)
6612 {
6613 save_p_ls = p_ls;
6614 save_p_wmh = p_wmh;
6615 p_ls = 2;
6616 p_wmh = 0;
6617 last_status(FALSE);
6618 }
6619 wild_menu_showing = WM_SHOWN;
6620 }
6621 }
6622
6623 screen_puts(buf, row, 0, attr);
6624 if (selstart != NULL && highlight)
6625 {
6626 *selend = NUL;
6627 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6628 }
6629
6630 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6631 }
6632
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006633#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 win_redraw_last_status(topframe);
6635#else
6636 lastwin->w_redr_status = TRUE;
6637#endif
6638 vim_free(buf);
6639}
6640#endif
6641
6642#if defined(FEAT_WINDOWS) || defined(PROTO)
6643/*
6644 * Redraw the status line of window wp.
6645 *
6646 * If inversion is possible we use it. Else '=' characters are used.
6647 */
6648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006649win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 int row;
6652 char_u *p;
6653 int len;
6654 int fillchar;
6655 int attr;
6656 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006657 static int busy = FALSE;
6658
6659 /* It's possible to get here recursively when 'statusline' (indirectly)
6660 * invokes ":redrawstatus". Simply ignore the call then. */
6661 if (busy)
6662 return;
6663 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
6665 wp->w_redr_status = FALSE;
6666 if (wp->w_status_height == 0)
6667 {
6668 /* no status line, can only be last window */
6669 redraw_cmdline = TRUE;
6670 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006671 else if (!redrawing()
6672#ifdef FEAT_INS_EXPAND
6673 /* don't update status line when popup menu is visible and may be
6674 * drawn over it */
6675 || pum_visible()
6676#endif
6677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
6679 /* Don't redraw right now, do it later. */
6680 wp->w_redr_status = TRUE;
6681 }
6682#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006683 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006686 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688#endif
6689 else
6690 {
6691 fillchar = fillchar_status(&attr, wp == curwin);
6692
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006693 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 p = NameBuff;
6695 len = (int)STRLEN(p);
6696
6697 if (wp->w_buffer->b_help
6698#ifdef FEAT_QUICKFIX
6699 || wp->w_p_pvw
6700#endif
6701 || bufIsChanged(wp->w_buffer)
6702 || wp->w_buffer->b_p_ro)
6703 *(p + len++) = ' ';
6704 if (wp->w_buffer->b_help)
6705 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006706 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 len += (int)STRLEN(p + len);
6708 }
6709#ifdef FEAT_QUICKFIX
6710 if (wp->w_p_pvw)
6711 {
6712 STRCPY(p + len, _("[Preview]"));
6713 len += (int)STRLEN(p + len);
6714 }
6715#endif
6716 if (bufIsChanged(wp->w_buffer))
6717 {
6718 STRCPY(p + len, "[+]");
6719 len += 3;
6720 }
6721 if (wp->w_buffer->b_p_ro)
6722 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006723 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 len += 4;
6725 }
6726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6728 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6729 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6730 if (this_ru_col <= 1)
6731 {
6732 p = (char_u *)"<"; /* No room for file name! */
6733 len = 1;
6734 }
6735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736#ifdef FEAT_MBYTE
6737 if (has_mbyte)
6738 {
6739 int clen = 0, i;
6740
6741 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006742 clen = mb_string2cells(p, -1);
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 /* Find first character that will fit.
6745 * Going from start to end is much faster for DBCS. */
6746 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006747 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 clen -= (*mb_ptr2cells)(p + i);
6749 len = clen;
6750 if (i > 0)
6751 {
6752 p = p + i - 1;
6753 *p = '<';
6754 ++len;
6755 }
6756
6757 }
6758 else
6759#endif
6760 if (len > this_ru_col - 1)
6761 {
6762 p += len - (this_ru_col - 1);
6763 *p = '<';
6764 len = this_ru_col - 1;
6765 }
6766
6767 row = W_WINROW(wp) + wp->w_height;
6768 screen_puts(p, row, W_WINCOL(wp), attr);
6769 screen_fill(row, row + 1, len + W_WINCOL(wp),
6770 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6771
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006772 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6774 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6775 - 1 + W_WINCOL(wp)), attr);
6776
6777#ifdef FEAT_CMDL_INFO
6778 win_redr_ruler(wp, TRUE);
6779#endif
6780 }
6781
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 /*
6783 * May need to draw the character below the vertical separator.
6784 */
6785 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6786 {
6787 if (stl_connected(wp))
6788 fillchar = fillchar_status(&attr, wp == curwin);
6789 else
6790 fillchar = fillchar_vsep(&attr);
6791 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6792 attr);
6793 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006794 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795}
6796
Bram Moolenaar238a5642006-02-21 22:12:05 +00006797#ifdef FEAT_STL_OPT
6798/*
6799 * Redraw the status line according to 'statusline' and take care of any
6800 * errors encountered.
6801 */
6802 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006803redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006804{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006805 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006806 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006807
6808 /* When called recursively return. This can happen when the statusline
6809 * contains an expression that triggers a redraw. */
6810 if (entered)
6811 return;
6812 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006813
Bram Moolenaara742e082016-04-05 21:10:38 +02006814 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006815 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006816 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006817 {
6818 /* When there is an error disable the statusline, otherwise the
6819 * display is messed up with errors and a redraw triggers the problem
6820 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006821 set_string_option_direct((char_u *)"statusline", -1,
6822 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006823 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006824 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006825 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006826 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006827}
6828#endif
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830/*
6831 * Return TRUE if the status line of window "wp" is connected to the status
6832 * line of the window right of it. If not, then it's a vertical separator.
6833 * Only call if (wp->w_vsep_width != 0).
6834 */
6835 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006836stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 frame_T *fr;
6839
6840 fr = wp->w_frame;
6841 while (fr->fr_parent != NULL)
6842 {
6843 if (fr->fr_parent->fr_layout == FR_COL)
6844 {
6845 if (fr->fr_next != NULL)
6846 break;
6847 }
6848 else
6849 {
6850 if (fr->fr_next != NULL)
6851 return TRUE;
6852 }
6853 fr = fr->fr_parent;
6854 }
6855 return FALSE;
6856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858#endif /* FEAT_WINDOWS */
6859
6860#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6861/*
6862 * Get the value to show for the language mappings, active 'keymap'.
6863 */
6864 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006865get_keymap_str(
6866 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006867 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006868 char_u *buf, /* buffer for the result */
6869 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 char_u *p;
6872
6873 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6874 return FALSE;
6875
6876 {
6877#ifdef FEAT_EVAL
6878 buf_T *old_curbuf = curbuf;
6879 win_T *old_curwin = curwin;
6880 char_u *s;
6881
6882 curbuf = wp->w_buffer;
6883 curwin = wp;
6884 STRCPY(buf, "b:keymap_name"); /* must be writable */
6885 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006886 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 --emsg_skip;
6888 curbuf = old_curbuf;
6889 curwin = old_curwin;
6890 if (p == NULL || *p == NUL)
6891#endif
6892 {
6893#ifdef FEAT_KEYMAP
6894 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6895 p = wp->w_buffer->b_p_keymap;
6896 else
6897#endif
6898 p = (char_u *)"lang";
6899 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006900 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 buf[0] = NUL;
6902#ifdef FEAT_EVAL
6903 vim_free(s);
6904#endif
6905 }
6906 return buf[0] != NUL;
6907}
6908#endif
6909
6910#if defined(FEAT_STL_OPT) || defined(PROTO)
6911/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006912 * Redraw the status line or ruler of window "wp".
6913 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 */
6915 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006916win_redr_custom(
6917 win_T *wp,
6918 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006920 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 int attr;
6922 int curattr;
6923 int row;
6924 int col = 0;
6925 int maxwidth;
6926 int width;
6927 int n;
6928 int len;
6929 int fillchar;
6930 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006931 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006933 struct stl_hlrec hltab[STL_MAX_ITEM];
6934 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006935 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006936 win_T *ewp;
6937 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
Bram Moolenaar1d633412013-12-11 15:52:01 +01006939 /* There is a tiny chance that this gets called recursively: When
6940 * redrawing a status line triggers redrawing the ruler or tabline.
6941 * Avoid trouble by not allowing recursion. */
6942 if (entered)
6943 return;
6944 entered = TRUE;
6945
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006947 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006949 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006950 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006951 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006952 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006953 attr = hl_attr(HLF_TPF);
6954 maxwidth = Columns;
6955# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006956 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006959 else
6960 {
6961 row = W_WINROW(wp) + wp->w_height;
6962 fillchar = fillchar_status(&attr, wp == curwin);
6963 maxwidth = W_WIDTH(wp);
6964
6965 if (draw_ruler)
6966 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006967 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006969 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006970 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006971 if (*++stl == '-')
6972 stl++;
6973 if (atoi((char *)stl))
6974 while (VIM_ISDIGIT(*stl))
6975 stl++;
6976 if (*stl++ != '(')
6977 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006978 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006979#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006980 col = ru_col - (Columns - W_WIDTH(wp));
6981 if (col < (W_WIDTH(wp) + 1) / 2)
6982 col = (W_WIDTH(wp) + 1) / 2;
6983#else
6984 col = ru_col;
6985 if (col > (Columns + 1) / 2)
6986 col = (Columns + 1) / 2;
6987#endif
6988 maxwidth = W_WIDTH(wp) - col;
6989#ifdef FEAT_WINDOWS
6990 if (!wp->w_status_height)
6991#endif
6992 {
6993 row = Rows - 1;
6994 --maxwidth; /* writing in last column may cause scrolling */
6995 fillchar = ' ';
6996 attr = 0;
6997 }
6998
6999# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007000 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007001# endif
7002 }
7003 else
7004 {
7005 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007007 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007008 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007009# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007010 use_sandbox = was_set_insecurely((char_u *)"statusline",
7011 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007012# endif
7013 }
7014
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007015#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007016 col += W_WINCOL(wp);
7017#endif
7018 }
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007021 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar61452852011-02-01 18:01:11 +01007023 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7024 * the cursor away and back. */
7025 ewp = wp == NULL ? curwin : wp;
7026 p_crb_save = ewp->w_p_crb;
7027 ewp->w_p_crb = FALSE;
7028
Bram Moolenaar362f3562009-11-03 16:20:34 +00007029 /* Make a copy, because the statusline may include a function call that
7030 * might change the option value and free the memory. */
7031 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007032 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007033 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007034 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007035 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007036 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007038 /* Make all characters printable. */
7039 p = transstr(buf);
7040 if (p != NULL)
7041 {
7042 vim_strncpy(buf, p, sizeof(buf) - 1);
7043 vim_free(p);
7044 }
7045
7046 /* fill up with "fillchar" */
7047 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007048 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
7050#ifdef FEAT_MBYTE
7051 len += (*mb_char2bytes)(fillchar, buf + len);
7052#else
7053 buf[len++] = fillchar;
7054#endif
7055 ++width;
7056 }
7057 buf[len] = NUL;
7058
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007059 /*
7060 * Draw each snippet with the specified highlighting.
7061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 curattr = attr;
7063 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007064 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007066 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 screen_puts_len(p, len, row, col, curattr);
7068 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007069 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007071 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007073 else if (hltab[n].userhl < 0)
7074 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007076 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007077 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#endif
7079 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007080 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
7082 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007083
7084 if (wp == NULL)
7085 {
7086 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7087 col = 0;
7088 len = 0;
7089 p = buf;
7090 fillchar = 0;
7091 for (n = 0; tabtab[n].start != NULL; n++)
7092 {
7093 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7094 while (col < len)
7095 TabPageIdxs[col++] = fillchar;
7096 p = tabtab[n].start;
7097 fillchar = tabtab[n].userhl;
7098 }
7099 while (col < Columns)
7100 TabPageIdxs[col++] = fillchar;
7101 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007102
7103theend:
7104 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105}
7106
7107#endif /* FEAT_STL_OPT */
7108
7109/*
7110 * Output a single character directly to the screen and update ScreenLines.
7111 */
7112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007113screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 char_u buf[MB_MAXBYTES + 1];
7116
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007117#ifdef FEAT_MBYTE
7118 if (has_mbyte)
7119 buf[(*mb_char2bytes)(c, buf)] = NUL;
7120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007122 {
7123 buf[0] = c;
7124 buf[1] = NUL;
7125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 screen_puts(buf, row, col, attr);
7127}
7128
7129/*
7130 * Get a single character directly from ScreenLines into "bytes[]".
7131 * Also return its attribute in *attrp;
7132 */
7133 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007134screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 unsigned off;
7137
7138 /* safety check */
7139 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7140 {
7141 off = LineOffset[row] + col;
7142 *attrp = ScreenAttrs[off];
7143 bytes[0] = ScreenLines[off];
7144 bytes[1] = NUL;
7145
7146#ifdef FEAT_MBYTE
7147 if (enc_utf8 && ScreenLinesUC[off] != 0)
7148 bytes[utfc_char2bytes(off, bytes)] = NUL;
7149 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7150 {
7151 bytes[0] = ScreenLines[off];
7152 bytes[1] = ScreenLines2[off];
7153 bytes[2] = NUL;
7154 }
7155 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7156 {
7157 bytes[1] = ScreenLines[off + 1];
7158 bytes[2] = NUL;
7159 }
7160#endif
7161 }
7162}
7163
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007164#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007165static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007166
7167/*
7168 * Return TRUE if composing characters for screen posn "off" differs from
7169 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007170 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007171 */
7172 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007173screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007174{
7175 int i;
7176
7177 for (i = 0; i < Screen_mco; ++i)
7178 {
7179 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7180 return TRUE;
7181 if (u8cc[i] == 0)
7182 break;
7183 }
7184 return FALSE;
7185}
7186#endif
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188/*
7189 * Put string '*text' on the screen at position 'row' and 'col', with
7190 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7191 * Note: only outputs within one row, message is truncated at screen boundary!
7192 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7193 */
7194 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007195screen_puts(
7196 char_u *text,
7197 int row,
7198 int col,
7199 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 screen_puts_len(text, -1, row, col, attr);
7202}
7203
7204/*
7205 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7206 * a NUL.
7207 */
7208 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007209screen_puts_len(
7210 char_u *text,
7211 int textlen,
7212 int row,
7213 int col,
7214 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 unsigned off;
7217 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007218 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 int c;
7220#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007221 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 int mbyte_blen = 1;
7223 int mbyte_cells = 1;
7224 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007225 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 int clear_next_cell = FALSE;
7227# ifdef FEAT_ARABIC
7228 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007229 int pc, nc, nc1;
7230 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231# endif
7232#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007233#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7234 int force_redraw_this;
7235 int force_redraw_next = FALSE;
7236#endif
7237 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7240 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007241 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaarc236c162008-07-13 17:41:49 +00007243#ifdef FEAT_MBYTE
7244 /* When drawing over the right halve of a double-wide char clear out the
7245 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007246 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007247# ifdef FEAT_GUI
7248 && !gui.in_use
7249# endif
7250 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007251 {
7252 ScreenLines[off - 1] = ' ';
7253 ScreenAttrs[off - 1] = 0;
7254 if (enc_utf8)
7255 {
7256 ScreenLinesUC[off - 1] = 0;
7257 ScreenLinesC[0][off - 1] = 0;
7258 }
7259 /* redraw the previous cell, make it empty */
7260 screen_char(off - 1, row, col - 1);
7261 /* force the cell at "col" to be redrawn */
7262 force_redraw_next = TRUE;
7263 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007264#endif
7265
Bram Moolenaar367329b2007-08-30 11:53:22 +00007266#ifdef FEAT_MBYTE
7267 max_off = LineOffset[row] + screen_Columns;
7268#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007269 while (col < screen_Columns
7270 && (len < 0 || (int)(ptr - text) < len)
7271 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 c = *ptr;
7274#ifdef FEAT_MBYTE
7275 /* check if this is the first byte of a multibyte */
7276 if (has_mbyte)
7277 {
7278 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007279 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007281 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7283 mbyte_cells = 1;
7284 else if (enc_dbcs != 0)
7285 mbyte_cells = mbyte_blen;
7286 else /* enc_utf8 */
7287 {
7288 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007289 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 (int)((text + len) - ptr));
7291 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007292 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007294# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* Non-BMP character: display as ? or fullwidth ?. */
7296 if (u8c >= 0x10000)
7297 {
7298 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7299 if (attr == 0)
7300 attr = hl_attr(HLF_8);
7301 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303# ifdef FEAT_ARABIC
7304 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7305 {
7306 /* Do Arabic shaping. */
7307 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7308 {
7309 /* Past end of string to be displayed. */
7310 nc = NUL;
7311 nc1 = NUL;
7312 }
7313 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007314 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007315 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7316 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007317 nc1 = pcc[0];
7318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 pc = prev_c;
7320 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007321 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
7323 else
7324 prev_c = u8c;
7325# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007326 if (col + mbyte_cells > screen_Columns)
7327 {
7328 /* Only 1 cell left, but character requires 2 cells:
7329 * display a '>' in the last column to avoid wrapping. */
7330 c = '>';
7331 mbyte_cells = 1;
7332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 }
7334 }
7335#endif
7336
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007337#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7338 force_redraw_this = force_redraw_next;
7339 force_redraw_next = FALSE;
7340#endif
7341
7342 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343#ifdef FEAT_MBYTE
7344 || (mbyte_cells == 2
7345 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7346 || (enc_dbcs == DBCS_JPNU
7347 && c == 0x8e
7348 && ScreenLines2[off] != ptr[1])
7349 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007350 && (ScreenLinesUC[off] !=
7351 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7352 || (ScreenLinesUC[off] != 0
7353 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354#endif
7355 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007356 || exmode_active;
7357
7358 if (need_redraw
7359#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7360 || force_redraw_this
7361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 )
7363 {
7364#if defined(FEAT_GUI) || defined(UNIX)
7365 /* The bold trick makes a single row of pixels appear in the next
7366 * character. When a bold character is removed, the next
7367 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007368 * and for some xterms. */
7369 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370# ifdef FEAT_GUI
7371 gui.in_use
7372# endif
7373# if defined(FEAT_GUI) && defined(UNIX)
7374 ||
7375# endif
7376# ifdef UNIX
7377 term_is_xterm
7378# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007379 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007381 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007383 if (n > HL_ALL)
7384 n = syn_attr2attr(n);
7385 if (n & HL_BOLD)
7386 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
7388#endif
7389#ifdef FEAT_MBYTE
7390 /* When at the end of the text and overwriting a two-cell
7391 * character with a one-cell character, need to clear the next
7392 * cell. Also when overwriting the left halve of a two-cell char
7393 * with the right halve of a two-cell char. Do this only once
7394 * (mb_off2cells() may return 2 on the right halve). */
7395 if (clear_next_cell)
7396 clear_next_cell = FALSE;
7397 else if (has_mbyte
7398 && (len < 0 ? ptr[mbyte_blen] == NUL
7399 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007400 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007402 && (*mb_off2cells)(off, max_off) == 1
7403 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 clear_next_cell = TRUE;
7405
7406 /* Make sure we never leave a second byte of a double-byte behind,
7407 * it confuses mb_off2cells(). */
7408 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007409 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007411 && (*mb_off2cells)(off, max_off) == 1
7412 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 ScreenLines[off + mbyte_blen] = 0;
7414#endif
7415 ScreenLines[off] = c;
7416 ScreenAttrs[off] = attr;
7417#ifdef FEAT_MBYTE
7418 if (enc_utf8)
7419 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007420 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 ScreenLinesUC[off] = 0;
7422 else
7423 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 int i;
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007427 for (i = 0; i < Screen_mco; ++i)
7428 {
7429 ScreenLinesC[i][off] = u8cc[i];
7430 if (u8cc[i] == 0)
7431 break;
7432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 if (mbyte_cells == 2)
7435 {
7436 ScreenLines[off + 1] = 0;
7437 ScreenAttrs[off + 1] = attr;
7438 }
7439 screen_char(off, row, col);
7440 }
7441 else if (mbyte_cells == 2)
7442 {
7443 ScreenLines[off + 1] = ptr[1];
7444 ScreenAttrs[off + 1] = attr;
7445 screen_char_2(off, row, col);
7446 }
7447 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7448 {
7449 ScreenLines2[off] = ptr[1];
7450 screen_char(off, row, col);
7451 }
7452 else
7453#endif
7454 screen_char(off, row, col);
7455 }
7456#ifdef FEAT_MBYTE
7457 if (has_mbyte)
7458 {
7459 off += mbyte_cells;
7460 col += mbyte_cells;
7461 ptr += mbyte_blen;
7462 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007463 {
7464 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007466 len = -1;
7467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 }
7469 else
7470#endif
7471 {
7472 ++off;
7473 ++col;
7474 ++ptr;
7475 }
7476 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007477
7478#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7479 /* If we detected the next character needs to be redrawn, but the text
7480 * doesn't extend up to there, update the character here. */
7481 if (force_redraw_next && col < screen_Columns)
7482 {
7483# ifdef FEAT_MBYTE
7484 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7485 screen_char_2(off, row, col);
7486 else
7487# endif
7488 screen_char(off, row, col);
7489 }
7490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492
7493#ifdef FEAT_SEARCH_EXTRA
7494/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007495 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 */
7497 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007498start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
7500 if (p_hls && !no_hlsearch)
7501 {
7502 last_pat_prog(&search_hl.rm);
7503 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007504# ifdef FEAT_RELTIME
7505 /* Set the time limit to 'redrawtime'. */
7506 profile_setlimit(p_rdt, &search_hl.tm);
7507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509}
7510
7511/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007512 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 */
7514 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007515end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 if (search_hl.rm.regprog != NULL)
7518 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007519 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 search_hl.rm.regprog = NULL;
7521 }
7522}
7523
7524/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007525 * Init for calling prepare_search_hl().
7526 */
7527 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007528init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007529{
7530 matchitem_T *cur;
7531
7532 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7533 * match */
7534 cur = wp->w_match_head;
7535 while (cur != NULL)
7536 {
7537 cur->hl.rm = cur->match;
7538 if (cur->hlg_id == 0)
7539 cur->hl.attr = 0;
7540 else
7541 cur->hl.attr = syn_id2attr(cur->hlg_id);
7542 cur->hl.buf = wp->w_buffer;
7543 cur->hl.lnum = 0;
7544 cur->hl.first_lnum = 0;
7545# ifdef FEAT_RELTIME
7546 /* Set the time limit to 'redrawtime'. */
7547 profile_setlimit(p_rdt, &(cur->hl.tm));
7548# endif
7549 cur = cur->next;
7550 }
7551 search_hl.buf = wp->w_buffer;
7552 search_hl.lnum = 0;
7553 search_hl.first_lnum = 0;
7554 /* time limit is set at the toplevel, for all windows */
7555}
7556
7557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 * Advance to the match in window "wp" line "lnum" or past it.
7559 */
7560 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007561prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007563 matchitem_T *cur; /* points to the match list */
7564 match_T *shl; /* points to search_hl or a match */
7565 int shl_flag; /* flag to indicate whether search_hl
7566 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007567 int pos_inprogress; /* marks that position match search is
7568 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 int n;
7570
7571 /*
7572 * When using a multi-line pattern, start searching at the top
7573 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007574 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007576 cur = wp->w_match_head;
7577 shl_flag = FALSE;
7578 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007580 if (shl_flag == FALSE)
7581 {
7582 shl = &search_hl;
7583 shl_flag = TRUE;
7584 }
7585 else
7586 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (shl->rm.regprog != NULL
7588 && shl->lnum == 0
7589 && re_multiline(shl->rm.regprog))
7590 {
7591 if (shl->first_lnum == 0)
7592 {
7593# ifdef FEAT_FOLDING
7594 for (shl->first_lnum = lnum;
7595 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7596 if (hasFoldingWin(wp, shl->first_lnum - 1,
7597 NULL, NULL, TRUE, NULL))
7598 break;
7599# else
7600 shl->first_lnum = wp->w_topline;
7601# endif
7602 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007603 if (cur != NULL)
7604 cur->pos.cur = 0;
7605 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007607 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7608 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007610 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7611 pos_inprogress = cur == NULL || cur->pos.cur == 0
7612 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (shl->lnum != 0)
7614 {
7615 shl->first_lnum = shl->lnum
7616 + shl->rm.endpos[0].lnum
7617 - shl->rm.startpos[0].lnum;
7618 n = shl->rm.endpos[0].col;
7619 }
7620 else
7621 {
7622 ++shl->first_lnum;
7623 n = 0;
7624 }
7625 }
7626 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007627 if (shl != &search_hl && cur != NULL)
7628 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630}
7631
7632/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007633 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 * Uses shl->buf.
7635 * Sets shl->lnum and shl->rm contents.
7636 * Note: Assumes a previous match is always before "lnum", unless
7637 * shl->lnum is zero.
7638 * Careful: Any pointers for buffer lines will become invalid.
7639 */
7640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007641next_search_hl(
7642 win_T *win,
7643 match_T *shl, /* points to search_hl or a match */
7644 linenr_T lnum,
7645 colnr_T mincol, /* minimal column for a match */
7646 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 linenr_T l;
7649 colnr_T matchcol;
7650 long nmatched;
7651
7652 if (shl->lnum != 0)
7653 {
7654 /* Check for three situations:
7655 * 1. If the "lnum" is below a previous match, start a new search.
7656 * 2. If the previous match includes "mincol", use it.
7657 * 3. Continue after the previous match.
7658 */
7659 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7660 if (lnum > l)
7661 shl->lnum = 0;
7662 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7663 return;
7664 }
7665
7666 /*
7667 * Repeat searching for a match until one is found that includes "mincol"
7668 * or none is found in this line.
7669 */
7670 called_emsg = FALSE;
7671 for (;;)
7672 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007673#ifdef FEAT_RELTIME
7674 /* Stop searching after passing the time limit. */
7675 if (profile_passed_limit(&(shl->tm)))
7676 {
7677 shl->lnum = 0; /* no match found in time */
7678 break;
7679 }
7680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /* Three situations:
7682 * 1. No useful previous match: search from start of line.
7683 * 2. Not Vi compatible or empty match: continue at next character.
7684 * Break the loop if this is beyond the end of the line.
7685 * 3. Vi compatible searching: continue at end of previous match.
7686 */
7687 if (shl->lnum == 0)
7688 matchcol = 0;
7689 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7690 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007691 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007693 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007694
7695 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007696 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007697 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007699 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 shl->lnum = 0;
7701 break;
7702 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007703#ifdef FEAT_MBYTE
7704 if (has_mbyte)
7705 matchcol += mb_ptr2len(ml);
7706 else
7707#endif
7708 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
7710 else
7711 matchcol = shl->rm.endpos[0].col;
7712
7713 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007714 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007716 /* Remember whether shl->rm is using a copy of the regprog in
7717 * cur->match. */
7718 int regprog_is_copy = (shl != &search_hl && cur != NULL
7719 && shl == &cur->hl
7720 && cur->match.regprog == cur->hl.rm.regprog);
7721
Bram Moolenaarb3414592014-06-17 17:48:32 +02007722 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7723 matchcol,
7724#ifdef FEAT_RELTIME
7725 &(shl->tm)
7726#else
7727 NULL
7728#endif
7729 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007730 /* Copy the regprog, in case it got freed and recompiled. */
7731 if (regprog_is_copy)
7732 cur->match.regprog = cur->hl.rm.regprog;
7733
Bram Moolenaarb3414592014-06-17 17:48:32 +02007734 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007735 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 /* Error while handling regexp: stop using this regexp. */
7737 if (shl == &search_hl)
7738 {
7739 /* don't free regprog in the match list, it's a copy */
7740 vim_regfree(shl->rm.regprog);
7741 SET_NO_HLSEARCH(TRUE);
7742 }
7743 shl->rm.regprog = NULL;
7744 shl->lnum = 0;
7745 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7746 message */
7747 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007748 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007749 }
7750 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007751 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007752 else
7753 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (nmatched == 0)
7755 {
7756 shl->lnum = 0; /* no match found */
7757 break;
7758 }
7759 if (shl->rm.startpos[0].lnum > 0
7760 || shl->rm.startpos[0].col >= mincol
7761 || nmatched > 1
7762 || shl->rm.endpos[0].col > mincol)
7763 {
7764 shl->lnum += shl->rm.startpos[0].lnum;
7765 break; /* useful match found */
7766 }
7767 }
7768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
Bram Moolenaarb3414592014-06-17 17:48:32 +02007770 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007771next_search_hl_pos(
7772 match_T *shl, /* points to a match */
7773 linenr_T lnum,
7774 posmatch_T *posmatch, /* match positions */
7775 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007776{
7777 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007778 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007779
7780 shl->lnum = 0;
7781 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7782 {
7783 if (posmatch->pos[i].lnum == 0)
7784 break;
7785 if (posmatch->pos[i].col < mincol)
7786 continue;
7787 if (posmatch->pos[i].lnum == lnum)
7788 {
7789 if (shl->lnum == lnum)
7790 {
7791 /* partially sort positions by column numbers
7792 * on the same line */
7793 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7794 {
7795 llpos_T tmp = posmatch->pos[i];
7796
7797 posmatch->pos[i] = posmatch->pos[bot];
7798 posmatch->pos[bot] = tmp;
7799 }
7800 }
7801 else
7802 {
7803 bot = i;
7804 shl->lnum = lnum;
7805 }
7806 }
7807 }
7808 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007809 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007810 {
7811 colnr_T start = posmatch->pos[bot].col == 0
7812 ? 0 : posmatch->pos[bot].col - 1;
7813 colnr_T end = posmatch->pos[bot].col == 0
7814 ? MAXCOL : start + posmatch->pos[bot].len;
7815
7816 shl->rm.startpos[0].lnum = 0;
7817 shl->rm.startpos[0].col = start;
7818 shl->rm.endpos[0].lnum = 0;
7819 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007820 shl->is_addpos = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007821 posmatch->cur = bot + 1;
7822 return TRUE;
7823 }
7824 return FALSE;
7825}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007826#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007829screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830{
7831 attrentry_T *aep = NULL;
7832
7833 screen_attr = attr;
7834 if (full_screen
7835#ifdef WIN3264
7836 && termcap_active
7837#endif
7838 )
7839 {
7840#ifdef FEAT_GUI
7841 if (gui.in_use)
7842 {
7843 char buf[20];
7844
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007845 /* The GUI handles this internally. */
7846 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 OUT_STR(buf);
7848 }
7849 else
7850#endif
7851 {
7852 if (attr > HL_ALL) /* special HL attr. */
7853 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007854 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 aep = syn_cterm_attr2entry(attr);
7856 else
7857 aep = syn_term_attr2entry(attr);
7858 if (aep == NULL) /* did ":syntax clear" */
7859 attr = 0;
7860 else
7861 attr = aep->ae_attr;
7862 }
7863 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7864 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007865 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007866#ifdef FEAT_TERMGUICOLORS
7867 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007868 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007869#endif
7870 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007871#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007872 )
7873#endif
7874 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007875 /* If the Normal FG color has BOLD attribute and the new HL
7876 * has a FG color defined, clear BOLD. */
7877 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7879 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007880 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7881 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 out_str(T_US);
7883 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7884 out_str(T_CZH);
7885 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7886 out_str(T_MR);
7887
7888 /*
7889 * Output the color or start string after bold etc., in case the
7890 * bold etc. override the color setting.
7891 */
7892 if (aep != NULL)
7893 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007894#ifdef FEAT_TERMGUICOLORS
7895 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007897 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007898 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007899 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007900 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007905 if (t_colors > 1)
7906 {
7907 if (aep->ae_u.cterm.fg_color)
7908 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7909 if (aep->ae_u.cterm.bg_color)
7910 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7911 }
7912 else
7913 {
7914 if (aep->ae_u.term.start != NULL)
7915 out_str(aep->ae_u.term.start);
7916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918 }
7919 }
7920 }
7921}
7922
7923 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007924screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
7926 int do_ME = FALSE; /* output T_ME code */
7927
7928 if (screen_attr != 0
7929#ifdef WIN3264
7930 && termcap_active
7931#endif
7932 )
7933 {
7934#ifdef FEAT_GUI
7935 if (gui.in_use)
7936 {
7937 char buf[20];
7938
7939 /* use internal GUI code */
7940 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7941 OUT_STR(buf);
7942 }
7943 else
7944#endif
7945 {
7946 if (screen_attr > HL_ALL) /* special HL attr. */
7947 {
7948 attrentry_T *aep;
7949
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007950 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
7952 /*
7953 * Assume that t_me restores the original colors!
7954 */
7955 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007956 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007957#ifdef FEAT_TERMGUICOLORS
7958 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007959 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
7960 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007961#endif
7962 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007963#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007964 )
7965#endif
7966 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 do_ME = TRUE;
7968 }
7969 else
7970 {
7971 aep = syn_term_attr2entry(screen_attr);
7972 if (aep != NULL && aep->ae_u.term.stop != NULL)
7973 {
7974 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7975 do_ME = TRUE;
7976 else
7977 out_str(aep->ae_u.term.stop);
7978 }
7979 }
7980 if (aep == NULL) /* did ":syntax clear" */
7981 screen_attr = 0;
7982 else
7983 screen_attr = aep->ae_attr;
7984 }
7985
7986 /*
7987 * Often all ending-codes are equal to T_ME. Avoid outputting the
7988 * same sequence several times.
7989 */
7990 if (screen_attr & HL_STANDOUT)
7991 {
7992 if (STRCMP(T_SE, T_ME) == 0)
7993 do_ME = TRUE;
7994 else
7995 out_str(T_SE);
7996 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007997 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
7999 if (STRCMP(T_UE, T_ME) == 0)
8000 do_ME = TRUE;
8001 else
8002 out_str(T_UE);
8003 }
8004 if (screen_attr & HL_ITALIC)
8005 {
8006 if (STRCMP(T_CZR, T_ME) == 0)
8007 do_ME = TRUE;
8008 else
8009 out_str(T_CZR);
8010 }
8011 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8012 out_str(T_ME);
8013
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008014#ifdef FEAT_TERMGUICOLORS
8015 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008017 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008018 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008019 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008020 term_bg_rgb_color(cterm_normal_bg_gui_color);
8021 }
8022 else
8023#endif
8024 {
8025 if (t_colors > 1)
8026 {
8027 /* set Normal cterm colors */
8028 if (cterm_normal_fg_color != 0)
8029 term_fg_color(cterm_normal_fg_color - 1);
8030 if (cterm_normal_bg_color != 0)
8031 term_bg_color(cterm_normal_bg_color - 1);
8032 if (cterm_normal_fg_bold)
8033 out_str(T_MD);
8034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 }
8037 }
8038 screen_attr = 0;
8039}
8040
8041/*
8042 * Reset the colors for a cterm. Used when leaving Vim.
8043 * The machine specific code may override this again.
8044 */
8045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008046reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008048 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {
8050 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008051#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008052 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8053 || cterm_normal_bg_gui_color != INVALCOLOR)
8054 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008055#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
8059 out_str(T_OP);
8060 screen_attr = -1;
8061 }
8062 if (cterm_normal_fg_bold)
8063 {
8064 out_str(T_ME);
8065 screen_attr = -1;
8066 }
8067 }
8068}
8069
8070/*
8071 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8072 * using the attributes from ScreenAttrs["off"].
8073 */
8074 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008075screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
8077 int attr;
8078
8079 /* Check for illegal values, just in case (could happen just after
8080 * resizing). */
8081 if (row >= screen_Rows || col >= screen_Columns)
8082 return;
8083
Bram Moolenaar494838a2015-02-10 19:20:37 +01008084 /* Outputting a character in the last cell on the screen may scroll the
8085 * screen up. Only do it when the "xn" termcap property is set, otherwise
8086 * mark the character invalid (update it when scrolled up). */
8087 if (*T_XN == NUL
8088 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#ifdef FEAT_RIGHTLEFT
8090 /* account for first command-line character in rightleft mode */
8091 && !cmdmsg_rl
8092#endif
8093 )
8094 {
8095 ScreenAttrs[off] = (sattr_T)-1;
8096 return;
8097 }
8098
8099 /*
8100 * Stop highlighting first, so it's easier to move the cursor.
8101 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008102#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (screen_char_attr != 0)
8104 attr = screen_char_attr;
8105 else
8106#endif
8107 attr = ScreenAttrs[off];
8108 if (screen_attr != attr)
8109 screen_stop_highlight();
8110
8111 windgoto(row, col);
8112
8113 if (screen_attr != attr)
8114 screen_start_highlight(attr);
8115
8116#ifdef FEAT_MBYTE
8117 if (enc_utf8 && ScreenLinesUC[off] != 0)
8118 {
8119 char_u buf[MB_MAXBYTES + 1];
8120
8121 /* Convert UTF-8 character to bytes and write it. */
8122
8123 buf[utfc_char2bytes(off, buf)] = NUL;
8124
8125 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008126 if (utf_ambiguous_width(ScreenLinesUC[off]))
8127 screen_cur_col = 9999;
8128 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 ++screen_cur_col;
8130 }
8131 else
8132#endif
8133 {
8134#ifdef FEAT_MBYTE
8135 out_flush_check();
8136#endif
8137 out_char(ScreenLines[off]);
8138#ifdef FEAT_MBYTE
8139 /* double-byte character in single-width cell */
8140 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8141 out_char(ScreenLines2[off]);
8142#endif
8143 }
8144
8145 screen_cur_col++;
8146}
8147
8148#ifdef FEAT_MBYTE
8149
8150/*
8151 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8152 * on the screen at position 'row' and 'col'.
8153 * The attributes of the first byte is used for all. This is required to
8154 * output the two bytes of a double-byte character with nothing in between.
8155 */
8156 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008157screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158{
8159 /* Check for illegal values (could be wrong when screen was resized). */
8160 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8161 return;
8162
8163 /* Outputting the last character on the screen may scrollup the screen.
8164 * Don't to it! Mark the character invalid (update it when scrolled up) */
8165 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8166 {
8167 ScreenAttrs[off] = (sattr_T)-1;
8168 return;
8169 }
8170
8171 /* Output the first byte normally (positions the cursor), then write the
8172 * second byte directly. */
8173 screen_char(off, row, col);
8174 out_char(ScreenLines[off + 1]);
8175 ++screen_cur_col;
8176}
8177#endif
8178
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008179#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180/*
8181 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8182 * This uses the contents of ScreenLines[] and doesn't change it.
8183 */
8184 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008185screen_draw_rectangle(
8186 int row,
8187 int col,
8188 int height,
8189 int width,
8190 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191{
8192 int r, c;
8193 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008194#ifdef FEAT_MBYTE
8195 int max_off;
8196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008198 /* Can't use ScreenLines unless initialized */
8199 if (ScreenLines == NULL)
8200 return;
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (invert)
8203 screen_char_attr = HL_INVERSE;
8204 for (r = row; r < row + height; ++r)
8205 {
8206 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008207#ifdef FEAT_MBYTE
8208 max_off = off + screen_Columns;
8209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 for (c = col; c < col + width; ++c)
8211 {
8212#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008213 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
8215 screen_char_2(off + c, r, c);
8216 ++c;
8217 }
8218 else
8219#endif
8220 {
8221 screen_char(off + c, r, c);
8222#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008223 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 ++c;
8225#endif
8226 }
8227 }
8228 }
8229 screen_char_attr = 0;
8230}
8231#endif
8232
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008233#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234/*
8235 * Redraw the characters for a vertically split window.
8236 */
8237 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008238redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
8240 int col;
8241 int width;
8242
8243# ifdef FEAT_CLIPBOARD
8244 clip_may_clear_selection(row, end - 1);
8245# endif
8246
8247 if (wp == NULL)
8248 {
8249 col = 0;
8250 width = Columns;
8251 }
8252 else
8253 {
8254 col = wp->w_wincol;
8255 width = wp->w_width;
8256 }
8257 screen_draw_rectangle(row, col, end - row, width, FALSE);
8258}
8259#endif
8260
8261/*
8262 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8263 * with character 'c1' in first column followed by 'c2' in the other columns.
8264 * Use attributes 'attr'.
8265 */
8266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008267screen_fill(
8268 int start_row,
8269 int end_row,
8270 int start_col,
8271 int end_col,
8272 int c1,
8273 int c2,
8274 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
8276 int row;
8277 int col;
8278 int off;
8279 int end_off;
8280 int did_delete;
8281 int c;
8282 int norm_term;
8283#if defined(FEAT_GUI) || defined(UNIX)
8284 int force_next = FALSE;
8285#endif
8286
8287 if (end_row > screen_Rows) /* safety check */
8288 end_row = screen_Rows;
8289 if (end_col > screen_Columns) /* safety check */
8290 end_col = screen_Columns;
8291 if (ScreenLines == NULL
8292 || start_row >= end_row
8293 || start_col >= end_col) /* nothing to do */
8294 return;
8295
8296 /* it's a "normal" terminal when not in a GUI or cterm */
8297 norm_term = (
8298#ifdef FEAT_GUI
8299 !gui.in_use &&
8300#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008301 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 for (row = start_row; row < end_row; ++row)
8303 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008304#ifdef FEAT_MBYTE
8305 if (has_mbyte
8306# ifdef FEAT_GUI
8307 && !gui.in_use
8308# endif
8309 )
8310 {
8311 /* When drawing over the right halve of a double-wide char clear
8312 * out the left halve. When drawing over the left halve of a
8313 * double wide-char clear out the right halve. Only needed in a
8314 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008315 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008316 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008317 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008318 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008319 }
8320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 /*
8322 * Try to use delete-line termcap code, when no attributes or in a
8323 * "normal" terminal, where a bold/italic space is just a
8324 * space.
8325 */
8326 did_delete = FALSE;
8327 if (c2 == ' '
8328 && end_col == Columns
8329 && can_clear(T_CE)
8330 && (attr == 0
8331 || (norm_term
8332 && attr <= HL_ALL
8333 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8334 {
8335 /*
8336 * check if we really need to clear something
8337 */
8338 col = start_col;
8339 if (c1 != ' ') /* don't clear first char */
8340 ++col;
8341
8342 off = LineOffset[row] + col;
8343 end_off = LineOffset[row] + end_col;
8344
8345 /* skip blanks (used often, keep it fast!) */
8346#ifdef FEAT_MBYTE
8347 if (enc_utf8)
8348 while (off < end_off && ScreenLines[off] == ' '
8349 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8350 ++off;
8351 else
8352#endif
8353 while (off < end_off && ScreenLines[off] == ' '
8354 && ScreenAttrs[off] == 0)
8355 ++off;
8356 if (off < end_off) /* something to be cleared */
8357 {
8358 col = off - LineOffset[row];
8359 screen_stop_highlight();
8360 term_windgoto(row, col);/* clear rest of this screen line */
8361 out_str(T_CE);
8362 screen_start(); /* don't know where cursor is now */
8363 col = end_col - col;
8364 while (col--) /* clear chars in ScreenLines */
8365 {
8366 ScreenLines[off] = ' ';
8367#ifdef FEAT_MBYTE
8368 if (enc_utf8)
8369 ScreenLinesUC[off] = 0;
8370#endif
8371 ScreenAttrs[off] = 0;
8372 ++off;
8373 }
8374 }
8375 did_delete = TRUE; /* the chars are cleared now */
8376 }
8377
8378 off = LineOffset[row] + start_col;
8379 c = c1;
8380 for (col = start_col; col < end_col; ++col)
8381 {
8382 if (ScreenLines[off] != c
8383#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008384 || (enc_utf8 && (int)ScreenLinesUC[off]
8385 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#endif
8387 || ScreenAttrs[off] != attr
8388#if defined(FEAT_GUI) || defined(UNIX)
8389 || force_next
8390#endif
8391 )
8392 {
8393#if defined(FEAT_GUI) || defined(UNIX)
8394 /* The bold trick may make a single row of pixels appear in
8395 * the next character. When a bold character is removed, the
8396 * next character should be redrawn too. This happens for our
8397 * own GUI and for some xterms. */
8398 if (
8399# ifdef FEAT_GUI
8400 gui.in_use
8401# endif
8402# if defined(FEAT_GUI) && defined(UNIX)
8403 ||
8404# endif
8405# ifdef UNIX
8406 term_is_xterm
8407# endif
8408 )
8409 {
8410 if (ScreenLines[off] != ' '
8411 && (ScreenAttrs[off] > HL_ALL
8412 || ScreenAttrs[off] & HL_BOLD))
8413 force_next = TRUE;
8414 else
8415 force_next = FALSE;
8416 }
8417#endif
8418 ScreenLines[off] = c;
8419#ifdef FEAT_MBYTE
8420 if (enc_utf8)
8421 {
8422 if (c >= 0x80)
8423 {
8424 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008425 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 else
8428 ScreenLinesUC[off] = 0;
8429 }
8430#endif
8431 ScreenAttrs[off] = attr;
8432 if (!did_delete || c != ' ')
8433 screen_char(off, row, col);
8434 }
8435 ++off;
8436 if (col == start_col)
8437 {
8438 if (did_delete)
8439 break;
8440 c = c2;
8441 }
8442 }
8443 if (end_col == Columns)
8444 LineWraps[row] = FALSE;
8445 if (row == Rows - 1) /* overwritten the command line */
8446 {
8447 redraw_cmdline = TRUE;
8448 if (c1 == ' ' && c2 == ' ')
8449 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008450 if (start_col == 0)
8451 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 }
8453 }
8454}
8455
8456/*
8457 * Check if there should be a delay. Used before clearing or redrawing the
8458 * screen or the command line.
8459 */
8460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008461check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462{
8463 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8464 && !did_wait_return
8465 && emsg_silent == 0)
8466 {
8467 out_flush();
8468 ui_delay(1000L, TRUE);
8469 emsg_on_display = FALSE;
8470 if (check_msg_scroll)
8471 msg_scroll = FALSE;
8472 }
8473}
8474
8475/*
8476 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008477 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 * Returns TRUE if there is a valid screen to write to.
8479 * Returns FALSE when starting up and screen not initialized yet.
8480 */
8481 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008482screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008484 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 return (ScreenLines != NULL);
8486}
8487
8488/*
8489 * Resize the shell to Rows and Columns.
8490 * Allocate ScreenLines[] and associated items.
8491 *
8492 * There may be some time between setting Rows and Columns and (re)allocating
8493 * ScreenLines[]. This happens when starting up and when (manually) changing
8494 * the shell size. Always use screen_Rows and screen_Columns to access items
8495 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8496 * final size of the shell is needed.
8497 */
8498 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008499screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int new_row, old_row;
8502#ifdef FEAT_GUI
8503 int old_Rows;
8504#endif
8505 win_T *wp;
8506 int outofmem = FALSE;
8507 int len;
8508 schar_T *new_ScreenLines;
8509#ifdef FEAT_MBYTE
8510 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008511 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008513 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514#endif
8515 sattr_T *new_ScreenAttrs;
8516 unsigned *new_LineOffset;
8517 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008518#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008519 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008520 tabpage_T *tp;
8521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008523 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008524#ifdef FEAT_AUTOCMD
8525 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008527retry:
8528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 /*
8530 * Allocation of the screen buffers is done only when the size changes and
8531 * when Rows and Columns have been set and we have started doing full
8532 * screen stuff.
8533 */
8534 if ((ScreenLines != NULL
8535 && Rows == screen_Rows
8536 && Columns == screen_Columns
8537#ifdef FEAT_MBYTE
8538 && enc_utf8 == (ScreenLinesUC != NULL)
8539 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008540 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541#endif
8542 )
8543 || Rows == 0
8544 || Columns == 0
8545 || (!full_screen && ScreenLines == NULL))
8546 return;
8547
8548 /*
8549 * It's possible that we produce an out-of-memory message below, which
8550 * will cause this function to be called again. To break the loop, just
8551 * return here.
8552 */
8553 if (entered)
8554 return;
8555 entered = TRUE;
8556
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008557 /*
8558 * Note that the window sizes are updated before reallocating the arrays,
8559 * thus we must not redraw here!
8560 */
8561 ++RedrawingDisabled;
8562
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 win_new_shellsize(); /* fit the windows in the new sized shell */
8564
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 comp_col(); /* recompute columns for shown command and ruler */
8566
8567 /*
8568 * We're changing the size of the screen.
8569 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8570 * - Move lines from the old arrays into the new arrays, clear extra
8571 * lines (unless the screen is going to be cleared).
8572 * - Free the old arrays.
8573 *
8574 * If anything fails, make ScreenLines NULL, so we don't do anything!
8575 * Continuing with the old ScreenLines may result in a crash, because the
8576 * size is wrong.
8577 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008578 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008580#ifdef FEAT_AUTOCMD
8581 if (aucmd_win != NULL)
8582 win_free_lsize(aucmd_win);
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
8585 new_ScreenLines = (schar_T *)lalloc((long_u)(
8586 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8587#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008588 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (enc_utf8)
8590 {
8591 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8592 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008593 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008594 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8596 }
8597 if (enc_dbcs == DBCS_JPNU)
8598 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8599 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8600#endif
8601 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8602 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8603 new_LineOffset = (unsigned *)lalloc((long_u)(
8604 Rows * sizeof(unsigned)), FALSE);
8605 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008606#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008607 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008610 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {
8612 if (win_alloc_lines(wp) == FAIL)
8613 {
8614 outofmem = TRUE;
8615#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008616 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617#endif
8618 }
8619 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008620#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008621 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8622 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008623 outofmem = TRUE;
8624#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008625#ifdef FEAT_WINDOWS
8626give_up:
8627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008629#ifdef FEAT_MBYTE
8630 for (i = 0; i < p_mco; ++i)
8631 if (new_ScreenLinesC[i] == NULL)
8632 break;
8633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 if (new_ScreenLines == NULL
8635#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008636 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8638#endif
8639 || new_ScreenAttrs == NULL
8640 || new_LineOffset == NULL
8641 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008642#ifdef FEAT_WINDOWS
8643 || new_TabPageIdxs == NULL
8644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 || outofmem)
8646 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008647 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008648 {
8649 /* guess the size */
8650 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8651
8652 /* Remember we did this to avoid getting outofmem messages over
8653 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008654 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 vim_free(new_ScreenLines);
8657 new_ScreenLines = NULL;
8658#ifdef FEAT_MBYTE
8659 vim_free(new_ScreenLinesUC);
8660 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008661 for (i = 0; i < p_mco; ++i)
8662 {
8663 vim_free(new_ScreenLinesC[i]);
8664 new_ScreenLinesC[i] = NULL;
8665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 vim_free(new_ScreenLines2);
8667 new_ScreenLines2 = NULL;
8668#endif
8669 vim_free(new_ScreenAttrs);
8670 new_ScreenAttrs = NULL;
8671 vim_free(new_LineOffset);
8672 new_LineOffset = NULL;
8673 vim_free(new_LineWraps);
8674 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008675#ifdef FEAT_WINDOWS
8676 vim_free(new_TabPageIdxs);
8677 new_TabPageIdxs = NULL;
8678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 }
8680 else
8681 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008682 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 for (new_row = 0; new_row < Rows; ++new_row)
8685 {
8686 new_LineOffset[new_row] = new_row * Columns;
8687 new_LineWraps[new_row] = FALSE;
8688
8689 /*
8690 * If the screen is not going to be cleared, copy as much as
8691 * possible from the old screen to the new one and clear the rest
8692 * (used when resizing the window at the "--more--" prompt or when
8693 * executing an external command, for the GUI).
8694 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008695 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 {
8697 (void)vim_memset(new_ScreenLines + new_row * Columns,
8698 ' ', (size_t)Columns * sizeof(schar_T));
8699#ifdef FEAT_MBYTE
8700 if (enc_utf8)
8701 {
8702 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8703 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008704 for (i = 0; i < p_mco; ++i)
8705 (void)vim_memset(new_ScreenLinesC[i]
8706 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 0, (size_t)Columns * sizeof(u8char_T));
8708 }
8709 if (enc_dbcs == DBCS_JPNU)
8710 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8711 0, (size_t)Columns * sizeof(schar_T));
8712#endif
8713 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8714 0, (size_t)Columns * sizeof(sattr_T));
8715 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008716 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {
8718 if (screen_Columns < Columns)
8719 len = screen_Columns;
8720 else
8721 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008722#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008723 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008724 * may be invalid now. Also when p_mco changes. */
8725 if (!(enc_utf8 && ScreenLinesUC == NULL)
8726 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008727#endif
8728 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8729 ScreenLines + LineOffset[old_row],
8730 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 if (enc_utf8 && ScreenLinesUC != NULL
8733 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {
8735 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8736 ScreenLinesUC + LineOffset[old_row],
8737 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008738 for (i = 0; i < p_mco; ++i)
8739 mch_memmove(new_ScreenLinesC[i]
8740 + new_LineOffset[new_row],
8741 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 (size_t)len * sizeof(u8char_T));
8743 }
8744 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8745 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8746 ScreenLines2 + LineOffset[old_row],
8747 (size_t)len * sizeof(schar_T));
8748#endif
8749 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8750 ScreenAttrs + LineOffset[old_row],
8751 (size_t)len * sizeof(sattr_T));
8752 }
8753 }
8754 }
8755 /* Use the last line of the screen for the current line. */
8756 current_ScreenLine = new_ScreenLines + Rows * Columns;
8757 }
8758
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008759 free_screenlines();
8760
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 ScreenLines = new_ScreenLines;
8762#ifdef FEAT_MBYTE
8763 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008764 for (i = 0; i < p_mco; ++i)
8765 ScreenLinesC[i] = new_ScreenLinesC[i];
8766 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 ScreenLines2 = new_ScreenLines2;
8768#endif
8769 ScreenAttrs = new_ScreenAttrs;
8770 LineOffset = new_LineOffset;
8771 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008772#ifdef FEAT_WINDOWS
8773 TabPageIdxs = new_TabPageIdxs;
8774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
8776 /* It's important that screen_Rows and screen_Columns reflect the actual
8777 * size of ScreenLines[]. Set them before calling anything. */
8778#ifdef FEAT_GUI
8779 old_Rows = screen_Rows;
8780#endif
8781 screen_Rows = Rows;
8782 screen_Columns = Columns;
8783
8784 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008785 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 screenclear2();
8787
8788#ifdef FEAT_GUI
8789 else if (gui.in_use
8790 && !gui.starting
8791 && ScreenLines != NULL
8792 && old_Rows != Rows)
8793 {
8794 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8795 /*
8796 * Adjust the position of the cursor, for when executing an external
8797 * command.
8798 */
8799 if (msg_row >= Rows) /* Rows got smaller */
8800 msg_row = Rows - 1; /* put cursor at last row */
8801 else if (Rows > old_Rows) /* Rows got bigger */
8802 msg_row += Rows - old_Rows; /* put cursor in same place */
8803 if (msg_col >= Columns) /* Columns got smaller */
8804 msg_col = Columns - 1; /* put cursor at last column */
8805 }
8806#endif
8807
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008809 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008810
8811#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008812 /*
8813 * Do not apply autocommands more than 3 times to avoid an endless loop
8814 * in case applying autocommands always changes Rows or Columns.
8815 */
8816 if (starting == 0 && ++retry_count <= 3)
8817 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008818 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008819 /* In rare cases, autocommands may have altered Rows or Columns,
8820 * jump back to check if we need to allocate the screen again. */
8821 goto retry;
8822 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
8826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008827free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008828{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008829#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008830 int i;
8831
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008832 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008833 for (i = 0; i < Screen_mco; ++i)
8834 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008835 vim_free(ScreenLines2);
8836#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008838 vim_free(ScreenAttrs);
8839 vim_free(LineOffset);
8840 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008841#ifdef FEAT_WINDOWS
8842 vim_free(TabPageIdxs);
8843#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008844}
8845
8846 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008847screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 check_for_delay(FALSE);
8850 screenalloc(FALSE); /* allocate screen buffers if size changed */
8851 screenclear2(); /* clear the screen */
8852}
8853
8854 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008855screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 int i;
8858
8859 if (starting == NO_SCREEN || ScreenLines == NULL
8860#ifdef FEAT_GUI
8861 || (gui.in_use && gui.starting)
8862#endif
8863 )
8864 return;
8865
8866#ifdef FEAT_GUI
8867 if (!gui.in_use)
8868#endif
8869 screen_attr = -1; /* force setting the Normal colors */
8870 screen_stop_highlight(); /* don't want highlighting here */
8871
8872#ifdef FEAT_CLIPBOARD
8873 /* disable selection without redrawing it */
8874 clip_scroll_selection(9999);
8875#endif
8876
8877 /* blank out ScreenLines */
8878 for (i = 0; i < Rows; ++i)
8879 {
8880 lineclear(LineOffset[i], (int)Columns);
8881 LineWraps[i] = FALSE;
8882 }
8883
8884 if (can_clear(T_CL))
8885 {
8886 out_str(T_CL); /* clear the display */
8887 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008888 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
8890 else
8891 {
8892 /* can't clear the screen, mark all chars with invalid attributes */
8893 for (i = 0; i < Rows; ++i)
8894 lineinvalid(LineOffset[i], (int)Columns);
8895 clear_cmdline = TRUE;
8896 }
8897
8898 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8899
8900 win_rest_invalid(firstwin);
8901 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008902#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008903 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (must_redraw == CLEAR) /* no need to clear again */
8906 must_redraw = NOT_VALID;
8907 compute_cmdrow();
8908 msg_row = cmdline_row; /* put cursor on last line for messages */
8909 msg_col = 0;
8910 screen_start(); /* don't know where cursor is now */
8911 msg_scrolled = 0; /* can't scroll back */
8912 msg_didany = FALSE;
8913 msg_didout = FALSE;
8914}
8915
8916/*
8917 * Clear one line in ScreenLines.
8918 */
8919 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008920lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8923#ifdef FEAT_MBYTE
8924 if (enc_utf8)
8925 (void)vim_memset(ScreenLinesUC + off, 0,
8926 (size_t)width * sizeof(u8char_T));
8927#endif
8928 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8929}
8930
8931/*
8932 * Mark one line in ScreenLines invalid by setting the attributes to an
8933 * invalid value.
8934 */
8935 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008936lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8939}
8940
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008941#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942/*
8943 * Copy part of a Screenline for vertically split window "wp".
8944 */
8945 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008946linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948 unsigned off_to = LineOffset[to] + wp->w_wincol;
8949 unsigned off_from = LineOffset[from] + wp->w_wincol;
8950
8951 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8952 wp->w_width * sizeof(schar_T));
8953# ifdef FEAT_MBYTE
8954 if (enc_utf8)
8955 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 int i;
8957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8959 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008960 for (i = 0; i < p_mco; ++i)
8961 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8962 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
8964 if (enc_dbcs == DBCS_JPNU)
8965 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8966 wp->w_width * sizeof(schar_T));
8967# endif
8968 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8969 wp->w_width * sizeof(sattr_T));
8970}
8971#endif
8972
8973/*
8974 * Return TRUE if clearing with term string "p" would work.
8975 * It can't work when the string is empty or it won't set the right background.
8976 */
8977 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008978can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 return (*p != NUL && (t_colors <= 1
8981#ifdef FEAT_GUI
8982 || gui.in_use
8983#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008984#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008985 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008986 || (!p_tgc && cterm_normal_bg_color == 0)
8987#else
8988 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008989#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02008990 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991}
8992
8993/*
8994 * Reset cursor position. Use whenever cursor was moved because of outputting
8995 * something directly to the screen (shell commands) or a terminal control
8996 * code.
8997 */
8998 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008999screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 screen_cur_row = screen_cur_col = 9999;
9002}
9003
9004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 * Move the cursor to position "row","col" in the screen.
9006 * This tries to find the most efficient way to move, minimizing the number of
9007 * characters sent to the terminal.
9008 */
9009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009010windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009012 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 int i;
9014 int plan;
9015 int cost;
9016 int wouldbe_col;
9017 int noinvcurs;
9018 char_u *bs;
9019 int goto_cost;
9020 int attr;
9021
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009022#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9024
9025#define PLAN_LE 1
9026#define PLAN_CR 2
9027#define PLAN_NL 3
9028#define PLAN_WRITE 4
9029 /* Can't use ScreenLines unless initialized */
9030 if (ScreenLines == NULL)
9031 return;
9032
9033 if (col != screen_cur_col || row != screen_cur_row)
9034 {
9035 /* Check for valid position. */
9036 if (row < 0) /* window without text lines? */
9037 row = 0;
9038 if (row >= screen_Rows)
9039 row = screen_Rows - 1;
9040 if (col >= screen_Columns)
9041 col = screen_Columns - 1;
9042
9043 /* check if no cursor movement is allowed in highlight mode */
9044 if (screen_attr && *T_MS == NUL)
9045 noinvcurs = HIGHL_COST;
9046 else
9047 noinvcurs = 0;
9048 goto_cost = GOTO_COST + noinvcurs;
9049
9050 /*
9051 * Plan how to do the positioning:
9052 * 1. Use CR to move it to column 0, same row.
9053 * 2. Use T_LE to move it a few columns to the left.
9054 * 3. Use NL to move a few lines down, column 0.
9055 * 4. Move a few columns to the right with T_ND or by writing chars.
9056 *
9057 * Don't do this if the cursor went beyond the last column, the cursor
9058 * position is unknown then (some terminals wrap, some don't )
9059 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009060 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 * characters to move the cursor to the right.
9062 */
9063 if (row >= screen_cur_row && screen_cur_col < Columns)
9064 {
9065 /*
9066 * If the cursor is in the same row, bigger col, we can use CR
9067 * or T_LE.
9068 */
9069 bs = NULL; /* init for GCC */
9070 attr = screen_attr;
9071 if (row == screen_cur_row && col < screen_cur_col)
9072 {
9073 /* "le" is preferred over "bc", because "bc" is obsolete */
9074 if (*T_LE)
9075 bs = T_LE; /* "cursor left" */
9076 else
9077 bs = T_BC; /* "backspace character (old) */
9078 if (*bs)
9079 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9080 else
9081 cost = 999;
9082 if (col + 1 < cost) /* using CR is less characters */
9083 {
9084 plan = PLAN_CR;
9085 wouldbe_col = 0;
9086 cost = 1; /* CR is just one character */
9087 }
9088 else
9089 {
9090 plan = PLAN_LE;
9091 wouldbe_col = col;
9092 }
9093 if (noinvcurs) /* will stop highlighting */
9094 {
9095 cost += noinvcurs;
9096 attr = 0;
9097 }
9098 }
9099
9100 /*
9101 * If the cursor is above where we want to be, we can use CR LF.
9102 */
9103 else if (row > screen_cur_row)
9104 {
9105 plan = PLAN_NL;
9106 wouldbe_col = 0;
9107 cost = (row - screen_cur_row) * 2; /* CR LF */
9108 if (noinvcurs) /* will stop highlighting */
9109 {
9110 cost += noinvcurs;
9111 attr = 0;
9112 }
9113 }
9114
9115 /*
9116 * If the cursor is in the same row, smaller col, just use write.
9117 */
9118 else
9119 {
9120 plan = PLAN_WRITE;
9121 wouldbe_col = screen_cur_col;
9122 cost = 0;
9123 }
9124
9125 /*
9126 * Check if any characters that need to be written have the
9127 * correct attributes. Also avoid UTF-8 characters.
9128 */
9129 i = col - wouldbe_col;
9130 if (i > 0)
9131 cost += i;
9132 if (cost < goto_cost && i > 0)
9133 {
9134 /*
9135 * Check if the attributes are correct without additionally
9136 * stopping highlighting.
9137 */
9138 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9139 while (i && *p++ == attr)
9140 --i;
9141 if (i != 0)
9142 {
9143 /*
9144 * Try if it works when highlighting is stopped here.
9145 */
9146 if (*--p == 0)
9147 {
9148 cost += noinvcurs;
9149 while (i && *p++ == 0)
9150 --i;
9151 }
9152 if (i != 0)
9153 cost = 999; /* different attributes, don't do it */
9154 }
9155#ifdef FEAT_MBYTE
9156 if (enc_utf8)
9157 {
9158 /* Don't use an UTF-8 char for positioning, it's slow. */
9159 for (i = wouldbe_col; i < col; ++i)
9160 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9161 {
9162 cost = 999;
9163 break;
9164 }
9165 }
9166#endif
9167 }
9168
9169 /*
9170 * We can do it without term_windgoto()!
9171 */
9172 if (cost < goto_cost)
9173 {
9174 if (plan == PLAN_LE)
9175 {
9176 if (noinvcurs)
9177 screen_stop_highlight();
9178 while (screen_cur_col > col)
9179 {
9180 out_str(bs);
9181 --screen_cur_col;
9182 }
9183 }
9184 else if (plan == PLAN_CR)
9185 {
9186 if (noinvcurs)
9187 screen_stop_highlight();
9188 out_char('\r');
9189 screen_cur_col = 0;
9190 }
9191 else if (plan == PLAN_NL)
9192 {
9193 if (noinvcurs)
9194 screen_stop_highlight();
9195 while (screen_cur_row < row)
9196 {
9197 out_char('\n');
9198 ++screen_cur_row;
9199 }
9200 screen_cur_col = 0;
9201 }
9202
9203 i = col - screen_cur_col;
9204 if (i > 0)
9205 {
9206 /*
9207 * Use cursor-right if it's one character only. Avoids
9208 * removing a line of pixels from the last bold char, when
9209 * using the bold trick in the GUI.
9210 */
9211 if (T_ND[0] != NUL && T_ND[1] == NUL)
9212 {
9213 while (i-- > 0)
9214 out_char(*T_ND);
9215 }
9216 else
9217 {
9218 int off;
9219
9220 off = LineOffset[row] + screen_cur_col;
9221 while (i-- > 0)
9222 {
9223 if (ScreenAttrs[off] != screen_attr)
9224 screen_stop_highlight();
9225#ifdef FEAT_MBYTE
9226 out_flush_check();
9227#endif
9228 out_char(ScreenLines[off]);
9229#ifdef FEAT_MBYTE
9230 if (enc_dbcs == DBCS_JPNU
9231 && ScreenLines[off] == 0x8e)
9232 out_char(ScreenLines2[off]);
9233#endif
9234 ++off;
9235 }
9236 }
9237 }
9238 }
9239 }
9240 else
9241 cost = 999;
9242
9243 if (cost >= goto_cost)
9244 {
9245 if (noinvcurs)
9246 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009247 if (row == screen_cur_row && (col > screen_cur_col)
9248 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 term_cursor_right(col - screen_cur_col);
9250 else
9251 term_windgoto(row, col);
9252 }
9253 screen_cur_row = row;
9254 screen_cur_col = col;
9255 }
9256}
9257
9258/*
9259 * Set cursor to its position in the current window.
9260 */
9261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009262setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264 if (redrawing())
9265 {
9266 validate_cursor();
9267 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9268 W_WINCOL(curwin) + (
9269#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009270 /* With 'rightleft' set and the cursor on a double-wide
9271 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9273# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009274 (has_mbyte
9275 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9276 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277# endif
9278 1)) :
9279#endif
9280 curwin->w_wcol));
9281 }
9282}
9283
9284
9285/*
9286 * insert 'line_count' lines at 'row' in window 'wp'
9287 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9288 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9289 * scrolling.
9290 * Returns FAIL if the lines are not inserted, OK for success.
9291 */
9292 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009293win_ins_lines(
9294 win_T *wp,
9295 int row,
9296 int line_count,
9297 int invalid,
9298 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 int did_delete;
9301 int nextrow;
9302 int lastrow;
9303 int retval;
9304
9305 if (invalid)
9306 wp->w_lines_valid = 0;
9307
9308 if (wp->w_height < 5)
9309 return FAIL;
9310
9311 if (line_count > wp->w_height - row)
9312 line_count = wp->w_height - row;
9313
9314 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9315 if (retval != MAYBE)
9316 return retval;
9317
9318 /*
9319 * If there is a next window or a status line, we first try to delete the
9320 * lines at the bottom to avoid messing what is after the window.
9321 * If this fails and there are following windows, don't do anything to avoid
9322 * messing up those windows, better just redraw.
9323 */
9324 did_delete = FALSE;
9325#ifdef FEAT_WINDOWS
9326 if (wp->w_next != NULL || wp->w_status_height)
9327 {
9328 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9329 line_count, (int)Rows, FALSE, NULL) == OK)
9330 did_delete = TRUE;
9331 else if (wp->w_next)
9332 return FAIL;
9333 }
9334#endif
9335 /*
9336 * if no lines deleted, blank the lines that will end up below the window
9337 */
9338 if (!did_delete)
9339 {
9340#ifdef FEAT_WINDOWS
9341 wp->w_redr_status = TRUE;
9342#endif
9343 redraw_cmdline = TRUE;
9344 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9345 lastrow = nextrow + line_count;
9346 if (lastrow > Rows)
9347 lastrow = Rows;
9348 screen_fill(nextrow - line_count, lastrow - line_count,
9349 W_WINCOL(wp), (int)W_ENDCOL(wp),
9350 ' ', ' ', 0);
9351 }
9352
9353 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9354 == FAIL)
9355 {
9356 /* deletion will have messed up other windows */
9357 if (did_delete)
9358 {
9359#ifdef FEAT_WINDOWS
9360 wp->w_redr_status = TRUE;
9361#endif
9362 win_rest_invalid(W_NEXT(wp));
9363 }
9364 return FAIL;
9365 }
9366
9367 return OK;
9368}
9369
9370/*
9371 * delete "line_count" window lines at "row" in window "wp"
9372 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9373 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9374 * scrolling
9375 * Return OK for success, FAIL if the lines are not deleted.
9376 */
9377 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009378win_del_lines(
9379 win_T *wp,
9380 int row,
9381 int line_count,
9382 int invalid,
9383 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385 int retval;
9386
9387 if (invalid)
9388 wp->w_lines_valid = 0;
9389
9390 if (line_count > wp->w_height - row)
9391 line_count = wp->w_height - row;
9392
9393 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9394 if (retval != MAYBE)
9395 return retval;
9396
9397 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9398 (int)Rows, FALSE, NULL) == FAIL)
9399 return FAIL;
9400
9401#ifdef FEAT_WINDOWS
9402 /*
9403 * If there are windows or status lines below, try to put them at the
9404 * correct place. If we can't do that, they have to be redrawn.
9405 */
9406 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9407 {
9408 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9409 line_count, (int)Rows, NULL) == FAIL)
9410 {
9411 wp->w_redr_status = TRUE;
9412 win_rest_invalid(wp->w_next);
9413 }
9414 }
9415 /*
9416 * If this is the last window and there is no status line, redraw the
9417 * command line later.
9418 */
9419 else
9420#endif
9421 redraw_cmdline = TRUE;
9422 return OK;
9423}
9424
9425/*
9426 * Common code for win_ins_lines() and win_del_lines().
9427 * Returns OK or FAIL when the work has been done.
9428 * Returns MAYBE when not finished yet.
9429 */
9430 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009431win_do_lines(
9432 win_T *wp,
9433 int row,
9434 int line_count,
9435 int mayclear,
9436 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 int retval;
9439
9440 if (!redrawing() || line_count <= 0)
9441 return FAIL;
9442
9443 /* only a few lines left: redraw is faster */
9444 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009445#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 && wp->w_width == Columns
9447#endif
9448 )
9449 {
9450 screenclear(); /* will set wp->w_lines_valid to 0 */
9451 return FAIL;
9452 }
9453
9454 /*
9455 * Delete all remaining lines
9456 */
9457 if (row + line_count >= wp->w_height)
9458 {
9459 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9460 W_WINCOL(wp), (int)W_ENDCOL(wp),
9461 ' ', ' ', 0);
9462 return OK;
9463 }
9464
9465 /*
9466 * when scrolling, the message on the command line should be cleared,
9467 * otherwise it will stay there forever.
9468 */
9469 clear_cmdline = TRUE;
9470
9471 /*
9472 * If the terminal can set a scroll region, use that.
9473 * Always do this in a vertically split window. This will redraw from
9474 * ScreenLines[] when t_CV isn't defined. That's faster than using
9475 * win_line().
9476 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009477 * a character in the lower right corner of the scroll region may cause a
9478 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 */
9480 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009481#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 || W_WIDTH(wp) != Columns
9483#endif
9484 )
9485 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009486#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9488#endif
9489 scroll_region_set(wp, row);
9490 if (del)
9491 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9492 wp->w_height - row, FALSE, wp);
9493 else
9494 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9495 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009496#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9498#endif
9499 scroll_region_reset();
9500 return retval;
9501 }
9502
9503#ifdef FEAT_WINDOWS
9504 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9505 return FAIL;
9506#endif
9507
9508 return MAYBE;
9509}
9510
9511/*
9512 * window 'wp' and everything after it is messed up, mark it for redraw
9513 */
9514 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009515win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517#ifdef FEAT_WINDOWS
9518 while (wp != NULL)
9519#else
9520 if (wp != NULL)
9521#endif
9522 {
9523 redraw_win_later(wp, NOT_VALID);
9524#ifdef FEAT_WINDOWS
9525 wp->w_redr_status = TRUE;
9526 wp = wp->w_next;
9527#endif
9528 }
9529 redraw_cmdline = TRUE;
9530}
9531
9532/*
9533 * The rest of the routines in this file perform screen manipulations. The
9534 * given operation is performed physically on the screen. The corresponding
9535 * change is also made to the internal screen image. In this way, the editor
9536 * anticipates the effect of editing changes on the appearance of the screen.
9537 * That way, when we call screenupdate a complete redraw isn't usually
9538 * necessary. Another advantage is that we can keep adding code to anticipate
9539 * screen changes, and in the meantime, everything still works.
9540 */
9541
9542/*
9543 * types for inserting or deleting lines
9544 */
9545#define USE_T_CAL 1
9546#define USE_T_CDL 2
9547#define USE_T_AL 3
9548#define USE_T_CE 4
9549#define USE_T_DL 5
9550#define USE_T_SR 6
9551#define USE_NL 7
9552#define USE_T_CD 8
9553#define USE_REDRAW 9
9554
9555/*
9556 * insert lines on the screen and update ScreenLines[]
9557 * 'end' is the line after the scrolled part. Normally it is Rows.
9558 * When scrolling region used 'off' is the offset from the top for the region.
9559 * 'row' and 'end' are relative to the start of the region.
9560 *
9561 * return FAIL for failure, OK for success.
9562 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009564screen_ins_lines(
9565 int off,
9566 int row,
9567 int line_count,
9568 int end,
9569 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 int i;
9572 int j;
9573 unsigned temp;
9574 int cursor_row;
9575 int type;
9576 int result_empty;
9577 int can_ce = can_clear(T_CE);
9578
9579 /*
9580 * FAIL if
9581 * - there is no valid screen
9582 * - the screen has to be redrawn completely
9583 * - the line count is less than one
9584 * - the line count is more than 'ttyscroll'
9585 */
9586 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9587 return FAIL;
9588
9589 /*
9590 * There are seven ways to insert lines:
9591 * 0. When in a vertically split window and t_CV isn't set, redraw the
9592 * characters from ScreenLines[].
9593 * 1. Use T_CD (clear to end of display) if it exists and the result of
9594 * the insert is just empty lines
9595 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9596 * present or line_count > 1. It looks better if we do all the inserts
9597 * at once.
9598 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9599 * insert is just empty lines and T_CE is not present or line_count >
9600 * 1.
9601 * 4. Use T_AL (insert line) if it exists.
9602 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9603 * just empty lines.
9604 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9605 * just empty lines.
9606 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9607 * the 'da' flag is not set or we have clear line capability.
9608 * 8. redraw the characters from ScreenLines[].
9609 *
9610 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9611 * the scrollbar for the window. It does have insert line, use that if it
9612 * exists.
9613 */
9614 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009615#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9617 type = USE_REDRAW;
9618 else
9619#endif
9620 if (can_clear(T_CD) && result_empty)
9621 type = USE_T_CD;
9622 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9623 type = USE_T_CAL;
9624 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9625 type = USE_T_CDL;
9626 else if (*T_AL != NUL)
9627 type = USE_T_AL;
9628 else if (can_ce && result_empty)
9629 type = USE_T_CE;
9630 else if (*T_DL != NUL && result_empty)
9631 type = USE_T_DL;
9632 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9633 type = USE_T_SR;
9634 else
9635 return FAIL;
9636
9637 /*
9638 * For clearing the lines screen_del_lines() is used. This will also take
9639 * care of t_db if necessary.
9640 */
9641 if (type == USE_T_CD || type == USE_T_CDL ||
9642 type == USE_T_CE || type == USE_T_DL)
9643 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9644
9645 /*
9646 * If text is retained below the screen, first clear or delete as many
9647 * lines at the bottom of the window as are about to be inserted so that
9648 * the deleted lines won't later surface during a screen_del_lines.
9649 */
9650 if (*T_DB)
9651 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9652
9653#ifdef FEAT_CLIPBOARD
9654 /* Remove a modeless selection when inserting lines halfway the screen
9655 * or not the full width of the screen. */
9656 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009657# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 || (wp != NULL && wp->w_width != Columns)
9659# endif
9660 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009661 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 else
9663 clip_scroll_selection(-line_count);
9664#endif
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666#ifdef FEAT_GUI
9667 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9668 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009669 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670#endif
9671
9672 if (*T_CCS != NUL) /* cursor relative to region */
9673 cursor_row = row;
9674 else
9675 cursor_row = row + off;
9676
9677 /*
9678 * Shift LineOffset[] line_count down to reflect the inserted lines.
9679 * Clear the inserted lines in ScreenLines[].
9680 */
9681 row += off;
9682 end += off;
9683 for (i = 0; i < line_count; ++i)
9684 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009685#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 if (wp != NULL && wp->w_width != Columns)
9687 {
9688 /* need to copy part of a line */
9689 j = end - 1 - i;
9690 while ((j -= line_count) >= row)
9691 linecopy(j + line_count, j, wp);
9692 j += line_count;
9693 if (can_clear((char_u *)" "))
9694 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9695 else
9696 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9697 LineWraps[j] = FALSE;
9698 }
9699 else
9700#endif
9701 {
9702 j = end - 1 - i;
9703 temp = LineOffset[j];
9704 while ((j -= line_count) >= row)
9705 {
9706 LineOffset[j + line_count] = LineOffset[j];
9707 LineWraps[j + line_count] = LineWraps[j];
9708 }
9709 LineOffset[j + line_count] = temp;
9710 LineWraps[j + line_count] = FALSE;
9711 if (can_clear((char_u *)" "))
9712 lineclear(temp, (int)Columns);
9713 else
9714 lineinvalid(temp, (int)Columns);
9715 }
9716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717
9718 screen_stop_highlight();
9719 windgoto(cursor_row, 0);
9720
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009721#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 /* redraw the characters */
9723 if (type == USE_REDRAW)
9724 redraw_block(row, end, wp);
9725 else
9726#endif
9727 if (type == USE_T_CAL)
9728 {
9729 term_append_lines(line_count);
9730 screen_start(); /* don't know where cursor is now */
9731 }
9732 else
9733 {
9734 for (i = 0; i < line_count; i++)
9735 {
9736 if (type == USE_T_AL)
9737 {
9738 if (i && cursor_row != 0)
9739 windgoto(cursor_row, 0);
9740 out_str(T_AL);
9741 }
9742 else /* type == USE_T_SR */
9743 out_str(T_SR);
9744 screen_start(); /* don't know where cursor is now */
9745 }
9746 }
9747
9748 /*
9749 * With scroll-reverse and 'da' flag set we need to clear the lines that
9750 * have been scrolled down into the region.
9751 */
9752 if (type == USE_T_SR && *T_DA)
9753 {
9754 for (i = 0; i < line_count; ++i)
9755 {
9756 windgoto(off + i, 0);
9757 out_str(T_CE);
9758 screen_start(); /* don't know where cursor is now */
9759 }
9760 }
9761
9762#ifdef FEAT_GUI
9763 gui_can_update_cursor();
9764 if (gui.in_use)
9765 out_flush(); /* always flush after a scroll */
9766#endif
9767 return OK;
9768}
9769
9770/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009771 * Delete lines on the screen and update ScreenLines[].
9772 * "end" is the line after the scrolled part. Normally it is Rows.
9773 * When scrolling region used "off" is the offset from the top for the region.
9774 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 *
9776 * Return OK for success, FAIL if the lines are not deleted.
9777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009779screen_del_lines(
9780 int off,
9781 int row,
9782 int line_count,
9783 int end,
9784 int force, /* even when line_count > p_ttyscroll */
9785 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
9787 int j;
9788 int i;
9789 unsigned temp;
9790 int cursor_row;
9791 int cursor_end;
9792 int result_empty; /* result is empty until end of region */
9793 int can_delete; /* deleting line codes can be used */
9794 int type;
9795
9796 /*
9797 * FAIL if
9798 * - there is no valid screen
9799 * - the screen has to be redrawn completely
9800 * - the line count is less than one
9801 * - the line count is more than 'ttyscroll'
9802 */
9803 if (!screen_valid(TRUE) || line_count <= 0 ||
9804 (!force && line_count > p_ttyscroll))
9805 return FAIL;
9806
9807 /*
9808 * Check if the rest of the current region will become empty.
9809 */
9810 result_empty = row + line_count >= end;
9811
9812 /*
9813 * We can delete lines only when 'db' flag not set or when 'ce' option
9814 * available.
9815 */
9816 can_delete = (*T_DB == NUL || can_clear(T_CE));
9817
9818 /*
9819 * There are six ways to delete lines:
9820 * 0. When in a vertically split window and t_CV isn't set, redraw the
9821 * characters from ScreenLines[].
9822 * 1. Use T_CD if it exists and the result is empty.
9823 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9824 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9825 * none of the other ways work.
9826 * 4. Use T_CE (erase line) if the result is empty.
9827 * 5. Use T_DL (delete line) if it exists.
9828 * 6. redraw the characters from ScreenLines[].
9829 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009830#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9832 type = USE_REDRAW;
9833 else
9834#endif
9835 if (can_clear(T_CD) && result_empty)
9836 type = USE_T_CD;
9837#if defined(__BEOS__) && defined(BEOS_DR8)
9838 /*
9839 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9840 * its internal termcap... this works okay for tests which test *T_DB !=
9841 * NUL. It has the disadvantage that the user cannot use any :set t_*
9842 * command to get T_DB (back) to empty_option, only :set term=... will do
9843 * the trick...
9844 * Anyway, this hack will hopefully go away with the next OS release.
9845 * (Olaf Seibert)
9846 */
9847 else if (row == 0 && T_DB == empty_option
9848 && (line_count == 1 || *T_CDL == NUL))
9849#else
9850 else if (row == 0 && (
9851#ifndef AMIGA
9852 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9853 * up, so use delete-line command */
9854 line_count == 1 ||
9855#endif
9856 *T_CDL == NUL))
9857#endif
9858 type = USE_NL;
9859 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9860 type = USE_T_CDL;
9861 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009862#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 && (wp == NULL || wp->w_width == Columns)
9864#endif
9865 )
9866 type = USE_T_CE;
9867 else if (*T_DL != NUL && can_delete)
9868 type = USE_T_DL;
9869 else if (*T_CDL != NUL && can_delete)
9870 type = USE_T_CDL;
9871 else
9872 return FAIL;
9873
9874#ifdef FEAT_CLIPBOARD
9875 /* Remove a modeless selection when deleting lines halfway the screen or
9876 * not the full width of the screen. */
9877 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009878# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 || (wp != NULL && wp->w_width != Columns)
9880# endif
9881 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009882 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 else
9884 clip_scroll_selection(line_count);
9885#endif
9886
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887#ifdef FEAT_GUI
9888 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9889 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009890 gui_dont_update_cursor(gui.cursor_row >= row + off
9891 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892#endif
9893
9894 if (*T_CCS != NUL) /* cursor relative to region */
9895 {
9896 cursor_row = row;
9897 cursor_end = end;
9898 }
9899 else
9900 {
9901 cursor_row = row + off;
9902 cursor_end = end + off;
9903 }
9904
9905 /*
9906 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9907 * Clear the inserted lines in ScreenLines[].
9908 */
9909 row += off;
9910 end += off;
9911 for (i = 0; i < line_count; ++i)
9912 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009913#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (wp != NULL && wp->w_width != Columns)
9915 {
9916 /* need to copy part of a line */
9917 j = row + i;
9918 while ((j += line_count) <= end - 1)
9919 linecopy(j - line_count, j, wp);
9920 j -= line_count;
9921 if (can_clear((char_u *)" "))
9922 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9923 else
9924 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9925 LineWraps[j] = FALSE;
9926 }
9927 else
9928#endif
9929 {
9930 /* whole width, moving the line pointers is faster */
9931 j = row + i;
9932 temp = LineOffset[j];
9933 while ((j += line_count) <= end - 1)
9934 {
9935 LineOffset[j - line_count] = LineOffset[j];
9936 LineWraps[j - line_count] = LineWraps[j];
9937 }
9938 LineOffset[j - line_count] = temp;
9939 LineWraps[j - line_count] = FALSE;
9940 if (can_clear((char_u *)" "))
9941 lineclear(temp, (int)Columns);
9942 else
9943 lineinvalid(temp, (int)Columns);
9944 }
9945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
9947 screen_stop_highlight();
9948
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009949#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 /* redraw the characters */
9951 if (type == USE_REDRAW)
9952 redraw_block(row, end, wp);
9953 else
9954#endif
9955 if (type == USE_T_CD) /* delete the lines */
9956 {
9957 windgoto(cursor_row, 0);
9958 out_str(T_CD);
9959 screen_start(); /* don't know where cursor is now */
9960 }
9961 else if (type == USE_T_CDL)
9962 {
9963 windgoto(cursor_row, 0);
9964 term_delete_lines(line_count);
9965 screen_start(); /* don't know where cursor is now */
9966 }
9967 /*
9968 * Deleting lines at top of the screen or scroll region: Just scroll
9969 * the whole screen (scroll region) up by outputting newlines on the
9970 * last line.
9971 */
9972 else if (type == USE_NL)
9973 {
9974 windgoto(cursor_end - 1, 0);
9975 for (i = line_count; --i >= 0; )
9976 out_char('\n'); /* cursor will remain on same line */
9977 }
9978 else
9979 {
9980 for (i = line_count; --i >= 0; )
9981 {
9982 if (type == USE_T_DL)
9983 {
9984 windgoto(cursor_row, 0);
9985 out_str(T_DL); /* delete a line */
9986 }
9987 else /* type == USE_T_CE */
9988 {
9989 windgoto(cursor_row + i, 0);
9990 out_str(T_CE); /* erase a line */
9991 }
9992 screen_start(); /* don't know where cursor is now */
9993 }
9994 }
9995
9996 /*
9997 * If the 'db' flag is set, we need to clear the lines that have been
9998 * scrolled up at the bottom of the region.
9999 */
10000 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10001 {
10002 for (i = line_count; i > 0; --i)
10003 {
10004 windgoto(cursor_end - i, 0);
10005 out_str(T_CE); /* erase a line */
10006 screen_start(); /* don't know where cursor is now */
10007 }
10008 }
10009
10010#ifdef FEAT_GUI
10011 gui_can_update_cursor();
10012 if (gui.in_use)
10013 out_flush(); /* always flush after a scroll */
10014#endif
10015
10016 return OK;
10017}
10018
10019/*
10020 * show the current mode and ruler
10021 *
10022 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10023 * If clear_cmdline is FALSE there may be a message there that needs to be
10024 * cleared only if a mode is shown.
10025 * Return the length of the message (0 if no message).
10026 */
10027 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010028showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 int need_clear;
10031 int length = 0;
10032 int do_mode;
10033 int attr;
10034 int nwr_save;
10035#ifdef FEAT_INS_EXPAND
10036 int sub_attr;
10037#endif
10038
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010039 do_mode = ((p_smd && msg_silent == 0)
10040 && ((State & INSERT)
10041 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010042 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (do_mode || Recording)
10044 {
10045 /*
10046 * Don't show mode right now, when not redrawing or inside a mapping.
10047 * Call char_avail() only when we are going to show something, because
10048 * it takes a bit of time.
10049 */
10050 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10051 {
10052 redraw_cmdline = TRUE; /* show mode later */
10053 return 0;
10054 }
10055
10056 nwr_save = need_wait_return;
10057
10058 /* wait a bit before overwriting an important message */
10059 check_for_delay(FALSE);
10060
10061 /* if the cmdline is more than one line high, erase top lines */
10062 need_clear = clear_cmdline;
10063 if (clear_cmdline && cmdline_row < Rows - 1)
10064 msg_clr_cmdline(); /* will reset clear_cmdline */
10065
10066 /* Position on the last line in the window, column 0 */
10067 msg_pos_mode();
10068 cursor_off();
10069 attr = hl_attr(HLF_CM); /* Highlight mode */
10070 if (do_mode)
10071 {
10072 MSG_PUTS_ATTR("--", attr);
10073#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010074 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010075# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010076 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010077# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010078 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010079# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010080 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010081# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 MSG_PUTS_ATTR(" IM", attr);
10083# else
10084 MSG_PUTS_ATTR(" XIM", attr);
10085# endif
10086#endif
10087#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10088 if (gui.in_use)
10089 {
10090 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010091 {
10092 /* HANGUL */
10093 if (enc_utf8)
10094 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10095 else
10096 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 }
10099#endif
10100#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010101 /* CTRL-X in Insert mode */
10102 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 {
10104 /* These messages can get long, avoid a wrap in a narrow
10105 * window. Prefer showing edit_submode_extra. */
10106 length = (Rows - msg_row) * Columns - 3;
10107 if (edit_submode_extra != NULL)
10108 length -= vim_strsize(edit_submode_extra);
10109 if (length > 0)
10110 {
10111 if (edit_submode_pre != NULL)
10112 length -= vim_strsize(edit_submode_pre);
10113 if (length - vim_strsize(edit_submode) > 0)
10114 {
10115 if (edit_submode_pre != NULL)
10116 msg_puts_attr(edit_submode_pre, attr);
10117 msg_puts_attr(edit_submode, attr);
10118 }
10119 if (edit_submode_extra != NULL)
10120 {
10121 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10122 if ((int)edit_submode_highl < (int)HLF_COUNT)
10123 sub_attr = hl_attr(edit_submode_highl);
10124 else
10125 sub_attr = attr;
10126 msg_puts_attr(edit_submode_extra, sub_attr);
10127 }
10128 }
10129 length = 0;
10130 }
10131 else
10132#endif
10133 {
10134#ifdef FEAT_VREPLACE
10135 if (State & VREPLACE_FLAG)
10136 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10137 else
10138#endif
10139 if (State & REPLACE_FLAG)
10140 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10141 else if (State & INSERT)
10142 {
10143#ifdef FEAT_RIGHTLEFT
10144 if (p_ri)
10145 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10146#endif
10147 MSG_PUTS_ATTR(_(" INSERT"), attr);
10148 }
10149 else if (restart_edit == 'I')
10150 MSG_PUTS_ATTR(_(" (insert)"), attr);
10151 else if (restart_edit == 'R')
10152 MSG_PUTS_ATTR(_(" (replace)"), attr);
10153 else if (restart_edit == 'V')
10154 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10155#ifdef FEAT_RIGHTLEFT
10156 if (p_hkmap)
10157 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10158# ifdef FEAT_FKMAP
10159 if (p_fkmap)
10160 MSG_PUTS_ATTR(farsi_text_5, attr);
10161# endif
10162#endif
10163#ifdef FEAT_KEYMAP
10164 if (State & LANGMAP)
10165 {
10166# ifdef FEAT_ARABIC
10167 if (curwin->w_p_arab)
10168 MSG_PUTS_ATTR(_(" Arabic"), attr);
10169 else
10170# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010171 if (get_keymap_str(curwin, (char_u *)" (%s)",
10172 NameBuff, MAXPATHL))
10173 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175#endif
10176 if ((State & INSERT) && p_paste)
10177 MSG_PUTS_ATTR(_(" (paste)"), attr);
10178
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (VIsual_active)
10180 {
10181 char *p;
10182
10183 /* Don't concatenate separate words to avoid translation
10184 * problems. */
10185 switch ((VIsual_select ? 4 : 0)
10186 + (VIsual_mode == Ctrl_V) * 2
10187 + (VIsual_mode == 'V'))
10188 {
10189 case 0: p = N_(" VISUAL"); break;
10190 case 1: p = N_(" VISUAL LINE"); break;
10191 case 2: p = N_(" VISUAL BLOCK"); break;
10192 case 4: p = N_(" SELECT"); break;
10193 case 5: p = N_(" SELECT LINE"); break;
10194 default: p = N_(" SELECT BLOCK"); break;
10195 }
10196 MSG_PUTS_ATTR(_(p), attr);
10197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 MSG_PUTS_ATTR(" --", attr);
10199 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 need_clear = TRUE;
10202 }
10203 if (Recording
10204#ifdef FEAT_INS_EXPAND
10205 && edit_submode == NULL /* otherwise it gets too long */
10206#endif
10207 )
10208 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010209 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 need_clear = TRUE;
10211 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010212
10213 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (need_clear || clear_cmdline)
10215 msg_clr_eos();
10216 msg_didout = FALSE; /* overwrite this message */
10217 length = msg_col;
10218 msg_col = 0;
10219 need_wait_return = nwr_save; /* never ask for hit-return for this */
10220 }
10221 else if (clear_cmdline && msg_silent == 0)
10222 /* Clear the whole command line. Will reset "clear_cmdline". */
10223 msg_clr_cmdline();
10224
10225#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 /* In Visual mode the size of the selected area must be redrawn. */
10227 if (VIsual_active)
10228 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229
10230 /* If the last window has no status line, the ruler is after the mode
10231 * message and must be redrawn */
10232 if (redrawing()
10233# ifdef FEAT_WINDOWS
10234 && lastwin->w_status_height == 0
10235# endif
10236 )
10237 win_redr_ruler(lastwin, TRUE);
10238#endif
10239 redraw_cmdline = FALSE;
10240 clear_cmdline = FALSE;
10241
10242 return length;
10243}
10244
10245/*
10246 * Position for a mode message.
10247 */
10248 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010249msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
10251 msg_col = 0;
10252 msg_row = Rows - 1;
10253}
10254
10255/*
10256 * Delete mode message. Used when ESC is typed which is expected to end
10257 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010258 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 */
10260 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010261unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262{
10263 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010264 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 */
10266 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10267 redraw_cmdline = TRUE; /* delete mode later */
10268 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010269 clearmode();
10270}
10271
10272/*
10273 * Clear the mode message.
10274 */
10275 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010276clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010277{
10278 msg_pos_mode();
10279 if (Recording)
10280 recording_mode(hl_attr(HLF_CM));
10281 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282}
10283
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010284 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010285recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010286{
10287 MSG_PUTS_ATTR(_("recording"), attr);
10288 if (!shortmess(SHM_RECORDING))
10289 {
10290 char_u s[4];
10291 sprintf((char *)s, " @%c", Recording);
10292 MSG_PUTS_ATTR(s, attr);
10293 }
10294}
10295
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010296#if defined(FEAT_WINDOWS)
10297/*
10298 * Draw the tab pages line at the top of the Vim window.
10299 */
10300 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010301draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010302{
10303 int tabcount = 0;
10304 tabpage_T *tp;
10305 int tabwidth;
10306 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010307 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010308 int attr;
10309 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010310 win_T *cwp;
10311 int wincount;
10312 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010313 int c;
10314 int len;
10315 int attr_sel = hl_attr(HLF_TPS);
10316 int attr_nosel = hl_attr(HLF_TP);
10317 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010318 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010319 int room;
10320 int use_sep_chars = (t_colors < 8
10321#ifdef FEAT_GUI
10322 && !gui.in_use
10323#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010324#ifdef FEAT_TERMGUICOLORS
10325 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010326#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010327 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010328
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010329 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010330
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010331#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010332 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010333 if (gui_use_tabline())
10334 {
10335 gui_update_tabline();
10336 return;
10337 }
10338#endif
10339
10340 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010341 return;
10342
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010343#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010344
10345 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10346 for (scol = 0; scol < Columns; ++scol)
10347 TabPageIdxs[scol] = 0;
10348
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010349 /* Use the 'tabline' option if it's set. */
10350 if (*p_tal != NUL)
10351 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010352 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010353
10354 /* Check for an error. If there is one we would loop in redrawing the
10355 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010356 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010357 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010358 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010359 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010360 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010361 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010362 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010364#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010365 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010366 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010367 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010368
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10370 if (tabwidth < 6)
10371 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010372
Bram Moolenaar238a5642006-02-21 22:12:05 +000010373 attr = attr_nosel;
10374 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010375 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010376 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10377 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010378 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010379 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010380
Bram Moolenaar238a5642006-02-21 22:12:05 +000010381 if (tp->tp_topframe == topframe)
10382 attr = attr_sel;
10383 if (use_sep_chars && col > 0)
10384 screen_putchar('|', 0, col++, attr);
10385
10386 if (tp->tp_topframe != topframe)
10387 attr = attr_nosel;
10388
10389 screen_putchar(' ', 0, col++, attr);
10390
10391 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010392 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010393 cwp = curwin;
10394 wp = firstwin;
10395 }
10396 else
10397 {
10398 cwp = tp->tp_curwin;
10399 wp = tp->tp_firstwin;
10400 }
10401
10402 modified = FALSE;
10403 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10404 if (bufIsChanged(wp->w_buffer))
10405 modified = TRUE;
10406 if (modified || wincount > 1)
10407 {
10408 if (wincount > 1)
10409 {
10410 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010411 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010412 if (col + len >= Columns - 3)
10413 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010414 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010415#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010416 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010417#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010418 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010419#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010420 );
10421 col += len;
10422 }
10423 if (modified)
10424 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10425 screen_putchar(' ', 0, col++, attr);
10426 }
10427
10428 room = scol - col + tabwidth - 1;
10429 if (room > 0)
10430 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010431 /* Get buffer name in NameBuff[] */
10432 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010433 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010434 len = vim_strsize(NameBuff);
10435 p = NameBuff;
10436#ifdef FEAT_MBYTE
10437 if (has_mbyte)
10438 while (len > room)
10439 {
10440 len -= ptr2cells(p);
10441 mb_ptr_adv(p);
10442 }
10443 else
10444#endif
10445 if (len > room)
10446 {
10447 p += len - room;
10448 len = room;
10449 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010450 if (len > Columns - col - 1)
10451 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010452
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010453 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010454 col += len;
10455 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010456 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010457
10458 /* Store the tab page number in TabPageIdxs[], so that
10459 * jump_to_mouse() knows where each one is. */
10460 ++tabcount;
10461 while (scol < col)
10462 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010463 }
10464
Bram Moolenaar238a5642006-02-21 22:12:05 +000010465 if (use_sep_chars)
10466 c = '_';
10467 else
10468 c = ' ';
10469 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010470
10471 /* Put an "X" for closing the current tab if there are several. */
10472 if (first_tabpage->tp_next != NULL)
10473 {
10474 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10475 TabPageIdxs[Columns - 1] = -999;
10476 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010477 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010478
10479 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10480 * set. */
10481 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010483
10484/*
10485 * Get buffer name for "buf" into NameBuff[].
10486 * Takes care of special buffer names and translates special characters.
10487 */
10488 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010489get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010490{
10491 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010492 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010493 else
10494 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10495 trans_characters(NameBuff, MAXPATHL);
10496}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010497#endif
10498
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10500/*
10501 * Get the character to use in a status line. Get its attributes in "*attr".
10502 */
10503 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010504fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505{
10506 int fill;
10507 if (is_curwin)
10508 {
10509 *attr = hl_attr(HLF_S);
10510 fill = fill_stl;
10511 }
10512 else
10513 {
10514 *attr = hl_attr(HLF_SNC);
10515 fill = fill_stlnc;
10516 }
10517 /* Use fill when there is highlighting, and highlighting of current
10518 * window differs, or the fillchars differ, or this is not the
10519 * current window */
10520 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10521 || !is_curwin || firstwin == lastwin)
10522 || (fill_stl != fill_stlnc)))
10523 return fill;
10524 if (is_curwin)
10525 return '^';
10526 return '=';
10527}
10528#endif
10529
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010530#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531/*
10532 * Get the character to use in a separator between vertically split windows.
10533 * Get its attributes in "*attr".
10534 */
10535 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010536fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537{
10538 *attr = hl_attr(HLF_C);
10539 if (*attr == 0 && fill_vert == ' ')
10540 return '|';
10541 else
10542 return fill_vert;
10543}
10544#endif
10545
10546/*
10547 * Return TRUE if redrawing should currently be done.
10548 */
10549 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010550redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552 return (!RedrawingDisabled
10553 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10554}
10555
10556/*
10557 * Return TRUE if printing messages should currently be done.
10558 */
10559 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010560messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561{
10562 return (!(p_lz && char_avail() && !KeyTyped));
10563}
10564
10565/*
10566 * Show current status info in ruler and various other places
10567 * If always is FALSE, only show ruler if position has changed.
10568 */
10569 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010570showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571{
10572 if (!always && !redrawing())
10573 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010574#ifdef FEAT_INS_EXPAND
10575 if (pum_visible())
10576 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010577# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010578 /* Don't redraw right now, do it later. */
10579 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010580# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010581 return;
10582 }
10583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010585 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010586 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010587 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 else
10590#endif
10591#ifdef FEAT_CMDL_INFO
10592 win_redr_ruler(curwin, always);
10593#endif
10594
10595#ifdef FEAT_TITLE
10596 if (need_maketitle
10597# ifdef FEAT_STL_OPT
10598 || (p_icon && (stl_syntax & STL_IN_ICON))
10599 || (p_title && (stl_syntax & STL_IN_TITLE))
10600# endif
10601 )
10602 maketitle();
10603#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010604#ifdef FEAT_WINDOWS
10605 /* Redraw the tab pages line if needed. */
10606 if (redraw_tabline)
10607 draw_tabline();
10608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609}
10610
10611#ifdef FEAT_CMDL_INFO
10612 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010613win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010615#define RULER_BUF_LEN 70
10616 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 int row;
10618 int fillchar;
10619 int attr;
10620 int empty_line = FALSE;
10621 colnr_T virtcol;
10622 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010623 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010625#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 int this_ru_col;
10627 int off = 0;
10628 int width = Columns;
10629# define WITH_OFF(x) x
10630# define WITH_WIDTH(x) x
10631#else
10632# define WITH_OFF(x) 0
10633# define WITH_WIDTH(x) Columns
10634# define this_ru_col ru_col
10635#endif
10636
10637 /* If 'ruler' off or redrawing disabled, don't do anything */
10638 if (!p_ru)
10639 return;
10640
10641 /*
10642 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10643 * after deleting lines, before cursor.lnum is corrected.
10644 */
10645 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10646 return;
10647
10648#ifdef FEAT_INS_EXPAND
10649 /* Don't draw the ruler while doing insert-completion, it might overwrite
10650 * the (long) mode message. */
10651# ifdef FEAT_WINDOWS
10652 if (wp == lastwin && lastwin->w_status_height == 0)
10653# endif
10654 if (edit_submode != NULL)
10655 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010656 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10657 if (pum_visible())
10658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#endif
10660
10661#ifdef FEAT_STL_OPT
10662 if (*p_ruf)
10663 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010664 int save_called_emsg = called_emsg;
10665
10666 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 if (called_emsg)
10669 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010670 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010671 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 return;
10673 }
10674#endif
10675
10676 /*
10677 * Check if not in Insert mode and the line is empty (will show "0-1").
10678 */
10679 if (!(State & INSERT)
10680 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10681 empty_line = TRUE;
10682
10683 /*
10684 * Only draw the ruler when something changed.
10685 */
10686 validate_virtcol_win(wp);
10687 if ( redraw_cmdline
10688 || always
10689 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10690 || wp->w_cursor.col != wp->w_ru_cursor.col
10691 || wp->w_virtcol != wp->w_ru_virtcol
10692#ifdef FEAT_VIRTUALEDIT
10693 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10694#endif
10695 || wp->w_topline != wp->w_ru_topline
10696 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10697#ifdef FEAT_DIFF
10698 || wp->w_topfill != wp->w_ru_topfill
10699#endif
10700 || empty_line != wp->w_ru_empty)
10701 {
10702 cursor_off();
10703#ifdef FEAT_WINDOWS
10704 if (wp->w_status_height)
10705 {
10706 row = W_WINROW(wp) + wp->w_height;
10707 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 off = W_WINCOL(wp);
10709 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 }
10711 else
10712#endif
10713 {
10714 row = Rows - 1;
10715 fillchar = ' ';
10716 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010717#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 width = Columns;
10719 off = 0;
10720#endif
10721 }
10722
10723 /* In list mode virtcol needs to be recomputed */
10724 virtcol = wp->w_virtcol;
10725 if (wp->w_p_list && lcs_tab1 == NUL)
10726 {
10727 wp->w_p_list = FALSE;
10728 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10729 wp->w_p_list = TRUE;
10730 }
10731
10732 /*
10733 * Some sprintfs return the length, some return a pointer.
10734 * To avoid portability problems we use strlen() here.
10735 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010736 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10738 ? 0L
10739 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010740 len = STRLEN(buffer);
10741 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10743 (int)virtcol + 1);
10744
10745 /*
10746 * Add a "50%" if there is room for it.
10747 * On the last line, don't print in the last column (scrolls the
10748 * screen up on some terminals).
10749 */
10750 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010751 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 o = i + vim_strsize(buffer + i + 1);
10753#ifdef FEAT_WINDOWS
10754 if (wp->w_status_height == 0) /* can't use last char of screen */
10755#endif
10756 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010757#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 this_ru_col = ru_col - (Columns - width);
10759 if (this_ru_col < 0)
10760 this_ru_col = 0;
10761#endif
10762 /* Never use more than half the window/screen width, leave the other
10763 * half for the filename. */
10764 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10765 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10766 if (this_ru_col + o < WITH_WIDTH(width))
10767 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010768 /* need at least 3 chars left for get_rel_pos() + NUL */
10769 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 {
10771#ifdef FEAT_MBYTE
10772 if (has_mbyte)
10773 i += (*mb_char2bytes)(fillchar, buffer + i);
10774 else
10775#endif
10776 buffer[i++] = fillchar;
10777 ++o;
10778 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010779 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 }
10781 /* Truncate at window boundary. */
10782#ifdef FEAT_MBYTE
10783 if (has_mbyte)
10784 {
10785 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010786 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 {
10788 o += (*mb_ptr2cells)(buffer + i);
10789 if (this_ru_col + o > WITH_WIDTH(width))
10790 {
10791 buffer[i] = NUL;
10792 break;
10793 }
10794 }
10795 }
10796 else
10797#endif
10798 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10799 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10800
10801 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10802 i = redraw_cmdline;
10803 screen_fill(row, row + 1,
10804 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10805 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10806 fillchar, fillchar, attr);
10807 /* don't redraw the cmdline because of showing the ruler */
10808 redraw_cmdline = i;
10809 wp->w_ru_cursor = wp->w_cursor;
10810 wp->w_ru_virtcol = wp->w_virtcol;
10811 wp->w_ru_empty = empty_line;
10812 wp->w_ru_topline = wp->w_topline;
10813 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10814#ifdef FEAT_DIFF
10815 wp->w_ru_topfill = wp->w_topfill;
10816#endif
10817 }
10818}
10819#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010820
10821#if defined(FEAT_LINEBREAK) || defined(PROTO)
10822/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010823 * Return the width of the 'number' and 'relativenumber' column.
10824 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010825 * Otherwise it depends on 'numberwidth' and the line count.
10826 */
10827 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010828number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010829{
10830 int n;
10831 linenr_T lnum;
10832
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010833 if (wp->w_p_rnu && !wp->w_p_nu)
10834 /* cursor line shows "0" */
10835 lnum = wp->w_height;
10836 else
10837 /* cursor line shows absolute line number */
10838 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010839
Bram Moolenaar6b314672015-03-20 15:42:10 +010010840 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010841 return wp->w_nrwidth_width;
10842 wp->w_nrwidth_line_count = lnum;
10843
10844 n = 0;
10845 do
10846 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010847 lnum /= 10;
10848 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010849 } while (lnum > 0);
10850
10851 /* 'numberwidth' gives the minimal width plus one */
10852 if (n < wp->w_p_nuw - 1)
10853 n = wp->w_p_nuw - 1;
10854
10855 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010856 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010857 return n;
10858}
10859#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010860
10861/*
10862 * Return the current cursor column. This is the actual position on the
10863 * screen. First column is 0.
10864 */
10865 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010866screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010867{
10868 return screen_cur_col;
10869}
10870
10871/*
10872 * Return the current cursor row. This is the actual position on the screen.
10873 * First row is 0.
10874 */
10875 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010876screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010877{
10878 return screen_cur_row;
10879}