blob: 3b281a89423e5058867bfa185e7235959036cbdf [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
278 FOR_ALL_WINDOWS(wp)
279 {
280 if (wp->w_buffer == buf)
281 {
282 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200283#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200285#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200286 }
287 }
288}
289
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 * Redraw as soon as possible. When the command line is not scrolled redraw
292 * right away and restore what was on the command line.
293 * Return a code indicating what happened.
294 */
295 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100296redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297{
298 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200299 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 int r;
301 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200302 schar_T *screenline; /* copy from ScreenLines[] */
303 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304#ifdef FEAT_MBYTE
305 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200306 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309#endif
310
311 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200312 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 return ret;
314
315 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200316 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200318 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200319 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 if (screenline == NULL || screenattr == NULL)
322 ret = 2;
323#ifdef FEAT_MBYTE
324 if (enc_utf8)
325 {
326 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 if (screenlineUC == NULL)
329 ret = 2;
330 for (i = 0; i < p_mco; ++i)
331 {
332 screenlineC[i] = (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 (screenlineC[i] == NULL)
335 ret = 2;
336 }
337 }
338 if (enc_dbcs == DBCS_JPNU)
339 {
340 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenline2 == NULL)
343 ret = 2;
344 }
345#endif
346
347 if (ret != 2)
348 {
349 /* Save the text displayed in the command line area. */
350 for (r = 0; r < rows; ++r)
351 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200352 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (size_t)cols * sizeof(schar_T));
355 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200356 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200357 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358#ifdef FEAT_MBYTE
359 if (enc_utf8)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenlineC[i] + r * cols,
366 ScreenLinesC[i] + LineOffset[cmdline_row + r],
367 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 }
369 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373#endif
374 }
375
376 update_screen(0);
377 ret = 3;
378
379 if (must_redraw == 0)
380 {
381 int off = (int)(current_ScreenLine - ScreenLines);
382
383 /* Restore the text displayed in the command line area. */
384 for (r = 0; r < rows; ++r)
385 {
386 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenline + r * cols,
388 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200390 screenattr + r * cols,
391 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200392#ifdef FEAT_MBYTE
393 if (enc_utf8)
394 {
395 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenlineUC + r * cols,
397 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 for (i = 0; i < p_mco; ++i)
399 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineC[i] + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403 if (enc_dbcs == DBCS_JPNU)
404 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenline2 + r * cols,
406 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200408 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 }
410 ret = 4;
411 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413
414 vim_free(screenline);
415 vim_free(screenattr);
416#ifdef FEAT_MBYTE
417 if (enc_utf8)
418 {
419 vim_free(screenlineUC);
420 for (i = 0; i < p_mco; ++i)
421 vim_free(screenlineC[i]);
422 }
423 if (enc_dbcs == DBCS_JPNU)
424 vim_free(screenline2);
425#endif
426
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200427 /* Show the intro message when appropriate. */
428 maybe_intro_message();
429
430 setcursor();
431
Bram Moolenaar2951b772013-07-03 12:45:31 +0200432 return ret;
433}
434
435/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100436 * Invoked after an asynchronous callback is called.
437 * If an echo command was used the cursor needs to be put back where
438 * it belongs. If highlighting was changed a redraw is needed.
439 */
440 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200441redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100443 if (State == HITRETURN || State == ASKMORE)
444 ; /* do nothing */
445 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200446 {
447 /* Redrawing only works when the screen didn't scroll. */
448 if (msg_scrolled == 0)
449 {
450 update_screen(0);
451 compute_cmdrow();
452 }
453 else
454 {
455 /* Redraw in the same position, so that the user can continue
456 * editing the command. */
457 compute_cmdrow();
458 if (cmdline_row > msg_scrolled)
459 cmdline_row -= msg_scrolled;
460 else
461 cmdline_row = 0;
462 }
463 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;
651 }
652
653 if (clear_cmdline) /* going to clear cmdline (done below) */
654 check_for_delay(FALSE);
655
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000656#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200657 /* Force redraw when width of 'number' or 'relativenumber' column
658 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000659 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200660 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
661 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000662 curwin->w_redr_type = NOT_VALID;
663#endif
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 /*
666 * Only start redrawing if there is really something to do.
667 */
668 if (type == INVERTED)
669 update_curswant();
670 if (curwin->w_redr_type < type
671 && !((type == VALID
672 && curwin->w_lines[0].wl_valid
673#ifdef FEAT_DIFF
674 && curwin->w_topfill == curwin->w_old_topfill
675 && curwin->w_botfill == curwin->w_old_botfill
676#endif
677 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000679 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
681 && curwin->w_old_visual_mode == VIsual_mode
682 && (curwin->w_valid & VALID_VIRTCOL)
683 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 ))
685 curwin->w_redr_type = type;
686
Bram Moolenaar5a305422006-04-28 22:38:25 +0000687#ifdef FEAT_WINDOWS
688 /* Redraw the tab pages line if needed. */
689 if (redraw_tabline || type >= NOT_VALID)
690 draw_tabline();
691#endif
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_SYN_HL
694 /*
695 * Correct stored syntax highlighting info for changes in each displayed
696 * buffer. Each buffer must only be done once.
697 */
698 FOR_ALL_WINDOWS(wp)
699 {
700 if (wp->w_buffer->b_mod_set)
701 {
702# ifdef FEAT_WINDOWS
703 win_T *wwp;
704
705 /* Check if we already did this buffer. */
706 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
707 if (wwp->w_buffer == wp->w_buffer)
708 break;
709# endif
710 if (
711# ifdef FEAT_WINDOWS
712 wwp == wp &&
713# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200714 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 syn_stack_apply_changes(wp->w_buffer);
716 }
717 }
718#endif
719
720 /*
721 * Go from top to bottom through the windows, redrawing the ones that need
722 * it.
723 */
724#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
725 did_one = FALSE;
726#endif
727#ifdef FEAT_SEARCH_EXTRA
728 search_hl.rm.regprog = NULL;
729#endif
730 FOR_ALL_WINDOWS(wp)
731 {
732 if (wp->w_redr_type != 0)
733 {
734 cursor_off();
735#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
736 if (!did_one)
737 {
738 did_one = TRUE;
739# ifdef FEAT_SEARCH_EXTRA
740 start_search_hl();
741# endif
742# ifdef FEAT_CLIPBOARD
743 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200744 if (clip_star.available && clip_isautosel_star())
745 clip_update_selection(&clip_star);
746 if (clip_plus.available && clip_isautosel_plus())
747 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748# endif
749#ifdef FEAT_GUI
750 /* Remove the cursor before starting to do anything, because
751 * scrolling may make it difficult to redraw the text under
752 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200753 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200754 {
755 gui_cursor_col = gui.cursor_col;
756 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200758 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760#endif
761 }
762#endif
763 win_update(wp);
764 }
765
766#ifdef FEAT_WINDOWS
767 /* redraw status line after the window to minimize cursor movement */
768 if (wp->w_redr_status)
769 {
770 cursor_off();
771 win_redr_status(wp);
772 }
773#endif
774 }
775#if defined(FEAT_SEARCH_EXTRA)
776 end_search_hl();
777#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100778#ifdef FEAT_INS_EXPAND
779 /* May need to redraw the popup menu. */
780 if (pum_visible())
781 pum_redraw();
782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784#ifdef FEAT_WINDOWS
785 /* Reset b_mod_set flags. Going through all windows is probably faster
786 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200787 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 wp->w_buffer->b_mod_set = FALSE;
789#else
790 curbuf->b_mod_set = FALSE;
791#endif
792
793 updating_screen = FALSE;
794#ifdef FEAT_GUI
795 gui_may_resize_shell();
796#endif
797
798 /* Clear or redraw the command line. Done last, because scrolling may
799 * mess up the command line. */
800 if (clear_cmdline || redraw_cmdline)
801 showmode();
802
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200803 if (no_update)
804 --no_win_do_lines_ins;
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200807 if (!did_intro)
808 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 did_intro = TRUE;
810
811#ifdef FEAT_GUI
812 /* Redraw the cursor and update the scrollbars when all screen updating is
813 * done. */
814 if (gui.in_use)
815 {
816 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200817 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200818 {
819 /* Put the GUI position where the cursor was, gui_update_cursor()
820 * uses that. */
821 gui.col = gui_cursor_col;
822 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200823# ifdef FEAT_MBYTE
824 gui.col = mb_fix_col(gui.col, gui.row);
825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200827 screen_cur_col = gui.col;
828 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui_update_scrollbars(FALSE);
831 }
832#endif
833}
834
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100835#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
836/*
837 * Prepare for updating one or more windows.
838 * Caller must check for "updating_screen" already set to avoid recursiveness.
839 */
840 static void
841update_prepare(void)
842{
843 cursor_off();
844 updating_screen = TRUE;
845#ifdef FEAT_GUI
846 /* Remove the cursor before starting to do anything, because scrolling may
847 * make it difficult to redraw the text under it. */
848 if (gui.in_use)
849 gui_undraw_cursor();
850#endif
851#ifdef FEAT_SEARCH_EXTRA
852 start_search_hl();
853#endif
854}
855
856/*
857 * Finish updating one or more windows.
858 */
859 static void
860update_finish(void)
861{
862 if (redraw_cmdline)
863 showmode();
864
865# ifdef FEAT_SEARCH_EXTRA
866 end_search_hl();
867# endif
868
869 updating_screen = FALSE;
870
871# ifdef FEAT_GUI
872 gui_may_resize_shell();
873
874 /* Redraw the cursor and update the scrollbars when all screen updating is
875 * done. */
876 if (gui.in_use)
877 {
878 out_flush(); /* required before updating the cursor */
879 gui_update_cursor(FALSE, FALSE);
880 gui_update_scrollbars(FALSE);
881 }
882# endif
883}
884#endif
885
Bram Moolenaar860cae12010-06-05 23:22:07 +0200886#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200887/*
888 * Return TRUE if the cursor line in window "wp" may be concealed, according
889 * to the 'concealcursor' option.
890 */
891 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100892conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200893{
894 int c;
895
896 if (*wp->w_p_cocu == NUL)
897 return FALSE;
898 if (get_real_state() & VISUAL)
899 c = 'v';
900 else if (State & INSERT)
901 c = 'i';
902 else if (State & NORMAL)
903 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200904 else if (State & CMDLINE)
905 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906 else
907 return FALSE;
908 return vim_strchr(wp->w_p_cocu, c) != NULL;
909}
910
911/*
912 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
913 */
914 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100915conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916{
917 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
918 {
919 need_cursor_line_redraw = TRUE;
920 /* Need to recompute cursor column, e.g., when starting Visual mode
921 * without concealing. */
922 curs_columns(TRUE);
923 }
924}
925
Bram Moolenaar860cae12010-06-05 23:22:07 +0200926 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100927update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928{
929 int row;
930 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200931#ifdef SYN_TIME_LIMIT
932 proftime_T syntax_tm;
933#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200934
Bram Moolenaar908be432016-05-24 10:51:30 +0200935 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100936 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200937 return;
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200940 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200942#ifdef SYN_TIME_LIMIT
943 /* Set the time limit to 'redrawtime'. */
944 profile_setlimit(p_rdt, &syntax_tm);
945#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100946 update_prepare();
947
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 row = 0;
949 for (j = 0; j < wp->w_lines_valid; ++j)
950 {
951 if (lnum == wp->w_lines[j].wl_lnum)
952 {
953 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200954# ifdef FEAT_SEARCH_EXTRA
955 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200956 start_search_hl();
957 prepare_search_hl(wp, lnum);
958# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200959 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
960#ifdef SYN_TIME_LIMIT
961 &syntax_tm
962#else
963 NULL
964#endif
965 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200966# if defined(FEAT_SEARCH_EXTRA)
967 end_search_hl();
968# endif
969 break;
970 }
971 row += wp->w_lines[j].wl_size;
972 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100973
974 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200975 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200976 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978#endif
979
980#if defined(FEAT_SIGNS) || defined(PROTO)
981 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100982update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983{
984 win_T *wp;
985 int doit = FALSE;
986
987# ifdef FEAT_FOLDING
988 win_foldinfo.fi_level = 0;
989# endif
990
991 /* update/delete a specific mark */
992 FOR_ALL_WINDOWS(wp)
993 {
994 if (buf != NULL && lnum > 0)
995 {
996 if (wp->w_buffer == buf && lnum >= wp->w_topline
997 && lnum < wp->w_botline)
998 {
999 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1000 wp->w_redraw_top = lnum;
1001 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1002 wp->w_redraw_bot = lnum;
1003 redraw_win_later(wp, VALID);
1004 }
1005 }
1006 else
1007 redraw_win_later(wp, VALID);
1008 if (wp->w_redr_type != 0)
1009 doit = TRUE;
1010 }
1011
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001012 /* Return when there is nothing to do, screen updating is already
1013 * happening (recursive call) or still starting up. */
1014 if (!doit || updating_screen
1015#ifdef FEAT_GUI
1016 || gui.starting
1017#endif
1018 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 return;
1020
1021 /* update all windows that need updating */
1022 update_prepare();
1023
1024# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001025 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
1027 if (wp->w_redr_type != 0)
1028 win_update(wp);
1029 if (wp->w_redr_status)
1030 win_redr_status(wp);
1031 }
1032# else
1033 if (curwin->w_redr_type != 0)
1034 win_update(curwin);
1035# endif
1036
1037 update_finish();
1038}
1039#endif
1040
1041
1042#if defined(FEAT_GUI) || defined(PROTO)
1043/*
1044 * Update a single window, its status line and maybe the command line msg.
1045 * Used for the GUI scrollbar.
1046 */
1047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001048updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001050 /* return if already busy updating */
1051 if (updating_screen)
1052 return;
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 update_prepare();
1055
1056#ifdef FEAT_CLIPBOARD
1057 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001058 if (clip_star.available && clip_isautosel_star())
1059 clip_update_selection(&clip_star);
1060 if (clip_plus.available && clip_isautosel_plus())
1061 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001068 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001069 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (wp->w_redr_status
1072# ifdef FEAT_CMDL_INFO
1073 || p_ru
1074# endif
1075# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001076 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077# endif
1078 )
1079 win_redr_status(wp);
1080#endif
1081
1082 update_finish();
1083}
1084#endif
1085
1086/*
1087 * Update a single window.
1088 *
1089 * This may cause the windows below it also to be redrawn (when clearing the
1090 * screen or scrolling lines).
1091 *
1092 * How the window is redrawn depends on wp->w_redr_type. Each type also
1093 * implies the one below it.
1094 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001095 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1097 * INVERTED redraw the changed part of the Visual area
1098 * INVERTED_ALL redraw the whole Visual area
1099 * VALID 1. scroll up/down to adjust for a changed w_topline
1100 * 2. update lines at the top when scrolled down
1101 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001102 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 * b_mod_top and b_mod_bot.
1104 * - if wp->w_redraw_top non-zero, redraw lines between
1105 * wp->w_redraw_top and wp->w_redr_bot.
1106 * - continue redrawing when syntax status is invalid.
1107 * 4. if scrolled up, update lines at the bottom.
1108 * This results in three areas that may need updating:
1109 * top: from first row to top_end (when scrolled down)
1110 * mid: from mid_start to mid_end (update inversion or changed text)
1111 * bot: from bot_start to last row (when scrolled up)
1112 */
1113 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001114win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 buf_T *buf = wp->w_buffer;
1117 int type;
1118 int top_end = 0; /* Below last row of the top area that needs
1119 updating. 0 when no top area updating. */
1120 int mid_start = 999;/* first row of the mid area that needs
1121 updating. 999 when no mid area updating. */
1122 int mid_end = 0; /* Below last row of the mid area that needs
1123 updating. 0 when no mid area updating. */
1124 int bot_start = 999;/* first row of the bot area that needs
1125 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 int scrolled_down = FALSE; /* TRUE when scrolled down when
1127 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001129 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int top_to_mod = FALSE; /* redraw above mod_top */
1131#endif
1132
1133 int row; /* current window row to display */
1134 linenr_T lnum; /* current buffer lnum to display */
1135 int idx; /* current index in w_lines[] */
1136 int srow; /* starting row of the current line */
1137
1138 int eof = FALSE; /* if TRUE, we hit the end of the file */
1139 int didline = FALSE; /* if TRUE, we finished the last line */
1140 int i;
1141 long j;
1142 static int recursive = FALSE; /* being called recursively */
1143 int old_botline = wp->w_botline;
1144#ifdef FEAT_FOLDING
1145 long fold_count;
1146#endif
1147#ifdef FEAT_SYN_HL
1148 /* remember what happened to the previous line, to know if
1149 * check_visual_highlight() can be used */
1150#define DID_NONE 1 /* didn't update a line */
1151#define DID_LINE 2 /* updated a normal line */
1152#define DID_FOLD 3 /* updated a folded line */
1153 int did_update = DID_NONE;
1154 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1155#endif
1156 linenr_T mod_top = 0;
1157 linenr_T mod_bot = 0;
1158#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1159 int save_got_int;
1160#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001161#ifdef SYN_TIME_LIMIT
1162 proftime_T syntax_tm;
1163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 type = wp->w_redr_type;
1166
1167 if (type == NOT_VALID)
1168 {
1169#ifdef FEAT_WINDOWS
1170 wp->w_redr_status = TRUE;
1171#endif
1172 wp->w_lines_valid = 0;
1173 }
1174
1175 /* Window is zero-height: nothing to draw. */
1176 if (wp->w_height == 0)
1177 {
1178 wp->w_redr_type = 0;
1179 return;
1180 }
1181
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001182#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 /* Window is zero-width: Only need to draw the separator. */
1184 if (wp->w_width == 0)
1185 {
1186 /* draw the vertical separator right of this window */
1187 draw_vsep_win(wp, 0);
1188 wp->w_redr_type = 0;
1189 return;
1190 }
1191#endif
1192
1193#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001194 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001197#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001198 /* Force redraw when width of 'number' or 'relativenumber' column
1199 * changes. */
1200 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001201 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001202 {
1203 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001204 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001205 }
1206 else
1207#endif
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1210 {
1211 /*
1212 * When there are both inserted/deleted lines and specific lines to be
1213 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1214 * everything (only happens when redrawing is off for while).
1215 */
1216 type = NOT_VALID;
1217 }
1218 else
1219 {
1220 /*
1221 * Set mod_top to the first line that needs displaying because of
1222 * changes. Set mod_bot to the first line after the changes.
1223 */
1224 mod_top = wp->w_redraw_top;
1225 if (wp->w_redraw_bot != 0)
1226 mod_bot = wp->w_redraw_bot + 1;
1227 else
1228 mod_bot = 0;
1229 wp->w_redraw_top = 0; /* reset for next time */
1230 wp->w_redraw_bot = 0;
1231 if (buf->b_mod_set)
1232 {
1233 if (mod_top == 0 || mod_top > buf->b_mod_top)
1234 {
1235 mod_top = buf->b_mod_top;
1236#ifdef FEAT_SYN_HL
1237 /* Need to redraw lines above the change that may be included
1238 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001239 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (mod_top < 1)
1243 mod_top = 1;
1244 }
1245#endif
1246 }
1247 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1248 mod_bot = buf->b_mod_bot;
1249
1250#ifdef FEAT_SEARCH_EXTRA
1251 /* When 'hlsearch' is on and using a multi-line search pattern, a
1252 * change in one line may make the Search highlighting in a
1253 * previous line invalid. Simple solution: redraw all visible
1254 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001255 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001257 if (search_hl.rm.regprog != NULL
1258 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001260 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001261 {
1262 cur = wp->w_match_head;
1263 while (cur != NULL)
1264 {
1265 if (cur->match.regprog != NULL
1266 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001267 {
1268 top_to_mod = TRUE;
1269 break;
1270 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001271 cur = cur->next;
1272 }
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#endif
1275 }
1276#ifdef FEAT_FOLDING
1277 if (mod_top != 0 && hasAnyFolding(wp))
1278 {
1279 linenr_T lnumt, lnumb;
1280
1281 /*
1282 * A change in a line can cause lines above it to become folded or
1283 * unfolded. Find the top most buffer line that may be affected.
1284 * If the line was previously folded and displayed, get the first
1285 * line of that fold. If the line is folded now, get the first
1286 * folded line. Use the minimum of these two.
1287 */
1288
1289 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1290 * the line below it. If there is no valid entry, use w_topline.
1291 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1292 * to this line. If there is no valid entry, use MAXLNUM. */
1293 lnumt = wp->w_topline;
1294 lnumb = MAXLNUM;
1295 for (i = 0; i < wp->w_lines_valid; ++i)
1296 if (wp->w_lines[i].wl_valid)
1297 {
1298 if (wp->w_lines[i].wl_lastlnum < mod_top)
1299 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1300 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1301 {
1302 lnumb = wp->w_lines[i].wl_lnum;
1303 /* When there is a fold column it might need updating
1304 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001305 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ++lnumb;
1307 }
1308 }
1309
1310 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1311 if (mod_top > lnumt)
1312 mod_top = lnumt;
1313
1314 /* Now do the same for the bottom line (one above mod_bot). */
1315 --mod_bot;
1316 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1317 ++mod_bot;
1318 if (mod_bot < lnumb)
1319 mod_bot = lnumb;
1320 }
1321#endif
1322
1323 /* When a change starts above w_topline and the end is below
1324 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325 * If the end of the change is above w_topline: do like no change was
1326 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (mod_top != 0 && mod_top < wp->w_topline)
1328 {
1329 if (mod_bot > wp->w_topline)
1330 mod_top = wp->w_topline;
1331#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001332 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 top_end = 1;
1334#endif
1335 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001336
1337 /* When line numbers are displayed need to redraw all lines below
1338 * inserted/deleted lines. */
1339 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1340 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 }
1342
1343 /*
1344 * When only displaying the lines at the top, set top_end. Used when
1345 * window has scrolled down for msg_scrolled.
1346 */
1347 if (type == REDRAW_TOP)
1348 {
1349 j = 0;
1350 for (i = 0; i < wp->w_lines_valid; ++i)
1351 {
1352 j += wp->w_lines[i].wl_size;
1353 if (j >= wp->w_upd_rows)
1354 {
1355 top_end = j;
1356 break;
1357 }
1358 }
1359 if (top_end == 0)
1360 /* not found (cannot happen?): redraw everything */
1361 type = NOT_VALID;
1362 else
1363 /* top area defined, the rest is VALID */
1364 type = VALID;
1365 }
1366
Bram Moolenaar367329b2007-08-30 11:53:22 +00001367 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001368 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1369 * non-zero and thus not FALSE) will indicate that screenclear() was not
1370 * called. */
1371 if (screen_cleared)
1372 screen_cleared = MAYBE;
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * If there are no changes on the screen that require a complete redraw,
1376 * handle three cases:
1377 * 1: we are off the top of the screen by a few lines: scroll down
1378 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1379 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1380 * w_lines[] that needs updating.
1381 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001382 if ((type == VALID || type == SOME_VALID
1383 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#ifdef FEAT_DIFF
1385 && !wp->w_botfill && !wp->w_old_botfill
1386#endif
1387 )
1388 {
1389 if (mod_top != 0 && wp->w_topline == mod_top)
1390 {
1391 /*
1392 * w_topline is the first changed line, the scrolling will be done
1393 * further down.
1394 */
1395 }
1396 else if (wp->w_lines[0].wl_valid
1397 && (wp->w_topline < wp->w_lines[0].wl_lnum
1398#ifdef FEAT_DIFF
1399 || (wp->w_topline == wp->w_lines[0].wl_lnum
1400 && wp->w_topfill > wp->w_old_topfill)
1401#endif
1402 ))
1403 {
1404 /*
1405 * New topline is above old topline: May scroll down.
1406 */
1407#ifdef FEAT_FOLDING
1408 if (hasAnyFolding(wp))
1409 {
1410 linenr_T ln;
1411
1412 /* count the number of lines we are off, counting a sequence
1413 * of folded lines as one */
1414 j = 0;
1415 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1416 {
1417 ++j;
1418 if (j >= wp->w_height - 2)
1419 break;
1420 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1421 }
1422 }
1423 else
1424#endif
1425 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1426 if (j < wp->w_height - 2) /* not too far off */
1427 {
1428 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1429#ifdef FEAT_DIFF
1430 /* insert extra lines for previously invisible filler lines */
1431 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1432 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1433 - wp->w_old_topfill;
1434#endif
1435 if (i < wp->w_height - 2) /* less than a screen off */
1436 {
1437 /*
1438 * Try to insert the correct number of lines.
1439 * If not the last window, delete the lines at the bottom.
1440 * win_ins_lines may fail when the terminal can't do it.
1441 */
1442 if (i > 0)
1443 check_for_delay(FALSE);
1444 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1445 {
1446 if (wp->w_lines_valid != 0)
1447 {
1448 /* Need to update rows that are new, stop at the
1449 * first one that scrolled down. */
1450 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
1453 /* Move the entries that were scrolled, disable
1454 * the entries for the lines to be redrawn. */
1455 if ((wp->w_lines_valid += j) > wp->w_height)
1456 wp->w_lines_valid = wp->w_height;
1457 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1458 wp->w_lines[idx] = wp->w_lines[idx - j];
1459 while (idx >= 0)
1460 wp->w_lines[idx--].wl_valid = FALSE;
1461 }
1462 }
1463 else
1464 mid_start = 0; /* redraw all lines */
1465 }
1466 else
1467 mid_start = 0; /* redraw all lines */
1468 }
1469 else
1470 mid_start = 0; /* redraw all lines */
1471 }
1472 else
1473 {
1474 /*
1475 * New topline is at or below old topline: May scroll up.
1476 * When topline didn't change, find first entry in w_lines[] that
1477 * needs updating.
1478 */
1479
1480 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1481 j = -1;
1482 row = 0;
1483 for (i = 0; i < wp->w_lines_valid; i++)
1484 {
1485 if (wp->w_lines[i].wl_valid
1486 && wp->w_lines[i].wl_lnum == wp->w_topline)
1487 {
1488 j = i;
1489 break;
1490 }
1491 row += wp->w_lines[i].wl_size;
1492 }
1493 if (j == -1)
1494 {
1495 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1496 * lines */
1497 mid_start = 0;
1498 }
1499 else
1500 {
1501 /*
1502 * Try to delete the correct number of lines.
1503 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1504 */
1505#ifdef FEAT_DIFF
1506 /* If the topline didn't change, delete old filler lines,
1507 * otherwise delete filler lines of the new topline... */
1508 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1509 row += wp->w_old_topfill;
1510 else
1511 row += diff_check_fill(wp, wp->w_topline);
1512 /* ... but don't delete new filler lines. */
1513 row -= wp->w_topfill;
1514#endif
1515 if (row > 0)
1516 {
1517 check_for_delay(FALSE);
1518 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1519 bot_start = wp->w_height - row;
1520 else
1521 mid_start = 0; /* redraw all lines */
1522 }
1523 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1524 {
1525 /*
1526 * Skip the lines (below the deleted lines) that are still
1527 * valid and don't need redrawing. Copy their info
1528 * upwards, to compensate for the deleted lines. Set
1529 * bot_start to the first row that needs redrawing.
1530 */
1531 bot_start = 0;
1532 idx = 0;
1533 for (;;)
1534 {
1535 wp->w_lines[idx] = wp->w_lines[j];
1536 /* stop at line that didn't fit, unless it is still
1537 * valid (no lines deleted) */
1538 if (row > 0 && bot_start + row
1539 + (int)wp->w_lines[j].wl_size > wp->w_height)
1540 {
1541 wp->w_lines_valid = idx + 1;
1542 break;
1543 }
1544 bot_start += wp->w_lines[idx++].wl_size;
1545
1546 /* stop at the last valid entry in w_lines[].wl_size */
1547 if (++j >= wp->w_lines_valid)
1548 {
1549 wp->w_lines_valid = idx;
1550 break;
1551 }
1552 }
1553#ifdef FEAT_DIFF
1554 /* Correct the first entry for filler lines at the top
1555 * when it won't get updated below. */
1556 if (wp->w_p_diff && bot_start > 0)
1557 wp->w_lines[0].wl_size =
1558 plines_win_nofill(wp, wp->w_topline, TRUE)
1559 + wp->w_topfill;
1560#endif
1561 }
1562 }
1563 }
1564
1565 /* When starting redraw in the first line, redraw all lines. When
1566 * there is only one window it's probably faster to clear the screen
1567 * first. */
1568 if (mid_start == 0)
1569 {
1570 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001571 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001572 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001573 /* Clear the screen when it was not done by win_del_lines() or
1574 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1575 * then. */
1576 if (screen_cleared != TRUE)
1577 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001578#ifdef FEAT_WINDOWS
1579 /* The screen was cleared, redraw the tab pages line. */
1580 if (redraw_tabline)
1581 draw_tabline();
1582#endif
1583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001585
1586 /* When win_del_lines() or win_ins_lines() caused the screen to be
1587 * cleared (only happens for the first window) or when screenclear()
1588 * was called directly above, "must_redraw" will have been set to
1589 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1590 if (screen_cleared == TRUE)
1591 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 }
1593 else
1594 {
1595 /* Not VALID or INVERTED: redraw all lines. */
1596 mid_start = 0;
1597 mid_end = wp->w_height;
1598 }
1599
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001600 if (type == SOME_VALID)
1601 {
1602 /* SOME_VALID: redraw all lines. */
1603 mid_start = 0;
1604 mid_end = wp->w_height;
1605 type = NOT_VALID;
1606 }
1607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /* check if we are updating or removing the inverted part */
1609 if ((VIsual_active && buf == curwin->w_buffer)
1610 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1611 {
1612 linenr_T from, to;
1613
1614 if (VIsual_active)
1615 {
1616 if (VIsual_active
1617 && (VIsual_mode != wp->w_old_visual_mode
1618 || type == INVERTED_ALL))
1619 {
1620 /*
1621 * If the type of Visual selection changed, redraw the whole
1622 * selection. Also when the ownership of the X selection is
1623 * gained or lost.
1624 */
1625 if (curwin->w_cursor.lnum < VIsual.lnum)
1626 {
1627 from = curwin->w_cursor.lnum;
1628 to = VIsual.lnum;
1629 }
1630 else
1631 {
1632 from = VIsual.lnum;
1633 to = curwin->w_cursor.lnum;
1634 }
1635 /* redraw more when the cursor moved as well */
1636 if (wp->w_old_cursor_lnum < from)
1637 from = wp->w_old_cursor_lnum;
1638 if (wp->w_old_cursor_lnum > to)
1639 to = wp->w_old_cursor_lnum;
1640 if (wp->w_old_visual_lnum < from)
1641 from = wp->w_old_visual_lnum;
1642 if (wp->w_old_visual_lnum > to)
1643 to = wp->w_old_visual_lnum;
1644 }
1645 else
1646 {
1647 /*
1648 * Find the line numbers that need to be updated: The lines
1649 * between the old cursor position and the current cursor
1650 * position. Also check if the Visual position changed.
1651 */
1652 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1653 {
1654 from = curwin->w_cursor.lnum;
1655 to = wp->w_old_cursor_lnum;
1656 }
1657 else
1658 {
1659 from = wp->w_old_cursor_lnum;
1660 to = curwin->w_cursor.lnum;
1661 if (from == 0) /* Visual mode just started */
1662 from = to;
1663 }
1664
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001665 if (VIsual.lnum != wp->w_old_visual_lnum
1666 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 if (wp->w_old_visual_lnum < from
1669 && wp->w_old_visual_lnum != 0)
1670 from = wp->w_old_visual_lnum;
1671 if (wp->w_old_visual_lnum > to)
1672 to = wp->w_old_visual_lnum;
1673 if (VIsual.lnum < from)
1674 from = VIsual.lnum;
1675 if (VIsual.lnum > to)
1676 to = VIsual.lnum;
1677 }
1678 }
1679
1680 /*
1681 * If in block mode and changed column or curwin->w_curswant:
1682 * update all lines.
1683 * First compute the actual start and end column.
1684 */
1685 if (VIsual_mode == Ctrl_V)
1686 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001687 colnr_T fromc, toc;
1688#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1689 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar404406a2014-10-09 13:24:43 +02001691 if (curwin->w_p_lbr)
1692 ve_flags = VE_ALL;
1693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001695#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1696 ve_flags = save_ve_flags;
1697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 ++toc;
1699 if (curwin->w_curswant == MAXCOL)
1700 toc = MAXCOL;
1701
1702 if (fromc != wp->w_old_cursor_fcol
1703 || toc != wp->w_old_cursor_lcol)
1704 {
1705 if (from > VIsual.lnum)
1706 from = VIsual.lnum;
1707 if (to < VIsual.lnum)
1708 to = VIsual.lnum;
1709 }
1710 wp->w_old_cursor_fcol = fromc;
1711 wp->w_old_cursor_lcol = toc;
1712 }
1713 }
1714 else
1715 {
1716 /* Use the line numbers of the old Visual area. */
1717 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1718 {
1719 from = wp->w_old_cursor_lnum;
1720 to = wp->w_old_visual_lnum;
1721 }
1722 else
1723 {
1724 from = wp->w_old_visual_lnum;
1725 to = wp->w_old_cursor_lnum;
1726 }
1727 }
1728
1729 /*
1730 * There is no need to update lines above the top of the window.
1731 */
1732 if (from < wp->w_topline)
1733 from = wp->w_topline;
1734
1735 /*
1736 * If we know the value of w_botline, use it to restrict the update to
1737 * the lines that are visible in the window.
1738 */
1739 if (wp->w_valid & VALID_BOTLINE)
1740 {
1741 if (from >= wp->w_botline)
1742 from = wp->w_botline - 1;
1743 if (to >= wp->w_botline)
1744 to = wp->w_botline - 1;
1745 }
1746
1747 /*
1748 * Find the minimal part to be updated.
1749 * Watch out for scrolling that made entries in w_lines[] invalid.
1750 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1751 * top_end; need to redraw from top_end to the "to" line.
1752 * A middle mouse click with a Visual selection may change the text
1753 * above the Visual area and reset wl_valid, do count these for
1754 * mid_end (in srow).
1755 */
1756 if (mid_start > 0)
1757 {
1758 lnum = wp->w_topline;
1759 idx = 0;
1760 srow = 0;
1761 if (scrolled_down)
1762 mid_start = top_end;
1763 else
1764 mid_start = 0;
1765 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1766 {
1767 if (wp->w_lines[idx].wl_valid)
1768 mid_start += wp->w_lines[idx].wl_size;
1769 else if (!scrolled_down)
1770 srow += wp->w_lines[idx].wl_size;
1771 ++idx;
1772# ifdef FEAT_FOLDING
1773 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1774 lnum = wp->w_lines[idx].wl_lnum;
1775 else
1776# endif
1777 ++lnum;
1778 }
1779 srow += mid_start;
1780 mid_end = wp->w_height;
1781 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1782 {
1783 if (wp->w_lines[idx].wl_valid
1784 && wp->w_lines[idx].wl_lnum >= to + 1)
1785 {
1786 /* Only update until first row of this line */
1787 mid_end = srow;
1788 break;
1789 }
1790 srow += wp->w_lines[idx].wl_size;
1791 }
1792 }
1793 }
1794
1795 if (VIsual_active && buf == curwin->w_buffer)
1796 {
1797 wp->w_old_visual_mode = VIsual_mode;
1798 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1799 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001800 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 wp->w_old_curswant = curwin->w_curswant;
1802 }
1803 else
1804 {
1805 wp->w_old_visual_mode = 0;
1806 wp->w_old_cursor_lnum = 0;
1807 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001808 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1812 /* reset got_int, otherwise regexp won't work */
1813 save_got_int = got_int;
1814 got_int = 0;
1815#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001816#ifdef SYN_TIME_LIMIT
1817 /* Set the time limit to 'redrawtime'. */
1818 profile_setlimit(p_rdt, &syntax_tm);
1819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#ifdef FEAT_FOLDING
1821 win_foldinfo.fi_level = 0;
1822#endif
1823
1824 /*
1825 * Update all the window rows.
1826 */
1827 idx = 0; /* first entry in w_lines[].wl_size */
1828 row = 0;
1829 srow = 0;
1830 lnum = wp->w_topline; /* first line shown in window */
1831 for (;;)
1832 {
1833 /* stop updating when reached the end of the window (check for _past_
1834 * the end of the window is at the end of the loop) */
1835 if (row == wp->w_height)
1836 {
1837 didline = TRUE;
1838 break;
1839 }
1840
1841 /* stop updating when hit the end of the file */
1842 if (lnum > buf->b_ml.ml_line_count)
1843 {
1844 eof = TRUE;
1845 break;
1846 }
1847
1848 /* Remember the starting row of the line that is going to be dealt
1849 * with. It is used further down when the line doesn't fit. */
1850 srow = row;
1851
1852 /*
1853 * Update a line when it is in an area that needs updating, when it
1854 * has changes or w_lines[idx] is invalid.
1855 * bot_start may be halfway a wrapped line after using
1856 * win_del_lines(), check if the current line includes it.
1857 * When syntax folding is being used, the saved syntax states will
1858 * already have been updated, we can't see where the syntax state is
1859 * the same again, just update until the end of the window.
1860 */
1861 if (row < top_end
1862 || (row >= mid_start && row < mid_end)
1863#ifdef FEAT_SEARCH_EXTRA
1864 || top_to_mod
1865#endif
1866 || idx >= wp->w_lines_valid
1867 || (row + wp->w_lines[idx].wl_size > bot_start)
1868 || (mod_top != 0
1869 && (lnum == mod_top
1870 || (lnum >= mod_top
1871 && (lnum < mod_bot
1872#ifdef FEAT_SYN_HL
1873 || did_update == DID_FOLD
1874 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001875 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 && (
1877# ifdef FEAT_FOLDING
1878 (foldmethodIsSyntax(wp)
1879 && hasAnyFolding(wp)) ||
1880# endif
1881 syntax_check_changed(lnum)))
1882#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001883#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001884 /* match in fixed position might need redraw
1885 * if lines were inserted or deleted */
1886 || (wp->w_match_head != NULL
1887 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 )))))
1890 {
1891#ifdef FEAT_SEARCH_EXTRA
1892 if (lnum == mod_top)
1893 top_to_mod = FALSE;
1894#endif
1895
1896 /*
1897 * When at start of changed lines: May scroll following lines
1898 * up or down to minimize redrawing.
1899 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001900 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 */
1902 if (lnum == mod_top
1903 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001904 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 int old_rows = 0;
1907 int new_rows = 0;
1908 int xtra_rows;
1909 linenr_T l;
1910
1911 /* Count the old number of window rows, using w_lines[], which
1912 * should still contain the sizes for the lines as they are
1913 * currently displayed. */
1914 for (i = idx; i < wp->w_lines_valid; ++i)
1915 {
1916 /* Only valid lines have a meaningful wl_lnum. Invalid
1917 * lines are part of the changed area. */
1918 if (wp->w_lines[i].wl_valid
1919 && wp->w_lines[i].wl_lnum == mod_bot)
1920 break;
1921 old_rows += wp->w_lines[i].wl_size;
1922#ifdef FEAT_FOLDING
1923 if (wp->w_lines[i].wl_valid
1924 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1925 {
1926 /* Must have found the last valid entry above mod_bot.
1927 * Add following invalid entries. */
1928 ++i;
1929 while (i < wp->w_lines_valid
1930 && !wp->w_lines[i].wl_valid)
1931 old_rows += wp->w_lines[i++].wl_size;
1932 break;
1933 }
1934#endif
1935 }
1936
1937 if (i >= wp->w_lines_valid)
1938 {
1939 /* We can't find a valid line below the changed lines,
1940 * need to redraw until the end of the window.
1941 * Inserting/deleting lines has no use. */
1942 bot_start = 0;
1943 }
1944 else
1945 {
1946 /* Able to count old number of rows: Count new window
1947 * rows, and may insert/delete lines */
1948 j = idx;
1949 for (l = lnum; l < mod_bot; ++l)
1950 {
1951#ifdef FEAT_FOLDING
1952 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1953 ++new_rows;
1954 else
1955#endif
1956#ifdef FEAT_DIFF
1957 if (l == wp->w_topline)
1958 new_rows += plines_win_nofill(wp, l, TRUE)
1959 + wp->w_topfill;
1960 else
1961#endif
1962 new_rows += plines_win(wp, l, TRUE);
1963 ++j;
1964 if (new_rows > wp->w_height - row - 2)
1965 {
1966 /* it's getting too much, must redraw the rest */
1967 new_rows = 9999;
1968 break;
1969 }
1970 }
1971 xtra_rows = new_rows - old_rows;
1972 if (xtra_rows < 0)
1973 {
1974 /* May scroll text up. If there is not enough
1975 * remaining text or scrolling fails, must redraw the
1976 * rest. If scrolling works, must redraw the text
1977 * below the scrolled text. */
1978 if (row - xtra_rows >= wp->w_height - 2)
1979 mod_bot = MAXLNUM;
1980 else
1981 {
1982 check_for_delay(FALSE);
1983 if (win_del_lines(wp, row,
1984 -xtra_rows, FALSE, FALSE) == FAIL)
1985 mod_bot = MAXLNUM;
1986 else
1987 bot_start = wp->w_height + xtra_rows;
1988 }
1989 }
1990 else if (xtra_rows > 0)
1991 {
1992 /* May scroll text down. If there is not enough
1993 * remaining text of scrolling fails, must redraw the
1994 * rest. */
1995 if (row + xtra_rows >= wp->w_height - 2)
1996 mod_bot = MAXLNUM;
1997 else
1998 {
1999 check_for_delay(FALSE);
2000 if (win_ins_lines(wp, row + old_rows,
2001 xtra_rows, FALSE, FALSE) == FAIL)
2002 mod_bot = MAXLNUM;
2003 else if (top_end > row + old_rows)
2004 /* Scrolled the part at the top that requires
2005 * updating down. */
2006 top_end += xtra_rows;
2007 }
2008 }
2009
2010 /* When not updating the rest, may need to move w_lines[]
2011 * entries. */
2012 if (mod_bot != MAXLNUM && i != j)
2013 {
2014 if (j < i)
2015 {
2016 int x = row + new_rows;
2017
2018 /* move entries in w_lines[] upwards */
2019 for (;;)
2020 {
2021 /* stop at last valid entry in w_lines[] */
2022 if (i >= wp->w_lines_valid)
2023 {
2024 wp->w_lines_valid = j;
2025 break;
2026 }
2027 wp->w_lines[j] = wp->w_lines[i];
2028 /* stop at a line that won't fit */
2029 if (x + (int)wp->w_lines[j].wl_size
2030 > wp->w_height)
2031 {
2032 wp->w_lines_valid = j + 1;
2033 break;
2034 }
2035 x += wp->w_lines[j++].wl_size;
2036 ++i;
2037 }
2038 if (bot_start > x)
2039 bot_start = x;
2040 }
2041 else /* j > i */
2042 {
2043 /* move entries in w_lines[] downwards */
2044 j -= i;
2045 wp->w_lines_valid += j;
2046 if (wp->w_lines_valid > wp->w_height)
2047 wp->w_lines_valid = wp->w_height;
2048 for (i = wp->w_lines_valid; i - j >= idx; --i)
2049 wp->w_lines[i] = wp->w_lines[i - j];
2050
2051 /* The w_lines[] entries for inserted lines are
2052 * now invalid, but wl_size may be used above.
2053 * Reset to zero. */
2054 while (i >= idx)
2055 {
2056 wp->w_lines[i].wl_size = 0;
2057 wp->w_lines[i--].wl_valid = FALSE;
2058 }
2059 }
2060 }
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
2064#ifdef FEAT_FOLDING
2065 /*
2066 * When lines are folded, display one line for all of them.
2067 * Otherwise, display normally (can be several display lines when
2068 * 'wrap' is on).
2069 */
2070 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2071 if (fold_count != 0)
2072 {
2073 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2074 ++row;
2075 --fold_count;
2076 wp->w_lines[idx].wl_folded = TRUE;
2077 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2078# ifdef FEAT_SYN_HL
2079 did_update = DID_FOLD;
2080# endif
2081 }
2082 else
2083#endif
2084 if (idx < wp->w_lines_valid
2085 && wp->w_lines[idx].wl_valid
2086 && wp->w_lines[idx].wl_lnum == lnum
2087 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002088 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 && srow + wp->w_lines[idx].wl_size > wp->w_height
2090#ifdef FEAT_DIFF
2091 && diff_check_fill(wp, lnum) == 0
2092#endif
2093 )
2094 {
2095 /* This line is not going to fit. Don't draw anything here,
2096 * will draw "@ " lines below. */
2097 row = wp->w_height + 1;
2098 }
2099 else
2100 {
2101#ifdef FEAT_SEARCH_EXTRA
2102 prepare_search_hl(wp, lnum);
2103#endif
2104#ifdef FEAT_SYN_HL
2105 /* Let the syntax stuff know we skipped a few lines. */
2106 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002107 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 syntax_end_parsing(syntax_last_parsed + 1);
2109#endif
2110
2111 /*
2112 * Display one line.
2113 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002114 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2115#ifdef SYN_TIME_LIMIT
2116 &syntax_tm
2117#else
2118 NULL
2119#endif
2120 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122#ifdef FEAT_FOLDING
2123 wp->w_lines[idx].wl_folded = FALSE;
2124 wp->w_lines[idx].wl_lastlnum = lnum;
2125#endif
2126#ifdef FEAT_SYN_HL
2127 did_update = DID_LINE;
2128 syntax_last_parsed = lnum;
2129#endif
2130 }
2131
2132 wp->w_lines[idx].wl_lnum = lnum;
2133 wp->w_lines[idx].wl_valid = TRUE;
2134 if (row > wp->w_height) /* past end of screen */
2135 {
2136 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002137 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2139 ++idx;
2140 break;
2141 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002142 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 wp->w_lines[idx].wl_size = row - srow;
2144 ++idx;
2145#ifdef FEAT_FOLDING
2146 lnum += fold_count + 1;
2147#else
2148 ++lnum;
2149#endif
2150 }
2151 else
2152 {
2153 /* This line does not need updating, advance to the next one */
2154 row += wp->w_lines[idx++].wl_size;
2155 if (row > wp->w_height) /* past end of screen */
2156 break;
2157#ifdef FEAT_FOLDING
2158 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2159#else
2160 ++lnum;
2161#endif
2162#ifdef FEAT_SYN_HL
2163 did_update = DID_NONE;
2164#endif
2165 }
2166
2167 if (lnum > buf->b_ml.ml_line_count)
2168 {
2169 eof = TRUE;
2170 break;
2171 }
2172 }
2173 /*
2174 * End of loop over all window lines.
2175 */
2176
2177
2178 if (idx > wp->w_lines_valid)
2179 wp->w_lines_valid = idx;
2180
2181#ifdef FEAT_SYN_HL
2182 /*
2183 * Let the syntax stuff know we stop parsing here.
2184 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002185 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 syntax_end_parsing(syntax_last_parsed + 1);
2187#endif
2188
2189 /*
2190 * If we didn't hit the end of the file, and we didn't finish the last
2191 * line we were working on, then the line didn't fit.
2192 */
2193 wp->w_empty_rows = 0;
2194#ifdef FEAT_DIFF
2195 wp->w_filler_rows = 0;
2196#endif
2197 if (!eof && !didline)
2198 {
2199 if (lnum == wp->w_topline)
2200 {
2201 /*
2202 * Single line that does not fit!
2203 * Don't overwrite it, it can be edited.
2204 */
2205 wp->w_botline = lnum + 1;
2206 }
2207#ifdef FEAT_DIFF
2208 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2209 {
2210 /* Window ends in filler lines. */
2211 wp->w_botline = lnum;
2212 wp->w_filler_rows = wp->w_height - srow;
2213 }
2214#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002215 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2216 {
2217 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2218
2219 /*
2220 * Last line isn't finished: Display "@@@" in the last screen line.
2221 */
2222 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002223 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002224 screen_fill(scr_row, scr_row + 1,
2225 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002226 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002227 set_empty_rows(wp, srow);
2228 wp->w_botline = lnum;
2229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2231 {
2232 /*
2233 * Last line isn't finished: Display "@@@" at the end.
2234 */
2235 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2236 W_WINROW(wp) + wp->w_height,
2237 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002238 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 set_empty_rows(wp, srow);
2240 wp->w_botline = lnum;
2241 }
2242 else
2243 {
2244 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2245 wp->w_botline = lnum;
2246 }
2247 }
2248 else
2249 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002250#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 draw_vsep_win(wp, row);
2252#endif
2253 if (eof) /* we hit the end of the file */
2254 {
2255 wp->w_botline = buf->b_ml.ml_line_count + 1;
2256#ifdef FEAT_DIFF
2257 j = diff_check_fill(wp, wp->w_botline);
2258 if (j > 0 && !wp->w_botfill)
2259 {
2260 /*
2261 * Display filler lines at the end of the file
2262 */
2263 if (char2cells(fill_diff) > 1)
2264 i = '-';
2265 else
2266 i = fill_diff;
2267 if (row + j > wp->w_height)
2268 j = wp->w_height - row;
2269 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2270 row += j;
2271 }
2272#endif
2273 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002274 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 wp->w_botline = lnum;
2276
2277 /* make sure the rest of the screen is blank */
2278 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002279 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281
2282 /* Reset the type of redrawing required, the window has been updated. */
2283 wp->w_redr_type = 0;
2284#ifdef FEAT_DIFF
2285 wp->w_old_topfill = wp->w_topfill;
2286 wp->w_old_botfill = wp->w_botfill;
2287#endif
2288
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002289 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 /*
2292 * There is a trick with w_botline. If we invalidate it on each
2293 * change that might modify it, this will cause a lot of expensive
2294 * calls to plines() in update_topline() each time. Therefore the
2295 * value of w_botline is often approximated, and this value is used to
2296 * compute the value of w_topline. If the value of w_botline was
2297 * wrong, check that the value of w_topline is correct (cursor is on
2298 * the visible part of the text). If it's not, we need to redraw
2299 * again. Mostly this just means scrolling up a few lines, so it
2300 * doesn't look too bad. Only do this for the current window (where
2301 * changes are relevant).
2302 */
2303 wp->w_valid |= VALID_BOTLINE;
2304 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2305 {
2306 recursive = TRUE;
2307 curwin->w_valid &= ~VALID_TOPLINE;
2308 update_topline(); /* may invalidate w_botline again */
2309 if (must_redraw != 0)
2310 {
2311 /* Don't update for changes in buffer again. */
2312 i = curbuf->b_mod_set;
2313 curbuf->b_mod_set = FALSE;
2314 win_update(curwin);
2315 must_redraw = 0;
2316 curbuf->b_mod_set = i;
2317 }
2318 recursive = FALSE;
2319 }
2320 }
2321
2322#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2323 /* restore got_int, unless CTRL-C was hit while redrawing */
2324 if (!got_int)
2325 got_int = save_got_int;
2326#endif
2327}
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2331 * as the filler character.
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334win_draw_end(
2335 win_T *wp,
2336 int c1,
2337 int c2,
2338 int row,
2339 int endrow,
2340 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2343 int n = 0;
2344# define FDC_OFF n
2345#else
2346# define FDC_OFF 0
2347#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002348#ifdef FEAT_FOLDING
2349 int fdc = compute_foldcolumn(wp, 0);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352#ifdef FEAT_RIGHTLEFT
2353 if (wp->w_p_rl)
2354 {
2355 /* No check for cmdline window: should never be right-left. */
2356# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002357 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 if (n > 0)
2360 {
2361 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002362 if (n > W_WIDTH(wp))
2363 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2365 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002366 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368# endif
2369# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002370 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 int nn = n + 2;
2373
2374 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002375 if (nn > W_WIDTH(wp))
2376 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2378 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 n = nn;
2381 }
2382# endif
2383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2384 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2387 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002388 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 else
2391#endif
2392 {
2393#ifdef FEAT_CMDWIN
2394 if (cmdwin_type != 0 && wp == curwin)
2395 {
2396 /* draw the cmdline character in the leftmost column */
2397 n = 1;
2398 if (n > wp->w_width)
2399 n = wp->w_width;
2400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2401 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404#endif
2405#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002406 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /* draw the fold column at the left */
2411 if (nn > W_WIDTH(wp))
2412 nn = W_WIDTH(wp);
2413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2414 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002415 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 n = nn;
2417 }
2418#endif
2419#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002420 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 int nn = n + 2;
2423
2424 /* draw the sign column after the fold column */
2425 if (nn > W_WIDTH(wp))
2426 nn = W_WIDTH(wp);
2427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2428 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 n = nn;
2431 }
2432#endif
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 set_empty_rows(wp, row);
2438}
2439
Bram Moolenaar1a384422010-07-14 19:53:30 +02002440#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002441static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002442
2443/*
2444 * Advance **color_cols and return TRUE when there are columns to draw.
2445 */
2446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002448{
2449 while (**color_cols >= 0 && vcol > **color_cols)
2450 ++*color_cols;
2451 return (**color_cols >= 0);
2452}
2453#endif
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455#ifdef FEAT_FOLDING
2456/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002457 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2458 * space is available for window "wp", minus "col".
2459 */
2460 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002462{
2463 int fdc = wp->w_p_fdc;
2464 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2465 int wwidth = W_WIDTH(wp);
2466
2467 if (fdc > wwidth - (col + wmw))
2468 fdc = wwidth - (col + wmw);
2469 return fdc;
2470}
2471
2472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 * Display one folded line.
2474 */
2475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002476fold_line(
2477 win_T *wp,
2478 long fold_count,
2479 foldinfo_T *foldinfo,
2480 linenr_T lnum,
2481 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002483 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 pos_T *top, *bot;
2485 linenr_T lnume = lnum + fold_count - 1;
2486 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002487 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int col;
2490 int txtcol;
2491 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002492 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 /* Build the fold line:
2495 * 1. Add the cmdwin_type for the command-line window
2496 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002497 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 * 4. Compose the text
2499 * 5. Add the text
2500 * 6. set highlighting for the Visual area an other text
2501 */
2502 col = 0;
2503
2504 /*
2505 * 1. Add the cmdwin_type for the command-line window
2506 * Ignores 'rightleft', this window is never right-left.
2507 */
2508#ifdef FEAT_CMDWIN
2509 if (cmdwin_type != 0 && wp == curwin)
2510 {
2511 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002512 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#ifdef FEAT_MBYTE
2514 if (enc_utf8)
2515 ScreenLinesUC[off] = 0;
2516#endif
2517 ++col;
2518 }
2519#endif
2520
2521 /*
2522 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002523 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002525 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (fdc > 0)
2527 {
2528 fill_foldcolumn(buf, wp, TRUE, lnum);
2529#ifdef FEAT_RIGHTLEFT
2530 if (wp->w_p_rl)
2531 {
2532 int i;
2533
2534 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002535 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /* reverse the fold column */
2537 for (i = 0; i < fdc; ++i)
2538 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2539 }
2540 else
2541#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002542 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 col += fdc;
2544 }
2545
2546#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002547# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2548 for (ri = 0; ri < l; ++ri) \
2549 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2550 else \
2551 for (ri = 0; ri < l; ++ri) \
2552 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002554# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2555 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556#endif
2557
Bram Moolenaar64486672010-05-16 15:46:46 +02002558 /* Set all attributes of the 'number' or 'relativenumber' column and the
2559 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002560 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562#ifdef FEAT_SIGNS
2563 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002564 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 len = W_WIDTH(wp) - col;
2567 if (len > 0)
2568 {
2569 if (len > 2)
2570 len = 2;
2571# ifdef FEAT_RIGHTLEFT
2572 if (wp->w_p_rl)
2573 /* the line number isn't reversed */
2574 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002575 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else
2577# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002578 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 col += len;
2580 }
2581 }
2582#endif
2583
2584 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002585 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002587 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 len = W_WIDTH(wp) - col;
2590 if (len > 0)
2591 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002592 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002593 long num;
2594 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002595
2596 if (len > w + 1)
2597 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002598
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002599 if (wp->w_p_nu && !wp->w_p_rnu)
2600 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002601 num = (long)lnum;
2602 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002603 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002604 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002605 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002606 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002607 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002608 /* 'number' + 'relativenumber': cursor line shows absolute
2609 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002610 num = lnum;
2611 fmt = "%-*ld ";
2612 }
2613 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002614
Bram Moolenaar700e7342013-01-30 12:31:36 +01002615 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616#ifdef FEAT_RIGHTLEFT
2617 if (wp->w_p_rl)
2618 /* the line number isn't reversed */
2619 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002620 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 else
2622#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002623 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 col += len;
2625 }
2626 }
2627
2628 /*
2629 * 4. Compose the folded-line string with 'foldtext', if set.
2630 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002631 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 txtcol = col; /* remember where text starts */
2634
2635 /*
2636 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2637 * Right-left text is put in columns 0 - number-col, normal text is put
2638 * in columns number-col - window-width.
2639 */
2640#ifdef FEAT_MBYTE
2641 if (has_mbyte)
2642 {
2643 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002644 int u8c, u8cc[MAX_MCO];
2645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int idx;
2647 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002648 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649# ifdef FEAT_ARABIC
2650 int prev_c = 0; /* previous Arabic character */
2651 int prev_c1 = 0; /* first composing char for prev_c */
2652# endif
2653
2654# ifdef FEAT_RIGHTLEFT
2655 if (wp->w_p_rl)
2656 idx = off;
2657 else
2658# endif
2659 idx = off + col;
2660
2661 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2662 for (p = text; *p != NUL; )
2663 {
2664 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002665 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (col + cells > W_WIDTH(wp)
2667# ifdef FEAT_RIGHTLEFT
2668 - (wp->w_p_rl ? col : 0)
2669# endif
2670 )
2671 break;
2672 ScreenLines[idx] = *p;
2673 if (enc_utf8)
2674 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002675 u8c = utfc_ptr2char(p, u8cc);
2676 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 ScreenLinesUC[idx] = 0;
2679#ifdef FEAT_ARABIC
2680 prev_c = u8c;
2681#endif
2682 }
2683 else
2684 {
2685#ifdef FEAT_ARABIC
2686 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2687 {
2688 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 int pc, pc1, nc;
2690 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int firstbyte = *p;
2692
2693 /* The idea of what is the previous and next
2694 * character depends on 'rightleft'. */
2695 if (wp->w_p_rl)
2696 {
2697 pc = prev_c;
2698 pc1 = prev_c1;
2699 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 else
2703 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 prev_c = u8c;
2709
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 pc, pc1, nc);
2712 ScreenLines[idx] = firstbyte;
2713 }
2714 else
2715 prev_c = u8c;
2716#endif
2717 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002718#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (u8c >= 0x10000)
2720 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2721 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002724 for (i = 0; i < Screen_mco; ++i)
2725 {
2726 ScreenLinesC[i][idx] = u8cc[i];
2727 if (u8cc[i] == 0)
2728 break;
2729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 if (cells > 1)
2732 ScreenLines[idx + 1] = 0;
2733 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002734 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2735 /* double-byte single width character */
2736 ScreenLines2[idx] = p[1];
2737 else if (cells > 1)
2738 /* double-width character */
2739 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 col += cells;
2741 idx += cells;
2742 p += c_len;
2743 }
2744 }
2745 else
2746#endif
2747 {
2748 len = (int)STRLEN(text);
2749 if (len > W_WIDTH(wp) - col)
2750 len = W_WIDTH(wp) - col;
2751 if (len > 0)
2752 {
2753#ifdef FEAT_RIGHTLEFT
2754 if (wp->w_p_rl)
2755 STRNCPY(current_ScreenLine, text, len);
2756 else
2757#endif
2758 STRNCPY(current_ScreenLine + col, text, len);
2759 col += len;
2760 }
2761 }
2762
2763 /* Fill the rest of the line with the fold filler */
2764#ifdef FEAT_RIGHTLEFT
2765 if (wp->w_p_rl)
2766 col -= txtcol;
2767#endif
2768 while (col < W_WIDTH(wp)
2769#ifdef FEAT_RIGHTLEFT
2770 - (wp->w_p_rl ? txtcol : 0)
2771#endif
2772 )
2773 {
2774#ifdef FEAT_MBYTE
2775 if (enc_utf8)
2776 {
2777 if (fill_fold >= 0x80)
2778 {
2779 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002780 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002781 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
2783 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002786 ScreenLines[off + col] = fill_fold;
2787 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002788 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002792 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794
2795 if (text != buf)
2796 vim_free(text);
2797
2798 /*
2799 * 6. set highlighting for the Visual area an other text.
2800 * If all folded lines are in the Visual area, highlight the line.
2801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2803 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002804 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
2806 /* Visual is after curwin->w_cursor */
2807 top = &curwin->w_cursor;
2808 bot = &VIsual;
2809 }
2810 else
2811 {
2812 /* Visual is before curwin->w_cursor */
2813 top = &VIsual;
2814 bot = &curwin->w_cursor;
2815 }
2816 if (lnum >= top->lnum
2817 && lnume <= bot->lnum
2818 && (VIsual_mode != 'v'
2819 || ((lnum > top->lnum
2820 || (lnum == top->lnum
2821 && top->col == 0))
2822 && (lnume < bot->lnum
2823 || (lnume == bot->lnum
2824 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002825 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 if (VIsual_mode == Ctrl_V)
2828 {
2829 /* Visual block mode: highlight the chars part of the block */
2830 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2831 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002832 if (wp->w_old_cursor_lcol != MAXCOL
2833 && wp->w_old_cursor_lcol + txtcol
2834 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 len = wp->w_old_cursor_lcol;
2836 else
2837 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002838 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002839 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841 }
2842 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002845 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002850#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002851 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2852 if (wp->w_p_cc_cols)
2853 {
2854 int i = 0;
2855 int j = wp->w_p_cc_cols[i];
2856 int old_txtcol = txtcol;
2857
2858 while (j > -1)
2859 {
2860 txtcol += j;
2861 if (wp->w_p_wrap)
2862 txtcol -= wp->w_skipcol;
2863 else
2864 txtcol -= wp->w_leftcol;
2865 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2866 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002867 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002868 txtcol = old_txtcol;
2869 j = wp->w_p_cc_cols[++i];
2870 }
2871 }
2872
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002873 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002874 if (wp->w_p_cuc)
2875 {
2876 txtcol += wp->w_virtcol;
2877 if (wp->w_p_wrap)
2878 txtcol -= wp->w_skipcol;
2879 else
2880 txtcol -= wp->w_leftcol;
2881 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2882 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002883 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002884 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2888 (int)W_WIDTH(wp), FALSE);
2889
2890 /*
2891 * Update w_cline_height and w_cline_folded if the cursor line was
2892 * updated (saves a call to plines() later).
2893 */
2894 if (wp == curwin
2895 && lnum <= curwin->w_cursor.lnum
2896 && lnume >= curwin->w_cursor.lnum)
2897 {
2898 curwin->w_cline_row = row;
2899 curwin->w_cline_height = 1;
2900 curwin->w_cline_folded = TRUE;
2901 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2902 }
2903}
2904
2905/*
2906 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2907 */
2908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002909copy_text_attr(
2910 int off,
2911 char_u *buf,
2912 int len,
2913 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002915 int i;
2916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 mch_memmove(ScreenLines + off, buf, (size_t)len);
2918# ifdef FEAT_MBYTE
2919 if (enc_utf8)
2920 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2921# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 for (i = 0; i < len; ++i)
2923 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924}
2925
2926/*
2927 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002928 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 */
2930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002931fill_foldcolumn(
2932 char_u *p,
2933 win_T *wp,
2934 int closed, /* TRUE of FALSE */
2935 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 int i = 0;
2938 int level;
2939 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002940 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002941 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002944 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 level = win_foldinfo.fi_level;
2947 if (level > 0)
2948 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002949 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002950 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 /* If the column is too narrow, we start at the lowest level that
2953 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002954 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (first_level < 1)
2956 first_level = 1;
2957
Bram Moolenaar1c934292015-01-27 16:39:29 +01002958 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 if (win_foldinfo.fi_lnum == lnum
2961 && first_level + i >= win_foldinfo.fi_low_level)
2962 p[i] = '-';
2963 else if (first_level == 1)
2964 p[i] = '|';
2965 else if (first_level + i <= 9)
2966 p[i] = '0' + first_level + i;
2967 else
2968 p[i] = '>';
2969 if (first_level + i == level)
2970 break;
2971 }
2972 }
2973 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975}
2976#endif /* FEAT_FOLDING */
2977
2978/*
2979 * Display line "lnum" of window 'wp' on the screen.
2980 * Start at row "startrow", stop when "endrow" is reached.
2981 * wp->w_virtcol needs to be valid.
2982 *
2983 * Return the number of last row the line occupies.
2984 */
2985 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002986win_line(
2987 win_T *wp,
2988 linenr_T lnum,
2989 int startrow,
2990 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002991 int nochange UNUSED, /* not updating for changed text */
2992 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002994 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2996 int c = 0; /* init for GCC */
2997 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002998#ifdef FEAT_LINEBREAK
2999 long vcol_sbr = -1; /* virtual column after showbreak */
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 long vcol_prev = -1; /* "vcol" of previous character */
3002 char_u *line; /* current line */
3003 char_u *ptr; /* current position in "line" */
3004 int row; /* row in the window, excl w_winrow */
3005 int screen_row; /* row on the screen, incl w_winrow */
3006
3007 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3008 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003009 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003010 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 int c_extra = NUL; /* extra chars, all the same */
3012 int extra_attr = 0; /* attributes when n_extra != 0 */
3013 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3014 displaying lcs_eol at end-of-line */
3015 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3016 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3017
3018 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3019 int saved_n_extra = 0;
3020 char_u *saved_p_extra = NULL;
3021 int saved_c_extra = 0;
3022 int saved_char_attr = 0;
3023
3024 int n_attr = 0; /* chars with special attr */
3025 int saved_attr2 = 0; /* char_attr saved for n_attr */
3026 int n_attr3 = 0; /* chars with overruling special attr */
3027 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3028
3029 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3030
3031 int fromcol, tocol; /* start/end of inverting */
3032 int fromcol_prev = -2; /* start of inverting after cursor */
3033 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003035 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 pos_T pos;
3037 long v;
3038
3039 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003040 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3042 in this line */
3043 int attr = 0; /* attributes for area highlighting */
3044 int area_attr = 0; /* attributes desired by highlighting */
3045 int search_attr = 0; /* attributes desired by 'hlsearch' */
3046#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003047 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 int syntax_attr = 0; /* attributes desired by syntax */
3049 int has_syntax = FALSE; /* this buffer has syntax highl. */
3050 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003051 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003052 int draw_color_col = FALSE; /* highlight colorcolumn */
3053 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003054#endif
3055#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003056 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003057# define SPWORDLEN 150
3058 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003059 int nextlinecol = 0; /* column where nextline[] starts */
3060 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003061 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003062 int spell_attr = 0; /* attributes desired by spelling */
3063 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003064 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3065 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003066 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003067 static int cap_col = -1; /* column to check for Cap word */
3068 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003069 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070#endif
3071 int extra_check; /* has syntax or linebreak */
3072#ifdef FEAT_MBYTE
3073 int multi_attr = 0; /* attributes desired by multibyte */
3074 int mb_l = 1; /* multi-byte byte length */
3075 int mb_c = 0; /* decoded multi-byte character */
3076 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003077 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#endif
3079#ifdef FEAT_DIFF
3080 int filler_lines; /* nr of filler lines to be drawn */
3081 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003082 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int change_start = MAXCOL; /* first col of changed area */
3084 int change_end = -1; /* last col of changed area */
3085#endif
3086 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3087#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003088 int need_showbreak = FALSE; /* overlong line, skipping first x
3089 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003091#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3092 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003094 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#endif
3096#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003097 matchitem_T *cur; /* points to the match list */
3098 match_T *shl; /* points to search_hl or a match */
3099 int shl_flag; /* flag to indicate whether search_hl
3100 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003101 int pos_inprogress; /* marks that position match search is
3102 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003103 int prevcol_hl_flag; /* flag to indicate whether prevcol
3104 equals startcol of search_hl or one
3105 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106#endif
3107#ifdef FEAT_ARABIC
3108 int prev_c = 0; /* previous Arabic character */
3109 int prev_c1 = 0; /* first composing char for prev_c */
3110#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003111#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003112 int did_line_attr = 0;
3113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 /* draw_state: items that are drawn in sequence: */
3116#define WL_START 0 /* nothing done yet */
3117#ifdef FEAT_CMDWIN
3118# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3119#else
3120# define WL_CMDLINE WL_START
3121#endif
3122#ifdef FEAT_FOLDING
3123# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3124#else
3125# define WL_FOLD WL_CMDLINE
3126#endif
3127#ifdef FEAT_SIGNS
3128# define WL_SIGN WL_FOLD + 1 /* column for signs */
3129#else
3130# define WL_SIGN WL_FOLD /* column for signs */
3131#endif
3132#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003133#ifdef FEAT_LINEBREAK
3134# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003136# define WL_BRI WL_NR
3137#endif
3138#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3139# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3140#else
3141# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142#endif
3143#define WL_LINE WL_SBR + 1 /* text in the line */
3144 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003145#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int feedback_col = 0;
3147 int feedback_old_attr = -1;
3148#endif
3149
Bram Moolenaar860cae12010-06-05 23:22:07 +02003150#ifdef FEAT_CONCEAL
3151 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003152 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003153 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003154 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003155 int is_concealing = FALSE;
3156 int boguscols = 0; /* nonexistent columns added to force
3157 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003158 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003159 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003160 int match_conc = 0; /* cchar for match functions */
3161 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003162 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003163# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003164# define FIX_FOR_BOGUSCOLS \
3165 { \
3166 n_extra += vcol_off; \
3167 vcol -= vcol_off; \
3168 vcol_off = 0; \
3169 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003170 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003171 boguscols = 0; \
3172 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003173#else
3174# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
3177 if (startrow > endrow) /* past the end already! */
3178 return startrow;
3179
3180 row = startrow;
3181 screen_row = row + W_WINROW(wp);
3182
3183 /*
3184 * To speed up the loop below, set extra_check when there is linebreak,
3185 * trailing white space and/or syntax processing to be done.
3186 */
3187#ifdef FEAT_LINEBREAK
3188 extra_check = wp->w_p_lbr;
3189#else
3190 extra_check = 0;
3191#endif
3192#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003193 if (syntax_present(wp) && !wp->w_s->b_syn_error
3194# ifdef SYN_TIME_LIMIT
3195 && !wp->w_s->b_syn_slow
3196# endif
3197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 {
3199 /* Prepare for syntax highlighting in this line. When there is an
3200 * error, stop syntax highlighting. */
3201 save_did_emsg = did_emsg;
3202 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003203 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003205 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else
3207 {
3208 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003209#ifdef SYN_TIME_LIMIT
3210 if (!wp->w_s->b_syn_slow)
3211#endif
3212 {
3213 has_syntax = TRUE;
3214 extra_check = TRUE;
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003218
3219 /* Check for columns to display for 'colorcolumn'. */
3220 color_cols = wp->w_p_cc_cols;
3221 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003222 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003223#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003224
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003225#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003226 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003227 && *wp->w_s->b_p_spl != NUL
3228 && wp->w_s->b_langp.ga_len > 0
3229 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003230 {
3231 /* Prepare for spell checking. */
3232 has_spell = TRUE;
3233 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003234
3235 /* Get the start of the next line, so that words that wrap to the next
3236 * line are found too: "et<line-break>al.".
3237 * Trick: skip a few chars for C/shell/Vim comments */
3238 nextline[SPWORDLEN] = NUL;
3239 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3240 {
3241 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3242 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3243 }
3244
3245 /* When a word wrapped from the previous line the start of the current
3246 * line is valid. */
3247 if (lnum == checked_lnum)
3248 cur_checked_col = checked_col;
3249 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003250
3251 /* When there was a sentence end in the previous line may require a
3252 * word starting with capital in this line. In line 1 always check
3253 * the first word. */
3254 if (lnum != capcol_lnum)
3255 cap_col = -1;
3256 if (lnum == 1)
3257 cap_col = 0;
3258 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260#endif
3261
3262 /*
3263 * handle visual active in this window
3264 */
3265 fromcol = -10;
3266 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3268 {
3269 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003270 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 top = &curwin->w_cursor;
3273 bot = &VIsual;
3274 }
3275 else /* Visual is before curwin->w_cursor */
3276 {
3277 top = &VIsual;
3278 bot = &curwin->w_cursor;
3279 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003280 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (VIsual_mode == Ctrl_V) /* block mode */
3282 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003283 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 fromcol = wp->w_old_cursor_fcol;
3286 tocol = wp->w_old_cursor_lcol;
3287 }
3288 }
3289 else /* non-block mode */
3290 {
3291 if (lnum > top->lnum && lnum <= bot->lnum)
3292 fromcol = 0;
3293 else if (lnum == top->lnum)
3294 {
3295 if (VIsual_mode == 'V') /* linewise */
3296 fromcol = 0;
3297 else
3298 {
3299 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3300 if (gchar_pos(top) == NUL)
3301 tocol = fromcol + 1;
3302 }
3303 }
3304 if (VIsual_mode != 'V' && lnum == bot->lnum)
3305 {
3306 if (*p_sel == 'e' && bot->col == 0
3307#ifdef FEAT_VIRTUALEDIT
3308 && bot->coladd == 0
3309#endif
3310 )
3311 {
3312 fromcol = -10;
3313 tocol = MAXCOL;
3314 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003315 else if (bot->col == MAXCOL)
3316 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 else
3318 {
3319 pos = *bot;
3320 if (*p_sel == 'e')
3321 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3322 else
3323 {
3324 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3325 ++tocol;
3326 }
3327 }
3328 }
3329 }
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /* Check if the character under the cursor should not be inverted */
3332 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003333#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 )
3337 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339 /* if inverting in this line set area_highlighting */
3340 if (fromcol >= 0)
3341 {
3342 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003343 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003345 if ((clip_star.available && !clip_star.owned
3346 && clip_isautosel_star())
3347 || (clip_plus.available && !clip_plus.owned
3348 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003349 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#endif
3351 }
3352 }
3353
3354 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003355 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003357 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 && wp == curwin
3359 && lnum >= curwin->w_cursor.lnum
3360 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3361 {
3362 if (lnum == curwin->w_cursor.lnum)
3363 getvcol(curwin, &(curwin->w_cursor),
3364 (colnr_T *)&fromcol, NULL, NULL);
3365 else
3366 fromcol = 0;
3367 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3368 {
3369 pos.lnum = lnum;
3370 pos.col = search_match_endcol;
3371 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3372 }
3373 else
3374 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003375 /* do at least one character; happens when past end of line */
3376 if (fromcol == tocol)
3377 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003379 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381
3382#ifdef FEAT_DIFF
3383 filler_lines = diff_check(wp, lnum);
3384 if (filler_lines < 0)
3385 {
3386 if (filler_lines == -1)
3387 {
3388 if (diff_find_change(wp, lnum, &change_start, &change_end))
3389 diff_hlf = HLF_ADD; /* added line */
3390 else if (change_start == 0)
3391 diff_hlf = HLF_TXD; /* changed text */
3392 else
3393 diff_hlf = HLF_CHD; /* changed line */
3394 }
3395 else
3396 diff_hlf = HLF_ADD; /* added line */
3397 filler_lines = 0;
3398 area_highlighting = TRUE;
3399 }
3400 if (lnum == wp->w_topline)
3401 filler_lines = wp->w_topfill;
3402 filler_todo = filler_lines;
3403#endif
3404
3405#ifdef LINE_ATTR
3406# ifdef FEAT_SIGNS
3407 /* If this line has a sign with line highlighting set line_attr. */
3408 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3409 if (v != 0)
3410 line_attr = sign_get_attr((int)v, TRUE);
3411# endif
3412# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3413 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003414 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003415 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416# endif
3417 if (line_attr != 0)
3418 area_highlighting = TRUE;
3419#endif
3420
3421 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3422 ptr = line;
3423
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003424#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003425 if (has_spell)
3426 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003427 /* For checking first word with a capital skip white space. */
3428 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003429 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003430
Bram Moolenaar30abd282005-06-22 22:35:10 +00003431 /* To be able to spell-check over line boundaries copy the end of the
3432 * current line into nextline[]. Above the start of the next line was
3433 * copied to nextline[SPWORDLEN]. */
3434 if (nextline[SPWORDLEN] == NUL)
3435 {
3436 /* No next line or it is empty. */
3437 nextlinecol = MAXCOL;
3438 nextline_idx = 0;
3439 }
3440 else
3441 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003442 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003443 if (v < SPWORDLEN)
3444 {
3445 /* Short line, use it completely and append the start of the
3446 * next line. */
3447 nextlinecol = 0;
3448 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003449 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003450 nextline_idx = v + 1;
3451 }
3452 else
3453 {
3454 /* Long line, use only the last SPWORDLEN bytes. */
3455 nextlinecol = v - SPWORDLEN;
3456 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3457 nextline_idx = SPWORDLEN + 1;
3458 }
3459 }
3460 }
3461#endif
3462
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003463 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003465 if (lcs_space || lcs_trail)
3466 extra_check = TRUE;
3467 /* find start of trailing whitespace */
3468 if (lcs_trail)
3469 {
3470 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003471 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003472 --trailcol;
3473 trailcol += (colnr_T) (ptr - line);
3474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476
3477 /*
3478 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3479 * first character to be displayed.
3480 */
3481 if (wp->w_p_wrap)
3482 v = wp->w_skipcol;
3483 else
3484 v = wp->w_leftcol;
3485 if (v > 0)
3486 {
3487#ifdef FEAT_MBYTE
3488 char_u *prev_ptr = ptr;
3489#endif
3490 while (vcol < v && *ptr != NUL)
3491 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003492 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 vcol += c;
3494#ifdef FEAT_MBYTE
3495 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003497 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003500 /* When:
3501 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003502 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003503 * - 'virtualedit' is set, or
3504 * - the visual mode is active,
3505 * the end of the line may be before the start of the displayed part.
3506 */
3507 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003508#ifdef FEAT_SYN_HL
3509 wp->w_p_cuc || draw_color_col ||
3510#endif
3511#ifdef FEAT_VIRTUALEDIT
3512 virtual_active() ||
3513#endif
3514 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 /* Handle a character that's not completely on the screen: Put ptr at
3520 * that character but skip the first few screen characters. */
3521 if (vcol > v)
3522 {
3523 vcol -= c;
3524#ifdef FEAT_MBYTE
3525 ptr = prev_ptr;
3526#else
3527 --ptr;
3528#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003529 /* If the character fits on the screen, don't need to skip it.
3530 * Except for a TAB. */
3531 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003532#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003533 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003534#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003535 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003536 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538
3539 /*
3540 * Adjust for when the inverted text is before the screen,
3541 * and when the start of the inverted text is before the screen.
3542 */
3543 if (tocol <= vcol)
3544 fromcol = 0;
3545 else if (fromcol >= 0 && fromcol < vcol)
3546 fromcol = vcol;
3547
3548#ifdef FEAT_LINEBREAK
3549 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3550 if (wp->w_p_wrap)
3551 need_showbreak = TRUE;
3552#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003553#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003554 /* When spell checking a word we need to figure out the start of the
3555 * word and if it's badly spelled or not. */
3556 if (has_spell)
3557 {
3558 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003559 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003560 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003561
3562 pos = wp->w_cursor;
3563 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003564 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003565 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003566
3567 /* spell_move_to() may call ml_get() and make "line" invalid */
3568 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3569 ptr = line + linecol;
3570
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003571 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003572 {
3573 /* no bad word found at line start, don't check until end of a
3574 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003575 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003576 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003577 }
3578 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003579 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003580 /* bad word found, use attributes until end of word */
3581 word_end = wp->w_cursor.col + len + 1;
3582
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003583 /* Turn index into actual attributes. */
3584 if (spell_hlf != HLF_COUNT)
3585 spell_attr = highlight_attr[spell_hlf];
3586 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003587 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003588
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003589# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003590 /* Need to restart syntax highlighting for this line. */
3591 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003592 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003593# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003594 }
3595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597
3598 /*
3599 * Correct highlighting for cursor that can't be disabled.
3600 * Avoids having to check this for each character.
3601 */
3602 if (fromcol >= 0)
3603 {
3604 if (noinvcur)
3605 {
3606 if ((colnr_T)fromcol == wp->w_virtcol)
3607 {
3608 /* highlighting starts at cursor, let it start just after the
3609 * cursor */
3610 fromcol_prev = fromcol;
3611 fromcol = -1;
3612 }
3613 else if ((colnr_T)fromcol < wp->w_virtcol)
3614 /* restart highlighting after the cursor */
3615 fromcol_prev = wp->w_virtcol;
3616 }
3617 if (fromcol >= tocol)
3618 fromcol = -1;
3619 }
3620
3621#ifdef FEAT_SEARCH_EXTRA
3622 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003623 * Handle highlighting the last used search pattern and matches.
3624 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003626 cur = wp->w_match_head;
3627 shl_flag = FALSE;
3628 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003630 if (shl_flag == FALSE)
3631 {
3632 shl = &search_hl;
3633 shl_flag = TRUE;
3634 }
3635 else
3636 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003637 shl->startcol = MAXCOL;
3638 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003640 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003641 v = (long)(ptr - line);
3642 if (cur != NULL)
3643 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003644 next_search_hl(wp, shl, lnum, (colnr_T)v,
3645 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003646
3647 /* Need to get the line again, a multi-line regexp may have made it
3648 * invalid. */
3649 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3650 ptr = line + v;
3651
3652 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003654 if (shl->lnum == lnum)
3655 shl->startcol = shl->rm.startpos[0].col;
3656 else
3657 shl->startcol = 0;
3658 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3659 - shl->rm.startpos[0].lnum)
3660 shl->endcol = shl->rm.endpos[0].col;
3661 else
3662 shl->endcol = MAXCOL;
3663 /* Highlight one character for an empty match. */
3664 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003667 if (has_mbyte && line[shl->endcol] != NUL)
3668 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3669 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003671 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 if ((long)shl->startcol < v) /* match at leftcol */
3674 {
3675 shl->attr_cur = shl->attr;
3676 search_attr = shl->attr;
3677 }
3678 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003680 if (shl != &search_hl && cur != NULL)
3681 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683#endif
3684
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003686 /* Cursor line highlighting for 'cursorline' in the current window. Not
3687 * when Visual mode is active, because it's not clear what is selected
3688 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003689 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003690 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003691 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003692 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003693 area_highlighting = TRUE;
3694 }
3695#endif
3696
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003697 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 col = 0;
3699#ifdef FEAT_RIGHTLEFT
3700 if (wp->w_p_rl)
3701 {
3702 /* Rightleft window: process the text in the normal direction, but put
3703 * it in current_ScreenLine[] from right to left. Start at the
3704 * rightmost column of the window. */
3705 col = W_WIDTH(wp) - 1;
3706 off += col;
3707 }
3708#endif
3709
3710 /*
3711 * Repeat for the whole displayed line.
3712 */
3713 for (;;)
3714 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003715#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003716 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 /* Skip this quickly when working on the text. */
3719 if (draw_state != WL_LINE)
3720 {
3721#ifdef FEAT_CMDWIN
3722 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3723 {
3724 draw_state = WL_CMDLINE;
3725 if (cmdwin_type != 0 && wp == curwin)
3726 {
3727 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003729 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003730 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 }
3733#endif
3734
3735#ifdef FEAT_FOLDING
3736 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3737 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003738 int fdc = compute_foldcolumn(wp, 0);
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003741 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003743 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003744 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003745 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003746 p_extra_free = alloc(12 + 1);
3747
3748 if (p_extra_free != NULL)
3749 {
3750 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3751 n_extra = fdc;
3752 p_extra_free[n_extra] = NUL;
3753 p_extra = p_extra_free;
3754 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003755 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 }
3759#endif
3760
3761#ifdef FEAT_SIGNS
3762 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3763 {
3764 draw_state = WL_SIGN;
3765 /* Show the sign column when there are any signs in this
3766 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003767 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003769 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003771 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772# endif
3773
3774 /* Draw two cells with the sign value or blank. */
3775 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003776 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 n_extra = 2;
3778
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003779 if (row == startrow
3780#ifdef FEAT_DIFF
3781 + filler_lines && filler_todo <= 0
3782#endif
3783 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
3785 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3786 SIGN_TEXT);
3787# ifdef FEAT_SIGN_ICONS
3788 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3789 SIGN_ICON);
3790 if (gui.in_use && icon_sign != 0)
3791 {
3792 /* Use the image in this position. */
3793 c_extra = SIGN_BYTE;
3794# ifdef FEAT_NETBEANS_INTG
3795 if (buf_signcount(wp->w_buffer, lnum) > 1)
3796 c_extra = MULTISIGN_BYTE;
3797# endif
3798 char_attr = icon_sign;
3799 }
3800 else
3801# endif
3802 if (text_sign != 0)
3803 {
3804 p_extra = sign_get_text(text_sign);
3805 if (p_extra != NULL)
3806 {
3807 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003808 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 char_attr = sign_get_attr(text_sign, FALSE);
3811 }
3812 }
3813 }
3814 }
3815#endif
3816
3817 if (draw_state == WL_NR - 1 && n_extra == 0)
3818 {
3819 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003820 /* Display the absolute or relative line number. After the
3821 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3822 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 && (row == startrow
3824#ifdef FEAT_DIFF
3825 + filler_lines
3826#endif
3827 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3828 {
3829 /* Draw the line number (empty space after wrapping). */
3830 if (row == startrow
3831#ifdef FEAT_DIFF
3832 + filler_lines
3833#endif
3834 )
3835 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003836 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003837 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003838
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003839 if (wp->w_p_nu && !wp->w_p_rnu)
3840 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003841 num = (long)lnum;
3842 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003843 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003844 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003845 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003846 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003847 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003848 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003849 num = lnum;
3850 fmt = "%-*ld ";
3851 }
3852 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003853
Bram Moolenaar700e7342013-01-30 12:31:36 +01003854 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003855 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (wp->w_skipcol > 0)
3857 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3858 *p_extra = '-';
3859#ifdef FEAT_RIGHTLEFT
3860 if (wp->w_p_rl) /* reverse line numbers */
3861 rl_mirror(extra);
3862#endif
3863 p_extra = extra;
3864 c_extra = NUL;
3865 }
3866 else
3867 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003868 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003869 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003870#ifdef FEAT_SYN_HL
3871 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003872 * the current line differently.
3873 * TODO: Can we use CursorLine instead of CursorLineNr
3874 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003875 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003876 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003877 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 }
3881
Bram Moolenaar597a4222014-06-25 14:39:50 +02003882#ifdef FEAT_LINEBREAK
3883 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3884 && n_extra == 0 && *p_sbr != NUL)
3885 /* draw indent after showbreak value */
3886 draw_state = WL_BRI;
3887 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3888 /* After the showbreak, draw the breakindent */
3889 draw_state = WL_BRI - 1;
3890
3891 /* draw 'breakindent': indent wrapped text accordingly */
3892 if (draw_state == WL_BRI - 1 && n_extra == 0)
3893 {
3894 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003895 /* if need_showbreak is set, breakindent also applies */
3896 if (wp->w_p_bri && n_extra == 0
3897 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003898# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003899 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003900# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003901 )
3902 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003903 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003904# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003905 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003906 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003907 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003908# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003909 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3910 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003911 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003912# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003913 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003914# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003915 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003916 c_extra = ' ';
3917 n_extra = get_breakindent_win(wp,
3918 ml_get_buf(wp->w_buffer, lnum, FALSE));
3919 /* Correct end of highlighted area for 'breakindent',
3920 * required when 'linebreak' is also set. */
3921 if (tocol == vcol)
3922 tocol += n_extra;
3923 }
3924 }
3925#endif
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3928 if (draw_state == WL_SBR - 1 && n_extra == 0)
3929 {
3930 draw_state = WL_SBR;
3931# ifdef FEAT_DIFF
3932 if (filler_todo > 0)
3933 {
3934 /* Draw "deleted" diff line(s). */
3935 if (char2cells(fill_diff) > 1)
3936 c_extra = '-';
3937 else
3938 c_extra = fill_diff;
3939# ifdef FEAT_RIGHTLEFT
3940 if (wp->w_p_rl)
3941 n_extra = col + 1;
3942 else
3943# endif
3944 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003945 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947# endif
3948# ifdef FEAT_LINEBREAK
3949 if (*p_sbr != NUL && need_showbreak)
3950 {
3951 /* Draw 'showbreak' at the start of each broken line. */
3952 p_extra = p_sbr;
3953 c_extra = NUL;
3954 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003955 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003957 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 /* Correct end of highlighted area for 'showbreak',
3959 * required when 'linebreak' is also set. */
3960 if (tocol == vcol)
3961 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003962#ifdef FEAT_SYN_HL
3963 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003964 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003965 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003966 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969# endif
3970 }
3971#endif
3972
3973 if (draw_state == WL_LINE - 1 && n_extra == 0)
3974 {
3975 draw_state = WL_LINE;
3976 if (saved_n_extra)
3977 {
3978 /* Continue item from end of wrapped line. */
3979 n_extra = saved_n_extra;
3980 c_extra = saved_c_extra;
3981 p_extra = saved_p_extra;
3982 char_attr = saved_char_attr;
3983 }
3984 else
3985 char_attr = 0;
3986 }
3987 }
3988
3989 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003990 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#ifdef FEAT_DIFF
3993 && filler_todo <= 0
3994#endif
3995 )
3996 {
3997 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3998 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003999 /* Pretend we have finished updating the window. Except when
4000 * 'cursorcolumn' is set. */
4001#ifdef FEAT_SYN_HL
4002 if (wp->w_p_cuc)
4003 row = wp->w_cline_row + wp->w_cline_height;
4004 else
4005#endif
4006 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 break;
4008 }
4009
4010 if (draw_state == WL_LINE && area_highlighting)
4011 {
4012 /* handle Visual or match highlighting in this line */
4013 if (vcol == fromcol
4014#ifdef FEAT_MBYTE
4015 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4016 && (*mb_ptr2cells)(ptr) > 1)
4017#endif
4018 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004019 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 && vcol < tocol))
4021 area_attr = attr; /* start highlighting */
4022 else if (area_attr != 0
4023 && (vcol == tocol
4024 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027#ifdef FEAT_SEARCH_EXTRA
4028 if (!n_extra)
4029 {
4030 /*
4031 * Check for start/end of search pattern match.
4032 * After end, check for start/end of next match.
4033 * When another match, have to check for start again.
4034 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004035 * Do this for 'search_hl' and the match list (ordered by
4036 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004038 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004039 cur = wp->w_match_head;
4040 shl_flag = FALSE;
4041 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004043 if (shl_flag == FALSE
4044 && ((cur != NULL
4045 && cur->priority > SEARCH_HL_PRIORITY)
4046 || cur == NULL))
4047 {
4048 shl = &search_hl;
4049 shl_flag = TRUE;
4050 }
4051 else
4052 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004053 if (cur != NULL)
4054 cur->pos.cur = 0;
4055 pos_inprogress = TRUE;
4056 while (shl->rm.regprog != NULL
4057 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004059 if (shl->startcol != MAXCOL
4060 && v >= (long)shl->startcol
4061 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004063#ifdef FEAT_MBYTE
4064 int tmp_col = v + MB_PTR2LEN(ptr);
4065
4066 if (shl->endcol < tmp_col)
4067 shl->endcol = tmp_col;
4068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004070#ifdef FEAT_CONCEAL
4071 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4072 == cur->hlg_id)
4073 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004074 has_match_conc =
4075 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004076 match_conc = cur->conceal_char;
4077 }
4078 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004079 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004082 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 {
4084 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004085 next_search_hl(wp, shl, lnum, (colnr_T)v,
4086 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004087 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004088 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089
4090 /* Need to get the line again, a multi-line regexp
4091 * may have made it invalid. */
4092 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4093 ptr = line + v;
4094
4095 if (shl->lnum == lnum)
4096 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004097 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004099 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004101 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004103 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 /* highlight empty match, try again after
4106 * it */
4107#ifdef FEAT_MBYTE
4108 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004109 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004110 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 else
4112#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004113 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
4116 /* Loop to check if the match starts at the
4117 * current position */
4118 continue;
4119 }
4120 }
4121 break;
4122 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004123 if (shl != &search_hl && cur != NULL)
4124 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004126
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004127 /* Use attributes from match with highest priority among
4128 * 'search_hl' and the match list. */
4129 search_attr = search_hl.attr_cur;
4130 cur = wp->w_match_head;
4131 shl_flag = FALSE;
4132 while (cur != NULL || shl_flag == FALSE)
4133 {
4134 if (shl_flag == FALSE
4135 && ((cur != NULL
4136 && cur->priority > SEARCH_HL_PRIORITY)
4137 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004138 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004139 shl = &search_hl;
4140 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004141 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004142 else
4143 shl = &cur->hl;
4144 if (shl->attr_cur != 0)
4145 search_attr = shl->attr_cur;
4146 if (shl != &search_hl && cur != NULL)
4147 cur = cur->next;
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150#endif
4151
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004153 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004155 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4156 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004158 if (diff_hlf == HLF_TXD && ptr - line > change_end
4159 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004161 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004162 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004163 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004166
4167 /* Decide which of the highlight attributes to use. */
4168 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004169#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004170 if (area_attr != 0)
4171 char_attr = hl_combine_attr(line_attr, area_attr);
4172 else if (search_attr != 0)
4173 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004174 /* Use line_attr when not in the Visual or 'incsearch' area
4175 * (area_attr may be 0 when "noinvcur" is set). */
4176 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004177 || vcol < fromcol || vcol_prev < fromcol_prev
4178 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004179 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004180#else
4181 if (area_attr != 0)
4182 char_attr = area_attr;
4183 else if (search_attr != 0)
4184 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185#endif
4186 else
4187 {
4188 attr_pri = FALSE;
4189#ifdef FEAT_SYN_HL
4190 if (has_syntax)
4191 char_attr = syntax_attr;
4192 else
4193#endif
4194 char_attr = 0;
4195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197
4198 /*
4199 * Get the next character to put on the screen.
4200 */
4201 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004202 * The "p_extra" points to the extra stuff that is inserted to
4203 * represent special characters (non-printable stuff) and other
4204 * things. When all characters are the same, c_extra is used.
4205 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4206 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4208 */
4209 if (n_extra > 0)
4210 {
4211 if (c_extra != NUL)
4212 {
4213 c = c_extra;
4214#ifdef FEAT_MBYTE
4215 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004216 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
4218 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004220 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 else
4223 mb_utf8 = FALSE;
4224#endif
4225 }
4226 else
4227 {
4228 c = *p_extra;
4229#ifdef FEAT_MBYTE
4230 if (has_mbyte)
4231 {
4232 mb_c = c;
4233 if (enc_utf8)
4234 {
4235 /* If the UTF-8 character is more than one byte:
4236 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004237 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 mb_utf8 = FALSE;
4239 if (mb_l > n_extra)
4240 mb_l = 1;
4241 else if (mb_l > 1)
4242 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004243 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004245 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 }
4248 else
4249 {
4250 /* if this is a DBCS character, put it in "mb_c" */
4251 mb_l = MB_BYTE2LEN(c);
4252 if (mb_l >= n_extra)
4253 mb_l = 1;
4254 else if (mb_l > 1)
4255 mb_c = (c << 8) + p_extra[1];
4256 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004257 if (mb_l == 0) /* at the NUL at end-of-line */
4258 mb_l = 1;
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 /* If a double-width char doesn't fit display a '>' in the
4261 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004262 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263# ifdef FEAT_RIGHTLEFT
4264 wp->w_p_rl ? (col <= 0) :
4265# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004266 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 && (*mb_char2cells)(mb_c) == 2)
4268 {
4269 c = '>';
4270 mb_c = c;
4271 mb_l = 1;
4272 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004273 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 /* put the pointer back to output the double-width
4275 * character at the start of the next line. */
4276 ++n_extra;
4277 --p_extra;
4278 }
4279 else
4280 {
4281 n_extra -= mb_l - 1;
4282 p_extra += mb_l - 1;
4283 }
4284 }
4285#endif
4286 ++p_extra;
4287 }
4288 --n_extra;
4289 }
4290 else
4291 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004292#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004293 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004294#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004295
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004296 if (p_extra_free != NULL)
4297 {
4298 vim_free(p_extra_free);
4299 p_extra_free = NULL;
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 /*
4302 * Get a character from the line itself.
4303 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004304 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004305#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004306 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_MBYTE
4309 if (has_mbyte)
4310 {
4311 mb_c = c;
4312 if (enc_utf8)
4313 {
4314 /* If the UTF-8 character is more than one byte: Decode it
4315 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004316 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 mb_utf8 = FALSE;
4318 if (mb_l > 1)
4319 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004320 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /* Overlong encoded ASCII or ASCII with composing char
4322 * is displayed normally, except a NUL. */
4323 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324 {
4325 c = mb_c;
4326# ifdef FEAT_LINEBREAK
4327 c0 = mb_c;
4328# endif
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004331
4332 /* At start of the line we can have a composing char.
4333 * Draw it as a space with a composing char. */
4334 if (utf_iscomposing(mb_c))
4335 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004336 int i;
4337
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004338 for (i = Screen_mco - 1; i > 0; --i)
4339 u8cc[i] = u8cc[i - 1];
4340 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004341 mb_c = ' ';
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344
4345 if ((mb_l == 1 && c >= 0x80)
4346 || (mb_l >= 1 && mb_c == 0)
4347 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004348# ifdef UNICODE16
4349 || mb_c >= 0x10000
4350# endif
4351 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 /*
4354 * Illegal UTF-8 byte: display as <xx>.
4355 * Non-BMP character : display as ? or fullwidth ?.
4356 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004357# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
4361 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004362# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 if (wp->w_p_rl) /* reverse */
4364 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004367# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 else if (utf_char2cells(mb_c) != 2)
4369 STRCPY(extra, "?");
4370 else
4371 /* 0xff1f in UTF-8: full-width '?' */
4372 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375 p_extra = extra;
4376 c = *p_extra;
4377 mb_c = mb_ptr2char_adv(&p_extra);
4378 mb_utf8 = (c >= 0x80);
4379 n_extra = (int)STRLEN(p_extra);
4380 c_extra = NUL;
4381 if (area_attr == 0 && search_attr == 0)
4382 {
4383 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004384 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 saved_attr2 = char_attr; /* save current attr */
4386 }
4387 }
4388 else if (mb_l == 0) /* at the NUL at end-of-line */
4389 mb_l = 1;
4390#ifdef FEAT_ARABIC
4391 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4392 {
4393 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004394 int pc, pc1, nc;
4395 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 /* The idea of what is the previous and next
4398 * character depends on 'rightleft'. */
4399 if (wp->w_p_rl)
4400 {
4401 pc = prev_c;
4402 pc1 = prev_c1;
4403 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004404 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 else
4407 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004408 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 prev_c = mb_c;
4413
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 else
4417 prev_c = mb_c;
4418#endif
4419 }
4420 else /* enc_dbcs */
4421 {
4422 mb_l = MB_BYTE2LEN(c);
4423 if (mb_l == 0) /* at the NUL at end-of-line */
4424 mb_l = 1;
4425 else if (mb_l > 1)
4426 {
4427 /* We assume a second byte below 32 is illegal.
4428 * Hopefully this is OK for all double-byte encodings!
4429 */
4430 if (ptr[1] >= 32)
4431 mb_c = (c << 8) + ptr[1];
4432 else
4433 {
4434 if (ptr[1] == NUL)
4435 {
4436 /* head byte at end of line */
4437 mb_l = 1;
4438 transchar_nonprint(extra, c);
4439 }
4440 else
4441 {
4442 /* illegal tail byte */
4443 mb_l = 2;
4444 STRCPY(extra, "XX");
4445 }
4446 p_extra = extra;
4447 n_extra = (int)STRLEN(extra) - 1;
4448 c_extra = NUL;
4449 c = *p_extra++;
4450 if (area_attr == 0 && search_attr == 0)
4451 {
4452 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004453 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 saved_attr2 = char_attr; /* save current attr */
4455 }
4456 mb_c = c;
4457 }
4458 }
4459 }
4460 /* If a double-width char doesn't fit display a '>' in the
4461 * last column; the character is displayed at the start of the
4462 * next line. */
4463 if ((
4464# ifdef FEAT_RIGHTLEFT
4465 wp->w_p_rl ? (col <= 0) :
4466# endif
4467 (col >= W_WIDTH(wp) - 1))
4468 && (*mb_char2cells)(mb_c) == 2)
4469 {
4470 c = '>';
4471 mb_c = c;
4472 mb_utf8 = FALSE;
4473 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004474 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 /* Put pointer back so that the character will be
4476 * displayed at the start of the next line. */
4477 --ptr;
4478 }
4479 else if (*ptr != NUL)
4480 ptr += mb_l - 1;
4481
4482 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004483 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004484 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004485 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004488 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 c = ' ';
4490 if (area_attr == 0 && search_attr == 0)
4491 {
4492 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004493 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 saved_attr2 = char_attr; /* save current attr */
4495 }
4496 mb_c = c;
4497 mb_utf8 = FALSE;
4498 mb_l = 1;
4499 }
4500
4501 }
4502#endif
4503 ++ptr;
4504
4505 if (extra_check)
4506 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004507#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004508 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004509#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004511#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /* Get syntax attribute, unless still at the start of the line
4513 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004514 v = (long)(ptr - line);
4515 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 /* Get the syntax attribute for the character. If there
4518 * is an error, disable syntax highlighting. */
4519 save_did_emsg = did_emsg;
4520 did_emsg = FALSE;
4521
Bram Moolenaar217ad922005-03-20 22:37:15 +00004522 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004523# ifdef FEAT_SPELL
4524 has_spell ? &can_spell :
4525# endif
4526 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004529 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004530 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004531 has_syntax = FALSE;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else
4534 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004535#ifdef SYN_TIME_LIMIT
4536 if (wp->w_s->b_syn_slow)
4537 has_syntax = FALSE;
4538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 /* Need to get the line again, a multi-line regexp may
4541 * have made it invalid. */
4542 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4543 ptr = line + v;
4544
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004545 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004547 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004548 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004549# ifdef FEAT_CONCEAL
4550 /* no concealing past the end of the line, it interferes
4551 * with line highlighting */
4552 if (c == NUL)
4553 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004554 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004555 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004558#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004561 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004562 * Only do this when there is no syntax highlighting, the
4563 * @Spell cluster is not used or the current syntax item
4564 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004565 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004566 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004567 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004568# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004569 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004570 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004571# endif
4572 if (c != 0 && (
4573# ifdef FEAT_SYN_HL
4574 !has_syntax ||
4575# endif
4576 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004577 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004578 char_u *prev_ptr, *p;
4579 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004580 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004581# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004582 if (has_mbyte)
4583 {
4584 prev_ptr = ptr - mb_l;
4585 v -= mb_l - 1;
4586 }
4587 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004588# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004589 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004590
4591 /* Use nextline[] if possible, it has the start of the
4592 * next line concatenated. */
4593 if ((prev_ptr - line) - nextlinecol >= 0)
4594 p = nextline + (prev_ptr - line) - nextlinecol;
4595 else
4596 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004597 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004598 len = spell_check(wp, p, &spell_hlf, &cap_col,
4599 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004600 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004601
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004602 /* In Insert mode only highlight a word that
4603 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004604 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004605 && (State & INSERT) != 0
4606 && wp->w_cursor.lnum == lnum
4607 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004609 && wp->w_cursor.col < (colnr_T)word_end)
4610 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004611 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004612 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004613 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004614
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004615 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004616 && (p - nextline) + len > nextline_idx)
4617 {
4618 /* Remember that the good word continues at the
4619 * start of the next line. */
4620 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004621 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004622 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004623
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004624 /* Turn index into actual attributes. */
4625 if (spell_hlf != HLF_COUNT)
4626 spell_attr = highlight_attr[spell_hlf];
4627
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004628 if (cap_col > 0)
4629 {
4630 if (p != prev_ptr
4631 && (p - nextline) + cap_col >= nextline_idx)
4632 {
4633 /* Remember that the word in the next line
4634 * must start with a capital. */
4635 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004636 cap_col = (int)((p - nextline) + cap_col
4637 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004638 }
4639 else
4640 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004641 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004642 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004644 }
4645 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004646 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004647 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004648 char_attr = hl_combine_attr(char_attr, spell_attr);
4649 else
4650 char_attr = hl_combine_attr(spell_attr, char_attr);
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#endif
4653#ifdef FEAT_LINEBREAK
4654 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004657 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004658 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004660# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004661 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004662# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004663 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004665 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004667 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004668
Bram Moolenaar597a4222014-06-25 14:39:50 +02004669 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004670 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004671 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004672 if (c == TAB && n_extra + col > W_WIDTH(wp))
4673 n_extra = (int)wp->w_buffer->b_p_ts
4674 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4675
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004676# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004677 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004678# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004680# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004681 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004682 {
4683#ifdef FEAT_CONCEAL
4684 if (c == TAB)
4685 /* See "Tab alignment" below. */
4686 FIX_FOR_BOGUSCOLS;
4687#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004688 if (!wp->w_p_list)
4689 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692#endif
4693
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004694 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4695 */
4696 if (wp->w_p_list
4697 && (((c == 160
4698#ifdef FEAT_MBYTE
4699 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4700#endif
4701 ) && lcs_nbsp)
4702 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4703 {
4704 c = (c == ' ') ? lcs_space : lcs_nbsp;
4705 if (area_attr == 0 && search_attr == 0)
4706 {
4707 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004708 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004709 saved_attr2 = char_attr; /* save current attr */
4710 }
4711#ifdef FEAT_MBYTE
4712 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004713 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004714 {
4715 mb_utf8 = TRUE;
4716 u8cc[0] = 0;
4717 c = 0xc0;
4718 }
4719 else
4720 mb_utf8 = FALSE;
4721#endif
4722 }
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4725 {
4726 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004727 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004730 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 saved_attr2 = char_attr; /* save current attr */
4732 }
4733#ifdef FEAT_MBYTE
4734 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004735 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
4737 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004738 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004739 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741 else
4742 mb_utf8 = FALSE;
4743#endif
4744 }
4745 }
4746
4747 /*
4748 * Handling of non-printable characters.
4749 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004750 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 /*
4753 * when getting a character from the file, we may have to
4754 * turn it into something else on the way to putting it
4755 * into "ScreenLines".
4756 */
4757 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4758 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004759 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004760 long vcol_adjusted = vcol; /* removed showbreak length */
4761#ifdef FEAT_LINEBREAK
4762 /* only adjust the tab_len, when at the first column
4763 * after the showbreak value was drawn */
4764 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4765 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004768 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004769 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4770
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004771#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004772 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004773#endif
4774 /* tab amount depends on current column */
4775 n_extra = tab_len;
4776#ifdef FEAT_LINEBREAK
4777 else
4778 {
4779 char_u *p;
4780 int len = n_extra;
4781 int i;
4782 int saved_nextra = n_extra;
4783
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004784#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004785 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004786 /* there are characters to conceal */
4787 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004788 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4789 */
4790 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4791 && n_extra > tab_len)
4792 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004793#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004794
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004795 /* if n_extra > 0, it gives the number of chars, to
4796 * use for a tab, else we need to calculate the width
4797 * for a tab */
4798#ifdef FEAT_MBYTE
4799 len = (tab_len * mb_char2len(lcs_tab2));
4800 if (n_extra > 0)
4801 len += n_extra - tab_len;
4802#endif
4803 c = lcs_tab1;
4804 p = alloc((unsigned)(len + 1));
4805 vim_memset(p, ' ', len);
4806 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004807 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808 p_extra_free = p;
4809 for (i = 0; i < tab_len; i++)
4810 {
4811#ifdef FEAT_MBYTE
4812 mb_char2bytes(lcs_tab2, p);
4813 p += mb_char2len(lcs_tab2);
4814 n_extra += mb_char2len(lcs_tab2)
4815 - (saved_nextra > 0 ? 1 : 0);
4816#else
4817 p[i] = lcs_tab2;
4818#endif
4819 }
4820 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004821#ifdef FEAT_CONCEAL
4822 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4823 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004824 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004825 n_extra -= vcol_off;
4826#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004827 }
4828#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004829#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004830 {
4831 int vc_saved = vcol_off;
4832
4833 /* Tab alignment should be identical regardless of
4834 * 'conceallevel' value. So tab compensates of all
4835 * previous concealed characters, and thus resets
4836 * vcol_off and boguscols accumulated so far in the
4837 * line. Note that the tab can be longer than
4838 * 'tabstop' when there are concealed characters. */
4839 FIX_FOR_BOGUSCOLS;
4840
4841 /* Make sure, the highlighting for the tab char will be
4842 * correctly set further below (effectively reverts the
4843 * FIX_FOR_BOGSUCOLS macro */
4844 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004845 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004846 tab_len += vc_saved;
4847 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849#ifdef FEAT_MBYTE
4850 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4851#endif
4852 if (wp->w_p_list)
4853 {
4854 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004855#ifdef FEAT_LINEBREAK
4856 if (wp->w_p_lbr)
4857 c_extra = NUL; /* using p_extra from above */
4858 else
4859#endif
4860 c_extra = lcs_tab2;
4861 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004862 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 saved_attr2 = char_attr; /* save current attr */
4864#ifdef FEAT_MBYTE
4865 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004866 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004869 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004870 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872#endif
4873 }
4874 else
4875 {
4876 c_extra = ' ';
4877 c = ' ';
4878 }
4879 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004880 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004881 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004882 || ((fromcol >= 0 || fromcol_prev >= 0)
4883 && tocol > vcol
4884 && VIsual_mode != Ctrl_V
4885 && (
4886# ifdef FEAT_RIGHTLEFT
4887 wp->w_p_rl ? (col >= 0) :
4888# endif
4889 (col < W_WIDTH(wp)))
4890 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004891 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004892 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004893 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004895 /* Display a '$' after the line or highlight an extra
4896 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4898 /* For a diff line the highlighting continues after the
4899 * "$". */
4900 if (
4901# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004902 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903# ifdef LINE_ATTR
4904 &&
4905# endif
4906# endif
4907# ifdef LINE_ATTR
4908 line_attr == 0
4909# endif
4910 )
4911#endif
4912 {
4913#ifdef FEAT_VIRTUALEDIT
4914 /* In virtualedit, visual selections may extend
4915 * beyond end of line. */
4916 if (area_highlighting && virtual_active()
4917 && tocol != MAXCOL && vcol < tocol)
4918 n_extra = 0;
4919 else
4920#endif
4921 {
4922 p_extra = at_end_str;
4923 n_extra = 1;
4924 c_extra = NUL;
4925 }
4926 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004927 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004928 c = lcs_eol;
4929 else
4930 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 lcs_eol_one = -1;
4932 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004933 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004935 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 n_attr = 1;
4937 }
4938#ifdef FEAT_MBYTE
4939 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004940 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004943 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004944 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 else
4947 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4948#endif
4949 }
4950 else if (c != NUL)
4951 {
4952 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004953 if (n_extra == 0)
4954 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#ifdef FEAT_RIGHTLEFT
4956 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4957 rl_mirror(p_extra); /* reverse "<12>" */
4958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004960#ifdef FEAT_LINEBREAK
4961 if (wp->w_p_lbr)
4962 {
4963 char_u *p;
4964
4965 c = *p_extra;
4966 p = alloc((unsigned)n_extra + 1);
4967 vim_memset(p, ' ', n_extra);
4968 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4969 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004970 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004971 p_extra_free = p_extra = p;
4972 }
4973 else
4974#endif
4975 {
4976 n_extra = byte2cells(c) - 1;
4977 c = *p_extra++;
4978 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004979 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004982 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 saved_attr2 = char_attr; /* save current attr */
4984 }
4985#ifdef FEAT_MBYTE
4986 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4987#endif
4988 }
4989#ifdef FEAT_VIRTUALEDIT
4990 else if (VIsual_active
4991 && (VIsual_mode == Ctrl_V
4992 || VIsual_mode == 'v')
4993 && virtual_active()
4994 && tocol != MAXCOL
4995 && vcol < tocol
4996 && (
4997# ifdef FEAT_RIGHTLEFT
4998 wp->w_p_rl ? (col >= 0) :
4999# endif
5000 (col < W_WIDTH(wp))))
5001 {
5002 c = ' ';
5003 --ptr; /* put it back at the NUL */
5004 }
5005#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005006#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 else if ((
5008# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005009 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 ) && (
5013# ifdef FEAT_RIGHTLEFT
5014 wp->w_p_rl ? (col >= 0) :
5015# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005016 (col
5017# ifdef FEAT_CONCEAL
5018 - boguscols
5019# endif
5020 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 /* Highlight until the right side of the window */
5023 c = ' ';
5024 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005025
5026 /* Remember we do the char for line highlighting. */
5027 ++did_line_attr;
5028
5029 /* don't do search HL for the rest of the line */
5030 if (line_attr != 0 && char_attr == search_attr && col > 0)
5031 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032# ifdef FEAT_DIFF
5033 if (diff_hlf == HLF_TXD)
5034 {
5035 diff_hlf = HLF_CHD;
5036 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005037 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005038 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005039 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5040 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005041 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044# endif
5045 }
5046#endif
5047 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005048
5049#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005050 if ( wp->w_p_cole > 0
5051 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005052 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005053 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005054 && !(lnum_in_visual_area
5055 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005056 {
5057 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005058 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005059 && (syn_get_sub_char() != NUL || match_conc
5060 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005061 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005062 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005063 /* First time at this concealed item: display one
5064 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005065 if (match_conc)
5066 c = match_conc;
5067 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005068 c = syn_get_sub_char();
5069 else if (lcs_conceal != NUL)
5070 c = lcs_conceal;
5071 else
5072 c = ' ';
5073
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005074 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005075
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005076 if (n_extra > 0)
5077 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005078 vcol += n_extra;
5079 if (wp->w_p_wrap && n_extra > 0)
5080 {
5081# ifdef FEAT_RIGHTLEFT
5082 if (wp->w_p_rl)
5083 {
5084 col -= n_extra;
5085 boguscols -= n_extra;
5086 }
5087 else
5088# endif
5089 {
5090 boguscols += n_extra;
5091 col += n_extra;
5092 }
5093 }
5094 n_extra = 0;
5095 n_attr = 0;
5096 }
5097 else if (n_skip == 0)
5098 {
5099 is_concealing = TRUE;
5100 n_skip = 1;
5101 }
5102# ifdef FEAT_MBYTE
5103 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005104 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005105 {
5106 mb_utf8 = TRUE;
5107 u8cc[0] = 0;
5108 c = 0xc0;
5109 }
5110 else
5111 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5112# endif
5113 }
5114 else
5115 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005116 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005117 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005118 }
5119#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005122#ifdef FEAT_CONCEAL
5123 /* In the cursor line and we may be concealing characters: correct
5124 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005125 if (!did_wcol && draw_state == WL_LINE
5126 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005127 && conceal_cursor_line(wp)
5128 && (int)wp->w_virtcol <= vcol + n_skip)
5129 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005130# ifdef FEAT_RIGHTLEFT
5131 if (wp->w_p_rl)
5132 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5133 else
5134# endif
5135 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005136 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005137 did_wcol = TRUE;
5138 }
5139#endif
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 /* Don't override visual selection highlighting. */
5142 if (n_attr > 0
5143 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005144 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 char_attr = extra_attr;
5146
Bram Moolenaar81695252004-12-29 20:58:21 +00005147#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 /* XIM don't send preedit_start and preedit_end, but they send
5149 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5150 * im_is_preediting() here. */
5151 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005152 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 && (State & INSERT)
5154 && !p_imdisable
5155 && im_is_preediting()
5156 && draw_state == WL_LINE)
5157 {
5158 colnr_T tcol;
5159
5160 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005161 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
5163 tcol = preedit_end_col;
5164 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5165 {
5166 if (feedback_old_attr < 0)
5167 {
5168 feedback_col = 0;
5169 feedback_old_attr = char_attr;
5170 }
5171 char_attr = im_get_feedback_attr(feedback_col);
5172 if (char_attr < 0)
5173 char_attr = feedback_old_attr;
5174 feedback_col++;
5175 }
5176 else if (feedback_old_attr >= 0)
5177 {
5178 char_attr = feedback_old_attr;
5179 feedback_old_attr = -1;
5180 feedback_col = 0;
5181 }
5182 }
5183#endif
5184 /*
5185 * Handle the case where we are in column 0 but not on the first
5186 * character of the line and the user wants us to show us a
5187 * special character (via 'listchars' option "precedes:<char>".
5188 */
5189 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005190 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5192#ifdef FEAT_DIFF
5193 && filler_todo <= 0
5194#endif
5195 && draw_state > WL_NR
5196 && c != NUL)
5197 {
5198 c = lcs_prec;
5199 lcs_prec_todo = NUL;
5200#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005201 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5202 {
5203 /* Double-width character being overwritten by the "precedes"
5204 * character, need to fill up half the character. */
5205 c_extra = MB_FILLER_CHAR;
5206 n_extra = 1;
5207 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005208 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005211 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005214 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005215 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 else
5218 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5219#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005220 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005223 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 n_attr3 = 1;
5225 }
5226 }
5227
5228 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005229 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005231 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005232#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005233 || did_line_attr == 1
5234#endif
5235 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005237#ifdef FEAT_SEARCH_EXTRA
5238 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005239
5240 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005241 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005242 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005243#endif
5244
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005245 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 * highlight match at end of line. If it's beyond the last
5247 * char on the screen, just overwrite that one (tricky!) Not
5248 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005249#ifdef FEAT_SEARCH_EXTRA
5250 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005251 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005252 prevcol_hl_flag = TRUE;
5253 else
5254 {
5255 cur = wp->w_match_head;
5256 while (cur != NULL)
5257 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005258 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005259 {
5260 prevcol_hl_flag = TRUE;
5261 break;
5262 }
5263 cur = cur->next;
5264 }
5265 }
5266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005268 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005269 && (VIsual_mode != Ctrl_V
5270 || lnum == VIsual.lnum
5271 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005272 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#ifdef FEAT_SEARCH_EXTRA
5274 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005275 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005276# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005277 && did_line_attr <= 1
5278# endif
5279 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280#endif
5281 ))
5282 {
5283 int n = 0;
5284
5285#ifdef FEAT_RIGHTLEFT
5286 if (wp->w_p_rl)
5287 {
5288 if (col < 0)
5289 n = 1;
5290 }
5291 else
5292#endif
5293 {
5294 if (col >= W_WIDTH(wp))
5295 n = -1;
5296 }
5297 if (n != 0)
5298 {
5299 /* At the window boundary, highlight the last character
5300 * instead (better than nothing). */
5301 off += n;
5302 col += n;
5303 }
5304 else
5305 {
5306 /* Add a blank character to highlight. */
5307 ScreenLines[off] = ' ';
5308#ifdef FEAT_MBYTE
5309 if (enc_utf8)
5310 ScreenLinesUC[off] = 0;
5311#endif
5312 }
5313#ifdef FEAT_SEARCH_EXTRA
5314 if (area_attr == 0)
5315 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005316 /* Use attributes from match with highest priority among
5317 * 'search_hl' and the match list. */
5318 char_attr = search_hl.attr;
5319 cur = wp->w_match_head;
5320 shl_flag = FALSE;
5321 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005322 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005323 if (shl_flag == FALSE
5324 && ((cur != NULL
5325 && cur->priority > SEARCH_HL_PRIORITY)
5326 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005327 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005328 shl = &search_hl;
5329 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005330 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005331 else
5332 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005333 if ((ptr - line) - 1 == (long)shl->startcol
5334 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335 char_attr = shl->attr;
5336 if (shl != &search_hl && cur != NULL)
5337 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340#endif
5341 ScreenAttrs[off] = char_attr;
5342#ifdef FEAT_RIGHTLEFT
5343 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005346 --off;
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 else
5349#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005352 ++off;
5353 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005354 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005355#ifdef FEAT_SYN_HL
5356 eol_hl_off = 1;
5357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
Bram Moolenaar91170f82006-05-05 21:15:17 +00005361 /*
5362 * At end of the text line.
5363 */
5364 if (c == NUL)
5365 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005366#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005367 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5368 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005369 {
5370 /* highlight last char after line */
5371 --col;
5372 --off;
5373 --vcol;
5374 }
5375
Bram Moolenaar1a384422010-07-14 19:53:30 +02005376 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005377 if (wp->w_p_wrap)
5378 v = wp->w_skipcol;
5379 else
5380 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005381
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005382 /* check if line ends before left margin */
5383 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005384 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005385#ifdef FEAT_CONCEAL
5386 /* Get rid of the boguscols now, we want to draw until the right
5387 * edge for 'cursorcolumn'. */
5388 col -= boguscols;
5389 boguscols = 0;
5390#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005391
5392 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005393 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005394
5395 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005396 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5397 && (int)wp->w_virtcol <
5398 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005399 && lnum != wp->w_cursor.lnum)
5400 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005401# ifdef FEAT_RIGHTLEFT
5402 && !wp->w_p_rl
5403# endif
5404 )
5405 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005406 int rightmost_vcol = 0;
5407 int i;
5408
5409 if (wp->w_p_cuc)
5410 rightmost_vcol = wp->w_virtcol;
5411 if (draw_color_col)
5412 /* determine rightmost colorcolumn to possibly draw */
5413 for (i = 0; color_cols[i] >= 0; ++i)
5414 if (rightmost_vcol < color_cols[i])
5415 rightmost_vcol = color_cols[i];
5416
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005417 while (col < W_WIDTH(wp))
5418 {
5419 ScreenLines[off] = ' ';
5420#ifdef FEAT_MBYTE
5421 if (enc_utf8)
5422 ScreenLinesUC[off] = 0;
5423#endif
5424 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005425 if (draw_color_col)
5426 draw_color_col = advance_color_col(VCOL_HLC,
5427 &color_cols);
5428
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005429 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005430 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005431 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005432 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005433 else
5434 ScreenAttrs[off++] = 0;
5435
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005436 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005437 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005438
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005439 ++vcol;
5440 }
5441 }
5442#endif
5443
Bram Moolenaar860cae12010-06-05 23:22:07 +02005444 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5445 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 row++;
5447
5448 /*
5449 * Update w_cline_height and w_cline_folded if the cursor line was
5450 * updated (saves a call to plines() later).
5451 */
5452 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5453 {
5454 curwin->w_cline_row = startrow;
5455 curwin->w_cline_height = row - startrow;
5456#ifdef FEAT_FOLDING
5457 curwin->w_cline_folded = FALSE;
5458#endif
5459 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5460 }
5461
5462 break;
5463 }
5464
5465 /* line continues beyond line end */
5466 if (lcs_ext
5467 && !wp->w_p_wrap
5468#ifdef FEAT_DIFF
5469 && filler_todo <= 0
5470#endif
5471 && (
5472#ifdef FEAT_RIGHTLEFT
5473 wp->w_p_rl ? col == 0 :
5474#endif
5475 col == W_WIDTH(wp) - 1)
5476 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005477 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5479 {
5480 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005481 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#ifdef FEAT_MBYTE
5483 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005484 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 {
5486 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005487 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005488 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490 else
5491 mb_utf8 = FALSE;
5492#endif
5493 }
5494
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005495#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005496 /* advance to the next 'colorcolumn' */
5497 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005499
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005500 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005501 * highlight the cursor position itself.
5502 * Also highlight the 'colorcolumn' if it is different than
5503 * 'cursorcolumn' */
5504 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005505 if (draw_state == WL_LINE && !lnum_in_visual_area
5506 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005507 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005508 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005509 && lnum != wp->w_cursor.lnum)
5510 {
5511 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005512 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005513 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005514 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 {
5516 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005517 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005518 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005520#endif
5521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 /*
5523 * Store character to be displayed.
5524 * Skip characters that are left of the screen for 'nowrap'.
5525 */
5526 vcol_prev = vcol;
5527 if (draw_state < WL_LINE || n_skip <= 0)
5528 {
5529 /*
5530 * Store the character.
5531 */
5532#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5533 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5534 {
5535 /* A double-wide character is: put first halve in left cell. */
5536 --off;
5537 --col;
5538 }
5539#endif
5540 ScreenLines[off] = c;
5541#ifdef FEAT_MBYTE
5542 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005543 {
5544 if ((mb_c & 0xff00) == 0x8e00)
5545 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 else if (enc_utf8)
5549 {
5550 if (mb_utf8)
5551 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005552 int i;
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005555 if ((c & 0xff) == 0)
5556 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005557 for (i = 0; i < Screen_mco; ++i)
5558 {
5559 ScreenLinesC[i][off] = u8cc[i];
5560 if (u8cc[i] == 0)
5561 break;
5562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 else
5565 ScreenLinesUC[off] = 0;
5566 }
5567 if (multi_attr)
5568 {
5569 ScreenAttrs[off] = multi_attr;
5570 multi_attr = 0;
5571 }
5572 else
5573#endif
5574 ScreenAttrs[off] = char_attr;
5575
5576#ifdef FEAT_MBYTE
5577 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5578 {
5579 /* Need to fill two screen columns. */
5580 ++off;
5581 ++col;
5582 if (enc_utf8)
5583 /* UTF-8: Put a 0 in the second screen char. */
5584 ScreenLines[off] = 0;
5585 else
5586 /* DBCS: Put second byte in the second screen char. */
5587 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005588 if (draw_state > WL_NR
5589#ifdef FEAT_DIFF
5590 && filler_todo <= 0
5591#endif
5592 )
5593 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 /* When "tocol" is halfway a character, set it to the end of
5595 * the character, otherwise highlighting won't stop. */
5596 if (tocol == vcol)
5597 ++tocol;
5598#ifdef FEAT_RIGHTLEFT
5599 if (wp->w_p_rl)
5600 {
5601 /* now it's time to backup one cell */
5602 --off;
5603 --col;
5604 }
5605#endif
5606 }
5607#endif
5608#ifdef FEAT_RIGHTLEFT
5609 if (wp->w_p_rl)
5610 {
5611 --off;
5612 --col;
5613 }
5614 else
5615#endif
5616 {
5617 ++off;
5618 ++col;
5619 }
5620 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005621#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005622 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005623 {
5624 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005625 ++vcol_off;
5626 if (n_extra > 0)
5627 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005628 if (wp->w_p_wrap)
5629 {
5630 /*
5631 * Special voodoo required if 'wrap' is on.
5632 *
5633 * Advance the column indicator to force the line
5634 * drawing to wrap early. This will make the line
5635 * take up the same screen space when parts are concealed,
5636 * so that cursor line computations aren't messed up.
5637 *
5638 * To avoid the fictitious advance of 'col' causing
5639 * trailing junk to be written out of the screen line
5640 * we are building, 'boguscols' keeps track of the number
5641 * of bad columns we have advanced.
5642 */
5643 if (n_extra > 0)
5644 {
5645 vcol += n_extra;
5646# ifdef FEAT_RIGHTLEFT
5647 if (wp->w_p_rl)
5648 {
5649 col -= n_extra;
5650 boguscols -= n_extra;
5651 }
5652 else
5653# endif
5654 {
5655 col += n_extra;
5656 boguscols += n_extra;
5657 }
5658 n_extra = 0;
5659 n_attr = 0;
5660 }
5661
5662
5663# ifdef FEAT_MBYTE
5664 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5665 {
5666 /* Need to fill two screen columns. */
5667# ifdef FEAT_RIGHTLEFT
5668 if (wp->w_p_rl)
5669 {
5670 --boguscols;
5671 --col;
5672 }
5673 else
5674# endif
5675 {
5676 ++boguscols;
5677 ++col;
5678 }
5679 }
5680# endif
5681
5682# ifdef FEAT_RIGHTLEFT
5683 if (wp->w_p_rl)
5684 {
5685 --boguscols;
5686 --col;
5687 }
5688 else
5689# endif
5690 {
5691 ++boguscols;
5692 ++col;
5693 }
5694 }
5695 else
5696 {
5697 if (n_extra > 0)
5698 {
5699 vcol += n_extra;
5700 n_extra = 0;
5701 n_attr = 0;
5702 }
5703 }
5704
5705 }
5706#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708 --n_skip;
5709
Bram Moolenaar64486672010-05-16 15:46:46 +02005710 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5711 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005712 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713#ifdef FEAT_DIFF
5714 && filler_todo <= 0
5715#endif
5716 )
5717 ++vcol;
5718
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005719#ifdef FEAT_SYN_HL
5720 if (vcol_save_attr >= 0)
5721 char_attr = vcol_save_attr;
5722#endif
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 /* restore attributes after "predeces" in 'listchars' */
5725 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5726 char_attr = saved_attr3;
5727
5728 /* restore attributes after last 'listchars' or 'number' char */
5729 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5730 char_attr = saved_attr2;
5731
5732 /*
5733 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005734 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 if ((
5737#ifdef FEAT_RIGHTLEFT
5738 wp->w_p_rl ? (col < 0) :
5739#endif
5740 (col >= W_WIDTH(wp)))
5741 && (*ptr != NUL
5742#ifdef FEAT_DIFF
5743 || filler_todo > 0
5744#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005745 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5747 )
5748 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005749#ifdef FEAT_CONCEAL
5750 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5751 (int)W_WIDTH(wp), wp->w_p_rl);
5752 boguscols = 0;
5753#else
5754 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5755 (int)W_WIDTH(wp), wp->w_p_rl);
5756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 ++row;
5758 ++screen_row;
5759
5760 /* When not wrapping and finished diff lines, or when displayed
5761 * '$' and highlighting until last column, break here. */
5762 if ((!wp->w_p_wrap
5763#ifdef FEAT_DIFF
5764 && filler_todo <= 0
5765#endif
5766 ) || lcs_eol_one == -1)
5767 break;
5768
5769 /* When the window is too narrow draw all "@" lines. */
5770 if (draw_state != WL_LINE
5771#ifdef FEAT_DIFF
5772 && filler_todo <= 0
5773#endif
5774 )
5775 {
5776 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005777#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 draw_vsep_win(wp, row);
5779#endif
5780 row = endrow;
5781 }
5782
5783 /* When line got too long for screen break here. */
5784 if (row == endrow)
5785 {
5786 ++row;
5787 break;
5788 }
5789
5790 if (screen_cur_row == screen_row - 1
5791#ifdef FEAT_DIFF
5792 && filler_todo <= 0
5793#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005794#ifdef FEAT_WINDOWS
5795 && W_WIDTH(wp) == Columns
5796#endif
5797 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
5799 /* Remember that the line wraps, used for modeless copy. */
5800 LineWraps[screen_row - 1] = TRUE;
5801
5802 /*
5803 * Special trick to make copy/paste of wrapped lines work with
5804 * xterm/screen: write an extra character beyond the end of
5805 * the line. This will work with all terminal types
5806 * (regardless of the xn,am settings).
5807 * Only do this on a fast tty.
5808 * Only do this if the cursor is on the current line
5809 * (something has been written in it).
5810 * Don't do this for the GUI.
5811 * Don't do this for double-width characters.
5812 * Don't do this for a window not at the right screen border.
5813 */
5814 if (p_tf
5815#ifdef FEAT_GUI
5816 && !gui.in_use
5817#endif
5818#ifdef FEAT_MBYTE
5819 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005820 && ((*mb_off2cells)(LineOffset[screen_row],
5821 LineOffset[screen_row] + screen_Columns)
5822 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005824 + (int)Columns - 2,
5825 LineOffset[screen_row] + screen_Columns)
5826 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
5828 )
5829 {
5830 /* First make sure we are at the end of the screen line,
5831 * then output the same character again to let the
5832 * terminal know about the wrap. If the terminal doesn't
5833 * auto-wrap, we overwrite the character. */
5834 if (screen_cur_col != W_WIDTH(wp))
5835 screen_char(LineOffset[screen_row - 1]
5836 + (unsigned)Columns - 1,
5837 screen_row - 1, (int)(Columns - 1));
5838
5839#ifdef FEAT_MBYTE
5840 /* When there is a multi-byte character, just output a
5841 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005842 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5843 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 out_char(' ');
5845 else
5846#endif
5847 out_char(ScreenLines[LineOffset[screen_row - 1]
5848 + (Columns - 1)]);
5849 /* force a redraw of the first char on the next line */
5850 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5851 screen_start(); /* don't know where cursor is now */
5852 }
5853 }
5854
5855 col = 0;
5856 off = (unsigned)(current_ScreenLine - ScreenLines);
5857#ifdef FEAT_RIGHTLEFT
5858 if (wp->w_p_rl)
5859 {
5860 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5861 off += col;
5862 }
5863#endif
5864
5865 /* reset the drawing state for the start of a wrapped line */
5866 draw_state = WL_START;
5867 saved_n_extra = n_extra;
5868 saved_p_extra = p_extra;
5869 saved_c_extra = c_extra;
5870 saved_char_attr = char_attr;
5871 n_extra = 0;
5872 lcs_prec_todo = lcs_prec;
5873#ifdef FEAT_LINEBREAK
5874# ifdef FEAT_DIFF
5875 if (filler_todo <= 0)
5876# endif
5877 need_showbreak = TRUE;
5878#endif
5879#ifdef FEAT_DIFF
5880 --filler_todo;
5881 /* When the filler lines are actually below the last line of the
5882 * file, don't draw the line itself, break here. */
5883 if (filler_todo == 0 && wp->w_botfill)
5884 break;
5885#endif
5886 }
5887
5888 } /* for every character in the line */
5889
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005890#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005891 /* After an empty line check first word for capital. */
5892 if (*skipwhite(line) == NUL)
5893 {
5894 capcol_lnum = lnum + 1;
5895 cap_col = 0;
5896 }
5897#endif
5898
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005899 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 return row;
5901}
5902
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005903#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005904static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005905
5906/*
5907 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005908 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005909 */
5910 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005911comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005912{
5913 int i;
5914
5915 for (i = 0; i < Screen_mco; ++i)
5916 {
5917 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5918 return TRUE;
5919 if (ScreenLinesC[i][off_from] == 0)
5920 break;
5921 }
5922 return FALSE;
5923}
5924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926/*
5927 * Check whether the given character needs redrawing:
5928 * - the (first byte of the) character is different
5929 * - the attributes are different
5930 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005931 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 */
5933 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005934char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 if (cols > 0
5937 && ((ScreenLines[off_from] != ScreenLines[off_to]
5938 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5939
5940#ifdef FEAT_MBYTE
5941 || (enc_dbcs != 0
5942 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5943 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5944 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5945 : (cols > 1 && ScreenLines[off_from + 1]
5946 != ScreenLines[off_to + 1])))
5947 || (enc_utf8
5948 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5949 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005950 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005951 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5952 && ScreenLines[off_from + 1]
5953 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954#endif
5955 ))
5956 return TRUE;
5957 return FALSE;
5958}
5959
5960/*
5961 * Move one "cooked" screen line to the screen, but only the characters that
5962 * have actually changed. Handle insert/delete character.
5963 * "coloff" gives the first column on the screen for this line.
5964 * "endcol" gives the columns where valid characters are.
5965 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5966 * needs to be cleared, negative otherwise.
5967 * "rlflag" is TRUE in a rightleft window:
5968 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5969 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5970 */
5971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005972screen_line(
5973 int row,
5974 int coloff,
5975 int endcol,
5976 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005978 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005980 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 unsigned off_from;
5983 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005984#ifdef FEAT_MBYTE
5985 unsigned max_off_from;
5986 unsigned max_off_to;
5987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005989#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 int hl;
5991#endif
5992 int force = FALSE; /* force update rest of the line */
5993 int redraw_this /* bool: does character need redraw? */
5994#ifdef FEAT_GUI
5995 = TRUE /* For GUI when while-loop empty */
5996#endif
5997 ;
5998 int redraw_next; /* redraw_this for next character */
5999#ifdef FEAT_MBYTE
6000 int clear_next = FALSE;
6001 int char_cells; /* 1: normal char */
6002 /* 2: occupies two display cells */
6003# define CHAR_CELLS char_cells
6004#else
6005# define CHAR_CELLS 1
6006#endif
6007
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006008 /* Check for illegal row and col, just in case. */
6009 if (row >= Rows)
6010 row = Rows - 1;
6011 if (endcol > Columns)
6012 endcol = Columns;
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014# ifdef FEAT_CLIPBOARD
6015 clip_may_clear_selection(row, row);
6016# endif
6017
6018 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6019 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006020#ifdef FEAT_MBYTE
6021 max_off_from = off_from + screen_Columns;
6022 max_off_to = LineOffset[row] + screen_Columns;
6023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025#ifdef FEAT_RIGHTLEFT
6026 if (rlflag)
6027 {
6028 /* Clear rest first, because it's left of the text. */
6029 if (clear_width > 0)
6030 {
6031 while (col <= endcol && ScreenLines[off_to] == ' '
6032 && ScreenAttrs[off_to] == 0
6033# ifdef FEAT_MBYTE
6034 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6035# endif
6036 )
6037 {
6038 ++off_to;
6039 ++col;
6040 }
6041 if (col <= endcol)
6042 screen_fill(row, row + 1, col + coloff,
6043 endcol + coloff + 1, ' ', ' ', 0);
6044 }
6045 col = endcol + 1;
6046 off_to = LineOffset[row] + col + coloff;
6047 off_from += col;
6048 endcol = (clear_width > 0 ? clear_width : -clear_width);
6049 }
6050#endif /* FEAT_RIGHTLEFT */
6051
6052 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6053
6054 while (col < endcol)
6055 {
6056#ifdef FEAT_MBYTE
6057 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006058 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 else
6060 char_cells = 1;
6061#endif
6062
6063 redraw_this = redraw_next;
6064 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6065 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6066
6067#ifdef FEAT_GUI
6068 /* If the next character was bold, then redraw the current character to
6069 * remove any pixels that might have spilt over into us. This only
6070 * happens in the GUI.
6071 */
6072 if (redraw_next && gui.in_use)
6073 {
6074 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006075 if (hl > HL_ALL)
6076 hl = syn_attr2attr(hl);
6077 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 redraw_this = TRUE;
6079 }
6080#endif
6081
6082 if (redraw_this)
6083 {
6084 /*
6085 * Special handling when 'xs' termcap flag set (hpterm):
6086 * Attributes for characters are stored at the position where the
6087 * cursor is when writing the highlighting code. The
6088 * start-highlighting code must be written with the cursor on the
6089 * first highlighted character. The stop-highlighting code must
6090 * be written with the cursor just after the last highlighted
6091 * character.
6092 * Overwriting a character doesn't remove it's highlighting. Need
6093 * to clear the rest of the line, and force redrawing it
6094 * completely.
6095 */
6096 if ( p_wiv
6097 && !force
6098#ifdef FEAT_GUI
6099 && !gui.in_use
6100#endif
6101 && ScreenAttrs[off_to] != 0
6102 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6103 {
6104 /*
6105 * Need to remove highlighting attributes here.
6106 */
6107 windgoto(row, col + coloff);
6108 out_str(T_CE); /* clear rest of this screen line */
6109 screen_start(); /* don't know where cursor is now */
6110 force = TRUE; /* force redraw of rest of the line */
6111 redraw_next = TRUE; /* or else next char would miss out */
6112
6113 /*
6114 * If the previous character was highlighted, need to stop
6115 * highlighting at this character.
6116 */
6117 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6118 {
6119 screen_attr = ScreenAttrs[off_to - 1];
6120 term_windgoto(row, col + coloff);
6121 screen_stop_highlight();
6122 }
6123 else
6124 screen_attr = 0; /* highlighting has stopped */
6125 }
6126#ifdef FEAT_MBYTE
6127 if (enc_dbcs != 0)
6128 {
6129 /* Check if overwriting a double-byte with a single-byte or
6130 * the other way around requires another character to be
6131 * redrawn. For UTF-8 this isn't needed, because comparing
6132 * ScreenLinesUC[] is sufficient. */
6133 if (char_cells == 1
6134 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006135 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 /* Writing a single-cell character over a double-cell
6138 * character: need to redraw the next cell. */
6139 ScreenLines[off_to + 1] = 0;
6140 redraw_next = TRUE;
6141 }
6142 else if (char_cells == 2
6143 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006144 && (*mb_off2cells)(off_to, max_off_to) == 1
6145 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 /* Writing the second half of a double-cell character over
6148 * a double-cell character: need to redraw the second
6149 * cell. */
6150 ScreenLines[off_to + 2] = 0;
6151 redraw_next = TRUE;
6152 }
6153
6154 if (enc_dbcs == DBCS_JPNU)
6155 ScreenLines2[off_to] = ScreenLines2[off_from];
6156 }
6157 /* When writing a single-width character over a double-width
6158 * character and at the end of the redrawn text, need to clear out
6159 * the right halve of the old character.
6160 * Also required when writing the right halve of a double-width
6161 * char over the left halve of an existing one. */
6162 if (has_mbyte && col + char_cells == endcol
6163 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006164 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006166 && (*mb_off2cells)(off_to, max_off_to) == 1
6167 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 clear_next = TRUE;
6169#endif
6170
6171 ScreenLines[off_to] = ScreenLines[off_from];
6172#ifdef FEAT_MBYTE
6173 if (enc_utf8)
6174 {
6175 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6176 if (ScreenLinesUC[off_from] != 0)
6177 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006178 int i;
6179
6180 for (i = 0; i < Screen_mco; ++i)
6181 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
6183 }
6184 if (char_cells == 2)
6185 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6186#endif
6187
6188#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006189 /* The bold trick makes a single column of pixels appear in the
6190 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 * character should be redrawn too. This happens for our own GUI
6192 * and for some xterms. */
6193 if (
6194# ifdef FEAT_GUI
6195 gui.in_use
6196# endif
6197# if defined(FEAT_GUI) && defined(UNIX)
6198 ||
6199# endif
6200# ifdef UNIX
6201 term_is_xterm
6202# endif
6203 )
6204 {
6205 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006206 if (hl > HL_ALL)
6207 hl = syn_attr2attr(hl);
6208 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 redraw_next = TRUE;
6210 }
6211#endif
6212 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6213#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006214 /* For simplicity set the attributes of second half of a
6215 * double-wide character equal to the first half. */
6216 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006218
6219 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 else
6222#endif
6223 screen_char(off_to, row, col + coloff);
6224 }
6225 else if ( p_wiv
6226#ifdef FEAT_GUI
6227 && !gui.in_use
6228#endif
6229 && col + coloff > 0)
6230 {
6231 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6232 {
6233 /*
6234 * Don't output stop-highlight when moving the cursor, it will
6235 * stop the highlighting when it should continue.
6236 */
6237 screen_attr = 0;
6238 }
6239 else if (screen_attr != 0)
6240 screen_stop_highlight();
6241 }
6242
6243 off_to += CHAR_CELLS;
6244 off_from += CHAR_CELLS;
6245 col += CHAR_CELLS;
6246 }
6247
6248#ifdef FEAT_MBYTE
6249 if (clear_next)
6250 {
6251 /* Clear the second half of a double-wide character of which the left
6252 * half was overwritten with a single-wide character. */
6253 ScreenLines[off_to] = ' ';
6254 if (enc_utf8)
6255 ScreenLinesUC[off_to] = 0;
6256 screen_char(off_to, row, col + coloff);
6257 }
6258#endif
6259
6260 if (clear_width > 0
6261#ifdef FEAT_RIGHTLEFT
6262 && !rlflag
6263#endif
6264 )
6265 {
6266#ifdef FEAT_GUI
6267 int startCol = col;
6268#endif
6269
6270 /* blank out the rest of the line */
6271 while (col < clear_width && ScreenLines[off_to] == ' '
6272 && ScreenAttrs[off_to] == 0
6273#ifdef FEAT_MBYTE
6274 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6275#endif
6276 )
6277 {
6278 ++off_to;
6279 ++col;
6280 }
6281 if (col < clear_width)
6282 {
6283#ifdef FEAT_GUI
6284 /*
6285 * In the GUI, clearing the rest of the line may leave pixels
6286 * behind if the first character cleared was bold. Some bold
6287 * fonts spill over the left. In this case we redraw the previous
6288 * character too. If we didn't skip any blanks above, then we
6289 * only redraw if the character wasn't already redrawn anyway.
6290 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006291 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293 hl = ScreenAttrs[off_to];
6294 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006295 {
6296 int prev_cells = 1;
6297# ifdef FEAT_MBYTE
6298 if (enc_utf8)
6299 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6300 * that its width is 2. */
6301 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6302 else if (enc_dbcs != 0)
6303 {
6304 /* find previous character by counting from first
6305 * column and get its width. */
6306 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006307 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006308
6309 while (off < off_to)
6310 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006311 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006312 off += prev_cells;
6313 }
6314 }
6315
6316 if (enc_dbcs != 0 && prev_cells > 1)
6317 screen_char_2(off_to - prev_cells, row,
6318 col + coloff - prev_cells);
6319 else
6320# endif
6321 screen_char(off_to - prev_cells, row,
6322 col + coloff - prev_cells);
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325#endif
6326 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6327 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006328#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 off_to += clear_width - col;
6330 col = clear_width;
6331#endif
6332 }
6333 }
6334
6335 if (clear_width > 0)
6336 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006337#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 /* For a window that's left of another, draw the separator char. */
6339 if (col + coloff < Columns)
6340 {
6341 int c;
6342
6343 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006344 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006346 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6347 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348# endif
6349 || ScreenAttrs[off_to] != hl)
6350 {
6351 ScreenLines[off_to] = c;
6352 ScreenAttrs[off_to] = hl;
6353# ifdef FEAT_MBYTE
6354 if (enc_utf8)
6355 {
6356 if (c >= 0x80)
6357 {
6358 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006359 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361 else
6362 ScreenLinesUC[off_to] = 0;
6363 }
6364# endif
6365 screen_char(off_to, row, col + coloff);
6366 }
6367 }
6368 else
6369#endif
6370 LineWraps[row] = FALSE;
6371 }
6372}
6373
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006374#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006376 * Mirror text "str" for right-left displaying.
6377 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006380rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 char_u *p1, *p2;
6383 int t;
6384
6385 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6386 {
6387 t = *p1;
6388 *p1 = *p2;
6389 *p2 = t;
6390 }
6391}
6392#endif
6393
6394#if defined(FEAT_WINDOWS) || defined(PROTO)
6395/*
6396 * mark all status lines for redraw; used after first :cd
6397 */
6398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006399status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400{
6401 win_T *wp;
6402
Bram Moolenaar29323592016-07-24 22:04:11 +02006403 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (wp->w_status_height)
6405 {
6406 wp->w_redr_status = TRUE;
6407 redraw_later(VALID);
6408 }
6409}
6410
6411/*
6412 * mark all status lines of the current buffer for redraw
6413 */
6414 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006415status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 win_T *wp;
6418
Bram Moolenaar29323592016-07-24 22:04:11 +02006419 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6421 {
6422 wp->w_redr_status = TRUE;
6423 redraw_later(VALID);
6424 }
6425}
6426
6427/*
6428 * Redraw all status lines that need to be redrawn.
6429 */
6430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006431redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 win_T *wp;
6434
Bram Moolenaar29323592016-07-24 22:04:11 +02006435 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (wp->w_redr_status)
6437 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006438 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006439 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441#endif
6442
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006443#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444/*
6445 * Redraw all status lines at the bottom of frame "frp".
6446 */
6447 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006448win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 if (frp->fr_layout == FR_LEAF)
6451 frp->fr_win->w_redr_status = TRUE;
6452 else if (frp->fr_layout == FR_ROW)
6453 {
6454 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6455 win_redraw_last_status(frp);
6456 }
6457 else /* frp->fr_layout == FR_COL */
6458 {
6459 frp = frp->fr_child;
6460 while (frp->fr_next != NULL)
6461 frp = frp->fr_next;
6462 win_redraw_last_status(frp);
6463 }
6464}
6465#endif
6466
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006467#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468/*
6469 * Draw the verticap separator right of window "wp" starting with line "row".
6470 */
6471 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006472draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 int hl;
6475 int c;
6476
6477 if (wp->w_vsep_width)
6478 {
6479 /* draw the vertical separator right of this window */
6480 c = fillchar_vsep(&hl);
6481 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6482 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6483 c, ' ', hl);
6484 }
6485}
6486#endif
6487
6488#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006489static int status_match_len(expand_T *xp, char_u *s);
6490static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006493 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 */
6495 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006496status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
6498 int len = 0;
6499
6500#ifdef FEAT_MENU
6501 int emenu = (xp->xp_context == EXPAND_MENUS
6502 || xp->xp_context == EXPAND_MENUNAMES);
6503
6504 /* Check for menu separators - replace with '|'. */
6505 if (emenu && menu_is_separator(s))
6506 return 1;
6507#endif
6508
6509 while (*s != NUL)
6510 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006511 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006512 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006513 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515
6516 return len;
6517}
6518
6519/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006520 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006521 * These are backslashes used for escaping. Do show backslashes in help tags.
6522 */
6523 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006524skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006525{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006526 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006527#ifdef FEAT_MENU
6528 || ((xp->xp_context == EXPAND_MENUS
6529 || xp->xp_context == EXPAND_MENUNAMES)
6530 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6531#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006532 )
6533 {
6534#ifndef BACKSLASH_IN_FILENAME
6535 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6536 return 2;
6537#endif
6538 return 1;
6539 }
6540 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006541}
6542
6543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 * Show wildchar matches in the status line.
6545 * Show at least the "match" item.
6546 * We start at item 'first_match' in the list and show all matches that fit.
6547 *
6548 * If inversion is possible we use it. Else '=' characters are used.
6549 */
6550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006551win_redr_status_matches(
6552 expand_T *xp,
6553 int num_matches,
6554 char_u **matches, /* list of matches */
6555 int match,
6556 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6559 int row;
6560 char_u *buf;
6561 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006562 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 int fillchar;
6564 int attr;
6565 int i;
6566 int highlight = TRUE;
6567 char_u *selstart = NULL;
6568 int selstart_col = 0;
6569 char_u *selend = NULL;
6570 static int first_match = 0;
6571 int add_left = FALSE;
6572 char_u *s;
6573#ifdef FEAT_MENU
6574 int emenu;
6575#endif
6576#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6577 int l;
6578#endif
6579
6580 if (matches == NULL) /* interrupted completion? */
6581 return;
6582
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006583#ifdef FEAT_MBYTE
6584 if (has_mbyte)
6585 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6586 else
6587#endif
6588 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (buf == NULL)
6590 return;
6591
6592 if (match == -1) /* don't show match but original text */
6593 {
6594 match = 0;
6595 highlight = FALSE;
6596 }
6597 /* count 1 for the ending ">" */
6598 clen = status_match_len(xp, L_MATCH(match)) + 3;
6599 if (match == 0)
6600 first_match = 0;
6601 else if (match < first_match)
6602 {
6603 /* jumping left, as far as we can go */
6604 first_match = match;
6605 add_left = TRUE;
6606 }
6607 else
6608 {
6609 /* check if match fits on the screen */
6610 for (i = first_match; i < match; ++i)
6611 clen += status_match_len(xp, L_MATCH(i)) + 2;
6612 if (first_match > 0)
6613 clen += 2;
6614 /* jumping right, put match at the left */
6615 if ((long)clen > Columns)
6616 {
6617 first_match = match;
6618 /* if showing the last match, we can add some on the left */
6619 clen = 2;
6620 for (i = match; i < num_matches; ++i)
6621 {
6622 clen += status_match_len(xp, L_MATCH(i)) + 2;
6623 if ((long)clen >= Columns)
6624 break;
6625 }
6626 if (i == num_matches)
6627 add_left = TRUE;
6628 }
6629 }
6630 if (add_left)
6631 while (first_match > 0)
6632 {
6633 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6634 if ((long)clen >= Columns)
6635 break;
6636 --first_match;
6637 }
6638
6639 fillchar = fillchar_status(&attr, TRUE);
6640
6641 if (first_match == 0)
6642 {
6643 *buf = NUL;
6644 len = 0;
6645 }
6646 else
6647 {
6648 STRCPY(buf, "< ");
6649 len = 2;
6650 }
6651 clen = len;
6652
6653 i = first_match;
6654 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6655 {
6656 if (i == match)
6657 {
6658 selstart = buf + len;
6659 selstart_col = clen;
6660 }
6661
6662 s = L_MATCH(i);
6663 /* Check for menu separators - replace with '|' */
6664#ifdef FEAT_MENU
6665 emenu = (xp->xp_context == EXPAND_MENUS
6666 || xp->xp_context == EXPAND_MENUNAMES);
6667 if (emenu && menu_is_separator(s))
6668 {
6669 STRCPY(buf + len, transchar('|'));
6670 l = (int)STRLEN(buf + len);
6671 len += l;
6672 clen += l;
6673 }
6674 else
6675#endif
6676 for ( ; *s != NUL; ++s)
6677 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006678 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 clen += ptr2cells(s);
6680#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006681 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
6683 STRNCPY(buf + len, s, l);
6684 s += l - 1;
6685 len += l;
6686 }
6687 else
6688#endif
6689 {
6690 STRCPY(buf + len, transchar_byte(*s));
6691 len += (int)STRLEN(buf + len);
6692 }
6693 }
6694 if (i == match)
6695 selend = buf + len;
6696
6697 *(buf + len++) = ' ';
6698 *(buf + len++) = ' ';
6699 clen += 2;
6700 if (++i == num_matches)
6701 break;
6702 }
6703
6704 if (i != num_matches)
6705 {
6706 *(buf + len++) = '>';
6707 ++clen;
6708 }
6709
6710 buf[len] = NUL;
6711
6712 row = cmdline_row - 1;
6713 if (row >= 0)
6714 {
6715 if (wild_menu_showing == 0)
6716 {
6717 if (msg_scrolled > 0)
6718 {
6719 /* Put the wildmenu just above the command line. If there is
6720 * no room, scroll the screen one line up. */
6721 if (cmdline_row == Rows - 1)
6722 {
6723 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6724 ++msg_scrolled;
6725 }
6726 else
6727 {
6728 ++cmdline_row;
6729 ++row;
6730 }
6731 wild_menu_showing = WM_SCROLLED;
6732 }
6733 else
6734 {
6735 /* Create status line if needed by setting 'laststatus' to 2.
6736 * Set 'winminheight' to zero to avoid that the window is
6737 * resized. */
6738 if (lastwin->w_status_height == 0)
6739 {
6740 save_p_ls = p_ls;
6741 save_p_wmh = p_wmh;
6742 p_ls = 2;
6743 p_wmh = 0;
6744 last_status(FALSE);
6745 }
6746 wild_menu_showing = WM_SHOWN;
6747 }
6748 }
6749
6750 screen_puts(buf, row, 0, attr);
6751 if (selstart != NULL && highlight)
6752 {
6753 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006754 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756
6757 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6758 }
6759
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006760#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 win_redraw_last_status(topframe);
6762#else
6763 lastwin->w_redr_status = TRUE;
6764#endif
6765 vim_free(buf);
6766}
6767#endif
6768
6769#if defined(FEAT_WINDOWS) || defined(PROTO)
6770/*
6771 * Redraw the status line of window wp.
6772 *
6773 * If inversion is possible we use it. Else '=' characters are used.
6774 */
6775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006776win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 int row;
6779 char_u *p;
6780 int len;
6781 int fillchar;
6782 int attr;
6783 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006784 static int busy = FALSE;
6785
6786 /* It's possible to get here recursively when 'statusline' (indirectly)
6787 * invokes ":redrawstatus". Simply ignore the call then. */
6788 if (busy)
6789 return;
6790 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792 wp->w_redr_status = FALSE;
6793 if (wp->w_status_height == 0)
6794 {
6795 /* no status line, can only be last window */
6796 redraw_cmdline = TRUE;
6797 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006798 else if (!redrawing()
6799#ifdef FEAT_INS_EXPAND
6800 /* don't update status line when popup menu is visible and may be
6801 * drawn over it */
6802 || pum_visible()
6803#endif
6804 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
6806 /* Don't redraw right now, do it later. */
6807 wp->w_redr_status = TRUE;
6808 }
6809#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006810 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006813 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 }
6815#endif
6816 else
6817 {
6818 fillchar = fillchar_status(&attr, wp == curwin);
6819
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006820 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 p = NameBuff;
6822 len = (int)STRLEN(p);
6823
6824 if (wp->w_buffer->b_help
6825#ifdef FEAT_QUICKFIX
6826 || wp->w_p_pvw
6827#endif
6828 || bufIsChanged(wp->w_buffer)
6829 || wp->w_buffer->b_p_ro)
6830 *(p + len++) = ' ';
6831 if (wp->w_buffer->b_help)
6832 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006833 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 len += (int)STRLEN(p + len);
6835 }
6836#ifdef FEAT_QUICKFIX
6837 if (wp->w_p_pvw)
6838 {
6839 STRCPY(p + len, _("[Preview]"));
6840 len += (int)STRLEN(p + len);
6841 }
6842#endif
6843 if (bufIsChanged(wp->w_buffer))
6844 {
6845 STRCPY(p + len, "[+]");
6846 len += 3;
6847 }
6848 if (wp->w_buffer->b_p_ro)
6849 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006850 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006851 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
6853
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6855 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6856 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6857 if (this_ru_col <= 1)
6858 {
6859 p = (char_u *)"<"; /* No room for file name! */
6860 len = 1;
6861 }
6862 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863#ifdef FEAT_MBYTE
6864 if (has_mbyte)
6865 {
6866 int clen = 0, i;
6867
6868 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006869 clen = mb_string2cells(p, -1);
6870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 /* Find first character that will fit.
6872 * Going from start to end is much faster for DBCS. */
6873 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006874 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 clen -= (*mb_ptr2cells)(p + i);
6876 len = clen;
6877 if (i > 0)
6878 {
6879 p = p + i - 1;
6880 *p = '<';
6881 ++len;
6882 }
6883
6884 }
6885 else
6886#endif
6887 if (len > this_ru_col - 1)
6888 {
6889 p += len - (this_ru_col - 1);
6890 *p = '<';
6891 len = this_ru_col - 1;
6892 }
6893
6894 row = W_WINROW(wp) + wp->w_height;
6895 screen_puts(p, row, W_WINCOL(wp), attr);
6896 screen_fill(row, row + 1, len + W_WINCOL(wp),
6897 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6898
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006899 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6901 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6902 - 1 + W_WINCOL(wp)), attr);
6903
6904#ifdef FEAT_CMDL_INFO
6905 win_redr_ruler(wp, TRUE);
6906#endif
6907 }
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 /*
6910 * May need to draw the character below the vertical separator.
6911 */
6912 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6913 {
6914 if (stl_connected(wp))
6915 fillchar = fillchar_status(&attr, wp == curwin);
6916 else
6917 fillchar = fillchar_vsep(&attr);
6918 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6919 attr);
6920 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006921 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
6923
Bram Moolenaar238a5642006-02-21 22:12:05 +00006924#ifdef FEAT_STL_OPT
6925/*
6926 * Redraw the status line according to 'statusline' and take care of any
6927 * errors encountered.
6928 */
6929 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006930redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006931{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006932 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006933 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006934
6935 /* When called recursively return. This can happen when the statusline
6936 * contains an expression that triggers a redraw. */
6937 if (entered)
6938 return;
6939 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006940
Bram Moolenaara742e082016-04-05 21:10:38 +02006941 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006942 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006943 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006944 {
6945 /* When there is an error disable the statusline, otherwise the
6946 * display is messed up with errors and a redraw triggers the problem
6947 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006948 set_string_option_direct((char_u *)"statusline", -1,
6949 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006950 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006951 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006952 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006953 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006954}
6955#endif
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957/*
6958 * Return TRUE if the status line of window "wp" is connected to the status
6959 * line of the window right of it. If not, then it's a vertical separator.
6960 * Only call if (wp->w_vsep_width != 0).
6961 */
6962 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006963stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 frame_T *fr;
6966
6967 fr = wp->w_frame;
6968 while (fr->fr_parent != NULL)
6969 {
6970 if (fr->fr_parent->fr_layout == FR_COL)
6971 {
6972 if (fr->fr_next != NULL)
6973 break;
6974 }
6975 else
6976 {
6977 if (fr->fr_next != NULL)
6978 return TRUE;
6979 }
6980 fr = fr->fr_parent;
6981 }
6982 return FALSE;
6983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985#endif /* FEAT_WINDOWS */
6986
6987#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6988/*
6989 * Get the value to show for the language mappings, active 'keymap'.
6990 */
6991 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006992get_keymap_str(
6993 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006994 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006995 char_u *buf, /* buffer for the result */
6996 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 char_u *p;
6999
7000 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7001 return FALSE;
7002
7003 {
7004#ifdef FEAT_EVAL
7005 buf_T *old_curbuf = curbuf;
7006 win_T *old_curwin = curwin;
7007 char_u *s;
7008
7009 curbuf = wp->w_buffer;
7010 curwin = wp;
7011 STRCPY(buf, "b:keymap_name"); /* must be writable */
7012 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007013 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 --emsg_skip;
7015 curbuf = old_curbuf;
7016 curwin = old_curwin;
7017 if (p == NULL || *p == NUL)
7018#endif
7019 {
7020#ifdef FEAT_KEYMAP
7021 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7022 p = wp->w_buffer->b_p_keymap;
7023 else
7024#endif
7025 p = (char_u *)"lang";
7026 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007027 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 buf[0] = NUL;
7029#ifdef FEAT_EVAL
7030 vim_free(s);
7031#endif
7032 }
7033 return buf[0] != NUL;
7034}
7035#endif
7036
7037#if defined(FEAT_STL_OPT) || defined(PROTO)
7038/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007039 * Redraw the status line or ruler of window "wp".
7040 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 */
7042 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007043win_redr_custom(
7044 win_T *wp,
7045 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007047 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 int attr;
7049 int curattr;
7050 int row;
7051 int col = 0;
7052 int maxwidth;
7053 int width;
7054 int n;
7055 int len;
7056 int fillchar;
7057 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007058 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007060 struct stl_hlrec hltab[STL_MAX_ITEM];
7061 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007062 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007063 win_T *ewp;
7064 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar1d633412013-12-11 15:52:01 +01007066 /* There is a tiny chance that this gets called recursively: When
7067 * redrawing a status line triggers redrawing the ruler or tabline.
7068 * Avoid trouble by not allowing recursion. */
7069 if (entered)
7070 return;
7071 entered = TRUE;
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007074 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007076 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007077 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007078 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007079 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007080 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007081 maxwidth = Columns;
7082# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007083 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007086 else
7087 {
7088 row = W_WINROW(wp) + wp->w_height;
7089 fillchar = fillchar_status(&attr, wp == curwin);
7090 maxwidth = W_WIDTH(wp);
7091
7092 if (draw_ruler)
7093 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007094 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007095 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007096 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007097 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007098 if (*++stl == '-')
7099 stl++;
7100 if (atoi((char *)stl))
7101 while (VIM_ISDIGIT(*stl))
7102 stl++;
7103 if (*stl++ != '(')
7104 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007105 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007106#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007107 col = ru_col - (Columns - W_WIDTH(wp));
7108 if (col < (W_WIDTH(wp) + 1) / 2)
7109 col = (W_WIDTH(wp) + 1) / 2;
7110#else
7111 col = ru_col;
7112 if (col > (Columns + 1) / 2)
7113 col = (Columns + 1) / 2;
7114#endif
7115 maxwidth = W_WIDTH(wp) - col;
7116#ifdef FEAT_WINDOWS
7117 if (!wp->w_status_height)
7118#endif
7119 {
7120 row = Rows - 1;
7121 --maxwidth; /* writing in last column may cause scrolling */
7122 fillchar = ' ';
7123 attr = 0;
7124 }
7125
7126# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007127 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128# endif
7129 }
7130 else
7131 {
7132 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007135 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007137 use_sandbox = was_set_insecurely((char_u *)"statusline",
7138 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139# endif
7140 }
7141
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007142#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143 col += W_WINCOL(wp);
7144#endif
7145 }
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007148 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar61452852011-02-01 18:01:11 +01007150 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7151 * the cursor away and back. */
7152 ewp = wp == NULL ? curwin : wp;
7153 p_crb_save = ewp->w_p_crb;
7154 ewp->w_p_crb = FALSE;
7155
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 /* Make a copy, because the statusline may include a function call that
7157 * might change the option value and free the memory. */
7158 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007159 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007161 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007163 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007165 /* Make all characters printable. */
7166 p = transstr(buf);
7167 if (p != NULL)
7168 {
7169 vim_strncpy(buf, p, sizeof(buf) - 1);
7170 vim_free(p);
7171 }
7172
7173 /* fill up with "fillchar" */
7174 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007175 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177#ifdef FEAT_MBYTE
7178 len += (*mb_char2bytes)(fillchar, buf + len);
7179#else
7180 buf[len++] = fillchar;
7181#endif
7182 ++width;
7183 }
7184 buf[len] = NUL;
7185
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007186 /*
7187 * Draw each snippet with the specified highlighting.
7188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 curattr = attr;
7190 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007191 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007193 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 screen_puts_len(p, len, row, col, curattr);
7195 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 else if (hltab[n].userhl < 0)
7201 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007203 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007204 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205#endif
7206 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007207 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
7209 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007210
7211 if (wp == NULL)
7212 {
7213 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7214 col = 0;
7215 len = 0;
7216 p = buf;
7217 fillchar = 0;
7218 for (n = 0; tabtab[n].start != NULL; n++)
7219 {
7220 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7221 while (col < len)
7222 TabPageIdxs[col++] = fillchar;
7223 p = tabtab[n].start;
7224 fillchar = tabtab[n].userhl;
7225 }
7226 while (col < Columns)
7227 TabPageIdxs[col++] = fillchar;
7228 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007229
7230theend:
7231 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232}
7233
7234#endif /* FEAT_STL_OPT */
7235
7236/*
7237 * Output a single character directly to the screen and update ScreenLines.
7238 */
7239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007240screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 char_u buf[MB_MAXBYTES + 1];
7243
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007244#ifdef FEAT_MBYTE
7245 if (has_mbyte)
7246 buf[(*mb_char2bytes)(c, buf)] = NUL;
7247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007249 {
7250 buf[0] = c;
7251 buf[1] = NUL;
7252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 screen_puts(buf, row, col, attr);
7254}
7255
7256/*
7257 * Get a single character directly from ScreenLines into "bytes[]".
7258 * Also return its attribute in *attrp;
7259 */
7260 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007261screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 unsigned off;
7264
7265 /* safety check */
7266 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7267 {
7268 off = LineOffset[row] + col;
7269 *attrp = ScreenAttrs[off];
7270 bytes[0] = ScreenLines[off];
7271 bytes[1] = NUL;
7272
7273#ifdef FEAT_MBYTE
7274 if (enc_utf8 && ScreenLinesUC[off] != 0)
7275 bytes[utfc_char2bytes(off, bytes)] = NUL;
7276 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7277 {
7278 bytes[0] = ScreenLines[off];
7279 bytes[1] = ScreenLines2[off];
7280 bytes[2] = NUL;
7281 }
7282 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7283 {
7284 bytes[1] = ScreenLines[off + 1];
7285 bytes[2] = NUL;
7286 }
7287#endif
7288 }
7289}
7290
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007291#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007292static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007293
7294/*
7295 * Return TRUE if composing characters for screen posn "off" differs from
7296 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007297 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007298 */
7299 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007300screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007301{
7302 int i;
7303
7304 for (i = 0; i < Screen_mco; ++i)
7305 {
7306 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7307 return TRUE;
7308 if (u8cc[i] == 0)
7309 break;
7310 }
7311 return FALSE;
7312}
7313#endif
7314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315/*
7316 * Put string '*text' on the screen at position 'row' and 'col', with
7317 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7318 * Note: only outputs within one row, message is truncated at screen boundary!
7319 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7320 */
7321 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007322screen_puts(
7323 char_u *text,
7324 int row,
7325 int col,
7326 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328 screen_puts_len(text, -1, row, col, attr);
7329}
7330
7331/*
7332 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7333 * a NUL.
7334 */
7335 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007336screen_puts_len(
7337 char_u *text,
7338 int textlen,
7339 int row,
7340 int col,
7341 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343 unsigned off;
7344 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007345 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 int c;
7347#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007348 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int mbyte_blen = 1;
7350 int mbyte_cells = 1;
7351 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int clear_next_cell = FALSE;
7354# ifdef FEAT_ARABIC
7355 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 int pc, nc, nc1;
7357 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358# endif
7359#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007360#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7361 int force_redraw_this;
7362 int force_redraw_next = FALSE;
7363#endif
7364 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7367 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007368 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
Bram Moolenaarc236c162008-07-13 17:41:49 +00007370#ifdef FEAT_MBYTE
7371 /* When drawing over the right halve of a double-wide char clear out the
7372 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007373 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007374# ifdef FEAT_GUI
7375 && !gui.in_use
7376# endif
7377 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007378 {
7379 ScreenLines[off - 1] = ' ';
7380 ScreenAttrs[off - 1] = 0;
7381 if (enc_utf8)
7382 {
7383 ScreenLinesUC[off - 1] = 0;
7384 ScreenLinesC[0][off - 1] = 0;
7385 }
7386 /* redraw the previous cell, make it empty */
7387 screen_char(off - 1, row, col - 1);
7388 /* force the cell at "col" to be redrawn */
7389 force_redraw_next = TRUE;
7390 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007391#endif
7392
Bram Moolenaar367329b2007-08-30 11:53:22 +00007393#ifdef FEAT_MBYTE
7394 max_off = LineOffset[row] + screen_Columns;
7395#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007396 while (col < screen_Columns
7397 && (len < 0 || (int)(ptr - text) < len)
7398 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
7400 c = *ptr;
7401#ifdef FEAT_MBYTE
7402 /* check if this is the first byte of a multibyte */
7403 if (has_mbyte)
7404 {
7405 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007406 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007408 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7410 mbyte_cells = 1;
7411 else if (enc_dbcs != 0)
7412 mbyte_cells = mbyte_blen;
7413 else /* enc_utf8 */
7414 {
7415 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007416 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 (int)((text + len) - ptr));
7418 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007419 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007421# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 /* Non-BMP character: display as ? or fullwidth ?. */
7423 if (u8c >= 0x10000)
7424 {
7425 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7426 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007427 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430# ifdef FEAT_ARABIC
7431 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7432 {
7433 /* Do Arabic shaping. */
7434 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7435 {
7436 /* Past end of string to be displayed. */
7437 nc = NUL;
7438 nc1 = NUL;
7439 }
7440 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007441 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007442 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7443 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 nc1 = pcc[0];
7445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 pc = prev_c;
7447 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007448 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450 else
7451 prev_c = u8c;
7452# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007453 if (col + mbyte_cells > screen_Columns)
7454 {
7455 /* Only 1 cell left, but character requires 2 cells:
7456 * display a '>' in the last column to avoid wrapping. */
7457 c = '>';
7458 mbyte_cells = 1;
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461 }
7462#endif
7463
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007464#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7465 force_redraw_this = force_redraw_next;
7466 force_redraw_next = FALSE;
7467#endif
7468
7469 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470#ifdef FEAT_MBYTE
7471 || (mbyte_cells == 2
7472 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7473 || (enc_dbcs == DBCS_JPNU
7474 && c == 0x8e
7475 && ScreenLines2[off] != ptr[1])
7476 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007477 && (ScreenLinesUC[off] !=
7478 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7479 || (ScreenLinesUC[off] != 0
7480 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481#endif
7482 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007483 || exmode_active;
7484
7485 if (need_redraw
7486#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7487 || force_redraw_this
7488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 )
7490 {
7491#if defined(FEAT_GUI) || defined(UNIX)
7492 /* The bold trick makes a single row of pixels appear in the next
7493 * character. When a bold character is removed, the next
7494 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007495 * and for some xterms. */
7496 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497# ifdef FEAT_GUI
7498 gui.in_use
7499# endif
7500# if defined(FEAT_GUI) && defined(UNIX)
7501 ||
7502# endif
7503# ifdef UNIX
7504 term_is_xterm
7505# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007506 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007508 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007510 if (n > HL_ALL)
7511 n = syn_attr2attr(n);
7512 if (n & HL_BOLD)
7513 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515#endif
7516#ifdef FEAT_MBYTE
7517 /* When at the end of the text and overwriting a two-cell
7518 * character with a one-cell character, need to clear the next
7519 * cell. Also when overwriting the left halve of a two-cell char
7520 * with the right halve of a two-cell char. Do this only once
7521 * (mb_off2cells() may return 2 on the right halve). */
7522 if (clear_next_cell)
7523 clear_next_cell = FALSE;
7524 else if (has_mbyte
7525 && (len < 0 ? ptr[mbyte_blen] == NUL
7526 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007527 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007529 && (*mb_off2cells)(off, max_off) == 1
7530 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 clear_next_cell = TRUE;
7532
7533 /* Make sure we never leave a second byte of a double-byte behind,
7534 * it confuses mb_off2cells(). */
7535 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007536 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007538 && (*mb_off2cells)(off, max_off) == 1
7539 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 ScreenLines[off + mbyte_blen] = 0;
7541#endif
7542 ScreenLines[off] = c;
7543 ScreenAttrs[off] = attr;
7544#ifdef FEAT_MBYTE
7545 if (enc_utf8)
7546 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 ScreenLinesUC[off] = 0;
7549 else
7550 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007551 int i;
7552
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007554 for (i = 0; i < Screen_mco; ++i)
7555 {
7556 ScreenLinesC[i][off] = u8cc[i];
7557 if (u8cc[i] == 0)
7558 break;
7559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561 if (mbyte_cells == 2)
7562 {
7563 ScreenLines[off + 1] = 0;
7564 ScreenAttrs[off + 1] = attr;
7565 }
7566 screen_char(off, row, col);
7567 }
7568 else if (mbyte_cells == 2)
7569 {
7570 ScreenLines[off + 1] = ptr[1];
7571 ScreenAttrs[off + 1] = attr;
7572 screen_char_2(off, row, col);
7573 }
7574 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7575 {
7576 ScreenLines2[off] = ptr[1];
7577 screen_char(off, row, col);
7578 }
7579 else
7580#endif
7581 screen_char(off, row, col);
7582 }
7583#ifdef FEAT_MBYTE
7584 if (has_mbyte)
7585 {
7586 off += mbyte_cells;
7587 col += mbyte_cells;
7588 ptr += mbyte_blen;
7589 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007590 {
7591 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007593 len = -1;
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else
7597#endif
7598 {
7599 ++off;
7600 ++col;
7601 ++ptr;
7602 }
7603 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007604
7605#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7606 /* If we detected the next character needs to be redrawn, but the text
7607 * doesn't extend up to there, update the character here. */
7608 if (force_redraw_next && col < screen_Columns)
7609 {
7610# ifdef FEAT_MBYTE
7611 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7612 screen_char_2(off, row, col);
7613 else
7614# endif
7615 screen_char(off, row, col);
7616 }
7617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618}
7619
7620#ifdef FEAT_SEARCH_EXTRA
7621/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007622 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
7624 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007625start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
7627 if (p_hls && !no_hlsearch)
7628 {
7629 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007630 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007631# ifdef FEAT_RELTIME
7632 /* Set the time limit to 'redrawtime'. */
7633 profile_setlimit(p_rdt, &search_hl.tm);
7634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636}
7637
7638/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007639 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 */
7641 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007642end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 if (search_hl.rm.regprog != NULL)
7645 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007646 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 search_hl.rm.regprog = NULL;
7648 }
7649}
7650
7651/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007652 * Init for calling prepare_search_hl().
7653 */
7654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007655init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007656{
7657 matchitem_T *cur;
7658
7659 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7660 * match */
7661 cur = wp->w_match_head;
7662 while (cur != NULL)
7663 {
7664 cur->hl.rm = cur->match;
7665 if (cur->hlg_id == 0)
7666 cur->hl.attr = 0;
7667 else
7668 cur->hl.attr = syn_id2attr(cur->hlg_id);
7669 cur->hl.buf = wp->w_buffer;
7670 cur->hl.lnum = 0;
7671 cur->hl.first_lnum = 0;
7672# ifdef FEAT_RELTIME
7673 /* Set the time limit to 'redrawtime'. */
7674 profile_setlimit(p_rdt, &(cur->hl.tm));
7675# endif
7676 cur = cur->next;
7677 }
7678 search_hl.buf = wp->w_buffer;
7679 search_hl.lnum = 0;
7680 search_hl.first_lnum = 0;
7681 /* time limit is set at the toplevel, for all windows */
7682}
7683
7684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 * Advance to the match in window "wp" line "lnum" or past it.
7686 */
7687 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007688prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007690 matchitem_T *cur; /* points to the match list */
7691 match_T *shl; /* points to search_hl or a match */
7692 int shl_flag; /* flag to indicate whether search_hl
7693 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007694 int pos_inprogress; /* marks that position match search is
7695 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 int n;
7697
7698 /*
7699 * When using a multi-line pattern, start searching at the top
7700 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007701 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007703 cur = wp->w_match_head;
7704 shl_flag = FALSE;
7705 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007707 if (shl_flag == FALSE)
7708 {
7709 shl = &search_hl;
7710 shl_flag = TRUE;
7711 }
7712 else
7713 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 if (shl->rm.regprog != NULL
7715 && shl->lnum == 0
7716 && re_multiline(shl->rm.regprog))
7717 {
7718 if (shl->first_lnum == 0)
7719 {
7720# ifdef FEAT_FOLDING
7721 for (shl->first_lnum = lnum;
7722 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7723 if (hasFoldingWin(wp, shl->first_lnum - 1,
7724 NULL, NULL, TRUE, NULL))
7725 break;
7726# else
7727 shl->first_lnum = wp->w_topline;
7728# endif
7729 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007730 if (cur != NULL)
7731 cur->pos.cur = 0;
7732 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007734 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7735 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007737 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7738 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007739 pos_inprogress = cur == NULL || cur->pos.cur == 0
7740 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (shl->lnum != 0)
7742 {
7743 shl->first_lnum = shl->lnum
7744 + shl->rm.endpos[0].lnum
7745 - shl->rm.startpos[0].lnum;
7746 n = shl->rm.endpos[0].col;
7747 }
7748 else
7749 {
7750 ++shl->first_lnum;
7751 n = 0;
7752 }
7753 }
7754 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007755 if (shl != &search_hl && cur != NULL)
7756 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758}
7759
7760/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007761 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 * Uses shl->buf.
7763 * Sets shl->lnum and shl->rm contents.
7764 * Note: Assumes a previous match is always before "lnum", unless
7765 * shl->lnum is zero.
7766 * Careful: Any pointers for buffer lines will become invalid.
7767 */
7768 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007769next_search_hl(
7770 win_T *win,
7771 match_T *shl, /* points to search_hl or a match */
7772 linenr_T lnum,
7773 colnr_T mincol, /* minimal column for a match */
7774 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 linenr_T l;
7777 colnr_T matchcol;
7778 long nmatched;
7779
7780 if (shl->lnum != 0)
7781 {
7782 /* Check for three situations:
7783 * 1. If the "lnum" is below a previous match, start a new search.
7784 * 2. If the previous match includes "mincol", use it.
7785 * 3. Continue after the previous match.
7786 */
7787 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7788 if (lnum > l)
7789 shl->lnum = 0;
7790 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7791 return;
7792 }
7793
7794 /*
7795 * Repeat searching for a match until one is found that includes "mincol"
7796 * or none is found in this line.
7797 */
7798 called_emsg = FALSE;
7799 for (;;)
7800 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007801#ifdef FEAT_RELTIME
7802 /* Stop searching after passing the time limit. */
7803 if (profile_passed_limit(&(shl->tm)))
7804 {
7805 shl->lnum = 0; /* no match found in time */
7806 break;
7807 }
7808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 /* Three situations:
7810 * 1. No useful previous match: search from start of line.
7811 * 2. Not Vi compatible or empty match: continue at next character.
7812 * Break the loop if this is beyond the end of the line.
7813 * 3. Vi compatible searching: continue at end of previous match.
7814 */
7815 if (shl->lnum == 0)
7816 matchcol = 0;
7817 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7818 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007819 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007821 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007822
7823 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007824 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007825 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007827 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 shl->lnum = 0;
7829 break;
7830 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007831#ifdef FEAT_MBYTE
7832 if (has_mbyte)
7833 matchcol += mb_ptr2len(ml);
7834 else
7835#endif
7836 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
7838 else
7839 matchcol = shl->rm.endpos[0].col;
7840
7841 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007842 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007844 /* Remember whether shl->rm is using a copy of the regprog in
7845 * cur->match. */
7846 int regprog_is_copy = (shl != &search_hl && cur != NULL
7847 && shl == &cur->hl
7848 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007849 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007850
Bram Moolenaarb3414592014-06-17 17:48:32 +02007851 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7852 matchcol,
7853#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007854 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007855#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007856 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007857#endif
7858 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007859 /* Copy the regprog, in case it got freed and recompiled. */
7860 if (regprog_is_copy)
7861 cur->match.regprog = cur->hl.rm.regprog;
7862
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007863 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007864 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007865 /* Error while handling regexp: stop using this regexp. */
7866 if (shl == &search_hl)
7867 {
7868 /* don't free regprog in the match list, it's a copy */
7869 vim_regfree(shl->rm.regprog);
7870 SET_NO_HLSEARCH(TRUE);
7871 }
7872 shl->rm.regprog = NULL;
7873 shl->lnum = 0;
7874 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7875 message */
7876 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007877 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007878 }
7879 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007880 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007881 else
7882 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 if (nmatched == 0)
7884 {
7885 shl->lnum = 0; /* no match found */
7886 break;
7887 }
7888 if (shl->rm.startpos[0].lnum > 0
7889 || shl->rm.startpos[0].col >= mincol
7890 || nmatched > 1
7891 || shl->rm.endpos[0].col > mincol)
7892 {
7893 shl->lnum += shl->rm.startpos[0].lnum;
7894 break; /* useful match found */
7895 }
7896 }
7897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898
Bram Moolenaar85077472016-10-16 14:35:48 +02007899/*
7900 * If there is a match fill "shl" and return one.
7901 * Return zero otherwise.
7902 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007903 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007904next_search_hl_pos(
7905 match_T *shl, /* points to a match */
7906 linenr_T lnum,
7907 posmatch_T *posmatch, /* match positions */
7908 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909{
7910 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007911 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007912
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7914 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007915 llpos_T *pos = &posmatch->pos[i];
7916
7917 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007919 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007921 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007923 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007925 /* if this match comes before the one at "found" then swap
7926 * them */
7927 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007929 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930
Bram Moolenaar85077472016-10-16 14:35:48 +02007931 *pos = posmatch->pos[found];
7932 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 }
7934 }
7935 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007936 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937 }
7938 }
7939 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007940 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007942 colnr_T start = posmatch->pos[found].col == 0
7943 ? 0 : posmatch->pos[found].col - 1;
7944 colnr_T end = posmatch->pos[found].col == 0
7945 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946
Bram Moolenaar85077472016-10-16 14:35:48 +02007947 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 shl->rm.startpos[0].lnum = 0;
7949 shl->rm.startpos[0].col = start;
7950 shl->rm.endpos[0].lnum = 0;
7951 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007952 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007953 posmatch->cur = found + 1;
7954 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007956 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007958#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007961screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963 attrentry_T *aep = NULL;
7964
7965 screen_attr = attr;
7966 if (full_screen
7967#ifdef WIN3264
7968 && termcap_active
7969#endif
7970 )
7971 {
7972#ifdef FEAT_GUI
7973 if (gui.in_use)
7974 {
7975 char buf[20];
7976
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007977 /* The GUI handles this internally. */
7978 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 OUT_STR(buf);
7980 }
7981 else
7982#endif
7983 {
7984 if (attr > HL_ALL) /* special HL attr. */
7985 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007986 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 aep = syn_cterm_attr2entry(attr);
7988 else
7989 aep = syn_term_attr2entry(attr);
7990 if (aep == NULL) /* did ":syntax clear" */
7991 attr = 0;
7992 else
7993 attr = aep->ae_attr;
7994 }
7995 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7996 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007997 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007998#ifdef FEAT_TERMGUICOLORS
7999 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008000 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008001#endif
8002 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008003#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008004 )
8005#endif
8006 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008007 /* If the Normal FG color has BOLD attribute and the new HL
8008 * has a FG color defined, clear BOLD. */
8009 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8011 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008012 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8013 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 out_str(T_US);
8015 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8016 out_str(T_CZH);
8017 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8018 out_str(T_MR);
8019
8020 /*
8021 * Output the color or start string after bold etc., in case the
8022 * bold etc. override the color setting.
8023 */
8024 if (aep != NULL)
8025 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008026#ifdef FEAT_TERMGUICOLORS
8027 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008029 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008030 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008031 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
8034 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008037 if (t_colors > 1)
8038 {
8039 if (aep->ae_u.cterm.fg_color)
8040 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8041 if (aep->ae_u.cterm.bg_color)
8042 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8043 }
8044 else
8045 {
8046 if (aep->ae_u.term.start != NULL)
8047 out_str(aep->ae_u.term.start);
8048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050 }
8051 }
8052 }
8053}
8054
8055 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008056screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 int do_ME = FALSE; /* output T_ME code */
8059
8060 if (screen_attr != 0
8061#ifdef WIN3264
8062 && termcap_active
8063#endif
8064 )
8065 {
8066#ifdef FEAT_GUI
8067 if (gui.in_use)
8068 {
8069 char buf[20];
8070
8071 /* use internal GUI code */
8072 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8073 OUT_STR(buf);
8074 }
8075 else
8076#endif
8077 {
8078 if (screen_attr > HL_ALL) /* special HL attr. */
8079 {
8080 attrentry_T *aep;
8081
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008082 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {
8084 /*
8085 * Assume that t_me restores the original colors!
8086 */
8087 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008089#ifdef FEAT_TERMGUICOLORS
8090 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008091 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8092 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008093#endif
8094 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008095#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008096 )
8097#endif
8098 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 do_ME = TRUE;
8100 }
8101 else
8102 {
8103 aep = syn_term_attr2entry(screen_attr);
8104 if (aep != NULL && aep->ae_u.term.stop != NULL)
8105 {
8106 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8107 do_ME = TRUE;
8108 else
8109 out_str(aep->ae_u.term.stop);
8110 }
8111 }
8112 if (aep == NULL) /* did ":syntax clear" */
8113 screen_attr = 0;
8114 else
8115 screen_attr = aep->ae_attr;
8116 }
8117
8118 /*
8119 * Often all ending-codes are equal to T_ME. Avoid outputting the
8120 * same sequence several times.
8121 */
8122 if (screen_attr & HL_STANDOUT)
8123 {
8124 if (STRCMP(T_SE, T_ME) == 0)
8125 do_ME = TRUE;
8126 else
8127 out_str(T_SE);
8128 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008129 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {
8131 if (STRCMP(T_UE, T_ME) == 0)
8132 do_ME = TRUE;
8133 else
8134 out_str(T_UE);
8135 }
8136 if (screen_attr & HL_ITALIC)
8137 {
8138 if (STRCMP(T_CZR, T_ME) == 0)
8139 do_ME = TRUE;
8140 else
8141 out_str(T_CZR);
8142 }
8143 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8144 out_str(T_ME);
8145
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008146#ifdef FEAT_TERMGUICOLORS
8147 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008149 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008150 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008151 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008152 term_bg_rgb_color(cterm_normal_bg_gui_color);
8153 }
8154 else
8155#endif
8156 {
8157 if (t_colors > 1)
8158 {
8159 /* set Normal cterm colors */
8160 if (cterm_normal_fg_color != 0)
8161 term_fg_color(cterm_normal_fg_color - 1);
8162 if (cterm_normal_bg_color != 0)
8163 term_bg_color(cterm_normal_bg_color - 1);
8164 if (cterm_normal_fg_bold)
8165 out_str(T_MD);
8166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 }
8168 }
8169 }
8170 screen_attr = 0;
8171}
8172
8173/*
8174 * Reset the colors for a cterm. Used when leaving Vim.
8175 * The machine specific code may override this again.
8176 */
8177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008178reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008180 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {
8182 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008183#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008184 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8185 || cterm_normal_bg_gui_color != INVALCOLOR)
8186 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008187#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
8191 out_str(T_OP);
8192 screen_attr = -1;
8193 }
8194 if (cterm_normal_fg_bold)
8195 {
8196 out_str(T_ME);
8197 screen_attr = -1;
8198 }
8199 }
8200}
8201
8202/*
8203 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8204 * using the attributes from ScreenAttrs["off"].
8205 */
8206 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008207screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 int attr;
8210
8211 /* Check for illegal values, just in case (could happen just after
8212 * resizing). */
8213 if (row >= screen_Rows || col >= screen_Columns)
8214 return;
8215
Bram Moolenaar494838a2015-02-10 19:20:37 +01008216 /* Outputting a character in the last cell on the screen may scroll the
8217 * screen up. Only do it when the "xn" termcap property is set, otherwise
8218 * mark the character invalid (update it when scrolled up). */
8219 if (*T_XN == NUL
8220 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221#ifdef FEAT_RIGHTLEFT
8222 /* account for first command-line character in rightleft mode */
8223 && !cmdmsg_rl
8224#endif
8225 )
8226 {
8227 ScreenAttrs[off] = (sattr_T)-1;
8228 return;
8229 }
8230
8231 /*
8232 * Stop highlighting first, so it's easier to move the cursor.
8233 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008234#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 if (screen_char_attr != 0)
8236 attr = screen_char_attr;
8237 else
8238#endif
8239 attr = ScreenAttrs[off];
8240 if (screen_attr != attr)
8241 screen_stop_highlight();
8242
8243 windgoto(row, col);
8244
8245 if (screen_attr != attr)
8246 screen_start_highlight(attr);
8247
8248#ifdef FEAT_MBYTE
8249 if (enc_utf8 && ScreenLinesUC[off] != 0)
8250 {
8251 char_u buf[MB_MAXBYTES + 1];
8252
8253 /* Convert UTF-8 character to bytes and write it. */
8254
8255 buf[utfc_char2bytes(off, buf)] = NUL;
8256
8257 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008258 if (utf_ambiguous_width(ScreenLinesUC[off]))
8259 screen_cur_col = 9999;
8260 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 ++screen_cur_col;
8262 }
8263 else
8264#endif
8265 {
8266#ifdef FEAT_MBYTE
8267 out_flush_check();
8268#endif
8269 out_char(ScreenLines[off]);
8270#ifdef FEAT_MBYTE
8271 /* double-byte character in single-width cell */
8272 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8273 out_char(ScreenLines2[off]);
8274#endif
8275 }
8276
8277 screen_cur_col++;
8278}
8279
8280#ifdef FEAT_MBYTE
8281
8282/*
8283 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8284 * on the screen at position 'row' and 'col'.
8285 * The attributes of the first byte is used for all. This is required to
8286 * output the two bytes of a double-byte character with nothing in between.
8287 */
8288 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008289screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 /* Check for illegal values (could be wrong when screen was resized). */
8292 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8293 return;
8294
8295 /* Outputting the last character on the screen may scrollup the screen.
8296 * Don't to it! Mark the character invalid (update it when scrolled up) */
8297 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8298 {
8299 ScreenAttrs[off] = (sattr_T)-1;
8300 return;
8301 }
8302
8303 /* Output the first byte normally (positions the cursor), then write the
8304 * second byte directly. */
8305 screen_char(off, row, col);
8306 out_char(ScreenLines[off + 1]);
8307 ++screen_cur_col;
8308}
8309#endif
8310
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008311#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312/*
8313 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8314 * This uses the contents of ScreenLines[] and doesn't change it.
8315 */
8316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008317screen_draw_rectangle(
8318 int row,
8319 int col,
8320 int height,
8321 int width,
8322 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323{
8324 int r, c;
8325 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008326#ifdef FEAT_MBYTE
8327 int max_off;
8328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008330 /* Can't use ScreenLines unless initialized */
8331 if (ScreenLines == NULL)
8332 return;
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (invert)
8335 screen_char_attr = HL_INVERSE;
8336 for (r = row; r < row + height; ++r)
8337 {
8338 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008339#ifdef FEAT_MBYTE
8340 max_off = off + screen_Columns;
8341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 for (c = col; c < col + width; ++c)
8343 {
8344#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008345 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {
8347 screen_char_2(off + c, r, c);
8348 ++c;
8349 }
8350 else
8351#endif
8352 {
8353 screen_char(off + c, r, c);
8354#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008355 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 ++c;
8357#endif
8358 }
8359 }
8360 }
8361 screen_char_attr = 0;
8362}
8363#endif
8364
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008365#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366/*
8367 * Redraw the characters for a vertically split window.
8368 */
8369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008370redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
8372 int col;
8373 int width;
8374
8375# ifdef FEAT_CLIPBOARD
8376 clip_may_clear_selection(row, end - 1);
8377# endif
8378
8379 if (wp == NULL)
8380 {
8381 col = 0;
8382 width = Columns;
8383 }
8384 else
8385 {
8386 col = wp->w_wincol;
8387 width = wp->w_width;
8388 }
8389 screen_draw_rectangle(row, col, end - row, width, FALSE);
8390}
8391#endif
8392
8393/*
8394 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8395 * with character 'c1' in first column followed by 'c2' in the other columns.
8396 * Use attributes 'attr'.
8397 */
8398 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008399screen_fill(
8400 int start_row,
8401 int end_row,
8402 int start_col,
8403 int end_col,
8404 int c1,
8405 int c2,
8406 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 int row;
8409 int col;
8410 int off;
8411 int end_off;
8412 int did_delete;
8413 int c;
8414 int norm_term;
8415#if defined(FEAT_GUI) || defined(UNIX)
8416 int force_next = FALSE;
8417#endif
8418
8419 if (end_row > screen_Rows) /* safety check */
8420 end_row = screen_Rows;
8421 if (end_col > screen_Columns) /* safety check */
8422 end_col = screen_Columns;
8423 if (ScreenLines == NULL
8424 || start_row >= end_row
8425 || start_col >= end_col) /* nothing to do */
8426 return;
8427
8428 /* it's a "normal" terminal when not in a GUI or cterm */
8429 norm_term = (
8430#ifdef FEAT_GUI
8431 !gui.in_use &&
8432#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008433 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 for (row = start_row; row < end_row; ++row)
8435 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008436#ifdef FEAT_MBYTE
8437 if (has_mbyte
8438# ifdef FEAT_GUI
8439 && !gui.in_use
8440# endif
8441 )
8442 {
8443 /* When drawing over the right halve of a double-wide char clear
8444 * out the left halve. When drawing over the left halve of a
8445 * double wide-char clear out the right halve. Only needed in a
8446 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008447 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008448 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008449 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008450 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008451 }
8452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 /*
8454 * Try to use delete-line termcap code, when no attributes or in a
8455 * "normal" terminal, where a bold/italic space is just a
8456 * space.
8457 */
8458 did_delete = FALSE;
8459 if (c2 == ' '
8460 && end_col == Columns
8461 && can_clear(T_CE)
8462 && (attr == 0
8463 || (norm_term
8464 && attr <= HL_ALL
8465 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8466 {
8467 /*
8468 * check if we really need to clear something
8469 */
8470 col = start_col;
8471 if (c1 != ' ') /* don't clear first char */
8472 ++col;
8473
8474 off = LineOffset[row] + col;
8475 end_off = LineOffset[row] + end_col;
8476
8477 /* skip blanks (used often, keep it fast!) */
8478#ifdef FEAT_MBYTE
8479 if (enc_utf8)
8480 while (off < end_off && ScreenLines[off] == ' '
8481 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8482 ++off;
8483 else
8484#endif
8485 while (off < end_off && ScreenLines[off] == ' '
8486 && ScreenAttrs[off] == 0)
8487 ++off;
8488 if (off < end_off) /* something to be cleared */
8489 {
8490 col = off - LineOffset[row];
8491 screen_stop_highlight();
8492 term_windgoto(row, col);/* clear rest of this screen line */
8493 out_str(T_CE);
8494 screen_start(); /* don't know where cursor is now */
8495 col = end_col - col;
8496 while (col--) /* clear chars in ScreenLines */
8497 {
8498 ScreenLines[off] = ' ';
8499#ifdef FEAT_MBYTE
8500 if (enc_utf8)
8501 ScreenLinesUC[off] = 0;
8502#endif
8503 ScreenAttrs[off] = 0;
8504 ++off;
8505 }
8506 }
8507 did_delete = TRUE; /* the chars are cleared now */
8508 }
8509
8510 off = LineOffset[row] + start_col;
8511 c = c1;
8512 for (col = start_col; col < end_col; ++col)
8513 {
8514 if (ScreenLines[off] != c
8515#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008516 || (enc_utf8 && (int)ScreenLinesUC[off]
8517 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518#endif
8519 || ScreenAttrs[off] != attr
8520#if defined(FEAT_GUI) || defined(UNIX)
8521 || force_next
8522#endif
8523 )
8524 {
8525#if defined(FEAT_GUI) || defined(UNIX)
8526 /* The bold trick may make a single row of pixels appear in
8527 * the next character. When a bold character is removed, the
8528 * next character should be redrawn too. This happens for our
8529 * own GUI and for some xterms. */
8530 if (
8531# ifdef FEAT_GUI
8532 gui.in_use
8533# endif
8534# if defined(FEAT_GUI) && defined(UNIX)
8535 ||
8536# endif
8537# ifdef UNIX
8538 term_is_xterm
8539# endif
8540 )
8541 {
8542 if (ScreenLines[off] != ' '
8543 && (ScreenAttrs[off] > HL_ALL
8544 || ScreenAttrs[off] & HL_BOLD))
8545 force_next = TRUE;
8546 else
8547 force_next = FALSE;
8548 }
8549#endif
8550 ScreenLines[off] = c;
8551#ifdef FEAT_MBYTE
8552 if (enc_utf8)
8553 {
8554 if (c >= 0x80)
8555 {
8556 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008557 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 }
8559 else
8560 ScreenLinesUC[off] = 0;
8561 }
8562#endif
8563 ScreenAttrs[off] = attr;
8564 if (!did_delete || c != ' ')
8565 screen_char(off, row, col);
8566 }
8567 ++off;
8568 if (col == start_col)
8569 {
8570 if (did_delete)
8571 break;
8572 c = c2;
8573 }
8574 }
8575 if (end_col == Columns)
8576 LineWraps[row] = FALSE;
8577 if (row == Rows - 1) /* overwritten the command line */
8578 {
8579 redraw_cmdline = TRUE;
8580 if (c1 == ' ' && c2 == ' ')
8581 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008582 if (start_col == 0)
8583 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585 }
8586}
8587
8588/*
8589 * Check if there should be a delay. Used before clearing or redrawing the
8590 * screen or the command line.
8591 */
8592 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008593check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8596 && !did_wait_return
8597 && emsg_silent == 0)
8598 {
8599 out_flush();
8600 ui_delay(1000L, TRUE);
8601 emsg_on_display = FALSE;
8602 if (check_msg_scroll)
8603 msg_scroll = FALSE;
8604 }
8605}
8606
8607/*
8608 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008609 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 * Returns TRUE if there is a valid screen to write to.
8611 * Returns FALSE when starting up and screen not initialized yet.
8612 */
8613 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008614screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008616 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 return (ScreenLines != NULL);
8618}
8619
8620/*
8621 * Resize the shell to Rows and Columns.
8622 * Allocate ScreenLines[] and associated items.
8623 *
8624 * There may be some time between setting Rows and Columns and (re)allocating
8625 * ScreenLines[]. This happens when starting up and when (manually) changing
8626 * the shell size. Always use screen_Rows and screen_Columns to access items
8627 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8628 * final size of the shell is needed.
8629 */
8630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008631screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 int new_row, old_row;
8634#ifdef FEAT_GUI
8635 int old_Rows;
8636#endif
8637 win_T *wp;
8638 int outofmem = FALSE;
8639 int len;
8640 schar_T *new_ScreenLines;
8641#ifdef FEAT_MBYTE
8642 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008643 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646#endif
8647 sattr_T *new_ScreenAttrs;
8648 unsigned *new_LineOffset;
8649 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008650#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008651 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008652 tabpage_T *tp;
8653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008655 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008656#ifdef FEAT_AUTOCMD
8657 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008659retry:
8660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 /*
8662 * Allocation of the screen buffers is done only when the size changes and
8663 * when Rows and Columns have been set and we have started doing full
8664 * screen stuff.
8665 */
8666 if ((ScreenLines != NULL
8667 && Rows == screen_Rows
8668 && Columns == screen_Columns
8669#ifdef FEAT_MBYTE
8670 && enc_utf8 == (ScreenLinesUC != NULL)
8671 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008672 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673#endif
8674 )
8675 || Rows == 0
8676 || Columns == 0
8677 || (!full_screen && ScreenLines == NULL))
8678 return;
8679
8680 /*
8681 * It's possible that we produce an out-of-memory message below, which
8682 * will cause this function to be called again. To break the loop, just
8683 * return here.
8684 */
8685 if (entered)
8686 return;
8687 entered = TRUE;
8688
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008689 /*
8690 * Note that the window sizes are updated before reallocating the arrays,
8691 * thus we must not redraw here!
8692 */
8693 ++RedrawingDisabled;
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 win_new_shellsize(); /* fit the windows in the new sized shell */
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 comp_col(); /* recompute columns for shown command and ruler */
8698
8699 /*
8700 * We're changing the size of the screen.
8701 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8702 * - Move lines from the old arrays into the new arrays, clear extra
8703 * lines (unless the screen is going to be cleared).
8704 * - Free the old arrays.
8705 *
8706 * If anything fails, make ScreenLines NULL, so we don't do anything!
8707 * Continuing with the old ScreenLines may result in a crash, because the
8708 * size is wrong.
8709 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008710 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008712#ifdef FEAT_AUTOCMD
8713 if (aucmd_win != NULL)
8714 win_free_lsize(aucmd_win);
8715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717 new_ScreenLines = (schar_T *)lalloc((long_u)(
8718 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8719#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008720 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (enc_utf8)
8722 {
8723 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8724 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008725 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008726 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8728 }
8729 if (enc_dbcs == DBCS_JPNU)
8730 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8731 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8732#endif
8733 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8734 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8735 new_LineOffset = (unsigned *)lalloc((long_u)(
8736 Rows * sizeof(unsigned)), FALSE);
8737 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008738#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008739 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008742 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {
8744 if (win_alloc_lines(wp) == FAIL)
8745 {
8746 outofmem = TRUE;
8747#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008748 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#endif
8750 }
8751 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008752#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008753 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8754 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008755 outofmem = TRUE;
8756#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008757#ifdef FEAT_WINDOWS
8758give_up:
8759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008761#ifdef FEAT_MBYTE
8762 for (i = 0; i < p_mco; ++i)
8763 if (new_ScreenLinesC[i] == NULL)
8764 break;
8765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 if (new_ScreenLines == NULL
8767#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008768 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8770#endif
8771 || new_ScreenAttrs == NULL
8772 || new_LineOffset == NULL
8773 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008774#ifdef FEAT_WINDOWS
8775 || new_TabPageIdxs == NULL
8776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 || outofmem)
8778 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008779 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008780 {
8781 /* guess the size */
8782 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8783
8784 /* Remember we did this to avoid getting outofmem messages over
8785 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008786 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 vim_free(new_ScreenLines);
8789 new_ScreenLines = NULL;
8790#ifdef FEAT_MBYTE
8791 vim_free(new_ScreenLinesUC);
8792 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008793 for (i = 0; i < p_mco; ++i)
8794 {
8795 vim_free(new_ScreenLinesC[i]);
8796 new_ScreenLinesC[i] = NULL;
8797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 vim_free(new_ScreenLines2);
8799 new_ScreenLines2 = NULL;
8800#endif
8801 vim_free(new_ScreenAttrs);
8802 new_ScreenAttrs = NULL;
8803 vim_free(new_LineOffset);
8804 new_LineOffset = NULL;
8805 vim_free(new_LineWraps);
8806 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008807#ifdef FEAT_WINDOWS
8808 vim_free(new_TabPageIdxs);
8809 new_TabPageIdxs = NULL;
8810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
8812 else
8813 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008814 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 for (new_row = 0; new_row < Rows; ++new_row)
8817 {
8818 new_LineOffset[new_row] = new_row * Columns;
8819 new_LineWraps[new_row] = FALSE;
8820
8821 /*
8822 * If the screen is not going to be cleared, copy as much as
8823 * possible from the old screen to the new one and clear the rest
8824 * (used when resizing the window at the "--more--" prompt or when
8825 * executing an external command, for the GUI).
8826 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008827 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
8829 (void)vim_memset(new_ScreenLines + new_row * Columns,
8830 ' ', (size_t)Columns * sizeof(schar_T));
8831#ifdef FEAT_MBYTE
8832 if (enc_utf8)
8833 {
8834 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8835 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008836 for (i = 0; i < p_mco; ++i)
8837 (void)vim_memset(new_ScreenLinesC[i]
8838 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 0, (size_t)Columns * sizeof(u8char_T));
8840 }
8841 if (enc_dbcs == DBCS_JPNU)
8842 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8843 0, (size_t)Columns * sizeof(schar_T));
8844#endif
8845 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8846 0, (size_t)Columns * sizeof(sattr_T));
8847 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008848 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {
8850 if (screen_Columns < Columns)
8851 len = screen_Columns;
8852 else
8853 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008854#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008855 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008856 * may be invalid now. Also when p_mco changes. */
8857 if (!(enc_utf8 && ScreenLinesUC == NULL)
8858 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008859#endif
8860 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8861 ScreenLines + LineOffset[old_row],
8862 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008864 if (enc_utf8 && ScreenLinesUC != NULL
8865 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 {
8867 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8868 ScreenLinesUC + LineOffset[old_row],
8869 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008870 for (i = 0; i < p_mco; ++i)
8871 mch_memmove(new_ScreenLinesC[i]
8872 + new_LineOffset[new_row],
8873 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 (size_t)len * sizeof(u8char_T));
8875 }
8876 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8877 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8878 ScreenLines2 + LineOffset[old_row],
8879 (size_t)len * sizeof(schar_T));
8880#endif
8881 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8882 ScreenAttrs + LineOffset[old_row],
8883 (size_t)len * sizeof(sattr_T));
8884 }
8885 }
8886 }
8887 /* Use the last line of the screen for the current line. */
8888 current_ScreenLine = new_ScreenLines + Rows * Columns;
8889 }
8890
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008891 free_screenlines();
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 ScreenLines = new_ScreenLines;
8894#ifdef FEAT_MBYTE
8895 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008896 for (i = 0; i < p_mco; ++i)
8897 ScreenLinesC[i] = new_ScreenLinesC[i];
8898 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 ScreenLines2 = new_ScreenLines2;
8900#endif
8901 ScreenAttrs = new_ScreenAttrs;
8902 LineOffset = new_LineOffset;
8903 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008904#ifdef FEAT_WINDOWS
8905 TabPageIdxs = new_TabPageIdxs;
8906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907
8908 /* It's important that screen_Rows and screen_Columns reflect the actual
8909 * size of ScreenLines[]. Set them before calling anything. */
8910#ifdef FEAT_GUI
8911 old_Rows = screen_Rows;
8912#endif
8913 screen_Rows = Rows;
8914 screen_Columns = Columns;
8915
8916 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008917 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 screenclear2();
8919
8920#ifdef FEAT_GUI
8921 else if (gui.in_use
8922 && !gui.starting
8923 && ScreenLines != NULL
8924 && old_Rows != Rows)
8925 {
8926 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8927 /*
8928 * Adjust the position of the cursor, for when executing an external
8929 * command.
8930 */
8931 if (msg_row >= Rows) /* Rows got smaller */
8932 msg_row = Rows - 1; /* put cursor at last row */
8933 else if (Rows > old_Rows) /* Rows got bigger */
8934 msg_row += Rows - old_Rows; /* put cursor in same place */
8935 if (msg_col >= Columns) /* Columns got smaller */
8936 msg_col = Columns - 1; /* put cursor at last column */
8937 }
8938#endif
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008941 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008942
8943#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008944 /*
8945 * Do not apply autocommands more than 3 times to avoid an endless loop
8946 * in case applying autocommands always changes Rows or Columns.
8947 */
8948 if (starting == 0 && ++retry_count <= 3)
8949 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008950 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008951 /* In rare cases, autocommands may have altered Rows or Columns,
8952 * jump back to check if we need to allocate the screen again. */
8953 goto retry;
8954 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
8958 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008959free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008960{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008961#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008962 int i;
8963
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008964 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 for (i = 0; i < Screen_mco; ++i)
8966 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008967 vim_free(ScreenLines2);
8968#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008969 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008970 vim_free(ScreenAttrs);
8971 vim_free(LineOffset);
8972 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008973#ifdef FEAT_WINDOWS
8974 vim_free(TabPageIdxs);
8975#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008976}
8977
8978 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008979screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
8981 check_for_delay(FALSE);
8982 screenalloc(FALSE); /* allocate screen buffers if size changed */
8983 screenclear2(); /* clear the screen */
8984}
8985
8986 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008987screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
8989 int i;
8990
8991 if (starting == NO_SCREEN || ScreenLines == NULL
8992#ifdef FEAT_GUI
8993 || (gui.in_use && gui.starting)
8994#endif
8995 )
8996 return;
8997
8998#ifdef FEAT_GUI
8999 if (!gui.in_use)
9000#endif
9001 screen_attr = -1; /* force setting the Normal colors */
9002 screen_stop_highlight(); /* don't want highlighting here */
9003
9004#ifdef FEAT_CLIPBOARD
9005 /* disable selection without redrawing it */
9006 clip_scroll_selection(9999);
9007#endif
9008
9009 /* blank out ScreenLines */
9010 for (i = 0; i < Rows; ++i)
9011 {
9012 lineclear(LineOffset[i], (int)Columns);
9013 LineWraps[i] = FALSE;
9014 }
9015
9016 if (can_clear(T_CL))
9017 {
9018 out_str(T_CL); /* clear the display */
9019 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009020 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
9022 else
9023 {
9024 /* can't clear the screen, mark all chars with invalid attributes */
9025 for (i = 0; i < Rows; ++i)
9026 lineinvalid(LineOffset[i], (int)Columns);
9027 clear_cmdline = TRUE;
9028 }
9029
9030 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9031
9032 win_rest_invalid(firstwin);
9033 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009034#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009035 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 if (must_redraw == CLEAR) /* no need to clear again */
9038 must_redraw = NOT_VALID;
9039 compute_cmdrow();
9040 msg_row = cmdline_row; /* put cursor on last line for messages */
9041 msg_col = 0;
9042 screen_start(); /* don't know where cursor is now */
9043 msg_scrolled = 0; /* can't scroll back */
9044 msg_didany = FALSE;
9045 msg_didout = FALSE;
9046}
9047
9048/*
9049 * Clear one line in ScreenLines.
9050 */
9051 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009052lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9055#ifdef FEAT_MBYTE
9056 if (enc_utf8)
9057 (void)vim_memset(ScreenLinesUC + off, 0,
9058 (size_t)width * sizeof(u8char_T));
9059#endif
9060 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9061}
9062
9063/*
9064 * Mark one line in ScreenLines invalid by setting the attributes to an
9065 * invalid value.
9066 */
9067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009068lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9071}
9072
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009073#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074/*
9075 * Copy part of a Screenline for vertically split window "wp".
9076 */
9077 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009078linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 unsigned off_to = LineOffset[to] + wp->w_wincol;
9081 unsigned off_from = LineOffset[from] + wp->w_wincol;
9082
9083 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9084 wp->w_width * sizeof(schar_T));
9085# ifdef FEAT_MBYTE
9086 if (enc_utf8)
9087 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009088 int i;
9089
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9091 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009092 for (i = 0; i < p_mco; ++i)
9093 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9094 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 }
9096 if (enc_dbcs == DBCS_JPNU)
9097 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9098 wp->w_width * sizeof(schar_T));
9099# endif
9100 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9101 wp->w_width * sizeof(sattr_T));
9102}
9103#endif
9104
9105/*
9106 * Return TRUE if clearing with term string "p" would work.
9107 * It can't work when the string is empty or it won't set the right background.
9108 */
9109 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009110can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 return (*p != NUL && (t_colors <= 1
9113#ifdef FEAT_GUI
9114 || gui.in_use
9115#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009116#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009117 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009118 || (!p_tgc && cterm_normal_bg_color == 0)
9119#else
9120 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009121#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009122 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123}
9124
9125/*
9126 * Reset cursor position. Use whenever cursor was moved because of outputting
9127 * something directly to the screen (shell commands) or a terminal control
9128 * code.
9129 */
9130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009131screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 screen_cur_row = screen_cur_col = 9999;
9134}
9135
9136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * Move the cursor to position "row","col" in the screen.
9138 * This tries to find the most efficient way to move, minimizing the number of
9139 * characters sent to the terminal.
9140 */
9141 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009142windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009144 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 int i;
9146 int plan;
9147 int cost;
9148 int wouldbe_col;
9149 int noinvcurs;
9150 char_u *bs;
9151 int goto_cost;
9152 int attr;
9153
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009154#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9156
9157#define PLAN_LE 1
9158#define PLAN_CR 2
9159#define PLAN_NL 3
9160#define PLAN_WRITE 4
9161 /* Can't use ScreenLines unless initialized */
9162 if (ScreenLines == NULL)
9163 return;
9164
9165 if (col != screen_cur_col || row != screen_cur_row)
9166 {
9167 /* Check for valid position. */
9168 if (row < 0) /* window without text lines? */
9169 row = 0;
9170 if (row >= screen_Rows)
9171 row = screen_Rows - 1;
9172 if (col >= screen_Columns)
9173 col = screen_Columns - 1;
9174
9175 /* check if no cursor movement is allowed in highlight mode */
9176 if (screen_attr && *T_MS == NUL)
9177 noinvcurs = HIGHL_COST;
9178 else
9179 noinvcurs = 0;
9180 goto_cost = GOTO_COST + noinvcurs;
9181
9182 /*
9183 * Plan how to do the positioning:
9184 * 1. Use CR to move it to column 0, same row.
9185 * 2. Use T_LE to move it a few columns to the left.
9186 * 3. Use NL to move a few lines down, column 0.
9187 * 4. Move a few columns to the right with T_ND or by writing chars.
9188 *
9189 * Don't do this if the cursor went beyond the last column, the cursor
9190 * position is unknown then (some terminals wrap, some don't )
9191 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009192 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 * characters to move the cursor to the right.
9194 */
9195 if (row >= screen_cur_row && screen_cur_col < Columns)
9196 {
9197 /*
9198 * If the cursor is in the same row, bigger col, we can use CR
9199 * or T_LE.
9200 */
9201 bs = NULL; /* init for GCC */
9202 attr = screen_attr;
9203 if (row == screen_cur_row && col < screen_cur_col)
9204 {
9205 /* "le" is preferred over "bc", because "bc" is obsolete */
9206 if (*T_LE)
9207 bs = T_LE; /* "cursor left" */
9208 else
9209 bs = T_BC; /* "backspace character (old) */
9210 if (*bs)
9211 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9212 else
9213 cost = 999;
9214 if (col + 1 < cost) /* using CR is less characters */
9215 {
9216 plan = PLAN_CR;
9217 wouldbe_col = 0;
9218 cost = 1; /* CR is just one character */
9219 }
9220 else
9221 {
9222 plan = PLAN_LE;
9223 wouldbe_col = col;
9224 }
9225 if (noinvcurs) /* will stop highlighting */
9226 {
9227 cost += noinvcurs;
9228 attr = 0;
9229 }
9230 }
9231
9232 /*
9233 * If the cursor is above where we want to be, we can use CR LF.
9234 */
9235 else if (row > screen_cur_row)
9236 {
9237 plan = PLAN_NL;
9238 wouldbe_col = 0;
9239 cost = (row - screen_cur_row) * 2; /* CR LF */
9240 if (noinvcurs) /* will stop highlighting */
9241 {
9242 cost += noinvcurs;
9243 attr = 0;
9244 }
9245 }
9246
9247 /*
9248 * If the cursor is in the same row, smaller col, just use write.
9249 */
9250 else
9251 {
9252 plan = PLAN_WRITE;
9253 wouldbe_col = screen_cur_col;
9254 cost = 0;
9255 }
9256
9257 /*
9258 * Check if any characters that need to be written have the
9259 * correct attributes. Also avoid UTF-8 characters.
9260 */
9261 i = col - wouldbe_col;
9262 if (i > 0)
9263 cost += i;
9264 if (cost < goto_cost && i > 0)
9265 {
9266 /*
9267 * Check if the attributes are correct without additionally
9268 * stopping highlighting.
9269 */
9270 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9271 while (i && *p++ == attr)
9272 --i;
9273 if (i != 0)
9274 {
9275 /*
9276 * Try if it works when highlighting is stopped here.
9277 */
9278 if (*--p == 0)
9279 {
9280 cost += noinvcurs;
9281 while (i && *p++ == 0)
9282 --i;
9283 }
9284 if (i != 0)
9285 cost = 999; /* different attributes, don't do it */
9286 }
9287#ifdef FEAT_MBYTE
9288 if (enc_utf8)
9289 {
9290 /* Don't use an UTF-8 char for positioning, it's slow. */
9291 for (i = wouldbe_col; i < col; ++i)
9292 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9293 {
9294 cost = 999;
9295 break;
9296 }
9297 }
9298#endif
9299 }
9300
9301 /*
9302 * We can do it without term_windgoto()!
9303 */
9304 if (cost < goto_cost)
9305 {
9306 if (plan == PLAN_LE)
9307 {
9308 if (noinvcurs)
9309 screen_stop_highlight();
9310 while (screen_cur_col > col)
9311 {
9312 out_str(bs);
9313 --screen_cur_col;
9314 }
9315 }
9316 else if (plan == PLAN_CR)
9317 {
9318 if (noinvcurs)
9319 screen_stop_highlight();
9320 out_char('\r');
9321 screen_cur_col = 0;
9322 }
9323 else if (plan == PLAN_NL)
9324 {
9325 if (noinvcurs)
9326 screen_stop_highlight();
9327 while (screen_cur_row < row)
9328 {
9329 out_char('\n');
9330 ++screen_cur_row;
9331 }
9332 screen_cur_col = 0;
9333 }
9334
9335 i = col - screen_cur_col;
9336 if (i > 0)
9337 {
9338 /*
9339 * Use cursor-right if it's one character only. Avoids
9340 * removing a line of pixels from the last bold char, when
9341 * using the bold trick in the GUI.
9342 */
9343 if (T_ND[0] != NUL && T_ND[1] == NUL)
9344 {
9345 while (i-- > 0)
9346 out_char(*T_ND);
9347 }
9348 else
9349 {
9350 int off;
9351
9352 off = LineOffset[row] + screen_cur_col;
9353 while (i-- > 0)
9354 {
9355 if (ScreenAttrs[off] != screen_attr)
9356 screen_stop_highlight();
9357#ifdef FEAT_MBYTE
9358 out_flush_check();
9359#endif
9360 out_char(ScreenLines[off]);
9361#ifdef FEAT_MBYTE
9362 if (enc_dbcs == DBCS_JPNU
9363 && ScreenLines[off] == 0x8e)
9364 out_char(ScreenLines2[off]);
9365#endif
9366 ++off;
9367 }
9368 }
9369 }
9370 }
9371 }
9372 else
9373 cost = 999;
9374
9375 if (cost >= goto_cost)
9376 {
9377 if (noinvcurs)
9378 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009379 if (row == screen_cur_row && (col > screen_cur_col)
9380 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 term_cursor_right(col - screen_cur_col);
9382 else
9383 term_windgoto(row, col);
9384 }
9385 screen_cur_row = row;
9386 screen_cur_col = col;
9387 }
9388}
9389
9390/*
9391 * Set cursor to its position in the current window.
9392 */
9393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009394setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 if (redrawing())
9397 {
9398 validate_cursor();
9399 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9400 W_WINCOL(curwin) + (
9401#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009402 /* With 'rightleft' set and the cursor on a double-wide
9403 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9405# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009406 (has_mbyte
9407 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9408 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409# endif
9410 1)) :
9411#endif
9412 curwin->w_wcol));
9413 }
9414}
9415
9416
9417/*
9418 * insert 'line_count' lines at 'row' in window 'wp'
9419 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9420 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9421 * scrolling.
9422 * Returns FAIL if the lines are not inserted, OK for success.
9423 */
9424 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009425win_ins_lines(
9426 win_T *wp,
9427 int row,
9428 int line_count,
9429 int invalid,
9430 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 int did_delete;
9433 int nextrow;
9434 int lastrow;
9435 int retval;
9436
9437 if (invalid)
9438 wp->w_lines_valid = 0;
9439
9440 if (wp->w_height < 5)
9441 return FAIL;
9442
9443 if (line_count > wp->w_height - row)
9444 line_count = wp->w_height - row;
9445
9446 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9447 if (retval != MAYBE)
9448 return retval;
9449
9450 /*
9451 * If there is a next window or a status line, we first try to delete the
9452 * lines at the bottom to avoid messing what is after the window.
9453 * If this fails and there are following windows, don't do anything to avoid
9454 * messing up those windows, better just redraw.
9455 */
9456 did_delete = FALSE;
9457#ifdef FEAT_WINDOWS
9458 if (wp->w_next != NULL || wp->w_status_height)
9459 {
9460 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9461 line_count, (int)Rows, FALSE, NULL) == OK)
9462 did_delete = TRUE;
9463 else if (wp->w_next)
9464 return FAIL;
9465 }
9466#endif
9467 /*
9468 * if no lines deleted, blank the lines that will end up below the window
9469 */
9470 if (!did_delete)
9471 {
9472#ifdef FEAT_WINDOWS
9473 wp->w_redr_status = TRUE;
9474#endif
9475 redraw_cmdline = TRUE;
9476 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9477 lastrow = nextrow + line_count;
9478 if (lastrow > Rows)
9479 lastrow = Rows;
9480 screen_fill(nextrow - line_count, lastrow - line_count,
9481 W_WINCOL(wp), (int)W_ENDCOL(wp),
9482 ' ', ' ', 0);
9483 }
9484
9485 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9486 == FAIL)
9487 {
9488 /* deletion will have messed up other windows */
9489 if (did_delete)
9490 {
9491#ifdef FEAT_WINDOWS
9492 wp->w_redr_status = TRUE;
9493#endif
9494 win_rest_invalid(W_NEXT(wp));
9495 }
9496 return FAIL;
9497 }
9498
9499 return OK;
9500}
9501
9502/*
9503 * delete "line_count" window lines at "row" in window "wp"
9504 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9505 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9506 * scrolling
9507 * Return OK for success, FAIL if the lines are not deleted.
9508 */
9509 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009510win_del_lines(
9511 win_T *wp,
9512 int row,
9513 int line_count,
9514 int invalid,
9515 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
9517 int retval;
9518
9519 if (invalid)
9520 wp->w_lines_valid = 0;
9521
9522 if (line_count > wp->w_height - row)
9523 line_count = wp->w_height - row;
9524
9525 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9526 if (retval != MAYBE)
9527 return retval;
9528
9529 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9530 (int)Rows, FALSE, NULL) == FAIL)
9531 return FAIL;
9532
9533#ifdef FEAT_WINDOWS
9534 /*
9535 * If there are windows or status lines below, try to put them at the
9536 * correct place. If we can't do that, they have to be redrawn.
9537 */
9538 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9539 {
9540 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9541 line_count, (int)Rows, NULL) == FAIL)
9542 {
9543 wp->w_redr_status = TRUE;
9544 win_rest_invalid(wp->w_next);
9545 }
9546 }
9547 /*
9548 * If this is the last window and there is no status line, redraw the
9549 * command line later.
9550 */
9551 else
9552#endif
9553 redraw_cmdline = TRUE;
9554 return OK;
9555}
9556
9557/*
9558 * Common code for win_ins_lines() and win_del_lines().
9559 * Returns OK or FAIL when the work has been done.
9560 * Returns MAYBE when not finished yet.
9561 */
9562 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009563win_do_lines(
9564 win_T *wp,
9565 int row,
9566 int line_count,
9567 int mayclear,
9568 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 int retval;
9571
9572 if (!redrawing() || line_count <= 0)
9573 return FAIL;
9574
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009575 /* When inserting lines would result in loss of command output, just redraw
9576 * the lines. */
9577 if (no_win_do_lines_ins && !del)
9578 return FAIL;
9579
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 /* only a few lines left: redraw is faster */
9581 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009582#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 && wp->w_width == Columns
9584#endif
9585 )
9586 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009587 if (!no_win_do_lines_ins)
9588 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 return FAIL;
9590 }
9591
9592 /*
9593 * Delete all remaining lines
9594 */
9595 if (row + line_count >= wp->w_height)
9596 {
9597 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9598 W_WINCOL(wp), (int)W_ENDCOL(wp),
9599 ' ', ' ', 0);
9600 return OK;
9601 }
9602
9603 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009604 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009606 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009608 if (!no_win_do_lines_ins)
9609 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 /*
9612 * If the terminal can set a scroll region, use that.
9613 * Always do this in a vertically split window. This will redraw from
9614 * ScreenLines[] when t_CV isn't defined. That's faster than using
9615 * win_line().
9616 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009617 * a character in the lower right corner of the scroll region may cause a
9618 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 */
9620 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009621#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 || W_WIDTH(wp) != Columns
9623#endif
9624 )
9625 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009626#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9628#endif
9629 scroll_region_set(wp, row);
9630 if (del)
9631 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9632 wp->w_height - row, FALSE, wp);
9633 else
9634 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9635 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009636#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9638#endif
9639 scroll_region_reset();
9640 return retval;
9641 }
9642
9643#ifdef FEAT_WINDOWS
9644 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9645 return FAIL;
9646#endif
9647
9648 return MAYBE;
9649}
9650
9651/*
9652 * window 'wp' and everything after it is messed up, mark it for redraw
9653 */
9654 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009655win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657#ifdef FEAT_WINDOWS
9658 while (wp != NULL)
9659#else
9660 if (wp != NULL)
9661#endif
9662 {
9663 redraw_win_later(wp, NOT_VALID);
9664#ifdef FEAT_WINDOWS
9665 wp->w_redr_status = TRUE;
9666 wp = wp->w_next;
9667#endif
9668 }
9669 redraw_cmdline = TRUE;
9670}
9671
9672/*
9673 * The rest of the routines in this file perform screen manipulations. The
9674 * given operation is performed physically on the screen. The corresponding
9675 * change is also made to the internal screen image. In this way, the editor
9676 * anticipates the effect of editing changes on the appearance of the screen.
9677 * That way, when we call screenupdate a complete redraw isn't usually
9678 * necessary. Another advantage is that we can keep adding code to anticipate
9679 * screen changes, and in the meantime, everything still works.
9680 */
9681
9682/*
9683 * types for inserting or deleting lines
9684 */
9685#define USE_T_CAL 1
9686#define USE_T_CDL 2
9687#define USE_T_AL 3
9688#define USE_T_CE 4
9689#define USE_T_DL 5
9690#define USE_T_SR 6
9691#define USE_NL 7
9692#define USE_T_CD 8
9693#define USE_REDRAW 9
9694
9695/*
9696 * insert lines on the screen and update ScreenLines[]
9697 * 'end' is the line after the scrolled part. Normally it is Rows.
9698 * When scrolling region used 'off' is the offset from the top for the region.
9699 * 'row' and 'end' are relative to the start of the region.
9700 *
9701 * return FAIL for failure, OK for success.
9702 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009703 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009704screen_ins_lines(
9705 int off,
9706 int row,
9707 int line_count,
9708 int end,
9709 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 int i;
9712 int j;
9713 unsigned temp;
9714 int cursor_row;
9715 int type;
9716 int result_empty;
9717 int can_ce = can_clear(T_CE);
9718
9719 /*
9720 * FAIL if
9721 * - there is no valid screen
9722 * - the screen has to be redrawn completely
9723 * - the line count is less than one
9724 * - the line count is more than 'ttyscroll'
9725 */
9726 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9727 return FAIL;
9728
9729 /*
9730 * There are seven ways to insert lines:
9731 * 0. When in a vertically split window and t_CV isn't set, redraw the
9732 * characters from ScreenLines[].
9733 * 1. Use T_CD (clear to end of display) if it exists and the result of
9734 * the insert is just empty lines
9735 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9736 * present or line_count > 1. It looks better if we do all the inserts
9737 * at once.
9738 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9739 * insert is just empty lines and T_CE is not present or line_count >
9740 * 1.
9741 * 4. Use T_AL (insert line) if it exists.
9742 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9743 * just empty lines.
9744 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9745 * just empty lines.
9746 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9747 * the 'da' flag is not set or we have clear line capability.
9748 * 8. redraw the characters from ScreenLines[].
9749 *
9750 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9751 * the scrollbar for the window. It does have insert line, use that if it
9752 * exists.
9753 */
9754 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009755#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9757 type = USE_REDRAW;
9758 else
9759#endif
9760 if (can_clear(T_CD) && result_empty)
9761 type = USE_T_CD;
9762 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9763 type = USE_T_CAL;
9764 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9765 type = USE_T_CDL;
9766 else if (*T_AL != NUL)
9767 type = USE_T_AL;
9768 else if (can_ce && result_empty)
9769 type = USE_T_CE;
9770 else if (*T_DL != NUL && result_empty)
9771 type = USE_T_DL;
9772 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9773 type = USE_T_SR;
9774 else
9775 return FAIL;
9776
9777 /*
9778 * For clearing the lines screen_del_lines() is used. This will also take
9779 * care of t_db if necessary.
9780 */
9781 if (type == USE_T_CD || type == USE_T_CDL ||
9782 type == USE_T_CE || type == USE_T_DL)
9783 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9784
9785 /*
9786 * If text is retained below the screen, first clear or delete as many
9787 * lines at the bottom of the window as are about to be inserted so that
9788 * the deleted lines won't later surface during a screen_del_lines.
9789 */
9790 if (*T_DB)
9791 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9792
9793#ifdef FEAT_CLIPBOARD
9794 /* Remove a modeless selection when inserting lines halfway the screen
9795 * or not the full width of the screen. */
9796 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009797# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 || (wp != NULL && wp->w_width != Columns)
9799# endif
9800 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009801 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 else
9803 clip_scroll_selection(-line_count);
9804#endif
9805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806#ifdef FEAT_GUI
9807 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9808 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009809 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#endif
9811
9812 if (*T_CCS != NUL) /* cursor relative to region */
9813 cursor_row = row;
9814 else
9815 cursor_row = row + off;
9816
9817 /*
9818 * Shift LineOffset[] line_count down to reflect the inserted lines.
9819 * Clear the inserted lines in ScreenLines[].
9820 */
9821 row += off;
9822 end += off;
9823 for (i = 0; i < line_count; ++i)
9824 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009825#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (wp != NULL && wp->w_width != Columns)
9827 {
9828 /* need to copy part of a line */
9829 j = end - 1 - i;
9830 while ((j -= line_count) >= row)
9831 linecopy(j + line_count, j, wp);
9832 j += line_count;
9833 if (can_clear((char_u *)" "))
9834 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9835 else
9836 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9837 LineWraps[j] = FALSE;
9838 }
9839 else
9840#endif
9841 {
9842 j = end - 1 - i;
9843 temp = LineOffset[j];
9844 while ((j -= line_count) >= row)
9845 {
9846 LineOffset[j + line_count] = LineOffset[j];
9847 LineWraps[j + line_count] = LineWraps[j];
9848 }
9849 LineOffset[j + line_count] = temp;
9850 LineWraps[j + line_count] = FALSE;
9851 if (can_clear((char_u *)" "))
9852 lineclear(temp, (int)Columns);
9853 else
9854 lineinvalid(temp, (int)Columns);
9855 }
9856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857
9858 screen_stop_highlight();
9859 windgoto(cursor_row, 0);
9860
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009861#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 /* redraw the characters */
9863 if (type == USE_REDRAW)
9864 redraw_block(row, end, wp);
9865 else
9866#endif
9867 if (type == USE_T_CAL)
9868 {
9869 term_append_lines(line_count);
9870 screen_start(); /* don't know where cursor is now */
9871 }
9872 else
9873 {
9874 for (i = 0; i < line_count; i++)
9875 {
9876 if (type == USE_T_AL)
9877 {
9878 if (i && cursor_row != 0)
9879 windgoto(cursor_row, 0);
9880 out_str(T_AL);
9881 }
9882 else /* type == USE_T_SR */
9883 out_str(T_SR);
9884 screen_start(); /* don't know where cursor is now */
9885 }
9886 }
9887
9888 /*
9889 * With scroll-reverse and 'da' flag set we need to clear the lines that
9890 * have been scrolled down into the region.
9891 */
9892 if (type == USE_T_SR && *T_DA)
9893 {
9894 for (i = 0; i < line_count; ++i)
9895 {
9896 windgoto(off + i, 0);
9897 out_str(T_CE);
9898 screen_start(); /* don't know where cursor is now */
9899 }
9900 }
9901
9902#ifdef FEAT_GUI
9903 gui_can_update_cursor();
9904 if (gui.in_use)
9905 out_flush(); /* always flush after a scroll */
9906#endif
9907 return OK;
9908}
9909
9910/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009911 * Delete lines on the screen and update ScreenLines[].
9912 * "end" is the line after the scrolled part. Normally it is Rows.
9913 * When scrolling region used "off" is the offset from the top for the region.
9914 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 *
9916 * Return OK for success, FAIL if the lines are not deleted.
9917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009919screen_del_lines(
9920 int off,
9921 int row,
9922 int line_count,
9923 int end,
9924 int force, /* even when line_count > p_ttyscroll */
9925 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 int j;
9928 int i;
9929 unsigned temp;
9930 int cursor_row;
9931 int cursor_end;
9932 int result_empty; /* result is empty until end of region */
9933 int can_delete; /* deleting line codes can be used */
9934 int type;
9935
9936 /*
9937 * FAIL if
9938 * - there is no valid screen
9939 * - the screen has to be redrawn completely
9940 * - the line count is less than one
9941 * - the line count is more than 'ttyscroll'
9942 */
9943 if (!screen_valid(TRUE) || line_count <= 0 ||
9944 (!force && line_count > p_ttyscroll))
9945 return FAIL;
9946
9947 /*
9948 * Check if the rest of the current region will become empty.
9949 */
9950 result_empty = row + line_count >= end;
9951
9952 /*
9953 * We can delete lines only when 'db' flag not set or when 'ce' option
9954 * available.
9955 */
9956 can_delete = (*T_DB == NUL || can_clear(T_CE));
9957
9958 /*
9959 * There are six ways to delete lines:
9960 * 0. When in a vertically split window and t_CV isn't set, redraw the
9961 * characters from ScreenLines[].
9962 * 1. Use T_CD if it exists and the result is empty.
9963 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9964 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9965 * none of the other ways work.
9966 * 4. Use T_CE (erase line) if the result is empty.
9967 * 5. Use T_DL (delete line) if it exists.
9968 * 6. redraw the characters from ScreenLines[].
9969 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009970#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9972 type = USE_REDRAW;
9973 else
9974#endif
9975 if (can_clear(T_CD) && result_empty)
9976 type = USE_T_CD;
9977#if defined(__BEOS__) && defined(BEOS_DR8)
9978 /*
9979 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9980 * its internal termcap... this works okay for tests which test *T_DB !=
9981 * NUL. It has the disadvantage that the user cannot use any :set t_*
9982 * command to get T_DB (back) to empty_option, only :set term=... will do
9983 * the trick...
9984 * Anyway, this hack will hopefully go away with the next OS release.
9985 * (Olaf Seibert)
9986 */
9987 else if (row == 0 && T_DB == empty_option
9988 && (line_count == 1 || *T_CDL == NUL))
9989#else
9990 else if (row == 0 && (
9991#ifndef AMIGA
9992 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9993 * up, so use delete-line command */
9994 line_count == 1 ||
9995#endif
9996 *T_CDL == NUL))
9997#endif
9998 type = USE_NL;
9999 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10000 type = USE_T_CDL;
10001 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010002#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 && (wp == NULL || wp->w_width == Columns)
10004#endif
10005 )
10006 type = USE_T_CE;
10007 else if (*T_DL != NUL && can_delete)
10008 type = USE_T_DL;
10009 else if (*T_CDL != NUL && can_delete)
10010 type = USE_T_CDL;
10011 else
10012 return FAIL;
10013
10014#ifdef FEAT_CLIPBOARD
10015 /* Remove a modeless selection when deleting lines halfway the screen or
10016 * not the full width of the screen. */
10017 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010018# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 || (wp != NULL && wp->w_width != Columns)
10020# endif
10021 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010022 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 else
10024 clip_scroll_selection(line_count);
10025#endif
10026
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027#ifdef FEAT_GUI
10028 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10029 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010030 gui_dont_update_cursor(gui.cursor_row >= row + off
10031 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033
10034 if (*T_CCS != NUL) /* cursor relative to region */
10035 {
10036 cursor_row = row;
10037 cursor_end = end;
10038 }
10039 else
10040 {
10041 cursor_row = row + off;
10042 cursor_end = end + off;
10043 }
10044
10045 /*
10046 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10047 * Clear the inserted lines in ScreenLines[].
10048 */
10049 row += off;
10050 end += off;
10051 for (i = 0; i < line_count; ++i)
10052 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010053#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 if (wp != NULL && wp->w_width != Columns)
10055 {
10056 /* need to copy part of a line */
10057 j = row + i;
10058 while ((j += line_count) <= end - 1)
10059 linecopy(j - line_count, j, wp);
10060 j -= line_count;
10061 if (can_clear((char_u *)" "))
10062 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10063 else
10064 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10065 LineWraps[j] = FALSE;
10066 }
10067 else
10068#endif
10069 {
10070 /* whole width, moving the line pointers is faster */
10071 j = row + i;
10072 temp = LineOffset[j];
10073 while ((j += line_count) <= end - 1)
10074 {
10075 LineOffset[j - line_count] = LineOffset[j];
10076 LineWraps[j - line_count] = LineWraps[j];
10077 }
10078 LineOffset[j - line_count] = temp;
10079 LineWraps[j - line_count] = FALSE;
10080 if (can_clear((char_u *)" "))
10081 lineclear(temp, (int)Columns);
10082 else
10083 lineinvalid(temp, (int)Columns);
10084 }
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
10087 screen_stop_highlight();
10088
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010089#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 /* redraw the characters */
10091 if (type == USE_REDRAW)
10092 redraw_block(row, end, wp);
10093 else
10094#endif
10095 if (type == USE_T_CD) /* delete the lines */
10096 {
10097 windgoto(cursor_row, 0);
10098 out_str(T_CD);
10099 screen_start(); /* don't know where cursor is now */
10100 }
10101 else if (type == USE_T_CDL)
10102 {
10103 windgoto(cursor_row, 0);
10104 term_delete_lines(line_count);
10105 screen_start(); /* don't know where cursor is now */
10106 }
10107 /*
10108 * Deleting lines at top of the screen or scroll region: Just scroll
10109 * the whole screen (scroll region) up by outputting newlines on the
10110 * last line.
10111 */
10112 else if (type == USE_NL)
10113 {
10114 windgoto(cursor_end - 1, 0);
10115 for (i = line_count; --i >= 0; )
10116 out_char('\n'); /* cursor will remain on same line */
10117 }
10118 else
10119 {
10120 for (i = line_count; --i >= 0; )
10121 {
10122 if (type == USE_T_DL)
10123 {
10124 windgoto(cursor_row, 0);
10125 out_str(T_DL); /* delete a line */
10126 }
10127 else /* type == USE_T_CE */
10128 {
10129 windgoto(cursor_row + i, 0);
10130 out_str(T_CE); /* erase a line */
10131 }
10132 screen_start(); /* don't know where cursor is now */
10133 }
10134 }
10135
10136 /*
10137 * If the 'db' flag is set, we need to clear the lines that have been
10138 * scrolled up at the bottom of the region.
10139 */
10140 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10141 {
10142 for (i = line_count; i > 0; --i)
10143 {
10144 windgoto(cursor_end - i, 0);
10145 out_str(T_CE); /* erase a line */
10146 screen_start(); /* don't know where cursor is now */
10147 }
10148 }
10149
10150#ifdef FEAT_GUI
10151 gui_can_update_cursor();
10152 if (gui.in_use)
10153 out_flush(); /* always flush after a scroll */
10154#endif
10155
10156 return OK;
10157}
10158
10159/*
10160 * show the current mode and ruler
10161 *
10162 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10163 * If clear_cmdline is FALSE there may be a message there that needs to be
10164 * cleared only if a mode is shown.
10165 * Return the length of the message (0 if no message).
10166 */
10167 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010168showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169{
10170 int need_clear;
10171 int length = 0;
10172 int do_mode;
10173 int attr;
10174 int nwr_save;
10175#ifdef FEAT_INS_EXPAND
10176 int sub_attr;
10177#endif
10178
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010179 do_mode = ((p_smd && msg_silent == 0)
10180 && ((State & INSERT)
10181 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010182 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (do_mode || Recording)
10184 {
10185 /*
10186 * Don't show mode right now, when not redrawing or inside a mapping.
10187 * Call char_avail() only when we are going to show something, because
10188 * it takes a bit of time.
10189 */
10190 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10191 {
10192 redraw_cmdline = TRUE; /* show mode later */
10193 return 0;
10194 }
10195
10196 nwr_save = need_wait_return;
10197
10198 /* wait a bit before overwriting an important message */
10199 check_for_delay(FALSE);
10200
10201 /* if the cmdline is more than one line high, erase top lines */
10202 need_clear = clear_cmdline;
10203 if (clear_cmdline && cmdline_row < Rows - 1)
10204 msg_clr_cmdline(); /* will reset clear_cmdline */
10205
10206 /* Position on the last line in the window, column 0 */
10207 msg_pos_mode();
10208 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010209 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 if (do_mode)
10211 {
10212 MSG_PUTS_ATTR("--", attr);
10213#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010214 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010215# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010216 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010217# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010218 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010219# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010220 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010221# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 MSG_PUTS_ATTR(" IM", attr);
10223# else
10224 MSG_PUTS_ATTR(" XIM", attr);
10225# endif
10226#endif
10227#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10228 if (gui.in_use)
10229 {
10230 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010231 {
10232 /* HANGUL */
10233 if (enc_utf8)
10234 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10235 else
10236 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 }
10239#endif
10240#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010241 /* CTRL-X in Insert mode */
10242 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 {
10244 /* These messages can get long, avoid a wrap in a narrow
10245 * window. Prefer showing edit_submode_extra. */
10246 length = (Rows - msg_row) * Columns - 3;
10247 if (edit_submode_extra != NULL)
10248 length -= vim_strsize(edit_submode_extra);
10249 if (length > 0)
10250 {
10251 if (edit_submode_pre != NULL)
10252 length -= vim_strsize(edit_submode_pre);
10253 if (length - vim_strsize(edit_submode) > 0)
10254 {
10255 if (edit_submode_pre != NULL)
10256 msg_puts_attr(edit_submode_pre, attr);
10257 msg_puts_attr(edit_submode, attr);
10258 }
10259 if (edit_submode_extra != NULL)
10260 {
10261 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10262 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010263 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 else
10265 sub_attr = attr;
10266 msg_puts_attr(edit_submode_extra, sub_attr);
10267 }
10268 }
10269 length = 0;
10270 }
10271 else
10272#endif
10273 {
10274#ifdef FEAT_VREPLACE
10275 if (State & VREPLACE_FLAG)
10276 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10277 else
10278#endif
10279 if (State & REPLACE_FLAG)
10280 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10281 else if (State & INSERT)
10282 {
10283#ifdef FEAT_RIGHTLEFT
10284 if (p_ri)
10285 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10286#endif
10287 MSG_PUTS_ATTR(_(" INSERT"), attr);
10288 }
10289 else if (restart_edit == 'I')
10290 MSG_PUTS_ATTR(_(" (insert)"), attr);
10291 else if (restart_edit == 'R')
10292 MSG_PUTS_ATTR(_(" (replace)"), attr);
10293 else if (restart_edit == 'V')
10294 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10295#ifdef FEAT_RIGHTLEFT
10296 if (p_hkmap)
10297 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10298# ifdef FEAT_FKMAP
10299 if (p_fkmap)
10300 MSG_PUTS_ATTR(farsi_text_5, attr);
10301# endif
10302#endif
10303#ifdef FEAT_KEYMAP
10304 if (State & LANGMAP)
10305 {
10306# ifdef FEAT_ARABIC
10307 if (curwin->w_p_arab)
10308 MSG_PUTS_ATTR(_(" Arabic"), attr);
10309 else
10310# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010311 if (get_keymap_str(curwin, (char_u *)" (%s)",
10312 NameBuff, MAXPATHL))
10313 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315#endif
10316 if ((State & INSERT) && p_paste)
10317 MSG_PUTS_ATTR(_(" (paste)"), attr);
10318
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 if (VIsual_active)
10320 {
10321 char *p;
10322
10323 /* Don't concatenate separate words to avoid translation
10324 * problems. */
10325 switch ((VIsual_select ? 4 : 0)
10326 + (VIsual_mode == Ctrl_V) * 2
10327 + (VIsual_mode == 'V'))
10328 {
10329 case 0: p = N_(" VISUAL"); break;
10330 case 1: p = N_(" VISUAL LINE"); break;
10331 case 2: p = N_(" VISUAL BLOCK"); break;
10332 case 4: p = N_(" SELECT"); break;
10333 case 5: p = N_(" SELECT LINE"); break;
10334 default: p = N_(" SELECT BLOCK"); break;
10335 }
10336 MSG_PUTS_ATTR(_(p), attr);
10337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 MSG_PUTS_ATTR(" --", attr);
10339 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010340
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 need_clear = TRUE;
10342 }
10343 if (Recording
10344#ifdef FEAT_INS_EXPAND
10345 && edit_submode == NULL /* otherwise it gets too long */
10346#endif
10347 )
10348 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010349 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 need_clear = TRUE;
10351 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010352
10353 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 if (need_clear || clear_cmdline)
10355 msg_clr_eos();
10356 msg_didout = FALSE; /* overwrite this message */
10357 length = msg_col;
10358 msg_col = 0;
10359 need_wait_return = nwr_save; /* never ask for hit-return for this */
10360 }
10361 else if (clear_cmdline && msg_silent == 0)
10362 /* Clear the whole command line. Will reset "clear_cmdline". */
10363 msg_clr_cmdline();
10364
10365#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 /* In Visual mode the size of the selected area must be redrawn. */
10367 if (VIsual_active)
10368 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369
10370 /* If the last window has no status line, the ruler is after the mode
10371 * message and must be redrawn */
10372 if (redrawing()
10373# ifdef FEAT_WINDOWS
10374 && lastwin->w_status_height == 0
10375# endif
10376 )
10377 win_redr_ruler(lastwin, TRUE);
10378#endif
10379 redraw_cmdline = FALSE;
10380 clear_cmdline = FALSE;
10381
10382 return length;
10383}
10384
10385/*
10386 * Position for a mode message.
10387 */
10388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010389msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390{
10391 msg_col = 0;
10392 msg_row = Rows - 1;
10393}
10394
10395/*
10396 * Delete mode message. Used when ESC is typed which is expected to end
10397 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010398 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 */
10400 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010401unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402{
10403 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010404 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 */
10406 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10407 redraw_cmdline = TRUE; /* delete mode later */
10408 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010409 clearmode();
10410}
10411
10412/*
10413 * Clear the mode message.
10414 */
10415 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010416clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010417{
10418 msg_pos_mode();
10419 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010420 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010421 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422}
10423
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010424 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010425recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010426{
10427 MSG_PUTS_ATTR(_("recording"), attr);
10428 if (!shortmess(SHM_RECORDING))
10429 {
10430 char_u s[4];
10431 sprintf((char *)s, " @%c", Recording);
10432 MSG_PUTS_ATTR(s, attr);
10433 }
10434}
10435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010436#if defined(FEAT_WINDOWS)
10437/*
10438 * Draw the tab pages line at the top of the Vim window.
10439 */
10440 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010441draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010442{
10443 int tabcount = 0;
10444 tabpage_T *tp;
10445 int tabwidth;
10446 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010447 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010448 int attr;
10449 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010450 win_T *cwp;
10451 int wincount;
10452 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010453 int c;
10454 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010455 int attr_sel = HL_ATTR(HLF_TPS);
10456 int attr_nosel = HL_ATTR(HLF_TP);
10457 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010458 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010459 int room;
10460 int use_sep_chars = (t_colors < 8
10461#ifdef FEAT_GUI
10462 && !gui.in_use
10463#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010464#ifdef FEAT_TERMGUICOLORS
10465 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010466#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010467 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010469 if (ScreenLines == NULL)
10470 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010471 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010472
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010473#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010474 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010475 if (gui_use_tabline())
10476 {
10477 gui_update_tabline();
10478 return;
10479 }
10480#endif
10481
10482 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010483 return;
10484
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010485#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010486
10487 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10488 for (scol = 0; scol < Columns; ++scol)
10489 TabPageIdxs[scol] = 0;
10490
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010491 /* Use the 'tabline' option if it's set. */
10492 if (*p_tal != NUL)
10493 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010494 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010495
10496 /* Check for an error. If there is one we would loop in redrawing the
10497 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010498 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010499 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010500 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010501 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010502 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010503 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010504 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010505 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010506#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010507 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010508 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010509 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010510
Bram Moolenaar238a5642006-02-21 22:12:05 +000010511 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10512 if (tabwidth < 6)
10513 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010514
Bram Moolenaar238a5642006-02-21 22:12:05 +000010515 attr = attr_nosel;
10516 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010517 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010518 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10519 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010520 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010521 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010522
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523 if (tp->tp_topframe == topframe)
10524 attr = attr_sel;
10525 if (use_sep_chars && col > 0)
10526 screen_putchar('|', 0, col++, attr);
10527
10528 if (tp->tp_topframe != topframe)
10529 attr = attr_nosel;
10530
10531 screen_putchar(' ', 0, col++, attr);
10532
10533 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010534 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535 cwp = curwin;
10536 wp = firstwin;
10537 }
10538 else
10539 {
10540 cwp = tp->tp_curwin;
10541 wp = tp->tp_firstwin;
10542 }
10543
10544 modified = FALSE;
10545 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10546 if (bufIsChanged(wp->w_buffer))
10547 modified = TRUE;
10548 if (modified || wincount > 1)
10549 {
10550 if (wincount > 1)
10551 {
10552 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010553 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010554 if (col + len >= Columns - 3)
10555 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010556 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010557#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010558 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010559#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010560 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010561#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010562 );
10563 col += len;
10564 }
10565 if (modified)
10566 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10567 screen_putchar(' ', 0, col++, attr);
10568 }
10569
10570 room = scol - col + tabwidth - 1;
10571 if (room > 0)
10572 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010573 /* Get buffer name in NameBuff[] */
10574 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010575 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 len = vim_strsize(NameBuff);
10577 p = NameBuff;
10578#ifdef FEAT_MBYTE
10579 if (has_mbyte)
10580 while (len > room)
10581 {
10582 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010583 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010584 }
10585 else
10586#endif
10587 if (len > room)
10588 {
10589 p += len - room;
10590 len = room;
10591 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010592 if (len > Columns - col - 1)
10593 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010595 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010596 col += len;
10597 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010598 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010599
10600 /* Store the tab page number in TabPageIdxs[], so that
10601 * jump_to_mouse() knows where each one is. */
10602 ++tabcount;
10603 while (scol < col)
10604 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010605 }
10606
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607 if (use_sep_chars)
10608 c = '_';
10609 else
10610 c = ' ';
10611 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010612
10613 /* Put an "X" for closing the current tab if there are several. */
10614 if (first_tabpage->tp_next != NULL)
10615 {
10616 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10617 TabPageIdxs[Columns - 1] = -999;
10618 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010619 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010620
10621 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10622 * set. */
10623 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010624}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010625
10626/*
10627 * Get buffer name for "buf" into NameBuff[].
10628 * Takes care of special buffer names and translates special characters.
10629 */
10630 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010631get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010632{
10633 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010634 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010635 else
10636 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10637 trans_characters(NameBuff, MAXPATHL);
10638}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010639#endif
10640
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10642/*
10643 * Get the character to use in a status line. Get its attributes in "*attr".
10644 */
10645 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010646fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
10648 int fill;
10649 if (is_curwin)
10650 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010651 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 fill = fill_stl;
10653 }
10654 else
10655 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010656 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 fill = fill_stlnc;
10658 }
10659 /* Use fill when there is highlighting, and highlighting of current
10660 * window differs, or the fillchars differ, or this is not the
10661 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010662 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010663 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 || (fill_stl != fill_stlnc)))
10665 return fill;
10666 if (is_curwin)
10667 return '^';
10668 return '=';
10669}
10670#endif
10671
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010672#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673/*
10674 * Get the character to use in a separator between vertically split windows.
10675 * Get its attributes in "*attr".
10676 */
10677 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010678fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010680 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 if (*attr == 0 && fill_vert == ' ')
10682 return '|';
10683 else
10684 return fill_vert;
10685}
10686#endif
10687
10688/*
10689 * Return TRUE if redrawing should currently be done.
10690 */
10691 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010692redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010694#ifdef FEAT_EVAL
10695 if (disable_redraw_for_testing)
10696 return 0;
10697 else
10698#endif
10699 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10701}
10702
10703/*
10704 * Return TRUE if printing messages should currently be done.
10705 */
10706 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010707messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708{
10709 return (!(p_lz && char_avail() && !KeyTyped));
10710}
10711
10712/*
10713 * Show current status info in ruler and various other places
10714 * If always is FALSE, only show ruler if position has changed.
10715 */
10716 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010717showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718{
10719 if (!always && !redrawing())
10720 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010721#ifdef FEAT_INS_EXPAND
10722 if (pum_visible())
10723 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010724# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010725 /* Don't redraw right now, do it later. */
10726 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010727# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010728 return;
10729 }
10730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010732 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010734 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 else
10737#endif
10738#ifdef FEAT_CMDL_INFO
10739 win_redr_ruler(curwin, always);
10740#endif
10741
10742#ifdef FEAT_TITLE
10743 if (need_maketitle
10744# ifdef FEAT_STL_OPT
10745 || (p_icon && (stl_syntax & STL_IN_ICON))
10746 || (p_title && (stl_syntax & STL_IN_TITLE))
10747# endif
10748 )
10749 maketitle();
10750#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010751#ifdef FEAT_WINDOWS
10752 /* Redraw the tab pages line if needed. */
10753 if (redraw_tabline)
10754 draw_tabline();
10755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756}
10757
10758#ifdef FEAT_CMDL_INFO
10759 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010760win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010762#define RULER_BUF_LEN 70
10763 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 int row;
10765 int fillchar;
10766 int attr;
10767 int empty_line = FALSE;
10768 colnr_T virtcol;
10769 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010770 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010772#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 int this_ru_col;
10774 int off = 0;
10775 int width = Columns;
10776# define WITH_OFF(x) x
10777# define WITH_WIDTH(x) x
10778#else
10779# define WITH_OFF(x) 0
10780# define WITH_WIDTH(x) Columns
10781# define this_ru_col ru_col
10782#endif
10783
10784 /* If 'ruler' off or redrawing disabled, don't do anything */
10785 if (!p_ru)
10786 return;
10787
10788 /*
10789 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10790 * after deleting lines, before cursor.lnum is corrected.
10791 */
10792 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10793 return;
10794
10795#ifdef FEAT_INS_EXPAND
10796 /* Don't draw the ruler while doing insert-completion, it might overwrite
10797 * the (long) mode message. */
10798# ifdef FEAT_WINDOWS
10799 if (wp == lastwin && lastwin->w_status_height == 0)
10800# endif
10801 if (edit_submode != NULL)
10802 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010803 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10804 if (pum_visible())
10805 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806#endif
10807
10808#ifdef FEAT_STL_OPT
10809 if (*p_ruf)
10810 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 int save_called_emsg = called_emsg;
10812
10813 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010815 if (called_emsg)
10816 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010817 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010818 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 return;
10820 }
10821#endif
10822
10823 /*
10824 * Check if not in Insert mode and the line is empty (will show "0-1").
10825 */
10826 if (!(State & INSERT)
10827 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10828 empty_line = TRUE;
10829
10830 /*
10831 * Only draw the ruler when something changed.
10832 */
10833 validate_virtcol_win(wp);
10834 if ( redraw_cmdline
10835 || always
10836 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10837 || wp->w_cursor.col != wp->w_ru_cursor.col
10838 || wp->w_virtcol != wp->w_ru_virtcol
10839#ifdef FEAT_VIRTUALEDIT
10840 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10841#endif
10842 || wp->w_topline != wp->w_ru_topline
10843 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10844#ifdef FEAT_DIFF
10845 || wp->w_topfill != wp->w_ru_topfill
10846#endif
10847 || empty_line != wp->w_ru_empty)
10848 {
10849 cursor_off();
10850#ifdef FEAT_WINDOWS
10851 if (wp->w_status_height)
10852 {
10853 row = W_WINROW(wp) + wp->w_height;
10854 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 off = W_WINCOL(wp);
10856 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 }
10858 else
10859#endif
10860 {
10861 row = Rows - 1;
10862 fillchar = ' ';
10863 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010864#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 width = Columns;
10866 off = 0;
10867#endif
10868 }
10869
10870 /* In list mode virtcol needs to be recomputed */
10871 virtcol = wp->w_virtcol;
10872 if (wp->w_p_list && lcs_tab1 == NUL)
10873 {
10874 wp->w_p_list = FALSE;
10875 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10876 wp->w_p_list = TRUE;
10877 }
10878
10879 /*
10880 * Some sprintfs return the length, some return a pointer.
10881 * To avoid portability problems we use strlen() here.
10882 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010883 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10885 ? 0L
10886 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010887 len = STRLEN(buffer);
10888 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10890 (int)virtcol + 1);
10891
10892 /*
10893 * Add a "50%" if there is room for it.
10894 * On the last line, don't print in the last column (scrolls the
10895 * screen up on some terminals).
10896 */
10897 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010898 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 o = i + vim_strsize(buffer + i + 1);
10900#ifdef FEAT_WINDOWS
10901 if (wp->w_status_height == 0) /* can't use last char of screen */
10902#endif
10903 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010904#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 this_ru_col = ru_col - (Columns - width);
10906 if (this_ru_col < 0)
10907 this_ru_col = 0;
10908#endif
10909 /* Never use more than half the window/screen width, leave the other
10910 * half for the filename. */
10911 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10912 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10913 if (this_ru_col + o < WITH_WIDTH(width))
10914 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010915 /* need at least 3 chars left for get_rel_pos() + NUL */
10916 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 {
10918#ifdef FEAT_MBYTE
10919 if (has_mbyte)
10920 i += (*mb_char2bytes)(fillchar, buffer + i);
10921 else
10922#endif
10923 buffer[i++] = fillchar;
10924 ++o;
10925 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010926 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 }
10928 /* Truncate at window boundary. */
10929#ifdef FEAT_MBYTE
10930 if (has_mbyte)
10931 {
10932 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010933 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 {
10935 o += (*mb_ptr2cells)(buffer + i);
10936 if (this_ru_col + o > WITH_WIDTH(width))
10937 {
10938 buffer[i] = NUL;
10939 break;
10940 }
10941 }
10942 }
10943 else
10944#endif
10945 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10946 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10947
10948 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10949 i = redraw_cmdline;
10950 screen_fill(row, row + 1,
10951 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10952 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10953 fillchar, fillchar, attr);
10954 /* don't redraw the cmdline because of showing the ruler */
10955 redraw_cmdline = i;
10956 wp->w_ru_cursor = wp->w_cursor;
10957 wp->w_ru_virtcol = wp->w_virtcol;
10958 wp->w_ru_empty = empty_line;
10959 wp->w_ru_topline = wp->w_topline;
10960 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10961#ifdef FEAT_DIFF
10962 wp->w_ru_topfill = wp->w_topfill;
10963#endif
10964 }
10965}
10966#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010967
10968#if defined(FEAT_LINEBREAK) || defined(PROTO)
10969/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010970 * Return the width of the 'number' and 'relativenumber' column.
10971 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010972 * Otherwise it depends on 'numberwidth' and the line count.
10973 */
10974 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010975number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010976{
10977 int n;
10978 linenr_T lnum;
10979
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010980 if (wp->w_p_rnu && !wp->w_p_nu)
10981 /* cursor line shows "0" */
10982 lnum = wp->w_height;
10983 else
10984 /* cursor line shows absolute line number */
10985 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010986
Bram Moolenaar6b314672015-03-20 15:42:10 +010010987 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010988 return wp->w_nrwidth_width;
10989 wp->w_nrwidth_line_count = lnum;
10990
10991 n = 0;
10992 do
10993 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010994 lnum /= 10;
10995 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010996 } while (lnum > 0);
10997
10998 /* 'numberwidth' gives the minimal width plus one */
10999 if (n < wp->w_p_nuw - 1)
11000 n = wp->w_p_nuw - 1;
11001
11002 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011003 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011004 return n;
11005}
11006#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011007
11008/*
11009 * Return the current cursor column. This is the actual position on the
11010 * screen. First column is 0.
11011 */
11012 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011013screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011014{
11015 return screen_cur_col;
11016}
11017
11018/*
11019 * Return the current cursor row. This is the actual position on the screen.
11020 * First row is 0.
11021 */
11022 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011023screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011024{
11025 return screen_cur_row;
11026}