blob: 5a360b5ff1ea3ed638873fcc5b86331e97f76c9c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200127static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200188#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
189/* Can limit syntax highlight time to 'redrawtime'. */
190# define SYN_TIME_LIMIT 1
191#endif
192
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193/*
194 * Redraw the current window later, with update_screen(type).
195 * Set must_redraw only if not already set to a higher value.
196 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
197 */
198 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100199redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 redraw_win_later(curwin, type);
202}
203
204 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100205redraw_win_later(
206 win_T *wp,
207 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 if (wp->w_redr_type < type)
210 {
211 wp->w_redr_type = type;
212 if (type >= NOT_VALID)
213 wp->w_lines_valid = 0;
214 if (must_redraw < type) /* must_redraw is the maximum of all windows */
215 must_redraw = type;
216 }
217}
218
219/*
220 * Force a complete redraw later. Also resets the highlighting. To be used
221 * after executing a shell command that messes up the screen.
222 */
223 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100224redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000227#ifdef FEAT_GUI
228 if (gui.in_use)
229 /* Use a code that will reset gui.highlight_mask in
230 * gui_stop_highlight(). */
231 screen_attr = HL_ALL + 1;
232 else
233#endif
234 /* Use attributes that is very unlikely to appear in text. */
235 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236}
237
238/*
239 * Mark all windows to be redrawn later.
240 */
241 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100242redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
244 win_T *wp;
245
246 FOR_ALL_WINDOWS(wp)
247 {
248 redraw_win_later(wp, type);
249 }
250}
251
252/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000253 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 */
255 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100256redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
258 redraw_buf_later(curbuf, type);
259}
260
261 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100262redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263{
264 win_T *wp;
265
266 FOR_ALL_WINDOWS(wp)
267 {
268 if (wp->w_buffer == buf)
269 redraw_win_later(wp, type);
270 }
271}
272
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200273 void
274redraw_buf_and_status_later(buf_T *buf, int type)
275{
276 win_T *wp;
277
Bram Moolenaar86033562017-07-12 20:24:41 +0200278 if (wild_menu_showing != 0)
279 /* Don't redraw while the command line completion is displayed, it
280 * would disappear. */
281 return;
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200282 FOR_ALL_WINDOWS(wp)
283 {
284 if (wp->w_buffer == buf)
285 {
286 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200287#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200288 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200289#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200290 }
291 }
292}
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 * Redraw as soon as possible. When the command line is not scrolled redraw
296 * right away and restore what was on the command line.
297 * Return a code indicating what happened.
298 */
299 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100300redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301{
302 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200303 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 int r;
305 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200306 schar_T *screenline; /* copy from ScreenLines[] */
307 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308#ifdef FEAT_MBYTE
309 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200312 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#endif
314
315 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200316 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 return ret;
318
319 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200322 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenline == NULL || screenattr == NULL)
326 ret = 2;
327#ifdef FEAT_MBYTE
328 if (enc_utf8)
329 {
330 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200331 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 if (screenlineUC == NULL)
333 ret = 2;
334 for (i = 0; i < p_mco; ++i)
335 {
336 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200337 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineC[i] == NULL)
339 ret = 2;
340 }
341 }
342 if (enc_dbcs == DBCS_JPNU)
343 {
344 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200345 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 if (screenline2 == NULL)
347 ret = 2;
348 }
349#endif
350
351 if (ret != 2)
352 {
353 /* Save the text displayed in the command line area. */
354 for (r = 0; r < rows; ++r)
355 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(schar_T));
359 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362#ifdef FEAT_MBYTE
363 if (enc_utf8)
364 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineC[i] + r * cols,
370 ScreenLinesC[i] + LineOffset[cmdline_row + r],
371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 }
373 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377#endif
378 }
379
380 update_screen(0);
381 ret = 3;
382
383 if (must_redraw == 0)
384 {
385 int off = (int)(current_ScreenLine - ScreenLines);
386
387 /* Restore the text displayed in the command line area. */
388 for (r = 0; r < rows; ++r)
389 {
390 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenline + r * cols,
392 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenattr + r * cols,
395 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396#ifdef FEAT_MBYTE
397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200412 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414 ret = 4;
415 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416 }
417
418 vim_free(screenline);
419 vim_free(screenattr);
420#ifdef FEAT_MBYTE
421 if (enc_utf8)
422 {
423 vim_free(screenlineUC);
424 for (i = 0; i < p_mco; ++i)
425 vim_free(screenlineC[i]);
426 }
427 if (enc_dbcs == DBCS_JPNU)
428 vim_free(screenline2);
429#endif
430
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200431 /* Show the intro message when appropriate. */
432 maybe_intro_message();
433
434 setcursor();
435
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436 return ret;
437}
438
439/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 * Invoked after an asynchronous callback is called.
441 * If an echo command was used the cursor needs to be put back where
442 * it belongs. If highlighting was changed a redraw is needed.
443 */
444 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200445redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100447 if (State == HITRETURN || State == ASKMORE)
448 ; /* do nothing */
449 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200450 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200451 /* Redrawing only works when the screen didn't scroll. Don't clear
452 * wildmenu entries. */
453 if (msg_scrolled == 0 && wild_menu_showing == 0)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200454 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200455 /* Redraw in the same position, so that the user can continue
456 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200457 redrawcmdline_ex(FALSE);
458 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200459 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100460 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200461 /* keep the command line if possible */
462 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100463 setcursor();
464 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100465 cursor_on();
466 out_flush();
467#ifdef FEAT_GUI
468 if (gui.in_use)
469 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200470 /* Don't update the cursor when it is blinking and off to avoid
471 * flicker. */
472 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200473 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474 gui_mch_flush();
475 }
476#endif
477}
478
479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 * Changed something in the current window, at buffer line "lnum", that
481 * requires that line and possibly other lines to be redrawn.
482 * Used when entering/leaving Insert mode with the cursor on a folded line.
483 * Used to remove the "$" from a change command.
484 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
485 * may become invalid and the whole window will have to be redrawn.
486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100488redrawWinline(
489 linenr_T lnum,
490 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492#ifdef FEAT_FOLDING
493 int i;
494#endif
495
496 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
497 curwin->w_redraw_top = lnum;
498 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
499 curwin->w_redraw_bot = lnum;
500 redraw_later(VALID);
501
502#ifdef FEAT_FOLDING
503 if (invalid)
504 {
505 /* A w_lines[] entry for this lnum has become invalid. */
506 i = find_wl_entry(curwin, lnum);
507 if (i >= 0)
508 curwin->w_lines[i].wl_valid = FALSE;
509 }
510#endif
511}
512
513/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200514 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 */
516 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100517update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 redraw_curbuf_later(type);
520 update_screen(type);
521}
522
523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 * Based on the current value of curwin->w_topline, transfer a screenfull
525 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
526 */
527 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200528update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200530 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 win_T *wp;
532 static int did_intro = FALSE;
533#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
534 int did_one;
535#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200536#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200537 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200538 int gui_cursor_col;
539 int gui_cursor_row;
540#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200541 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000543 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (!screen_valid(TRUE))
545 return;
546
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547 if (type == VALID_NO_UPDATE)
548 {
549 no_update = TRUE;
550 type = 0;
551 }
552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (must_redraw)
554 {
555 if (type < must_redraw) /* use maximal type */
556 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000557
558 /* must_redraw is reset here, so that when we run into some weird
559 * reason to redraw while busy redrawing (e.g., asynchronous
560 * scrolling), or update_topline() in win_update() will cause a
561 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 must_redraw = 0;
563 }
564
565 /* Need to update w_lines[]. */
566 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
567 type = NOT_VALID;
568
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000569 /* Postpone the redrawing when it's not needed and when being called
570 * recursively. */
571 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 redraw_later(type); /* remember type for next time */
574 must_redraw = type;
575 if (type > INVERTED_ALL)
576 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
577 return;
578 }
579
580 updating_screen = TRUE;
581#ifdef FEAT_SYN_HL
582 ++display_tick; /* let syntax code know we're in a next round of
583 * display updating */
584#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200585 if (no_update)
586 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 /*
589 * if the screen was scrolled up when displaying a message, scroll it down
590 */
591 if (msg_scrolled)
592 {
593 clear_cmdline = TRUE;
594 if (msg_scrolled > Rows - 5) /* clearing is faster */
595 type = CLEAR;
596 else if (type != CLEAR)
597 {
598 check_for_delay(FALSE);
599 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
600 type = CLEAR;
601 FOR_ALL_WINDOWS(wp)
602 {
603 if (W_WINROW(wp) < msg_scrolled)
604 {
605 if (W_WINROW(wp) + wp->w_height > msg_scrolled
606 && wp->w_redr_type < REDRAW_TOP
607 && wp->w_lines_valid > 0
608 && wp->w_topline == wp->w_lines[0].wl_lnum)
609 {
610 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
611 wp->w_redr_type = REDRAW_TOP;
612 }
613 else
614 {
615 wp->w_redr_type = NOT_VALID;
616#ifdef FEAT_WINDOWS
617 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
618 <= msg_scrolled)
619 wp->w_redr_status = TRUE;
620#endif
621 }
622 }
623 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200624 if (!no_update)
625 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000626#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000627 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
630 msg_scrolled = 0;
631 need_wait_return = FALSE;
632 }
633
634 /* reset cmdline_row now (may have been changed temporarily) */
635 compute_cmdrow();
636
637 /* Check for changed highlighting */
638 if (need_highlight_changed)
639 highlight_changed();
640
641 if (type == CLEAR) /* first clear screen */
642 {
643 screenclear(); /* will reset clear_cmdline */
644 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200645 /* must_redraw may be set indirectly, avoid another redraw later */
646 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648
649 if (clear_cmdline) /* going to clear cmdline (done below) */
650 check_for_delay(FALSE);
651
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000652#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200653 /* Force redraw when width of 'number' or 'relativenumber' column
654 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000655 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200656 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
657 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000658 curwin->w_redr_type = NOT_VALID;
659#endif
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 /*
662 * Only start redrawing if there is really something to do.
663 */
664 if (type == INVERTED)
665 update_curswant();
666 if (curwin->w_redr_type < type
667 && !((type == VALID
668 && curwin->w_lines[0].wl_valid
669#ifdef FEAT_DIFF
670 && curwin->w_topfill == curwin->w_old_topfill
671 && curwin->w_botfill == curwin->w_old_botfill
672#endif
673 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000675 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
677 && curwin->w_old_visual_mode == VIsual_mode
678 && (curwin->w_valid & VALID_VIRTCOL)
679 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 ))
681 curwin->w_redr_type = type;
682
Bram Moolenaar5a305422006-04-28 22:38:25 +0000683#ifdef FEAT_WINDOWS
684 /* Redraw the tab pages line if needed. */
685 if (redraw_tabline || type >= NOT_VALID)
686 draw_tabline();
687#endif
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_SYN_HL
690 /*
691 * Correct stored syntax highlighting info for changes in each displayed
692 * buffer. Each buffer must only be done once.
693 */
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer->b_mod_set)
697 {
698# ifdef FEAT_WINDOWS
699 win_T *wwp;
700
701 /* Check if we already did this buffer. */
702 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
703 if (wwp->w_buffer == wp->w_buffer)
704 break;
705# endif
706 if (
707# ifdef FEAT_WINDOWS
708 wwp == wp &&
709# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200710 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 syn_stack_apply_changes(wp->w_buffer);
712 }
713 }
714#endif
715
716 /*
717 * Go from top to bottom through the windows, redrawing the ones that need
718 * it.
719 */
720#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
721 did_one = FALSE;
722#endif
723#ifdef FEAT_SEARCH_EXTRA
724 search_hl.rm.regprog = NULL;
725#endif
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_redr_type != 0)
729 {
730 cursor_off();
731#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
732 if (!did_one)
733 {
734 did_one = TRUE;
735# ifdef FEAT_SEARCH_EXTRA
736 start_search_hl();
737# endif
738# ifdef FEAT_CLIPBOARD
739 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200740 if (clip_star.available && clip_isautosel_star())
741 clip_update_selection(&clip_star);
742 if (clip_plus.available && clip_isautosel_plus())
743 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744# endif
745#ifdef FEAT_GUI
746 /* Remove the cursor before starting to do anything, because
747 * scrolling may make it difficult to redraw the text under
748 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200749 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200750 {
751 gui_cursor_col = gui.cursor_col;
752 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200754 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#endif
757 }
758#endif
759 win_update(wp);
760 }
761
762#ifdef FEAT_WINDOWS
763 /* redraw status line after the window to minimize cursor movement */
764 if (wp->w_redr_status)
765 {
766 cursor_off();
767 win_redr_status(wp);
768 }
769#endif
770 }
771#if defined(FEAT_SEARCH_EXTRA)
772 end_search_hl();
773#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100774#ifdef FEAT_INS_EXPAND
775 /* May need to redraw the popup menu. */
776 if (pum_visible())
777 pum_redraw();
778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780#ifdef FEAT_WINDOWS
781 /* Reset b_mod_set flags. Going through all windows is probably faster
782 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200783 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 wp->w_buffer->b_mod_set = FALSE;
785#else
786 curbuf->b_mod_set = FALSE;
787#endif
788
789 updating_screen = FALSE;
790#ifdef FEAT_GUI
791 gui_may_resize_shell();
792#endif
793
794 /* Clear or redraw the command line. Done last, because scrolling may
795 * mess up the command line. */
796 if (clear_cmdline || redraw_cmdline)
797 showmode();
798
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200799 if (no_update)
800 --no_win_do_lines_ins;
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200803 if (!did_intro)
804 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 did_intro = TRUE;
806
807#ifdef FEAT_GUI
808 /* Redraw the cursor and update the scrollbars when all screen updating is
809 * done. */
810 if (gui.in_use)
811 {
812 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200813 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200814 {
815 /* Put the GUI position where the cursor was, gui_update_cursor()
816 * uses that. */
817 gui.col = gui_cursor_col;
818 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200819# ifdef FEAT_MBYTE
820 gui.col = mb_fix_col(gui.col, gui.row);
821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200823 screen_cur_col = gui.col;
824 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 gui_update_scrollbars(FALSE);
827 }
828#endif
829}
830
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100831#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
832/*
833 * Prepare for updating one or more windows.
834 * Caller must check for "updating_screen" already set to avoid recursiveness.
835 */
836 static void
837update_prepare(void)
838{
839 cursor_off();
840 updating_screen = TRUE;
841#ifdef FEAT_GUI
842 /* Remove the cursor before starting to do anything, because scrolling may
843 * make it difficult to redraw the text under it. */
844 if (gui.in_use)
845 gui_undraw_cursor();
846#endif
847#ifdef FEAT_SEARCH_EXTRA
848 start_search_hl();
849#endif
850}
851
852/*
853 * Finish updating one or more windows.
854 */
855 static void
856update_finish(void)
857{
858 if (redraw_cmdline)
859 showmode();
860
861# ifdef FEAT_SEARCH_EXTRA
862 end_search_hl();
863# endif
864
865 updating_screen = FALSE;
866
867# ifdef FEAT_GUI
868 gui_may_resize_shell();
869
870 /* Redraw the cursor and update the scrollbars when all screen updating is
871 * done. */
872 if (gui.in_use)
873 {
874 out_flush(); /* required before updating the cursor */
875 gui_update_cursor(FALSE, FALSE);
876 gui_update_scrollbars(FALSE);
877 }
878# endif
879}
880#endif
881
Bram Moolenaar860cae12010-06-05 23:22:07 +0200882#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200883/*
884 * Return TRUE if the cursor line in window "wp" may be concealed, according
885 * to the 'concealcursor' option.
886 */
887 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100888conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200889{
890 int c;
891
892 if (*wp->w_p_cocu == NUL)
893 return FALSE;
894 if (get_real_state() & VISUAL)
895 c = 'v';
896 else if (State & INSERT)
897 c = 'i';
898 else if (State & NORMAL)
899 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200900 else if (State & CMDLINE)
901 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200902 else
903 return FALSE;
904 return vim_strchr(wp->w_p_cocu, c) != NULL;
905}
906
907/*
908 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
909 */
910 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100911conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200912{
913 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
914 {
915 need_cursor_line_redraw = TRUE;
916 /* Need to recompute cursor column, e.g., when starting Visual mode
917 * without concealing. */
918 curs_columns(TRUE);
919 }
920}
921
Bram Moolenaar860cae12010-06-05 23:22:07 +0200922 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100923update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200924{
925 int row;
926 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200927#ifdef SYN_TIME_LIMIT
928 proftime_T syntax_tm;
929#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930
Bram Moolenaar908be432016-05-24 10:51:30 +0200931 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100932 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200933 return;
934
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200936 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200937 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200938#ifdef SYN_TIME_LIMIT
939 /* Set the time limit to 'redrawtime'. */
940 profile_setlimit(p_rdt, &syntax_tm);
941#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100942 update_prepare();
943
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944 row = 0;
945 for (j = 0; j < wp->w_lines_valid; ++j)
946 {
947 if (lnum == wp->w_lines[j].wl_lnum)
948 {
949 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200950# ifdef FEAT_SEARCH_EXTRA
951 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200952 start_search_hl();
953 prepare_search_hl(wp, lnum);
954# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200955 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
956#ifdef SYN_TIME_LIMIT
957 &syntax_tm
958#else
959 NULL
960#endif
961 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200962# if defined(FEAT_SEARCH_EXTRA)
963 end_search_hl();
964# endif
965 break;
966 }
967 row += wp->w_lines[j].wl_size;
968 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100969
970 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200971 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200972 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974#endif
975
976#if defined(FEAT_SIGNS) || defined(PROTO)
977 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100978update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
980 win_T *wp;
981 int doit = FALSE;
982
983# ifdef FEAT_FOLDING
984 win_foldinfo.fi_level = 0;
985# endif
986
987 /* update/delete a specific mark */
988 FOR_ALL_WINDOWS(wp)
989 {
990 if (buf != NULL && lnum > 0)
991 {
992 if (wp->w_buffer == buf && lnum >= wp->w_topline
993 && lnum < wp->w_botline)
994 {
995 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
996 wp->w_redraw_top = lnum;
997 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
998 wp->w_redraw_bot = lnum;
999 redraw_win_later(wp, VALID);
1000 }
1001 }
1002 else
1003 redraw_win_later(wp, VALID);
1004 if (wp->w_redr_type != 0)
1005 doit = TRUE;
1006 }
1007
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001008 /* Return when there is nothing to do, screen updating is already
1009 * happening (recursive call) or still starting up. */
1010 if (!doit || updating_screen
1011#ifdef FEAT_GUI
1012 || gui.starting
1013#endif
1014 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return;
1016
1017 /* update all windows that need updating */
1018 update_prepare();
1019
1020# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001021 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 if (wp->w_redr_type != 0)
1024 win_update(wp);
1025 if (wp->w_redr_status)
1026 win_redr_status(wp);
1027 }
1028# else
1029 if (curwin->w_redr_type != 0)
1030 win_update(curwin);
1031# endif
1032
1033 update_finish();
1034}
1035#endif
1036
1037
1038#if defined(FEAT_GUI) || defined(PROTO)
1039/*
1040 * Update a single window, its status line and maybe the command line msg.
1041 * Used for the GUI scrollbar.
1042 */
1043 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001044updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001046 /* return if already busy updating */
1047 if (updating_screen)
1048 return;
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 update_prepare();
1051
1052#ifdef FEAT_CLIPBOARD
1053 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001054 if (clip_star.available && clip_isautosel_star())
1055 clip_update_selection(&clip_star);
1056 if (clip_plus.available && clip_isautosel_plus())
1057 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001063 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001064 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001065 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (wp->w_redr_status
1068# ifdef FEAT_CMDL_INFO
1069 || p_ru
1070# endif
1071# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001072 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073# endif
1074 )
1075 win_redr_status(wp);
1076#endif
1077
1078 update_finish();
1079}
1080#endif
1081
1082/*
1083 * Update a single window.
1084 *
1085 * This may cause the windows below it also to be redrawn (when clearing the
1086 * screen or scrolling lines).
1087 *
1088 * How the window is redrawn depends on wp->w_redr_type. Each type also
1089 * implies the one below it.
1090 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001091 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1093 * INVERTED redraw the changed part of the Visual area
1094 * INVERTED_ALL redraw the whole Visual area
1095 * VALID 1. scroll up/down to adjust for a changed w_topline
1096 * 2. update lines at the top when scrolled down
1097 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001098 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * b_mod_top and b_mod_bot.
1100 * - if wp->w_redraw_top non-zero, redraw lines between
1101 * wp->w_redraw_top and wp->w_redr_bot.
1102 * - continue redrawing when syntax status is invalid.
1103 * 4. if scrolled up, update lines at the bottom.
1104 * This results in three areas that may need updating:
1105 * top: from first row to top_end (when scrolled down)
1106 * mid: from mid_start to mid_end (update inversion or changed text)
1107 * bot: from bot_start to last row (when scrolled up)
1108 */
1109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001110win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 buf_T *buf = wp->w_buffer;
1113 int type;
1114 int top_end = 0; /* Below last row of the top area that needs
1115 updating. 0 when no top area updating. */
1116 int mid_start = 999;/* first row of the mid area that needs
1117 updating. 999 when no mid area updating. */
1118 int mid_end = 0; /* Below last row of the mid area that needs
1119 updating. 0 when no mid area updating. */
1120 int bot_start = 999;/* first row of the bot area that needs
1121 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int scrolled_down = FALSE; /* TRUE when scrolled down when
1123 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001125 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 int top_to_mod = FALSE; /* redraw above mod_top */
1127#endif
1128
1129 int row; /* current window row to display */
1130 linenr_T lnum; /* current buffer lnum to display */
1131 int idx; /* current index in w_lines[] */
1132 int srow; /* starting row of the current line */
1133
1134 int eof = FALSE; /* if TRUE, we hit the end of the file */
1135 int didline = FALSE; /* if TRUE, we finished the last line */
1136 int i;
1137 long j;
1138 static int recursive = FALSE; /* being called recursively */
1139 int old_botline = wp->w_botline;
1140#ifdef FEAT_FOLDING
1141 long fold_count;
1142#endif
1143#ifdef FEAT_SYN_HL
1144 /* remember what happened to the previous line, to know if
1145 * check_visual_highlight() can be used */
1146#define DID_NONE 1 /* didn't update a line */
1147#define DID_LINE 2 /* updated a normal line */
1148#define DID_FOLD 3 /* updated a folded line */
1149 int did_update = DID_NONE;
1150 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1151#endif
1152 linenr_T mod_top = 0;
1153 linenr_T mod_bot = 0;
1154#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1155 int save_got_int;
1156#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001157#ifdef SYN_TIME_LIMIT
1158 proftime_T syntax_tm;
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 type = wp->w_redr_type;
1162
1163 if (type == NOT_VALID)
1164 {
1165#ifdef FEAT_WINDOWS
1166 wp->w_redr_status = TRUE;
1167#endif
1168 wp->w_lines_valid = 0;
1169 }
1170
1171 /* Window is zero-height: nothing to draw. */
1172 if (wp->w_height == 0)
1173 {
1174 wp->w_redr_type = 0;
1175 return;
1176 }
1177
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001178#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 /* Window is zero-width: Only need to draw the separator. */
1180 if (wp->w_width == 0)
1181 {
1182 /* draw the vertical separator right of this window */
1183 draw_vsep_win(wp, 0);
1184 wp->w_redr_type = 0;
1185 return;
1186 }
1187#endif
1188
1189#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001190 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001193#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001194 /* Force redraw when width of 'number' or 'relativenumber' column
1195 * changes. */
1196 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001197 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001198 {
1199 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001200 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001201 }
1202 else
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1206 {
1207 /*
1208 * When there are both inserted/deleted lines and specific lines to be
1209 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1210 * everything (only happens when redrawing is off for while).
1211 */
1212 type = NOT_VALID;
1213 }
1214 else
1215 {
1216 /*
1217 * Set mod_top to the first line that needs displaying because of
1218 * changes. Set mod_bot to the first line after the changes.
1219 */
1220 mod_top = wp->w_redraw_top;
1221 if (wp->w_redraw_bot != 0)
1222 mod_bot = wp->w_redraw_bot + 1;
1223 else
1224 mod_bot = 0;
1225 wp->w_redraw_top = 0; /* reset for next time */
1226 wp->w_redraw_bot = 0;
1227 if (buf->b_mod_set)
1228 {
1229 if (mod_top == 0 || mod_top > buf->b_mod_top)
1230 {
1231 mod_top = buf->b_mod_top;
1232#ifdef FEAT_SYN_HL
1233 /* Need to redraw lines above the change that may be included
1234 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001235 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001237 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (mod_top < 1)
1239 mod_top = 1;
1240 }
1241#endif
1242 }
1243 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1244 mod_bot = buf->b_mod_bot;
1245
1246#ifdef FEAT_SEARCH_EXTRA
1247 /* When 'hlsearch' is on and using a multi-line search pattern, a
1248 * change in one line may make the Search highlighting in a
1249 * previous line invalid. Simple solution: redraw all visible
1250 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001251 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001253 if (search_hl.rm.regprog != NULL
1254 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001256 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 {
1258 cur = wp->w_match_head;
1259 while (cur != NULL)
1260 {
1261 if (cur->match.regprog != NULL
1262 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001263 {
1264 top_to_mod = TRUE;
1265 break;
1266 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 cur = cur->next;
1268 }
1269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#endif
1271 }
1272#ifdef FEAT_FOLDING
1273 if (mod_top != 0 && hasAnyFolding(wp))
1274 {
1275 linenr_T lnumt, lnumb;
1276
1277 /*
1278 * A change in a line can cause lines above it to become folded or
1279 * unfolded. Find the top most buffer line that may be affected.
1280 * If the line was previously folded and displayed, get the first
1281 * line of that fold. If the line is folded now, get the first
1282 * folded line. Use the minimum of these two.
1283 */
1284
1285 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1286 * the line below it. If there is no valid entry, use w_topline.
1287 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1288 * to this line. If there is no valid entry, use MAXLNUM. */
1289 lnumt = wp->w_topline;
1290 lnumb = MAXLNUM;
1291 for (i = 0; i < wp->w_lines_valid; ++i)
1292 if (wp->w_lines[i].wl_valid)
1293 {
1294 if (wp->w_lines[i].wl_lastlnum < mod_top)
1295 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1296 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1297 {
1298 lnumb = wp->w_lines[i].wl_lnum;
1299 /* When there is a fold column it might need updating
1300 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001301 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 ++lnumb;
1303 }
1304 }
1305
1306 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1307 if (mod_top > lnumt)
1308 mod_top = lnumt;
1309
1310 /* Now do the same for the bottom line (one above mod_bot). */
1311 --mod_bot;
1312 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1313 ++mod_bot;
1314 if (mod_bot < lnumb)
1315 mod_bot = lnumb;
1316 }
1317#endif
1318
1319 /* When a change starts above w_topline and the end is below
1320 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001321 * If the end of the change is above w_topline: do like no change was
1322 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (mod_top != 0 && mod_top < wp->w_topline)
1324 {
1325 if (mod_bot > wp->w_topline)
1326 mod_top = wp->w_topline;
1327#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001328 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 top_end = 1;
1330#endif
1331 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001332
1333 /* When line numbers are displayed need to redraw all lines below
1334 * inserted/deleted lines. */
1335 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1336 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338
1339 /*
1340 * When only displaying the lines at the top, set top_end. Used when
1341 * window has scrolled down for msg_scrolled.
1342 */
1343 if (type == REDRAW_TOP)
1344 {
1345 j = 0;
1346 for (i = 0; i < wp->w_lines_valid; ++i)
1347 {
1348 j += wp->w_lines[i].wl_size;
1349 if (j >= wp->w_upd_rows)
1350 {
1351 top_end = j;
1352 break;
1353 }
1354 }
1355 if (top_end == 0)
1356 /* not found (cannot happen?): redraw everything */
1357 type = NOT_VALID;
1358 else
1359 /* top area defined, the rest is VALID */
1360 type = VALID;
1361 }
1362
Bram Moolenaar367329b2007-08-30 11:53:22 +00001363 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001364 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1365 * non-zero and thus not FALSE) will indicate that screenclear() was not
1366 * called. */
1367 if (screen_cleared)
1368 screen_cleared = MAYBE;
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /*
1371 * If there are no changes on the screen that require a complete redraw,
1372 * handle three cases:
1373 * 1: we are off the top of the screen by a few lines: scroll down
1374 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1375 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1376 * w_lines[] that needs updating.
1377 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001378 if ((type == VALID || type == SOME_VALID
1379 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#ifdef FEAT_DIFF
1381 && !wp->w_botfill && !wp->w_old_botfill
1382#endif
1383 )
1384 {
1385 if (mod_top != 0 && wp->w_topline == mod_top)
1386 {
1387 /*
1388 * w_topline is the first changed line, the scrolling will be done
1389 * further down.
1390 */
1391 }
1392 else if (wp->w_lines[0].wl_valid
1393 && (wp->w_topline < wp->w_lines[0].wl_lnum
1394#ifdef FEAT_DIFF
1395 || (wp->w_topline == wp->w_lines[0].wl_lnum
1396 && wp->w_topfill > wp->w_old_topfill)
1397#endif
1398 ))
1399 {
1400 /*
1401 * New topline is above old topline: May scroll down.
1402 */
1403#ifdef FEAT_FOLDING
1404 if (hasAnyFolding(wp))
1405 {
1406 linenr_T ln;
1407
1408 /* count the number of lines we are off, counting a sequence
1409 * of folded lines as one */
1410 j = 0;
1411 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1412 {
1413 ++j;
1414 if (j >= wp->w_height - 2)
1415 break;
1416 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1417 }
1418 }
1419 else
1420#endif
1421 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1422 if (j < wp->w_height - 2) /* not too far off */
1423 {
1424 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1425#ifdef FEAT_DIFF
1426 /* insert extra lines for previously invisible filler lines */
1427 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1428 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1429 - wp->w_old_topfill;
1430#endif
1431 if (i < wp->w_height - 2) /* less than a screen off */
1432 {
1433 /*
1434 * Try to insert the correct number of lines.
1435 * If not the last window, delete the lines at the bottom.
1436 * win_ins_lines may fail when the terminal can't do it.
1437 */
1438 if (i > 0)
1439 check_for_delay(FALSE);
1440 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1441 {
1442 if (wp->w_lines_valid != 0)
1443 {
1444 /* Need to update rows that are new, stop at the
1445 * first one that scrolled down. */
1446 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 /* Move the entries that were scrolled, disable
1450 * the entries for the lines to be redrawn. */
1451 if ((wp->w_lines_valid += j) > wp->w_height)
1452 wp->w_lines_valid = wp->w_height;
1453 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1454 wp->w_lines[idx] = wp->w_lines[idx - j];
1455 while (idx >= 0)
1456 wp->w_lines[idx--].wl_valid = FALSE;
1457 }
1458 }
1459 else
1460 mid_start = 0; /* redraw all lines */
1461 }
1462 else
1463 mid_start = 0; /* redraw all lines */
1464 }
1465 else
1466 mid_start = 0; /* redraw all lines */
1467 }
1468 else
1469 {
1470 /*
1471 * New topline is at or below old topline: May scroll up.
1472 * When topline didn't change, find first entry in w_lines[] that
1473 * needs updating.
1474 */
1475
1476 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1477 j = -1;
1478 row = 0;
1479 for (i = 0; i < wp->w_lines_valid; i++)
1480 {
1481 if (wp->w_lines[i].wl_valid
1482 && wp->w_lines[i].wl_lnum == wp->w_topline)
1483 {
1484 j = i;
1485 break;
1486 }
1487 row += wp->w_lines[i].wl_size;
1488 }
1489 if (j == -1)
1490 {
1491 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1492 * lines */
1493 mid_start = 0;
1494 }
1495 else
1496 {
1497 /*
1498 * Try to delete the correct number of lines.
1499 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1500 */
1501#ifdef FEAT_DIFF
1502 /* If the topline didn't change, delete old filler lines,
1503 * otherwise delete filler lines of the new topline... */
1504 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1505 row += wp->w_old_topfill;
1506 else
1507 row += diff_check_fill(wp, wp->w_topline);
1508 /* ... but don't delete new filler lines. */
1509 row -= wp->w_topfill;
1510#endif
1511 if (row > 0)
1512 {
1513 check_for_delay(FALSE);
1514 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1515 bot_start = wp->w_height - row;
1516 else
1517 mid_start = 0; /* redraw all lines */
1518 }
1519 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1520 {
1521 /*
1522 * Skip the lines (below the deleted lines) that are still
1523 * valid and don't need redrawing. Copy their info
1524 * upwards, to compensate for the deleted lines. Set
1525 * bot_start to the first row that needs redrawing.
1526 */
1527 bot_start = 0;
1528 idx = 0;
1529 for (;;)
1530 {
1531 wp->w_lines[idx] = wp->w_lines[j];
1532 /* stop at line that didn't fit, unless it is still
1533 * valid (no lines deleted) */
1534 if (row > 0 && bot_start + row
1535 + (int)wp->w_lines[j].wl_size > wp->w_height)
1536 {
1537 wp->w_lines_valid = idx + 1;
1538 break;
1539 }
1540 bot_start += wp->w_lines[idx++].wl_size;
1541
1542 /* stop at the last valid entry in w_lines[].wl_size */
1543 if (++j >= wp->w_lines_valid)
1544 {
1545 wp->w_lines_valid = idx;
1546 break;
1547 }
1548 }
1549#ifdef FEAT_DIFF
1550 /* Correct the first entry for filler lines at the top
1551 * when it won't get updated below. */
1552 if (wp->w_p_diff && bot_start > 0)
1553 wp->w_lines[0].wl_size =
1554 plines_win_nofill(wp, wp->w_topline, TRUE)
1555 + wp->w_topfill;
1556#endif
1557 }
1558 }
1559 }
1560
1561 /* When starting redraw in the first line, redraw all lines. When
1562 * there is only one window it's probably faster to clear the screen
1563 * first. */
1564 if (mid_start == 0)
1565 {
1566 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001567 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001568 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001569 /* Clear the screen when it was not done by win_del_lines() or
1570 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1571 * then. */
1572 if (screen_cleared != TRUE)
1573 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001574#ifdef FEAT_WINDOWS
1575 /* The screen was cleared, redraw the tab pages line. */
1576 if (redraw_tabline)
1577 draw_tabline();
1578#endif
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001581
1582 /* When win_del_lines() or win_ins_lines() caused the screen to be
1583 * cleared (only happens for the first window) or when screenclear()
1584 * was called directly above, "must_redraw" will have been set to
1585 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1586 if (screen_cleared == TRUE)
1587 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 else
1590 {
1591 /* Not VALID or INVERTED: redraw all lines. */
1592 mid_start = 0;
1593 mid_end = wp->w_height;
1594 }
1595
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001596 if (type == SOME_VALID)
1597 {
1598 /* SOME_VALID: redraw all lines. */
1599 mid_start = 0;
1600 mid_end = wp->w_height;
1601 type = NOT_VALID;
1602 }
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 /* check if we are updating or removing the inverted part */
1605 if ((VIsual_active && buf == curwin->w_buffer)
1606 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1607 {
1608 linenr_T from, to;
1609
1610 if (VIsual_active)
1611 {
1612 if (VIsual_active
1613 && (VIsual_mode != wp->w_old_visual_mode
1614 || type == INVERTED_ALL))
1615 {
1616 /*
1617 * If the type of Visual selection changed, redraw the whole
1618 * selection. Also when the ownership of the X selection is
1619 * gained or lost.
1620 */
1621 if (curwin->w_cursor.lnum < VIsual.lnum)
1622 {
1623 from = curwin->w_cursor.lnum;
1624 to = VIsual.lnum;
1625 }
1626 else
1627 {
1628 from = VIsual.lnum;
1629 to = curwin->w_cursor.lnum;
1630 }
1631 /* redraw more when the cursor moved as well */
1632 if (wp->w_old_cursor_lnum < from)
1633 from = wp->w_old_cursor_lnum;
1634 if (wp->w_old_cursor_lnum > to)
1635 to = wp->w_old_cursor_lnum;
1636 if (wp->w_old_visual_lnum < from)
1637 from = wp->w_old_visual_lnum;
1638 if (wp->w_old_visual_lnum > to)
1639 to = wp->w_old_visual_lnum;
1640 }
1641 else
1642 {
1643 /*
1644 * Find the line numbers that need to be updated: The lines
1645 * between the old cursor position and the current cursor
1646 * position. Also check if the Visual position changed.
1647 */
1648 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1649 {
1650 from = curwin->w_cursor.lnum;
1651 to = wp->w_old_cursor_lnum;
1652 }
1653 else
1654 {
1655 from = wp->w_old_cursor_lnum;
1656 to = curwin->w_cursor.lnum;
1657 if (from == 0) /* Visual mode just started */
1658 from = to;
1659 }
1660
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001661 if (VIsual.lnum != wp->w_old_visual_lnum
1662 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 if (wp->w_old_visual_lnum < from
1665 && wp->w_old_visual_lnum != 0)
1666 from = wp->w_old_visual_lnum;
1667 if (wp->w_old_visual_lnum > to)
1668 to = wp->w_old_visual_lnum;
1669 if (VIsual.lnum < from)
1670 from = VIsual.lnum;
1671 if (VIsual.lnum > to)
1672 to = VIsual.lnum;
1673 }
1674 }
1675
1676 /*
1677 * If in block mode and changed column or curwin->w_curswant:
1678 * update all lines.
1679 * First compute the actual start and end column.
1680 */
1681 if (VIsual_mode == Ctrl_V)
1682 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001683 colnr_T fromc, toc;
1684#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1685 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar404406a2014-10-09 13:24:43 +02001687 if (curwin->w_p_lbr)
1688 ve_flags = VE_ALL;
1689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001691#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1692 ve_flags = save_ve_flags;
1693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ++toc;
1695 if (curwin->w_curswant == MAXCOL)
1696 toc = MAXCOL;
1697
1698 if (fromc != wp->w_old_cursor_fcol
1699 || toc != wp->w_old_cursor_lcol)
1700 {
1701 if (from > VIsual.lnum)
1702 from = VIsual.lnum;
1703 if (to < VIsual.lnum)
1704 to = VIsual.lnum;
1705 }
1706 wp->w_old_cursor_fcol = fromc;
1707 wp->w_old_cursor_lcol = toc;
1708 }
1709 }
1710 else
1711 {
1712 /* Use the line numbers of the old Visual area. */
1713 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1714 {
1715 from = wp->w_old_cursor_lnum;
1716 to = wp->w_old_visual_lnum;
1717 }
1718 else
1719 {
1720 from = wp->w_old_visual_lnum;
1721 to = wp->w_old_cursor_lnum;
1722 }
1723 }
1724
1725 /*
1726 * There is no need to update lines above the top of the window.
1727 */
1728 if (from < wp->w_topline)
1729 from = wp->w_topline;
1730
1731 /*
1732 * If we know the value of w_botline, use it to restrict the update to
1733 * the lines that are visible in the window.
1734 */
1735 if (wp->w_valid & VALID_BOTLINE)
1736 {
1737 if (from >= wp->w_botline)
1738 from = wp->w_botline - 1;
1739 if (to >= wp->w_botline)
1740 to = wp->w_botline - 1;
1741 }
1742
1743 /*
1744 * Find the minimal part to be updated.
1745 * Watch out for scrolling that made entries in w_lines[] invalid.
1746 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1747 * top_end; need to redraw from top_end to the "to" line.
1748 * A middle mouse click with a Visual selection may change the text
1749 * above the Visual area and reset wl_valid, do count these for
1750 * mid_end (in srow).
1751 */
1752 if (mid_start > 0)
1753 {
1754 lnum = wp->w_topline;
1755 idx = 0;
1756 srow = 0;
1757 if (scrolled_down)
1758 mid_start = top_end;
1759 else
1760 mid_start = 0;
1761 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1762 {
1763 if (wp->w_lines[idx].wl_valid)
1764 mid_start += wp->w_lines[idx].wl_size;
1765 else if (!scrolled_down)
1766 srow += wp->w_lines[idx].wl_size;
1767 ++idx;
1768# ifdef FEAT_FOLDING
1769 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1770 lnum = wp->w_lines[idx].wl_lnum;
1771 else
1772# endif
1773 ++lnum;
1774 }
1775 srow += mid_start;
1776 mid_end = wp->w_height;
1777 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1778 {
1779 if (wp->w_lines[idx].wl_valid
1780 && wp->w_lines[idx].wl_lnum >= to + 1)
1781 {
1782 /* Only update until first row of this line */
1783 mid_end = srow;
1784 break;
1785 }
1786 srow += wp->w_lines[idx].wl_size;
1787 }
1788 }
1789 }
1790
1791 if (VIsual_active && buf == curwin->w_buffer)
1792 {
1793 wp->w_old_visual_mode = VIsual_mode;
1794 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1795 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001796 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 wp->w_old_curswant = curwin->w_curswant;
1798 }
1799 else
1800 {
1801 wp->w_old_visual_mode = 0;
1802 wp->w_old_cursor_lnum = 0;
1803 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001804 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1808 /* reset got_int, otherwise regexp won't work */
1809 save_got_int = got_int;
1810 got_int = 0;
1811#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001812#ifdef SYN_TIME_LIMIT
1813 /* Set the time limit to 'redrawtime'. */
1814 profile_setlimit(p_rdt, &syntax_tm);
1815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#ifdef FEAT_FOLDING
1817 win_foldinfo.fi_level = 0;
1818#endif
1819
1820 /*
1821 * Update all the window rows.
1822 */
1823 idx = 0; /* first entry in w_lines[].wl_size */
1824 row = 0;
1825 srow = 0;
1826 lnum = wp->w_topline; /* first line shown in window */
1827 for (;;)
1828 {
1829 /* stop updating when reached the end of the window (check for _past_
1830 * the end of the window is at the end of the loop) */
1831 if (row == wp->w_height)
1832 {
1833 didline = TRUE;
1834 break;
1835 }
1836
1837 /* stop updating when hit the end of the file */
1838 if (lnum > buf->b_ml.ml_line_count)
1839 {
1840 eof = TRUE;
1841 break;
1842 }
1843
1844 /* Remember the starting row of the line that is going to be dealt
1845 * with. It is used further down when the line doesn't fit. */
1846 srow = row;
1847
1848 /*
1849 * Update a line when it is in an area that needs updating, when it
1850 * has changes or w_lines[idx] is invalid.
1851 * bot_start may be halfway a wrapped line after using
1852 * win_del_lines(), check if the current line includes it.
1853 * When syntax folding is being used, the saved syntax states will
1854 * already have been updated, we can't see where the syntax state is
1855 * the same again, just update until the end of the window.
1856 */
1857 if (row < top_end
1858 || (row >= mid_start && row < mid_end)
1859#ifdef FEAT_SEARCH_EXTRA
1860 || top_to_mod
1861#endif
1862 || idx >= wp->w_lines_valid
1863 || (row + wp->w_lines[idx].wl_size > bot_start)
1864 || (mod_top != 0
1865 && (lnum == mod_top
1866 || (lnum >= mod_top
1867 && (lnum < mod_bot
1868#ifdef FEAT_SYN_HL
1869 || did_update == DID_FOLD
1870 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001871 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && (
1873# ifdef FEAT_FOLDING
1874 (foldmethodIsSyntax(wp)
1875 && hasAnyFolding(wp)) ||
1876# endif
1877 syntax_check_changed(lnum)))
1878#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001879#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001880 /* match in fixed position might need redraw
1881 * if lines were inserted or deleted */
1882 || (wp->w_match_head != NULL
1883 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 )))))
1886 {
1887#ifdef FEAT_SEARCH_EXTRA
1888 if (lnum == mod_top)
1889 top_to_mod = FALSE;
1890#endif
1891
1892 /*
1893 * When at start of changed lines: May scroll following lines
1894 * up or down to minimize redrawing.
1895 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001896 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898 if (lnum == mod_top
1899 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001900 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
1902 int old_rows = 0;
1903 int new_rows = 0;
1904 int xtra_rows;
1905 linenr_T l;
1906
1907 /* Count the old number of window rows, using w_lines[], which
1908 * should still contain the sizes for the lines as they are
1909 * currently displayed. */
1910 for (i = idx; i < wp->w_lines_valid; ++i)
1911 {
1912 /* Only valid lines have a meaningful wl_lnum. Invalid
1913 * lines are part of the changed area. */
1914 if (wp->w_lines[i].wl_valid
1915 && wp->w_lines[i].wl_lnum == mod_bot)
1916 break;
1917 old_rows += wp->w_lines[i].wl_size;
1918#ifdef FEAT_FOLDING
1919 if (wp->w_lines[i].wl_valid
1920 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1921 {
1922 /* Must have found the last valid entry above mod_bot.
1923 * Add following invalid entries. */
1924 ++i;
1925 while (i < wp->w_lines_valid
1926 && !wp->w_lines[i].wl_valid)
1927 old_rows += wp->w_lines[i++].wl_size;
1928 break;
1929 }
1930#endif
1931 }
1932
1933 if (i >= wp->w_lines_valid)
1934 {
1935 /* We can't find a valid line below the changed lines,
1936 * need to redraw until the end of the window.
1937 * Inserting/deleting lines has no use. */
1938 bot_start = 0;
1939 }
1940 else
1941 {
1942 /* Able to count old number of rows: Count new window
1943 * rows, and may insert/delete lines */
1944 j = idx;
1945 for (l = lnum; l < mod_bot; ++l)
1946 {
1947#ifdef FEAT_FOLDING
1948 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1949 ++new_rows;
1950 else
1951#endif
1952#ifdef FEAT_DIFF
1953 if (l == wp->w_topline)
1954 new_rows += plines_win_nofill(wp, l, TRUE)
1955 + wp->w_topfill;
1956 else
1957#endif
1958 new_rows += plines_win(wp, l, TRUE);
1959 ++j;
1960 if (new_rows > wp->w_height - row - 2)
1961 {
1962 /* it's getting too much, must redraw the rest */
1963 new_rows = 9999;
1964 break;
1965 }
1966 }
1967 xtra_rows = new_rows - old_rows;
1968 if (xtra_rows < 0)
1969 {
1970 /* May scroll text up. If there is not enough
1971 * remaining text or scrolling fails, must redraw the
1972 * rest. If scrolling works, must redraw the text
1973 * below the scrolled text. */
1974 if (row - xtra_rows >= wp->w_height - 2)
1975 mod_bot = MAXLNUM;
1976 else
1977 {
1978 check_for_delay(FALSE);
1979 if (win_del_lines(wp, row,
1980 -xtra_rows, FALSE, FALSE) == FAIL)
1981 mod_bot = MAXLNUM;
1982 else
1983 bot_start = wp->w_height + xtra_rows;
1984 }
1985 }
1986 else if (xtra_rows > 0)
1987 {
1988 /* May scroll text down. If there is not enough
1989 * remaining text of scrolling fails, must redraw the
1990 * rest. */
1991 if (row + xtra_rows >= wp->w_height - 2)
1992 mod_bot = MAXLNUM;
1993 else
1994 {
1995 check_for_delay(FALSE);
1996 if (win_ins_lines(wp, row + old_rows,
1997 xtra_rows, FALSE, FALSE) == FAIL)
1998 mod_bot = MAXLNUM;
1999 else if (top_end > row + old_rows)
2000 /* Scrolled the part at the top that requires
2001 * updating down. */
2002 top_end += xtra_rows;
2003 }
2004 }
2005
2006 /* When not updating the rest, may need to move w_lines[]
2007 * entries. */
2008 if (mod_bot != MAXLNUM && i != j)
2009 {
2010 if (j < i)
2011 {
2012 int x = row + new_rows;
2013
2014 /* move entries in w_lines[] upwards */
2015 for (;;)
2016 {
2017 /* stop at last valid entry in w_lines[] */
2018 if (i >= wp->w_lines_valid)
2019 {
2020 wp->w_lines_valid = j;
2021 break;
2022 }
2023 wp->w_lines[j] = wp->w_lines[i];
2024 /* stop at a line that won't fit */
2025 if (x + (int)wp->w_lines[j].wl_size
2026 > wp->w_height)
2027 {
2028 wp->w_lines_valid = j + 1;
2029 break;
2030 }
2031 x += wp->w_lines[j++].wl_size;
2032 ++i;
2033 }
2034 if (bot_start > x)
2035 bot_start = x;
2036 }
2037 else /* j > i */
2038 {
2039 /* move entries in w_lines[] downwards */
2040 j -= i;
2041 wp->w_lines_valid += j;
2042 if (wp->w_lines_valid > wp->w_height)
2043 wp->w_lines_valid = wp->w_height;
2044 for (i = wp->w_lines_valid; i - j >= idx; --i)
2045 wp->w_lines[i] = wp->w_lines[i - j];
2046
2047 /* The w_lines[] entries for inserted lines are
2048 * now invalid, but wl_size may be used above.
2049 * Reset to zero. */
2050 while (i >= idx)
2051 {
2052 wp->w_lines[i].wl_size = 0;
2053 wp->w_lines[i--].wl_valid = FALSE;
2054 }
2055 }
2056 }
2057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059
2060#ifdef FEAT_FOLDING
2061 /*
2062 * When lines are folded, display one line for all of them.
2063 * Otherwise, display normally (can be several display lines when
2064 * 'wrap' is on).
2065 */
2066 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2067 if (fold_count != 0)
2068 {
2069 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2070 ++row;
2071 --fold_count;
2072 wp->w_lines[idx].wl_folded = TRUE;
2073 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2074# ifdef FEAT_SYN_HL
2075 did_update = DID_FOLD;
2076# endif
2077 }
2078 else
2079#endif
2080 if (idx < wp->w_lines_valid
2081 && wp->w_lines[idx].wl_valid
2082 && wp->w_lines[idx].wl_lnum == lnum
2083 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002084 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 && srow + wp->w_lines[idx].wl_size > wp->w_height
2086#ifdef FEAT_DIFF
2087 && diff_check_fill(wp, lnum) == 0
2088#endif
2089 )
2090 {
2091 /* This line is not going to fit. Don't draw anything here,
2092 * will draw "@ " lines below. */
2093 row = wp->w_height + 1;
2094 }
2095 else
2096 {
2097#ifdef FEAT_SEARCH_EXTRA
2098 prepare_search_hl(wp, lnum);
2099#endif
2100#ifdef FEAT_SYN_HL
2101 /* Let the syntax stuff know we skipped a few lines. */
2102 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002103 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 syntax_end_parsing(syntax_last_parsed + 1);
2105#endif
2106
2107 /*
2108 * Display one line.
2109 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002110 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2111#ifdef SYN_TIME_LIMIT
2112 &syntax_tm
2113#else
2114 NULL
2115#endif
2116 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
2118#ifdef FEAT_FOLDING
2119 wp->w_lines[idx].wl_folded = FALSE;
2120 wp->w_lines[idx].wl_lastlnum = lnum;
2121#endif
2122#ifdef FEAT_SYN_HL
2123 did_update = DID_LINE;
2124 syntax_last_parsed = lnum;
2125#endif
2126 }
2127
2128 wp->w_lines[idx].wl_lnum = lnum;
2129 wp->w_lines[idx].wl_valid = TRUE;
2130 if (row > wp->w_height) /* past end of screen */
2131 {
2132 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002133 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2135 ++idx;
2136 break;
2137 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002138 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 wp->w_lines[idx].wl_size = row - srow;
2140 ++idx;
2141#ifdef FEAT_FOLDING
2142 lnum += fold_count + 1;
2143#else
2144 ++lnum;
2145#endif
2146 }
2147 else
2148 {
2149 /* This line does not need updating, advance to the next one */
2150 row += wp->w_lines[idx++].wl_size;
2151 if (row > wp->w_height) /* past end of screen */
2152 break;
2153#ifdef FEAT_FOLDING
2154 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2155#else
2156 ++lnum;
2157#endif
2158#ifdef FEAT_SYN_HL
2159 did_update = DID_NONE;
2160#endif
2161 }
2162
2163 if (lnum > buf->b_ml.ml_line_count)
2164 {
2165 eof = TRUE;
2166 break;
2167 }
2168 }
2169 /*
2170 * End of loop over all window lines.
2171 */
2172
2173
2174 if (idx > wp->w_lines_valid)
2175 wp->w_lines_valid = idx;
2176
2177#ifdef FEAT_SYN_HL
2178 /*
2179 * Let the syntax stuff know we stop parsing here.
2180 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002181 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 syntax_end_parsing(syntax_last_parsed + 1);
2183#endif
2184
2185 /*
2186 * If we didn't hit the end of the file, and we didn't finish the last
2187 * line we were working on, then the line didn't fit.
2188 */
2189 wp->w_empty_rows = 0;
2190#ifdef FEAT_DIFF
2191 wp->w_filler_rows = 0;
2192#endif
2193 if (!eof && !didline)
2194 {
2195 if (lnum == wp->w_topline)
2196 {
2197 /*
2198 * Single line that does not fit!
2199 * Don't overwrite it, it can be edited.
2200 */
2201 wp->w_botline = lnum + 1;
2202 }
2203#ifdef FEAT_DIFF
2204 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2205 {
2206 /* Window ends in filler lines. */
2207 wp->w_botline = lnum;
2208 wp->w_filler_rows = wp->w_height - srow;
2209 }
2210#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002211 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2212 {
2213 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2214
2215 /*
2216 * Last line isn't finished: Display "@@@" in the last screen line.
2217 */
2218 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002219 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002220 screen_fill(scr_row, scr_row + 1,
2221 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002222 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002223 set_empty_rows(wp, srow);
2224 wp->w_botline = lnum;
2225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2227 {
2228 /*
2229 * Last line isn't finished: Display "@@@" at the end.
2230 */
2231 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2232 W_WINROW(wp) + wp->w_height,
2233 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002234 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 set_empty_rows(wp, srow);
2236 wp->w_botline = lnum;
2237 }
2238 else
2239 {
2240 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2241 wp->w_botline = lnum;
2242 }
2243 }
2244 else
2245 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002246#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 draw_vsep_win(wp, row);
2248#endif
2249 if (eof) /* we hit the end of the file */
2250 {
2251 wp->w_botline = buf->b_ml.ml_line_count + 1;
2252#ifdef FEAT_DIFF
2253 j = diff_check_fill(wp, wp->w_botline);
2254 if (j > 0 && !wp->w_botfill)
2255 {
2256 /*
2257 * Display filler lines at the end of the file
2258 */
2259 if (char2cells(fill_diff) > 1)
2260 i = '-';
2261 else
2262 i = fill_diff;
2263 if (row + j > wp->w_height)
2264 j = wp->w_height - row;
2265 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2266 row += j;
2267 }
2268#endif
2269 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002270 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 wp->w_botline = lnum;
2272
2273 /* make sure the rest of the screen is blank */
2274 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002275 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
2278 /* Reset the type of redrawing required, the window has been updated. */
2279 wp->w_redr_type = 0;
2280#ifdef FEAT_DIFF
2281 wp->w_old_topfill = wp->w_topfill;
2282 wp->w_old_botfill = wp->w_botfill;
2283#endif
2284
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002285 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 /*
2288 * There is a trick with w_botline. If we invalidate it on each
2289 * change that might modify it, this will cause a lot of expensive
2290 * calls to plines() in update_topline() each time. Therefore the
2291 * value of w_botline is often approximated, and this value is used to
2292 * compute the value of w_topline. If the value of w_botline was
2293 * wrong, check that the value of w_topline is correct (cursor is on
2294 * the visible part of the text). If it's not, we need to redraw
2295 * again. Mostly this just means scrolling up a few lines, so it
2296 * doesn't look too bad. Only do this for the current window (where
2297 * changes are relevant).
2298 */
2299 wp->w_valid |= VALID_BOTLINE;
2300 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2301 {
2302 recursive = TRUE;
2303 curwin->w_valid &= ~VALID_TOPLINE;
2304 update_topline(); /* may invalidate w_botline again */
2305 if (must_redraw != 0)
2306 {
2307 /* Don't update for changes in buffer again. */
2308 i = curbuf->b_mod_set;
2309 curbuf->b_mod_set = FALSE;
2310 win_update(curwin);
2311 must_redraw = 0;
2312 curbuf->b_mod_set = i;
2313 }
2314 recursive = FALSE;
2315 }
2316 }
2317
2318#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2319 /* restore got_int, unless CTRL-C was hit while redrawing */
2320 if (!got_int)
2321 got_int = save_got_int;
2322#endif
2323}
2324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325/*
2326 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2327 * as the filler character.
2328 */
2329 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002330win_draw_end(
2331 win_T *wp,
2332 int c1,
2333 int c2,
2334 int row,
2335 int endrow,
2336 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
2338#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2339 int n = 0;
2340# define FDC_OFF n
2341#else
2342# define FDC_OFF 0
2343#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002344#ifdef FEAT_FOLDING
2345 int fdc = compute_foldcolumn(wp, 0);
2346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348#ifdef FEAT_RIGHTLEFT
2349 if (wp->w_p_rl)
2350 {
2351 /* No check for cmdline window: should never be right-left. */
2352# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002353 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 if (n > 0)
2356 {
2357 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002358 if (n > W_WIDTH(wp))
2359 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2361 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002362 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364# endif
2365# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002366 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
2368 int nn = n + 2;
2369
2370 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002371 if (nn > W_WIDTH(wp))
2372 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2374 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002375 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 n = nn;
2377 }
2378# endif
2379 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2380 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002381 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2383 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002384 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386 else
2387#endif
2388 {
2389#ifdef FEAT_CMDWIN
2390 if (cmdwin_type != 0 && wp == curwin)
2391 {
2392 /* draw the cmdline character in the leftmost column */
2393 n = 1;
2394 if (n > wp->w_width)
2395 n = wp->w_width;
2396 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2397 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002398 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400#endif
2401#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002402 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002404 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /* draw the fold column at the left */
2407 if (nn > W_WIDTH(wp))
2408 nn = W_WIDTH(wp);
2409 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2410 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002411 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 n = nn;
2413 }
2414#endif
2415#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002416 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 int nn = n + 2;
2419
2420 /* draw the sign column after the fold column */
2421 if (nn > W_WIDTH(wp))
2422 nn = W_WIDTH(wp);
2423 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2424 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002425 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 n = nn;
2427 }
2428#endif
2429 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2430 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002431 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 set_empty_rows(wp, row);
2434}
2435
Bram Moolenaar1a384422010-07-14 19:53:30 +02002436#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002437static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002438
2439/*
2440 * Advance **color_cols and return TRUE when there are columns to draw.
2441 */
2442 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002443advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002444{
2445 while (**color_cols >= 0 && vcol > **color_cols)
2446 ++*color_cols;
2447 return (**color_cols >= 0);
2448}
2449#endif
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#ifdef FEAT_FOLDING
2452/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002453 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2454 * space is available for window "wp", minus "col".
2455 */
2456 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002457compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002458{
2459 int fdc = wp->w_p_fdc;
2460 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2461 int wwidth = W_WIDTH(wp);
2462
2463 if (fdc > wwidth - (col + wmw))
2464 fdc = wwidth - (col + wmw);
2465 return fdc;
2466}
2467
2468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 * Display one folded line.
2470 */
2471 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002472fold_line(
2473 win_T *wp,
2474 long fold_count,
2475 foldinfo_T *foldinfo,
2476 linenr_T lnum,
2477 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002479 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 pos_T *top, *bot;
2481 linenr_T lnume = lnum + fold_count - 1;
2482 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002483 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 int col;
2486 int txtcol;
2487 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002488 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 /* Build the fold line:
2491 * 1. Add the cmdwin_type for the command-line window
2492 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002493 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 * 4. Compose the text
2495 * 5. Add the text
2496 * 6. set highlighting for the Visual area an other text
2497 */
2498 col = 0;
2499
2500 /*
2501 * 1. Add the cmdwin_type for the command-line window
2502 * Ignores 'rightleft', this window is never right-left.
2503 */
2504#ifdef FEAT_CMDWIN
2505 if (cmdwin_type != 0 && wp == curwin)
2506 {
2507 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002508 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_MBYTE
2510 if (enc_utf8)
2511 ScreenLinesUC[off] = 0;
2512#endif
2513 ++col;
2514 }
2515#endif
2516
2517 /*
2518 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002519 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002521 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (fdc > 0)
2523 {
2524 fill_foldcolumn(buf, wp, TRUE, lnum);
2525#ifdef FEAT_RIGHTLEFT
2526 if (wp->w_p_rl)
2527 {
2528 int i;
2529
2530 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002531 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /* reverse the fold column */
2533 for (i = 0; i < fdc; ++i)
2534 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2535 }
2536 else
2537#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002538 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 col += fdc;
2540 }
2541
2542#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002543# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2544 for (ri = 0; ri < l; ++ri) \
2545 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2546 else \
2547 for (ri = 0; ri < l; ++ri) \
2548 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002550# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2551 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#endif
2553
Bram Moolenaar64486672010-05-16 15:46:46 +02002554 /* Set all attributes of the 'number' or 'relativenumber' column and the
2555 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002556 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558#ifdef FEAT_SIGNS
2559 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002560 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 len = W_WIDTH(wp) - col;
2563 if (len > 0)
2564 {
2565 if (len > 2)
2566 len = 2;
2567# ifdef FEAT_RIGHTLEFT
2568 if (wp->w_p_rl)
2569 /* the line number isn't reversed */
2570 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002571 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 else
2573# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002574 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 col += len;
2576 }
2577 }
2578#endif
2579
2580 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002581 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002583 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 len = W_WIDTH(wp) - col;
2586 if (len > 0)
2587 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002588 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002589 long num;
2590 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002591
2592 if (len > w + 1)
2593 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002594
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002595 if (wp->w_p_nu && !wp->w_p_rnu)
2596 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002597 num = (long)lnum;
2598 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002599 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002600 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002601 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002602 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002603 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002604 /* 'number' + 'relativenumber': cursor line shows absolute
2605 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002606 num = lnum;
2607 fmt = "%-*ld ";
2608 }
2609 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002610
Bram Moolenaar700e7342013-01-30 12:31:36 +01002611 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612#ifdef FEAT_RIGHTLEFT
2613 if (wp->w_p_rl)
2614 /* the line number isn't reversed */
2615 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002616 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 else
2618#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002619 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 col += len;
2621 }
2622 }
2623
2624 /*
2625 * 4. Compose the folded-line string with 'foldtext', if set.
2626 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002627 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 txtcol = col; /* remember where text starts */
2630
2631 /*
2632 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2633 * Right-left text is put in columns 0 - number-col, normal text is put
2634 * in columns number-col - window-width.
2635 */
2636#ifdef FEAT_MBYTE
2637 if (has_mbyte)
2638 {
2639 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002640 int u8c, u8cc[MAX_MCO];
2641 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int idx;
2643 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002644 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645# ifdef FEAT_ARABIC
2646 int prev_c = 0; /* previous Arabic character */
2647 int prev_c1 = 0; /* first composing char for prev_c */
2648# endif
2649
2650# ifdef FEAT_RIGHTLEFT
2651 if (wp->w_p_rl)
2652 idx = off;
2653 else
2654# endif
2655 idx = off + col;
2656
2657 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2658 for (p = text; *p != NUL; )
2659 {
2660 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002661 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (col + cells > W_WIDTH(wp)
2663# ifdef FEAT_RIGHTLEFT
2664 - (wp->w_p_rl ? col : 0)
2665# endif
2666 )
2667 break;
2668 ScreenLines[idx] = *p;
2669 if (enc_utf8)
2670 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002671 u8c = utfc_ptr2char(p, u8cc);
2672 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 ScreenLinesUC[idx] = 0;
2675#ifdef FEAT_ARABIC
2676 prev_c = u8c;
2677#endif
2678 }
2679 else
2680 {
2681#ifdef FEAT_ARABIC
2682 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2683 {
2684 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002685 int pc, pc1, nc;
2686 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 int firstbyte = *p;
2688
2689 /* The idea of what is the previous and next
2690 * character depends on 'rightleft'. */
2691 if (wp->w_p_rl)
2692 {
2693 pc = prev_c;
2694 pc1 = prev_c1;
2695 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002696 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 else
2699 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002702 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 prev_c = u8c;
2705
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 pc, pc1, nc);
2708 ScreenLines[idx] = firstbyte;
2709 }
2710 else
2711 prev_c = u8c;
2712#endif
2713 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002714#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (u8c >= 0x10000)
2716 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2717 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002720 for (i = 0; i < Screen_mco; ++i)
2721 {
2722 ScreenLinesC[i][idx] = u8cc[i];
2723 if (u8cc[i] == 0)
2724 break;
2725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 if (cells > 1)
2728 ScreenLines[idx + 1] = 0;
2729 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002730 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2731 /* double-byte single width character */
2732 ScreenLines2[idx] = p[1];
2733 else if (cells > 1)
2734 /* double-width character */
2735 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 col += cells;
2737 idx += cells;
2738 p += c_len;
2739 }
2740 }
2741 else
2742#endif
2743 {
2744 len = (int)STRLEN(text);
2745 if (len > W_WIDTH(wp) - col)
2746 len = W_WIDTH(wp) - col;
2747 if (len > 0)
2748 {
2749#ifdef FEAT_RIGHTLEFT
2750 if (wp->w_p_rl)
2751 STRNCPY(current_ScreenLine, text, len);
2752 else
2753#endif
2754 STRNCPY(current_ScreenLine + col, text, len);
2755 col += len;
2756 }
2757 }
2758
2759 /* Fill the rest of the line with the fold filler */
2760#ifdef FEAT_RIGHTLEFT
2761 if (wp->w_p_rl)
2762 col -= txtcol;
2763#endif
2764 while (col < W_WIDTH(wp)
2765#ifdef FEAT_RIGHTLEFT
2766 - (wp->w_p_rl ? txtcol : 0)
2767#endif
2768 )
2769 {
2770#ifdef FEAT_MBYTE
2771 if (enc_utf8)
2772 {
2773 if (fill_fold >= 0x80)
2774 {
2775 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002776 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002777 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
2779 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002782 ScreenLines[off + col] = fill_fold;
2783 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002784 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002788 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
2791 if (text != buf)
2792 vim_free(text);
2793
2794 /*
2795 * 6. set highlighting for the Visual area an other text.
2796 * If all folded lines are in the Visual area, highlight the line.
2797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2799 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002800 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
2802 /* Visual is after curwin->w_cursor */
2803 top = &curwin->w_cursor;
2804 bot = &VIsual;
2805 }
2806 else
2807 {
2808 /* Visual is before curwin->w_cursor */
2809 top = &VIsual;
2810 bot = &curwin->w_cursor;
2811 }
2812 if (lnum >= top->lnum
2813 && lnume <= bot->lnum
2814 && (VIsual_mode != 'v'
2815 || ((lnum > top->lnum
2816 || (lnum == top->lnum
2817 && top->col == 0))
2818 && (lnume < bot->lnum
2819 || (lnume == bot->lnum
2820 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002821 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
2823 if (VIsual_mode == Ctrl_V)
2824 {
2825 /* Visual block mode: highlight the chars part of the block */
2826 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2827 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002828 if (wp->w_old_cursor_lcol != MAXCOL
2829 && wp->w_old_cursor_lcol + txtcol
2830 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 len = wp->w_old_cursor_lcol;
2832 else
2833 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002834 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002835 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837 }
2838 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002841 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002846#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002847 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2848 if (wp->w_p_cc_cols)
2849 {
2850 int i = 0;
2851 int j = wp->w_p_cc_cols[i];
2852 int old_txtcol = txtcol;
2853
2854 while (j > -1)
2855 {
2856 txtcol += j;
2857 if (wp->w_p_wrap)
2858 txtcol -= wp->w_skipcol;
2859 else
2860 txtcol -= wp->w_leftcol;
2861 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2862 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002863 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002864 txtcol = old_txtcol;
2865 j = wp->w_p_cc_cols[++i];
2866 }
2867 }
2868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002869 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002870 if (wp->w_p_cuc)
2871 {
2872 txtcol += wp->w_virtcol;
2873 if (wp->w_p_wrap)
2874 txtcol -= wp->w_skipcol;
2875 else
2876 txtcol -= wp->w_leftcol;
2877 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2878 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002879 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002880 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2884 (int)W_WIDTH(wp), FALSE);
2885
2886 /*
2887 * Update w_cline_height and w_cline_folded if the cursor line was
2888 * updated (saves a call to plines() later).
2889 */
2890 if (wp == curwin
2891 && lnum <= curwin->w_cursor.lnum
2892 && lnume >= curwin->w_cursor.lnum)
2893 {
2894 curwin->w_cline_row = row;
2895 curwin->w_cline_height = 1;
2896 curwin->w_cline_folded = TRUE;
2897 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2898 }
2899}
2900
2901/*
2902 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2903 */
2904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002905copy_text_attr(
2906 int off,
2907 char_u *buf,
2908 int len,
2909 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002911 int i;
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 mch_memmove(ScreenLines + off, buf, (size_t)len);
2914# ifdef FEAT_MBYTE
2915 if (enc_utf8)
2916 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2917# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002918 for (i = 0; i < len; ++i)
2919 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
2923 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002924 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 */
2926 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002927fill_foldcolumn(
2928 char_u *p,
2929 win_T *wp,
2930 int closed, /* TRUE of FALSE */
2931 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 int i = 0;
2934 int level;
2935 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002936 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002937 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002940 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 level = win_foldinfo.fi_level;
2943 if (level > 0)
2944 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002945 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002946 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 /* If the column is too narrow, we start at the lowest level that
2949 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002950 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 if (first_level < 1)
2952 first_level = 1;
2953
Bram Moolenaar1c934292015-01-27 16:39:29 +01002954 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 if (win_foldinfo.fi_lnum == lnum
2957 && first_level + i >= win_foldinfo.fi_low_level)
2958 p[i] = '-';
2959 else if (first_level == 1)
2960 p[i] = '|';
2961 else if (first_level + i <= 9)
2962 p[i] = '0' + first_level + i;
2963 else
2964 p[i] = '>';
2965 if (first_level + i == level)
2966 break;
2967 }
2968 }
2969 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972#endif /* FEAT_FOLDING */
2973
2974/*
2975 * Display line "lnum" of window 'wp' on the screen.
2976 * Start at row "startrow", stop when "endrow" is reached.
2977 * wp->w_virtcol needs to be valid.
2978 *
2979 * Return the number of last row the line occupies.
2980 */
2981 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002982win_line(
2983 win_T *wp,
2984 linenr_T lnum,
2985 int startrow,
2986 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002987 int nochange UNUSED, /* not updating for changed text */
2988 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01002990 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2992 int c = 0; /* init for GCC */
2993 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002994#ifdef FEAT_LINEBREAK
2995 long vcol_sbr = -1; /* virtual column after showbreak */
2996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 long vcol_prev = -1; /* "vcol" of previous character */
2998 char_u *line; /* current line */
2999 char_u *ptr; /* current position in "line" */
3000 int row; /* row in the window, excl w_winrow */
3001 int screen_row; /* row on the screen, incl w_winrow */
3002
3003 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3004 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003005 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003006 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int c_extra = NUL; /* extra chars, all the same */
3008 int extra_attr = 0; /* attributes when n_extra != 0 */
3009 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3010 displaying lcs_eol at end-of-line */
3011 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3012 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3013
3014 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3015 int saved_n_extra = 0;
3016 char_u *saved_p_extra = NULL;
3017 int saved_c_extra = 0;
3018 int saved_char_attr = 0;
3019
3020 int n_attr = 0; /* chars with special attr */
3021 int saved_attr2 = 0; /* char_attr saved for n_attr */
3022 int n_attr3 = 0; /* chars with overruling special attr */
3023 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3024
3025 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3026
3027 int fromcol, tocol; /* start/end of inverting */
3028 int fromcol_prev = -2; /* start of inverting after cursor */
3029 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003031 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 pos_T pos;
3033 long v;
3034
3035 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003036 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3038 in this line */
3039 int attr = 0; /* attributes for area highlighting */
3040 int area_attr = 0; /* attributes desired by highlighting */
3041 int search_attr = 0; /* attributes desired by 'hlsearch' */
3042#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003043 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 int syntax_attr = 0; /* attributes desired by syntax */
3045 int has_syntax = FALSE; /* this buffer has syntax highl. */
3046 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003047 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003048 int draw_color_col = FALSE; /* highlight colorcolumn */
3049 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003050#endif
3051#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003052 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003053# define SPWORDLEN 150
3054 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003055 int nextlinecol = 0; /* column where nextline[] starts */
3056 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003057 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003058 int spell_attr = 0; /* attributes desired by spelling */
3059 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003060 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3061 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003062 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003063 static int cap_col = -1; /* column to check for Cap word */
3064 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003065 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#endif
3067 int extra_check; /* has syntax or linebreak */
3068#ifdef FEAT_MBYTE
3069 int multi_attr = 0; /* attributes desired by multibyte */
3070 int mb_l = 1; /* multi-byte byte length */
3071 int mb_c = 0; /* decoded multi-byte character */
3072 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003073 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075#ifdef FEAT_DIFF
3076 int filler_lines; /* nr of filler lines to be drawn */
3077 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003078 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 int change_start = MAXCOL; /* first col of changed area */
3080 int change_end = -1; /* last col of changed area */
3081#endif
3082 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3083#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003084 int need_showbreak = FALSE; /* overlong line, skipping first x
3085 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003087#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3088 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003090 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#endif
3092#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003093 matchitem_T *cur; /* points to the match list */
3094 match_T *shl; /* points to search_hl or a match */
3095 int shl_flag; /* flag to indicate whether search_hl
3096 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003097 int pos_inprogress; /* marks that position match search is
3098 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003099 int prevcol_hl_flag; /* flag to indicate whether prevcol
3100 equals startcol of search_hl or one
3101 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
3103#ifdef FEAT_ARABIC
3104 int prev_c = 0; /* previous Arabic character */
3105 int prev_c1 = 0; /* first composing char for prev_c */
3106#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003107#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003108 int did_line_attr = 0;
3109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 /* draw_state: items that are drawn in sequence: */
3112#define WL_START 0 /* nothing done yet */
3113#ifdef FEAT_CMDWIN
3114# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3115#else
3116# define WL_CMDLINE WL_START
3117#endif
3118#ifdef FEAT_FOLDING
3119# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3120#else
3121# define WL_FOLD WL_CMDLINE
3122#endif
3123#ifdef FEAT_SIGNS
3124# define WL_SIGN WL_FOLD + 1 /* column for signs */
3125#else
3126# define WL_SIGN WL_FOLD /* column for signs */
3127#endif
3128#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003129#ifdef FEAT_LINEBREAK
3130# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003132# define WL_BRI WL_NR
3133#endif
3134#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3135# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3136#else
3137# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
3139#define WL_LINE WL_SBR + 1 /* text in the line */
3140 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003141#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 int feedback_col = 0;
3143 int feedback_old_attr = -1;
3144#endif
3145
Bram Moolenaar860cae12010-06-05 23:22:07 +02003146#ifdef FEAT_CONCEAL
3147 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003148 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003149 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003150 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003151 int is_concealing = FALSE;
3152 int boguscols = 0; /* nonexistent columns added to force
3153 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003154 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003155 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003156 int match_conc = 0; /* cchar for match functions */
3157 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003158 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003159# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003160# define FIX_FOR_BOGUSCOLS \
3161 { \
3162 n_extra += vcol_off; \
3163 vcol -= vcol_off; \
3164 vcol_off = 0; \
3165 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003166 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003167 boguscols = 0; \
3168 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003169#else
3170# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 if (startrow > endrow) /* past the end already! */
3174 return startrow;
3175
3176 row = startrow;
3177 screen_row = row + W_WINROW(wp);
3178
3179 /*
3180 * To speed up the loop below, set extra_check when there is linebreak,
3181 * trailing white space and/or syntax processing to be done.
3182 */
3183#ifdef FEAT_LINEBREAK
3184 extra_check = wp->w_p_lbr;
3185#else
3186 extra_check = 0;
3187#endif
3188#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003189 if (syntax_present(wp) && !wp->w_s->b_syn_error
3190# ifdef SYN_TIME_LIMIT
3191 && !wp->w_s->b_syn_slow
3192# endif
3193 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 /* Prepare for syntax highlighting in this line. When there is an
3196 * error, stop syntax highlighting. */
3197 save_did_emsg = did_emsg;
3198 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003199 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003201 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 else
3203 {
3204 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003205#ifdef SYN_TIME_LIMIT
3206 if (!wp->w_s->b_syn_slow)
3207#endif
3208 {
3209 has_syntax = TRUE;
3210 extra_check = TRUE;
3211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
3213 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003214
3215 /* Check for columns to display for 'colorcolumn'. */
3216 color_cols = wp->w_p_cc_cols;
3217 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003218 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003219#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003220
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003221#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003222 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003223 && *wp->w_s->b_p_spl != NUL
3224 && wp->w_s->b_langp.ga_len > 0
3225 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003226 {
3227 /* Prepare for spell checking. */
3228 has_spell = TRUE;
3229 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003230
3231 /* Get the start of the next line, so that words that wrap to the next
3232 * line are found too: "et<line-break>al.".
3233 * Trick: skip a few chars for C/shell/Vim comments */
3234 nextline[SPWORDLEN] = NUL;
3235 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3236 {
3237 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3238 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3239 }
3240
3241 /* When a word wrapped from the previous line the start of the current
3242 * line is valid. */
3243 if (lnum == checked_lnum)
3244 cur_checked_col = checked_col;
3245 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003246
3247 /* When there was a sentence end in the previous line may require a
3248 * word starting with capital in this line. In line 1 always check
3249 * the first word. */
3250 if (lnum != capcol_lnum)
3251 cap_col = -1;
3252 if (lnum == 1)
3253 cap_col = 0;
3254 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif
3257
3258 /*
3259 * handle visual active in this window
3260 */
3261 fromcol = -10;
3262 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3264 {
3265 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003266 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 top = &curwin->w_cursor;
3269 bot = &VIsual;
3270 }
3271 else /* Visual is before curwin->w_cursor */
3272 {
3273 top = &VIsual;
3274 bot = &curwin->w_cursor;
3275 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003276 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (VIsual_mode == Ctrl_V) /* block mode */
3278 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003279 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 fromcol = wp->w_old_cursor_fcol;
3282 tocol = wp->w_old_cursor_lcol;
3283 }
3284 }
3285 else /* non-block mode */
3286 {
3287 if (lnum > top->lnum && lnum <= bot->lnum)
3288 fromcol = 0;
3289 else if (lnum == top->lnum)
3290 {
3291 if (VIsual_mode == 'V') /* linewise */
3292 fromcol = 0;
3293 else
3294 {
3295 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3296 if (gchar_pos(top) == NUL)
3297 tocol = fromcol + 1;
3298 }
3299 }
3300 if (VIsual_mode != 'V' && lnum == bot->lnum)
3301 {
3302 if (*p_sel == 'e' && bot->col == 0
3303#ifdef FEAT_VIRTUALEDIT
3304 && bot->coladd == 0
3305#endif
3306 )
3307 {
3308 fromcol = -10;
3309 tocol = MAXCOL;
3310 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003311 else if (bot->col == MAXCOL)
3312 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
3314 {
3315 pos = *bot;
3316 if (*p_sel == 'e')
3317 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3318 else
3319 {
3320 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3321 ++tocol;
3322 }
3323 }
3324 }
3325 }
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 /* Check if the character under the cursor should not be inverted */
3328 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003329#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 )
3333 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
3335 /* if inverting in this line set area_highlighting */
3336 if (fromcol >= 0)
3337 {
3338 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003339 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003341 if ((clip_star.available && !clip_star.owned
3342 && clip_isautosel_star())
3343 || (clip_plus.available && !clip_plus.owned
3344 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003345 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
3347 }
3348 }
3349
3350 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003351 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003353 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 && wp == curwin
3355 && lnum >= curwin->w_cursor.lnum
3356 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3357 {
3358 if (lnum == curwin->w_cursor.lnum)
3359 getvcol(curwin, &(curwin->w_cursor),
3360 (colnr_T *)&fromcol, NULL, NULL);
3361 else
3362 fromcol = 0;
3363 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3364 {
3365 pos.lnum = lnum;
3366 pos.col = search_match_endcol;
3367 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3368 }
3369 else
3370 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003371 /* do at least one character; happens when past end of line */
3372 if (fromcol == tocol)
3373 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003375 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377
3378#ifdef FEAT_DIFF
3379 filler_lines = diff_check(wp, lnum);
3380 if (filler_lines < 0)
3381 {
3382 if (filler_lines == -1)
3383 {
3384 if (diff_find_change(wp, lnum, &change_start, &change_end))
3385 diff_hlf = HLF_ADD; /* added line */
3386 else if (change_start == 0)
3387 diff_hlf = HLF_TXD; /* changed text */
3388 else
3389 diff_hlf = HLF_CHD; /* changed line */
3390 }
3391 else
3392 diff_hlf = HLF_ADD; /* added line */
3393 filler_lines = 0;
3394 area_highlighting = TRUE;
3395 }
3396 if (lnum == wp->w_topline)
3397 filler_lines = wp->w_topfill;
3398 filler_todo = filler_lines;
3399#endif
3400
3401#ifdef LINE_ATTR
3402# ifdef FEAT_SIGNS
3403 /* If this line has a sign with line highlighting set line_attr. */
3404 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3405 if (v != 0)
3406 line_attr = sign_get_attr((int)v, TRUE);
3407# endif
3408# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3409 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003410 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003411 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412# endif
3413 if (line_attr != 0)
3414 area_highlighting = TRUE;
3415#endif
3416
3417 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3418 ptr = line;
3419
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003420#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003421 if (has_spell)
3422 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003423 /* For checking first word with a capital skip white space. */
3424 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003425 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003426
Bram Moolenaar30abd282005-06-22 22:35:10 +00003427 /* To be able to spell-check over line boundaries copy the end of the
3428 * current line into nextline[]. Above the start of the next line was
3429 * copied to nextline[SPWORDLEN]. */
3430 if (nextline[SPWORDLEN] == NUL)
3431 {
3432 /* No next line or it is empty. */
3433 nextlinecol = MAXCOL;
3434 nextline_idx = 0;
3435 }
3436 else
3437 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003438 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003439 if (v < SPWORDLEN)
3440 {
3441 /* Short line, use it completely and append the start of the
3442 * next line. */
3443 nextlinecol = 0;
3444 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003445 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003446 nextline_idx = v + 1;
3447 }
3448 else
3449 {
3450 /* Long line, use only the last SPWORDLEN bytes. */
3451 nextlinecol = v - SPWORDLEN;
3452 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3453 nextline_idx = SPWORDLEN + 1;
3454 }
3455 }
3456 }
3457#endif
3458
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003459 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003461 if (lcs_space || lcs_trail)
3462 extra_check = TRUE;
3463 /* find start of trailing whitespace */
3464 if (lcs_trail)
3465 {
3466 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003467 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003468 --trailcol;
3469 trailcol += (colnr_T) (ptr - line);
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472
3473 /*
3474 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3475 * first character to be displayed.
3476 */
3477 if (wp->w_p_wrap)
3478 v = wp->w_skipcol;
3479 else
3480 v = wp->w_leftcol;
3481 if (v > 0)
3482 {
3483#ifdef FEAT_MBYTE
3484 char_u *prev_ptr = ptr;
3485#endif
3486 while (vcol < v && *ptr != NUL)
3487 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003488 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 vcol += c;
3490#ifdef FEAT_MBYTE
3491 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003493 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003496 /* When:
3497 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003498 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003499 * - 'virtualedit' is set, or
3500 * - the visual mode is active,
3501 * the end of the line may be before the start of the displayed part.
3502 */
3503 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003504#ifdef FEAT_SYN_HL
3505 wp->w_p_cuc || draw_color_col ||
3506#endif
3507#ifdef FEAT_VIRTUALEDIT
3508 virtual_active() ||
3509#endif
3510 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 /* Handle a character that's not completely on the screen: Put ptr at
3516 * that character but skip the first few screen characters. */
3517 if (vcol > v)
3518 {
3519 vcol -= c;
3520#ifdef FEAT_MBYTE
3521 ptr = prev_ptr;
3522#else
3523 --ptr;
3524#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003525 /* If the character fits on the screen, don't need to skip it.
3526 * Except for a TAB. */
3527 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003528#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003529 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003530#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003531 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003532 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
3535 /*
3536 * Adjust for when the inverted text is before the screen,
3537 * and when the start of the inverted text is before the screen.
3538 */
3539 if (tocol <= vcol)
3540 fromcol = 0;
3541 else if (fromcol >= 0 && fromcol < vcol)
3542 fromcol = vcol;
3543
3544#ifdef FEAT_LINEBREAK
3545 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3546 if (wp->w_p_wrap)
3547 need_showbreak = TRUE;
3548#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003549#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003550 /* When spell checking a word we need to figure out the start of the
3551 * word and if it's badly spelled or not. */
3552 if (has_spell)
3553 {
3554 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003555 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003556 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003557
3558 pos = wp->w_cursor;
3559 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003560 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003561 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003562
3563 /* spell_move_to() may call ml_get() and make "line" invalid */
3564 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3565 ptr = line + linecol;
3566
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003567 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003568 {
3569 /* no bad word found at line start, don't check until end of a
3570 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003571 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003572 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003573 }
3574 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003575 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003576 /* bad word found, use attributes until end of word */
3577 word_end = wp->w_cursor.col + len + 1;
3578
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003579 /* Turn index into actual attributes. */
3580 if (spell_hlf != HLF_COUNT)
3581 spell_attr = highlight_attr[spell_hlf];
3582 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003583 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003584
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003585# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003586 /* Need to restart syntax highlighting for this line. */
3587 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003588 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003589# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003590 }
3591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593
3594 /*
3595 * Correct highlighting for cursor that can't be disabled.
3596 * Avoids having to check this for each character.
3597 */
3598 if (fromcol >= 0)
3599 {
3600 if (noinvcur)
3601 {
3602 if ((colnr_T)fromcol == wp->w_virtcol)
3603 {
3604 /* highlighting starts at cursor, let it start just after the
3605 * cursor */
3606 fromcol_prev = fromcol;
3607 fromcol = -1;
3608 }
3609 else if ((colnr_T)fromcol < wp->w_virtcol)
3610 /* restart highlighting after the cursor */
3611 fromcol_prev = wp->w_virtcol;
3612 }
3613 if (fromcol >= tocol)
3614 fromcol = -1;
3615 }
3616
3617#ifdef FEAT_SEARCH_EXTRA
3618 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003619 * Handle highlighting the last used search pattern and matches.
3620 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003622 cur = wp->w_match_head;
3623 shl_flag = FALSE;
3624 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003626 if (shl_flag == FALSE)
3627 {
3628 shl = &search_hl;
3629 shl_flag = TRUE;
3630 }
3631 else
3632 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003633 shl->startcol = MAXCOL;
3634 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003636 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003637 v = (long)(ptr - line);
3638 if (cur != NULL)
3639 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003640 next_search_hl(wp, shl, lnum, (colnr_T)v,
3641 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003642
3643 /* Need to get the line again, a multi-line regexp may have made it
3644 * invalid. */
3645 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3646 ptr = line + v;
3647
3648 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003650 if (shl->lnum == lnum)
3651 shl->startcol = shl->rm.startpos[0].col;
3652 else
3653 shl->startcol = 0;
3654 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3655 - shl->rm.startpos[0].lnum)
3656 shl->endcol = shl->rm.endpos[0].col;
3657 else
3658 shl->endcol = MAXCOL;
3659 /* Highlight one character for an empty match. */
3660 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003663 if (has_mbyte && line[shl->endcol] != NUL)
3664 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003667 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003669 if ((long)shl->startcol < v) /* match at leftcol */
3670 {
3671 shl->attr_cur = shl->attr;
3672 search_attr = shl->attr;
3673 }
3674 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003676 if (shl != &search_hl && cur != NULL)
3677 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679#endif
3680
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003681#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003682 /* Cursor line highlighting for 'cursorline' in the current window. Not
3683 * when Visual mode is active, because it's not clear what is selected
3684 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003685 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003686 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003687 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003688 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003689 area_highlighting = TRUE;
3690 }
3691#endif
3692
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003693 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 col = 0;
3695#ifdef FEAT_RIGHTLEFT
3696 if (wp->w_p_rl)
3697 {
3698 /* Rightleft window: process the text in the normal direction, but put
3699 * it in current_ScreenLine[] from right to left. Start at the
3700 * rightmost column of the window. */
3701 col = W_WIDTH(wp) - 1;
3702 off += col;
3703 }
3704#endif
3705
3706 /*
3707 * Repeat for the whole displayed line.
3708 */
3709 for (;;)
3710 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003711#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003712 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /* Skip this quickly when working on the text. */
3715 if (draw_state != WL_LINE)
3716 {
3717#ifdef FEAT_CMDWIN
3718 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3719 {
3720 draw_state = WL_CMDLINE;
3721 if (cmdwin_type != 0 && wp == curwin)
3722 {
3723 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003725 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003726 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728 }
3729#endif
3730
3731#ifdef FEAT_FOLDING
3732 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3733 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003734 int fdc = compute_foldcolumn(wp, 0);
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003737 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003739 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003740 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003741 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003742 p_extra_free = alloc(12 + 1);
3743
3744 if (p_extra_free != NULL)
3745 {
3746 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3747 n_extra = fdc;
3748 p_extra_free[n_extra] = NUL;
3749 p_extra = p_extra_free;
3750 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003751 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 }
3755#endif
3756
3757#ifdef FEAT_SIGNS
3758 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3759 {
3760 draw_state = WL_SIGN;
3761 /* Show the sign column when there are any signs in this
3762 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003763 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003765 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003767 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768# endif
3769
3770 /* Draw two cells with the sign value or blank. */
3771 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003772 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 n_extra = 2;
3774
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003775 if (row == startrow
3776#ifdef FEAT_DIFF
3777 + filler_lines && filler_todo <= 0
3778#endif
3779 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
3781 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3782 SIGN_TEXT);
3783# ifdef FEAT_SIGN_ICONS
3784 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3785 SIGN_ICON);
3786 if (gui.in_use && icon_sign != 0)
3787 {
3788 /* Use the image in this position. */
3789 c_extra = SIGN_BYTE;
3790# ifdef FEAT_NETBEANS_INTG
3791 if (buf_signcount(wp->w_buffer, lnum) > 1)
3792 c_extra = MULTISIGN_BYTE;
3793# endif
3794 char_attr = icon_sign;
3795 }
3796 else
3797# endif
3798 if (text_sign != 0)
3799 {
3800 p_extra = sign_get_text(text_sign);
3801 if (p_extra != NULL)
3802 {
3803 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 char_attr = sign_get_attr(text_sign, FALSE);
3807 }
3808 }
3809 }
3810 }
3811#endif
3812
3813 if (draw_state == WL_NR - 1 && n_extra == 0)
3814 {
3815 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003816 /* Display the absolute or relative line number. After the
3817 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3818 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 && (row == startrow
3820#ifdef FEAT_DIFF
3821 + filler_lines
3822#endif
3823 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3824 {
3825 /* Draw the line number (empty space after wrapping). */
3826 if (row == startrow
3827#ifdef FEAT_DIFF
3828 + filler_lines
3829#endif
3830 )
3831 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003832 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003833 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003834
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003835 if (wp->w_p_nu && !wp->w_p_rnu)
3836 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003837 num = (long)lnum;
3838 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003839 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003840 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003841 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003842 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003843 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003844 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003845 num = lnum;
3846 fmt = "%-*ld ";
3847 }
3848 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003849
Bram Moolenaar700e7342013-01-30 12:31:36 +01003850 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003851 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 if (wp->w_skipcol > 0)
3853 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3854 *p_extra = '-';
3855#ifdef FEAT_RIGHTLEFT
3856 if (wp->w_p_rl) /* reverse line numbers */
3857 rl_mirror(extra);
3858#endif
3859 p_extra = extra;
3860 c_extra = NUL;
3861 }
3862 else
3863 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003864 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003865 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003866#ifdef FEAT_SYN_HL
3867 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003868 * the current line differently.
3869 * TODO: Can we use CursorLine instead of CursorLineNr
3870 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003871 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003872 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003873 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 }
3877
Bram Moolenaar597a4222014-06-25 14:39:50 +02003878#ifdef FEAT_LINEBREAK
3879 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3880 && n_extra == 0 && *p_sbr != NUL)
3881 /* draw indent after showbreak value */
3882 draw_state = WL_BRI;
3883 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3884 /* After the showbreak, draw the breakindent */
3885 draw_state = WL_BRI - 1;
3886
3887 /* draw 'breakindent': indent wrapped text accordingly */
3888 if (draw_state == WL_BRI - 1 && n_extra == 0)
3889 {
3890 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003891 /* if need_showbreak is set, breakindent also applies */
3892 if (wp->w_p_bri && n_extra == 0
3893 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003894# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003895 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003896# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003897 )
3898 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003899 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003900# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003901 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003902 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003903 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003904# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003905 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3906 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003907 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003908# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003909 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003910# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003911 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003912 c_extra = ' ';
3913 n_extra = get_breakindent_win(wp,
3914 ml_get_buf(wp->w_buffer, lnum, FALSE));
3915 /* Correct end of highlighted area for 'breakindent',
3916 * required when 'linebreak' is also set. */
3917 if (tocol == vcol)
3918 tocol += n_extra;
3919 }
3920 }
3921#endif
3922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3924 if (draw_state == WL_SBR - 1 && n_extra == 0)
3925 {
3926 draw_state = WL_SBR;
3927# ifdef FEAT_DIFF
3928 if (filler_todo > 0)
3929 {
3930 /* Draw "deleted" diff line(s). */
3931 if (char2cells(fill_diff) > 1)
3932 c_extra = '-';
3933 else
3934 c_extra = fill_diff;
3935# ifdef FEAT_RIGHTLEFT
3936 if (wp->w_p_rl)
3937 n_extra = col + 1;
3938 else
3939# endif
3940 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003941 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943# endif
3944# ifdef FEAT_LINEBREAK
3945 if (*p_sbr != NUL && need_showbreak)
3946 {
3947 /* Draw 'showbreak' at the start of each broken line. */
3948 p_extra = p_sbr;
3949 c_extra = NUL;
3950 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003951 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003953 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 /* Correct end of highlighted area for 'showbreak',
3955 * required when 'linebreak' is also set. */
3956 if (tocol == vcol)
3957 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003958#ifdef FEAT_SYN_HL
3959 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003960 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003961 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003962 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965# endif
3966 }
3967#endif
3968
3969 if (draw_state == WL_LINE - 1 && n_extra == 0)
3970 {
3971 draw_state = WL_LINE;
3972 if (saved_n_extra)
3973 {
3974 /* Continue item from end of wrapped line. */
3975 n_extra = saved_n_extra;
3976 c_extra = saved_c_extra;
3977 p_extra = saved_p_extra;
3978 char_attr = saved_char_attr;
3979 }
3980 else
3981 char_attr = 0;
3982 }
3983 }
3984
3985 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003986 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988#ifdef FEAT_DIFF
3989 && filler_todo <= 0
3990#endif
3991 )
3992 {
3993 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3994 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003995 /* Pretend we have finished updating the window. Except when
3996 * 'cursorcolumn' is set. */
3997#ifdef FEAT_SYN_HL
3998 if (wp->w_p_cuc)
3999 row = wp->w_cline_row + wp->w_cline_height;
4000 else
4001#endif
4002 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 break;
4004 }
4005
4006 if (draw_state == WL_LINE && area_highlighting)
4007 {
4008 /* handle Visual or match highlighting in this line */
4009 if (vcol == fromcol
4010#ifdef FEAT_MBYTE
4011 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4012 && (*mb_ptr2cells)(ptr) > 1)
4013#endif
4014 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004015 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 && vcol < tocol))
4017 area_attr = attr; /* start highlighting */
4018 else if (area_attr != 0
4019 && (vcol == tocol
4020 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023#ifdef FEAT_SEARCH_EXTRA
4024 if (!n_extra)
4025 {
4026 /*
4027 * Check for start/end of search pattern match.
4028 * After end, check for start/end of next match.
4029 * When another match, have to check for start again.
4030 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004031 * Do this for 'search_hl' and the match list (ordered by
4032 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004034 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004035 cur = wp->w_match_head;
4036 shl_flag = FALSE;
4037 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004039 if (shl_flag == FALSE
4040 && ((cur != NULL
4041 && cur->priority > SEARCH_HL_PRIORITY)
4042 || cur == NULL))
4043 {
4044 shl = &search_hl;
4045 shl_flag = TRUE;
4046 }
4047 else
4048 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004049 if (cur != NULL)
4050 cur->pos.cur = 0;
4051 pos_inprogress = TRUE;
4052 while (shl->rm.regprog != NULL
4053 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004055 if (shl->startcol != MAXCOL
4056 && v >= (long)shl->startcol
4057 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004059#ifdef FEAT_MBYTE
4060 int tmp_col = v + MB_PTR2LEN(ptr);
4061
4062 if (shl->endcol < tmp_col)
4063 shl->endcol = tmp_col;
4064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004066#ifdef FEAT_CONCEAL
4067 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4068 == cur->hlg_id)
4069 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004070 has_match_conc =
4071 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004072 match_conc = cur->conceal_char;
4073 }
4074 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004075 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004078 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
4080 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004081 next_search_hl(wp, shl, lnum, (colnr_T)v,
4082 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004083 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004084 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 /* Need to get the line again, a multi-line regexp
4087 * may have made it invalid. */
4088 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4089 ptr = line + v;
4090
4091 if (shl->lnum == lnum)
4092 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004093 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004095 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004097 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004099 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
4101 /* highlight empty match, try again after
4102 * it */
4103#ifdef FEAT_MBYTE
4104 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004105 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004106 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
4108#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004109 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111
4112 /* Loop to check if the match starts at the
4113 * current position */
4114 continue;
4115 }
4116 }
4117 break;
4118 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004119 if (shl != &search_hl && cur != NULL)
4120 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004122
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004123 /* Use attributes from match with highest priority among
4124 * 'search_hl' and the match list. */
4125 search_attr = search_hl.attr_cur;
4126 cur = wp->w_match_head;
4127 shl_flag = FALSE;
4128 while (cur != NULL || shl_flag == FALSE)
4129 {
4130 if (shl_flag == FALSE
4131 && ((cur != NULL
4132 && cur->priority > SEARCH_HL_PRIORITY)
4133 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004134 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004135 shl = &search_hl;
4136 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004137 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004138 else
4139 shl = &cur->hl;
4140 if (shl->attr_cur != 0)
4141 search_attr = shl->attr_cur;
4142 if (shl != &search_hl && cur != NULL)
4143 cur = cur->next;
4144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146#endif
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004149 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004151 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4152 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004154 if (diff_hlf == HLF_TXD && ptr - line > change_end
4155 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004157 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004158 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004159 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004162
4163 /* Decide which of the highlight attributes to use. */
4164 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004165#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004166 if (area_attr != 0)
4167 char_attr = hl_combine_attr(line_attr, area_attr);
4168 else if (search_attr != 0)
4169 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004170 /* Use line_attr when not in the Visual or 'incsearch' area
4171 * (area_attr may be 0 when "noinvcur" is set). */
4172 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004173 || vcol < fromcol || vcol_prev < fromcol_prev
4174 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004175 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004176#else
4177 if (area_attr != 0)
4178 char_attr = area_attr;
4179 else if (search_attr != 0)
4180 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004181#endif
4182 else
4183 {
4184 attr_pri = FALSE;
4185#ifdef FEAT_SYN_HL
4186 if (has_syntax)
4187 char_attr = syntax_attr;
4188 else
4189#endif
4190 char_attr = 0;
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193
4194 /*
4195 * Get the next character to put on the screen.
4196 */
4197 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004198 * The "p_extra" points to the extra stuff that is inserted to
4199 * represent special characters (non-printable stuff) and other
4200 * things. When all characters are the same, c_extra is used.
4201 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4202 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4204 */
4205 if (n_extra > 0)
4206 {
4207 if (c_extra != NUL)
4208 {
4209 c = c_extra;
4210#ifdef FEAT_MBYTE
4211 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004212 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004215 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004216 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else
4219 mb_utf8 = FALSE;
4220#endif
4221 }
4222 else
4223 {
4224 c = *p_extra;
4225#ifdef FEAT_MBYTE
4226 if (has_mbyte)
4227 {
4228 mb_c = c;
4229 if (enc_utf8)
4230 {
4231 /* If the UTF-8 character is more than one byte:
4232 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004233 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 mb_utf8 = FALSE;
4235 if (mb_l > n_extra)
4236 mb_l = 1;
4237 else if (mb_l > 1)
4238 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004241 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 }
4244 else
4245 {
4246 /* if this is a DBCS character, put it in "mb_c" */
4247 mb_l = MB_BYTE2LEN(c);
4248 if (mb_l >= n_extra)
4249 mb_l = 1;
4250 else if (mb_l > 1)
4251 mb_c = (c << 8) + p_extra[1];
4252 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004253 if (mb_l == 0) /* at the NUL at end-of-line */
4254 mb_l = 1;
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* If a double-width char doesn't fit display a '>' in the
4257 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004258 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259# ifdef FEAT_RIGHTLEFT
4260 wp->w_p_rl ? (col <= 0) :
4261# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004262 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 && (*mb_char2cells)(mb_c) == 2)
4264 {
4265 c = '>';
4266 mb_c = c;
4267 mb_l = 1;
4268 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004269 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 /* put the pointer back to output the double-width
4271 * character at the start of the next line. */
4272 ++n_extra;
4273 --p_extra;
4274 }
4275 else
4276 {
4277 n_extra -= mb_l - 1;
4278 p_extra += mb_l - 1;
4279 }
4280 }
4281#endif
4282 ++p_extra;
4283 }
4284 --n_extra;
4285 }
4286 else
4287 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004288#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004289 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004290#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004291
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004292 if (p_extra_free != NULL)
4293 {
4294 vim_free(p_extra_free);
4295 p_extra_free = NULL;
4296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /*
4298 * Get a character from the line itself.
4299 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004300 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004301#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004302 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#ifdef FEAT_MBYTE
4305 if (has_mbyte)
4306 {
4307 mb_c = c;
4308 if (enc_utf8)
4309 {
4310 /* If the UTF-8 character is more than one byte: Decode it
4311 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004312 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 mb_utf8 = FALSE;
4314 if (mb_l > 1)
4315 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 /* Overlong encoded ASCII or ASCII with composing char
4318 * is displayed normally, except a NUL. */
4319 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004320 {
4321 c = mb_c;
4322# ifdef FEAT_LINEBREAK
4323 c0 = mb_c;
4324# endif
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004327
4328 /* At start of the line we can have a composing char.
4329 * Draw it as a space with a composing char. */
4330 if (utf_iscomposing(mb_c))
4331 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004332 int i;
4333
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004334 for (i = Screen_mco - 1; i > 0; --i)
4335 u8cc[i] = u8cc[i - 1];
4336 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004337 mb_c = ' ';
4338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340
4341 if ((mb_l == 1 && c >= 0x80)
4342 || (mb_l >= 1 && mb_c == 0)
4343 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004344# ifdef UNICODE16
4345 || mb_c >= 0x10000
4346# endif
4347 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 /*
4350 * Illegal UTF-8 byte: display as <xx>.
4351 * Non-BMP character : display as ? or fullwidth ?.
4352 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004353# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004358# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (wp->w_p_rl) /* reverse */
4360 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004363# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 else if (utf_char2cells(mb_c) != 2)
4365 STRCPY(extra, "?");
4366 else
4367 /* 0xff1f in UTF-8: full-width '?' */
4368 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 p_extra = extra;
4372 c = *p_extra;
4373 mb_c = mb_ptr2char_adv(&p_extra);
4374 mb_utf8 = (c >= 0x80);
4375 n_extra = (int)STRLEN(p_extra);
4376 c_extra = NUL;
4377 if (area_attr == 0 && search_attr == 0)
4378 {
4379 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004380 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 saved_attr2 = char_attr; /* save current attr */
4382 }
4383 }
4384 else if (mb_l == 0) /* at the NUL at end-of-line */
4385 mb_l = 1;
4386#ifdef FEAT_ARABIC
4387 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4388 {
4389 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004390 int pc, pc1, nc;
4391 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 /* The idea of what is the previous and next
4394 * character depends on 'rightleft'. */
4395 if (wp->w_p_rl)
4396 {
4397 pc = prev_c;
4398 pc1 = prev_c1;
4399 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004400 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else
4403 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004404 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004406 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408 prev_c = mb_c;
4409
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 else
4413 prev_c = mb_c;
4414#endif
4415 }
4416 else /* enc_dbcs */
4417 {
4418 mb_l = MB_BYTE2LEN(c);
4419 if (mb_l == 0) /* at the NUL at end-of-line */
4420 mb_l = 1;
4421 else if (mb_l > 1)
4422 {
4423 /* We assume a second byte below 32 is illegal.
4424 * Hopefully this is OK for all double-byte encodings!
4425 */
4426 if (ptr[1] >= 32)
4427 mb_c = (c << 8) + ptr[1];
4428 else
4429 {
4430 if (ptr[1] == NUL)
4431 {
4432 /* head byte at end of line */
4433 mb_l = 1;
4434 transchar_nonprint(extra, c);
4435 }
4436 else
4437 {
4438 /* illegal tail byte */
4439 mb_l = 2;
4440 STRCPY(extra, "XX");
4441 }
4442 p_extra = extra;
4443 n_extra = (int)STRLEN(extra) - 1;
4444 c_extra = NUL;
4445 c = *p_extra++;
4446 if (area_attr == 0 && search_attr == 0)
4447 {
4448 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004449 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 saved_attr2 = char_attr; /* save current attr */
4451 }
4452 mb_c = c;
4453 }
4454 }
4455 }
4456 /* If a double-width char doesn't fit display a '>' in the
4457 * last column; the character is displayed at the start of the
4458 * next line. */
4459 if ((
4460# ifdef FEAT_RIGHTLEFT
4461 wp->w_p_rl ? (col <= 0) :
4462# endif
4463 (col >= W_WIDTH(wp) - 1))
4464 && (*mb_char2cells)(mb_c) == 2)
4465 {
4466 c = '>';
4467 mb_c = c;
4468 mb_utf8 = FALSE;
4469 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004470 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 /* Put pointer back so that the character will be
4472 * displayed at the start of the next line. */
4473 --ptr;
4474 }
4475 else if (*ptr != NUL)
4476 ptr += mb_l - 1;
4477
4478 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004479 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004480 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004481 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004484 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 c = ' ';
4486 if (area_attr == 0 && search_attr == 0)
4487 {
4488 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004489 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 saved_attr2 = char_attr; /* save current attr */
4491 }
4492 mb_c = c;
4493 mb_utf8 = FALSE;
4494 mb_l = 1;
4495 }
4496
4497 }
4498#endif
4499 ++ptr;
4500
4501 if (extra_check)
4502 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004503#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004504 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004505#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004506
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004507#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 /* Get syntax attribute, unless still at the start of the line
4509 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510 v = (long)(ptr - line);
4511 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
4513 /* Get the syntax attribute for the character. If there
4514 * is an error, disable syntax highlighting. */
4515 save_did_emsg = did_emsg;
4516 did_emsg = FALSE;
4517
Bram Moolenaar217ad922005-03-20 22:37:15 +00004518 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519# ifdef FEAT_SPELL
4520 has_spell ? &can_spell :
4521# endif
4522 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004525 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004526 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004527 has_syntax = FALSE;
4528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else
4530 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004531#ifdef SYN_TIME_LIMIT
4532 if (wp->w_s->b_syn_slow)
4533 has_syntax = FALSE;
4534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536 /* Need to get the line again, a multi-line regexp may
4537 * have made it invalid. */
4538 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4539 ptr = line + v;
4540
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004541 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004543 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004544 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004545# ifdef FEAT_CONCEAL
4546 /* no concealing past the end of the line, it interferes
4547 * with line highlighting */
4548 if (c == NUL)
4549 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004550 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004551 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004554#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004555
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004556#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004557 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004558 * Only do this when there is no syntax highlighting, the
4559 * @Spell cluster is not used or the current syntax item
4560 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004561 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004562 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004563 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004564# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004565 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004566 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004567# endif
4568 if (c != 0 && (
4569# ifdef FEAT_SYN_HL
4570 !has_syntax ||
4571# endif
4572 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004573 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004574 char_u *prev_ptr, *p;
4575 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004576 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004577# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004578 if (has_mbyte)
4579 {
4580 prev_ptr = ptr - mb_l;
4581 v -= mb_l - 1;
4582 }
4583 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004584# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004585 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004586
4587 /* Use nextline[] if possible, it has the start of the
4588 * next line concatenated. */
4589 if ((prev_ptr - line) - nextlinecol >= 0)
4590 p = nextline + (prev_ptr - line) - nextlinecol;
4591 else
4592 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004593 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004594 len = spell_check(wp, p, &spell_hlf, &cap_col,
4595 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004596 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004598 /* In Insert mode only highlight a word that
4599 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004600 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004601 && (State & INSERT) != 0
4602 && wp->w_cursor.lnum == lnum
4603 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004604 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004605 && wp->w_cursor.col < (colnr_T)word_end)
4606 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004607 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004608 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004609 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004610
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004611 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004612 && (p - nextline) + len > nextline_idx)
4613 {
4614 /* Remember that the good word continues at the
4615 * start of the next line. */
4616 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004617 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004618 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004619
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004620 /* Turn index into actual attributes. */
4621 if (spell_hlf != HLF_COUNT)
4622 spell_attr = highlight_attr[spell_hlf];
4623
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004624 if (cap_col > 0)
4625 {
4626 if (p != prev_ptr
4627 && (p - nextline) + cap_col >= nextline_idx)
4628 {
4629 /* Remember that the word in the next line
4630 * must start with a capital. */
4631 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004632 cap_col = (int)((p - nextline) + cap_col
4633 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004634 }
4635 else
4636 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004637 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004638 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004639 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004640 }
4641 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004642 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004643 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004644 char_attr = hl_combine_attr(char_attr, spell_attr);
4645 else
4646 char_attr = hl_combine_attr(spell_attr, char_attr);
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#endif
4649#ifdef FEAT_LINEBREAK
4650 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004651 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004653 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004654 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004656# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004657 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004658# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004659 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004661 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004663 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004664
Bram Moolenaar597a4222014-06-25 14:39:50 +02004665 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004666 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004667 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004668 if (c == TAB && n_extra + col > W_WIDTH(wp))
4669 n_extra = (int)wp->w_buffer->b_p_ts
4670 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4671
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004672# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004673 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004674# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004676# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004677 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004678 {
4679#ifdef FEAT_CONCEAL
4680 if (c == TAB)
4681 /* See "Tab alignment" below. */
4682 FIX_FOR_BOGUSCOLS;
4683#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004684 if (!wp->w_p_list)
4685 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688#endif
4689
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004690 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4691 */
4692 if (wp->w_p_list
4693 && (((c == 160
4694#ifdef FEAT_MBYTE
4695 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4696#endif
4697 ) && lcs_nbsp)
4698 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4699 {
4700 c = (c == ' ') ? lcs_space : lcs_nbsp;
4701 if (area_attr == 0 && search_attr == 0)
4702 {
4703 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004704 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004705 saved_attr2 = char_attr; /* save current attr */
4706 }
4707#ifdef FEAT_MBYTE
4708 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004709 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004710 {
4711 mb_utf8 = TRUE;
4712 u8cc[0] = 0;
4713 c = 0xc0;
4714 }
4715 else
4716 mb_utf8 = FALSE;
4717#endif
4718 }
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4721 {
4722 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004723 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004726 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 saved_attr2 = char_attr; /* save current attr */
4728 }
4729#ifdef FEAT_MBYTE
4730 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004731 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
4733 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004734 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004735 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 else
4738 mb_utf8 = FALSE;
4739#endif
4740 }
4741 }
4742
4743 /*
4744 * Handling of non-printable characters.
4745 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004746 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 /*
4749 * when getting a character from the file, we may have to
4750 * turn it into something else on the way to putting it
4751 * into "ScreenLines".
4752 */
4753 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4754 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004755 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004756 long vcol_adjusted = vcol; /* removed showbreak length */
4757#ifdef FEAT_LINEBREAK
4758 /* only adjust the tab_len, when at the first column
4759 * after the showbreak value was drawn */
4760 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4761 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004764 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004765 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4766
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004767#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004768 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004769#endif
4770 /* tab amount depends on current column */
4771 n_extra = tab_len;
4772#ifdef FEAT_LINEBREAK
4773 else
4774 {
4775 char_u *p;
4776 int len = n_extra;
4777 int i;
4778 int saved_nextra = n_extra;
4779
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004780#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004781 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004782 /* there are characters to conceal */
4783 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004784 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4785 */
4786 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4787 && n_extra > tab_len)
4788 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004789#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004790
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004791 /* if n_extra > 0, it gives the number of chars, to
4792 * use for a tab, else we need to calculate the width
4793 * for a tab */
4794#ifdef FEAT_MBYTE
4795 len = (tab_len * mb_char2len(lcs_tab2));
4796 if (n_extra > 0)
4797 len += n_extra - tab_len;
4798#endif
4799 c = lcs_tab1;
4800 p = alloc((unsigned)(len + 1));
4801 vim_memset(p, ' ', len);
4802 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004803 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004804 p_extra_free = p;
4805 for (i = 0; i < tab_len; i++)
4806 {
4807#ifdef FEAT_MBYTE
4808 mb_char2bytes(lcs_tab2, p);
4809 p += mb_char2len(lcs_tab2);
4810 n_extra += mb_char2len(lcs_tab2)
4811 - (saved_nextra > 0 ? 1 : 0);
4812#else
4813 p[i] = lcs_tab2;
4814#endif
4815 }
4816 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004817#ifdef FEAT_CONCEAL
4818 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4819 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004820 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004821 n_extra -= vcol_off;
4822#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004823 }
4824#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004825#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004826 {
4827 int vc_saved = vcol_off;
4828
4829 /* Tab alignment should be identical regardless of
4830 * 'conceallevel' value. So tab compensates of all
4831 * previous concealed characters, and thus resets
4832 * vcol_off and boguscols accumulated so far in the
4833 * line. Note that the tab can be longer than
4834 * 'tabstop' when there are concealed characters. */
4835 FIX_FOR_BOGUSCOLS;
4836
4837 /* Make sure, the highlighting for the tab char will be
4838 * correctly set further below (effectively reverts the
4839 * FIX_FOR_BOGSUCOLS macro */
4840 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004841 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004842 tab_len += vc_saved;
4843 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#ifdef FEAT_MBYTE
4846 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4847#endif
4848 if (wp->w_p_list)
4849 {
4850 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004851#ifdef FEAT_LINEBREAK
4852 if (wp->w_p_lbr)
4853 c_extra = NUL; /* using p_extra from above */
4854 else
4855#endif
4856 c_extra = lcs_tab2;
4857 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004858 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 saved_attr2 = char_attr; /* save current attr */
4860#ifdef FEAT_MBYTE
4861 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004862 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
4864 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004865 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004866 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868#endif
4869 }
4870 else
4871 {
4872 c_extra = ' ';
4873 c = ' ';
4874 }
4875 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004876 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004877 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004878 || ((fromcol >= 0 || fromcol_prev >= 0)
4879 && tocol > vcol
4880 && VIsual_mode != Ctrl_V
4881 && (
4882# ifdef FEAT_RIGHTLEFT
4883 wp->w_p_rl ? (col >= 0) :
4884# endif
4885 (col < W_WIDTH(wp)))
4886 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004887 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004888 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004889 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004891 /* Display a '$' after the line or highlight an extra
4892 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4894 /* For a diff line the highlighting continues after the
4895 * "$". */
4896 if (
4897# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004898 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899# ifdef LINE_ATTR
4900 &&
4901# endif
4902# endif
4903# ifdef LINE_ATTR
4904 line_attr == 0
4905# endif
4906 )
4907#endif
4908 {
4909#ifdef FEAT_VIRTUALEDIT
4910 /* In virtualedit, visual selections may extend
4911 * beyond end of line. */
4912 if (area_highlighting && virtual_active()
4913 && tocol != MAXCOL && vcol < tocol)
4914 n_extra = 0;
4915 else
4916#endif
4917 {
4918 p_extra = at_end_str;
4919 n_extra = 1;
4920 c_extra = NUL;
4921 }
4922 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004923 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004924 c = lcs_eol;
4925 else
4926 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 lcs_eol_one = -1;
4928 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004929 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004931 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 n_attr = 1;
4933 }
4934#ifdef FEAT_MBYTE
4935 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004936 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004939 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004940 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 else
4943 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4944#endif
4945 }
4946 else if (c != NUL)
4947 {
4948 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004949 if (n_extra == 0)
4950 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951#ifdef FEAT_RIGHTLEFT
4952 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4953 rl_mirror(p_extra); /* reverse "<12>" */
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004956#ifdef FEAT_LINEBREAK
4957 if (wp->w_p_lbr)
4958 {
4959 char_u *p;
4960
4961 c = *p_extra;
4962 p = alloc((unsigned)n_extra + 1);
4963 vim_memset(p, ' ', n_extra);
4964 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4965 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004966 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004967 p_extra_free = p_extra = p;
4968 }
4969 else
4970#endif
4971 {
4972 n_extra = byte2cells(c) - 1;
4973 c = *p_extra++;
4974 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004975 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004978 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 saved_attr2 = char_attr; /* save current attr */
4980 }
4981#ifdef FEAT_MBYTE
4982 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4983#endif
4984 }
4985#ifdef FEAT_VIRTUALEDIT
4986 else if (VIsual_active
4987 && (VIsual_mode == Ctrl_V
4988 || VIsual_mode == 'v')
4989 && virtual_active()
4990 && tocol != MAXCOL
4991 && vcol < tocol
4992 && (
4993# ifdef FEAT_RIGHTLEFT
4994 wp->w_p_rl ? (col >= 0) :
4995# endif
4996 (col < W_WIDTH(wp))))
4997 {
4998 c = ' ';
4999 --ptr; /* put it back at the NUL */
5000 }
5001#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005002#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else if ((
5004# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005005 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 ) && (
5009# ifdef FEAT_RIGHTLEFT
5010 wp->w_p_rl ? (col >= 0) :
5011# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005012 (col
5013# ifdef FEAT_CONCEAL
5014 - boguscols
5015# endif
5016 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 /* Highlight until the right side of the window */
5019 c = ' ';
5020 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005021
5022 /* Remember we do the char for line highlighting. */
5023 ++did_line_attr;
5024
5025 /* don't do search HL for the rest of the line */
5026 if (line_attr != 0 && char_attr == search_attr && col > 0)
5027 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028# ifdef FEAT_DIFF
5029 if (diff_hlf == HLF_TXD)
5030 {
5031 diff_hlf = HLF_CHD;
5032 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005033 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005034 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005035 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5036 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005037 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040# endif
5041 }
5042#endif
5043 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005044
5045#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005046 if ( wp->w_p_cole > 0
5047 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005048 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005049 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005050 && !(lnum_in_visual_area
5051 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005052 {
5053 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005054 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005055 && (syn_get_sub_char() != NUL || match_conc
5056 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005057 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005058 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005059 /* First time at this concealed item: display one
5060 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005061 if (match_conc)
5062 c = match_conc;
5063 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005064 c = syn_get_sub_char();
5065 else if (lcs_conceal != NUL)
5066 c = lcs_conceal;
5067 else
5068 c = ' ';
5069
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005070 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005071
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005072 if (n_extra > 0)
5073 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005074 vcol += n_extra;
5075 if (wp->w_p_wrap && n_extra > 0)
5076 {
5077# ifdef FEAT_RIGHTLEFT
5078 if (wp->w_p_rl)
5079 {
5080 col -= n_extra;
5081 boguscols -= n_extra;
5082 }
5083 else
5084# endif
5085 {
5086 boguscols += n_extra;
5087 col += n_extra;
5088 }
5089 }
5090 n_extra = 0;
5091 n_attr = 0;
5092 }
5093 else if (n_skip == 0)
5094 {
5095 is_concealing = TRUE;
5096 n_skip = 1;
5097 }
5098# ifdef FEAT_MBYTE
5099 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005100 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005101 {
5102 mb_utf8 = TRUE;
5103 u8cc[0] = 0;
5104 c = 0xc0;
5105 }
5106 else
5107 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5108# endif
5109 }
5110 else
5111 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005112 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005113 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005114 }
5115#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005118#ifdef FEAT_CONCEAL
5119 /* In the cursor line and we may be concealing characters: correct
5120 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005121 if (!did_wcol && draw_state == WL_LINE
5122 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005123 && conceal_cursor_line(wp)
5124 && (int)wp->w_virtcol <= vcol + n_skip)
5125 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005126# ifdef FEAT_RIGHTLEFT
5127 if (wp->w_p_rl)
5128 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5129 else
5130# endif
5131 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005132 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005133 did_wcol = TRUE;
5134 }
5135#endif
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 /* Don't override visual selection highlighting. */
5138 if (n_attr > 0
5139 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005140 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 char_attr = extra_attr;
5142
Bram Moolenaar81695252004-12-29 20:58:21 +00005143#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 /* XIM don't send preedit_start and preedit_end, but they send
5145 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5146 * im_is_preediting() here. */
5147 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005148 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 && (State & INSERT)
5150 && !p_imdisable
5151 && im_is_preediting()
5152 && draw_state == WL_LINE)
5153 {
5154 colnr_T tcol;
5155
5156 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005157 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
5159 tcol = preedit_end_col;
5160 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5161 {
5162 if (feedback_old_attr < 0)
5163 {
5164 feedback_col = 0;
5165 feedback_old_attr = char_attr;
5166 }
5167 char_attr = im_get_feedback_attr(feedback_col);
5168 if (char_attr < 0)
5169 char_attr = feedback_old_attr;
5170 feedback_col++;
5171 }
5172 else if (feedback_old_attr >= 0)
5173 {
5174 char_attr = feedback_old_attr;
5175 feedback_old_attr = -1;
5176 feedback_col = 0;
5177 }
5178 }
5179#endif
5180 /*
5181 * Handle the case where we are in column 0 but not on the first
5182 * character of the line and the user wants us to show us a
5183 * special character (via 'listchars' option "precedes:<char>".
5184 */
5185 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005186 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5188#ifdef FEAT_DIFF
5189 && filler_todo <= 0
5190#endif
5191 && draw_state > WL_NR
5192 && c != NUL)
5193 {
5194 c = lcs_prec;
5195 lcs_prec_todo = NUL;
5196#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005197 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5198 {
5199 /* Double-width character being overwritten by the "precedes"
5200 * character, need to fill up half the character. */
5201 c_extra = MB_FILLER_CHAR;
5202 n_extra = 1;
5203 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005204 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005207 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
5209 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005210 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005211 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 else
5214 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5215#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005216 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005219 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 n_attr3 = 1;
5221 }
5222 }
5223
5224 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005225 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005227 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005228#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005229 || did_line_attr == 1
5230#endif
5231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005233#ifdef FEAT_SEARCH_EXTRA
5234 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005235
5236 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005237 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005238 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005239#endif
5240
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005241 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 * highlight match at end of line. If it's beyond the last
5243 * char on the screen, just overwrite that one (tricky!) Not
5244 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005245#ifdef FEAT_SEARCH_EXTRA
5246 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005247 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005248 prevcol_hl_flag = TRUE;
5249 else
5250 {
5251 cur = wp->w_match_head;
5252 while (cur != NULL)
5253 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005254 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005255 {
5256 prevcol_hl_flag = TRUE;
5257 break;
5258 }
5259 cur = cur->next;
5260 }
5261 }
5262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005264 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005265 && (VIsual_mode != Ctrl_V
5266 || lnum == VIsual.lnum
5267 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005268 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#ifdef FEAT_SEARCH_EXTRA
5270 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005271 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005272# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005273 && did_line_attr <= 1
5274# endif
5275 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#endif
5277 ))
5278 {
5279 int n = 0;
5280
5281#ifdef FEAT_RIGHTLEFT
5282 if (wp->w_p_rl)
5283 {
5284 if (col < 0)
5285 n = 1;
5286 }
5287 else
5288#endif
5289 {
5290 if (col >= W_WIDTH(wp))
5291 n = -1;
5292 }
5293 if (n != 0)
5294 {
5295 /* At the window boundary, highlight the last character
5296 * instead (better than nothing). */
5297 off += n;
5298 col += n;
5299 }
5300 else
5301 {
5302 /* Add a blank character to highlight. */
5303 ScreenLines[off] = ' ';
5304#ifdef FEAT_MBYTE
5305 if (enc_utf8)
5306 ScreenLinesUC[off] = 0;
5307#endif
5308 }
5309#ifdef FEAT_SEARCH_EXTRA
5310 if (area_attr == 0)
5311 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005312 /* Use attributes from match with highest priority among
5313 * 'search_hl' and the match list. */
5314 char_attr = search_hl.attr;
5315 cur = wp->w_match_head;
5316 shl_flag = FALSE;
5317 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005318 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005319 if (shl_flag == FALSE
5320 && ((cur != NULL
5321 && cur->priority > SEARCH_HL_PRIORITY)
5322 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005323 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005324 shl = &search_hl;
5325 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005326 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005327 else
5328 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005329 if ((ptr - line) - 1 == (long)shl->startcol
5330 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005331 char_attr = shl->attr;
5332 if (shl != &search_hl && cur != NULL)
5333 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336#endif
5337 ScreenAttrs[off] = char_attr;
5338#ifdef FEAT_RIGHTLEFT
5339 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005342 --off;
5343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 else
5345#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005348 ++off;
5349 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005350 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005351#ifdef FEAT_SYN_HL
5352 eol_hl_off = 1;
5353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaar91170f82006-05-05 21:15:17 +00005357 /*
5358 * At end of the text line.
5359 */
5360 if (c == NUL)
5361 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005362#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005363 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5364 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005365 {
5366 /* highlight last char after line */
5367 --col;
5368 --off;
5369 --vcol;
5370 }
5371
Bram Moolenaar1a384422010-07-14 19:53:30 +02005372 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005373 if (wp->w_p_wrap)
5374 v = wp->w_skipcol;
5375 else
5376 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005377
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005378 /* check if line ends before left margin */
5379 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005380 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005381#ifdef FEAT_CONCEAL
5382 /* Get rid of the boguscols now, we want to draw until the right
5383 * edge for 'cursorcolumn'. */
5384 col -= boguscols;
5385 boguscols = 0;
5386#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005387
5388 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005389 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005390
5391 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005392 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5393 && (int)wp->w_virtcol <
5394 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005395 && lnum != wp->w_cursor.lnum)
5396 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005397# ifdef FEAT_RIGHTLEFT
5398 && !wp->w_p_rl
5399# endif
5400 )
5401 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005402 int rightmost_vcol = 0;
5403 int i;
5404
5405 if (wp->w_p_cuc)
5406 rightmost_vcol = wp->w_virtcol;
5407 if (draw_color_col)
5408 /* determine rightmost colorcolumn to possibly draw */
5409 for (i = 0; color_cols[i] >= 0; ++i)
5410 if (rightmost_vcol < color_cols[i])
5411 rightmost_vcol = color_cols[i];
5412
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005413 while (col < W_WIDTH(wp))
5414 {
5415 ScreenLines[off] = ' ';
5416#ifdef FEAT_MBYTE
5417 if (enc_utf8)
5418 ScreenLinesUC[off] = 0;
5419#endif
5420 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005421 if (draw_color_col)
5422 draw_color_col = advance_color_col(VCOL_HLC,
5423 &color_cols);
5424
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005425 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005426 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005427 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005428 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005429 else
5430 ScreenAttrs[off++] = 0;
5431
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005432 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005433 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005434
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005435 ++vcol;
5436 }
5437 }
5438#endif
5439
Bram Moolenaar860cae12010-06-05 23:22:07 +02005440 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5441 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 row++;
5443
5444 /*
5445 * Update w_cline_height and w_cline_folded if the cursor line was
5446 * updated (saves a call to plines() later).
5447 */
5448 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5449 {
5450 curwin->w_cline_row = startrow;
5451 curwin->w_cline_height = row - startrow;
5452#ifdef FEAT_FOLDING
5453 curwin->w_cline_folded = FALSE;
5454#endif
5455 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5456 }
5457
5458 break;
5459 }
5460
5461 /* line continues beyond line end */
5462 if (lcs_ext
5463 && !wp->w_p_wrap
5464#ifdef FEAT_DIFF
5465 && filler_todo <= 0
5466#endif
5467 && (
5468#ifdef FEAT_RIGHTLEFT
5469 wp->w_p_rl ? col == 0 :
5470#endif
5471 col == W_WIDTH(wp) - 1)
5472 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005473 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5475 {
5476 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005477 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478#ifdef FEAT_MBYTE
5479 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005480 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005483 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005484 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 mb_utf8 = FALSE;
5488#endif
5489 }
5490
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005491#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492 /* advance to the next 'colorcolumn' */
5493 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005494 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005495
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005496 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497 * highlight the cursor position itself.
5498 * Also highlight the 'colorcolumn' if it is different than
5499 * 'cursorcolumn' */
5500 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005501 if (draw_state == WL_LINE && !lnum_in_visual_area
5502 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005503 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005504 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505 && lnum != wp->w_cursor.lnum)
5506 {
5507 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005508 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005509 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005510 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005511 {
5512 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005513 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005514 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005515 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005516#endif
5517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 /*
5519 * Store character to be displayed.
5520 * Skip characters that are left of the screen for 'nowrap'.
5521 */
5522 vcol_prev = vcol;
5523 if (draw_state < WL_LINE || n_skip <= 0)
5524 {
5525 /*
5526 * Store the character.
5527 */
5528#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5529 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5530 {
5531 /* A double-wide character is: put first halve in left cell. */
5532 --off;
5533 --col;
5534 }
5535#endif
5536 ScreenLines[off] = c;
5537#ifdef FEAT_MBYTE
5538 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005539 {
5540 if ((mb_c & 0xff00) == 0x8e00)
5541 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else if (enc_utf8)
5545 {
5546 if (mb_utf8)
5547 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005548 int i;
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005551 if ((c & 0xff) == 0)
5552 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005553 for (i = 0; i < Screen_mco; ++i)
5554 {
5555 ScreenLinesC[i][off] = u8cc[i];
5556 if (u8cc[i] == 0)
5557 break;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
5560 else
5561 ScreenLinesUC[off] = 0;
5562 }
5563 if (multi_attr)
5564 {
5565 ScreenAttrs[off] = multi_attr;
5566 multi_attr = 0;
5567 }
5568 else
5569#endif
5570 ScreenAttrs[off] = char_attr;
5571
5572#ifdef FEAT_MBYTE
5573 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5574 {
5575 /* Need to fill two screen columns. */
5576 ++off;
5577 ++col;
5578 if (enc_utf8)
5579 /* UTF-8: Put a 0 in the second screen char. */
5580 ScreenLines[off] = 0;
5581 else
5582 /* DBCS: Put second byte in the second screen char. */
5583 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005584 if (draw_state > WL_NR
5585#ifdef FEAT_DIFF
5586 && filler_todo <= 0
5587#endif
5588 )
5589 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 /* When "tocol" is halfway a character, set it to the end of
5591 * the character, otherwise highlighting won't stop. */
5592 if (tocol == vcol)
5593 ++tocol;
5594#ifdef FEAT_RIGHTLEFT
5595 if (wp->w_p_rl)
5596 {
5597 /* now it's time to backup one cell */
5598 --off;
5599 --col;
5600 }
5601#endif
5602 }
5603#endif
5604#ifdef FEAT_RIGHTLEFT
5605 if (wp->w_p_rl)
5606 {
5607 --off;
5608 --col;
5609 }
5610 else
5611#endif
5612 {
5613 ++off;
5614 ++col;
5615 }
5616 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005617#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005618 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005619 {
5620 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005621 ++vcol_off;
5622 if (n_extra > 0)
5623 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005624 if (wp->w_p_wrap)
5625 {
5626 /*
5627 * Special voodoo required if 'wrap' is on.
5628 *
5629 * Advance the column indicator to force the line
5630 * drawing to wrap early. This will make the line
5631 * take up the same screen space when parts are concealed,
5632 * so that cursor line computations aren't messed up.
5633 *
5634 * To avoid the fictitious advance of 'col' causing
5635 * trailing junk to be written out of the screen line
5636 * we are building, 'boguscols' keeps track of the number
5637 * of bad columns we have advanced.
5638 */
5639 if (n_extra > 0)
5640 {
5641 vcol += n_extra;
5642# ifdef FEAT_RIGHTLEFT
5643 if (wp->w_p_rl)
5644 {
5645 col -= n_extra;
5646 boguscols -= n_extra;
5647 }
5648 else
5649# endif
5650 {
5651 col += n_extra;
5652 boguscols += n_extra;
5653 }
5654 n_extra = 0;
5655 n_attr = 0;
5656 }
5657
5658
5659# ifdef FEAT_MBYTE
5660 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5661 {
5662 /* Need to fill two screen columns. */
5663# ifdef FEAT_RIGHTLEFT
5664 if (wp->w_p_rl)
5665 {
5666 --boguscols;
5667 --col;
5668 }
5669 else
5670# endif
5671 {
5672 ++boguscols;
5673 ++col;
5674 }
5675 }
5676# endif
5677
5678# ifdef FEAT_RIGHTLEFT
5679 if (wp->w_p_rl)
5680 {
5681 --boguscols;
5682 --col;
5683 }
5684 else
5685# endif
5686 {
5687 ++boguscols;
5688 ++col;
5689 }
5690 }
5691 else
5692 {
5693 if (n_extra > 0)
5694 {
5695 vcol += n_extra;
5696 n_extra = 0;
5697 n_attr = 0;
5698 }
5699 }
5700
5701 }
5702#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else
5704 --n_skip;
5705
Bram Moolenaar64486672010-05-16 15:46:46 +02005706 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5707 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005708 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#ifdef FEAT_DIFF
5710 && filler_todo <= 0
5711#endif
5712 )
5713 ++vcol;
5714
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005715#ifdef FEAT_SYN_HL
5716 if (vcol_save_attr >= 0)
5717 char_attr = vcol_save_attr;
5718#endif
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 /* restore attributes after "predeces" in 'listchars' */
5721 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5722 char_attr = saved_attr3;
5723
5724 /* restore attributes after last 'listchars' or 'number' char */
5725 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5726 char_attr = saved_attr2;
5727
5728 /*
5729 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005730 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 */
5732 if ((
5733#ifdef FEAT_RIGHTLEFT
5734 wp->w_p_rl ? (col < 0) :
5735#endif
5736 (col >= W_WIDTH(wp)))
5737 && (*ptr != NUL
5738#ifdef FEAT_DIFF
5739 || filler_todo > 0
5740#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005741 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5743 )
5744 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005745#ifdef FEAT_CONCEAL
5746 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5747 (int)W_WIDTH(wp), wp->w_p_rl);
5748 boguscols = 0;
5749#else
5750 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5751 (int)W_WIDTH(wp), wp->w_p_rl);
5752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 ++row;
5754 ++screen_row;
5755
5756 /* When not wrapping and finished diff lines, or when displayed
5757 * '$' and highlighting until last column, break here. */
5758 if ((!wp->w_p_wrap
5759#ifdef FEAT_DIFF
5760 && filler_todo <= 0
5761#endif
5762 ) || lcs_eol_one == -1)
5763 break;
5764
5765 /* When the window is too narrow draw all "@" lines. */
5766 if (draw_state != WL_LINE
5767#ifdef FEAT_DIFF
5768 && filler_todo <= 0
5769#endif
5770 )
5771 {
5772 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005773#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 draw_vsep_win(wp, row);
5775#endif
5776 row = endrow;
5777 }
5778
5779 /* When line got too long for screen break here. */
5780 if (row == endrow)
5781 {
5782 ++row;
5783 break;
5784 }
5785
5786 if (screen_cur_row == screen_row - 1
5787#ifdef FEAT_DIFF
5788 && filler_todo <= 0
5789#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005790#ifdef FEAT_WINDOWS
5791 && W_WIDTH(wp) == Columns
5792#endif
5793 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 /* Remember that the line wraps, used for modeless copy. */
5796 LineWraps[screen_row - 1] = TRUE;
5797
5798 /*
5799 * Special trick to make copy/paste of wrapped lines work with
5800 * xterm/screen: write an extra character beyond the end of
5801 * the line. This will work with all terminal types
5802 * (regardless of the xn,am settings).
5803 * Only do this on a fast tty.
5804 * Only do this if the cursor is on the current line
5805 * (something has been written in it).
5806 * Don't do this for the GUI.
5807 * Don't do this for double-width characters.
5808 * Don't do this for a window not at the right screen border.
5809 */
5810 if (p_tf
5811#ifdef FEAT_GUI
5812 && !gui.in_use
5813#endif
5814#ifdef FEAT_MBYTE
5815 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005816 && ((*mb_off2cells)(LineOffset[screen_row],
5817 LineOffset[screen_row] + screen_Columns)
5818 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005820 + (int)Columns - 2,
5821 LineOffset[screen_row] + screen_Columns)
5822 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#endif
5824 )
5825 {
5826 /* First make sure we are at the end of the screen line,
5827 * then output the same character again to let the
5828 * terminal know about the wrap. If the terminal doesn't
5829 * auto-wrap, we overwrite the character. */
5830 if (screen_cur_col != W_WIDTH(wp))
5831 screen_char(LineOffset[screen_row - 1]
5832 + (unsigned)Columns - 1,
5833 screen_row - 1, (int)(Columns - 1));
5834
5835#ifdef FEAT_MBYTE
5836 /* When there is a multi-byte character, just output a
5837 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005838 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5839 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 out_char(' ');
5841 else
5842#endif
5843 out_char(ScreenLines[LineOffset[screen_row - 1]
5844 + (Columns - 1)]);
5845 /* force a redraw of the first char on the next line */
5846 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5847 screen_start(); /* don't know where cursor is now */
5848 }
5849 }
5850
5851 col = 0;
5852 off = (unsigned)(current_ScreenLine - ScreenLines);
5853#ifdef FEAT_RIGHTLEFT
5854 if (wp->w_p_rl)
5855 {
5856 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5857 off += col;
5858 }
5859#endif
5860
5861 /* reset the drawing state for the start of a wrapped line */
5862 draw_state = WL_START;
5863 saved_n_extra = n_extra;
5864 saved_p_extra = p_extra;
5865 saved_c_extra = c_extra;
5866 saved_char_attr = char_attr;
5867 n_extra = 0;
5868 lcs_prec_todo = lcs_prec;
5869#ifdef FEAT_LINEBREAK
5870# ifdef FEAT_DIFF
5871 if (filler_todo <= 0)
5872# endif
5873 need_showbreak = TRUE;
5874#endif
5875#ifdef FEAT_DIFF
5876 --filler_todo;
5877 /* When the filler lines are actually below the last line of the
5878 * file, don't draw the line itself, break here. */
5879 if (filler_todo == 0 && wp->w_botfill)
5880 break;
5881#endif
5882 }
5883
5884 } /* for every character in the line */
5885
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005886#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005887 /* After an empty line check first word for capital. */
5888 if (*skipwhite(line) == NUL)
5889 {
5890 capcol_lnum = lnum + 1;
5891 cap_col = 0;
5892 }
5893#endif
5894
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005895 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return row;
5897}
5898
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005899#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005900static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005901
5902/*
5903 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005904 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005905 */
5906 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005907comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005908{
5909 int i;
5910
5911 for (i = 0; i < Screen_mco; ++i)
5912 {
5913 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5914 return TRUE;
5915 if (ScreenLinesC[i][off_from] == 0)
5916 break;
5917 }
5918 return FALSE;
5919}
5920#endif
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922/*
5923 * Check whether the given character needs redrawing:
5924 * - the (first byte of the) character is different
5925 * - the attributes are different
5926 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005927 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 */
5929 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005930char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 if (cols > 0
5933 && ((ScreenLines[off_from] != ScreenLines[off_to]
5934 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5935
5936#ifdef FEAT_MBYTE
5937 || (enc_dbcs != 0
5938 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5939 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5940 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5941 : (cols > 1 && ScreenLines[off_from + 1]
5942 != ScreenLines[off_to + 1])))
5943 || (enc_utf8
5944 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5945 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005946 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005947 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5948 && ScreenLines[off_from + 1]
5949 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
5951 ))
5952 return TRUE;
5953 return FALSE;
5954}
5955
5956/*
5957 * Move one "cooked" screen line to the screen, but only the characters that
5958 * have actually changed. Handle insert/delete character.
5959 * "coloff" gives the first column on the screen for this line.
5960 * "endcol" gives the columns where valid characters are.
5961 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5962 * needs to be cleared, negative otherwise.
5963 * "rlflag" is TRUE in a rightleft window:
5964 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5965 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5966 */
5967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005968screen_line(
5969 int row,
5970 int coloff,
5971 int endcol,
5972 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005974 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 unsigned off_from;
5979 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005980#ifdef FEAT_MBYTE
5981 unsigned max_off_from;
5982 unsigned max_off_to;
5983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005985#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 int hl;
5987#endif
5988 int force = FALSE; /* force update rest of the line */
5989 int redraw_this /* bool: does character need redraw? */
5990#ifdef FEAT_GUI
5991 = TRUE /* For GUI when while-loop empty */
5992#endif
5993 ;
5994 int redraw_next; /* redraw_this for next character */
5995#ifdef FEAT_MBYTE
5996 int clear_next = FALSE;
5997 int char_cells; /* 1: normal char */
5998 /* 2: occupies two display cells */
5999# define CHAR_CELLS char_cells
6000#else
6001# define CHAR_CELLS 1
6002#endif
6003
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006004 /* Check for illegal row and col, just in case. */
6005 if (row >= Rows)
6006 row = Rows - 1;
6007 if (endcol > Columns)
6008 endcol = Columns;
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010# ifdef FEAT_CLIPBOARD
6011 clip_may_clear_selection(row, row);
6012# endif
6013
6014 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6015 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006016#ifdef FEAT_MBYTE
6017 max_off_from = off_from + screen_Columns;
6018 max_off_to = LineOffset[row] + screen_Columns;
6019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021#ifdef FEAT_RIGHTLEFT
6022 if (rlflag)
6023 {
6024 /* Clear rest first, because it's left of the text. */
6025 if (clear_width > 0)
6026 {
6027 while (col <= endcol && ScreenLines[off_to] == ' '
6028 && ScreenAttrs[off_to] == 0
6029# ifdef FEAT_MBYTE
6030 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6031# endif
6032 )
6033 {
6034 ++off_to;
6035 ++col;
6036 }
6037 if (col <= endcol)
6038 screen_fill(row, row + 1, col + coloff,
6039 endcol + coloff + 1, ' ', ' ', 0);
6040 }
6041 col = endcol + 1;
6042 off_to = LineOffset[row] + col + coloff;
6043 off_from += col;
6044 endcol = (clear_width > 0 ? clear_width : -clear_width);
6045 }
6046#endif /* FEAT_RIGHTLEFT */
6047
6048 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6049
6050 while (col < endcol)
6051 {
6052#ifdef FEAT_MBYTE
6053 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006054 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 else
6056 char_cells = 1;
6057#endif
6058
6059 redraw_this = redraw_next;
6060 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6061 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6062
6063#ifdef FEAT_GUI
6064 /* If the next character was bold, then redraw the current character to
6065 * remove any pixels that might have spilt over into us. This only
6066 * happens in the GUI.
6067 */
6068 if (redraw_next && gui.in_use)
6069 {
6070 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006071 if (hl > HL_ALL)
6072 hl = syn_attr2attr(hl);
6073 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 redraw_this = TRUE;
6075 }
6076#endif
6077
6078 if (redraw_this)
6079 {
6080 /*
6081 * Special handling when 'xs' termcap flag set (hpterm):
6082 * Attributes for characters are stored at the position where the
6083 * cursor is when writing the highlighting code. The
6084 * start-highlighting code must be written with the cursor on the
6085 * first highlighted character. The stop-highlighting code must
6086 * be written with the cursor just after the last highlighted
6087 * character.
6088 * Overwriting a character doesn't remove it's highlighting. Need
6089 * to clear the rest of the line, and force redrawing it
6090 * completely.
6091 */
6092 if ( p_wiv
6093 && !force
6094#ifdef FEAT_GUI
6095 && !gui.in_use
6096#endif
6097 && ScreenAttrs[off_to] != 0
6098 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6099 {
6100 /*
6101 * Need to remove highlighting attributes here.
6102 */
6103 windgoto(row, col + coloff);
6104 out_str(T_CE); /* clear rest of this screen line */
6105 screen_start(); /* don't know where cursor is now */
6106 force = TRUE; /* force redraw of rest of the line */
6107 redraw_next = TRUE; /* or else next char would miss out */
6108
6109 /*
6110 * If the previous character was highlighted, need to stop
6111 * highlighting at this character.
6112 */
6113 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6114 {
6115 screen_attr = ScreenAttrs[off_to - 1];
6116 term_windgoto(row, col + coloff);
6117 screen_stop_highlight();
6118 }
6119 else
6120 screen_attr = 0; /* highlighting has stopped */
6121 }
6122#ifdef FEAT_MBYTE
6123 if (enc_dbcs != 0)
6124 {
6125 /* Check if overwriting a double-byte with a single-byte or
6126 * the other way around requires another character to be
6127 * redrawn. For UTF-8 this isn't needed, because comparing
6128 * ScreenLinesUC[] is sufficient. */
6129 if (char_cells == 1
6130 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006131 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 /* Writing a single-cell character over a double-cell
6134 * character: need to redraw the next cell. */
6135 ScreenLines[off_to + 1] = 0;
6136 redraw_next = TRUE;
6137 }
6138 else if (char_cells == 2
6139 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006140 && (*mb_off2cells)(off_to, max_off_to) == 1
6141 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
6143 /* Writing the second half of a double-cell character over
6144 * a double-cell character: need to redraw the second
6145 * cell. */
6146 ScreenLines[off_to + 2] = 0;
6147 redraw_next = TRUE;
6148 }
6149
6150 if (enc_dbcs == DBCS_JPNU)
6151 ScreenLines2[off_to] = ScreenLines2[off_from];
6152 }
6153 /* When writing a single-width character over a double-width
6154 * character and at the end of the redrawn text, need to clear out
6155 * the right halve of the old character.
6156 * Also required when writing the right halve of a double-width
6157 * char over the left halve of an existing one. */
6158 if (has_mbyte && col + char_cells == endcol
6159 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006160 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006162 && (*mb_off2cells)(off_to, max_off_to) == 1
6163 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 clear_next = TRUE;
6165#endif
6166
6167 ScreenLines[off_to] = ScreenLines[off_from];
6168#ifdef FEAT_MBYTE
6169 if (enc_utf8)
6170 {
6171 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6172 if (ScreenLinesUC[off_from] != 0)
6173 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006174 int i;
6175
6176 for (i = 0; i < Screen_mco; ++i)
6177 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
6179 }
6180 if (char_cells == 2)
6181 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6182#endif
6183
6184#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006185 /* The bold trick makes a single column of pixels appear in the
6186 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 * character should be redrawn too. This happens for our own GUI
6188 * and for some xterms. */
6189 if (
6190# ifdef FEAT_GUI
6191 gui.in_use
6192# endif
6193# if defined(FEAT_GUI) && defined(UNIX)
6194 ||
6195# endif
6196# ifdef UNIX
6197 term_is_xterm
6198# endif
6199 )
6200 {
6201 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006202 if (hl > HL_ALL)
6203 hl = syn_attr2attr(hl);
6204 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 redraw_next = TRUE;
6206 }
6207#endif
6208 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6209#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006210 /* For simplicity set the attributes of second half of a
6211 * double-wide character equal to the first half. */
6212 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006214
6215 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 else
6218#endif
6219 screen_char(off_to, row, col + coloff);
6220 }
6221 else if ( p_wiv
6222#ifdef FEAT_GUI
6223 && !gui.in_use
6224#endif
6225 && col + coloff > 0)
6226 {
6227 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6228 {
6229 /*
6230 * Don't output stop-highlight when moving the cursor, it will
6231 * stop the highlighting when it should continue.
6232 */
6233 screen_attr = 0;
6234 }
6235 else if (screen_attr != 0)
6236 screen_stop_highlight();
6237 }
6238
6239 off_to += CHAR_CELLS;
6240 off_from += CHAR_CELLS;
6241 col += CHAR_CELLS;
6242 }
6243
6244#ifdef FEAT_MBYTE
6245 if (clear_next)
6246 {
6247 /* Clear the second half of a double-wide character of which the left
6248 * half was overwritten with a single-wide character. */
6249 ScreenLines[off_to] = ' ';
6250 if (enc_utf8)
6251 ScreenLinesUC[off_to] = 0;
6252 screen_char(off_to, row, col + coloff);
6253 }
6254#endif
6255
6256 if (clear_width > 0
6257#ifdef FEAT_RIGHTLEFT
6258 && !rlflag
6259#endif
6260 )
6261 {
6262#ifdef FEAT_GUI
6263 int startCol = col;
6264#endif
6265
6266 /* blank out the rest of the line */
6267 while (col < clear_width && ScreenLines[off_to] == ' '
6268 && ScreenAttrs[off_to] == 0
6269#ifdef FEAT_MBYTE
6270 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6271#endif
6272 )
6273 {
6274 ++off_to;
6275 ++col;
6276 }
6277 if (col < clear_width)
6278 {
6279#ifdef FEAT_GUI
6280 /*
6281 * In the GUI, clearing the rest of the line may leave pixels
6282 * behind if the first character cleared was bold. Some bold
6283 * fonts spill over the left. In this case we redraw the previous
6284 * character too. If we didn't skip any blanks above, then we
6285 * only redraw if the character wasn't already redrawn anyway.
6286 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006287 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
6289 hl = ScreenAttrs[off_to];
6290 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006291 {
6292 int prev_cells = 1;
6293# ifdef FEAT_MBYTE
6294 if (enc_utf8)
6295 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6296 * that its width is 2. */
6297 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6298 else if (enc_dbcs != 0)
6299 {
6300 /* find previous character by counting from first
6301 * column and get its width. */
6302 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006303 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006304
6305 while (off < off_to)
6306 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006307 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006308 off += prev_cells;
6309 }
6310 }
6311
6312 if (enc_dbcs != 0 && prev_cells > 1)
6313 screen_char_2(off_to - prev_cells, row,
6314 col + coloff - prev_cells);
6315 else
6316# endif
6317 screen_char(off_to - prev_cells, row,
6318 col + coloff - prev_cells);
6319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321#endif
6322 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6323 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006324#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 off_to += clear_width - col;
6326 col = clear_width;
6327#endif
6328 }
6329 }
6330
6331 if (clear_width > 0)
6332 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006333#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 /* For a window that's left of another, draw the separator char. */
6335 if (col + coloff < Columns)
6336 {
6337 int c;
6338
6339 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006340 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006342 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6343 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344# endif
6345 || ScreenAttrs[off_to] != hl)
6346 {
6347 ScreenLines[off_to] = c;
6348 ScreenAttrs[off_to] = hl;
6349# ifdef FEAT_MBYTE
6350 if (enc_utf8)
6351 {
6352 if (c >= 0x80)
6353 {
6354 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006355 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357 else
6358 ScreenLinesUC[off_to] = 0;
6359 }
6360# endif
6361 screen_char(off_to, row, col + coloff);
6362 }
6363 }
6364 else
6365#endif
6366 LineWraps[row] = FALSE;
6367 }
6368}
6369
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006370#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006372 * Mirror text "str" for right-left displaying.
6373 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006375 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006376rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 char_u *p1, *p2;
6379 int t;
6380
6381 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6382 {
6383 t = *p1;
6384 *p1 = *p2;
6385 *p2 = t;
6386 }
6387}
6388#endif
6389
6390#if defined(FEAT_WINDOWS) || defined(PROTO)
6391/*
6392 * mark all status lines for redraw; used after first :cd
6393 */
6394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006395status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 win_T *wp;
6398
Bram Moolenaar29323592016-07-24 22:04:11 +02006399 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (wp->w_status_height)
6401 {
6402 wp->w_redr_status = TRUE;
6403 redraw_later(VALID);
6404 }
6405}
6406
6407/*
6408 * mark all status lines of the current buffer for redraw
6409 */
6410 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006411status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 win_T *wp;
6414
Bram Moolenaar29323592016-07-24 22:04:11 +02006415 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6417 {
6418 wp->w_redr_status = TRUE;
6419 redraw_later(VALID);
6420 }
6421}
6422
6423/*
6424 * Redraw all status lines that need to be redrawn.
6425 */
6426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006427redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 win_T *wp;
6430
Bram Moolenaar29323592016-07-24 22:04:11 +02006431 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (wp->w_redr_status)
6433 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006434 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006435 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
6437#endif
6438
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006439#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440/*
6441 * Redraw all status lines at the bottom of frame "frp".
6442 */
6443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006444win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 if (frp->fr_layout == FR_LEAF)
6447 frp->fr_win->w_redr_status = TRUE;
6448 else if (frp->fr_layout == FR_ROW)
6449 {
6450 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6451 win_redraw_last_status(frp);
6452 }
6453 else /* frp->fr_layout == FR_COL */
6454 {
6455 frp = frp->fr_child;
6456 while (frp->fr_next != NULL)
6457 frp = frp->fr_next;
6458 win_redraw_last_status(frp);
6459 }
6460}
6461#endif
6462
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006463#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464/*
6465 * Draw the verticap separator right of window "wp" starting with line "row".
6466 */
6467 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006468draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 int hl;
6471 int c;
6472
6473 if (wp->w_vsep_width)
6474 {
6475 /* draw the vertical separator right of this window */
6476 c = fillchar_vsep(&hl);
6477 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6478 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6479 c, ' ', hl);
6480 }
6481}
6482#endif
6483
6484#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006485static int status_match_len(expand_T *xp, char_u *s);
6486static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
6488/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006489 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 */
6491 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006492status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 int len = 0;
6495
6496#ifdef FEAT_MENU
6497 int emenu = (xp->xp_context == EXPAND_MENUS
6498 || xp->xp_context == EXPAND_MENUNAMES);
6499
6500 /* Check for menu separators - replace with '|'. */
6501 if (emenu && menu_is_separator(s))
6502 return 1;
6503#endif
6504
6505 while (*s != NUL)
6506 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006507 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006508 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006509 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511
6512 return len;
6513}
6514
6515/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006516 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006517 * These are backslashes used for escaping. Do show backslashes in help tags.
6518 */
6519 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006520skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006521{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006522 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006523#ifdef FEAT_MENU
6524 || ((xp->xp_context == EXPAND_MENUS
6525 || xp->xp_context == EXPAND_MENUNAMES)
6526 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6527#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006528 )
6529 {
6530#ifndef BACKSLASH_IN_FILENAME
6531 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6532 return 2;
6533#endif
6534 return 1;
6535 }
6536 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006537}
6538
6539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 * Show wildchar matches in the status line.
6541 * Show at least the "match" item.
6542 * We start at item 'first_match' in the list and show all matches that fit.
6543 *
6544 * If inversion is possible we use it. Else '=' characters are used.
6545 */
6546 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006547win_redr_status_matches(
6548 expand_T *xp,
6549 int num_matches,
6550 char_u **matches, /* list of matches */
6551 int match,
6552 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6555 int row;
6556 char_u *buf;
6557 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006558 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 int fillchar;
6560 int attr;
6561 int i;
6562 int highlight = TRUE;
6563 char_u *selstart = NULL;
6564 int selstart_col = 0;
6565 char_u *selend = NULL;
6566 static int first_match = 0;
6567 int add_left = FALSE;
6568 char_u *s;
6569#ifdef FEAT_MENU
6570 int emenu;
6571#endif
6572#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6573 int l;
6574#endif
6575
6576 if (matches == NULL) /* interrupted completion? */
6577 return;
6578
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006579#ifdef FEAT_MBYTE
6580 if (has_mbyte)
6581 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6582 else
6583#endif
6584 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (buf == NULL)
6586 return;
6587
6588 if (match == -1) /* don't show match but original text */
6589 {
6590 match = 0;
6591 highlight = FALSE;
6592 }
6593 /* count 1 for the ending ">" */
6594 clen = status_match_len(xp, L_MATCH(match)) + 3;
6595 if (match == 0)
6596 first_match = 0;
6597 else if (match < first_match)
6598 {
6599 /* jumping left, as far as we can go */
6600 first_match = match;
6601 add_left = TRUE;
6602 }
6603 else
6604 {
6605 /* check if match fits on the screen */
6606 for (i = first_match; i < match; ++i)
6607 clen += status_match_len(xp, L_MATCH(i)) + 2;
6608 if (first_match > 0)
6609 clen += 2;
6610 /* jumping right, put match at the left */
6611 if ((long)clen > Columns)
6612 {
6613 first_match = match;
6614 /* if showing the last match, we can add some on the left */
6615 clen = 2;
6616 for (i = match; i < num_matches; ++i)
6617 {
6618 clen += status_match_len(xp, L_MATCH(i)) + 2;
6619 if ((long)clen >= Columns)
6620 break;
6621 }
6622 if (i == num_matches)
6623 add_left = TRUE;
6624 }
6625 }
6626 if (add_left)
6627 while (first_match > 0)
6628 {
6629 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6630 if ((long)clen >= Columns)
6631 break;
6632 --first_match;
6633 }
6634
6635 fillchar = fillchar_status(&attr, TRUE);
6636
6637 if (first_match == 0)
6638 {
6639 *buf = NUL;
6640 len = 0;
6641 }
6642 else
6643 {
6644 STRCPY(buf, "< ");
6645 len = 2;
6646 }
6647 clen = len;
6648
6649 i = first_match;
6650 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6651 {
6652 if (i == match)
6653 {
6654 selstart = buf + len;
6655 selstart_col = clen;
6656 }
6657
6658 s = L_MATCH(i);
6659 /* Check for menu separators - replace with '|' */
6660#ifdef FEAT_MENU
6661 emenu = (xp->xp_context == EXPAND_MENUS
6662 || xp->xp_context == EXPAND_MENUNAMES);
6663 if (emenu && menu_is_separator(s))
6664 {
6665 STRCPY(buf + len, transchar('|'));
6666 l = (int)STRLEN(buf + len);
6667 len += l;
6668 clen += l;
6669 }
6670 else
6671#endif
6672 for ( ; *s != NUL; ++s)
6673 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006674 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 clen += ptr2cells(s);
6676#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006677 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
6679 STRNCPY(buf + len, s, l);
6680 s += l - 1;
6681 len += l;
6682 }
6683 else
6684#endif
6685 {
6686 STRCPY(buf + len, transchar_byte(*s));
6687 len += (int)STRLEN(buf + len);
6688 }
6689 }
6690 if (i == match)
6691 selend = buf + len;
6692
6693 *(buf + len++) = ' ';
6694 *(buf + len++) = ' ';
6695 clen += 2;
6696 if (++i == num_matches)
6697 break;
6698 }
6699
6700 if (i != num_matches)
6701 {
6702 *(buf + len++) = '>';
6703 ++clen;
6704 }
6705
6706 buf[len] = NUL;
6707
6708 row = cmdline_row - 1;
6709 if (row >= 0)
6710 {
6711 if (wild_menu_showing == 0)
6712 {
6713 if (msg_scrolled > 0)
6714 {
6715 /* Put the wildmenu just above the command line. If there is
6716 * no room, scroll the screen one line up. */
6717 if (cmdline_row == Rows - 1)
6718 {
6719 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6720 ++msg_scrolled;
6721 }
6722 else
6723 {
6724 ++cmdline_row;
6725 ++row;
6726 }
6727 wild_menu_showing = WM_SCROLLED;
6728 }
6729 else
6730 {
6731 /* Create status line if needed by setting 'laststatus' to 2.
6732 * Set 'winminheight' to zero to avoid that the window is
6733 * resized. */
6734 if (lastwin->w_status_height == 0)
6735 {
6736 save_p_ls = p_ls;
6737 save_p_wmh = p_wmh;
6738 p_ls = 2;
6739 p_wmh = 0;
6740 last_status(FALSE);
6741 }
6742 wild_menu_showing = WM_SHOWN;
6743 }
6744 }
6745
6746 screen_puts(buf, row, 0, attr);
6747 if (selstart != NULL && highlight)
6748 {
6749 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006750 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752
6753 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6754 }
6755
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006756#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 win_redraw_last_status(topframe);
6758#else
6759 lastwin->w_redr_status = TRUE;
6760#endif
6761 vim_free(buf);
6762}
6763#endif
6764
6765#if defined(FEAT_WINDOWS) || defined(PROTO)
6766/*
6767 * Redraw the status line of window wp.
6768 *
6769 * If inversion is possible we use it. Else '=' characters are used.
6770 */
6771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006772win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 int row;
6775 char_u *p;
6776 int len;
6777 int fillchar;
6778 int attr;
6779 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006780 static int busy = FALSE;
6781
6782 /* It's possible to get here recursively when 'statusline' (indirectly)
6783 * invokes ":redrawstatus". Simply ignore the call then. */
6784 if (busy)
6785 return;
6786 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 wp->w_redr_status = FALSE;
6789 if (wp->w_status_height == 0)
6790 {
6791 /* no status line, can only be last window */
6792 redraw_cmdline = TRUE;
6793 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006794 else if (!redrawing()
6795#ifdef FEAT_INS_EXPAND
6796 /* don't update status line when popup menu is visible and may be
6797 * drawn over it */
6798 || pum_visible()
6799#endif
6800 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 /* Don't redraw right now, do it later. */
6803 wp->w_redr_status = TRUE;
6804 }
6805#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006806 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006809 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811#endif
6812 else
6813 {
6814 fillchar = fillchar_status(&attr, wp == curwin);
6815
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006816 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 p = NameBuff;
6818 len = (int)STRLEN(p);
6819
6820 if (wp->w_buffer->b_help
6821#ifdef FEAT_QUICKFIX
6822 || wp->w_p_pvw
6823#endif
6824 || bufIsChanged(wp->w_buffer)
6825 || wp->w_buffer->b_p_ro)
6826 *(p + len++) = ' ';
6827 if (wp->w_buffer->b_help)
6828 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006829 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 len += (int)STRLEN(p + len);
6831 }
6832#ifdef FEAT_QUICKFIX
6833 if (wp->w_p_pvw)
6834 {
6835 STRCPY(p + len, _("[Preview]"));
6836 len += (int)STRLEN(p + len);
6837 }
6838#endif
6839 if (bufIsChanged(wp->w_buffer))
6840 {
6841 STRCPY(p + len, "[+]");
6842 len += 3;
6843 }
6844 if (wp->w_buffer->b_p_ro)
6845 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006846 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006847 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 }
6849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6851 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6852 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6853 if (this_ru_col <= 1)
6854 {
6855 p = (char_u *)"<"; /* No room for file name! */
6856 len = 1;
6857 }
6858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859#ifdef FEAT_MBYTE
6860 if (has_mbyte)
6861 {
6862 int clen = 0, i;
6863
6864 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006865 clen = mb_string2cells(p, -1);
6866
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 /* Find first character that will fit.
6868 * Going from start to end is much faster for DBCS. */
6869 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006870 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 clen -= (*mb_ptr2cells)(p + i);
6872 len = clen;
6873 if (i > 0)
6874 {
6875 p = p + i - 1;
6876 *p = '<';
6877 ++len;
6878 }
6879
6880 }
6881 else
6882#endif
6883 if (len > this_ru_col - 1)
6884 {
6885 p += len - (this_ru_col - 1);
6886 *p = '<';
6887 len = this_ru_col - 1;
6888 }
6889
6890 row = W_WINROW(wp) + wp->w_height;
6891 screen_puts(p, row, W_WINCOL(wp), attr);
6892 screen_fill(row, row + 1, len + W_WINCOL(wp),
6893 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6894
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006895 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6897 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6898 - 1 + W_WINCOL(wp)), attr);
6899
6900#ifdef FEAT_CMDL_INFO
6901 win_redr_ruler(wp, TRUE);
6902#endif
6903 }
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 /*
6906 * May need to draw the character below the vertical separator.
6907 */
6908 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6909 {
6910 if (stl_connected(wp))
6911 fillchar = fillchar_status(&attr, wp == curwin);
6912 else
6913 fillchar = fillchar_vsep(&attr);
6914 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6915 attr);
6916 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006917 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
Bram Moolenaar238a5642006-02-21 22:12:05 +00006920#ifdef FEAT_STL_OPT
6921/*
6922 * Redraw the status line according to 'statusline' and take care of any
6923 * errors encountered.
6924 */
6925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006926redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006927{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006928 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006929 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006930
6931 /* When called recursively return. This can happen when the statusline
6932 * contains an expression that triggers a redraw. */
6933 if (entered)
6934 return;
6935 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006936
Bram Moolenaara742e082016-04-05 21:10:38 +02006937 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006938 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006939 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006940 {
6941 /* When there is an error disable the statusline, otherwise the
6942 * display is messed up with errors and a redraw triggers the problem
6943 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006944 set_string_option_direct((char_u *)"statusline", -1,
6945 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006946 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006947 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006948 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006949 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006950}
6951#endif
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953/*
6954 * Return TRUE if the status line of window "wp" is connected to the status
6955 * line of the window right of it. If not, then it's a vertical separator.
6956 * Only call if (wp->w_vsep_width != 0).
6957 */
6958 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006959stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 frame_T *fr;
6962
6963 fr = wp->w_frame;
6964 while (fr->fr_parent != NULL)
6965 {
6966 if (fr->fr_parent->fr_layout == FR_COL)
6967 {
6968 if (fr->fr_next != NULL)
6969 break;
6970 }
6971 else
6972 {
6973 if (fr->fr_next != NULL)
6974 return TRUE;
6975 }
6976 fr = fr->fr_parent;
6977 }
6978 return FALSE;
6979}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981#endif /* FEAT_WINDOWS */
6982
6983#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6984/*
6985 * Get the value to show for the language mappings, active 'keymap'.
6986 */
6987 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006988get_keymap_str(
6989 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006990 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01006991 char_u *buf, /* buffer for the result */
6992 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
6994 char_u *p;
6995
6996 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6997 return FALSE;
6998
6999 {
7000#ifdef FEAT_EVAL
7001 buf_T *old_curbuf = curbuf;
7002 win_T *old_curwin = curwin;
7003 char_u *s;
7004
7005 curbuf = wp->w_buffer;
7006 curwin = wp;
7007 STRCPY(buf, "b:keymap_name"); /* must be writable */
7008 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007009 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 --emsg_skip;
7011 curbuf = old_curbuf;
7012 curwin = old_curwin;
7013 if (p == NULL || *p == NUL)
7014#endif
7015 {
7016#ifdef FEAT_KEYMAP
7017 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7018 p = wp->w_buffer->b_p_keymap;
7019 else
7020#endif
7021 p = (char_u *)"lang";
7022 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007023 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 buf[0] = NUL;
7025#ifdef FEAT_EVAL
7026 vim_free(s);
7027#endif
7028 }
7029 return buf[0] != NUL;
7030}
7031#endif
7032
7033#if defined(FEAT_STL_OPT) || defined(PROTO)
7034/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007035 * Redraw the status line or ruler of window "wp".
7036 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 */
7038 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007039win_redr_custom(
7040 win_T *wp,
7041 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007043 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 int attr;
7045 int curattr;
7046 int row;
7047 int col = 0;
7048 int maxwidth;
7049 int width;
7050 int n;
7051 int len;
7052 int fillchar;
7053 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007054 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 struct stl_hlrec hltab[STL_MAX_ITEM];
7057 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007058 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007059 win_T *ewp;
7060 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar1d633412013-12-11 15:52:01 +01007062 /* There is a tiny chance that this gets called recursively: When
7063 * redrawing a status line triggers redrawing the ruler or tabline.
7064 * Avoid trouble by not allowing recursion. */
7065 if (entered)
7066 return;
7067 entered = TRUE;
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007070 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007072 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007073 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007074 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007075 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007076 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007077 maxwidth = Columns;
7078# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007079 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007082 else
7083 {
7084 row = W_WINROW(wp) + wp->w_height;
7085 fillchar = fillchar_status(&attr, wp == curwin);
7086 maxwidth = W_WIDTH(wp);
7087
7088 if (draw_ruler)
7089 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007090 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007091 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007092 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007093 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007094 if (*++stl == '-')
7095 stl++;
7096 if (atoi((char *)stl))
7097 while (VIM_ISDIGIT(*stl))
7098 stl++;
7099 if (*stl++ != '(')
7100 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007101 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007102#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007103 col = ru_col - (Columns - W_WIDTH(wp));
7104 if (col < (W_WIDTH(wp) + 1) / 2)
7105 col = (W_WIDTH(wp) + 1) / 2;
7106#else
7107 col = ru_col;
7108 if (col > (Columns + 1) / 2)
7109 col = (Columns + 1) / 2;
7110#endif
7111 maxwidth = W_WIDTH(wp) - col;
7112#ifdef FEAT_WINDOWS
7113 if (!wp->w_status_height)
7114#endif
7115 {
7116 row = Rows - 1;
7117 --maxwidth; /* writing in last column may cause scrolling */
7118 fillchar = ' ';
7119 attr = 0;
7120 }
7121
7122# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007123 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124# endif
7125 }
7126 else
7127 {
7128 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007129 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007130 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007132# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007133 use_sandbox = was_set_insecurely((char_u *)"statusline",
7134 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135# endif
7136 }
7137
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007138#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139 col += W_WINCOL(wp);
7140#endif
7141 }
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007144 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
Bram Moolenaar61452852011-02-01 18:01:11 +01007146 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7147 * the cursor away and back. */
7148 ewp = wp == NULL ? curwin : wp;
7149 p_crb_save = ewp->w_p_crb;
7150 ewp->w_p_crb = FALSE;
7151
Bram Moolenaar362f3562009-11-03 16:20:34 +00007152 /* Make a copy, because the statusline may include a function call that
7153 * might change the option value and free the memory. */
7154 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007155 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007157 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007159 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007161 /* Make all characters printable. */
7162 p = transstr(buf);
7163 if (p != NULL)
7164 {
7165 vim_strncpy(buf, p, sizeof(buf) - 1);
7166 vim_free(p);
7167 }
7168
7169 /* fill up with "fillchar" */
7170 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007171 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 {
7173#ifdef FEAT_MBYTE
7174 len += (*mb_char2bytes)(fillchar, buf + len);
7175#else
7176 buf[len++] = fillchar;
7177#endif
7178 ++width;
7179 }
7180 buf[len] = NUL;
7181
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007182 /*
7183 * Draw each snippet with the specified highlighting.
7184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 curattr = attr;
7186 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007187 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 screen_puts_len(p, len, row, col, curattr);
7191 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007192 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007194 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 else if (hltab[n].userhl < 0)
7197 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007199 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201#endif
7202 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007203 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007206
7207 if (wp == NULL)
7208 {
7209 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7210 col = 0;
7211 len = 0;
7212 p = buf;
7213 fillchar = 0;
7214 for (n = 0; tabtab[n].start != NULL; n++)
7215 {
7216 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7217 while (col < len)
7218 TabPageIdxs[col++] = fillchar;
7219 p = tabtab[n].start;
7220 fillchar = tabtab[n].userhl;
7221 }
7222 while (col < Columns)
7223 TabPageIdxs[col++] = fillchar;
7224 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007225
7226theend:
7227 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228}
7229
7230#endif /* FEAT_STL_OPT */
7231
7232/*
7233 * Output a single character directly to the screen and update ScreenLines.
7234 */
7235 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007236screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 char_u buf[MB_MAXBYTES + 1];
7239
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007240#ifdef FEAT_MBYTE
7241 if (has_mbyte)
7242 buf[(*mb_char2bytes)(c, buf)] = NUL;
7243 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007245 {
7246 buf[0] = c;
7247 buf[1] = NUL;
7248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 screen_puts(buf, row, col, attr);
7250}
7251
7252/*
7253 * Get a single character directly from ScreenLines into "bytes[]".
7254 * Also return its attribute in *attrp;
7255 */
7256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007257screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 unsigned off;
7260
7261 /* safety check */
7262 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7263 {
7264 off = LineOffset[row] + col;
7265 *attrp = ScreenAttrs[off];
7266 bytes[0] = ScreenLines[off];
7267 bytes[1] = NUL;
7268
7269#ifdef FEAT_MBYTE
7270 if (enc_utf8 && ScreenLinesUC[off] != 0)
7271 bytes[utfc_char2bytes(off, bytes)] = NUL;
7272 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7273 {
7274 bytes[0] = ScreenLines[off];
7275 bytes[1] = ScreenLines2[off];
7276 bytes[2] = NUL;
7277 }
7278 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7279 {
7280 bytes[1] = ScreenLines[off + 1];
7281 bytes[2] = NUL;
7282 }
7283#endif
7284 }
7285}
7286
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007287#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007288static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007289
7290/*
7291 * Return TRUE if composing characters for screen posn "off" differs from
7292 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007293 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 */
7295 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007296screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297{
7298 int i;
7299
7300 for (i = 0; i < Screen_mco; ++i)
7301 {
7302 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7303 return TRUE;
7304 if (u8cc[i] == 0)
7305 break;
7306 }
7307 return FALSE;
7308}
7309#endif
7310
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311/*
7312 * Put string '*text' on the screen at position 'row' and 'col', with
7313 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7314 * Note: only outputs within one row, message is truncated at screen boundary!
7315 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7316 */
7317 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007318screen_puts(
7319 char_u *text,
7320 int row,
7321 int col,
7322 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
7324 screen_puts_len(text, -1, row, col, attr);
7325}
7326
7327/*
7328 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7329 * a NUL.
7330 */
7331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007332screen_puts_len(
7333 char_u *text,
7334 int textlen,
7335 int row,
7336 int col,
7337 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
7339 unsigned off;
7340 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007341 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 int c;
7343#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007344 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 int mbyte_blen = 1;
7346 int mbyte_cells = 1;
7347 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007348 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int clear_next_cell = FALSE;
7350# ifdef FEAT_ARABIC
7351 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007352 int pc, nc, nc1;
7353 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354# endif
7355#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007356#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7357 int force_redraw_this;
7358 int force_redraw_next = FALSE;
7359#endif
7360 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
7362 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7363 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007364 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
Bram Moolenaarc236c162008-07-13 17:41:49 +00007366#ifdef FEAT_MBYTE
7367 /* When drawing over the right halve of a double-wide char clear out the
7368 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007369 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007370# ifdef FEAT_GUI
7371 && !gui.in_use
7372# endif
7373 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007374 {
7375 ScreenLines[off - 1] = ' ';
7376 ScreenAttrs[off - 1] = 0;
7377 if (enc_utf8)
7378 {
7379 ScreenLinesUC[off - 1] = 0;
7380 ScreenLinesC[0][off - 1] = 0;
7381 }
7382 /* redraw the previous cell, make it empty */
7383 screen_char(off - 1, row, col - 1);
7384 /* force the cell at "col" to be redrawn */
7385 force_redraw_next = TRUE;
7386 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007387#endif
7388
Bram Moolenaar367329b2007-08-30 11:53:22 +00007389#ifdef FEAT_MBYTE
7390 max_off = LineOffset[row] + screen_Columns;
7391#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007392 while (col < screen_Columns
7393 && (len < 0 || (int)(ptr - text) < len)
7394 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
7396 c = *ptr;
7397#ifdef FEAT_MBYTE
7398 /* check if this is the first byte of a multibyte */
7399 if (has_mbyte)
7400 {
7401 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007402 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007404 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7406 mbyte_cells = 1;
7407 else if (enc_dbcs != 0)
7408 mbyte_cells = mbyte_blen;
7409 else /* enc_utf8 */
7410 {
7411 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 (int)((text + len) - ptr));
7414 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007415 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007417# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 /* Non-BMP character: display as ? or fullwidth ?. */
7419 if (u8c >= 0x10000)
7420 {
7421 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7422 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007423 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007425# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426# ifdef FEAT_ARABIC
7427 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7428 {
7429 /* Do Arabic shaping. */
7430 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7431 {
7432 /* Past end of string to be displayed. */
7433 nc = NUL;
7434 nc1 = NUL;
7435 }
7436 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007437 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007438 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7439 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007440 nc1 = pcc[0];
7441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 pc = prev_c;
7443 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446 else
7447 prev_c = u8c;
7448# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007449 if (col + mbyte_cells > screen_Columns)
7450 {
7451 /* Only 1 cell left, but character requires 2 cells:
7452 * display a '>' in the last column to avoid wrapping. */
7453 c = '>';
7454 mbyte_cells = 1;
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457 }
7458#endif
7459
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007460#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7461 force_redraw_this = force_redraw_next;
7462 force_redraw_next = FALSE;
7463#endif
7464
7465 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466#ifdef FEAT_MBYTE
7467 || (mbyte_cells == 2
7468 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7469 || (enc_dbcs == DBCS_JPNU
7470 && c == 0x8e
7471 && ScreenLines2[off] != ptr[1])
7472 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007473 && (ScreenLinesUC[off] !=
7474 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7475 || (ScreenLinesUC[off] != 0
7476 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#endif
7478 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007479 || exmode_active;
7480
7481 if (need_redraw
7482#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7483 || force_redraw_this
7484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 )
7486 {
7487#if defined(FEAT_GUI) || defined(UNIX)
7488 /* The bold trick makes a single row of pixels appear in the next
7489 * character. When a bold character is removed, the next
7490 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007491 * and for some xterms. */
7492 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493# ifdef FEAT_GUI
7494 gui.in_use
7495# endif
7496# if defined(FEAT_GUI) && defined(UNIX)
7497 ||
7498# endif
7499# ifdef UNIX
7500 term_is_xterm
7501# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007502 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007504 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007506 if (n > HL_ALL)
7507 n = syn_attr2attr(n);
7508 if (n & HL_BOLD)
7509 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511#endif
7512#ifdef FEAT_MBYTE
7513 /* When at the end of the text and overwriting a two-cell
7514 * character with a one-cell character, need to clear the next
7515 * cell. Also when overwriting the left halve of a two-cell char
7516 * with the right halve of a two-cell char. Do this only once
7517 * (mb_off2cells() may return 2 on the right halve). */
7518 if (clear_next_cell)
7519 clear_next_cell = FALSE;
7520 else if (has_mbyte
7521 && (len < 0 ? ptr[mbyte_blen] == NUL
7522 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007523 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007525 && (*mb_off2cells)(off, max_off) == 1
7526 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 clear_next_cell = TRUE;
7528
7529 /* Make sure we never leave a second byte of a double-byte behind,
7530 * it confuses mb_off2cells(). */
7531 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007532 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007534 && (*mb_off2cells)(off, max_off) == 1
7535 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 ScreenLines[off + mbyte_blen] = 0;
7537#endif
7538 ScreenLines[off] = c;
7539 ScreenAttrs[off] = attr;
7540#ifdef FEAT_MBYTE
7541 if (enc_utf8)
7542 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007543 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 ScreenLinesUC[off] = 0;
7545 else
7546 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547 int i;
7548
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007550 for (i = 0; i < Screen_mco; ++i)
7551 {
7552 ScreenLinesC[i][off] = u8cc[i];
7553 if (u8cc[i] == 0)
7554 break;
7555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557 if (mbyte_cells == 2)
7558 {
7559 ScreenLines[off + 1] = 0;
7560 ScreenAttrs[off + 1] = attr;
7561 }
7562 screen_char(off, row, col);
7563 }
7564 else if (mbyte_cells == 2)
7565 {
7566 ScreenLines[off + 1] = ptr[1];
7567 ScreenAttrs[off + 1] = attr;
7568 screen_char_2(off, row, col);
7569 }
7570 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7571 {
7572 ScreenLines2[off] = ptr[1];
7573 screen_char(off, row, col);
7574 }
7575 else
7576#endif
7577 screen_char(off, row, col);
7578 }
7579#ifdef FEAT_MBYTE
7580 if (has_mbyte)
7581 {
7582 off += mbyte_cells;
7583 col += mbyte_cells;
7584 ptr += mbyte_blen;
7585 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007586 {
7587 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007589 len = -1;
7590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592 else
7593#endif
7594 {
7595 ++off;
7596 ++col;
7597 ++ptr;
7598 }
7599 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007600
7601#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7602 /* If we detected the next character needs to be redrawn, but the text
7603 * doesn't extend up to there, update the character here. */
7604 if (force_redraw_next && col < screen_Columns)
7605 {
7606# ifdef FEAT_MBYTE
7607 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7608 screen_char_2(off, row, col);
7609 else
7610# endif
7611 screen_char(off, row, col);
7612 }
7613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614}
7615
7616#ifdef FEAT_SEARCH_EXTRA
7617/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007618 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 */
7620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
7623 if (p_hls && !no_hlsearch)
7624 {
7625 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007626 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007627# ifdef FEAT_RELTIME
7628 /* Set the time limit to 'redrawtime'. */
7629 profile_setlimit(p_rdt, &search_hl.tm);
7630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632}
7633
7634/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007635 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 */
7637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007638end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
7640 if (search_hl.rm.regprog != NULL)
7641 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007642 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 search_hl.rm.regprog = NULL;
7644 }
7645}
7646
7647/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007648 * Init for calling prepare_search_hl().
7649 */
7650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007651init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007652{
7653 matchitem_T *cur;
7654
7655 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7656 * match */
7657 cur = wp->w_match_head;
7658 while (cur != NULL)
7659 {
7660 cur->hl.rm = cur->match;
7661 if (cur->hlg_id == 0)
7662 cur->hl.attr = 0;
7663 else
7664 cur->hl.attr = syn_id2attr(cur->hlg_id);
7665 cur->hl.buf = wp->w_buffer;
7666 cur->hl.lnum = 0;
7667 cur->hl.first_lnum = 0;
7668# ifdef FEAT_RELTIME
7669 /* Set the time limit to 'redrawtime'. */
7670 profile_setlimit(p_rdt, &(cur->hl.tm));
7671# endif
7672 cur = cur->next;
7673 }
7674 search_hl.buf = wp->w_buffer;
7675 search_hl.lnum = 0;
7676 search_hl.first_lnum = 0;
7677 /* time limit is set at the toplevel, for all windows */
7678}
7679
7680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 * Advance to the match in window "wp" line "lnum" or past it.
7682 */
7683 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007684prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007686 matchitem_T *cur; /* points to the match list */
7687 match_T *shl; /* points to search_hl or a match */
7688 int shl_flag; /* flag to indicate whether search_hl
7689 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007690 int pos_inprogress; /* marks that position match search is
7691 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 int n;
7693
7694 /*
7695 * When using a multi-line pattern, start searching at the top
7696 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007697 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007699 cur = wp->w_match_head;
7700 shl_flag = FALSE;
7701 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007703 if (shl_flag == FALSE)
7704 {
7705 shl = &search_hl;
7706 shl_flag = TRUE;
7707 }
7708 else
7709 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 if (shl->rm.regprog != NULL
7711 && shl->lnum == 0
7712 && re_multiline(shl->rm.regprog))
7713 {
7714 if (shl->first_lnum == 0)
7715 {
7716# ifdef FEAT_FOLDING
7717 for (shl->first_lnum = lnum;
7718 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7719 if (hasFoldingWin(wp, shl->first_lnum - 1,
7720 NULL, NULL, TRUE, NULL))
7721 break;
7722# else
7723 shl->first_lnum = wp->w_topline;
7724# endif
7725 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007726 if (cur != NULL)
7727 cur->pos.cur = 0;
7728 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007730 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7731 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007733 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7734 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007735 pos_inprogress = cur == NULL || cur->pos.cur == 0
7736 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 if (shl->lnum != 0)
7738 {
7739 shl->first_lnum = shl->lnum
7740 + shl->rm.endpos[0].lnum
7741 - shl->rm.startpos[0].lnum;
7742 n = shl->rm.endpos[0].col;
7743 }
7744 else
7745 {
7746 ++shl->first_lnum;
7747 n = 0;
7748 }
7749 }
7750 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007751 if (shl != &search_hl && cur != NULL)
7752 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754}
7755
7756/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * Uses shl->buf.
7759 * Sets shl->lnum and shl->rm contents.
7760 * Note: Assumes a previous match is always before "lnum", unless
7761 * shl->lnum is zero.
7762 * Careful: Any pointers for buffer lines will become invalid.
7763 */
7764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007765next_search_hl(
7766 win_T *win,
7767 match_T *shl, /* points to search_hl or a match */
7768 linenr_T lnum,
7769 colnr_T mincol, /* minimal column for a match */
7770 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 linenr_T l;
7773 colnr_T matchcol;
7774 long nmatched;
7775
7776 if (shl->lnum != 0)
7777 {
7778 /* Check for three situations:
7779 * 1. If the "lnum" is below a previous match, start a new search.
7780 * 2. If the previous match includes "mincol", use it.
7781 * 3. Continue after the previous match.
7782 */
7783 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7784 if (lnum > l)
7785 shl->lnum = 0;
7786 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7787 return;
7788 }
7789
7790 /*
7791 * Repeat searching for a match until one is found that includes "mincol"
7792 * or none is found in this line.
7793 */
7794 called_emsg = FALSE;
7795 for (;;)
7796 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007797#ifdef FEAT_RELTIME
7798 /* Stop searching after passing the time limit. */
7799 if (profile_passed_limit(&(shl->tm)))
7800 {
7801 shl->lnum = 0; /* no match found in time */
7802 break;
7803 }
7804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* Three situations:
7806 * 1. No useful previous match: search from start of line.
7807 * 2. Not Vi compatible or empty match: continue at next character.
7808 * Break the loop if this is beyond the end of the line.
7809 * 3. Vi compatible searching: continue at end of previous match.
7810 */
7811 if (shl->lnum == 0)
7812 matchcol = 0;
7813 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7814 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007815 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007817 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007818
7819 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007820 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007821 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007823 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 shl->lnum = 0;
7825 break;
7826 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007827#ifdef FEAT_MBYTE
7828 if (has_mbyte)
7829 matchcol += mb_ptr2len(ml);
7830 else
7831#endif
7832 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
7834 else
7835 matchcol = shl->rm.endpos[0].col;
7836
7837 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007838 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007840 /* Remember whether shl->rm is using a copy of the regprog in
7841 * cur->match. */
7842 int regprog_is_copy = (shl != &search_hl && cur != NULL
7843 && shl == &cur->hl
7844 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007845 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007846
Bram Moolenaarb3414592014-06-17 17:48:32 +02007847 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7848 matchcol,
7849#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007850 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007851#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007852 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007853#endif
7854 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007855 /* Copy the regprog, in case it got freed and recompiled. */
7856 if (regprog_is_copy)
7857 cur->match.regprog = cur->hl.rm.regprog;
7858
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007859 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007860 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007861 /* Error while handling regexp: stop using this regexp. */
7862 if (shl == &search_hl)
7863 {
7864 /* don't free regprog in the match list, it's a copy */
7865 vim_regfree(shl->rm.regprog);
7866 SET_NO_HLSEARCH(TRUE);
7867 }
7868 shl->rm.regprog = NULL;
7869 shl->lnum = 0;
7870 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7871 message */
7872 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007873 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007874 }
7875 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007876 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007877 else
7878 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (nmatched == 0)
7880 {
7881 shl->lnum = 0; /* no match found */
7882 break;
7883 }
7884 if (shl->rm.startpos[0].lnum > 0
7885 || shl->rm.startpos[0].col >= mincol
7886 || nmatched > 1
7887 || shl->rm.endpos[0].col > mincol)
7888 {
7889 shl->lnum += shl->rm.startpos[0].lnum;
7890 break; /* useful match found */
7891 }
7892 }
7893}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894
Bram Moolenaar85077472016-10-16 14:35:48 +02007895/*
7896 * If there is a match fill "shl" and return one.
7897 * Return zero otherwise.
7898 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007900next_search_hl_pos(
7901 match_T *shl, /* points to a match */
7902 linenr_T lnum,
7903 posmatch_T *posmatch, /* match positions */
7904 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905{
7906 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007907 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7910 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007911 llpos_T *pos = &posmatch->pos[i];
7912
7913 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007914 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007915 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007916 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007917 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007919 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007921 /* if this match comes before the one at "found" then swap
7922 * them */
7923 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007925 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926
Bram Moolenaar85077472016-10-16 14:35:48 +02007927 *pos = posmatch->pos[found];
7928 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007929 }
7930 }
7931 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007932 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007933 }
7934 }
7935 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007936 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007938 colnr_T start = posmatch->pos[found].col == 0
7939 ? 0 : posmatch->pos[found].col - 1;
7940 colnr_T end = posmatch->pos[found].col == 0
7941 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007942
Bram Moolenaar85077472016-10-16 14:35:48 +02007943 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007944 shl->rm.startpos[0].lnum = 0;
7945 shl->rm.startpos[0].col = start;
7946 shl->rm.endpos[0].lnum = 0;
7947 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007948 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007949 posmatch->cur = found + 1;
7950 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007952 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007954#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007957screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959 attrentry_T *aep = NULL;
7960
7961 screen_attr = attr;
7962 if (full_screen
7963#ifdef WIN3264
7964 && termcap_active
7965#endif
7966 )
7967 {
7968#ifdef FEAT_GUI
7969 if (gui.in_use)
7970 {
7971 char buf[20];
7972
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007973 /* The GUI handles this internally. */
7974 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 OUT_STR(buf);
7976 }
7977 else
7978#endif
7979 {
7980 if (attr > HL_ALL) /* special HL attr. */
7981 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007982 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 aep = syn_cterm_attr2entry(attr);
7984 else
7985 aep = syn_term_attr2entry(attr);
7986 if (aep == NULL) /* did ":syntax clear" */
7987 attr = 0;
7988 else
7989 attr = aep->ae_attr;
7990 }
7991 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7992 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007993 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007994#ifdef FEAT_TERMGUICOLORS
7995 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007996 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007997#endif
7998 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007999#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008000 )
8001#endif
8002 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008003 /* If the Normal FG color has BOLD attribute and the new HL
8004 * has a FG color defined, clear BOLD. */
8005 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8007 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008008 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8009 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 out_str(T_US);
8011 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8012 out_str(T_CZH);
8013 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8014 out_str(T_MR);
8015
8016 /*
8017 * Output the color or start string after bold etc., in case the
8018 * bold etc. override the color setting.
8019 */
8020 if (aep != NULL)
8021 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008022#ifdef FEAT_TERMGUICOLORS
8023 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008025 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008026 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008027 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008028 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008033 if (t_colors > 1)
8034 {
8035 if (aep->ae_u.cterm.fg_color)
8036 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8037 if (aep->ae_u.cterm.bg_color)
8038 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8039 }
8040 else
8041 {
8042 if (aep->ae_u.term.start != NULL)
8043 out_str(aep->ae_u.term.start);
8044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046 }
8047 }
8048 }
8049}
8050
8051 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008052screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 int do_ME = FALSE; /* output T_ME code */
8055
8056 if (screen_attr != 0
8057#ifdef WIN3264
8058 && termcap_active
8059#endif
8060 )
8061 {
8062#ifdef FEAT_GUI
8063 if (gui.in_use)
8064 {
8065 char buf[20];
8066
8067 /* use internal GUI code */
8068 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8069 OUT_STR(buf);
8070 }
8071 else
8072#endif
8073 {
8074 if (screen_attr > HL_ALL) /* special HL attr. */
8075 {
8076 attrentry_T *aep;
8077
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008078 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
8080 /*
8081 * Assume that t_me restores the original colors!
8082 */
8083 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008084 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008085#ifdef FEAT_TERMGUICOLORS
8086 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008087 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8088 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008089#endif
8090 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008091#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008092 )
8093#endif
8094 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 do_ME = TRUE;
8096 }
8097 else
8098 {
8099 aep = syn_term_attr2entry(screen_attr);
8100 if (aep != NULL && aep->ae_u.term.stop != NULL)
8101 {
8102 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8103 do_ME = TRUE;
8104 else
8105 out_str(aep->ae_u.term.stop);
8106 }
8107 }
8108 if (aep == NULL) /* did ":syntax clear" */
8109 screen_attr = 0;
8110 else
8111 screen_attr = aep->ae_attr;
8112 }
8113
8114 /*
8115 * Often all ending-codes are equal to T_ME. Avoid outputting the
8116 * same sequence several times.
8117 */
8118 if (screen_attr & HL_STANDOUT)
8119 {
8120 if (STRCMP(T_SE, T_ME) == 0)
8121 do_ME = TRUE;
8122 else
8123 out_str(T_SE);
8124 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008125 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {
8127 if (STRCMP(T_UE, T_ME) == 0)
8128 do_ME = TRUE;
8129 else
8130 out_str(T_UE);
8131 }
8132 if (screen_attr & HL_ITALIC)
8133 {
8134 if (STRCMP(T_CZR, T_ME) == 0)
8135 do_ME = TRUE;
8136 else
8137 out_str(T_CZR);
8138 }
8139 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8140 out_str(T_ME);
8141
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008142#ifdef FEAT_TERMGUICOLORS
8143 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008145 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008146 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008147 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008148 term_bg_rgb_color(cterm_normal_bg_gui_color);
8149 }
8150 else
8151#endif
8152 {
8153 if (t_colors > 1)
8154 {
8155 /* set Normal cterm colors */
8156 if (cterm_normal_fg_color != 0)
8157 term_fg_color(cterm_normal_fg_color - 1);
8158 if (cterm_normal_bg_color != 0)
8159 term_bg_color(cterm_normal_bg_color - 1);
8160 if (cterm_normal_fg_bold)
8161 out_str(T_MD);
8162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 }
8164 }
8165 }
8166 screen_attr = 0;
8167}
8168
8169/*
8170 * Reset the colors for a cterm. Used when leaving Vim.
8171 * The machine specific code may override this again.
8172 */
8173 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008174reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008176 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {
8178 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008179#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008180 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8181 || cterm_normal_bg_gui_color != INVALCOLOR)
8182 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008183#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {
8187 out_str(T_OP);
8188 screen_attr = -1;
8189 }
8190 if (cterm_normal_fg_bold)
8191 {
8192 out_str(T_ME);
8193 screen_attr = -1;
8194 }
8195 }
8196}
8197
8198/*
8199 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8200 * using the attributes from ScreenAttrs["off"].
8201 */
8202 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008203screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 int attr;
8206
8207 /* Check for illegal values, just in case (could happen just after
8208 * resizing). */
8209 if (row >= screen_Rows || col >= screen_Columns)
8210 return;
8211
Bram Moolenaar494838a2015-02-10 19:20:37 +01008212 /* Outputting a character in the last cell on the screen may scroll the
8213 * screen up. Only do it when the "xn" termcap property is set, otherwise
8214 * mark the character invalid (update it when scrolled up). */
8215 if (*T_XN == NUL
8216 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217#ifdef FEAT_RIGHTLEFT
8218 /* account for first command-line character in rightleft mode */
8219 && !cmdmsg_rl
8220#endif
8221 )
8222 {
8223 ScreenAttrs[off] = (sattr_T)-1;
8224 return;
8225 }
8226
8227 /*
8228 * Stop highlighting first, so it's easier to move the cursor.
8229 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008230#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (screen_char_attr != 0)
8232 attr = screen_char_attr;
8233 else
8234#endif
8235 attr = ScreenAttrs[off];
8236 if (screen_attr != attr)
8237 screen_stop_highlight();
8238
8239 windgoto(row, col);
8240
8241 if (screen_attr != attr)
8242 screen_start_highlight(attr);
8243
8244#ifdef FEAT_MBYTE
8245 if (enc_utf8 && ScreenLinesUC[off] != 0)
8246 {
8247 char_u buf[MB_MAXBYTES + 1];
8248
8249 /* Convert UTF-8 character to bytes and write it. */
8250
8251 buf[utfc_char2bytes(off, buf)] = NUL;
8252
8253 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008254 if (utf_ambiguous_width(ScreenLinesUC[off]))
8255 screen_cur_col = 9999;
8256 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 ++screen_cur_col;
8258 }
8259 else
8260#endif
8261 {
8262#ifdef FEAT_MBYTE
8263 out_flush_check();
8264#endif
8265 out_char(ScreenLines[off]);
8266#ifdef FEAT_MBYTE
8267 /* double-byte character in single-width cell */
8268 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8269 out_char(ScreenLines2[off]);
8270#endif
8271 }
8272
8273 screen_cur_col++;
8274}
8275
8276#ifdef FEAT_MBYTE
8277
8278/*
8279 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8280 * on the screen at position 'row' and 'col'.
8281 * The attributes of the first byte is used for all. This is required to
8282 * output the two bytes of a double-byte character with nothing in between.
8283 */
8284 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008285screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286{
8287 /* Check for illegal values (could be wrong when screen was resized). */
8288 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8289 return;
8290
8291 /* Outputting the last character on the screen may scrollup the screen.
8292 * Don't to it! Mark the character invalid (update it when scrolled up) */
8293 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8294 {
8295 ScreenAttrs[off] = (sattr_T)-1;
8296 return;
8297 }
8298
8299 /* Output the first byte normally (positions the cursor), then write the
8300 * second byte directly. */
8301 screen_char(off, row, col);
8302 out_char(ScreenLines[off + 1]);
8303 ++screen_cur_col;
8304}
8305#endif
8306
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008307#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308/*
8309 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8310 * This uses the contents of ScreenLines[] and doesn't change it.
8311 */
8312 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008313screen_draw_rectangle(
8314 int row,
8315 int col,
8316 int height,
8317 int width,
8318 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 int r, c;
8321 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008322#ifdef FEAT_MBYTE
8323 int max_off;
8324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008326 /* Can't use ScreenLines unless initialized */
8327 if (ScreenLines == NULL)
8328 return;
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (invert)
8331 screen_char_attr = HL_INVERSE;
8332 for (r = row; r < row + height; ++r)
8333 {
8334 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008335#ifdef FEAT_MBYTE
8336 max_off = off + screen_Columns;
8337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 for (c = col; c < col + width; ++c)
8339 {
8340#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008341 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
8343 screen_char_2(off + c, r, c);
8344 ++c;
8345 }
8346 else
8347#endif
8348 {
8349 screen_char(off + c, r, c);
8350#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008351 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 ++c;
8353#endif
8354 }
8355 }
8356 }
8357 screen_char_attr = 0;
8358}
8359#endif
8360
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008361#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362/*
8363 * Redraw the characters for a vertically split window.
8364 */
8365 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008366redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 int col;
8369 int width;
8370
8371# ifdef FEAT_CLIPBOARD
8372 clip_may_clear_selection(row, end - 1);
8373# endif
8374
8375 if (wp == NULL)
8376 {
8377 col = 0;
8378 width = Columns;
8379 }
8380 else
8381 {
8382 col = wp->w_wincol;
8383 width = wp->w_width;
8384 }
8385 screen_draw_rectangle(row, col, end - row, width, FALSE);
8386}
8387#endif
8388
8389/*
8390 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8391 * with character 'c1' in first column followed by 'c2' in the other columns.
8392 * Use attributes 'attr'.
8393 */
8394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008395screen_fill(
8396 int start_row,
8397 int end_row,
8398 int start_col,
8399 int end_col,
8400 int c1,
8401 int c2,
8402 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int row;
8405 int col;
8406 int off;
8407 int end_off;
8408 int did_delete;
8409 int c;
8410 int norm_term;
8411#if defined(FEAT_GUI) || defined(UNIX)
8412 int force_next = FALSE;
8413#endif
8414
8415 if (end_row > screen_Rows) /* safety check */
8416 end_row = screen_Rows;
8417 if (end_col > screen_Columns) /* safety check */
8418 end_col = screen_Columns;
8419 if (ScreenLines == NULL
8420 || start_row >= end_row
8421 || start_col >= end_col) /* nothing to do */
8422 return;
8423
8424 /* it's a "normal" terminal when not in a GUI or cterm */
8425 norm_term = (
8426#ifdef FEAT_GUI
8427 !gui.in_use &&
8428#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008429 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 for (row = start_row; row < end_row; ++row)
8431 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008432#ifdef FEAT_MBYTE
8433 if (has_mbyte
8434# ifdef FEAT_GUI
8435 && !gui.in_use
8436# endif
8437 )
8438 {
8439 /* When drawing over the right halve of a double-wide char clear
8440 * out the left halve. When drawing over the left halve of a
8441 * double wide-char clear out the right halve. Only needed in a
8442 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008443 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008444 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008445 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008446 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008447 }
8448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 /*
8450 * Try to use delete-line termcap code, when no attributes or in a
8451 * "normal" terminal, where a bold/italic space is just a
8452 * space.
8453 */
8454 did_delete = FALSE;
8455 if (c2 == ' '
8456 && end_col == Columns
8457 && can_clear(T_CE)
8458 && (attr == 0
8459 || (norm_term
8460 && attr <= HL_ALL
8461 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8462 {
8463 /*
8464 * check if we really need to clear something
8465 */
8466 col = start_col;
8467 if (c1 != ' ') /* don't clear first char */
8468 ++col;
8469
8470 off = LineOffset[row] + col;
8471 end_off = LineOffset[row] + end_col;
8472
8473 /* skip blanks (used often, keep it fast!) */
8474#ifdef FEAT_MBYTE
8475 if (enc_utf8)
8476 while (off < end_off && ScreenLines[off] == ' '
8477 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8478 ++off;
8479 else
8480#endif
8481 while (off < end_off && ScreenLines[off] == ' '
8482 && ScreenAttrs[off] == 0)
8483 ++off;
8484 if (off < end_off) /* something to be cleared */
8485 {
8486 col = off - LineOffset[row];
8487 screen_stop_highlight();
8488 term_windgoto(row, col);/* clear rest of this screen line */
8489 out_str(T_CE);
8490 screen_start(); /* don't know where cursor is now */
8491 col = end_col - col;
8492 while (col--) /* clear chars in ScreenLines */
8493 {
8494 ScreenLines[off] = ' ';
8495#ifdef FEAT_MBYTE
8496 if (enc_utf8)
8497 ScreenLinesUC[off] = 0;
8498#endif
8499 ScreenAttrs[off] = 0;
8500 ++off;
8501 }
8502 }
8503 did_delete = TRUE; /* the chars are cleared now */
8504 }
8505
8506 off = LineOffset[row] + start_col;
8507 c = c1;
8508 for (col = start_col; col < end_col; ++col)
8509 {
8510 if (ScreenLines[off] != c
8511#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008512 || (enc_utf8 && (int)ScreenLinesUC[off]
8513 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514#endif
8515 || ScreenAttrs[off] != attr
8516#if defined(FEAT_GUI) || defined(UNIX)
8517 || force_next
8518#endif
8519 )
8520 {
8521#if defined(FEAT_GUI) || defined(UNIX)
8522 /* The bold trick may make a single row of pixels appear in
8523 * the next character. When a bold character is removed, the
8524 * next character should be redrawn too. This happens for our
8525 * own GUI and for some xterms. */
8526 if (
8527# ifdef FEAT_GUI
8528 gui.in_use
8529# endif
8530# if defined(FEAT_GUI) && defined(UNIX)
8531 ||
8532# endif
8533# ifdef UNIX
8534 term_is_xterm
8535# endif
8536 )
8537 {
8538 if (ScreenLines[off] != ' '
8539 && (ScreenAttrs[off] > HL_ALL
8540 || ScreenAttrs[off] & HL_BOLD))
8541 force_next = TRUE;
8542 else
8543 force_next = FALSE;
8544 }
8545#endif
8546 ScreenLines[off] = c;
8547#ifdef FEAT_MBYTE
8548 if (enc_utf8)
8549 {
8550 if (c >= 0x80)
8551 {
8552 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008553 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 }
8555 else
8556 ScreenLinesUC[off] = 0;
8557 }
8558#endif
8559 ScreenAttrs[off] = attr;
8560 if (!did_delete || c != ' ')
8561 screen_char(off, row, col);
8562 }
8563 ++off;
8564 if (col == start_col)
8565 {
8566 if (did_delete)
8567 break;
8568 c = c2;
8569 }
8570 }
8571 if (end_col == Columns)
8572 LineWraps[row] = FALSE;
8573 if (row == Rows - 1) /* overwritten the command line */
8574 {
8575 redraw_cmdline = TRUE;
8576 if (c1 == ' ' && c2 == ' ')
8577 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008578 if (start_col == 0)
8579 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 }
8581 }
8582}
8583
8584/*
8585 * Check if there should be a delay. Used before clearing or redrawing the
8586 * screen or the command line.
8587 */
8588 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008589check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8592 && !did_wait_return
8593 && emsg_silent == 0)
8594 {
8595 out_flush();
8596 ui_delay(1000L, TRUE);
8597 emsg_on_display = FALSE;
8598 if (check_msg_scroll)
8599 msg_scroll = FALSE;
8600 }
8601}
8602
8603/*
8604 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008605 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 * Returns TRUE if there is a valid screen to write to.
8607 * Returns FALSE when starting up and screen not initialized yet.
8608 */
8609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008610screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008612 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 return (ScreenLines != NULL);
8614}
8615
8616/*
8617 * Resize the shell to Rows and Columns.
8618 * Allocate ScreenLines[] and associated items.
8619 *
8620 * There may be some time between setting Rows and Columns and (re)allocating
8621 * ScreenLines[]. This happens when starting up and when (manually) changing
8622 * the shell size. Always use screen_Rows and screen_Columns to access items
8623 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8624 * final size of the shell is needed.
8625 */
8626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008627screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 int new_row, old_row;
8630#ifdef FEAT_GUI
8631 int old_Rows;
8632#endif
8633 win_T *wp;
8634 int outofmem = FALSE;
8635 int len;
8636 schar_T *new_ScreenLines;
8637#ifdef FEAT_MBYTE
8638 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008639 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008641 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#endif
8643 sattr_T *new_ScreenAttrs;
8644 unsigned *new_LineOffset;
8645 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008646#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008647 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008648 tabpage_T *tp;
8649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008651 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008652#ifdef FEAT_AUTOCMD
8653 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008655retry:
8656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 /*
8658 * Allocation of the screen buffers is done only when the size changes and
8659 * when Rows and Columns have been set and we have started doing full
8660 * screen stuff.
8661 */
8662 if ((ScreenLines != NULL
8663 && Rows == screen_Rows
8664 && Columns == screen_Columns
8665#ifdef FEAT_MBYTE
8666 && enc_utf8 == (ScreenLinesUC != NULL)
8667 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008668 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669#endif
8670 )
8671 || Rows == 0
8672 || Columns == 0
8673 || (!full_screen && ScreenLines == NULL))
8674 return;
8675
8676 /*
8677 * It's possible that we produce an out-of-memory message below, which
8678 * will cause this function to be called again. To break the loop, just
8679 * return here.
8680 */
8681 if (entered)
8682 return;
8683 entered = TRUE;
8684
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008685 /*
8686 * Note that the window sizes are updated before reallocating the arrays,
8687 * thus we must not redraw here!
8688 */
8689 ++RedrawingDisabled;
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 win_new_shellsize(); /* fit the windows in the new sized shell */
8692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 comp_col(); /* recompute columns for shown command and ruler */
8694
8695 /*
8696 * We're changing the size of the screen.
8697 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8698 * - Move lines from the old arrays into the new arrays, clear extra
8699 * lines (unless the screen is going to be cleared).
8700 * - Free the old arrays.
8701 *
8702 * If anything fails, make ScreenLines NULL, so we don't do anything!
8703 * Continuing with the old ScreenLines may result in a crash, because the
8704 * size is wrong.
8705 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008706 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008708#ifdef FEAT_AUTOCMD
8709 if (aucmd_win != NULL)
8710 win_free_lsize(aucmd_win);
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 new_ScreenLines = (schar_T *)lalloc((long_u)(
8714 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8715#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008716 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 if (enc_utf8)
8718 {
8719 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8720 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008721 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008722 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8724 }
8725 if (enc_dbcs == DBCS_JPNU)
8726 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8727 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8728#endif
8729 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8730 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8731 new_LineOffset = (unsigned *)lalloc((long_u)(
8732 Rows * sizeof(unsigned)), FALSE);
8733 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008734#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008735 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008738 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
8740 if (win_alloc_lines(wp) == FAIL)
8741 {
8742 outofmem = TRUE;
8743#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008744 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745#endif
8746 }
8747 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008748#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008749 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8750 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008751 outofmem = TRUE;
8752#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008753#ifdef FEAT_WINDOWS
8754give_up:
8755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008757#ifdef FEAT_MBYTE
8758 for (i = 0; i < p_mco; ++i)
8759 if (new_ScreenLinesC[i] == NULL)
8760 break;
8761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 if (new_ScreenLines == NULL
8763#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008764 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8766#endif
8767 || new_ScreenAttrs == NULL
8768 || new_LineOffset == NULL
8769 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008770#ifdef FEAT_WINDOWS
8771 || new_TabPageIdxs == NULL
8772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 || outofmem)
8774 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008775 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008776 {
8777 /* guess the size */
8778 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8779
8780 /* Remember we did this to avoid getting outofmem messages over
8781 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008782 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 vim_free(new_ScreenLines);
8785 new_ScreenLines = NULL;
8786#ifdef FEAT_MBYTE
8787 vim_free(new_ScreenLinesUC);
8788 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008789 for (i = 0; i < p_mco; ++i)
8790 {
8791 vim_free(new_ScreenLinesC[i]);
8792 new_ScreenLinesC[i] = NULL;
8793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 vim_free(new_ScreenLines2);
8795 new_ScreenLines2 = NULL;
8796#endif
8797 vim_free(new_ScreenAttrs);
8798 new_ScreenAttrs = NULL;
8799 vim_free(new_LineOffset);
8800 new_LineOffset = NULL;
8801 vim_free(new_LineWraps);
8802 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008803#ifdef FEAT_WINDOWS
8804 vim_free(new_TabPageIdxs);
8805 new_TabPageIdxs = NULL;
8806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 }
8808 else
8809 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008810 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008811
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 for (new_row = 0; new_row < Rows; ++new_row)
8813 {
8814 new_LineOffset[new_row] = new_row * Columns;
8815 new_LineWraps[new_row] = FALSE;
8816
8817 /*
8818 * If the screen is not going to be cleared, copy as much as
8819 * possible from the old screen to the new one and clear the rest
8820 * (used when resizing the window at the "--more--" prompt or when
8821 * executing an external command, for the GUI).
8822 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008823 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
8825 (void)vim_memset(new_ScreenLines + new_row * Columns,
8826 ' ', (size_t)Columns * sizeof(schar_T));
8827#ifdef FEAT_MBYTE
8828 if (enc_utf8)
8829 {
8830 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8831 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008832 for (i = 0; i < p_mco; ++i)
8833 (void)vim_memset(new_ScreenLinesC[i]
8834 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 0, (size_t)Columns * sizeof(u8char_T));
8836 }
8837 if (enc_dbcs == DBCS_JPNU)
8838 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8839 0, (size_t)Columns * sizeof(schar_T));
8840#endif
8841 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8842 0, (size_t)Columns * sizeof(sattr_T));
8843 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008844 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 if (screen_Columns < Columns)
8847 len = screen_Columns;
8848 else
8849 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008850#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008851 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008852 * may be invalid now. Also when p_mco changes. */
8853 if (!(enc_utf8 && ScreenLinesUC == NULL)
8854 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008855#endif
8856 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8857 ScreenLines + LineOffset[old_row],
8858 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008860 if (enc_utf8 && ScreenLinesUC != NULL
8861 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
8863 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8864 ScreenLinesUC + LineOffset[old_row],
8865 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 for (i = 0; i < p_mco; ++i)
8867 mch_memmove(new_ScreenLinesC[i]
8868 + new_LineOffset[new_row],
8869 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 (size_t)len * sizeof(u8char_T));
8871 }
8872 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8873 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8874 ScreenLines2 + LineOffset[old_row],
8875 (size_t)len * sizeof(schar_T));
8876#endif
8877 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8878 ScreenAttrs + LineOffset[old_row],
8879 (size_t)len * sizeof(sattr_T));
8880 }
8881 }
8882 }
8883 /* Use the last line of the screen for the current line. */
8884 current_ScreenLine = new_ScreenLines + Rows * Columns;
8885 }
8886
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008887 free_screenlines();
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 ScreenLines = new_ScreenLines;
8890#ifdef FEAT_MBYTE
8891 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008892 for (i = 0; i < p_mco; ++i)
8893 ScreenLinesC[i] = new_ScreenLinesC[i];
8894 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 ScreenLines2 = new_ScreenLines2;
8896#endif
8897 ScreenAttrs = new_ScreenAttrs;
8898 LineOffset = new_LineOffset;
8899 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008900#ifdef FEAT_WINDOWS
8901 TabPageIdxs = new_TabPageIdxs;
8902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
8904 /* It's important that screen_Rows and screen_Columns reflect the actual
8905 * size of ScreenLines[]. Set them before calling anything. */
8906#ifdef FEAT_GUI
8907 old_Rows = screen_Rows;
8908#endif
8909 screen_Rows = Rows;
8910 screen_Columns = Columns;
8911
8912 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008913 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 screenclear2();
8915
8916#ifdef FEAT_GUI
8917 else if (gui.in_use
8918 && !gui.starting
8919 && ScreenLines != NULL
8920 && old_Rows != Rows)
8921 {
8922 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8923 /*
8924 * Adjust the position of the cursor, for when executing an external
8925 * command.
8926 */
8927 if (msg_row >= Rows) /* Rows got smaller */
8928 msg_row = Rows - 1; /* put cursor at last row */
8929 else if (Rows > old_Rows) /* Rows got bigger */
8930 msg_row += Rows - old_Rows; /* put cursor in same place */
8931 if (msg_col >= Columns) /* Columns got smaller */
8932 msg_col = Columns - 1; /* put cursor at last column */
8933 }
8934#endif
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008937 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008938
8939#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008940 /*
8941 * Do not apply autocommands more than 3 times to avoid an endless loop
8942 * in case applying autocommands always changes Rows or Columns.
8943 */
8944 if (starting == 0 && ++retry_count <= 3)
8945 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008946 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008947 /* In rare cases, autocommands may have altered Rows or Columns,
8948 * jump back to check if we need to allocate the screen again. */
8949 goto retry;
8950 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008955free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008956{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008957#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 int i;
8959
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008960 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008961 for (i = 0; i < Screen_mco; ++i)
8962 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008963 vim_free(ScreenLines2);
8964#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008966 vim_free(ScreenAttrs);
8967 vim_free(LineOffset);
8968 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008969#ifdef FEAT_WINDOWS
8970 vim_free(TabPageIdxs);
8971#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008972}
8973
8974 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008975screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 check_for_delay(FALSE);
8978 screenalloc(FALSE); /* allocate screen buffers if size changed */
8979 screenclear2(); /* clear the screen */
8980}
8981
8982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008983screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 int i;
8986
8987 if (starting == NO_SCREEN || ScreenLines == NULL
8988#ifdef FEAT_GUI
8989 || (gui.in_use && gui.starting)
8990#endif
8991 )
8992 return;
8993
8994#ifdef FEAT_GUI
8995 if (!gui.in_use)
8996#endif
8997 screen_attr = -1; /* force setting the Normal colors */
8998 screen_stop_highlight(); /* don't want highlighting here */
8999
9000#ifdef FEAT_CLIPBOARD
9001 /* disable selection without redrawing it */
9002 clip_scroll_selection(9999);
9003#endif
9004
9005 /* blank out ScreenLines */
9006 for (i = 0; i < Rows; ++i)
9007 {
9008 lineclear(LineOffset[i], (int)Columns);
9009 LineWraps[i] = FALSE;
9010 }
9011
9012 if (can_clear(T_CL))
9013 {
9014 out_str(T_CL); /* clear the display */
9015 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009016 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018 else
9019 {
9020 /* can't clear the screen, mark all chars with invalid attributes */
9021 for (i = 0; i < Rows; ++i)
9022 lineinvalid(LineOffset[i], (int)Columns);
9023 clear_cmdline = TRUE;
9024 }
9025
9026 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9027
9028 win_rest_invalid(firstwin);
9029 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009030#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009031 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 if (must_redraw == CLEAR) /* no need to clear again */
9034 must_redraw = NOT_VALID;
9035 compute_cmdrow();
9036 msg_row = cmdline_row; /* put cursor on last line for messages */
9037 msg_col = 0;
9038 screen_start(); /* don't know where cursor is now */
9039 msg_scrolled = 0; /* can't scroll back */
9040 msg_didany = FALSE;
9041 msg_didout = FALSE;
9042}
9043
9044/*
9045 * Clear one line in ScreenLines.
9046 */
9047 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009048lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9051#ifdef FEAT_MBYTE
9052 if (enc_utf8)
9053 (void)vim_memset(ScreenLinesUC + off, 0,
9054 (size_t)width * sizeof(u8char_T));
9055#endif
9056 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9057}
9058
9059/*
9060 * Mark one line in ScreenLines invalid by setting the attributes to an
9061 * invalid value.
9062 */
9063 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009064lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
9066 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9067}
9068
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009069#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070/*
9071 * Copy part of a Screenline for vertically split window "wp".
9072 */
9073 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009074linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
9076 unsigned off_to = LineOffset[to] + wp->w_wincol;
9077 unsigned off_from = LineOffset[from] + wp->w_wincol;
9078
9079 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9080 wp->w_width * sizeof(schar_T));
9081# ifdef FEAT_MBYTE
9082 if (enc_utf8)
9083 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009084 int i;
9085
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9087 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009088 for (i = 0; i < p_mco; ++i)
9089 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9090 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 }
9092 if (enc_dbcs == DBCS_JPNU)
9093 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9094 wp->w_width * sizeof(schar_T));
9095# endif
9096 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9097 wp->w_width * sizeof(sattr_T));
9098}
9099#endif
9100
9101/*
9102 * Return TRUE if clearing with term string "p" would work.
9103 * It can't work when the string is empty or it won't set the right background.
9104 */
9105 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009106can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 return (*p != NUL && (t_colors <= 1
9109#ifdef FEAT_GUI
9110 || gui.in_use
9111#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009112#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009113 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009114 || (!p_tgc && cterm_normal_bg_color == 0)
9115#else
9116 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009117#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009118 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119}
9120
9121/*
9122 * Reset cursor position. Use whenever cursor was moved because of outputting
9123 * something directly to the screen (shell commands) or a terminal control
9124 * code.
9125 */
9126 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009127screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
9129 screen_cur_row = screen_cur_col = 9999;
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * Move the cursor to position "row","col" in the screen.
9134 * This tries to find the most efficient way to move, minimizing the number of
9135 * characters sent to the terminal.
9136 */
9137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009138windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009140 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 int i;
9142 int plan;
9143 int cost;
9144 int wouldbe_col;
9145 int noinvcurs;
9146 char_u *bs;
9147 int goto_cost;
9148 int attr;
9149
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009150#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9152
9153#define PLAN_LE 1
9154#define PLAN_CR 2
9155#define PLAN_NL 3
9156#define PLAN_WRITE 4
9157 /* Can't use ScreenLines unless initialized */
9158 if (ScreenLines == NULL)
9159 return;
9160
9161 if (col != screen_cur_col || row != screen_cur_row)
9162 {
9163 /* Check for valid position. */
9164 if (row < 0) /* window without text lines? */
9165 row = 0;
9166 if (row >= screen_Rows)
9167 row = screen_Rows - 1;
9168 if (col >= screen_Columns)
9169 col = screen_Columns - 1;
9170
9171 /* check if no cursor movement is allowed in highlight mode */
9172 if (screen_attr && *T_MS == NUL)
9173 noinvcurs = HIGHL_COST;
9174 else
9175 noinvcurs = 0;
9176 goto_cost = GOTO_COST + noinvcurs;
9177
9178 /*
9179 * Plan how to do the positioning:
9180 * 1. Use CR to move it to column 0, same row.
9181 * 2. Use T_LE to move it a few columns to the left.
9182 * 3. Use NL to move a few lines down, column 0.
9183 * 4. Move a few columns to the right with T_ND or by writing chars.
9184 *
9185 * Don't do this if the cursor went beyond the last column, the cursor
9186 * position is unknown then (some terminals wrap, some don't )
9187 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009188 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 * characters to move the cursor to the right.
9190 */
9191 if (row >= screen_cur_row && screen_cur_col < Columns)
9192 {
9193 /*
9194 * If the cursor is in the same row, bigger col, we can use CR
9195 * or T_LE.
9196 */
9197 bs = NULL; /* init for GCC */
9198 attr = screen_attr;
9199 if (row == screen_cur_row && col < screen_cur_col)
9200 {
9201 /* "le" is preferred over "bc", because "bc" is obsolete */
9202 if (*T_LE)
9203 bs = T_LE; /* "cursor left" */
9204 else
9205 bs = T_BC; /* "backspace character (old) */
9206 if (*bs)
9207 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9208 else
9209 cost = 999;
9210 if (col + 1 < cost) /* using CR is less characters */
9211 {
9212 plan = PLAN_CR;
9213 wouldbe_col = 0;
9214 cost = 1; /* CR is just one character */
9215 }
9216 else
9217 {
9218 plan = PLAN_LE;
9219 wouldbe_col = col;
9220 }
9221 if (noinvcurs) /* will stop highlighting */
9222 {
9223 cost += noinvcurs;
9224 attr = 0;
9225 }
9226 }
9227
9228 /*
9229 * If the cursor is above where we want to be, we can use CR LF.
9230 */
9231 else if (row > screen_cur_row)
9232 {
9233 plan = PLAN_NL;
9234 wouldbe_col = 0;
9235 cost = (row - screen_cur_row) * 2; /* CR LF */
9236 if (noinvcurs) /* will stop highlighting */
9237 {
9238 cost += noinvcurs;
9239 attr = 0;
9240 }
9241 }
9242
9243 /*
9244 * If the cursor is in the same row, smaller col, just use write.
9245 */
9246 else
9247 {
9248 plan = PLAN_WRITE;
9249 wouldbe_col = screen_cur_col;
9250 cost = 0;
9251 }
9252
9253 /*
9254 * Check if any characters that need to be written have the
9255 * correct attributes. Also avoid UTF-8 characters.
9256 */
9257 i = col - wouldbe_col;
9258 if (i > 0)
9259 cost += i;
9260 if (cost < goto_cost && i > 0)
9261 {
9262 /*
9263 * Check if the attributes are correct without additionally
9264 * stopping highlighting.
9265 */
9266 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9267 while (i && *p++ == attr)
9268 --i;
9269 if (i != 0)
9270 {
9271 /*
9272 * Try if it works when highlighting is stopped here.
9273 */
9274 if (*--p == 0)
9275 {
9276 cost += noinvcurs;
9277 while (i && *p++ == 0)
9278 --i;
9279 }
9280 if (i != 0)
9281 cost = 999; /* different attributes, don't do it */
9282 }
9283#ifdef FEAT_MBYTE
9284 if (enc_utf8)
9285 {
9286 /* Don't use an UTF-8 char for positioning, it's slow. */
9287 for (i = wouldbe_col; i < col; ++i)
9288 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9289 {
9290 cost = 999;
9291 break;
9292 }
9293 }
9294#endif
9295 }
9296
9297 /*
9298 * We can do it without term_windgoto()!
9299 */
9300 if (cost < goto_cost)
9301 {
9302 if (plan == PLAN_LE)
9303 {
9304 if (noinvcurs)
9305 screen_stop_highlight();
9306 while (screen_cur_col > col)
9307 {
9308 out_str(bs);
9309 --screen_cur_col;
9310 }
9311 }
9312 else if (plan == PLAN_CR)
9313 {
9314 if (noinvcurs)
9315 screen_stop_highlight();
9316 out_char('\r');
9317 screen_cur_col = 0;
9318 }
9319 else if (plan == PLAN_NL)
9320 {
9321 if (noinvcurs)
9322 screen_stop_highlight();
9323 while (screen_cur_row < row)
9324 {
9325 out_char('\n');
9326 ++screen_cur_row;
9327 }
9328 screen_cur_col = 0;
9329 }
9330
9331 i = col - screen_cur_col;
9332 if (i > 0)
9333 {
9334 /*
9335 * Use cursor-right if it's one character only. Avoids
9336 * removing a line of pixels from the last bold char, when
9337 * using the bold trick in the GUI.
9338 */
9339 if (T_ND[0] != NUL && T_ND[1] == NUL)
9340 {
9341 while (i-- > 0)
9342 out_char(*T_ND);
9343 }
9344 else
9345 {
9346 int off;
9347
9348 off = LineOffset[row] + screen_cur_col;
9349 while (i-- > 0)
9350 {
9351 if (ScreenAttrs[off] != screen_attr)
9352 screen_stop_highlight();
9353#ifdef FEAT_MBYTE
9354 out_flush_check();
9355#endif
9356 out_char(ScreenLines[off]);
9357#ifdef FEAT_MBYTE
9358 if (enc_dbcs == DBCS_JPNU
9359 && ScreenLines[off] == 0x8e)
9360 out_char(ScreenLines2[off]);
9361#endif
9362 ++off;
9363 }
9364 }
9365 }
9366 }
9367 }
9368 else
9369 cost = 999;
9370
9371 if (cost >= goto_cost)
9372 {
9373 if (noinvcurs)
9374 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009375 if (row == screen_cur_row && (col > screen_cur_col)
9376 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 term_cursor_right(col - screen_cur_col);
9378 else
9379 term_windgoto(row, col);
9380 }
9381 screen_cur_row = row;
9382 screen_cur_col = col;
9383 }
9384}
9385
9386/*
9387 * Set cursor to its position in the current window.
9388 */
9389 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009390setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
9392 if (redrawing())
9393 {
9394 validate_cursor();
9395 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9396 W_WINCOL(curwin) + (
9397#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009398 /* With 'rightleft' set and the cursor on a double-wide
9399 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9401# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009402 (has_mbyte
9403 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9404 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405# endif
9406 1)) :
9407#endif
9408 curwin->w_wcol));
9409 }
9410}
9411
9412
9413/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009414 * Insert 'line_count' lines at 'row' in window 'wp'.
9415 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9416 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 * scrolling.
9418 * Returns FAIL if the lines are not inserted, OK for success.
9419 */
9420 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009421win_ins_lines(
9422 win_T *wp,
9423 int row,
9424 int line_count,
9425 int invalid,
9426 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427{
9428 int did_delete;
9429 int nextrow;
9430 int lastrow;
9431 int retval;
9432
9433 if (invalid)
9434 wp->w_lines_valid = 0;
9435
9436 if (wp->w_height < 5)
9437 return FAIL;
9438
9439 if (line_count > wp->w_height - row)
9440 line_count = wp->w_height - row;
9441
9442 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9443 if (retval != MAYBE)
9444 return retval;
9445
9446 /*
9447 * If there is a next window or a status line, we first try to delete the
9448 * lines at the bottom to avoid messing what is after the window.
9449 * If this fails and there are following windows, don't do anything to avoid
9450 * messing up those windows, better just redraw.
9451 */
9452 did_delete = FALSE;
9453#ifdef FEAT_WINDOWS
9454 if (wp->w_next != NULL || wp->w_status_height)
9455 {
9456 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9457 line_count, (int)Rows, FALSE, NULL) == OK)
9458 did_delete = TRUE;
9459 else if (wp->w_next)
9460 return FAIL;
9461 }
9462#endif
9463 /*
9464 * if no lines deleted, blank the lines that will end up below the window
9465 */
9466 if (!did_delete)
9467 {
9468#ifdef FEAT_WINDOWS
9469 wp->w_redr_status = TRUE;
9470#endif
9471 redraw_cmdline = TRUE;
9472 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9473 lastrow = nextrow + line_count;
9474 if (lastrow > Rows)
9475 lastrow = Rows;
9476 screen_fill(nextrow - line_count, lastrow - line_count,
9477 W_WINCOL(wp), (int)W_ENDCOL(wp),
9478 ' ', ' ', 0);
9479 }
9480
9481 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9482 == FAIL)
9483 {
9484 /* deletion will have messed up other windows */
9485 if (did_delete)
9486 {
9487#ifdef FEAT_WINDOWS
9488 wp->w_redr_status = TRUE;
9489#endif
9490 win_rest_invalid(W_NEXT(wp));
9491 }
9492 return FAIL;
9493 }
9494
9495 return OK;
9496}
9497
9498/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009499 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9501 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9502 * scrolling
9503 * Return OK for success, FAIL if the lines are not deleted.
9504 */
9505 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009506win_del_lines(
9507 win_T *wp,
9508 int row,
9509 int line_count,
9510 int invalid,
9511 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512{
9513 int retval;
9514
9515 if (invalid)
9516 wp->w_lines_valid = 0;
9517
9518 if (line_count > wp->w_height - row)
9519 line_count = wp->w_height - row;
9520
9521 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9522 if (retval != MAYBE)
9523 return retval;
9524
9525 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9526 (int)Rows, FALSE, NULL) == FAIL)
9527 return FAIL;
9528
9529#ifdef FEAT_WINDOWS
9530 /*
9531 * If there are windows or status lines below, try to put them at the
9532 * correct place. If we can't do that, they have to be redrawn.
9533 */
9534 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9535 {
9536 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9537 line_count, (int)Rows, NULL) == FAIL)
9538 {
9539 wp->w_redr_status = TRUE;
9540 win_rest_invalid(wp->w_next);
9541 }
9542 }
9543 /*
9544 * If this is the last window and there is no status line, redraw the
9545 * command line later.
9546 */
9547 else
9548#endif
9549 redraw_cmdline = TRUE;
9550 return OK;
9551}
9552
9553/*
9554 * Common code for win_ins_lines() and win_del_lines().
9555 * Returns OK or FAIL when the work has been done.
9556 * Returns MAYBE when not finished yet.
9557 */
9558 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009559win_do_lines(
9560 win_T *wp,
9561 int row,
9562 int line_count,
9563 int mayclear,
9564 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566 int retval;
9567
9568 if (!redrawing() || line_count <= 0)
9569 return FAIL;
9570
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009571 /* When inserting lines would result in loss of command output, just redraw
9572 * the lines. */
9573 if (no_win_do_lines_ins && !del)
9574 return FAIL;
9575
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 /* only a few lines left: redraw is faster */
9577 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009578#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 && wp->w_width == Columns
9580#endif
9581 )
9582 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009583 if (!no_win_do_lines_ins)
9584 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 return FAIL;
9586 }
9587
9588 /*
9589 * Delete all remaining lines
9590 */
9591 if (row + line_count >= wp->w_height)
9592 {
9593 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9594 W_WINCOL(wp), (int)W_ENDCOL(wp),
9595 ' ', ' ', 0);
9596 return OK;
9597 }
9598
9599 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009600 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009602 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009604 if (!no_win_do_lines_ins)
9605 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607 /*
9608 * If the terminal can set a scroll region, use that.
9609 * Always do this in a vertically split window. This will redraw from
9610 * ScreenLines[] when t_CV isn't defined. That's faster than using
9611 * win_line().
9612 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009613 * a character in the lower right corner of the scroll region may cause a
9614 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 */
9616 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009617#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 || W_WIDTH(wp) != Columns
9619#endif
9620 )
9621 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009622#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9624#endif
9625 scroll_region_set(wp, row);
9626 if (del)
9627 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9628 wp->w_height - row, FALSE, wp);
9629 else
9630 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9631 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009632#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9634#endif
9635 scroll_region_reset();
9636 return retval;
9637 }
9638
9639#ifdef FEAT_WINDOWS
9640 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9641 return FAIL;
9642#endif
9643
9644 return MAYBE;
9645}
9646
9647/*
9648 * window 'wp' and everything after it is messed up, mark it for redraw
9649 */
9650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009651win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653#ifdef FEAT_WINDOWS
9654 while (wp != NULL)
9655#else
9656 if (wp != NULL)
9657#endif
9658 {
9659 redraw_win_later(wp, NOT_VALID);
9660#ifdef FEAT_WINDOWS
9661 wp->w_redr_status = TRUE;
9662 wp = wp->w_next;
9663#endif
9664 }
9665 redraw_cmdline = TRUE;
9666}
9667
9668/*
9669 * The rest of the routines in this file perform screen manipulations. The
9670 * given operation is performed physically on the screen. The corresponding
9671 * change is also made to the internal screen image. In this way, the editor
9672 * anticipates the effect of editing changes on the appearance of the screen.
9673 * That way, when we call screenupdate a complete redraw isn't usually
9674 * necessary. Another advantage is that we can keep adding code to anticipate
9675 * screen changes, and in the meantime, everything still works.
9676 */
9677
9678/*
9679 * types for inserting or deleting lines
9680 */
9681#define USE_T_CAL 1
9682#define USE_T_CDL 2
9683#define USE_T_AL 3
9684#define USE_T_CE 4
9685#define USE_T_DL 5
9686#define USE_T_SR 6
9687#define USE_NL 7
9688#define USE_T_CD 8
9689#define USE_REDRAW 9
9690
9691/*
9692 * insert lines on the screen and update ScreenLines[]
9693 * 'end' is the line after the scrolled part. Normally it is Rows.
9694 * When scrolling region used 'off' is the offset from the top for the region.
9695 * 'row' and 'end' are relative to the start of the region.
9696 *
9697 * return FAIL for failure, OK for success.
9698 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009699 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009700screen_ins_lines(
9701 int off,
9702 int row,
9703 int line_count,
9704 int end,
9705 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 int i;
9708 int j;
9709 unsigned temp;
9710 int cursor_row;
9711 int type;
9712 int result_empty;
9713 int can_ce = can_clear(T_CE);
9714
9715 /*
9716 * FAIL if
9717 * - there is no valid screen
9718 * - the screen has to be redrawn completely
9719 * - the line count is less than one
9720 * - the line count is more than 'ttyscroll'
9721 */
9722 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9723 return FAIL;
9724
9725 /*
9726 * There are seven ways to insert lines:
9727 * 0. When in a vertically split window and t_CV isn't set, redraw the
9728 * characters from ScreenLines[].
9729 * 1. Use T_CD (clear to end of display) if it exists and the result of
9730 * the insert is just empty lines
9731 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9732 * present or line_count > 1. It looks better if we do all the inserts
9733 * at once.
9734 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9735 * insert is just empty lines and T_CE is not present or line_count >
9736 * 1.
9737 * 4. Use T_AL (insert line) if it exists.
9738 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9739 * just empty lines.
9740 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9741 * just empty lines.
9742 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9743 * the 'da' flag is not set or we have clear line capability.
9744 * 8. redraw the characters from ScreenLines[].
9745 *
9746 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9747 * the scrollbar for the window. It does have insert line, use that if it
9748 * exists.
9749 */
9750 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009751#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9753 type = USE_REDRAW;
9754 else
9755#endif
9756 if (can_clear(T_CD) && result_empty)
9757 type = USE_T_CD;
9758 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9759 type = USE_T_CAL;
9760 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9761 type = USE_T_CDL;
9762 else if (*T_AL != NUL)
9763 type = USE_T_AL;
9764 else if (can_ce && result_empty)
9765 type = USE_T_CE;
9766 else if (*T_DL != NUL && result_empty)
9767 type = USE_T_DL;
9768 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9769 type = USE_T_SR;
9770 else
9771 return FAIL;
9772
9773 /*
9774 * For clearing the lines screen_del_lines() is used. This will also take
9775 * care of t_db if necessary.
9776 */
9777 if (type == USE_T_CD || type == USE_T_CDL ||
9778 type == USE_T_CE || type == USE_T_DL)
9779 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9780
9781 /*
9782 * If text is retained below the screen, first clear or delete as many
9783 * lines at the bottom of the window as are about to be inserted so that
9784 * the deleted lines won't later surface during a screen_del_lines.
9785 */
9786 if (*T_DB)
9787 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9788
9789#ifdef FEAT_CLIPBOARD
9790 /* Remove a modeless selection when inserting lines halfway the screen
9791 * or not the full width of the screen. */
9792 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009793# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 || (wp != NULL && wp->w_width != Columns)
9795# endif
9796 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009797 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 else
9799 clip_scroll_selection(-line_count);
9800#endif
9801
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#ifdef FEAT_GUI
9803 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9804 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009805 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806#endif
9807
9808 if (*T_CCS != NUL) /* cursor relative to region */
9809 cursor_row = row;
9810 else
9811 cursor_row = row + off;
9812
9813 /*
9814 * Shift LineOffset[] line_count down to reflect the inserted lines.
9815 * Clear the inserted lines in ScreenLines[].
9816 */
9817 row += off;
9818 end += off;
9819 for (i = 0; i < line_count; ++i)
9820 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009821#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 if (wp != NULL && wp->w_width != Columns)
9823 {
9824 /* need to copy part of a line */
9825 j = end - 1 - i;
9826 while ((j -= line_count) >= row)
9827 linecopy(j + line_count, j, wp);
9828 j += line_count;
9829 if (can_clear((char_u *)" "))
9830 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9831 else
9832 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9833 LineWraps[j] = FALSE;
9834 }
9835 else
9836#endif
9837 {
9838 j = end - 1 - i;
9839 temp = LineOffset[j];
9840 while ((j -= line_count) >= row)
9841 {
9842 LineOffset[j + line_count] = LineOffset[j];
9843 LineWraps[j + line_count] = LineWraps[j];
9844 }
9845 LineOffset[j + line_count] = temp;
9846 LineWraps[j + line_count] = FALSE;
9847 if (can_clear((char_u *)" "))
9848 lineclear(temp, (int)Columns);
9849 else
9850 lineinvalid(temp, (int)Columns);
9851 }
9852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
9854 screen_stop_highlight();
9855 windgoto(cursor_row, 0);
9856
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009857#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 /* redraw the characters */
9859 if (type == USE_REDRAW)
9860 redraw_block(row, end, wp);
9861 else
9862#endif
9863 if (type == USE_T_CAL)
9864 {
9865 term_append_lines(line_count);
9866 screen_start(); /* don't know where cursor is now */
9867 }
9868 else
9869 {
9870 for (i = 0; i < line_count; i++)
9871 {
9872 if (type == USE_T_AL)
9873 {
9874 if (i && cursor_row != 0)
9875 windgoto(cursor_row, 0);
9876 out_str(T_AL);
9877 }
9878 else /* type == USE_T_SR */
9879 out_str(T_SR);
9880 screen_start(); /* don't know where cursor is now */
9881 }
9882 }
9883
9884 /*
9885 * With scroll-reverse and 'da' flag set we need to clear the lines that
9886 * have been scrolled down into the region.
9887 */
9888 if (type == USE_T_SR && *T_DA)
9889 {
9890 for (i = 0; i < line_count; ++i)
9891 {
9892 windgoto(off + i, 0);
9893 out_str(T_CE);
9894 screen_start(); /* don't know where cursor is now */
9895 }
9896 }
9897
9898#ifdef FEAT_GUI
9899 gui_can_update_cursor();
9900 if (gui.in_use)
9901 out_flush(); /* always flush after a scroll */
9902#endif
9903 return OK;
9904}
9905
9906/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009907 * Delete lines on the screen and update ScreenLines[].
9908 * "end" is the line after the scrolled part. Normally it is Rows.
9909 * When scrolling region used "off" is the offset from the top for the region.
9910 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 *
9912 * Return OK for success, FAIL if the lines are not deleted.
9913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009915screen_del_lines(
9916 int off,
9917 int row,
9918 int line_count,
9919 int end,
9920 int force, /* even when line_count > p_ttyscroll */
9921 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 int j;
9924 int i;
9925 unsigned temp;
9926 int cursor_row;
9927 int cursor_end;
9928 int result_empty; /* result is empty until end of region */
9929 int can_delete; /* deleting line codes can be used */
9930 int type;
9931
9932 /*
9933 * FAIL if
9934 * - there is no valid screen
9935 * - the screen has to be redrawn completely
9936 * - the line count is less than one
9937 * - the line count is more than 'ttyscroll'
9938 */
9939 if (!screen_valid(TRUE) || line_count <= 0 ||
9940 (!force && line_count > p_ttyscroll))
9941 return FAIL;
9942
9943 /*
9944 * Check if the rest of the current region will become empty.
9945 */
9946 result_empty = row + line_count >= end;
9947
9948 /*
9949 * We can delete lines only when 'db' flag not set or when 'ce' option
9950 * available.
9951 */
9952 can_delete = (*T_DB == NUL || can_clear(T_CE));
9953
9954 /*
9955 * There are six ways to delete lines:
9956 * 0. When in a vertically split window and t_CV isn't set, redraw the
9957 * characters from ScreenLines[].
9958 * 1. Use T_CD if it exists and the result is empty.
9959 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9960 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9961 * none of the other ways work.
9962 * 4. Use T_CE (erase line) if the result is empty.
9963 * 5. Use T_DL (delete line) if it exists.
9964 * 6. redraw the characters from ScreenLines[].
9965 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009966#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9968 type = USE_REDRAW;
9969 else
9970#endif
9971 if (can_clear(T_CD) && result_empty)
9972 type = USE_T_CD;
9973#if defined(__BEOS__) && defined(BEOS_DR8)
9974 /*
9975 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9976 * its internal termcap... this works okay for tests which test *T_DB !=
9977 * NUL. It has the disadvantage that the user cannot use any :set t_*
9978 * command to get T_DB (back) to empty_option, only :set term=... will do
9979 * the trick...
9980 * Anyway, this hack will hopefully go away with the next OS release.
9981 * (Olaf Seibert)
9982 */
9983 else if (row == 0 && T_DB == empty_option
9984 && (line_count == 1 || *T_CDL == NUL))
9985#else
9986 else if (row == 0 && (
9987#ifndef AMIGA
9988 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9989 * up, so use delete-line command */
9990 line_count == 1 ||
9991#endif
9992 *T_CDL == NUL))
9993#endif
9994 type = USE_NL;
9995 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9996 type = USE_T_CDL;
9997 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009998#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 && (wp == NULL || wp->w_width == Columns)
10000#endif
10001 )
10002 type = USE_T_CE;
10003 else if (*T_DL != NUL && can_delete)
10004 type = USE_T_DL;
10005 else if (*T_CDL != NUL && can_delete)
10006 type = USE_T_CDL;
10007 else
10008 return FAIL;
10009
10010#ifdef FEAT_CLIPBOARD
10011 /* Remove a modeless selection when deleting lines halfway the screen or
10012 * not the full width of the screen. */
10013 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010014# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 || (wp != NULL && wp->w_width != Columns)
10016# endif
10017 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010018 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020 clip_scroll_selection(line_count);
10021#endif
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023#ifdef FEAT_GUI
10024 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10025 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010026 gui_dont_update_cursor(gui.cursor_row >= row + off
10027 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028#endif
10029
10030 if (*T_CCS != NUL) /* cursor relative to region */
10031 {
10032 cursor_row = row;
10033 cursor_end = end;
10034 }
10035 else
10036 {
10037 cursor_row = row + off;
10038 cursor_end = end + off;
10039 }
10040
10041 /*
10042 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10043 * Clear the inserted lines in ScreenLines[].
10044 */
10045 row += off;
10046 end += off;
10047 for (i = 0; i < line_count; ++i)
10048 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010049#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 if (wp != NULL && wp->w_width != Columns)
10051 {
10052 /* need to copy part of a line */
10053 j = row + i;
10054 while ((j += line_count) <= end - 1)
10055 linecopy(j - line_count, j, wp);
10056 j -= line_count;
10057 if (can_clear((char_u *)" "))
10058 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10059 else
10060 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10061 LineWraps[j] = FALSE;
10062 }
10063 else
10064#endif
10065 {
10066 /* whole width, moving the line pointers is faster */
10067 j = row + i;
10068 temp = LineOffset[j];
10069 while ((j += line_count) <= end - 1)
10070 {
10071 LineOffset[j - line_count] = LineOffset[j];
10072 LineWraps[j - line_count] = LineWraps[j];
10073 }
10074 LineOffset[j - line_count] = temp;
10075 LineWraps[j - line_count] = FALSE;
10076 if (can_clear((char_u *)" "))
10077 lineclear(temp, (int)Columns);
10078 else
10079 lineinvalid(temp, (int)Columns);
10080 }
10081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
10083 screen_stop_highlight();
10084
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010085#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 /* redraw the characters */
10087 if (type == USE_REDRAW)
10088 redraw_block(row, end, wp);
10089 else
10090#endif
10091 if (type == USE_T_CD) /* delete the lines */
10092 {
10093 windgoto(cursor_row, 0);
10094 out_str(T_CD);
10095 screen_start(); /* don't know where cursor is now */
10096 }
10097 else if (type == USE_T_CDL)
10098 {
10099 windgoto(cursor_row, 0);
10100 term_delete_lines(line_count);
10101 screen_start(); /* don't know where cursor is now */
10102 }
10103 /*
10104 * Deleting lines at top of the screen or scroll region: Just scroll
10105 * the whole screen (scroll region) up by outputting newlines on the
10106 * last line.
10107 */
10108 else if (type == USE_NL)
10109 {
10110 windgoto(cursor_end - 1, 0);
10111 for (i = line_count; --i >= 0; )
10112 out_char('\n'); /* cursor will remain on same line */
10113 }
10114 else
10115 {
10116 for (i = line_count; --i >= 0; )
10117 {
10118 if (type == USE_T_DL)
10119 {
10120 windgoto(cursor_row, 0);
10121 out_str(T_DL); /* delete a line */
10122 }
10123 else /* type == USE_T_CE */
10124 {
10125 windgoto(cursor_row + i, 0);
10126 out_str(T_CE); /* erase a line */
10127 }
10128 screen_start(); /* don't know where cursor is now */
10129 }
10130 }
10131
10132 /*
10133 * If the 'db' flag is set, we need to clear the lines that have been
10134 * scrolled up at the bottom of the region.
10135 */
10136 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10137 {
10138 for (i = line_count; i > 0; --i)
10139 {
10140 windgoto(cursor_end - i, 0);
10141 out_str(T_CE); /* erase a line */
10142 screen_start(); /* don't know where cursor is now */
10143 }
10144 }
10145
10146#ifdef FEAT_GUI
10147 gui_can_update_cursor();
10148 if (gui.in_use)
10149 out_flush(); /* always flush after a scroll */
10150#endif
10151
10152 return OK;
10153}
10154
10155/*
10156 * show the current mode and ruler
10157 *
10158 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10159 * If clear_cmdline is FALSE there may be a message there that needs to be
10160 * cleared only if a mode is shown.
10161 * Return the length of the message (0 if no message).
10162 */
10163 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010164showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165{
10166 int need_clear;
10167 int length = 0;
10168 int do_mode;
10169 int attr;
10170 int nwr_save;
10171#ifdef FEAT_INS_EXPAND
10172 int sub_attr;
10173#endif
10174
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010175 do_mode = ((p_smd && msg_silent == 0)
10176 && ((State & INSERT)
10177 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010178 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 if (do_mode || Recording)
10180 {
10181 /*
10182 * Don't show mode right now, when not redrawing or inside a mapping.
10183 * Call char_avail() only when we are going to show something, because
10184 * it takes a bit of time.
10185 */
10186 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10187 {
10188 redraw_cmdline = TRUE; /* show mode later */
10189 return 0;
10190 }
10191
10192 nwr_save = need_wait_return;
10193
10194 /* wait a bit before overwriting an important message */
10195 check_for_delay(FALSE);
10196
10197 /* if the cmdline is more than one line high, erase top lines */
10198 need_clear = clear_cmdline;
10199 if (clear_cmdline && cmdline_row < Rows - 1)
10200 msg_clr_cmdline(); /* will reset clear_cmdline */
10201
10202 /* Position on the last line in the window, column 0 */
10203 msg_pos_mode();
10204 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010205 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (do_mode)
10207 {
10208 MSG_PUTS_ATTR("--", attr);
10209#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010210 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010211# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010212 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010213# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010214 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010215# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010216 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010217# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 MSG_PUTS_ATTR(" IM", attr);
10219# else
10220 MSG_PUTS_ATTR(" XIM", attr);
10221# endif
10222#endif
10223#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10224 if (gui.in_use)
10225 {
10226 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010227 {
10228 /* HANGUL */
10229 if (enc_utf8)
10230 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10231 else
10232 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 }
10235#endif
10236#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010237 /* CTRL-X in Insert mode */
10238 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 {
10240 /* These messages can get long, avoid a wrap in a narrow
10241 * window. Prefer showing edit_submode_extra. */
10242 length = (Rows - msg_row) * Columns - 3;
10243 if (edit_submode_extra != NULL)
10244 length -= vim_strsize(edit_submode_extra);
10245 if (length > 0)
10246 {
10247 if (edit_submode_pre != NULL)
10248 length -= vim_strsize(edit_submode_pre);
10249 if (length - vim_strsize(edit_submode) > 0)
10250 {
10251 if (edit_submode_pre != NULL)
10252 msg_puts_attr(edit_submode_pre, attr);
10253 msg_puts_attr(edit_submode, attr);
10254 }
10255 if (edit_submode_extra != NULL)
10256 {
10257 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10258 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010259 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 else
10261 sub_attr = attr;
10262 msg_puts_attr(edit_submode_extra, sub_attr);
10263 }
10264 }
10265 length = 0;
10266 }
10267 else
10268#endif
10269 {
10270#ifdef FEAT_VREPLACE
10271 if (State & VREPLACE_FLAG)
10272 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10273 else
10274#endif
10275 if (State & REPLACE_FLAG)
10276 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10277 else if (State & INSERT)
10278 {
10279#ifdef FEAT_RIGHTLEFT
10280 if (p_ri)
10281 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10282#endif
10283 MSG_PUTS_ATTR(_(" INSERT"), attr);
10284 }
10285 else if (restart_edit == 'I')
10286 MSG_PUTS_ATTR(_(" (insert)"), attr);
10287 else if (restart_edit == 'R')
10288 MSG_PUTS_ATTR(_(" (replace)"), attr);
10289 else if (restart_edit == 'V')
10290 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10291#ifdef FEAT_RIGHTLEFT
10292 if (p_hkmap)
10293 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10294# ifdef FEAT_FKMAP
10295 if (p_fkmap)
10296 MSG_PUTS_ATTR(farsi_text_5, attr);
10297# endif
10298#endif
10299#ifdef FEAT_KEYMAP
10300 if (State & LANGMAP)
10301 {
10302# ifdef FEAT_ARABIC
10303 if (curwin->w_p_arab)
10304 MSG_PUTS_ATTR(_(" Arabic"), attr);
10305 else
10306# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010307 if (get_keymap_str(curwin, (char_u *)" (%s)",
10308 NameBuff, MAXPATHL))
10309 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 }
10311#endif
10312 if ((State & INSERT) && p_paste)
10313 MSG_PUTS_ATTR(_(" (paste)"), attr);
10314
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (VIsual_active)
10316 {
10317 char *p;
10318
10319 /* Don't concatenate separate words to avoid translation
10320 * problems. */
10321 switch ((VIsual_select ? 4 : 0)
10322 + (VIsual_mode == Ctrl_V) * 2
10323 + (VIsual_mode == 'V'))
10324 {
10325 case 0: p = N_(" VISUAL"); break;
10326 case 1: p = N_(" VISUAL LINE"); break;
10327 case 2: p = N_(" VISUAL BLOCK"); break;
10328 case 4: p = N_(" SELECT"); break;
10329 case 5: p = N_(" SELECT LINE"); break;
10330 default: p = N_(" SELECT BLOCK"); break;
10331 }
10332 MSG_PUTS_ATTR(_(p), attr);
10333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 MSG_PUTS_ATTR(" --", attr);
10335 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010336
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 need_clear = TRUE;
10338 }
10339 if (Recording
10340#ifdef FEAT_INS_EXPAND
10341 && edit_submode == NULL /* otherwise it gets too long */
10342#endif
10343 )
10344 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010345 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 need_clear = TRUE;
10347 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010348
10349 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 if (need_clear || clear_cmdline)
10351 msg_clr_eos();
10352 msg_didout = FALSE; /* overwrite this message */
10353 length = msg_col;
10354 msg_col = 0;
10355 need_wait_return = nwr_save; /* never ask for hit-return for this */
10356 }
10357 else if (clear_cmdline && msg_silent == 0)
10358 /* Clear the whole command line. Will reset "clear_cmdline". */
10359 msg_clr_cmdline();
10360
10361#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 /* In Visual mode the size of the selected area must be redrawn. */
10363 if (VIsual_active)
10364 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365
10366 /* If the last window has no status line, the ruler is after the mode
10367 * message and must be redrawn */
10368 if (redrawing()
10369# ifdef FEAT_WINDOWS
10370 && lastwin->w_status_height == 0
10371# endif
10372 )
10373 win_redr_ruler(lastwin, TRUE);
10374#endif
10375 redraw_cmdline = FALSE;
10376 clear_cmdline = FALSE;
10377
10378 return length;
10379}
10380
10381/*
10382 * Position for a mode message.
10383 */
10384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010385msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386{
10387 msg_col = 0;
10388 msg_row = Rows - 1;
10389}
10390
10391/*
10392 * Delete mode message. Used when ESC is typed which is expected to end
10393 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010394 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 */
10396 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010397unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010400 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 */
10402 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10403 redraw_cmdline = TRUE; /* delete mode later */
10404 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010405 clearmode();
10406}
10407
10408/*
10409 * Clear the mode message.
10410 */
10411 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010412clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010413{
10414 msg_pos_mode();
10415 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010416 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010417 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418}
10419
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010420 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010421recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010422{
10423 MSG_PUTS_ATTR(_("recording"), attr);
10424 if (!shortmess(SHM_RECORDING))
10425 {
10426 char_u s[4];
10427 sprintf((char *)s, " @%c", Recording);
10428 MSG_PUTS_ATTR(s, attr);
10429 }
10430}
10431
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010432#if defined(FEAT_WINDOWS)
10433/*
10434 * Draw the tab pages line at the top of the Vim window.
10435 */
10436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010437draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010438{
10439 int tabcount = 0;
10440 tabpage_T *tp;
10441 int tabwidth;
10442 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010443 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010444 int attr;
10445 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010446 win_T *cwp;
10447 int wincount;
10448 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010449 int c;
10450 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010451 int attr_sel = HL_ATTR(HLF_TPS);
10452 int attr_nosel = HL_ATTR(HLF_TP);
10453 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010454 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010455 int room;
10456 int use_sep_chars = (t_colors < 8
10457#ifdef FEAT_GUI
10458 && !gui.in_use
10459#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010460#ifdef FEAT_TERMGUICOLORS
10461 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010462#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010463 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010464
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010465 if (ScreenLines == NULL)
10466 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010467 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010469#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010470 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010471 if (gui_use_tabline())
10472 {
10473 gui_update_tabline();
10474 return;
10475 }
10476#endif
10477
10478 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010479 return;
10480
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010481#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010482
10483 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10484 for (scol = 0; scol < Columns; ++scol)
10485 TabPageIdxs[scol] = 0;
10486
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010487 /* Use the 'tabline' option if it's set. */
10488 if (*p_tal != NUL)
10489 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010490 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010491
10492 /* Check for an error. If there is one we would loop in redrawing the
10493 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010494 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010495 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010496 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010498 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010499 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010500 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010501 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010502#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010503 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010504 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010505 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010506
Bram Moolenaar238a5642006-02-21 22:12:05 +000010507 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10508 if (tabwidth < 6)
10509 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010510
Bram Moolenaar238a5642006-02-21 22:12:05 +000010511 attr = attr_nosel;
10512 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010513 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010514 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10515 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010516 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010517 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010518
Bram Moolenaar238a5642006-02-21 22:12:05 +000010519 if (tp->tp_topframe == topframe)
10520 attr = attr_sel;
10521 if (use_sep_chars && col > 0)
10522 screen_putchar('|', 0, col++, attr);
10523
10524 if (tp->tp_topframe != topframe)
10525 attr = attr_nosel;
10526
10527 screen_putchar(' ', 0, col++, attr);
10528
10529 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010530 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010531 cwp = curwin;
10532 wp = firstwin;
10533 }
10534 else
10535 {
10536 cwp = tp->tp_curwin;
10537 wp = tp->tp_firstwin;
10538 }
10539
10540 modified = FALSE;
10541 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10542 if (bufIsChanged(wp->w_buffer))
10543 modified = TRUE;
10544 if (modified || wincount > 1)
10545 {
10546 if (wincount > 1)
10547 {
10548 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010549 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010550 if (col + len >= Columns - 3)
10551 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010552 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010553#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010554 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010555#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010556 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010557#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010558 );
10559 col += len;
10560 }
10561 if (modified)
10562 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10563 screen_putchar(' ', 0, col++, attr);
10564 }
10565
10566 room = scol - col + tabwidth - 1;
10567 if (room > 0)
10568 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010569 /* Get buffer name in NameBuff[] */
10570 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010571 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010572 len = vim_strsize(NameBuff);
10573 p = NameBuff;
10574#ifdef FEAT_MBYTE
10575 if (has_mbyte)
10576 while (len > room)
10577 {
10578 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010579 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010580 }
10581 else
10582#endif
10583 if (len > room)
10584 {
10585 p += len - room;
10586 len = room;
10587 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010588 if (len > Columns - col - 1)
10589 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010591 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010592 col += len;
10593 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010594 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595
10596 /* Store the tab page number in TabPageIdxs[], so that
10597 * jump_to_mouse() knows where each one is. */
10598 ++tabcount;
10599 while (scol < col)
10600 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010601 }
10602
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 if (use_sep_chars)
10604 c = '_';
10605 else
10606 c = ' ';
10607 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010608
10609 /* Put an "X" for closing the current tab if there are several. */
10610 if (first_tabpage->tp_next != NULL)
10611 {
10612 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10613 TabPageIdxs[Columns - 1] = -999;
10614 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010615 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010616
10617 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10618 * set. */
10619 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010620}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010621
10622/*
10623 * Get buffer name for "buf" into NameBuff[].
10624 * Takes care of special buffer names and translates special characters.
10625 */
10626 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010627get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010628{
10629 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010630 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010631 else
10632 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10633 trans_characters(NameBuff, MAXPATHL);
10634}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010635#endif
10636
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10638/*
10639 * Get the character to use in a status line. Get its attributes in "*attr".
10640 */
10641 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010642fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
10644 int fill;
10645 if (is_curwin)
10646 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010647 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 fill = fill_stl;
10649 }
10650 else
10651 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010652 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 fill = fill_stlnc;
10654 }
10655 /* Use fill when there is highlighting, and highlighting of current
10656 * window differs, or the fillchars differ, or this is not the
10657 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010659 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 || (fill_stl != fill_stlnc)))
10661 return fill;
10662 if (is_curwin)
10663 return '^';
10664 return '=';
10665}
10666#endif
10667
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010668#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669/*
10670 * Get the character to use in a separator between vertically split windows.
10671 * Get its attributes in "*attr".
10672 */
10673 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010674fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 if (*attr == 0 && fill_vert == ' ')
10678 return '|';
10679 else
10680 return fill_vert;
10681}
10682#endif
10683
10684/*
10685 * Return TRUE if redrawing should currently be done.
10686 */
10687 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010688redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010690#ifdef FEAT_EVAL
10691 if (disable_redraw_for_testing)
10692 return 0;
10693 else
10694#endif
10695 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10697}
10698
10699/*
10700 * Return TRUE if printing messages should currently be done.
10701 */
10702 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010703messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 return (!(p_lz && char_avail() && !KeyTyped));
10706}
10707
10708/*
10709 * Show current status info in ruler and various other places
10710 * If always is FALSE, only show ruler if position has changed.
10711 */
10712 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010713showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714{
10715 if (!always && !redrawing())
10716 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010717#ifdef FEAT_INS_EXPAND
10718 if (pum_visible())
10719 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010720# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010721 /* Don't redraw right now, do it later. */
10722 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010723# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010724 return;
10725 }
10726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010728 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010729 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010730 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 else
10733#endif
10734#ifdef FEAT_CMDL_INFO
10735 win_redr_ruler(curwin, always);
10736#endif
10737
10738#ifdef FEAT_TITLE
10739 if (need_maketitle
10740# ifdef FEAT_STL_OPT
10741 || (p_icon && (stl_syntax & STL_IN_ICON))
10742 || (p_title && (stl_syntax & STL_IN_TITLE))
10743# endif
10744 )
10745 maketitle();
10746#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010747#ifdef FEAT_WINDOWS
10748 /* Redraw the tab pages line if needed. */
10749 if (redraw_tabline)
10750 draw_tabline();
10751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752}
10753
10754#ifdef FEAT_CMDL_INFO
10755 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010756win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010758#define RULER_BUF_LEN 70
10759 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 int row;
10761 int fillchar;
10762 int attr;
10763 int empty_line = FALSE;
10764 colnr_T virtcol;
10765 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010766 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010768#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 int this_ru_col;
10770 int off = 0;
10771 int width = Columns;
10772# define WITH_OFF(x) x
10773# define WITH_WIDTH(x) x
10774#else
10775# define WITH_OFF(x) 0
10776# define WITH_WIDTH(x) Columns
10777# define this_ru_col ru_col
10778#endif
10779
10780 /* If 'ruler' off or redrawing disabled, don't do anything */
10781 if (!p_ru)
10782 return;
10783
10784 /*
10785 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10786 * after deleting lines, before cursor.lnum is corrected.
10787 */
10788 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10789 return;
10790
10791#ifdef FEAT_INS_EXPAND
10792 /* Don't draw the ruler while doing insert-completion, it might overwrite
10793 * the (long) mode message. */
10794# ifdef FEAT_WINDOWS
10795 if (wp == lastwin && lastwin->w_status_height == 0)
10796# endif
10797 if (edit_submode != NULL)
10798 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010799 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10800 if (pum_visible())
10801 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802#endif
10803
10804#ifdef FEAT_STL_OPT
10805 if (*p_ruf)
10806 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 int save_called_emsg = called_emsg;
10808
10809 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 if (called_emsg)
10812 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010813 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010814 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 return;
10816 }
10817#endif
10818
10819 /*
10820 * Check if not in Insert mode and the line is empty (will show "0-1").
10821 */
10822 if (!(State & INSERT)
10823 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10824 empty_line = TRUE;
10825
10826 /*
10827 * Only draw the ruler when something changed.
10828 */
10829 validate_virtcol_win(wp);
10830 if ( redraw_cmdline
10831 || always
10832 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10833 || wp->w_cursor.col != wp->w_ru_cursor.col
10834 || wp->w_virtcol != wp->w_ru_virtcol
10835#ifdef FEAT_VIRTUALEDIT
10836 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10837#endif
10838 || wp->w_topline != wp->w_ru_topline
10839 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10840#ifdef FEAT_DIFF
10841 || wp->w_topfill != wp->w_ru_topfill
10842#endif
10843 || empty_line != wp->w_ru_empty)
10844 {
10845 cursor_off();
10846#ifdef FEAT_WINDOWS
10847 if (wp->w_status_height)
10848 {
10849 row = W_WINROW(wp) + wp->w_height;
10850 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 off = W_WINCOL(wp);
10852 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 }
10854 else
10855#endif
10856 {
10857 row = Rows - 1;
10858 fillchar = ' ';
10859 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010860#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 width = Columns;
10862 off = 0;
10863#endif
10864 }
10865
10866 /* In list mode virtcol needs to be recomputed */
10867 virtcol = wp->w_virtcol;
10868 if (wp->w_p_list && lcs_tab1 == NUL)
10869 {
10870 wp->w_p_list = FALSE;
10871 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10872 wp->w_p_list = TRUE;
10873 }
10874
10875 /*
10876 * Some sprintfs return the length, some return a pointer.
10877 * To avoid portability problems we use strlen() here.
10878 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010879 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10881 ? 0L
10882 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010883 len = STRLEN(buffer);
10884 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10886 (int)virtcol + 1);
10887
10888 /*
10889 * Add a "50%" if there is room for it.
10890 * On the last line, don't print in the last column (scrolls the
10891 * screen up on some terminals).
10892 */
10893 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010894 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 o = i + vim_strsize(buffer + i + 1);
10896#ifdef FEAT_WINDOWS
10897 if (wp->w_status_height == 0) /* can't use last char of screen */
10898#endif
10899 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010900#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 this_ru_col = ru_col - (Columns - width);
10902 if (this_ru_col < 0)
10903 this_ru_col = 0;
10904#endif
10905 /* Never use more than half the window/screen width, leave the other
10906 * half for the filename. */
10907 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10908 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10909 if (this_ru_col + o < WITH_WIDTH(width))
10910 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010911 /* need at least 3 chars left for get_rel_pos() + NUL */
10912 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 {
10914#ifdef FEAT_MBYTE
10915 if (has_mbyte)
10916 i += (*mb_char2bytes)(fillchar, buffer + i);
10917 else
10918#endif
10919 buffer[i++] = fillchar;
10920 ++o;
10921 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010922 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 }
10924 /* Truncate at window boundary. */
10925#ifdef FEAT_MBYTE
10926 if (has_mbyte)
10927 {
10928 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010929 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 {
10931 o += (*mb_ptr2cells)(buffer + i);
10932 if (this_ru_col + o > WITH_WIDTH(width))
10933 {
10934 buffer[i] = NUL;
10935 break;
10936 }
10937 }
10938 }
10939 else
10940#endif
10941 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10942 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10943
10944 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10945 i = redraw_cmdline;
10946 screen_fill(row, row + 1,
10947 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10948 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10949 fillchar, fillchar, attr);
10950 /* don't redraw the cmdline because of showing the ruler */
10951 redraw_cmdline = i;
10952 wp->w_ru_cursor = wp->w_cursor;
10953 wp->w_ru_virtcol = wp->w_virtcol;
10954 wp->w_ru_empty = empty_line;
10955 wp->w_ru_topline = wp->w_topline;
10956 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10957#ifdef FEAT_DIFF
10958 wp->w_ru_topfill = wp->w_topfill;
10959#endif
10960 }
10961}
10962#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010963
10964#if defined(FEAT_LINEBREAK) || defined(PROTO)
10965/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010966 * Return the width of the 'number' and 'relativenumber' column.
10967 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010968 * Otherwise it depends on 'numberwidth' and the line count.
10969 */
10970 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010971number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010972{
10973 int n;
10974 linenr_T lnum;
10975
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010976 if (wp->w_p_rnu && !wp->w_p_nu)
10977 /* cursor line shows "0" */
10978 lnum = wp->w_height;
10979 else
10980 /* cursor line shows absolute line number */
10981 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010982
Bram Moolenaar6b314672015-03-20 15:42:10 +010010983 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010984 return wp->w_nrwidth_width;
10985 wp->w_nrwidth_line_count = lnum;
10986
10987 n = 0;
10988 do
10989 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010990 lnum /= 10;
10991 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010992 } while (lnum > 0);
10993
10994 /* 'numberwidth' gives the minimal width plus one */
10995 if (n < wp->w_p_nuw - 1)
10996 n = wp->w_p_nuw - 1;
10997
10998 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010999 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011000 return n;
11001}
11002#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011003
11004/*
11005 * Return the current cursor column. This is the actual position on the
11006 * screen. First column is 0.
11007 */
11008 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011009screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011010{
11011 return screen_cur_col;
11012}
11013
11014/*
11015 * Return the current cursor row. This is the actual position on the screen.
11016 * First row is 0.
11017 */
11018 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011019screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011020{
11021 return screen_cur_row;
11022}