blob: a7e881e3687770d753efe22325dfb3c31577a2d6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaar06f1ed22017-06-18 22:41:03 +0200127static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static 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
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200188#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
189/* Can limit syntax highlight time to 'redrawtime'. */
190# define SYN_TIME_LIMIT 1
191#endif
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193/*
194 * Redraw the current window later, with update_screen(type).
195 * Set must_redraw only if not already set to a higher value.
196 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
197 */
198 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100199redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 redraw_win_later(curwin, type);
202}
203
204 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100205redraw_win_later(
206 win_T *wp,
207 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 if (wp->w_redr_type < type)
210 {
211 wp->w_redr_type = type;
212 if (type >= NOT_VALID)
213 wp->w_lines_valid = 0;
214 if (must_redraw < type) /* must_redraw is the maximum of all windows */
215 must_redraw = type;
216 }
217}
218
219/*
220 * Force a complete redraw later. Also resets the highlighting. To be used
221 * after executing a shell command that messes up the screen.
222 */
223 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100224redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000227#ifdef FEAT_GUI
228 if (gui.in_use)
229 /* Use a code that will reset gui.highlight_mask in
230 * gui_stop_highlight(). */
231 screen_attr = HL_ALL + 1;
232 else
233#endif
234 /* Use attributes that is very unlikely to appear in text. */
235 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236}
237
238/*
239 * Mark all windows to be redrawn later.
240 */
241 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100242redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
244 win_T *wp;
245
246 FOR_ALL_WINDOWS(wp)
247 {
248 redraw_win_later(wp, type);
249 }
250}
251
252/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000253 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 */
255 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100256redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
258 redraw_buf_later(curbuf, type);
259}
260
261 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100262redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263{
264 win_T *wp;
265
266 FOR_ALL_WINDOWS(wp)
267 {
268 if (wp->w_buffer == buf)
269 redraw_win_later(wp, type);
270 }
271}
272
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200273 void
274redraw_buf_and_status_later(buf_T *buf, int type)
275{
276 win_T *wp;
277
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200278#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200279 if (wild_menu_showing != 0)
280 /* Don't redraw while the command line completion is displayed, it
281 * would disappear. */
282 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200283#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284 FOR_ALL_WINDOWS(wp)
285 {
286 if (wp->w_buffer == buf)
287 {
288 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200289#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200290 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200291#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200292 }
293 }
294}
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 * Redraw as soon as possible. When the command line is not scrolled redraw
298 * right away and restore what was on the command line.
299 * Return a code indicating what happened.
300 */
301 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100302redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303{
304 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 int r;
307 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 schar_T *screenline; /* copy from ScreenLines[] */
309 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#ifdef FEAT_MBYTE
311 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200312 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200314 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315#endif
316
317 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 return ret;
320
321 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 if (screenline == NULL || screenattr == NULL)
328 ret = 2;
329#ifdef FEAT_MBYTE
330 if (enc_utf8)
331 {
332 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200333 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 if (screenlineUC == NULL)
335 ret = 2;
336 for (i = 0; i < p_mco; ++i)
337 {
338 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 if (screenlineC[i] == NULL)
341 ret = 2;
342 }
343 }
344 if (enc_dbcs == DBCS_JPNU)
345 {
346 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 if (screenline2 == NULL)
349 ret = 2;
350 }
351#endif
352
353 if (ret != 2)
354 {
355 /* Save the text displayed in the command line area. */
356 for (r = 0; r < rows; ++r)
357 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 (size_t)cols * sizeof(schar_T));
361 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364#ifdef FEAT_MBYTE
365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379#endif
380 }
381
382 update_screen(0);
383 ret = 3;
384
385 if (must_redraw == 0)
386 {
387 int off = (int)(current_ScreenLine - ScreenLines);
388
389 /* Restore the text displayed in the command line area. */
390 for (r = 0; r < rows; ++r)
391 {
392 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenline + r * cols,
394 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenattr + r * cols,
397 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398#ifdef FEAT_MBYTE
399 if (enc_utf8)
400 {
401 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineUC + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 for (i = 0; i < p_mco; ++i)
405 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenlineC[i] + r * cols,
407 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408 }
409 if (enc_dbcs == DBCS_JPNU)
410 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200411 screenline2 + r * cols,
412 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416 ret = 4;
417 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419
420 vim_free(screenline);
421 vim_free(screenattr);
422#ifdef FEAT_MBYTE
423 if (enc_utf8)
424 {
425 vim_free(screenlineUC);
426 for (i = 0; i < p_mco; ++i)
427 vim_free(screenlineC[i]);
428 }
429 if (enc_dbcs == DBCS_JPNU)
430 vim_free(screenline2);
431#endif
432
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200433 /* Show the intro message when appropriate. */
434 maybe_intro_message();
435
436 setcursor();
437
Bram Moolenaar2951b772013-07-03 12:45:31 +0200438 return ret;
439}
440
441/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 * Invoked after an asynchronous callback is called.
443 * If an echo command was used the cursor needs to be put back where
444 * it belongs. If highlighting was changed a redraw is needed.
445 */
446 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200447redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100448{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
450 ; /* do nothing */
451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200453 /* Redrawing only works when the screen didn't scroll. Don't clear
454 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200455 if (msg_scrolled == 0
456#ifdef FEAT_WILDMENU
457 && wild_menu_showing == 0
458#endif
459 )
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200461 /* Redraw in the same position, so that the user can continue
462 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200463 redrawcmdline_ex(FALSE);
464 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200465 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 /* keep the command line if possible */
468 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 setcursor();
470 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471 cursor_on();
472 out_flush();
473#ifdef FEAT_GUI
474 if (gui.in_use)
475 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200476 /* Don't update the cursor when it is blinking and off to avoid
477 * flicker. */
478 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200479 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100480 gui_mch_flush();
481 }
482#endif
483}
484
485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 * Changed something in the current window, at buffer line "lnum", that
487 * requires that line and possibly other lines to be redrawn.
488 * Used when entering/leaving Insert mode with the cursor on a folded line.
489 * Used to remove the "$" from a change command.
490 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
491 * may become invalid and the whole window will have to be redrawn.
492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100494redrawWinline(
495 linenr_T lnum,
496 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498#ifdef FEAT_FOLDING
499 int i;
500#endif
501
502 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
503 curwin->w_redraw_top = lnum;
504 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
505 curwin->w_redraw_bot = lnum;
506 redraw_later(VALID);
507
508#ifdef FEAT_FOLDING
509 if (invalid)
510 {
511 /* A w_lines[] entry for this lnum has become invalid. */
512 i = find_wl_entry(curwin, lnum);
513 if (i >= 0)
514 curwin->w_lines[i].wl_valid = FALSE;
515 }
516#endif
517}
518
519/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200520 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
522 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100523update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 redraw_curbuf_later(type);
526 update_screen(type);
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Based on the current value of curwin->w_topline, transfer a screenfull
531 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
532 */
533 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200534update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200536 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 win_T *wp;
538 static int did_intro = FALSE;
539#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
540 int did_one;
541#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200542#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200543 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200544 int gui_cursor_col;
545 int gui_cursor_row;
546#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000549 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 if (!screen_valid(TRUE))
551 return;
552
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200553 if (type == VALID_NO_UPDATE)
554 {
555 no_update = TRUE;
556 type = 0;
557 }
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (must_redraw)
560 {
561 if (type < must_redraw) /* use maximal type */
562 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000563
564 /* must_redraw is reset here, so that when we run into some weird
565 * reason to redraw while busy redrawing (e.g., asynchronous
566 * scrolling), or update_topline() in win_update() will cause a
567 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 must_redraw = 0;
569 }
570
571 /* Need to update w_lines[]. */
572 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
573 type = NOT_VALID;
574
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000575 /* Postpone the redrawing when it's not needed and when being called
576 * recursively. */
577 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 redraw_later(type); /* remember type for next time */
580 must_redraw = type;
581 if (type > INVERTED_ALL)
582 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
583 return;
584 }
585
586 updating_screen = TRUE;
587#ifdef FEAT_SYN_HL
588 ++display_tick; /* let syntax code know we're in a next round of
589 * display updating */
590#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200591 if (no_update)
592 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 /*
595 * if the screen was scrolled up when displaying a message, scroll it down
596 */
597 if (msg_scrolled)
598 {
599 clear_cmdline = TRUE;
600 if (msg_scrolled > Rows - 5) /* clearing is faster */
601 type = CLEAR;
602 else if (type != CLEAR)
603 {
604 check_for_delay(FALSE);
605 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
606 type = CLEAR;
607 FOR_ALL_WINDOWS(wp)
608 {
609 if (W_WINROW(wp) < msg_scrolled)
610 {
611 if (W_WINROW(wp) + wp->w_height > msg_scrolled
612 && wp->w_redr_type < REDRAW_TOP
613 && wp->w_lines_valid > 0
614 && wp->w_topline == wp->w_lines[0].wl_lnum)
615 {
616 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
617 wp->w_redr_type = REDRAW_TOP;
618 }
619 else
620 {
621 wp->w_redr_type = NOT_VALID;
622#ifdef FEAT_WINDOWS
623 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
624 <= msg_scrolled)
625 wp->w_redr_status = TRUE;
626#endif
627 }
628 }
629 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200630 if (!no_update)
631 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000632#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000633 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
636 msg_scrolled = 0;
637 need_wait_return = FALSE;
638 }
639
640 /* reset cmdline_row now (may have been changed temporarily) */
641 compute_cmdrow();
642
643 /* Check for changed highlighting */
644 if (need_highlight_changed)
645 highlight_changed();
646
647 if (type == CLEAR) /* first clear screen */
648 {
649 screenclear(); /* will reset clear_cmdline */
650 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200651 /* must_redraw may be set indirectly, avoid another redraw later */
652 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654
655 if (clear_cmdline) /* going to clear cmdline (done below) */
656 check_for_delay(FALSE);
657
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000658#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200659 /* Force redraw when width of 'number' or 'relativenumber' column
660 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000661 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200662 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
663 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000664 curwin->w_redr_type = NOT_VALID;
665#endif
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /*
668 * Only start redrawing if there is really something to do.
669 */
670 if (type == INVERTED)
671 update_curswant();
672 if (curwin->w_redr_type < type
673 && !((type == VALID
674 && curwin->w_lines[0].wl_valid
675#ifdef FEAT_DIFF
676 && curwin->w_topfill == curwin->w_old_topfill
677 && curwin->w_botfill == curwin->w_old_botfill
678#endif
679 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000681 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
683 && curwin->w_old_visual_mode == VIsual_mode
684 && (curwin->w_valid & VALID_VIRTCOL)
685 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ))
687 curwin->w_redr_type = type;
688
Bram Moolenaar5a305422006-04-28 22:38:25 +0000689#ifdef FEAT_WINDOWS
690 /* Redraw the tab pages line if needed. */
691 if (redraw_tabline || type >= NOT_VALID)
692 draw_tabline();
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_SYN_HL
696 /*
697 * Correct stored syntax highlighting info for changes in each displayed
698 * buffer. Each buffer must only be done once.
699 */
700 FOR_ALL_WINDOWS(wp)
701 {
702 if (wp->w_buffer->b_mod_set)
703 {
704# ifdef FEAT_WINDOWS
705 win_T *wwp;
706
707 /* Check if we already did this buffer. */
708 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
709 if (wwp->w_buffer == wp->w_buffer)
710 break;
711# endif
712 if (
713# ifdef FEAT_WINDOWS
714 wwp == wp &&
715# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200716 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 syn_stack_apply_changes(wp->w_buffer);
718 }
719 }
720#endif
721
722 /*
723 * Go from top to bottom through the windows, redrawing the ones that need
724 * it.
725 */
726#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
727 did_one = FALSE;
728#endif
729#ifdef FEAT_SEARCH_EXTRA
730 search_hl.rm.regprog = NULL;
731#endif
732 FOR_ALL_WINDOWS(wp)
733 {
734 if (wp->w_redr_type != 0)
735 {
736 cursor_off();
737#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
738 if (!did_one)
739 {
740 did_one = TRUE;
741# ifdef FEAT_SEARCH_EXTRA
742 start_search_hl();
743# endif
744# ifdef FEAT_CLIPBOARD
745 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200746 if (clip_star.available && clip_isautosel_star())
747 clip_update_selection(&clip_star);
748 if (clip_plus.available && clip_isautosel_plus())
749 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750# endif
751#ifdef FEAT_GUI
752 /* Remove the cursor before starting to do anything, because
753 * scrolling may make it difficult to redraw the text under
754 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200755 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200756 {
757 gui_cursor_col = gui.cursor_col;
758 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200760 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#endif
763 }
764#endif
765 win_update(wp);
766 }
767
768#ifdef FEAT_WINDOWS
769 /* redraw status line after the window to minimize cursor movement */
770 if (wp->w_redr_status)
771 {
772 cursor_off();
773 win_redr_status(wp);
774 }
775#endif
776 }
777#if defined(FEAT_SEARCH_EXTRA)
778 end_search_hl();
779#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100780#ifdef FEAT_INS_EXPAND
781 /* May need to redraw the popup menu. */
782 if (pum_visible())
783 pum_redraw();
784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786#ifdef FEAT_WINDOWS
787 /* Reset b_mod_set flags. Going through all windows is probably faster
788 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200789 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 wp->w_buffer->b_mod_set = FALSE;
791#else
792 curbuf->b_mod_set = FALSE;
793#endif
794
795 updating_screen = FALSE;
796#ifdef FEAT_GUI
797 gui_may_resize_shell();
798#endif
799
800 /* Clear or redraw the command line. Done last, because scrolling may
801 * mess up the command line. */
802 if (clear_cmdline || redraw_cmdline)
803 showmode();
804
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200805 if (no_update)
806 --no_win_do_lines_ins;
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200809 if (!did_intro)
810 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 did_intro = TRUE;
812
813#ifdef FEAT_GUI
814 /* Redraw the cursor and update the scrollbars when all screen updating is
815 * done. */
816 if (gui.in_use)
817 {
818 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200819 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 {
821 /* Put the GUI position where the cursor was, gui_update_cursor()
822 * uses that. */
823 gui.col = gui_cursor_col;
824 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200825# ifdef FEAT_MBYTE
826 gui.col = mb_fix_col(gui.col, gui.row);
827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200829 screen_cur_col = gui.col;
830 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui_update_scrollbars(FALSE);
833 }
834#endif
835}
836
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100837#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
838/*
839 * Prepare for updating one or more windows.
840 * Caller must check for "updating_screen" already set to avoid recursiveness.
841 */
842 static void
843update_prepare(void)
844{
845 cursor_off();
846 updating_screen = TRUE;
847#ifdef FEAT_GUI
848 /* Remove the cursor before starting to do anything, because scrolling may
849 * make it difficult to redraw the text under it. */
850 if (gui.in_use)
851 gui_undraw_cursor();
852#endif
853#ifdef FEAT_SEARCH_EXTRA
854 start_search_hl();
855#endif
856}
857
858/*
859 * Finish updating one or more windows.
860 */
861 static void
862update_finish(void)
863{
864 if (redraw_cmdline)
865 showmode();
866
867# ifdef FEAT_SEARCH_EXTRA
868 end_search_hl();
869# endif
870
871 updating_screen = FALSE;
872
873# ifdef FEAT_GUI
874 gui_may_resize_shell();
875
876 /* Redraw the cursor and update the scrollbars when all screen updating is
877 * done. */
878 if (gui.in_use)
879 {
880 out_flush(); /* required before updating the cursor */
881 gui_update_cursor(FALSE, FALSE);
882 gui_update_scrollbars(FALSE);
883 }
884# endif
885}
886#endif
887
Bram Moolenaar860cae12010-06-05 23:22:07 +0200888#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200889/*
890 * Return TRUE if the cursor line in window "wp" may be concealed, according
891 * to the 'concealcursor' option.
892 */
893 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100894conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200895{
896 int c;
897
898 if (*wp->w_p_cocu == NUL)
899 return FALSE;
900 if (get_real_state() & VISUAL)
901 c = 'v';
902 else if (State & INSERT)
903 c = 'i';
904 else if (State & NORMAL)
905 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200906 else if (State & CMDLINE)
907 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908 else
909 return FALSE;
910 return vim_strchr(wp->w_p_cocu, c) != NULL;
911}
912
913/*
914 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
915 */
916 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100917conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918{
919 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
920 {
921 need_cursor_line_redraw = TRUE;
922 /* Need to recompute cursor column, e.g., when starting Visual mode
923 * without concealing. */
924 curs_columns(TRUE);
925 }
926}
927
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930{
931 int row;
932 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200933#ifdef SYN_TIME_LIMIT
934 proftime_T syntax_tm;
935#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936
Bram Moolenaar908be432016-05-24 10:51:30 +0200937 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100938 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200939 return;
940
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200942 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200944#ifdef SYN_TIME_LIMIT
945 /* Set the time limit to 'redrawtime'. */
946 profile_setlimit(p_rdt, &syntax_tm);
947#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100948 update_prepare();
949
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 row = 0;
951 for (j = 0; j < wp->w_lines_valid; ++j)
952 {
953 if (lnum == wp->w_lines[j].wl_lnum)
954 {
955 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200956# ifdef FEAT_SEARCH_EXTRA
957 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200958 start_search_hl();
959 prepare_search_hl(wp, lnum);
960# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200961 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
962#ifdef SYN_TIME_LIMIT
963 &syntax_tm
964#else
965 NULL
966#endif
967 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968# if defined(FEAT_SEARCH_EXTRA)
969 end_search_hl();
970# endif
971 break;
972 }
973 row += wp->w_lines[j].wl_size;
974 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100975
976 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200977 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200978 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980#endif
981
982#if defined(FEAT_SIGNS) || defined(PROTO)
983 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100984update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 win_T *wp;
987 int doit = FALSE;
988
989# ifdef FEAT_FOLDING
990 win_foldinfo.fi_level = 0;
991# endif
992
993 /* update/delete a specific mark */
994 FOR_ALL_WINDOWS(wp)
995 {
996 if (buf != NULL && lnum > 0)
997 {
998 if (wp->w_buffer == buf && lnum >= wp->w_topline
999 && lnum < wp->w_botline)
1000 {
1001 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1002 wp->w_redraw_top = lnum;
1003 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1004 wp->w_redraw_bot = lnum;
1005 redraw_win_later(wp, VALID);
1006 }
1007 }
1008 else
1009 redraw_win_later(wp, VALID);
1010 if (wp->w_redr_type != 0)
1011 doit = TRUE;
1012 }
1013
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001014 /* Return when there is nothing to do, screen updating is already
1015 * happening (recursive call) or still starting up. */
1016 if (!doit || updating_screen
1017#ifdef FEAT_GUI
1018 || gui.starting
1019#endif
1020 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return;
1022
1023 /* update all windows that need updating */
1024 update_prepare();
1025
1026# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001027 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 {
1029 if (wp->w_redr_type != 0)
1030 win_update(wp);
1031 if (wp->w_redr_status)
1032 win_redr_status(wp);
1033 }
1034# else
1035 if (curwin->w_redr_type != 0)
1036 win_update(curwin);
1037# endif
1038
1039 update_finish();
1040}
1041#endif
1042
1043
1044#if defined(FEAT_GUI) || defined(PROTO)
1045/*
1046 * Update a single window, its status line and maybe the command line msg.
1047 * Used for the GUI scrollbar.
1048 */
1049 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001050updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001052 /* return if already busy updating */
1053 if (updating_screen)
1054 return;
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 update_prepare();
1057
1058#ifdef FEAT_CLIPBOARD
1059 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001060 if (clip_star.available && clip_isautosel_star())
1061 clip_update_selection(&clip_star);
1062 if (clip_plus.available && clip_isautosel_plus())
1063 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001069 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001070 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001071 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (wp->w_redr_status
1074# ifdef FEAT_CMDL_INFO
1075 || p_ru
1076# endif
1077# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001078 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079# endif
1080 )
1081 win_redr_status(wp);
1082#endif
1083
1084 update_finish();
1085}
1086#endif
1087
1088/*
1089 * Update a single window.
1090 *
1091 * This may cause the windows below it also to be redrawn (when clearing the
1092 * screen or scrolling lines).
1093 *
1094 * How the window is redrawn depends on wp->w_redr_type. Each type also
1095 * implies the one below it.
1096 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001097 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1099 * INVERTED redraw the changed part of the Visual area
1100 * INVERTED_ALL redraw the whole Visual area
1101 * VALID 1. scroll up/down to adjust for a changed w_topline
1102 * 2. update lines at the top when scrolled down
1103 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001104 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * b_mod_top and b_mod_bot.
1106 * - if wp->w_redraw_top non-zero, redraw lines between
1107 * wp->w_redraw_top and wp->w_redr_bot.
1108 * - continue redrawing when syntax status is invalid.
1109 * 4. if scrolled up, update lines at the bottom.
1110 * This results in three areas that may need updating:
1111 * top: from first row to top_end (when scrolled down)
1112 * mid: from mid_start to mid_end (update inversion or changed text)
1113 * bot: from bot_start to last row (when scrolled up)
1114 */
1115 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001116win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 buf_T *buf = wp->w_buffer;
1119 int type;
1120 int top_end = 0; /* Below last row of the top area that needs
1121 updating. 0 when no top area updating. */
1122 int mid_start = 999;/* first row of the mid area that needs
1123 updating. 999 when no mid area updating. */
1124 int mid_end = 0; /* Below last row of the mid area that needs
1125 updating. 0 when no mid area updating. */
1126 int bot_start = 999;/* first row of the bot area that needs
1127 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 int scrolled_down = FALSE; /* TRUE when scrolled down when
1129 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001131 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int top_to_mod = FALSE; /* redraw above mod_top */
1133#endif
1134
1135 int row; /* current window row to display */
1136 linenr_T lnum; /* current buffer lnum to display */
1137 int idx; /* current index in w_lines[] */
1138 int srow; /* starting row of the current line */
1139
1140 int eof = FALSE; /* if TRUE, we hit the end of the file */
1141 int didline = FALSE; /* if TRUE, we finished the last line */
1142 int i;
1143 long j;
1144 static int recursive = FALSE; /* being called recursively */
1145 int old_botline = wp->w_botline;
1146#ifdef FEAT_FOLDING
1147 long fold_count;
1148#endif
1149#ifdef FEAT_SYN_HL
1150 /* remember what happened to the previous line, to know if
1151 * check_visual_highlight() can be used */
1152#define DID_NONE 1 /* didn't update a line */
1153#define DID_LINE 2 /* updated a normal line */
1154#define DID_FOLD 3 /* updated a folded line */
1155 int did_update = DID_NONE;
1156 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1157#endif
1158 linenr_T mod_top = 0;
1159 linenr_T mod_bot = 0;
1160#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1161 int save_got_int;
1162#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001163#ifdef SYN_TIME_LIMIT
1164 proftime_T syntax_tm;
1165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 type = wp->w_redr_type;
1168
1169 if (type == NOT_VALID)
1170 {
1171#ifdef FEAT_WINDOWS
1172 wp->w_redr_status = TRUE;
1173#endif
1174 wp->w_lines_valid = 0;
1175 }
1176
1177 /* Window is zero-height: nothing to draw. */
1178 if (wp->w_height == 0)
1179 {
1180 wp->w_redr_type = 0;
1181 return;
1182 }
1183
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001184#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 /* Window is zero-width: Only need to draw the separator. */
1186 if (wp->w_width == 0)
1187 {
1188 /* draw the vertical separator right of this window */
1189 draw_vsep_win(wp, 0);
1190 wp->w_redr_type = 0;
1191 return;
1192 }
1193#endif
1194
1195#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001196 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#endif
1198
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001199#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001200 /* Force redraw when width of 'number' or 'relativenumber' column
1201 * changes. */
1202 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001203 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001204 {
1205 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001206 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001207 }
1208 else
1209#endif
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1212 {
1213 /*
1214 * When there are both inserted/deleted lines and specific lines to be
1215 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1216 * everything (only happens when redrawing is off for while).
1217 */
1218 type = NOT_VALID;
1219 }
1220 else
1221 {
1222 /*
1223 * Set mod_top to the first line that needs displaying because of
1224 * changes. Set mod_bot to the first line after the changes.
1225 */
1226 mod_top = wp->w_redraw_top;
1227 if (wp->w_redraw_bot != 0)
1228 mod_bot = wp->w_redraw_bot + 1;
1229 else
1230 mod_bot = 0;
1231 wp->w_redraw_top = 0; /* reset for next time */
1232 wp->w_redraw_bot = 0;
1233 if (buf->b_mod_set)
1234 {
1235 if (mod_top == 0 || mod_top > buf->b_mod_top)
1236 {
1237 mod_top = buf->b_mod_top;
1238#ifdef FEAT_SYN_HL
1239 /* Need to redraw lines above the change that may be included
1240 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (mod_top < 1)
1245 mod_top = 1;
1246 }
1247#endif
1248 }
1249 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1250 mod_bot = buf->b_mod_bot;
1251
1252#ifdef FEAT_SEARCH_EXTRA
1253 /* When 'hlsearch' is on and using a multi-line search pattern, a
1254 * change in one line may make the Search highlighting in a
1255 * previous line invalid. Simple solution: redraw all visible
1256 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001259 if (search_hl.rm.regprog != NULL
1260 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001262 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 {
1264 cur = wp->w_match_head;
1265 while (cur != NULL)
1266 {
1267 if (cur->match.regprog != NULL
1268 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001269 {
1270 top_to_mod = TRUE;
1271 break;
1272 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 cur = cur->next;
1274 }
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277 }
1278#ifdef FEAT_FOLDING
1279 if (mod_top != 0 && hasAnyFolding(wp))
1280 {
1281 linenr_T lnumt, lnumb;
1282
1283 /*
1284 * A change in a line can cause lines above it to become folded or
1285 * unfolded. Find the top most buffer line that may be affected.
1286 * If the line was previously folded and displayed, get the first
1287 * line of that fold. If the line is folded now, get the first
1288 * folded line. Use the minimum of these two.
1289 */
1290
1291 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1292 * the line below it. If there is no valid entry, use w_topline.
1293 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1294 * to this line. If there is no valid entry, use MAXLNUM. */
1295 lnumt = wp->w_topline;
1296 lnumb = MAXLNUM;
1297 for (i = 0; i < wp->w_lines_valid; ++i)
1298 if (wp->w_lines[i].wl_valid)
1299 {
1300 if (wp->w_lines[i].wl_lastlnum < mod_top)
1301 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1302 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1303 {
1304 lnumb = wp->w_lines[i].wl_lnum;
1305 /* When there is a fold column it might need updating
1306 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001307 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ++lnumb;
1309 }
1310 }
1311
1312 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1313 if (mod_top > lnumt)
1314 mod_top = lnumt;
1315
1316 /* Now do the same for the bottom line (one above mod_bot). */
1317 --mod_bot;
1318 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1319 ++mod_bot;
1320 if (mod_bot < lnumb)
1321 mod_bot = lnumb;
1322 }
1323#endif
1324
1325 /* When a change starts above w_topline and the end is below
1326 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001327 * If the end of the change is above w_topline: do like no change was
1328 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (mod_top != 0 && mod_top < wp->w_topline)
1330 {
1331 if (mod_bot > wp->w_topline)
1332 mod_top = wp->w_topline;
1333#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001334 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 top_end = 1;
1336#endif
1337 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001338
1339 /* When line numbers are displayed need to redraw all lines below
1340 * inserted/deleted lines. */
1341 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1342 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344
1345 /*
1346 * When only displaying the lines at the top, set top_end. Used when
1347 * window has scrolled down for msg_scrolled.
1348 */
1349 if (type == REDRAW_TOP)
1350 {
1351 j = 0;
1352 for (i = 0; i < wp->w_lines_valid; ++i)
1353 {
1354 j += wp->w_lines[i].wl_size;
1355 if (j >= wp->w_upd_rows)
1356 {
1357 top_end = j;
1358 break;
1359 }
1360 }
1361 if (top_end == 0)
1362 /* not found (cannot happen?): redraw everything */
1363 type = NOT_VALID;
1364 else
1365 /* top area defined, the rest is VALID */
1366 type = VALID;
1367 }
1368
Bram Moolenaar367329b2007-08-30 11:53:22 +00001369 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001370 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1371 * non-zero and thus not FALSE) will indicate that screenclear() was not
1372 * called. */
1373 if (screen_cleared)
1374 screen_cleared = MAYBE;
1375
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 /*
1377 * If there are no changes on the screen that require a complete redraw,
1378 * handle three cases:
1379 * 1: we are off the top of the screen by a few lines: scroll down
1380 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1381 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1382 * w_lines[] that needs updating.
1383 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001384 if ((type == VALID || type == SOME_VALID
1385 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#ifdef FEAT_DIFF
1387 && !wp->w_botfill && !wp->w_old_botfill
1388#endif
1389 )
1390 {
1391 if (mod_top != 0 && wp->w_topline == mod_top)
1392 {
1393 /*
1394 * w_topline is the first changed line, the scrolling will be done
1395 * further down.
1396 */
1397 }
1398 else if (wp->w_lines[0].wl_valid
1399 && (wp->w_topline < wp->w_lines[0].wl_lnum
1400#ifdef FEAT_DIFF
1401 || (wp->w_topline == wp->w_lines[0].wl_lnum
1402 && wp->w_topfill > wp->w_old_topfill)
1403#endif
1404 ))
1405 {
1406 /*
1407 * New topline is above old topline: May scroll down.
1408 */
1409#ifdef FEAT_FOLDING
1410 if (hasAnyFolding(wp))
1411 {
1412 linenr_T ln;
1413
1414 /* count the number of lines we are off, counting a sequence
1415 * of folded lines as one */
1416 j = 0;
1417 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1418 {
1419 ++j;
1420 if (j >= wp->w_height - 2)
1421 break;
1422 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1423 }
1424 }
1425 else
1426#endif
1427 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1428 if (j < wp->w_height - 2) /* not too far off */
1429 {
1430 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1431#ifdef FEAT_DIFF
1432 /* insert extra lines for previously invisible filler lines */
1433 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1434 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1435 - wp->w_old_topfill;
1436#endif
1437 if (i < wp->w_height - 2) /* less than a screen off */
1438 {
1439 /*
1440 * Try to insert the correct number of lines.
1441 * If not the last window, delete the lines at the bottom.
1442 * win_ins_lines may fail when the terminal can't do it.
1443 */
1444 if (i > 0)
1445 check_for_delay(FALSE);
1446 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1447 {
1448 if (wp->w_lines_valid != 0)
1449 {
1450 /* Need to update rows that are new, stop at the
1451 * first one that scrolled down. */
1452 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
1455 /* Move the entries that were scrolled, disable
1456 * the entries for the lines to be redrawn. */
1457 if ((wp->w_lines_valid += j) > wp->w_height)
1458 wp->w_lines_valid = wp->w_height;
1459 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1460 wp->w_lines[idx] = wp->w_lines[idx - j];
1461 while (idx >= 0)
1462 wp->w_lines[idx--].wl_valid = FALSE;
1463 }
1464 }
1465 else
1466 mid_start = 0; /* redraw all lines */
1467 }
1468 else
1469 mid_start = 0; /* redraw all lines */
1470 }
1471 else
1472 mid_start = 0; /* redraw all lines */
1473 }
1474 else
1475 {
1476 /*
1477 * New topline is at or below old topline: May scroll up.
1478 * When topline didn't change, find first entry in w_lines[] that
1479 * needs updating.
1480 */
1481
1482 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1483 j = -1;
1484 row = 0;
1485 for (i = 0; i < wp->w_lines_valid; i++)
1486 {
1487 if (wp->w_lines[i].wl_valid
1488 && wp->w_lines[i].wl_lnum == wp->w_topline)
1489 {
1490 j = i;
1491 break;
1492 }
1493 row += wp->w_lines[i].wl_size;
1494 }
1495 if (j == -1)
1496 {
1497 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1498 * lines */
1499 mid_start = 0;
1500 }
1501 else
1502 {
1503 /*
1504 * Try to delete the correct number of lines.
1505 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1506 */
1507#ifdef FEAT_DIFF
1508 /* If the topline didn't change, delete old filler lines,
1509 * otherwise delete filler lines of the new topline... */
1510 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1511 row += wp->w_old_topfill;
1512 else
1513 row += diff_check_fill(wp, wp->w_topline);
1514 /* ... but don't delete new filler lines. */
1515 row -= wp->w_topfill;
1516#endif
1517 if (row > 0)
1518 {
1519 check_for_delay(FALSE);
1520 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1521 bot_start = wp->w_height - row;
1522 else
1523 mid_start = 0; /* redraw all lines */
1524 }
1525 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1526 {
1527 /*
1528 * Skip the lines (below the deleted lines) that are still
1529 * valid and don't need redrawing. Copy their info
1530 * upwards, to compensate for the deleted lines. Set
1531 * bot_start to the first row that needs redrawing.
1532 */
1533 bot_start = 0;
1534 idx = 0;
1535 for (;;)
1536 {
1537 wp->w_lines[idx] = wp->w_lines[j];
1538 /* stop at line that didn't fit, unless it is still
1539 * valid (no lines deleted) */
1540 if (row > 0 && bot_start + row
1541 + (int)wp->w_lines[j].wl_size > wp->w_height)
1542 {
1543 wp->w_lines_valid = idx + 1;
1544 break;
1545 }
1546 bot_start += wp->w_lines[idx++].wl_size;
1547
1548 /* stop at the last valid entry in w_lines[].wl_size */
1549 if (++j >= wp->w_lines_valid)
1550 {
1551 wp->w_lines_valid = idx;
1552 break;
1553 }
1554 }
1555#ifdef FEAT_DIFF
1556 /* Correct the first entry for filler lines at the top
1557 * when it won't get updated below. */
1558 if (wp->w_p_diff && bot_start > 0)
1559 wp->w_lines[0].wl_size =
1560 plines_win_nofill(wp, wp->w_topline, TRUE)
1561 + wp->w_topfill;
1562#endif
1563 }
1564 }
1565 }
1566
1567 /* When starting redraw in the first line, redraw all lines. When
1568 * there is only one window it's probably faster to clear the screen
1569 * first. */
1570 if (mid_start == 0)
1571 {
1572 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001573 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001574 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001575 /* Clear the screen when it was not done by win_del_lines() or
1576 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1577 * then. */
1578 if (screen_cleared != TRUE)
1579 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001580#ifdef FEAT_WINDOWS
1581 /* The screen was cleared, redraw the tab pages line. */
1582 if (redraw_tabline)
1583 draw_tabline();
1584#endif
1585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001587
1588 /* When win_del_lines() or win_ins_lines() caused the screen to be
1589 * cleared (only happens for the first window) or when screenclear()
1590 * was called directly above, "must_redraw" will have been set to
1591 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1592 if (screen_cleared == TRUE)
1593 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 else
1596 {
1597 /* Not VALID or INVERTED: redraw all lines. */
1598 mid_start = 0;
1599 mid_end = wp->w_height;
1600 }
1601
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001602 if (type == SOME_VALID)
1603 {
1604 /* SOME_VALID: redraw all lines. */
1605 mid_start = 0;
1606 mid_end = wp->w_height;
1607 type = NOT_VALID;
1608 }
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* check if we are updating or removing the inverted part */
1611 if ((VIsual_active && buf == curwin->w_buffer)
1612 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1613 {
1614 linenr_T from, to;
1615
1616 if (VIsual_active)
1617 {
1618 if (VIsual_active
1619 && (VIsual_mode != wp->w_old_visual_mode
1620 || type == INVERTED_ALL))
1621 {
1622 /*
1623 * If the type of Visual selection changed, redraw the whole
1624 * selection. Also when the ownership of the X selection is
1625 * gained or lost.
1626 */
1627 if (curwin->w_cursor.lnum < VIsual.lnum)
1628 {
1629 from = curwin->w_cursor.lnum;
1630 to = VIsual.lnum;
1631 }
1632 else
1633 {
1634 from = VIsual.lnum;
1635 to = curwin->w_cursor.lnum;
1636 }
1637 /* redraw more when the cursor moved as well */
1638 if (wp->w_old_cursor_lnum < from)
1639 from = wp->w_old_cursor_lnum;
1640 if (wp->w_old_cursor_lnum > to)
1641 to = wp->w_old_cursor_lnum;
1642 if (wp->w_old_visual_lnum < from)
1643 from = wp->w_old_visual_lnum;
1644 if (wp->w_old_visual_lnum > to)
1645 to = wp->w_old_visual_lnum;
1646 }
1647 else
1648 {
1649 /*
1650 * Find the line numbers that need to be updated: The lines
1651 * between the old cursor position and the current cursor
1652 * position. Also check if the Visual position changed.
1653 */
1654 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1655 {
1656 from = curwin->w_cursor.lnum;
1657 to = wp->w_old_cursor_lnum;
1658 }
1659 else
1660 {
1661 from = wp->w_old_cursor_lnum;
1662 to = curwin->w_cursor.lnum;
1663 if (from == 0) /* Visual mode just started */
1664 from = to;
1665 }
1666
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001667 if (VIsual.lnum != wp->w_old_visual_lnum
1668 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670 if (wp->w_old_visual_lnum < from
1671 && wp->w_old_visual_lnum != 0)
1672 from = wp->w_old_visual_lnum;
1673 if (wp->w_old_visual_lnum > to)
1674 to = wp->w_old_visual_lnum;
1675 if (VIsual.lnum < from)
1676 from = VIsual.lnum;
1677 if (VIsual.lnum > to)
1678 to = VIsual.lnum;
1679 }
1680 }
1681
1682 /*
1683 * If in block mode and changed column or curwin->w_curswant:
1684 * update all lines.
1685 * First compute the actual start and end column.
1686 */
1687 if (VIsual_mode == Ctrl_V)
1688 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001689 colnr_T fromc, toc;
1690#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1691 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar404406a2014-10-09 13:24:43 +02001693 if (curwin->w_p_lbr)
1694 ve_flags = VE_ALL;
1695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001697#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1698 ve_flags = save_ve_flags;
1699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ++toc;
1701 if (curwin->w_curswant == MAXCOL)
1702 toc = MAXCOL;
1703
1704 if (fromc != wp->w_old_cursor_fcol
1705 || toc != wp->w_old_cursor_lcol)
1706 {
1707 if (from > VIsual.lnum)
1708 from = VIsual.lnum;
1709 if (to < VIsual.lnum)
1710 to = VIsual.lnum;
1711 }
1712 wp->w_old_cursor_fcol = fromc;
1713 wp->w_old_cursor_lcol = toc;
1714 }
1715 }
1716 else
1717 {
1718 /* Use the line numbers of the old Visual area. */
1719 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1720 {
1721 from = wp->w_old_cursor_lnum;
1722 to = wp->w_old_visual_lnum;
1723 }
1724 else
1725 {
1726 from = wp->w_old_visual_lnum;
1727 to = wp->w_old_cursor_lnum;
1728 }
1729 }
1730
1731 /*
1732 * There is no need to update lines above the top of the window.
1733 */
1734 if (from < wp->w_topline)
1735 from = wp->w_topline;
1736
1737 /*
1738 * If we know the value of w_botline, use it to restrict the update to
1739 * the lines that are visible in the window.
1740 */
1741 if (wp->w_valid & VALID_BOTLINE)
1742 {
1743 if (from >= wp->w_botline)
1744 from = wp->w_botline - 1;
1745 if (to >= wp->w_botline)
1746 to = wp->w_botline - 1;
1747 }
1748
1749 /*
1750 * Find the minimal part to be updated.
1751 * Watch out for scrolling that made entries in w_lines[] invalid.
1752 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1753 * top_end; need to redraw from top_end to the "to" line.
1754 * A middle mouse click with a Visual selection may change the text
1755 * above the Visual area and reset wl_valid, do count these for
1756 * mid_end (in srow).
1757 */
1758 if (mid_start > 0)
1759 {
1760 lnum = wp->w_topline;
1761 idx = 0;
1762 srow = 0;
1763 if (scrolled_down)
1764 mid_start = top_end;
1765 else
1766 mid_start = 0;
1767 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1768 {
1769 if (wp->w_lines[idx].wl_valid)
1770 mid_start += wp->w_lines[idx].wl_size;
1771 else if (!scrolled_down)
1772 srow += wp->w_lines[idx].wl_size;
1773 ++idx;
1774# ifdef FEAT_FOLDING
1775 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1776 lnum = wp->w_lines[idx].wl_lnum;
1777 else
1778# endif
1779 ++lnum;
1780 }
1781 srow += mid_start;
1782 mid_end = wp->w_height;
1783 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1784 {
1785 if (wp->w_lines[idx].wl_valid
1786 && wp->w_lines[idx].wl_lnum >= to + 1)
1787 {
1788 /* Only update until first row of this line */
1789 mid_end = srow;
1790 break;
1791 }
1792 srow += wp->w_lines[idx].wl_size;
1793 }
1794 }
1795 }
1796
1797 if (VIsual_active && buf == curwin->w_buffer)
1798 {
1799 wp->w_old_visual_mode = VIsual_mode;
1800 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1801 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001802 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 wp->w_old_curswant = curwin->w_curswant;
1804 }
1805 else
1806 {
1807 wp->w_old_visual_mode = 0;
1808 wp->w_old_cursor_lnum = 0;
1809 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001810 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1814 /* reset got_int, otherwise regexp won't work */
1815 save_got_int = got_int;
1816 got_int = 0;
1817#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001818#ifdef SYN_TIME_LIMIT
1819 /* Set the time limit to 'redrawtime'. */
1820 profile_setlimit(p_rdt, &syntax_tm);
1821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#ifdef FEAT_FOLDING
1823 win_foldinfo.fi_level = 0;
1824#endif
1825
1826 /*
1827 * Update all the window rows.
1828 */
1829 idx = 0; /* first entry in w_lines[].wl_size */
1830 row = 0;
1831 srow = 0;
1832 lnum = wp->w_topline; /* first line shown in window */
1833 for (;;)
1834 {
1835 /* stop updating when reached the end of the window (check for _past_
1836 * the end of the window is at the end of the loop) */
1837 if (row == wp->w_height)
1838 {
1839 didline = TRUE;
1840 break;
1841 }
1842
1843 /* stop updating when hit the end of the file */
1844 if (lnum > buf->b_ml.ml_line_count)
1845 {
1846 eof = TRUE;
1847 break;
1848 }
1849
1850 /* Remember the starting row of the line that is going to be dealt
1851 * with. It is used further down when the line doesn't fit. */
1852 srow = row;
1853
1854 /*
1855 * Update a line when it is in an area that needs updating, when it
1856 * has changes or w_lines[idx] is invalid.
1857 * bot_start may be halfway a wrapped line after using
1858 * win_del_lines(), check if the current line includes it.
1859 * When syntax folding is being used, the saved syntax states will
1860 * already have been updated, we can't see where the syntax state is
1861 * the same again, just update until the end of the window.
1862 */
1863 if (row < top_end
1864 || (row >= mid_start && row < mid_end)
1865#ifdef FEAT_SEARCH_EXTRA
1866 || top_to_mod
1867#endif
1868 || idx >= wp->w_lines_valid
1869 || (row + wp->w_lines[idx].wl_size > bot_start)
1870 || (mod_top != 0
1871 && (lnum == mod_top
1872 || (lnum >= mod_top
1873 && (lnum < mod_bot
1874#ifdef FEAT_SYN_HL
1875 || did_update == DID_FOLD
1876 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001877 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 && (
1879# ifdef FEAT_FOLDING
1880 (foldmethodIsSyntax(wp)
1881 && hasAnyFolding(wp)) ||
1882# endif
1883 syntax_check_changed(lnum)))
1884#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001885#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001886 /* match in fixed position might need redraw
1887 * if lines were inserted or deleted */
1888 || (wp->w_match_head != NULL
1889 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 )))))
1892 {
1893#ifdef FEAT_SEARCH_EXTRA
1894 if (lnum == mod_top)
1895 top_to_mod = FALSE;
1896#endif
1897
1898 /*
1899 * When at start of changed lines: May scroll following lines
1900 * up or down to minimize redrawing.
1901 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001902 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 */
1904 if (lnum == mod_top
1905 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001906 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 int old_rows = 0;
1909 int new_rows = 0;
1910 int xtra_rows;
1911 linenr_T l;
1912
1913 /* Count the old number of window rows, using w_lines[], which
1914 * should still contain the sizes for the lines as they are
1915 * currently displayed. */
1916 for (i = idx; i < wp->w_lines_valid; ++i)
1917 {
1918 /* Only valid lines have a meaningful wl_lnum. Invalid
1919 * lines are part of the changed area. */
1920 if (wp->w_lines[i].wl_valid
1921 && wp->w_lines[i].wl_lnum == mod_bot)
1922 break;
1923 old_rows += wp->w_lines[i].wl_size;
1924#ifdef FEAT_FOLDING
1925 if (wp->w_lines[i].wl_valid
1926 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1927 {
1928 /* Must have found the last valid entry above mod_bot.
1929 * Add following invalid entries. */
1930 ++i;
1931 while (i < wp->w_lines_valid
1932 && !wp->w_lines[i].wl_valid)
1933 old_rows += wp->w_lines[i++].wl_size;
1934 break;
1935 }
1936#endif
1937 }
1938
1939 if (i >= wp->w_lines_valid)
1940 {
1941 /* We can't find a valid line below the changed lines,
1942 * need to redraw until the end of the window.
1943 * Inserting/deleting lines has no use. */
1944 bot_start = 0;
1945 }
1946 else
1947 {
1948 /* Able to count old number of rows: Count new window
1949 * rows, and may insert/delete lines */
1950 j = idx;
1951 for (l = lnum; l < mod_bot; ++l)
1952 {
1953#ifdef FEAT_FOLDING
1954 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1955 ++new_rows;
1956 else
1957#endif
1958#ifdef FEAT_DIFF
1959 if (l == wp->w_topline)
1960 new_rows += plines_win_nofill(wp, l, TRUE)
1961 + wp->w_topfill;
1962 else
1963#endif
1964 new_rows += plines_win(wp, l, TRUE);
1965 ++j;
1966 if (new_rows > wp->w_height - row - 2)
1967 {
1968 /* it's getting too much, must redraw the rest */
1969 new_rows = 9999;
1970 break;
1971 }
1972 }
1973 xtra_rows = new_rows - old_rows;
1974 if (xtra_rows < 0)
1975 {
1976 /* May scroll text up. If there is not enough
1977 * remaining text or scrolling fails, must redraw the
1978 * rest. If scrolling works, must redraw the text
1979 * below the scrolled text. */
1980 if (row - xtra_rows >= wp->w_height - 2)
1981 mod_bot = MAXLNUM;
1982 else
1983 {
1984 check_for_delay(FALSE);
1985 if (win_del_lines(wp, row,
1986 -xtra_rows, FALSE, FALSE) == FAIL)
1987 mod_bot = MAXLNUM;
1988 else
1989 bot_start = wp->w_height + xtra_rows;
1990 }
1991 }
1992 else if (xtra_rows > 0)
1993 {
1994 /* May scroll text down. If there is not enough
1995 * remaining text of scrolling fails, must redraw the
1996 * rest. */
1997 if (row + xtra_rows >= wp->w_height - 2)
1998 mod_bot = MAXLNUM;
1999 else
2000 {
2001 check_for_delay(FALSE);
2002 if (win_ins_lines(wp, row + old_rows,
2003 xtra_rows, FALSE, FALSE) == FAIL)
2004 mod_bot = MAXLNUM;
2005 else if (top_end > row + old_rows)
2006 /* Scrolled the part at the top that requires
2007 * updating down. */
2008 top_end += xtra_rows;
2009 }
2010 }
2011
2012 /* When not updating the rest, may need to move w_lines[]
2013 * entries. */
2014 if (mod_bot != MAXLNUM && i != j)
2015 {
2016 if (j < i)
2017 {
2018 int x = row + new_rows;
2019
2020 /* move entries in w_lines[] upwards */
2021 for (;;)
2022 {
2023 /* stop at last valid entry in w_lines[] */
2024 if (i >= wp->w_lines_valid)
2025 {
2026 wp->w_lines_valid = j;
2027 break;
2028 }
2029 wp->w_lines[j] = wp->w_lines[i];
2030 /* stop at a line that won't fit */
2031 if (x + (int)wp->w_lines[j].wl_size
2032 > wp->w_height)
2033 {
2034 wp->w_lines_valid = j + 1;
2035 break;
2036 }
2037 x += wp->w_lines[j++].wl_size;
2038 ++i;
2039 }
2040 if (bot_start > x)
2041 bot_start = x;
2042 }
2043 else /* j > i */
2044 {
2045 /* move entries in w_lines[] downwards */
2046 j -= i;
2047 wp->w_lines_valid += j;
2048 if (wp->w_lines_valid > wp->w_height)
2049 wp->w_lines_valid = wp->w_height;
2050 for (i = wp->w_lines_valid; i - j >= idx; --i)
2051 wp->w_lines[i] = wp->w_lines[i - j];
2052
2053 /* The w_lines[] entries for inserted lines are
2054 * now invalid, but wl_size may be used above.
2055 * Reset to zero. */
2056 while (i >= idx)
2057 {
2058 wp->w_lines[i].wl_size = 0;
2059 wp->w_lines[i--].wl_valid = FALSE;
2060 }
2061 }
2062 }
2063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065
2066#ifdef FEAT_FOLDING
2067 /*
2068 * When lines are folded, display one line for all of them.
2069 * Otherwise, display normally (can be several display lines when
2070 * 'wrap' is on).
2071 */
2072 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2073 if (fold_count != 0)
2074 {
2075 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2076 ++row;
2077 --fold_count;
2078 wp->w_lines[idx].wl_folded = TRUE;
2079 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2080# ifdef FEAT_SYN_HL
2081 did_update = DID_FOLD;
2082# endif
2083 }
2084 else
2085#endif
2086 if (idx < wp->w_lines_valid
2087 && wp->w_lines[idx].wl_valid
2088 && wp->w_lines[idx].wl_lnum == lnum
2089 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002090 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 && srow + wp->w_lines[idx].wl_size > wp->w_height
2092#ifdef FEAT_DIFF
2093 && diff_check_fill(wp, lnum) == 0
2094#endif
2095 )
2096 {
2097 /* This line is not going to fit. Don't draw anything here,
2098 * will draw "@ " lines below. */
2099 row = wp->w_height + 1;
2100 }
2101 else
2102 {
2103#ifdef FEAT_SEARCH_EXTRA
2104 prepare_search_hl(wp, lnum);
2105#endif
2106#ifdef FEAT_SYN_HL
2107 /* Let the syntax stuff know we skipped a few lines. */
2108 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002109 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 syntax_end_parsing(syntax_last_parsed + 1);
2111#endif
2112
2113 /*
2114 * Display one line.
2115 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002116 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2117#ifdef SYN_TIME_LIMIT
2118 &syntax_tm
2119#else
2120 NULL
2121#endif
2122 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124#ifdef FEAT_FOLDING
2125 wp->w_lines[idx].wl_folded = FALSE;
2126 wp->w_lines[idx].wl_lastlnum = lnum;
2127#endif
2128#ifdef FEAT_SYN_HL
2129 did_update = DID_LINE;
2130 syntax_last_parsed = lnum;
2131#endif
2132 }
2133
2134 wp->w_lines[idx].wl_lnum = lnum;
2135 wp->w_lines[idx].wl_valid = TRUE;
2136 if (row > wp->w_height) /* past end of screen */
2137 {
2138 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002139 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2141 ++idx;
2142 break;
2143 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002144 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 wp->w_lines[idx].wl_size = row - srow;
2146 ++idx;
2147#ifdef FEAT_FOLDING
2148 lnum += fold_count + 1;
2149#else
2150 ++lnum;
2151#endif
2152 }
2153 else
2154 {
2155 /* This line does not need updating, advance to the next one */
2156 row += wp->w_lines[idx++].wl_size;
2157 if (row > wp->w_height) /* past end of screen */
2158 break;
2159#ifdef FEAT_FOLDING
2160 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2161#else
2162 ++lnum;
2163#endif
2164#ifdef FEAT_SYN_HL
2165 did_update = DID_NONE;
2166#endif
2167 }
2168
2169 if (lnum > buf->b_ml.ml_line_count)
2170 {
2171 eof = TRUE;
2172 break;
2173 }
2174 }
2175 /*
2176 * End of loop over all window lines.
2177 */
2178
2179
2180 if (idx > wp->w_lines_valid)
2181 wp->w_lines_valid = idx;
2182
2183#ifdef FEAT_SYN_HL
2184 /*
2185 * Let the syntax stuff know we stop parsing here.
2186 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002187 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 syntax_end_parsing(syntax_last_parsed + 1);
2189#endif
2190
2191 /*
2192 * If we didn't hit the end of the file, and we didn't finish the last
2193 * line we were working on, then the line didn't fit.
2194 */
2195 wp->w_empty_rows = 0;
2196#ifdef FEAT_DIFF
2197 wp->w_filler_rows = 0;
2198#endif
2199 if (!eof && !didline)
2200 {
2201 if (lnum == wp->w_topline)
2202 {
2203 /*
2204 * Single line that does not fit!
2205 * Don't overwrite it, it can be edited.
2206 */
2207 wp->w_botline = lnum + 1;
2208 }
2209#ifdef FEAT_DIFF
2210 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2211 {
2212 /* Window ends in filler lines. */
2213 wp->w_botline = lnum;
2214 wp->w_filler_rows = wp->w_height - srow;
2215 }
2216#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002217 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2218 {
2219 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2220
2221 /*
2222 * Last line isn't finished: Display "@@@" in the last screen line.
2223 */
2224 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002225 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002226 screen_fill(scr_row, scr_row + 1,
2227 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002228 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002229 set_empty_rows(wp, srow);
2230 wp->w_botline = lnum;
2231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2233 {
2234 /*
2235 * Last line isn't finished: Display "@@@" at the end.
2236 */
2237 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2238 W_WINROW(wp) + wp->w_height,
2239 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002240 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 set_empty_rows(wp, srow);
2242 wp->w_botline = lnum;
2243 }
2244 else
2245 {
2246 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2247 wp->w_botline = lnum;
2248 }
2249 }
2250 else
2251 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002252#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 draw_vsep_win(wp, row);
2254#endif
2255 if (eof) /* we hit the end of the file */
2256 {
2257 wp->w_botline = buf->b_ml.ml_line_count + 1;
2258#ifdef FEAT_DIFF
2259 j = diff_check_fill(wp, wp->w_botline);
2260 if (j > 0 && !wp->w_botfill)
2261 {
2262 /*
2263 * Display filler lines at the end of the file
2264 */
2265 if (char2cells(fill_diff) > 1)
2266 i = '-';
2267 else
2268 i = fill_diff;
2269 if (row + j > wp->w_height)
2270 j = wp->w_height - row;
2271 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2272 row += j;
2273 }
2274#endif
2275 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002276 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 wp->w_botline = lnum;
2278
2279 /* make sure the rest of the screen is blank */
2280 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002281 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283
2284 /* Reset the type of redrawing required, the window has been updated. */
2285 wp->w_redr_type = 0;
2286#ifdef FEAT_DIFF
2287 wp->w_old_topfill = wp->w_topfill;
2288 wp->w_old_botfill = wp->w_botfill;
2289#endif
2290
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002291 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 /*
2294 * There is a trick with w_botline. If we invalidate it on each
2295 * change that might modify it, this will cause a lot of expensive
2296 * calls to plines() in update_topline() each time. Therefore the
2297 * value of w_botline is often approximated, and this value is used to
2298 * compute the value of w_topline. If the value of w_botline was
2299 * wrong, check that the value of w_topline is correct (cursor is on
2300 * the visible part of the text). If it's not, we need to redraw
2301 * again. Mostly this just means scrolling up a few lines, so it
2302 * doesn't look too bad. Only do this for the current window (where
2303 * changes are relevant).
2304 */
2305 wp->w_valid |= VALID_BOTLINE;
2306 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2307 {
2308 recursive = TRUE;
2309 curwin->w_valid &= ~VALID_TOPLINE;
2310 update_topline(); /* may invalidate w_botline again */
2311 if (must_redraw != 0)
2312 {
2313 /* Don't update for changes in buffer again. */
2314 i = curbuf->b_mod_set;
2315 curbuf->b_mod_set = FALSE;
2316 win_update(curwin);
2317 must_redraw = 0;
2318 curbuf->b_mod_set = i;
2319 }
2320 recursive = FALSE;
2321 }
2322 }
2323
2324#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2325 /* restore got_int, unless CTRL-C was hit while redrawing */
2326 if (!got_int)
2327 got_int = save_got_int;
2328#endif
2329}
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331/*
2332 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2333 * as the filler character.
2334 */
2335 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002336win_draw_end(
2337 win_T *wp,
2338 int c1,
2339 int c2,
2340 int row,
2341 int endrow,
2342 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2345 int n = 0;
2346# define FDC_OFF n
2347#else
2348# define FDC_OFF 0
2349#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002350#ifdef FEAT_FOLDING
2351 int fdc = compute_foldcolumn(wp, 0);
2352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354#ifdef FEAT_RIGHTLEFT
2355 if (wp->w_p_rl)
2356 {
2357 /* No check for cmdline window: should never be right-left. */
2358# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002359 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 if (n > 0)
2362 {
2363 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002364 if (n > W_WIDTH(wp))
2365 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2367 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002368 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370# endif
2371# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002372 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 int nn = n + 2;
2375
2376 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002377 if (nn > W_WIDTH(wp))
2378 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002381 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 n = nn;
2383 }
2384# endif
2385 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2386 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002387 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2389 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392 else
2393#endif
2394 {
2395#ifdef FEAT_CMDWIN
2396 if (cmdwin_type != 0 && wp == curwin)
2397 {
2398 /* draw the cmdline character in the leftmost column */
2399 n = 1;
2400 if (n > wp->w_width)
2401 n = wp->w_width;
2402 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2403 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002404 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406#endif
2407#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002410 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 /* draw the fold column at the left */
2413 if (nn > W_WIDTH(wp))
2414 nn = W_WIDTH(wp);
2415 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2416 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002417 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 n = nn;
2419 }
2420#endif
2421#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002422 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 int nn = n + 2;
2425
2426 /* draw the sign column after the fold column */
2427 if (nn > W_WIDTH(wp))
2428 nn = W_WIDTH(wp);
2429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2430 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 n = nn;
2433 }
2434#endif
2435 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2436 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002437 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 set_empty_rows(wp, row);
2440}
2441
Bram Moolenaar1a384422010-07-14 19:53:30 +02002442#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002443static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002444
2445/*
2446 * Advance **color_cols and return TRUE when there are columns to draw.
2447 */
2448 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002449advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002450{
2451 while (**color_cols >= 0 && vcol > **color_cols)
2452 ++*color_cols;
2453 return (**color_cols >= 0);
2454}
2455#endif
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#ifdef FEAT_FOLDING
2458/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002459 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2460 * space is available for window "wp", minus "col".
2461 */
2462 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002463compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002464{
2465 int fdc = wp->w_p_fdc;
2466 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2467 int wwidth = W_WIDTH(wp);
2468
2469 if (fdc > wwidth - (col + wmw))
2470 fdc = wwidth - (col + wmw);
2471 return fdc;
2472}
2473
2474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 * Display one folded line.
2476 */
2477 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002478fold_line(
2479 win_T *wp,
2480 long fold_count,
2481 foldinfo_T *foldinfo,
2482 linenr_T lnum,
2483 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002485 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 pos_T *top, *bot;
2487 linenr_T lnume = lnum + fold_count - 1;
2488 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002489 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int col;
2492 int txtcol;
2493 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002494 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 /* Build the fold line:
2497 * 1. Add the cmdwin_type for the command-line window
2498 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002499 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * 4. Compose the text
2501 * 5. Add the text
2502 * 6. set highlighting for the Visual area an other text
2503 */
2504 col = 0;
2505
2506 /*
2507 * 1. Add the cmdwin_type for the command-line window
2508 * Ignores 'rightleft', this window is never right-left.
2509 */
2510#ifdef FEAT_CMDWIN
2511 if (cmdwin_type != 0 && wp == curwin)
2512 {
2513 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002514 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#ifdef FEAT_MBYTE
2516 if (enc_utf8)
2517 ScreenLinesUC[off] = 0;
2518#endif
2519 ++col;
2520 }
2521#endif
2522
2523 /*
2524 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002525 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002527 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (fdc > 0)
2529 {
2530 fill_foldcolumn(buf, wp, TRUE, lnum);
2531#ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 {
2534 int i;
2535
2536 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002537 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 /* reverse the fold column */
2539 for (i = 0; i < fdc; ++i)
2540 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2541 }
2542 else
2543#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002544 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 col += fdc;
2546 }
2547
2548#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002549# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2550 for (ri = 0; ri < l; ++ri) \
2551 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2552 else \
2553 for (ri = 0; ri < l; ++ri) \
2554 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002556# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2557 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558#endif
2559
Bram Moolenaar64486672010-05-16 15:46:46 +02002560 /* Set all attributes of the 'number' or 'relativenumber' column and the
2561 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002562 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564#ifdef FEAT_SIGNS
2565 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002566 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
2568 len = W_WIDTH(wp) - col;
2569 if (len > 0)
2570 {
2571 if (len > 2)
2572 len = 2;
2573# ifdef FEAT_RIGHTLEFT
2574 if (wp->w_p_rl)
2575 /* the line number isn't reversed */
2576 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002577 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 else
2579# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002580 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 col += len;
2582 }
2583 }
2584#endif
2585
2586 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002587 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002589 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
2591 len = W_WIDTH(wp) - col;
2592 if (len > 0)
2593 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002594 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002595 long num;
2596 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002597
2598 if (len > w + 1)
2599 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002600
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002601 if (wp->w_p_nu && !wp->w_p_rnu)
2602 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002603 num = (long)lnum;
2604 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002605 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002606 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002607 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002608 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002609 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002610 /* 'number' + 'relativenumber': cursor line shows absolute
2611 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002612 num = lnum;
2613 fmt = "%-*ld ";
2614 }
2615 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002616
Bram Moolenaar700e7342013-01-30 12:31:36 +01002617 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#ifdef FEAT_RIGHTLEFT
2619 if (wp->w_p_rl)
2620 /* the line number isn't reversed */
2621 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002622 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 else
2624#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002625 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 col += len;
2627 }
2628 }
2629
2630 /*
2631 * 4. Compose the folded-line string with 'foldtext', if set.
2632 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002633 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 txtcol = col; /* remember where text starts */
2636
2637 /*
2638 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2639 * Right-left text is put in columns 0 - number-col, normal text is put
2640 * in columns number-col - window-width.
2641 */
2642#ifdef FEAT_MBYTE
2643 if (has_mbyte)
2644 {
2645 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 int u8c, u8cc[MAX_MCO];
2647 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 int idx;
2649 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002650 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651# ifdef FEAT_ARABIC
2652 int prev_c = 0; /* previous Arabic character */
2653 int prev_c1 = 0; /* first composing char for prev_c */
2654# endif
2655
2656# ifdef FEAT_RIGHTLEFT
2657 if (wp->w_p_rl)
2658 idx = off;
2659 else
2660# endif
2661 idx = off + col;
2662
2663 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2664 for (p = text; *p != NUL; )
2665 {
2666 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002667 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (col + cells > W_WIDTH(wp)
2669# ifdef FEAT_RIGHTLEFT
2670 - (wp->w_p_rl ? col : 0)
2671# endif
2672 )
2673 break;
2674 ScreenLines[idx] = *p;
2675 if (enc_utf8)
2676 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002677 u8c = utfc_ptr2char(p, u8cc);
2678 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 ScreenLinesUC[idx] = 0;
2681#ifdef FEAT_ARABIC
2682 prev_c = u8c;
2683#endif
2684 }
2685 else
2686 {
2687#ifdef FEAT_ARABIC
2688 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2689 {
2690 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002691 int pc, pc1, nc;
2692 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int firstbyte = *p;
2694
2695 /* The idea of what is the previous and next
2696 * character depends on 'rightleft'. */
2697 if (wp->w_p_rl)
2698 {
2699 pc = prev_c;
2700 pc1 = prev_c1;
2701 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002702 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 else
2705 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002708 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 prev_c = u8c;
2711
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 pc, pc1, nc);
2714 ScreenLines[idx] = firstbyte;
2715 }
2716 else
2717 prev_c = u8c;
2718#endif
2719 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002720#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (u8c >= 0x10000)
2722 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2723 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 for (i = 0; i < Screen_mco; ++i)
2727 {
2728 ScreenLinesC[i][idx] = u8cc[i];
2729 if (u8cc[i] == 0)
2730 break;
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 if (cells > 1)
2734 ScreenLines[idx + 1] = 0;
2735 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002736 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2737 /* double-byte single width character */
2738 ScreenLines2[idx] = p[1];
2739 else if (cells > 1)
2740 /* double-width character */
2741 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 col += cells;
2743 idx += cells;
2744 p += c_len;
2745 }
2746 }
2747 else
2748#endif
2749 {
2750 len = (int)STRLEN(text);
2751 if (len > W_WIDTH(wp) - col)
2752 len = W_WIDTH(wp) - col;
2753 if (len > 0)
2754 {
2755#ifdef FEAT_RIGHTLEFT
2756 if (wp->w_p_rl)
2757 STRNCPY(current_ScreenLine, text, len);
2758 else
2759#endif
2760 STRNCPY(current_ScreenLine + col, text, len);
2761 col += len;
2762 }
2763 }
2764
2765 /* Fill the rest of the line with the fold filler */
2766#ifdef FEAT_RIGHTLEFT
2767 if (wp->w_p_rl)
2768 col -= txtcol;
2769#endif
2770 while (col < W_WIDTH(wp)
2771#ifdef FEAT_RIGHTLEFT
2772 - (wp->w_p_rl ? txtcol : 0)
2773#endif
2774 )
2775 {
2776#ifdef FEAT_MBYTE
2777 if (enc_utf8)
2778 {
2779 if (fill_fold >= 0x80)
2780 {
2781 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002782 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002783 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002788 ScreenLines[off + col] = fill_fold;
2789 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002790 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002792 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002794 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
2797 if (text != buf)
2798 vim_free(text);
2799
2800 /*
2801 * 6. set highlighting for the Visual area an other text.
2802 * If all folded lines are in the Visual area, highlight the line.
2803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2805 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002806 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 /* Visual is after curwin->w_cursor */
2809 top = &curwin->w_cursor;
2810 bot = &VIsual;
2811 }
2812 else
2813 {
2814 /* Visual is before curwin->w_cursor */
2815 top = &VIsual;
2816 bot = &curwin->w_cursor;
2817 }
2818 if (lnum >= top->lnum
2819 && lnume <= bot->lnum
2820 && (VIsual_mode != 'v'
2821 || ((lnum > top->lnum
2822 || (lnum == top->lnum
2823 && top->col == 0))
2824 && (lnume < bot->lnum
2825 || (lnume == bot->lnum
2826 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002827 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 if (VIsual_mode == Ctrl_V)
2830 {
2831 /* Visual block mode: highlight the chars part of the block */
2832 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2833 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002834 if (wp->w_old_cursor_lcol != MAXCOL
2835 && wp->w_old_cursor_lcol + txtcol
2836 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 len = wp->w_old_cursor_lcol;
2838 else
2839 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002840 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002841 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 }
2844 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002847 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002852#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002853 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2854 if (wp->w_p_cc_cols)
2855 {
2856 int i = 0;
2857 int j = wp->w_p_cc_cols[i];
2858 int old_txtcol = txtcol;
2859
2860 while (j > -1)
2861 {
2862 txtcol += j;
2863 if (wp->w_p_wrap)
2864 txtcol -= wp->w_skipcol;
2865 else
2866 txtcol -= wp->w_leftcol;
2867 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2868 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002869 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002870 txtcol = old_txtcol;
2871 j = wp->w_p_cc_cols[++i];
2872 }
2873 }
2874
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002875 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002876 if (wp->w_p_cuc)
2877 {
2878 txtcol += wp->w_virtcol;
2879 if (wp->w_p_wrap)
2880 txtcol -= wp->w_skipcol;
2881 else
2882 txtcol -= wp->w_leftcol;
2883 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2884 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002885 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002886 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2890 (int)W_WIDTH(wp), FALSE);
2891
2892 /*
2893 * Update w_cline_height and w_cline_folded if the cursor line was
2894 * updated (saves a call to plines() later).
2895 */
2896 if (wp == curwin
2897 && lnum <= curwin->w_cursor.lnum
2898 && lnume >= curwin->w_cursor.lnum)
2899 {
2900 curwin->w_cline_row = row;
2901 curwin->w_cline_height = 1;
2902 curwin->w_cline_folded = TRUE;
2903 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2904 }
2905}
2906
2907/*
2908 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2909 */
2910 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002911copy_text_attr(
2912 int off,
2913 char_u *buf,
2914 int len,
2915 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002917 int i;
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 mch_memmove(ScreenLines + off, buf, (size_t)len);
2920# ifdef FEAT_MBYTE
2921 if (enc_utf8)
2922 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2923# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002924 for (i = 0; i < len; ++i)
2925 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926}
2927
2928/*
2929 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002930 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
2932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002933fill_foldcolumn(
2934 char_u *p,
2935 win_T *wp,
2936 int closed, /* TRUE of FALSE */
2937 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 int i = 0;
2940 int level;
2941 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002942 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002943 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002946 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 level = win_foldinfo.fi_level;
2949 if (level > 0)
2950 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002951 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002952 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 /* If the column is too narrow, we start at the lowest level that
2955 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002956 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (first_level < 1)
2958 first_level = 1;
2959
Bram Moolenaar1c934292015-01-27 16:39:29 +01002960 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 if (win_foldinfo.fi_lnum == lnum
2963 && first_level + i >= win_foldinfo.fi_low_level)
2964 p[i] = '-';
2965 else if (first_level == 1)
2966 p[i] = '|';
2967 else if (first_level + i <= 9)
2968 p[i] = '0' + first_level + i;
2969 else
2970 p[i] = '>';
2971 if (first_level + i == level)
2972 break;
2973 }
2974 }
2975 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978#endif /* FEAT_FOLDING */
2979
2980/*
2981 * Display line "lnum" of window 'wp' on the screen.
2982 * Start at row "startrow", stop when "endrow" is reached.
2983 * wp->w_virtcol needs to be valid.
2984 *
2985 * Return the number of last row the line occupies.
2986 */
2987 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002988win_line(
2989 win_T *wp,
2990 linenr_T lnum,
2991 int startrow,
2992 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002993 int nochange UNUSED, /* not updating for changed text */
2994 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002996 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2998 int c = 0; /* init for GCC */
2999 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003000#ifdef FEAT_LINEBREAK
3001 long vcol_sbr = -1; /* virtual column after showbreak */
3002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 long vcol_prev = -1; /* "vcol" of previous character */
3004 char_u *line; /* current line */
3005 char_u *ptr; /* current position in "line" */
3006 int row; /* row in the window, excl w_winrow */
3007 int screen_row; /* row on the screen, incl w_winrow */
3008
3009 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3010 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003011 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003012 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 int c_extra = NUL; /* extra chars, all the same */
3014 int extra_attr = 0; /* attributes when n_extra != 0 */
3015 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3016 displaying lcs_eol at end-of-line */
3017 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3018 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3019
3020 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3021 int saved_n_extra = 0;
3022 char_u *saved_p_extra = NULL;
3023 int saved_c_extra = 0;
3024 int saved_char_attr = 0;
3025
3026 int n_attr = 0; /* chars with special attr */
3027 int saved_attr2 = 0; /* char_attr saved for n_attr */
3028 int n_attr3 = 0; /* chars with overruling special attr */
3029 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3030
3031 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3032
3033 int fromcol, tocol; /* start/end of inverting */
3034 int fromcol_prev = -2; /* start of inverting after cursor */
3035 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003037 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 pos_T pos;
3039 long v;
3040
3041 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003042 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3044 in this line */
3045 int attr = 0; /* attributes for area highlighting */
3046 int area_attr = 0; /* attributes desired by highlighting */
3047 int search_attr = 0; /* attributes desired by 'hlsearch' */
3048#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003049 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 int syntax_attr = 0; /* attributes desired by syntax */
3051 int has_syntax = FALSE; /* this buffer has syntax highl. */
3052 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003053 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003054 int draw_color_col = FALSE; /* highlight colorcolumn */
3055 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003056#endif
3057#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003058 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003059# define SPWORDLEN 150
3060 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003061 int nextlinecol = 0; /* column where nextline[] starts */
3062 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003063 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003064 int spell_attr = 0; /* attributes desired by spelling */
3065 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003066 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3067 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003068 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003069 static int cap_col = -1; /* column to check for Cap word */
3070 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003071 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072#endif
3073 int extra_check; /* has syntax or linebreak */
3074#ifdef FEAT_MBYTE
3075 int multi_attr = 0; /* attributes desired by multibyte */
3076 int mb_l = 1; /* multi-byte byte length */
3077 int mb_c = 0; /* decoded multi-byte character */
3078 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003079 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080#endif
3081#ifdef FEAT_DIFF
3082 int filler_lines; /* nr of filler lines to be drawn */
3083 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003084 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 int change_start = MAXCOL; /* first col of changed area */
3086 int change_end = -1; /* last col of changed area */
3087#endif
3088 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3089#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003090 int need_showbreak = FALSE; /* overlong line, skipping first x
3091 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003093#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3094 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003096 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
3098#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003099 matchitem_T *cur; /* points to the match list */
3100 match_T *shl; /* points to search_hl or a match */
3101 int shl_flag; /* flag to indicate whether search_hl
3102 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003103 int pos_inprogress; /* marks that position match search is
3104 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003105 int prevcol_hl_flag; /* flag to indicate whether prevcol
3106 equals startcol of search_hl or one
3107 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108#endif
3109#ifdef FEAT_ARABIC
3110 int prev_c = 0; /* previous Arabic character */
3111 int prev_c1 = 0; /* first composing char for prev_c */
3112#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003113#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003114 int did_line_attr = 0;
3115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 /* draw_state: items that are drawn in sequence: */
3118#define WL_START 0 /* nothing done yet */
3119#ifdef FEAT_CMDWIN
3120# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3121#else
3122# define WL_CMDLINE WL_START
3123#endif
3124#ifdef FEAT_FOLDING
3125# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3126#else
3127# define WL_FOLD WL_CMDLINE
3128#endif
3129#ifdef FEAT_SIGNS
3130# define WL_SIGN WL_FOLD + 1 /* column for signs */
3131#else
3132# define WL_SIGN WL_FOLD /* column for signs */
3133#endif
3134#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003135#ifdef FEAT_LINEBREAK
3136# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003138# define WL_BRI WL_NR
3139#endif
3140#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3141# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3142#else
3143# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#endif
3145#define WL_LINE WL_SBR + 1 /* text in the line */
3146 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003147#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int feedback_col = 0;
3149 int feedback_old_attr = -1;
3150#endif
3151
Bram Moolenaar860cae12010-06-05 23:22:07 +02003152#ifdef FEAT_CONCEAL
3153 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003154 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003155 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003156 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003157 int is_concealing = FALSE;
3158 int boguscols = 0; /* nonexistent columns added to force
3159 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003160 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003161 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003162 int match_conc = 0; /* cchar for match functions */
3163 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003164 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003165# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003166# define FIX_FOR_BOGUSCOLS \
3167 { \
3168 n_extra += vcol_off; \
3169 vcol -= vcol_off; \
3170 vcol_off = 0; \
3171 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003172 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003173 boguscols = 0; \
3174 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003175#else
3176# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 if (startrow > endrow) /* past the end already! */
3180 return startrow;
3181
3182 row = startrow;
3183 screen_row = row + W_WINROW(wp);
3184
3185 /*
3186 * To speed up the loop below, set extra_check when there is linebreak,
3187 * trailing white space and/or syntax processing to be done.
3188 */
3189#ifdef FEAT_LINEBREAK
3190 extra_check = wp->w_p_lbr;
3191#else
3192 extra_check = 0;
3193#endif
3194#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003195 if (syntax_present(wp) && !wp->w_s->b_syn_error
3196# ifdef SYN_TIME_LIMIT
3197 && !wp->w_s->b_syn_slow
3198# endif
3199 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /* Prepare for syntax highlighting in this line. When there is an
3202 * error, stop syntax highlighting. */
3203 save_did_emsg = did_emsg;
3204 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003205 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003207 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209 {
3210 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003211#ifdef SYN_TIME_LIMIT
3212 if (!wp->w_s->b_syn_slow)
3213#endif
3214 {
3215 has_syntax = TRUE;
3216 extra_check = TRUE;
3217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
3219 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003220
3221 /* Check for columns to display for 'colorcolumn'. */
3222 color_cols = wp->w_p_cc_cols;
3223 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003224 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003225#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003226
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003227#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003228 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003229 && *wp->w_s->b_p_spl != NUL
3230 && wp->w_s->b_langp.ga_len > 0
3231 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003232 {
3233 /* Prepare for spell checking. */
3234 has_spell = TRUE;
3235 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003236
3237 /* Get the start of the next line, so that words that wrap to the next
3238 * line are found too: "et<line-break>al.".
3239 * Trick: skip a few chars for C/shell/Vim comments */
3240 nextline[SPWORDLEN] = NUL;
3241 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3242 {
3243 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3244 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3245 }
3246
3247 /* When a word wrapped from the previous line the start of the current
3248 * line is valid. */
3249 if (lnum == checked_lnum)
3250 cur_checked_col = checked_col;
3251 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003252
3253 /* When there was a sentence end in the previous line may require a
3254 * word starting with capital in this line. In line 1 always check
3255 * the first word. */
3256 if (lnum != capcol_lnum)
3257 cap_col = -1;
3258 if (lnum == 1)
3259 cap_col = 0;
3260 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif
3263
3264 /*
3265 * handle visual active in this window
3266 */
3267 fromcol = -10;
3268 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3270 {
3271 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003272 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 top = &curwin->w_cursor;
3275 bot = &VIsual;
3276 }
3277 else /* Visual is before curwin->w_cursor */
3278 {
3279 top = &VIsual;
3280 bot = &curwin->w_cursor;
3281 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003282 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (VIsual_mode == Ctrl_V) /* block mode */
3284 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003285 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 fromcol = wp->w_old_cursor_fcol;
3288 tocol = wp->w_old_cursor_lcol;
3289 }
3290 }
3291 else /* non-block mode */
3292 {
3293 if (lnum > top->lnum && lnum <= bot->lnum)
3294 fromcol = 0;
3295 else if (lnum == top->lnum)
3296 {
3297 if (VIsual_mode == 'V') /* linewise */
3298 fromcol = 0;
3299 else
3300 {
3301 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3302 if (gchar_pos(top) == NUL)
3303 tocol = fromcol + 1;
3304 }
3305 }
3306 if (VIsual_mode != 'V' && lnum == bot->lnum)
3307 {
3308 if (*p_sel == 'e' && bot->col == 0
3309#ifdef FEAT_VIRTUALEDIT
3310 && bot->coladd == 0
3311#endif
3312 )
3313 {
3314 fromcol = -10;
3315 tocol = MAXCOL;
3316 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003317 else if (bot->col == MAXCOL)
3318 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
3320 {
3321 pos = *bot;
3322 if (*p_sel == 'e')
3323 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3324 else
3325 {
3326 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3327 ++tocol;
3328 }
3329 }
3330 }
3331 }
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 /* Check if the character under the cursor should not be inverted */
3334 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003335#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 )
3339 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
3341 /* if inverting in this line set area_highlighting */
3342 if (fromcol >= 0)
3343 {
3344 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003345 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003347 if ((clip_star.available && !clip_star.owned
3348 && clip_isautosel_star())
3349 || (clip_plus.available && !clip_plus.owned
3350 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003351 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
3353 }
3354 }
3355
3356 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003357 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003359 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 && wp == curwin
3361 && lnum >= curwin->w_cursor.lnum
3362 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3363 {
3364 if (lnum == curwin->w_cursor.lnum)
3365 getvcol(curwin, &(curwin->w_cursor),
3366 (colnr_T *)&fromcol, NULL, NULL);
3367 else
3368 fromcol = 0;
3369 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3370 {
3371 pos.lnum = lnum;
3372 pos.col = search_match_endcol;
3373 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3374 }
3375 else
3376 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003377 /* do at least one character; happens when past end of line */
3378 if (fromcol == tocol)
3379 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003381 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384#ifdef FEAT_DIFF
3385 filler_lines = diff_check(wp, lnum);
3386 if (filler_lines < 0)
3387 {
3388 if (filler_lines == -1)
3389 {
3390 if (diff_find_change(wp, lnum, &change_start, &change_end))
3391 diff_hlf = HLF_ADD; /* added line */
3392 else if (change_start == 0)
3393 diff_hlf = HLF_TXD; /* changed text */
3394 else
3395 diff_hlf = HLF_CHD; /* changed line */
3396 }
3397 else
3398 diff_hlf = HLF_ADD; /* added line */
3399 filler_lines = 0;
3400 area_highlighting = TRUE;
3401 }
3402 if (lnum == wp->w_topline)
3403 filler_lines = wp->w_topfill;
3404 filler_todo = filler_lines;
3405#endif
3406
3407#ifdef LINE_ATTR
3408# ifdef FEAT_SIGNS
3409 /* If this line has a sign with line highlighting set line_attr. */
3410 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3411 if (v != 0)
3412 line_attr = sign_get_attr((int)v, TRUE);
3413# endif
3414# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3415 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003416 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003417 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418# endif
3419 if (line_attr != 0)
3420 area_highlighting = TRUE;
3421#endif
3422
3423 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3424 ptr = line;
3425
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003426#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003427 if (has_spell)
3428 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003429 /* For checking first word with a capital skip white space. */
3430 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003431 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003432
Bram Moolenaar30abd282005-06-22 22:35:10 +00003433 /* To be able to spell-check over line boundaries copy the end of the
3434 * current line into nextline[]. Above the start of the next line was
3435 * copied to nextline[SPWORDLEN]. */
3436 if (nextline[SPWORDLEN] == NUL)
3437 {
3438 /* No next line or it is empty. */
3439 nextlinecol = MAXCOL;
3440 nextline_idx = 0;
3441 }
3442 else
3443 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003444 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003445 if (v < SPWORDLEN)
3446 {
3447 /* Short line, use it completely and append the start of the
3448 * next line. */
3449 nextlinecol = 0;
3450 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003451 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003452 nextline_idx = v + 1;
3453 }
3454 else
3455 {
3456 /* Long line, use only the last SPWORDLEN bytes. */
3457 nextlinecol = v - SPWORDLEN;
3458 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3459 nextline_idx = SPWORDLEN + 1;
3460 }
3461 }
3462 }
3463#endif
3464
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003465 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003467 if (lcs_space || lcs_trail)
3468 extra_check = TRUE;
3469 /* find start of trailing whitespace */
3470 if (lcs_trail)
3471 {
3472 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003473 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003474 --trailcol;
3475 trailcol += (colnr_T) (ptr - line);
3476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
3478
3479 /*
3480 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3481 * first character to be displayed.
3482 */
3483 if (wp->w_p_wrap)
3484 v = wp->w_skipcol;
3485 else
3486 v = wp->w_leftcol;
3487 if (v > 0)
3488 {
3489#ifdef FEAT_MBYTE
3490 char_u *prev_ptr = ptr;
3491#endif
3492 while (vcol < v && *ptr != NUL)
3493 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003494 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 vcol += c;
3496#ifdef FEAT_MBYTE
3497 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003499 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003502 /* When:
3503 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003504 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003505 * - 'virtualedit' is set, or
3506 * - the visual mode is active,
3507 * the end of the line may be before the start of the displayed part.
3508 */
3509 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003510#ifdef FEAT_SYN_HL
3511 wp->w_p_cuc || draw_color_col ||
3512#endif
3513#ifdef FEAT_VIRTUALEDIT
3514 virtual_active() ||
3515#endif
3516 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /* Handle a character that's not completely on the screen: Put ptr at
3522 * that character but skip the first few screen characters. */
3523 if (vcol > v)
3524 {
3525 vcol -= c;
3526#ifdef FEAT_MBYTE
3527 ptr = prev_ptr;
3528#else
3529 --ptr;
3530#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003531 /* If the character fits on the screen, don't need to skip it.
3532 * Except for a TAB. */
3533 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003534#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003535 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003536#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003537 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003538 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 /*
3542 * Adjust for when the inverted text is before the screen,
3543 * and when the start of the inverted text is before the screen.
3544 */
3545 if (tocol <= vcol)
3546 fromcol = 0;
3547 else if (fromcol >= 0 && fromcol < vcol)
3548 fromcol = vcol;
3549
3550#ifdef FEAT_LINEBREAK
3551 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3552 if (wp->w_p_wrap)
3553 need_showbreak = TRUE;
3554#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003555#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003556 /* When spell checking a word we need to figure out the start of the
3557 * word and if it's badly spelled or not. */
3558 if (has_spell)
3559 {
3560 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003561 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003562 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003563
3564 pos = wp->w_cursor;
3565 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003566 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003567 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003568
3569 /* spell_move_to() may call ml_get() and make "line" invalid */
3570 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3571 ptr = line + linecol;
3572
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003573 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003574 {
3575 /* no bad word found at line start, don't check until end of a
3576 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003577 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003578 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003579 }
3580 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003581 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003582 /* bad word found, use attributes until end of word */
3583 word_end = wp->w_cursor.col + len + 1;
3584
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003585 /* Turn index into actual attributes. */
3586 if (spell_hlf != HLF_COUNT)
3587 spell_attr = highlight_attr[spell_hlf];
3588 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003590
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003591# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003592 /* Need to restart syntax highlighting for this line. */
3593 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003594 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003595# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003596 }
3597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599
3600 /*
3601 * Correct highlighting for cursor that can't be disabled.
3602 * Avoids having to check this for each character.
3603 */
3604 if (fromcol >= 0)
3605 {
3606 if (noinvcur)
3607 {
3608 if ((colnr_T)fromcol == wp->w_virtcol)
3609 {
3610 /* highlighting starts at cursor, let it start just after the
3611 * cursor */
3612 fromcol_prev = fromcol;
3613 fromcol = -1;
3614 }
3615 else if ((colnr_T)fromcol < wp->w_virtcol)
3616 /* restart highlighting after the cursor */
3617 fromcol_prev = wp->w_virtcol;
3618 }
3619 if (fromcol >= tocol)
3620 fromcol = -1;
3621 }
3622
3623#ifdef FEAT_SEARCH_EXTRA
3624 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003625 * Handle highlighting the last used search pattern and matches.
3626 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003628 cur = wp->w_match_head;
3629 shl_flag = FALSE;
3630 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003632 if (shl_flag == FALSE)
3633 {
3634 shl = &search_hl;
3635 shl_flag = TRUE;
3636 }
3637 else
3638 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003639 shl->startcol = MAXCOL;
3640 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003642 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003643 v = (long)(ptr - line);
3644 if (cur != NULL)
3645 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003646 next_search_hl(wp, shl, lnum, (colnr_T)v,
3647 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003648
3649 /* Need to get the line again, a multi-line regexp may have made it
3650 * invalid. */
3651 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3652 ptr = line + v;
3653
3654 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003656 if (shl->lnum == lnum)
3657 shl->startcol = shl->rm.startpos[0].col;
3658 else
3659 shl->startcol = 0;
3660 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3661 - shl->rm.startpos[0].lnum)
3662 shl->endcol = shl->rm.endpos[0].col;
3663 else
3664 shl->endcol = MAXCOL;
3665 /* Highlight one character for an empty match. */
3666 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003669 if (has_mbyte && line[shl->endcol] != NUL)
3670 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003675 if ((long)shl->startcol < v) /* match at leftcol */
3676 {
3677 shl->attr_cur = shl->attr;
3678 search_attr = shl->attr;
3679 }
3680 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003682 if (shl != &search_hl && cur != NULL)
3683 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685#endif
3686
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003687#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003688 /* Cursor line highlighting for 'cursorline' in the current window. Not
3689 * when Visual mode is active, because it's not clear what is selected
3690 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003691 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003692 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003693 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003694 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003695 area_highlighting = TRUE;
3696 }
3697#endif
3698
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003699 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 col = 0;
3701#ifdef FEAT_RIGHTLEFT
3702 if (wp->w_p_rl)
3703 {
3704 /* Rightleft window: process the text in the normal direction, but put
3705 * it in current_ScreenLine[] from right to left. Start at the
3706 * rightmost column of the window. */
3707 col = W_WIDTH(wp) - 1;
3708 off += col;
3709 }
3710#endif
3711
3712 /*
3713 * Repeat for the whole displayed line.
3714 */
3715 for (;;)
3716 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003717#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003718 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 /* Skip this quickly when working on the text. */
3721 if (draw_state != WL_LINE)
3722 {
3723#ifdef FEAT_CMDWIN
3724 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3725 {
3726 draw_state = WL_CMDLINE;
3727 if (cmdwin_type != 0 && wp == curwin)
3728 {
3729 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003731 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003732 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734 }
3735#endif
3736
3737#ifdef FEAT_FOLDING
3738 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3739 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003740 int fdc = compute_foldcolumn(wp, 0);
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003743 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003745 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003746 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003747 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003748 p_extra_free = alloc(12 + 1);
3749
3750 if (p_extra_free != NULL)
3751 {
3752 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3753 n_extra = fdc;
3754 p_extra_free[n_extra] = NUL;
3755 p_extra = p_extra_free;
3756 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003757 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761#endif
3762
3763#ifdef FEAT_SIGNS
3764 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3765 {
3766 draw_state = WL_SIGN;
3767 /* Show the sign column when there are any signs in this
3768 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003769 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003771 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003773 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774# endif
3775
3776 /* Draw two cells with the sign value or blank. */
3777 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003778 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 n_extra = 2;
3780
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003781 if (row == startrow
3782#ifdef FEAT_DIFF
3783 + filler_lines && filler_todo <= 0
3784#endif
3785 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
3787 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3788 SIGN_TEXT);
3789# ifdef FEAT_SIGN_ICONS
3790 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3791 SIGN_ICON);
3792 if (gui.in_use && icon_sign != 0)
3793 {
3794 /* Use the image in this position. */
3795 c_extra = SIGN_BYTE;
3796# ifdef FEAT_NETBEANS_INTG
3797 if (buf_signcount(wp->w_buffer, lnum) > 1)
3798 c_extra = MULTISIGN_BYTE;
3799# endif
3800 char_attr = icon_sign;
3801 }
3802 else
3803# endif
3804 if (text_sign != 0)
3805 {
3806 p_extra = sign_get_text(text_sign);
3807 if (p_extra != NULL)
3808 {
3809 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 char_attr = sign_get_attr(text_sign, FALSE);
3813 }
3814 }
3815 }
3816 }
3817#endif
3818
3819 if (draw_state == WL_NR - 1 && n_extra == 0)
3820 {
3821 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003822 /* Display the absolute or relative line number. After the
3823 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3824 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 && (row == startrow
3826#ifdef FEAT_DIFF
3827 + filler_lines
3828#endif
3829 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3830 {
3831 /* Draw the line number (empty space after wrapping). */
3832 if (row == startrow
3833#ifdef FEAT_DIFF
3834 + filler_lines
3835#endif
3836 )
3837 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003838 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003839 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003840
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003841 if (wp->w_p_nu && !wp->w_p_rnu)
3842 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003843 num = (long)lnum;
3844 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003845 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003846 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003847 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003848 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003849 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003850 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003851 num = lnum;
3852 fmt = "%-*ld ";
3853 }
3854 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003855
Bram Moolenaar700e7342013-01-30 12:31:36 +01003856 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003857 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (wp->w_skipcol > 0)
3859 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3860 *p_extra = '-';
3861#ifdef FEAT_RIGHTLEFT
3862 if (wp->w_p_rl) /* reverse line numbers */
3863 rl_mirror(extra);
3864#endif
3865 p_extra = extra;
3866 c_extra = NUL;
3867 }
3868 else
3869 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003870 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003871 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003872#ifdef FEAT_SYN_HL
3873 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003874 * the current line differently.
3875 * TODO: Can we use CursorLine instead of CursorLineNr
3876 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003877 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003878 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003879 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 }
3883
Bram Moolenaar597a4222014-06-25 14:39:50 +02003884#ifdef FEAT_LINEBREAK
3885 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3886 && n_extra == 0 && *p_sbr != NUL)
3887 /* draw indent after showbreak value */
3888 draw_state = WL_BRI;
3889 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3890 /* After the showbreak, draw the breakindent */
3891 draw_state = WL_BRI - 1;
3892
3893 /* draw 'breakindent': indent wrapped text accordingly */
3894 if (draw_state == WL_BRI - 1 && n_extra == 0)
3895 {
3896 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003897 /* if need_showbreak is set, breakindent also applies */
3898 if (wp->w_p_bri && n_extra == 0
3899 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003900# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003901 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003902# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003903 )
3904 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003905 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003906# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003907 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003908 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003909 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003910# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003911 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3912 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003913 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003914# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003915 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003916# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003917 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003918 c_extra = ' ';
3919 n_extra = get_breakindent_win(wp,
3920 ml_get_buf(wp->w_buffer, lnum, FALSE));
3921 /* Correct end of highlighted area for 'breakindent',
3922 * required when 'linebreak' is also set. */
3923 if (tocol == vcol)
3924 tocol += n_extra;
3925 }
3926 }
3927#endif
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3930 if (draw_state == WL_SBR - 1 && n_extra == 0)
3931 {
3932 draw_state = WL_SBR;
3933# ifdef FEAT_DIFF
3934 if (filler_todo > 0)
3935 {
3936 /* Draw "deleted" diff line(s). */
3937 if (char2cells(fill_diff) > 1)
3938 c_extra = '-';
3939 else
3940 c_extra = fill_diff;
3941# ifdef FEAT_RIGHTLEFT
3942 if (wp->w_p_rl)
3943 n_extra = col + 1;
3944 else
3945# endif
3946 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003947 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949# endif
3950# ifdef FEAT_LINEBREAK
3951 if (*p_sbr != NUL && need_showbreak)
3952 {
3953 /* Draw 'showbreak' at the start of each broken line. */
3954 p_extra = p_sbr;
3955 c_extra = NUL;
3956 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003957 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003959 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 /* Correct end of highlighted area for 'showbreak',
3961 * required when 'linebreak' is also set. */
3962 if (tocol == vcol)
3963 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003964#ifdef FEAT_SYN_HL
3965 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003966 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003967 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003968 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971# endif
3972 }
3973#endif
3974
3975 if (draw_state == WL_LINE - 1 && n_extra == 0)
3976 {
3977 draw_state = WL_LINE;
3978 if (saved_n_extra)
3979 {
3980 /* Continue item from end of wrapped line. */
3981 n_extra = saved_n_extra;
3982 c_extra = saved_c_extra;
3983 p_extra = saved_p_extra;
3984 char_attr = saved_char_attr;
3985 }
3986 else
3987 char_attr = 0;
3988 }
3989 }
3990
3991 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003992 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003993 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994#ifdef FEAT_DIFF
3995 && filler_todo <= 0
3996#endif
3997 )
3998 {
3999 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
4000 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001 /* Pretend we have finished updating the window. Except when
4002 * 'cursorcolumn' is set. */
4003#ifdef FEAT_SYN_HL
4004 if (wp->w_p_cuc)
4005 row = wp->w_cline_row + wp->w_cline_height;
4006 else
4007#endif
4008 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 break;
4010 }
4011
4012 if (draw_state == WL_LINE && area_highlighting)
4013 {
4014 /* handle Visual or match highlighting in this line */
4015 if (vcol == fromcol
4016#ifdef FEAT_MBYTE
4017 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4018 && (*mb_ptr2cells)(ptr) > 1)
4019#endif
4020 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004021 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 && vcol < tocol))
4023 area_attr = attr; /* start highlighting */
4024 else if (area_attr != 0
4025 && (vcol == tocol
4026 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029#ifdef FEAT_SEARCH_EXTRA
4030 if (!n_extra)
4031 {
4032 /*
4033 * Check for start/end of search pattern match.
4034 * After end, check for start/end of next match.
4035 * When another match, have to check for start again.
4036 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004037 * Do this for 'search_hl' and the match list (ordered by
4038 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004040 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004041 cur = wp->w_match_head;
4042 shl_flag = FALSE;
4043 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004045 if (shl_flag == FALSE
4046 && ((cur != NULL
4047 && cur->priority > SEARCH_HL_PRIORITY)
4048 || cur == NULL))
4049 {
4050 shl = &search_hl;
4051 shl_flag = TRUE;
4052 }
4053 else
4054 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004055 if (cur != NULL)
4056 cur->pos.cur = 0;
4057 pos_inprogress = TRUE;
4058 while (shl->rm.regprog != NULL
4059 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004061 if (shl->startcol != MAXCOL
4062 && v >= (long)shl->startcol
4063 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004065#ifdef FEAT_MBYTE
4066 int tmp_col = v + MB_PTR2LEN(ptr);
4067
4068 if (shl->endcol < tmp_col)
4069 shl->endcol = tmp_col;
4070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004072#ifdef FEAT_CONCEAL
4073 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4074 == cur->hlg_id)
4075 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004076 has_match_conc =
4077 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004078 match_conc = cur->conceal_char;
4079 }
4080 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004081 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004084 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
4086 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004087 next_search_hl(wp, shl, lnum, (colnr_T)v,
4088 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004089 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004090 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092 /* Need to get the line again, a multi-line regexp
4093 * may have made it invalid. */
4094 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4095 ptr = line + v;
4096
4097 if (shl->lnum == lnum)
4098 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004099 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004101 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004103 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004105 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 /* highlight empty match, try again after
4108 * it */
4109#ifdef FEAT_MBYTE
4110 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004111 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004112 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 else
4114#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004115 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /* Loop to check if the match starts at the
4119 * current position */
4120 continue;
4121 }
4122 }
4123 break;
4124 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004125 if (shl != &search_hl && cur != NULL)
4126 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004128
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004129 /* Use attributes from match with highest priority among
4130 * 'search_hl' and the match list. */
4131 search_attr = search_hl.attr_cur;
4132 cur = wp->w_match_head;
4133 shl_flag = FALSE;
4134 while (cur != NULL || shl_flag == FALSE)
4135 {
4136 if (shl_flag == FALSE
4137 && ((cur != NULL
4138 && cur->priority > SEARCH_HL_PRIORITY)
4139 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004140 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004141 shl = &search_hl;
4142 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004143 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004144 else
4145 shl = &cur->hl;
4146 if (shl->attr_cur != 0)
4147 search_attr = shl->attr_cur;
4148 if (shl != &search_hl && cur != NULL)
4149 cur = cur->next;
4150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152#endif
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004155 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004157 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4158 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004160 if (diff_hlf == HLF_TXD && ptr - line > change_end
4161 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004163 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004164 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004165 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004168
4169 /* Decide which of the highlight attributes to use. */
4170 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004171#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004172 if (area_attr != 0)
4173 char_attr = hl_combine_attr(line_attr, area_attr);
4174 else if (search_attr != 0)
4175 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004176 /* Use line_attr when not in the Visual or 'incsearch' area
4177 * (area_attr may be 0 when "noinvcur" is set). */
4178 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004179 || vcol < fromcol || vcol_prev < fromcol_prev
4180 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004181 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004182#else
4183 if (area_attr != 0)
4184 char_attr = area_attr;
4185 else if (search_attr != 0)
4186 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004187#endif
4188 else
4189 {
4190 attr_pri = FALSE;
4191#ifdef FEAT_SYN_HL
4192 if (has_syntax)
4193 char_attr = syntax_attr;
4194 else
4195#endif
4196 char_attr = 0;
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 /*
4201 * Get the next character to put on the screen.
4202 */
4203 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004204 * The "p_extra" points to the extra stuff that is inserted to
4205 * represent special characters (non-printable stuff) and other
4206 * things. When all characters are the same, c_extra is used.
4207 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4208 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4210 */
4211 if (n_extra > 0)
4212 {
4213 if (c_extra != NUL)
4214 {
4215 c = c_extra;
4216#ifdef FEAT_MBYTE
4217 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004218 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004221 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004222 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 else
4225 mb_utf8 = FALSE;
4226#endif
4227 }
4228 else
4229 {
4230 c = *p_extra;
4231#ifdef FEAT_MBYTE
4232 if (has_mbyte)
4233 {
4234 mb_c = c;
4235 if (enc_utf8)
4236 {
4237 /* If the UTF-8 character is more than one byte:
4238 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004239 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 mb_utf8 = FALSE;
4241 if (mb_l > n_extra)
4242 mb_l = 1;
4243 else if (mb_l > 1)
4244 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004245 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004247 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 }
4250 else
4251 {
4252 /* if this is a DBCS character, put it in "mb_c" */
4253 mb_l = MB_BYTE2LEN(c);
4254 if (mb_l >= n_extra)
4255 mb_l = 1;
4256 else if (mb_l > 1)
4257 mb_c = (c << 8) + p_extra[1];
4258 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004259 if (mb_l == 0) /* at the NUL at end-of-line */
4260 mb_l = 1;
4261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 /* If a double-width char doesn't fit display a '>' in the
4263 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004264 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265# ifdef FEAT_RIGHTLEFT
4266 wp->w_p_rl ? (col <= 0) :
4267# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004268 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 && (*mb_char2cells)(mb_c) == 2)
4270 {
4271 c = '>';
4272 mb_c = c;
4273 mb_l = 1;
4274 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004275 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 /* put the pointer back to output the double-width
4277 * character at the start of the next line. */
4278 ++n_extra;
4279 --p_extra;
4280 }
4281 else
4282 {
4283 n_extra -= mb_l - 1;
4284 p_extra += mb_l - 1;
4285 }
4286 }
4287#endif
4288 ++p_extra;
4289 }
4290 --n_extra;
4291 }
4292 else
4293 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004294#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004295 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004296#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004297
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004298 if (p_extra_free != NULL)
4299 {
4300 vim_free(p_extra_free);
4301 p_extra_free = NULL;
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 /*
4304 * Get a character from the line itself.
4305 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004306 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004307#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004308 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_MBYTE
4311 if (has_mbyte)
4312 {
4313 mb_c = c;
4314 if (enc_utf8)
4315 {
4316 /* If the UTF-8 character is more than one byte: Decode it
4317 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004318 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 mb_utf8 = FALSE;
4320 if (mb_l > 1)
4321 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004322 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 /* Overlong encoded ASCII or ASCII with composing char
4324 * is displayed normally, except a NUL. */
4325 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004326 {
4327 c = mb_c;
4328# ifdef FEAT_LINEBREAK
4329 c0 = mb_c;
4330# endif
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004333
4334 /* At start of the line we can have a composing char.
4335 * Draw it as a space with a composing char. */
4336 if (utf_iscomposing(mb_c))
4337 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004338 int i;
4339
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004340 for (i = Screen_mco - 1; i > 0; --i)
4341 u8cc[i] = u8cc[i - 1];
4342 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004343 mb_c = ' ';
4344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346
4347 if ((mb_l == 1 && c >= 0x80)
4348 || (mb_l >= 1 && mb_c == 0)
4349 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004350# ifdef UNICODE16
4351 || mb_c >= 0x10000
4352# endif
4353 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 /*
4356 * Illegal UTF-8 byte: display as <xx>.
4357 * Non-BMP character : display as ? or fullwidth ?.
4358 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004359# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
4363 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004364# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 if (wp->w_p_rl) /* reverse */
4366 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004369# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 else if (utf_char2cells(mb_c) != 2)
4371 STRCPY(extra, "?");
4372 else
4373 /* 0xff1f in UTF-8: full-width '?' */
4374 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 p_extra = extra;
4378 c = *p_extra;
4379 mb_c = mb_ptr2char_adv(&p_extra);
4380 mb_utf8 = (c >= 0x80);
4381 n_extra = (int)STRLEN(p_extra);
4382 c_extra = NUL;
4383 if (area_attr == 0 && search_attr == 0)
4384 {
4385 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004386 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 saved_attr2 = char_attr; /* save current attr */
4388 }
4389 }
4390 else if (mb_l == 0) /* at the NUL at end-of-line */
4391 mb_l = 1;
4392#ifdef FEAT_ARABIC
4393 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4394 {
4395 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004396 int pc, pc1, nc;
4397 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 /* The idea of what is the previous and next
4400 * character depends on 'rightleft'. */
4401 if (wp->w_p_rl)
4402 {
4403 pc = prev_c;
4404 pc1 = prev_c1;
4405 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004406 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408 else
4409 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004412 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 prev_c = mb_c;
4415
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004416 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 else
4419 prev_c = mb_c;
4420#endif
4421 }
4422 else /* enc_dbcs */
4423 {
4424 mb_l = MB_BYTE2LEN(c);
4425 if (mb_l == 0) /* at the NUL at end-of-line */
4426 mb_l = 1;
4427 else if (mb_l > 1)
4428 {
4429 /* We assume a second byte below 32 is illegal.
4430 * Hopefully this is OK for all double-byte encodings!
4431 */
4432 if (ptr[1] >= 32)
4433 mb_c = (c << 8) + ptr[1];
4434 else
4435 {
4436 if (ptr[1] == NUL)
4437 {
4438 /* head byte at end of line */
4439 mb_l = 1;
4440 transchar_nonprint(extra, c);
4441 }
4442 else
4443 {
4444 /* illegal tail byte */
4445 mb_l = 2;
4446 STRCPY(extra, "XX");
4447 }
4448 p_extra = extra;
4449 n_extra = (int)STRLEN(extra) - 1;
4450 c_extra = NUL;
4451 c = *p_extra++;
4452 if (area_attr == 0 && search_attr == 0)
4453 {
4454 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004455 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 saved_attr2 = char_attr; /* save current attr */
4457 }
4458 mb_c = c;
4459 }
4460 }
4461 }
4462 /* If a double-width char doesn't fit display a '>' in the
4463 * last column; the character is displayed at the start of the
4464 * next line. */
4465 if ((
4466# ifdef FEAT_RIGHTLEFT
4467 wp->w_p_rl ? (col <= 0) :
4468# endif
4469 (col >= W_WIDTH(wp) - 1))
4470 && (*mb_char2cells)(mb_c) == 2)
4471 {
4472 c = '>';
4473 mb_c = c;
4474 mb_utf8 = FALSE;
4475 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004476 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 /* Put pointer back so that the character will be
4478 * displayed at the start of the next line. */
4479 --ptr;
4480 }
4481 else if (*ptr != NUL)
4482 ptr += mb_l - 1;
4483
4484 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004485 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004486 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004487 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004490 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 c = ' ';
4492 if (area_attr == 0 && search_attr == 0)
4493 {
4494 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 saved_attr2 = char_attr; /* save current attr */
4497 }
4498 mb_c = c;
4499 mb_utf8 = FALSE;
4500 mb_l = 1;
4501 }
4502
4503 }
4504#endif
4505 ++ptr;
4506
4507 if (extra_check)
4508 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004509#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004511#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004512
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004513#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /* Get syntax attribute, unless still at the start of the line
4515 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004516 v = (long)(ptr - line);
4517 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
4519 /* Get the syntax attribute for the character. If there
4520 * is an error, disable syntax highlighting. */
4521 save_did_emsg = did_emsg;
4522 did_emsg = FALSE;
4523
Bram Moolenaar217ad922005-03-20 22:37:15 +00004524 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004525# ifdef FEAT_SPELL
4526 has_spell ? &can_spell :
4527# endif
4528 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
4530 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004531 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004532 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004533 has_syntax = FALSE;
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 else
4536 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004537#ifdef SYN_TIME_LIMIT
4538 if (wp->w_s->b_syn_slow)
4539 has_syntax = FALSE;
4540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 /* Need to get the line again, a multi-line regexp may
4543 * have made it invalid. */
4544 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4545 ptr = line + v;
4546
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004547 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004549 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004550 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004551# ifdef FEAT_CONCEAL
4552 /* no concealing past the end of the line, it interferes
4553 * with line highlighting */
4554 if (c == NUL)
4555 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004556 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004557 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004561
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004562#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004563 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004564 * Only do this when there is no syntax highlighting, the
4565 * @Spell cluster is not used or the current syntax item
4566 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004567 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004568 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004569 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004570# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004571 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004572 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004573# endif
4574 if (c != 0 && (
4575# ifdef FEAT_SYN_HL
4576 !has_syntax ||
4577# endif
4578 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004579 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004580 char_u *prev_ptr, *p;
4581 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004582 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004583# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004584 if (has_mbyte)
4585 {
4586 prev_ptr = ptr - mb_l;
4587 v -= mb_l - 1;
4588 }
4589 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004590# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004591 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004592
4593 /* Use nextline[] if possible, it has the start of the
4594 * next line concatenated. */
4595 if ((prev_ptr - line) - nextlinecol >= 0)
4596 p = nextline + (prev_ptr - line) - nextlinecol;
4597 else
4598 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004599 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004600 len = spell_check(wp, p, &spell_hlf, &cap_col,
4601 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004602 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004603
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004604 /* In Insert mode only highlight a word that
4605 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004606 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004607 && (State & INSERT) != 0
4608 && wp->w_cursor.lnum == lnum
4609 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004610 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004611 && wp->w_cursor.col < (colnr_T)word_end)
4612 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004613 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004614 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004615 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004616
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004618 && (p - nextline) + len > nextline_idx)
4619 {
4620 /* Remember that the good word continues at the
4621 * start of the next line. */
4622 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004623 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004624 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004625
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004626 /* Turn index into actual attributes. */
4627 if (spell_hlf != HLF_COUNT)
4628 spell_attr = highlight_attr[spell_hlf];
4629
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004630 if (cap_col > 0)
4631 {
4632 if (p != prev_ptr
4633 && (p - nextline) + cap_col >= nextline_idx)
4634 {
4635 /* Remember that the word in the next line
4636 * must start with a capital. */
4637 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004638 cap_col = (int)((p - nextline) + cap_col
4639 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004640 }
4641 else
4642 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004643 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004644 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004645 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004646 }
4647 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004648 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004649 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004650 char_attr = hl_combine_attr(char_attr, spell_attr);
4651 else
4652 char_attr = hl_combine_attr(spell_attr, char_attr);
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655#ifdef FEAT_LINEBREAK
4656 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004657 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004659 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004660 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004662# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004663 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004664# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004665 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004667 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004669 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004670
Bram Moolenaar597a4222014-06-25 14:39:50 +02004671 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004672 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004673 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004674 if (c == TAB && n_extra + col > W_WIDTH(wp))
4675 n_extra = (int)wp->w_buffer->b_p_ts
4676 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4677
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004678# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004679 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004680# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004682# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004683 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004684 {
4685#ifdef FEAT_CONCEAL
4686 if (c == TAB)
4687 /* See "Tab alignment" below. */
4688 FIX_FOR_BOGUSCOLS;
4689#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004690 if (!wp->w_p_list)
4691 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694#endif
4695
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004696 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4697 */
4698 if (wp->w_p_list
4699 && (((c == 160
4700#ifdef FEAT_MBYTE
4701 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4702#endif
4703 ) && lcs_nbsp)
4704 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4705 {
4706 c = (c == ' ') ? lcs_space : lcs_nbsp;
4707 if (area_attr == 0 && search_attr == 0)
4708 {
4709 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004710 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004711 saved_attr2 = char_attr; /* save current attr */
4712 }
4713#ifdef FEAT_MBYTE
4714 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004715 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004716 {
4717 mb_utf8 = TRUE;
4718 u8cc[0] = 0;
4719 c = 0xc0;
4720 }
4721 else
4722 mb_utf8 = FALSE;
4723#endif
4724 }
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4727 {
4728 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004729 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004732 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 saved_attr2 = char_attr; /* save current attr */
4734 }
4735#ifdef FEAT_MBYTE
4736 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004737 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
4739 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004740 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004741 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743 else
4744 mb_utf8 = FALSE;
4745#endif
4746 }
4747 }
4748
4749 /*
4750 * Handling of non-printable characters.
4751 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004752 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 /*
4755 * when getting a character from the file, we may have to
4756 * turn it into something else on the way to putting it
4757 * into "ScreenLines".
4758 */
4759 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4760 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004761 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004762 long vcol_adjusted = vcol; /* removed showbreak length */
4763#ifdef FEAT_LINEBREAK
4764 /* only adjust the tab_len, when at the first column
4765 * after the showbreak value was drawn */
4766 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4767 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004770 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004771 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4772
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004773#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004774 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004775#endif
4776 /* tab amount depends on current column */
4777 n_extra = tab_len;
4778#ifdef FEAT_LINEBREAK
4779 else
4780 {
4781 char_u *p;
4782 int len = n_extra;
4783 int i;
4784 int saved_nextra = n_extra;
4785
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004786#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004787 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004788 /* there are characters to conceal */
4789 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004790 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4791 */
4792 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4793 && n_extra > tab_len)
4794 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004795#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004796
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004797 /* if n_extra > 0, it gives the number of chars, to
4798 * use for a tab, else we need to calculate the width
4799 * for a tab */
4800#ifdef FEAT_MBYTE
4801 len = (tab_len * mb_char2len(lcs_tab2));
4802 if (n_extra > 0)
4803 len += n_extra - tab_len;
4804#endif
4805 c = lcs_tab1;
4806 p = alloc((unsigned)(len + 1));
4807 vim_memset(p, ' ', len);
4808 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004809 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810 p_extra_free = p;
4811 for (i = 0; i < tab_len; i++)
4812 {
4813#ifdef FEAT_MBYTE
4814 mb_char2bytes(lcs_tab2, p);
4815 p += mb_char2len(lcs_tab2);
4816 n_extra += mb_char2len(lcs_tab2)
4817 - (saved_nextra > 0 ? 1 : 0);
4818#else
4819 p[i] = lcs_tab2;
4820#endif
4821 }
4822 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004823#ifdef FEAT_CONCEAL
4824 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4825 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004826 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004827 n_extra -= vcol_off;
4828#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004829 }
4830#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004831#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004832 {
4833 int vc_saved = vcol_off;
4834
4835 /* Tab alignment should be identical regardless of
4836 * 'conceallevel' value. So tab compensates of all
4837 * previous concealed characters, and thus resets
4838 * vcol_off and boguscols accumulated so far in the
4839 * line. Note that the tab can be longer than
4840 * 'tabstop' when there are concealed characters. */
4841 FIX_FOR_BOGUSCOLS;
4842
4843 /* Make sure, the highlighting for the tab char will be
4844 * correctly set further below (effectively reverts the
4845 * FIX_FOR_BOGSUCOLS macro */
4846 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004847 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004848 tab_len += vc_saved;
4849 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#ifdef FEAT_MBYTE
4852 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4853#endif
4854 if (wp->w_p_list)
4855 {
4856 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004857#ifdef FEAT_LINEBREAK
4858 if (wp->w_p_lbr)
4859 c_extra = NUL; /* using p_extra from above */
4860 else
4861#endif
4862 c_extra = lcs_tab2;
4863 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004864 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 saved_attr2 = char_attr; /* save current attr */
4866#ifdef FEAT_MBYTE
4867 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004868 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004871 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004872 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874#endif
4875 }
4876 else
4877 {
4878 c_extra = ' ';
4879 c = ' ';
4880 }
4881 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004882 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004883 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004884 || ((fromcol >= 0 || fromcol_prev >= 0)
4885 && tocol > vcol
4886 && VIsual_mode != Ctrl_V
4887 && (
4888# ifdef FEAT_RIGHTLEFT
4889 wp->w_p_rl ? (col >= 0) :
4890# endif
4891 (col < W_WIDTH(wp)))
4892 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004893 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004894 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004895 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004897 /* Display a '$' after the line or highlight an extra
4898 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4900 /* For a diff line the highlighting continues after the
4901 * "$". */
4902 if (
4903# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004904 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905# ifdef LINE_ATTR
4906 &&
4907# endif
4908# endif
4909# ifdef LINE_ATTR
4910 line_attr == 0
4911# endif
4912 )
4913#endif
4914 {
4915#ifdef FEAT_VIRTUALEDIT
4916 /* In virtualedit, visual selections may extend
4917 * beyond end of line. */
4918 if (area_highlighting && virtual_active()
4919 && tocol != MAXCOL && vcol < tocol)
4920 n_extra = 0;
4921 else
4922#endif
4923 {
4924 p_extra = at_end_str;
4925 n_extra = 1;
4926 c_extra = NUL;
4927 }
4928 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004929 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004930 c = lcs_eol;
4931 else
4932 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 lcs_eol_one = -1;
4934 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004935 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004937 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 n_attr = 1;
4939 }
4940#ifdef FEAT_MBYTE
4941 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004942 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004945 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004946 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 else
4949 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4950#endif
4951 }
4952 else if (c != NUL)
4953 {
4954 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004955 if (n_extra == 0)
4956 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957#ifdef FEAT_RIGHTLEFT
4958 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4959 rl_mirror(p_extra); /* reverse "<12>" */
4960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004962#ifdef FEAT_LINEBREAK
4963 if (wp->w_p_lbr)
4964 {
4965 char_u *p;
4966
4967 c = *p_extra;
4968 p = alloc((unsigned)n_extra + 1);
4969 vim_memset(p, ' ', n_extra);
4970 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4971 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004972 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004973 p_extra_free = p_extra = p;
4974 }
4975 else
4976#endif
4977 {
4978 n_extra = byte2cells(c) - 1;
4979 c = *p_extra++;
4980 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004981 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004984 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 saved_attr2 = char_attr; /* save current attr */
4986 }
4987#ifdef FEAT_MBYTE
4988 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4989#endif
4990 }
4991#ifdef FEAT_VIRTUALEDIT
4992 else if (VIsual_active
4993 && (VIsual_mode == Ctrl_V
4994 || VIsual_mode == 'v')
4995 && virtual_active()
4996 && tocol != MAXCOL
4997 && vcol < tocol
4998 && (
4999# ifdef FEAT_RIGHTLEFT
5000 wp->w_p_rl ? (col >= 0) :
5001# endif
5002 (col < W_WIDTH(wp))))
5003 {
5004 c = ' ';
5005 --ptr; /* put it back at the NUL */
5006 }
5007#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005008#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else if ((
5010# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005011 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 ) && (
5015# ifdef FEAT_RIGHTLEFT
5016 wp->w_p_rl ? (col >= 0) :
5017# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005018 (col
5019# ifdef FEAT_CONCEAL
5020 - boguscols
5021# endif
5022 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 /* Highlight until the right side of the window */
5025 c = ' ';
5026 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005027
5028 /* Remember we do the char for line highlighting. */
5029 ++did_line_attr;
5030
5031 /* don't do search HL for the rest of the line */
5032 if (line_attr != 0 && char_attr == search_attr && col > 0)
5033 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034# ifdef FEAT_DIFF
5035 if (diff_hlf == HLF_TXD)
5036 {
5037 diff_hlf = HLF_CHD;
5038 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005039 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005040 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005041 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5042 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005043 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046# endif
5047 }
5048#endif
5049 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005050
5051#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005052 if ( wp->w_p_cole > 0
5053 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005054 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005055 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005056 && !(lnum_in_visual_area
5057 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005058 {
5059 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005060 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005061 && (syn_get_sub_char() != NUL || match_conc
5062 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005063 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005064 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005065 /* First time at this concealed item: display one
5066 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005067 if (match_conc)
5068 c = match_conc;
5069 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005070 c = syn_get_sub_char();
5071 else if (lcs_conceal != NUL)
5072 c = lcs_conceal;
5073 else
5074 c = ' ';
5075
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005076 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005077
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005078 if (n_extra > 0)
5079 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005080 vcol += n_extra;
5081 if (wp->w_p_wrap && n_extra > 0)
5082 {
5083# ifdef FEAT_RIGHTLEFT
5084 if (wp->w_p_rl)
5085 {
5086 col -= n_extra;
5087 boguscols -= n_extra;
5088 }
5089 else
5090# endif
5091 {
5092 boguscols += n_extra;
5093 col += n_extra;
5094 }
5095 }
5096 n_extra = 0;
5097 n_attr = 0;
5098 }
5099 else if (n_skip == 0)
5100 {
5101 is_concealing = TRUE;
5102 n_skip = 1;
5103 }
5104# ifdef FEAT_MBYTE
5105 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005106 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005107 {
5108 mb_utf8 = TRUE;
5109 u8cc[0] = 0;
5110 c = 0xc0;
5111 }
5112 else
5113 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5114# endif
5115 }
5116 else
5117 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005118 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005119 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005120 }
5121#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005124#ifdef FEAT_CONCEAL
5125 /* In the cursor line and we may be concealing characters: correct
5126 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005127 if (!did_wcol && draw_state == WL_LINE
5128 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005129 && conceal_cursor_line(wp)
5130 && (int)wp->w_virtcol <= vcol + n_skip)
5131 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005132# ifdef FEAT_RIGHTLEFT
5133 if (wp->w_p_rl)
5134 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5135 else
5136# endif
5137 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005138 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005139 did_wcol = TRUE;
5140 }
5141#endif
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 /* Don't override visual selection highlighting. */
5144 if (n_attr > 0
5145 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005146 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 char_attr = extra_attr;
5148
Bram Moolenaar81695252004-12-29 20:58:21 +00005149#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 /* XIM don't send preedit_start and preedit_end, but they send
5151 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5152 * im_is_preediting() here. */
5153 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005154 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 && (State & INSERT)
5156 && !p_imdisable
5157 && im_is_preediting()
5158 && draw_state == WL_LINE)
5159 {
5160 colnr_T tcol;
5161
5162 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005163 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 else
5165 tcol = preedit_end_col;
5166 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5167 {
5168 if (feedback_old_attr < 0)
5169 {
5170 feedback_col = 0;
5171 feedback_old_attr = char_attr;
5172 }
5173 char_attr = im_get_feedback_attr(feedback_col);
5174 if (char_attr < 0)
5175 char_attr = feedback_old_attr;
5176 feedback_col++;
5177 }
5178 else if (feedback_old_attr >= 0)
5179 {
5180 char_attr = feedback_old_attr;
5181 feedback_old_attr = -1;
5182 feedback_col = 0;
5183 }
5184 }
5185#endif
5186 /*
5187 * Handle the case where we are in column 0 but not on the first
5188 * character of the line and the user wants us to show us a
5189 * special character (via 'listchars' option "precedes:<char>".
5190 */
5191 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005192 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5194#ifdef FEAT_DIFF
5195 && filler_todo <= 0
5196#endif
5197 && draw_state > WL_NR
5198 && c != NUL)
5199 {
5200 c = lcs_prec;
5201 lcs_prec_todo = NUL;
5202#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005203 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5204 {
5205 /* Double-width character being overwritten by the "precedes"
5206 * character, need to fill up half the character. */
5207 c_extra = MB_FILLER_CHAR;
5208 n_extra = 1;
5209 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005210 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005213 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 {
5215 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005216 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005217 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 else
5220 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5221#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005222 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005225 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 n_attr3 = 1;
5227 }
5228 }
5229
5230 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005231 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005233 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005234#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005235 || did_line_attr == 1
5236#endif
5237 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005239#ifdef FEAT_SEARCH_EXTRA
5240 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005241
5242 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005243 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005244 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005245#endif
5246
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005247 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 * highlight match at end of line. If it's beyond the last
5249 * char on the screen, just overwrite that one (tricky!) Not
5250 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005251#ifdef FEAT_SEARCH_EXTRA
5252 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005253 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005254 prevcol_hl_flag = TRUE;
5255 else
5256 {
5257 cur = wp->w_match_head;
5258 while (cur != NULL)
5259 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005260 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005261 {
5262 prevcol_hl_flag = TRUE;
5263 break;
5264 }
5265 cur = cur->next;
5266 }
5267 }
5268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005270 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005271 && (VIsual_mode != Ctrl_V
5272 || lnum == VIsual.lnum
5273 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005274 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275#ifdef FEAT_SEARCH_EXTRA
5276 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005277 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005278# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279 && did_line_attr <= 1
5280# endif
5281 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#endif
5283 ))
5284 {
5285 int n = 0;
5286
5287#ifdef FEAT_RIGHTLEFT
5288 if (wp->w_p_rl)
5289 {
5290 if (col < 0)
5291 n = 1;
5292 }
5293 else
5294#endif
5295 {
5296 if (col >= W_WIDTH(wp))
5297 n = -1;
5298 }
5299 if (n != 0)
5300 {
5301 /* At the window boundary, highlight the last character
5302 * instead (better than nothing). */
5303 off += n;
5304 col += n;
5305 }
5306 else
5307 {
5308 /* Add a blank character to highlight. */
5309 ScreenLines[off] = ' ';
5310#ifdef FEAT_MBYTE
5311 if (enc_utf8)
5312 ScreenLinesUC[off] = 0;
5313#endif
5314 }
5315#ifdef FEAT_SEARCH_EXTRA
5316 if (area_attr == 0)
5317 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005318 /* Use attributes from match with highest priority among
5319 * 'search_hl' and the match list. */
5320 char_attr = search_hl.attr;
5321 cur = wp->w_match_head;
5322 shl_flag = FALSE;
5323 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005324 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005325 if (shl_flag == FALSE
5326 && ((cur != NULL
5327 && cur->priority > SEARCH_HL_PRIORITY)
5328 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005329 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005330 shl = &search_hl;
5331 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005332 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005333 else
5334 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005335 if ((ptr - line) - 1 == (long)shl->startcol
5336 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005337 char_attr = shl->attr;
5338 if (shl != &search_hl && cur != NULL)
5339 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342#endif
5343 ScreenAttrs[off] = char_attr;
5344#ifdef FEAT_RIGHTLEFT
5345 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005348 --off;
5349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 else
5351#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005354 ++off;
5355 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005356 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005357#ifdef FEAT_SYN_HL
5358 eol_hl_off = 1;
5359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Bram Moolenaar91170f82006-05-05 21:15:17 +00005363 /*
5364 * At end of the text line.
5365 */
5366 if (c == NUL)
5367 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005368#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005369 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5370 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005371 {
5372 /* highlight last char after line */
5373 --col;
5374 --off;
5375 --vcol;
5376 }
5377
Bram Moolenaar1a384422010-07-14 19:53:30 +02005378 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005379 if (wp->w_p_wrap)
5380 v = wp->w_skipcol;
5381 else
5382 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005383
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005384 /* check if line ends before left margin */
5385 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005386 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005387#ifdef FEAT_CONCEAL
5388 /* Get rid of the boguscols now, we want to draw until the right
5389 * edge for 'cursorcolumn'. */
5390 col -= boguscols;
5391 boguscols = 0;
5392#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005393
5394 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005395 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005396
5397 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005398 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5399 && (int)wp->w_virtcol <
5400 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005401 && lnum != wp->w_cursor.lnum)
5402 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005403# ifdef FEAT_RIGHTLEFT
5404 && !wp->w_p_rl
5405# endif
5406 )
5407 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005408 int rightmost_vcol = 0;
5409 int i;
5410
5411 if (wp->w_p_cuc)
5412 rightmost_vcol = wp->w_virtcol;
5413 if (draw_color_col)
5414 /* determine rightmost colorcolumn to possibly draw */
5415 for (i = 0; color_cols[i] >= 0; ++i)
5416 if (rightmost_vcol < color_cols[i])
5417 rightmost_vcol = color_cols[i];
5418
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005419 while (col < W_WIDTH(wp))
5420 {
5421 ScreenLines[off] = ' ';
5422#ifdef FEAT_MBYTE
5423 if (enc_utf8)
5424 ScreenLinesUC[off] = 0;
5425#endif
5426 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005427 if (draw_color_col)
5428 draw_color_col = advance_color_col(VCOL_HLC,
5429 &color_cols);
5430
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005431 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005432 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005433 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005434 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005435 else
5436 ScreenAttrs[off++] = 0;
5437
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005438 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005439 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005440
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005441 ++vcol;
5442 }
5443 }
5444#endif
5445
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5447 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 row++;
5449
5450 /*
5451 * Update w_cline_height and w_cline_folded if the cursor line was
5452 * updated (saves a call to plines() later).
5453 */
5454 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5455 {
5456 curwin->w_cline_row = startrow;
5457 curwin->w_cline_height = row - startrow;
5458#ifdef FEAT_FOLDING
5459 curwin->w_cline_folded = FALSE;
5460#endif
5461 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5462 }
5463
5464 break;
5465 }
5466
5467 /* line continues beyond line end */
5468 if (lcs_ext
5469 && !wp->w_p_wrap
5470#ifdef FEAT_DIFF
5471 && filler_todo <= 0
5472#endif
5473 && (
5474#ifdef FEAT_RIGHTLEFT
5475 wp->w_p_rl ? col == 0 :
5476#endif
5477 col == W_WIDTH(wp) - 1)
5478 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005479 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5481 {
5482 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005483 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#ifdef FEAT_MBYTE
5485 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005486 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005489 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005490 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else
5493 mb_utf8 = FALSE;
5494#endif
5495 }
5496
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005498 /* advance to the next 'colorcolumn' */
5499 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005500 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005501
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005502 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005503 * highlight the cursor position itself.
5504 * Also highlight the 'colorcolumn' if it is different than
5505 * 'cursorcolumn' */
5506 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005507 if (draw_state == WL_LINE && !lnum_in_visual_area
5508 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005509 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005510 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511 && lnum != wp->w_cursor.lnum)
5512 {
5513 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005514 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005516 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517 {
5518 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005519 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005521 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005522#endif
5523
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 /*
5525 * Store character to be displayed.
5526 * Skip characters that are left of the screen for 'nowrap'.
5527 */
5528 vcol_prev = vcol;
5529 if (draw_state < WL_LINE || n_skip <= 0)
5530 {
5531 /*
5532 * Store the character.
5533 */
5534#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5535 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5536 {
5537 /* A double-wide character is: put first halve in left cell. */
5538 --off;
5539 --col;
5540 }
5541#endif
5542 ScreenLines[off] = c;
5543#ifdef FEAT_MBYTE
5544 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005545 {
5546 if ((mb_c & 0xff00) == 0x8e00)
5547 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else if (enc_utf8)
5551 {
5552 if (mb_utf8)
5553 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005554 int i;
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005557 if ((c & 0xff) == 0)
5558 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005559 for (i = 0; i < Screen_mco; ++i)
5560 {
5561 ScreenLinesC[i][off] = u8cc[i];
5562 if (u8cc[i] == 0)
5563 break;
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else
5567 ScreenLinesUC[off] = 0;
5568 }
5569 if (multi_attr)
5570 {
5571 ScreenAttrs[off] = multi_attr;
5572 multi_attr = 0;
5573 }
5574 else
5575#endif
5576 ScreenAttrs[off] = char_attr;
5577
5578#ifdef FEAT_MBYTE
5579 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5580 {
5581 /* Need to fill two screen columns. */
5582 ++off;
5583 ++col;
5584 if (enc_utf8)
5585 /* UTF-8: Put a 0 in the second screen char. */
5586 ScreenLines[off] = 0;
5587 else
5588 /* DBCS: Put second byte in the second screen char. */
5589 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005590 if (draw_state > WL_NR
5591#ifdef FEAT_DIFF
5592 && filler_todo <= 0
5593#endif
5594 )
5595 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 /* When "tocol" is halfway a character, set it to the end of
5597 * the character, otherwise highlighting won't stop. */
5598 if (tocol == vcol)
5599 ++tocol;
5600#ifdef FEAT_RIGHTLEFT
5601 if (wp->w_p_rl)
5602 {
5603 /* now it's time to backup one cell */
5604 --off;
5605 --col;
5606 }
5607#endif
5608 }
5609#endif
5610#ifdef FEAT_RIGHTLEFT
5611 if (wp->w_p_rl)
5612 {
5613 --off;
5614 --col;
5615 }
5616 else
5617#endif
5618 {
5619 ++off;
5620 ++col;
5621 }
5622 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005623#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005624 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005625 {
5626 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005627 ++vcol_off;
5628 if (n_extra > 0)
5629 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005630 if (wp->w_p_wrap)
5631 {
5632 /*
5633 * Special voodoo required if 'wrap' is on.
5634 *
5635 * Advance the column indicator to force the line
5636 * drawing to wrap early. This will make the line
5637 * take up the same screen space when parts are concealed,
5638 * so that cursor line computations aren't messed up.
5639 *
5640 * To avoid the fictitious advance of 'col' causing
5641 * trailing junk to be written out of the screen line
5642 * we are building, 'boguscols' keeps track of the number
5643 * of bad columns we have advanced.
5644 */
5645 if (n_extra > 0)
5646 {
5647 vcol += n_extra;
5648# ifdef FEAT_RIGHTLEFT
5649 if (wp->w_p_rl)
5650 {
5651 col -= n_extra;
5652 boguscols -= n_extra;
5653 }
5654 else
5655# endif
5656 {
5657 col += n_extra;
5658 boguscols += n_extra;
5659 }
5660 n_extra = 0;
5661 n_attr = 0;
5662 }
5663
5664
5665# ifdef FEAT_MBYTE
5666 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5667 {
5668 /* Need to fill two screen columns. */
5669# ifdef FEAT_RIGHTLEFT
5670 if (wp->w_p_rl)
5671 {
5672 --boguscols;
5673 --col;
5674 }
5675 else
5676# endif
5677 {
5678 ++boguscols;
5679 ++col;
5680 }
5681 }
5682# endif
5683
5684# ifdef FEAT_RIGHTLEFT
5685 if (wp->w_p_rl)
5686 {
5687 --boguscols;
5688 --col;
5689 }
5690 else
5691# endif
5692 {
5693 ++boguscols;
5694 ++col;
5695 }
5696 }
5697 else
5698 {
5699 if (n_extra > 0)
5700 {
5701 vcol += n_extra;
5702 n_extra = 0;
5703 n_attr = 0;
5704 }
5705 }
5706
5707 }
5708#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
5710 --n_skip;
5711
Bram Moolenaar64486672010-05-16 15:46:46 +02005712 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5713 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005714 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715#ifdef FEAT_DIFF
5716 && filler_todo <= 0
5717#endif
5718 )
5719 ++vcol;
5720
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005721#ifdef FEAT_SYN_HL
5722 if (vcol_save_attr >= 0)
5723 char_attr = vcol_save_attr;
5724#endif
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 /* restore attributes after "predeces" in 'listchars' */
5727 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5728 char_attr = saved_attr3;
5729
5730 /* restore attributes after last 'listchars' or 'number' char */
5731 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5732 char_attr = saved_attr2;
5733
5734 /*
5735 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005736 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 */
5738 if ((
5739#ifdef FEAT_RIGHTLEFT
5740 wp->w_p_rl ? (col < 0) :
5741#endif
5742 (col >= W_WIDTH(wp)))
5743 && (*ptr != NUL
5744#ifdef FEAT_DIFF
5745 || filler_todo > 0
5746#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005747 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5749 )
5750 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005751#ifdef FEAT_CONCEAL
5752 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5753 (int)W_WIDTH(wp), wp->w_p_rl);
5754 boguscols = 0;
5755#else
5756 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5757 (int)W_WIDTH(wp), wp->w_p_rl);
5758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 ++row;
5760 ++screen_row;
5761
5762 /* When not wrapping and finished diff lines, or when displayed
5763 * '$' and highlighting until last column, break here. */
5764 if ((!wp->w_p_wrap
5765#ifdef FEAT_DIFF
5766 && filler_todo <= 0
5767#endif
5768 ) || lcs_eol_one == -1)
5769 break;
5770
5771 /* When the window is too narrow draw all "@" lines. */
5772 if (draw_state != WL_LINE
5773#ifdef FEAT_DIFF
5774 && filler_todo <= 0
5775#endif
5776 )
5777 {
5778 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005779#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 draw_vsep_win(wp, row);
5781#endif
5782 row = endrow;
5783 }
5784
5785 /* When line got too long for screen break here. */
5786 if (row == endrow)
5787 {
5788 ++row;
5789 break;
5790 }
5791
5792 if (screen_cur_row == screen_row - 1
5793#ifdef FEAT_DIFF
5794 && filler_todo <= 0
5795#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005796#ifdef FEAT_WINDOWS
5797 && W_WIDTH(wp) == Columns
5798#endif
5799 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 {
5801 /* Remember that the line wraps, used for modeless copy. */
5802 LineWraps[screen_row - 1] = TRUE;
5803
5804 /*
5805 * Special trick to make copy/paste of wrapped lines work with
5806 * xterm/screen: write an extra character beyond the end of
5807 * the line. This will work with all terminal types
5808 * (regardless of the xn,am settings).
5809 * Only do this on a fast tty.
5810 * Only do this if the cursor is on the current line
5811 * (something has been written in it).
5812 * Don't do this for the GUI.
5813 * Don't do this for double-width characters.
5814 * Don't do this for a window not at the right screen border.
5815 */
5816 if (p_tf
5817#ifdef FEAT_GUI
5818 && !gui.in_use
5819#endif
5820#ifdef FEAT_MBYTE
5821 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005822 && ((*mb_off2cells)(LineOffset[screen_row],
5823 LineOffset[screen_row] + screen_Columns)
5824 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005826 + (int)Columns - 2,
5827 LineOffset[screen_row] + screen_Columns)
5828 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#endif
5830 )
5831 {
5832 /* First make sure we are at the end of the screen line,
5833 * then output the same character again to let the
5834 * terminal know about the wrap. If the terminal doesn't
5835 * auto-wrap, we overwrite the character. */
5836 if (screen_cur_col != W_WIDTH(wp))
5837 screen_char(LineOffset[screen_row - 1]
5838 + (unsigned)Columns - 1,
5839 screen_row - 1, (int)(Columns - 1));
5840
5841#ifdef FEAT_MBYTE
5842 /* When there is a multi-byte character, just output a
5843 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005844 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5845 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 out_char(' ');
5847 else
5848#endif
5849 out_char(ScreenLines[LineOffset[screen_row - 1]
5850 + (Columns - 1)]);
5851 /* force a redraw of the first char on the next line */
5852 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5853 screen_start(); /* don't know where cursor is now */
5854 }
5855 }
5856
5857 col = 0;
5858 off = (unsigned)(current_ScreenLine - ScreenLines);
5859#ifdef FEAT_RIGHTLEFT
5860 if (wp->w_p_rl)
5861 {
5862 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5863 off += col;
5864 }
5865#endif
5866
5867 /* reset the drawing state for the start of a wrapped line */
5868 draw_state = WL_START;
5869 saved_n_extra = n_extra;
5870 saved_p_extra = p_extra;
5871 saved_c_extra = c_extra;
5872 saved_char_attr = char_attr;
5873 n_extra = 0;
5874 lcs_prec_todo = lcs_prec;
5875#ifdef FEAT_LINEBREAK
5876# ifdef FEAT_DIFF
5877 if (filler_todo <= 0)
5878# endif
5879 need_showbreak = TRUE;
5880#endif
5881#ifdef FEAT_DIFF
5882 --filler_todo;
5883 /* When the filler lines are actually below the last line of the
5884 * file, don't draw the line itself, break here. */
5885 if (filler_todo == 0 && wp->w_botfill)
5886 break;
5887#endif
5888 }
5889
5890 } /* for every character in the line */
5891
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005892#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005893 /* After an empty line check first word for capital. */
5894 if (*skipwhite(line) == NUL)
5895 {
5896 capcol_lnum = lnum + 1;
5897 cap_col = 0;
5898 }
5899#endif
5900
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005901 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 return row;
5903}
5904
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005905#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005906static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005907
5908/*
5909 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005910 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005911 */
5912 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005913comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005914{
5915 int i;
5916
5917 for (i = 0; i < Screen_mco; ++i)
5918 {
5919 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5920 return TRUE;
5921 if (ScreenLinesC[i][off_from] == 0)
5922 break;
5923 }
5924 return FALSE;
5925}
5926#endif
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
5929 * Check whether the given character needs redrawing:
5930 * - the (first byte of the) character is different
5931 * - the attributes are different
5932 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005933 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 */
5935 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005936char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 if (cols > 0
5939 && ((ScreenLines[off_from] != ScreenLines[off_to]
5940 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5941
5942#ifdef FEAT_MBYTE
5943 || (enc_dbcs != 0
5944 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5945 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5946 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5947 : (cols > 1 && ScreenLines[off_from + 1]
5948 != ScreenLines[off_to + 1])))
5949 || (enc_utf8
5950 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5951 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005952 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005953 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5954 && ScreenLines[off_from + 1]
5955 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956#endif
5957 ))
5958 return TRUE;
5959 return FALSE;
5960}
5961
5962/*
5963 * Move one "cooked" screen line to the screen, but only the characters that
5964 * have actually changed. Handle insert/delete character.
5965 * "coloff" gives the first column on the screen for this line.
5966 * "endcol" gives the columns where valid characters are.
5967 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5968 * needs to be cleared, negative otherwise.
5969 * "rlflag" is TRUE in a rightleft window:
5970 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5971 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5972 */
5973 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005974screen_line(
5975 int row,
5976 int coloff,
5977 int endcol,
5978 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005980 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005982 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 unsigned off_from;
5985 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005986#ifdef FEAT_MBYTE
5987 unsigned max_off_from;
5988 unsigned max_off_to;
5989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005991#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 int hl;
5993#endif
5994 int force = FALSE; /* force update rest of the line */
5995 int redraw_this /* bool: does character need redraw? */
5996#ifdef FEAT_GUI
5997 = TRUE /* For GUI when while-loop empty */
5998#endif
5999 ;
6000 int redraw_next; /* redraw_this for next character */
6001#ifdef FEAT_MBYTE
6002 int clear_next = FALSE;
6003 int char_cells; /* 1: normal char */
6004 /* 2: occupies two display cells */
6005# define CHAR_CELLS char_cells
6006#else
6007# define CHAR_CELLS 1
6008#endif
6009
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006010 /* Check for illegal row and col, just in case. */
6011 if (row >= Rows)
6012 row = Rows - 1;
6013 if (endcol > Columns)
6014 endcol = Columns;
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016# ifdef FEAT_CLIPBOARD
6017 clip_may_clear_selection(row, row);
6018# endif
6019
6020 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6021 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006022#ifdef FEAT_MBYTE
6023 max_off_from = off_from + screen_Columns;
6024 max_off_to = LineOffset[row] + screen_Columns;
6025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027#ifdef FEAT_RIGHTLEFT
6028 if (rlflag)
6029 {
6030 /* Clear rest first, because it's left of the text. */
6031 if (clear_width > 0)
6032 {
6033 while (col <= endcol && ScreenLines[off_to] == ' '
6034 && ScreenAttrs[off_to] == 0
6035# ifdef FEAT_MBYTE
6036 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6037# endif
6038 )
6039 {
6040 ++off_to;
6041 ++col;
6042 }
6043 if (col <= endcol)
6044 screen_fill(row, row + 1, col + coloff,
6045 endcol + coloff + 1, ' ', ' ', 0);
6046 }
6047 col = endcol + 1;
6048 off_to = LineOffset[row] + col + coloff;
6049 off_from += col;
6050 endcol = (clear_width > 0 ? clear_width : -clear_width);
6051 }
6052#endif /* FEAT_RIGHTLEFT */
6053
6054 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6055
6056 while (col < endcol)
6057 {
6058#ifdef FEAT_MBYTE
6059 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006060 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 else
6062 char_cells = 1;
6063#endif
6064
6065 redraw_this = redraw_next;
6066 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6067 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6068
6069#ifdef FEAT_GUI
6070 /* If the next character was bold, then redraw the current character to
6071 * remove any pixels that might have spilt over into us. This only
6072 * happens in the GUI.
6073 */
6074 if (redraw_next && gui.in_use)
6075 {
6076 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006077 if (hl > HL_ALL)
6078 hl = syn_attr2attr(hl);
6079 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 redraw_this = TRUE;
6081 }
6082#endif
6083
6084 if (redraw_this)
6085 {
6086 /*
6087 * Special handling when 'xs' termcap flag set (hpterm):
6088 * Attributes for characters are stored at the position where the
6089 * cursor is when writing the highlighting code. The
6090 * start-highlighting code must be written with the cursor on the
6091 * first highlighted character. The stop-highlighting code must
6092 * be written with the cursor just after the last highlighted
6093 * character.
6094 * Overwriting a character doesn't remove it's highlighting. Need
6095 * to clear the rest of the line, and force redrawing it
6096 * completely.
6097 */
6098 if ( p_wiv
6099 && !force
6100#ifdef FEAT_GUI
6101 && !gui.in_use
6102#endif
6103 && ScreenAttrs[off_to] != 0
6104 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6105 {
6106 /*
6107 * Need to remove highlighting attributes here.
6108 */
6109 windgoto(row, col + coloff);
6110 out_str(T_CE); /* clear rest of this screen line */
6111 screen_start(); /* don't know where cursor is now */
6112 force = TRUE; /* force redraw of rest of the line */
6113 redraw_next = TRUE; /* or else next char would miss out */
6114
6115 /*
6116 * If the previous character was highlighted, need to stop
6117 * highlighting at this character.
6118 */
6119 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6120 {
6121 screen_attr = ScreenAttrs[off_to - 1];
6122 term_windgoto(row, col + coloff);
6123 screen_stop_highlight();
6124 }
6125 else
6126 screen_attr = 0; /* highlighting has stopped */
6127 }
6128#ifdef FEAT_MBYTE
6129 if (enc_dbcs != 0)
6130 {
6131 /* Check if overwriting a double-byte with a single-byte or
6132 * the other way around requires another character to be
6133 * redrawn. For UTF-8 this isn't needed, because comparing
6134 * ScreenLinesUC[] is sufficient. */
6135 if (char_cells == 1
6136 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006137 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 /* Writing a single-cell character over a double-cell
6140 * character: need to redraw the next cell. */
6141 ScreenLines[off_to + 1] = 0;
6142 redraw_next = TRUE;
6143 }
6144 else if (char_cells == 2
6145 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006146 && (*mb_off2cells)(off_to, max_off_to) == 1
6147 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
6149 /* Writing the second half of a double-cell character over
6150 * a double-cell character: need to redraw the second
6151 * cell. */
6152 ScreenLines[off_to + 2] = 0;
6153 redraw_next = TRUE;
6154 }
6155
6156 if (enc_dbcs == DBCS_JPNU)
6157 ScreenLines2[off_to] = ScreenLines2[off_from];
6158 }
6159 /* When writing a single-width character over a double-width
6160 * character and at the end of the redrawn text, need to clear out
6161 * the right halve of the old character.
6162 * Also required when writing the right halve of a double-width
6163 * char over the left halve of an existing one. */
6164 if (has_mbyte && col + char_cells == endcol
6165 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006166 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006168 && (*mb_off2cells)(off_to, max_off_to) == 1
6169 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 clear_next = TRUE;
6171#endif
6172
6173 ScreenLines[off_to] = ScreenLines[off_from];
6174#ifdef FEAT_MBYTE
6175 if (enc_utf8)
6176 {
6177 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6178 if (ScreenLinesUC[off_from] != 0)
6179 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006180 int i;
6181
6182 for (i = 0; i < Screen_mco; ++i)
6183 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 }
6186 if (char_cells == 2)
6187 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6188#endif
6189
6190#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006191 /* The bold trick makes a single column of pixels appear in the
6192 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 * character should be redrawn too. This happens for our own GUI
6194 * and for some xterms. */
6195 if (
6196# ifdef FEAT_GUI
6197 gui.in_use
6198# endif
6199# if defined(FEAT_GUI) && defined(UNIX)
6200 ||
6201# endif
6202# ifdef UNIX
6203 term_is_xterm
6204# endif
6205 )
6206 {
6207 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006208 if (hl > HL_ALL)
6209 hl = syn_attr2attr(hl);
6210 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 redraw_next = TRUE;
6212 }
6213#endif
6214 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6215#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006216 /* For simplicity set the attributes of second half of a
6217 * double-wide character equal to the first half. */
6218 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006220
6221 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 else
6224#endif
6225 screen_char(off_to, row, col + coloff);
6226 }
6227 else if ( p_wiv
6228#ifdef FEAT_GUI
6229 && !gui.in_use
6230#endif
6231 && col + coloff > 0)
6232 {
6233 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6234 {
6235 /*
6236 * Don't output stop-highlight when moving the cursor, it will
6237 * stop the highlighting when it should continue.
6238 */
6239 screen_attr = 0;
6240 }
6241 else if (screen_attr != 0)
6242 screen_stop_highlight();
6243 }
6244
6245 off_to += CHAR_CELLS;
6246 off_from += CHAR_CELLS;
6247 col += CHAR_CELLS;
6248 }
6249
6250#ifdef FEAT_MBYTE
6251 if (clear_next)
6252 {
6253 /* Clear the second half of a double-wide character of which the left
6254 * half was overwritten with a single-wide character. */
6255 ScreenLines[off_to] = ' ';
6256 if (enc_utf8)
6257 ScreenLinesUC[off_to] = 0;
6258 screen_char(off_to, row, col + coloff);
6259 }
6260#endif
6261
6262 if (clear_width > 0
6263#ifdef FEAT_RIGHTLEFT
6264 && !rlflag
6265#endif
6266 )
6267 {
6268#ifdef FEAT_GUI
6269 int startCol = col;
6270#endif
6271
6272 /* blank out the rest of the line */
6273 while (col < clear_width && ScreenLines[off_to] == ' '
6274 && ScreenAttrs[off_to] == 0
6275#ifdef FEAT_MBYTE
6276 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6277#endif
6278 )
6279 {
6280 ++off_to;
6281 ++col;
6282 }
6283 if (col < clear_width)
6284 {
6285#ifdef FEAT_GUI
6286 /*
6287 * In the GUI, clearing the rest of the line may leave pixels
6288 * behind if the first character cleared was bold. Some bold
6289 * fonts spill over the left. In this case we redraw the previous
6290 * character too. If we didn't skip any blanks above, then we
6291 * only redraw if the character wasn't already redrawn anyway.
6292 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006293 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
6295 hl = ScreenAttrs[off_to];
6296 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006297 {
6298 int prev_cells = 1;
6299# ifdef FEAT_MBYTE
6300 if (enc_utf8)
6301 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6302 * that its width is 2. */
6303 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6304 else if (enc_dbcs != 0)
6305 {
6306 /* find previous character by counting from first
6307 * column and get its width. */
6308 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006309 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006310
6311 while (off < off_to)
6312 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006313 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006314 off += prev_cells;
6315 }
6316 }
6317
6318 if (enc_dbcs != 0 && prev_cells > 1)
6319 screen_char_2(off_to - prev_cells, row,
6320 col + coloff - prev_cells);
6321 else
6322# endif
6323 screen_char(off_to - prev_cells, row,
6324 col + coloff - prev_cells);
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327#endif
6328 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6329 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006330#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 off_to += clear_width - col;
6332 col = clear_width;
6333#endif
6334 }
6335 }
6336
6337 if (clear_width > 0)
6338 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006339#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 /* For a window that's left of another, draw the separator char. */
6341 if (col + coloff < Columns)
6342 {
6343 int c;
6344
6345 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006346 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006348 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6349 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350# endif
6351 || ScreenAttrs[off_to] != hl)
6352 {
6353 ScreenLines[off_to] = c;
6354 ScreenAttrs[off_to] = hl;
6355# ifdef FEAT_MBYTE
6356 if (enc_utf8)
6357 {
6358 if (c >= 0x80)
6359 {
6360 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006361 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363 else
6364 ScreenLinesUC[off_to] = 0;
6365 }
6366# endif
6367 screen_char(off_to, row, col + coloff);
6368 }
6369 }
6370 else
6371#endif
6372 LineWraps[row] = FALSE;
6373 }
6374}
6375
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006376#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006378 * Mirror text "str" for right-left displaying.
6379 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006381 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006382rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 char_u *p1, *p2;
6385 int t;
6386
6387 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6388 {
6389 t = *p1;
6390 *p1 = *p2;
6391 *p2 = t;
6392 }
6393}
6394#endif
6395
6396#if defined(FEAT_WINDOWS) || defined(PROTO)
6397/*
6398 * mark all status lines for redraw; used after first :cd
6399 */
6400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006401status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 win_T *wp;
6404
Bram Moolenaar29323592016-07-24 22:04:11 +02006405 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (wp->w_status_height)
6407 {
6408 wp->w_redr_status = TRUE;
6409 redraw_later(VALID);
6410 }
6411}
6412
6413/*
6414 * mark all status lines of the current buffer for redraw
6415 */
6416 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006417status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 win_T *wp;
6420
Bram Moolenaar29323592016-07-24 22:04:11 +02006421 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6423 {
6424 wp->w_redr_status = TRUE;
6425 redraw_later(VALID);
6426 }
6427}
6428
6429/*
6430 * Redraw all status lines that need to be redrawn.
6431 */
6432 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006433redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
6435 win_T *wp;
6436
Bram Moolenaar29323592016-07-24 22:04:11 +02006437 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (wp->w_redr_status)
6439 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006440 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006441 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442}
6443#endif
6444
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006445#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446/*
6447 * Redraw all status lines at the bottom of frame "frp".
6448 */
6449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006450win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 if (frp->fr_layout == FR_LEAF)
6453 frp->fr_win->w_redr_status = TRUE;
6454 else if (frp->fr_layout == FR_ROW)
6455 {
6456 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6457 win_redraw_last_status(frp);
6458 }
6459 else /* frp->fr_layout == FR_COL */
6460 {
6461 frp = frp->fr_child;
6462 while (frp->fr_next != NULL)
6463 frp = frp->fr_next;
6464 win_redraw_last_status(frp);
6465 }
6466}
6467#endif
6468
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006469#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470/*
6471 * Draw the verticap separator right of window "wp" starting with line "row".
6472 */
6473 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006474draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 int hl;
6477 int c;
6478
6479 if (wp->w_vsep_width)
6480 {
6481 /* draw the vertical separator right of this window */
6482 c = fillchar_vsep(&hl);
6483 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6484 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6485 c, ' ', hl);
6486 }
6487}
6488#endif
6489
6490#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006491static int status_match_len(expand_T *xp, char_u *s);
6492static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006495 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 */
6497 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006498status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 int len = 0;
6501
6502#ifdef FEAT_MENU
6503 int emenu = (xp->xp_context == EXPAND_MENUS
6504 || xp->xp_context == EXPAND_MENUNAMES);
6505
6506 /* Check for menu separators - replace with '|'. */
6507 if (emenu && menu_is_separator(s))
6508 return 1;
6509#endif
6510
6511 while (*s != NUL)
6512 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006513 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006514 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006515 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517
6518 return len;
6519}
6520
6521/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006522 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006523 * These are backslashes used for escaping. Do show backslashes in help tags.
6524 */
6525 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006526skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006527{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006528 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006529#ifdef FEAT_MENU
6530 || ((xp->xp_context == EXPAND_MENUS
6531 || xp->xp_context == EXPAND_MENUNAMES)
6532 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6533#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006534 )
6535 {
6536#ifndef BACKSLASH_IN_FILENAME
6537 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6538 return 2;
6539#endif
6540 return 1;
6541 }
6542 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006543}
6544
6545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 * Show wildchar matches in the status line.
6547 * Show at least the "match" item.
6548 * We start at item 'first_match' in the list and show all matches that fit.
6549 *
6550 * If inversion is possible we use it. Else '=' characters are used.
6551 */
6552 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006553win_redr_status_matches(
6554 expand_T *xp,
6555 int num_matches,
6556 char_u **matches, /* list of matches */
6557 int match,
6558 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6561 int row;
6562 char_u *buf;
6563 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006564 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 int fillchar;
6566 int attr;
6567 int i;
6568 int highlight = TRUE;
6569 char_u *selstart = NULL;
6570 int selstart_col = 0;
6571 char_u *selend = NULL;
6572 static int first_match = 0;
6573 int add_left = FALSE;
6574 char_u *s;
6575#ifdef FEAT_MENU
6576 int emenu;
6577#endif
6578#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6579 int l;
6580#endif
6581
6582 if (matches == NULL) /* interrupted completion? */
6583 return;
6584
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006585#ifdef FEAT_MBYTE
6586 if (has_mbyte)
6587 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6588 else
6589#endif
6590 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (buf == NULL)
6592 return;
6593
6594 if (match == -1) /* don't show match but original text */
6595 {
6596 match = 0;
6597 highlight = FALSE;
6598 }
6599 /* count 1 for the ending ">" */
6600 clen = status_match_len(xp, L_MATCH(match)) + 3;
6601 if (match == 0)
6602 first_match = 0;
6603 else if (match < first_match)
6604 {
6605 /* jumping left, as far as we can go */
6606 first_match = match;
6607 add_left = TRUE;
6608 }
6609 else
6610 {
6611 /* check if match fits on the screen */
6612 for (i = first_match; i < match; ++i)
6613 clen += status_match_len(xp, L_MATCH(i)) + 2;
6614 if (first_match > 0)
6615 clen += 2;
6616 /* jumping right, put match at the left */
6617 if ((long)clen > Columns)
6618 {
6619 first_match = match;
6620 /* if showing the last match, we can add some on the left */
6621 clen = 2;
6622 for (i = match; i < num_matches; ++i)
6623 {
6624 clen += status_match_len(xp, L_MATCH(i)) + 2;
6625 if ((long)clen >= Columns)
6626 break;
6627 }
6628 if (i == num_matches)
6629 add_left = TRUE;
6630 }
6631 }
6632 if (add_left)
6633 while (first_match > 0)
6634 {
6635 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6636 if ((long)clen >= Columns)
6637 break;
6638 --first_match;
6639 }
6640
6641 fillchar = fillchar_status(&attr, TRUE);
6642
6643 if (first_match == 0)
6644 {
6645 *buf = NUL;
6646 len = 0;
6647 }
6648 else
6649 {
6650 STRCPY(buf, "< ");
6651 len = 2;
6652 }
6653 clen = len;
6654
6655 i = first_match;
6656 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6657 {
6658 if (i == match)
6659 {
6660 selstart = buf + len;
6661 selstart_col = clen;
6662 }
6663
6664 s = L_MATCH(i);
6665 /* Check for menu separators - replace with '|' */
6666#ifdef FEAT_MENU
6667 emenu = (xp->xp_context == EXPAND_MENUS
6668 || xp->xp_context == EXPAND_MENUNAMES);
6669 if (emenu && menu_is_separator(s))
6670 {
6671 STRCPY(buf + len, transchar('|'));
6672 l = (int)STRLEN(buf + len);
6673 len += l;
6674 clen += l;
6675 }
6676 else
6677#endif
6678 for ( ; *s != NUL; ++s)
6679 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006680 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 clen += ptr2cells(s);
6682#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 STRNCPY(buf + len, s, l);
6686 s += l - 1;
6687 len += l;
6688 }
6689 else
6690#endif
6691 {
6692 STRCPY(buf + len, transchar_byte(*s));
6693 len += (int)STRLEN(buf + len);
6694 }
6695 }
6696 if (i == match)
6697 selend = buf + len;
6698
6699 *(buf + len++) = ' ';
6700 *(buf + len++) = ' ';
6701 clen += 2;
6702 if (++i == num_matches)
6703 break;
6704 }
6705
6706 if (i != num_matches)
6707 {
6708 *(buf + len++) = '>';
6709 ++clen;
6710 }
6711
6712 buf[len] = NUL;
6713
6714 row = cmdline_row - 1;
6715 if (row >= 0)
6716 {
6717 if (wild_menu_showing == 0)
6718 {
6719 if (msg_scrolled > 0)
6720 {
6721 /* Put the wildmenu just above the command line. If there is
6722 * no room, scroll the screen one line up. */
6723 if (cmdline_row == Rows - 1)
6724 {
6725 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6726 ++msg_scrolled;
6727 }
6728 else
6729 {
6730 ++cmdline_row;
6731 ++row;
6732 }
6733 wild_menu_showing = WM_SCROLLED;
6734 }
6735 else
6736 {
6737 /* Create status line if needed by setting 'laststatus' to 2.
6738 * Set 'winminheight' to zero to avoid that the window is
6739 * resized. */
6740 if (lastwin->w_status_height == 0)
6741 {
6742 save_p_ls = p_ls;
6743 save_p_wmh = p_wmh;
6744 p_ls = 2;
6745 p_wmh = 0;
6746 last_status(FALSE);
6747 }
6748 wild_menu_showing = WM_SHOWN;
6749 }
6750 }
6751
6752 screen_puts(buf, row, 0, attr);
6753 if (selstart != NULL && highlight)
6754 {
6755 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006756 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
6758
6759 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6760 }
6761
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006762#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 win_redraw_last_status(topframe);
6764#else
6765 lastwin->w_redr_status = TRUE;
6766#endif
6767 vim_free(buf);
6768}
6769#endif
6770
6771#if defined(FEAT_WINDOWS) || defined(PROTO)
6772/*
6773 * Redraw the status line of window wp.
6774 *
6775 * If inversion is possible we use it. Else '=' characters are used.
6776 */
6777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006778win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 int row;
6781 char_u *p;
6782 int len;
6783 int fillchar;
6784 int attr;
6785 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006786 static int busy = FALSE;
6787
6788 /* It's possible to get here recursively when 'statusline' (indirectly)
6789 * invokes ":redrawstatus". Simply ignore the call then. */
6790 if (busy)
6791 return;
6792 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
6794 wp->w_redr_status = FALSE;
6795 if (wp->w_status_height == 0)
6796 {
6797 /* no status line, can only be last window */
6798 redraw_cmdline = TRUE;
6799 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006800 else if (!redrawing()
6801#ifdef FEAT_INS_EXPAND
6802 /* don't update status line when popup menu is visible and may be
6803 * drawn over it */
6804 || pum_visible()
6805#endif
6806 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 /* Don't redraw right now, do it later. */
6809 wp->w_redr_status = TRUE;
6810 }
6811#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006812 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
6814 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006815 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817#endif
6818 else
6819 {
6820 fillchar = fillchar_status(&attr, wp == curwin);
6821
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006822 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 p = NameBuff;
6824 len = (int)STRLEN(p);
6825
6826 if (wp->w_buffer->b_help
6827#ifdef FEAT_QUICKFIX
6828 || wp->w_p_pvw
6829#endif
6830 || bufIsChanged(wp->w_buffer)
6831 || wp->w_buffer->b_p_ro)
6832 *(p + len++) = ' ';
6833 if (wp->w_buffer->b_help)
6834 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006835 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 len += (int)STRLEN(p + len);
6837 }
6838#ifdef FEAT_QUICKFIX
6839 if (wp->w_p_pvw)
6840 {
6841 STRCPY(p + len, _("[Preview]"));
6842 len += (int)STRLEN(p + len);
6843 }
6844#endif
6845 if (bufIsChanged(wp->w_buffer))
6846 {
6847 STRCPY(p + len, "[+]");
6848 len += 3;
6849 }
6850 if (wp->w_buffer->b_p_ro)
6851 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006852 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006853 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6857 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6858 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6859 if (this_ru_col <= 1)
6860 {
6861 p = (char_u *)"<"; /* No room for file name! */
6862 len = 1;
6863 }
6864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#ifdef FEAT_MBYTE
6866 if (has_mbyte)
6867 {
6868 int clen = 0, i;
6869
6870 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006871 clen = mb_string2cells(p, -1);
6872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 /* Find first character that will fit.
6874 * Going from start to end is much faster for DBCS. */
6875 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006876 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 clen -= (*mb_ptr2cells)(p + i);
6878 len = clen;
6879 if (i > 0)
6880 {
6881 p = p + i - 1;
6882 *p = '<';
6883 ++len;
6884 }
6885
6886 }
6887 else
6888#endif
6889 if (len > this_ru_col - 1)
6890 {
6891 p += len - (this_ru_col - 1);
6892 *p = '<';
6893 len = this_ru_col - 1;
6894 }
6895
6896 row = W_WINROW(wp) + wp->w_height;
6897 screen_puts(p, row, W_WINCOL(wp), attr);
6898 screen_fill(row, row + 1, len + W_WINCOL(wp),
6899 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6900
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006901 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6903 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6904 - 1 + W_WINCOL(wp)), attr);
6905
6906#ifdef FEAT_CMDL_INFO
6907 win_redr_ruler(wp, TRUE);
6908#endif
6909 }
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 /*
6912 * May need to draw the character below the vertical separator.
6913 */
6914 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6915 {
6916 if (stl_connected(wp))
6917 fillchar = fillchar_status(&attr, wp == curwin);
6918 else
6919 fillchar = fillchar_vsep(&attr);
6920 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6921 attr);
6922 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006923 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
Bram Moolenaar238a5642006-02-21 22:12:05 +00006926#ifdef FEAT_STL_OPT
6927/*
6928 * Redraw the status line according to 'statusline' and take care of any
6929 * errors encountered.
6930 */
6931 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006932redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006933{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006934 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006935 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006936
6937 /* When called recursively return. This can happen when the statusline
6938 * contains an expression that triggers a redraw. */
6939 if (entered)
6940 return;
6941 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006942
Bram Moolenaara742e082016-04-05 21:10:38 +02006943 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006944 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006945 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006946 {
6947 /* When there is an error disable the statusline, otherwise the
6948 * display is messed up with errors and a redraw triggers the problem
6949 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006950 set_string_option_direct((char_u *)"statusline", -1,
6951 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006952 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006953 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006954 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006955 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006956}
6957#endif
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959/*
6960 * Return TRUE if the status line of window "wp" is connected to the status
6961 * line of the window right of it. If not, then it's a vertical separator.
6962 * Only call if (wp->w_vsep_width != 0).
6963 */
6964 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006965stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 frame_T *fr;
6968
6969 fr = wp->w_frame;
6970 while (fr->fr_parent != NULL)
6971 {
6972 if (fr->fr_parent->fr_layout == FR_COL)
6973 {
6974 if (fr->fr_next != NULL)
6975 break;
6976 }
6977 else
6978 {
6979 if (fr->fr_next != NULL)
6980 return TRUE;
6981 }
6982 fr = fr->fr_parent;
6983 }
6984 return FALSE;
6985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
6987#endif /* FEAT_WINDOWS */
6988
6989#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6990/*
6991 * Get the value to show for the language mappings, active 'keymap'.
6992 */
6993 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006994get_keymap_str(
6995 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006996 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006997 char_u *buf, /* buffer for the result */
6998 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
7000 char_u *p;
7001
7002 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7003 return FALSE;
7004
7005 {
7006#ifdef FEAT_EVAL
7007 buf_T *old_curbuf = curbuf;
7008 win_T *old_curwin = curwin;
7009 char_u *s;
7010
7011 curbuf = wp->w_buffer;
7012 curwin = wp;
7013 STRCPY(buf, "b:keymap_name"); /* must be writable */
7014 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007015 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 --emsg_skip;
7017 curbuf = old_curbuf;
7018 curwin = old_curwin;
7019 if (p == NULL || *p == NUL)
7020#endif
7021 {
7022#ifdef FEAT_KEYMAP
7023 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7024 p = wp->w_buffer->b_p_keymap;
7025 else
7026#endif
7027 p = (char_u *)"lang";
7028 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007029 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 buf[0] = NUL;
7031#ifdef FEAT_EVAL
7032 vim_free(s);
7033#endif
7034 }
7035 return buf[0] != NUL;
7036}
7037#endif
7038
7039#if defined(FEAT_STL_OPT) || defined(PROTO)
7040/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007041 * Redraw the status line or ruler of window "wp".
7042 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 */
7044 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007045win_redr_custom(
7046 win_T *wp,
7047 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007049 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 int attr;
7051 int curattr;
7052 int row;
7053 int col = 0;
7054 int maxwidth;
7055 int width;
7056 int n;
7057 int len;
7058 int fillchar;
7059 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007060 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007062 struct stl_hlrec hltab[STL_MAX_ITEM];
7063 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007064 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007065 win_T *ewp;
7066 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar1d633412013-12-11 15:52:01 +01007068 /* There is a tiny chance that this gets called recursively: When
7069 * redrawing a status line triggers redrawing the ruler or tabline.
7070 * Avoid trouble by not allowing recursion. */
7071 if (entered)
7072 return;
7073 entered = TRUE;
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007076 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007078 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007079 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007080 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007081 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007082 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007083 maxwidth = Columns;
7084# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007085 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007088 else
7089 {
7090 row = W_WINROW(wp) + wp->w_height;
7091 fillchar = fillchar_status(&attr, wp == curwin);
7092 maxwidth = W_WIDTH(wp);
7093
7094 if (draw_ruler)
7095 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007096 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007097 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007098 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007099 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007100 if (*++stl == '-')
7101 stl++;
7102 if (atoi((char *)stl))
7103 while (VIM_ISDIGIT(*stl))
7104 stl++;
7105 if (*stl++ != '(')
7106 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007107 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007108#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007109 col = ru_col - (Columns - W_WIDTH(wp));
7110 if (col < (W_WIDTH(wp) + 1) / 2)
7111 col = (W_WIDTH(wp) + 1) / 2;
7112#else
7113 col = ru_col;
7114 if (col > (Columns + 1) / 2)
7115 col = (Columns + 1) / 2;
7116#endif
7117 maxwidth = W_WIDTH(wp) - col;
7118#ifdef FEAT_WINDOWS
7119 if (!wp->w_status_height)
7120#endif
7121 {
7122 row = Rows - 1;
7123 --maxwidth; /* writing in last column may cause scrolling */
7124 fillchar = ' ';
7125 attr = 0;
7126 }
7127
7128# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007129 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007130# endif
7131 }
7132 else
7133 {
7134 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007139 use_sandbox = was_set_insecurely((char_u *)"statusline",
7140 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141# endif
7142 }
7143
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007144#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 col += W_WINCOL(wp);
7146#endif
7147 }
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007150 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
Bram Moolenaar61452852011-02-01 18:01:11 +01007152 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7153 * the cursor away and back. */
7154 ewp = wp == NULL ? curwin : wp;
7155 p_crb_save = ewp->w_p_crb;
7156 ewp->w_p_crb = FALSE;
7157
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 /* Make a copy, because the statusline may include a function call that
7159 * might change the option value and free the memory. */
7160 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007161 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007163 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007165 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007167 /* Make all characters printable. */
7168 p = transstr(buf);
7169 if (p != NULL)
7170 {
7171 vim_strncpy(buf, p, sizeof(buf) - 1);
7172 vim_free(p);
7173 }
7174
7175 /* fill up with "fillchar" */
7176 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007177 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
7179#ifdef FEAT_MBYTE
7180 len += (*mb_char2bytes)(fillchar, buf + len);
7181#else
7182 buf[len++] = fillchar;
7183#endif
7184 ++width;
7185 }
7186 buf[len] = NUL;
7187
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007188 /*
7189 * Draw each snippet with the specified highlighting.
7190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 curattr = attr;
7192 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007193 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007195 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 screen_puts_len(p, len, row, col, curattr);
7197 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007202 else if (hltab[n].userhl < 0)
7203 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007205 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207#endif
7208 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007209 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007212
7213 if (wp == NULL)
7214 {
7215 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7216 col = 0;
7217 len = 0;
7218 p = buf;
7219 fillchar = 0;
7220 for (n = 0; tabtab[n].start != NULL; n++)
7221 {
7222 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7223 while (col < len)
7224 TabPageIdxs[col++] = fillchar;
7225 p = tabtab[n].start;
7226 fillchar = tabtab[n].userhl;
7227 }
7228 while (col < Columns)
7229 TabPageIdxs[col++] = fillchar;
7230 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007231
7232theend:
7233 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234}
7235
7236#endif /* FEAT_STL_OPT */
7237
7238/*
7239 * Output a single character directly to the screen and update ScreenLines.
7240 */
7241 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007242screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 char_u buf[MB_MAXBYTES + 1];
7245
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007246#ifdef FEAT_MBYTE
7247 if (has_mbyte)
7248 buf[(*mb_char2bytes)(c, buf)] = NUL;
7249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007251 {
7252 buf[0] = c;
7253 buf[1] = NUL;
7254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 screen_puts(buf, row, col, attr);
7256}
7257
7258/*
7259 * Get a single character directly from ScreenLines into "bytes[]".
7260 * Also return its attribute in *attrp;
7261 */
7262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007263screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 unsigned off;
7266
7267 /* safety check */
7268 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7269 {
7270 off = LineOffset[row] + col;
7271 *attrp = ScreenAttrs[off];
7272 bytes[0] = ScreenLines[off];
7273 bytes[1] = NUL;
7274
7275#ifdef FEAT_MBYTE
7276 if (enc_utf8 && ScreenLinesUC[off] != 0)
7277 bytes[utfc_char2bytes(off, bytes)] = NUL;
7278 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7279 {
7280 bytes[0] = ScreenLines[off];
7281 bytes[1] = ScreenLines2[off];
7282 bytes[2] = NUL;
7283 }
7284 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7285 {
7286 bytes[1] = ScreenLines[off + 1];
7287 bytes[2] = NUL;
7288 }
7289#endif
7290 }
7291}
7292
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007293#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007294static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007295
7296/*
7297 * Return TRUE if composing characters for screen posn "off" differs from
7298 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007299 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007300 */
7301 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007302screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007303{
7304 int i;
7305
7306 for (i = 0; i < Screen_mco; ++i)
7307 {
7308 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7309 return TRUE;
7310 if (u8cc[i] == 0)
7311 break;
7312 }
7313 return FALSE;
7314}
7315#endif
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317/*
7318 * Put string '*text' on the screen at position 'row' and 'col', with
7319 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7320 * Note: only outputs within one row, message is truncated at screen boundary!
7321 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7322 */
7323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007324screen_puts(
7325 char_u *text,
7326 int row,
7327 int col,
7328 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 screen_puts_len(text, -1, row, col, attr);
7331}
7332
7333/*
7334 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7335 * a NUL.
7336 */
7337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007338screen_puts_len(
7339 char_u *text,
7340 int textlen,
7341 int row,
7342 int col,
7343 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 unsigned off;
7346 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007347 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 int c;
7349#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007350 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 int mbyte_blen = 1;
7352 int mbyte_cells = 1;
7353 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007354 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 int clear_next_cell = FALSE;
7356# ifdef FEAT_ARABIC
7357 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007358 int pc, nc, nc1;
7359 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360# endif
7361#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007362#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7363 int force_redraw_this;
7364 int force_redraw_next = FALSE;
7365#endif
7366 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
7368 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7369 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007370 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371
Bram Moolenaarc236c162008-07-13 17:41:49 +00007372#ifdef FEAT_MBYTE
7373 /* When drawing over the right halve of a double-wide char clear out the
7374 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007375 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007376# ifdef FEAT_GUI
7377 && !gui.in_use
7378# endif
7379 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007380 {
7381 ScreenLines[off - 1] = ' ';
7382 ScreenAttrs[off - 1] = 0;
7383 if (enc_utf8)
7384 {
7385 ScreenLinesUC[off - 1] = 0;
7386 ScreenLinesC[0][off - 1] = 0;
7387 }
7388 /* redraw the previous cell, make it empty */
7389 screen_char(off - 1, row, col - 1);
7390 /* force the cell at "col" to be redrawn */
7391 force_redraw_next = TRUE;
7392 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007393#endif
7394
Bram Moolenaar367329b2007-08-30 11:53:22 +00007395#ifdef FEAT_MBYTE
7396 max_off = LineOffset[row] + screen_Columns;
7397#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007398 while (col < screen_Columns
7399 && (len < 0 || (int)(ptr - text) < len)
7400 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 {
7402 c = *ptr;
7403#ifdef FEAT_MBYTE
7404 /* check if this is the first byte of a multibyte */
7405 if (has_mbyte)
7406 {
7407 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007408 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007410 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7412 mbyte_cells = 1;
7413 else if (enc_dbcs != 0)
7414 mbyte_cells = mbyte_blen;
7415 else /* enc_utf8 */
7416 {
7417 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 (int)((text + len) - ptr));
7420 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007421 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007423# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 /* Non-BMP character: display as ? or fullwidth ?. */
7425 if (u8c >= 0x10000)
7426 {
7427 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7428 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007429 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432# ifdef FEAT_ARABIC
7433 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7434 {
7435 /* Do Arabic shaping. */
7436 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7437 {
7438 /* Past end of string to be displayed. */
7439 nc = NUL;
7440 nc1 = NUL;
7441 }
7442 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007443 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007444 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7445 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007446 nc1 = pcc[0];
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 pc = prev_c;
7449 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007450 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452 else
7453 prev_c = u8c;
7454# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007455 if (col + mbyte_cells > screen_Columns)
7456 {
7457 /* Only 1 cell left, but character requires 2 cells:
7458 * display a '>' in the last column to avoid wrapping. */
7459 c = '>';
7460 mbyte_cells = 1;
7461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 }
7464#endif
7465
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007466#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7467 force_redraw_this = force_redraw_next;
7468 force_redraw_next = FALSE;
7469#endif
7470
7471 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472#ifdef FEAT_MBYTE
7473 || (mbyte_cells == 2
7474 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7475 || (enc_dbcs == DBCS_JPNU
7476 && c == 0x8e
7477 && ScreenLines2[off] != ptr[1])
7478 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007479 && (ScreenLinesUC[off] !=
7480 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7481 || (ScreenLinesUC[off] != 0
7482 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#endif
7484 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007485 || exmode_active;
7486
7487 if (need_redraw
7488#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7489 || force_redraw_this
7490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 )
7492 {
7493#if defined(FEAT_GUI) || defined(UNIX)
7494 /* The bold trick makes a single row of pixels appear in the next
7495 * character. When a bold character is removed, the next
7496 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007497 * and for some xterms. */
7498 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499# ifdef FEAT_GUI
7500 gui.in_use
7501# endif
7502# if defined(FEAT_GUI) && defined(UNIX)
7503 ||
7504# endif
7505# ifdef UNIX
7506 term_is_xterm
7507# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007508 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007510 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007512 if (n > HL_ALL)
7513 n = syn_attr2attr(n);
7514 if (n & HL_BOLD)
7515 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517#endif
7518#ifdef FEAT_MBYTE
7519 /* When at the end of the text and overwriting a two-cell
7520 * character with a one-cell character, need to clear the next
7521 * cell. Also when overwriting the left halve of a two-cell char
7522 * with the right halve of a two-cell char. Do this only once
7523 * (mb_off2cells() may return 2 on the right halve). */
7524 if (clear_next_cell)
7525 clear_next_cell = FALSE;
7526 else if (has_mbyte
7527 && (len < 0 ? ptr[mbyte_blen] == NUL
7528 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007529 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007531 && (*mb_off2cells)(off, max_off) == 1
7532 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 clear_next_cell = TRUE;
7534
7535 /* Make sure we never leave a second byte of a double-byte behind,
7536 * it confuses mb_off2cells(). */
7537 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007538 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007540 && (*mb_off2cells)(off, max_off) == 1
7541 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 ScreenLines[off + mbyte_blen] = 0;
7543#endif
7544 ScreenLines[off] = c;
7545 ScreenAttrs[off] = attr;
7546#ifdef FEAT_MBYTE
7547 if (enc_utf8)
7548 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007549 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 ScreenLinesUC[off] = 0;
7551 else
7552 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007553 int i;
7554
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007556 for (i = 0; i < Screen_mco; ++i)
7557 {
7558 ScreenLinesC[i][off] = u8cc[i];
7559 if (u8cc[i] == 0)
7560 break;
7561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 }
7563 if (mbyte_cells == 2)
7564 {
7565 ScreenLines[off + 1] = 0;
7566 ScreenAttrs[off + 1] = attr;
7567 }
7568 screen_char(off, row, col);
7569 }
7570 else if (mbyte_cells == 2)
7571 {
7572 ScreenLines[off + 1] = ptr[1];
7573 ScreenAttrs[off + 1] = attr;
7574 screen_char_2(off, row, col);
7575 }
7576 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7577 {
7578 ScreenLines2[off] = ptr[1];
7579 screen_char(off, row, col);
7580 }
7581 else
7582#endif
7583 screen_char(off, row, col);
7584 }
7585#ifdef FEAT_MBYTE
7586 if (has_mbyte)
7587 {
7588 off += mbyte_cells;
7589 col += mbyte_cells;
7590 ptr += mbyte_blen;
7591 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007592 {
7593 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007595 len = -1;
7596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598 else
7599#endif
7600 {
7601 ++off;
7602 ++col;
7603 ++ptr;
7604 }
7605 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007606
7607#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7608 /* If we detected the next character needs to be redrawn, but the text
7609 * doesn't extend up to there, update the character here. */
7610 if (force_redraw_next && col < screen_Columns)
7611 {
7612# ifdef FEAT_MBYTE
7613 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7614 screen_char_2(off, row, col);
7615 else
7616# endif
7617 screen_char(off, row, col);
7618 }
7619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620}
7621
7622#ifdef FEAT_SEARCH_EXTRA
7623/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007624 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 */
7626 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007627start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
7629 if (p_hls && !no_hlsearch)
7630 {
7631 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007632 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007633# ifdef FEAT_RELTIME
7634 /* Set the time limit to 'redrawtime'. */
7635 profile_setlimit(p_rdt, &search_hl.tm);
7636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638}
7639
7640/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007641 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 */
7643 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007644end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 if (search_hl.rm.regprog != NULL)
7647 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007648 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 search_hl.rm.regprog = NULL;
7650 }
7651}
7652
7653/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007654 * Init for calling prepare_search_hl().
7655 */
7656 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007657init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007658{
7659 matchitem_T *cur;
7660
7661 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7662 * match */
7663 cur = wp->w_match_head;
7664 while (cur != NULL)
7665 {
7666 cur->hl.rm = cur->match;
7667 if (cur->hlg_id == 0)
7668 cur->hl.attr = 0;
7669 else
7670 cur->hl.attr = syn_id2attr(cur->hlg_id);
7671 cur->hl.buf = wp->w_buffer;
7672 cur->hl.lnum = 0;
7673 cur->hl.first_lnum = 0;
7674# ifdef FEAT_RELTIME
7675 /* Set the time limit to 'redrawtime'. */
7676 profile_setlimit(p_rdt, &(cur->hl.tm));
7677# endif
7678 cur = cur->next;
7679 }
7680 search_hl.buf = wp->w_buffer;
7681 search_hl.lnum = 0;
7682 search_hl.first_lnum = 0;
7683 /* time limit is set at the toplevel, for all windows */
7684}
7685
7686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 * Advance to the match in window "wp" line "lnum" or past it.
7688 */
7689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007690prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007692 matchitem_T *cur; /* points to the match list */
7693 match_T *shl; /* points to search_hl or a match */
7694 int shl_flag; /* flag to indicate whether search_hl
7695 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007696 int pos_inprogress; /* marks that position match search is
7697 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 int n;
7699
7700 /*
7701 * When using a multi-line pattern, start searching at the top
7702 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007703 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007705 cur = wp->w_match_head;
7706 shl_flag = FALSE;
7707 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007709 if (shl_flag == FALSE)
7710 {
7711 shl = &search_hl;
7712 shl_flag = TRUE;
7713 }
7714 else
7715 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (shl->rm.regprog != NULL
7717 && shl->lnum == 0
7718 && re_multiline(shl->rm.regprog))
7719 {
7720 if (shl->first_lnum == 0)
7721 {
7722# ifdef FEAT_FOLDING
7723 for (shl->first_lnum = lnum;
7724 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7725 if (hasFoldingWin(wp, shl->first_lnum - 1,
7726 NULL, NULL, TRUE, NULL))
7727 break;
7728# else
7729 shl->first_lnum = wp->w_topline;
7730# endif
7731 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007732 if (cur != NULL)
7733 cur->pos.cur = 0;
7734 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7737 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007739 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7740 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007741 pos_inprogress = cur == NULL || cur->pos.cur == 0
7742 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 if (shl->lnum != 0)
7744 {
7745 shl->first_lnum = shl->lnum
7746 + shl->rm.endpos[0].lnum
7747 - shl->rm.startpos[0].lnum;
7748 n = shl->rm.endpos[0].col;
7749 }
7750 else
7751 {
7752 ++shl->first_lnum;
7753 n = 0;
7754 }
7755 }
7756 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 if (shl != &search_hl && cur != NULL)
7758 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760}
7761
7762/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007763 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 * Uses shl->buf.
7765 * Sets shl->lnum and shl->rm contents.
7766 * Note: Assumes a previous match is always before "lnum", unless
7767 * shl->lnum is zero.
7768 * Careful: Any pointers for buffer lines will become invalid.
7769 */
7770 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007771next_search_hl(
7772 win_T *win,
7773 match_T *shl, /* points to search_hl or a match */
7774 linenr_T lnum,
7775 colnr_T mincol, /* minimal column for a match */
7776 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 linenr_T l;
7779 colnr_T matchcol;
7780 long nmatched;
7781
7782 if (shl->lnum != 0)
7783 {
7784 /* Check for three situations:
7785 * 1. If the "lnum" is below a previous match, start a new search.
7786 * 2. If the previous match includes "mincol", use it.
7787 * 3. Continue after the previous match.
7788 */
7789 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7790 if (lnum > l)
7791 shl->lnum = 0;
7792 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7793 return;
7794 }
7795
7796 /*
7797 * Repeat searching for a match until one is found that includes "mincol"
7798 * or none is found in this line.
7799 */
7800 called_emsg = FALSE;
7801 for (;;)
7802 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007803#ifdef FEAT_RELTIME
7804 /* Stop searching after passing the time limit. */
7805 if (profile_passed_limit(&(shl->tm)))
7806 {
7807 shl->lnum = 0; /* no match found in time */
7808 break;
7809 }
7810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 /* Three situations:
7812 * 1. No useful previous match: search from start of line.
7813 * 2. Not Vi compatible or empty match: continue at next character.
7814 * Break the loop if this is beyond the end of the line.
7815 * 3. Vi compatible searching: continue at end of previous match.
7816 */
7817 if (shl->lnum == 0)
7818 matchcol = 0;
7819 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7820 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007821 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007823 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007824
7825 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007826 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007827 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007829 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 shl->lnum = 0;
7831 break;
7832 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007833#ifdef FEAT_MBYTE
7834 if (has_mbyte)
7835 matchcol += mb_ptr2len(ml);
7836 else
7837#endif
7838 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840 else
7841 matchcol = shl->rm.endpos[0].col;
7842
7843 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007844 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007846 /* Remember whether shl->rm is using a copy of the regprog in
7847 * cur->match. */
7848 int regprog_is_copy = (shl != &search_hl && cur != NULL
7849 && shl == &cur->hl
7850 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007851 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007852
Bram Moolenaarb3414592014-06-17 17:48:32 +02007853 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7854 matchcol,
7855#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007856 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007858 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007859#endif
7860 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007861 /* Copy the regprog, in case it got freed and recompiled. */
7862 if (regprog_is_copy)
7863 cur->match.regprog = cur->hl.rm.regprog;
7864
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007865 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007866 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007867 /* Error while handling regexp: stop using this regexp. */
7868 if (shl == &search_hl)
7869 {
7870 /* don't free regprog in the match list, it's a copy */
7871 vim_regfree(shl->rm.regprog);
7872 SET_NO_HLSEARCH(TRUE);
7873 }
7874 shl->rm.regprog = NULL;
7875 shl->lnum = 0;
7876 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7877 message */
7878 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007879 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007880 }
7881 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007883 else
7884 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (nmatched == 0)
7886 {
7887 shl->lnum = 0; /* no match found */
7888 break;
7889 }
7890 if (shl->rm.startpos[0].lnum > 0
7891 || shl->rm.startpos[0].col >= mincol
7892 || nmatched > 1
7893 || shl->rm.endpos[0].col > mincol)
7894 {
7895 shl->lnum += shl->rm.startpos[0].lnum;
7896 break; /* useful match found */
7897 }
7898 }
7899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
Bram Moolenaar85077472016-10-16 14:35:48 +02007901/*
7902 * If there is a match fill "shl" and return one.
7903 * Return zero otherwise.
7904 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007906next_search_hl_pos(
7907 match_T *shl, /* points to a match */
7908 linenr_T lnum,
7909 posmatch_T *posmatch, /* match positions */
7910 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007911{
7912 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007913 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007914
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7916 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007917 llpos_T *pos = &posmatch->pos[i];
7918
7919 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007921 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007923 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007925 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007927 /* if this match comes before the one at "found" then swap
7928 * them */
7929 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007931 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932
Bram Moolenaar85077472016-10-16 14:35:48 +02007933 *pos = posmatch->pos[found];
7934 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007935 }
7936 }
7937 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007938 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939 }
7940 }
7941 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007942 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007943 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007944 colnr_T start = posmatch->pos[found].col == 0
7945 ? 0 : posmatch->pos[found].col - 1;
7946 colnr_T end = posmatch->pos[found].col == 0
7947 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948
Bram Moolenaar85077472016-10-16 14:35:48 +02007949 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 shl->rm.startpos[0].lnum = 0;
7951 shl->rm.startpos[0].col = start;
7952 shl->rm.endpos[0].lnum = 0;
7953 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007954 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007955 posmatch->cur = found + 1;
7956 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007958 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007960#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007963screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 attrentry_T *aep = NULL;
7966
7967 screen_attr = attr;
7968 if (full_screen
7969#ifdef WIN3264
7970 && termcap_active
7971#endif
7972 )
7973 {
7974#ifdef FEAT_GUI
7975 if (gui.in_use)
7976 {
7977 char buf[20];
7978
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007979 /* The GUI handles this internally. */
7980 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 OUT_STR(buf);
7982 }
7983 else
7984#endif
7985 {
7986 if (attr > HL_ALL) /* special HL attr. */
7987 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007988 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 aep = syn_cterm_attr2entry(attr);
7990 else
7991 aep = syn_term_attr2entry(attr);
7992 if (aep == NULL) /* did ":syntax clear" */
7993 attr = 0;
7994 else
7995 attr = aep->ae_attr;
7996 }
7997 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7998 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007999 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008000#ifdef FEAT_TERMGUICOLORS
8001 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008002 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008003#endif
8004 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008005#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008006 )
8007#endif
8008 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008009 /* If the Normal FG color has BOLD attribute and the new HL
8010 * has a FG color defined, clear BOLD. */
8011 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8013 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008014 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8015 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 out_str(T_US);
8017 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8018 out_str(T_CZH);
8019 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8020 out_str(T_MR);
8021
8022 /*
8023 * Output the color or start string after bold etc., in case the
8024 * bold etc. override the color setting.
8025 */
8026 if (aep != NULL)
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 Moolenaar1b58cdd2016-08-22 23:04:33 +02008031 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008033 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008034 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039 if (t_colors > 1)
8040 {
8041 if (aep->ae_u.cterm.fg_color)
8042 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8043 if (aep->ae_u.cterm.bg_color)
8044 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8045 }
8046 else
8047 {
8048 if (aep->ae_u.term.start != NULL)
8049 out_str(aep->ae_u.term.start);
8050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052 }
8053 }
8054 }
8055}
8056
8057 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 int do_ME = FALSE; /* output T_ME code */
8061
8062 if (screen_attr != 0
8063#ifdef WIN3264
8064 && termcap_active
8065#endif
8066 )
8067 {
8068#ifdef FEAT_GUI
8069 if (gui.in_use)
8070 {
8071 char buf[20];
8072
8073 /* use internal GUI code */
8074 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8075 OUT_STR(buf);
8076 }
8077 else
8078#endif
8079 {
8080 if (screen_attr > HL_ALL) /* special HL attr. */
8081 {
8082 attrentry_T *aep;
8083
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008084 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
8086 /*
8087 * Assume that t_me restores the original colors!
8088 */
8089 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008090 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008091#ifdef FEAT_TERMGUICOLORS
8092 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008093 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8094 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008095#endif
8096 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008097#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008098 )
8099#endif
8100 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 do_ME = TRUE;
8102 }
8103 else
8104 {
8105 aep = syn_term_attr2entry(screen_attr);
8106 if (aep != NULL && aep->ae_u.term.stop != NULL)
8107 {
8108 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8109 do_ME = TRUE;
8110 else
8111 out_str(aep->ae_u.term.stop);
8112 }
8113 }
8114 if (aep == NULL) /* did ":syntax clear" */
8115 screen_attr = 0;
8116 else
8117 screen_attr = aep->ae_attr;
8118 }
8119
8120 /*
8121 * Often all ending-codes are equal to T_ME. Avoid outputting the
8122 * same sequence several times.
8123 */
8124 if (screen_attr & HL_STANDOUT)
8125 {
8126 if (STRCMP(T_SE, T_ME) == 0)
8127 do_ME = TRUE;
8128 else
8129 out_str(T_SE);
8130 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008131 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {
8133 if (STRCMP(T_UE, T_ME) == 0)
8134 do_ME = TRUE;
8135 else
8136 out_str(T_UE);
8137 }
8138 if (screen_attr & HL_ITALIC)
8139 {
8140 if (STRCMP(T_CZR, T_ME) == 0)
8141 do_ME = TRUE;
8142 else
8143 out_str(T_CZR);
8144 }
8145 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8146 out_str(T_ME);
8147
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008148#ifdef FEAT_TERMGUICOLORS
8149 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008151 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008152 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008153 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008154 term_bg_rgb_color(cterm_normal_bg_gui_color);
8155 }
8156 else
8157#endif
8158 {
8159 if (t_colors > 1)
8160 {
8161 /* set Normal cterm colors */
8162 if (cterm_normal_fg_color != 0)
8163 term_fg_color(cterm_normal_fg_color - 1);
8164 if (cterm_normal_bg_color != 0)
8165 term_bg_color(cterm_normal_bg_color - 1);
8166 if (cterm_normal_fg_bold)
8167 out_str(T_MD);
8168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170 }
8171 }
8172 screen_attr = 0;
8173}
8174
8175/*
8176 * Reset the colors for a cterm. Used when leaving Vim.
8177 * The machine specific code may override this again.
8178 */
8179 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008180reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008182 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008185#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008186 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8187 || cterm_normal_bg_gui_color != INVALCOLOR)
8188 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008189#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 out_str(T_OP);
8194 screen_attr = -1;
8195 }
8196 if (cterm_normal_fg_bold)
8197 {
8198 out_str(T_ME);
8199 screen_attr = -1;
8200 }
8201 }
8202}
8203
8204/*
8205 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8206 * using the attributes from ScreenAttrs["off"].
8207 */
8208 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008209screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211 int attr;
8212
8213 /* Check for illegal values, just in case (could happen just after
8214 * resizing). */
8215 if (row >= screen_Rows || col >= screen_Columns)
8216 return;
8217
Bram Moolenaar494838a2015-02-10 19:20:37 +01008218 /* Outputting a character in the last cell on the screen may scroll the
8219 * screen up. Only do it when the "xn" termcap property is set, otherwise
8220 * mark the character invalid (update it when scrolled up). */
8221 if (*T_XN == NUL
8222 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223#ifdef FEAT_RIGHTLEFT
8224 /* account for first command-line character in rightleft mode */
8225 && !cmdmsg_rl
8226#endif
8227 )
8228 {
8229 ScreenAttrs[off] = (sattr_T)-1;
8230 return;
8231 }
8232
8233 /*
8234 * Stop highlighting first, so it's easier to move the cursor.
8235 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008236#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (screen_char_attr != 0)
8238 attr = screen_char_attr;
8239 else
8240#endif
8241 attr = ScreenAttrs[off];
8242 if (screen_attr != attr)
8243 screen_stop_highlight();
8244
8245 windgoto(row, col);
8246
8247 if (screen_attr != attr)
8248 screen_start_highlight(attr);
8249
8250#ifdef FEAT_MBYTE
8251 if (enc_utf8 && ScreenLinesUC[off] != 0)
8252 {
8253 char_u buf[MB_MAXBYTES + 1];
8254
8255 /* Convert UTF-8 character to bytes and write it. */
8256
8257 buf[utfc_char2bytes(off, buf)] = NUL;
8258
8259 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008260 if (utf_ambiguous_width(ScreenLinesUC[off]))
8261 screen_cur_col = 9999;
8262 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 ++screen_cur_col;
8264 }
8265 else
8266#endif
8267 {
8268#ifdef FEAT_MBYTE
8269 out_flush_check();
8270#endif
8271 out_char(ScreenLines[off]);
8272#ifdef FEAT_MBYTE
8273 /* double-byte character in single-width cell */
8274 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8275 out_char(ScreenLines2[off]);
8276#endif
8277 }
8278
8279 screen_cur_col++;
8280}
8281
8282#ifdef FEAT_MBYTE
8283
8284/*
8285 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8286 * on the screen at position 'row' and 'col'.
8287 * The attributes of the first byte is used for all. This is required to
8288 * output the two bytes of a double-byte character with nothing in between.
8289 */
8290 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008291screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
8293 /* Check for illegal values (could be wrong when screen was resized). */
8294 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8295 return;
8296
8297 /* Outputting the last character on the screen may scrollup the screen.
8298 * Don't to it! Mark the character invalid (update it when scrolled up) */
8299 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8300 {
8301 ScreenAttrs[off] = (sattr_T)-1;
8302 return;
8303 }
8304
8305 /* Output the first byte normally (positions the cursor), then write the
8306 * second byte directly. */
8307 screen_char(off, row, col);
8308 out_char(ScreenLines[off + 1]);
8309 ++screen_cur_col;
8310}
8311#endif
8312
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008313#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314/*
8315 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8316 * This uses the contents of ScreenLines[] and doesn't change it.
8317 */
8318 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008319screen_draw_rectangle(
8320 int row,
8321 int col,
8322 int height,
8323 int width,
8324 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 int r, c;
8327 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008328#ifdef FEAT_MBYTE
8329 int max_off;
8330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008332 /* Can't use ScreenLines unless initialized */
8333 if (ScreenLines == NULL)
8334 return;
8335
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (invert)
8337 screen_char_attr = HL_INVERSE;
8338 for (r = row; r < row + height; ++r)
8339 {
8340 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008341#ifdef FEAT_MBYTE
8342 max_off = off + screen_Columns;
8343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 for (c = col; c < col + width; ++c)
8345 {
8346#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008347 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 screen_char_2(off + c, r, c);
8350 ++c;
8351 }
8352 else
8353#endif
8354 {
8355 screen_char(off + c, r, c);
8356#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008357 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 ++c;
8359#endif
8360 }
8361 }
8362 }
8363 screen_char_attr = 0;
8364}
8365#endif
8366
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008367#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368/*
8369 * Redraw the characters for a vertically split window.
8370 */
8371 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 int col;
8375 int width;
8376
8377# ifdef FEAT_CLIPBOARD
8378 clip_may_clear_selection(row, end - 1);
8379# endif
8380
8381 if (wp == NULL)
8382 {
8383 col = 0;
8384 width = Columns;
8385 }
8386 else
8387 {
8388 col = wp->w_wincol;
8389 width = wp->w_width;
8390 }
8391 screen_draw_rectangle(row, col, end - row, width, FALSE);
8392}
8393#endif
8394
8395/*
8396 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8397 * with character 'c1' in first column followed by 'c2' in the other columns.
8398 * Use attributes 'attr'.
8399 */
8400 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008401screen_fill(
8402 int start_row,
8403 int end_row,
8404 int start_col,
8405 int end_col,
8406 int c1,
8407 int c2,
8408 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409{
8410 int row;
8411 int col;
8412 int off;
8413 int end_off;
8414 int did_delete;
8415 int c;
8416 int norm_term;
8417#if defined(FEAT_GUI) || defined(UNIX)
8418 int force_next = FALSE;
8419#endif
8420
8421 if (end_row > screen_Rows) /* safety check */
8422 end_row = screen_Rows;
8423 if (end_col > screen_Columns) /* safety check */
8424 end_col = screen_Columns;
8425 if (ScreenLines == NULL
8426 || start_row >= end_row
8427 || start_col >= end_col) /* nothing to do */
8428 return;
8429
8430 /* it's a "normal" terminal when not in a GUI or cterm */
8431 norm_term = (
8432#ifdef FEAT_GUI
8433 !gui.in_use &&
8434#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008435 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 for (row = start_row; row < end_row; ++row)
8437 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008438#ifdef FEAT_MBYTE
8439 if (has_mbyte
8440# ifdef FEAT_GUI
8441 && !gui.in_use
8442# endif
8443 )
8444 {
8445 /* When drawing over the right halve of a double-wide char clear
8446 * out the left halve. When drawing over the left halve of a
8447 * double wide-char clear out the right halve. Only needed in a
8448 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008449 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008450 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008451 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008452 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008453 }
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 /*
8456 * Try to use delete-line termcap code, when no attributes or in a
8457 * "normal" terminal, where a bold/italic space is just a
8458 * space.
8459 */
8460 did_delete = FALSE;
8461 if (c2 == ' '
8462 && end_col == Columns
8463 && can_clear(T_CE)
8464 && (attr == 0
8465 || (norm_term
8466 && attr <= HL_ALL
8467 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8468 {
8469 /*
8470 * check if we really need to clear something
8471 */
8472 col = start_col;
8473 if (c1 != ' ') /* don't clear first char */
8474 ++col;
8475
8476 off = LineOffset[row] + col;
8477 end_off = LineOffset[row] + end_col;
8478
8479 /* skip blanks (used often, keep it fast!) */
8480#ifdef FEAT_MBYTE
8481 if (enc_utf8)
8482 while (off < end_off && ScreenLines[off] == ' '
8483 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8484 ++off;
8485 else
8486#endif
8487 while (off < end_off && ScreenLines[off] == ' '
8488 && ScreenAttrs[off] == 0)
8489 ++off;
8490 if (off < end_off) /* something to be cleared */
8491 {
8492 col = off - LineOffset[row];
8493 screen_stop_highlight();
8494 term_windgoto(row, col);/* clear rest of this screen line */
8495 out_str(T_CE);
8496 screen_start(); /* don't know where cursor is now */
8497 col = end_col - col;
8498 while (col--) /* clear chars in ScreenLines */
8499 {
8500 ScreenLines[off] = ' ';
8501#ifdef FEAT_MBYTE
8502 if (enc_utf8)
8503 ScreenLinesUC[off] = 0;
8504#endif
8505 ScreenAttrs[off] = 0;
8506 ++off;
8507 }
8508 }
8509 did_delete = TRUE; /* the chars are cleared now */
8510 }
8511
8512 off = LineOffset[row] + start_col;
8513 c = c1;
8514 for (col = start_col; col < end_col; ++col)
8515 {
8516 if (ScreenLines[off] != c
8517#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008518 || (enc_utf8 && (int)ScreenLinesUC[off]
8519 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520#endif
8521 || ScreenAttrs[off] != attr
8522#if defined(FEAT_GUI) || defined(UNIX)
8523 || force_next
8524#endif
8525 )
8526 {
8527#if defined(FEAT_GUI) || defined(UNIX)
8528 /* The bold trick may make a single row of pixels appear in
8529 * the next character. When a bold character is removed, the
8530 * next character should be redrawn too. This happens for our
8531 * own GUI and for some xterms. */
8532 if (
8533# ifdef FEAT_GUI
8534 gui.in_use
8535# endif
8536# if defined(FEAT_GUI) && defined(UNIX)
8537 ||
8538# endif
8539# ifdef UNIX
8540 term_is_xterm
8541# endif
8542 )
8543 {
8544 if (ScreenLines[off] != ' '
8545 && (ScreenAttrs[off] > HL_ALL
8546 || ScreenAttrs[off] & HL_BOLD))
8547 force_next = TRUE;
8548 else
8549 force_next = FALSE;
8550 }
8551#endif
8552 ScreenLines[off] = c;
8553#ifdef FEAT_MBYTE
8554 if (enc_utf8)
8555 {
8556 if (c >= 0x80)
8557 {
8558 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008559 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 }
8561 else
8562 ScreenLinesUC[off] = 0;
8563 }
8564#endif
8565 ScreenAttrs[off] = attr;
8566 if (!did_delete || c != ' ')
8567 screen_char(off, row, col);
8568 }
8569 ++off;
8570 if (col == start_col)
8571 {
8572 if (did_delete)
8573 break;
8574 c = c2;
8575 }
8576 }
8577 if (end_col == Columns)
8578 LineWraps[row] = FALSE;
8579 if (row == Rows - 1) /* overwritten the command line */
8580 {
8581 redraw_cmdline = TRUE;
8582 if (c1 == ' ' && c2 == ' ')
8583 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008584 if (start_col == 0)
8585 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 }
8588}
8589
8590/*
8591 * Check if there should be a delay. Used before clearing or redrawing the
8592 * screen or the command line.
8593 */
8594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008595check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8598 && !did_wait_return
8599 && emsg_silent == 0)
8600 {
8601 out_flush();
8602 ui_delay(1000L, TRUE);
8603 emsg_on_display = FALSE;
8604 if (check_msg_scroll)
8605 msg_scroll = FALSE;
8606 }
8607}
8608
8609/*
8610 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008611 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 * Returns TRUE if there is a valid screen to write to.
8613 * Returns FALSE when starting up and screen not initialized yet.
8614 */
8615 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008616screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008618 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 return (ScreenLines != NULL);
8620}
8621
8622/*
8623 * Resize the shell to Rows and Columns.
8624 * Allocate ScreenLines[] and associated items.
8625 *
8626 * There may be some time between setting Rows and Columns and (re)allocating
8627 * ScreenLines[]. This happens when starting up and when (manually) changing
8628 * the shell size. Always use screen_Rows and screen_Columns to access items
8629 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8630 * final size of the shell is needed.
8631 */
8632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008633screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634{
8635 int new_row, old_row;
8636#ifdef FEAT_GUI
8637 int old_Rows;
8638#endif
8639 win_T *wp;
8640 int outofmem = FALSE;
8641 int len;
8642 schar_T *new_ScreenLines;
8643#ifdef FEAT_MBYTE
8644 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008645 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008647 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#endif
8649 sattr_T *new_ScreenAttrs;
8650 unsigned *new_LineOffset;
8651 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008652#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008653 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008654 tabpage_T *tp;
8655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008657 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008658#ifdef FEAT_AUTOCMD
8659 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008661retry:
8662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 /*
8664 * Allocation of the screen buffers is done only when the size changes and
8665 * when Rows and Columns have been set and we have started doing full
8666 * screen stuff.
8667 */
8668 if ((ScreenLines != NULL
8669 && Rows == screen_Rows
8670 && Columns == screen_Columns
8671#ifdef FEAT_MBYTE
8672 && enc_utf8 == (ScreenLinesUC != NULL)
8673 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008674 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675#endif
8676 )
8677 || Rows == 0
8678 || Columns == 0
8679 || (!full_screen && ScreenLines == NULL))
8680 return;
8681
8682 /*
8683 * It's possible that we produce an out-of-memory message below, which
8684 * will cause this function to be called again. To break the loop, just
8685 * return here.
8686 */
8687 if (entered)
8688 return;
8689 entered = TRUE;
8690
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008691 /*
8692 * Note that the window sizes are updated before reallocating the arrays,
8693 * thus we must not redraw here!
8694 */
8695 ++RedrawingDisabled;
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 win_new_shellsize(); /* fit the windows in the new sized shell */
8698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 comp_col(); /* recompute columns for shown command and ruler */
8700
8701 /*
8702 * We're changing the size of the screen.
8703 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8704 * - Move lines from the old arrays into the new arrays, clear extra
8705 * lines (unless the screen is going to be cleared).
8706 * - Free the old arrays.
8707 *
8708 * If anything fails, make ScreenLines NULL, so we don't do anything!
8709 * Continuing with the old ScreenLines may result in a crash, because the
8710 * size is wrong.
8711 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008712 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008714#ifdef FEAT_AUTOCMD
8715 if (aucmd_win != NULL)
8716 win_free_lsize(aucmd_win);
8717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 new_ScreenLines = (schar_T *)lalloc((long_u)(
8720 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8721#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008722 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (enc_utf8)
8724 {
8725 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8726 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008727 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008728 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8730 }
8731 if (enc_dbcs == DBCS_JPNU)
8732 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8733 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8734#endif
8735 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8736 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8737 new_LineOffset = (unsigned *)lalloc((long_u)(
8738 Rows * sizeof(unsigned)), FALSE);
8739 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008740#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008741 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008744 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {
8746 if (win_alloc_lines(wp) == FAIL)
8747 {
8748 outofmem = TRUE;
8749#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008750 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751#endif
8752 }
8753 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008754#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008755 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8756 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008757 outofmem = TRUE;
8758#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008759#ifdef FEAT_WINDOWS
8760give_up:
8761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008763#ifdef FEAT_MBYTE
8764 for (i = 0; i < p_mco; ++i)
8765 if (new_ScreenLinesC[i] == NULL)
8766 break;
8767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 if (new_ScreenLines == NULL
8769#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008770 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8772#endif
8773 || new_ScreenAttrs == NULL
8774 || new_LineOffset == NULL
8775 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008776#ifdef FEAT_WINDOWS
8777 || new_TabPageIdxs == NULL
8778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 || outofmem)
8780 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008781 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008782 {
8783 /* guess the size */
8784 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8785
8786 /* Remember we did this to avoid getting outofmem messages over
8787 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008788 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 vim_free(new_ScreenLines);
8791 new_ScreenLines = NULL;
8792#ifdef FEAT_MBYTE
8793 vim_free(new_ScreenLinesUC);
8794 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008795 for (i = 0; i < p_mco; ++i)
8796 {
8797 vim_free(new_ScreenLinesC[i]);
8798 new_ScreenLinesC[i] = NULL;
8799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 vim_free(new_ScreenLines2);
8801 new_ScreenLines2 = NULL;
8802#endif
8803 vim_free(new_ScreenAttrs);
8804 new_ScreenAttrs = NULL;
8805 vim_free(new_LineOffset);
8806 new_LineOffset = NULL;
8807 vim_free(new_LineWraps);
8808 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008809#ifdef FEAT_WINDOWS
8810 vim_free(new_TabPageIdxs);
8811 new_TabPageIdxs = NULL;
8812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 }
8814 else
8815 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008816 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 for (new_row = 0; new_row < Rows; ++new_row)
8819 {
8820 new_LineOffset[new_row] = new_row * Columns;
8821 new_LineWraps[new_row] = FALSE;
8822
8823 /*
8824 * If the screen is not going to be cleared, copy as much as
8825 * possible from the old screen to the new one and clear the rest
8826 * (used when resizing the window at the "--more--" prompt or when
8827 * executing an external command, for the GUI).
8828 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008829 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {
8831 (void)vim_memset(new_ScreenLines + new_row * Columns,
8832 ' ', (size_t)Columns * sizeof(schar_T));
8833#ifdef FEAT_MBYTE
8834 if (enc_utf8)
8835 {
8836 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8837 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838 for (i = 0; i < p_mco; ++i)
8839 (void)vim_memset(new_ScreenLinesC[i]
8840 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 0, (size_t)Columns * sizeof(u8char_T));
8842 }
8843 if (enc_dbcs == DBCS_JPNU)
8844 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8845 0, (size_t)Columns * sizeof(schar_T));
8846#endif
8847 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8848 0, (size_t)Columns * sizeof(sattr_T));
8849 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008850 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 {
8852 if (screen_Columns < Columns)
8853 len = screen_Columns;
8854 else
8855 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008856#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008857 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008858 * may be invalid now. Also when p_mco changes. */
8859 if (!(enc_utf8 && ScreenLinesUC == NULL)
8860 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008861#endif
8862 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8863 ScreenLines + LineOffset[old_row],
8864 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 if (enc_utf8 && ScreenLinesUC != NULL
8867 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 {
8869 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8870 ScreenLinesUC + LineOffset[old_row],
8871 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008872 for (i = 0; i < p_mco; ++i)
8873 mch_memmove(new_ScreenLinesC[i]
8874 + new_LineOffset[new_row],
8875 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 (size_t)len * sizeof(u8char_T));
8877 }
8878 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8879 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8880 ScreenLines2 + LineOffset[old_row],
8881 (size_t)len * sizeof(schar_T));
8882#endif
8883 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8884 ScreenAttrs + LineOffset[old_row],
8885 (size_t)len * sizeof(sattr_T));
8886 }
8887 }
8888 }
8889 /* Use the last line of the screen for the current line. */
8890 current_ScreenLine = new_ScreenLines + Rows * Columns;
8891 }
8892
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008893 free_screenlines();
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 ScreenLines = new_ScreenLines;
8896#ifdef FEAT_MBYTE
8897 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008898 for (i = 0; i < p_mco; ++i)
8899 ScreenLinesC[i] = new_ScreenLinesC[i];
8900 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 ScreenLines2 = new_ScreenLines2;
8902#endif
8903 ScreenAttrs = new_ScreenAttrs;
8904 LineOffset = new_LineOffset;
8905 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008906#ifdef FEAT_WINDOWS
8907 TabPageIdxs = new_TabPageIdxs;
8908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
8910 /* It's important that screen_Rows and screen_Columns reflect the actual
8911 * size of ScreenLines[]. Set them before calling anything. */
8912#ifdef FEAT_GUI
8913 old_Rows = screen_Rows;
8914#endif
8915 screen_Rows = Rows;
8916 screen_Columns = Columns;
8917
8918 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008919 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 screenclear2();
8921
8922#ifdef FEAT_GUI
8923 else if (gui.in_use
8924 && !gui.starting
8925 && ScreenLines != NULL
8926 && old_Rows != Rows)
8927 {
8928 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8929 /*
8930 * Adjust the position of the cursor, for when executing an external
8931 * command.
8932 */
8933 if (msg_row >= Rows) /* Rows got smaller */
8934 msg_row = Rows - 1; /* put cursor at last row */
8935 else if (Rows > old_Rows) /* Rows got bigger */
8936 msg_row += Rows - old_Rows; /* put cursor in same place */
8937 if (msg_col >= Columns) /* Columns got smaller */
8938 msg_col = Columns - 1; /* put cursor at last column */
8939 }
8940#endif
8941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008943 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008944
8945#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008946 /*
8947 * Do not apply autocommands more than 3 times to avoid an endless loop
8948 * in case applying autocommands always changes Rows or Columns.
8949 */
8950 if (starting == 0 && ++retry_count <= 3)
8951 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008952 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008953 /* In rare cases, autocommands may have altered Rows or Columns,
8954 * jump back to check if we need to allocate the screen again. */
8955 goto retry;
8956 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
8960 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008961free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008962{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008963#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008964 int i;
8965
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008966 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008967 for (i = 0; i < Screen_mco; ++i)
8968 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008969 vim_free(ScreenLines2);
8970#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008971 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008972 vim_free(ScreenAttrs);
8973 vim_free(LineOffset);
8974 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008975#ifdef FEAT_WINDOWS
8976 vim_free(TabPageIdxs);
8977#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008978}
8979
8980 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008981screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983 check_for_delay(FALSE);
8984 screenalloc(FALSE); /* allocate screen buffers if size changed */
8985 screenclear2(); /* clear the screen */
8986}
8987
8988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008989screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991 int i;
8992
8993 if (starting == NO_SCREEN || ScreenLines == NULL
8994#ifdef FEAT_GUI
8995 || (gui.in_use && gui.starting)
8996#endif
8997 )
8998 return;
8999
9000#ifdef FEAT_GUI
9001 if (!gui.in_use)
9002#endif
9003 screen_attr = -1; /* force setting the Normal colors */
9004 screen_stop_highlight(); /* don't want highlighting here */
9005
9006#ifdef FEAT_CLIPBOARD
9007 /* disable selection without redrawing it */
9008 clip_scroll_selection(9999);
9009#endif
9010
9011 /* blank out ScreenLines */
9012 for (i = 0; i < Rows; ++i)
9013 {
9014 lineclear(LineOffset[i], (int)Columns);
9015 LineWraps[i] = FALSE;
9016 }
9017
9018 if (can_clear(T_CL))
9019 {
9020 out_str(T_CL); /* clear the display */
9021 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009022 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 }
9024 else
9025 {
9026 /* can't clear the screen, mark all chars with invalid attributes */
9027 for (i = 0; i < Rows; ++i)
9028 lineinvalid(LineOffset[i], (int)Columns);
9029 clear_cmdline = TRUE;
9030 }
9031
9032 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9033
9034 win_rest_invalid(firstwin);
9035 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009036#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009037 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (must_redraw == CLEAR) /* no need to clear again */
9040 must_redraw = NOT_VALID;
9041 compute_cmdrow();
9042 msg_row = cmdline_row; /* put cursor on last line for messages */
9043 msg_col = 0;
9044 screen_start(); /* don't know where cursor is now */
9045 msg_scrolled = 0; /* can't scroll back */
9046 msg_didany = FALSE;
9047 msg_didout = FALSE;
9048}
9049
9050/*
9051 * Clear one line in ScreenLines.
9052 */
9053 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009054lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9057#ifdef FEAT_MBYTE
9058 if (enc_utf8)
9059 (void)vim_memset(ScreenLinesUC + off, 0,
9060 (size_t)width * sizeof(u8char_T));
9061#endif
9062 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9063}
9064
9065/*
9066 * Mark one line in ScreenLines invalid by setting the attributes to an
9067 * invalid value.
9068 */
9069 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009070lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9073}
9074
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009075#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*
9077 * Copy part of a Screenline for vertically split window "wp".
9078 */
9079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009080linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 unsigned off_to = LineOffset[to] + wp->w_wincol;
9083 unsigned off_from = LineOffset[from] + wp->w_wincol;
9084
9085 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9086 wp->w_width * sizeof(schar_T));
9087# ifdef FEAT_MBYTE
9088 if (enc_utf8)
9089 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009090 int i;
9091
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9093 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009094 for (i = 0; i < p_mco; ++i)
9095 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9096 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 }
9098 if (enc_dbcs == DBCS_JPNU)
9099 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9100 wp->w_width * sizeof(schar_T));
9101# endif
9102 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9103 wp->w_width * sizeof(sattr_T));
9104}
9105#endif
9106
9107/*
9108 * Return TRUE if clearing with term string "p" would work.
9109 * It can't work when the string is empty or it won't set the right background.
9110 */
9111 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009112can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 return (*p != NUL && (t_colors <= 1
9115#ifdef FEAT_GUI
9116 || gui.in_use
9117#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009118#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009119 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009120 || (!p_tgc && cterm_normal_bg_color == 0)
9121#else
9122 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009123#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009124 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * Reset cursor position. Use whenever cursor was moved because of outputting
9129 * something directly to the screen (shell commands) or a terminal control
9130 * code.
9131 */
9132 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009133screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 screen_cur_row = screen_cur_col = 9999;
9136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * Move the cursor to position "row","col" in the screen.
9140 * This tries to find the most efficient way to move, minimizing the number of
9141 * characters sent to the terminal.
9142 */
9143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009144windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009146 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 int i;
9148 int plan;
9149 int cost;
9150 int wouldbe_col;
9151 int noinvcurs;
9152 char_u *bs;
9153 int goto_cost;
9154 int attr;
9155
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009156#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9158
9159#define PLAN_LE 1
9160#define PLAN_CR 2
9161#define PLAN_NL 3
9162#define PLAN_WRITE 4
9163 /* Can't use ScreenLines unless initialized */
9164 if (ScreenLines == NULL)
9165 return;
9166
9167 if (col != screen_cur_col || row != screen_cur_row)
9168 {
9169 /* Check for valid position. */
9170 if (row < 0) /* window without text lines? */
9171 row = 0;
9172 if (row >= screen_Rows)
9173 row = screen_Rows - 1;
9174 if (col >= screen_Columns)
9175 col = screen_Columns - 1;
9176
9177 /* check if no cursor movement is allowed in highlight mode */
9178 if (screen_attr && *T_MS == NUL)
9179 noinvcurs = HIGHL_COST;
9180 else
9181 noinvcurs = 0;
9182 goto_cost = GOTO_COST + noinvcurs;
9183
9184 /*
9185 * Plan how to do the positioning:
9186 * 1. Use CR to move it to column 0, same row.
9187 * 2. Use T_LE to move it a few columns to the left.
9188 * 3. Use NL to move a few lines down, column 0.
9189 * 4. Move a few columns to the right with T_ND or by writing chars.
9190 *
9191 * Don't do this if the cursor went beyond the last column, the cursor
9192 * position is unknown then (some terminals wrap, some don't )
9193 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009194 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * characters to move the cursor to the right.
9196 */
9197 if (row >= screen_cur_row && screen_cur_col < Columns)
9198 {
9199 /*
9200 * If the cursor is in the same row, bigger col, we can use CR
9201 * or T_LE.
9202 */
9203 bs = NULL; /* init for GCC */
9204 attr = screen_attr;
9205 if (row == screen_cur_row && col < screen_cur_col)
9206 {
9207 /* "le" is preferred over "bc", because "bc" is obsolete */
9208 if (*T_LE)
9209 bs = T_LE; /* "cursor left" */
9210 else
9211 bs = T_BC; /* "backspace character (old) */
9212 if (*bs)
9213 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9214 else
9215 cost = 999;
9216 if (col + 1 < cost) /* using CR is less characters */
9217 {
9218 plan = PLAN_CR;
9219 wouldbe_col = 0;
9220 cost = 1; /* CR is just one character */
9221 }
9222 else
9223 {
9224 plan = PLAN_LE;
9225 wouldbe_col = col;
9226 }
9227 if (noinvcurs) /* will stop highlighting */
9228 {
9229 cost += noinvcurs;
9230 attr = 0;
9231 }
9232 }
9233
9234 /*
9235 * If the cursor is above where we want to be, we can use CR LF.
9236 */
9237 else if (row > screen_cur_row)
9238 {
9239 plan = PLAN_NL;
9240 wouldbe_col = 0;
9241 cost = (row - screen_cur_row) * 2; /* CR LF */
9242 if (noinvcurs) /* will stop highlighting */
9243 {
9244 cost += noinvcurs;
9245 attr = 0;
9246 }
9247 }
9248
9249 /*
9250 * If the cursor is in the same row, smaller col, just use write.
9251 */
9252 else
9253 {
9254 plan = PLAN_WRITE;
9255 wouldbe_col = screen_cur_col;
9256 cost = 0;
9257 }
9258
9259 /*
9260 * Check if any characters that need to be written have the
9261 * correct attributes. Also avoid UTF-8 characters.
9262 */
9263 i = col - wouldbe_col;
9264 if (i > 0)
9265 cost += i;
9266 if (cost < goto_cost && i > 0)
9267 {
9268 /*
9269 * Check if the attributes are correct without additionally
9270 * stopping highlighting.
9271 */
9272 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9273 while (i && *p++ == attr)
9274 --i;
9275 if (i != 0)
9276 {
9277 /*
9278 * Try if it works when highlighting is stopped here.
9279 */
9280 if (*--p == 0)
9281 {
9282 cost += noinvcurs;
9283 while (i && *p++ == 0)
9284 --i;
9285 }
9286 if (i != 0)
9287 cost = 999; /* different attributes, don't do it */
9288 }
9289#ifdef FEAT_MBYTE
9290 if (enc_utf8)
9291 {
9292 /* Don't use an UTF-8 char for positioning, it's slow. */
9293 for (i = wouldbe_col; i < col; ++i)
9294 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9295 {
9296 cost = 999;
9297 break;
9298 }
9299 }
9300#endif
9301 }
9302
9303 /*
9304 * We can do it without term_windgoto()!
9305 */
9306 if (cost < goto_cost)
9307 {
9308 if (plan == PLAN_LE)
9309 {
9310 if (noinvcurs)
9311 screen_stop_highlight();
9312 while (screen_cur_col > col)
9313 {
9314 out_str(bs);
9315 --screen_cur_col;
9316 }
9317 }
9318 else if (plan == PLAN_CR)
9319 {
9320 if (noinvcurs)
9321 screen_stop_highlight();
9322 out_char('\r');
9323 screen_cur_col = 0;
9324 }
9325 else if (plan == PLAN_NL)
9326 {
9327 if (noinvcurs)
9328 screen_stop_highlight();
9329 while (screen_cur_row < row)
9330 {
9331 out_char('\n');
9332 ++screen_cur_row;
9333 }
9334 screen_cur_col = 0;
9335 }
9336
9337 i = col - screen_cur_col;
9338 if (i > 0)
9339 {
9340 /*
9341 * Use cursor-right if it's one character only. Avoids
9342 * removing a line of pixels from the last bold char, when
9343 * using the bold trick in the GUI.
9344 */
9345 if (T_ND[0] != NUL && T_ND[1] == NUL)
9346 {
9347 while (i-- > 0)
9348 out_char(*T_ND);
9349 }
9350 else
9351 {
9352 int off;
9353
9354 off = LineOffset[row] + screen_cur_col;
9355 while (i-- > 0)
9356 {
9357 if (ScreenAttrs[off] != screen_attr)
9358 screen_stop_highlight();
9359#ifdef FEAT_MBYTE
9360 out_flush_check();
9361#endif
9362 out_char(ScreenLines[off]);
9363#ifdef FEAT_MBYTE
9364 if (enc_dbcs == DBCS_JPNU
9365 && ScreenLines[off] == 0x8e)
9366 out_char(ScreenLines2[off]);
9367#endif
9368 ++off;
9369 }
9370 }
9371 }
9372 }
9373 }
9374 else
9375 cost = 999;
9376
9377 if (cost >= goto_cost)
9378 {
9379 if (noinvcurs)
9380 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009381 if (row == screen_cur_row && (col > screen_cur_col)
9382 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 term_cursor_right(col - screen_cur_col);
9384 else
9385 term_windgoto(row, col);
9386 }
9387 screen_cur_row = row;
9388 screen_cur_col = col;
9389 }
9390}
9391
9392/*
9393 * Set cursor to its position in the current window.
9394 */
9395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009396setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 if (redrawing())
9399 {
9400 validate_cursor();
9401 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9402 W_WINCOL(curwin) + (
9403#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009404 /* With 'rightleft' set and the cursor on a double-wide
9405 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9407# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009408 (has_mbyte
9409 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9410 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411# endif
9412 1)) :
9413#endif
9414 curwin->w_wcol));
9415 }
9416}
9417
9418
9419/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009420 * Insert 'line_count' lines at 'row' in window 'wp'.
9421 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9422 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 * scrolling.
9424 * Returns FAIL if the lines are not inserted, OK for success.
9425 */
9426 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009427win_ins_lines(
9428 win_T *wp,
9429 int row,
9430 int line_count,
9431 int invalid,
9432 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 int did_delete;
9435 int nextrow;
9436 int lastrow;
9437 int retval;
9438
9439 if (invalid)
9440 wp->w_lines_valid = 0;
9441
9442 if (wp->w_height < 5)
9443 return FAIL;
9444
9445 if (line_count > wp->w_height - row)
9446 line_count = wp->w_height - row;
9447
9448 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9449 if (retval != MAYBE)
9450 return retval;
9451
9452 /*
9453 * If there is a next window or a status line, we first try to delete the
9454 * lines at the bottom to avoid messing what is after the window.
9455 * If this fails and there are following windows, don't do anything to avoid
9456 * messing up those windows, better just redraw.
9457 */
9458 did_delete = FALSE;
9459#ifdef FEAT_WINDOWS
9460 if (wp->w_next != NULL || wp->w_status_height)
9461 {
9462 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9463 line_count, (int)Rows, FALSE, NULL) == OK)
9464 did_delete = TRUE;
9465 else if (wp->w_next)
9466 return FAIL;
9467 }
9468#endif
9469 /*
9470 * if no lines deleted, blank the lines that will end up below the window
9471 */
9472 if (!did_delete)
9473 {
9474#ifdef FEAT_WINDOWS
9475 wp->w_redr_status = TRUE;
9476#endif
9477 redraw_cmdline = TRUE;
9478 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9479 lastrow = nextrow + line_count;
9480 if (lastrow > Rows)
9481 lastrow = Rows;
9482 screen_fill(nextrow - line_count, lastrow - line_count,
9483 W_WINCOL(wp), (int)W_ENDCOL(wp),
9484 ' ', ' ', 0);
9485 }
9486
9487 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9488 == FAIL)
9489 {
9490 /* deletion will have messed up other windows */
9491 if (did_delete)
9492 {
9493#ifdef FEAT_WINDOWS
9494 wp->w_redr_status = TRUE;
9495#endif
9496 win_rest_invalid(W_NEXT(wp));
9497 }
9498 return FAIL;
9499 }
9500
9501 return OK;
9502}
9503
9504/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009505 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9507 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9508 * scrolling
9509 * Return OK for success, FAIL if the lines are not deleted.
9510 */
9511 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009512win_del_lines(
9513 win_T *wp,
9514 int row,
9515 int line_count,
9516 int invalid,
9517 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 int retval;
9520
9521 if (invalid)
9522 wp->w_lines_valid = 0;
9523
9524 if (line_count > wp->w_height - row)
9525 line_count = wp->w_height - row;
9526
9527 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9528 if (retval != MAYBE)
9529 return retval;
9530
9531 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9532 (int)Rows, FALSE, NULL) == FAIL)
9533 return FAIL;
9534
9535#ifdef FEAT_WINDOWS
9536 /*
9537 * If there are windows or status lines below, try to put them at the
9538 * correct place. If we can't do that, they have to be redrawn.
9539 */
9540 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9541 {
9542 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9543 line_count, (int)Rows, NULL) == FAIL)
9544 {
9545 wp->w_redr_status = TRUE;
9546 win_rest_invalid(wp->w_next);
9547 }
9548 }
9549 /*
9550 * If this is the last window and there is no status line, redraw the
9551 * command line later.
9552 */
9553 else
9554#endif
9555 redraw_cmdline = TRUE;
9556 return OK;
9557}
9558
9559/*
9560 * Common code for win_ins_lines() and win_del_lines().
9561 * Returns OK or FAIL when the work has been done.
9562 * Returns MAYBE when not finished yet.
9563 */
9564 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009565win_do_lines(
9566 win_T *wp,
9567 int row,
9568 int line_count,
9569 int mayclear,
9570 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 int retval;
9573
9574 if (!redrawing() || line_count <= 0)
9575 return FAIL;
9576
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009577 /* When inserting lines would result in loss of command output, just redraw
9578 * the lines. */
9579 if (no_win_do_lines_ins && !del)
9580 return FAIL;
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 /* only a few lines left: redraw is faster */
9583 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009584#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 && wp->w_width == Columns
9586#endif
9587 )
9588 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009589 if (!no_win_do_lines_ins)
9590 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 return FAIL;
9592 }
9593
9594 /*
9595 * Delete all remaining lines
9596 */
9597 if (row + line_count >= wp->w_height)
9598 {
9599 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9600 W_WINCOL(wp), (int)W_ENDCOL(wp),
9601 ' ', ' ', 0);
9602 return OK;
9603 }
9604
9605 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009606 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009608 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009610 if (!no_win_do_lines_ins)
9611 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
9613 /*
9614 * If the terminal can set a scroll region, use that.
9615 * Always do this in a vertically split window. This will redraw from
9616 * ScreenLines[] when t_CV isn't defined. That's faster than using
9617 * win_line().
9618 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009619 * a character in the lower right corner of the scroll region may cause a
9620 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 */
9622 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009623#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 || W_WIDTH(wp) != Columns
9625#endif
9626 )
9627 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009628#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9630#endif
9631 scroll_region_set(wp, row);
9632 if (del)
9633 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9634 wp->w_height - row, FALSE, wp);
9635 else
9636 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9637 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009638#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9640#endif
9641 scroll_region_reset();
9642 return retval;
9643 }
9644
9645#ifdef FEAT_WINDOWS
9646 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9647 return FAIL;
9648#endif
9649
9650 return MAYBE;
9651}
9652
9653/*
9654 * window 'wp' and everything after it is messed up, mark it for redraw
9655 */
9656 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009657win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659#ifdef FEAT_WINDOWS
9660 while (wp != NULL)
9661#else
9662 if (wp != NULL)
9663#endif
9664 {
9665 redraw_win_later(wp, NOT_VALID);
9666#ifdef FEAT_WINDOWS
9667 wp->w_redr_status = TRUE;
9668 wp = wp->w_next;
9669#endif
9670 }
9671 redraw_cmdline = TRUE;
9672}
9673
9674/*
9675 * The rest of the routines in this file perform screen manipulations. The
9676 * given operation is performed physically on the screen. The corresponding
9677 * change is also made to the internal screen image. In this way, the editor
9678 * anticipates the effect of editing changes on the appearance of the screen.
9679 * That way, when we call screenupdate a complete redraw isn't usually
9680 * necessary. Another advantage is that we can keep adding code to anticipate
9681 * screen changes, and in the meantime, everything still works.
9682 */
9683
9684/*
9685 * types for inserting or deleting lines
9686 */
9687#define USE_T_CAL 1
9688#define USE_T_CDL 2
9689#define USE_T_AL 3
9690#define USE_T_CE 4
9691#define USE_T_DL 5
9692#define USE_T_SR 6
9693#define USE_NL 7
9694#define USE_T_CD 8
9695#define USE_REDRAW 9
9696
9697/*
9698 * insert lines on the screen and update ScreenLines[]
9699 * 'end' is the line after the scrolled part. Normally it is Rows.
9700 * When scrolling region used 'off' is the offset from the top for the region.
9701 * 'row' and 'end' are relative to the start of the region.
9702 *
9703 * return FAIL for failure, OK for success.
9704 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009705 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009706screen_ins_lines(
9707 int off,
9708 int row,
9709 int line_count,
9710 int end,
9711 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
9713 int i;
9714 int j;
9715 unsigned temp;
9716 int cursor_row;
9717 int type;
9718 int result_empty;
9719 int can_ce = can_clear(T_CE);
9720
9721 /*
9722 * FAIL if
9723 * - there is no valid screen
9724 * - the screen has to be redrawn completely
9725 * - the line count is less than one
9726 * - the line count is more than 'ttyscroll'
9727 */
9728 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9729 return FAIL;
9730
9731 /*
9732 * There are seven ways to insert lines:
9733 * 0. When in a vertically split window and t_CV isn't set, redraw the
9734 * characters from ScreenLines[].
9735 * 1. Use T_CD (clear to end of display) if it exists and the result of
9736 * the insert is just empty lines
9737 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9738 * present or line_count > 1. It looks better if we do all the inserts
9739 * at once.
9740 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9741 * insert is just empty lines and T_CE is not present or line_count >
9742 * 1.
9743 * 4. Use T_AL (insert line) if it exists.
9744 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9745 * just empty lines.
9746 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9747 * just empty lines.
9748 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9749 * the 'da' flag is not set or we have clear line capability.
9750 * 8. redraw the characters from ScreenLines[].
9751 *
9752 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9753 * the scrollbar for the window. It does have insert line, use that if it
9754 * exists.
9755 */
9756 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009757#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9759 type = USE_REDRAW;
9760 else
9761#endif
9762 if (can_clear(T_CD) && result_empty)
9763 type = USE_T_CD;
9764 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9765 type = USE_T_CAL;
9766 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9767 type = USE_T_CDL;
9768 else if (*T_AL != NUL)
9769 type = USE_T_AL;
9770 else if (can_ce && result_empty)
9771 type = USE_T_CE;
9772 else if (*T_DL != NUL && result_empty)
9773 type = USE_T_DL;
9774 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9775 type = USE_T_SR;
9776 else
9777 return FAIL;
9778
9779 /*
9780 * For clearing the lines screen_del_lines() is used. This will also take
9781 * care of t_db if necessary.
9782 */
9783 if (type == USE_T_CD || type == USE_T_CDL ||
9784 type == USE_T_CE || type == USE_T_DL)
9785 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9786
9787 /*
9788 * If text is retained below the screen, first clear or delete as many
9789 * lines at the bottom of the window as are about to be inserted so that
9790 * the deleted lines won't later surface during a screen_del_lines.
9791 */
9792 if (*T_DB)
9793 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9794
9795#ifdef FEAT_CLIPBOARD
9796 /* Remove a modeless selection when inserting lines halfway the screen
9797 * or not the full width of the screen. */
9798 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009799# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 || (wp != NULL && wp->w_width != Columns)
9801# endif
9802 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009803 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 else
9805 clip_scroll_selection(-line_count);
9806#endif
9807
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#ifdef FEAT_GUI
9809 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9810 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009811 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812#endif
9813
9814 if (*T_CCS != NUL) /* cursor relative to region */
9815 cursor_row = row;
9816 else
9817 cursor_row = row + off;
9818
9819 /*
9820 * Shift LineOffset[] line_count down to reflect the inserted lines.
9821 * Clear the inserted lines in ScreenLines[].
9822 */
9823 row += off;
9824 end += off;
9825 for (i = 0; i < line_count; ++i)
9826 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009827#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (wp != NULL && wp->w_width != Columns)
9829 {
9830 /* need to copy part of a line */
9831 j = end - 1 - i;
9832 while ((j -= line_count) >= row)
9833 linecopy(j + line_count, j, wp);
9834 j += line_count;
9835 if (can_clear((char_u *)" "))
9836 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9837 else
9838 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9839 LineWraps[j] = FALSE;
9840 }
9841 else
9842#endif
9843 {
9844 j = end - 1 - i;
9845 temp = LineOffset[j];
9846 while ((j -= line_count) >= row)
9847 {
9848 LineOffset[j + line_count] = LineOffset[j];
9849 LineWraps[j + line_count] = LineWraps[j];
9850 }
9851 LineOffset[j + line_count] = temp;
9852 LineWraps[j + line_count] = FALSE;
9853 if (can_clear((char_u *)" "))
9854 lineclear(temp, (int)Columns);
9855 else
9856 lineinvalid(temp, (int)Columns);
9857 }
9858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 screen_stop_highlight();
9861 windgoto(cursor_row, 0);
9862
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009863#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 /* redraw the characters */
9865 if (type == USE_REDRAW)
9866 redraw_block(row, end, wp);
9867 else
9868#endif
9869 if (type == USE_T_CAL)
9870 {
9871 term_append_lines(line_count);
9872 screen_start(); /* don't know where cursor is now */
9873 }
9874 else
9875 {
9876 for (i = 0; i < line_count; i++)
9877 {
9878 if (type == USE_T_AL)
9879 {
9880 if (i && cursor_row != 0)
9881 windgoto(cursor_row, 0);
9882 out_str(T_AL);
9883 }
9884 else /* type == USE_T_SR */
9885 out_str(T_SR);
9886 screen_start(); /* don't know where cursor is now */
9887 }
9888 }
9889
9890 /*
9891 * With scroll-reverse and 'da' flag set we need to clear the lines that
9892 * have been scrolled down into the region.
9893 */
9894 if (type == USE_T_SR && *T_DA)
9895 {
9896 for (i = 0; i < line_count; ++i)
9897 {
9898 windgoto(off + i, 0);
9899 out_str(T_CE);
9900 screen_start(); /* don't know where cursor is now */
9901 }
9902 }
9903
9904#ifdef FEAT_GUI
9905 gui_can_update_cursor();
9906 if (gui.in_use)
9907 out_flush(); /* always flush after a scroll */
9908#endif
9909 return OK;
9910}
9911
9912/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009913 * Delete lines on the screen and update ScreenLines[].
9914 * "end" is the line after the scrolled part. Normally it is Rows.
9915 * When scrolling region used "off" is the offset from the top for the region.
9916 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 *
9918 * Return OK for success, FAIL if the lines are not deleted.
9919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009921screen_del_lines(
9922 int off,
9923 int row,
9924 int line_count,
9925 int end,
9926 int force, /* even when line_count > p_ttyscroll */
9927 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929 int j;
9930 int i;
9931 unsigned temp;
9932 int cursor_row;
9933 int cursor_end;
9934 int result_empty; /* result is empty until end of region */
9935 int can_delete; /* deleting line codes can be used */
9936 int type;
9937
9938 /*
9939 * FAIL if
9940 * - there is no valid screen
9941 * - the screen has to be redrawn completely
9942 * - the line count is less than one
9943 * - the line count is more than 'ttyscroll'
9944 */
9945 if (!screen_valid(TRUE) || line_count <= 0 ||
9946 (!force && line_count > p_ttyscroll))
9947 return FAIL;
9948
9949 /*
9950 * Check if the rest of the current region will become empty.
9951 */
9952 result_empty = row + line_count >= end;
9953
9954 /*
9955 * We can delete lines only when 'db' flag not set or when 'ce' option
9956 * available.
9957 */
9958 can_delete = (*T_DB == NUL || can_clear(T_CE));
9959
9960 /*
9961 * There are six ways to delete lines:
9962 * 0. When in a vertically split window and t_CV isn't set, redraw the
9963 * characters from ScreenLines[].
9964 * 1. Use T_CD if it exists and the result is empty.
9965 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9966 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9967 * none of the other ways work.
9968 * 4. Use T_CE (erase line) if the result is empty.
9969 * 5. Use T_DL (delete line) if it exists.
9970 * 6. redraw the characters from ScreenLines[].
9971 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009972#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9974 type = USE_REDRAW;
9975 else
9976#endif
9977 if (can_clear(T_CD) && result_empty)
9978 type = USE_T_CD;
9979#if defined(__BEOS__) && defined(BEOS_DR8)
9980 /*
9981 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9982 * its internal termcap... this works okay for tests which test *T_DB !=
9983 * NUL. It has the disadvantage that the user cannot use any :set t_*
9984 * command to get T_DB (back) to empty_option, only :set term=... will do
9985 * the trick...
9986 * Anyway, this hack will hopefully go away with the next OS release.
9987 * (Olaf Seibert)
9988 */
9989 else if (row == 0 && T_DB == empty_option
9990 && (line_count == 1 || *T_CDL == NUL))
9991#else
9992 else if (row == 0 && (
9993#ifndef AMIGA
9994 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9995 * up, so use delete-line command */
9996 line_count == 1 ||
9997#endif
9998 *T_CDL == NUL))
9999#endif
10000 type = USE_NL;
10001 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10002 type = USE_T_CDL;
10003 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010004#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 && (wp == NULL || wp->w_width == Columns)
10006#endif
10007 )
10008 type = USE_T_CE;
10009 else if (*T_DL != NUL && can_delete)
10010 type = USE_T_DL;
10011 else if (*T_CDL != NUL && can_delete)
10012 type = USE_T_CDL;
10013 else
10014 return FAIL;
10015
10016#ifdef FEAT_CLIPBOARD
10017 /* Remove a modeless selection when deleting lines halfway the screen or
10018 * not the full width of the screen. */
10019 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010020# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 || (wp != NULL && wp->w_width != Columns)
10022# endif
10023 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010024 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 else
10026 clip_scroll_selection(line_count);
10027#endif
10028
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029#ifdef FEAT_GUI
10030 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10031 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010032 gui_dont_update_cursor(gui.cursor_row >= row + off
10033 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034#endif
10035
10036 if (*T_CCS != NUL) /* cursor relative to region */
10037 {
10038 cursor_row = row;
10039 cursor_end = end;
10040 }
10041 else
10042 {
10043 cursor_row = row + off;
10044 cursor_end = end + off;
10045 }
10046
10047 /*
10048 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10049 * Clear the inserted lines in ScreenLines[].
10050 */
10051 row += off;
10052 end += off;
10053 for (i = 0; i < line_count; ++i)
10054 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010055#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 if (wp != NULL && wp->w_width != Columns)
10057 {
10058 /* need to copy part of a line */
10059 j = row + i;
10060 while ((j += line_count) <= end - 1)
10061 linecopy(j - line_count, j, wp);
10062 j -= line_count;
10063 if (can_clear((char_u *)" "))
10064 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10065 else
10066 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10067 LineWraps[j] = FALSE;
10068 }
10069 else
10070#endif
10071 {
10072 /* whole width, moving the line pointers is faster */
10073 j = row + i;
10074 temp = LineOffset[j];
10075 while ((j += line_count) <= end - 1)
10076 {
10077 LineOffset[j - line_count] = LineOffset[j];
10078 LineWraps[j - line_count] = LineWraps[j];
10079 }
10080 LineOffset[j - line_count] = temp;
10081 LineWraps[j - line_count] = FALSE;
10082 if (can_clear((char_u *)" "))
10083 lineclear(temp, (int)Columns);
10084 else
10085 lineinvalid(temp, (int)Columns);
10086 }
10087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
10089 screen_stop_highlight();
10090
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010091#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 /* redraw the characters */
10093 if (type == USE_REDRAW)
10094 redraw_block(row, end, wp);
10095 else
10096#endif
10097 if (type == USE_T_CD) /* delete the lines */
10098 {
10099 windgoto(cursor_row, 0);
10100 out_str(T_CD);
10101 screen_start(); /* don't know where cursor is now */
10102 }
10103 else if (type == USE_T_CDL)
10104 {
10105 windgoto(cursor_row, 0);
10106 term_delete_lines(line_count);
10107 screen_start(); /* don't know where cursor is now */
10108 }
10109 /*
10110 * Deleting lines at top of the screen or scroll region: Just scroll
10111 * the whole screen (scroll region) up by outputting newlines on the
10112 * last line.
10113 */
10114 else if (type == USE_NL)
10115 {
10116 windgoto(cursor_end - 1, 0);
10117 for (i = line_count; --i >= 0; )
10118 out_char('\n'); /* cursor will remain on same line */
10119 }
10120 else
10121 {
10122 for (i = line_count; --i >= 0; )
10123 {
10124 if (type == USE_T_DL)
10125 {
10126 windgoto(cursor_row, 0);
10127 out_str(T_DL); /* delete a line */
10128 }
10129 else /* type == USE_T_CE */
10130 {
10131 windgoto(cursor_row + i, 0);
10132 out_str(T_CE); /* erase a line */
10133 }
10134 screen_start(); /* don't know where cursor is now */
10135 }
10136 }
10137
10138 /*
10139 * If the 'db' flag is set, we need to clear the lines that have been
10140 * scrolled up at the bottom of the region.
10141 */
10142 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10143 {
10144 for (i = line_count; i > 0; --i)
10145 {
10146 windgoto(cursor_end - i, 0);
10147 out_str(T_CE); /* erase a line */
10148 screen_start(); /* don't know where cursor is now */
10149 }
10150 }
10151
10152#ifdef FEAT_GUI
10153 gui_can_update_cursor();
10154 if (gui.in_use)
10155 out_flush(); /* always flush after a scroll */
10156#endif
10157
10158 return OK;
10159}
10160
10161/*
10162 * show the current mode and ruler
10163 *
10164 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10165 * If clear_cmdline is FALSE there may be a message there that needs to be
10166 * cleared only if a mode is shown.
10167 * Return the length of the message (0 if no message).
10168 */
10169 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010170showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 int need_clear;
10173 int length = 0;
10174 int do_mode;
10175 int attr;
10176 int nwr_save;
10177#ifdef FEAT_INS_EXPAND
10178 int sub_attr;
10179#endif
10180
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010181 do_mode = ((p_smd && msg_silent == 0)
10182 && ((State & INSERT)
10183 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010184 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 if (do_mode || Recording)
10186 {
10187 /*
10188 * Don't show mode right now, when not redrawing or inside a mapping.
10189 * Call char_avail() only when we are going to show something, because
10190 * it takes a bit of time.
10191 */
10192 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10193 {
10194 redraw_cmdline = TRUE; /* show mode later */
10195 return 0;
10196 }
10197
10198 nwr_save = need_wait_return;
10199
10200 /* wait a bit before overwriting an important message */
10201 check_for_delay(FALSE);
10202
10203 /* if the cmdline is more than one line high, erase top lines */
10204 need_clear = clear_cmdline;
10205 if (clear_cmdline && cmdline_row < Rows - 1)
10206 msg_clr_cmdline(); /* will reset clear_cmdline */
10207
10208 /* Position on the last line in the window, column 0 */
10209 msg_pos_mode();
10210 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010211 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (do_mode)
10213 {
10214 MSG_PUTS_ATTR("--", attr);
10215#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010216 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010217# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010218 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010219# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010220 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010221# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010222 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010223# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 MSG_PUTS_ATTR(" IM", attr);
10225# else
10226 MSG_PUTS_ATTR(" XIM", attr);
10227# endif
10228#endif
10229#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10230 if (gui.in_use)
10231 {
10232 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010233 {
10234 /* HANGUL */
10235 if (enc_utf8)
10236 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10237 else
10238 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241#endif
10242#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010243 /* CTRL-X in Insert mode */
10244 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 {
10246 /* These messages can get long, avoid a wrap in a narrow
10247 * window. Prefer showing edit_submode_extra. */
10248 length = (Rows - msg_row) * Columns - 3;
10249 if (edit_submode_extra != NULL)
10250 length -= vim_strsize(edit_submode_extra);
10251 if (length > 0)
10252 {
10253 if (edit_submode_pre != NULL)
10254 length -= vim_strsize(edit_submode_pre);
10255 if (length - vim_strsize(edit_submode) > 0)
10256 {
10257 if (edit_submode_pre != NULL)
10258 msg_puts_attr(edit_submode_pre, attr);
10259 msg_puts_attr(edit_submode, attr);
10260 }
10261 if (edit_submode_extra != NULL)
10262 {
10263 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10264 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010265 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 else
10267 sub_attr = attr;
10268 msg_puts_attr(edit_submode_extra, sub_attr);
10269 }
10270 }
10271 length = 0;
10272 }
10273 else
10274#endif
10275 {
10276#ifdef FEAT_VREPLACE
10277 if (State & VREPLACE_FLAG)
10278 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10279 else
10280#endif
10281 if (State & REPLACE_FLAG)
10282 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10283 else if (State & INSERT)
10284 {
10285#ifdef FEAT_RIGHTLEFT
10286 if (p_ri)
10287 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10288#endif
10289 MSG_PUTS_ATTR(_(" INSERT"), attr);
10290 }
10291 else if (restart_edit == 'I')
10292 MSG_PUTS_ATTR(_(" (insert)"), attr);
10293 else if (restart_edit == 'R')
10294 MSG_PUTS_ATTR(_(" (replace)"), attr);
10295 else if (restart_edit == 'V')
10296 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10297#ifdef FEAT_RIGHTLEFT
10298 if (p_hkmap)
10299 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10300# ifdef FEAT_FKMAP
10301 if (p_fkmap)
10302 MSG_PUTS_ATTR(farsi_text_5, attr);
10303# endif
10304#endif
10305#ifdef FEAT_KEYMAP
10306 if (State & LANGMAP)
10307 {
10308# ifdef FEAT_ARABIC
10309 if (curwin->w_p_arab)
10310 MSG_PUTS_ATTR(_(" Arabic"), attr);
10311 else
10312# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010313 if (get_keymap_str(curwin, (char_u *)" (%s)",
10314 NameBuff, MAXPATHL))
10315 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 }
10317#endif
10318 if ((State & INSERT) && p_paste)
10319 MSG_PUTS_ATTR(_(" (paste)"), attr);
10320
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 if (VIsual_active)
10322 {
10323 char *p;
10324
10325 /* Don't concatenate separate words to avoid translation
10326 * problems. */
10327 switch ((VIsual_select ? 4 : 0)
10328 + (VIsual_mode == Ctrl_V) * 2
10329 + (VIsual_mode == 'V'))
10330 {
10331 case 0: p = N_(" VISUAL"); break;
10332 case 1: p = N_(" VISUAL LINE"); break;
10333 case 2: p = N_(" VISUAL BLOCK"); break;
10334 case 4: p = N_(" SELECT"); break;
10335 case 5: p = N_(" SELECT LINE"); break;
10336 default: p = N_(" SELECT BLOCK"); break;
10337 }
10338 MSG_PUTS_ATTR(_(p), attr);
10339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 MSG_PUTS_ATTR(" --", attr);
10341 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010342
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 need_clear = TRUE;
10344 }
10345 if (Recording
10346#ifdef FEAT_INS_EXPAND
10347 && edit_submode == NULL /* otherwise it gets too long */
10348#endif
10349 )
10350 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010351 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 need_clear = TRUE;
10353 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010354
10355 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 if (need_clear || clear_cmdline)
10357 msg_clr_eos();
10358 msg_didout = FALSE; /* overwrite this message */
10359 length = msg_col;
10360 msg_col = 0;
10361 need_wait_return = nwr_save; /* never ask for hit-return for this */
10362 }
10363 else if (clear_cmdline && msg_silent == 0)
10364 /* Clear the whole command line. Will reset "clear_cmdline". */
10365 msg_clr_cmdline();
10366
10367#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 /* In Visual mode the size of the selected area must be redrawn. */
10369 if (VIsual_active)
10370 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371
10372 /* If the last window has no status line, the ruler is after the mode
10373 * message and must be redrawn */
10374 if (redrawing()
10375# ifdef FEAT_WINDOWS
10376 && lastwin->w_status_height == 0
10377# endif
10378 )
10379 win_redr_ruler(lastwin, TRUE);
10380#endif
10381 redraw_cmdline = FALSE;
10382 clear_cmdline = FALSE;
10383
10384 return length;
10385}
10386
10387/*
10388 * Position for a mode message.
10389 */
10390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010391msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 msg_col = 0;
10394 msg_row = Rows - 1;
10395}
10396
10397/*
10398 * Delete mode message. Used when ESC is typed which is expected to end
10399 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010400 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 */
10402 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010403unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
10405 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010406 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 */
10408 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10409 redraw_cmdline = TRUE; /* delete mode later */
10410 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010411 clearmode();
10412}
10413
10414/*
10415 * Clear the mode message.
10416 */
10417 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010418clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010419{
10420 msg_pos_mode();
10421 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010422 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010423 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424}
10425
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010427recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010428{
10429 MSG_PUTS_ATTR(_("recording"), attr);
10430 if (!shortmess(SHM_RECORDING))
10431 {
10432 char_u s[4];
10433 sprintf((char *)s, " @%c", Recording);
10434 MSG_PUTS_ATTR(s, attr);
10435 }
10436}
10437
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010438#if defined(FEAT_WINDOWS)
10439/*
10440 * Draw the tab pages line at the top of the Vim window.
10441 */
10442 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010443draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010444{
10445 int tabcount = 0;
10446 tabpage_T *tp;
10447 int tabwidth;
10448 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010449 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010450 int attr;
10451 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010452 win_T *cwp;
10453 int wincount;
10454 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010455 int c;
10456 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010457 int attr_sel = HL_ATTR(HLF_TPS);
10458 int attr_nosel = HL_ATTR(HLF_TP);
10459 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010460 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010461 int room;
10462 int use_sep_chars = (t_colors < 8
10463#ifdef FEAT_GUI
10464 && !gui.in_use
10465#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010466#ifdef FEAT_TERMGUICOLORS
10467 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010468#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010469 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010470
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010471 if (ScreenLines == NULL)
10472 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010473 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010474
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010475#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010476 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010477 if (gui_use_tabline())
10478 {
10479 gui_update_tabline();
10480 return;
10481 }
10482#endif
10483
10484 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010485 return;
10486
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010487#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010488
10489 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10490 for (scol = 0; scol < Columns; ++scol)
10491 TabPageIdxs[scol] = 0;
10492
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010493 /* Use the 'tabline' option if it's set. */
10494 if (*p_tal != NUL)
10495 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010496 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497
10498 /* Check for an error. If there is one we would loop in redrawing the
10499 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010500 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010501 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010502 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010503 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010504 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010505 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010506 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010507 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010508#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010509 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010510 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010511 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010512
Bram Moolenaar238a5642006-02-21 22:12:05 +000010513 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10514 if (tabwidth < 6)
10515 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010516
Bram Moolenaar238a5642006-02-21 22:12:05 +000010517 attr = attr_nosel;
10518 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010519 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010520 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10521 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010522 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010524
Bram Moolenaar238a5642006-02-21 22:12:05 +000010525 if (tp->tp_topframe == topframe)
10526 attr = attr_sel;
10527 if (use_sep_chars && col > 0)
10528 screen_putchar('|', 0, col++, attr);
10529
10530 if (tp->tp_topframe != topframe)
10531 attr = attr_nosel;
10532
10533 screen_putchar(' ', 0, col++, attr);
10534
10535 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010536 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010537 cwp = curwin;
10538 wp = firstwin;
10539 }
10540 else
10541 {
10542 cwp = tp->tp_curwin;
10543 wp = tp->tp_firstwin;
10544 }
10545
10546 modified = FALSE;
10547 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10548 if (bufIsChanged(wp->w_buffer))
10549 modified = TRUE;
10550 if (modified || wincount > 1)
10551 {
10552 if (wincount > 1)
10553 {
10554 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010555 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010556 if (col + len >= Columns - 3)
10557 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010559#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010560 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010561#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010562 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010563#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010564 );
10565 col += len;
10566 }
10567 if (modified)
10568 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10569 screen_putchar(' ', 0, col++, attr);
10570 }
10571
10572 room = scol - col + tabwidth - 1;
10573 if (room > 0)
10574 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010575 /* Get buffer name in NameBuff[] */
10576 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010577 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010578 len = vim_strsize(NameBuff);
10579 p = NameBuff;
10580#ifdef FEAT_MBYTE
10581 if (has_mbyte)
10582 while (len > room)
10583 {
10584 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010585 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010586 }
10587 else
10588#endif
10589 if (len > room)
10590 {
10591 p += len - room;
10592 len = room;
10593 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010594 if (len > Columns - col - 1)
10595 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010597 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010598 col += len;
10599 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010600 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601
10602 /* Store the tab page number in TabPageIdxs[], so that
10603 * jump_to_mouse() knows where each one is. */
10604 ++tabcount;
10605 while (scol < col)
10606 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010607 }
10608
Bram Moolenaar238a5642006-02-21 22:12:05 +000010609 if (use_sep_chars)
10610 c = '_';
10611 else
10612 c = ' ';
10613 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010614
10615 /* Put an "X" for closing the current tab if there are several. */
10616 if (first_tabpage->tp_next != NULL)
10617 {
10618 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10619 TabPageIdxs[Columns - 1] = -999;
10620 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010621 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010622
10623 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10624 * set. */
10625 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010627
10628/*
10629 * Get buffer name for "buf" into NameBuff[].
10630 * Takes care of special buffer names and translates special characters.
10631 */
10632 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010633get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010634{
10635 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010636 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010637 else
10638 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10639 trans_characters(NameBuff, MAXPATHL);
10640}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010641#endif
10642
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10644/*
10645 * Get the character to use in a status line. Get its attributes in "*attr".
10646 */
10647 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010648fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
10650 int fill;
10651 if (is_curwin)
10652 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010653 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 fill = fill_stl;
10655 }
10656 else
10657 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 fill = fill_stlnc;
10660 }
10661 /* Use fill when there is highlighting, and highlighting of current
10662 * window differs, or the fillchars differ, or this is not the
10663 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010664 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010665 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 || (fill_stl != fill_stlnc)))
10667 return fill;
10668 if (is_curwin)
10669 return '^';
10670 return '=';
10671}
10672#endif
10673
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010674#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675/*
10676 * Get the character to use in a separator between vertically split windows.
10677 * Get its attributes in "*attr".
10678 */
10679 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010680fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010682 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 if (*attr == 0 && fill_vert == ' ')
10684 return '|';
10685 else
10686 return fill_vert;
10687}
10688#endif
10689
10690/*
10691 * Return TRUE if redrawing should currently be done.
10692 */
10693 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010694redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010696#ifdef FEAT_EVAL
10697 if (disable_redraw_for_testing)
10698 return 0;
10699 else
10700#endif
10701 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10703}
10704
10705/*
10706 * Return TRUE if printing messages should currently be done.
10707 */
10708 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010709messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711 return (!(p_lz && char_avail() && !KeyTyped));
10712}
10713
10714/*
10715 * Show current status info in ruler and various other places
10716 * If always is FALSE, only show ruler if position has changed.
10717 */
10718 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010719showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
10721 if (!always && !redrawing())
10722 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010723#ifdef FEAT_INS_EXPAND
10724 if (pum_visible())
10725 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010726# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010727 /* Don't redraw right now, do it later. */
10728 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010729# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010730 return;
10731 }
10732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010734 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010736 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 else
10739#endif
10740#ifdef FEAT_CMDL_INFO
10741 win_redr_ruler(curwin, always);
10742#endif
10743
10744#ifdef FEAT_TITLE
10745 if (need_maketitle
10746# ifdef FEAT_STL_OPT
10747 || (p_icon && (stl_syntax & STL_IN_ICON))
10748 || (p_title && (stl_syntax & STL_IN_TITLE))
10749# endif
10750 )
10751 maketitle();
10752#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010753#ifdef FEAT_WINDOWS
10754 /* Redraw the tab pages line if needed. */
10755 if (redraw_tabline)
10756 draw_tabline();
10757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758}
10759
10760#ifdef FEAT_CMDL_INFO
10761 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010762win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010764#define RULER_BUF_LEN 70
10765 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 int row;
10767 int fillchar;
10768 int attr;
10769 int empty_line = FALSE;
10770 colnr_T virtcol;
10771 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010772 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010774#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 int this_ru_col;
10776 int off = 0;
10777 int width = Columns;
10778# define WITH_OFF(x) x
10779# define WITH_WIDTH(x) x
10780#else
10781# define WITH_OFF(x) 0
10782# define WITH_WIDTH(x) Columns
10783# define this_ru_col ru_col
10784#endif
10785
10786 /* If 'ruler' off or redrawing disabled, don't do anything */
10787 if (!p_ru)
10788 return;
10789
10790 /*
10791 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10792 * after deleting lines, before cursor.lnum is corrected.
10793 */
10794 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10795 return;
10796
10797#ifdef FEAT_INS_EXPAND
10798 /* Don't draw the ruler while doing insert-completion, it might overwrite
10799 * the (long) mode message. */
10800# ifdef FEAT_WINDOWS
10801 if (wp == lastwin && lastwin->w_status_height == 0)
10802# endif
10803 if (edit_submode != NULL)
10804 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010805 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10806 if (pum_visible())
10807 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808#endif
10809
10810#ifdef FEAT_STL_OPT
10811 if (*p_ruf)
10812 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 int save_called_emsg = called_emsg;
10814
10815 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010817 if (called_emsg)
10818 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010819 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 return;
10822 }
10823#endif
10824
10825 /*
10826 * Check if not in Insert mode and the line is empty (will show "0-1").
10827 */
10828 if (!(State & INSERT)
10829 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10830 empty_line = TRUE;
10831
10832 /*
10833 * Only draw the ruler when something changed.
10834 */
10835 validate_virtcol_win(wp);
10836 if ( redraw_cmdline
10837 || always
10838 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10839 || wp->w_cursor.col != wp->w_ru_cursor.col
10840 || wp->w_virtcol != wp->w_ru_virtcol
10841#ifdef FEAT_VIRTUALEDIT
10842 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10843#endif
10844 || wp->w_topline != wp->w_ru_topline
10845 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10846#ifdef FEAT_DIFF
10847 || wp->w_topfill != wp->w_ru_topfill
10848#endif
10849 || empty_line != wp->w_ru_empty)
10850 {
10851 cursor_off();
10852#ifdef FEAT_WINDOWS
10853 if (wp->w_status_height)
10854 {
10855 row = W_WINROW(wp) + wp->w_height;
10856 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 off = W_WINCOL(wp);
10858 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 }
10860 else
10861#endif
10862 {
10863 row = Rows - 1;
10864 fillchar = ' ';
10865 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010866#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 width = Columns;
10868 off = 0;
10869#endif
10870 }
10871
10872 /* In list mode virtcol needs to be recomputed */
10873 virtcol = wp->w_virtcol;
10874 if (wp->w_p_list && lcs_tab1 == NUL)
10875 {
10876 wp->w_p_list = FALSE;
10877 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10878 wp->w_p_list = TRUE;
10879 }
10880
10881 /*
10882 * Some sprintfs return the length, some return a pointer.
10883 * To avoid portability problems we use strlen() here.
10884 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010885 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10887 ? 0L
10888 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010889 len = STRLEN(buffer);
10890 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10892 (int)virtcol + 1);
10893
10894 /*
10895 * Add a "50%" if there is room for it.
10896 * On the last line, don't print in the last column (scrolls the
10897 * screen up on some terminals).
10898 */
10899 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010900 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 o = i + vim_strsize(buffer + i + 1);
10902#ifdef FEAT_WINDOWS
10903 if (wp->w_status_height == 0) /* can't use last char of screen */
10904#endif
10905 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010906#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 this_ru_col = ru_col - (Columns - width);
10908 if (this_ru_col < 0)
10909 this_ru_col = 0;
10910#endif
10911 /* Never use more than half the window/screen width, leave the other
10912 * half for the filename. */
10913 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10914 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10915 if (this_ru_col + o < WITH_WIDTH(width))
10916 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010917 /* need at least 3 chars left for get_rel_pos() + NUL */
10918 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
10920#ifdef FEAT_MBYTE
10921 if (has_mbyte)
10922 i += (*mb_char2bytes)(fillchar, buffer + i);
10923 else
10924#endif
10925 buffer[i++] = fillchar;
10926 ++o;
10927 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010928 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 }
10930 /* Truncate at window boundary. */
10931#ifdef FEAT_MBYTE
10932 if (has_mbyte)
10933 {
10934 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010935 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 {
10937 o += (*mb_ptr2cells)(buffer + i);
10938 if (this_ru_col + o > WITH_WIDTH(width))
10939 {
10940 buffer[i] = NUL;
10941 break;
10942 }
10943 }
10944 }
10945 else
10946#endif
10947 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10948 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10949
10950 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10951 i = redraw_cmdline;
10952 screen_fill(row, row + 1,
10953 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10954 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10955 fillchar, fillchar, attr);
10956 /* don't redraw the cmdline because of showing the ruler */
10957 redraw_cmdline = i;
10958 wp->w_ru_cursor = wp->w_cursor;
10959 wp->w_ru_virtcol = wp->w_virtcol;
10960 wp->w_ru_empty = empty_line;
10961 wp->w_ru_topline = wp->w_topline;
10962 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10963#ifdef FEAT_DIFF
10964 wp->w_ru_topfill = wp->w_topfill;
10965#endif
10966 }
10967}
10968#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010969
10970#if defined(FEAT_LINEBREAK) || defined(PROTO)
10971/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010972 * Return the width of the 'number' and 'relativenumber' column.
10973 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010974 * Otherwise it depends on 'numberwidth' and the line count.
10975 */
10976 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010977number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010978{
10979 int n;
10980 linenr_T lnum;
10981
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010982 if (wp->w_p_rnu && !wp->w_p_nu)
10983 /* cursor line shows "0" */
10984 lnum = wp->w_height;
10985 else
10986 /* cursor line shows absolute line number */
10987 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010988
Bram Moolenaar6b314672015-03-20 15:42:10 +010010989 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010990 return wp->w_nrwidth_width;
10991 wp->w_nrwidth_line_count = lnum;
10992
10993 n = 0;
10994 do
10995 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010996 lnum /= 10;
10997 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010998 } while (lnum > 0);
10999
11000 /* 'numberwidth' gives the minimal width plus one */
11001 if (n < wp->w_p_nuw - 1)
11002 n = wp->w_p_nuw - 1;
11003
11004 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011005 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011006 return n;
11007}
11008#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011009
11010/*
11011 * Return the current cursor column. This is the actual position on the
11012 * screen. First column is 0.
11013 */
11014 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011015screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011016{
11017 return screen_cur_col;
11018}
11019
11020/*
11021 * Return the current cursor row. This is the actual position on the screen.
11022 * First row is 0.
11023 */
11024 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011025screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011026{
11027 return screen_cur_row;
11028}