blob: 617051c95ec73f610b6768161d44a9fb21914e56 [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;
5505 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005506 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005507 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005508 && lnum != wp->w_cursor.lnum)
5509 {
5510 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005511 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005512 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005513 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005514 {
5515 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005516 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005518 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519#endif
5520
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 /*
5522 * Store character to be displayed.
5523 * Skip characters that are left of the screen for 'nowrap'.
5524 */
5525 vcol_prev = vcol;
5526 if (draw_state < WL_LINE || n_skip <= 0)
5527 {
5528 /*
5529 * Store the character.
5530 */
5531#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5532 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5533 {
5534 /* A double-wide character is: put first halve in left cell. */
5535 --off;
5536 --col;
5537 }
5538#endif
5539 ScreenLines[off] = c;
5540#ifdef FEAT_MBYTE
5541 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005542 {
5543 if ((mb_c & 0xff00) == 0x8e00)
5544 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 else if (enc_utf8)
5548 {
5549 if (mb_utf8)
5550 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005551 int i;
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005554 if ((c & 0xff) == 0)
5555 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005556 for (i = 0; i < Screen_mco; ++i)
5557 {
5558 ScreenLinesC[i][off] = u8cc[i];
5559 if (u8cc[i] == 0)
5560 break;
5561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else
5564 ScreenLinesUC[off] = 0;
5565 }
5566 if (multi_attr)
5567 {
5568 ScreenAttrs[off] = multi_attr;
5569 multi_attr = 0;
5570 }
5571 else
5572#endif
5573 ScreenAttrs[off] = char_attr;
5574
5575#ifdef FEAT_MBYTE
5576 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5577 {
5578 /* Need to fill two screen columns. */
5579 ++off;
5580 ++col;
5581 if (enc_utf8)
5582 /* UTF-8: Put a 0 in the second screen char. */
5583 ScreenLines[off] = 0;
5584 else
5585 /* DBCS: Put second byte in the second screen char. */
5586 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005587 if (draw_state > WL_NR
5588#ifdef FEAT_DIFF
5589 && filler_todo <= 0
5590#endif
5591 )
5592 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 /* When "tocol" is halfway a character, set it to the end of
5594 * the character, otherwise highlighting won't stop. */
5595 if (tocol == vcol)
5596 ++tocol;
5597#ifdef FEAT_RIGHTLEFT
5598 if (wp->w_p_rl)
5599 {
5600 /* now it's time to backup one cell */
5601 --off;
5602 --col;
5603 }
5604#endif
5605 }
5606#endif
5607#ifdef FEAT_RIGHTLEFT
5608 if (wp->w_p_rl)
5609 {
5610 --off;
5611 --col;
5612 }
5613 else
5614#endif
5615 {
5616 ++off;
5617 ++col;
5618 }
5619 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005620#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005621 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005622 {
5623 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005624 ++vcol_off;
5625 if (n_extra > 0)
5626 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005627 if (wp->w_p_wrap)
5628 {
5629 /*
5630 * Special voodoo required if 'wrap' is on.
5631 *
5632 * Advance the column indicator to force the line
5633 * drawing to wrap early. This will make the line
5634 * take up the same screen space when parts are concealed,
5635 * so that cursor line computations aren't messed up.
5636 *
5637 * To avoid the fictitious advance of 'col' causing
5638 * trailing junk to be written out of the screen line
5639 * we are building, 'boguscols' keeps track of the number
5640 * of bad columns we have advanced.
5641 */
5642 if (n_extra > 0)
5643 {
5644 vcol += n_extra;
5645# ifdef FEAT_RIGHTLEFT
5646 if (wp->w_p_rl)
5647 {
5648 col -= n_extra;
5649 boguscols -= n_extra;
5650 }
5651 else
5652# endif
5653 {
5654 col += n_extra;
5655 boguscols += n_extra;
5656 }
5657 n_extra = 0;
5658 n_attr = 0;
5659 }
5660
5661
5662# ifdef FEAT_MBYTE
5663 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5664 {
5665 /* Need to fill two screen columns. */
5666# ifdef FEAT_RIGHTLEFT
5667 if (wp->w_p_rl)
5668 {
5669 --boguscols;
5670 --col;
5671 }
5672 else
5673# endif
5674 {
5675 ++boguscols;
5676 ++col;
5677 }
5678 }
5679# endif
5680
5681# ifdef FEAT_RIGHTLEFT
5682 if (wp->w_p_rl)
5683 {
5684 --boguscols;
5685 --col;
5686 }
5687 else
5688# endif
5689 {
5690 ++boguscols;
5691 ++col;
5692 }
5693 }
5694 else
5695 {
5696 if (n_extra > 0)
5697 {
5698 vcol += n_extra;
5699 n_extra = 0;
5700 n_attr = 0;
5701 }
5702 }
5703
5704 }
5705#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 else
5707 --n_skip;
5708
Bram Moolenaar64486672010-05-16 15:46:46 +02005709 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5710 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005711 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712#ifdef FEAT_DIFF
5713 && filler_todo <= 0
5714#endif
5715 )
5716 ++vcol;
5717
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005718#ifdef FEAT_SYN_HL
5719 if (vcol_save_attr >= 0)
5720 char_attr = vcol_save_attr;
5721#endif
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 /* restore attributes after "predeces" in 'listchars' */
5724 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5725 char_attr = saved_attr3;
5726
5727 /* restore attributes after last 'listchars' or 'number' char */
5728 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5729 char_attr = saved_attr2;
5730
5731 /*
5732 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005733 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
5735 if ((
5736#ifdef FEAT_RIGHTLEFT
5737 wp->w_p_rl ? (col < 0) :
5738#endif
5739 (col >= W_WIDTH(wp)))
5740 && (*ptr != NUL
5741#ifdef FEAT_DIFF
5742 || filler_todo > 0
5743#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005744 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5746 )
5747 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005748#ifdef FEAT_CONCEAL
5749 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5750 (int)W_WIDTH(wp), wp->w_p_rl);
5751 boguscols = 0;
5752#else
5753 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5754 (int)W_WIDTH(wp), wp->w_p_rl);
5755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 ++row;
5757 ++screen_row;
5758
5759 /* When not wrapping and finished diff lines, or when displayed
5760 * '$' and highlighting until last column, break here. */
5761 if ((!wp->w_p_wrap
5762#ifdef FEAT_DIFF
5763 && filler_todo <= 0
5764#endif
5765 ) || lcs_eol_one == -1)
5766 break;
5767
5768 /* When the window is too narrow draw all "@" lines. */
5769 if (draw_state != WL_LINE
5770#ifdef FEAT_DIFF
5771 && filler_todo <= 0
5772#endif
5773 )
5774 {
5775 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005776#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 draw_vsep_win(wp, row);
5778#endif
5779 row = endrow;
5780 }
5781
5782 /* When line got too long for screen break here. */
5783 if (row == endrow)
5784 {
5785 ++row;
5786 break;
5787 }
5788
5789 if (screen_cur_row == screen_row - 1
5790#ifdef FEAT_DIFF
5791 && filler_todo <= 0
5792#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005793#ifdef FEAT_WINDOWS
5794 && W_WIDTH(wp) == Columns
5795#endif
5796 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
5798 /* Remember that the line wraps, used for modeless copy. */
5799 LineWraps[screen_row - 1] = TRUE;
5800
5801 /*
5802 * Special trick to make copy/paste of wrapped lines work with
5803 * xterm/screen: write an extra character beyond the end of
5804 * the line. This will work with all terminal types
5805 * (regardless of the xn,am settings).
5806 * Only do this on a fast tty.
5807 * Only do this if the cursor is on the current line
5808 * (something has been written in it).
5809 * Don't do this for the GUI.
5810 * Don't do this for double-width characters.
5811 * Don't do this for a window not at the right screen border.
5812 */
5813 if (p_tf
5814#ifdef FEAT_GUI
5815 && !gui.in_use
5816#endif
5817#ifdef FEAT_MBYTE
5818 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005819 && ((*mb_off2cells)(LineOffset[screen_row],
5820 LineOffset[screen_row] + screen_Columns)
5821 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005823 + (int)Columns - 2,
5824 LineOffset[screen_row] + screen_Columns)
5825 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827 )
5828 {
5829 /* First make sure we are at the end of the screen line,
5830 * then output the same character again to let the
5831 * terminal know about the wrap. If the terminal doesn't
5832 * auto-wrap, we overwrite the character. */
5833 if (screen_cur_col != W_WIDTH(wp))
5834 screen_char(LineOffset[screen_row - 1]
5835 + (unsigned)Columns - 1,
5836 screen_row - 1, (int)(Columns - 1));
5837
5838#ifdef FEAT_MBYTE
5839 /* When there is a multi-byte character, just output a
5840 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005841 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5842 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 out_char(' ');
5844 else
5845#endif
5846 out_char(ScreenLines[LineOffset[screen_row - 1]
5847 + (Columns - 1)]);
5848 /* force a redraw of the first char on the next line */
5849 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5850 screen_start(); /* don't know where cursor is now */
5851 }
5852 }
5853
5854 col = 0;
5855 off = (unsigned)(current_ScreenLine - ScreenLines);
5856#ifdef FEAT_RIGHTLEFT
5857 if (wp->w_p_rl)
5858 {
5859 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5860 off += col;
5861 }
5862#endif
5863
5864 /* reset the drawing state for the start of a wrapped line */
5865 draw_state = WL_START;
5866 saved_n_extra = n_extra;
5867 saved_p_extra = p_extra;
5868 saved_c_extra = c_extra;
5869 saved_char_attr = char_attr;
5870 n_extra = 0;
5871 lcs_prec_todo = lcs_prec;
5872#ifdef FEAT_LINEBREAK
5873# ifdef FEAT_DIFF
5874 if (filler_todo <= 0)
5875# endif
5876 need_showbreak = TRUE;
5877#endif
5878#ifdef FEAT_DIFF
5879 --filler_todo;
5880 /* When the filler lines are actually below the last line of the
5881 * file, don't draw the line itself, break here. */
5882 if (filler_todo == 0 && wp->w_botfill)
5883 break;
5884#endif
5885 }
5886
5887 } /* for every character in the line */
5888
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005889#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005890 /* After an empty line check first word for capital. */
5891 if (*skipwhite(line) == NUL)
5892 {
5893 capcol_lnum = lnum + 1;
5894 cap_col = 0;
5895 }
5896#endif
5897
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005898 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 return row;
5900}
5901
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005902#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005903static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005904
5905/*
5906 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005907 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005908 */
5909 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005910comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005911{
5912 int i;
5913
5914 for (i = 0; i < Screen_mco; ++i)
5915 {
5916 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5917 return TRUE;
5918 if (ScreenLinesC[i][off_from] == 0)
5919 break;
5920 }
5921 return FALSE;
5922}
5923#endif
5924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925/*
5926 * Check whether the given character needs redrawing:
5927 * - the (first byte of the) character is different
5928 * - the attributes are different
5929 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005930 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 */
5932 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005933char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 if (cols > 0
5936 && ((ScreenLines[off_from] != ScreenLines[off_to]
5937 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5938
5939#ifdef FEAT_MBYTE
5940 || (enc_dbcs != 0
5941 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5942 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5943 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5944 : (cols > 1 && ScreenLines[off_from + 1]
5945 != ScreenLines[off_to + 1])))
5946 || (enc_utf8
5947 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5948 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005949 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005950 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5951 && ScreenLines[off_from + 1]
5952 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#endif
5954 ))
5955 return TRUE;
5956 return FALSE;
5957}
5958
5959/*
5960 * Move one "cooked" screen line to the screen, but only the characters that
5961 * have actually changed. Handle insert/delete character.
5962 * "coloff" gives the first column on the screen for this line.
5963 * "endcol" gives the columns where valid characters are.
5964 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5965 * needs to be cleared, negative otherwise.
5966 * "rlflag" is TRUE in a rightleft window:
5967 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5968 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5969 */
5970 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005971screen_line(
5972 int row,
5973 int coloff,
5974 int endcol,
5975 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005977 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005979 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 unsigned off_from;
5982 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005983#ifdef FEAT_MBYTE
5984 unsigned max_off_from;
5985 unsigned max_off_to;
5986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005988#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 int hl;
5990#endif
5991 int force = FALSE; /* force update rest of the line */
5992 int redraw_this /* bool: does character need redraw? */
5993#ifdef FEAT_GUI
5994 = TRUE /* For GUI when while-loop empty */
5995#endif
5996 ;
5997 int redraw_next; /* redraw_this for next character */
5998#ifdef FEAT_MBYTE
5999 int clear_next = FALSE;
6000 int char_cells; /* 1: normal char */
6001 /* 2: occupies two display cells */
6002# define CHAR_CELLS char_cells
6003#else
6004# define CHAR_CELLS 1
6005#endif
6006
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006007 /* Check for illegal row and col, just in case. */
6008 if (row >= Rows)
6009 row = Rows - 1;
6010 if (endcol > Columns)
6011 endcol = Columns;
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013# ifdef FEAT_CLIPBOARD
6014 clip_may_clear_selection(row, row);
6015# endif
6016
6017 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6018 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006019#ifdef FEAT_MBYTE
6020 max_off_from = off_from + screen_Columns;
6021 max_off_to = LineOffset[row] + screen_Columns;
6022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023
6024#ifdef FEAT_RIGHTLEFT
6025 if (rlflag)
6026 {
6027 /* Clear rest first, because it's left of the text. */
6028 if (clear_width > 0)
6029 {
6030 while (col <= endcol && ScreenLines[off_to] == ' '
6031 && ScreenAttrs[off_to] == 0
6032# ifdef FEAT_MBYTE
6033 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6034# endif
6035 )
6036 {
6037 ++off_to;
6038 ++col;
6039 }
6040 if (col <= endcol)
6041 screen_fill(row, row + 1, col + coloff,
6042 endcol + coloff + 1, ' ', ' ', 0);
6043 }
6044 col = endcol + 1;
6045 off_to = LineOffset[row] + col + coloff;
6046 off_from += col;
6047 endcol = (clear_width > 0 ? clear_width : -clear_width);
6048 }
6049#endif /* FEAT_RIGHTLEFT */
6050
6051 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6052
6053 while (col < endcol)
6054 {
6055#ifdef FEAT_MBYTE
6056 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006057 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 else
6059 char_cells = 1;
6060#endif
6061
6062 redraw_this = redraw_next;
6063 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6064 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6065
6066#ifdef FEAT_GUI
6067 /* If the next character was bold, then redraw the current character to
6068 * remove any pixels that might have spilt over into us. This only
6069 * happens in the GUI.
6070 */
6071 if (redraw_next && gui.in_use)
6072 {
6073 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006074 if (hl > HL_ALL)
6075 hl = syn_attr2attr(hl);
6076 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 redraw_this = TRUE;
6078 }
6079#endif
6080
6081 if (redraw_this)
6082 {
6083 /*
6084 * Special handling when 'xs' termcap flag set (hpterm):
6085 * Attributes for characters are stored at the position where the
6086 * cursor is when writing the highlighting code. The
6087 * start-highlighting code must be written with the cursor on the
6088 * first highlighted character. The stop-highlighting code must
6089 * be written with the cursor just after the last highlighted
6090 * character.
6091 * Overwriting a character doesn't remove it's highlighting. Need
6092 * to clear the rest of the line, and force redrawing it
6093 * completely.
6094 */
6095 if ( p_wiv
6096 && !force
6097#ifdef FEAT_GUI
6098 && !gui.in_use
6099#endif
6100 && ScreenAttrs[off_to] != 0
6101 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6102 {
6103 /*
6104 * Need to remove highlighting attributes here.
6105 */
6106 windgoto(row, col + coloff);
6107 out_str(T_CE); /* clear rest of this screen line */
6108 screen_start(); /* don't know where cursor is now */
6109 force = TRUE; /* force redraw of rest of the line */
6110 redraw_next = TRUE; /* or else next char would miss out */
6111
6112 /*
6113 * If the previous character was highlighted, need to stop
6114 * highlighting at this character.
6115 */
6116 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6117 {
6118 screen_attr = ScreenAttrs[off_to - 1];
6119 term_windgoto(row, col + coloff);
6120 screen_stop_highlight();
6121 }
6122 else
6123 screen_attr = 0; /* highlighting has stopped */
6124 }
6125#ifdef FEAT_MBYTE
6126 if (enc_dbcs != 0)
6127 {
6128 /* Check if overwriting a double-byte with a single-byte or
6129 * the other way around requires another character to be
6130 * redrawn. For UTF-8 this isn't needed, because comparing
6131 * ScreenLinesUC[] is sufficient. */
6132 if (char_cells == 1
6133 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006134 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
6136 /* Writing a single-cell character over a double-cell
6137 * character: need to redraw the next cell. */
6138 ScreenLines[off_to + 1] = 0;
6139 redraw_next = TRUE;
6140 }
6141 else if (char_cells == 2
6142 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006143 && (*mb_off2cells)(off_to, max_off_to) == 1
6144 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 /* Writing the second half of a double-cell character over
6147 * a double-cell character: need to redraw the second
6148 * cell. */
6149 ScreenLines[off_to + 2] = 0;
6150 redraw_next = TRUE;
6151 }
6152
6153 if (enc_dbcs == DBCS_JPNU)
6154 ScreenLines2[off_to] = ScreenLines2[off_from];
6155 }
6156 /* When writing a single-width character over a double-width
6157 * character and at the end of the redrawn text, need to clear out
6158 * the right halve of the old character.
6159 * Also required when writing the right halve of a double-width
6160 * char over the left halve of an existing one. */
6161 if (has_mbyte && col + char_cells == endcol
6162 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006163 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006165 && (*mb_off2cells)(off_to, max_off_to) == 1
6166 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 clear_next = TRUE;
6168#endif
6169
6170 ScreenLines[off_to] = ScreenLines[off_from];
6171#ifdef FEAT_MBYTE
6172 if (enc_utf8)
6173 {
6174 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6175 if (ScreenLinesUC[off_from] != 0)
6176 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006177 int i;
6178
6179 for (i = 0; i < Screen_mco; ++i)
6180 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182 }
6183 if (char_cells == 2)
6184 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6185#endif
6186
6187#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006188 /* The bold trick makes a single column of pixels appear in the
6189 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 * character should be redrawn too. This happens for our own GUI
6191 * and for some xterms. */
6192 if (
6193# ifdef FEAT_GUI
6194 gui.in_use
6195# endif
6196# if defined(FEAT_GUI) && defined(UNIX)
6197 ||
6198# endif
6199# ifdef UNIX
6200 term_is_xterm
6201# endif
6202 )
6203 {
6204 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006205 if (hl > HL_ALL)
6206 hl = syn_attr2attr(hl);
6207 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 redraw_next = TRUE;
6209 }
6210#endif
6211 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6212#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006213 /* For simplicity set the attributes of second half of a
6214 * double-wide character equal to the first half. */
6215 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006217
6218 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 else
6221#endif
6222 screen_char(off_to, row, col + coloff);
6223 }
6224 else if ( p_wiv
6225#ifdef FEAT_GUI
6226 && !gui.in_use
6227#endif
6228 && col + coloff > 0)
6229 {
6230 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6231 {
6232 /*
6233 * Don't output stop-highlight when moving the cursor, it will
6234 * stop the highlighting when it should continue.
6235 */
6236 screen_attr = 0;
6237 }
6238 else if (screen_attr != 0)
6239 screen_stop_highlight();
6240 }
6241
6242 off_to += CHAR_CELLS;
6243 off_from += CHAR_CELLS;
6244 col += CHAR_CELLS;
6245 }
6246
6247#ifdef FEAT_MBYTE
6248 if (clear_next)
6249 {
6250 /* Clear the second half of a double-wide character of which the left
6251 * half was overwritten with a single-wide character. */
6252 ScreenLines[off_to] = ' ';
6253 if (enc_utf8)
6254 ScreenLinesUC[off_to] = 0;
6255 screen_char(off_to, row, col + coloff);
6256 }
6257#endif
6258
6259 if (clear_width > 0
6260#ifdef FEAT_RIGHTLEFT
6261 && !rlflag
6262#endif
6263 )
6264 {
6265#ifdef FEAT_GUI
6266 int startCol = col;
6267#endif
6268
6269 /* blank out the rest of the line */
6270 while (col < clear_width && ScreenLines[off_to] == ' '
6271 && ScreenAttrs[off_to] == 0
6272#ifdef FEAT_MBYTE
6273 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6274#endif
6275 )
6276 {
6277 ++off_to;
6278 ++col;
6279 }
6280 if (col < clear_width)
6281 {
6282#ifdef FEAT_GUI
6283 /*
6284 * In the GUI, clearing the rest of the line may leave pixels
6285 * behind if the first character cleared was bold. Some bold
6286 * fonts spill over the left. In this case we redraw the previous
6287 * character too. If we didn't skip any blanks above, then we
6288 * only redraw if the character wasn't already redrawn anyway.
6289 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006290 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
6292 hl = ScreenAttrs[off_to];
6293 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006294 {
6295 int prev_cells = 1;
6296# ifdef FEAT_MBYTE
6297 if (enc_utf8)
6298 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6299 * that its width is 2. */
6300 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6301 else if (enc_dbcs != 0)
6302 {
6303 /* find previous character by counting from first
6304 * column and get its width. */
6305 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006306 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006307
6308 while (off < off_to)
6309 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006310 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006311 off += prev_cells;
6312 }
6313 }
6314
6315 if (enc_dbcs != 0 && prev_cells > 1)
6316 screen_char_2(off_to - prev_cells, row,
6317 col + coloff - prev_cells);
6318 else
6319# endif
6320 screen_char(off_to - prev_cells, row,
6321 col + coloff - prev_cells);
6322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 }
6324#endif
6325 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6326 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006327#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 off_to += clear_width - col;
6329 col = clear_width;
6330#endif
6331 }
6332 }
6333
6334 if (clear_width > 0)
6335 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006336#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 /* For a window that's left of another, draw the separator char. */
6338 if (col + coloff < Columns)
6339 {
6340 int c;
6341
6342 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006343 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006345 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6346 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347# endif
6348 || ScreenAttrs[off_to] != hl)
6349 {
6350 ScreenLines[off_to] = c;
6351 ScreenAttrs[off_to] = hl;
6352# ifdef FEAT_MBYTE
6353 if (enc_utf8)
6354 {
6355 if (c >= 0x80)
6356 {
6357 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006358 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360 else
6361 ScreenLinesUC[off_to] = 0;
6362 }
6363# endif
6364 screen_char(off_to, row, col + coloff);
6365 }
6366 }
6367 else
6368#endif
6369 LineWraps[row] = FALSE;
6370 }
6371}
6372
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006373#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006375 * Mirror text "str" for right-left displaying.
6376 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006378 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006379rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381 char_u *p1, *p2;
6382 int t;
6383
6384 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6385 {
6386 t = *p1;
6387 *p1 = *p2;
6388 *p2 = t;
6389 }
6390}
6391#endif
6392
6393#if defined(FEAT_WINDOWS) || defined(PROTO)
6394/*
6395 * mark all status lines for redraw; used after first :cd
6396 */
6397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006398status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400 win_T *wp;
6401
Bram Moolenaar29323592016-07-24 22:04:11 +02006402 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (wp->w_status_height)
6404 {
6405 wp->w_redr_status = TRUE;
6406 redraw_later(VALID);
6407 }
6408}
6409
6410/*
6411 * mark all status lines of the current buffer for redraw
6412 */
6413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006414status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 win_T *wp;
6417
Bram Moolenaar29323592016-07-24 22:04:11 +02006418 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6420 {
6421 wp->w_redr_status = TRUE;
6422 redraw_later(VALID);
6423 }
6424}
6425
6426/*
6427 * Redraw all status lines that need to be redrawn.
6428 */
6429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006430redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 win_T *wp;
6433
Bram Moolenaar29323592016-07-24 22:04:11 +02006434 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (wp->w_redr_status)
6436 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006437 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006438 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439}
6440#endif
6441
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006442#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443/*
6444 * Redraw all status lines at the bottom of frame "frp".
6445 */
6446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006447win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 if (frp->fr_layout == FR_LEAF)
6450 frp->fr_win->w_redr_status = TRUE;
6451 else if (frp->fr_layout == FR_ROW)
6452 {
6453 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6454 win_redraw_last_status(frp);
6455 }
6456 else /* frp->fr_layout == FR_COL */
6457 {
6458 frp = frp->fr_child;
6459 while (frp->fr_next != NULL)
6460 frp = frp->fr_next;
6461 win_redraw_last_status(frp);
6462 }
6463}
6464#endif
6465
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006466#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467/*
6468 * Draw the verticap separator right of window "wp" starting with line "row".
6469 */
6470 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006471draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 int hl;
6474 int c;
6475
6476 if (wp->w_vsep_width)
6477 {
6478 /* draw the vertical separator right of this window */
6479 c = fillchar_vsep(&hl);
6480 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6481 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6482 c, ' ', hl);
6483 }
6484}
6485#endif
6486
6487#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006488static int status_match_len(expand_T *xp, char_u *s);
6489static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006492 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 */
6494 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006495status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 int len = 0;
6498
6499#ifdef FEAT_MENU
6500 int emenu = (xp->xp_context == EXPAND_MENUS
6501 || xp->xp_context == EXPAND_MENUNAMES);
6502
6503 /* Check for menu separators - replace with '|'. */
6504 if (emenu && menu_is_separator(s))
6505 return 1;
6506#endif
6507
6508 while (*s != NUL)
6509 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006510 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006511 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006512 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514
6515 return len;
6516}
6517
6518/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006519 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006520 * These are backslashes used for escaping. Do show backslashes in help tags.
6521 */
6522 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006523skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006524{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006525 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006526#ifdef FEAT_MENU
6527 || ((xp->xp_context == EXPAND_MENUS
6528 || xp->xp_context == EXPAND_MENUNAMES)
6529 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6530#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006531 )
6532 {
6533#ifndef BACKSLASH_IN_FILENAME
6534 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6535 return 2;
6536#endif
6537 return 1;
6538 }
6539 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006540}
6541
6542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 * Show wildchar matches in the status line.
6544 * Show at least the "match" item.
6545 * We start at item 'first_match' in the list and show all matches that fit.
6546 *
6547 * If inversion is possible we use it. Else '=' characters are used.
6548 */
6549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006550win_redr_status_matches(
6551 expand_T *xp,
6552 int num_matches,
6553 char_u **matches, /* list of matches */
6554 int match,
6555 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
6557#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6558 int row;
6559 char_u *buf;
6560 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006561 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 int fillchar;
6563 int attr;
6564 int i;
6565 int highlight = TRUE;
6566 char_u *selstart = NULL;
6567 int selstart_col = 0;
6568 char_u *selend = NULL;
6569 static int first_match = 0;
6570 int add_left = FALSE;
6571 char_u *s;
6572#ifdef FEAT_MENU
6573 int emenu;
6574#endif
6575#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6576 int l;
6577#endif
6578
6579 if (matches == NULL) /* interrupted completion? */
6580 return;
6581
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006582#ifdef FEAT_MBYTE
6583 if (has_mbyte)
6584 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6585 else
6586#endif
6587 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 if (buf == NULL)
6589 return;
6590
6591 if (match == -1) /* don't show match but original text */
6592 {
6593 match = 0;
6594 highlight = FALSE;
6595 }
6596 /* count 1 for the ending ">" */
6597 clen = status_match_len(xp, L_MATCH(match)) + 3;
6598 if (match == 0)
6599 first_match = 0;
6600 else if (match < first_match)
6601 {
6602 /* jumping left, as far as we can go */
6603 first_match = match;
6604 add_left = TRUE;
6605 }
6606 else
6607 {
6608 /* check if match fits on the screen */
6609 for (i = first_match; i < match; ++i)
6610 clen += status_match_len(xp, L_MATCH(i)) + 2;
6611 if (first_match > 0)
6612 clen += 2;
6613 /* jumping right, put match at the left */
6614 if ((long)clen > Columns)
6615 {
6616 first_match = match;
6617 /* if showing the last match, we can add some on the left */
6618 clen = 2;
6619 for (i = match; i < num_matches; ++i)
6620 {
6621 clen += status_match_len(xp, L_MATCH(i)) + 2;
6622 if ((long)clen >= Columns)
6623 break;
6624 }
6625 if (i == num_matches)
6626 add_left = TRUE;
6627 }
6628 }
6629 if (add_left)
6630 while (first_match > 0)
6631 {
6632 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6633 if ((long)clen >= Columns)
6634 break;
6635 --first_match;
6636 }
6637
6638 fillchar = fillchar_status(&attr, TRUE);
6639
6640 if (first_match == 0)
6641 {
6642 *buf = NUL;
6643 len = 0;
6644 }
6645 else
6646 {
6647 STRCPY(buf, "< ");
6648 len = 2;
6649 }
6650 clen = len;
6651
6652 i = first_match;
6653 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6654 {
6655 if (i == match)
6656 {
6657 selstart = buf + len;
6658 selstart_col = clen;
6659 }
6660
6661 s = L_MATCH(i);
6662 /* Check for menu separators - replace with '|' */
6663#ifdef FEAT_MENU
6664 emenu = (xp->xp_context == EXPAND_MENUS
6665 || xp->xp_context == EXPAND_MENUNAMES);
6666 if (emenu && menu_is_separator(s))
6667 {
6668 STRCPY(buf + len, transchar('|'));
6669 l = (int)STRLEN(buf + len);
6670 len += l;
6671 clen += l;
6672 }
6673 else
6674#endif
6675 for ( ; *s != NUL; ++s)
6676 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006677 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 clen += ptr2cells(s);
6679#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006680 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
6682 STRNCPY(buf + len, s, l);
6683 s += l - 1;
6684 len += l;
6685 }
6686 else
6687#endif
6688 {
6689 STRCPY(buf + len, transchar_byte(*s));
6690 len += (int)STRLEN(buf + len);
6691 }
6692 }
6693 if (i == match)
6694 selend = buf + len;
6695
6696 *(buf + len++) = ' ';
6697 *(buf + len++) = ' ';
6698 clen += 2;
6699 if (++i == num_matches)
6700 break;
6701 }
6702
6703 if (i != num_matches)
6704 {
6705 *(buf + len++) = '>';
6706 ++clen;
6707 }
6708
6709 buf[len] = NUL;
6710
6711 row = cmdline_row - 1;
6712 if (row >= 0)
6713 {
6714 if (wild_menu_showing == 0)
6715 {
6716 if (msg_scrolled > 0)
6717 {
6718 /* Put the wildmenu just above the command line. If there is
6719 * no room, scroll the screen one line up. */
6720 if (cmdline_row == Rows - 1)
6721 {
6722 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6723 ++msg_scrolled;
6724 }
6725 else
6726 {
6727 ++cmdline_row;
6728 ++row;
6729 }
6730 wild_menu_showing = WM_SCROLLED;
6731 }
6732 else
6733 {
6734 /* Create status line if needed by setting 'laststatus' to 2.
6735 * Set 'winminheight' to zero to avoid that the window is
6736 * resized. */
6737 if (lastwin->w_status_height == 0)
6738 {
6739 save_p_ls = p_ls;
6740 save_p_wmh = p_wmh;
6741 p_ls = 2;
6742 p_wmh = 0;
6743 last_status(FALSE);
6744 }
6745 wild_menu_showing = WM_SHOWN;
6746 }
6747 }
6748
6749 screen_puts(buf, row, 0, attr);
6750 if (selstart != NULL && highlight)
6751 {
6752 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006753 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755
6756 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6757 }
6758
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006759#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 win_redraw_last_status(topframe);
6761#else
6762 lastwin->w_redr_status = TRUE;
6763#endif
6764 vim_free(buf);
6765}
6766#endif
6767
6768#if defined(FEAT_WINDOWS) || defined(PROTO)
6769/*
6770 * Redraw the status line of window wp.
6771 *
6772 * If inversion is possible we use it. Else '=' characters are used.
6773 */
6774 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006775win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 int row;
6778 char_u *p;
6779 int len;
6780 int fillchar;
6781 int attr;
6782 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006783 static int busy = FALSE;
6784
6785 /* It's possible to get here recursively when 'statusline' (indirectly)
6786 * invokes ":redrawstatus". Simply ignore the call then. */
6787 if (busy)
6788 return;
6789 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 wp->w_redr_status = FALSE;
6792 if (wp->w_status_height == 0)
6793 {
6794 /* no status line, can only be last window */
6795 redraw_cmdline = TRUE;
6796 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006797 else if (!redrawing()
6798#ifdef FEAT_INS_EXPAND
6799 /* don't update status line when popup menu is visible and may be
6800 * drawn over it */
6801 || pum_visible()
6802#endif
6803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
6805 /* Don't redraw right now, do it later. */
6806 wp->w_redr_status = TRUE;
6807 }
6808#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006809 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
6811 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006812 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814#endif
6815 else
6816 {
6817 fillchar = fillchar_status(&attr, wp == curwin);
6818
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006819 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 p = NameBuff;
6821 len = (int)STRLEN(p);
6822
6823 if (wp->w_buffer->b_help
6824#ifdef FEAT_QUICKFIX
6825 || wp->w_p_pvw
6826#endif
6827 || bufIsChanged(wp->w_buffer)
6828 || wp->w_buffer->b_p_ro)
6829 *(p + len++) = ' ';
6830 if (wp->w_buffer->b_help)
6831 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006832 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 len += (int)STRLEN(p + len);
6834 }
6835#ifdef FEAT_QUICKFIX
6836 if (wp->w_p_pvw)
6837 {
6838 STRCPY(p + len, _("[Preview]"));
6839 len += (int)STRLEN(p + len);
6840 }
6841#endif
6842 if (bufIsChanged(wp->w_buffer))
6843 {
6844 STRCPY(p + len, "[+]");
6845 len += 3;
6846 }
6847 if (wp->w_buffer->b_p_ro)
6848 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006849 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006850 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6854 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6855 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6856 if (this_ru_col <= 1)
6857 {
6858 p = (char_u *)"<"; /* No room for file name! */
6859 len = 1;
6860 }
6861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862#ifdef FEAT_MBYTE
6863 if (has_mbyte)
6864 {
6865 int clen = 0, i;
6866
6867 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006868 clen = mb_string2cells(p, -1);
6869
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 /* Find first character that will fit.
6871 * Going from start to end is much faster for DBCS. */
6872 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006873 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 clen -= (*mb_ptr2cells)(p + i);
6875 len = clen;
6876 if (i > 0)
6877 {
6878 p = p + i - 1;
6879 *p = '<';
6880 ++len;
6881 }
6882
6883 }
6884 else
6885#endif
6886 if (len > this_ru_col - 1)
6887 {
6888 p += len - (this_ru_col - 1);
6889 *p = '<';
6890 len = this_ru_col - 1;
6891 }
6892
6893 row = W_WINROW(wp) + wp->w_height;
6894 screen_puts(p, row, W_WINCOL(wp), attr);
6895 screen_fill(row, row + 1, len + W_WINCOL(wp),
6896 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6897
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006898 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6900 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6901 - 1 + W_WINCOL(wp)), attr);
6902
6903#ifdef FEAT_CMDL_INFO
6904 win_redr_ruler(wp, TRUE);
6905#endif
6906 }
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 /*
6909 * May need to draw the character below the vertical separator.
6910 */
6911 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6912 {
6913 if (stl_connected(wp))
6914 fillchar = fillchar_status(&attr, wp == curwin);
6915 else
6916 fillchar = fillchar_vsep(&attr);
6917 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6918 attr);
6919 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006920 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921}
6922
Bram Moolenaar238a5642006-02-21 22:12:05 +00006923#ifdef FEAT_STL_OPT
6924/*
6925 * Redraw the status line according to 'statusline' and take care of any
6926 * errors encountered.
6927 */
6928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006929redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006930{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006931 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006932 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006933
6934 /* When called recursively return. This can happen when the statusline
6935 * contains an expression that triggers a redraw. */
6936 if (entered)
6937 return;
6938 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006939
Bram Moolenaara742e082016-04-05 21:10:38 +02006940 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006941 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006942 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006943 {
6944 /* When there is an error disable the statusline, otherwise the
6945 * display is messed up with errors and a redraw triggers the problem
6946 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006947 set_string_option_direct((char_u *)"statusline", -1,
6948 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006949 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006950 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006951 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006953}
6954#endif
6955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956/*
6957 * Return TRUE if the status line of window "wp" is connected to the status
6958 * line of the window right of it. If not, then it's a vertical separator.
6959 * Only call if (wp->w_vsep_width != 0).
6960 */
6961 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006962stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 frame_T *fr;
6965
6966 fr = wp->w_frame;
6967 while (fr->fr_parent != NULL)
6968 {
6969 if (fr->fr_parent->fr_layout == FR_COL)
6970 {
6971 if (fr->fr_next != NULL)
6972 break;
6973 }
6974 else
6975 {
6976 if (fr->fr_next != NULL)
6977 return TRUE;
6978 }
6979 fr = fr->fr_parent;
6980 }
6981 return FALSE;
6982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984#endif /* FEAT_WINDOWS */
6985
6986#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6987/*
6988 * Get the value to show for the language mappings, active 'keymap'.
6989 */
6990 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006991get_keymap_str(
6992 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006993 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006994 char_u *buf, /* buffer for the result */
6995 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996{
6997 char_u *p;
6998
6999 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7000 return FALSE;
7001
7002 {
7003#ifdef FEAT_EVAL
7004 buf_T *old_curbuf = curbuf;
7005 win_T *old_curwin = curwin;
7006 char_u *s;
7007
7008 curbuf = wp->w_buffer;
7009 curwin = wp;
7010 STRCPY(buf, "b:keymap_name"); /* must be writable */
7011 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007012 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 --emsg_skip;
7014 curbuf = old_curbuf;
7015 curwin = old_curwin;
7016 if (p == NULL || *p == NUL)
7017#endif
7018 {
7019#ifdef FEAT_KEYMAP
7020 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7021 p = wp->w_buffer->b_p_keymap;
7022 else
7023#endif
7024 p = (char_u *)"lang";
7025 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007026 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 buf[0] = NUL;
7028#ifdef FEAT_EVAL
7029 vim_free(s);
7030#endif
7031 }
7032 return buf[0] != NUL;
7033}
7034#endif
7035
7036#if defined(FEAT_STL_OPT) || defined(PROTO)
7037/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007038 * Redraw the status line or ruler of window "wp".
7039 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 */
7041 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007042win_redr_custom(
7043 win_T *wp,
7044 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007046 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 int attr;
7048 int curattr;
7049 int row;
7050 int col = 0;
7051 int maxwidth;
7052 int width;
7053 int n;
7054 int len;
7055 int fillchar;
7056 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007057 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007059 struct stl_hlrec hltab[STL_MAX_ITEM];
7060 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007061 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007062 win_T *ewp;
7063 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
Bram Moolenaar1d633412013-12-11 15:52:01 +01007065 /* There is a tiny chance that this gets called recursively: When
7066 * redrawing a status line triggers redrawing the ruler or tabline.
7067 * Avoid trouble by not allowing recursion. */
7068 if (entered)
7069 return;
7070 entered = TRUE;
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007073 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007075 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007076 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007077 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007078 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007079 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007080 maxwidth = Columns;
7081# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007082 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007085 else
7086 {
7087 row = W_WINROW(wp) + wp->w_height;
7088 fillchar = fillchar_status(&attr, wp == curwin);
7089 maxwidth = W_WIDTH(wp);
7090
7091 if (draw_ruler)
7092 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007093 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007094 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007095 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007096 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007097 if (*++stl == '-')
7098 stl++;
7099 if (atoi((char *)stl))
7100 while (VIM_ISDIGIT(*stl))
7101 stl++;
7102 if (*stl++ != '(')
7103 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007104 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007105#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007106 col = ru_col - (Columns - W_WIDTH(wp));
7107 if (col < (W_WIDTH(wp) + 1) / 2)
7108 col = (W_WIDTH(wp) + 1) / 2;
7109#else
7110 col = ru_col;
7111 if (col > (Columns + 1) / 2)
7112 col = (Columns + 1) / 2;
7113#endif
7114 maxwidth = W_WIDTH(wp) - col;
7115#ifdef FEAT_WINDOWS
7116 if (!wp->w_status_height)
7117#endif
7118 {
7119 row = Rows - 1;
7120 --maxwidth; /* writing in last column may cause scrolling */
7121 fillchar = ' ';
7122 attr = 0;
7123 }
7124
7125# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007126 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007127# endif
7128 }
7129 else
7130 {
7131 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007132 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007134 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007136 use_sandbox = was_set_insecurely((char_u *)"statusline",
7137 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138# endif
7139 }
7140
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007141#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142 col += W_WINCOL(wp);
7143#endif
7144 }
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007147 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
Bram Moolenaar61452852011-02-01 18:01:11 +01007149 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7150 * the cursor away and back. */
7151 ewp = wp == NULL ? curwin : wp;
7152 p_crb_save = ewp->w_p_crb;
7153 ewp->w_p_crb = FALSE;
7154
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 /* Make a copy, because the statusline may include a function call that
7156 * might change the option value and free the memory. */
7157 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007158 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007159 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007160 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007161 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007162 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007164 /* Make all characters printable. */
7165 p = transstr(buf);
7166 if (p != NULL)
7167 {
7168 vim_strncpy(buf, p, sizeof(buf) - 1);
7169 vim_free(p);
7170 }
7171
7172 /* fill up with "fillchar" */
7173 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007174 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176#ifdef FEAT_MBYTE
7177 len += (*mb_char2bytes)(fillchar, buf + len);
7178#else
7179 buf[len++] = fillchar;
7180#endif
7181 ++width;
7182 }
7183 buf[len] = NUL;
7184
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007185 /*
7186 * Draw each snippet with the specified highlighting.
7187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 curattr = attr;
7189 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007190 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007192 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 screen_puts_len(p, len, row, col, curattr);
7194 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007195 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007197 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007199 else if (hltab[n].userhl < 0)
7200 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007202 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007203 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204#endif
7205 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007209
7210 if (wp == NULL)
7211 {
7212 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7213 col = 0;
7214 len = 0;
7215 p = buf;
7216 fillchar = 0;
7217 for (n = 0; tabtab[n].start != NULL; n++)
7218 {
7219 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7220 while (col < len)
7221 TabPageIdxs[col++] = fillchar;
7222 p = tabtab[n].start;
7223 fillchar = tabtab[n].userhl;
7224 }
7225 while (col < Columns)
7226 TabPageIdxs[col++] = fillchar;
7227 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007228
7229theend:
7230 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231}
7232
7233#endif /* FEAT_STL_OPT */
7234
7235/*
7236 * Output a single character directly to the screen and update ScreenLines.
7237 */
7238 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007239screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 char_u buf[MB_MAXBYTES + 1];
7242
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007243#ifdef FEAT_MBYTE
7244 if (has_mbyte)
7245 buf[(*mb_char2bytes)(c, buf)] = NUL;
7246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007248 {
7249 buf[0] = c;
7250 buf[1] = NUL;
7251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 screen_puts(buf, row, col, attr);
7253}
7254
7255/*
7256 * Get a single character directly from ScreenLines into "bytes[]".
7257 * Also return its attribute in *attrp;
7258 */
7259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007260screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 unsigned off;
7263
7264 /* safety check */
7265 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7266 {
7267 off = LineOffset[row] + col;
7268 *attrp = ScreenAttrs[off];
7269 bytes[0] = ScreenLines[off];
7270 bytes[1] = NUL;
7271
7272#ifdef FEAT_MBYTE
7273 if (enc_utf8 && ScreenLinesUC[off] != 0)
7274 bytes[utfc_char2bytes(off, bytes)] = NUL;
7275 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7276 {
7277 bytes[0] = ScreenLines[off];
7278 bytes[1] = ScreenLines2[off];
7279 bytes[2] = NUL;
7280 }
7281 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7282 {
7283 bytes[1] = ScreenLines[off + 1];
7284 bytes[2] = NUL;
7285 }
7286#endif
7287 }
7288}
7289
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007290#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007291static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007292
7293/*
7294 * Return TRUE if composing characters for screen posn "off" differs from
7295 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007296 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297 */
7298 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007299screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007300{
7301 int i;
7302
7303 for (i = 0; i < Screen_mco; ++i)
7304 {
7305 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7306 return TRUE;
7307 if (u8cc[i] == 0)
7308 break;
7309 }
7310 return FALSE;
7311}
7312#endif
7313
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314/*
7315 * Put string '*text' on the screen at position 'row' and 'col', with
7316 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7317 * Note: only outputs within one row, message is truncated at screen boundary!
7318 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7319 */
7320 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007321screen_puts(
7322 char_u *text,
7323 int row,
7324 int col,
7325 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 screen_puts_len(text, -1, row, col, attr);
7328}
7329
7330/*
7331 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7332 * a NUL.
7333 */
7334 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007335screen_puts_len(
7336 char_u *text,
7337 int textlen,
7338 int row,
7339 int col,
7340 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 unsigned off;
7343 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007344 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 int c;
7346#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007347 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 int mbyte_blen = 1;
7349 int mbyte_cells = 1;
7350 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007351 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 int clear_next_cell = FALSE;
7353# ifdef FEAT_ARABIC
7354 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007355 int pc, nc, nc1;
7356 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357# endif
7358#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007359#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7360 int force_redraw_this;
7361 int force_redraw_next = FALSE;
7362#endif
7363 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7366 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007367 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
Bram Moolenaarc236c162008-07-13 17:41:49 +00007369#ifdef FEAT_MBYTE
7370 /* When drawing over the right halve of a double-wide char clear out the
7371 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007372 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007373# ifdef FEAT_GUI
7374 && !gui.in_use
7375# endif
7376 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007377 {
7378 ScreenLines[off - 1] = ' ';
7379 ScreenAttrs[off - 1] = 0;
7380 if (enc_utf8)
7381 {
7382 ScreenLinesUC[off - 1] = 0;
7383 ScreenLinesC[0][off - 1] = 0;
7384 }
7385 /* redraw the previous cell, make it empty */
7386 screen_char(off - 1, row, col - 1);
7387 /* force the cell at "col" to be redrawn */
7388 force_redraw_next = TRUE;
7389 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007390#endif
7391
Bram Moolenaar367329b2007-08-30 11:53:22 +00007392#ifdef FEAT_MBYTE
7393 max_off = LineOffset[row] + screen_Columns;
7394#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007395 while (col < screen_Columns
7396 && (len < 0 || (int)(ptr - text) < len)
7397 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 c = *ptr;
7400#ifdef FEAT_MBYTE
7401 /* check if this is the first byte of a multibyte */
7402 if (has_mbyte)
7403 {
7404 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007405 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007407 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7409 mbyte_cells = 1;
7410 else if (enc_dbcs != 0)
7411 mbyte_cells = mbyte_blen;
7412 else /* enc_utf8 */
7413 {
7414 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007415 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 (int)((text + len) - ptr));
7417 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007420# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 /* Non-BMP character: display as ? or fullwidth ?. */
7422 if (u8c >= 0x10000)
7423 {
7424 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7425 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007426 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429# ifdef FEAT_ARABIC
7430 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7431 {
7432 /* Do Arabic shaping. */
7433 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7434 {
7435 /* Past end of string to be displayed. */
7436 nc = NUL;
7437 nc1 = NUL;
7438 }
7439 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007440 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007441 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7442 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007443 nc1 = pcc[0];
7444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 pc = prev_c;
7446 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007447 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449 else
7450 prev_c = u8c;
7451# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007452 if (col + mbyte_cells > screen_Columns)
7453 {
7454 /* Only 1 cell left, but character requires 2 cells:
7455 * display a '>' in the last column to avoid wrapping. */
7456 c = '>';
7457 mbyte_cells = 1;
7458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 }
7460 }
7461#endif
7462
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007463#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7464 force_redraw_this = force_redraw_next;
7465 force_redraw_next = FALSE;
7466#endif
7467
7468 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469#ifdef FEAT_MBYTE
7470 || (mbyte_cells == 2
7471 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7472 || (enc_dbcs == DBCS_JPNU
7473 && c == 0x8e
7474 && ScreenLines2[off] != ptr[1])
7475 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007476 && (ScreenLinesUC[off] !=
7477 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7478 || (ScreenLinesUC[off] != 0
7479 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480#endif
7481 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007482 || exmode_active;
7483
7484 if (need_redraw
7485#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7486 || force_redraw_this
7487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 )
7489 {
7490#if defined(FEAT_GUI) || defined(UNIX)
7491 /* The bold trick makes a single row of pixels appear in the next
7492 * character. When a bold character is removed, the next
7493 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007494 * and for some xterms. */
7495 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496# ifdef FEAT_GUI
7497 gui.in_use
7498# endif
7499# if defined(FEAT_GUI) && defined(UNIX)
7500 ||
7501# endif
7502# ifdef UNIX
7503 term_is_xterm
7504# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007505 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007507 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007509 if (n > HL_ALL)
7510 n = syn_attr2attr(n);
7511 if (n & HL_BOLD)
7512 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514#endif
7515#ifdef FEAT_MBYTE
7516 /* When at the end of the text and overwriting a two-cell
7517 * character with a one-cell character, need to clear the next
7518 * cell. Also when overwriting the left halve of a two-cell char
7519 * with the right halve of a two-cell char. Do this only once
7520 * (mb_off2cells() may return 2 on the right halve). */
7521 if (clear_next_cell)
7522 clear_next_cell = FALSE;
7523 else if (has_mbyte
7524 && (len < 0 ? ptr[mbyte_blen] == NUL
7525 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007526 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007528 && (*mb_off2cells)(off, max_off) == 1
7529 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 clear_next_cell = TRUE;
7531
7532 /* Make sure we never leave a second byte of a double-byte behind,
7533 * it confuses mb_off2cells(). */
7534 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007535 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007537 && (*mb_off2cells)(off, max_off) == 1
7538 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 ScreenLines[off + mbyte_blen] = 0;
7540#endif
7541 ScreenLines[off] = c;
7542 ScreenAttrs[off] = attr;
7543#ifdef FEAT_MBYTE
7544 if (enc_utf8)
7545 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007546 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 ScreenLinesUC[off] = 0;
7548 else
7549 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007550 int i;
7551
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007553 for (i = 0; i < Screen_mco; ++i)
7554 {
7555 ScreenLinesC[i][off] = u8cc[i];
7556 if (u8cc[i] == 0)
7557 break;
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 if (mbyte_cells == 2)
7561 {
7562 ScreenLines[off + 1] = 0;
7563 ScreenAttrs[off + 1] = attr;
7564 }
7565 screen_char(off, row, col);
7566 }
7567 else if (mbyte_cells == 2)
7568 {
7569 ScreenLines[off + 1] = ptr[1];
7570 ScreenAttrs[off + 1] = attr;
7571 screen_char_2(off, row, col);
7572 }
7573 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7574 {
7575 ScreenLines2[off] = ptr[1];
7576 screen_char(off, row, col);
7577 }
7578 else
7579#endif
7580 screen_char(off, row, col);
7581 }
7582#ifdef FEAT_MBYTE
7583 if (has_mbyte)
7584 {
7585 off += mbyte_cells;
7586 col += mbyte_cells;
7587 ptr += mbyte_blen;
7588 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007589 {
7590 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007592 len = -1;
7593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595 else
7596#endif
7597 {
7598 ++off;
7599 ++col;
7600 ++ptr;
7601 }
7602 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007603
7604#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7605 /* If we detected the next character needs to be redrawn, but the text
7606 * doesn't extend up to there, update the character here. */
7607 if (force_redraw_next && col < screen_Columns)
7608 {
7609# ifdef FEAT_MBYTE
7610 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7611 screen_char_2(off, row, col);
7612 else
7613# endif
7614 screen_char(off, row, col);
7615 }
7616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617}
7618
7619#ifdef FEAT_SEARCH_EXTRA
7620/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007621 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 */
7623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007624start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 if (p_hls && !no_hlsearch)
7627 {
7628 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007629 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007630# ifdef FEAT_RELTIME
7631 /* Set the time limit to 'redrawtime'. */
7632 profile_setlimit(p_rdt, &search_hl.tm);
7633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635}
7636
7637/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007638 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 */
7640 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007641end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 if (search_hl.rm.regprog != NULL)
7644 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007645 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 search_hl.rm.regprog = NULL;
7647 }
7648}
7649
7650/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007651 * Init for calling prepare_search_hl().
7652 */
7653 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007654init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007655{
7656 matchitem_T *cur;
7657
7658 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7659 * match */
7660 cur = wp->w_match_head;
7661 while (cur != NULL)
7662 {
7663 cur->hl.rm = cur->match;
7664 if (cur->hlg_id == 0)
7665 cur->hl.attr = 0;
7666 else
7667 cur->hl.attr = syn_id2attr(cur->hlg_id);
7668 cur->hl.buf = wp->w_buffer;
7669 cur->hl.lnum = 0;
7670 cur->hl.first_lnum = 0;
7671# ifdef FEAT_RELTIME
7672 /* Set the time limit to 'redrawtime'. */
7673 profile_setlimit(p_rdt, &(cur->hl.tm));
7674# endif
7675 cur = cur->next;
7676 }
7677 search_hl.buf = wp->w_buffer;
7678 search_hl.lnum = 0;
7679 search_hl.first_lnum = 0;
7680 /* time limit is set at the toplevel, for all windows */
7681}
7682
7683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 * Advance to the match in window "wp" line "lnum" or past it.
7685 */
7686 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007687prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007689 matchitem_T *cur; /* points to the match list */
7690 match_T *shl; /* points to search_hl or a match */
7691 int shl_flag; /* flag to indicate whether search_hl
7692 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007693 int pos_inprogress; /* marks that position match search is
7694 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 int n;
7696
7697 /*
7698 * When using a multi-line pattern, start searching at the top
7699 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007700 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007702 cur = wp->w_match_head;
7703 shl_flag = FALSE;
7704 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007706 if (shl_flag == FALSE)
7707 {
7708 shl = &search_hl;
7709 shl_flag = TRUE;
7710 }
7711 else
7712 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (shl->rm.regprog != NULL
7714 && shl->lnum == 0
7715 && re_multiline(shl->rm.regprog))
7716 {
7717 if (shl->first_lnum == 0)
7718 {
7719# ifdef FEAT_FOLDING
7720 for (shl->first_lnum = lnum;
7721 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7722 if (hasFoldingWin(wp, shl->first_lnum - 1,
7723 NULL, NULL, TRUE, NULL))
7724 break;
7725# else
7726 shl->first_lnum = wp->w_topline;
7727# endif
7728 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 if (cur != NULL)
7730 cur->pos.cur = 0;
7731 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007733 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7734 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007736 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7737 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007738 pos_inprogress = cur == NULL || cur->pos.cur == 0
7739 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (shl->lnum != 0)
7741 {
7742 shl->first_lnum = shl->lnum
7743 + shl->rm.endpos[0].lnum
7744 - shl->rm.startpos[0].lnum;
7745 n = shl->rm.endpos[0].col;
7746 }
7747 else
7748 {
7749 ++shl->first_lnum;
7750 n = 0;
7751 }
7752 }
7753 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007754 if (shl != &search_hl && cur != NULL)
7755 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757}
7758
7759/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007760 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 * Uses shl->buf.
7762 * Sets shl->lnum and shl->rm contents.
7763 * Note: Assumes a previous match is always before "lnum", unless
7764 * shl->lnum is zero.
7765 * Careful: Any pointers for buffer lines will become invalid.
7766 */
7767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007768next_search_hl(
7769 win_T *win,
7770 match_T *shl, /* points to search_hl or a match */
7771 linenr_T lnum,
7772 colnr_T mincol, /* minimal column for a match */
7773 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 linenr_T l;
7776 colnr_T matchcol;
7777 long nmatched;
7778
7779 if (shl->lnum != 0)
7780 {
7781 /* Check for three situations:
7782 * 1. If the "lnum" is below a previous match, start a new search.
7783 * 2. If the previous match includes "mincol", use it.
7784 * 3. Continue after the previous match.
7785 */
7786 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7787 if (lnum > l)
7788 shl->lnum = 0;
7789 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7790 return;
7791 }
7792
7793 /*
7794 * Repeat searching for a match until one is found that includes "mincol"
7795 * or none is found in this line.
7796 */
7797 called_emsg = FALSE;
7798 for (;;)
7799 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007800#ifdef FEAT_RELTIME
7801 /* Stop searching after passing the time limit. */
7802 if (profile_passed_limit(&(shl->tm)))
7803 {
7804 shl->lnum = 0; /* no match found in time */
7805 break;
7806 }
7807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 /* Three situations:
7809 * 1. No useful previous match: search from start of line.
7810 * 2. Not Vi compatible or empty match: continue at next character.
7811 * Break the loop if this is beyond the end of the line.
7812 * 3. Vi compatible searching: continue at end of previous match.
7813 */
7814 if (shl->lnum == 0)
7815 matchcol = 0;
7816 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7817 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007818 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007820 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007821
7822 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007823 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007824 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007826 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 shl->lnum = 0;
7828 break;
7829 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007830#ifdef FEAT_MBYTE
7831 if (has_mbyte)
7832 matchcol += mb_ptr2len(ml);
7833 else
7834#endif
7835 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
7837 else
7838 matchcol = shl->rm.endpos[0].col;
7839
7840 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007843 /* Remember whether shl->rm is using a copy of the regprog in
7844 * cur->match. */
7845 int regprog_is_copy = (shl != &search_hl && cur != NULL
7846 && shl == &cur->hl
7847 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007848 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007849
Bram Moolenaarb3414592014-06-17 17:48:32 +02007850 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7851 matchcol,
7852#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007853 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007854#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007855 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007856#endif
7857 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007858 /* Copy the regprog, in case it got freed and recompiled. */
7859 if (regprog_is_copy)
7860 cur->match.regprog = cur->hl.rm.regprog;
7861
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007862 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007863 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007864 /* Error while handling regexp: stop using this regexp. */
7865 if (shl == &search_hl)
7866 {
7867 /* don't free regprog in the match list, it's a copy */
7868 vim_regfree(shl->rm.regprog);
7869 SET_NO_HLSEARCH(TRUE);
7870 }
7871 shl->rm.regprog = NULL;
7872 shl->lnum = 0;
7873 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7874 message */
7875 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007876 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877 }
7878 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007880 else
7881 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (nmatched == 0)
7883 {
7884 shl->lnum = 0; /* no match found */
7885 break;
7886 }
7887 if (shl->rm.startpos[0].lnum > 0
7888 || shl->rm.startpos[0].col >= mincol
7889 || nmatched > 1
7890 || shl->rm.endpos[0].col > mincol)
7891 {
7892 shl->lnum += shl->rm.startpos[0].lnum;
7893 break; /* useful match found */
7894 }
7895 }
7896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
Bram Moolenaar85077472016-10-16 14:35:48 +02007898/*
7899 * If there is a match fill "shl" and return one.
7900 * Return zero otherwise.
7901 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007902 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007903next_search_hl_pos(
7904 match_T *shl, /* points to a match */
7905 linenr_T lnum,
7906 posmatch_T *posmatch, /* match positions */
7907 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908{
7909 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007910 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007911
Bram Moolenaarb3414592014-06-17 17:48:32 +02007912 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7913 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007914 llpos_T *pos = &posmatch->pos[i];
7915
7916 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007917 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007918 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007919 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007920 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007921 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007922 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007923 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007924 /* if this match comes before the one at "found" then swap
7925 * them */
7926 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007928 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929
Bram Moolenaar85077472016-10-16 14:35:48 +02007930 *pos = posmatch->pos[found];
7931 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 }
7933 }
7934 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007935 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007936 }
7937 }
7938 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007939 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007940 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007941 colnr_T start = posmatch->pos[found].col == 0
7942 ? 0 : posmatch->pos[found].col - 1;
7943 colnr_T end = posmatch->pos[found].col == 0
7944 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945
Bram Moolenaar85077472016-10-16 14:35:48 +02007946 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 shl->rm.startpos[0].lnum = 0;
7948 shl->rm.startpos[0].col = start;
7949 shl->rm.endpos[0].lnum = 0;
7950 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007951 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007952 posmatch->cur = found + 1;
7953 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007955 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007957#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007960screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 attrentry_T *aep = NULL;
7963
7964 screen_attr = attr;
7965 if (full_screen
7966#ifdef WIN3264
7967 && termcap_active
7968#endif
7969 )
7970 {
7971#ifdef FEAT_GUI
7972 if (gui.in_use)
7973 {
7974 char buf[20];
7975
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007976 /* The GUI handles this internally. */
7977 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 OUT_STR(buf);
7979 }
7980 else
7981#endif
7982 {
7983 if (attr > HL_ALL) /* special HL attr. */
7984 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007985 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 aep = syn_cterm_attr2entry(attr);
7987 else
7988 aep = syn_term_attr2entry(attr);
7989 if (aep == NULL) /* did ":syntax clear" */
7990 attr = 0;
7991 else
7992 attr = aep->ae_attr;
7993 }
7994 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7995 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007996 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007997#ifdef FEAT_TERMGUICOLORS
7998 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007999 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008000#endif
8001 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008002#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008003 )
8004#endif
8005 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008006 /* If the Normal FG color has BOLD attribute and the new HL
8007 * has a FG color defined, clear BOLD. */
8008 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8010 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008011 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8012 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 out_str(T_US);
8014 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8015 out_str(T_CZH);
8016 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8017 out_str(T_MR);
8018
8019 /*
8020 * Output the color or start string after bold etc., in case the
8021 * bold etc. override the color setting.
8022 */
8023 if (aep != NULL)
8024 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008025#ifdef FEAT_TERMGUICOLORS
8026 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008028 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008029 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008030 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 if (t_colors > 1)
8037 {
8038 if (aep->ae_u.cterm.fg_color)
8039 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8040 if (aep->ae_u.cterm.bg_color)
8041 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8042 }
8043 else
8044 {
8045 if (aep->ae_u.term.start != NULL)
8046 out_str(aep->ae_u.term.start);
8047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049 }
8050 }
8051 }
8052}
8053
8054 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008055screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
8057 int do_ME = FALSE; /* output T_ME code */
8058
8059 if (screen_attr != 0
8060#ifdef WIN3264
8061 && termcap_active
8062#endif
8063 )
8064 {
8065#ifdef FEAT_GUI
8066 if (gui.in_use)
8067 {
8068 char buf[20];
8069
8070 /* use internal GUI code */
8071 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8072 OUT_STR(buf);
8073 }
8074 else
8075#endif
8076 {
8077 if (screen_attr > HL_ALL) /* special HL attr. */
8078 {
8079 attrentry_T *aep;
8080
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008081 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
8083 /*
8084 * Assume that t_me restores the original colors!
8085 */
8086 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008087 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008088#ifdef FEAT_TERMGUICOLORS
8089 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008090 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8091 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008092#endif
8093 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008094#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008095 )
8096#endif
8097 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 do_ME = TRUE;
8099 }
8100 else
8101 {
8102 aep = syn_term_attr2entry(screen_attr);
8103 if (aep != NULL && aep->ae_u.term.stop != NULL)
8104 {
8105 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8106 do_ME = TRUE;
8107 else
8108 out_str(aep->ae_u.term.stop);
8109 }
8110 }
8111 if (aep == NULL) /* did ":syntax clear" */
8112 screen_attr = 0;
8113 else
8114 screen_attr = aep->ae_attr;
8115 }
8116
8117 /*
8118 * Often all ending-codes are equal to T_ME. Avoid outputting the
8119 * same sequence several times.
8120 */
8121 if (screen_attr & HL_STANDOUT)
8122 {
8123 if (STRCMP(T_SE, T_ME) == 0)
8124 do_ME = TRUE;
8125 else
8126 out_str(T_SE);
8127 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008128 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 if (STRCMP(T_UE, T_ME) == 0)
8131 do_ME = TRUE;
8132 else
8133 out_str(T_UE);
8134 }
8135 if (screen_attr & HL_ITALIC)
8136 {
8137 if (STRCMP(T_CZR, T_ME) == 0)
8138 do_ME = TRUE;
8139 else
8140 out_str(T_CZR);
8141 }
8142 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8143 out_str(T_ME);
8144
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008145#ifdef FEAT_TERMGUICOLORS
8146 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008148 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008149 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008150 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151 term_bg_rgb_color(cterm_normal_bg_gui_color);
8152 }
8153 else
8154#endif
8155 {
8156 if (t_colors > 1)
8157 {
8158 /* set Normal cterm colors */
8159 if (cterm_normal_fg_color != 0)
8160 term_fg_color(cterm_normal_fg_color - 1);
8161 if (cterm_normal_bg_color != 0)
8162 term_bg_color(cterm_normal_bg_color - 1);
8163 if (cterm_normal_fg_bold)
8164 out_str(T_MD);
8165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 }
8167 }
8168 }
8169 screen_attr = 0;
8170}
8171
8172/*
8173 * Reset the colors for a cterm. Used when leaving Vim.
8174 * The machine specific code may override this again.
8175 */
8176 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008177reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008179 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008182#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008183 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8184 || cterm_normal_bg_gui_color != INVALCOLOR)
8185 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008186#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 out_str(T_OP);
8191 screen_attr = -1;
8192 }
8193 if (cterm_normal_fg_bold)
8194 {
8195 out_str(T_ME);
8196 screen_attr = -1;
8197 }
8198 }
8199}
8200
8201/*
8202 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8203 * using the attributes from ScreenAttrs["off"].
8204 */
8205 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008206screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
8208 int attr;
8209
8210 /* Check for illegal values, just in case (could happen just after
8211 * resizing). */
8212 if (row >= screen_Rows || col >= screen_Columns)
8213 return;
8214
Bram Moolenaar494838a2015-02-10 19:20:37 +01008215 /* Outputting a character in the last cell on the screen may scroll the
8216 * screen up. Only do it when the "xn" termcap property is set, otherwise
8217 * mark the character invalid (update it when scrolled up). */
8218 if (*T_XN == NUL
8219 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220#ifdef FEAT_RIGHTLEFT
8221 /* account for first command-line character in rightleft mode */
8222 && !cmdmsg_rl
8223#endif
8224 )
8225 {
8226 ScreenAttrs[off] = (sattr_T)-1;
8227 return;
8228 }
8229
8230 /*
8231 * Stop highlighting first, so it's easier to move the cursor.
8232 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008233#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (screen_char_attr != 0)
8235 attr = screen_char_attr;
8236 else
8237#endif
8238 attr = ScreenAttrs[off];
8239 if (screen_attr != attr)
8240 screen_stop_highlight();
8241
8242 windgoto(row, col);
8243
8244 if (screen_attr != attr)
8245 screen_start_highlight(attr);
8246
8247#ifdef FEAT_MBYTE
8248 if (enc_utf8 && ScreenLinesUC[off] != 0)
8249 {
8250 char_u buf[MB_MAXBYTES + 1];
8251
8252 /* Convert UTF-8 character to bytes and write it. */
8253
8254 buf[utfc_char2bytes(off, buf)] = NUL;
8255
8256 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008257 if (utf_ambiguous_width(ScreenLinesUC[off]))
8258 screen_cur_col = 9999;
8259 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 ++screen_cur_col;
8261 }
8262 else
8263#endif
8264 {
8265#ifdef FEAT_MBYTE
8266 out_flush_check();
8267#endif
8268 out_char(ScreenLines[off]);
8269#ifdef FEAT_MBYTE
8270 /* double-byte character in single-width cell */
8271 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8272 out_char(ScreenLines2[off]);
8273#endif
8274 }
8275
8276 screen_cur_col++;
8277}
8278
8279#ifdef FEAT_MBYTE
8280
8281/*
8282 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8283 * on the screen at position 'row' and 'col'.
8284 * The attributes of the first byte is used for all. This is required to
8285 * output the two bytes of a double-byte character with nothing in between.
8286 */
8287 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008288screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289{
8290 /* Check for illegal values (could be wrong when screen was resized). */
8291 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8292 return;
8293
8294 /* Outputting the last character on the screen may scrollup the screen.
8295 * Don't to it! Mark the character invalid (update it when scrolled up) */
8296 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8297 {
8298 ScreenAttrs[off] = (sattr_T)-1;
8299 return;
8300 }
8301
8302 /* Output the first byte normally (positions the cursor), then write the
8303 * second byte directly. */
8304 screen_char(off, row, col);
8305 out_char(ScreenLines[off + 1]);
8306 ++screen_cur_col;
8307}
8308#endif
8309
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008310#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311/*
8312 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8313 * This uses the contents of ScreenLines[] and doesn't change it.
8314 */
8315 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008316screen_draw_rectangle(
8317 int row,
8318 int col,
8319 int height,
8320 int width,
8321 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 int r, c;
8324 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008325#ifdef FEAT_MBYTE
8326 int max_off;
8327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008329 /* Can't use ScreenLines unless initialized */
8330 if (ScreenLines == NULL)
8331 return;
8332
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (invert)
8334 screen_char_attr = HL_INVERSE;
8335 for (r = row; r < row + height; ++r)
8336 {
8337 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008338#ifdef FEAT_MBYTE
8339 max_off = off + screen_Columns;
8340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 for (c = col; c < col + width; ++c)
8342 {
8343#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008344 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {
8346 screen_char_2(off + c, r, c);
8347 ++c;
8348 }
8349 else
8350#endif
8351 {
8352 screen_char(off + c, r, c);
8353#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008354 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 ++c;
8356#endif
8357 }
8358 }
8359 }
8360 screen_char_attr = 0;
8361}
8362#endif
8363
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008364#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365/*
8366 * Redraw the characters for a vertically split window.
8367 */
8368 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008369redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 int col;
8372 int width;
8373
8374# ifdef FEAT_CLIPBOARD
8375 clip_may_clear_selection(row, end - 1);
8376# endif
8377
8378 if (wp == NULL)
8379 {
8380 col = 0;
8381 width = Columns;
8382 }
8383 else
8384 {
8385 col = wp->w_wincol;
8386 width = wp->w_width;
8387 }
8388 screen_draw_rectangle(row, col, end - row, width, FALSE);
8389}
8390#endif
8391
8392/*
8393 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8394 * with character 'c1' in first column followed by 'c2' in the other columns.
8395 * Use attributes 'attr'.
8396 */
8397 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008398screen_fill(
8399 int start_row,
8400 int end_row,
8401 int start_col,
8402 int end_col,
8403 int c1,
8404 int c2,
8405 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 int row;
8408 int col;
8409 int off;
8410 int end_off;
8411 int did_delete;
8412 int c;
8413 int norm_term;
8414#if defined(FEAT_GUI) || defined(UNIX)
8415 int force_next = FALSE;
8416#endif
8417
8418 if (end_row > screen_Rows) /* safety check */
8419 end_row = screen_Rows;
8420 if (end_col > screen_Columns) /* safety check */
8421 end_col = screen_Columns;
8422 if (ScreenLines == NULL
8423 || start_row >= end_row
8424 || start_col >= end_col) /* nothing to do */
8425 return;
8426
8427 /* it's a "normal" terminal when not in a GUI or cterm */
8428 norm_term = (
8429#ifdef FEAT_GUI
8430 !gui.in_use &&
8431#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008432 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 for (row = start_row; row < end_row; ++row)
8434 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008435#ifdef FEAT_MBYTE
8436 if (has_mbyte
8437# ifdef FEAT_GUI
8438 && !gui.in_use
8439# endif
8440 )
8441 {
8442 /* When drawing over the right halve of a double-wide char clear
8443 * out the left halve. When drawing over the left halve of a
8444 * double wide-char clear out the right halve. Only needed in a
8445 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008446 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008447 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008448 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008449 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008450 }
8451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 /*
8453 * Try to use delete-line termcap code, when no attributes or in a
8454 * "normal" terminal, where a bold/italic space is just a
8455 * space.
8456 */
8457 did_delete = FALSE;
8458 if (c2 == ' '
8459 && end_col == Columns
8460 && can_clear(T_CE)
8461 && (attr == 0
8462 || (norm_term
8463 && attr <= HL_ALL
8464 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8465 {
8466 /*
8467 * check if we really need to clear something
8468 */
8469 col = start_col;
8470 if (c1 != ' ') /* don't clear first char */
8471 ++col;
8472
8473 off = LineOffset[row] + col;
8474 end_off = LineOffset[row] + end_col;
8475
8476 /* skip blanks (used often, keep it fast!) */
8477#ifdef FEAT_MBYTE
8478 if (enc_utf8)
8479 while (off < end_off && ScreenLines[off] == ' '
8480 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8481 ++off;
8482 else
8483#endif
8484 while (off < end_off && ScreenLines[off] == ' '
8485 && ScreenAttrs[off] == 0)
8486 ++off;
8487 if (off < end_off) /* something to be cleared */
8488 {
8489 col = off - LineOffset[row];
8490 screen_stop_highlight();
8491 term_windgoto(row, col);/* clear rest of this screen line */
8492 out_str(T_CE);
8493 screen_start(); /* don't know where cursor is now */
8494 col = end_col - col;
8495 while (col--) /* clear chars in ScreenLines */
8496 {
8497 ScreenLines[off] = ' ';
8498#ifdef FEAT_MBYTE
8499 if (enc_utf8)
8500 ScreenLinesUC[off] = 0;
8501#endif
8502 ScreenAttrs[off] = 0;
8503 ++off;
8504 }
8505 }
8506 did_delete = TRUE; /* the chars are cleared now */
8507 }
8508
8509 off = LineOffset[row] + start_col;
8510 c = c1;
8511 for (col = start_col; col < end_col; ++col)
8512 {
8513 if (ScreenLines[off] != c
8514#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008515 || (enc_utf8 && (int)ScreenLinesUC[off]
8516 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#endif
8518 || ScreenAttrs[off] != attr
8519#if defined(FEAT_GUI) || defined(UNIX)
8520 || force_next
8521#endif
8522 )
8523 {
8524#if defined(FEAT_GUI) || defined(UNIX)
8525 /* The bold trick may make a single row of pixels appear in
8526 * the next character. When a bold character is removed, the
8527 * next character should be redrawn too. This happens for our
8528 * own GUI and for some xterms. */
8529 if (
8530# ifdef FEAT_GUI
8531 gui.in_use
8532# endif
8533# if defined(FEAT_GUI) && defined(UNIX)
8534 ||
8535# endif
8536# ifdef UNIX
8537 term_is_xterm
8538# endif
8539 )
8540 {
8541 if (ScreenLines[off] != ' '
8542 && (ScreenAttrs[off] > HL_ALL
8543 || ScreenAttrs[off] & HL_BOLD))
8544 force_next = TRUE;
8545 else
8546 force_next = FALSE;
8547 }
8548#endif
8549 ScreenLines[off] = c;
8550#ifdef FEAT_MBYTE
8551 if (enc_utf8)
8552 {
8553 if (c >= 0x80)
8554 {
8555 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008556 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
8558 else
8559 ScreenLinesUC[off] = 0;
8560 }
8561#endif
8562 ScreenAttrs[off] = attr;
8563 if (!did_delete || c != ' ')
8564 screen_char(off, row, col);
8565 }
8566 ++off;
8567 if (col == start_col)
8568 {
8569 if (did_delete)
8570 break;
8571 c = c2;
8572 }
8573 }
8574 if (end_col == Columns)
8575 LineWraps[row] = FALSE;
8576 if (row == Rows - 1) /* overwritten the command line */
8577 {
8578 redraw_cmdline = TRUE;
8579 if (c1 == ' ' && c2 == ' ')
8580 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008581 if (start_col == 0)
8582 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584 }
8585}
8586
8587/*
8588 * Check if there should be a delay. Used before clearing or redrawing the
8589 * screen or the command line.
8590 */
8591 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008592check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8595 && !did_wait_return
8596 && emsg_silent == 0)
8597 {
8598 out_flush();
8599 ui_delay(1000L, TRUE);
8600 emsg_on_display = FALSE;
8601 if (check_msg_scroll)
8602 msg_scroll = FALSE;
8603 }
8604}
8605
8606/*
8607 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008608 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 * Returns TRUE if there is a valid screen to write to.
8610 * Returns FALSE when starting up and screen not initialized yet.
8611 */
8612 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008613screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008615 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 return (ScreenLines != NULL);
8617}
8618
8619/*
8620 * Resize the shell to Rows and Columns.
8621 * Allocate ScreenLines[] and associated items.
8622 *
8623 * There may be some time between setting Rows and Columns and (re)allocating
8624 * ScreenLines[]. This happens when starting up and when (manually) changing
8625 * the shell size. Always use screen_Rows and screen_Columns to access items
8626 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8627 * final size of the shell is needed.
8628 */
8629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008630screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
8632 int new_row, old_row;
8633#ifdef FEAT_GUI
8634 int old_Rows;
8635#endif
8636 win_T *wp;
8637 int outofmem = FALSE;
8638 int len;
8639 schar_T *new_ScreenLines;
8640#ifdef FEAT_MBYTE
8641 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008642 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008644 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#endif
8646 sattr_T *new_ScreenAttrs;
8647 unsigned *new_LineOffset;
8648 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008649#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008650 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008651 tabpage_T *tp;
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008654 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008655#ifdef FEAT_AUTOCMD
8656 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008658retry:
8659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 /*
8661 * Allocation of the screen buffers is done only when the size changes and
8662 * when Rows and Columns have been set and we have started doing full
8663 * screen stuff.
8664 */
8665 if ((ScreenLines != NULL
8666 && Rows == screen_Rows
8667 && Columns == screen_Columns
8668#ifdef FEAT_MBYTE
8669 && enc_utf8 == (ScreenLinesUC != NULL)
8670 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008671 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672#endif
8673 )
8674 || Rows == 0
8675 || Columns == 0
8676 || (!full_screen && ScreenLines == NULL))
8677 return;
8678
8679 /*
8680 * It's possible that we produce an out-of-memory message below, which
8681 * will cause this function to be called again. To break the loop, just
8682 * return here.
8683 */
8684 if (entered)
8685 return;
8686 entered = TRUE;
8687
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008688 /*
8689 * Note that the window sizes are updated before reallocating the arrays,
8690 * thus we must not redraw here!
8691 */
8692 ++RedrawingDisabled;
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 win_new_shellsize(); /* fit the windows in the new sized shell */
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 comp_col(); /* recompute columns for shown command and ruler */
8697
8698 /*
8699 * We're changing the size of the screen.
8700 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8701 * - Move lines from the old arrays into the new arrays, clear extra
8702 * lines (unless the screen is going to be cleared).
8703 * - Free the old arrays.
8704 *
8705 * If anything fails, make ScreenLines NULL, so we don't do anything!
8706 * Continuing with the old ScreenLines may result in a crash, because the
8707 * size is wrong.
8708 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008709 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008711#ifdef FEAT_AUTOCMD
8712 if (aucmd_win != NULL)
8713 win_free_lsize(aucmd_win);
8714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716 new_ScreenLines = (schar_T *)lalloc((long_u)(
8717 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8718#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008719 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 if (enc_utf8)
8721 {
8722 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8723 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008724 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008725 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8727 }
8728 if (enc_dbcs == DBCS_JPNU)
8729 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8730 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8731#endif
8732 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8733 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8734 new_LineOffset = (unsigned *)lalloc((long_u)(
8735 Rows * sizeof(unsigned)), FALSE);
8736 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008737#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008738 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008741 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
8743 if (win_alloc_lines(wp) == FAIL)
8744 {
8745 outofmem = TRUE;
8746#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008747 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748#endif
8749 }
8750 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008751#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008752 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8753 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008754 outofmem = TRUE;
8755#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008756#ifdef FEAT_WINDOWS
8757give_up:
8758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008760#ifdef FEAT_MBYTE
8761 for (i = 0; i < p_mco; ++i)
8762 if (new_ScreenLinesC[i] == NULL)
8763 break;
8764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (new_ScreenLines == NULL
8766#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8769#endif
8770 || new_ScreenAttrs == NULL
8771 || new_LineOffset == NULL
8772 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008773#ifdef FEAT_WINDOWS
8774 || new_TabPageIdxs == NULL
8775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 || outofmem)
8777 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008778 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008779 {
8780 /* guess the size */
8781 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8782
8783 /* Remember we did this to avoid getting outofmem messages over
8784 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008785 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 vim_free(new_ScreenLines);
8788 new_ScreenLines = NULL;
8789#ifdef FEAT_MBYTE
8790 vim_free(new_ScreenLinesUC);
8791 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008792 for (i = 0; i < p_mco; ++i)
8793 {
8794 vim_free(new_ScreenLinesC[i]);
8795 new_ScreenLinesC[i] = NULL;
8796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 vim_free(new_ScreenLines2);
8798 new_ScreenLines2 = NULL;
8799#endif
8800 vim_free(new_ScreenAttrs);
8801 new_ScreenAttrs = NULL;
8802 vim_free(new_LineOffset);
8803 new_LineOffset = NULL;
8804 vim_free(new_LineWraps);
8805 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008806#ifdef FEAT_WINDOWS
8807 vim_free(new_TabPageIdxs);
8808 new_TabPageIdxs = NULL;
8809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 }
8811 else
8812 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008813 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 for (new_row = 0; new_row < Rows; ++new_row)
8816 {
8817 new_LineOffset[new_row] = new_row * Columns;
8818 new_LineWraps[new_row] = FALSE;
8819
8820 /*
8821 * If the screen is not going to be cleared, copy as much as
8822 * possible from the old screen to the new one and clear the rest
8823 * (used when resizing the window at the "--more--" prompt or when
8824 * executing an external command, for the GUI).
8825 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008826 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {
8828 (void)vim_memset(new_ScreenLines + new_row * Columns,
8829 ' ', (size_t)Columns * sizeof(schar_T));
8830#ifdef FEAT_MBYTE
8831 if (enc_utf8)
8832 {
8833 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8834 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 for (i = 0; i < p_mco; ++i)
8836 (void)vim_memset(new_ScreenLinesC[i]
8837 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 0, (size_t)Columns * sizeof(u8char_T));
8839 }
8840 if (enc_dbcs == DBCS_JPNU)
8841 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8842 0, (size_t)Columns * sizeof(schar_T));
8843#endif
8844 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8845 0, (size_t)Columns * sizeof(sattr_T));
8846 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008847 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 {
8849 if (screen_Columns < Columns)
8850 len = screen_Columns;
8851 else
8852 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008853#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008854 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008855 * may be invalid now. Also when p_mco changes. */
8856 if (!(enc_utf8 && ScreenLinesUC == NULL)
8857 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008858#endif
8859 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8860 ScreenLines + LineOffset[old_row],
8861 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 if (enc_utf8 && ScreenLinesUC != NULL
8864 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
8866 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8867 ScreenLinesUC + LineOffset[old_row],
8868 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869 for (i = 0; i < p_mco; ++i)
8870 mch_memmove(new_ScreenLinesC[i]
8871 + new_LineOffset[new_row],
8872 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 (size_t)len * sizeof(u8char_T));
8874 }
8875 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8876 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8877 ScreenLines2 + LineOffset[old_row],
8878 (size_t)len * sizeof(schar_T));
8879#endif
8880 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8881 ScreenAttrs + LineOffset[old_row],
8882 (size_t)len * sizeof(sattr_T));
8883 }
8884 }
8885 }
8886 /* Use the last line of the screen for the current line. */
8887 current_ScreenLine = new_ScreenLines + Rows * Columns;
8888 }
8889
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008890 free_screenlines();
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 ScreenLines = new_ScreenLines;
8893#ifdef FEAT_MBYTE
8894 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008895 for (i = 0; i < p_mco; ++i)
8896 ScreenLinesC[i] = new_ScreenLinesC[i];
8897 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 ScreenLines2 = new_ScreenLines2;
8899#endif
8900 ScreenAttrs = new_ScreenAttrs;
8901 LineOffset = new_LineOffset;
8902 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008903#ifdef FEAT_WINDOWS
8904 TabPageIdxs = new_TabPageIdxs;
8905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
8907 /* It's important that screen_Rows and screen_Columns reflect the actual
8908 * size of ScreenLines[]. Set them before calling anything. */
8909#ifdef FEAT_GUI
8910 old_Rows = screen_Rows;
8911#endif
8912 screen_Rows = Rows;
8913 screen_Columns = Columns;
8914
8915 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008916 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 screenclear2();
8918
8919#ifdef FEAT_GUI
8920 else if (gui.in_use
8921 && !gui.starting
8922 && ScreenLines != NULL
8923 && old_Rows != Rows)
8924 {
8925 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8926 /*
8927 * Adjust the position of the cursor, for when executing an external
8928 * command.
8929 */
8930 if (msg_row >= Rows) /* Rows got smaller */
8931 msg_row = Rows - 1; /* put cursor at last row */
8932 else if (Rows > old_Rows) /* Rows got bigger */
8933 msg_row += Rows - old_Rows; /* put cursor in same place */
8934 if (msg_col >= Columns) /* Columns got smaller */
8935 msg_col = Columns - 1; /* put cursor at last column */
8936 }
8937#endif
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008940 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008941
8942#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008943 /*
8944 * Do not apply autocommands more than 3 times to avoid an endless loop
8945 * in case applying autocommands always changes Rows or Columns.
8946 */
8947 if (starting == 0 && ++retry_count <= 3)
8948 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008949 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008950 /* In rare cases, autocommands may have altered Rows or Columns,
8951 * jump back to check if we need to allocate the screen again. */
8952 goto retry;
8953 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955}
8956
8957 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008958free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008959{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008960#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008961 int i;
8962
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008963 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008964 for (i = 0; i < Screen_mco; ++i)
8965 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008966 vim_free(ScreenLines2);
8967#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008968 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008969 vim_free(ScreenAttrs);
8970 vim_free(LineOffset);
8971 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008972#ifdef FEAT_WINDOWS
8973 vim_free(TabPageIdxs);
8974#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008975}
8976
8977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 check_for_delay(FALSE);
8981 screenalloc(FALSE); /* allocate screen buffers if size changed */
8982 screenclear2(); /* clear the screen */
8983}
8984
8985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008986screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 int i;
8989
8990 if (starting == NO_SCREEN || ScreenLines == NULL
8991#ifdef FEAT_GUI
8992 || (gui.in_use && gui.starting)
8993#endif
8994 )
8995 return;
8996
8997#ifdef FEAT_GUI
8998 if (!gui.in_use)
8999#endif
9000 screen_attr = -1; /* force setting the Normal colors */
9001 screen_stop_highlight(); /* don't want highlighting here */
9002
9003#ifdef FEAT_CLIPBOARD
9004 /* disable selection without redrawing it */
9005 clip_scroll_selection(9999);
9006#endif
9007
9008 /* blank out ScreenLines */
9009 for (i = 0; i < Rows; ++i)
9010 {
9011 lineclear(LineOffset[i], (int)Columns);
9012 LineWraps[i] = FALSE;
9013 }
9014
9015 if (can_clear(T_CL))
9016 {
9017 out_str(T_CL); /* clear the display */
9018 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009019 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 }
9021 else
9022 {
9023 /* can't clear the screen, mark all chars with invalid attributes */
9024 for (i = 0; i < Rows; ++i)
9025 lineinvalid(LineOffset[i], (int)Columns);
9026 clear_cmdline = TRUE;
9027 }
9028
9029 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9030
9031 win_rest_invalid(firstwin);
9032 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009033#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009034 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 if (must_redraw == CLEAR) /* no need to clear again */
9037 must_redraw = NOT_VALID;
9038 compute_cmdrow();
9039 msg_row = cmdline_row; /* put cursor on last line for messages */
9040 msg_col = 0;
9041 screen_start(); /* don't know where cursor is now */
9042 msg_scrolled = 0; /* can't scroll back */
9043 msg_didany = FALSE;
9044 msg_didout = FALSE;
9045}
9046
9047/*
9048 * Clear one line in ScreenLines.
9049 */
9050 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009051lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
9053 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9054#ifdef FEAT_MBYTE
9055 if (enc_utf8)
9056 (void)vim_memset(ScreenLinesUC + off, 0,
9057 (size_t)width * sizeof(u8char_T));
9058#endif
9059 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9060}
9061
9062/*
9063 * Mark one line in ScreenLines invalid by setting the attributes to an
9064 * invalid value.
9065 */
9066 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009067lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9070}
9071
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009072#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073/*
9074 * Copy part of a Screenline for vertically split window "wp".
9075 */
9076 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009077linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 unsigned off_to = LineOffset[to] + wp->w_wincol;
9080 unsigned off_from = LineOffset[from] + wp->w_wincol;
9081
9082 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9083 wp->w_width * sizeof(schar_T));
9084# ifdef FEAT_MBYTE
9085 if (enc_utf8)
9086 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009087 int i;
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9090 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009091 for (i = 0; i < p_mco; ++i)
9092 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9093 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 }
9095 if (enc_dbcs == DBCS_JPNU)
9096 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9097 wp->w_width * sizeof(schar_T));
9098# endif
9099 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9100 wp->w_width * sizeof(sattr_T));
9101}
9102#endif
9103
9104/*
9105 * Return TRUE if clearing with term string "p" would work.
9106 * It can't work when the string is empty or it won't set the right background.
9107 */
9108 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009109can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 return (*p != NUL && (t_colors <= 1
9112#ifdef FEAT_GUI
9113 || gui.in_use
9114#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009115#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009116 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009117 || (!p_tgc && cterm_normal_bg_color == 0)
9118#else
9119 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009120#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009121 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
9124/*
9125 * Reset cursor position. Use whenever cursor was moved because of outputting
9126 * something directly to the screen (shell commands) or a terminal control
9127 * code.
9128 */
9129 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009130screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 screen_cur_row = screen_cur_col = 9999;
9133}
9134
9135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 * Move the cursor to position "row","col" in the screen.
9137 * This tries to find the most efficient way to move, minimizing the number of
9138 * characters sent to the terminal.
9139 */
9140 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009141windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009143 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 int i;
9145 int plan;
9146 int cost;
9147 int wouldbe_col;
9148 int noinvcurs;
9149 char_u *bs;
9150 int goto_cost;
9151 int attr;
9152
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009153#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9155
9156#define PLAN_LE 1
9157#define PLAN_CR 2
9158#define PLAN_NL 3
9159#define PLAN_WRITE 4
9160 /* Can't use ScreenLines unless initialized */
9161 if (ScreenLines == NULL)
9162 return;
9163
9164 if (col != screen_cur_col || row != screen_cur_row)
9165 {
9166 /* Check for valid position. */
9167 if (row < 0) /* window without text lines? */
9168 row = 0;
9169 if (row >= screen_Rows)
9170 row = screen_Rows - 1;
9171 if (col >= screen_Columns)
9172 col = screen_Columns - 1;
9173
9174 /* check if no cursor movement is allowed in highlight mode */
9175 if (screen_attr && *T_MS == NUL)
9176 noinvcurs = HIGHL_COST;
9177 else
9178 noinvcurs = 0;
9179 goto_cost = GOTO_COST + noinvcurs;
9180
9181 /*
9182 * Plan how to do the positioning:
9183 * 1. Use CR to move it to column 0, same row.
9184 * 2. Use T_LE to move it a few columns to the left.
9185 * 3. Use NL to move a few lines down, column 0.
9186 * 4. Move a few columns to the right with T_ND or by writing chars.
9187 *
9188 * Don't do this if the cursor went beyond the last column, the cursor
9189 * position is unknown then (some terminals wrap, some don't )
9190 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009191 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 * characters to move the cursor to the right.
9193 */
9194 if (row >= screen_cur_row && screen_cur_col < Columns)
9195 {
9196 /*
9197 * If the cursor is in the same row, bigger col, we can use CR
9198 * or T_LE.
9199 */
9200 bs = NULL; /* init for GCC */
9201 attr = screen_attr;
9202 if (row == screen_cur_row && col < screen_cur_col)
9203 {
9204 /* "le" is preferred over "bc", because "bc" is obsolete */
9205 if (*T_LE)
9206 bs = T_LE; /* "cursor left" */
9207 else
9208 bs = T_BC; /* "backspace character (old) */
9209 if (*bs)
9210 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9211 else
9212 cost = 999;
9213 if (col + 1 < cost) /* using CR is less characters */
9214 {
9215 plan = PLAN_CR;
9216 wouldbe_col = 0;
9217 cost = 1; /* CR is just one character */
9218 }
9219 else
9220 {
9221 plan = PLAN_LE;
9222 wouldbe_col = col;
9223 }
9224 if (noinvcurs) /* will stop highlighting */
9225 {
9226 cost += noinvcurs;
9227 attr = 0;
9228 }
9229 }
9230
9231 /*
9232 * If the cursor is above where we want to be, we can use CR LF.
9233 */
9234 else if (row > screen_cur_row)
9235 {
9236 plan = PLAN_NL;
9237 wouldbe_col = 0;
9238 cost = (row - screen_cur_row) * 2; /* CR LF */
9239 if (noinvcurs) /* will stop highlighting */
9240 {
9241 cost += noinvcurs;
9242 attr = 0;
9243 }
9244 }
9245
9246 /*
9247 * If the cursor is in the same row, smaller col, just use write.
9248 */
9249 else
9250 {
9251 plan = PLAN_WRITE;
9252 wouldbe_col = screen_cur_col;
9253 cost = 0;
9254 }
9255
9256 /*
9257 * Check if any characters that need to be written have the
9258 * correct attributes. Also avoid UTF-8 characters.
9259 */
9260 i = col - wouldbe_col;
9261 if (i > 0)
9262 cost += i;
9263 if (cost < goto_cost && i > 0)
9264 {
9265 /*
9266 * Check if the attributes are correct without additionally
9267 * stopping highlighting.
9268 */
9269 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9270 while (i && *p++ == attr)
9271 --i;
9272 if (i != 0)
9273 {
9274 /*
9275 * Try if it works when highlighting is stopped here.
9276 */
9277 if (*--p == 0)
9278 {
9279 cost += noinvcurs;
9280 while (i && *p++ == 0)
9281 --i;
9282 }
9283 if (i != 0)
9284 cost = 999; /* different attributes, don't do it */
9285 }
9286#ifdef FEAT_MBYTE
9287 if (enc_utf8)
9288 {
9289 /* Don't use an UTF-8 char for positioning, it's slow. */
9290 for (i = wouldbe_col; i < col; ++i)
9291 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9292 {
9293 cost = 999;
9294 break;
9295 }
9296 }
9297#endif
9298 }
9299
9300 /*
9301 * We can do it without term_windgoto()!
9302 */
9303 if (cost < goto_cost)
9304 {
9305 if (plan == PLAN_LE)
9306 {
9307 if (noinvcurs)
9308 screen_stop_highlight();
9309 while (screen_cur_col > col)
9310 {
9311 out_str(bs);
9312 --screen_cur_col;
9313 }
9314 }
9315 else if (plan == PLAN_CR)
9316 {
9317 if (noinvcurs)
9318 screen_stop_highlight();
9319 out_char('\r');
9320 screen_cur_col = 0;
9321 }
9322 else if (plan == PLAN_NL)
9323 {
9324 if (noinvcurs)
9325 screen_stop_highlight();
9326 while (screen_cur_row < row)
9327 {
9328 out_char('\n');
9329 ++screen_cur_row;
9330 }
9331 screen_cur_col = 0;
9332 }
9333
9334 i = col - screen_cur_col;
9335 if (i > 0)
9336 {
9337 /*
9338 * Use cursor-right if it's one character only. Avoids
9339 * removing a line of pixels from the last bold char, when
9340 * using the bold trick in the GUI.
9341 */
9342 if (T_ND[0] != NUL && T_ND[1] == NUL)
9343 {
9344 while (i-- > 0)
9345 out_char(*T_ND);
9346 }
9347 else
9348 {
9349 int off;
9350
9351 off = LineOffset[row] + screen_cur_col;
9352 while (i-- > 0)
9353 {
9354 if (ScreenAttrs[off] != screen_attr)
9355 screen_stop_highlight();
9356#ifdef FEAT_MBYTE
9357 out_flush_check();
9358#endif
9359 out_char(ScreenLines[off]);
9360#ifdef FEAT_MBYTE
9361 if (enc_dbcs == DBCS_JPNU
9362 && ScreenLines[off] == 0x8e)
9363 out_char(ScreenLines2[off]);
9364#endif
9365 ++off;
9366 }
9367 }
9368 }
9369 }
9370 }
9371 else
9372 cost = 999;
9373
9374 if (cost >= goto_cost)
9375 {
9376 if (noinvcurs)
9377 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009378 if (row == screen_cur_row && (col > screen_cur_col)
9379 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 term_cursor_right(col - screen_cur_col);
9381 else
9382 term_windgoto(row, col);
9383 }
9384 screen_cur_row = row;
9385 screen_cur_col = col;
9386 }
9387}
9388
9389/*
9390 * Set cursor to its position in the current window.
9391 */
9392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009393setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
9395 if (redrawing())
9396 {
9397 validate_cursor();
9398 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9399 W_WINCOL(curwin) + (
9400#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009401 /* With 'rightleft' set and the cursor on a double-wide
9402 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9404# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009405 (has_mbyte
9406 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9407 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408# endif
9409 1)) :
9410#endif
9411 curwin->w_wcol));
9412 }
9413}
9414
9415
9416/*
9417 * insert 'line_count' lines at 'row' in window 'wp'
9418 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9419 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9420 * scrolling.
9421 * Returns FAIL if the lines are not inserted, OK for success.
9422 */
9423 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009424win_ins_lines(
9425 win_T *wp,
9426 int row,
9427 int line_count,
9428 int invalid,
9429 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
9431 int did_delete;
9432 int nextrow;
9433 int lastrow;
9434 int retval;
9435
9436 if (invalid)
9437 wp->w_lines_valid = 0;
9438
9439 if (wp->w_height < 5)
9440 return FAIL;
9441
9442 if (line_count > wp->w_height - row)
9443 line_count = wp->w_height - row;
9444
9445 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9446 if (retval != MAYBE)
9447 return retval;
9448
9449 /*
9450 * If there is a next window or a status line, we first try to delete the
9451 * lines at the bottom to avoid messing what is after the window.
9452 * If this fails and there are following windows, don't do anything to avoid
9453 * messing up those windows, better just redraw.
9454 */
9455 did_delete = FALSE;
9456#ifdef FEAT_WINDOWS
9457 if (wp->w_next != NULL || wp->w_status_height)
9458 {
9459 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9460 line_count, (int)Rows, FALSE, NULL) == OK)
9461 did_delete = TRUE;
9462 else if (wp->w_next)
9463 return FAIL;
9464 }
9465#endif
9466 /*
9467 * if no lines deleted, blank the lines that will end up below the window
9468 */
9469 if (!did_delete)
9470 {
9471#ifdef FEAT_WINDOWS
9472 wp->w_redr_status = TRUE;
9473#endif
9474 redraw_cmdline = TRUE;
9475 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9476 lastrow = nextrow + line_count;
9477 if (lastrow > Rows)
9478 lastrow = Rows;
9479 screen_fill(nextrow - line_count, lastrow - line_count,
9480 W_WINCOL(wp), (int)W_ENDCOL(wp),
9481 ' ', ' ', 0);
9482 }
9483
9484 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9485 == FAIL)
9486 {
9487 /* deletion will have messed up other windows */
9488 if (did_delete)
9489 {
9490#ifdef FEAT_WINDOWS
9491 wp->w_redr_status = TRUE;
9492#endif
9493 win_rest_invalid(W_NEXT(wp));
9494 }
9495 return FAIL;
9496 }
9497
9498 return OK;
9499}
9500
9501/*
9502 * delete "line_count" window lines at "row" in window "wp"
9503 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9504 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9505 * scrolling
9506 * Return OK for success, FAIL if the lines are not deleted.
9507 */
9508 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009509win_del_lines(
9510 win_T *wp,
9511 int row,
9512 int line_count,
9513 int invalid,
9514 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 int retval;
9517
9518 if (invalid)
9519 wp->w_lines_valid = 0;
9520
9521 if (line_count > wp->w_height - row)
9522 line_count = wp->w_height - row;
9523
9524 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9525 if (retval != MAYBE)
9526 return retval;
9527
9528 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9529 (int)Rows, FALSE, NULL) == FAIL)
9530 return FAIL;
9531
9532#ifdef FEAT_WINDOWS
9533 /*
9534 * If there are windows or status lines below, try to put them at the
9535 * correct place. If we can't do that, they have to be redrawn.
9536 */
9537 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9538 {
9539 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9540 line_count, (int)Rows, NULL) == FAIL)
9541 {
9542 wp->w_redr_status = TRUE;
9543 win_rest_invalid(wp->w_next);
9544 }
9545 }
9546 /*
9547 * If this is the last window and there is no status line, redraw the
9548 * command line later.
9549 */
9550 else
9551#endif
9552 redraw_cmdline = TRUE;
9553 return OK;
9554}
9555
9556/*
9557 * Common code for win_ins_lines() and win_del_lines().
9558 * Returns OK or FAIL when the work has been done.
9559 * Returns MAYBE when not finished yet.
9560 */
9561 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009562win_do_lines(
9563 win_T *wp,
9564 int row,
9565 int line_count,
9566 int mayclear,
9567 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569 int retval;
9570
9571 if (!redrawing() || line_count <= 0)
9572 return FAIL;
9573
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009574 /* When inserting lines would result in loss of command output, just redraw
9575 * the lines. */
9576 if (no_win_do_lines_ins && !del)
9577 return FAIL;
9578
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 /* only a few lines left: redraw is faster */
9580 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009581#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 && wp->w_width == Columns
9583#endif
9584 )
9585 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009586 if (!no_win_do_lines_ins)
9587 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 return FAIL;
9589 }
9590
9591 /*
9592 * Delete all remaining lines
9593 */
9594 if (row + line_count >= wp->w_height)
9595 {
9596 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9597 W_WINCOL(wp), (int)W_ENDCOL(wp),
9598 ' ', ' ', 0);
9599 return OK;
9600 }
9601
9602 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009603 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009605 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009607 if (!no_win_do_lines_ins)
9608 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609
9610 /*
9611 * If the terminal can set a scroll region, use that.
9612 * Always do this in a vertically split window. This will redraw from
9613 * ScreenLines[] when t_CV isn't defined. That's faster than using
9614 * win_line().
9615 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009616 * a character in the lower right corner of the scroll region may cause a
9617 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 */
9619 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009620#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 || W_WIDTH(wp) != Columns
9622#endif
9623 )
9624 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009625#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9627#endif
9628 scroll_region_set(wp, row);
9629 if (del)
9630 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9631 wp->w_height - row, FALSE, wp);
9632 else
9633 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9634 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009635#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9637#endif
9638 scroll_region_reset();
9639 return retval;
9640 }
9641
9642#ifdef FEAT_WINDOWS
9643 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9644 return FAIL;
9645#endif
9646
9647 return MAYBE;
9648}
9649
9650/*
9651 * window 'wp' and everything after it is messed up, mark it for redraw
9652 */
9653 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009654win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655{
9656#ifdef FEAT_WINDOWS
9657 while (wp != NULL)
9658#else
9659 if (wp != NULL)
9660#endif
9661 {
9662 redraw_win_later(wp, NOT_VALID);
9663#ifdef FEAT_WINDOWS
9664 wp->w_redr_status = TRUE;
9665 wp = wp->w_next;
9666#endif
9667 }
9668 redraw_cmdline = TRUE;
9669}
9670
9671/*
9672 * The rest of the routines in this file perform screen manipulations. The
9673 * given operation is performed physically on the screen. The corresponding
9674 * change is also made to the internal screen image. In this way, the editor
9675 * anticipates the effect of editing changes on the appearance of the screen.
9676 * That way, when we call screenupdate a complete redraw isn't usually
9677 * necessary. Another advantage is that we can keep adding code to anticipate
9678 * screen changes, and in the meantime, everything still works.
9679 */
9680
9681/*
9682 * types for inserting or deleting lines
9683 */
9684#define USE_T_CAL 1
9685#define USE_T_CDL 2
9686#define USE_T_AL 3
9687#define USE_T_CE 4
9688#define USE_T_DL 5
9689#define USE_T_SR 6
9690#define USE_NL 7
9691#define USE_T_CD 8
9692#define USE_REDRAW 9
9693
9694/*
9695 * insert lines on the screen and update ScreenLines[]
9696 * 'end' is the line after the scrolled part. Normally it is Rows.
9697 * When scrolling region used 'off' is the offset from the top for the region.
9698 * 'row' and 'end' are relative to the start of the region.
9699 *
9700 * return FAIL for failure, OK for success.
9701 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009702 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009703screen_ins_lines(
9704 int off,
9705 int row,
9706 int line_count,
9707 int end,
9708 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 int i;
9711 int j;
9712 unsigned temp;
9713 int cursor_row;
9714 int type;
9715 int result_empty;
9716 int can_ce = can_clear(T_CE);
9717
9718 /*
9719 * FAIL if
9720 * - there is no valid screen
9721 * - the screen has to be redrawn completely
9722 * - the line count is less than one
9723 * - the line count is more than 'ttyscroll'
9724 */
9725 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9726 return FAIL;
9727
9728 /*
9729 * There are seven ways to insert lines:
9730 * 0. When in a vertically split window and t_CV isn't set, redraw the
9731 * characters from ScreenLines[].
9732 * 1. Use T_CD (clear to end of display) if it exists and the result of
9733 * the insert is just empty lines
9734 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9735 * present or line_count > 1. It looks better if we do all the inserts
9736 * at once.
9737 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9738 * insert is just empty lines and T_CE is not present or line_count >
9739 * 1.
9740 * 4. Use T_AL (insert line) if it exists.
9741 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9742 * just empty lines.
9743 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9744 * just empty lines.
9745 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9746 * the 'da' flag is not set or we have clear line capability.
9747 * 8. redraw the characters from ScreenLines[].
9748 *
9749 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9750 * the scrollbar for the window. It does have insert line, use that if it
9751 * exists.
9752 */
9753 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009754#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9756 type = USE_REDRAW;
9757 else
9758#endif
9759 if (can_clear(T_CD) && result_empty)
9760 type = USE_T_CD;
9761 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9762 type = USE_T_CAL;
9763 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9764 type = USE_T_CDL;
9765 else if (*T_AL != NUL)
9766 type = USE_T_AL;
9767 else if (can_ce && result_empty)
9768 type = USE_T_CE;
9769 else if (*T_DL != NUL && result_empty)
9770 type = USE_T_DL;
9771 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9772 type = USE_T_SR;
9773 else
9774 return FAIL;
9775
9776 /*
9777 * For clearing the lines screen_del_lines() is used. This will also take
9778 * care of t_db if necessary.
9779 */
9780 if (type == USE_T_CD || type == USE_T_CDL ||
9781 type == USE_T_CE || type == USE_T_DL)
9782 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9783
9784 /*
9785 * If text is retained below the screen, first clear or delete as many
9786 * lines at the bottom of the window as are about to be inserted so that
9787 * the deleted lines won't later surface during a screen_del_lines.
9788 */
9789 if (*T_DB)
9790 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9791
9792#ifdef FEAT_CLIPBOARD
9793 /* Remove a modeless selection when inserting lines halfway the screen
9794 * or not the full width of the screen. */
9795 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009796# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 || (wp != NULL && wp->w_width != Columns)
9798# endif
9799 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009800 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
9802 clip_scroll_selection(-line_count);
9803#endif
9804
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805#ifdef FEAT_GUI
9806 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9807 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009808 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#endif
9810
9811 if (*T_CCS != NUL) /* cursor relative to region */
9812 cursor_row = row;
9813 else
9814 cursor_row = row + off;
9815
9816 /*
9817 * Shift LineOffset[] line_count down to reflect the inserted lines.
9818 * Clear the inserted lines in ScreenLines[].
9819 */
9820 row += off;
9821 end += off;
9822 for (i = 0; i < line_count; ++i)
9823 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009824#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (wp != NULL && wp->w_width != Columns)
9826 {
9827 /* need to copy part of a line */
9828 j = end - 1 - i;
9829 while ((j -= line_count) >= row)
9830 linecopy(j + line_count, j, wp);
9831 j += line_count;
9832 if (can_clear((char_u *)" "))
9833 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9834 else
9835 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9836 LineWraps[j] = FALSE;
9837 }
9838 else
9839#endif
9840 {
9841 j = end - 1 - i;
9842 temp = LineOffset[j];
9843 while ((j -= line_count) >= row)
9844 {
9845 LineOffset[j + line_count] = LineOffset[j];
9846 LineWraps[j + line_count] = LineWraps[j];
9847 }
9848 LineOffset[j + line_count] = temp;
9849 LineWraps[j + line_count] = FALSE;
9850 if (can_clear((char_u *)" "))
9851 lineclear(temp, (int)Columns);
9852 else
9853 lineinvalid(temp, (int)Columns);
9854 }
9855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856
9857 screen_stop_highlight();
9858 windgoto(cursor_row, 0);
9859
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009860#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 /* redraw the characters */
9862 if (type == USE_REDRAW)
9863 redraw_block(row, end, wp);
9864 else
9865#endif
9866 if (type == USE_T_CAL)
9867 {
9868 term_append_lines(line_count);
9869 screen_start(); /* don't know where cursor is now */
9870 }
9871 else
9872 {
9873 for (i = 0; i < line_count; i++)
9874 {
9875 if (type == USE_T_AL)
9876 {
9877 if (i && cursor_row != 0)
9878 windgoto(cursor_row, 0);
9879 out_str(T_AL);
9880 }
9881 else /* type == USE_T_SR */
9882 out_str(T_SR);
9883 screen_start(); /* don't know where cursor is now */
9884 }
9885 }
9886
9887 /*
9888 * With scroll-reverse and 'da' flag set we need to clear the lines that
9889 * have been scrolled down into the region.
9890 */
9891 if (type == USE_T_SR && *T_DA)
9892 {
9893 for (i = 0; i < line_count; ++i)
9894 {
9895 windgoto(off + i, 0);
9896 out_str(T_CE);
9897 screen_start(); /* don't know where cursor is now */
9898 }
9899 }
9900
9901#ifdef FEAT_GUI
9902 gui_can_update_cursor();
9903 if (gui.in_use)
9904 out_flush(); /* always flush after a scroll */
9905#endif
9906 return OK;
9907}
9908
9909/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009910 * Delete lines on the screen and update ScreenLines[].
9911 * "end" is the line after the scrolled part. Normally it is Rows.
9912 * When scrolling region used "off" is the offset from the top for the region.
9913 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 *
9915 * Return OK for success, FAIL if the lines are not deleted.
9916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009918screen_del_lines(
9919 int off,
9920 int row,
9921 int line_count,
9922 int end,
9923 int force, /* even when line_count > p_ttyscroll */
9924 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925{
9926 int j;
9927 int i;
9928 unsigned temp;
9929 int cursor_row;
9930 int cursor_end;
9931 int result_empty; /* result is empty until end of region */
9932 int can_delete; /* deleting line codes can be used */
9933 int type;
9934
9935 /*
9936 * FAIL if
9937 * - there is no valid screen
9938 * - the screen has to be redrawn completely
9939 * - the line count is less than one
9940 * - the line count is more than 'ttyscroll'
9941 */
9942 if (!screen_valid(TRUE) || line_count <= 0 ||
9943 (!force && line_count > p_ttyscroll))
9944 return FAIL;
9945
9946 /*
9947 * Check if the rest of the current region will become empty.
9948 */
9949 result_empty = row + line_count >= end;
9950
9951 /*
9952 * We can delete lines only when 'db' flag not set or when 'ce' option
9953 * available.
9954 */
9955 can_delete = (*T_DB == NUL || can_clear(T_CE));
9956
9957 /*
9958 * There are six ways to delete lines:
9959 * 0. When in a vertically split window and t_CV isn't set, redraw the
9960 * characters from ScreenLines[].
9961 * 1. Use T_CD if it exists and the result is empty.
9962 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9963 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9964 * none of the other ways work.
9965 * 4. Use T_CE (erase line) if the result is empty.
9966 * 5. Use T_DL (delete line) if it exists.
9967 * 6. redraw the characters from ScreenLines[].
9968 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009969#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9971 type = USE_REDRAW;
9972 else
9973#endif
9974 if (can_clear(T_CD) && result_empty)
9975 type = USE_T_CD;
9976#if defined(__BEOS__) && defined(BEOS_DR8)
9977 /*
9978 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9979 * its internal termcap... this works okay for tests which test *T_DB !=
9980 * NUL. It has the disadvantage that the user cannot use any :set t_*
9981 * command to get T_DB (back) to empty_option, only :set term=... will do
9982 * the trick...
9983 * Anyway, this hack will hopefully go away with the next OS release.
9984 * (Olaf Seibert)
9985 */
9986 else if (row == 0 && T_DB == empty_option
9987 && (line_count == 1 || *T_CDL == NUL))
9988#else
9989 else if (row == 0 && (
9990#ifndef AMIGA
9991 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9992 * up, so use delete-line command */
9993 line_count == 1 ||
9994#endif
9995 *T_CDL == NUL))
9996#endif
9997 type = USE_NL;
9998 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9999 type = USE_T_CDL;
10000 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010001#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 && (wp == NULL || wp->w_width == Columns)
10003#endif
10004 )
10005 type = USE_T_CE;
10006 else if (*T_DL != NUL && can_delete)
10007 type = USE_T_DL;
10008 else if (*T_CDL != NUL && can_delete)
10009 type = USE_T_CDL;
10010 else
10011 return FAIL;
10012
10013#ifdef FEAT_CLIPBOARD
10014 /* Remove a modeless selection when deleting lines halfway the screen or
10015 * not the full width of the screen. */
10016 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010017# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 || (wp != NULL && wp->w_width != Columns)
10019# endif
10020 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010021 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 else
10023 clip_scroll_selection(line_count);
10024#endif
10025
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026#ifdef FEAT_GUI
10027 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10028 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010029 gui_dont_update_cursor(gui.cursor_row >= row + off
10030 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031#endif
10032
10033 if (*T_CCS != NUL) /* cursor relative to region */
10034 {
10035 cursor_row = row;
10036 cursor_end = end;
10037 }
10038 else
10039 {
10040 cursor_row = row + off;
10041 cursor_end = end + off;
10042 }
10043
10044 /*
10045 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10046 * Clear the inserted lines in ScreenLines[].
10047 */
10048 row += off;
10049 end += off;
10050 for (i = 0; i < line_count; ++i)
10051 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010052#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 if (wp != NULL && wp->w_width != Columns)
10054 {
10055 /* need to copy part of a line */
10056 j = row + i;
10057 while ((j += line_count) <= end - 1)
10058 linecopy(j - line_count, j, wp);
10059 j -= line_count;
10060 if (can_clear((char_u *)" "))
10061 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10062 else
10063 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10064 LineWraps[j] = FALSE;
10065 }
10066 else
10067#endif
10068 {
10069 /* whole width, moving the line pointers is faster */
10070 j = row + i;
10071 temp = LineOffset[j];
10072 while ((j += line_count) <= end - 1)
10073 {
10074 LineOffset[j - line_count] = LineOffset[j];
10075 LineWraps[j - line_count] = LineWraps[j];
10076 }
10077 LineOffset[j - line_count] = temp;
10078 LineWraps[j - line_count] = FALSE;
10079 if (can_clear((char_u *)" "))
10080 lineclear(temp, (int)Columns);
10081 else
10082 lineinvalid(temp, (int)Columns);
10083 }
10084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
10086 screen_stop_highlight();
10087
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010088#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 /* redraw the characters */
10090 if (type == USE_REDRAW)
10091 redraw_block(row, end, wp);
10092 else
10093#endif
10094 if (type == USE_T_CD) /* delete the lines */
10095 {
10096 windgoto(cursor_row, 0);
10097 out_str(T_CD);
10098 screen_start(); /* don't know where cursor is now */
10099 }
10100 else if (type == USE_T_CDL)
10101 {
10102 windgoto(cursor_row, 0);
10103 term_delete_lines(line_count);
10104 screen_start(); /* don't know where cursor is now */
10105 }
10106 /*
10107 * Deleting lines at top of the screen or scroll region: Just scroll
10108 * the whole screen (scroll region) up by outputting newlines on the
10109 * last line.
10110 */
10111 else if (type == USE_NL)
10112 {
10113 windgoto(cursor_end - 1, 0);
10114 for (i = line_count; --i >= 0; )
10115 out_char('\n'); /* cursor will remain on same line */
10116 }
10117 else
10118 {
10119 for (i = line_count; --i >= 0; )
10120 {
10121 if (type == USE_T_DL)
10122 {
10123 windgoto(cursor_row, 0);
10124 out_str(T_DL); /* delete a line */
10125 }
10126 else /* type == USE_T_CE */
10127 {
10128 windgoto(cursor_row + i, 0);
10129 out_str(T_CE); /* erase a line */
10130 }
10131 screen_start(); /* don't know where cursor is now */
10132 }
10133 }
10134
10135 /*
10136 * If the 'db' flag is set, we need to clear the lines that have been
10137 * scrolled up at the bottom of the region.
10138 */
10139 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10140 {
10141 for (i = line_count; i > 0; --i)
10142 {
10143 windgoto(cursor_end - i, 0);
10144 out_str(T_CE); /* erase a line */
10145 screen_start(); /* don't know where cursor is now */
10146 }
10147 }
10148
10149#ifdef FEAT_GUI
10150 gui_can_update_cursor();
10151 if (gui.in_use)
10152 out_flush(); /* always flush after a scroll */
10153#endif
10154
10155 return OK;
10156}
10157
10158/*
10159 * show the current mode and ruler
10160 *
10161 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10162 * If clear_cmdline is FALSE there may be a message there that needs to be
10163 * cleared only if a mode is shown.
10164 * Return the length of the message (0 if no message).
10165 */
10166 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010167showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168{
10169 int need_clear;
10170 int length = 0;
10171 int do_mode;
10172 int attr;
10173 int nwr_save;
10174#ifdef FEAT_INS_EXPAND
10175 int sub_attr;
10176#endif
10177
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010178 do_mode = ((p_smd && msg_silent == 0)
10179 && ((State & INSERT)
10180 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010181 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 if (do_mode || Recording)
10183 {
10184 /*
10185 * Don't show mode right now, when not redrawing or inside a mapping.
10186 * Call char_avail() only when we are going to show something, because
10187 * it takes a bit of time.
10188 */
10189 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10190 {
10191 redraw_cmdline = TRUE; /* show mode later */
10192 return 0;
10193 }
10194
10195 nwr_save = need_wait_return;
10196
10197 /* wait a bit before overwriting an important message */
10198 check_for_delay(FALSE);
10199
10200 /* if the cmdline is more than one line high, erase top lines */
10201 need_clear = clear_cmdline;
10202 if (clear_cmdline && cmdline_row < Rows - 1)
10203 msg_clr_cmdline(); /* will reset clear_cmdline */
10204
10205 /* Position on the last line in the window, column 0 */
10206 msg_pos_mode();
10207 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010208 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 if (do_mode)
10210 {
10211 MSG_PUTS_ATTR("--", attr);
10212#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010213 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010214# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010215 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010216# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010217 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010218# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010219 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010220# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 MSG_PUTS_ATTR(" IM", attr);
10222# else
10223 MSG_PUTS_ATTR(" XIM", attr);
10224# endif
10225#endif
10226#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10227 if (gui.in_use)
10228 {
10229 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010230 {
10231 /* HANGUL */
10232 if (enc_utf8)
10233 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10234 else
10235 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 }
10238#endif
10239#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010240 /* CTRL-X in Insert mode */
10241 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 {
10243 /* These messages can get long, avoid a wrap in a narrow
10244 * window. Prefer showing edit_submode_extra. */
10245 length = (Rows - msg_row) * Columns - 3;
10246 if (edit_submode_extra != NULL)
10247 length -= vim_strsize(edit_submode_extra);
10248 if (length > 0)
10249 {
10250 if (edit_submode_pre != NULL)
10251 length -= vim_strsize(edit_submode_pre);
10252 if (length - vim_strsize(edit_submode) > 0)
10253 {
10254 if (edit_submode_pre != NULL)
10255 msg_puts_attr(edit_submode_pre, attr);
10256 msg_puts_attr(edit_submode, attr);
10257 }
10258 if (edit_submode_extra != NULL)
10259 {
10260 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10261 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010262 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 else
10264 sub_attr = attr;
10265 msg_puts_attr(edit_submode_extra, sub_attr);
10266 }
10267 }
10268 length = 0;
10269 }
10270 else
10271#endif
10272 {
10273#ifdef FEAT_VREPLACE
10274 if (State & VREPLACE_FLAG)
10275 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10276 else
10277#endif
10278 if (State & REPLACE_FLAG)
10279 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10280 else if (State & INSERT)
10281 {
10282#ifdef FEAT_RIGHTLEFT
10283 if (p_ri)
10284 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10285#endif
10286 MSG_PUTS_ATTR(_(" INSERT"), attr);
10287 }
10288 else if (restart_edit == 'I')
10289 MSG_PUTS_ATTR(_(" (insert)"), attr);
10290 else if (restart_edit == 'R')
10291 MSG_PUTS_ATTR(_(" (replace)"), attr);
10292 else if (restart_edit == 'V')
10293 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10294#ifdef FEAT_RIGHTLEFT
10295 if (p_hkmap)
10296 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10297# ifdef FEAT_FKMAP
10298 if (p_fkmap)
10299 MSG_PUTS_ATTR(farsi_text_5, attr);
10300# endif
10301#endif
10302#ifdef FEAT_KEYMAP
10303 if (State & LANGMAP)
10304 {
10305# ifdef FEAT_ARABIC
10306 if (curwin->w_p_arab)
10307 MSG_PUTS_ATTR(_(" Arabic"), attr);
10308 else
10309# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010310 if (get_keymap_str(curwin, (char_u *)" (%s)",
10311 NameBuff, MAXPATHL))
10312 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 }
10314#endif
10315 if ((State & INSERT) && p_paste)
10316 MSG_PUTS_ATTR(_(" (paste)"), attr);
10317
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 if (VIsual_active)
10319 {
10320 char *p;
10321
10322 /* Don't concatenate separate words to avoid translation
10323 * problems. */
10324 switch ((VIsual_select ? 4 : 0)
10325 + (VIsual_mode == Ctrl_V) * 2
10326 + (VIsual_mode == 'V'))
10327 {
10328 case 0: p = N_(" VISUAL"); break;
10329 case 1: p = N_(" VISUAL LINE"); break;
10330 case 2: p = N_(" VISUAL BLOCK"); break;
10331 case 4: p = N_(" SELECT"); break;
10332 case 5: p = N_(" SELECT LINE"); break;
10333 default: p = N_(" SELECT BLOCK"); break;
10334 }
10335 MSG_PUTS_ATTR(_(p), attr);
10336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 MSG_PUTS_ATTR(" --", attr);
10338 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010339
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 need_clear = TRUE;
10341 }
10342 if (Recording
10343#ifdef FEAT_INS_EXPAND
10344 && edit_submode == NULL /* otherwise it gets too long */
10345#endif
10346 )
10347 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010348 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 need_clear = TRUE;
10350 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010351
10352 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 if (need_clear || clear_cmdline)
10354 msg_clr_eos();
10355 msg_didout = FALSE; /* overwrite this message */
10356 length = msg_col;
10357 msg_col = 0;
10358 need_wait_return = nwr_save; /* never ask for hit-return for this */
10359 }
10360 else if (clear_cmdline && msg_silent == 0)
10361 /* Clear the whole command line. Will reset "clear_cmdline". */
10362 msg_clr_cmdline();
10363
10364#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 /* In Visual mode the size of the selected area must be redrawn. */
10366 if (VIsual_active)
10367 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368
10369 /* If the last window has no status line, the ruler is after the mode
10370 * message and must be redrawn */
10371 if (redrawing()
10372# ifdef FEAT_WINDOWS
10373 && lastwin->w_status_height == 0
10374# endif
10375 )
10376 win_redr_ruler(lastwin, TRUE);
10377#endif
10378 redraw_cmdline = FALSE;
10379 clear_cmdline = FALSE;
10380
10381 return length;
10382}
10383
10384/*
10385 * Position for a mode message.
10386 */
10387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010388msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389{
10390 msg_col = 0;
10391 msg_row = Rows - 1;
10392}
10393
10394/*
10395 * Delete mode message. Used when ESC is typed which is expected to end
10396 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010397 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 */
10399 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010400unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401{
10402 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010403 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 */
10405 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10406 redraw_cmdline = TRUE; /* delete mode later */
10407 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010408 clearmode();
10409}
10410
10411/*
10412 * Clear the mode message.
10413 */
10414 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010415clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010416{
10417 msg_pos_mode();
10418 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010419 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010420 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421}
10422
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010424recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010425{
10426 MSG_PUTS_ATTR(_("recording"), attr);
10427 if (!shortmess(SHM_RECORDING))
10428 {
10429 char_u s[4];
10430 sprintf((char *)s, " @%c", Recording);
10431 MSG_PUTS_ATTR(s, attr);
10432 }
10433}
10434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010435#if defined(FEAT_WINDOWS)
10436/*
10437 * Draw the tab pages line at the top of the Vim window.
10438 */
10439 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010440draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010441{
10442 int tabcount = 0;
10443 tabpage_T *tp;
10444 int tabwidth;
10445 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010446 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010447 int attr;
10448 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010449 win_T *cwp;
10450 int wincount;
10451 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010452 int c;
10453 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010454 int attr_sel = HL_ATTR(HLF_TPS);
10455 int attr_nosel = HL_ATTR(HLF_TP);
10456 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010457 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010458 int room;
10459 int use_sep_chars = (t_colors < 8
10460#ifdef FEAT_GUI
10461 && !gui.in_use
10462#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010463#ifdef FEAT_TERMGUICOLORS
10464 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010465#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010466 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010467
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010468 if (ScreenLines == NULL)
10469 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010470 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010471
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010472#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010473 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010474 if (gui_use_tabline())
10475 {
10476 gui_update_tabline();
10477 return;
10478 }
10479#endif
10480
10481 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482 return;
10483
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010484#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010485
10486 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10487 for (scol = 0; scol < Columns; ++scol)
10488 TabPageIdxs[scol] = 0;
10489
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010490 /* Use the 'tabline' option if it's set. */
10491 if (*p_tal != NUL)
10492 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010493 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010494
10495 /* Check for an error. If there is one we would loop in redrawing the
10496 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010497 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010498 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010499 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010500 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010501 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010502 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010503 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010504 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010505#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010506 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010507 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010508 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010509
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10511 if (tabwidth < 6)
10512 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010513
Bram Moolenaar238a5642006-02-21 22:12:05 +000010514 attr = attr_nosel;
10515 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010516 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010517 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10518 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010519 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010520 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010521
Bram Moolenaar238a5642006-02-21 22:12:05 +000010522 if (tp->tp_topframe == topframe)
10523 attr = attr_sel;
10524 if (use_sep_chars && col > 0)
10525 screen_putchar('|', 0, col++, attr);
10526
10527 if (tp->tp_topframe != topframe)
10528 attr = attr_nosel;
10529
10530 screen_putchar(' ', 0, col++, attr);
10531
10532 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010533 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010534 cwp = curwin;
10535 wp = firstwin;
10536 }
10537 else
10538 {
10539 cwp = tp->tp_curwin;
10540 wp = tp->tp_firstwin;
10541 }
10542
10543 modified = FALSE;
10544 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10545 if (bufIsChanged(wp->w_buffer))
10546 modified = TRUE;
10547 if (modified || wincount > 1)
10548 {
10549 if (wincount > 1)
10550 {
10551 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010552 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010553 if (col + len >= Columns - 3)
10554 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010555 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010556#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010557 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010558#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010559 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010560#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 );
10562 col += len;
10563 }
10564 if (modified)
10565 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10566 screen_putchar(' ', 0, col++, attr);
10567 }
10568
10569 room = scol - col + tabwidth - 1;
10570 if (room > 0)
10571 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010572 /* Get buffer name in NameBuff[] */
10573 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010574 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010575 len = vim_strsize(NameBuff);
10576 p = NameBuff;
10577#ifdef FEAT_MBYTE
10578 if (has_mbyte)
10579 while (len > room)
10580 {
10581 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010582 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010583 }
10584 else
10585#endif
10586 if (len > room)
10587 {
10588 p += len - room;
10589 len = room;
10590 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010591 if (len > Columns - col - 1)
10592 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010594 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010595 col += len;
10596 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010597 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010598
10599 /* Store the tab page number in TabPageIdxs[], so that
10600 * jump_to_mouse() knows where each one is. */
10601 ++tabcount;
10602 while (scol < col)
10603 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010604 }
10605
Bram Moolenaar238a5642006-02-21 22:12:05 +000010606 if (use_sep_chars)
10607 c = '_';
10608 else
10609 c = ' ';
10610 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010611
10612 /* Put an "X" for closing the current tab if there are several. */
10613 if (first_tabpage->tp_next != NULL)
10614 {
10615 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10616 TabPageIdxs[Columns - 1] = -999;
10617 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010618 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010619
10620 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10621 * set. */
10622 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010623}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010624
10625/*
10626 * Get buffer name for "buf" into NameBuff[].
10627 * Takes care of special buffer names and translates special characters.
10628 */
10629 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010630get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010631{
10632 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010633 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010634 else
10635 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10636 trans_characters(NameBuff, MAXPATHL);
10637}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010638#endif
10639
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10641/*
10642 * Get the character to use in a status line. Get its attributes in "*attr".
10643 */
10644 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010645fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647 int fill;
10648 if (is_curwin)
10649 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010650 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 fill = fill_stl;
10652 }
10653 else
10654 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010655 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 fill = fill_stlnc;
10657 }
10658 /* Use fill when there is highlighting, and highlighting of current
10659 * window differs, or the fillchars differ, or this is not the
10660 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010661 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010662 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 || (fill_stl != fill_stlnc)))
10664 return fill;
10665 if (is_curwin)
10666 return '^';
10667 return '=';
10668}
10669#endif
10670
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010671#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672/*
10673 * Get the character to use in a separator between vertically split windows.
10674 * Get its attributes in "*attr".
10675 */
10676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010677fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010679 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 if (*attr == 0 && fill_vert == ' ')
10681 return '|';
10682 else
10683 return fill_vert;
10684}
10685#endif
10686
10687/*
10688 * Return TRUE if redrawing should currently be done.
10689 */
10690 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010691redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010693#ifdef FEAT_EVAL
10694 if (disable_redraw_for_testing)
10695 return 0;
10696 else
10697#endif
10698 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10700}
10701
10702/*
10703 * Return TRUE if printing messages should currently be done.
10704 */
10705 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010706messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708 return (!(p_lz && char_avail() && !KeyTyped));
10709}
10710
10711/*
10712 * Show current status info in ruler and various other places
10713 * If always is FALSE, only show ruler if position has changed.
10714 */
10715 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010716showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 if (!always && !redrawing())
10719 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010720#ifdef FEAT_INS_EXPAND
10721 if (pum_visible())
10722 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010723# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010724 /* Don't redraw right now, do it later. */
10725 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010726# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010727 return;
10728 }
10729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010731 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010732 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010733 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735 else
10736#endif
10737#ifdef FEAT_CMDL_INFO
10738 win_redr_ruler(curwin, always);
10739#endif
10740
10741#ifdef FEAT_TITLE
10742 if (need_maketitle
10743# ifdef FEAT_STL_OPT
10744 || (p_icon && (stl_syntax & STL_IN_ICON))
10745 || (p_title && (stl_syntax & STL_IN_TITLE))
10746# endif
10747 )
10748 maketitle();
10749#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010750#ifdef FEAT_WINDOWS
10751 /* Redraw the tab pages line if needed. */
10752 if (redraw_tabline)
10753 draw_tabline();
10754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755}
10756
10757#ifdef FEAT_CMDL_INFO
10758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010759win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010761#define RULER_BUF_LEN 70
10762 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 int row;
10764 int fillchar;
10765 int attr;
10766 int empty_line = FALSE;
10767 colnr_T virtcol;
10768 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010769 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010771#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 int this_ru_col;
10773 int off = 0;
10774 int width = Columns;
10775# define WITH_OFF(x) x
10776# define WITH_WIDTH(x) x
10777#else
10778# define WITH_OFF(x) 0
10779# define WITH_WIDTH(x) Columns
10780# define this_ru_col ru_col
10781#endif
10782
10783 /* If 'ruler' off or redrawing disabled, don't do anything */
10784 if (!p_ru)
10785 return;
10786
10787 /*
10788 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10789 * after deleting lines, before cursor.lnum is corrected.
10790 */
10791 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10792 return;
10793
10794#ifdef FEAT_INS_EXPAND
10795 /* Don't draw the ruler while doing insert-completion, it might overwrite
10796 * the (long) mode message. */
10797# ifdef FEAT_WINDOWS
10798 if (wp == lastwin && lastwin->w_status_height == 0)
10799# endif
10800 if (edit_submode != NULL)
10801 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010802 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10803 if (pum_visible())
10804 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805#endif
10806
10807#ifdef FEAT_STL_OPT
10808 if (*p_ruf)
10809 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010810 int save_called_emsg = called_emsg;
10811
10812 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010814 if (called_emsg)
10815 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010816 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010817 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 return;
10819 }
10820#endif
10821
10822 /*
10823 * Check if not in Insert mode and the line is empty (will show "0-1").
10824 */
10825 if (!(State & INSERT)
10826 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10827 empty_line = TRUE;
10828
10829 /*
10830 * Only draw the ruler when something changed.
10831 */
10832 validate_virtcol_win(wp);
10833 if ( redraw_cmdline
10834 || always
10835 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10836 || wp->w_cursor.col != wp->w_ru_cursor.col
10837 || wp->w_virtcol != wp->w_ru_virtcol
10838#ifdef FEAT_VIRTUALEDIT
10839 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10840#endif
10841 || wp->w_topline != wp->w_ru_topline
10842 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10843#ifdef FEAT_DIFF
10844 || wp->w_topfill != wp->w_ru_topfill
10845#endif
10846 || empty_line != wp->w_ru_empty)
10847 {
10848 cursor_off();
10849#ifdef FEAT_WINDOWS
10850 if (wp->w_status_height)
10851 {
10852 row = W_WINROW(wp) + wp->w_height;
10853 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 off = W_WINCOL(wp);
10855 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 }
10857 else
10858#endif
10859 {
10860 row = Rows - 1;
10861 fillchar = ' ';
10862 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010863#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 width = Columns;
10865 off = 0;
10866#endif
10867 }
10868
10869 /* In list mode virtcol needs to be recomputed */
10870 virtcol = wp->w_virtcol;
10871 if (wp->w_p_list && lcs_tab1 == NUL)
10872 {
10873 wp->w_p_list = FALSE;
10874 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10875 wp->w_p_list = TRUE;
10876 }
10877
10878 /*
10879 * Some sprintfs return the length, some return a pointer.
10880 * To avoid portability problems we use strlen() here.
10881 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010882 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10884 ? 0L
10885 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010886 len = STRLEN(buffer);
10887 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10889 (int)virtcol + 1);
10890
10891 /*
10892 * Add a "50%" if there is room for it.
10893 * On the last line, don't print in the last column (scrolls the
10894 * screen up on some terminals).
10895 */
10896 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010897 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 o = i + vim_strsize(buffer + i + 1);
10899#ifdef FEAT_WINDOWS
10900 if (wp->w_status_height == 0) /* can't use last char of screen */
10901#endif
10902 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010903#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 this_ru_col = ru_col - (Columns - width);
10905 if (this_ru_col < 0)
10906 this_ru_col = 0;
10907#endif
10908 /* Never use more than half the window/screen width, leave the other
10909 * half for the filename. */
10910 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10911 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10912 if (this_ru_col + o < WITH_WIDTH(width))
10913 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010914 /* need at least 3 chars left for get_rel_pos() + NUL */
10915 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 {
10917#ifdef FEAT_MBYTE
10918 if (has_mbyte)
10919 i += (*mb_char2bytes)(fillchar, buffer + i);
10920 else
10921#endif
10922 buffer[i++] = fillchar;
10923 ++o;
10924 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010925 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 }
10927 /* Truncate at window boundary. */
10928#ifdef FEAT_MBYTE
10929 if (has_mbyte)
10930 {
10931 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010932 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 {
10934 o += (*mb_ptr2cells)(buffer + i);
10935 if (this_ru_col + o > WITH_WIDTH(width))
10936 {
10937 buffer[i] = NUL;
10938 break;
10939 }
10940 }
10941 }
10942 else
10943#endif
10944 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10945 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10946
10947 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10948 i = redraw_cmdline;
10949 screen_fill(row, row + 1,
10950 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10951 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10952 fillchar, fillchar, attr);
10953 /* don't redraw the cmdline because of showing the ruler */
10954 redraw_cmdline = i;
10955 wp->w_ru_cursor = wp->w_cursor;
10956 wp->w_ru_virtcol = wp->w_virtcol;
10957 wp->w_ru_empty = empty_line;
10958 wp->w_ru_topline = wp->w_topline;
10959 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10960#ifdef FEAT_DIFF
10961 wp->w_ru_topfill = wp->w_topfill;
10962#endif
10963 }
10964}
10965#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010966
10967#if defined(FEAT_LINEBREAK) || defined(PROTO)
10968/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010969 * Return the width of the 'number' and 'relativenumber' column.
10970 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010971 * Otherwise it depends on 'numberwidth' and the line count.
10972 */
10973 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010974number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010975{
10976 int n;
10977 linenr_T lnum;
10978
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010979 if (wp->w_p_rnu && !wp->w_p_nu)
10980 /* cursor line shows "0" */
10981 lnum = wp->w_height;
10982 else
10983 /* cursor line shows absolute line number */
10984 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010985
Bram Moolenaar6b314672015-03-20 15:42:10 +010010986 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010987 return wp->w_nrwidth_width;
10988 wp->w_nrwidth_line_count = lnum;
10989
10990 n = 0;
10991 do
10992 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010993 lnum /= 10;
10994 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010995 } while (lnum > 0);
10996
10997 /* 'numberwidth' gives the minimal width plus one */
10998 if (n < wp->w_p_nuw - 1)
10999 n = wp->w_p_nuw - 1;
11000
11001 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011002 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011003 return n;
11004}
11005#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011006
11007/*
11008 * Return the current cursor column. This is the actual position on the
11009 * screen. First column is 0.
11010 */
11011 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011012screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011013{
11014 return screen_cur_col;
11015}
11016
11017/*
11018 * Return the current cursor row. This is the actual position on the screen.
11019 * First row is 0.
11020 */
11021 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011022screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011023{
11024 return screen_cur_row;
11025}