blob: 0458ed78015693589f4348fe008f7398312526ea [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. */
2208 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2209 }
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
2258#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002259static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261/*
2262 * Return TRUE when window "wp" has a column to draw signs in.
2263 */
2264 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002265draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 return (wp->w_buffer->b_signlist != NULL
2268# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002269 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270# endif
2271 );
2272}
2273#endif
2274
2275/*
2276 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2277 * as the filler character.
2278 */
2279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002280win_draw_end(
2281 win_T *wp,
2282 int c1,
2283 int c2,
2284 int row,
2285 int endrow,
2286 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2289 int n = 0;
2290# define FDC_OFF n
2291#else
2292# define FDC_OFF 0
2293#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002294#ifdef FEAT_FOLDING
2295 int fdc = compute_foldcolumn(wp, 0);
2296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298#ifdef FEAT_RIGHTLEFT
2299 if (wp->w_p_rl)
2300 {
2301 /* No check for cmdline window: should never be right-left. */
2302# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002303 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 if (n > 0)
2306 {
2307 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002308 if (n > W_WIDTH(wp))
2309 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2311 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2312 ' ', ' ', hl_attr(HLF_FC));
2313 }
2314# endif
2315# ifdef FEAT_SIGNS
2316 if (draw_signcolumn(wp))
2317 {
2318 int nn = n + 2;
2319
2320 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002321 if (nn > W_WIDTH(wp))
2322 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2324 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2325 ' ', ' ', hl_attr(HLF_SC));
2326 n = nn;
2327 }
2328# endif
2329 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2330 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2331 c2, c2, hl_attr(hl));
2332 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2333 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2334 c1, c2, hl_attr(hl));
2335 }
2336 else
2337#endif
2338 {
2339#ifdef FEAT_CMDWIN
2340 if (cmdwin_type != 0 && wp == curwin)
2341 {
2342 /* draw the cmdline character in the leftmost column */
2343 n = 1;
2344 if (n > wp->w_width)
2345 n = wp->w_width;
2346 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2347 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2348 cmdwin_type, ' ', hl_attr(HLF_AT));
2349 }
2350#endif
2351#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002352 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002354 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 /* draw the fold column at the left */
2357 if (nn > W_WIDTH(wp))
2358 nn = W_WIDTH(wp);
2359 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2360 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2361 ' ', ' ', hl_attr(HLF_FC));
2362 n = nn;
2363 }
2364#endif
2365#ifdef FEAT_SIGNS
2366 if (draw_signcolumn(wp))
2367 {
2368 int nn = n + 2;
2369
2370 /* draw the sign column after the fold column */
2371 if (nn > W_WIDTH(wp))
2372 nn = W_WIDTH(wp);
2373 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2374 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2375 ' ', ' ', hl_attr(HLF_SC));
2376 n = nn;
2377 }
2378#endif
2379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2381 c1, c2, hl_attr(hl));
2382 }
2383 set_empty_rows(wp, row);
2384}
2385
Bram Moolenaar1a384422010-07-14 19:53:30 +02002386#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002387static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002388
2389/*
2390 * Advance **color_cols and return TRUE when there are columns to draw.
2391 */
2392 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002393advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002394{
2395 while (**color_cols >= 0 && vcol > **color_cols)
2396 ++*color_cols;
2397 return (**color_cols >= 0);
2398}
2399#endif
2400
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401#ifdef FEAT_FOLDING
2402/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002403 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2404 * space is available for window "wp", minus "col".
2405 */
2406 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002407compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408{
2409 int fdc = wp->w_p_fdc;
2410 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2411 int wwidth = W_WIDTH(wp);
2412
2413 if (fdc > wwidth - (col + wmw))
2414 fdc = wwidth - (col + wmw);
2415 return fdc;
2416}
2417
2418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 * Display one folded line.
2420 */
2421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002422fold_line(
2423 win_T *wp,
2424 long fold_count,
2425 foldinfo_T *foldinfo,
2426 linenr_T lnum,
2427 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002429 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 pos_T *top, *bot;
2431 linenr_T lnume = lnum + fold_count - 1;
2432 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002433 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int col;
2436 int txtcol;
2437 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002438 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /* Build the fold line:
2441 * 1. Add the cmdwin_type for the command-line window
2442 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002443 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 * 4. Compose the text
2445 * 5. Add the text
2446 * 6. set highlighting for the Visual area an other text
2447 */
2448 col = 0;
2449
2450 /*
2451 * 1. Add the cmdwin_type for the command-line window
2452 * Ignores 'rightleft', this window is never right-left.
2453 */
2454#ifdef FEAT_CMDWIN
2455 if (cmdwin_type != 0 && wp == curwin)
2456 {
2457 ScreenLines[off] = cmdwin_type;
2458 ScreenAttrs[off] = hl_attr(HLF_AT);
2459#ifdef FEAT_MBYTE
2460 if (enc_utf8)
2461 ScreenLinesUC[off] = 0;
2462#endif
2463 ++col;
2464 }
2465#endif
2466
2467 /*
2468 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002469 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002471 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (fdc > 0)
2473 {
2474 fill_foldcolumn(buf, wp, TRUE, lnum);
2475#ifdef FEAT_RIGHTLEFT
2476 if (wp->w_p_rl)
2477 {
2478 int i;
2479
2480 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2481 hl_attr(HLF_FC));
2482 /* reverse the fold column */
2483 for (i = 0; i < fdc; ++i)
2484 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2485 }
2486 else
2487#endif
2488 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2489 col += fdc;
2490 }
2491
2492#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002493# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2494 for (ri = 0; ri < l; ++ri) \
2495 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2496 else \
2497 for (ri = 0; ri < l; ++ri) \
2498 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002500# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2501 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502#endif
2503
Bram Moolenaar64486672010-05-16 15:46:46 +02002504 /* Set all attributes of the 'number' or 'relativenumber' column and the
2505 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002506 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508#ifdef FEAT_SIGNS
2509 /* If signs are being displayed, add two spaces. */
2510 if (draw_signcolumn(wp))
2511 {
2512 len = W_WIDTH(wp) - col;
2513 if (len > 0)
2514 {
2515 if (len > 2)
2516 len = 2;
2517# ifdef FEAT_RIGHTLEFT
2518 if (wp->w_p_rl)
2519 /* the line number isn't reversed */
2520 copy_text_attr(off + W_WIDTH(wp) - len - col,
2521 (char_u *)" ", len, hl_attr(HLF_FL));
2522 else
2523# endif
2524 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2525 col += len;
2526 }
2527 }
2528#endif
2529
2530 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002531 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002533 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 len = W_WIDTH(wp) - col;
2536 if (len > 0)
2537 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002538 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002539 long num;
2540 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002541
2542 if (len > w + 1)
2543 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002544
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002545 if (wp->w_p_nu && !wp->w_p_rnu)
2546 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002547 num = (long)lnum;
2548 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002549 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002550 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002551 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002552 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002553 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002554 /* 'number' + 'relativenumber': cursor line shows absolute
2555 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002556 num = lnum;
2557 fmt = "%-*ld ";
2558 }
2559 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002560
Bram Moolenaar700e7342013-01-30 12:31:36 +01002561 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#ifdef FEAT_RIGHTLEFT
2563 if (wp->w_p_rl)
2564 /* the line number isn't reversed */
2565 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2566 hl_attr(HLF_FL));
2567 else
2568#endif
2569 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2570 col += len;
2571 }
2572 }
2573
2574 /*
2575 * 4. Compose the folded-line string with 'foldtext', if set.
2576 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002577 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579 txtcol = col; /* remember where text starts */
2580
2581 /*
2582 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2583 * Right-left text is put in columns 0 - number-col, normal text is put
2584 * in columns number-col - window-width.
2585 */
2586#ifdef FEAT_MBYTE
2587 if (has_mbyte)
2588 {
2589 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 int u8c, u8cc[MAX_MCO];
2591 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int idx;
2593 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002594 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595# ifdef FEAT_ARABIC
2596 int prev_c = 0; /* previous Arabic character */
2597 int prev_c1 = 0; /* first composing char for prev_c */
2598# endif
2599
2600# ifdef FEAT_RIGHTLEFT
2601 if (wp->w_p_rl)
2602 idx = off;
2603 else
2604# endif
2605 idx = off + col;
2606
2607 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2608 for (p = text; *p != NUL; )
2609 {
2610 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002611 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (col + cells > W_WIDTH(wp)
2613# ifdef FEAT_RIGHTLEFT
2614 - (wp->w_p_rl ? col : 0)
2615# endif
2616 )
2617 break;
2618 ScreenLines[idx] = *p;
2619 if (enc_utf8)
2620 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621 u8c = utfc_ptr2char(p, u8cc);
2622 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
2624 ScreenLinesUC[idx] = 0;
2625#ifdef FEAT_ARABIC
2626 prev_c = u8c;
2627#endif
2628 }
2629 else
2630 {
2631#ifdef FEAT_ARABIC
2632 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2633 {
2634 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002635 int pc, pc1, nc;
2636 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int firstbyte = *p;
2638
2639 /* The idea of what is the previous and next
2640 * character depends on 'rightleft'. */
2641 if (wp->w_p_rl)
2642 {
2643 pc = prev_c;
2644 pc1 = prev_c1;
2645 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 else
2649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002650 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002652 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654 prev_c = u8c;
2655
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 pc, pc1, nc);
2658 ScreenLines[idx] = firstbyte;
2659 }
2660 else
2661 prev_c = u8c;
2662#endif
2663 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002664#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (u8c >= 0x10000)
2666 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2667 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670 for (i = 0; i < Screen_mco; ++i)
2671 {
2672 ScreenLinesC[i][idx] = u8cc[i];
2673 if (u8cc[i] == 0)
2674 break;
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 if (cells > 1)
2678 ScreenLines[idx + 1] = 0;
2679 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002680 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2681 /* double-byte single width character */
2682 ScreenLines2[idx] = p[1];
2683 else if (cells > 1)
2684 /* double-width character */
2685 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 col += cells;
2687 idx += cells;
2688 p += c_len;
2689 }
2690 }
2691 else
2692#endif
2693 {
2694 len = (int)STRLEN(text);
2695 if (len > W_WIDTH(wp) - col)
2696 len = W_WIDTH(wp) - col;
2697 if (len > 0)
2698 {
2699#ifdef FEAT_RIGHTLEFT
2700 if (wp->w_p_rl)
2701 STRNCPY(current_ScreenLine, text, len);
2702 else
2703#endif
2704 STRNCPY(current_ScreenLine + col, text, len);
2705 col += len;
2706 }
2707 }
2708
2709 /* Fill the rest of the line with the fold filler */
2710#ifdef FEAT_RIGHTLEFT
2711 if (wp->w_p_rl)
2712 col -= txtcol;
2713#endif
2714 while (col < W_WIDTH(wp)
2715#ifdef FEAT_RIGHTLEFT
2716 - (wp->w_p_rl ? txtcol : 0)
2717#endif
2718 )
2719 {
2720#ifdef FEAT_MBYTE
2721 if (enc_utf8)
2722 {
2723 if (fill_fold >= 0x80)
2724 {
2725 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 else
2729 ScreenLinesUC[off + col] = 0;
2730 }
2731#endif
2732 ScreenLines[off + col++] = fill_fold;
2733 }
2734
2735 if (text != buf)
2736 vim_free(text);
2737
2738 /*
2739 * 6. set highlighting for the Visual area an other text.
2740 * If all folded lines are in the Visual area, highlight the line.
2741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2743 {
2744 if (ltoreq(curwin->w_cursor, VIsual))
2745 {
2746 /* Visual is after curwin->w_cursor */
2747 top = &curwin->w_cursor;
2748 bot = &VIsual;
2749 }
2750 else
2751 {
2752 /* Visual is before curwin->w_cursor */
2753 top = &VIsual;
2754 bot = &curwin->w_cursor;
2755 }
2756 if (lnum >= top->lnum
2757 && lnume <= bot->lnum
2758 && (VIsual_mode != 'v'
2759 || ((lnum > top->lnum
2760 || (lnum == top->lnum
2761 && top->col == 0))
2762 && (lnume < bot->lnum
2763 || (lnume == bot->lnum
2764 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002765 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
2767 if (VIsual_mode == Ctrl_V)
2768 {
2769 /* Visual block mode: highlight the chars part of the block */
2770 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2771 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002772 if (wp->w_old_cursor_lcol != MAXCOL
2773 && wp->w_old_cursor_lcol + txtcol
2774 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 len = wp->w_old_cursor_lcol;
2776 else
2777 len = W_WIDTH(wp) - txtcol;
2778 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002779 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 }
2782 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002785 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002790#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002791 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2792 if (wp->w_p_cc_cols)
2793 {
2794 int i = 0;
2795 int j = wp->w_p_cc_cols[i];
2796 int old_txtcol = txtcol;
2797
2798 while (j > -1)
2799 {
2800 txtcol += j;
2801 if (wp->w_p_wrap)
2802 txtcol -= wp->w_skipcol;
2803 else
2804 txtcol -= wp->w_leftcol;
2805 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2806 ScreenAttrs[off + txtcol] = hl_combine_attr(
2807 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2808 txtcol = old_txtcol;
2809 j = wp->w_p_cc_cols[++i];
2810 }
2811 }
2812
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002813 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002814 if (wp->w_p_cuc)
2815 {
2816 txtcol += wp->w_virtcol;
2817 if (wp->w_p_wrap)
2818 txtcol -= wp->w_skipcol;
2819 else
2820 txtcol -= wp->w_leftcol;
2821 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2822 ScreenAttrs[off + txtcol] = hl_combine_attr(
2823 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2824 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
2827 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2828 (int)W_WIDTH(wp), FALSE);
2829
2830 /*
2831 * Update w_cline_height and w_cline_folded if the cursor line was
2832 * updated (saves a call to plines() later).
2833 */
2834 if (wp == curwin
2835 && lnum <= curwin->w_cursor.lnum
2836 && lnume >= curwin->w_cursor.lnum)
2837 {
2838 curwin->w_cline_row = row;
2839 curwin->w_cline_height = 1;
2840 curwin->w_cline_folded = TRUE;
2841 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2842 }
2843}
2844
2845/*
2846 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2847 */
2848 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002849copy_text_attr(
2850 int off,
2851 char_u *buf,
2852 int len,
2853 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002855 int i;
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 mch_memmove(ScreenLines + off, buf, (size_t)len);
2858# ifdef FEAT_MBYTE
2859 if (enc_utf8)
2860 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2861# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002862 for (i = 0; i < len; ++i)
2863 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864}
2865
2866/*
2867 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002868 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 */
2870 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002871fill_foldcolumn(
2872 char_u *p,
2873 win_T *wp,
2874 int closed, /* TRUE of FALSE */
2875 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
2877 int i = 0;
2878 int level;
2879 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002880 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002881 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002884 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 level = win_foldinfo.fi_level;
2887 if (level > 0)
2888 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002889 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002890 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 /* If the column is too narrow, we start at the lowest level that
2893 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002894 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (first_level < 1)
2896 first_level = 1;
2897
Bram Moolenaar1c934292015-01-27 16:39:29 +01002898 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
2900 if (win_foldinfo.fi_lnum == lnum
2901 && first_level + i >= win_foldinfo.fi_low_level)
2902 p[i] = '-';
2903 else if (first_level == 1)
2904 p[i] = '|';
2905 else if (first_level + i <= 9)
2906 p[i] = '0' + first_level + i;
2907 else
2908 p[i] = '>';
2909 if (first_level + i == level)
2910 break;
2911 }
2912 }
2913 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002914 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915}
2916#endif /* FEAT_FOLDING */
2917
2918/*
2919 * Display line "lnum" of window 'wp' on the screen.
2920 * Start at row "startrow", stop when "endrow" is reached.
2921 * wp->w_virtcol needs to be valid.
2922 *
2923 * Return the number of last row the line occupies.
2924 */
2925 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002926win_line(
2927 win_T *wp,
2928 linenr_T lnum,
2929 int startrow,
2930 int endrow,
2931 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 int col; /* visual column on screen */
2934 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2935 int c = 0; /* init for GCC */
2936 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002937#ifdef FEAT_LINEBREAK
2938 long vcol_sbr = -1; /* virtual column after showbreak */
2939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 long vcol_prev = -1; /* "vcol" of previous character */
2941 char_u *line; /* current line */
2942 char_u *ptr; /* current position in "line" */
2943 int row; /* row in the window, excl w_winrow */
2944 int screen_row; /* row on the screen, incl w_winrow */
2945
2946 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2947 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002948 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002949 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int c_extra = NUL; /* extra chars, all the same */
2951 int extra_attr = 0; /* attributes when n_extra != 0 */
2952 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2953 displaying lcs_eol at end-of-line */
2954 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2955 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2956
2957 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2958 int saved_n_extra = 0;
2959 char_u *saved_p_extra = NULL;
2960 int saved_c_extra = 0;
2961 int saved_char_attr = 0;
2962
2963 int n_attr = 0; /* chars with special attr */
2964 int saved_attr2 = 0; /* char_attr saved for n_attr */
2965 int n_attr3 = 0; /* chars with overruling special attr */
2966 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2967
2968 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2969
2970 int fromcol, tocol; /* start/end of inverting */
2971 int fromcol_prev = -2; /* start of inverting after cursor */
2972 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002974 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 pos_T pos;
2976 long v;
2977
2978 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002979 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2981 in this line */
2982 int attr = 0; /* attributes for area highlighting */
2983 int area_attr = 0; /* attributes desired by highlighting */
2984 int search_attr = 0; /* attributes desired by 'hlsearch' */
2985#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002986 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 int syntax_attr = 0; /* attributes desired by syntax */
2988 int has_syntax = FALSE; /* this buffer has syntax highl. */
2989 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002990 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002991 int draw_color_col = FALSE; /* highlight colorcolumn */
2992 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002993#endif
2994#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002995 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002996# define SPWORDLEN 150
2997 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002998 int nextlinecol = 0; /* column where nextline[] starts */
2999 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003000 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003001 int spell_attr = 0; /* attributes desired by spelling */
3002 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003003 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3004 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003005 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003006 static int cap_col = -1; /* column to check for Cap word */
3007 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003008 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#endif
3010 int extra_check; /* has syntax or linebreak */
3011#ifdef FEAT_MBYTE
3012 int multi_attr = 0; /* attributes desired by multibyte */
3013 int mb_l = 1; /* multi-byte byte length */
3014 int mb_c = 0; /* decoded multi-byte character */
3015 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003016 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#endif
3018#ifdef FEAT_DIFF
3019 int filler_lines; /* nr of filler lines to be drawn */
3020 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003021 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 int change_start = MAXCOL; /* first col of changed area */
3023 int change_end = -1; /* last col of changed area */
3024#endif
3025 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3026#ifdef FEAT_LINEBREAK
3027 int need_showbreak = FALSE;
3028#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003029#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3030 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003032 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033#endif
3034#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003035 matchitem_T *cur; /* points to the match list */
3036 match_T *shl; /* points to search_hl or a match */
3037 int shl_flag; /* flag to indicate whether search_hl
3038 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003039 int pos_inprogress; /* marks that position match search is
3040 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003041 int prevcol_hl_flag; /* flag to indicate whether prevcol
3042 equals startcol of search_hl or one
3043 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#endif
3045#ifdef FEAT_ARABIC
3046 int prev_c = 0; /* previous Arabic character */
3047 int prev_c1 = 0; /* first composing char for prev_c */
3048#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003049#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003050 int did_line_attr = 0;
3051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 /* draw_state: items that are drawn in sequence: */
3054#define WL_START 0 /* nothing done yet */
3055#ifdef FEAT_CMDWIN
3056# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3057#else
3058# define WL_CMDLINE WL_START
3059#endif
3060#ifdef FEAT_FOLDING
3061# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3062#else
3063# define WL_FOLD WL_CMDLINE
3064#endif
3065#ifdef FEAT_SIGNS
3066# define WL_SIGN WL_FOLD + 1 /* column for signs */
3067#else
3068# define WL_SIGN WL_FOLD /* column for signs */
3069#endif
3070#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003071#ifdef FEAT_LINEBREAK
3072# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003074# define WL_BRI WL_NR
3075#endif
3076#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3077# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3078#else
3079# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#endif
3081#define WL_LINE WL_SBR + 1 /* text in the line */
3082 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003083#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 int feedback_col = 0;
3085 int feedback_old_attr = -1;
3086#endif
3087
Bram Moolenaar860cae12010-06-05 23:22:07 +02003088#ifdef FEAT_CONCEAL
3089 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003090 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003091 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003092 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003093 int is_concealing = FALSE;
3094 int boguscols = 0; /* nonexistent columns added to force
3095 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003096 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003097 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003098 int match_conc = 0; /* cchar for match functions */
3099 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003100 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003101# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003102# define FIX_FOR_BOGUSCOLS \
3103 { \
3104 n_extra += vcol_off; \
3105 vcol -= vcol_off; \
3106 vcol_off = 0; \
3107 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003108 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003109 boguscols = 0; \
3110 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003111#else
3112# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 if (startrow > endrow) /* past the end already! */
3116 return startrow;
3117
3118 row = startrow;
3119 screen_row = row + W_WINROW(wp);
3120
3121 /*
3122 * To speed up the loop below, set extra_check when there is linebreak,
3123 * trailing white space and/or syntax processing to be done.
3124 */
3125#ifdef FEAT_LINEBREAK
3126 extra_check = wp->w_p_lbr;
3127#else
3128 extra_check = 0;
3129#endif
3130#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003131 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 /* Prepare for syntax highlighting in this line. When there is an
3134 * error, stop syntax highlighting. */
3135 save_did_emsg = did_emsg;
3136 did_emsg = FALSE;
3137 syntax_start(wp, lnum);
3138 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003139 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 else
3141 {
3142 did_emsg = save_did_emsg;
3143 has_syntax = TRUE;
3144 extra_check = TRUE;
3145 }
3146 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003147
3148 /* Check for columns to display for 'colorcolumn'. */
3149 color_cols = wp->w_p_cc_cols;
3150 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003151 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003152#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003153
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003154#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003155 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156 && *wp->w_s->b_p_spl != NUL
3157 && wp->w_s->b_langp.ga_len > 0
3158 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003159 {
3160 /* Prepare for spell checking. */
3161 has_spell = TRUE;
3162 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003163
3164 /* Get the start of the next line, so that words that wrap to the next
3165 * line are found too: "et<line-break>al.".
3166 * Trick: skip a few chars for C/shell/Vim comments */
3167 nextline[SPWORDLEN] = NUL;
3168 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3169 {
3170 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3171 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3172 }
3173
3174 /* When a word wrapped from the previous line the start of the current
3175 * line is valid. */
3176 if (lnum == checked_lnum)
3177 cur_checked_col = checked_col;
3178 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003179
3180 /* When there was a sentence end in the previous line may require a
3181 * word starting with capital in this line. In line 1 always check
3182 * the first word. */
3183 if (lnum != capcol_lnum)
3184 cap_col = -1;
3185 if (lnum == 1)
3186 cap_col = 0;
3187 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189#endif
3190
3191 /*
3192 * handle visual active in this window
3193 */
3194 fromcol = -10;
3195 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3197 {
3198 /* Visual is after curwin->w_cursor */
3199 if (ltoreq(curwin->w_cursor, VIsual))
3200 {
3201 top = &curwin->w_cursor;
3202 bot = &VIsual;
3203 }
3204 else /* Visual is before curwin->w_cursor */
3205 {
3206 top = &VIsual;
3207 bot = &curwin->w_cursor;
3208 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003209 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (VIsual_mode == Ctrl_V) /* block mode */
3211 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003212 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 fromcol = wp->w_old_cursor_fcol;
3215 tocol = wp->w_old_cursor_lcol;
3216 }
3217 }
3218 else /* non-block mode */
3219 {
3220 if (lnum > top->lnum && lnum <= bot->lnum)
3221 fromcol = 0;
3222 else if (lnum == top->lnum)
3223 {
3224 if (VIsual_mode == 'V') /* linewise */
3225 fromcol = 0;
3226 else
3227 {
3228 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3229 if (gchar_pos(top) == NUL)
3230 tocol = fromcol + 1;
3231 }
3232 }
3233 if (VIsual_mode != 'V' && lnum == bot->lnum)
3234 {
3235 if (*p_sel == 'e' && bot->col == 0
3236#ifdef FEAT_VIRTUALEDIT
3237 && bot->coladd == 0
3238#endif
3239 )
3240 {
3241 fromcol = -10;
3242 tocol = MAXCOL;
3243 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003244 else if (bot->col == MAXCOL)
3245 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 else
3247 {
3248 pos = *bot;
3249 if (*p_sel == 'e')
3250 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3251 else
3252 {
3253 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3254 ++tocol;
3255 }
3256 }
3257 }
3258 }
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 /* Check if the character under the cursor should not be inverted */
3261 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003262#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 )
3266 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268 /* if inverting in this line set area_highlighting */
3269 if (fromcol >= 0)
3270 {
3271 area_highlighting = TRUE;
3272 attr = hl_attr(HLF_V);
3273#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003274 if ((clip_star.available && !clip_star.owned
3275 && clip_isautosel_star())
3276 || (clip_plus.available && !clip_plus.owned
3277 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 attr = hl_attr(HLF_VNC);
3279#endif
3280 }
3281 }
3282
3283 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003284 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003286 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 && wp == curwin
3288 && lnum >= curwin->w_cursor.lnum
3289 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3290 {
3291 if (lnum == curwin->w_cursor.lnum)
3292 getvcol(curwin, &(curwin->w_cursor),
3293 (colnr_T *)&fromcol, NULL, NULL);
3294 else
3295 fromcol = 0;
3296 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3297 {
3298 pos.lnum = lnum;
3299 pos.col = search_match_endcol;
3300 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3301 }
3302 else
3303 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003304 /* do at least one character; happens when past end of line */
3305 if (fromcol == tocol)
3306 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 area_highlighting = TRUE;
3308 attr = hl_attr(HLF_I);
3309 }
3310
3311#ifdef FEAT_DIFF
3312 filler_lines = diff_check(wp, lnum);
3313 if (filler_lines < 0)
3314 {
3315 if (filler_lines == -1)
3316 {
3317 if (diff_find_change(wp, lnum, &change_start, &change_end))
3318 diff_hlf = HLF_ADD; /* added line */
3319 else if (change_start == 0)
3320 diff_hlf = HLF_TXD; /* changed text */
3321 else
3322 diff_hlf = HLF_CHD; /* changed line */
3323 }
3324 else
3325 diff_hlf = HLF_ADD; /* added line */
3326 filler_lines = 0;
3327 area_highlighting = TRUE;
3328 }
3329 if (lnum == wp->w_topline)
3330 filler_lines = wp->w_topfill;
3331 filler_todo = filler_lines;
3332#endif
3333
3334#ifdef LINE_ATTR
3335# ifdef FEAT_SIGNS
3336 /* If this line has a sign with line highlighting set line_attr. */
3337 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3338 if (v != 0)
3339 line_attr = sign_get_attr((int)v, TRUE);
3340# endif
3341# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3342 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003343 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 line_attr = hl_attr(HLF_L);
3345# endif
3346 if (line_attr != 0)
3347 area_highlighting = TRUE;
3348#endif
3349
3350 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3351 ptr = line;
3352
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003353#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003354 if (has_spell)
3355 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003356 /* For checking first word with a capital skip white space. */
3357 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003358 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003359
Bram Moolenaar30abd282005-06-22 22:35:10 +00003360 /* To be able to spell-check over line boundaries copy the end of the
3361 * current line into nextline[]. Above the start of the next line was
3362 * copied to nextline[SPWORDLEN]. */
3363 if (nextline[SPWORDLEN] == NUL)
3364 {
3365 /* No next line or it is empty. */
3366 nextlinecol = MAXCOL;
3367 nextline_idx = 0;
3368 }
3369 else
3370 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003371 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003372 if (v < SPWORDLEN)
3373 {
3374 /* Short line, use it completely and append the start of the
3375 * next line. */
3376 nextlinecol = 0;
3377 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003378 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003379 nextline_idx = v + 1;
3380 }
3381 else
3382 {
3383 /* Long line, use only the last SPWORDLEN bytes. */
3384 nextlinecol = v - SPWORDLEN;
3385 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3386 nextline_idx = SPWORDLEN + 1;
3387 }
3388 }
3389 }
3390#endif
3391
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003392 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003394 if (lcs_space || lcs_trail)
3395 extra_check = TRUE;
3396 /* find start of trailing whitespace */
3397 if (lcs_trail)
3398 {
3399 trailcol = (colnr_T)STRLEN(ptr);
3400 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3401 --trailcol;
3402 trailcol += (colnr_T) (ptr - line);
3403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 }
3405
3406 /*
3407 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3408 * first character to be displayed.
3409 */
3410 if (wp->w_p_wrap)
3411 v = wp->w_skipcol;
3412 else
3413 v = wp->w_leftcol;
3414 if (v > 0)
3415 {
3416#ifdef FEAT_MBYTE
3417 char_u *prev_ptr = ptr;
3418#endif
3419 while (vcol < v && *ptr != NUL)
3420 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003421 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 vcol += c;
3423#ifdef FEAT_MBYTE
3424 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003426 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003429 /* When:
3430 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003431 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003432 * - 'virtualedit' is set, or
3433 * - the visual mode is active,
3434 * the end of the line may be before the start of the displayed part.
3435 */
3436 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003437#ifdef FEAT_SYN_HL
3438 wp->w_p_cuc || draw_color_col ||
3439#endif
3440#ifdef FEAT_VIRTUALEDIT
3441 virtual_active() ||
3442#endif
3443 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 /* Handle a character that's not completely on the screen: Put ptr at
3449 * that character but skip the first few screen characters. */
3450 if (vcol > v)
3451 {
3452 vcol -= c;
3453#ifdef FEAT_MBYTE
3454 ptr = prev_ptr;
3455#else
3456 --ptr;
3457#endif
3458 n_skip = v - vcol;
3459 }
3460
3461 /*
3462 * Adjust for when the inverted text is before the screen,
3463 * and when the start of the inverted text is before the screen.
3464 */
3465 if (tocol <= vcol)
3466 fromcol = 0;
3467 else if (fromcol >= 0 && fromcol < vcol)
3468 fromcol = vcol;
3469
3470#ifdef FEAT_LINEBREAK
3471 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3472 if (wp->w_p_wrap)
3473 need_showbreak = TRUE;
3474#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003475#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003476 /* When spell checking a word we need to figure out the start of the
3477 * word and if it's badly spelled or not. */
3478 if (has_spell)
3479 {
3480 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003481 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003482 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003483
3484 pos = wp->w_cursor;
3485 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003486 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003487 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003488
3489 /* spell_move_to() may call ml_get() and make "line" invalid */
3490 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3491 ptr = line + linecol;
3492
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003493 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003494 {
3495 /* no bad word found at line start, don't check until end of a
3496 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003497 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003498 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003499 }
3500 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003501 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003502 /* bad word found, use attributes until end of word */
3503 word_end = wp->w_cursor.col + len + 1;
3504
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003505 /* Turn index into actual attributes. */
3506 if (spell_hlf != HLF_COUNT)
3507 spell_attr = highlight_attr[spell_hlf];
3508 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003509 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003510
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003511# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003512 /* Need to restart syntax highlighting for this line. */
3513 if (has_syntax)
3514 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003515# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003516 }
3517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
3520 /*
3521 * Correct highlighting for cursor that can't be disabled.
3522 * Avoids having to check this for each character.
3523 */
3524 if (fromcol >= 0)
3525 {
3526 if (noinvcur)
3527 {
3528 if ((colnr_T)fromcol == wp->w_virtcol)
3529 {
3530 /* highlighting starts at cursor, let it start just after the
3531 * cursor */
3532 fromcol_prev = fromcol;
3533 fromcol = -1;
3534 }
3535 else if ((colnr_T)fromcol < wp->w_virtcol)
3536 /* restart highlighting after the cursor */
3537 fromcol_prev = wp->w_virtcol;
3538 }
3539 if (fromcol >= tocol)
3540 fromcol = -1;
3541 }
3542
3543#ifdef FEAT_SEARCH_EXTRA
3544 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003545 * Handle highlighting the last used search pattern and matches.
3546 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003548 cur = wp->w_match_head;
3549 shl_flag = FALSE;
3550 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003552 if (shl_flag == FALSE)
3553 {
3554 shl = &search_hl;
3555 shl_flag = TRUE;
3556 }
3557 else
3558 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003559 shl->startcol = MAXCOL;
3560 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003562 v = (long)(ptr - line);
3563 if (cur != NULL)
3564 cur->pos.cur = 0;
3565 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3566
3567 /* Need to get the line again, a multi-line regexp may have made it
3568 * invalid. */
3569 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3570 ptr = line + v;
3571
3572 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003574 if (shl->lnum == lnum)
3575 shl->startcol = shl->rm.startpos[0].col;
3576 else
3577 shl->startcol = 0;
3578 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3579 - shl->rm.startpos[0].lnum)
3580 shl->endcol = shl->rm.endpos[0].col;
3581 else
3582 shl->endcol = MAXCOL;
3583 /* Highlight one character for an empty match. */
3584 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003587 if (has_mbyte && line[shl->endcol] != NUL)
3588 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003591 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003593 if ((long)shl->startcol < v) /* match at leftcol */
3594 {
3595 shl->attr_cur = shl->attr;
3596 search_attr = shl->attr;
3597 }
3598 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003600 if (shl != &search_hl && cur != NULL)
3601 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603#endif
3604
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003605#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003606 /* Cursor line highlighting for 'cursorline' in the current window. Not
3607 * when Visual mode is active, because it's not clear what is selected
3608 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003609 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003610 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003611 {
3612 line_attr = hl_attr(HLF_CUL);
3613 area_highlighting = TRUE;
3614 }
3615#endif
3616
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003617 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 col = 0;
3619#ifdef FEAT_RIGHTLEFT
3620 if (wp->w_p_rl)
3621 {
3622 /* Rightleft window: process the text in the normal direction, but put
3623 * it in current_ScreenLine[] from right to left. Start at the
3624 * rightmost column of the window. */
3625 col = W_WIDTH(wp) - 1;
3626 off += col;
3627 }
3628#endif
3629
3630 /*
3631 * Repeat for the whole displayed line.
3632 */
3633 for (;;)
3634 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003635#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003636 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 /* Skip this quickly when working on the text. */
3639 if (draw_state != WL_LINE)
3640 {
3641#ifdef FEAT_CMDWIN
3642 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3643 {
3644 draw_state = WL_CMDLINE;
3645 if (cmdwin_type != 0 && wp == curwin)
3646 {
3647 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003649 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 char_attr = hl_attr(HLF_AT);
3651 }
3652 }
3653#endif
3654
3655#ifdef FEAT_FOLDING
3656 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3657 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003658 int fdc = compute_foldcolumn(wp, 0);
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003661 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 /* Draw the 'foldcolumn'. */
3664 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003665 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003667 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 c_extra = NUL;
3669 char_attr = hl_attr(HLF_FC);
3670 }
3671 }
3672#endif
3673
3674#ifdef FEAT_SIGNS
3675 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3676 {
3677 draw_state = WL_SIGN;
3678 /* Show the sign column when there are any signs in this
3679 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003680 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003682 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003684 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685# endif
3686
3687 /* Draw two cells with the sign value or blank. */
3688 c_extra = ' ';
3689 char_attr = hl_attr(HLF_SC);
3690 n_extra = 2;
3691
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003692 if (row == startrow
3693#ifdef FEAT_DIFF
3694 + filler_lines && filler_todo <= 0
3695#endif
3696 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3699 SIGN_TEXT);
3700# ifdef FEAT_SIGN_ICONS
3701 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3702 SIGN_ICON);
3703 if (gui.in_use && icon_sign != 0)
3704 {
3705 /* Use the image in this position. */
3706 c_extra = SIGN_BYTE;
3707# ifdef FEAT_NETBEANS_INTG
3708 if (buf_signcount(wp->w_buffer, lnum) > 1)
3709 c_extra = MULTISIGN_BYTE;
3710# endif
3711 char_attr = icon_sign;
3712 }
3713 else
3714# endif
3715 if (text_sign != 0)
3716 {
3717 p_extra = sign_get_text(text_sign);
3718 if (p_extra != NULL)
3719 {
3720 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003721 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 char_attr = sign_get_attr(text_sign, FALSE);
3724 }
3725 }
3726 }
3727 }
3728#endif
3729
3730 if (draw_state == WL_NR - 1 && n_extra == 0)
3731 {
3732 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003733 /* Display the absolute or relative line number. After the
3734 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3735 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 && (row == startrow
3737#ifdef FEAT_DIFF
3738 + filler_lines
3739#endif
3740 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3741 {
3742 /* Draw the line number (empty space after wrapping). */
3743 if (row == startrow
3744#ifdef FEAT_DIFF
3745 + filler_lines
3746#endif
3747 )
3748 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003749 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003750 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003751
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003752 if (wp->w_p_nu && !wp->w_p_rnu)
3753 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003754 num = (long)lnum;
3755 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003756 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003757 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003758 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003759 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003760 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003761 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003762 num = lnum;
3763 fmt = "%-*ld ";
3764 }
3765 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003766
Bram Moolenaar700e7342013-01-30 12:31:36 +01003767 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003768 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (wp->w_skipcol > 0)
3770 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3771 *p_extra = '-';
3772#ifdef FEAT_RIGHTLEFT
3773 if (wp->w_p_rl) /* reverse line numbers */
3774 rl_mirror(extra);
3775#endif
3776 p_extra = extra;
3777 c_extra = NUL;
3778 }
3779 else
3780 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003781 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003783#ifdef FEAT_SYN_HL
3784 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003785 * the current line differently.
3786 * TODO: Can we use CursorLine instead of CursorLineNr
3787 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003788 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003789 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003790 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 }
3794
Bram Moolenaar597a4222014-06-25 14:39:50 +02003795#ifdef FEAT_LINEBREAK
3796 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3797 && n_extra == 0 && *p_sbr != NUL)
3798 /* draw indent after showbreak value */
3799 draw_state = WL_BRI;
3800 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3801 /* After the showbreak, draw the breakindent */
3802 draw_state = WL_BRI - 1;
3803
3804 /* draw 'breakindent': indent wrapped text accordingly */
3805 if (draw_state == WL_BRI - 1 && n_extra == 0)
3806 {
3807 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003808 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003809# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003810 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003811# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003812 )
3813 {
3814 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003815# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003816 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003817 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003818 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003819# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003820 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3821 char_attr = hl_combine_attr(char_attr,
3822 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003823# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003824 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003825# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003826 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003827 c_extra = ' ';
3828 n_extra = get_breakindent_win(wp,
3829 ml_get_buf(wp->w_buffer, lnum, FALSE));
3830 /* Correct end of highlighted area for 'breakindent',
3831 * required when 'linebreak' is also set. */
3832 if (tocol == vcol)
3833 tocol += n_extra;
3834 }
3835 }
3836#endif
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3839 if (draw_state == WL_SBR - 1 && n_extra == 0)
3840 {
3841 draw_state = WL_SBR;
3842# ifdef FEAT_DIFF
3843 if (filler_todo > 0)
3844 {
3845 /* Draw "deleted" diff line(s). */
3846 if (char2cells(fill_diff) > 1)
3847 c_extra = '-';
3848 else
3849 c_extra = fill_diff;
3850# ifdef FEAT_RIGHTLEFT
3851 if (wp->w_p_rl)
3852 n_extra = col + 1;
3853 else
3854# endif
3855 n_extra = W_WIDTH(wp) - col;
3856 char_attr = hl_attr(HLF_DED);
3857 }
3858# endif
3859# ifdef FEAT_LINEBREAK
3860 if (*p_sbr != NUL && need_showbreak)
3861 {
3862 /* Draw 'showbreak' at the start of each broken line. */
3863 p_extra = p_sbr;
3864 c_extra = NUL;
3865 n_extra = (int)STRLEN(p_sbr);
3866 char_attr = hl_attr(HLF_AT);
3867 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003868 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 /* Correct end of highlighted area for 'showbreak',
3870 * required when 'linebreak' is also set. */
3871 if (tocol == vcol)
3872 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003873#ifdef FEAT_SYN_HL
3874 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003875 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003876 char_attr = hl_combine_attr(char_attr,
3877 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880# endif
3881 }
3882#endif
3883
3884 if (draw_state == WL_LINE - 1 && n_extra == 0)
3885 {
3886 draw_state = WL_LINE;
3887 if (saved_n_extra)
3888 {
3889 /* Continue item from end of wrapped line. */
3890 n_extra = saved_n_extra;
3891 c_extra = saved_c_extra;
3892 p_extra = saved_p_extra;
3893 char_attr = saved_char_attr;
3894 }
3895 else
3896 char_attr = 0;
3897 }
3898 }
3899
3900 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003901 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#ifdef FEAT_DIFF
3904 && filler_todo <= 0
3905#endif
3906 )
3907 {
3908 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3909 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910 /* Pretend we have finished updating the window. Except when
3911 * 'cursorcolumn' is set. */
3912#ifdef FEAT_SYN_HL
3913 if (wp->w_p_cuc)
3914 row = wp->w_cline_row + wp->w_cline_height;
3915 else
3916#endif
3917 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 break;
3919 }
3920
3921 if (draw_state == WL_LINE && area_highlighting)
3922 {
3923 /* handle Visual or match highlighting in this line */
3924 if (vcol == fromcol
3925#ifdef FEAT_MBYTE
3926 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3927 && (*mb_ptr2cells)(ptr) > 1)
3928#endif
3929 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003930 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 && vcol < tocol))
3932 area_attr = attr; /* start highlighting */
3933 else if (area_attr != 0
3934 && (vcol == tocol
3935 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938#ifdef FEAT_SEARCH_EXTRA
3939 if (!n_extra)
3940 {
3941 /*
3942 * Check for start/end of search pattern match.
3943 * After end, check for start/end of next match.
3944 * When another match, have to check for start again.
3945 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003946 * Do this for 'search_hl' and the match list (ordered by
3947 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003949 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003950 cur = wp->w_match_head;
3951 shl_flag = FALSE;
3952 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003954 if (shl_flag == FALSE
3955 && ((cur != NULL
3956 && cur->priority > SEARCH_HL_PRIORITY)
3957 || cur == NULL))
3958 {
3959 shl = &search_hl;
3960 shl_flag = TRUE;
3961 }
3962 else
3963 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003964 if (cur != NULL)
3965 cur->pos.cur = 0;
3966 pos_inprogress = TRUE;
3967 while (shl->rm.regprog != NULL
3968 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003970 if (shl->startcol != MAXCOL
3971 && v >= (long)shl->startcol
3972 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003974#ifdef FEAT_MBYTE
3975 int tmp_col = v + MB_PTR2LEN(ptr);
3976
3977 if (shl->endcol < tmp_col)
3978 shl->endcol = tmp_col;
3979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003981#ifdef FEAT_CONCEAL
3982 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3983 == cur->hlg_id)
3984 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003985 has_match_conc =
3986 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003987 match_conc = cur->conceal_char;
3988 }
3989 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003990 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003993 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
3995 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003996#ifdef FEAT_CONCEAL
3997 prev_syntax_id = 0;
3998#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003999 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
4000 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004001 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002
4003 /* Need to get the line again, a multi-line regexp
4004 * may have made it invalid. */
4005 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4006 ptr = line + v;
4007
4008 if (shl->lnum == lnum)
4009 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004010 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004012 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004014 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004016 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 /* highlight empty match, try again after
4019 * it */
4020#ifdef FEAT_MBYTE
4021 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004022 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 else
4025#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004026 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029 /* Loop to check if the match starts at the
4030 * current position */
4031 continue;
4032 }
4033 }
4034 break;
4035 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004036 if (shl != &search_hl && cur != NULL)
4037 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004039
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004040 /* Use attributes from match with highest priority among
4041 * 'search_hl' and the match list. */
4042 search_attr = search_hl.attr_cur;
4043 cur = wp->w_match_head;
4044 shl_flag = FALSE;
4045 while (cur != NULL || shl_flag == FALSE)
4046 {
4047 if (shl_flag == FALSE
4048 && ((cur != NULL
4049 && cur->priority > SEARCH_HL_PRIORITY)
4050 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004051 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004052 shl = &search_hl;
4053 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004054 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004055 else
4056 shl = &cur->hl;
4057 if (shl->attr_cur != 0)
4058 search_attr = shl->attr_cur;
4059 if (shl != &search_hl && cur != NULL)
4060 cur = cur->next;
4061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063#endif
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004066 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004068 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4069 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004071 if (diff_hlf == HLF_TXD && ptr - line > change_end
4072 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004074 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004075 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4076 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004079
4080 /* Decide which of the highlight attributes to use. */
4081 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004082#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004083 if (area_attr != 0)
4084 char_attr = hl_combine_attr(line_attr, area_attr);
4085 else if (search_attr != 0)
4086 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004087 /* Use line_attr when not in the Visual or 'incsearch' area
4088 * (area_attr may be 0 when "noinvcur" is set). */
4089 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004090 || vcol < fromcol || vcol_prev < fromcol_prev
4091 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004092 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004093#else
4094 if (area_attr != 0)
4095 char_attr = area_attr;
4096 else if (search_attr != 0)
4097 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004098#endif
4099 else
4100 {
4101 attr_pri = FALSE;
4102#ifdef FEAT_SYN_HL
4103 if (has_syntax)
4104 char_attr = syntax_attr;
4105 else
4106#endif
4107 char_attr = 0;
4108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110
4111 /*
4112 * Get the next character to put on the screen.
4113 */
4114 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004115 * The "p_extra" points to the extra stuff that is inserted to
4116 * represent special characters (non-printable stuff) and other
4117 * things. When all characters are the same, c_extra is used.
4118 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4119 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4121 */
4122 if (n_extra > 0)
4123 {
4124 if (c_extra != NUL)
4125 {
4126 c = c_extra;
4127#ifdef FEAT_MBYTE
4128 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4129 if (enc_utf8 && (*mb_char2len)(c) > 1)
4130 {
4131 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004132 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004133 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 else
4136 mb_utf8 = FALSE;
4137#endif
4138 }
4139 else
4140 {
4141 c = *p_extra;
4142#ifdef FEAT_MBYTE
4143 if (has_mbyte)
4144 {
4145 mb_c = c;
4146 if (enc_utf8)
4147 {
4148 /* If the UTF-8 character is more than one byte:
4149 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004150 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 mb_utf8 = FALSE;
4152 if (mb_l > n_extra)
4153 mb_l = 1;
4154 else if (mb_l > 1)
4155 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004156 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004158 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160 }
4161 else
4162 {
4163 /* if this is a DBCS character, put it in "mb_c" */
4164 mb_l = MB_BYTE2LEN(c);
4165 if (mb_l >= n_extra)
4166 mb_l = 1;
4167 else if (mb_l > 1)
4168 mb_c = (c << 8) + p_extra[1];
4169 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004170 if (mb_l == 0) /* at the NUL at end-of-line */
4171 mb_l = 1;
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 /* If a double-width char doesn't fit display a '>' in the
4174 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004175 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176# ifdef FEAT_RIGHTLEFT
4177 wp->w_p_rl ? (col <= 0) :
4178# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004179 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 && (*mb_char2cells)(mb_c) == 2)
4181 {
4182 c = '>';
4183 mb_c = c;
4184 mb_l = 1;
4185 mb_utf8 = FALSE;
4186 multi_attr = hl_attr(HLF_AT);
4187 /* put the pointer back to output the double-width
4188 * character at the start of the next line. */
4189 ++n_extra;
4190 --p_extra;
4191 }
4192 else
4193 {
4194 n_extra -= mb_l - 1;
4195 p_extra += mb_l - 1;
4196 }
4197 }
4198#endif
4199 ++p_extra;
4200 }
4201 --n_extra;
4202 }
4203 else
4204 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004205 if (p_extra_free != NULL)
4206 {
4207 vim_free(p_extra_free);
4208 p_extra_free = NULL;
4209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /*
4211 * Get a character from the line itself.
4212 */
4213 c = *ptr;
4214#ifdef FEAT_MBYTE
4215 if (has_mbyte)
4216 {
4217 mb_c = c;
4218 if (enc_utf8)
4219 {
4220 /* If the UTF-8 character is more than one byte: Decode it
4221 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004222 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 mb_utf8 = FALSE;
4224 if (mb_l > 1)
4225 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004226 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 /* Overlong encoded ASCII or ASCII with composing char
4228 * is displayed normally, except a NUL. */
4229 if (mb_c < 0x80)
4230 c = mb_c;
4231 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004232
4233 /* At start of the line we can have a composing char.
4234 * Draw it as a space with a composing char. */
4235 if (utf_iscomposing(mb_c))
4236 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004237 int i;
4238
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 for (i = Screen_mco - 1; i > 0; --i)
4240 u8cc[i] = u8cc[i - 1];
4241 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004242 mb_c = ' ';
4243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245
4246 if ((mb_l == 1 && c >= 0x80)
4247 || (mb_l >= 1 && mb_c == 0)
4248 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004249# ifdef UNICODE16
4250 || mb_c >= 0x10000
4251# endif
4252 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 /*
4255 * Illegal UTF-8 byte: display as <xx>.
4256 * Non-BMP character : display as ? or fullwidth ?.
4257 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004258# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
4262 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004263# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (wp->w_p_rl) /* reverse */
4265 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004268# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 else if (utf_char2cells(mb_c) != 2)
4270 STRCPY(extra, "?");
4271 else
4272 /* 0xff1f in UTF-8: full-width '?' */
4273 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 p_extra = extra;
4277 c = *p_extra;
4278 mb_c = mb_ptr2char_adv(&p_extra);
4279 mb_utf8 = (c >= 0x80);
4280 n_extra = (int)STRLEN(p_extra);
4281 c_extra = NUL;
4282 if (area_attr == 0 && search_attr == 0)
4283 {
4284 n_attr = n_extra + 1;
4285 extra_attr = hl_attr(HLF_8);
4286 saved_attr2 = char_attr; /* save current attr */
4287 }
4288 }
4289 else if (mb_l == 0) /* at the NUL at end-of-line */
4290 mb_l = 1;
4291#ifdef FEAT_ARABIC
4292 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4293 {
4294 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004295 int pc, pc1, nc;
4296 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 /* The idea of what is the previous and next
4299 * character depends on 'rightleft'. */
4300 if (wp->w_p_rl)
4301 {
4302 pc = prev_c;
4303 pc1 = prev_c1;
4304 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 else
4308 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004309 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004311 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 prev_c = mb_c;
4314
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004315 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 else
4318 prev_c = mb_c;
4319#endif
4320 }
4321 else /* enc_dbcs */
4322 {
4323 mb_l = MB_BYTE2LEN(c);
4324 if (mb_l == 0) /* at the NUL at end-of-line */
4325 mb_l = 1;
4326 else if (mb_l > 1)
4327 {
4328 /* We assume a second byte below 32 is illegal.
4329 * Hopefully this is OK for all double-byte encodings!
4330 */
4331 if (ptr[1] >= 32)
4332 mb_c = (c << 8) + ptr[1];
4333 else
4334 {
4335 if (ptr[1] == NUL)
4336 {
4337 /* head byte at end of line */
4338 mb_l = 1;
4339 transchar_nonprint(extra, c);
4340 }
4341 else
4342 {
4343 /* illegal tail byte */
4344 mb_l = 2;
4345 STRCPY(extra, "XX");
4346 }
4347 p_extra = extra;
4348 n_extra = (int)STRLEN(extra) - 1;
4349 c_extra = NUL;
4350 c = *p_extra++;
4351 if (area_attr == 0 && search_attr == 0)
4352 {
4353 n_attr = n_extra + 1;
4354 extra_attr = hl_attr(HLF_8);
4355 saved_attr2 = char_attr; /* save current attr */
4356 }
4357 mb_c = c;
4358 }
4359 }
4360 }
4361 /* If a double-width char doesn't fit display a '>' in the
4362 * last column; the character is displayed at the start of the
4363 * next line. */
4364 if ((
4365# ifdef FEAT_RIGHTLEFT
4366 wp->w_p_rl ? (col <= 0) :
4367# endif
4368 (col >= W_WIDTH(wp) - 1))
4369 && (*mb_char2cells)(mb_c) == 2)
4370 {
4371 c = '>';
4372 mb_c = c;
4373 mb_utf8 = FALSE;
4374 mb_l = 1;
4375 multi_attr = hl_attr(HLF_AT);
4376 /* Put pointer back so that the character will be
4377 * displayed at the start of the next line. */
4378 --ptr;
4379 }
4380 else if (*ptr != NUL)
4381 ptr += mb_l - 1;
4382
4383 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004384 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004385 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004386 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004389 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 c = ' ';
4391 if (area_attr == 0 && search_attr == 0)
4392 {
4393 n_attr = n_extra + 1;
4394 extra_attr = hl_attr(HLF_AT);
4395 saved_attr2 = char_attr; /* save current attr */
4396 }
4397 mb_c = c;
4398 mb_utf8 = FALSE;
4399 mb_l = 1;
4400 }
4401
4402 }
4403#endif
4404 ++ptr;
4405
4406 if (extra_check)
4407 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004408#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004409 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004410#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004411
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004412#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 /* Get syntax attribute, unless still at the start of the line
4414 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004415 v = (long)(ptr - line);
4416 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 /* Get the syntax attribute for the character. If there
4419 * is an error, disable syntax highlighting. */
4420 save_did_emsg = did_emsg;
4421 did_emsg = FALSE;
4422
Bram Moolenaar217ad922005-03-20 22:37:15 +00004423 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004424# ifdef FEAT_SPELL
4425 has_spell ? &can_spell :
4426# endif
4427 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004430 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004431 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004432 has_syntax = FALSE;
4433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 else
4435 did_emsg = save_did_emsg;
4436
4437 /* Need to get the line again, a multi-line regexp may
4438 * have made it invalid. */
4439 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4440 ptr = line + v;
4441
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004442 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004444 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004445 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004446# ifdef FEAT_CONCEAL
4447 /* no concealing past the end of the line, it interferes
4448 * with line highlighting */
4449 if (c == NUL)
4450 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004451 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004452 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004455#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004456
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004457#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004458 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004459 * Only do this when there is no syntax highlighting, the
4460 * @Spell cluster is not used or the current syntax item
4461 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004462 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004463 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004464 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004465# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004466 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004467 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004468# endif
4469 if (c != 0 && (
4470# ifdef FEAT_SYN_HL
4471 !has_syntax ||
4472# endif
4473 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004474 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004475 char_u *prev_ptr, *p;
4476 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004477 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004478# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004479 if (has_mbyte)
4480 {
4481 prev_ptr = ptr - mb_l;
4482 v -= mb_l - 1;
4483 }
4484 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004485# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004486 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004487
4488 /* Use nextline[] if possible, it has the start of the
4489 * next line concatenated. */
4490 if ((prev_ptr - line) - nextlinecol >= 0)
4491 p = nextline + (prev_ptr - line) - nextlinecol;
4492 else
4493 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004494 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004495 len = spell_check(wp, p, &spell_hlf, &cap_col,
4496 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004497 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004498
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004499 /* In Insert mode only highlight a word that
4500 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004501 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004502 && (State & INSERT) != 0
4503 && wp->w_cursor.lnum == lnum
4504 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004505 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004506 && wp->w_cursor.col < (colnr_T)word_end)
4507 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004508 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004509 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004511
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004512 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004513 && (p - nextline) + len > nextline_idx)
4514 {
4515 /* Remember that the good word continues at the
4516 * start of the next line. */
4517 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004518 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004519 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004520
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004521 /* Turn index into actual attributes. */
4522 if (spell_hlf != HLF_COUNT)
4523 spell_attr = highlight_attr[spell_hlf];
4524
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004525 if (cap_col > 0)
4526 {
4527 if (p != prev_ptr
4528 && (p - nextline) + cap_col >= nextline_idx)
4529 {
4530 /* Remember that the word in the next line
4531 * must start with a capital. */
4532 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004533 cap_col = (int)((p - nextline) + cap_col
4534 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004535 }
4536 else
4537 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004538 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004539 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004540 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004541 }
4542 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004543 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004544 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004545 char_attr = hl_combine_attr(char_attr, spell_attr);
4546 else
4547 char_attr = hl_combine_attr(spell_attr, char_attr);
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#endif
4550#ifdef FEAT_LINEBREAK
4551 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004552 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004554 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004556# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004557 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004558# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004559 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004561 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004563 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004564
Bram Moolenaar597a4222014-06-25 14:39:50 +02004565 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004566 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004567 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004568 if (c == TAB && n_extra + col > W_WIDTH(wp))
4569 n_extra = (int)wp->w_buffer->b_p_ts
4570 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4571
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004572# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004573 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004574# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004578 {
4579#ifdef FEAT_CONCEAL
4580 if (c == TAB)
4581 /* See "Tab alignment" below. */
4582 FIX_FOR_BOGUSCOLS;
4583#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004584 if (!wp->w_p_list)
4585 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588#endif
4589
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004590 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4591 */
4592 if (wp->w_p_list
4593 && (((c == 160
4594#ifdef FEAT_MBYTE
4595 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4596#endif
4597 ) && lcs_nbsp)
4598 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4599 {
4600 c = (c == ' ') ? lcs_space : lcs_nbsp;
4601 if (area_attr == 0 && search_attr == 0)
4602 {
4603 n_attr = 1;
4604 extra_attr = hl_attr(HLF_8);
4605 saved_attr2 = char_attr; /* save current attr */
4606 }
4607#ifdef FEAT_MBYTE
4608 mb_c = c;
4609 if (enc_utf8 && (*mb_char2len)(c) > 1)
4610 {
4611 mb_utf8 = TRUE;
4612 u8cc[0] = 0;
4613 c = 0xc0;
4614 }
4615 else
4616 mb_utf8 = FALSE;
4617#endif
4618 }
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4621 {
4622 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004623 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 {
4625 n_attr = 1;
4626 extra_attr = hl_attr(HLF_8);
4627 saved_attr2 = char_attr; /* save current attr */
4628 }
4629#ifdef FEAT_MBYTE
4630 mb_c = c;
4631 if (enc_utf8 && (*mb_char2len)(c) > 1)
4632 {
4633 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004635 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 else
4638 mb_utf8 = FALSE;
4639#endif
4640 }
4641 }
4642
4643 /*
4644 * Handling of non-printable characters.
4645 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004646 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 /*
4649 * when getting a character from the file, we may have to
4650 * turn it into something else on the way to putting it
4651 * into "ScreenLines".
4652 */
4653 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4654 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004655 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004656 long vcol_adjusted = vcol; /* removed showbreak length */
4657#ifdef FEAT_LINEBREAK
4658 /* only adjust the tab_len, when at the first column
4659 * after the showbreak value was drawn */
4660 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4661 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004664 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004665 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4666
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004667#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004668 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004669#endif
4670 /* tab amount depends on current column */
4671 n_extra = tab_len;
4672#ifdef FEAT_LINEBREAK
4673 else
4674 {
4675 char_u *p;
4676 int len = n_extra;
4677 int i;
4678 int saved_nextra = n_extra;
4679
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004680#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004681 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004682 /* there are characters to conceal */
4683 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004684 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4685 */
4686 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4687 && n_extra > tab_len)
4688 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004689#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004690
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004691 /* if n_extra > 0, it gives the number of chars, to
4692 * use for a tab, else we need to calculate the width
4693 * for a tab */
4694#ifdef FEAT_MBYTE
4695 len = (tab_len * mb_char2len(lcs_tab2));
4696 if (n_extra > 0)
4697 len += n_extra - tab_len;
4698#endif
4699 c = lcs_tab1;
4700 p = alloc((unsigned)(len + 1));
4701 vim_memset(p, ' ', len);
4702 p[len] = NUL;
4703 p_extra_free = p;
4704 for (i = 0; i < tab_len; i++)
4705 {
4706#ifdef FEAT_MBYTE
4707 mb_char2bytes(lcs_tab2, p);
4708 p += mb_char2len(lcs_tab2);
4709 n_extra += mb_char2len(lcs_tab2)
4710 - (saved_nextra > 0 ? 1 : 0);
4711#else
4712 p[i] = lcs_tab2;
4713#endif
4714 }
4715 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004716#ifdef FEAT_CONCEAL
4717 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4718 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004719 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004720 n_extra -= vcol_off;
4721#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004722 }
4723#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004724#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004725 {
4726 int vc_saved = vcol_off;
4727
4728 /* Tab alignment should be identical regardless of
4729 * 'conceallevel' value. So tab compensates of all
4730 * previous concealed characters, and thus resets
4731 * vcol_off and boguscols accumulated so far in the
4732 * line. Note that the tab can be longer than
4733 * 'tabstop' when there are concealed characters. */
4734 FIX_FOR_BOGUSCOLS;
4735
4736 /* Make sure, the highlighting for the tab char will be
4737 * correctly set further below (effectively reverts the
4738 * FIX_FOR_BOGSUCOLS macro */
4739 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004740 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004741 tab_len += vc_saved;
4742 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744#ifdef FEAT_MBYTE
4745 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4746#endif
4747 if (wp->w_p_list)
4748 {
4749 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004750#ifdef FEAT_LINEBREAK
4751 if (wp->w_p_lbr)
4752 c_extra = NUL; /* using p_extra from above */
4753 else
4754#endif
4755 c_extra = lcs_tab2;
4756 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 extra_attr = hl_attr(HLF_8);
4758 saved_attr2 = char_attr; /* save current attr */
4759#ifdef FEAT_MBYTE
4760 mb_c = c;
4761 if (enc_utf8 && (*mb_char2len)(c) > 1)
4762 {
4763 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004765 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767#endif
4768 }
4769 else
4770 {
4771 c_extra = ' ';
4772 c = ' ';
4773 }
4774 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004775 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004776 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004777 || ((fromcol >= 0 || fromcol_prev >= 0)
4778 && tocol > vcol
4779 && VIsual_mode != Ctrl_V
4780 && (
4781# ifdef FEAT_RIGHTLEFT
4782 wp->w_p_rl ? (col >= 0) :
4783# endif
4784 (col < W_WIDTH(wp)))
4785 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004786 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004787 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004788 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004790 /* Display a '$' after the line or highlight an extra
4791 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4793 /* For a diff line the highlighting continues after the
4794 * "$". */
4795 if (
4796# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004797 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798# ifdef LINE_ATTR
4799 &&
4800# endif
4801# endif
4802# ifdef LINE_ATTR
4803 line_attr == 0
4804# endif
4805 )
4806#endif
4807 {
4808#ifdef FEAT_VIRTUALEDIT
4809 /* In virtualedit, visual selections may extend
4810 * beyond end of line. */
4811 if (area_highlighting && virtual_active()
4812 && tocol != MAXCOL && vcol < tocol)
4813 n_extra = 0;
4814 else
4815#endif
4816 {
4817 p_extra = at_end_str;
4818 n_extra = 1;
4819 c_extra = NUL;
4820 }
4821 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004822 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004823 c = lcs_eol;
4824 else
4825 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 lcs_eol_one = -1;
4827 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004828 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
4830 extra_attr = hl_attr(HLF_AT);
4831 n_attr = 1;
4832 }
4833#ifdef FEAT_MBYTE
4834 mb_c = c;
4835 if (enc_utf8 && (*mb_char2len)(c) > 1)
4836 {
4837 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004838 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004839 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 else
4842 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4843#endif
4844 }
4845 else if (c != NUL)
4846 {
4847 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004848 if (n_extra == 0)
4849 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#ifdef FEAT_RIGHTLEFT
4851 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4852 rl_mirror(p_extra); /* reverse "<12>" */
4853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004855#ifdef FEAT_LINEBREAK
4856 if (wp->w_p_lbr)
4857 {
4858 char_u *p;
4859
4860 c = *p_extra;
4861 p = alloc((unsigned)n_extra + 1);
4862 vim_memset(p, ' ', n_extra);
4863 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4864 p[n_extra] = NUL;
4865 p_extra_free = p_extra = p;
4866 }
4867 else
4868#endif
4869 {
4870 n_extra = byte2cells(c) - 1;
4871 c = *p_extra++;
4872 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004873 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
4875 n_attr = n_extra + 1;
4876 extra_attr = hl_attr(HLF_8);
4877 saved_attr2 = char_attr; /* save current attr */
4878 }
4879#ifdef FEAT_MBYTE
4880 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4881#endif
4882 }
4883#ifdef FEAT_VIRTUALEDIT
4884 else if (VIsual_active
4885 && (VIsual_mode == Ctrl_V
4886 || VIsual_mode == 'v')
4887 && virtual_active()
4888 && tocol != MAXCOL
4889 && vcol < tocol
4890 && (
4891# ifdef FEAT_RIGHTLEFT
4892 wp->w_p_rl ? (col >= 0) :
4893# endif
4894 (col < W_WIDTH(wp))))
4895 {
4896 c = ' ';
4897 --ptr; /* put it back at the NUL */
4898 }
4899#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004900#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 else if ((
4902# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004903 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 ) && (
4907# ifdef FEAT_RIGHTLEFT
4908 wp->w_p_rl ? (col >= 0) :
4909# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004910 (col
4911# ifdef FEAT_CONCEAL
4912 - boguscols
4913# endif
4914 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 /* Highlight until the right side of the window */
4917 c = ' ';
4918 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004919
4920 /* Remember we do the char for line highlighting. */
4921 ++did_line_attr;
4922
4923 /* don't do search HL for the rest of the line */
4924 if (line_attr != 0 && char_attr == search_attr && col > 0)
4925 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926# ifdef FEAT_DIFF
4927 if (diff_hlf == HLF_TXD)
4928 {
4929 diff_hlf = HLF_CHD;
4930 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004933 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4934 char_attr = hl_combine_attr(char_attr,
4935 hl_attr(HLF_CUL));
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938# endif
4939 }
4940#endif
4941 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004942
4943#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004944 if ( wp->w_p_cole > 0
4945 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004946 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004947 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004948 && !(lnum_in_visual_area
4949 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004950 {
4951 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004952 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004953 && (syn_get_sub_char() != NUL || match_conc
4954 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004955 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004956 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004957 /* First time at this concealed item: display one
4958 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004959 if (match_conc)
4960 c = match_conc;
4961 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004962 c = syn_get_sub_char();
4963 else if (lcs_conceal != NUL)
4964 c = lcs_conceal;
4965 else
4966 c = ' ';
4967
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004968 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004969
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004970 if (n_extra > 0)
4971 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004972 vcol += n_extra;
4973 if (wp->w_p_wrap && n_extra > 0)
4974 {
4975# ifdef FEAT_RIGHTLEFT
4976 if (wp->w_p_rl)
4977 {
4978 col -= n_extra;
4979 boguscols -= n_extra;
4980 }
4981 else
4982# endif
4983 {
4984 boguscols += n_extra;
4985 col += n_extra;
4986 }
4987 }
4988 n_extra = 0;
4989 n_attr = 0;
4990 }
4991 else if (n_skip == 0)
4992 {
4993 is_concealing = TRUE;
4994 n_skip = 1;
4995 }
4996# ifdef FEAT_MBYTE
4997 mb_c = c;
4998 if (enc_utf8 && (*mb_char2len)(c) > 1)
4999 {
5000 mb_utf8 = TRUE;
5001 u8cc[0] = 0;
5002 c = 0xc0;
5003 }
5004 else
5005 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5006# endif
5007 }
5008 else
5009 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005010 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005011 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005012 }
5013#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005016#ifdef FEAT_CONCEAL
5017 /* In the cursor line and we may be concealing characters: correct
5018 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005019 if (!did_wcol && draw_state == WL_LINE
5020 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005021 && conceal_cursor_line(wp)
5022 && (int)wp->w_virtcol <= vcol + n_skip)
5023 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005024# ifdef FEAT_RIGHTLEFT
5025 if (wp->w_p_rl)
5026 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5027 else
5028# endif
5029 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005030 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005031 did_wcol = TRUE;
5032 }
5033#endif
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /* Don't override visual selection highlighting. */
5036 if (n_attr > 0
5037 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005038 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 char_attr = extra_attr;
5040
Bram Moolenaar81695252004-12-29 20:58:21 +00005041#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* XIM don't send preedit_start and preedit_end, but they send
5043 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5044 * im_is_preediting() here. */
5045 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005046 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 && (State & INSERT)
5048 && !p_imdisable
5049 && im_is_preediting()
5050 && draw_state == WL_LINE)
5051 {
5052 colnr_T tcol;
5053
5054 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005055 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else
5057 tcol = preedit_end_col;
5058 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5059 {
5060 if (feedback_old_attr < 0)
5061 {
5062 feedback_col = 0;
5063 feedback_old_attr = char_attr;
5064 }
5065 char_attr = im_get_feedback_attr(feedback_col);
5066 if (char_attr < 0)
5067 char_attr = feedback_old_attr;
5068 feedback_col++;
5069 }
5070 else if (feedback_old_attr >= 0)
5071 {
5072 char_attr = feedback_old_attr;
5073 feedback_old_attr = -1;
5074 feedback_col = 0;
5075 }
5076 }
5077#endif
5078 /*
5079 * Handle the case where we are in column 0 but not on the first
5080 * character of the line and the user wants us to show us a
5081 * special character (via 'listchars' option "precedes:<char>".
5082 */
5083 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005084 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5086#ifdef FEAT_DIFF
5087 && filler_todo <= 0
5088#endif
5089 && draw_state > WL_NR
5090 && c != NUL)
5091 {
5092 c = lcs_prec;
5093 lcs_prec_todo = NUL;
5094#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005095 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5096 {
5097 /* Double-width character being overwritten by the "precedes"
5098 * character, need to fill up half the character. */
5099 c_extra = MB_FILLER_CHAR;
5100 n_extra = 1;
5101 n_attr = 2;
5102 extra_attr = hl_attr(HLF_AT);
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 mb_c = c;
5105 if (enc_utf8 && (*mb_char2len)(c) > 1)
5106 {
5107 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005108 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005109 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111 else
5112 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5113#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005114 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 saved_attr3 = char_attr; /* save current attr */
5117 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5118 n_attr3 = 1;
5119 }
5120 }
5121
5122 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005123 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005125 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005126#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005127 || did_line_attr == 1
5128#endif
5129 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005131#ifdef FEAT_SEARCH_EXTRA
5132 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005133
5134 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005135 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005136 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005137#endif
5138
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005139 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 * highlight match at end of line. If it's beyond the last
5141 * char on the screen, just overwrite that one (tricky!) Not
5142 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005143#ifdef FEAT_SEARCH_EXTRA
5144 prevcol_hl_flag = FALSE;
5145 if (prevcol == (long)search_hl.startcol)
5146 prevcol_hl_flag = TRUE;
5147 else
5148 {
5149 cur = wp->w_match_head;
5150 while (cur != NULL)
5151 {
5152 if (prevcol == (long)cur->hl.startcol)
5153 {
5154 prevcol_hl_flag = TRUE;
5155 break;
5156 }
5157 cur = cur->next;
5158 }
5159 }
5160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005162 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005163 && (VIsual_mode != Ctrl_V
5164 || lnum == VIsual.lnum
5165 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005166 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#ifdef FEAT_SEARCH_EXTRA
5168 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005169 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005170# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005171 && did_line_attr <= 1
5172# endif
5173 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#endif
5175 ))
5176 {
5177 int n = 0;
5178
5179#ifdef FEAT_RIGHTLEFT
5180 if (wp->w_p_rl)
5181 {
5182 if (col < 0)
5183 n = 1;
5184 }
5185 else
5186#endif
5187 {
5188 if (col >= W_WIDTH(wp))
5189 n = -1;
5190 }
5191 if (n != 0)
5192 {
5193 /* At the window boundary, highlight the last character
5194 * instead (better than nothing). */
5195 off += n;
5196 col += n;
5197 }
5198 else
5199 {
5200 /* Add a blank character to highlight. */
5201 ScreenLines[off] = ' ';
5202#ifdef FEAT_MBYTE
5203 if (enc_utf8)
5204 ScreenLinesUC[off] = 0;
5205#endif
5206 }
5207#ifdef FEAT_SEARCH_EXTRA
5208 if (area_attr == 0)
5209 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005210 /* Use attributes from match with highest priority among
5211 * 'search_hl' and the match list. */
5212 char_attr = search_hl.attr;
5213 cur = wp->w_match_head;
5214 shl_flag = FALSE;
5215 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005216 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005217 if (shl_flag == FALSE
5218 && ((cur != NULL
5219 && cur->priority > SEARCH_HL_PRIORITY)
5220 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005221 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005222 shl = &search_hl;
5223 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005224 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005225 else
5226 shl = &cur->hl;
5227 if ((ptr - line) - 1 == (long)shl->startcol)
5228 char_attr = shl->attr;
5229 if (shl != &search_hl && cur != NULL)
5230 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
5233#endif
5234 ScreenAttrs[off] = char_attr;
5235#ifdef FEAT_RIGHTLEFT
5236 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005239 --off;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
5242#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005245 ++off;
5246 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005247 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005248#ifdef FEAT_SYN_HL
5249 eol_hl_off = 1;
5250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
Bram Moolenaar91170f82006-05-05 21:15:17 +00005254 /*
5255 * At end of the text line.
5256 */
5257 if (c == NUL)
5258 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005259#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005260 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5261 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005262 {
5263 /* highlight last char after line */
5264 --col;
5265 --off;
5266 --vcol;
5267 }
5268
Bram Moolenaar1a384422010-07-14 19:53:30 +02005269 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005270 if (wp->w_p_wrap)
5271 v = wp->w_skipcol;
5272 else
5273 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005274
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005275 /* check if line ends before left margin */
5276 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005277 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005278#ifdef FEAT_CONCEAL
5279 /* Get rid of the boguscols now, we want to draw until the right
5280 * edge for 'cursorcolumn'. */
5281 col -= boguscols;
5282 boguscols = 0;
5283#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005284
5285 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005286 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005287
5288 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005289 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5290 && (int)wp->w_virtcol <
5291 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005292 && lnum != wp->w_cursor.lnum)
5293 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005294# ifdef FEAT_RIGHTLEFT
5295 && !wp->w_p_rl
5296# endif
5297 )
5298 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005299 int rightmost_vcol = 0;
5300 int i;
5301
5302 if (wp->w_p_cuc)
5303 rightmost_vcol = wp->w_virtcol;
5304 if (draw_color_col)
5305 /* determine rightmost colorcolumn to possibly draw */
5306 for (i = 0; color_cols[i] >= 0; ++i)
5307 if (rightmost_vcol < color_cols[i])
5308 rightmost_vcol = color_cols[i];
5309
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005310 while (col < W_WIDTH(wp))
5311 {
5312 ScreenLines[off] = ' ';
5313#ifdef FEAT_MBYTE
5314 if (enc_utf8)
5315 ScreenLinesUC[off] = 0;
5316#endif
5317 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005318 if (draw_color_col)
5319 draw_color_col = advance_color_col(VCOL_HLC,
5320 &color_cols);
5321
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005322 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005323 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005324 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005325 ScreenAttrs[off++] = hl_attr(HLF_MC);
5326 else
5327 ScreenAttrs[off++] = 0;
5328
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005329 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005330 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005331
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005332 ++vcol;
5333 }
5334 }
5335#endif
5336
Bram Moolenaar860cae12010-06-05 23:22:07 +02005337 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5338 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 row++;
5340
5341 /*
5342 * Update w_cline_height and w_cline_folded if the cursor line was
5343 * updated (saves a call to plines() later).
5344 */
5345 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5346 {
5347 curwin->w_cline_row = startrow;
5348 curwin->w_cline_height = row - startrow;
5349#ifdef FEAT_FOLDING
5350 curwin->w_cline_folded = FALSE;
5351#endif
5352 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5353 }
5354
5355 break;
5356 }
5357
5358 /* line continues beyond line end */
5359 if (lcs_ext
5360 && !wp->w_p_wrap
5361#ifdef FEAT_DIFF
5362 && filler_todo <= 0
5363#endif
5364 && (
5365#ifdef FEAT_RIGHTLEFT
5366 wp->w_p_rl ? col == 0 :
5367#endif
5368 col == W_WIDTH(wp) - 1)
5369 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005370 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5372 {
5373 c = lcs_ext;
5374 char_attr = hl_attr(HLF_AT);
5375#ifdef FEAT_MBYTE
5376 mb_c = c;
5377 if (enc_utf8 && (*mb_char2len)(c) > 1)
5378 {
5379 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005380 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005381 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383 else
5384 mb_utf8 = FALSE;
5385#endif
5386 }
5387
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005388#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005389 /* advance to the next 'colorcolumn' */
5390 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005391 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005392
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005393 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005394 * highlight the cursor position itself.
5395 * Also highlight the 'colorcolumn' if it is different than
5396 * 'cursorcolumn' */
5397 vcol_save_attr = -1;
5398 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005399 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005400 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005401 && lnum != wp->w_cursor.lnum)
5402 {
5403 vcol_save_attr = char_attr;
5404 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5405 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005406 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005407 {
5408 vcol_save_attr = char_attr;
5409 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5410 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005411 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005412#endif
5413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 /*
5415 * Store character to be displayed.
5416 * Skip characters that are left of the screen for 'nowrap'.
5417 */
5418 vcol_prev = vcol;
5419 if (draw_state < WL_LINE || n_skip <= 0)
5420 {
5421 /*
5422 * Store the character.
5423 */
5424#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5425 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5426 {
5427 /* A double-wide character is: put first halve in left cell. */
5428 --off;
5429 --col;
5430 }
5431#endif
5432 ScreenLines[off] = c;
5433#ifdef FEAT_MBYTE
5434 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005435 {
5436 if ((mb_c & 0xff00) == 0x8e00)
5437 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 else if (enc_utf8)
5441 {
5442 if (mb_utf8)
5443 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005444 int i;
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005447 if ((c & 0xff) == 0)
5448 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005449 for (i = 0; i < Screen_mco; ++i)
5450 {
5451 ScreenLinesC[i][off] = u8cc[i];
5452 if (u8cc[i] == 0)
5453 break;
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456 else
5457 ScreenLinesUC[off] = 0;
5458 }
5459 if (multi_attr)
5460 {
5461 ScreenAttrs[off] = multi_attr;
5462 multi_attr = 0;
5463 }
5464 else
5465#endif
5466 ScreenAttrs[off] = char_attr;
5467
5468#ifdef FEAT_MBYTE
5469 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5470 {
5471 /* Need to fill two screen columns. */
5472 ++off;
5473 ++col;
5474 if (enc_utf8)
5475 /* UTF-8: Put a 0 in the second screen char. */
5476 ScreenLines[off] = 0;
5477 else
5478 /* DBCS: Put second byte in the second screen char. */
5479 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005480 if (draw_state > WL_NR
5481#ifdef FEAT_DIFF
5482 && filler_todo <= 0
5483#endif
5484 )
5485 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 /* When "tocol" is halfway a character, set it to the end of
5487 * the character, otherwise highlighting won't stop. */
5488 if (tocol == vcol)
5489 ++tocol;
5490#ifdef FEAT_RIGHTLEFT
5491 if (wp->w_p_rl)
5492 {
5493 /* now it's time to backup one cell */
5494 --off;
5495 --col;
5496 }
5497#endif
5498 }
5499#endif
5500#ifdef FEAT_RIGHTLEFT
5501 if (wp->w_p_rl)
5502 {
5503 --off;
5504 --col;
5505 }
5506 else
5507#endif
5508 {
5509 ++off;
5510 ++col;
5511 }
5512 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005513#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005514 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005515 {
5516 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005517 ++vcol_off;
5518 if (n_extra > 0)
5519 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005520 if (wp->w_p_wrap)
5521 {
5522 /*
5523 * Special voodoo required if 'wrap' is on.
5524 *
5525 * Advance the column indicator to force the line
5526 * drawing to wrap early. This will make the line
5527 * take up the same screen space when parts are concealed,
5528 * so that cursor line computations aren't messed up.
5529 *
5530 * To avoid the fictitious advance of 'col' causing
5531 * trailing junk to be written out of the screen line
5532 * we are building, 'boguscols' keeps track of the number
5533 * of bad columns we have advanced.
5534 */
5535 if (n_extra > 0)
5536 {
5537 vcol += n_extra;
5538# ifdef FEAT_RIGHTLEFT
5539 if (wp->w_p_rl)
5540 {
5541 col -= n_extra;
5542 boguscols -= n_extra;
5543 }
5544 else
5545# endif
5546 {
5547 col += n_extra;
5548 boguscols += n_extra;
5549 }
5550 n_extra = 0;
5551 n_attr = 0;
5552 }
5553
5554
5555# ifdef FEAT_MBYTE
5556 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5557 {
5558 /* Need to fill two screen columns. */
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# endif
5573
5574# ifdef FEAT_RIGHTLEFT
5575 if (wp->w_p_rl)
5576 {
5577 --boguscols;
5578 --col;
5579 }
5580 else
5581# endif
5582 {
5583 ++boguscols;
5584 ++col;
5585 }
5586 }
5587 else
5588 {
5589 if (n_extra > 0)
5590 {
5591 vcol += n_extra;
5592 n_extra = 0;
5593 n_attr = 0;
5594 }
5595 }
5596
5597 }
5598#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 else
5600 --n_skip;
5601
Bram Moolenaar64486672010-05-16 15:46:46 +02005602 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5603 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005604 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605#ifdef FEAT_DIFF
5606 && filler_todo <= 0
5607#endif
5608 )
5609 ++vcol;
5610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005611#ifdef FEAT_SYN_HL
5612 if (vcol_save_attr >= 0)
5613 char_attr = vcol_save_attr;
5614#endif
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 /* restore attributes after "predeces" in 'listchars' */
5617 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5618 char_attr = saved_attr3;
5619
5620 /* restore attributes after last 'listchars' or 'number' char */
5621 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5622 char_attr = saved_attr2;
5623
5624 /*
5625 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005626 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 */
5628 if ((
5629#ifdef FEAT_RIGHTLEFT
5630 wp->w_p_rl ? (col < 0) :
5631#endif
5632 (col >= W_WIDTH(wp)))
5633 && (*ptr != NUL
5634#ifdef FEAT_DIFF
5635 || filler_todo > 0
5636#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005637 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5639 )
5640 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005641#ifdef FEAT_CONCEAL
5642 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5643 (int)W_WIDTH(wp), wp->w_p_rl);
5644 boguscols = 0;
5645#else
5646 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5647 (int)W_WIDTH(wp), wp->w_p_rl);
5648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 ++row;
5650 ++screen_row;
5651
5652 /* When not wrapping and finished diff lines, or when displayed
5653 * '$' and highlighting until last column, break here. */
5654 if ((!wp->w_p_wrap
5655#ifdef FEAT_DIFF
5656 && filler_todo <= 0
5657#endif
5658 ) || lcs_eol_one == -1)
5659 break;
5660
5661 /* When the window is too narrow draw all "@" lines. */
5662 if (draw_state != WL_LINE
5663#ifdef FEAT_DIFF
5664 && filler_todo <= 0
5665#endif
5666 )
5667 {
5668 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005669#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 draw_vsep_win(wp, row);
5671#endif
5672 row = endrow;
5673 }
5674
5675 /* When line got too long for screen break here. */
5676 if (row == endrow)
5677 {
5678 ++row;
5679 break;
5680 }
5681
5682 if (screen_cur_row == screen_row - 1
5683#ifdef FEAT_DIFF
5684 && filler_todo <= 0
5685#endif
5686 && W_WIDTH(wp) == Columns)
5687 {
5688 /* Remember that the line wraps, used for modeless copy. */
5689 LineWraps[screen_row - 1] = TRUE;
5690
5691 /*
5692 * Special trick to make copy/paste of wrapped lines work with
5693 * xterm/screen: write an extra character beyond the end of
5694 * the line. This will work with all terminal types
5695 * (regardless of the xn,am settings).
5696 * Only do this on a fast tty.
5697 * Only do this if the cursor is on the current line
5698 * (something has been written in it).
5699 * Don't do this for the GUI.
5700 * Don't do this for double-width characters.
5701 * Don't do this for a window not at the right screen border.
5702 */
5703 if (p_tf
5704#ifdef FEAT_GUI
5705 && !gui.in_use
5706#endif
5707#ifdef FEAT_MBYTE
5708 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005709 && ((*mb_off2cells)(LineOffset[screen_row],
5710 LineOffset[screen_row] + screen_Columns)
5711 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005713 + (int)Columns - 2,
5714 LineOffset[screen_row] + screen_Columns)
5715 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#endif
5717 )
5718 {
5719 /* First make sure we are at the end of the screen line,
5720 * then output the same character again to let the
5721 * terminal know about the wrap. If the terminal doesn't
5722 * auto-wrap, we overwrite the character. */
5723 if (screen_cur_col != W_WIDTH(wp))
5724 screen_char(LineOffset[screen_row - 1]
5725 + (unsigned)Columns - 1,
5726 screen_row - 1, (int)(Columns - 1));
5727
5728#ifdef FEAT_MBYTE
5729 /* When there is a multi-byte character, just output a
5730 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005731 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5732 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 out_char(' ');
5734 else
5735#endif
5736 out_char(ScreenLines[LineOffset[screen_row - 1]
5737 + (Columns - 1)]);
5738 /* force a redraw of the first char on the next line */
5739 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5740 screen_start(); /* don't know where cursor is now */
5741 }
5742 }
5743
5744 col = 0;
5745 off = (unsigned)(current_ScreenLine - ScreenLines);
5746#ifdef FEAT_RIGHTLEFT
5747 if (wp->w_p_rl)
5748 {
5749 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5750 off += col;
5751 }
5752#endif
5753
5754 /* reset the drawing state for the start of a wrapped line */
5755 draw_state = WL_START;
5756 saved_n_extra = n_extra;
5757 saved_p_extra = p_extra;
5758 saved_c_extra = c_extra;
5759 saved_char_attr = char_attr;
5760 n_extra = 0;
5761 lcs_prec_todo = lcs_prec;
5762#ifdef FEAT_LINEBREAK
5763# ifdef FEAT_DIFF
5764 if (filler_todo <= 0)
5765# endif
5766 need_showbreak = TRUE;
5767#endif
5768#ifdef FEAT_DIFF
5769 --filler_todo;
5770 /* When the filler lines are actually below the last line of the
5771 * file, don't draw the line itself, break here. */
5772 if (filler_todo == 0 && wp->w_botfill)
5773 break;
5774#endif
5775 }
5776
5777 } /* for every character in the line */
5778
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005779#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005780 /* After an empty line check first word for capital. */
5781 if (*skipwhite(line) == NUL)
5782 {
5783 capcol_lnum = lnum + 1;
5784 cap_col = 0;
5785 }
5786#endif
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 return row;
5789}
5790
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005791#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005792static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005793
5794/*
5795 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005796 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005797 */
5798 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005799comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005800{
5801 int i;
5802
5803 for (i = 0; i < Screen_mco; ++i)
5804 {
5805 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5806 return TRUE;
5807 if (ScreenLinesC[i][off_from] == 0)
5808 break;
5809 }
5810 return FALSE;
5811}
5812#endif
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814/*
5815 * Check whether the given character needs redrawing:
5816 * - the (first byte of the) character is different
5817 * - the attributes are different
5818 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005819 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 */
5821 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005822char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 if (cols > 0
5825 && ((ScreenLines[off_from] != ScreenLines[off_to]
5826 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5827
5828#ifdef FEAT_MBYTE
5829 || (enc_dbcs != 0
5830 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5831 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5832 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5833 : (cols > 1 && ScreenLines[off_from + 1]
5834 != ScreenLines[off_to + 1])))
5835 || (enc_utf8
5836 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5837 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005838 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005839 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5840 && ScreenLines[off_from + 1]
5841 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#endif
5843 ))
5844 return TRUE;
5845 return FALSE;
5846}
5847
5848/*
5849 * Move one "cooked" screen line to the screen, but only the characters that
5850 * have actually changed. Handle insert/delete character.
5851 * "coloff" gives the first column on the screen for this line.
5852 * "endcol" gives the columns where valid characters are.
5853 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5854 * needs to be cleared, negative otherwise.
5855 * "rlflag" is TRUE in a rightleft window:
5856 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5857 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5858 */
5859 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005860screen_line(
5861 int row,
5862 int coloff,
5863 int endcol,
5864 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005866 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005868 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 unsigned off_from;
5871 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005872#ifdef FEAT_MBYTE
5873 unsigned max_off_from;
5874 unsigned max_off_to;
5875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005877#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 int hl;
5879#endif
5880 int force = FALSE; /* force update rest of the line */
5881 int redraw_this /* bool: does character need redraw? */
5882#ifdef FEAT_GUI
5883 = TRUE /* For GUI when while-loop empty */
5884#endif
5885 ;
5886 int redraw_next; /* redraw_this for next character */
5887#ifdef FEAT_MBYTE
5888 int clear_next = FALSE;
5889 int char_cells; /* 1: normal char */
5890 /* 2: occupies two display cells */
5891# define CHAR_CELLS char_cells
5892#else
5893# define CHAR_CELLS 1
5894#endif
5895
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005896 /* Check for illegal row and col, just in case. */
5897 if (row >= Rows)
5898 row = Rows - 1;
5899 if (endcol > Columns)
5900 endcol = Columns;
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902# ifdef FEAT_CLIPBOARD
5903 clip_may_clear_selection(row, row);
5904# endif
5905
5906 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5907 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005908#ifdef FEAT_MBYTE
5909 max_off_from = off_from + screen_Columns;
5910 max_off_to = LineOffset[row] + screen_Columns;
5911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913#ifdef FEAT_RIGHTLEFT
5914 if (rlflag)
5915 {
5916 /* Clear rest first, because it's left of the text. */
5917 if (clear_width > 0)
5918 {
5919 while (col <= endcol && ScreenLines[off_to] == ' '
5920 && ScreenAttrs[off_to] == 0
5921# ifdef FEAT_MBYTE
5922 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5923# endif
5924 )
5925 {
5926 ++off_to;
5927 ++col;
5928 }
5929 if (col <= endcol)
5930 screen_fill(row, row + 1, col + coloff,
5931 endcol + coloff + 1, ' ', ' ', 0);
5932 }
5933 col = endcol + 1;
5934 off_to = LineOffset[row] + col + coloff;
5935 off_from += col;
5936 endcol = (clear_width > 0 ? clear_width : -clear_width);
5937 }
5938#endif /* FEAT_RIGHTLEFT */
5939
5940 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5941
5942 while (col < endcol)
5943 {
5944#ifdef FEAT_MBYTE
5945 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005946 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else
5948 char_cells = 1;
5949#endif
5950
5951 redraw_this = redraw_next;
5952 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5953 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5954
5955#ifdef FEAT_GUI
5956 /* If the next character was bold, then redraw the current character to
5957 * remove any pixels that might have spilt over into us. This only
5958 * happens in the GUI.
5959 */
5960 if (redraw_next && gui.in_use)
5961 {
5962 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005963 if (hl > HL_ALL)
5964 hl = syn_attr2attr(hl);
5965 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 redraw_this = TRUE;
5967 }
5968#endif
5969
5970 if (redraw_this)
5971 {
5972 /*
5973 * Special handling when 'xs' termcap flag set (hpterm):
5974 * Attributes for characters are stored at the position where the
5975 * cursor is when writing the highlighting code. The
5976 * start-highlighting code must be written with the cursor on the
5977 * first highlighted character. The stop-highlighting code must
5978 * be written with the cursor just after the last highlighted
5979 * character.
5980 * Overwriting a character doesn't remove it's highlighting. Need
5981 * to clear the rest of the line, and force redrawing it
5982 * completely.
5983 */
5984 if ( p_wiv
5985 && !force
5986#ifdef FEAT_GUI
5987 && !gui.in_use
5988#endif
5989 && ScreenAttrs[off_to] != 0
5990 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5991 {
5992 /*
5993 * Need to remove highlighting attributes here.
5994 */
5995 windgoto(row, col + coloff);
5996 out_str(T_CE); /* clear rest of this screen line */
5997 screen_start(); /* don't know where cursor is now */
5998 force = TRUE; /* force redraw of rest of the line */
5999 redraw_next = TRUE; /* or else next char would miss out */
6000
6001 /*
6002 * If the previous character was highlighted, need to stop
6003 * highlighting at this character.
6004 */
6005 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6006 {
6007 screen_attr = ScreenAttrs[off_to - 1];
6008 term_windgoto(row, col + coloff);
6009 screen_stop_highlight();
6010 }
6011 else
6012 screen_attr = 0; /* highlighting has stopped */
6013 }
6014#ifdef FEAT_MBYTE
6015 if (enc_dbcs != 0)
6016 {
6017 /* Check if overwriting a double-byte with a single-byte or
6018 * the other way around requires another character to be
6019 * redrawn. For UTF-8 this isn't needed, because comparing
6020 * ScreenLinesUC[] is sufficient. */
6021 if (char_cells == 1
6022 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006023 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 /* Writing a single-cell character over a double-cell
6026 * character: need to redraw the next cell. */
6027 ScreenLines[off_to + 1] = 0;
6028 redraw_next = TRUE;
6029 }
6030 else if (char_cells == 2
6031 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006032 && (*mb_off2cells)(off_to, max_off_to) == 1
6033 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
6035 /* Writing the second half of a double-cell character over
6036 * a double-cell character: need to redraw the second
6037 * cell. */
6038 ScreenLines[off_to + 2] = 0;
6039 redraw_next = TRUE;
6040 }
6041
6042 if (enc_dbcs == DBCS_JPNU)
6043 ScreenLines2[off_to] = ScreenLines2[off_from];
6044 }
6045 /* When writing a single-width character over a double-width
6046 * character and at the end of the redrawn text, need to clear out
6047 * the right halve of the old character.
6048 * Also required when writing the right halve of a double-width
6049 * char over the left halve of an existing one. */
6050 if (has_mbyte && col + char_cells == endcol
6051 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006052 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006054 && (*mb_off2cells)(off_to, max_off_to) == 1
6055 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 clear_next = TRUE;
6057#endif
6058
6059 ScreenLines[off_to] = ScreenLines[off_from];
6060#ifdef FEAT_MBYTE
6061 if (enc_utf8)
6062 {
6063 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6064 if (ScreenLinesUC[off_from] != 0)
6065 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006066 int i;
6067
6068 for (i = 0; i < Screen_mco; ++i)
6069 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
6071 }
6072 if (char_cells == 2)
6073 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6074#endif
6075
6076#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006077 /* The bold trick makes a single column of pixels appear in the
6078 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 * character should be redrawn too. This happens for our own GUI
6080 * and for some xterms. */
6081 if (
6082# ifdef FEAT_GUI
6083 gui.in_use
6084# endif
6085# if defined(FEAT_GUI) && defined(UNIX)
6086 ||
6087# endif
6088# ifdef UNIX
6089 term_is_xterm
6090# endif
6091 )
6092 {
6093 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006094 if (hl > HL_ALL)
6095 hl = syn_attr2attr(hl);
6096 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 redraw_next = TRUE;
6098 }
6099#endif
6100 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6101#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006102 /* For simplicity set the attributes of second half of a
6103 * double-wide character equal to the first half. */
6104 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006106
6107 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 else
6110#endif
6111 screen_char(off_to, row, col + coloff);
6112 }
6113 else if ( p_wiv
6114#ifdef FEAT_GUI
6115 && !gui.in_use
6116#endif
6117 && col + coloff > 0)
6118 {
6119 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6120 {
6121 /*
6122 * Don't output stop-highlight when moving the cursor, it will
6123 * stop the highlighting when it should continue.
6124 */
6125 screen_attr = 0;
6126 }
6127 else if (screen_attr != 0)
6128 screen_stop_highlight();
6129 }
6130
6131 off_to += CHAR_CELLS;
6132 off_from += CHAR_CELLS;
6133 col += CHAR_CELLS;
6134 }
6135
6136#ifdef FEAT_MBYTE
6137 if (clear_next)
6138 {
6139 /* Clear the second half of a double-wide character of which the left
6140 * half was overwritten with a single-wide character. */
6141 ScreenLines[off_to] = ' ';
6142 if (enc_utf8)
6143 ScreenLinesUC[off_to] = 0;
6144 screen_char(off_to, row, col + coloff);
6145 }
6146#endif
6147
6148 if (clear_width > 0
6149#ifdef FEAT_RIGHTLEFT
6150 && !rlflag
6151#endif
6152 )
6153 {
6154#ifdef FEAT_GUI
6155 int startCol = col;
6156#endif
6157
6158 /* blank out the rest of the line */
6159 while (col < clear_width && ScreenLines[off_to] == ' '
6160 && ScreenAttrs[off_to] == 0
6161#ifdef FEAT_MBYTE
6162 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6163#endif
6164 )
6165 {
6166 ++off_to;
6167 ++col;
6168 }
6169 if (col < clear_width)
6170 {
6171#ifdef FEAT_GUI
6172 /*
6173 * In the GUI, clearing the rest of the line may leave pixels
6174 * behind if the first character cleared was bold. Some bold
6175 * fonts spill over the left. In this case we redraw the previous
6176 * character too. If we didn't skip any blanks above, then we
6177 * only redraw if the character wasn't already redrawn anyway.
6178 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006179 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
6181 hl = ScreenAttrs[off_to];
6182 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006183 {
6184 int prev_cells = 1;
6185# ifdef FEAT_MBYTE
6186 if (enc_utf8)
6187 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6188 * that its width is 2. */
6189 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6190 else if (enc_dbcs != 0)
6191 {
6192 /* find previous character by counting from first
6193 * column and get its width. */
6194 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006195 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006196
6197 while (off < off_to)
6198 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006200 off += prev_cells;
6201 }
6202 }
6203
6204 if (enc_dbcs != 0 && prev_cells > 1)
6205 screen_char_2(off_to - prev_cells, row,
6206 col + coloff - prev_cells);
6207 else
6208# endif
6209 screen_char(off_to - prev_cells, row,
6210 col + coloff - prev_cells);
6211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 }
6213#endif
6214 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6215 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006216#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 off_to += clear_width - col;
6218 col = clear_width;
6219#endif
6220 }
6221 }
6222
6223 if (clear_width > 0)
6224 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006225#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 /* For a window that's left of another, draw the separator char. */
6227 if (col + coloff < Columns)
6228 {
6229 int c;
6230
6231 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006232 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006234 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6235 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236# endif
6237 || ScreenAttrs[off_to] != hl)
6238 {
6239 ScreenLines[off_to] = c;
6240 ScreenAttrs[off_to] = hl;
6241# ifdef FEAT_MBYTE
6242 if (enc_utf8)
6243 {
6244 if (c >= 0x80)
6245 {
6246 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006247 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249 else
6250 ScreenLinesUC[off_to] = 0;
6251 }
6252# endif
6253 screen_char(off_to, row, col + coloff);
6254 }
6255 }
6256 else
6257#endif
6258 LineWraps[row] = FALSE;
6259 }
6260}
6261
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006262#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006264 * Mirror text "str" for right-left displaying.
6265 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006268rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 char_u *p1, *p2;
6271 int t;
6272
6273 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6274 {
6275 t = *p1;
6276 *p1 = *p2;
6277 *p2 = t;
6278 }
6279}
6280#endif
6281
6282#if defined(FEAT_WINDOWS) || defined(PROTO)
6283/*
6284 * mark all status lines for redraw; used after first :cd
6285 */
6286 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006287status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 win_T *wp;
6290
Bram Moolenaar29323592016-07-24 22:04:11 +02006291 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (wp->w_status_height)
6293 {
6294 wp->w_redr_status = TRUE;
6295 redraw_later(VALID);
6296 }
6297}
6298
6299/*
6300 * mark all status lines of the current buffer for redraw
6301 */
6302 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006303status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 win_T *wp;
6306
Bram Moolenaar29323592016-07-24 22:04:11 +02006307 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6309 {
6310 wp->w_redr_status = TRUE;
6311 redraw_later(VALID);
6312 }
6313}
6314
6315/*
6316 * Redraw all status lines that need to be redrawn.
6317 */
6318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006319redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 win_T *wp;
6322
Bram Moolenaar29323592016-07-24 22:04:11 +02006323 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (wp->w_redr_status)
6325 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006326 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006327 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328}
6329#endif
6330
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006331#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332/*
6333 * Redraw all status lines at the bottom of frame "frp".
6334 */
6335 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006336win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 if (frp->fr_layout == FR_LEAF)
6339 frp->fr_win->w_redr_status = TRUE;
6340 else if (frp->fr_layout == FR_ROW)
6341 {
6342 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6343 win_redraw_last_status(frp);
6344 }
6345 else /* frp->fr_layout == FR_COL */
6346 {
6347 frp = frp->fr_child;
6348 while (frp->fr_next != NULL)
6349 frp = frp->fr_next;
6350 win_redraw_last_status(frp);
6351 }
6352}
6353#endif
6354
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006355#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356/*
6357 * Draw the verticap separator right of window "wp" starting with line "row".
6358 */
6359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006360draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 int hl;
6363 int c;
6364
6365 if (wp->w_vsep_width)
6366 {
6367 /* draw the vertical separator right of this window */
6368 c = fillchar_vsep(&hl);
6369 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6370 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6371 c, ' ', hl);
6372 }
6373}
6374#endif
6375
6376#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006377static int status_match_len(expand_T *xp, char_u *s);
6378static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006381 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 */
6383 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006384status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int len = 0;
6387
6388#ifdef FEAT_MENU
6389 int emenu = (xp->xp_context == EXPAND_MENUS
6390 || xp->xp_context == EXPAND_MENUNAMES);
6391
6392 /* Check for menu separators - replace with '|'. */
6393 if (emenu && menu_is_separator(s))
6394 return 1;
6395#endif
6396
6397 while (*s != NUL)
6398 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006399 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006400 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006401 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403
6404 return len;
6405}
6406
6407/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006408 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006409 * These are backslashes used for escaping. Do show backslashes in help tags.
6410 */
6411 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006412skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006413{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006414 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006415#ifdef FEAT_MENU
6416 || ((xp->xp_context == EXPAND_MENUS
6417 || xp->xp_context == EXPAND_MENUNAMES)
6418 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6419#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006420 )
6421 {
6422#ifndef BACKSLASH_IN_FILENAME
6423 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6424 return 2;
6425#endif
6426 return 1;
6427 }
6428 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006429}
6430
6431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 * Show wildchar matches in the status line.
6433 * Show at least the "match" item.
6434 * We start at item 'first_match' in the list and show all matches that fit.
6435 *
6436 * If inversion is possible we use it. Else '=' characters are used.
6437 */
6438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006439win_redr_status_matches(
6440 expand_T *xp,
6441 int num_matches,
6442 char_u **matches, /* list of matches */
6443 int match,
6444 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6447 int row;
6448 char_u *buf;
6449 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006450 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 int fillchar;
6452 int attr;
6453 int i;
6454 int highlight = TRUE;
6455 char_u *selstart = NULL;
6456 int selstart_col = 0;
6457 char_u *selend = NULL;
6458 static int first_match = 0;
6459 int add_left = FALSE;
6460 char_u *s;
6461#ifdef FEAT_MENU
6462 int emenu;
6463#endif
6464#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6465 int l;
6466#endif
6467
6468 if (matches == NULL) /* interrupted completion? */
6469 return;
6470
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006471#ifdef FEAT_MBYTE
6472 if (has_mbyte)
6473 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6474 else
6475#endif
6476 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (buf == NULL)
6478 return;
6479
6480 if (match == -1) /* don't show match but original text */
6481 {
6482 match = 0;
6483 highlight = FALSE;
6484 }
6485 /* count 1 for the ending ">" */
6486 clen = status_match_len(xp, L_MATCH(match)) + 3;
6487 if (match == 0)
6488 first_match = 0;
6489 else if (match < first_match)
6490 {
6491 /* jumping left, as far as we can go */
6492 first_match = match;
6493 add_left = TRUE;
6494 }
6495 else
6496 {
6497 /* check if match fits on the screen */
6498 for (i = first_match; i < match; ++i)
6499 clen += status_match_len(xp, L_MATCH(i)) + 2;
6500 if (first_match > 0)
6501 clen += 2;
6502 /* jumping right, put match at the left */
6503 if ((long)clen > Columns)
6504 {
6505 first_match = match;
6506 /* if showing the last match, we can add some on the left */
6507 clen = 2;
6508 for (i = match; i < num_matches; ++i)
6509 {
6510 clen += status_match_len(xp, L_MATCH(i)) + 2;
6511 if ((long)clen >= Columns)
6512 break;
6513 }
6514 if (i == num_matches)
6515 add_left = TRUE;
6516 }
6517 }
6518 if (add_left)
6519 while (first_match > 0)
6520 {
6521 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6522 if ((long)clen >= Columns)
6523 break;
6524 --first_match;
6525 }
6526
6527 fillchar = fillchar_status(&attr, TRUE);
6528
6529 if (first_match == 0)
6530 {
6531 *buf = NUL;
6532 len = 0;
6533 }
6534 else
6535 {
6536 STRCPY(buf, "< ");
6537 len = 2;
6538 }
6539 clen = len;
6540
6541 i = first_match;
6542 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6543 {
6544 if (i == match)
6545 {
6546 selstart = buf + len;
6547 selstart_col = clen;
6548 }
6549
6550 s = L_MATCH(i);
6551 /* Check for menu separators - replace with '|' */
6552#ifdef FEAT_MENU
6553 emenu = (xp->xp_context == EXPAND_MENUS
6554 || xp->xp_context == EXPAND_MENUNAMES);
6555 if (emenu && menu_is_separator(s))
6556 {
6557 STRCPY(buf + len, transchar('|'));
6558 l = (int)STRLEN(buf + len);
6559 len += l;
6560 clen += l;
6561 }
6562 else
6563#endif
6564 for ( ; *s != NUL; ++s)
6565 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006566 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 clen += ptr2cells(s);
6568#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006569 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
6571 STRNCPY(buf + len, s, l);
6572 s += l - 1;
6573 len += l;
6574 }
6575 else
6576#endif
6577 {
6578 STRCPY(buf + len, transchar_byte(*s));
6579 len += (int)STRLEN(buf + len);
6580 }
6581 }
6582 if (i == match)
6583 selend = buf + len;
6584
6585 *(buf + len++) = ' ';
6586 *(buf + len++) = ' ';
6587 clen += 2;
6588 if (++i == num_matches)
6589 break;
6590 }
6591
6592 if (i != num_matches)
6593 {
6594 *(buf + len++) = '>';
6595 ++clen;
6596 }
6597
6598 buf[len] = NUL;
6599
6600 row = cmdline_row - 1;
6601 if (row >= 0)
6602 {
6603 if (wild_menu_showing == 0)
6604 {
6605 if (msg_scrolled > 0)
6606 {
6607 /* Put the wildmenu just above the command line. If there is
6608 * no room, scroll the screen one line up. */
6609 if (cmdline_row == Rows - 1)
6610 {
6611 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6612 ++msg_scrolled;
6613 }
6614 else
6615 {
6616 ++cmdline_row;
6617 ++row;
6618 }
6619 wild_menu_showing = WM_SCROLLED;
6620 }
6621 else
6622 {
6623 /* Create status line if needed by setting 'laststatus' to 2.
6624 * Set 'winminheight' to zero to avoid that the window is
6625 * resized. */
6626 if (lastwin->w_status_height == 0)
6627 {
6628 save_p_ls = p_ls;
6629 save_p_wmh = p_wmh;
6630 p_ls = 2;
6631 p_wmh = 0;
6632 last_status(FALSE);
6633 }
6634 wild_menu_showing = WM_SHOWN;
6635 }
6636 }
6637
6638 screen_puts(buf, row, 0, attr);
6639 if (selstart != NULL && highlight)
6640 {
6641 *selend = NUL;
6642 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6643 }
6644
6645 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6646 }
6647
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006648#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 win_redraw_last_status(topframe);
6650#else
6651 lastwin->w_redr_status = TRUE;
6652#endif
6653 vim_free(buf);
6654}
6655#endif
6656
6657#if defined(FEAT_WINDOWS) || defined(PROTO)
6658/*
6659 * Redraw the status line of window wp.
6660 *
6661 * If inversion is possible we use it. Else '=' characters are used.
6662 */
6663 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006664win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 int row;
6667 char_u *p;
6668 int len;
6669 int fillchar;
6670 int attr;
6671 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006672 static int busy = FALSE;
6673
6674 /* It's possible to get here recursively when 'statusline' (indirectly)
6675 * invokes ":redrawstatus". Simply ignore the call then. */
6676 if (busy)
6677 return;
6678 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 wp->w_redr_status = FALSE;
6681 if (wp->w_status_height == 0)
6682 {
6683 /* no status line, can only be last window */
6684 redraw_cmdline = TRUE;
6685 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006686 else if (!redrawing()
6687#ifdef FEAT_INS_EXPAND
6688 /* don't update status line when popup menu is visible and may be
6689 * drawn over it */
6690 || pum_visible()
6691#endif
6692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
6694 /* Don't redraw right now, do it later. */
6695 wp->w_redr_status = TRUE;
6696 }
6697#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006698 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
6700 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006701 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
6703#endif
6704 else
6705 {
6706 fillchar = fillchar_status(&attr, wp == curwin);
6707
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006708 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 p = NameBuff;
6710 len = (int)STRLEN(p);
6711
6712 if (wp->w_buffer->b_help
6713#ifdef FEAT_QUICKFIX
6714 || wp->w_p_pvw
6715#endif
6716 || bufIsChanged(wp->w_buffer)
6717 || wp->w_buffer->b_p_ro)
6718 *(p + len++) = ' ';
6719 if (wp->w_buffer->b_help)
6720 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006721 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 len += (int)STRLEN(p + len);
6723 }
6724#ifdef FEAT_QUICKFIX
6725 if (wp->w_p_pvw)
6726 {
6727 STRCPY(p + len, _("[Preview]"));
6728 len += (int)STRLEN(p + len);
6729 }
6730#endif
6731 if (bufIsChanged(wp->w_buffer))
6732 {
6733 STRCPY(p + len, "[+]");
6734 len += 3;
6735 }
6736 if (wp->w_buffer->b_p_ro)
6737 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006738 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 len += 4;
6740 }
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6743 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6744 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6745 if (this_ru_col <= 1)
6746 {
6747 p = (char_u *)"<"; /* No room for file name! */
6748 len = 1;
6749 }
6750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751#ifdef FEAT_MBYTE
6752 if (has_mbyte)
6753 {
6754 int clen = 0, i;
6755
6756 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006757 clen = mb_string2cells(p, -1);
6758
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 /* Find first character that will fit.
6760 * Going from start to end is much faster for DBCS. */
6761 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006762 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 clen -= (*mb_ptr2cells)(p + i);
6764 len = clen;
6765 if (i > 0)
6766 {
6767 p = p + i - 1;
6768 *p = '<';
6769 ++len;
6770 }
6771
6772 }
6773 else
6774#endif
6775 if (len > this_ru_col - 1)
6776 {
6777 p += len - (this_ru_col - 1);
6778 *p = '<';
6779 len = this_ru_col - 1;
6780 }
6781
6782 row = W_WINROW(wp) + wp->w_height;
6783 screen_puts(p, row, W_WINCOL(wp), attr);
6784 screen_fill(row, row + 1, len + W_WINCOL(wp),
6785 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6786
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006787 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6789 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6790 - 1 + W_WINCOL(wp)), attr);
6791
6792#ifdef FEAT_CMDL_INFO
6793 win_redr_ruler(wp, TRUE);
6794#endif
6795 }
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /*
6798 * May need to draw the character below the vertical separator.
6799 */
6800 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6801 {
6802 if (stl_connected(wp))
6803 fillchar = fillchar_status(&attr, wp == curwin);
6804 else
6805 fillchar = fillchar_vsep(&attr);
6806 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6807 attr);
6808 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006809 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810}
6811
Bram Moolenaar238a5642006-02-21 22:12:05 +00006812#ifdef FEAT_STL_OPT
6813/*
6814 * Redraw the status line according to 'statusline' and take care of any
6815 * errors encountered.
6816 */
6817 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006818redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006819{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006820 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006821 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006822
6823 /* When called recursively return. This can happen when the statusline
6824 * contains an expression that triggers a redraw. */
6825 if (entered)
6826 return;
6827 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006828
Bram Moolenaara742e082016-04-05 21:10:38 +02006829 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006830 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006831 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006832 {
6833 /* When there is an error disable the statusline, otherwise the
6834 * display is messed up with errors and a redraw triggers the problem
6835 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006836 set_string_option_direct((char_u *)"statusline", -1,
6837 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006838 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006839 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006840 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006841 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006842}
6843#endif
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845/*
6846 * Return TRUE if the status line of window "wp" is connected to the status
6847 * line of the window right of it. If not, then it's a vertical separator.
6848 * Only call if (wp->w_vsep_width != 0).
6849 */
6850 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006851stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 frame_T *fr;
6854
6855 fr = wp->w_frame;
6856 while (fr->fr_parent != NULL)
6857 {
6858 if (fr->fr_parent->fr_layout == FR_COL)
6859 {
6860 if (fr->fr_next != NULL)
6861 break;
6862 }
6863 else
6864 {
6865 if (fr->fr_next != NULL)
6866 return TRUE;
6867 }
6868 fr = fr->fr_parent;
6869 }
6870 return FALSE;
6871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
6873#endif /* FEAT_WINDOWS */
6874
6875#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6876/*
6877 * Get the value to show for the language mappings, active 'keymap'.
6878 */
6879 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006880get_keymap_str(
6881 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006882 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006883 char_u *buf, /* buffer for the result */
6884 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 char_u *p;
6887
6888 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6889 return FALSE;
6890
6891 {
6892#ifdef FEAT_EVAL
6893 buf_T *old_curbuf = curbuf;
6894 win_T *old_curwin = curwin;
6895 char_u *s;
6896
6897 curbuf = wp->w_buffer;
6898 curwin = wp;
6899 STRCPY(buf, "b:keymap_name"); /* must be writable */
6900 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006901 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 --emsg_skip;
6903 curbuf = old_curbuf;
6904 curwin = old_curwin;
6905 if (p == NULL || *p == NUL)
6906#endif
6907 {
6908#ifdef FEAT_KEYMAP
6909 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6910 p = wp->w_buffer->b_p_keymap;
6911 else
6912#endif
6913 p = (char_u *)"lang";
6914 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006915 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 buf[0] = NUL;
6917#ifdef FEAT_EVAL
6918 vim_free(s);
6919#endif
6920 }
6921 return buf[0] != NUL;
6922}
6923#endif
6924
6925#if defined(FEAT_STL_OPT) || defined(PROTO)
6926/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006927 * Redraw the status line or ruler of window "wp".
6928 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 */
6930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006931win_redr_custom(
6932 win_T *wp,
6933 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006935 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 int attr;
6937 int curattr;
6938 int row;
6939 int col = 0;
6940 int maxwidth;
6941 int width;
6942 int n;
6943 int len;
6944 int fillchar;
6945 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006946 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006948 struct stl_hlrec hltab[STL_MAX_ITEM];
6949 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006950 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006951 win_T *ewp;
6952 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar1d633412013-12-11 15:52:01 +01006954 /* There is a tiny chance that this gets called recursively: When
6955 * redrawing a status line triggers redrawing the ruler or tabline.
6956 * Avoid trouble by not allowing recursion. */
6957 if (entered)
6958 return;
6959 entered = TRUE;
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006964 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006965 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006966 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006967 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968 attr = hl_attr(HLF_TPF);
6969 maxwidth = Columns;
6970# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006971 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006974 else
6975 {
6976 row = W_WINROW(wp) + wp->w_height;
6977 fillchar = fillchar_status(&attr, wp == curwin);
6978 maxwidth = W_WIDTH(wp);
6979
6980 if (draw_ruler)
6981 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006982 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006983 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006984 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006985 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006986 if (*++stl == '-')
6987 stl++;
6988 if (atoi((char *)stl))
6989 while (VIM_ISDIGIT(*stl))
6990 stl++;
6991 if (*stl++ != '(')
6992 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006993 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006994#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006995 col = ru_col - (Columns - W_WIDTH(wp));
6996 if (col < (W_WIDTH(wp) + 1) / 2)
6997 col = (W_WIDTH(wp) + 1) / 2;
6998#else
6999 col = ru_col;
7000 if (col > (Columns + 1) / 2)
7001 col = (Columns + 1) / 2;
7002#endif
7003 maxwidth = W_WIDTH(wp) - col;
7004#ifdef FEAT_WINDOWS
7005 if (!wp->w_status_height)
7006#endif
7007 {
7008 row = Rows - 1;
7009 --maxwidth; /* writing in last column may cause scrolling */
7010 fillchar = ' ';
7011 attr = 0;
7012 }
7013
7014# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007015 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007016# endif
7017 }
7018 else
7019 {
7020 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007021 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007022 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007023 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007024# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007025 use_sandbox = was_set_insecurely((char_u *)"statusline",
7026 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007027# endif
7028 }
7029
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007030#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007031 col += W_WINCOL(wp);
7032#endif
7033 }
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007036 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
Bram Moolenaar61452852011-02-01 18:01:11 +01007038 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7039 * the cursor away and back. */
7040 ewp = wp == NULL ? curwin : wp;
7041 p_crb_save = ewp->w_p_crb;
7042 ewp->w_p_crb = FALSE;
7043
Bram Moolenaar362f3562009-11-03 16:20:34 +00007044 /* Make a copy, because the statusline may include a function call that
7045 * might change the option value and free the memory. */
7046 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007047 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007048 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007051 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007053 /* Make all characters printable. */
7054 p = transstr(buf);
7055 if (p != NULL)
7056 {
7057 vim_strncpy(buf, p, sizeof(buf) - 1);
7058 vim_free(p);
7059 }
7060
7061 /* fill up with "fillchar" */
7062 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007063 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
7065#ifdef FEAT_MBYTE
7066 len += (*mb_char2bytes)(fillchar, buf + len);
7067#else
7068 buf[len++] = fillchar;
7069#endif
7070 ++width;
7071 }
7072 buf[len] = NUL;
7073
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007074 /*
7075 * Draw each snippet with the specified highlighting.
7076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 curattr = attr;
7078 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007079 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007081 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 screen_puts_len(p, len, row, col, curattr);
7083 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007084 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007086 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007088 else if (hltab[n].userhl < 0)
7089 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007091 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007092 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#endif
7094 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007095 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007098
7099 if (wp == NULL)
7100 {
7101 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7102 col = 0;
7103 len = 0;
7104 p = buf;
7105 fillchar = 0;
7106 for (n = 0; tabtab[n].start != NULL; n++)
7107 {
7108 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7109 while (col < len)
7110 TabPageIdxs[col++] = fillchar;
7111 p = tabtab[n].start;
7112 fillchar = tabtab[n].userhl;
7113 }
7114 while (col < Columns)
7115 TabPageIdxs[col++] = fillchar;
7116 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007117
7118theend:
7119 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120}
7121
7122#endif /* FEAT_STL_OPT */
7123
7124/*
7125 * Output a single character directly to the screen and update ScreenLines.
7126 */
7127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007128screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 char_u buf[MB_MAXBYTES + 1];
7131
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007132#ifdef FEAT_MBYTE
7133 if (has_mbyte)
7134 buf[(*mb_char2bytes)(c, buf)] = NUL;
7135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007137 {
7138 buf[0] = c;
7139 buf[1] = NUL;
7140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 screen_puts(buf, row, col, attr);
7142}
7143
7144/*
7145 * Get a single character directly from ScreenLines into "bytes[]".
7146 * Also return its attribute in *attrp;
7147 */
7148 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007149screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 unsigned off;
7152
7153 /* safety check */
7154 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7155 {
7156 off = LineOffset[row] + col;
7157 *attrp = ScreenAttrs[off];
7158 bytes[0] = ScreenLines[off];
7159 bytes[1] = NUL;
7160
7161#ifdef FEAT_MBYTE
7162 if (enc_utf8 && ScreenLinesUC[off] != 0)
7163 bytes[utfc_char2bytes(off, bytes)] = NUL;
7164 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7165 {
7166 bytes[0] = ScreenLines[off];
7167 bytes[1] = ScreenLines2[off];
7168 bytes[2] = NUL;
7169 }
7170 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7171 {
7172 bytes[1] = ScreenLines[off + 1];
7173 bytes[2] = NUL;
7174 }
7175#endif
7176 }
7177}
7178
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007179#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007180static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007181
7182/*
7183 * Return TRUE if composing characters for screen posn "off" differs from
7184 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007185 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007186 */
7187 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007188screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007189{
7190 int i;
7191
7192 for (i = 0; i < Screen_mco; ++i)
7193 {
7194 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7195 return TRUE;
7196 if (u8cc[i] == 0)
7197 break;
7198 }
7199 return FALSE;
7200}
7201#endif
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203/*
7204 * Put string '*text' on the screen at position 'row' and 'col', with
7205 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7206 * Note: only outputs within one row, message is truncated at screen boundary!
7207 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7208 */
7209 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007210screen_puts(
7211 char_u *text,
7212 int row,
7213 int col,
7214 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 screen_puts_len(text, -1, row, col, attr);
7217}
7218
7219/*
7220 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7221 * a NUL.
7222 */
7223 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007224screen_puts_len(
7225 char_u *text,
7226 int textlen,
7227 int row,
7228 int col,
7229 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 unsigned off;
7232 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007233 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 int c;
7235#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007236 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 int mbyte_blen = 1;
7238 int mbyte_cells = 1;
7239 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007240 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 int clear_next_cell = FALSE;
7242# ifdef FEAT_ARABIC
7243 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007244 int pc, nc, nc1;
7245 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246# endif
7247#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007248#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7249 int force_redraw_this;
7250 int force_redraw_next = FALSE;
7251#endif
7252 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7255 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007256 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
Bram Moolenaarc236c162008-07-13 17:41:49 +00007258#ifdef FEAT_MBYTE
7259 /* When drawing over the right halve of a double-wide char clear out the
7260 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007261 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007262# ifdef FEAT_GUI
7263 && !gui.in_use
7264# endif
7265 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007266 {
7267 ScreenLines[off - 1] = ' ';
7268 ScreenAttrs[off - 1] = 0;
7269 if (enc_utf8)
7270 {
7271 ScreenLinesUC[off - 1] = 0;
7272 ScreenLinesC[0][off - 1] = 0;
7273 }
7274 /* redraw the previous cell, make it empty */
7275 screen_char(off - 1, row, col - 1);
7276 /* force the cell at "col" to be redrawn */
7277 force_redraw_next = TRUE;
7278 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007279#endif
7280
Bram Moolenaar367329b2007-08-30 11:53:22 +00007281#ifdef FEAT_MBYTE
7282 max_off = LineOffset[row] + screen_Columns;
7283#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007284 while (col < screen_Columns
7285 && (len < 0 || (int)(ptr - text) < len)
7286 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 {
7288 c = *ptr;
7289#ifdef FEAT_MBYTE
7290 /* check if this is the first byte of a multibyte */
7291 if (has_mbyte)
7292 {
7293 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007294 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007296 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7298 mbyte_cells = 1;
7299 else if (enc_dbcs != 0)
7300 mbyte_cells = mbyte_blen;
7301 else /* enc_utf8 */
7302 {
7303 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007304 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 (int)((text + len) - ptr));
7306 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007307 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007309# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 /* Non-BMP character: display as ? or fullwidth ?. */
7311 if (u8c >= 0x10000)
7312 {
7313 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7314 if (attr == 0)
7315 attr = hl_attr(HLF_8);
7316 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318# ifdef FEAT_ARABIC
7319 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7320 {
7321 /* Do Arabic shaping. */
7322 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7323 {
7324 /* Past end of string to be displayed. */
7325 nc = NUL;
7326 nc1 = NUL;
7327 }
7328 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007329 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007330 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7331 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007332 nc1 = pcc[0];
7333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 pc = prev_c;
7335 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007336 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 }
7338 else
7339 prev_c = u8c;
7340# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007341 if (col + mbyte_cells > screen_Columns)
7342 {
7343 /* Only 1 cell left, but character requires 2 cells:
7344 * display a '>' in the last column to avoid wrapping. */
7345 c = '>';
7346 mbyte_cells = 1;
7347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349 }
7350#endif
7351
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007352#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7353 force_redraw_this = force_redraw_next;
7354 force_redraw_next = FALSE;
7355#endif
7356
7357 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358#ifdef FEAT_MBYTE
7359 || (mbyte_cells == 2
7360 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7361 || (enc_dbcs == DBCS_JPNU
7362 && c == 0x8e
7363 && ScreenLines2[off] != ptr[1])
7364 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007365 && (ScreenLinesUC[off] !=
7366 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7367 || (ScreenLinesUC[off] != 0
7368 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007371 || exmode_active;
7372
7373 if (need_redraw
7374#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7375 || force_redraw_this
7376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 )
7378 {
7379#if defined(FEAT_GUI) || defined(UNIX)
7380 /* The bold trick makes a single row of pixels appear in the next
7381 * character. When a bold character is removed, the next
7382 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007383 * and for some xterms. */
7384 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385# ifdef FEAT_GUI
7386 gui.in_use
7387# endif
7388# if defined(FEAT_GUI) && defined(UNIX)
7389 ||
7390# endif
7391# ifdef UNIX
7392 term_is_xterm
7393# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007394 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007396 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007398 if (n > HL_ALL)
7399 n = syn_attr2attr(n);
7400 if (n & HL_BOLD)
7401 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403#endif
7404#ifdef FEAT_MBYTE
7405 /* When at the end of the text and overwriting a two-cell
7406 * character with a one-cell character, need to clear the next
7407 * cell. Also when overwriting the left halve of a two-cell char
7408 * with the right halve of a two-cell char. Do this only once
7409 * (mb_off2cells() may return 2 on the right halve). */
7410 if (clear_next_cell)
7411 clear_next_cell = FALSE;
7412 else if (has_mbyte
7413 && (len < 0 ? ptr[mbyte_blen] == NUL
7414 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007415 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007417 && (*mb_off2cells)(off, max_off) == 1
7418 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 clear_next_cell = TRUE;
7420
7421 /* Make sure we never leave a second byte of a double-byte behind,
7422 * it confuses mb_off2cells(). */
7423 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007424 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007426 && (*mb_off2cells)(off, max_off) == 1
7427 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 ScreenLines[off + mbyte_blen] = 0;
7429#endif
7430 ScreenLines[off] = c;
7431 ScreenAttrs[off] = attr;
7432#ifdef FEAT_MBYTE
7433 if (enc_utf8)
7434 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007435 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 ScreenLinesUC[off] = 0;
7437 else
7438 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007439 int i;
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007442 for (i = 0; i < Screen_mco; ++i)
7443 {
7444 ScreenLinesC[i][off] = u8cc[i];
7445 if (u8cc[i] == 0)
7446 break;
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449 if (mbyte_cells == 2)
7450 {
7451 ScreenLines[off + 1] = 0;
7452 ScreenAttrs[off + 1] = attr;
7453 }
7454 screen_char(off, row, col);
7455 }
7456 else if (mbyte_cells == 2)
7457 {
7458 ScreenLines[off + 1] = ptr[1];
7459 ScreenAttrs[off + 1] = attr;
7460 screen_char_2(off, row, col);
7461 }
7462 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7463 {
7464 ScreenLines2[off] = ptr[1];
7465 screen_char(off, row, col);
7466 }
7467 else
7468#endif
7469 screen_char(off, row, col);
7470 }
7471#ifdef FEAT_MBYTE
7472 if (has_mbyte)
7473 {
7474 off += mbyte_cells;
7475 col += mbyte_cells;
7476 ptr += mbyte_blen;
7477 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007478 {
7479 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007481 len = -1;
7482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 else
7485#endif
7486 {
7487 ++off;
7488 ++col;
7489 ++ptr;
7490 }
7491 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007492
7493#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7494 /* If we detected the next character needs to be redrawn, but the text
7495 * doesn't extend up to there, update the character here. */
7496 if (force_redraw_next && col < screen_Columns)
7497 {
7498# ifdef FEAT_MBYTE
7499 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7500 screen_char_2(off, row, col);
7501 else
7502# endif
7503 screen_char(off, row, col);
7504 }
7505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506}
7507
7508#ifdef FEAT_SEARCH_EXTRA
7509/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007510 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
7512 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007513start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 if (p_hls && !no_hlsearch)
7516 {
7517 last_pat_prog(&search_hl.rm);
7518 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007519# ifdef FEAT_RELTIME
7520 /* Set the time limit to 'redrawtime'. */
7521 profile_setlimit(p_rdt, &search_hl.tm);
7522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524}
7525
7526/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007527 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 */
7529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007530end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 if (search_hl.rm.regprog != NULL)
7533 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007534 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 search_hl.rm.regprog = NULL;
7536 }
7537}
7538
7539/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007540 * Init for calling prepare_search_hl().
7541 */
7542 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007543init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007544{
7545 matchitem_T *cur;
7546
7547 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7548 * match */
7549 cur = wp->w_match_head;
7550 while (cur != NULL)
7551 {
7552 cur->hl.rm = cur->match;
7553 if (cur->hlg_id == 0)
7554 cur->hl.attr = 0;
7555 else
7556 cur->hl.attr = syn_id2attr(cur->hlg_id);
7557 cur->hl.buf = wp->w_buffer;
7558 cur->hl.lnum = 0;
7559 cur->hl.first_lnum = 0;
7560# ifdef FEAT_RELTIME
7561 /* Set the time limit to 'redrawtime'. */
7562 profile_setlimit(p_rdt, &(cur->hl.tm));
7563# endif
7564 cur = cur->next;
7565 }
7566 search_hl.buf = wp->w_buffer;
7567 search_hl.lnum = 0;
7568 search_hl.first_lnum = 0;
7569 /* time limit is set at the toplevel, for all windows */
7570}
7571
7572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 * Advance to the match in window "wp" line "lnum" or past it.
7574 */
7575 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007576prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007578 matchitem_T *cur; /* points to the match list */
7579 match_T *shl; /* points to search_hl or a match */
7580 int shl_flag; /* flag to indicate whether search_hl
7581 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007582 int pos_inprogress; /* marks that position match search is
7583 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 int n;
7585
7586 /*
7587 * When using a multi-line pattern, start searching at the top
7588 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007589 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007591 cur = wp->w_match_head;
7592 shl_flag = FALSE;
7593 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007595 if (shl_flag == FALSE)
7596 {
7597 shl = &search_hl;
7598 shl_flag = TRUE;
7599 }
7600 else
7601 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (shl->rm.regprog != NULL
7603 && shl->lnum == 0
7604 && re_multiline(shl->rm.regprog))
7605 {
7606 if (shl->first_lnum == 0)
7607 {
7608# ifdef FEAT_FOLDING
7609 for (shl->first_lnum = lnum;
7610 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7611 if (hasFoldingWin(wp, shl->first_lnum - 1,
7612 NULL, NULL, TRUE, NULL))
7613 break;
7614# else
7615 shl->first_lnum = wp->w_topline;
7616# endif
7617 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007618 if (cur != NULL)
7619 cur->pos.cur = 0;
7620 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007622 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7623 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007625 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7626 pos_inprogress = cur == NULL || cur->pos.cur == 0
7627 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (shl->lnum != 0)
7629 {
7630 shl->first_lnum = shl->lnum
7631 + shl->rm.endpos[0].lnum
7632 - shl->rm.startpos[0].lnum;
7633 n = shl->rm.endpos[0].col;
7634 }
7635 else
7636 {
7637 ++shl->first_lnum;
7638 n = 0;
7639 }
7640 }
7641 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007642 if (shl != &search_hl && cur != NULL)
7643 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645}
7646
7647/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007648 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 * Uses shl->buf.
7650 * Sets shl->lnum and shl->rm contents.
7651 * Note: Assumes a previous match is always before "lnum", unless
7652 * shl->lnum is zero.
7653 * Careful: Any pointers for buffer lines will become invalid.
7654 */
7655 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007656next_search_hl(
7657 win_T *win,
7658 match_T *shl, /* points to search_hl or a match */
7659 linenr_T lnum,
7660 colnr_T mincol, /* minimal column for a match */
7661 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 linenr_T l;
7664 colnr_T matchcol;
7665 long nmatched;
7666
7667 if (shl->lnum != 0)
7668 {
7669 /* Check for three situations:
7670 * 1. If the "lnum" is below a previous match, start a new search.
7671 * 2. If the previous match includes "mincol", use it.
7672 * 3. Continue after the previous match.
7673 */
7674 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7675 if (lnum > l)
7676 shl->lnum = 0;
7677 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7678 return;
7679 }
7680
7681 /*
7682 * Repeat searching for a match until one is found that includes "mincol"
7683 * or none is found in this line.
7684 */
7685 called_emsg = FALSE;
7686 for (;;)
7687 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007688#ifdef FEAT_RELTIME
7689 /* Stop searching after passing the time limit. */
7690 if (profile_passed_limit(&(shl->tm)))
7691 {
7692 shl->lnum = 0; /* no match found in time */
7693 break;
7694 }
7695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 /* Three situations:
7697 * 1. No useful previous match: search from start of line.
7698 * 2. Not Vi compatible or empty match: continue at next character.
7699 * Break the loop if this is beyond the end of the line.
7700 * 3. Vi compatible searching: continue at end of previous match.
7701 */
7702 if (shl->lnum == 0)
7703 matchcol = 0;
7704 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7705 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007706 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007708 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007709
7710 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007711 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007712 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007714 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 shl->lnum = 0;
7716 break;
7717 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007718#ifdef FEAT_MBYTE
7719 if (has_mbyte)
7720 matchcol += mb_ptr2len(ml);
7721 else
7722#endif
7723 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725 else
7726 matchcol = shl->rm.endpos[0].col;
7727
7728 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007731 /* Remember whether shl->rm is using a copy of the regprog in
7732 * cur->match. */
7733 int regprog_is_copy = (shl != &search_hl && cur != NULL
7734 && shl == &cur->hl
7735 && cur->match.regprog == cur->hl.rm.regprog);
7736
Bram Moolenaarb3414592014-06-17 17:48:32 +02007737 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7738 matchcol,
7739#ifdef FEAT_RELTIME
7740 &(shl->tm)
7741#else
7742 NULL
7743#endif
7744 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007745 /* Copy the regprog, in case it got freed and recompiled. */
7746 if (regprog_is_copy)
7747 cur->match.regprog = cur->hl.rm.regprog;
7748
Bram Moolenaarb3414592014-06-17 17:48:32 +02007749 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007750 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007751 /* Error while handling regexp: stop using this regexp. */
7752 if (shl == &search_hl)
7753 {
7754 /* don't free regprog in the match list, it's a copy */
7755 vim_regfree(shl->rm.regprog);
7756 SET_NO_HLSEARCH(TRUE);
7757 }
7758 shl->rm.regprog = NULL;
7759 shl->lnum = 0;
7760 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7761 message */
7762 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007763 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007764 }
7765 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007766 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007767 else
7768 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 if (nmatched == 0)
7770 {
7771 shl->lnum = 0; /* no match found */
7772 break;
7773 }
7774 if (shl->rm.startpos[0].lnum > 0
7775 || shl->rm.startpos[0].col >= mincol
7776 || nmatched > 1
7777 || shl->rm.endpos[0].col > mincol)
7778 {
7779 shl->lnum += shl->rm.startpos[0].lnum;
7780 break; /* useful match found */
7781 }
7782 }
7783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaarb3414592014-06-17 17:48:32 +02007785 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007786next_search_hl_pos(
7787 match_T *shl, /* points to a match */
7788 linenr_T lnum,
7789 posmatch_T *posmatch, /* match positions */
7790 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007791{
7792 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007793 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007794
7795 shl->lnum = 0;
7796 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7797 {
7798 if (posmatch->pos[i].lnum == 0)
7799 break;
7800 if (posmatch->pos[i].col < mincol)
7801 continue;
7802 if (posmatch->pos[i].lnum == lnum)
7803 {
7804 if (shl->lnum == lnum)
7805 {
7806 /* partially sort positions by column numbers
7807 * on the same line */
7808 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7809 {
7810 llpos_T tmp = posmatch->pos[i];
7811
7812 posmatch->pos[i] = posmatch->pos[bot];
7813 posmatch->pos[bot] = tmp;
7814 }
7815 }
7816 else
7817 {
7818 bot = i;
7819 shl->lnum = lnum;
7820 }
7821 }
7822 }
7823 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007824 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007825 {
7826 colnr_T start = posmatch->pos[bot].col == 0
7827 ? 0 : posmatch->pos[bot].col - 1;
7828 colnr_T end = posmatch->pos[bot].col == 0
7829 ? MAXCOL : start + posmatch->pos[bot].len;
7830
7831 shl->rm.startpos[0].lnum = 0;
7832 shl->rm.startpos[0].col = start;
7833 shl->rm.endpos[0].lnum = 0;
7834 shl->rm.endpos[0].col = end;
7835 posmatch->cur = bot + 1;
7836 return TRUE;
7837 }
7838 return FALSE;
7839}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007840#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007843screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 attrentry_T *aep = NULL;
7846
7847 screen_attr = attr;
7848 if (full_screen
7849#ifdef WIN3264
7850 && termcap_active
7851#endif
7852 )
7853 {
7854#ifdef FEAT_GUI
7855 if (gui.in_use)
7856 {
7857 char buf[20];
7858
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007859 /* The GUI handles this internally. */
7860 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 OUT_STR(buf);
7862 }
7863 else
7864#endif
7865 {
7866 if (attr > HL_ALL) /* special HL attr. */
7867 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007868 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 aep = syn_cterm_attr2entry(attr);
7870 else
7871 aep = syn_term_attr2entry(attr);
7872 if (aep == NULL) /* did ":syntax clear" */
7873 attr = 0;
7874 else
7875 attr = aep->ae_attr;
7876 }
7877 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7878 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007879 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007880#ifdef FEAT_TERMGUICOLORS
7881 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007882 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007883#endif
7884 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007885#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007886 )
7887#endif
7888 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007889 /* If the Normal FG color has BOLD attribute and the new HL
7890 * has a FG color defined, clear BOLD. */
7891 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7893 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007894 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7895 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 out_str(T_US);
7897 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7898 out_str(T_CZH);
7899 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7900 out_str(T_MR);
7901
7902 /*
7903 * Output the color or start string after bold etc., in case the
7904 * bold etc. override the color setting.
7905 */
7906 if (aep != NULL)
7907 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007908#ifdef FEAT_TERMGUICOLORS
7909 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007911 if (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007912 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007913 if (aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007914 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 }
7916 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007919 if (t_colors > 1)
7920 {
7921 if (aep->ae_u.cterm.fg_color)
7922 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7923 if (aep->ae_u.cterm.bg_color)
7924 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7925 }
7926 else
7927 {
7928 if (aep->ae_u.term.start != NULL)
7929 out_str(aep->ae_u.term.start);
7930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 }
7932 }
7933 }
7934 }
7935}
7936
7937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007938screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 int do_ME = FALSE; /* output T_ME code */
7941
7942 if (screen_attr != 0
7943#ifdef WIN3264
7944 && termcap_active
7945#endif
7946 )
7947 {
7948#ifdef FEAT_GUI
7949 if (gui.in_use)
7950 {
7951 char buf[20];
7952
7953 /* use internal GUI code */
7954 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7955 OUT_STR(buf);
7956 }
7957 else
7958#endif
7959 {
7960 if (screen_attr > HL_ALL) /* special HL attr. */
7961 {
7962 attrentry_T *aep;
7963
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007964 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {
7966 /*
7967 * Assume that t_me restores the original colors!
7968 */
7969 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007970 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007971#ifdef FEAT_TERMGUICOLORS
7972 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007973 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR ||
7974 aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007975#endif
7976 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007977#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007978 )
7979#endif
7980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 do_ME = TRUE;
7982 }
7983 else
7984 {
7985 aep = syn_term_attr2entry(screen_attr);
7986 if (aep != NULL && aep->ae_u.term.stop != NULL)
7987 {
7988 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7989 do_ME = TRUE;
7990 else
7991 out_str(aep->ae_u.term.stop);
7992 }
7993 }
7994 if (aep == NULL) /* did ":syntax clear" */
7995 screen_attr = 0;
7996 else
7997 screen_attr = aep->ae_attr;
7998 }
7999
8000 /*
8001 * Often all ending-codes are equal to T_ME. Avoid outputting the
8002 * same sequence several times.
8003 */
8004 if (screen_attr & HL_STANDOUT)
8005 {
8006 if (STRCMP(T_SE, T_ME) == 0)
8007 do_ME = TRUE;
8008 else
8009 out_str(T_SE);
8010 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008011 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {
8013 if (STRCMP(T_UE, T_ME) == 0)
8014 do_ME = TRUE;
8015 else
8016 out_str(T_UE);
8017 }
8018 if (screen_attr & HL_ITALIC)
8019 {
8020 if (STRCMP(T_CZR, T_ME) == 0)
8021 do_ME = TRUE;
8022 else
8023 out_str(T_CZR);
8024 }
8025 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8026 out_str(T_ME);
8027
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008028#ifdef FEAT_TERMGUICOLORS
8029 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02008031 if (cterm_normal_fg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar902647d2016-04-22 11:49:06 +02008033 if (cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008034 term_bg_rgb_color(cterm_normal_bg_gui_color);
8035 }
8036 else
8037#endif
8038 {
8039 if (t_colors > 1)
8040 {
8041 /* set Normal cterm colors */
8042 if (cterm_normal_fg_color != 0)
8043 term_fg_color(cterm_normal_fg_color - 1);
8044 if (cterm_normal_bg_color != 0)
8045 term_bg_color(cterm_normal_bg_color - 1);
8046 if (cterm_normal_fg_bold)
8047 out_str(T_MD);
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050 }
8051 }
8052 screen_attr = 0;
8053}
8054
8055/*
8056 * Reset the colors for a cterm. Used when leaving Vim.
8057 * The machine specific code may override this again.
8058 */
8059 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008060reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008062 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {
8064 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008065#ifdef FEAT_TERMGUICOLORS
8066 if (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02008067 (cterm_normal_fg_gui_color != (long_u)INVALCOLOR
8068 || cterm_normal_bg_gui_color != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008069 (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
8070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
8074 out_str(T_OP);
8075 screen_attr = -1;
8076 }
8077 if (cterm_normal_fg_bold)
8078 {
8079 out_str(T_ME);
8080 screen_attr = -1;
8081 }
8082 }
8083}
8084
8085/*
8086 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8087 * using the attributes from ScreenAttrs["off"].
8088 */
8089 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008090screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 int attr;
8093
8094 /* Check for illegal values, just in case (could happen just after
8095 * resizing). */
8096 if (row >= screen_Rows || col >= screen_Columns)
8097 return;
8098
Bram Moolenaar494838a2015-02-10 19:20:37 +01008099 /* Outputting a character in the last cell on the screen may scroll the
8100 * screen up. Only do it when the "xn" termcap property is set, otherwise
8101 * mark the character invalid (update it when scrolled up). */
8102 if (*T_XN == NUL
8103 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#ifdef FEAT_RIGHTLEFT
8105 /* account for first command-line character in rightleft mode */
8106 && !cmdmsg_rl
8107#endif
8108 )
8109 {
8110 ScreenAttrs[off] = (sattr_T)-1;
8111 return;
8112 }
8113
8114 /*
8115 * Stop highlighting first, so it's easier to move the cursor.
8116 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008117#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (screen_char_attr != 0)
8119 attr = screen_char_attr;
8120 else
8121#endif
8122 attr = ScreenAttrs[off];
8123 if (screen_attr != attr)
8124 screen_stop_highlight();
8125
8126 windgoto(row, col);
8127
8128 if (screen_attr != attr)
8129 screen_start_highlight(attr);
8130
8131#ifdef FEAT_MBYTE
8132 if (enc_utf8 && ScreenLinesUC[off] != 0)
8133 {
8134 char_u buf[MB_MAXBYTES + 1];
8135
8136 /* Convert UTF-8 character to bytes and write it. */
8137
8138 buf[utfc_char2bytes(off, buf)] = NUL;
8139
8140 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008141 if (utf_ambiguous_width(ScreenLinesUC[off]))
8142 screen_cur_col = 9999;
8143 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 ++screen_cur_col;
8145 }
8146 else
8147#endif
8148 {
8149#ifdef FEAT_MBYTE
8150 out_flush_check();
8151#endif
8152 out_char(ScreenLines[off]);
8153#ifdef FEAT_MBYTE
8154 /* double-byte character in single-width cell */
8155 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8156 out_char(ScreenLines2[off]);
8157#endif
8158 }
8159
8160 screen_cur_col++;
8161}
8162
8163#ifdef FEAT_MBYTE
8164
8165/*
8166 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8167 * on the screen at position 'row' and 'col'.
8168 * The attributes of the first byte is used for all. This is required to
8169 * output the two bytes of a double-byte character with nothing in between.
8170 */
8171 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008172screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 /* Check for illegal values (could be wrong when screen was resized). */
8175 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8176 return;
8177
8178 /* Outputting the last character on the screen may scrollup the screen.
8179 * Don't to it! Mark the character invalid (update it when scrolled up) */
8180 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8181 {
8182 ScreenAttrs[off] = (sattr_T)-1;
8183 return;
8184 }
8185
8186 /* Output the first byte normally (positions the cursor), then write the
8187 * second byte directly. */
8188 screen_char(off, row, col);
8189 out_char(ScreenLines[off + 1]);
8190 ++screen_cur_col;
8191}
8192#endif
8193
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008194#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195/*
8196 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8197 * This uses the contents of ScreenLines[] and doesn't change it.
8198 */
8199 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008200screen_draw_rectangle(
8201 int row,
8202 int col,
8203 int height,
8204 int width,
8205 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 int r, c;
8208 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008209#ifdef FEAT_MBYTE
8210 int max_off;
8211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008213 /* Can't use ScreenLines unless initialized */
8214 if (ScreenLines == NULL)
8215 return;
8216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (invert)
8218 screen_char_attr = HL_INVERSE;
8219 for (r = row; r < row + height; ++r)
8220 {
8221 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008222#ifdef FEAT_MBYTE
8223 max_off = off + screen_Columns;
8224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 for (c = col; c < col + width; ++c)
8226 {
8227#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008228 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {
8230 screen_char_2(off + c, r, c);
8231 ++c;
8232 }
8233 else
8234#endif
8235 {
8236 screen_char(off + c, r, c);
8237#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008238 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 ++c;
8240#endif
8241 }
8242 }
8243 }
8244 screen_char_attr = 0;
8245}
8246#endif
8247
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008248#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249/*
8250 * Redraw the characters for a vertically split window.
8251 */
8252 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008253redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254{
8255 int col;
8256 int width;
8257
8258# ifdef FEAT_CLIPBOARD
8259 clip_may_clear_selection(row, end - 1);
8260# endif
8261
8262 if (wp == NULL)
8263 {
8264 col = 0;
8265 width = Columns;
8266 }
8267 else
8268 {
8269 col = wp->w_wincol;
8270 width = wp->w_width;
8271 }
8272 screen_draw_rectangle(row, col, end - row, width, FALSE);
8273}
8274#endif
8275
8276/*
8277 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8278 * with character 'c1' in first column followed by 'c2' in the other columns.
8279 * Use attributes 'attr'.
8280 */
8281 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008282screen_fill(
8283 int start_row,
8284 int end_row,
8285 int start_col,
8286 int end_col,
8287 int c1,
8288 int c2,
8289 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 int row;
8292 int col;
8293 int off;
8294 int end_off;
8295 int did_delete;
8296 int c;
8297 int norm_term;
8298#if defined(FEAT_GUI) || defined(UNIX)
8299 int force_next = FALSE;
8300#endif
8301
8302 if (end_row > screen_Rows) /* safety check */
8303 end_row = screen_Rows;
8304 if (end_col > screen_Columns) /* safety check */
8305 end_col = screen_Columns;
8306 if (ScreenLines == NULL
8307 || start_row >= end_row
8308 || start_col >= end_col) /* nothing to do */
8309 return;
8310
8311 /* it's a "normal" terminal when not in a GUI or cterm */
8312 norm_term = (
8313#ifdef FEAT_GUI
8314 !gui.in_use &&
8315#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008316 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 for (row = start_row; row < end_row; ++row)
8318 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008319#ifdef FEAT_MBYTE
8320 if (has_mbyte
8321# ifdef FEAT_GUI
8322 && !gui.in_use
8323# endif
8324 )
8325 {
8326 /* When drawing over the right halve of a double-wide char clear
8327 * out the left halve. When drawing over the left halve of a
8328 * double wide-char clear out the right halve. Only needed in a
8329 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008330 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008331 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008332 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008333 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008334 }
8335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 /*
8337 * Try to use delete-line termcap code, when no attributes or in a
8338 * "normal" terminal, where a bold/italic space is just a
8339 * space.
8340 */
8341 did_delete = FALSE;
8342 if (c2 == ' '
8343 && end_col == Columns
8344 && can_clear(T_CE)
8345 && (attr == 0
8346 || (norm_term
8347 && attr <= HL_ALL
8348 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8349 {
8350 /*
8351 * check if we really need to clear something
8352 */
8353 col = start_col;
8354 if (c1 != ' ') /* don't clear first char */
8355 ++col;
8356
8357 off = LineOffset[row] + col;
8358 end_off = LineOffset[row] + end_col;
8359
8360 /* skip blanks (used often, keep it fast!) */
8361#ifdef FEAT_MBYTE
8362 if (enc_utf8)
8363 while (off < end_off && ScreenLines[off] == ' '
8364 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8365 ++off;
8366 else
8367#endif
8368 while (off < end_off && ScreenLines[off] == ' '
8369 && ScreenAttrs[off] == 0)
8370 ++off;
8371 if (off < end_off) /* something to be cleared */
8372 {
8373 col = off - LineOffset[row];
8374 screen_stop_highlight();
8375 term_windgoto(row, col);/* clear rest of this screen line */
8376 out_str(T_CE);
8377 screen_start(); /* don't know where cursor is now */
8378 col = end_col - col;
8379 while (col--) /* clear chars in ScreenLines */
8380 {
8381 ScreenLines[off] = ' ';
8382#ifdef FEAT_MBYTE
8383 if (enc_utf8)
8384 ScreenLinesUC[off] = 0;
8385#endif
8386 ScreenAttrs[off] = 0;
8387 ++off;
8388 }
8389 }
8390 did_delete = TRUE; /* the chars are cleared now */
8391 }
8392
8393 off = LineOffset[row] + start_col;
8394 c = c1;
8395 for (col = start_col; col < end_col; ++col)
8396 {
8397 if (ScreenLines[off] != c
8398#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008399 || (enc_utf8 && (int)ScreenLinesUC[off]
8400 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401#endif
8402 || ScreenAttrs[off] != attr
8403#if defined(FEAT_GUI) || defined(UNIX)
8404 || force_next
8405#endif
8406 )
8407 {
8408#if defined(FEAT_GUI) || defined(UNIX)
8409 /* The bold trick may make a single row of pixels appear in
8410 * the next character. When a bold character is removed, the
8411 * next character should be redrawn too. This happens for our
8412 * own GUI and for some xterms. */
8413 if (
8414# ifdef FEAT_GUI
8415 gui.in_use
8416# endif
8417# if defined(FEAT_GUI) && defined(UNIX)
8418 ||
8419# endif
8420# ifdef UNIX
8421 term_is_xterm
8422# endif
8423 )
8424 {
8425 if (ScreenLines[off] != ' '
8426 && (ScreenAttrs[off] > HL_ALL
8427 || ScreenAttrs[off] & HL_BOLD))
8428 force_next = TRUE;
8429 else
8430 force_next = FALSE;
8431 }
8432#endif
8433 ScreenLines[off] = c;
8434#ifdef FEAT_MBYTE
8435 if (enc_utf8)
8436 {
8437 if (c >= 0x80)
8438 {
8439 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008440 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
8442 else
8443 ScreenLinesUC[off] = 0;
8444 }
8445#endif
8446 ScreenAttrs[off] = attr;
8447 if (!did_delete || c != ' ')
8448 screen_char(off, row, col);
8449 }
8450 ++off;
8451 if (col == start_col)
8452 {
8453 if (did_delete)
8454 break;
8455 c = c2;
8456 }
8457 }
8458 if (end_col == Columns)
8459 LineWraps[row] = FALSE;
8460 if (row == Rows - 1) /* overwritten the command line */
8461 {
8462 redraw_cmdline = TRUE;
8463 if (c1 == ' ' && c2 == ' ')
8464 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008465 if (start_col == 0)
8466 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468 }
8469}
8470
8471/*
8472 * Check if there should be a delay. Used before clearing or redrawing the
8473 * screen or the command line.
8474 */
8475 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008476check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8479 && !did_wait_return
8480 && emsg_silent == 0)
8481 {
8482 out_flush();
8483 ui_delay(1000L, TRUE);
8484 emsg_on_display = FALSE;
8485 if (check_msg_scroll)
8486 msg_scroll = FALSE;
8487 }
8488}
8489
8490/*
8491 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008492 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 * Returns TRUE if there is a valid screen to write to.
8494 * Returns FALSE when starting up and screen not initialized yet.
8495 */
8496 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008497screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008499 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 return (ScreenLines != NULL);
8501}
8502
8503/*
8504 * Resize the shell to Rows and Columns.
8505 * Allocate ScreenLines[] and associated items.
8506 *
8507 * There may be some time between setting Rows and Columns and (re)allocating
8508 * ScreenLines[]. This happens when starting up and when (manually) changing
8509 * the shell size. Always use screen_Rows and screen_Columns to access items
8510 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8511 * final size of the shell is needed.
8512 */
8513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008514screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
8516 int new_row, old_row;
8517#ifdef FEAT_GUI
8518 int old_Rows;
8519#endif
8520 win_T *wp;
8521 int outofmem = FALSE;
8522 int len;
8523 schar_T *new_ScreenLines;
8524#ifdef FEAT_MBYTE
8525 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008526 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008528 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529#endif
8530 sattr_T *new_ScreenAttrs;
8531 unsigned *new_LineOffset;
8532 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008533#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008534 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008535 tabpage_T *tp;
8536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008538 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008539#ifdef FEAT_AUTOCMD
8540 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008542retry:
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 /*
8545 * Allocation of the screen buffers is done only when the size changes and
8546 * when Rows and Columns have been set and we have started doing full
8547 * screen stuff.
8548 */
8549 if ((ScreenLines != NULL
8550 && Rows == screen_Rows
8551 && Columns == screen_Columns
8552#ifdef FEAT_MBYTE
8553 && enc_utf8 == (ScreenLinesUC != NULL)
8554 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008555 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556#endif
8557 )
8558 || Rows == 0
8559 || Columns == 0
8560 || (!full_screen && ScreenLines == NULL))
8561 return;
8562
8563 /*
8564 * It's possible that we produce an out-of-memory message below, which
8565 * will cause this function to be called again. To break the loop, just
8566 * return here.
8567 */
8568 if (entered)
8569 return;
8570 entered = TRUE;
8571
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008572 /*
8573 * Note that the window sizes are updated before reallocating the arrays,
8574 * thus we must not redraw here!
8575 */
8576 ++RedrawingDisabled;
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 win_new_shellsize(); /* fit the windows in the new sized shell */
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 comp_col(); /* recompute columns for shown command and ruler */
8581
8582 /*
8583 * We're changing the size of the screen.
8584 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8585 * - Move lines from the old arrays into the new arrays, clear extra
8586 * lines (unless the screen is going to be cleared).
8587 * - Free the old arrays.
8588 *
8589 * If anything fails, make ScreenLines NULL, so we don't do anything!
8590 * Continuing with the old ScreenLines may result in a crash, because the
8591 * size is wrong.
8592 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008593 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008595#ifdef FEAT_AUTOCMD
8596 if (aucmd_win != NULL)
8597 win_free_lsize(aucmd_win);
8598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 new_ScreenLines = (schar_T *)lalloc((long_u)(
8601 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8602#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008603 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (enc_utf8)
8605 {
8606 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8607 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008608 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008609 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8611 }
8612 if (enc_dbcs == DBCS_JPNU)
8613 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8614 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8615#endif
8616 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8617 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8618 new_LineOffset = (unsigned *)lalloc((long_u)(
8619 Rows * sizeof(unsigned)), FALSE);
8620 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008621#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008622 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008625 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
8627 if (win_alloc_lines(wp) == FAIL)
8628 {
8629 outofmem = TRUE;
8630#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008631 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#endif
8633 }
8634 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008635#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008636 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8637 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008638 outofmem = TRUE;
8639#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008640#ifdef FEAT_WINDOWS
8641give_up:
8642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008644#ifdef FEAT_MBYTE
8645 for (i = 0; i < p_mco; ++i)
8646 if (new_ScreenLinesC[i] == NULL)
8647 break;
8648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 if (new_ScreenLines == NULL
8650#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8653#endif
8654 || new_ScreenAttrs == NULL
8655 || new_LineOffset == NULL
8656 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008657#ifdef FEAT_WINDOWS
8658 || new_TabPageIdxs == NULL
8659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 || outofmem)
8661 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008662 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008663 {
8664 /* guess the size */
8665 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8666
8667 /* Remember we did this to avoid getting outofmem messages over
8668 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008669 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 vim_free(new_ScreenLines);
8672 new_ScreenLines = NULL;
8673#ifdef FEAT_MBYTE
8674 vim_free(new_ScreenLinesUC);
8675 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008676 for (i = 0; i < p_mco; ++i)
8677 {
8678 vim_free(new_ScreenLinesC[i]);
8679 new_ScreenLinesC[i] = NULL;
8680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 vim_free(new_ScreenLines2);
8682 new_ScreenLines2 = NULL;
8683#endif
8684 vim_free(new_ScreenAttrs);
8685 new_ScreenAttrs = NULL;
8686 vim_free(new_LineOffset);
8687 new_LineOffset = NULL;
8688 vim_free(new_LineWraps);
8689 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008690#ifdef FEAT_WINDOWS
8691 vim_free(new_TabPageIdxs);
8692 new_TabPageIdxs = NULL;
8693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 }
8695 else
8696 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008697 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 for (new_row = 0; new_row < Rows; ++new_row)
8700 {
8701 new_LineOffset[new_row] = new_row * Columns;
8702 new_LineWraps[new_row] = FALSE;
8703
8704 /*
8705 * If the screen is not going to be cleared, copy as much as
8706 * possible from the old screen to the new one and clear the rest
8707 * (used when resizing the window at the "--more--" prompt or when
8708 * executing an external command, for the GUI).
8709 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008710 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {
8712 (void)vim_memset(new_ScreenLines + new_row * Columns,
8713 ' ', (size_t)Columns * sizeof(schar_T));
8714#ifdef FEAT_MBYTE
8715 if (enc_utf8)
8716 {
8717 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8718 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008719 for (i = 0; i < p_mco; ++i)
8720 (void)vim_memset(new_ScreenLinesC[i]
8721 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 0, (size_t)Columns * sizeof(u8char_T));
8723 }
8724 if (enc_dbcs == DBCS_JPNU)
8725 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8726 0, (size_t)Columns * sizeof(schar_T));
8727#endif
8728 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8729 0, (size_t)Columns * sizeof(sattr_T));
8730 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008731 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 {
8733 if (screen_Columns < Columns)
8734 len = screen_Columns;
8735 else
8736 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008737#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008738 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 * may be invalid now. Also when p_mco changes. */
8740 if (!(enc_utf8 && ScreenLinesUC == NULL)
8741 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008742#endif
8743 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8744 ScreenLines + LineOffset[old_row],
8745 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008747 if (enc_utf8 && ScreenLinesUC != NULL
8748 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8751 ScreenLinesUC + LineOffset[old_row],
8752 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008753 for (i = 0; i < p_mco; ++i)
8754 mch_memmove(new_ScreenLinesC[i]
8755 + new_LineOffset[new_row],
8756 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 (size_t)len * sizeof(u8char_T));
8758 }
8759 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8760 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8761 ScreenLines2 + LineOffset[old_row],
8762 (size_t)len * sizeof(schar_T));
8763#endif
8764 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8765 ScreenAttrs + LineOffset[old_row],
8766 (size_t)len * sizeof(sattr_T));
8767 }
8768 }
8769 }
8770 /* Use the last line of the screen for the current line. */
8771 current_ScreenLine = new_ScreenLines + Rows * Columns;
8772 }
8773
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008774 free_screenlines();
8775
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 ScreenLines = new_ScreenLines;
8777#ifdef FEAT_MBYTE
8778 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008779 for (i = 0; i < p_mco; ++i)
8780 ScreenLinesC[i] = new_ScreenLinesC[i];
8781 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 ScreenLines2 = new_ScreenLines2;
8783#endif
8784 ScreenAttrs = new_ScreenAttrs;
8785 LineOffset = new_LineOffset;
8786 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008787#ifdef FEAT_WINDOWS
8788 TabPageIdxs = new_TabPageIdxs;
8789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
8791 /* It's important that screen_Rows and screen_Columns reflect the actual
8792 * size of ScreenLines[]. Set them before calling anything. */
8793#ifdef FEAT_GUI
8794 old_Rows = screen_Rows;
8795#endif
8796 screen_Rows = Rows;
8797 screen_Columns = Columns;
8798
8799 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008800 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 screenclear2();
8802
8803#ifdef FEAT_GUI
8804 else if (gui.in_use
8805 && !gui.starting
8806 && ScreenLines != NULL
8807 && old_Rows != Rows)
8808 {
8809 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8810 /*
8811 * Adjust the position of the cursor, for when executing an external
8812 * command.
8813 */
8814 if (msg_row >= Rows) /* Rows got smaller */
8815 msg_row = Rows - 1; /* put cursor at last row */
8816 else if (Rows > old_Rows) /* Rows got bigger */
8817 msg_row += Rows - old_Rows; /* put cursor in same place */
8818 if (msg_col >= Columns) /* Columns got smaller */
8819 msg_col = Columns - 1; /* put cursor at last column */
8820 }
8821#endif
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008824 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008825
8826#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008827 /*
8828 * Do not apply autocommands more than 3 times to avoid an endless loop
8829 * in case applying autocommands always changes Rows or Columns.
8830 */
8831 if (starting == 0 && ++retry_count <= 3)
8832 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008833 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008834 /* In rare cases, autocommands may have altered Rows or Columns,
8835 * jump back to check if we need to allocate the screen again. */
8836 goto retry;
8837 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008842free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008843{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008844#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008845 int i;
8846
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008847 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008848 for (i = 0; i < Screen_mco; ++i)
8849 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008850 vim_free(ScreenLines2);
8851#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008853 vim_free(ScreenAttrs);
8854 vim_free(LineOffset);
8855 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008856#ifdef FEAT_WINDOWS
8857 vim_free(TabPageIdxs);
8858#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008859}
8860
8861 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008862screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864 check_for_delay(FALSE);
8865 screenalloc(FALSE); /* allocate screen buffers if size changed */
8866 screenclear2(); /* clear the screen */
8867}
8868
8869 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008870screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 int i;
8873
8874 if (starting == NO_SCREEN || ScreenLines == NULL
8875#ifdef FEAT_GUI
8876 || (gui.in_use && gui.starting)
8877#endif
8878 )
8879 return;
8880
8881#ifdef FEAT_GUI
8882 if (!gui.in_use)
8883#endif
8884 screen_attr = -1; /* force setting the Normal colors */
8885 screen_stop_highlight(); /* don't want highlighting here */
8886
8887#ifdef FEAT_CLIPBOARD
8888 /* disable selection without redrawing it */
8889 clip_scroll_selection(9999);
8890#endif
8891
8892 /* blank out ScreenLines */
8893 for (i = 0; i < Rows; ++i)
8894 {
8895 lineclear(LineOffset[i], (int)Columns);
8896 LineWraps[i] = FALSE;
8897 }
8898
8899 if (can_clear(T_CL))
8900 {
8901 out_str(T_CL); /* clear the display */
8902 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008903 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 else
8906 {
8907 /* can't clear the screen, mark all chars with invalid attributes */
8908 for (i = 0; i < Rows; ++i)
8909 lineinvalid(LineOffset[i], (int)Columns);
8910 clear_cmdline = TRUE;
8911 }
8912
8913 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8914
8915 win_rest_invalid(firstwin);
8916 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008917#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008918 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (must_redraw == CLEAR) /* no need to clear again */
8921 must_redraw = NOT_VALID;
8922 compute_cmdrow();
8923 msg_row = cmdline_row; /* put cursor on last line for messages */
8924 msg_col = 0;
8925 screen_start(); /* don't know where cursor is now */
8926 msg_scrolled = 0; /* can't scroll back */
8927 msg_didany = FALSE;
8928 msg_didout = FALSE;
8929}
8930
8931/*
8932 * Clear one line in ScreenLines.
8933 */
8934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008935lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8938#ifdef FEAT_MBYTE
8939 if (enc_utf8)
8940 (void)vim_memset(ScreenLinesUC + off, 0,
8941 (size_t)width * sizeof(u8char_T));
8942#endif
8943 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8944}
8945
8946/*
8947 * Mark one line in ScreenLines invalid by setting the attributes to an
8948 * invalid value.
8949 */
8950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008951lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8954}
8955
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008956#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957/*
8958 * Copy part of a Screenline for vertically split window "wp".
8959 */
8960 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008961linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 unsigned off_to = LineOffset[to] + wp->w_wincol;
8964 unsigned off_from = LineOffset[from] + wp->w_wincol;
8965
8966 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8967 wp->w_width * sizeof(schar_T));
8968# ifdef FEAT_MBYTE
8969 if (enc_utf8)
8970 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008971 int i;
8972
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8974 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 for (i = 0; i < p_mco; ++i)
8976 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8977 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 }
8979 if (enc_dbcs == DBCS_JPNU)
8980 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8981 wp->w_width * sizeof(schar_T));
8982# endif
8983 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8984 wp->w_width * sizeof(sattr_T));
8985}
8986#endif
8987
8988/*
8989 * Return TRUE if clearing with term string "p" would work.
8990 * It can't work when the string is empty or it won't set the right background.
8991 */
8992 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008993can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 return (*p != NUL && (t_colors <= 1
8996#ifdef FEAT_GUI
8997 || gui.in_use
8998#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008999#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard18f6722016-06-17 13:18:49 +02009000 || (p_tgc && cterm_normal_bg_gui_color == (long_u)INVALCOLOR)
9001 || (!p_tgc && cterm_normal_bg_color == 0)
9002#else
9003 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009004#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009005 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008/*
9009 * Reset cursor position. Use whenever cursor was moved because of outputting
9010 * something directly to the screen (shell commands) or a terminal control
9011 * code.
9012 */
9013 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009014screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 screen_cur_row = screen_cur_col = 9999;
9017}
9018
9019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 * Move the cursor to position "row","col" in the screen.
9021 * This tries to find the most efficient way to move, minimizing the number of
9022 * characters sent to the terminal.
9023 */
9024 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009025windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009027 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 int i;
9029 int plan;
9030 int cost;
9031 int wouldbe_col;
9032 int noinvcurs;
9033 char_u *bs;
9034 int goto_cost;
9035 int attr;
9036
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009037#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9039
9040#define PLAN_LE 1
9041#define PLAN_CR 2
9042#define PLAN_NL 3
9043#define PLAN_WRITE 4
9044 /* Can't use ScreenLines unless initialized */
9045 if (ScreenLines == NULL)
9046 return;
9047
9048 if (col != screen_cur_col || row != screen_cur_row)
9049 {
9050 /* Check for valid position. */
9051 if (row < 0) /* window without text lines? */
9052 row = 0;
9053 if (row >= screen_Rows)
9054 row = screen_Rows - 1;
9055 if (col >= screen_Columns)
9056 col = screen_Columns - 1;
9057
9058 /* check if no cursor movement is allowed in highlight mode */
9059 if (screen_attr && *T_MS == NUL)
9060 noinvcurs = HIGHL_COST;
9061 else
9062 noinvcurs = 0;
9063 goto_cost = GOTO_COST + noinvcurs;
9064
9065 /*
9066 * Plan how to do the positioning:
9067 * 1. Use CR to move it to column 0, same row.
9068 * 2. Use T_LE to move it a few columns to the left.
9069 * 3. Use NL to move a few lines down, column 0.
9070 * 4. Move a few columns to the right with T_ND or by writing chars.
9071 *
9072 * Don't do this if the cursor went beyond the last column, the cursor
9073 * position is unknown then (some terminals wrap, some don't )
9074 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009075 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076 * characters to move the cursor to the right.
9077 */
9078 if (row >= screen_cur_row && screen_cur_col < Columns)
9079 {
9080 /*
9081 * If the cursor is in the same row, bigger col, we can use CR
9082 * or T_LE.
9083 */
9084 bs = NULL; /* init for GCC */
9085 attr = screen_attr;
9086 if (row == screen_cur_row && col < screen_cur_col)
9087 {
9088 /* "le" is preferred over "bc", because "bc" is obsolete */
9089 if (*T_LE)
9090 bs = T_LE; /* "cursor left" */
9091 else
9092 bs = T_BC; /* "backspace character (old) */
9093 if (*bs)
9094 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9095 else
9096 cost = 999;
9097 if (col + 1 < cost) /* using CR is less characters */
9098 {
9099 plan = PLAN_CR;
9100 wouldbe_col = 0;
9101 cost = 1; /* CR is just one character */
9102 }
9103 else
9104 {
9105 plan = PLAN_LE;
9106 wouldbe_col = col;
9107 }
9108 if (noinvcurs) /* will stop highlighting */
9109 {
9110 cost += noinvcurs;
9111 attr = 0;
9112 }
9113 }
9114
9115 /*
9116 * If the cursor is above where we want to be, we can use CR LF.
9117 */
9118 else if (row > screen_cur_row)
9119 {
9120 plan = PLAN_NL;
9121 wouldbe_col = 0;
9122 cost = (row - screen_cur_row) * 2; /* CR LF */
9123 if (noinvcurs) /* will stop highlighting */
9124 {
9125 cost += noinvcurs;
9126 attr = 0;
9127 }
9128 }
9129
9130 /*
9131 * If the cursor is in the same row, smaller col, just use write.
9132 */
9133 else
9134 {
9135 plan = PLAN_WRITE;
9136 wouldbe_col = screen_cur_col;
9137 cost = 0;
9138 }
9139
9140 /*
9141 * Check if any characters that need to be written have the
9142 * correct attributes. Also avoid UTF-8 characters.
9143 */
9144 i = col - wouldbe_col;
9145 if (i > 0)
9146 cost += i;
9147 if (cost < goto_cost && i > 0)
9148 {
9149 /*
9150 * Check if the attributes are correct without additionally
9151 * stopping highlighting.
9152 */
9153 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9154 while (i && *p++ == attr)
9155 --i;
9156 if (i != 0)
9157 {
9158 /*
9159 * Try if it works when highlighting is stopped here.
9160 */
9161 if (*--p == 0)
9162 {
9163 cost += noinvcurs;
9164 while (i && *p++ == 0)
9165 --i;
9166 }
9167 if (i != 0)
9168 cost = 999; /* different attributes, don't do it */
9169 }
9170#ifdef FEAT_MBYTE
9171 if (enc_utf8)
9172 {
9173 /* Don't use an UTF-8 char for positioning, it's slow. */
9174 for (i = wouldbe_col; i < col; ++i)
9175 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9176 {
9177 cost = 999;
9178 break;
9179 }
9180 }
9181#endif
9182 }
9183
9184 /*
9185 * We can do it without term_windgoto()!
9186 */
9187 if (cost < goto_cost)
9188 {
9189 if (plan == PLAN_LE)
9190 {
9191 if (noinvcurs)
9192 screen_stop_highlight();
9193 while (screen_cur_col > col)
9194 {
9195 out_str(bs);
9196 --screen_cur_col;
9197 }
9198 }
9199 else if (plan == PLAN_CR)
9200 {
9201 if (noinvcurs)
9202 screen_stop_highlight();
9203 out_char('\r');
9204 screen_cur_col = 0;
9205 }
9206 else if (plan == PLAN_NL)
9207 {
9208 if (noinvcurs)
9209 screen_stop_highlight();
9210 while (screen_cur_row < row)
9211 {
9212 out_char('\n');
9213 ++screen_cur_row;
9214 }
9215 screen_cur_col = 0;
9216 }
9217
9218 i = col - screen_cur_col;
9219 if (i > 0)
9220 {
9221 /*
9222 * Use cursor-right if it's one character only. Avoids
9223 * removing a line of pixels from the last bold char, when
9224 * using the bold trick in the GUI.
9225 */
9226 if (T_ND[0] != NUL && T_ND[1] == NUL)
9227 {
9228 while (i-- > 0)
9229 out_char(*T_ND);
9230 }
9231 else
9232 {
9233 int off;
9234
9235 off = LineOffset[row] + screen_cur_col;
9236 while (i-- > 0)
9237 {
9238 if (ScreenAttrs[off] != screen_attr)
9239 screen_stop_highlight();
9240#ifdef FEAT_MBYTE
9241 out_flush_check();
9242#endif
9243 out_char(ScreenLines[off]);
9244#ifdef FEAT_MBYTE
9245 if (enc_dbcs == DBCS_JPNU
9246 && ScreenLines[off] == 0x8e)
9247 out_char(ScreenLines2[off]);
9248#endif
9249 ++off;
9250 }
9251 }
9252 }
9253 }
9254 }
9255 else
9256 cost = 999;
9257
9258 if (cost >= goto_cost)
9259 {
9260 if (noinvcurs)
9261 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009262 if (row == screen_cur_row && (col > screen_cur_col)
9263 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 term_cursor_right(col - screen_cur_col);
9265 else
9266 term_windgoto(row, col);
9267 }
9268 screen_cur_row = row;
9269 screen_cur_col = col;
9270 }
9271}
9272
9273/*
9274 * Set cursor to its position in the current window.
9275 */
9276 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009277setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 if (redrawing())
9280 {
9281 validate_cursor();
9282 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9283 W_WINCOL(curwin) + (
9284#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009285 /* With 'rightleft' set and the cursor on a double-wide
9286 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9288# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009289 (has_mbyte
9290 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9291 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292# endif
9293 1)) :
9294#endif
9295 curwin->w_wcol));
9296 }
9297}
9298
9299
9300/*
9301 * insert 'line_count' lines at 'row' in window 'wp'
9302 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9303 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9304 * scrolling.
9305 * Returns FAIL if the lines are not inserted, OK for success.
9306 */
9307 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009308win_ins_lines(
9309 win_T *wp,
9310 int row,
9311 int line_count,
9312 int invalid,
9313 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 int did_delete;
9316 int nextrow;
9317 int lastrow;
9318 int retval;
9319
9320 if (invalid)
9321 wp->w_lines_valid = 0;
9322
9323 if (wp->w_height < 5)
9324 return FAIL;
9325
9326 if (line_count > wp->w_height - row)
9327 line_count = wp->w_height - row;
9328
9329 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9330 if (retval != MAYBE)
9331 return retval;
9332
9333 /*
9334 * If there is a next window or a status line, we first try to delete the
9335 * lines at the bottom to avoid messing what is after the window.
9336 * If this fails and there are following windows, don't do anything to avoid
9337 * messing up those windows, better just redraw.
9338 */
9339 did_delete = FALSE;
9340#ifdef FEAT_WINDOWS
9341 if (wp->w_next != NULL || wp->w_status_height)
9342 {
9343 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9344 line_count, (int)Rows, FALSE, NULL) == OK)
9345 did_delete = TRUE;
9346 else if (wp->w_next)
9347 return FAIL;
9348 }
9349#endif
9350 /*
9351 * if no lines deleted, blank the lines that will end up below the window
9352 */
9353 if (!did_delete)
9354 {
9355#ifdef FEAT_WINDOWS
9356 wp->w_redr_status = TRUE;
9357#endif
9358 redraw_cmdline = TRUE;
9359 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9360 lastrow = nextrow + line_count;
9361 if (lastrow > Rows)
9362 lastrow = Rows;
9363 screen_fill(nextrow - line_count, lastrow - line_count,
9364 W_WINCOL(wp), (int)W_ENDCOL(wp),
9365 ' ', ' ', 0);
9366 }
9367
9368 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9369 == FAIL)
9370 {
9371 /* deletion will have messed up other windows */
9372 if (did_delete)
9373 {
9374#ifdef FEAT_WINDOWS
9375 wp->w_redr_status = TRUE;
9376#endif
9377 win_rest_invalid(W_NEXT(wp));
9378 }
9379 return FAIL;
9380 }
9381
9382 return OK;
9383}
9384
9385/*
9386 * delete "line_count" window lines at "row" in window "wp"
9387 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9388 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9389 * scrolling
9390 * Return OK for success, FAIL if the lines are not deleted.
9391 */
9392 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009393win_del_lines(
9394 win_T *wp,
9395 int row,
9396 int line_count,
9397 int invalid,
9398 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400 int retval;
9401
9402 if (invalid)
9403 wp->w_lines_valid = 0;
9404
9405 if (line_count > wp->w_height - row)
9406 line_count = wp->w_height - row;
9407
9408 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9409 if (retval != MAYBE)
9410 return retval;
9411
9412 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9413 (int)Rows, FALSE, NULL) == FAIL)
9414 return FAIL;
9415
9416#ifdef FEAT_WINDOWS
9417 /*
9418 * If there are windows or status lines below, try to put them at the
9419 * correct place. If we can't do that, they have to be redrawn.
9420 */
9421 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9422 {
9423 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9424 line_count, (int)Rows, NULL) == FAIL)
9425 {
9426 wp->w_redr_status = TRUE;
9427 win_rest_invalid(wp->w_next);
9428 }
9429 }
9430 /*
9431 * If this is the last window and there is no status line, redraw the
9432 * command line later.
9433 */
9434 else
9435#endif
9436 redraw_cmdline = TRUE;
9437 return OK;
9438}
9439
9440/*
9441 * Common code for win_ins_lines() and win_del_lines().
9442 * Returns OK or FAIL when the work has been done.
9443 * Returns MAYBE when not finished yet.
9444 */
9445 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009446win_do_lines(
9447 win_T *wp,
9448 int row,
9449 int line_count,
9450 int mayclear,
9451 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 int retval;
9454
9455 if (!redrawing() || line_count <= 0)
9456 return FAIL;
9457
9458 /* only a few lines left: redraw is faster */
9459 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009460#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 && wp->w_width == Columns
9462#endif
9463 )
9464 {
9465 screenclear(); /* will set wp->w_lines_valid to 0 */
9466 return FAIL;
9467 }
9468
9469 /*
9470 * Delete all remaining lines
9471 */
9472 if (row + line_count >= wp->w_height)
9473 {
9474 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9475 W_WINCOL(wp), (int)W_ENDCOL(wp),
9476 ' ', ' ', 0);
9477 return OK;
9478 }
9479
9480 /*
9481 * when scrolling, the message on the command line should be cleared,
9482 * otherwise it will stay there forever.
9483 */
9484 clear_cmdline = TRUE;
9485
9486 /*
9487 * If the terminal can set a scroll region, use that.
9488 * Always do this in a vertically split window. This will redraw from
9489 * ScreenLines[] when t_CV isn't defined. That's faster than using
9490 * win_line().
9491 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009492 * a character in the lower right corner of the scroll region may cause a
9493 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 */
9495 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009496#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 || W_WIDTH(wp) != Columns
9498#endif
9499 )
9500 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009501#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9503#endif
9504 scroll_region_set(wp, row);
9505 if (del)
9506 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9507 wp->w_height - row, FALSE, wp);
9508 else
9509 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9510 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009511#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9513#endif
9514 scroll_region_reset();
9515 return retval;
9516 }
9517
9518#ifdef FEAT_WINDOWS
9519 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9520 return FAIL;
9521#endif
9522
9523 return MAYBE;
9524}
9525
9526/*
9527 * window 'wp' and everything after it is messed up, mark it for redraw
9528 */
9529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009530win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
9532#ifdef FEAT_WINDOWS
9533 while (wp != NULL)
9534#else
9535 if (wp != NULL)
9536#endif
9537 {
9538 redraw_win_later(wp, NOT_VALID);
9539#ifdef FEAT_WINDOWS
9540 wp->w_redr_status = TRUE;
9541 wp = wp->w_next;
9542#endif
9543 }
9544 redraw_cmdline = TRUE;
9545}
9546
9547/*
9548 * The rest of the routines in this file perform screen manipulations. The
9549 * given operation is performed physically on the screen. The corresponding
9550 * change is also made to the internal screen image. In this way, the editor
9551 * anticipates the effect of editing changes on the appearance of the screen.
9552 * That way, when we call screenupdate a complete redraw isn't usually
9553 * necessary. Another advantage is that we can keep adding code to anticipate
9554 * screen changes, and in the meantime, everything still works.
9555 */
9556
9557/*
9558 * types for inserting or deleting lines
9559 */
9560#define USE_T_CAL 1
9561#define USE_T_CDL 2
9562#define USE_T_AL 3
9563#define USE_T_CE 4
9564#define USE_T_DL 5
9565#define USE_T_SR 6
9566#define USE_NL 7
9567#define USE_T_CD 8
9568#define USE_REDRAW 9
9569
9570/*
9571 * insert lines on the screen and update ScreenLines[]
9572 * 'end' is the line after the scrolled part. Normally it is Rows.
9573 * When scrolling region used 'off' is the offset from the top for the region.
9574 * 'row' and 'end' are relative to the start of the region.
9575 *
9576 * return FAIL for failure, OK for success.
9577 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009578 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009579screen_ins_lines(
9580 int off,
9581 int row,
9582 int line_count,
9583 int end,
9584 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
9586 int i;
9587 int j;
9588 unsigned temp;
9589 int cursor_row;
9590 int type;
9591 int result_empty;
9592 int can_ce = can_clear(T_CE);
9593
9594 /*
9595 * FAIL if
9596 * - there is no valid screen
9597 * - the screen has to be redrawn completely
9598 * - the line count is less than one
9599 * - the line count is more than 'ttyscroll'
9600 */
9601 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9602 return FAIL;
9603
9604 /*
9605 * There are seven ways to insert lines:
9606 * 0. When in a vertically split window and t_CV isn't set, redraw the
9607 * characters from ScreenLines[].
9608 * 1. Use T_CD (clear to end of display) if it exists and the result of
9609 * the insert is just empty lines
9610 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9611 * present or line_count > 1. It looks better if we do all the inserts
9612 * at once.
9613 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9614 * insert is just empty lines and T_CE is not present or line_count >
9615 * 1.
9616 * 4. Use T_AL (insert line) if it exists.
9617 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9618 * just empty lines.
9619 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9620 * just empty lines.
9621 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9622 * the 'da' flag is not set or we have clear line capability.
9623 * 8. redraw the characters from ScreenLines[].
9624 *
9625 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9626 * the scrollbar for the window. It does have insert line, use that if it
9627 * exists.
9628 */
9629 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009630#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9632 type = USE_REDRAW;
9633 else
9634#endif
9635 if (can_clear(T_CD) && result_empty)
9636 type = USE_T_CD;
9637 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9638 type = USE_T_CAL;
9639 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9640 type = USE_T_CDL;
9641 else if (*T_AL != NUL)
9642 type = USE_T_AL;
9643 else if (can_ce && result_empty)
9644 type = USE_T_CE;
9645 else if (*T_DL != NUL && result_empty)
9646 type = USE_T_DL;
9647 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9648 type = USE_T_SR;
9649 else
9650 return FAIL;
9651
9652 /*
9653 * For clearing the lines screen_del_lines() is used. This will also take
9654 * care of t_db if necessary.
9655 */
9656 if (type == USE_T_CD || type == USE_T_CDL ||
9657 type == USE_T_CE || type == USE_T_DL)
9658 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9659
9660 /*
9661 * If text is retained below the screen, first clear or delete as many
9662 * lines at the bottom of the window as are about to be inserted so that
9663 * the deleted lines won't later surface during a screen_del_lines.
9664 */
9665 if (*T_DB)
9666 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9667
9668#ifdef FEAT_CLIPBOARD
9669 /* Remove a modeless selection when inserting lines halfway the screen
9670 * or not the full width of the screen. */
9671 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009672# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 || (wp != NULL && wp->w_width != Columns)
9674# endif
9675 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009676 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else
9678 clip_scroll_selection(-line_count);
9679#endif
9680
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681#ifdef FEAT_GUI
9682 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9683 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009684 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685#endif
9686
9687 if (*T_CCS != NUL) /* cursor relative to region */
9688 cursor_row = row;
9689 else
9690 cursor_row = row + off;
9691
9692 /*
9693 * Shift LineOffset[] line_count down to reflect the inserted lines.
9694 * Clear the inserted lines in ScreenLines[].
9695 */
9696 row += off;
9697 end += off;
9698 for (i = 0; i < line_count; ++i)
9699 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009700#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (wp != NULL && wp->w_width != Columns)
9702 {
9703 /* need to copy part of a line */
9704 j = end - 1 - i;
9705 while ((j -= line_count) >= row)
9706 linecopy(j + line_count, j, wp);
9707 j += line_count;
9708 if (can_clear((char_u *)" "))
9709 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9710 else
9711 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9712 LineWraps[j] = FALSE;
9713 }
9714 else
9715#endif
9716 {
9717 j = end - 1 - i;
9718 temp = LineOffset[j];
9719 while ((j -= line_count) >= row)
9720 {
9721 LineOffset[j + line_count] = LineOffset[j];
9722 LineWraps[j + line_count] = LineWraps[j];
9723 }
9724 LineOffset[j + line_count] = temp;
9725 LineWraps[j + line_count] = FALSE;
9726 if (can_clear((char_u *)" "))
9727 lineclear(temp, (int)Columns);
9728 else
9729 lineinvalid(temp, (int)Columns);
9730 }
9731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732
9733 screen_stop_highlight();
9734 windgoto(cursor_row, 0);
9735
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009736#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 /* redraw the characters */
9738 if (type == USE_REDRAW)
9739 redraw_block(row, end, wp);
9740 else
9741#endif
9742 if (type == USE_T_CAL)
9743 {
9744 term_append_lines(line_count);
9745 screen_start(); /* don't know where cursor is now */
9746 }
9747 else
9748 {
9749 for (i = 0; i < line_count; i++)
9750 {
9751 if (type == USE_T_AL)
9752 {
9753 if (i && cursor_row != 0)
9754 windgoto(cursor_row, 0);
9755 out_str(T_AL);
9756 }
9757 else /* type == USE_T_SR */
9758 out_str(T_SR);
9759 screen_start(); /* don't know where cursor is now */
9760 }
9761 }
9762
9763 /*
9764 * With scroll-reverse and 'da' flag set we need to clear the lines that
9765 * have been scrolled down into the region.
9766 */
9767 if (type == USE_T_SR && *T_DA)
9768 {
9769 for (i = 0; i < line_count; ++i)
9770 {
9771 windgoto(off + i, 0);
9772 out_str(T_CE);
9773 screen_start(); /* don't know where cursor is now */
9774 }
9775 }
9776
9777#ifdef FEAT_GUI
9778 gui_can_update_cursor();
9779 if (gui.in_use)
9780 out_flush(); /* always flush after a scroll */
9781#endif
9782 return OK;
9783}
9784
9785/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009786 * Delete lines on the screen and update ScreenLines[].
9787 * "end" is the line after the scrolled part. Normally it is Rows.
9788 * When scrolling region used "off" is the offset from the top for the region.
9789 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 *
9791 * Return OK for success, FAIL if the lines are not deleted.
9792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009794screen_del_lines(
9795 int off,
9796 int row,
9797 int line_count,
9798 int end,
9799 int force, /* even when line_count > p_ttyscroll */
9800 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 int j;
9803 int i;
9804 unsigned temp;
9805 int cursor_row;
9806 int cursor_end;
9807 int result_empty; /* result is empty until end of region */
9808 int can_delete; /* deleting line codes can be used */
9809 int type;
9810
9811 /*
9812 * FAIL if
9813 * - there is no valid screen
9814 * - the screen has to be redrawn completely
9815 * - the line count is less than one
9816 * - the line count is more than 'ttyscroll'
9817 */
9818 if (!screen_valid(TRUE) || line_count <= 0 ||
9819 (!force && line_count > p_ttyscroll))
9820 return FAIL;
9821
9822 /*
9823 * Check if the rest of the current region will become empty.
9824 */
9825 result_empty = row + line_count >= end;
9826
9827 /*
9828 * We can delete lines only when 'db' flag not set or when 'ce' option
9829 * available.
9830 */
9831 can_delete = (*T_DB == NUL || can_clear(T_CE));
9832
9833 /*
9834 * There are six ways to delete lines:
9835 * 0. When in a vertically split window and t_CV isn't set, redraw the
9836 * characters from ScreenLines[].
9837 * 1. Use T_CD if it exists and the result is empty.
9838 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9839 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9840 * none of the other ways work.
9841 * 4. Use T_CE (erase line) if the result is empty.
9842 * 5. Use T_DL (delete line) if it exists.
9843 * 6. redraw the characters from ScreenLines[].
9844 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009845#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9847 type = USE_REDRAW;
9848 else
9849#endif
9850 if (can_clear(T_CD) && result_empty)
9851 type = USE_T_CD;
9852#if defined(__BEOS__) && defined(BEOS_DR8)
9853 /*
9854 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9855 * its internal termcap... this works okay for tests which test *T_DB !=
9856 * NUL. It has the disadvantage that the user cannot use any :set t_*
9857 * command to get T_DB (back) to empty_option, only :set term=... will do
9858 * the trick...
9859 * Anyway, this hack will hopefully go away with the next OS release.
9860 * (Olaf Seibert)
9861 */
9862 else if (row == 0 && T_DB == empty_option
9863 && (line_count == 1 || *T_CDL == NUL))
9864#else
9865 else if (row == 0 && (
9866#ifndef AMIGA
9867 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9868 * up, so use delete-line command */
9869 line_count == 1 ||
9870#endif
9871 *T_CDL == NUL))
9872#endif
9873 type = USE_NL;
9874 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9875 type = USE_T_CDL;
9876 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009877#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 && (wp == NULL || wp->w_width == Columns)
9879#endif
9880 )
9881 type = USE_T_CE;
9882 else if (*T_DL != NUL && can_delete)
9883 type = USE_T_DL;
9884 else if (*T_CDL != NUL && can_delete)
9885 type = USE_T_CDL;
9886 else
9887 return FAIL;
9888
9889#ifdef FEAT_CLIPBOARD
9890 /* Remove a modeless selection when deleting lines halfway the screen or
9891 * not the full width of the screen. */
9892 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009893# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 || (wp != NULL && wp->w_width != Columns)
9895# endif
9896 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009897 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 else
9899 clip_scroll_selection(line_count);
9900#endif
9901
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902#ifdef FEAT_GUI
9903 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9904 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009905 gui_dont_update_cursor(gui.cursor_row >= row + off
9906 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907#endif
9908
9909 if (*T_CCS != NUL) /* cursor relative to region */
9910 {
9911 cursor_row = row;
9912 cursor_end = end;
9913 }
9914 else
9915 {
9916 cursor_row = row + off;
9917 cursor_end = end + off;
9918 }
9919
9920 /*
9921 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9922 * Clear the inserted lines in ScreenLines[].
9923 */
9924 row += off;
9925 end += off;
9926 for (i = 0; i < line_count; ++i)
9927 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009928#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (wp != NULL && wp->w_width != Columns)
9930 {
9931 /* need to copy part of a line */
9932 j = row + i;
9933 while ((j += line_count) <= end - 1)
9934 linecopy(j - line_count, j, wp);
9935 j -= line_count;
9936 if (can_clear((char_u *)" "))
9937 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9938 else
9939 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9940 LineWraps[j] = FALSE;
9941 }
9942 else
9943#endif
9944 {
9945 /* whole width, moving the line pointers is faster */
9946 j = row + i;
9947 temp = LineOffset[j];
9948 while ((j += line_count) <= end - 1)
9949 {
9950 LineOffset[j - line_count] = LineOffset[j];
9951 LineWraps[j - line_count] = LineWraps[j];
9952 }
9953 LineOffset[j - line_count] = temp;
9954 LineWraps[j - line_count] = FALSE;
9955 if (can_clear((char_u *)" "))
9956 lineclear(temp, (int)Columns);
9957 else
9958 lineinvalid(temp, (int)Columns);
9959 }
9960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
9962 screen_stop_highlight();
9963
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009964#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 /* redraw the characters */
9966 if (type == USE_REDRAW)
9967 redraw_block(row, end, wp);
9968 else
9969#endif
9970 if (type == USE_T_CD) /* delete the lines */
9971 {
9972 windgoto(cursor_row, 0);
9973 out_str(T_CD);
9974 screen_start(); /* don't know where cursor is now */
9975 }
9976 else if (type == USE_T_CDL)
9977 {
9978 windgoto(cursor_row, 0);
9979 term_delete_lines(line_count);
9980 screen_start(); /* don't know where cursor is now */
9981 }
9982 /*
9983 * Deleting lines at top of the screen or scroll region: Just scroll
9984 * the whole screen (scroll region) up by outputting newlines on the
9985 * last line.
9986 */
9987 else if (type == USE_NL)
9988 {
9989 windgoto(cursor_end - 1, 0);
9990 for (i = line_count; --i >= 0; )
9991 out_char('\n'); /* cursor will remain on same line */
9992 }
9993 else
9994 {
9995 for (i = line_count; --i >= 0; )
9996 {
9997 if (type == USE_T_DL)
9998 {
9999 windgoto(cursor_row, 0);
10000 out_str(T_DL); /* delete a line */
10001 }
10002 else /* type == USE_T_CE */
10003 {
10004 windgoto(cursor_row + i, 0);
10005 out_str(T_CE); /* erase a line */
10006 }
10007 screen_start(); /* don't know where cursor is now */
10008 }
10009 }
10010
10011 /*
10012 * If the 'db' flag is set, we need to clear the lines that have been
10013 * scrolled up at the bottom of the region.
10014 */
10015 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10016 {
10017 for (i = line_count; i > 0; --i)
10018 {
10019 windgoto(cursor_end - i, 0);
10020 out_str(T_CE); /* erase a line */
10021 screen_start(); /* don't know where cursor is now */
10022 }
10023 }
10024
10025#ifdef FEAT_GUI
10026 gui_can_update_cursor();
10027 if (gui.in_use)
10028 out_flush(); /* always flush after a scroll */
10029#endif
10030
10031 return OK;
10032}
10033
10034/*
10035 * show the current mode and ruler
10036 *
10037 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10038 * If clear_cmdline is FALSE there may be a message there that needs to be
10039 * cleared only if a mode is shown.
10040 * Return the length of the message (0 if no message).
10041 */
10042 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010043showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044{
10045 int need_clear;
10046 int length = 0;
10047 int do_mode;
10048 int attr;
10049 int nwr_save;
10050#ifdef FEAT_INS_EXPAND
10051 int sub_attr;
10052#endif
10053
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010054 do_mode = ((p_smd && msg_silent == 0)
10055 && ((State & INSERT)
10056 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010057 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 if (do_mode || Recording)
10059 {
10060 /*
10061 * Don't show mode right now, when not redrawing or inside a mapping.
10062 * Call char_avail() only when we are going to show something, because
10063 * it takes a bit of time.
10064 */
10065 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10066 {
10067 redraw_cmdline = TRUE; /* show mode later */
10068 return 0;
10069 }
10070
10071 nwr_save = need_wait_return;
10072
10073 /* wait a bit before overwriting an important message */
10074 check_for_delay(FALSE);
10075
10076 /* if the cmdline is more than one line high, erase top lines */
10077 need_clear = clear_cmdline;
10078 if (clear_cmdline && cmdline_row < Rows - 1)
10079 msg_clr_cmdline(); /* will reset clear_cmdline */
10080
10081 /* Position on the last line in the window, column 0 */
10082 msg_pos_mode();
10083 cursor_off();
10084 attr = hl_attr(HLF_CM); /* Highlight mode */
10085 if (do_mode)
10086 {
10087 MSG_PUTS_ATTR("--", attr);
10088#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010089 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010090# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010091 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010092# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010093 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010094# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010095 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010096# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 MSG_PUTS_ATTR(" IM", attr);
10098# else
10099 MSG_PUTS_ATTR(" XIM", attr);
10100# endif
10101#endif
10102#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10103 if (gui.in_use)
10104 {
10105 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010106 {
10107 /* HANGUL */
10108 if (enc_utf8)
10109 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10110 else
10111 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 }
10114#endif
10115#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010116 /* CTRL-X in Insert mode */
10117 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 {
10119 /* These messages can get long, avoid a wrap in a narrow
10120 * window. Prefer showing edit_submode_extra. */
10121 length = (Rows - msg_row) * Columns - 3;
10122 if (edit_submode_extra != NULL)
10123 length -= vim_strsize(edit_submode_extra);
10124 if (length > 0)
10125 {
10126 if (edit_submode_pre != NULL)
10127 length -= vim_strsize(edit_submode_pre);
10128 if (length - vim_strsize(edit_submode) > 0)
10129 {
10130 if (edit_submode_pre != NULL)
10131 msg_puts_attr(edit_submode_pre, attr);
10132 msg_puts_attr(edit_submode, attr);
10133 }
10134 if (edit_submode_extra != NULL)
10135 {
10136 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10137 if ((int)edit_submode_highl < (int)HLF_COUNT)
10138 sub_attr = hl_attr(edit_submode_highl);
10139 else
10140 sub_attr = attr;
10141 msg_puts_attr(edit_submode_extra, sub_attr);
10142 }
10143 }
10144 length = 0;
10145 }
10146 else
10147#endif
10148 {
10149#ifdef FEAT_VREPLACE
10150 if (State & VREPLACE_FLAG)
10151 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10152 else
10153#endif
10154 if (State & REPLACE_FLAG)
10155 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10156 else if (State & INSERT)
10157 {
10158#ifdef FEAT_RIGHTLEFT
10159 if (p_ri)
10160 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10161#endif
10162 MSG_PUTS_ATTR(_(" INSERT"), attr);
10163 }
10164 else if (restart_edit == 'I')
10165 MSG_PUTS_ATTR(_(" (insert)"), attr);
10166 else if (restart_edit == 'R')
10167 MSG_PUTS_ATTR(_(" (replace)"), attr);
10168 else if (restart_edit == 'V')
10169 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10170#ifdef FEAT_RIGHTLEFT
10171 if (p_hkmap)
10172 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10173# ifdef FEAT_FKMAP
10174 if (p_fkmap)
10175 MSG_PUTS_ATTR(farsi_text_5, attr);
10176# endif
10177#endif
10178#ifdef FEAT_KEYMAP
10179 if (State & LANGMAP)
10180 {
10181# ifdef FEAT_ARABIC
10182 if (curwin->w_p_arab)
10183 MSG_PUTS_ATTR(_(" Arabic"), attr);
10184 else
10185# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010186 if (get_keymap_str(curwin, (char_u *)" (%s)",
10187 NameBuff, MAXPATHL))
10188 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 }
10190#endif
10191 if ((State & INSERT) && p_paste)
10192 MSG_PUTS_ATTR(_(" (paste)"), attr);
10193
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 if (VIsual_active)
10195 {
10196 char *p;
10197
10198 /* Don't concatenate separate words to avoid translation
10199 * problems. */
10200 switch ((VIsual_select ? 4 : 0)
10201 + (VIsual_mode == Ctrl_V) * 2
10202 + (VIsual_mode == 'V'))
10203 {
10204 case 0: p = N_(" VISUAL"); break;
10205 case 1: p = N_(" VISUAL LINE"); break;
10206 case 2: p = N_(" VISUAL BLOCK"); break;
10207 case 4: p = N_(" SELECT"); break;
10208 case 5: p = N_(" SELECT LINE"); break;
10209 default: p = N_(" SELECT BLOCK"); break;
10210 }
10211 MSG_PUTS_ATTR(_(p), attr);
10212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 MSG_PUTS_ATTR(" --", attr);
10214 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010215
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 need_clear = TRUE;
10217 }
10218 if (Recording
10219#ifdef FEAT_INS_EXPAND
10220 && edit_submode == NULL /* otherwise it gets too long */
10221#endif
10222 )
10223 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010224 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 need_clear = TRUE;
10226 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010227
10228 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 if (need_clear || clear_cmdline)
10230 msg_clr_eos();
10231 msg_didout = FALSE; /* overwrite this message */
10232 length = msg_col;
10233 msg_col = 0;
10234 need_wait_return = nwr_save; /* never ask for hit-return for this */
10235 }
10236 else if (clear_cmdline && msg_silent == 0)
10237 /* Clear the whole command line. Will reset "clear_cmdline". */
10238 msg_clr_cmdline();
10239
10240#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 /* In Visual mode the size of the selected area must be redrawn. */
10242 if (VIsual_active)
10243 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244
10245 /* If the last window has no status line, the ruler is after the mode
10246 * message and must be redrawn */
10247 if (redrawing()
10248# ifdef FEAT_WINDOWS
10249 && lastwin->w_status_height == 0
10250# endif
10251 )
10252 win_redr_ruler(lastwin, TRUE);
10253#endif
10254 redraw_cmdline = FALSE;
10255 clear_cmdline = FALSE;
10256
10257 return length;
10258}
10259
10260/*
10261 * Position for a mode message.
10262 */
10263 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010264msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265{
10266 msg_col = 0;
10267 msg_row = Rows - 1;
10268}
10269
10270/*
10271 * Delete mode message. Used when ESC is typed which is expected to end
10272 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010273 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 */
10275 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010276unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277{
10278 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010279 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 */
10281 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10282 redraw_cmdline = TRUE; /* delete mode later */
10283 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010284 clearmode();
10285}
10286
10287/*
10288 * Clear the mode message.
10289 */
10290 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010291clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010292{
10293 msg_pos_mode();
10294 if (Recording)
10295 recording_mode(hl_attr(HLF_CM));
10296 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297}
10298
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010299 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010300recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010301{
10302 MSG_PUTS_ATTR(_("recording"), attr);
10303 if (!shortmess(SHM_RECORDING))
10304 {
10305 char_u s[4];
10306 sprintf((char *)s, " @%c", Recording);
10307 MSG_PUTS_ATTR(s, attr);
10308 }
10309}
10310
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010311#if defined(FEAT_WINDOWS)
10312/*
10313 * Draw the tab pages line at the top of the Vim window.
10314 */
10315 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010316draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010317{
10318 int tabcount = 0;
10319 tabpage_T *tp;
10320 int tabwidth;
10321 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010322 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010323 int attr;
10324 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010325 win_T *cwp;
10326 int wincount;
10327 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010328 int c;
10329 int len;
10330 int attr_sel = hl_attr(HLF_TPS);
10331 int attr_nosel = hl_attr(HLF_TP);
10332 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010333 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010334 int room;
10335 int use_sep_chars = (t_colors < 8
10336#ifdef FEAT_GUI
10337 && !gui.in_use
10338#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010339#ifdef FEAT_TERMGUICOLORS
10340 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010341#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010342 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010343
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010344 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010345
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010346#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010347 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010348 if (gui_use_tabline())
10349 {
10350 gui_update_tabline();
10351 return;
10352 }
10353#endif
10354
10355 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010356 return;
10357
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010358#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010359
10360 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10361 for (scol = 0; scol < Columns; ++scol)
10362 TabPageIdxs[scol] = 0;
10363
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010364 /* Use the 'tabline' option if it's set. */
10365 if (*p_tal != NUL)
10366 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010367 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368
10369 /* Check for an error. If there is one we would loop in redrawing the
10370 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010371 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010372 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010373 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010374 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010375 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010376 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010377 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010379#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010380 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010381 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010382 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010383
Bram Moolenaar238a5642006-02-21 22:12:05 +000010384 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10385 if (tabwidth < 6)
10386 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387
Bram Moolenaar238a5642006-02-21 22:12:05 +000010388 attr = attr_nosel;
10389 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010390 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010391 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10392 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010393 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010394 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010395
Bram Moolenaar238a5642006-02-21 22:12:05 +000010396 if (tp->tp_topframe == topframe)
10397 attr = attr_sel;
10398 if (use_sep_chars && col > 0)
10399 screen_putchar('|', 0, col++, attr);
10400
10401 if (tp->tp_topframe != topframe)
10402 attr = attr_nosel;
10403
10404 screen_putchar(' ', 0, col++, attr);
10405
10406 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010407 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010408 cwp = curwin;
10409 wp = firstwin;
10410 }
10411 else
10412 {
10413 cwp = tp->tp_curwin;
10414 wp = tp->tp_firstwin;
10415 }
10416
10417 modified = FALSE;
10418 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10419 if (bufIsChanged(wp->w_buffer))
10420 modified = TRUE;
10421 if (modified || wincount > 1)
10422 {
10423 if (wincount > 1)
10424 {
10425 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010426 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010427 if (col + len >= Columns - 3)
10428 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010429 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010430#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010431 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010432#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010433 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010434#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010435 );
10436 col += len;
10437 }
10438 if (modified)
10439 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10440 screen_putchar(' ', 0, col++, attr);
10441 }
10442
10443 room = scol - col + tabwidth - 1;
10444 if (room > 0)
10445 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010446 /* Get buffer name in NameBuff[] */
10447 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010448 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010449 len = vim_strsize(NameBuff);
10450 p = NameBuff;
10451#ifdef FEAT_MBYTE
10452 if (has_mbyte)
10453 while (len > room)
10454 {
10455 len -= ptr2cells(p);
10456 mb_ptr_adv(p);
10457 }
10458 else
10459#endif
10460 if (len > room)
10461 {
10462 p += len - room;
10463 len = room;
10464 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010465 if (len > Columns - col - 1)
10466 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010467
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010468 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010469 col += len;
10470 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010471 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010472
10473 /* Store the tab page number in TabPageIdxs[], so that
10474 * jump_to_mouse() knows where each one is. */
10475 ++tabcount;
10476 while (scol < col)
10477 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010478 }
10479
Bram Moolenaar238a5642006-02-21 22:12:05 +000010480 if (use_sep_chars)
10481 c = '_';
10482 else
10483 c = ' ';
10484 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010485
10486 /* Put an "X" for closing the current tab if there are several. */
10487 if (first_tabpage->tp_next != NULL)
10488 {
10489 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10490 TabPageIdxs[Columns - 1] = -999;
10491 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010492 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010493
10494 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10495 * set. */
10496 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010497}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010498
10499/*
10500 * Get buffer name for "buf" into NameBuff[].
10501 * Takes care of special buffer names and translates special characters.
10502 */
10503 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010504get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010505{
10506 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010507 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010508 else
10509 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10510 trans_characters(NameBuff, MAXPATHL);
10511}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010512#endif
10513
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10515/*
10516 * Get the character to use in a status line. Get its attributes in "*attr".
10517 */
10518 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010519fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520{
10521 int fill;
10522 if (is_curwin)
10523 {
10524 *attr = hl_attr(HLF_S);
10525 fill = fill_stl;
10526 }
10527 else
10528 {
10529 *attr = hl_attr(HLF_SNC);
10530 fill = fill_stlnc;
10531 }
10532 /* Use fill when there is highlighting, and highlighting of current
10533 * window differs, or the fillchars differ, or this is not the
10534 * current window */
10535 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10536 || !is_curwin || firstwin == lastwin)
10537 || (fill_stl != fill_stlnc)))
10538 return fill;
10539 if (is_curwin)
10540 return '^';
10541 return '=';
10542}
10543#endif
10544
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010545#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546/*
10547 * Get the character to use in a separator between vertically split windows.
10548 * Get its attributes in "*attr".
10549 */
10550 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010551fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552{
10553 *attr = hl_attr(HLF_C);
10554 if (*attr == 0 && fill_vert == ' ')
10555 return '|';
10556 else
10557 return fill_vert;
10558}
10559#endif
10560
10561/*
10562 * Return TRUE if redrawing should currently be done.
10563 */
10564 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010565redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566{
10567 return (!RedrawingDisabled
10568 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10569}
10570
10571/*
10572 * Return TRUE if printing messages should currently be done.
10573 */
10574 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010575messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 return (!(p_lz && char_avail() && !KeyTyped));
10578}
10579
10580/*
10581 * Show current status info in ruler and various other places
10582 * If always is FALSE, only show ruler if position has changed.
10583 */
10584 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010585showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 if (!always && !redrawing())
10588 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010589#ifdef FEAT_INS_EXPAND
10590 if (pum_visible())
10591 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010592# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010593 /* Don't redraw right now, do it later. */
10594 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010595# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010596 return;
10597 }
10598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010600 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010602 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 else
10605#endif
10606#ifdef FEAT_CMDL_INFO
10607 win_redr_ruler(curwin, always);
10608#endif
10609
10610#ifdef FEAT_TITLE
10611 if (need_maketitle
10612# ifdef FEAT_STL_OPT
10613 || (p_icon && (stl_syntax & STL_IN_ICON))
10614 || (p_title && (stl_syntax & STL_IN_TITLE))
10615# endif
10616 )
10617 maketitle();
10618#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010619#ifdef FEAT_WINDOWS
10620 /* Redraw the tab pages line if needed. */
10621 if (redraw_tabline)
10622 draw_tabline();
10623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624}
10625
10626#ifdef FEAT_CMDL_INFO
10627 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010628win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010630#define RULER_BUF_LEN 70
10631 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 int row;
10633 int fillchar;
10634 int attr;
10635 int empty_line = FALSE;
10636 colnr_T virtcol;
10637 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010638 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010640#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 int this_ru_col;
10642 int off = 0;
10643 int width = Columns;
10644# define WITH_OFF(x) x
10645# define WITH_WIDTH(x) x
10646#else
10647# define WITH_OFF(x) 0
10648# define WITH_WIDTH(x) Columns
10649# define this_ru_col ru_col
10650#endif
10651
10652 /* If 'ruler' off or redrawing disabled, don't do anything */
10653 if (!p_ru)
10654 return;
10655
10656 /*
10657 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10658 * after deleting lines, before cursor.lnum is corrected.
10659 */
10660 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10661 return;
10662
10663#ifdef FEAT_INS_EXPAND
10664 /* Don't draw the ruler while doing insert-completion, it might overwrite
10665 * the (long) mode message. */
10666# ifdef FEAT_WINDOWS
10667 if (wp == lastwin && lastwin->w_status_height == 0)
10668# endif
10669 if (edit_submode != NULL)
10670 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010671 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10672 if (pum_visible())
10673 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674#endif
10675
10676#ifdef FEAT_STL_OPT
10677 if (*p_ruf)
10678 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010679 int save_called_emsg = called_emsg;
10680
10681 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010683 if (called_emsg)
10684 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010685 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 return;
10688 }
10689#endif
10690
10691 /*
10692 * Check if not in Insert mode and the line is empty (will show "0-1").
10693 */
10694 if (!(State & INSERT)
10695 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10696 empty_line = TRUE;
10697
10698 /*
10699 * Only draw the ruler when something changed.
10700 */
10701 validate_virtcol_win(wp);
10702 if ( redraw_cmdline
10703 || always
10704 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10705 || wp->w_cursor.col != wp->w_ru_cursor.col
10706 || wp->w_virtcol != wp->w_ru_virtcol
10707#ifdef FEAT_VIRTUALEDIT
10708 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10709#endif
10710 || wp->w_topline != wp->w_ru_topline
10711 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10712#ifdef FEAT_DIFF
10713 || wp->w_topfill != wp->w_ru_topfill
10714#endif
10715 || empty_line != wp->w_ru_empty)
10716 {
10717 cursor_off();
10718#ifdef FEAT_WINDOWS
10719 if (wp->w_status_height)
10720 {
10721 row = W_WINROW(wp) + wp->w_height;
10722 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 off = W_WINCOL(wp);
10724 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 }
10726 else
10727#endif
10728 {
10729 row = Rows - 1;
10730 fillchar = ' ';
10731 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010732#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 width = Columns;
10734 off = 0;
10735#endif
10736 }
10737
10738 /* In list mode virtcol needs to be recomputed */
10739 virtcol = wp->w_virtcol;
10740 if (wp->w_p_list && lcs_tab1 == NUL)
10741 {
10742 wp->w_p_list = FALSE;
10743 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10744 wp->w_p_list = TRUE;
10745 }
10746
10747 /*
10748 * Some sprintfs return the length, some return a pointer.
10749 * To avoid portability problems we use strlen() here.
10750 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010751 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10753 ? 0L
10754 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010755 len = STRLEN(buffer);
10756 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10758 (int)virtcol + 1);
10759
10760 /*
10761 * Add a "50%" if there is room for it.
10762 * On the last line, don't print in the last column (scrolls the
10763 * screen up on some terminals).
10764 */
10765 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010766 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 o = i + vim_strsize(buffer + i + 1);
10768#ifdef FEAT_WINDOWS
10769 if (wp->w_status_height == 0) /* can't use last char of screen */
10770#endif
10771 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010772#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 this_ru_col = ru_col - (Columns - width);
10774 if (this_ru_col < 0)
10775 this_ru_col = 0;
10776#endif
10777 /* Never use more than half the window/screen width, leave the other
10778 * half for the filename. */
10779 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10780 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10781 if (this_ru_col + o < WITH_WIDTH(width))
10782 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010783 /* need at least 3 chars left for get_rel_pos() + NUL */
10784 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 {
10786#ifdef FEAT_MBYTE
10787 if (has_mbyte)
10788 i += (*mb_char2bytes)(fillchar, buffer + i);
10789 else
10790#endif
10791 buffer[i++] = fillchar;
10792 ++o;
10793 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010794 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 }
10796 /* Truncate at window boundary. */
10797#ifdef FEAT_MBYTE
10798 if (has_mbyte)
10799 {
10800 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010801 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 {
10803 o += (*mb_ptr2cells)(buffer + i);
10804 if (this_ru_col + o > WITH_WIDTH(width))
10805 {
10806 buffer[i] = NUL;
10807 break;
10808 }
10809 }
10810 }
10811 else
10812#endif
10813 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10814 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10815
10816 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10817 i = redraw_cmdline;
10818 screen_fill(row, row + 1,
10819 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10820 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10821 fillchar, fillchar, attr);
10822 /* don't redraw the cmdline because of showing the ruler */
10823 redraw_cmdline = i;
10824 wp->w_ru_cursor = wp->w_cursor;
10825 wp->w_ru_virtcol = wp->w_virtcol;
10826 wp->w_ru_empty = empty_line;
10827 wp->w_ru_topline = wp->w_topline;
10828 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10829#ifdef FEAT_DIFF
10830 wp->w_ru_topfill = wp->w_topfill;
10831#endif
10832 }
10833}
10834#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010835
10836#if defined(FEAT_LINEBREAK) || defined(PROTO)
10837/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010838 * Return the width of the 'number' and 'relativenumber' column.
10839 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010840 * Otherwise it depends on 'numberwidth' and the line count.
10841 */
10842 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010843number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010844{
10845 int n;
10846 linenr_T lnum;
10847
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010848 if (wp->w_p_rnu && !wp->w_p_nu)
10849 /* cursor line shows "0" */
10850 lnum = wp->w_height;
10851 else
10852 /* cursor line shows absolute line number */
10853 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010854
Bram Moolenaar6b314672015-03-20 15:42:10 +010010855 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010856 return wp->w_nrwidth_width;
10857 wp->w_nrwidth_line_count = lnum;
10858
10859 n = 0;
10860 do
10861 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010862 lnum /= 10;
10863 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010864 } while (lnum > 0);
10865
10866 /* 'numberwidth' gives the minimal width plus one */
10867 if (n < wp->w_p_nuw - 1)
10868 n = wp->w_p_nuw - 1;
10869
10870 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010871 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010872 return n;
10873}
10874#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010875
10876/*
10877 * Return the current cursor column. This is the actual position on the
10878 * screen. First column is 0.
10879 */
10880 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010881screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010882{
10883 return screen_cur_col;
10884}
10885
10886/*
10887 * Return the current cursor row. This is the actual position on the screen.
10888 * First row is 0.
10889 */
10890 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010891screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010892{
10893 return screen_cur_row;
10894}