blob: 944eecd02fb661253a2c18a5d9f867d072e04220 [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 Moolenaar44a2f922016-03-19 22:11:51 +0100129#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000132#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200136# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void start_search_hl(void);
138static void end_search_hl(void);
139static void init_search_hl(win_T *wp);
140static void prepare_search_hl(win_T *wp, linenr_T lnum);
141static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
142static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void screen_start_highlight(int attr);
145static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100147static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screenclear2(void);
150static void lineclear(unsigned off, int width);
151static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100152#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void linecopy(int to, int from, win_T *wp);
154static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
157static void win_rest_invalid(win_T *wp);
158static void msg_pos_mode(void);
159static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000160#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100166#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100167static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100176#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/* Ugly global: overrule attribute used by screen_char() */
178static int screen_char_attr = 0;
179#endif
180
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200181#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
182/* Can limit syntax highlight time to 'redrawtime'. */
183# define SYN_TIME_LIMIT 1
184#endif
185
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186/*
187 * Redraw the current window later, with update_screen(type).
188 * Set must_redraw only if not already set to a higher value.
189 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
190 */
191 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100192redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193{
194 redraw_win_later(curwin, type);
195}
196
197 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100198redraw_win_later(
199 win_T *wp,
200 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 if (wp->w_redr_type < type)
203 {
204 wp->w_redr_type = type;
205 if (type >= NOT_VALID)
206 wp->w_lines_valid = 0;
207 if (must_redraw < type) /* must_redraw is the maximum of all windows */
208 must_redraw = type;
209 }
210}
211
212/*
213 * Force a complete redraw later. Also resets the highlighting. To be used
214 * after executing a shell command that messes up the screen.
215 */
216 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100217redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000220#ifdef FEAT_GUI
221 if (gui.in_use)
222 /* Use a code that will reset gui.highlight_mask in
223 * gui_stop_highlight(). */
224 screen_attr = HL_ALL + 1;
225 else
226#endif
227 /* Use attributes that is very unlikely to appear in text. */
228 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229}
230
231/*
232 * Mark all windows to be redrawn later.
233 */
234 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100235redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 win_T *wp;
238
239 FOR_ALL_WINDOWS(wp)
240 {
241 redraw_win_later(wp, type);
242 }
243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200266 void
267redraw_buf_and_status_later(buf_T *buf, int type)
268{
269 win_T *wp;
270
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200271#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200272 if (wild_menu_showing != 0)
273 /* Don't redraw while the command line completion is displayed, it
274 * would disappear. */
275 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200276#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 {
281 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200282#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200283 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200284#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200285 }
286 }
287}
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200290 * Redraw as soon as possible. When the command line is not scrolled redraw
291 * right away and restore what was on the command line.
292 * Return a code indicating what happened.
293 */
294 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100295redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200296{
297 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 int r;
300 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200301 schar_T *screenline; /* copy from ScreenLines[] */
302 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303#ifdef FEAT_MBYTE
304 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308#endif
309
310 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 return ret;
313
314 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline == NULL || screenattr == NULL)
321 ret = 2;
322#ifdef FEAT_MBYTE
323 if (enc_utf8)
324 {
325 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 if (screenlineUC == NULL)
328 ret = 2;
329 for (i = 0; i < p_mco; ++i)
330 {
331 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenlineC[i] == NULL)
334 ret = 2;
335 }
336 }
337 if (enc_dbcs == DBCS_JPNU)
338 {
339 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200340 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200341 if (screenline2 == NULL)
342 ret = 2;
343 }
344#endif
345
346 if (ret != 2)
347 {
348 /* Save the text displayed in the command line area. */
349 for (r = 0; r < rows; ++r)
350 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 (size_t)cols * sizeof(schar_T));
354 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357#ifdef FEAT_MBYTE
358 if (enc_utf8)
359 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 mch_memmove(screenlineC[i] + r * cols,
365 ScreenLinesC[i] + LineOffset[cmdline_row + r],
366 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 }
368 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372#endif
373 }
374
375 update_screen(0);
376 ret = 3;
377
378 if (must_redraw == 0)
379 {
380 int off = (int)(current_ScreenLine - ScreenLines);
381
382 /* Restore the text displayed in the command line area. */
383 for (r = 0; r < rows; ++r)
384 {
385 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 screenline + r * cols,
387 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200388 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenattr + r * cols,
390 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391#ifdef FEAT_MBYTE
392 if (enc_utf8)
393 {
394 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenlineUC + r * cols,
396 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 for (i = 0; i < p_mco; ++i)
398 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenlineC[i] + r * cols,
400 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401 }
402 if (enc_dbcs == DBCS_JPNU)
403 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenline2 + r * cols,
405 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200407 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408 }
409 ret = 4;
410 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412
413 vim_free(screenline);
414 vim_free(screenattr);
415#ifdef FEAT_MBYTE
416 if (enc_utf8)
417 {
418 vim_free(screenlineUC);
419 for (i = 0; i < p_mco; ++i)
420 vim_free(screenlineC[i]);
421 }
422 if (enc_dbcs == DBCS_JPNU)
423 vim_free(screenline2);
424#endif
425
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200426 /* Show the intro message when appropriate. */
427 maybe_intro_message();
428
429 setcursor();
430
Bram Moolenaar2951b772013-07-03 12:45:31 +0200431 return ret;
432}
433
434/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100435 * Invoked after an asynchronous callback is called.
436 * If an echo command was used the cursor needs to be put back where
437 * it belongs. If highlighting was changed a redraw is needed.
438 */
439 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200440redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100441{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100442 if (State == HITRETURN || State == ASKMORE)
443 ; /* do nothing */
444 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200445 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200446 /* Redrawing only works when the screen didn't scroll. Don't clear
447 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200448 if (msg_scrolled == 0
449#ifdef FEAT_WILDMENU
450 && wild_menu_showing == 0
451#endif
452 )
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200453 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200454 /* Redraw in the same position, so that the user can continue
455 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200456 redrawcmdline_ex(FALSE);
457 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200458 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100459 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 /* keep the command line if possible */
461 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100462 setcursor();
463 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464 cursor_on();
465 out_flush();
466#ifdef FEAT_GUI
467 if (gui.in_use)
468 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200469 /* Don't update the cursor when it is blinking and off to avoid
470 * flicker. */
471 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200472 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100473 gui_mch_flush();
474 }
475#endif
476}
477
478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 * Changed something in the current window, at buffer line "lnum", that
480 * requires that line and possibly other lines to be redrawn.
481 * Used when entering/leaving Insert mode with the cursor on a folded line.
482 * Used to remove the "$" from a change command.
483 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
484 * may become invalid and the whole window will have to be redrawn.
485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100487redrawWinline(
488 linenr_T lnum,
489 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
491#ifdef FEAT_FOLDING
492 int i;
493#endif
494
495 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
496 curwin->w_redraw_top = lnum;
497 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
498 curwin->w_redraw_bot = lnum;
499 redraw_later(VALID);
500
501#ifdef FEAT_FOLDING
502 if (invalid)
503 {
504 /* A w_lines[] entry for this lnum has become invalid. */
505 i = find_wl_entry(curwin, lnum);
506 if (i >= 0)
507 curwin->w_lines[i].wl_valid = FALSE;
508 }
509#endif
510}
511
512/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200513 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 */
515 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100516update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 redraw_curbuf_later(type);
519 update_screen(type);
520}
521
522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 * Based on the current value of curwin->w_topline, transfer a screenfull
524 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
525 */
526 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200527update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 win_T *wp;
531 static int did_intro = FALSE;
532#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
533 int did_one;
534#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200535#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200536 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200537 int gui_cursor_col;
538 int gui_cursor_row;
539#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200540 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000542 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 if (!screen_valid(TRUE))
544 return;
545
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 if (type == VALID_NO_UPDATE)
547 {
548 no_update = TRUE;
549 type = 0;
550 }
551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (must_redraw)
553 {
554 if (type < must_redraw) /* use maximal type */
555 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000556
557 /* must_redraw is reset here, so that when we run into some weird
558 * reason to redraw while busy redrawing (e.g., asynchronous
559 * scrolling), or update_topline() in win_update() will cause a
560 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 must_redraw = 0;
562 }
563
564 /* Need to update w_lines[]. */
565 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
566 type = NOT_VALID;
567
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000568 /* Postpone the redrawing when it's not needed and when being called
569 * recursively. */
570 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
572 redraw_later(type); /* remember type for next time */
573 must_redraw = type;
574 if (type > INVERTED_ALL)
575 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
576 return;
577 }
578
579 updating_screen = TRUE;
580#ifdef FEAT_SYN_HL
581 ++display_tick; /* let syntax code know we're in a next round of
582 * display updating */
583#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200584 if (no_update)
585 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 /*
588 * if the screen was scrolled up when displaying a message, scroll it down
589 */
590 if (msg_scrolled)
591 {
592 clear_cmdline = TRUE;
593 if (msg_scrolled > Rows - 5) /* clearing is faster */
594 type = CLEAR;
595 else if (type != CLEAR)
596 {
597 check_for_delay(FALSE);
598 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
599 type = CLEAR;
600 FOR_ALL_WINDOWS(wp)
601 {
602 if (W_WINROW(wp) < msg_scrolled)
603 {
604 if (W_WINROW(wp) + wp->w_height > msg_scrolled
605 && wp->w_redr_type < REDRAW_TOP
606 && wp->w_lines_valid > 0
607 && wp->w_topline == wp->w_lines[0].wl_lnum)
608 {
609 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
610 wp->w_redr_type = REDRAW_TOP;
611 }
612 else
613 {
614 wp->w_redr_type = NOT_VALID;
615#ifdef FEAT_WINDOWS
616 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
617 <= msg_scrolled)
618 wp->w_redr_status = TRUE;
619#endif
620 }
621 }
622 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200623 if (!no_update)
624 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000625#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000626 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 }
629 msg_scrolled = 0;
630 need_wait_return = FALSE;
631 }
632
633 /* reset cmdline_row now (may have been changed temporarily) */
634 compute_cmdrow();
635
636 /* Check for changed highlighting */
637 if (need_highlight_changed)
638 highlight_changed();
639
640 if (type == CLEAR) /* first clear screen */
641 {
642 screenclear(); /* will reset clear_cmdline */
643 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200644 /* must_redraw may be set indirectly, avoid another redraw later */
645 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647
648 if (clear_cmdline) /* going to clear cmdline (done below) */
649 check_for_delay(FALSE);
650
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000651#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200652 /* Force redraw when width of 'number' or 'relativenumber' column
653 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
656 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 curwin->w_redr_type = NOT_VALID;
658#endif
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /*
661 * Only start redrawing if there is really something to do.
662 */
663 if (type == INVERTED)
664 update_curswant();
665 if (curwin->w_redr_type < type
666 && !((type == VALID
667 && curwin->w_lines[0].wl_valid
668#ifdef FEAT_DIFF
669 && curwin->w_topfill == curwin->w_old_topfill
670 && curwin->w_botfill == curwin->w_old_botfill
671#endif
672 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000674 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
676 && curwin->w_old_visual_mode == VIsual_mode
677 && (curwin->w_valid & VALID_VIRTCOL)
678 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 ))
680 curwin->w_redr_type = type;
681
Bram Moolenaar5a305422006-04-28 22:38:25 +0000682#ifdef FEAT_WINDOWS
683 /* Redraw the tab pages line if needed. */
684 if (redraw_tabline || type >= NOT_VALID)
685 draw_tabline();
686#endif
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_SYN_HL
689 /*
690 * Correct stored syntax highlighting info for changes in each displayed
691 * buffer. Each buffer must only be done once.
692 */
693 FOR_ALL_WINDOWS(wp)
694 {
695 if (wp->w_buffer->b_mod_set)
696 {
697# ifdef FEAT_WINDOWS
698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
704# endif
705 if (
706# ifdef FEAT_WINDOWS
707 wwp == wp &&
708# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200709 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 syn_stack_apply_changes(wp->w_buffer);
711 }
712 }
713#endif
714
715 /*
716 * Go from top to bottom through the windows, redrawing the ones that need
717 * it.
718 */
719#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
720 did_one = FALSE;
721#endif
722#ifdef FEAT_SEARCH_EXTRA
723 search_hl.rm.regprog = NULL;
724#endif
725 FOR_ALL_WINDOWS(wp)
726 {
727 if (wp->w_redr_type != 0)
728 {
729 cursor_off();
730#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
731 if (!did_one)
732 {
733 did_one = TRUE;
734# ifdef FEAT_SEARCH_EXTRA
735 start_search_hl();
736# endif
737# ifdef FEAT_CLIPBOARD
738 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200739 if (clip_star.available && clip_isautosel_star())
740 clip_update_selection(&clip_star);
741 if (clip_plus.available && clip_isautosel_plus())
742 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743# endif
744#ifdef FEAT_GUI
745 /* Remove the cursor before starting to do anything, because
746 * scrolling may make it difficult to redraw the text under
747 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 {
750 gui_cursor_col = gui.cursor_col;
751 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200753 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#endif
756 }
757#endif
758 win_update(wp);
759 }
760
761#ifdef FEAT_WINDOWS
762 /* redraw status line after the window to minimize cursor movement */
763 if (wp->w_redr_status)
764 {
765 cursor_off();
766 win_redr_status(wp);
767 }
768#endif
769 }
770#if defined(FEAT_SEARCH_EXTRA)
771 end_search_hl();
772#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100773#ifdef FEAT_INS_EXPAND
774 /* May need to redraw the popup menu. */
775 if (pum_visible())
776 pum_redraw();
777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779#ifdef FEAT_WINDOWS
780 /* Reset b_mod_set flags. Going through all windows is probably faster
781 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200782 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 wp->w_buffer->b_mod_set = FALSE;
784#else
785 curbuf->b_mod_set = FALSE;
786#endif
787
788 updating_screen = FALSE;
789#ifdef FEAT_GUI
790 gui_may_resize_shell();
791#endif
792
793 /* Clear or redraw the command line. Done last, because scrolling may
794 * mess up the command line. */
795 if (clear_cmdline || redraw_cmdline)
796 showmode();
797
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200798 if (no_update)
799 --no_win_do_lines_ins;
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200802 if (!did_intro)
803 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 did_intro = TRUE;
805
806#ifdef FEAT_GUI
807 /* Redraw the cursor and update the scrollbars when all screen updating is
808 * done. */
809 if (gui.in_use)
810 {
811 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200812 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200813 {
814 /* Put the GUI position where the cursor was, gui_update_cursor()
815 * uses that. */
816 gui.col = gui_cursor_col;
817 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200818# ifdef FEAT_MBYTE
819 gui.col = mb_fix_col(gui.col, gui.row);
820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200822 screen_cur_col = gui.col;
823 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 gui_update_scrollbars(FALSE);
826 }
827#endif
828}
829
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100830#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
831/*
832 * Prepare for updating one or more windows.
833 * Caller must check for "updating_screen" already set to avoid recursiveness.
834 */
835 static void
836update_prepare(void)
837{
838 cursor_off();
839 updating_screen = TRUE;
840#ifdef FEAT_GUI
841 /* Remove the cursor before starting to do anything, because scrolling may
842 * make it difficult to redraw the text under it. */
843 if (gui.in_use)
844 gui_undraw_cursor();
845#endif
846#ifdef FEAT_SEARCH_EXTRA
847 start_search_hl();
848#endif
849}
850
851/*
852 * Finish updating one or more windows.
853 */
854 static void
855update_finish(void)
856{
857 if (redraw_cmdline)
858 showmode();
859
860# ifdef FEAT_SEARCH_EXTRA
861 end_search_hl();
862# endif
863
864 updating_screen = FALSE;
865
866# ifdef FEAT_GUI
867 gui_may_resize_shell();
868
869 /* Redraw the cursor and update the scrollbars when all screen updating is
870 * done. */
871 if (gui.in_use)
872 {
873 out_flush(); /* required before updating the cursor */
874 gui_update_cursor(FALSE, FALSE);
875 gui_update_scrollbars(FALSE);
876 }
877# endif
878}
879#endif
880
Bram Moolenaar860cae12010-06-05 23:22:07 +0200881#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200882/*
883 * Return TRUE if the cursor line in window "wp" may be concealed, according
884 * to the 'concealcursor' option.
885 */
886 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100887conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200888{
889 int c;
890
891 if (*wp->w_p_cocu == NUL)
892 return FALSE;
893 if (get_real_state() & VISUAL)
894 c = 'v';
895 else if (State & INSERT)
896 c = 'i';
897 else if (State & NORMAL)
898 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200899 else if (State & CMDLINE)
900 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901 else
902 return FALSE;
903 return vim_strchr(wp->w_p_cocu, c) != NULL;
904}
905
906/*
907 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
908 */
909 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100910conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911{
912 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
913 {
914 need_cursor_line_redraw = TRUE;
915 /* Need to recompute cursor column, e.g., when starting Visual mode
916 * without concealing. */
917 curs_columns(TRUE);
918 }
919}
920
Bram Moolenaar860cae12010-06-05 23:22:07 +0200921 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100922update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200923{
924 int row;
925 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200926#ifdef SYN_TIME_LIMIT
927 proftime_T syntax_tm;
928#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929
Bram Moolenaar908be432016-05-24 10:51:30 +0200930 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100931 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200932 return;
933
Bram Moolenaar860cae12010-06-05 23:22:07 +0200934 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200935 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200937#ifdef SYN_TIME_LIMIT
938 /* Set the time limit to 'redrawtime'. */
939 profile_setlimit(p_rdt, &syntax_tm);
940#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100941 update_prepare();
942
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 row = 0;
944 for (j = 0; j < wp->w_lines_valid; ++j)
945 {
946 if (lnum == wp->w_lines[j].wl_lnum)
947 {
948 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200949# ifdef FEAT_SEARCH_EXTRA
950 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 start_search_hl();
952 prepare_search_hl(wp, lnum);
953# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200954 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
955#ifdef SYN_TIME_LIMIT
956 &syntax_tm
957#else
958 NULL
959#endif
960 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200961# if defined(FEAT_SEARCH_EXTRA)
962 end_search_hl();
963# endif
964 break;
965 }
966 row += wp->w_lines[j].wl_size;
967 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100968
969 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200970 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200971 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972}
973#endif
974
975#if defined(FEAT_SIGNS) || defined(PROTO)
976 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100977update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 win_T *wp;
980 int doit = FALSE;
981
982# ifdef FEAT_FOLDING
983 win_foldinfo.fi_level = 0;
984# endif
985
986 /* update/delete a specific mark */
987 FOR_ALL_WINDOWS(wp)
988 {
989 if (buf != NULL && lnum > 0)
990 {
991 if (wp->w_buffer == buf && lnum >= wp->w_topline
992 && lnum < wp->w_botline)
993 {
994 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
995 wp->w_redraw_top = lnum;
996 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
997 wp->w_redraw_bot = lnum;
998 redraw_win_later(wp, VALID);
999 }
1000 }
1001 else
1002 redraw_win_later(wp, VALID);
1003 if (wp->w_redr_type != 0)
1004 doit = TRUE;
1005 }
1006
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001007 /* Return when there is nothing to do, screen updating is already
1008 * happening (recursive call) or still starting up. */
1009 if (!doit || updating_screen
1010#ifdef FEAT_GUI
1011 || gui.starting
1012#endif
1013 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 return;
1015
1016 /* update all windows that need updating */
1017 update_prepare();
1018
1019# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001020 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 if (wp->w_redr_type != 0)
1023 win_update(wp);
1024 if (wp->w_redr_status)
1025 win_redr_status(wp);
1026 }
1027# else
1028 if (curwin->w_redr_type != 0)
1029 win_update(curwin);
1030# endif
1031
1032 update_finish();
1033}
1034#endif
1035
1036
1037#if defined(FEAT_GUI) || defined(PROTO)
1038/*
1039 * Update a single window, its status line and maybe the command line msg.
1040 * Used for the GUI scrollbar.
1041 */
1042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001043updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001045 /* return if already busy updating */
1046 if (updating_screen)
1047 return;
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 update_prepare();
1050
1051#ifdef FEAT_CLIPBOARD
1052 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001053 if (clip_star.available && clip_isautosel_star())
1054 clip_update_selection(&clip_star);
1055 if (clip_plus.available && clip_isautosel_plus())
1056 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001062 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001063 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001064 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (wp->w_redr_status
1067# ifdef FEAT_CMDL_INFO
1068 || p_ru
1069# endif
1070# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001071 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072# endif
1073 )
1074 win_redr_status(wp);
1075#endif
1076
1077 update_finish();
1078}
1079#endif
1080
1081/*
1082 * Update a single window.
1083 *
1084 * This may cause the windows below it also to be redrawn (when clearing the
1085 * screen or scrolling lines).
1086 *
1087 * How the window is redrawn depends on wp->w_redr_type. Each type also
1088 * implies the one below it.
1089 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001090 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1092 * INVERTED redraw the changed part of the Visual area
1093 * INVERTED_ALL redraw the whole Visual area
1094 * VALID 1. scroll up/down to adjust for a changed w_topline
1095 * 2. update lines at the top when scrolled down
1096 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001097 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 * b_mod_top and b_mod_bot.
1099 * - if wp->w_redraw_top non-zero, redraw lines between
1100 * wp->w_redraw_top and wp->w_redr_bot.
1101 * - continue redrawing when syntax status is invalid.
1102 * 4. if scrolled up, update lines at the bottom.
1103 * This results in three areas that may need updating:
1104 * top: from first row to top_end (when scrolled down)
1105 * mid: from mid_start to mid_end (update inversion or changed text)
1106 * bot: from bot_start to last row (when scrolled up)
1107 */
1108 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001109win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 buf_T *buf = wp->w_buffer;
1112 int type;
1113 int top_end = 0; /* Below last row of the top area that needs
1114 updating. 0 when no top area updating. */
1115 int mid_start = 999;/* first row of the mid area that needs
1116 updating. 999 when no mid area updating. */
1117 int mid_end = 0; /* Below last row of the mid area that needs
1118 updating. 0 when no mid area updating. */
1119 int bot_start = 999;/* first row of the bot area that needs
1120 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 int scrolled_down = FALSE; /* TRUE when scrolled down when
1122 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001124 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 int top_to_mod = FALSE; /* redraw above mod_top */
1126#endif
1127
1128 int row; /* current window row to display */
1129 linenr_T lnum; /* current buffer lnum to display */
1130 int idx; /* current index in w_lines[] */
1131 int srow; /* starting row of the current line */
1132
1133 int eof = FALSE; /* if TRUE, we hit the end of the file */
1134 int didline = FALSE; /* if TRUE, we finished the last line */
1135 int i;
1136 long j;
1137 static int recursive = FALSE; /* being called recursively */
1138 int old_botline = wp->w_botline;
1139#ifdef FEAT_FOLDING
1140 long fold_count;
1141#endif
1142#ifdef FEAT_SYN_HL
1143 /* remember what happened to the previous line, to know if
1144 * check_visual_highlight() can be used */
1145#define DID_NONE 1 /* didn't update a line */
1146#define DID_LINE 2 /* updated a normal line */
1147#define DID_FOLD 3 /* updated a folded line */
1148 int did_update = DID_NONE;
1149 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1150#endif
1151 linenr_T mod_top = 0;
1152 linenr_T mod_bot = 0;
1153#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1154 int save_got_int;
1155#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001156#ifdef SYN_TIME_LIMIT
1157 proftime_T syntax_tm;
1158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
1160 type = wp->w_redr_type;
1161
1162 if (type == NOT_VALID)
1163 {
1164#ifdef FEAT_WINDOWS
1165 wp->w_redr_status = TRUE;
1166#endif
1167 wp->w_lines_valid = 0;
1168 }
1169
1170 /* Window is zero-height: nothing to draw. */
1171 if (wp->w_height == 0)
1172 {
1173 wp->w_redr_type = 0;
1174 return;
1175 }
1176
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001177#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 /* Window is zero-width: Only need to draw the separator. */
1179 if (wp->w_width == 0)
1180 {
1181 /* draw the vertical separator right of this window */
1182 draw_vsep_win(wp, 0);
1183 wp->w_redr_type = 0;
1184 return;
1185 }
1186#endif
1187
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001188#ifdef FEAT_TERMINAL
1189 if (wp->w_buffer->b_term != NULL)
1190 {
1191 /* This window contains a terminal, redraw works completely
1192 * differently. */
1193 term_update_window(wp);
1194 wp->w_redr_type = 0;
1195 return;
1196 }
1197#endif
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001200 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#endif
1202
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001203#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001204 /* Force redraw when width of 'number' or 'relativenumber' column
1205 * changes. */
1206 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001207 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001208 {
1209 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001210 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001211 }
1212 else
1213#endif
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1216 {
1217 /*
1218 * When there are both inserted/deleted lines and specific lines to be
1219 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1220 * everything (only happens when redrawing is off for while).
1221 */
1222 type = NOT_VALID;
1223 }
1224 else
1225 {
1226 /*
1227 * Set mod_top to the first line that needs displaying because of
1228 * changes. Set mod_bot to the first line after the changes.
1229 */
1230 mod_top = wp->w_redraw_top;
1231 if (wp->w_redraw_bot != 0)
1232 mod_bot = wp->w_redraw_bot + 1;
1233 else
1234 mod_bot = 0;
1235 wp->w_redraw_top = 0; /* reset for next time */
1236 wp->w_redraw_bot = 0;
1237 if (buf->b_mod_set)
1238 {
1239 if (mod_top == 0 || mod_top > buf->b_mod_top)
1240 {
1241 mod_top = buf->b_mod_top;
1242#ifdef FEAT_SYN_HL
1243 /* Need to redraw lines above the change that may be included
1244 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001245 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001247 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (mod_top < 1)
1249 mod_top = 1;
1250 }
1251#endif
1252 }
1253 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1254 mod_bot = buf->b_mod_bot;
1255
1256#ifdef FEAT_SEARCH_EXTRA
1257 /* When 'hlsearch' is on and using a multi-line search pattern, a
1258 * change in one line may make the Search highlighting in a
1259 * previous line invalid. Simple solution: redraw all visible
1260 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001261 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001263 if (search_hl.rm.regprog != NULL
1264 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001266 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 {
1268 cur = wp->w_match_head;
1269 while (cur != NULL)
1270 {
1271 if (cur->match.regprog != NULL
1272 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001273 {
1274 top_to_mod = TRUE;
1275 break;
1276 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001277 cur = cur->next;
1278 }
1279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#endif
1281 }
1282#ifdef FEAT_FOLDING
1283 if (mod_top != 0 && hasAnyFolding(wp))
1284 {
1285 linenr_T lnumt, lnumb;
1286
1287 /*
1288 * A change in a line can cause lines above it to become folded or
1289 * unfolded. Find the top most buffer line that may be affected.
1290 * If the line was previously folded and displayed, get the first
1291 * line of that fold. If the line is folded now, get the first
1292 * folded line. Use the minimum of these two.
1293 */
1294
1295 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1296 * the line below it. If there is no valid entry, use w_topline.
1297 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1298 * to this line. If there is no valid entry, use MAXLNUM. */
1299 lnumt = wp->w_topline;
1300 lnumb = MAXLNUM;
1301 for (i = 0; i < wp->w_lines_valid; ++i)
1302 if (wp->w_lines[i].wl_valid)
1303 {
1304 if (wp->w_lines[i].wl_lastlnum < mod_top)
1305 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1306 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1307 {
1308 lnumb = wp->w_lines[i].wl_lnum;
1309 /* When there is a fold column it might need updating
1310 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001311 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 ++lnumb;
1313 }
1314 }
1315
1316 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1317 if (mod_top > lnumt)
1318 mod_top = lnumt;
1319
1320 /* Now do the same for the bottom line (one above mod_bot). */
1321 --mod_bot;
1322 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1323 ++mod_bot;
1324 if (mod_bot < lnumb)
1325 mod_bot = lnumb;
1326 }
1327#endif
1328
1329 /* When a change starts above w_topline and the end is below
1330 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001331 * If the end of the change is above w_topline: do like no change was
1332 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (mod_top != 0 && mod_top < wp->w_topline)
1334 {
1335 if (mod_bot > wp->w_topline)
1336 mod_top = wp->w_topline;
1337#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001338 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 top_end = 1;
1340#endif
1341 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001342
1343 /* When line numbers are displayed need to redraw all lines below
1344 * inserted/deleted lines. */
1345 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1346 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348
1349 /*
1350 * When only displaying the lines at the top, set top_end. Used when
1351 * window has scrolled down for msg_scrolled.
1352 */
1353 if (type == REDRAW_TOP)
1354 {
1355 j = 0;
1356 for (i = 0; i < wp->w_lines_valid; ++i)
1357 {
1358 j += wp->w_lines[i].wl_size;
1359 if (j >= wp->w_upd_rows)
1360 {
1361 top_end = j;
1362 break;
1363 }
1364 }
1365 if (top_end == 0)
1366 /* not found (cannot happen?): redraw everything */
1367 type = NOT_VALID;
1368 else
1369 /* top area defined, the rest is VALID */
1370 type = VALID;
1371 }
1372
Bram Moolenaar367329b2007-08-30 11:53:22 +00001373 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001374 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1375 * non-zero and thus not FALSE) will indicate that screenclear() was not
1376 * called. */
1377 if (screen_cleared)
1378 screen_cleared = MAYBE;
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 /*
1381 * If there are no changes on the screen that require a complete redraw,
1382 * handle three cases:
1383 * 1: we are off the top of the screen by a few lines: scroll down
1384 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1385 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1386 * w_lines[] that needs updating.
1387 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001388 if ((type == VALID || type == SOME_VALID
1389 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef FEAT_DIFF
1391 && !wp->w_botfill && !wp->w_old_botfill
1392#endif
1393 )
1394 {
1395 if (mod_top != 0 && wp->w_topline == mod_top)
1396 {
1397 /*
1398 * w_topline is the first changed line, the scrolling will be done
1399 * further down.
1400 */
1401 }
1402 else if (wp->w_lines[0].wl_valid
1403 && (wp->w_topline < wp->w_lines[0].wl_lnum
1404#ifdef FEAT_DIFF
1405 || (wp->w_topline == wp->w_lines[0].wl_lnum
1406 && wp->w_topfill > wp->w_old_topfill)
1407#endif
1408 ))
1409 {
1410 /*
1411 * New topline is above old topline: May scroll down.
1412 */
1413#ifdef FEAT_FOLDING
1414 if (hasAnyFolding(wp))
1415 {
1416 linenr_T ln;
1417
1418 /* count the number of lines we are off, counting a sequence
1419 * of folded lines as one */
1420 j = 0;
1421 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1422 {
1423 ++j;
1424 if (j >= wp->w_height - 2)
1425 break;
1426 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1427 }
1428 }
1429 else
1430#endif
1431 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1432 if (j < wp->w_height - 2) /* not too far off */
1433 {
1434 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1435#ifdef FEAT_DIFF
1436 /* insert extra lines for previously invisible filler lines */
1437 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1438 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1439 - wp->w_old_topfill;
1440#endif
1441 if (i < wp->w_height - 2) /* less than a screen off */
1442 {
1443 /*
1444 * Try to insert the correct number of lines.
1445 * If not the last window, delete the lines at the bottom.
1446 * win_ins_lines may fail when the terminal can't do it.
1447 */
1448 if (i > 0)
1449 check_for_delay(FALSE);
1450 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1451 {
1452 if (wp->w_lines_valid != 0)
1453 {
1454 /* Need to update rows that are new, stop at the
1455 * first one that scrolled down. */
1456 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
1459 /* Move the entries that were scrolled, disable
1460 * the entries for the lines to be redrawn. */
1461 if ((wp->w_lines_valid += j) > wp->w_height)
1462 wp->w_lines_valid = wp->w_height;
1463 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1464 wp->w_lines[idx] = wp->w_lines[idx - j];
1465 while (idx >= 0)
1466 wp->w_lines[idx--].wl_valid = FALSE;
1467 }
1468 }
1469 else
1470 mid_start = 0; /* redraw all lines */
1471 }
1472 else
1473 mid_start = 0; /* redraw all lines */
1474 }
1475 else
1476 mid_start = 0; /* redraw all lines */
1477 }
1478 else
1479 {
1480 /*
1481 * New topline is at or below old topline: May scroll up.
1482 * When topline didn't change, find first entry in w_lines[] that
1483 * needs updating.
1484 */
1485
1486 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1487 j = -1;
1488 row = 0;
1489 for (i = 0; i < wp->w_lines_valid; i++)
1490 {
1491 if (wp->w_lines[i].wl_valid
1492 && wp->w_lines[i].wl_lnum == wp->w_topline)
1493 {
1494 j = i;
1495 break;
1496 }
1497 row += wp->w_lines[i].wl_size;
1498 }
1499 if (j == -1)
1500 {
1501 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1502 * lines */
1503 mid_start = 0;
1504 }
1505 else
1506 {
1507 /*
1508 * Try to delete the correct number of lines.
1509 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1510 */
1511#ifdef FEAT_DIFF
1512 /* If the topline didn't change, delete old filler lines,
1513 * otherwise delete filler lines of the new topline... */
1514 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1515 row += wp->w_old_topfill;
1516 else
1517 row += diff_check_fill(wp, wp->w_topline);
1518 /* ... but don't delete new filler lines. */
1519 row -= wp->w_topfill;
1520#endif
1521 if (row > 0)
1522 {
1523 check_for_delay(FALSE);
1524 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1525 bot_start = wp->w_height - row;
1526 else
1527 mid_start = 0; /* redraw all lines */
1528 }
1529 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1530 {
1531 /*
1532 * Skip the lines (below the deleted lines) that are still
1533 * valid and don't need redrawing. Copy their info
1534 * upwards, to compensate for the deleted lines. Set
1535 * bot_start to the first row that needs redrawing.
1536 */
1537 bot_start = 0;
1538 idx = 0;
1539 for (;;)
1540 {
1541 wp->w_lines[idx] = wp->w_lines[j];
1542 /* stop at line that didn't fit, unless it is still
1543 * valid (no lines deleted) */
1544 if (row > 0 && bot_start + row
1545 + (int)wp->w_lines[j].wl_size > wp->w_height)
1546 {
1547 wp->w_lines_valid = idx + 1;
1548 break;
1549 }
1550 bot_start += wp->w_lines[idx++].wl_size;
1551
1552 /* stop at the last valid entry in w_lines[].wl_size */
1553 if (++j >= wp->w_lines_valid)
1554 {
1555 wp->w_lines_valid = idx;
1556 break;
1557 }
1558 }
1559#ifdef FEAT_DIFF
1560 /* Correct the first entry for filler lines at the top
1561 * when it won't get updated below. */
1562 if (wp->w_p_diff && bot_start > 0)
1563 wp->w_lines[0].wl_size =
1564 plines_win_nofill(wp, wp->w_topline, TRUE)
1565 + wp->w_topfill;
1566#endif
1567 }
1568 }
1569 }
1570
1571 /* When starting redraw in the first line, redraw all lines. When
1572 * there is only one window it's probably faster to clear the screen
1573 * first. */
1574 if (mid_start == 0)
1575 {
1576 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001577 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001578 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001579 /* Clear the screen when it was not done by win_del_lines() or
1580 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1581 * then. */
1582 if (screen_cleared != TRUE)
1583 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001584#ifdef FEAT_WINDOWS
1585 /* The screen was cleared, redraw the tab pages line. */
1586 if (redraw_tabline)
1587 draw_tabline();
1588#endif
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001591
1592 /* When win_del_lines() or win_ins_lines() caused the screen to be
1593 * cleared (only happens for the first window) or when screenclear()
1594 * was called directly above, "must_redraw" will have been set to
1595 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1596 if (screen_cleared == TRUE)
1597 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
1601 /* Not VALID or INVERTED: redraw all lines. */
1602 mid_start = 0;
1603 mid_end = wp->w_height;
1604 }
1605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001606 if (type == SOME_VALID)
1607 {
1608 /* SOME_VALID: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 type = NOT_VALID;
1612 }
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /* check if we are updating or removing the inverted part */
1615 if ((VIsual_active && buf == curwin->w_buffer)
1616 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1617 {
1618 linenr_T from, to;
1619
1620 if (VIsual_active)
1621 {
1622 if (VIsual_active
1623 && (VIsual_mode != wp->w_old_visual_mode
1624 || type == INVERTED_ALL))
1625 {
1626 /*
1627 * If the type of Visual selection changed, redraw the whole
1628 * selection. Also when the ownership of the X selection is
1629 * gained or lost.
1630 */
1631 if (curwin->w_cursor.lnum < VIsual.lnum)
1632 {
1633 from = curwin->w_cursor.lnum;
1634 to = VIsual.lnum;
1635 }
1636 else
1637 {
1638 from = VIsual.lnum;
1639 to = curwin->w_cursor.lnum;
1640 }
1641 /* redraw more when the cursor moved as well */
1642 if (wp->w_old_cursor_lnum < from)
1643 from = wp->w_old_cursor_lnum;
1644 if (wp->w_old_cursor_lnum > to)
1645 to = wp->w_old_cursor_lnum;
1646 if (wp->w_old_visual_lnum < from)
1647 from = wp->w_old_visual_lnum;
1648 if (wp->w_old_visual_lnum > to)
1649 to = wp->w_old_visual_lnum;
1650 }
1651 else
1652 {
1653 /*
1654 * Find the line numbers that need to be updated: The lines
1655 * between the old cursor position and the current cursor
1656 * position. Also check if the Visual position changed.
1657 */
1658 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1659 {
1660 from = curwin->w_cursor.lnum;
1661 to = wp->w_old_cursor_lnum;
1662 }
1663 else
1664 {
1665 from = wp->w_old_cursor_lnum;
1666 to = curwin->w_cursor.lnum;
1667 if (from == 0) /* Visual mode just started */
1668 from = to;
1669 }
1670
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001671 if (VIsual.lnum != wp->w_old_visual_lnum
1672 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 if (wp->w_old_visual_lnum < from
1675 && wp->w_old_visual_lnum != 0)
1676 from = wp->w_old_visual_lnum;
1677 if (wp->w_old_visual_lnum > to)
1678 to = wp->w_old_visual_lnum;
1679 if (VIsual.lnum < from)
1680 from = VIsual.lnum;
1681 if (VIsual.lnum > to)
1682 to = VIsual.lnum;
1683 }
1684 }
1685
1686 /*
1687 * If in block mode and changed column or curwin->w_curswant:
1688 * update all lines.
1689 * First compute the actual start and end column.
1690 */
1691 if (VIsual_mode == Ctrl_V)
1692 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001693 colnr_T fromc, toc;
1694#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1695 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar404406a2014-10-09 13:24:43 +02001697 if (curwin->w_p_lbr)
1698 ve_flags = VE_ALL;
1699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001701#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1702 ve_flags = save_ve_flags;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 ++toc;
1705 if (curwin->w_curswant == MAXCOL)
1706 toc = MAXCOL;
1707
1708 if (fromc != wp->w_old_cursor_fcol
1709 || toc != wp->w_old_cursor_lcol)
1710 {
1711 if (from > VIsual.lnum)
1712 from = VIsual.lnum;
1713 if (to < VIsual.lnum)
1714 to = VIsual.lnum;
1715 }
1716 wp->w_old_cursor_fcol = fromc;
1717 wp->w_old_cursor_lcol = toc;
1718 }
1719 }
1720 else
1721 {
1722 /* Use the line numbers of the old Visual area. */
1723 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1724 {
1725 from = wp->w_old_cursor_lnum;
1726 to = wp->w_old_visual_lnum;
1727 }
1728 else
1729 {
1730 from = wp->w_old_visual_lnum;
1731 to = wp->w_old_cursor_lnum;
1732 }
1733 }
1734
1735 /*
1736 * There is no need to update lines above the top of the window.
1737 */
1738 if (from < wp->w_topline)
1739 from = wp->w_topline;
1740
1741 /*
1742 * If we know the value of w_botline, use it to restrict the update to
1743 * the lines that are visible in the window.
1744 */
1745 if (wp->w_valid & VALID_BOTLINE)
1746 {
1747 if (from >= wp->w_botline)
1748 from = wp->w_botline - 1;
1749 if (to >= wp->w_botline)
1750 to = wp->w_botline - 1;
1751 }
1752
1753 /*
1754 * Find the minimal part to be updated.
1755 * Watch out for scrolling that made entries in w_lines[] invalid.
1756 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1757 * top_end; need to redraw from top_end to the "to" line.
1758 * A middle mouse click with a Visual selection may change the text
1759 * above the Visual area and reset wl_valid, do count these for
1760 * mid_end (in srow).
1761 */
1762 if (mid_start > 0)
1763 {
1764 lnum = wp->w_topline;
1765 idx = 0;
1766 srow = 0;
1767 if (scrolled_down)
1768 mid_start = top_end;
1769 else
1770 mid_start = 0;
1771 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1772 {
1773 if (wp->w_lines[idx].wl_valid)
1774 mid_start += wp->w_lines[idx].wl_size;
1775 else if (!scrolled_down)
1776 srow += wp->w_lines[idx].wl_size;
1777 ++idx;
1778# ifdef FEAT_FOLDING
1779 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1780 lnum = wp->w_lines[idx].wl_lnum;
1781 else
1782# endif
1783 ++lnum;
1784 }
1785 srow += mid_start;
1786 mid_end = wp->w_height;
1787 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1788 {
1789 if (wp->w_lines[idx].wl_valid
1790 && wp->w_lines[idx].wl_lnum >= to + 1)
1791 {
1792 /* Only update until first row of this line */
1793 mid_end = srow;
1794 break;
1795 }
1796 srow += wp->w_lines[idx].wl_size;
1797 }
1798 }
1799 }
1800
1801 if (VIsual_active && buf == curwin->w_buffer)
1802 {
1803 wp->w_old_visual_mode = VIsual_mode;
1804 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1805 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001806 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 wp->w_old_curswant = curwin->w_curswant;
1808 }
1809 else
1810 {
1811 wp->w_old_visual_mode = 0;
1812 wp->w_old_cursor_lnum = 0;
1813 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001814 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1818 /* reset got_int, otherwise regexp won't work */
1819 save_got_int = got_int;
1820 got_int = 0;
1821#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001822#ifdef SYN_TIME_LIMIT
1823 /* Set the time limit to 'redrawtime'. */
1824 profile_setlimit(p_rdt, &syntax_tm);
1825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826#ifdef FEAT_FOLDING
1827 win_foldinfo.fi_level = 0;
1828#endif
1829
1830 /*
1831 * Update all the window rows.
1832 */
1833 idx = 0; /* first entry in w_lines[].wl_size */
1834 row = 0;
1835 srow = 0;
1836 lnum = wp->w_topline; /* first line shown in window */
1837 for (;;)
1838 {
1839 /* stop updating when reached the end of the window (check for _past_
1840 * the end of the window is at the end of the loop) */
1841 if (row == wp->w_height)
1842 {
1843 didline = TRUE;
1844 break;
1845 }
1846
1847 /* stop updating when hit the end of the file */
1848 if (lnum > buf->b_ml.ml_line_count)
1849 {
1850 eof = TRUE;
1851 break;
1852 }
1853
1854 /* Remember the starting row of the line that is going to be dealt
1855 * with. It is used further down when the line doesn't fit. */
1856 srow = row;
1857
1858 /*
1859 * Update a line when it is in an area that needs updating, when it
1860 * has changes or w_lines[idx] is invalid.
1861 * bot_start may be halfway a wrapped line after using
1862 * win_del_lines(), check if the current line includes it.
1863 * When syntax folding is being used, the saved syntax states will
1864 * already have been updated, we can't see where the syntax state is
1865 * the same again, just update until the end of the window.
1866 */
1867 if (row < top_end
1868 || (row >= mid_start && row < mid_end)
1869#ifdef FEAT_SEARCH_EXTRA
1870 || top_to_mod
1871#endif
1872 || idx >= wp->w_lines_valid
1873 || (row + wp->w_lines[idx].wl_size > bot_start)
1874 || (mod_top != 0
1875 && (lnum == mod_top
1876 || (lnum >= mod_top
1877 && (lnum < mod_bot
1878#ifdef FEAT_SYN_HL
1879 || did_update == DID_FOLD
1880 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001881 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 && (
1883# ifdef FEAT_FOLDING
1884 (foldmethodIsSyntax(wp)
1885 && hasAnyFolding(wp)) ||
1886# endif
1887 syntax_check_changed(lnum)))
1888#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001889#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001890 /* match in fixed position might need redraw
1891 * if lines were inserted or deleted */
1892 || (wp->w_match_head != NULL
1893 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 )))))
1896 {
1897#ifdef FEAT_SEARCH_EXTRA
1898 if (lnum == mod_top)
1899 top_to_mod = FALSE;
1900#endif
1901
1902 /*
1903 * When at start of changed lines: May scroll following lines
1904 * up or down to minimize redrawing.
1905 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001906 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908 if (lnum == mod_top
1909 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001910 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 int old_rows = 0;
1913 int new_rows = 0;
1914 int xtra_rows;
1915 linenr_T l;
1916
1917 /* Count the old number of window rows, using w_lines[], which
1918 * should still contain the sizes for the lines as they are
1919 * currently displayed. */
1920 for (i = idx; i < wp->w_lines_valid; ++i)
1921 {
1922 /* Only valid lines have a meaningful wl_lnum. Invalid
1923 * lines are part of the changed area. */
1924 if (wp->w_lines[i].wl_valid
1925 && wp->w_lines[i].wl_lnum == mod_bot)
1926 break;
1927 old_rows += wp->w_lines[i].wl_size;
1928#ifdef FEAT_FOLDING
1929 if (wp->w_lines[i].wl_valid
1930 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1931 {
1932 /* Must have found the last valid entry above mod_bot.
1933 * Add following invalid entries. */
1934 ++i;
1935 while (i < wp->w_lines_valid
1936 && !wp->w_lines[i].wl_valid)
1937 old_rows += wp->w_lines[i++].wl_size;
1938 break;
1939 }
1940#endif
1941 }
1942
1943 if (i >= wp->w_lines_valid)
1944 {
1945 /* We can't find a valid line below the changed lines,
1946 * need to redraw until the end of the window.
1947 * Inserting/deleting lines has no use. */
1948 bot_start = 0;
1949 }
1950 else
1951 {
1952 /* Able to count old number of rows: Count new window
1953 * rows, and may insert/delete lines */
1954 j = idx;
1955 for (l = lnum; l < mod_bot; ++l)
1956 {
1957#ifdef FEAT_FOLDING
1958 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1959 ++new_rows;
1960 else
1961#endif
1962#ifdef FEAT_DIFF
1963 if (l == wp->w_topline)
1964 new_rows += plines_win_nofill(wp, l, TRUE)
1965 + wp->w_topfill;
1966 else
1967#endif
1968 new_rows += plines_win(wp, l, TRUE);
1969 ++j;
1970 if (new_rows > wp->w_height - row - 2)
1971 {
1972 /* it's getting too much, must redraw the rest */
1973 new_rows = 9999;
1974 break;
1975 }
1976 }
1977 xtra_rows = new_rows - old_rows;
1978 if (xtra_rows < 0)
1979 {
1980 /* May scroll text up. If there is not enough
1981 * remaining text or scrolling fails, must redraw the
1982 * rest. If scrolling works, must redraw the text
1983 * below the scrolled text. */
1984 if (row - xtra_rows >= wp->w_height - 2)
1985 mod_bot = MAXLNUM;
1986 else
1987 {
1988 check_for_delay(FALSE);
1989 if (win_del_lines(wp, row,
1990 -xtra_rows, FALSE, FALSE) == FAIL)
1991 mod_bot = MAXLNUM;
1992 else
1993 bot_start = wp->w_height + xtra_rows;
1994 }
1995 }
1996 else if (xtra_rows > 0)
1997 {
1998 /* May scroll text down. If there is not enough
1999 * remaining text of scrolling fails, must redraw the
2000 * rest. */
2001 if (row + xtra_rows >= wp->w_height - 2)
2002 mod_bot = MAXLNUM;
2003 else
2004 {
2005 check_for_delay(FALSE);
2006 if (win_ins_lines(wp, row + old_rows,
2007 xtra_rows, FALSE, FALSE) == FAIL)
2008 mod_bot = MAXLNUM;
2009 else if (top_end > row + old_rows)
2010 /* Scrolled the part at the top that requires
2011 * updating down. */
2012 top_end += xtra_rows;
2013 }
2014 }
2015
2016 /* When not updating the rest, may need to move w_lines[]
2017 * entries. */
2018 if (mod_bot != MAXLNUM && i != j)
2019 {
2020 if (j < i)
2021 {
2022 int x = row + new_rows;
2023
2024 /* move entries in w_lines[] upwards */
2025 for (;;)
2026 {
2027 /* stop at last valid entry in w_lines[] */
2028 if (i >= wp->w_lines_valid)
2029 {
2030 wp->w_lines_valid = j;
2031 break;
2032 }
2033 wp->w_lines[j] = wp->w_lines[i];
2034 /* stop at a line that won't fit */
2035 if (x + (int)wp->w_lines[j].wl_size
2036 > wp->w_height)
2037 {
2038 wp->w_lines_valid = j + 1;
2039 break;
2040 }
2041 x += wp->w_lines[j++].wl_size;
2042 ++i;
2043 }
2044 if (bot_start > x)
2045 bot_start = x;
2046 }
2047 else /* j > i */
2048 {
2049 /* move entries in w_lines[] downwards */
2050 j -= i;
2051 wp->w_lines_valid += j;
2052 if (wp->w_lines_valid > wp->w_height)
2053 wp->w_lines_valid = wp->w_height;
2054 for (i = wp->w_lines_valid; i - j >= idx; --i)
2055 wp->w_lines[i] = wp->w_lines[i - j];
2056
2057 /* The w_lines[] entries for inserted lines are
2058 * now invalid, but wl_size may be used above.
2059 * Reset to zero. */
2060 while (i >= idx)
2061 {
2062 wp->w_lines[i].wl_size = 0;
2063 wp->w_lines[i--].wl_valid = FALSE;
2064 }
2065 }
2066 }
2067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 }
2069
2070#ifdef FEAT_FOLDING
2071 /*
2072 * When lines are folded, display one line for all of them.
2073 * Otherwise, display normally (can be several display lines when
2074 * 'wrap' is on).
2075 */
2076 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2077 if (fold_count != 0)
2078 {
2079 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2080 ++row;
2081 --fold_count;
2082 wp->w_lines[idx].wl_folded = TRUE;
2083 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2084# ifdef FEAT_SYN_HL
2085 did_update = DID_FOLD;
2086# endif
2087 }
2088 else
2089#endif
2090 if (idx < wp->w_lines_valid
2091 && wp->w_lines[idx].wl_valid
2092 && wp->w_lines[idx].wl_lnum == lnum
2093 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002094 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 && srow + wp->w_lines[idx].wl_size > wp->w_height
2096#ifdef FEAT_DIFF
2097 && diff_check_fill(wp, lnum) == 0
2098#endif
2099 )
2100 {
2101 /* This line is not going to fit. Don't draw anything here,
2102 * will draw "@ " lines below. */
2103 row = wp->w_height + 1;
2104 }
2105 else
2106 {
2107#ifdef FEAT_SEARCH_EXTRA
2108 prepare_search_hl(wp, lnum);
2109#endif
2110#ifdef FEAT_SYN_HL
2111 /* Let the syntax stuff know we skipped a few lines. */
2112 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002113 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 syntax_end_parsing(syntax_last_parsed + 1);
2115#endif
2116
2117 /*
2118 * Display one line.
2119 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002120 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2121#ifdef SYN_TIME_LIMIT
2122 &syntax_tm
2123#else
2124 NULL
2125#endif
2126 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128#ifdef FEAT_FOLDING
2129 wp->w_lines[idx].wl_folded = FALSE;
2130 wp->w_lines[idx].wl_lastlnum = lnum;
2131#endif
2132#ifdef FEAT_SYN_HL
2133 did_update = DID_LINE;
2134 syntax_last_parsed = lnum;
2135#endif
2136 }
2137
2138 wp->w_lines[idx].wl_lnum = lnum;
2139 wp->w_lines[idx].wl_valid = TRUE;
2140 if (row > wp->w_height) /* past end of screen */
2141 {
2142 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002143 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2145 ++idx;
2146 break;
2147 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002148 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 wp->w_lines[idx].wl_size = row - srow;
2150 ++idx;
2151#ifdef FEAT_FOLDING
2152 lnum += fold_count + 1;
2153#else
2154 ++lnum;
2155#endif
2156 }
2157 else
2158 {
2159 /* This line does not need updating, advance to the next one */
2160 row += wp->w_lines[idx++].wl_size;
2161 if (row > wp->w_height) /* past end of screen */
2162 break;
2163#ifdef FEAT_FOLDING
2164 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2165#else
2166 ++lnum;
2167#endif
2168#ifdef FEAT_SYN_HL
2169 did_update = DID_NONE;
2170#endif
2171 }
2172
2173 if (lnum > buf->b_ml.ml_line_count)
2174 {
2175 eof = TRUE;
2176 break;
2177 }
2178 }
2179 /*
2180 * End of loop over all window lines.
2181 */
2182
2183
2184 if (idx > wp->w_lines_valid)
2185 wp->w_lines_valid = idx;
2186
2187#ifdef FEAT_SYN_HL
2188 /*
2189 * Let the syntax stuff know we stop parsing here.
2190 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002191 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 syntax_end_parsing(syntax_last_parsed + 1);
2193#endif
2194
2195 /*
2196 * If we didn't hit the end of the file, and we didn't finish the last
2197 * line we were working on, then the line didn't fit.
2198 */
2199 wp->w_empty_rows = 0;
2200#ifdef FEAT_DIFF
2201 wp->w_filler_rows = 0;
2202#endif
2203 if (!eof && !didline)
2204 {
2205 if (lnum == wp->w_topline)
2206 {
2207 /*
2208 * Single line that does not fit!
2209 * Don't overwrite it, it can be edited.
2210 */
2211 wp->w_botline = lnum + 1;
2212 }
2213#ifdef FEAT_DIFF
2214 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2215 {
2216 /* Window ends in filler lines. */
2217 wp->w_botline = lnum;
2218 wp->w_filler_rows = wp->w_height - srow;
2219 }
2220#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002221 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2222 {
2223 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2224
2225 /*
2226 * Last line isn't finished: Display "@@@" in the last screen line.
2227 */
2228 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002229 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002230 screen_fill(scr_row, scr_row + 1,
2231 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002232 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002233 set_empty_rows(wp, srow);
2234 wp->w_botline = lnum;
2235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2237 {
2238 /*
2239 * Last line isn't finished: Display "@@@" at the end.
2240 */
2241 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2242 W_WINROW(wp) + wp->w_height,
2243 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002244 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 set_empty_rows(wp, srow);
2246 wp->w_botline = lnum;
2247 }
2248 else
2249 {
2250 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2251 wp->w_botline = lnum;
2252 }
2253 }
2254 else
2255 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002256#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 draw_vsep_win(wp, row);
2258#endif
2259 if (eof) /* we hit the end of the file */
2260 {
2261 wp->w_botline = buf->b_ml.ml_line_count + 1;
2262#ifdef FEAT_DIFF
2263 j = diff_check_fill(wp, wp->w_botline);
2264 if (j > 0 && !wp->w_botfill)
2265 {
2266 /*
2267 * Display filler lines at the end of the file
2268 */
2269 if (char2cells(fill_diff) > 1)
2270 i = '-';
2271 else
2272 i = fill_diff;
2273 if (row + j > wp->w_height)
2274 j = wp->w_height - row;
2275 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2276 row += j;
2277 }
2278#endif
2279 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002280 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 wp->w_botline = lnum;
2282
2283 /* make sure the rest of the screen is blank */
2284 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002285 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 /* Reset the type of redrawing required, the window has been updated. */
2289 wp->w_redr_type = 0;
2290#ifdef FEAT_DIFF
2291 wp->w_old_topfill = wp->w_topfill;
2292 wp->w_old_botfill = wp->w_botfill;
2293#endif
2294
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002295 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 /*
2298 * There is a trick with w_botline. If we invalidate it on each
2299 * change that might modify it, this will cause a lot of expensive
2300 * calls to plines() in update_topline() each time. Therefore the
2301 * value of w_botline is often approximated, and this value is used to
2302 * compute the value of w_topline. If the value of w_botline was
2303 * wrong, check that the value of w_topline is correct (cursor is on
2304 * the visible part of the text). If it's not, we need to redraw
2305 * again. Mostly this just means scrolling up a few lines, so it
2306 * doesn't look too bad. Only do this for the current window (where
2307 * changes are relevant).
2308 */
2309 wp->w_valid |= VALID_BOTLINE;
2310 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2311 {
2312 recursive = TRUE;
2313 curwin->w_valid &= ~VALID_TOPLINE;
2314 update_topline(); /* may invalidate w_botline again */
2315 if (must_redraw != 0)
2316 {
2317 /* Don't update for changes in buffer again. */
2318 i = curbuf->b_mod_set;
2319 curbuf->b_mod_set = FALSE;
2320 win_update(curwin);
2321 must_redraw = 0;
2322 curbuf->b_mod_set = i;
2323 }
2324 recursive = FALSE;
2325 }
2326 }
2327
2328#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2329 /* restore got_int, unless CTRL-C was hit while redrawing */
2330 if (!got_int)
2331 got_int = save_got_int;
2332#endif
2333}
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335/*
2336 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2337 * as the filler character.
2338 */
2339 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002340win_draw_end(
2341 win_T *wp,
2342 int c1,
2343 int c2,
2344 int row,
2345 int endrow,
2346 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
2348#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2349 int n = 0;
2350# define FDC_OFF n
2351#else
2352# define FDC_OFF 0
2353#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002354#ifdef FEAT_FOLDING
2355 int fdc = compute_foldcolumn(wp, 0);
2356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (wp->w_p_rl)
2360 {
2361 /* No check for cmdline window: should never be right-left. */
2362# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002363 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 if (n > 0)
2366 {
2367 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002368 if (n > W_WIDTH(wp))
2369 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2371 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002372 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374# endif
2375# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002376 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 int nn = n + 2;
2379
2380 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002381 if (nn > W_WIDTH(wp))
2382 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2384 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 n = nn;
2387 }
2388# endif
2389 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2390 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002391 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2393 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002394 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 else
2397#endif
2398 {
2399#ifdef FEAT_CMDWIN
2400 if (cmdwin_type != 0 && wp == curwin)
2401 {
2402 /* draw the cmdline character in the leftmost column */
2403 n = 1;
2404 if (n > wp->w_width)
2405 n = wp->w_width;
2406 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2407 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002408 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410#endif
2411#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002412 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002414 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 /* draw the fold column at the left */
2417 if (nn > W_WIDTH(wp))
2418 nn = W_WIDTH(wp);
2419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2420 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 n = nn;
2423 }
2424#endif
2425#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002426 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 int nn = n + 2;
2429
2430 /* draw the sign column after the fold column */
2431 if (nn > W_WIDTH(wp))
2432 nn = W_WIDTH(wp);
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 n = nn;
2437 }
2438#endif
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2440 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 set_empty_rows(wp, row);
2444}
2445
Bram Moolenaar1a384422010-07-14 19:53:30 +02002446#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002447static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002448
2449/*
2450 * Advance **color_cols and return TRUE when there are columns to draw.
2451 */
2452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002453advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002454{
2455 while (**color_cols >= 0 && vcol > **color_cols)
2456 ++*color_cols;
2457 return (**color_cols >= 0);
2458}
2459#endif
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#ifdef FEAT_FOLDING
2462/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002463 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2464 * space is available for window "wp", minus "col".
2465 */
2466 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002467compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002468{
2469 int fdc = wp->w_p_fdc;
2470 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2471 int wwidth = W_WIDTH(wp);
2472
2473 if (fdc > wwidth - (col + wmw))
2474 fdc = wwidth - (col + wmw);
2475 return fdc;
2476}
2477
2478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 * Display one folded line.
2480 */
2481 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002482fold_line(
2483 win_T *wp,
2484 long fold_count,
2485 foldinfo_T *foldinfo,
2486 linenr_T lnum,
2487 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002489 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 pos_T *top, *bot;
2491 linenr_T lnume = lnum + fold_count - 1;
2492 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002493 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 int col;
2496 int txtcol;
2497 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002498 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 /* Build the fold line:
2501 * 1. Add the cmdwin_type for the command-line window
2502 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002503 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * 4. Compose the text
2505 * 5. Add the text
2506 * 6. set highlighting for the Visual area an other text
2507 */
2508 col = 0;
2509
2510 /*
2511 * 1. Add the cmdwin_type for the command-line window
2512 * Ignores 'rightleft', this window is never right-left.
2513 */
2514#ifdef FEAT_CMDWIN
2515 if (cmdwin_type != 0 && wp == curwin)
2516 {
2517 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002518 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519#ifdef FEAT_MBYTE
2520 if (enc_utf8)
2521 ScreenLinesUC[off] = 0;
2522#endif
2523 ++col;
2524 }
2525#endif
2526
2527 /*
2528 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002529 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002531 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (fdc > 0)
2533 {
2534 fill_foldcolumn(buf, wp, TRUE, lnum);
2535#ifdef FEAT_RIGHTLEFT
2536 if (wp->w_p_rl)
2537 {
2538 int i;
2539
2540 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002541 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 /* reverse the fold column */
2543 for (i = 0; i < fdc; ++i)
2544 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2545 }
2546 else
2547#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002548 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 col += fdc;
2550 }
2551
2552#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002553# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2554 for (ri = 0; ri < l; ++ri) \
2555 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2556 else \
2557 for (ri = 0; ri < l; ++ri) \
2558 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002560# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2561 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563
Bram Moolenaar64486672010-05-16 15:46:46 +02002564 /* Set all attributes of the 'number' or 'relativenumber' column and the
2565 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002566 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568#ifdef FEAT_SIGNS
2569 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002570 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
2572 len = W_WIDTH(wp) - col;
2573 if (len > 0)
2574 {
2575 if (len > 2)
2576 len = 2;
2577# ifdef FEAT_RIGHTLEFT
2578 if (wp->w_p_rl)
2579 /* the line number isn't reversed */
2580 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002581 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 else
2583# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002584 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 col += len;
2586 }
2587 }
2588#endif
2589
2590 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002591 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002593 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 len = W_WIDTH(wp) - col;
2596 if (len > 0)
2597 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002598 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002599 long num;
2600 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002601
2602 if (len > w + 1)
2603 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002604
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002605 if (wp->w_p_nu && !wp->w_p_rnu)
2606 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002607 num = (long)lnum;
2608 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002609 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002610 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002611 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002612 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002613 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002614 /* 'number' + 'relativenumber': cursor line shows absolute
2615 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002616 num = lnum;
2617 fmt = "%-*ld ";
2618 }
2619 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002620
Bram Moolenaar700e7342013-01-30 12:31:36 +01002621 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622#ifdef FEAT_RIGHTLEFT
2623 if (wp->w_p_rl)
2624 /* the line number isn't reversed */
2625 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002626 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 else
2628#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002629 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 col += len;
2631 }
2632 }
2633
2634 /*
2635 * 4. Compose the folded-line string with 'foldtext', if set.
2636 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002637 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 txtcol = col; /* remember where text starts */
2640
2641 /*
2642 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2643 * Right-left text is put in columns 0 - number-col, normal text is put
2644 * in columns number-col - window-width.
2645 */
2646#ifdef FEAT_MBYTE
2647 if (has_mbyte)
2648 {
2649 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002650 int u8c, u8cc[MAX_MCO];
2651 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 int idx;
2653 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002654 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655# ifdef FEAT_ARABIC
2656 int prev_c = 0; /* previous Arabic character */
2657 int prev_c1 = 0; /* first composing char for prev_c */
2658# endif
2659
2660# ifdef FEAT_RIGHTLEFT
2661 if (wp->w_p_rl)
2662 idx = off;
2663 else
2664# endif
2665 idx = off + col;
2666
2667 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2668 for (p = text; *p != NUL; )
2669 {
2670 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002671 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (col + cells > W_WIDTH(wp)
2673# ifdef FEAT_RIGHTLEFT
2674 - (wp->w_p_rl ? col : 0)
2675# endif
2676 )
2677 break;
2678 ScreenLines[idx] = *p;
2679 if (enc_utf8)
2680 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 u8c = utfc_ptr2char(p, u8cc);
2682 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
2684 ScreenLinesUC[idx] = 0;
2685#ifdef FEAT_ARABIC
2686 prev_c = u8c;
2687#endif
2688 }
2689 else
2690 {
2691#ifdef FEAT_ARABIC
2692 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2693 {
2694 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002695 int pc, pc1, nc;
2696 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 int firstbyte = *p;
2698
2699 /* The idea of what is the previous and next
2700 * character depends on 'rightleft'. */
2701 if (wp->w_p_rl)
2702 {
2703 pc = prev_c;
2704 pc1 = prev_c1;
2705 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 else
2709 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 prev_c = u8c;
2715
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002716 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 pc, pc1, nc);
2718 ScreenLines[idx] = firstbyte;
2719 }
2720 else
2721 prev_c = u8c;
2722#endif
2723 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002724#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (u8c >= 0x10000)
2726 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2727 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 for (i = 0; i < Screen_mco; ++i)
2731 {
2732 ScreenLinesC[i][idx] = u8cc[i];
2733 if (u8cc[i] == 0)
2734 break;
2735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
2737 if (cells > 1)
2738 ScreenLines[idx + 1] = 0;
2739 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002740 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2741 /* double-byte single width character */
2742 ScreenLines2[idx] = p[1];
2743 else if (cells > 1)
2744 /* double-width character */
2745 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 col += cells;
2747 idx += cells;
2748 p += c_len;
2749 }
2750 }
2751 else
2752#endif
2753 {
2754 len = (int)STRLEN(text);
2755 if (len > W_WIDTH(wp) - col)
2756 len = W_WIDTH(wp) - col;
2757 if (len > 0)
2758 {
2759#ifdef FEAT_RIGHTLEFT
2760 if (wp->w_p_rl)
2761 STRNCPY(current_ScreenLine, text, len);
2762 else
2763#endif
2764 STRNCPY(current_ScreenLine + col, text, len);
2765 col += len;
2766 }
2767 }
2768
2769 /* Fill the rest of the line with the fold filler */
2770#ifdef FEAT_RIGHTLEFT
2771 if (wp->w_p_rl)
2772 col -= txtcol;
2773#endif
2774 while (col < W_WIDTH(wp)
2775#ifdef FEAT_RIGHTLEFT
2776 - (wp->w_p_rl ? txtcol : 0)
2777#endif
2778 )
2779 {
2780#ifdef FEAT_MBYTE
2781 if (enc_utf8)
2782 {
2783 if (fill_fold >= 0x80)
2784 {
2785 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002786 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002787 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
2789 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002792 ScreenLines[off + col] = fill_fold;
2793 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002794 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002798 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
2800
2801 if (text != buf)
2802 vim_free(text);
2803
2804 /*
2805 * 6. set highlighting for the Visual area an other text.
2806 * If all folded lines are in the Visual area, highlight the line.
2807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2809 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002810 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
2812 /* Visual is after curwin->w_cursor */
2813 top = &curwin->w_cursor;
2814 bot = &VIsual;
2815 }
2816 else
2817 {
2818 /* Visual is before curwin->w_cursor */
2819 top = &VIsual;
2820 bot = &curwin->w_cursor;
2821 }
2822 if (lnum >= top->lnum
2823 && lnume <= bot->lnum
2824 && (VIsual_mode != 'v'
2825 || ((lnum > top->lnum
2826 || (lnum == top->lnum
2827 && top->col == 0))
2828 && (lnume < bot->lnum
2829 || (lnume == bot->lnum
2830 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002831 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 if (VIsual_mode == Ctrl_V)
2834 {
2835 /* Visual block mode: highlight the chars part of the block */
2836 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2837 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002838 if (wp->w_old_cursor_lcol != MAXCOL
2839 && wp->w_old_cursor_lcol + txtcol
2840 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 len = wp->w_old_cursor_lcol;
2842 else
2843 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002844 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002845 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
2847 }
2848 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002851 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002856#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002857 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2858 if (wp->w_p_cc_cols)
2859 {
2860 int i = 0;
2861 int j = wp->w_p_cc_cols[i];
2862 int old_txtcol = txtcol;
2863
2864 while (j > -1)
2865 {
2866 txtcol += j;
2867 if (wp->w_p_wrap)
2868 txtcol -= wp->w_skipcol;
2869 else
2870 txtcol -= wp->w_leftcol;
2871 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2872 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002873 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002874 txtcol = old_txtcol;
2875 j = wp->w_p_cc_cols[++i];
2876 }
2877 }
2878
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002879 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002880 if (wp->w_p_cuc)
2881 {
2882 txtcol += wp->w_virtcol;
2883 if (wp->w_p_wrap)
2884 txtcol -= wp->w_skipcol;
2885 else
2886 txtcol -= wp->w_leftcol;
2887 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2888 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002889 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002890 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002893 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 (int)W_WIDTH(wp), FALSE);
2895
2896 /*
2897 * Update w_cline_height and w_cline_folded if the cursor line was
2898 * updated (saves a call to plines() later).
2899 */
2900 if (wp == curwin
2901 && lnum <= curwin->w_cursor.lnum
2902 && lnume >= curwin->w_cursor.lnum)
2903 {
2904 curwin->w_cline_row = row;
2905 curwin->w_cline_height = 1;
2906 curwin->w_cline_folded = TRUE;
2907 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2908 }
2909}
2910
2911/*
2912 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2913 */
2914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002915copy_text_attr(
2916 int off,
2917 char_u *buf,
2918 int len,
2919 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002921 int i;
2922
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 mch_memmove(ScreenLines + off, buf, (size_t)len);
2924# ifdef FEAT_MBYTE
2925 if (enc_utf8)
2926 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2927# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002928 for (i = 0; i < len; ++i)
2929 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
2932/*
2933 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002934 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937fill_foldcolumn(
2938 char_u *p,
2939 win_T *wp,
2940 int closed, /* TRUE of FALSE */
2941 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 int i = 0;
2944 int level;
2945 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002946 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002947 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002950 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 level = win_foldinfo.fi_level;
2953 if (level > 0)
2954 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002955 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002956 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 /* If the column is too narrow, we start at the lowest level that
2959 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002960 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (first_level < 1)
2962 first_level = 1;
2963
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {
2966 if (win_foldinfo.fi_lnum == lnum
2967 && first_level + i >= win_foldinfo.fi_low_level)
2968 p[i] = '-';
2969 else if (first_level == 1)
2970 p[i] = '|';
2971 else if (first_level + i <= 9)
2972 p[i] = '0' + first_level + i;
2973 else
2974 p[i] = '>';
2975 if (first_level + i == level)
2976 break;
2977 }
2978 }
2979 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002980 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981}
2982#endif /* FEAT_FOLDING */
2983
2984/*
2985 * Display line "lnum" of window 'wp' on the screen.
2986 * Start at row "startrow", stop when "endrow" is reached.
2987 * wp->w_virtcol needs to be valid.
2988 *
2989 * Return the number of last row the line occupies.
2990 */
2991 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002992win_line(
2993 win_T *wp,
2994 linenr_T lnum,
2995 int startrow,
2996 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002997 int nochange UNUSED, /* not updating for changed text */
2998 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003000 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3002 int c = 0; /* init for GCC */
3003 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003004#ifdef FEAT_LINEBREAK
3005 long vcol_sbr = -1; /* virtual column after showbreak */
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 long vcol_prev = -1; /* "vcol" of previous character */
3008 char_u *line; /* current line */
3009 char_u *ptr; /* current position in "line" */
3010 int row; /* row in the window, excl w_winrow */
3011 int screen_row; /* row on the screen, incl w_winrow */
3012
3013 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3014 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003015 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003016 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 int c_extra = NUL; /* extra chars, all the same */
3018 int extra_attr = 0; /* attributes when n_extra != 0 */
3019 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3020 displaying lcs_eol at end-of-line */
3021 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3022 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3023
3024 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3025 int saved_n_extra = 0;
3026 char_u *saved_p_extra = NULL;
3027 int saved_c_extra = 0;
3028 int saved_char_attr = 0;
3029
3030 int n_attr = 0; /* chars with special attr */
3031 int saved_attr2 = 0; /* char_attr saved for n_attr */
3032 int n_attr3 = 0; /* chars with overruling special attr */
3033 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3034
3035 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3036
3037 int fromcol, tocol; /* start/end of inverting */
3038 int fromcol_prev = -2; /* start of inverting after cursor */
3039 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003041 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 pos_T pos;
3043 long v;
3044
3045 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003046 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3048 in this line */
3049 int attr = 0; /* attributes for area highlighting */
3050 int area_attr = 0; /* attributes desired by highlighting */
3051 int search_attr = 0; /* attributes desired by 'hlsearch' */
3052#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003053 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int syntax_attr = 0; /* attributes desired by syntax */
3055 int has_syntax = FALSE; /* this buffer has syntax highl. */
3056 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003057 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003058 int draw_color_col = FALSE; /* highlight colorcolumn */
3059 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003060#endif
3061#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003062 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003063# define SPWORDLEN 150
3064 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003065 int nextlinecol = 0; /* column where nextline[] starts */
3066 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003067 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003068 int spell_attr = 0; /* attributes desired by spelling */
3069 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003070 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3071 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003072 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003073 static int cap_col = -1; /* column to check for Cap word */
3074 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003075 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#endif
3077 int extra_check; /* has syntax or linebreak */
3078#ifdef FEAT_MBYTE
3079 int multi_attr = 0; /* attributes desired by multibyte */
3080 int mb_l = 1; /* multi-byte byte length */
3081 int mb_c = 0; /* decoded multi-byte character */
3082 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003083 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084#endif
3085#ifdef FEAT_DIFF
3086 int filler_lines; /* nr of filler lines to be drawn */
3087 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003088 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int change_start = MAXCOL; /* first col of changed area */
3090 int change_end = -1; /* last col of changed area */
3091#endif
3092 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3093#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003094 int need_showbreak = FALSE; /* overlong line, skipping first x
3095 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003097#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3098 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003100 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#endif
3102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003103 matchitem_T *cur; /* points to the match list */
3104 match_T *shl; /* points to search_hl or a match */
3105 int shl_flag; /* flag to indicate whether search_hl
3106 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003107 int pos_inprogress; /* marks that position match search is
3108 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003109 int prevcol_hl_flag; /* flag to indicate whether prevcol
3110 equals startcol of search_hl or one
3111 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#endif
3113#ifdef FEAT_ARABIC
3114 int prev_c = 0; /* previous Arabic character */
3115 int prev_c1 = 0; /* first composing char for prev_c */
3116#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003117#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003118 int did_line_attr = 0;
3119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /* draw_state: items that are drawn in sequence: */
3122#define WL_START 0 /* nothing done yet */
3123#ifdef FEAT_CMDWIN
3124# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3125#else
3126# define WL_CMDLINE WL_START
3127#endif
3128#ifdef FEAT_FOLDING
3129# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3130#else
3131# define WL_FOLD WL_CMDLINE
3132#endif
3133#ifdef FEAT_SIGNS
3134# define WL_SIGN WL_FOLD + 1 /* column for signs */
3135#else
3136# define WL_SIGN WL_FOLD /* column for signs */
3137#endif
3138#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003139#ifdef FEAT_LINEBREAK
3140# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003142# define WL_BRI WL_NR
3143#endif
3144#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3145# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3146#else
3147# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
3149#define WL_LINE WL_SBR + 1 /* text in the line */
3150 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003151#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int feedback_col = 0;
3153 int feedback_old_attr = -1;
3154#endif
3155
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156#ifdef FEAT_CONCEAL
3157 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003158 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003159 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003160 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003161 int is_concealing = FALSE;
3162 int boguscols = 0; /* nonexistent columns added to force
3163 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003164 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003165 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003166 int match_conc = 0; /* cchar for match functions */
3167 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003168 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003169# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003170# define FIX_FOR_BOGUSCOLS \
3171 { \
3172 n_extra += vcol_off; \
3173 vcol -= vcol_off; \
3174 vcol_off = 0; \
3175 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003176 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003177 boguscols = 0; \
3178 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003179#else
3180# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 if (startrow > endrow) /* past the end already! */
3184 return startrow;
3185
3186 row = startrow;
3187 screen_row = row + W_WINROW(wp);
3188
3189 /*
3190 * To speed up the loop below, set extra_check when there is linebreak,
3191 * trailing white space and/or syntax processing to be done.
3192 */
3193#ifdef FEAT_LINEBREAK
3194 extra_check = wp->w_p_lbr;
3195#else
3196 extra_check = 0;
3197#endif
3198#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003199 if (syntax_present(wp) && !wp->w_s->b_syn_error
3200# ifdef SYN_TIME_LIMIT
3201 && !wp->w_s->b_syn_slow
3202# endif
3203 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 {
3205 /* Prepare for syntax highlighting in this line. When there is an
3206 * error, stop syntax highlighting. */
3207 save_did_emsg = did_emsg;
3208 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003209 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003211 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
3213 {
3214 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003215#ifdef SYN_TIME_LIMIT
3216 if (!wp->w_s->b_syn_slow)
3217#endif
3218 {
3219 has_syntax = TRUE;
3220 extra_check = TRUE;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003224
3225 /* Check for columns to display for 'colorcolumn'. */
3226 color_cols = wp->w_p_cc_cols;
3227 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003228 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003229#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003230
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003231#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003232 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003233 && *wp->w_s->b_p_spl != NUL
3234 && wp->w_s->b_langp.ga_len > 0
3235 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003236 {
3237 /* Prepare for spell checking. */
3238 has_spell = TRUE;
3239 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003240
3241 /* Get the start of the next line, so that words that wrap to the next
3242 * line are found too: "et<line-break>al.".
3243 * Trick: skip a few chars for C/shell/Vim comments */
3244 nextline[SPWORDLEN] = NUL;
3245 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3246 {
3247 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3248 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3249 }
3250
3251 /* When a word wrapped from the previous line the start of the current
3252 * line is valid. */
3253 if (lnum == checked_lnum)
3254 cur_checked_col = checked_col;
3255 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003256
3257 /* When there was a sentence end in the previous line may require a
3258 * word starting with capital in this line. In line 1 always check
3259 * the first word. */
3260 if (lnum != capcol_lnum)
3261 cap_col = -1;
3262 if (lnum == 1)
3263 cap_col = 0;
3264 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#endif
3267
3268 /*
3269 * handle visual active in this window
3270 */
3271 fromcol = -10;
3272 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3274 {
3275 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003276 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 top = &curwin->w_cursor;
3279 bot = &VIsual;
3280 }
3281 else /* Visual is before curwin->w_cursor */
3282 {
3283 top = &VIsual;
3284 bot = &curwin->w_cursor;
3285 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003286 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (VIsual_mode == Ctrl_V) /* block mode */
3288 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003289 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
3291 fromcol = wp->w_old_cursor_fcol;
3292 tocol = wp->w_old_cursor_lcol;
3293 }
3294 }
3295 else /* non-block mode */
3296 {
3297 if (lnum > top->lnum && lnum <= bot->lnum)
3298 fromcol = 0;
3299 else if (lnum == top->lnum)
3300 {
3301 if (VIsual_mode == 'V') /* linewise */
3302 fromcol = 0;
3303 else
3304 {
3305 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3306 if (gchar_pos(top) == NUL)
3307 tocol = fromcol + 1;
3308 }
3309 }
3310 if (VIsual_mode != 'V' && lnum == bot->lnum)
3311 {
3312 if (*p_sel == 'e' && bot->col == 0
3313#ifdef FEAT_VIRTUALEDIT
3314 && bot->coladd == 0
3315#endif
3316 )
3317 {
3318 fromcol = -10;
3319 tocol = MAXCOL;
3320 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003321 else if (bot->col == MAXCOL)
3322 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 else
3324 {
3325 pos = *bot;
3326 if (*p_sel == 'e')
3327 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3328 else
3329 {
3330 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3331 ++tocol;
3332 }
3333 }
3334 }
3335 }
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 /* Check if the character under the cursor should not be inverted */
3338 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003339#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 )
3343 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 /* if inverting in this line set area_highlighting */
3346 if (fromcol >= 0)
3347 {
3348 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003349 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003351 if ((clip_star.available && !clip_star.owned
3352 && clip_isautosel_star())
3353 || (clip_plus.available && !clip_plus.owned
3354 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003355 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#endif
3357 }
3358 }
3359
3360 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003361 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003363 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && wp == curwin
3365 && lnum >= curwin->w_cursor.lnum
3366 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3367 {
3368 if (lnum == curwin->w_cursor.lnum)
3369 getvcol(curwin, &(curwin->w_cursor),
3370 (colnr_T *)&fromcol, NULL, NULL);
3371 else
3372 fromcol = 0;
3373 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3374 {
3375 pos.lnum = lnum;
3376 pos.col = search_match_endcol;
3377 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3378 }
3379 else
3380 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003381 /* do at least one character; happens when past end of line */
3382 if (fromcol == tocol)
3383 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003385 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387
3388#ifdef FEAT_DIFF
3389 filler_lines = diff_check(wp, lnum);
3390 if (filler_lines < 0)
3391 {
3392 if (filler_lines == -1)
3393 {
3394 if (diff_find_change(wp, lnum, &change_start, &change_end))
3395 diff_hlf = HLF_ADD; /* added line */
3396 else if (change_start == 0)
3397 diff_hlf = HLF_TXD; /* changed text */
3398 else
3399 diff_hlf = HLF_CHD; /* changed line */
3400 }
3401 else
3402 diff_hlf = HLF_ADD; /* added line */
3403 filler_lines = 0;
3404 area_highlighting = TRUE;
3405 }
3406 if (lnum == wp->w_topline)
3407 filler_lines = wp->w_topfill;
3408 filler_todo = filler_lines;
3409#endif
3410
3411#ifdef LINE_ATTR
3412# ifdef FEAT_SIGNS
3413 /* If this line has a sign with line highlighting set line_attr. */
3414 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3415 if (v != 0)
3416 line_attr = sign_get_attr((int)v, TRUE);
3417# endif
3418# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3419 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003420 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003421 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422# endif
3423 if (line_attr != 0)
3424 area_highlighting = TRUE;
3425#endif
3426
3427 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3428 ptr = line;
3429
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003430#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003431 if (has_spell)
3432 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003433 /* For checking first word with a capital skip white space. */
3434 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003435 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003436
Bram Moolenaar30abd282005-06-22 22:35:10 +00003437 /* To be able to spell-check over line boundaries copy the end of the
3438 * current line into nextline[]. Above the start of the next line was
3439 * copied to nextline[SPWORDLEN]. */
3440 if (nextline[SPWORDLEN] == NUL)
3441 {
3442 /* No next line or it is empty. */
3443 nextlinecol = MAXCOL;
3444 nextline_idx = 0;
3445 }
3446 else
3447 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003448 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003449 if (v < SPWORDLEN)
3450 {
3451 /* Short line, use it completely and append the start of the
3452 * next line. */
3453 nextlinecol = 0;
3454 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003455 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003456 nextline_idx = v + 1;
3457 }
3458 else
3459 {
3460 /* Long line, use only the last SPWORDLEN bytes. */
3461 nextlinecol = v - SPWORDLEN;
3462 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3463 nextline_idx = SPWORDLEN + 1;
3464 }
3465 }
3466 }
3467#endif
3468
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003469 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003471 if (lcs_space || lcs_trail)
3472 extra_check = TRUE;
3473 /* find start of trailing whitespace */
3474 if (lcs_trail)
3475 {
3476 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003477 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003478 --trailcol;
3479 trailcol += (colnr_T) (ptr - line);
3480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482
3483 /*
3484 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3485 * first character to be displayed.
3486 */
3487 if (wp->w_p_wrap)
3488 v = wp->w_skipcol;
3489 else
3490 v = wp->w_leftcol;
3491 if (v > 0)
3492 {
3493#ifdef FEAT_MBYTE
3494 char_u *prev_ptr = ptr;
3495#endif
3496 while (vcol < v && *ptr != NUL)
3497 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003498 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 vcol += c;
3500#ifdef FEAT_MBYTE
3501 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003503 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003506 /* When:
3507 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003508 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003509 * - 'virtualedit' is set, or
3510 * - the visual mode is active,
3511 * the end of the line may be before the start of the displayed part.
3512 */
3513 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003514#ifdef FEAT_SYN_HL
3515 wp->w_p_cuc || draw_color_col ||
3516#endif
3517#ifdef FEAT_VIRTUALEDIT
3518 virtual_active() ||
3519#endif
3520 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524
3525 /* Handle a character that's not completely on the screen: Put ptr at
3526 * that character but skip the first few screen characters. */
3527 if (vcol > v)
3528 {
3529 vcol -= c;
3530#ifdef FEAT_MBYTE
3531 ptr = prev_ptr;
3532#else
3533 --ptr;
3534#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003535 /* If the character fits on the screen, don't need to skip it.
3536 * Except for a TAB. */
3537 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003538#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003539 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003540#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003541 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003542 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544
3545 /*
3546 * Adjust for when the inverted text is before the screen,
3547 * and when the start of the inverted text is before the screen.
3548 */
3549 if (tocol <= vcol)
3550 fromcol = 0;
3551 else if (fromcol >= 0 && fromcol < vcol)
3552 fromcol = vcol;
3553
3554#ifdef FEAT_LINEBREAK
3555 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3556 if (wp->w_p_wrap)
3557 need_showbreak = TRUE;
3558#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003559#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003560 /* When spell checking a word we need to figure out the start of the
3561 * word and if it's badly spelled or not. */
3562 if (has_spell)
3563 {
3564 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003565 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003566 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003567
3568 pos = wp->w_cursor;
3569 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003570 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003571 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003572
3573 /* spell_move_to() may call ml_get() and make "line" invalid */
3574 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3575 ptr = line + linecol;
3576
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003577 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003578 {
3579 /* no bad word found at line start, don't check until end of a
3580 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003581 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003582 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003583 }
3584 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003585 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003586 /* bad word found, use attributes until end of word */
3587 word_end = wp->w_cursor.col + len + 1;
3588
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003589 /* Turn index into actual attributes. */
3590 if (spell_hlf != HLF_COUNT)
3591 spell_attr = highlight_attr[spell_hlf];
3592 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003593 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003594
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003595# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003596 /* Need to restart syntax highlighting for this line. */
3597 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003598 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003599# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003600 }
3601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
3604 /*
3605 * Correct highlighting for cursor that can't be disabled.
3606 * Avoids having to check this for each character.
3607 */
3608 if (fromcol >= 0)
3609 {
3610 if (noinvcur)
3611 {
3612 if ((colnr_T)fromcol == wp->w_virtcol)
3613 {
3614 /* highlighting starts at cursor, let it start just after the
3615 * cursor */
3616 fromcol_prev = fromcol;
3617 fromcol = -1;
3618 }
3619 else if ((colnr_T)fromcol < wp->w_virtcol)
3620 /* restart highlighting after the cursor */
3621 fromcol_prev = wp->w_virtcol;
3622 }
3623 if (fromcol >= tocol)
3624 fromcol = -1;
3625 }
3626
3627#ifdef FEAT_SEARCH_EXTRA
3628 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003629 * Handle highlighting the last used search pattern and matches.
3630 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003632 cur = wp->w_match_head;
3633 shl_flag = FALSE;
3634 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003636 if (shl_flag == FALSE)
3637 {
3638 shl = &search_hl;
3639 shl_flag = TRUE;
3640 }
3641 else
3642 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003643 shl->startcol = MAXCOL;
3644 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003646 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003647 v = (long)(ptr - line);
3648 if (cur != NULL)
3649 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003650 next_search_hl(wp, shl, lnum, (colnr_T)v,
3651 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003652
3653 /* Need to get the line again, a multi-line regexp may have made it
3654 * invalid. */
3655 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3656 ptr = line + v;
3657
3658 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003660 if (shl->lnum == lnum)
3661 shl->startcol = shl->rm.startpos[0].col;
3662 else
3663 shl->startcol = 0;
3664 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3665 - shl->rm.startpos[0].lnum)
3666 shl->endcol = shl->rm.endpos[0].col;
3667 else
3668 shl->endcol = MAXCOL;
3669 /* Highlight one character for an empty match. */
3670 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 if (has_mbyte && line[shl->endcol] != NUL)
3674 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003677 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003679 if ((long)shl->startcol < v) /* match at leftcol */
3680 {
3681 shl->attr_cur = shl->attr;
3682 search_attr = shl->attr;
3683 }
3684 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003686 if (shl != &search_hl && cur != NULL)
3687 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689#endif
3690
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003691#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003692 /* Cursor line highlighting for 'cursorline' in the current window. Not
3693 * when Visual mode is active, because it's not clear what is selected
3694 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003695 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003696 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003697 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003698 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003699 area_highlighting = TRUE;
3700 }
3701#endif
3702
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003703 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 col = 0;
3705#ifdef FEAT_RIGHTLEFT
3706 if (wp->w_p_rl)
3707 {
3708 /* Rightleft window: process the text in the normal direction, but put
3709 * it in current_ScreenLine[] from right to left. Start at the
3710 * rightmost column of the window. */
3711 col = W_WIDTH(wp) - 1;
3712 off += col;
3713 }
3714#endif
3715
3716 /*
3717 * Repeat for the whole displayed line.
3718 */
3719 for (;;)
3720 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003721#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003722 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /* Skip this quickly when working on the text. */
3725 if (draw_state != WL_LINE)
3726 {
3727#ifdef FEAT_CMDWIN
3728 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3729 {
3730 draw_state = WL_CMDLINE;
3731 if (cmdwin_type != 0 && wp == curwin)
3732 {
3733 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003735 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003736 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 }
3739#endif
3740
3741#ifdef FEAT_FOLDING
3742 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3743 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003744 int fdc = compute_foldcolumn(wp, 0);
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003747 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003749 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003750 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003751 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003752 p_extra_free = alloc(12 + 1);
3753
3754 if (p_extra_free != NULL)
3755 {
3756 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3757 n_extra = fdc;
3758 p_extra_free[n_extra] = NUL;
3759 p_extra = p_extra_free;
3760 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003761 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764 }
3765#endif
3766
3767#ifdef FEAT_SIGNS
3768 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3769 {
3770 draw_state = WL_SIGN;
3771 /* Show the sign column when there are any signs in this
3772 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003773 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003775 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003777 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778# endif
3779
3780 /* Draw two cells with the sign value or blank. */
3781 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003782 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 n_extra = 2;
3784
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003785 if (row == startrow
3786#ifdef FEAT_DIFF
3787 + filler_lines && filler_todo <= 0
3788#endif
3789 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
3791 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3792 SIGN_TEXT);
3793# ifdef FEAT_SIGN_ICONS
3794 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3795 SIGN_ICON);
3796 if (gui.in_use && icon_sign != 0)
3797 {
3798 /* Use the image in this position. */
3799 c_extra = SIGN_BYTE;
3800# ifdef FEAT_NETBEANS_INTG
3801 if (buf_signcount(wp->w_buffer, lnum) > 1)
3802 c_extra = MULTISIGN_BYTE;
3803# endif
3804 char_attr = icon_sign;
3805 }
3806 else
3807# endif
3808 if (text_sign != 0)
3809 {
3810 p_extra = sign_get_text(text_sign);
3811 if (p_extra != NULL)
3812 {
3813 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003814 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 char_attr = sign_get_attr(text_sign, FALSE);
3817 }
3818 }
3819 }
3820 }
3821#endif
3822
3823 if (draw_state == WL_NR - 1 && n_extra == 0)
3824 {
3825 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003826 /* Display the absolute or relative line number. After the
3827 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3828 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 && (row == startrow
3830#ifdef FEAT_DIFF
3831 + filler_lines
3832#endif
3833 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3834 {
3835 /* Draw the line number (empty space after wrapping). */
3836 if (row == startrow
3837#ifdef FEAT_DIFF
3838 + filler_lines
3839#endif
3840 )
3841 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003842 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003843 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003844
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003845 if (wp->w_p_nu && !wp->w_p_rnu)
3846 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003847 num = (long)lnum;
3848 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003849 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003850 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003851 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003852 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003853 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003854 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003855 num = lnum;
3856 fmt = "%-*ld ";
3857 }
3858 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003859
Bram Moolenaar700e7342013-01-30 12:31:36 +01003860 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003861 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (wp->w_skipcol > 0)
3863 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3864 *p_extra = '-';
3865#ifdef FEAT_RIGHTLEFT
3866 if (wp->w_p_rl) /* reverse line numbers */
3867 rl_mirror(extra);
3868#endif
3869 p_extra = extra;
3870 c_extra = NUL;
3871 }
3872 else
3873 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003874 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003875 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003876#ifdef FEAT_SYN_HL
3877 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003878 * the current line differently.
3879 * TODO: Can we use CursorLine instead of CursorLineNr
3880 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003881 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003883 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 }
3886 }
3887
Bram Moolenaar597a4222014-06-25 14:39:50 +02003888#ifdef FEAT_LINEBREAK
3889 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3890 && n_extra == 0 && *p_sbr != NUL)
3891 /* draw indent after showbreak value */
3892 draw_state = WL_BRI;
3893 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3894 /* After the showbreak, draw the breakindent */
3895 draw_state = WL_BRI - 1;
3896
3897 /* draw 'breakindent': indent wrapped text accordingly */
3898 if (draw_state == WL_BRI - 1 && n_extra == 0)
3899 {
3900 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003901 /* if need_showbreak is set, breakindent also applies */
3902 if (wp->w_p_bri && n_extra == 0
3903 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003904# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003905 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003906# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003907 )
3908 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003909 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003910# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003911 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003912 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003913 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003914# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003915 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3916 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003917 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003918# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003919 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003920# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003921 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003922 c_extra = ' ';
3923 n_extra = get_breakindent_win(wp,
3924 ml_get_buf(wp->w_buffer, lnum, FALSE));
3925 /* Correct end of highlighted area for 'breakindent',
3926 * required when 'linebreak' is also set. */
3927 if (tocol == vcol)
3928 tocol += n_extra;
3929 }
3930 }
3931#endif
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3934 if (draw_state == WL_SBR - 1 && n_extra == 0)
3935 {
3936 draw_state = WL_SBR;
3937# ifdef FEAT_DIFF
3938 if (filler_todo > 0)
3939 {
3940 /* Draw "deleted" diff line(s). */
3941 if (char2cells(fill_diff) > 1)
3942 c_extra = '-';
3943 else
3944 c_extra = fill_diff;
3945# ifdef FEAT_RIGHTLEFT
3946 if (wp->w_p_rl)
3947 n_extra = col + 1;
3948 else
3949# endif
3950 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003951 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953# endif
3954# ifdef FEAT_LINEBREAK
3955 if (*p_sbr != NUL && need_showbreak)
3956 {
3957 /* Draw 'showbreak' at the start of each broken line. */
3958 p_extra = p_sbr;
3959 c_extra = NUL;
3960 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003961 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003963 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 /* Correct end of highlighted area for 'showbreak',
3965 * required when 'linebreak' is also set. */
3966 if (tocol == vcol)
3967 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003968#ifdef FEAT_SYN_HL
3969 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003970 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003971 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003972 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975# endif
3976 }
3977#endif
3978
3979 if (draw_state == WL_LINE - 1 && n_extra == 0)
3980 {
3981 draw_state = WL_LINE;
3982 if (saved_n_extra)
3983 {
3984 /* Continue item from end of wrapped line. */
3985 n_extra = saved_n_extra;
3986 c_extra = saved_c_extra;
3987 p_extra = saved_p_extra;
3988 char_attr = saved_char_attr;
3989 }
3990 else
3991 char_attr = 0;
3992 }
3993 }
3994
3995 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003996 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003997 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998#ifdef FEAT_DIFF
3999 && filler_todo <= 0
4000#endif
4001 )
4002 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004003 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004005 /* Pretend we have finished updating the window. Except when
4006 * 'cursorcolumn' is set. */
4007#ifdef FEAT_SYN_HL
4008 if (wp->w_p_cuc)
4009 row = wp->w_cline_row + wp->w_cline_height;
4010 else
4011#endif
4012 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 break;
4014 }
4015
4016 if (draw_state == WL_LINE && area_highlighting)
4017 {
4018 /* handle Visual or match highlighting in this line */
4019 if (vcol == fromcol
4020#ifdef FEAT_MBYTE
4021 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4022 && (*mb_ptr2cells)(ptr) > 1)
4023#endif
4024 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004025 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 && vcol < tocol))
4027 area_attr = attr; /* start highlighting */
4028 else if (area_attr != 0
4029 && (vcol == tocol
4030 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033#ifdef FEAT_SEARCH_EXTRA
4034 if (!n_extra)
4035 {
4036 /*
4037 * Check for start/end of search pattern match.
4038 * After end, check for start/end of next match.
4039 * When another match, have to check for start again.
4040 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004041 * Do this for 'search_hl' and the match list (ordered by
4042 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004044 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004045 cur = wp->w_match_head;
4046 shl_flag = FALSE;
4047 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004049 if (shl_flag == FALSE
4050 && ((cur != NULL
4051 && cur->priority > SEARCH_HL_PRIORITY)
4052 || cur == NULL))
4053 {
4054 shl = &search_hl;
4055 shl_flag = TRUE;
4056 }
4057 else
4058 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004059 if (cur != NULL)
4060 cur->pos.cur = 0;
4061 pos_inprogress = TRUE;
4062 while (shl->rm.regprog != NULL
4063 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004065 if (shl->startcol != MAXCOL
4066 && v >= (long)shl->startcol
4067 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004069#ifdef FEAT_MBYTE
4070 int tmp_col = v + MB_PTR2LEN(ptr);
4071
4072 if (shl->endcol < tmp_col)
4073 shl->endcol = tmp_col;
4074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004076#ifdef FEAT_CONCEAL
4077 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4078 == cur->hlg_id)
4079 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004080 has_match_conc =
4081 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004082 match_conc = cur->conceal_char;
4083 }
4084 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004085 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004088 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
4090 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004091 next_search_hl(wp, shl, lnum, (colnr_T)v,
4092 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004093 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004094 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /* Need to get the line again, a multi-line regexp
4097 * may have made it invalid. */
4098 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4099 ptr = line + v;
4100
4101 if (shl->lnum == lnum)
4102 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004103 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004105 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004107 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004109 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
4111 /* highlight empty match, try again after
4112 * it */
4113#ifdef FEAT_MBYTE
4114 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004115 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004116 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 else
4118#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004119 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
4122 /* Loop to check if the match starts at the
4123 * current position */
4124 continue;
4125 }
4126 }
4127 break;
4128 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004129 if (shl != &search_hl && cur != NULL)
4130 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004132
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004133 /* Use attributes from match with highest priority among
4134 * 'search_hl' and the match list. */
4135 search_attr = search_hl.attr_cur;
4136 cur = wp->w_match_head;
4137 shl_flag = FALSE;
4138 while (cur != NULL || shl_flag == FALSE)
4139 {
4140 if (shl_flag == FALSE
4141 && ((cur != NULL
4142 && cur->priority > SEARCH_HL_PRIORITY)
4143 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004144 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004145 shl = &search_hl;
4146 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004147 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004148 else
4149 shl = &cur->hl;
4150 if (shl->attr_cur != 0)
4151 search_attr = shl->attr_cur;
4152 if (shl != &search_hl && cur != NULL)
4153 cur = cur->next;
4154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156#endif
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004159 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004161 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4162 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004164 if (diff_hlf == HLF_TXD && ptr - line > change_end
4165 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004167 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004168 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004169 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004172
4173 /* Decide which of the highlight attributes to use. */
4174 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004175#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004176 if (area_attr != 0)
4177 char_attr = hl_combine_attr(line_attr, area_attr);
4178 else if (search_attr != 0)
4179 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004180 /* Use line_attr when not in the Visual or 'incsearch' area
4181 * (area_attr may be 0 when "noinvcur" is set). */
4182 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004183 || vcol < fromcol || vcol_prev < fromcol_prev
4184 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004186#else
4187 if (area_attr != 0)
4188 char_attr = area_attr;
4189 else if (search_attr != 0)
4190 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004191#endif
4192 else
4193 {
4194 attr_pri = FALSE;
4195#ifdef FEAT_SYN_HL
4196 if (has_syntax)
4197 char_attr = syntax_attr;
4198 else
4199#endif
4200 char_attr = 0;
4201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203
4204 /*
4205 * Get the next character to put on the screen.
4206 */
4207 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004208 * The "p_extra" points to the extra stuff that is inserted to
4209 * represent special characters (non-printable stuff) and other
4210 * things. When all characters are the same, c_extra is used.
4211 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4212 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4214 */
4215 if (n_extra > 0)
4216 {
4217 if (c_extra != NUL)
4218 {
4219 c = c_extra;
4220#ifdef FEAT_MBYTE
4221 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004222 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004225 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004226 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 else
4229 mb_utf8 = FALSE;
4230#endif
4231 }
4232 else
4233 {
4234 c = *p_extra;
4235#ifdef FEAT_MBYTE
4236 if (has_mbyte)
4237 {
4238 mb_c = c;
4239 if (enc_utf8)
4240 {
4241 /* If the UTF-8 character is more than one byte:
4242 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004243 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 mb_utf8 = FALSE;
4245 if (mb_l > n_extra)
4246 mb_l = 1;
4247 else if (mb_l > 1)
4248 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004251 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 }
4254 else
4255 {
4256 /* if this is a DBCS character, put it in "mb_c" */
4257 mb_l = MB_BYTE2LEN(c);
4258 if (mb_l >= n_extra)
4259 mb_l = 1;
4260 else if (mb_l > 1)
4261 mb_c = (c << 8) + p_extra[1];
4262 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004263 if (mb_l == 0) /* at the NUL at end-of-line */
4264 mb_l = 1;
4265
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 /* If a double-width char doesn't fit display a '>' in the
4267 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004268 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269# ifdef FEAT_RIGHTLEFT
4270 wp->w_p_rl ? (col <= 0) :
4271# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004272 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 && (*mb_char2cells)(mb_c) == 2)
4274 {
4275 c = '>';
4276 mb_c = c;
4277 mb_l = 1;
4278 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004279 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* put the pointer back to output the double-width
4281 * character at the start of the next line. */
4282 ++n_extra;
4283 --p_extra;
4284 }
4285 else
4286 {
4287 n_extra -= mb_l - 1;
4288 p_extra += mb_l - 1;
4289 }
4290 }
4291#endif
4292 ++p_extra;
4293 }
4294 --n_extra;
4295 }
4296 else
4297 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004298#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004299 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004300#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004301
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004302 if (p_extra_free != NULL)
4303 {
4304 vim_free(p_extra_free);
4305 p_extra_free = NULL;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 /*
4308 * Get a character from the line itself.
4309 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004310 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004311#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004312 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314#ifdef FEAT_MBYTE
4315 if (has_mbyte)
4316 {
4317 mb_c = c;
4318 if (enc_utf8)
4319 {
4320 /* If the UTF-8 character is more than one byte: Decode it
4321 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004322 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 mb_utf8 = FALSE;
4324 if (mb_l > 1)
4325 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004326 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 /* Overlong encoded ASCII or ASCII with composing char
4328 * is displayed normally, except a NUL. */
4329 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004330 {
4331 c = mb_c;
4332# ifdef FEAT_LINEBREAK
4333 c0 = mb_c;
4334# endif
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004337
4338 /* At start of the line we can have a composing char.
4339 * Draw it as a space with a composing char. */
4340 if (utf_iscomposing(mb_c))
4341 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004342 int i;
4343
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004344 for (i = Screen_mco - 1; i > 0; --i)
4345 u8cc[i] = u8cc[i - 1];
4346 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004347 mb_c = ' ';
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350
4351 if ((mb_l == 1 && c >= 0x80)
4352 || (mb_l >= 1 && mb_c == 0)
4353 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004354# ifdef UNICODE16
4355 || mb_c >= 0x10000
4356# endif
4357 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 /*
4360 * Illegal UTF-8 byte: display as <xx>.
4361 * Non-BMP character : display as ? or fullwidth ?.
4362 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004363# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
4367 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004368# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (wp->w_p_rl) /* reverse */
4370 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004371# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004373# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 else if (utf_char2cells(mb_c) != 2)
4375 STRCPY(extra, "?");
4376 else
4377 /* 0xff1f in UTF-8: full-width '?' */
4378 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 p_extra = extra;
4382 c = *p_extra;
4383 mb_c = mb_ptr2char_adv(&p_extra);
4384 mb_utf8 = (c >= 0x80);
4385 n_extra = (int)STRLEN(p_extra);
4386 c_extra = NUL;
4387 if (area_attr == 0 && search_attr == 0)
4388 {
4389 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004390 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 saved_attr2 = char_attr; /* save current attr */
4392 }
4393 }
4394 else if (mb_l == 0) /* at the NUL at end-of-line */
4395 mb_l = 1;
4396#ifdef FEAT_ARABIC
4397 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4398 {
4399 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004400 int pc, pc1, nc;
4401 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 /* The idea of what is the previous and next
4404 * character depends on 'rightleft'. */
4405 if (wp->w_p_rl)
4406 {
4407 pc = prev_c;
4408 pc1 = prev_c1;
4409 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 else
4413 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004416 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 prev_c = mb_c;
4419
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004420 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 else
4423 prev_c = mb_c;
4424#endif
4425 }
4426 else /* enc_dbcs */
4427 {
4428 mb_l = MB_BYTE2LEN(c);
4429 if (mb_l == 0) /* at the NUL at end-of-line */
4430 mb_l = 1;
4431 else if (mb_l > 1)
4432 {
4433 /* We assume a second byte below 32 is illegal.
4434 * Hopefully this is OK for all double-byte encodings!
4435 */
4436 if (ptr[1] >= 32)
4437 mb_c = (c << 8) + ptr[1];
4438 else
4439 {
4440 if (ptr[1] == NUL)
4441 {
4442 /* head byte at end of line */
4443 mb_l = 1;
4444 transchar_nonprint(extra, c);
4445 }
4446 else
4447 {
4448 /* illegal tail byte */
4449 mb_l = 2;
4450 STRCPY(extra, "XX");
4451 }
4452 p_extra = extra;
4453 n_extra = (int)STRLEN(extra) - 1;
4454 c_extra = NUL;
4455 c = *p_extra++;
4456 if (area_attr == 0 && search_attr == 0)
4457 {
4458 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004459 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 saved_attr2 = char_attr; /* save current attr */
4461 }
4462 mb_c = c;
4463 }
4464 }
4465 }
4466 /* If a double-width char doesn't fit display a '>' in the
4467 * last column; the character is displayed at the start of the
4468 * next line. */
4469 if ((
4470# ifdef FEAT_RIGHTLEFT
4471 wp->w_p_rl ? (col <= 0) :
4472# endif
4473 (col >= W_WIDTH(wp) - 1))
4474 && (*mb_char2cells)(mb_c) == 2)
4475 {
4476 c = '>';
4477 mb_c = c;
4478 mb_utf8 = FALSE;
4479 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004480 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /* Put pointer back so that the character will be
4482 * displayed at the start of the next line. */
4483 --ptr;
4484 }
4485 else if (*ptr != NUL)
4486 ptr += mb_l - 1;
4487
4488 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004489 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004490 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004491 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004494 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 c = ' ';
4496 if (area_attr == 0 && search_attr == 0)
4497 {
4498 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 saved_attr2 = char_attr; /* save current attr */
4501 }
4502 mb_c = c;
4503 mb_utf8 = FALSE;
4504 mb_l = 1;
4505 }
4506
4507 }
4508#endif
4509 ++ptr;
4510
4511 if (extra_check)
4512 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004513#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004514 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004515#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004516
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004517#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 /* Get syntax attribute, unless still at the start of the line
4519 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004520 v = (long)(ptr - line);
4521 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 /* Get the syntax attribute for the character. If there
4524 * is an error, disable syntax highlighting. */
4525 save_did_emsg = did_emsg;
4526 did_emsg = FALSE;
4527
Bram Moolenaar217ad922005-03-20 22:37:15 +00004528 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004529# ifdef FEAT_SPELL
4530 has_spell ? &can_spell :
4531# endif
4532 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004535 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004536 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004537 has_syntax = FALSE;
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
4540 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004541#ifdef SYN_TIME_LIMIT
4542 if (wp->w_s->b_syn_slow)
4543 has_syntax = FALSE;
4544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 /* Need to get the line again, a multi-line regexp may
4547 * have made it invalid. */
4548 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4549 ptr = line + v;
4550
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004551 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004553 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004554 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004555# ifdef FEAT_CONCEAL
4556 /* no concealing past the end of the line, it interferes
4557 * with line highlighting */
4558 if (c == NUL)
4559 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004560 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004561 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004564#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004565
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004566#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004567 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004568 * Only do this when there is no syntax highlighting, the
4569 * @Spell cluster is not used or the current syntax item
4570 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004571 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004572 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004573 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004574# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004575 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004576 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004577# endif
4578 if (c != 0 && (
4579# ifdef FEAT_SYN_HL
4580 !has_syntax ||
4581# endif
4582 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004583 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004584 char_u *prev_ptr, *p;
4585 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004586 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004587# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004588 if (has_mbyte)
4589 {
4590 prev_ptr = ptr - mb_l;
4591 v -= mb_l - 1;
4592 }
4593 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004594# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004595 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004596
4597 /* Use nextline[] if possible, it has the start of the
4598 * next line concatenated. */
4599 if ((prev_ptr - line) - nextlinecol >= 0)
4600 p = nextline + (prev_ptr - line) - nextlinecol;
4601 else
4602 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004603 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004604 len = spell_check(wp, p, &spell_hlf, &cap_col,
4605 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004606 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004608 /* In Insert mode only highlight a word that
4609 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004610 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004611 && (State & INSERT) != 0
4612 && wp->w_cursor.lnum == lnum
4613 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004615 && wp->w_cursor.col < (colnr_T)word_end)
4616 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004618 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004620
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004621 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004622 && (p - nextline) + len > nextline_idx)
4623 {
4624 /* Remember that the good word continues at the
4625 * start of the next line. */
4626 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004627 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004628 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004629
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004630 /* Turn index into actual attributes. */
4631 if (spell_hlf != HLF_COUNT)
4632 spell_attr = highlight_attr[spell_hlf];
4633
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004634 if (cap_col > 0)
4635 {
4636 if (p != prev_ptr
4637 && (p - nextline) + cap_col >= nextline_idx)
4638 {
4639 /* Remember that the word in the next line
4640 * must start with a capital. */
4641 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004642 cap_col = (int)((p - nextline) + cap_col
4643 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004644 }
4645 else
4646 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004647 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004648 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004649 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 }
4651 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004652 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004653 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004654 char_attr = hl_combine_attr(char_attr, spell_attr);
4655 else
4656 char_attr = hl_combine_attr(spell_attr, char_attr);
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659#ifdef FEAT_LINEBREAK
4660 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004661 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004663 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004664 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004666# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004667 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004668# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004669 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004671 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004673 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004674
Bram Moolenaar597a4222014-06-25 14:39:50 +02004675 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004676 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004677 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004678 if (c == TAB && n_extra + col > W_WIDTH(wp))
4679 n_extra = (int)wp->w_buffer->b_p_ts
4680 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4681
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004682# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004683 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004684# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004686# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004687 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004688 {
4689#ifdef FEAT_CONCEAL
4690 if (c == TAB)
4691 /* See "Tab alignment" below. */
4692 FIX_FOR_BOGUSCOLS;
4693#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004694 if (!wp->w_p_list)
4695 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698#endif
4699
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004700 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4701 */
4702 if (wp->w_p_list
4703 && (((c == 160
4704#ifdef FEAT_MBYTE
4705 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4706#endif
4707 ) && lcs_nbsp)
4708 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4709 {
4710 c = (c == ' ') ? lcs_space : lcs_nbsp;
4711 if (area_attr == 0 && search_attr == 0)
4712 {
4713 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004714 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004715 saved_attr2 = char_attr; /* save current attr */
4716 }
4717#ifdef FEAT_MBYTE
4718 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004719 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004720 {
4721 mb_utf8 = TRUE;
4722 u8cc[0] = 0;
4723 c = 0xc0;
4724 }
4725 else
4726 mb_utf8 = FALSE;
4727#endif
4728 }
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4731 {
4732 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004733 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004736 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 saved_attr2 = char_attr; /* save current attr */
4738 }
4739#ifdef FEAT_MBYTE
4740 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004741 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004744 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004745 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747 else
4748 mb_utf8 = FALSE;
4749#endif
4750 }
4751 }
4752
4753 /*
4754 * Handling of non-printable characters.
4755 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004756 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /*
4759 * when getting a character from the file, we may have to
4760 * turn it into something else on the way to putting it
4761 * into "ScreenLines".
4762 */
4763 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4764 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004765 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004766 long vcol_adjusted = vcol; /* removed showbreak length */
4767#ifdef FEAT_LINEBREAK
4768 /* only adjust the tab_len, when at the first column
4769 * after the showbreak value was drawn */
4770 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4771 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004774 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004775 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4776
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004777#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004778 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004779#endif
4780 /* tab amount depends on current column */
4781 n_extra = tab_len;
4782#ifdef FEAT_LINEBREAK
4783 else
4784 {
4785 char_u *p;
4786 int len = n_extra;
4787 int i;
4788 int saved_nextra = n_extra;
4789
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004790#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004791 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004792 /* there are characters to conceal */
4793 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004794 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4795 */
4796 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4797 && n_extra > tab_len)
4798 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004799#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004800
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004801 /* if n_extra > 0, it gives the number of chars, to
4802 * use for a tab, else we need to calculate the width
4803 * for a tab */
4804#ifdef FEAT_MBYTE
4805 len = (tab_len * mb_char2len(lcs_tab2));
4806 if (n_extra > 0)
4807 len += n_extra - tab_len;
4808#endif
4809 c = lcs_tab1;
4810 p = alloc((unsigned)(len + 1));
4811 vim_memset(p, ' ', len);
4812 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004813 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004814 p_extra_free = p;
4815 for (i = 0; i < tab_len; i++)
4816 {
4817#ifdef FEAT_MBYTE
4818 mb_char2bytes(lcs_tab2, p);
4819 p += mb_char2len(lcs_tab2);
4820 n_extra += mb_char2len(lcs_tab2)
4821 - (saved_nextra > 0 ? 1 : 0);
4822#else
4823 p[i] = lcs_tab2;
4824#endif
4825 }
4826 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004827#ifdef FEAT_CONCEAL
4828 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4829 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004830 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004831 n_extra -= vcol_off;
4832#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004833 }
4834#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004835#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004836 {
4837 int vc_saved = vcol_off;
4838
4839 /* Tab alignment should be identical regardless of
4840 * 'conceallevel' value. So tab compensates of all
4841 * previous concealed characters, and thus resets
4842 * vcol_off and boguscols accumulated so far in the
4843 * line. Note that the tab can be longer than
4844 * 'tabstop' when there are concealed characters. */
4845 FIX_FOR_BOGUSCOLS;
4846
4847 /* Make sure, the highlighting for the tab char will be
4848 * correctly set further below (effectively reverts the
4849 * FIX_FOR_BOGSUCOLS macro */
4850 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004851 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004852 tab_len += vc_saved;
4853 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#ifdef FEAT_MBYTE
4856 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4857#endif
4858 if (wp->w_p_list)
4859 {
4860 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004861#ifdef FEAT_LINEBREAK
4862 if (wp->w_p_lbr)
4863 c_extra = NUL; /* using p_extra from above */
4864 else
4865#endif
4866 c_extra = lcs_tab2;
4867 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004868 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 saved_attr2 = char_attr; /* save current attr */
4870#ifdef FEAT_MBYTE
4871 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004872 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004875 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878#endif
4879 }
4880 else
4881 {
4882 c_extra = ' ';
4883 c = ' ';
4884 }
4885 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004886 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004887 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004888 || ((fromcol >= 0 || fromcol_prev >= 0)
4889 && tocol > vcol
4890 && VIsual_mode != Ctrl_V
4891 && (
4892# ifdef FEAT_RIGHTLEFT
4893 wp->w_p_rl ? (col >= 0) :
4894# endif
4895 (col < W_WIDTH(wp)))
4896 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004897 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004898 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004899 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004901 /* Display a '$' after the line or highlight an extra
4902 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4904 /* For a diff line the highlighting continues after the
4905 * "$". */
4906 if (
4907# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004908 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909# ifdef LINE_ATTR
4910 &&
4911# endif
4912# endif
4913# ifdef LINE_ATTR
4914 line_attr == 0
4915# endif
4916 )
4917#endif
4918 {
4919#ifdef FEAT_VIRTUALEDIT
4920 /* In virtualedit, visual selections may extend
4921 * beyond end of line. */
4922 if (area_highlighting && virtual_active()
4923 && tocol != MAXCOL && vcol < tocol)
4924 n_extra = 0;
4925 else
4926#endif
4927 {
4928 p_extra = at_end_str;
4929 n_extra = 1;
4930 c_extra = NUL;
4931 }
4932 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004933 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004934 c = lcs_eol;
4935 else
4936 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 lcs_eol_one = -1;
4938 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004939 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004941 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 n_attr = 1;
4943 }
4944#ifdef FEAT_MBYTE
4945 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004946 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004949 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004950 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
4953 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4954#endif
4955 }
4956 else if (c != NUL)
4957 {
4958 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004959 if (n_extra == 0)
4960 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#ifdef FEAT_RIGHTLEFT
4962 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4963 rl_mirror(p_extra); /* reverse "<12>" */
4964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004966#ifdef FEAT_LINEBREAK
4967 if (wp->w_p_lbr)
4968 {
4969 char_u *p;
4970
4971 c = *p_extra;
4972 p = alloc((unsigned)n_extra + 1);
4973 vim_memset(p, ' ', n_extra);
4974 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4975 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004976 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004977 p_extra_free = p_extra = p;
4978 }
4979 else
4980#endif
4981 {
4982 n_extra = byte2cells(c) - 1;
4983 c = *p_extra++;
4984 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004985 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 {
4987 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004988 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 saved_attr2 = char_attr; /* save current attr */
4990 }
4991#ifdef FEAT_MBYTE
4992 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4993#endif
4994 }
4995#ifdef FEAT_VIRTUALEDIT
4996 else if (VIsual_active
4997 && (VIsual_mode == Ctrl_V
4998 || VIsual_mode == 'v')
4999 && virtual_active()
5000 && tocol != MAXCOL
5001 && vcol < tocol
5002 && (
5003# ifdef FEAT_RIGHTLEFT
5004 wp->w_p_rl ? (col >= 0) :
5005# endif
5006 (col < W_WIDTH(wp))))
5007 {
5008 c = ' ';
5009 --ptr; /* put it back at the NUL */
5010 }
5011#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005012#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 else if ((
5014# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005015 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 ) && (
5019# ifdef FEAT_RIGHTLEFT
5020 wp->w_p_rl ? (col >= 0) :
5021# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005022 (col
5023# ifdef FEAT_CONCEAL
5024 - boguscols
5025# endif
5026 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 /* Highlight until the right side of the window */
5029 c = ' ';
5030 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005031
5032 /* Remember we do the char for line highlighting. */
5033 ++did_line_attr;
5034
5035 /* don't do search HL for the rest of the line */
5036 if (line_attr != 0 && char_attr == search_attr && col > 0)
5037 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038# ifdef FEAT_DIFF
5039 if (diff_hlf == HLF_TXD)
5040 {
5041 diff_hlf = HLF_CHD;
5042 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005043 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005044 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005045 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5046 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005047 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050# endif
5051 }
5052#endif
5053 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005054
5055#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005056 if ( wp->w_p_cole > 0
5057 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005058 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005059 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005060 && !(lnum_in_visual_area
5061 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005062 {
5063 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005064 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005065 && (syn_get_sub_char() != NUL || match_conc
5066 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005067 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005068 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005069 /* First time at this concealed item: display one
5070 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005071 if (match_conc)
5072 c = match_conc;
5073 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005074 c = syn_get_sub_char();
5075 else if (lcs_conceal != NUL)
5076 c = lcs_conceal;
5077 else
5078 c = ' ';
5079
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005080 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005081
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005082 if (n_extra > 0)
5083 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005084 vcol += n_extra;
5085 if (wp->w_p_wrap && n_extra > 0)
5086 {
5087# ifdef FEAT_RIGHTLEFT
5088 if (wp->w_p_rl)
5089 {
5090 col -= n_extra;
5091 boguscols -= n_extra;
5092 }
5093 else
5094# endif
5095 {
5096 boguscols += n_extra;
5097 col += n_extra;
5098 }
5099 }
5100 n_extra = 0;
5101 n_attr = 0;
5102 }
5103 else if (n_skip == 0)
5104 {
5105 is_concealing = TRUE;
5106 n_skip = 1;
5107 }
5108# ifdef FEAT_MBYTE
5109 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005110 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 {
5112 mb_utf8 = TRUE;
5113 u8cc[0] = 0;
5114 c = 0xc0;
5115 }
5116 else
5117 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5118# endif
5119 }
5120 else
5121 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005122 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005123 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124 }
5125#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005128#ifdef FEAT_CONCEAL
5129 /* In the cursor line and we may be concealing characters: correct
5130 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005131 if (!did_wcol && draw_state == WL_LINE
5132 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005133 && conceal_cursor_line(wp)
5134 && (int)wp->w_virtcol <= vcol + n_skip)
5135 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005136# ifdef FEAT_RIGHTLEFT
5137 if (wp->w_p_rl)
5138 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5139 else
5140# endif
5141 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005142 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005143 did_wcol = TRUE;
5144 }
5145#endif
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 /* Don't override visual selection highlighting. */
5148 if (n_attr > 0
5149 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005150 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 char_attr = extra_attr;
5152
Bram Moolenaar81695252004-12-29 20:58:21 +00005153#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 /* XIM don't send preedit_start and preedit_end, but they send
5155 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5156 * im_is_preediting() here. */
5157 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005158 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 && (State & INSERT)
5160 && !p_imdisable
5161 && im_is_preediting()
5162 && draw_state == WL_LINE)
5163 {
5164 colnr_T tcol;
5165
5166 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005167 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 else
5169 tcol = preedit_end_col;
5170 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5171 {
5172 if (feedback_old_attr < 0)
5173 {
5174 feedback_col = 0;
5175 feedback_old_attr = char_attr;
5176 }
5177 char_attr = im_get_feedback_attr(feedback_col);
5178 if (char_attr < 0)
5179 char_attr = feedback_old_attr;
5180 feedback_col++;
5181 }
5182 else if (feedback_old_attr >= 0)
5183 {
5184 char_attr = feedback_old_attr;
5185 feedback_old_attr = -1;
5186 feedback_col = 0;
5187 }
5188 }
5189#endif
5190 /*
5191 * Handle the case where we are in column 0 but not on the first
5192 * character of the line and the user wants us to show us a
5193 * special character (via 'listchars' option "precedes:<char>".
5194 */
5195 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005196 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5198#ifdef FEAT_DIFF
5199 && filler_todo <= 0
5200#endif
5201 && draw_state > WL_NR
5202 && c != NUL)
5203 {
5204 c = lcs_prec;
5205 lcs_prec_todo = NUL;
5206#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005207 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5208 {
5209 /* Double-width character being overwritten by the "precedes"
5210 * character, need to fill up half the character. */
5211 c_extra = MB_FILLER_CHAR;
5212 n_extra = 1;
5213 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005214 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005217 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005220 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005221 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223 else
5224 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5225#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005226 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005229 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 n_attr3 = 1;
5231 }
5232 }
5233
5234 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005235 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005237 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005238#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005239 || did_line_attr == 1
5240#endif
5241 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005243#ifdef FEAT_SEARCH_EXTRA
5244 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005245
5246 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005247 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005248 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005249#endif
5250
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005251 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 * highlight match at end of line. If it's beyond the last
5253 * char on the screen, just overwrite that one (tricky!) Not
5254 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005255#ifdef FEAT_SEARCH_EXTRA
5256 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005257 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005258 prevcol_hl_flag = TRUE;
5259 else
5260 {
5261 cur = wp->w_match_head;
5262 while (cur != NULL)
5263 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005264 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005265 {
5266 prevcol_hl_flag = TRUE;
5267 break;
5268 }
5269 cur = cur->next;
5270 }
5271 }
5272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005274 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005275 && (VIsual_mode != Ctrl_V
5276 || lnum == VIsual.lnum
5277 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005278 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#ifdef FEAT_SEARCH_EXTRA
5280 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005281 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005282# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005283 && did_line_attr <= 1
5284# endif
5285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286#endif
5287 ))
5288 {
5289 int n = 0;
5290
5291#ifdef FEAT_RIGHTLEFT
5292 if (wp->w_p_rl)
5293 {
5294 if (col < 0)
5295 n = 1;
5296 }
5297 else
5298#endif
5299 {
5300 if (col >= W_WIDTH(wp))
5301 n = -1;
5302 }
5303 if (n != 0)
5304 {
5305 /* At the window boundary, highlight the last character
5306 * instead (better than nothing). */
5307 off += n;
5308 col += n;
5309 }
5310 else
5311 {
5312 /* Add a blank character to highlight. */
5313 ScreenLines[off] = ' ';
5314#ifdef FEAT_MBYTE
5315 if (enc_utf8)
5316 ScreenLinesUC[off] = 0;
5317#endif
5318 }
5319#ifdef FEAT_SEARCH_EXTRA
5320 if (area_attr == 0)
5321 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005322 /* Use attributes from match with highest priority among
5323 * 'search_hl' and the match list. */
5324 char_attr = search_hl.attr;
5325 cur = wp->w_match_head;
5326 shl_flag = FALSE;
5327 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005328 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005329 if (shl_flag == FALSE
5330 && ((cur != NULL
5331 && cur->priority > SEARCH_HL_PRIORITY)
5332 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005333 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005334 shl = &search_hl;
5335 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005336 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005337 else
5338 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005339 if ((ptr - line) - 1 == (long)shl->startcol
5340 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005341 char_attr = shl->attr;
5342 if (shl != &search_hl && cur != NULL)
5343 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346#endif
5347 ScreenAttrs[off] = char_attr;
5348#ifdef FEAT_RIGHTLEFT
5349 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005352 --off;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else
5355#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005358 ++off;
5359 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005360 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005361#ifdef FEAT_SYN_HL
5362 eol_hl_off = 1;
5363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaar91170f82006-05-05 21:15:17 +00005367 /*
5368 * At end of the text line.
5369 */
5370 if (c == NUL)
5371 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005372#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005373 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5374 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005375 {
5376 /* highlight last char after line */
5377 --col;
5378 --off;
5379 --vcol;
5380 }
5381
Bram Moolenaar1a384422010-07-14 19:53:30 +02005382 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005383 if (wp->w_p_wrap)
5384 v = wp->w_skipcol;
5385 else
5386 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005387
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005388 /* check if line ends before left margin */
5389 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005390 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005391#ifdef FEAT_CONCEAL
5392 /* Get rid of the boguscols now, we want to draw until the right
5393 * edge for 'cursorcolumn'. */
5394 col -= boguscols;
5395 boguscols = 0;
5396#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005397
5398 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005399 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005400
5401 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005402 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5403 && (int)wp->w_virtcol <
5404 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005405 && lnum != wp->w_cursor.lnum)
5406 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005407# ifdef FEAT_RIGHTLEFT
5408 && !wp->w_p_rl
5409# endif
5410 )
5411 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005412 int rightmost_vcol = 0;
5413 int i;
5414
5415 if (wp->w_p_cuc)
5416 rightmost_vcol = wp->w_virtcol;
5417 if (draw_color_col)
5418 /* determine rightmost colorcolumn to possibly draw */
5419 for (i = 0; color_cols[i] >= 0; ++i)
5420 if (rightmost_vcol < color_cols[i])
5421 rightmost_vcol = color_cols[i];
5422
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005423 while (col < W_WIDTH(wp))
5424 {
5425 ScreenLines[off] = ' ';
5426#ifdef FEAT_MBYTE
5427 if (enc_utf8)
5428 ScreenLinesUC[off] = 0;
5429#endif
5430 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005431 if (draw_color_col)
5432 draw_color_col = advance_color_col(VCOL_HLC,
5433 &color_cols);
5434
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005435 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005436 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005437 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005438 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005439 else
5440 ScreenAttrs[off++] = 0;
5441
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005442 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005443 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005444
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005445 ++vcol;
5446 }
5447 }
5448#endif
5449
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005450 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005451 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 row++;
5453
5454 /*
5455 * Update w_cline_height and w_cline_folded if the cursor line was
5456 * updated (saves a call to plines() later).
5457 */
5458 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5459 {
5460 curwin->w_cline_row = startrow;
5461 curwin->w_cline_height = row - startrow;
5462#ifdef FEAT_FOLDING
5463 curwin->w_cline_folded = FALSE;
5464#endif
5465 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5466 }
5467
5468 break;
5469 }
5470
5471 /* line continues beyond line end */
5472 if (lcs_ext
5473 && !wp->w_p_wrap
5474#ifdef FEAT_DIFF
5475 && filler_todo <= 0
5476#endif
5477 && (
5478#ifdef FEAT_RIGHTLEFT
5479 wp->w_p_rl ? col == 0 :
5480#endif
5481 col == W_WIDTH(wp) - 1)
5482 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005483 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5485 {
5486 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005487 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488#ifdef FEAT_MBYTE
5489 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005490 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005493 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005494 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496 else
5497 mb_utf8 = FALSE;
5498#endif
5499 }
5500
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005501#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005502 /* advance to the next 'colorcolumn' */
5503 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005504 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005506 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005507 * highlight the cursor position itself.
5508 * Also highlight the 'colorcolumn' if it is different than
5509 * 'cursorcolumn' */
5510 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005511 if (draw_state == WL_LINE && !lnum_in_visual_area
5512 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005513 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005514 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 && lnum != wp->w_cursor.lnum)
5516 {
5517 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005518 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005519 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005520 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005521 {
5522 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005523 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005524 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005525 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526#endif
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 /*
5529 * Store character to be displayed.
5530 * Skip characters that are left of the screen for 'nowrap'.
5531 */
5532 vcol_prev = vcol;
5533 if (draw_state < WL_LINE || n_skip <= 0)
5534 {
5535 /*
5536 * Store the character.
5537 */
5538#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5539 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5540 {
5541 /* A double-wide character is: put first halve in left cell. */
5542 --off;
5543 --col;
5544 }
5545#endif
5546 ScreenLines[off] = c;
5547#ifdef FEAT_MBYTE
5548 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005549 {
5550 if ((mb_c & 0xff00) == 0x8e00)
5551 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 else if (enc_utf8)
5555 {
5556 if (mb_utf8)
5557 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005558 int i;
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005561 if ((c & 0xff) == 0)
5562 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005563 for (i = 0; i < Screen_mco; ++i)
5564 {
5565 ScreenLinesC[i][off] = u8cc[i];
5566 if (u8cc[i] == 0)
5567 break;
5568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else
5571 ScreenLinesUC[off] = 0;
5572 }
5573 if (multi_attr)
5574 {
5575 ScreenAttrs[off] = multi_attr;
5576 multi_attr = 0;
5577 }
5578 else
5579#endif
5580 ScreenAttrs[off] = char_attr;
5581
5582#ifdef FEAT_MBYTE
5583 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5584 {
5585 /* Need to fill two screen columns. */
5586 ++off;
5587 ++col;
5588 if (enc_utf8)
5589 /* UTF-8: Put a 0 in the second screen char. */
5590 ScreenLines[off] = 0;
5591 else
5592 /* DBCS: Put second byte in the second screen char. */
5593 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005594 if (draw_state > WL_NR
5595#ifdef FEAT_DIFF
5596 && filler_todo <= 0
5597#endif
5598 )
5599 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 /* When "tocol" is halfway a character, set it to the end of
5601 * the character, otherwise highlighting won't stop. */
5602 if (tocol == vcol)
5603 ++tocol;
5604#ifdef FEAT_RIGHTLEFT
5605 if (wp->w_p_rl)
5606 {
5607 /* now it's time to backup one cell */
5608 --off;
5609 --col;
5610 }
5611#endif
5612 }
5613#endif
5614#ifdef FEAT_RIGHTLEFT
5615 if (wp->w_p_rl)
5616 {
5617 --off;
5618 --col;
5619 }
5620 else
5621#endif
5622 {
5623 ++off;
5624 ++col;
5625 }
5626 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005627#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005628 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005629 {
5630 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005631 ++vcol_off;
5632 if (n_extra > 0)
5633 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005634 if (wp->w_p_wrap)
5635 {
5636 /*
5637 * Special voodoo required if 'wrap' is on.
5638 *
5639 * Advance the column indicator to force the line
5640 * drawing to wrap early. This will make the line
5641 * take up the same screen space when parts are concealed,
5642 * so that cursor line computations aren't messed up.
5643 *
5644 * To avoid the fictitious advance of 'col' causing
5645 * trailing junk to be written out of the screen line
5646 * we are building, 'boguscols' keeps track of the number
5647 * of bad columns we have advanced.
5648 */
5649 if (n_extra > 0)
5650 {
5651 vcol += n_extra;
5652# ifdef FEAT_RIGHTLEFT
5653 if (wp->w_p_rl)
5654 {
5655 col -= n_extra;
5656 boguscols -= n_extra;
5657 }
5658 else
5659# endif
5660 {
5661 col += n_extra;
5662 boguscols += n_extra;
5663 }
5664 n_extra = 0;
5665 n_attr = 0;
5666 }
5667
5668
5669# ifdef FEAT_MBYTE
5670 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5671 {
5672 /* Need to fill two screen columns. */
5673# ifdef FEAT_RIGHTLEFT
5674 if (wp->w_p_rl)
5675 {
5676 --boguscols;
5677 --col;
5678 }
5679 else
5680# endif
5681 {
5682 ++boguscols;
5683 ++col;
5684 }
5685 }
5686# endif
5687
5688# ifdef FEAT_RIGHTLEFT
5689 if (wp->w_p_rl)
5690 {
5691 --boguscols;
5692 --col;
5693 }
5694 else
5695# endif
5696 {
5697 ++boguscols;
5698 ++col;
5699 }
5700 }
5701 else
5702 {
5703 if (n_extra > 0)
5704 {
5705 vcol += n_extra;
5706 n_extra = 0;
5707 n_attr = 0;
5708 }
5709 }
5710
5711 }
5712#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
5714 --n_skip;
5715
Bram Moolenaar64486672010-05-16 15:46:46 +02005716 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5717 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005718 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#ifdef FEAT_DIFF
5720 && filler_todo <= 0
5721#endif
5722 )
5723 ++vcol;
5724
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005725#ifdef FEAT_SYN_HL
5726 if (vcol_save_attr >= 0)
5727 char_attr = vcol_save_attr;
5728#endif
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 /* restore attributes after "predeces" in 'listchars' */
5731 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5732 char_attr = saved_attr3;
5733
5734 /* restore attributes after last 'listchars' or 'number' char */
5735 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5736 char_attr = saved_attr2;
5737
5738 /*
5739 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005740 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 */
5742 if ((
5743#ifdef FEAT_RIGHTLEFT
5744 wp->w_p_rl ? (col < 0) :
5745#endif
5746 (col >= W_WIDTH(wp)))
5747 && (*ptr != NUL
5748#ifdef FEAT_DIFF
5749 || filler_todo > 0
5750#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005751 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5753 )
5754 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005755#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005756 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005757 (int)W_WIDTH(wp), wp->w_p_rl);
5758 boguscols = 0;
5759#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005760 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005761 (int)W_WIDTH(wp), wp->w_p_rl);
5762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 ++row;
5764 ++screen_row;
5765
5766 /* When not wrapping and finished diff lines, or when displayed
5767 * '$' and highlighting until last column, break here. */
5768 if ((!wp->w_p_wrap
5769#ifdef FEAT_DIFF
5770 && filler_todo <= 0
5771#endif
5772 ) || lcs_eol_one == -1)
5773 break;
5774
5775 /* When the window is too narrow draw all "@" lines. */
5776 if (draw_state != WL_LINE
5777#ifdef FEAT_DIFF
5778 && filler_todo <= 0
5779#endif
5780 )
5781 {
5782 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005783#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 draw_vsep_win(wp, row);
5785#endif
5786 row = endrow;
5787 }
5788
5789 /* When line got too long for screen break here. */
5790 if (row == endrow)
5791 {
5792 ++row;
5793 break;
5794 }
5795
5796 if (screen_cur_row == screen_row - 1
5797#ifdef FEAT_DIFF
5798 && filler_todo <= 0
5799#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005800#ifdef FEAT_WINDOWS
5801 && W_WIDTH(wp) == Columns
5802#endif
5803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
5805 /* Remember that the line wraps, used for modeless copy. */
5806 LineWraps[screen_row - 1] = TRUE;
5807
5808 /*
5809 * Special trick to make copy/paste of wrapped lines work with
5810 * xterm/screen: write an extra character beyond the end of
5811 * the line. This will work with all terminal types
5812 * (regardless of the xn,am settings).
5813 * Only do this on a fast tty.
5814 * Only do this if the cursor is on the current line
5815 * (something has been written in it).
5816 * Don't do this for the GUI.
5817 * Don't do this for double-width characters.
5818 * Don't do this for a window not at the right screen border.
5819 */
5820 if (p_tf
5821#ifdef FEAT_GUI
5822 && !gui.in_use
5823#endif
5824#ifdef FEAT_MBYTE
5825 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005826 && ((*mb_off2cells)(LineOffset[screen_row],
5827 LineOffset[screen_row] + screen_Columns)
5828 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005830 + (int)Columns - 2,
5831 LineOffset[screen_row] + screen_Columns)
5832 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
5834 )
5835 {
5836 /* First make sure we are at the end of the screen line,
5837 * then output the same character again to let the
5838 * terminal know about the wrap. If the terminal doesn't
5839 * auto-wrap, we overwrite the character. */
5840 if (screen_cur_col != W_WIDTH(wp))
5841 screen_char(LineOffset[screen_row - 1]
5842 + (unsigned)Columns - 1,
5843 screen_row - 1, (int)(Columns - 1));
5844
5845#ifdef FEAT_MBYTE
5846 /* When there is a multi-byte character, just output a
5847 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005848 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5849 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 out_char(' ');
5851 else
5852#endif
5853 out_char(ScreenLines[LineOffset[screen_row - 1]
5854 + (Columns - 1)]);
5855 /* force a redraw of the first char on the next line */
5856 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5857 screen_start(); /* don't know where cursor is now */
5858 }
5859 }
5860
5861 col = 0;
5862 off = (unsigned)(current_ScreenLine - ScreenLines);
5863#ifdef FEAT_RIGHTLEFT
5864 if (wp->w_p_rl)
5865 {
5866 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5867 off += col;
5868 }
5869#endif
5870
5871 /* reset the drawing state for the start of a wrapped line */
5872 draw_state = WL_START;
5873 saved_n_extra = n_extra;
5874 saved_p_extra = p_extra;
5875 saved_c_extra = c_extra;
5876 saved_char_attr = char_attr;
5877 n_extra = 0;
5878 lcs_prec_todo = lcs_prec;
5879#ifdef FEAT_LINEBREAK
5880# ifdef FEAT_DIFF
5881 if (filler_todo <= 0)
5882# endif
5883 need_showbreak = TRUE;
5884#endif
5885#ifdef FEAT_DIFF
5886 --filler_todo;
5887 /* When the filler lines are actually below the last line of the
5888 * file, don't draw the line itself, break here. */
5889 if (filler_todo == 0 && wp->w_botfill)
5890 break;
5891#endif
5892 }
5893
5894 } /* for every character in the line */
5895
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005896#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005897 /* After an empty line check first word for capital. */
5898 if (*skipwhite(line) == NUL)
5899 {
5900 capcol_lnum = lnum + 1;
5901 cap_col = 0;
5902 }
5903#endif
5904
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005905 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 return row;
5907}
5908
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005909#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005910static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005911
5912/*
5913 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005914 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005915 */
5916 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005917comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005918{
5919 int i;
5920
5921 for (i = 0; i < Screen_mco; ++i)
5922 {
5923 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5924 return TRUE;
5925 if (ScreenLinesC[i][off_from] == 0)
5926 break;
5927 }
5928 return FALSE;
5929}
5930#endif
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932/*
5933 * Check whether the given character needs redrawing:
5934 * - the (first byte of the) character is different
5935 * - the attributes are different
5936 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005937 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 */
5939 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005940char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 if (cols > 0
5943 && ((ScreenLines[off_from] != ScreenLines[off_to]
5944 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5945
5946#ifdef FEAT_MBYTE
5947 || (enc_dbcs != 0
5948 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5949 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5950 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5951 : (cols > 1 && ScreenLines[off_from + 1]
5952 != ScreenLines[off_to + 1])))
5953 || (enc_utf8
5954 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5955 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005956 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005957 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5958 && ScreenLines[off_from + 1]
5959 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif
5961 ))
5962 return TRUE;
5963 return FALSE;
5964}
5965
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005966#if defined(FEAT_TERMINAL) || defined(PROTO)
5967/*
5968 * Return the index in ScreenLines[] for the current screen line.
5969 */
5970 int
5971screen_get_current_line_off()
5972{
5973 return (int)(current_ScreenLine - ScreenLines);
5974}
5975#endif
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977/*
5978 * Move one "cooked" screen line to the screen, but only the characters that
5979 * have actually changed. Handle insert/delete character.
5980 * "coloff" gives the first column on the screen for this line.
5981 * "endcol" gives the columns where valid characters are.
5982 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5983 * needs to be cleared, negative otherwise.
5984 * "rlflag" is TRUE in a rightleft window:
5985 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5986 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5987 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01005989screen_line(
5990 int row,
5991 int coloff,
5992 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005993 int clear_width,
5994 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 unsigned off_from;
5997 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005998#ifdef FEAT_MBYTE
5999 unsigned max_off_from;
6000 unsigned max_off_to;
6001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006003#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 int hl;
6005#endif
6006 int force = FALSE; /* force update rest of the line */
6007 int redraw_this /* bool: does character need redraw? */
6008#ifdef FEAT_GUI
6009 = TRUE /* For GUI when while-loop empty */
6010#endif
6011 ;
6012 int redraw_next; /* redraw_this for next character */
6013#ifdef FEAT_MBYTE
6014 int clear_next = FALSE;
6015 int char_cells; /* 1: normal char */
6016 /* 2: occupies two display cells */
6017# define CHAR_CELLS char_cells
6018#else
6019# define CHAR_CELLS 1
6020#endif
6021
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006022 /* Check for illegal row and col, just in case. */
6023 if (row >= Rows)
6024 row = Rows - 1;
6025 if (endcol > Columns)
6026 endcol = Columns;
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028# ifdef FEAT_CLIPBOARD
6029 clip_may_clear_selection(row, row);
6030# endif
6031
6032 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6033 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006034#ifdef FEAT_MBYTE
6035 max_off_from = off_from + screen_Columns;
6036 max_off_to = LineOffset[row] + screen_Columns;
6037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039#ifdef FEAT_RIGHTLEFT
6040 if (rlflag)
6041 {
6042 /* Clear rest first, because it's left of the text. */
6043 if (clear_width > 0)
6044 {
6045 while (col <= endcol && ScreenLines[off_to] == ' '
6046 && ScreenAttrs[off_to] == 0
6047# ifdef FEAT_MBYTE
6048 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6049# endif
6050 )
6051 {
6052 ++off_to;
6053 ++col;
6054 }
6055 if (col <= endcol)
6056 screen_fill(row, row + 1, col + coloff,
6057 endcol + coloff + 1, ' ', ' ', 0);
6058 }
6059 col = endcol + 1;
6060 off_to = LineOffset[row] + col + coloff;
6061 off_from += col;
6062 endcol = (clear_width > 0 ? clear_width : -clear_width);
6063 }
6064#endif /* FEAT_RIGHTLEFT */
6065
6066 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6067
6068 while (col < endcol)
6069 {
6070#ifdef FEAT_MBYTE
6071 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006072 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 else
6074 char_cells = 1;
6075#endif
6076
6077 redraw_this = redraw_next;
6078 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6079 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6080
6081#ifdef FEAT_GUI
6082 /* If the next character was bold, then redraw the current character to
6083 * remove any pixels that might have spilt over into us. This only
6084 * happens in the GUI.
6085 */
6086 if (redraw_next && gui.in_use)
6087 {
6088 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006089 if (hl > HL_ALL)
6090 hl = syn_attr2attr(hl);
6091 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 redraw_this = TRUE;
6093 }
6094#endif
6095
6096 if (redraw_this)
6097 {
6098 /*
6099 * Special handling when 'xs' termcap flag set (hpterm):
6100 * Attributes for characters are stored at the position where the
6101 * cursor is when writing the highlighting code. The
6102 * start-highlighting code must be written with the cursor on the
6103 * first highlighted character. The stop-highlighting code must
6104 * be written with the cursor just after the last highlighted
6105 * character.
6106 * Overwriting a character doesn't remove it's highlighting. Need
6107 * to clear the rest of the line, and force redrawing it
6108 * completely.
6109 */
6110 if ( p_wiv
6111 && !force
6112#ifdef FEAT_GUI
6113 && !gui.in_use
6114#endif
6115 && ScreenAttrs[off_to] != 0
6116 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6117 {
6118 /*
6119 * Need to remove highlighting attributes here.
6120 */
6121 windgoto(row, col + coloff);
6122 out_str(T_CE); /* clear rest of this screen line */
6123 screen_start(); /* don't know where cursor is now */
6124 force = TRUE; /* force redraw of rest of the line */
6125 redraw_next = TRUE; /* or else next char would miss out */
6126
6127 /*
6128 * If the previous character was highlighted, need to stop
6129 * highlighting at this character.
6130 */
6131 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6132 {
6133 screen_attr = ScreenAttrs[off_to - 1];
6134 term_windgoto(row, col + coloff);
6135 screen_stop_highlight();
6136 }
6137 else
6138 screen_attr = 0; /* highlighting has stopped */
6139 }
6140#ifdef FEAT_MBYTE
6141 if (enc_dbcs != 0)
6142 {
6143 /* Check if overwriting a double-byte with a single-byte or
6144 * the other way around requires another character to be
6145 * redrawn. For UTF-8 this isn't needed, because comparing
6146 * ScreenLinesUC[] is sufficient. */
6147 if (char_cells == 1
6148 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006149 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 {
6151 /* Writing a single-cell character over a double-cell
6152 * character: need to redraw the next cell. */
6153 ScreenLines[off_to + 1] = 0;
6154 redraw_next = TRUE;
6155 }
6156 else if (char_cells == 2
6157 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006158 && (*mb_off2cells)(off_to, max_off_to) == 1
6159 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 /* Writing the second half of a double-cell character over
6162 * a double-cell character: need to redraw the second
6163 * cell. */
6164 ScreenLines[off_to + 2] = 0;
6165 redraw_next = TRUE;
6166 }
6167
6168 if (enc_dbcs == DBCS_JPNU)
6169 ScreenLines2[off_to] = ScreenLines2[off_from];
6170 }
6171 /* When writing a single-width character over a double-width
6172 * character and at the end of the redrawn text, need to clear out
6173 * the right halve of the old character.
6174 * Also required when writing the right halve of a double-width
6175 * char over the left halve of an existing one. */
6176 if (has_mbyte && col + char_cells == endcol
6177 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006178 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006180 && (*mb_off2cells)(off_to, max_off_to) == 1
6181 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 clear_next = TRUE;
6183#endif
6184
6185 ScreenLines[off_to] = ScreenLines[off_from];
6186#ifdef FEAT_MBYTE
6187 if (enc_utf8)
6188 {
6189 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6190 if (ScreenLinesUC[off_from] != 0)
6191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006192 int i;
6193
6194 for (i = 0; i < Screen_mco; ++i)
6195 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 }
6198 if (char_cells == 2)
6199 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6200#endif
6201
6202#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006203 /* The bold trick makes a single column of pixels appear in the
6204 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 * character should be redrawn too. This happens for our own GUI
6206 * and for some xterms. */
6207 if (
6208# ifdef FEAT_GUI
6209 gui.in_use
6210# endif
6211# if defined(FEAT_GUI) && defined(UNIX)
6212 ||
6213# endif
6214# ifdef UNIX
6215 term_is_xterm
6216# endif
6217 )
6218 {
6219 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006220 if (hl > HL_ALL)
6221 hl = syn_attr2attr(hl);
6222 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 redraw_next = TRUE;
6224 }
6225#endif
6226 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6227#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006228 /* For simplicity set the attributes of second half of a
6229 * double-wide character equal to the first half. */
6230 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006232
6233 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 else
6236#endif
6237 screen_char(off_to, row, col + coloff);
6238 }
6239 else if ( p_wiv
6240#ifdef FEAT_GUI
6241 && !gui.in_use
6242#endif
6243 && col + coloff > 0)
6244 {
6245 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6246 {
6247 /*
6248 * Don't output stop-highlight when moving the cursor, it will
6249 * stop the highlighting when it should continue.
6250 */
6251 screen_attr = 0;
6252 }
6253 else if (screen_attr != 0)
6254 screen_stop_highlight();
6255 }
6256
6257 off_to += CHAR_CELLS;
6258 off_from += CHAR_CELLS;
6259 col += CHAR_CELLS;
6260 }
6261
6262#ifdef FEAT_MBYTE
6263 if (clear_next)
6264 {
6265 /* Clear the second half of a double-wide character of which the left
6266 * half was overwritten with a single-wide character. */
6267 ScreenLines[off_to] = ' ';
6268 if (enc_utf8)
6269 ScreenLinesUC[off_to] = 0;
6270 screen_char(off_to, row, col + coloff);
6271 }
6272#endif
6273
6274 if (clear_width > 0
6275#ifdef FEAT_RIGHTLEFT
6276 && !rlflag
6277#endif
6278 )
6279 {
6280#ifdef FEAT_GUI
6281 int startCol = col;
6282#endif
6283
6284 /* blank out the rest of the line */
6285 while (col < clear_width && ScreenLines[off_to] == ' '
6286 && ScreenAttrs[off_to] == 0
6287#ifdef FEAT_MBYTE
6288 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6289#endif
6290 )
6291 {
6292 ++off_to;
6293 ++col;
6294 }
6295 if (col < clear_width)
6296 {
6297#ifdef FEAT_GUI
6298 /*
6299 * In the GUI, clearing the rest of the line may leave pixels
6300 * behind if the first character cleared was bold. Some bold
6301 * fonts spill over the left. In this case we redraw the previous
6302 * character too. If we didn't skip any blanks above, then we
6303 * only redraw if the character wasn't already redrawn anyway.
6304 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006305 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 hl = ScreenAttrs[off_to];
6308 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006309 {
6310 int prev_cells = 1;
6311# ifdef FEAT_MBYTE
6312 if (enc_utf8)
6313 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6314 * that its width is 2. */
6315 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6316 else if (enc_dbcs != 0)
6317 {
6318 /* find previous character by counting from first
6319 * column and get its width. */
6320 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006321 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006322
6323 while (off < off_to)
6324 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006325 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006326 off += prev_cells;
6327 }
6328 }
6329
6330 if (enc_dbcs != 0 && prev_cells > 1)
6331 screen_char_2(off_to - prev_cells, row,
6332 col + coloff - prev_cells);
6333 else
6334# endif
6335 screen_char(off_to - prev_cells, row,
6336 col + coloff - prev_cells);
6337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339#endif
6340 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6341 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006342#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 off_to += clear_width - col;
6344 col = clear_width;
6345#endif
6346 }
6347 }
6348
6349 if (clear_width > 0)
6350 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006351#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 /* For a window that's left of another, draw the separator char. */
6353 if (col + coloff < Columns)
6354 {
6355 int c;
6356
6357 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006358 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006360 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6361 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362# endif
6363 || ScreenAttrs[off_to] != hl)
6364 {
6365 ScreenLines[off_to] = c;
6366 ScreenAttrs[off_to] = hl;
6367# ifdef FEAT_MBYTE
6368 if (enc_utf8)
6369 {
6370 if (c >= 0x80)
6371 {
6372 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006373 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
6375 else
6376 ScreenLinesUC[off_to] = 0;
6377 }
6378# endif
6379 screen_char(off_to, row, col + coloff);
6380 }
6381 }
6382 else
6383#endif
6384 LineWraps[row] = FALSE;
6385 }
6386}
6387
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006388#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006390 * Mirror text "str" for right-left displaying.
6391 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006393 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006394rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 char_u *p1, *p2;
6397 int t;
6398
6399 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6400 {
6401 t = *p1;
6402 *p1 = *p2;
6403 *p2 = t;
6404 }
6405}
6406#endif
6407
6408#if defined(FEAT_WINDOWS) || defined(PROTO)
6409/*
6410 * mark all status lines for redraw; used after first :cd
6411 */
6412 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006413status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 win_T *wp;
6416
Bram Moolenaar29323592016-07-24 22:04:11 +02006417 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (wp->w_status_height)
6419 {
6420 wp->w_redr_status = TRUE;
6421 redraw_later(VALID);
6422 }
6423}
6424
6425/*
6426 * mark all status lines of the current buffer for redraw
6427 */
6428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006429status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 win_T *wp;
6432
Bram Moolenaar29323592016-07-24 22:04:11 +02006433 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6435 {
6436 wp->w_redr_status = TRUE;
6437 redraw_later(VALID);
6438 }
6439}
6440
6441/*
6442 * Redraw all status lines that need to be redrawn.
6443 */
6444 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006445redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 win_T *wp;
6448
Bram Moolenaar29323592016-07-24 22:04:11 +02006449 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (wp->w_redr_status)
6451 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006452 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006453 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454}
6455#endif
6456
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006457#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458/*
6459 * Redraw all status lines at the bottom of frame "frp".
6460 */
6461 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006462win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 if (frp->fr_layout == FR_LEAF)
6465 frp->fr_win->w_redr_status = TRUE;
6466 else if (frp->fr_layout == FR_ROW)
6467 {
6468 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6469 win_redraw_last_status(frp);
6470 }
6471 else /* frp->fr_layout == FR_COL */
6472 {
6473 frp = frp->fr_child;
6474 while (frp->fr_next != NULL)
6475 frp = frp->fr_next;
6476 win_redraw_last_status(frp);
6477 }
6478}
6479#endif
6480
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006481#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482/*
6483 * Draw the verticap separator right of window "wp" starting with line "row".
6484 */
6485 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006486draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 int hl;
6489 int c;
6490
6491 if (wp->w_vsep_width)
6492 {
6493 /* draw the vertical separator right of this window */
6494 c = fillchar_vsep(&hl);
6495 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6496 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6497 c, ' ', hl);
6498 }
6499}
6500#endif
6501
6502#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006503static int status_match_len(expand_T *xp, char_u *s);
6504static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006507 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 */
6509 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006510status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 int len = 0;
6513
6514#ifdef FEAT_MENU
6515 int emenu = (xp->xp_context == EXPAND_MENUS
6516 || xp->xp_context == EXPAND_MENUNAMES);
6517
6518 /* Check for menu separators - replace with '|'. */
6519 if (emenu && menu_is_separator(s))
6520 return 1;
6521#endif
6522
6523 while (*s != NUL)
6524 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006525 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006526 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006527 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529
6530 return len;
6531}
6532
6533/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006534 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006535 * These are backslashes used for escaping. Do show backslashes in help tags.
6536 */
6537 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006538skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006539{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006540 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006541#ifdef FEAT_MENU
6542 || ((xp->xp_context == EXPAND_MENUS
6543 || xp->xp_context == EXPAND_MENUNAMES)
6544 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6545#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006546 )
6547 {
6548#ifndef BACKSLASH_IN_FILENAME
6549 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6550 return 2;
6551#endif
6552 return 1;
6553 }
6554 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006555}
6556
6557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 * Show wildchar matches in the status line.
6559 * Show at least the "match" item.
6560 * We start at item 'first_match' in the list and show all matches that fit.
6561 *
6562 * If inversion is possible we use it. Else '=' characters are used.
6563 */
6564 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006565win_redr_status_matches(
6566 expand_T *xp,
6567 int num_matches,
6568 char_u **matches, /* list of matches */
6569 int match,
6570 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6573 int row;
6574 char_u *buf;
6575 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006576 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 int fillchar;
6578 int attr;
6579 int i;
6580 int highlight = TRUE;
6581 char_u *selstart = NULL;
6582 int selstart_col = 0;
6583 char_u *selend = NULL;
6584 static int first_match = 0;
6585 int add_left = FALSE;
6586 char_u *s;
6587#ifdef FEAT_MENU
6588 int emenu;
6589#endif
6590#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6591 int l;
6592#endif
6593
6594 if (matches == NULL) /* interrupted completion? */
6595 return;
6596
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006597#ifdef FEAT_MBYTE
6598 if (has_mbyte)
6599 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6600 else
6601#endif
6602 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (buf == NULL)
6604 return;
6605
6606 if (match == -1) /* don't show match but original text */
6607 {
6608 match = 0;
6609 highlight = FALSE;
6610 }
6611 /* count 1 for the ending ">" */
6612 clen = status_match_len(xp, L_MATCH(match)) + 3;
6613 if (match == 0)
6614 first_match = 0;
6615 else if (match < first_match)
6616 {
6617 /* jumping left, as far as we can go */
6618 first_match = match;
6619 add_left = TRUE;
6620 }
6621 else
6622 {
6623 /* check if match fits on the screen */
6624 for (i = first_match; i < match; ++i)
6625 clen += status_match_len(xp, L_MATCH(i)) + 2;
6626 if (first_match > 0)
6627 clen += 2;
6628 /* jumping right, put match at the left */
6629 if ((long)clen > Columns)
6630 {
6631 first_match = match;
6632 /* if showing the last match, we can add some on the left */
6633 clen = 2;
6634 for (i = match; i < num_matches; ++i)
6635 {
6636 clen += status_match_len(xp, L_MATCH(i)) + 2;
6637 if ((long)clen >= Columns)
6638 break;
6639 }
6640 if (i == num_matches)
6641 add_left = TRUE;
6642 }
6643 }
6644 if (add_left)
6645 while (first_match > 0)
6646 {
6647 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6648 if ((long)clen >= Columns)
6649 break;
6650 --first_match;
6651 }
6652
6653 fillchar = fillchar_status(&attr, TRUE);
6654
6655 if (first_match == 0)
6656 {
6657 *buf = NUL;
6658 len = 0;
6659 }
6660 else
6661 {
6662 STRCPY(buf, "< ");
6663 len = 2;
6664 }
6665 clen = len;
6666
6667 i = first_match;
6668 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6669 {
6670 if (i == match)
6671 {
6672 selstart = buf + len;
6673 selstart_col = clen;
6674 }
6675
6676 s = L_MATCH(i);
6677 /* Check for menu separators - replace with '|' */
6678#ifdef FEAT_MENU
6679 emenu = (xp->xp_context == EXPAND_MENUS
6680 || xp->xp_context == EXPAND_MENUNAMES);
6681 if (emenu && menu_is_separator(s))
6682 {
6683 STRCPY(buf + len, transchar('|'));
6684 l = (int)STRLEN(buf + len);
6685 len += l;
6686 clen += l;
6687 }
6688 else
6689#endif
6690 for ( ; *s != NUL; ++s)
6691 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006692 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 clen += ptr2cells(s);
6694#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 {
6697 STRNCPY(buf + len, s, l);
6698 s += l - 1;
6699 len += l;
6700 }
6701 else
6702#endif
6703 {
6704 STRCPY(buf + len, transchar_byte(*s));
6705 len += (int)STRLEN(buf + len);
6706 }
6707 }
6708 if (i == match)
6709 selend = buf + len;
6710
6711 *(buf + len++) = ' ';
6712 *(buf + len++) = ' ';
6713 clen += 2;
6714 if (++i == num_matches)
6715 break;
6716 }
6717
6718 if (i != num_matches)
6719 {
6720 *(buf + len++) = '>';
6721 ++clen;
6722 }
6723
6724 buf[len] = NUL;
6725
6726 row = cmdline_row - 1;
6727 if (row >= 0)
6728 {
6729 if (wild_menu_showing == 0)
6730 {
6731 if (msg_scrolled > 0)
6732 {
6733 /* Put the wildmenu just above the command line. If there is
6734 * no room, scroll the screen one line up. */
6735 if (cmdline_row == Rows - 1)
6736 {
6737 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6738 ++msg_scrolled;
6739 }
6740 else
6741 {
6742 ++cmdline_row;
6743 ++row;
6744 }
6745 wild_menu_showing = WM_SCROLLED;
6746 }
6747 else
6748 {
6749 /* Create status line if needed by setting 'laststatus' to 2.
6750 * Set 'winminheight' to zero to avoid that the window is
6751 * resized. */
6752 if (lastwin->w_status_height == 0)
6753 {
6754 save_p_ls = p_ls;
6755 save_p_wmh = p_wmh;
6756 p_ls = 2;
6757 p_wmh = 0;
6758 last_status(FALSE);
6759 }
6760 wild_menu_showing = WM_SHOWN;
6761 }
6762 }
6763
6764 screen_puts(buf, row, 0, attr);
6765 if (selstart != NULL && highlight)
6766 {
6767 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006768 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770
6771 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6772 }
6773
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006774#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 win_redraw_last_status(topframe);
6776#else
6777 lastwin->w_redr_status = TRUE;
6778#endif
6779 vim_free(buf);
6780}
6781#endif
6782
6783#if defined(FEAT_WINDOWS) || defined(PROTO)
6784/*
6785 * Redraw the status line of window wp.
6786 *
6787 * If inversion is possible we use it. Else '=' characters are used.
6788 */
6789 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006790win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 int row;
6793 char_u *p;
6794 int len;
6795 int fillchar;
6796 int attr;
6797 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006798 static int busy = FALSE;
6799
6800 /* It's possible to get here recursively when 'statusline' (indirectly)
6801 * invokes ":redrawstatus". Simply ignore the call then. */
6802 if (busy)
6803 return;
6804 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 wp->w_redr_status = FALSE;
6807 if (wp->w_status_height == 0)
6808 {
6809 /* no status line, can only be last window */
6810 redraw_cmdline = TRUE;
6811 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006812 else if (!redrawing()
6813#ifdef FEAT_INS_EXPAND
6814 /* don't update status line when popup menu is visible and may be
6815 * drawn over it */
6816 || pum_visible()
6817#endif
6818 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 {
6820 /* Don't redraw right now, do it later. */
6821 wp->w_redr_status = TRUE;
6822 }
6823#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006824 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 {
6826 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006827 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829#endif
6830 else
6831 {
6832 fillchar = fillchar_status(&attr, wp == curwin);
6833
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006834 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 p = NameBuff;
6836 len = (int)STRLEN(p);
6837
6838 if (wp->w_buffer->b_help
6839#ifdef FEAT_QUICKFIX
6840 || wp->w_p_pvw
6841#endif
6842 || bufIsChanged(wp->w_buffer)
6843 || wp->w_buffer->b_p_ro)
6844 *(p + len++) = ' ';
6845 if (wp->w_buffer->b_help)
6846 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006847 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 len += (int)STRLEN(p + len);
6849 }
6850#ifdef FEAT_QUICKFIX
6851 if (wp->w_p_pvw)
6852 {
6853 STRCPY(p + len, _("[Preview]"));
6854 len += (int)STRLEN(p + len);
6855 }
6856#endif
6857 if (bufIsChanged(wp->w_buffer))
6858 {
6859 STRCPY(p + len, "[+]");
6860 len += 3;
6861 }
6862 if (wp->w_buffer->b_p_ro)
6863 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006864 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006865 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 }
6867
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6869 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6870 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6871 if (this_ru_col <= 1)
6872 {
6873 p = (char_u *)"<"; /* No room for file name! */
6874 len = 1;
6875 }
6876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#ifdef FEAT_MBYTE
6878 if (has_mbyte)
6879 {
6880 int clen = 0, i;
6881
6882 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006883 clen = mb_string2cells(p, -1);
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 /* Find first character that will fit.
6886 * Going from start to end is much faster for DBCS. */
6887 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006888 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 clen -= (*mb_ptr2cells)(p + i);
6890 len = clen;
6891 if (i > 0)
6892 {
6893 p = p + i - 1;
6894 *p = '<';
6895 ++len;
6896 }
6897
6898 }
6899 else
6900#endif
6901 if (len > this_ru_col - 1)
6902 {
6903 p += len - (this_ru_col - 1);
6904 *p = '<';
6905 len = this_ru_col - 1;
6906 }
6907
6908 row = W_WINROW(wp) + wp->w_height;
6909 screen_puts(p, row, W_WINCOL(wp), attr);
6910 screen_fill(row, row + 1, len + W_WINCOL(wp),
6911 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6912
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006913 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6915 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6916 - 1 + W_WINCOL(wp)), attr);
6917
6918#ifdef FEAT_CMDL_INFO
6919 win_redr_ruler(wp, TRUE);
6920#endif
6921 }
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /*
6924 * May need to draw the character below the vertical separator.
6925 */
6926 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6927 {
6928 if (stl_connected(wp))
6929 fillchar = fillchar_status(&attr, wp == curwin);
6930 else
6931 fillchar = fillchar_vsep(&attr);
6932 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6933 attr);
6934 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006935 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936}
6937
Bram Moolenaar238a5642006-02-21 22:12:05 +00006938#ifdef FEAT_STL_OPT
6939/*
6940 * Redraw the status line according to 'statusline' and take care of any
6941 * errors encountered.
6942 */
6943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006944redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006945{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006946 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006947 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006948
6949 /* When called recursively return. This can happen when the statusline
6950 * contains an expression that triggers a redraw. */
6951 if (entered)
6952 return;
6953 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006954
Bram Moolenaara742e082016-04-05 21:10:38 +02006955 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006956 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006957 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006958 {
6959 /* When there is an error disable the statusline, otherwise the
6960 * display is messed up with errors and a redraw triggers the problem
6961 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006962 set_string_option_direct((char_u *)"statusline", -1,
6963 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006964 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006965 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006966 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006967 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006968}
6969#endif
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
6972 * Return TRUE if the status line of window "wp" is connected to the status
6973 * line of the window right of it. If not, then it's a vertical separator.
6974 * Only call if (wp->w_vsep_width != 0).
6975 */
6976 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006977stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 frame_T *fr;
6980
6981 fr = wp->w_frame;
6982 while (fr->fr_parent != NULL)
6983 {
6984 if (fr->fr_parent->fr_layout == FR_COL)
6985 {
6986 if (fr->fr_next != NULL)
6987 break;
6988 }
6989 else
6990 {
6991 if (fr->fr_next != NULL)
6992 return TRUE;
6993 }
6994 fr = fr->fr_parent;
6995 }
6996 return FALSE;
6997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999#endif /* FEAT_WINDOWS */
7000
7001#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7002/*
7003 * Get the value to show for the language mappings, active 'keymap'.
7004 */
7005 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007006get_keymap_str(
7007 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007008 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007009 char_u *buf, /* buffer for the result */
7010 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 char_u *p;
7013
7014 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7015 return FALSE;
7016
7017 {
7018#ifdef FEAT_EVAL
7019 buf_T *old_curbuf = curbuf;
7020 win_T *old_curwin = curwin;
7021 char_u *s;
7022
7023 curbuf = wp->w_buffer;
7024 curwin = wp;
7025 STRCPY(buf, "b:keymap_name"); /* must be writable */
7026 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007027 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 --emsg_skip;
7029 curbuf = old_curbuf;
7030 curwin = old_curwin;
7031 if (p == NULL || *p == NUL)
7032#endif
7033 {
7034#ifdef FEAT_KEYMAP
7035 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7036 p = wp->w_buffer->b_p_keymap;
7037 else
7038#endif
7039 p = (char_u *)"lang";
7040 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007041 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 buf[0] = NUL;
7043#ifdef FEAT_EVAL
7044 vim_free(s);
7045#endif
7046 }
7047 return buf[0] != NUL;
7048}
7049#endif
7050
7051#if defined(FEAT_STL_OPT) || defined(PROTO)
7052/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007053 * Redraw the status line or ruler of window "wp".
7054 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 */
7056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007057win_redr_custom(
7058 win_T *wp,
7059 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007061 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 int attr;
7063 int curattr;
7064 int row;
7065 int col = 0;
7066 int maxwidth;
7067 int width;
7068 int n;
7069 int len;
7070 int fillchar;
7071 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007072 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007074 struct stl_hlrec hltab[STL_MAX_ITEM];
7075 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007076 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007077 win_T *ewp;
7078 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar1d633412013-12-11 15:52:01 +01007080 /* There is a tiny chance that this gets called recursively: When
7081 * redrawing a status line triggers redrawing the ruler or tabline.
7082 * Avoid trouble by not allowing recursion. */
7083 if (entered)
7084 return;
7085 entered = TRUE;
7086
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007088 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007090 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007091 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007092 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007093 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007094 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007095 maxwidth = Columns;
7096# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007097 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007100 else
7101 {
7102 row = W_WINROW(wp) + wp->w_height;
7103 fillchar = fillchar_status(&attr, wp == curwin);
7104 maxwidth = W_WIDTH(wp);
7105
7106 if (draw_ruler)
7107 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007108 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007109 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007110 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007111 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007112 if (*++stl == '-')
7113 stl++;
7114 if (atoi((char *)stl))
7115 while (VIM_ISDIGIT(*stl))
7116 stl++;
7117 if (*stl++ != '(')
7118 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007119 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007120#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007121 col = ru_col - (Columns - W_WIDTH(wp));
7122 if (col < (W_WIDTH(wp) + 1) / 2)
7123 col = (W_WIDTH(wp) + 1) / 2;
7124#else
7125 col = ru_col;
7126 if (col > (Columns + 1) / 2)
7127 col = (Columns + 1) / 2;
7128#endif
7129 maxwidth = W_WIDTH(wp) - col;
7130#ifdef FEAT_WINDOWS
7131 if (!wp->w_status_height)
7132#endif
7133 {
7134 row = Rows - 1;
7135 --maxwidth; /* writing in last column may cause scrolling */
7136 fillchar = ' ';
7137 attr = 0;
7138 }
7139
7140# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007141 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142# endif
7143 }
7144 else
7145 {
7146 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007151 use_sandbox = was_set_insecurely((char_u *)"statusline",
7152 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007153# endif
7154 }
7155
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007156#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 col += W_WINCOL(wp);
7158#endif
7159 }
7160
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007162 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaar61452852011-02-01 18:01:11 +01007164 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7165 * the cursor away and back. */
7166 ewp = wp == NULL ? curwin : wp;
7167 p_crb_save = ewp->w_p_crb;
7168 ewp->w_p_crb = FALSE;
7169
Bram Moolenaar362f3562009-11-03 16:20:34 +00007170 /* Make a copy, because the statusline may include a function call that
7171 * might change the option value and free the memory. */
7172 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007173 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007174 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007175 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007177 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007179 /* Make all characters printable. */
7180 p = transstr(buf);
7181 if (p != NULL)
7182 {
7183 vim_strncpy(buf, p, sizeof(buf) - 1);
7184 vim_free(p);
7185 }
7186
7187 /* fill up with "fillchar" */
7188 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007189 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
7191#ifdef FEAT_MBYTE
7192 len += (*mb_char2bytes)(fillchar, buf + len);
7193#else
7194 buf[len++] = fillchar;
7195#endif
7196 ++width;
7197 }
7198 buf[len] = NUL;
7199
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007200 /*
7201 * Draw each snippet with the specified highlighting.
7202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 curattr = attr;
7204 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007205 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007207 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 screen_puts_len(p, len, row, col, curattr);
7209 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007210 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007212 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007214 else if (hltab[n].userhl < 0)
7215 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007217 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007218 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219#endif
7220 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007221 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007224
7225 if (wp == NULL)
7226 {
7227 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7228 col = 0;
7229 len = 0;
7230 p = buf;
7231 fillchar = 0;
7232 for (n = 0; tabtab[n].start != NULL; n++)
7233 {
7234 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7235 while (col < len)
7236 TabPageIdxs[col++] = fillchar;
7237 p = tabtab[n].start;
7238 fillchar = tabtab[n].userhl;
7239 }
7240 while (col < Columns)
7241 TabPageIdxs[col++] = fillchar;
7242 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007243
7244theend:
7245 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246}
7247
7248#endif /* FEAT_STL_OPT */
7249
7250/*
7251 * Output a single character directly to the screen and update ScreenLines.
7252 */
7253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007254screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 char_u buf[MB_MAXBYTES + 1];
7257
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007258#ifdef FEAT_MBYTE
7259 if (has_mbyte)
7260 buf[(*mb_char2bytes)(c, buf)] = NUL;
7261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007263 {
7264 buf[0] = c;
7265 buf[1] = NUL;
7266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 screen_puts(buf, row, col, attr);
7268}
7269
7270/*
7271 * Get a single character directly from ScreenLines into "bytes[]".
7272 * Also return its attribute in *attrp;
7273 */
7274 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007275screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 unsigned off;
7278
7279 /* safety check */
7280 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7281 {
7282 off = LineOffset[row] + col;
7283 *attrp = ScreenAttrs[off];
7284 bytes[0] = ScreenLines[off];
7285 bytes[1] = NUL;
7286
7287#ifdef FEAT_MBYTE
7288 if (enc_utf8 && ScreenLinesUC[off] != 0)
7289 bytes[utfc_char2bytes(off, bytes)] = NUL;
7290 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7291 {
7292 bytes[0] = ScreenLines[off];
7293 bytes[1] = ScreenLines2[off];
7294 bytes[2] = NUL;
7295 }
7296 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7297 {
7298 bytes[1] = ScreenLines[off + 1];
7299 bytes[2] = NUL;
7300 }
7301#endif
7302 }
7303}
7304
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007305#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007306static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007307
7308/*
7309 * Return TRUE if composing characters for screen posn "off" differs from
7310 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007311 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007312 */
7313 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007314screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007315{
7316 int i;
7317
7318 for (i = 0; i < Screen_mco; ++i)
7319 {
7320 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7321 return TRUE;
7322 if (u8cc[i] == 0)
7323 break;
7324 }
7325 return FALSE;
7326}
7327#endif
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329/*
7330 * Put string '*text' on the screen at position 'row' and 'col', with
7331 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7332 * Note: only outputs within one row, message is truncated at screen boundary!
7333 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7334 */
7335 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007336screen_puts(
7337 char_u *text,
7338 int row,
7339 int col,
7340 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 screen_puts_len(text, -1, row, col, attr);
7343}
7344
7345/*
7346 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7347 * a NUL.
7348 */
7349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007350screen_puts_len(
7351 char_u *text,
7352 int textlen,
7353 int row,
7354 int col,
7355 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356{
7357 unsigned off;
7358 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007359 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int c;
7361#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007362 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 int mbyte_blen = 1;
7364 int mbyte_cells = 1;
7365 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007366 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 int clear_next_cell = FALSE;
7368# ifdef FEAT_ARABIC
7369 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370 int pc, nc, nc1;
7371 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372# endif
7373#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007374#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7375 int force_redraw_this;
7376 int force_redraw_next = FALSE;
7377#endif
7378 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
7380 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7381 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007382 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Bram Moolenaarc236c162008-07-13 17:41:49 +00007384#ifdef FEAT_MBYTE
7385 /* When drawing over the right halve of a double-wide char clear out the
7386 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007387 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007388# ifdef FEAT_GUI
7389 && !gui.in_use
7390# endif
7391 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007392 {
7393 ScreenLines[off - 1] = ' ';
7394 ScreenAttrs[off - 1] = 0;
7395 if (enc_utf8)
7396 {
7397 ScreenLinesUC[off - 1] = 0;
7398 ScreenLinesC[0][off - 1] = 0;
7399 }
7400 /* redraw the previous cell, make it empty */
7401 screen_char(off - 1, row, col - 1);
7402 /* force the cell at "col" to be redrawn */
7403 force_redraw_next = TRUE;
7404 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007405#endif
7406
Bram Moolenaar367329b2007-08-30 11:53:22 +00007407#ifdef FEAT_MBYTE
7408 max_off = LineOffset[row] + screen_Columns;
7409#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007410 while (col < screen_Columns
7411 && (len < 0 || (int)(ptr - text) < len)
7412 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 c = *ptr;
7415#ifdef FEAT_MBYTE
7416 /* check if this is the first byte of a multibyte */
7417 if (has_mbyte)
7418 {
7419 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007420 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007422 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7424 mbyte_cells = 1;
7425 else if (enc_dbcs != 0)
7426 mbyte_cells = mbyte_blen;
7427 else /* enc_utf8 */
7428 {
7429 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007430 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 (int)((text + len) - ptr));
7432 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007433 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007435# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 /* Non-BMP character: display as ? or fullwidth ?. */
7437 if (u8c >= 0x10000)
7438 {
7439 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7440 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007441 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444# ifdef FEAT_ARABIC
7445 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7446 {
7447 /* Do Arabic shaping. */
7448 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7449 {
7450 /* Past end of string to be displayed. */
7451 nc = NUL;
7452 nc1 = NUL;
7453 }
7454 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007455 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007456 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7457 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007458 nc1 = pcc[0];
7459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 pc = prev_c;
7461 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007462 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464 else
7465 prev_c = u8c;
7466# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007467 if (col + mbyte_cells > screen_Columns)
7468 {
7469 /* Only 1 cell left, but character requires 2 cells:
7470 * display a '>' in the last column to avoid wrapping. */
7471 c = '>';
7472 mbyte_cells = 1;
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475 }
7476#endif
7477
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007478#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7479 force_redraw_this = force_redraw_next;
7480 force_redraw_next = FALSE;
7481#endif
7482
7483 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484#ifdef FEAT_MBYTE
7485 || (mbyte_cells == 2
7486 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7487 || (enc_dbcs == DBCS_JPNU
7488 && c == 0x8e
7489 && ScreenLines2[off] != ptr[1])
7490 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007491 && (ScreenLinesUC[off] !=
7492 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7493 || (ScreenLinesUC[off] != 0
7494 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495#endif
7496 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007497 || exmode_active;
7498
7499 if (need_redraw
7500#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7501 || force_redraw_this
7502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 )
7504 {
7505#if defined(FEAT_GUI) || defined(UNIX)
7506 /* The bold trick makes a single row of pixels appear in the next
7507 * character. When a bold character is removed, the next
7508 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007509 * and for some xterms. */
7510 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511# ifdef FEAT_GUI
7512 gui.in_use
7513# endif
7514# if defined(FEAT_GUI) && defined(UNIX)
7515 ||
7516# endif
7517# ifdef UNIX
7518 term_is_xterm
7519# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007520 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007522 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007524 if (n > HL_ALL)
7525 n = syn_attr2attr(n);
7526 if (n & HL_BOLD)
7527 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529#endif
7530#ifdef FEAT_MBYTE
7531 /* When at the end of the text and overwriting a two-cell
7532 * character with a one-cell character, need to clear the next
7533 * cell. Also when overwriting the left halve of a two-cell char
7534 * with the right halve of a two-cell char. Do this only once
7535 * (mb_off2cells() may return 2 on the right halve). */
7536 if (clear_next_cell)
7537 clear_next_cell = FALSE;
7538 else if (has_mbyte
7539 && (len < 0 ? ptr[mbyte_blen] == NUL
7540 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007541 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007543 && (*mb_off2cells)(off, max_off) == 1
7544 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 clear_next_cell = TRUE;
7546
7547 /* Make sure we never leave a second byte of a double-byte behind,
7548 * it confuses mb_off2cells(). */
7549 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007550 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007552 && (*mb_off2cells)(off, max_off) == 1
7553 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 ScreenLines[off + mbyte_blen] = 0;
7555#endif
7556 ScreenLines[off] = c;
7557 ScreenAttrs[off] = attr;
7558#ifdef FEAT_MBYTE
7559 if (enc_utf8)
7560 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007561 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 ScreenLinesUC[off] = 0;
7563 else
7564 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007565 int i;
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007568 for (i = 0; i < Screen_mco; ++i)
7569 {
7570 ScreenLinesC[i][off] = u8cc[i];
7571 if (u8cc[i] == 0)
7572 break;
7573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 if (mbyte_cells == 2)
7576 {
7577 ScreenLines[off + 1] = 0;
7578 ScreenAttrs[off + 1] = attr;
7579 }
7580 screen_char(off, row, col);
7581 }
7582 else if (mbyte_cells == 2)
7583 {
7584 ScreenLines[off + 1] = ptr[1];
7585 ScreenAttrs[off + 1] = attr;
7586 screen_char_2(off, row, col);
7587 }
7588 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7589 {
7590 ScreenLines2[off] = ptr[1];
7591 screen_char(off, row, col);
7592 }
7593 else
7594#endif
7595 screen_char(off, row, col);
7596 }
7597#ifdef FEAT_MBYTE
7598 if (has_mbyte)
7599 {
7600 off += mbyte_cells;
7601 col += mbyte_cells;
7602 ptr += mbyte_blen;
7603 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007604 {
7605 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007607 len = -1;
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610 else
7611#endif
7612 {
7613 ++off;
7614 ++col;
7615 ++ptr;
7616 }
7617 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007618
7619#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7620 /* If we detected the next character needs to be redrawn, but the text
7621 * doesn't extend up to there, update the character here. */
7622 if (force_redraw_next && col < screen_Columns)
7623 {
7624# ifdef FEAT_MBYTE
7625 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7626 screen_char_2(off, row, col);
7627 else
7628# endif
7629 screen_char(off, row, col);
7630 }
7631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632}
7633
7634#ifdef FEAT_SEARCH_EXTRA
7635/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007636 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 */
7638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007639start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 if (p_hls && !no_hlsearch)
7642 {
7643 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007644 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007645# ifdef FEAT_RELTIME
7646 /* Set the time limit to 'redrawtime'. */
7647 profile_setlimit(p_rdt, &search_hl.tm);
7648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650}
7651
7652/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007653 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 */
7655 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007656end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 if (search_hl.rm.regprog != NULL)
7659 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007660 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 search_hl.rm.regprog = NULL;
7662 }
7663}
7664
7665/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007666 * Init for calling prepare_search_hl().
7667 */
7668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007669init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007670{
7671 matchitem_T *cur;
7672
7673 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7674 * match */
7675 cur = wp->w_match_head;
7676 while (cur != NULL)
7677 {
7678 cur->hl.rm = cur->match;
7679 if (cur->hlg_id == 0)
7680 cur->hl.attr = 0;
7681 else
7682 cur->hl.attr = syn_id2attr(cur->hlg_id);
7683 cur->hl.buf = wp->w_buffer;
7684 cur->hl.lnum = 0;
7685 cur->hl.first_lnum = 0;
7686# ifdef FEAT_RELTIME
7687 /* Set the time limit to 'redrawtime'. */
7688 profile_setlimit(p_rdt, &(cur->hl.tm));
7689# endif
7690 cur = cur->next;
7691 }
7692 search_hl.buf = wp->w_buffer;
7693 search_hl.lnum = 0;
7694 search_hl.first_lnum = 0;
7695 /* time limit is set at the toplevel, for all windows */
7696}
7697
7698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 * Advance to the match in window "wp" line "lnum" or past it.
7700 */
7701 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007702prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007704 matchitem_T *cur; /* points to the match list */
7705 match_T *shl; /* points to search_hl or a match */
7706 int shl_flag; /* flag to indicate whether search_hl
7707 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007708 int pos_inprogress; /* marks that position match search is
7709 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 int n;
7711
7712 /*
7713 * When using a multi-line pattern, start searching at the top
7714 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007715 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007717 cur = wp->w_match_head;
7718 shl_flag = FALSE;
7719 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007721 if (shl_flag == FALSE)
7722 {
7723 shl = &search_hl;
7724 shl_flag = TRUE;
7725 }
7726 else
7727 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (shl->rm.regprog != NULL
7729 && shl->lnum == 0
7730 && re_multiline(shl->rm.regprog))
7731 {
7732 if (shl->first_lnum == 0)
7733 {
7734# ifdef FEAT_FOLDING
7735 for (shl->first_lnum = lnum;
7736 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7737 if (hasFoldingWin(wp, shl->first_lnum - 1,
7738 NULL, NULL, TRUE, NULL))
7739 break;
7740# else
7741 shl->first_lnum = wp->w_topline;
7742# endif
7743 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007744 if (cur != NULL)
7745 cur->pos.cur = 0;
7746 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007748 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7749 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007751 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7752 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007753 pos_inprogress = cur == NULL || cur->pos.cur == 0
7754 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 if (shl->lnum != 0)
7756 {
7757 shl->first_lnum = shl->lnum
7758 + shl->rm.endpos[0].lnum
7759 - shl->rm.startpos[0].lnum;
7760 n = shl->rm.endpos[0].col;
7761 }
7762 else
7763 {
7764 ++shl->first_lnum;
7765 n = 0;
7766 }
7767 }
7768 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007769 if (shl != &search_hl && cur != NULL)
7770 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772}
7773
7774/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007775 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 * Uses shl->buf.
7777 * Sets shl->lnum and shl->rm contents.
7778 * Note: Assumes a previous match is always before "lnum", unless
7779 * shl->lnum is zero.
7780 * Careful: Any pointers for buffer lines will become invalid.
7781 */
7782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007783next_search_hl(
7784 win_T *win,
7785 match_T *shl, /* points to search_hl or a match */
7786 linenr_T lnum,
7787 colnr_T mincol, /* minimal column for a match */
7788 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 linenr_T l;
7791 colnr_T matchcol;
7792 long nmatched;
7793
7794 if (shl->lnum != 0)
7795 {
7796 /* Check for three situations:
7797 * 1. If the "lnum" is below a previous match, start a new search.
7798 * 2. If the previous match includes "mincol", use it.
7799 * 3. Continue after the previous match.
7800 */
7801 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7802 if (lnum > l)
7803 shl->lnum = 0;
7804 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7805 return;
7806 }
7807
7808 /*
7809 * Repeat searching for a match until one is found that includes "mincol"
7810 * or none is found in this line.
7811 */
7812 called_emsg = FALSE;
7813 for (;;)
7814 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007815#ifdef FEAT_RELTIME
7816 /* Stop searching after passing the time limit. */
7817 if (profile_passed_limit(&(shl->tm)))
7818 {
7819 shl->lnum = 0; /* no match found in time */
7820 break;
7821 }
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 /* Three situations:
7824 * 1. No useful previous match: search from start of line.
7825 * 2. Not Vi compatible or empty match: continue at next character.
7826 * Break the loop if this is beyond the end of the line.
7827 * 3. Vi compatible searching: continue at end of previous match.
7828 */
7829 if (shl->lnum == 0)
7830 matchcol = 0;
7831 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7832 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007833 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007835 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007836
7837 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007838 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007839 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007841 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 shl->lnum = 0;
7843 break;
7844 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007845#ifdef FEAT_MBYTE
7846 if (has_mbyte)
7847 matchcol += mb_ptr2len(ml);
7848 else
7849#endif
7850 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852 else
7853 matchcol = shl->rm.endpos[0].col;
7854
7855 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007856 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007858 /* Remember whether shl->rm is using a copy of the regprog in
7859 * cur->match. */
7860 int regprog_is_copy = (shl != &search_hl && cur != NULL
7861 && shl == &cur->hl
7862 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007863 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007864
Bram Moolenaarb3414592014-06-17 17:48:32 +02007865 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7866 matchcol,
7867#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007868 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007869#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007870 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007871#endif
7872 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007873 /* Copy the regprog, in case it got freed and recompiled. */
7874 if (regprog_is_copy)
7875 cur->match.regprog = cur->hl.rm.regprog;
7876
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007877 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007878 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 /* Error while handling regexp: stop using this regexp. */
7880 if (shl == &search_hl)
7881 {
7882 /* don't free regprog in the match list, it's a copy */
7883 vim_regfree(shl->rm.regprog);
7884 SET_NO_HLSEARCH(TRUE);
7885 }
7886 shl->rm.regprog = NULL;
7887 shl->lnum = 0;
7888 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7889 message */
7890 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007891 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892 }
7893 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007895 else
7896 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (nmatched == 0)
7898 {
7899 shl->lnum = 0; /* no match found */
7900 break;
7901 }
7902 if (shl->rm.startpos[0].lnum > 0
7903 || shl->rm.startpos[0].col >= mincol
7904 || nmatched > 1
7905 || shl->rm.endpos[0].col > mincol)
7906 {
7907 shl->lnum += shl->rm.startpos[0].lnum;
7908 break; /* useful match found */
7909 }
7910 }
7911}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
Bram Moolenaar85077472016-10-16 14:35:48 +02007913/*
7914 * If there is a match fill "shl" and return one.
7915 * Return zero otherwise.
7916 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007917 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007918next_search_hl_pos(
7919 match_T *shl, /* points to a match */
7920 linenr_T lnum,
7921 posmatch_T *posmatch, /* match positions */
7922 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007923{
7924 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007925 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7928 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007929 llpos_T *pos = &posmatch->pos[i];
7930
7931 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007933 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007934 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007935 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007936 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007937 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007938 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007939 /* if this match comes before the one at "found" then swap
7940 * them */
7941 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007942 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007943 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007944
Bram Moolenaar85077472016-10-16 14:35:48 +02007945 *pos = posmatch->pos[found];
7946 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 }
7948 }
7949 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007950 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 }
7952 }
7953 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007954 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007956 colnr_T start = posmatch->pos[found].col == 0
7957 ? 0 : posmatch->pos[found].col - 1;
7958 colnr_T end = posmatch->pos[found].col == 0
7959 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960
Bram Moolenaar85077472016-10-16 14:35:48 +02007961 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962 shl->rm.startpos[0].lnum = 0;
7963 shl->rm.startpos[0].col = start;
7964 shl->rm.endpos[0].lnum = 0;
7965 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007966 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007967 posmatch->cur = found + 1;
7968 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007970 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007971}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007972#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007975screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 attrentry_T *aep = NULL;
7978
7979 screen_attr = attr;
7980 if (full_screen
7981#ifdef WIN3264
7982 && termcap_active
7983#endif
7984 )
7985 {
7986#ifdef FEAT_GUI
7987 if (gui.in_use)
7988 {
7989 char buf[20];
7990
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007991 /* The GUI handles this internally. */
7992 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 OUT_STR(buf);
7994 }
7995 else
7996#endif
7997 {
7998 if (attr > HL_ALL) /* special HL attr. */
7999 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008000 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 aep = syn_cterm_attr2entry(attr);
8002 else
8003 aep = syn_term_attr2entry(attr);
8004 if (aep == NULL) /* did ":syntax clear" */
8005 attr = 0;
8006 else
8007 attr = aep->ae_attr;
8008 }
8009 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8010 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008011 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008012#ifdef FEAT_TERMGUICOLORS
8013 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008014 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008015#endif
8016 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008017#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008018 )
8019#endif
8020 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008021 /* If the Normal FG color has BOLD attribute and the new HL
8022 * has a FG color defined, clear BOLD. */
8023 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8025 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008026 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8027 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 out_str(T_US);
8029 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8030 out_str(T_CZH);
8031 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8032 out_str(T_MR);
8033
8034 /*
8035 * Output the color or start string after bold etc., in case the
8036 * bold etc. override the color setting.
8037 */
8038 if (aep != NULL)
8039 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008040#ifdef FEAT_TERMGUICOLORS
8041 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008043 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008044 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008045 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008046 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008051 if (t_colors > 1)
8052 {
8053 if (aep->ae_u.cterm.fg_color)
8054 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8055 if (aep->ae_u.cterm.bg_color)
8056 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8057 }
8058 else
8059 {
8060 if (aep->ae_u.term.start != NULL)
8061 out_str(aep->ae_u.term.start);
8062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 }
8064 }
8065 }
8066 }
8067}
8068
8069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008070screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 int do_ME = FALSE; /* output T_ME code */
8073
8074 if (screen_attr != 0
8075#ifdef WIN3264
8076 && termcap_active
8077#endif
8078 )
8079 {
8080#ifdef FEAT_GUI
8081 if (gui.in_use)
8082 {
8083 char buf[20];
8084
8085 /* use internal GUI code */
8086 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8087 OUT_STR(buf);
8088 }
8089 else
8090#endif
8091 {
8092 if (screen_attr > HL_ALL) /* special HL attr. */
8093 {
8094 attrentry_T *aep;
8095
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008096 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
8098 /*
8099 * Assume that t_me restores the original colors!
8100 */
8101 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008102 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008103#ifdef FEAT_TERMGUICOLORS
8104 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008105 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8106 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008107#endif
8108 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008109#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008110 )
8111#endif
8112 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 do_ME = TRUE;
8114 }
8115 else
8116 {
8117 aep = syn_term_attr2entry(screen_attr);
8118 if (aep != NULL && aep->ae_u.term.stop != NULL)
8119 {
8120 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8121 do_ME = TRUE;
8122 else
8123 out_str(aep->ae_u.term.stop);
8124 }
8125 }
8126 if (aep == NULL) /* did ":syntax clear" */
8127 screen_attr = 0;
8128 else
8129 screen_attr = aep->ae_attr;
8130 }
8131
8132 /*
8133 * Often all ending-codes are equal to T_ME. Avoid outputting the
8134 * same sequence several times.
8135 */
8136 if (screen_attr & HL_STANDOUT)
8137 {
8138 if (STRCMP(T_SE, T_ME) == 0)
8139 do_ME = TRUE;
8140 else
8141 out_str(T_SE);
8142 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008143 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 if (STRCMP(T_UE, T_ME) == 0)
8146 do_ME = TRUE;
8147 else
8148 out_str(T_UE);
8149 }
8150 if (screen_attr & HL_ITALIC)
8151 {
8152 if (STRCMP(T_CZR, T_ME) == 0)
8153 do_ME = TRUE;
8154 else
8155 out_str(T_CZR);
8156 }
8157 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8158 out_str(T_ME);
8159
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008160#ifdef FEAT_TERMGUICOLORS
8161 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008163 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008164 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008165 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008166 term_bg_rgb_color(cterm_normal_bg_gui_color);
8167 }
8168 else
8169#endif
8170 {
8171 if (t_colors > 1)
8172 {
8173 /* set Normal cterm colors */
8174 if (cterm_normal_fg_color != 0)
8175 term_fg_color(cterm_normal_fg_color - 1);
8176 if (cterm_normal_bg_color != 0)
8177 term_bg_color(cterm_normal_bg_color - 1);
8178 if (cterm_normal_fg_bold)
8179 out_str(T_MD);
8180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 }
8182 }
8183 }
8184 screen_attr = 0;
8185}
8186
8187/*
8188 * Reset the colors for a cterm. Used when leaving Vim.
8189 * The machine specific code may override this again.
8190 */
8191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008192reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008194 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
8196 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008197#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008198 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8199 || cterm_normal_bg_gui_color != INVALCOLOR)
8200 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008201#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 out_str(T_OP);
8206 screen_attr = -1;
8207 }
8208 if (cterm_normal_fg_bold)
8209 {
8210 out_str(T_ME);
8211 screen_attr = -1;
8212 }
8213 }
8214}
8215
8216/*
8217 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8218 * using the attributes from ScreenAttrs["off"].
8219 */
8220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008221screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 int attr;
8224
8225 /* Check for illegal values, just in case (could happen just after
8226 * resizing). */
8227 if (row >= screen_Rows || col >= screen_Columns)
8228 return;
8229
Bram Moolenaar494838a2015-02-10 19:20:37 +01008230 /* Outputting a character in the last cell on the screen may scroll the
8231 * screen up. Only do it when the "xn" termcap property is set, otherwise
8232 * mark the character invalid (update it when scrolled up). */
8233 if (*T_XN == NUL
8234 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235#ifdef FEAT_RIGHTLEFT
8236 /* account for first command-line character in rightleft mode */
8237 && !cmdmsg_rl
8238#endif
8239 )
8240 {
8241 ScreenAttrs[off] = (sattr_T)-1;
8242 return;
8243 }
8244
8245 /*
8246 * Stop highlighting first, so it's easier to move the cursor.
8247 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008248#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (screen_char_attr != 0)
8250 attr = screen_char_attr;
8251 else
8252#endif
8253 attr = ScreenAttrs[off];
8254 if (screen_attr != attr)
8255 screen_stop_highlight();
8256
8257 windgoto(row, col);
8258
8259 if (screen_attr != attr)
8260 screen_start_highlight(attr);
8261
8262#ifdef FEAT_MBYTE
8263 if (enc_utf8 && ScreenLinesUC[off] != 0)
8264 {
8265 char_u buf[MB_MAXBYTES + 1];
8266
8267 /* Convert UTF-8 character to bytes and write it. */
8268
8269 buf[utfc_char2bytes(off, buf)] = NUL;
8270
8271 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008272 if (utf_ambiguous_width(ScreenLinesUC[off]))
8273 screen_cur_col = 9999;
8274 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 ++screen_cur_col;
8276 }
8277 else
8278#endif
8279 {
8280#ifdef FEAT_MBYTE
8281 out_flush_check();
8282#endif
8283 out_char(ScreenLines[off]);
8284#ifdef FEAT_MBYTE
8285 /* double-byte character in single-width cell */
8286 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8287 out_char(ScreenLines2[off]);
8288#endif
8289 }
8290
8291 screen_cur_col++;
8292}
8293
8294#ifdef FEAT_MBYTE
8295
8296/*
8297 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8298 * on the screen at position 'row' and 'col'.
8299 * The attributes of the first byte is used for all. This is required to
8300 * output the two bytes of a double-byte character with nothing in between.
8301 */
8302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008303screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304{
8305 /* Check for illegal values (could be wrong when screen was resized). */
8306 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8307 return;
8308
8309 /* Outputting the last character on the screen may scrollup the screen.
8310 * Don't to it! Mark the character invalid (update it when scrolled up) */
8311 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8312 {
8313 ScreenAttrs[off] = (sattr_T)-1;
8314 return;
8315 }
8316
8317 /* Output the first byte normally (positions the cursor), then write the
8318 * second byte directly. */
8319 screen_char(off, row, col);
8320 out_char(ScreenLines[off + 1]);
8321 ++screen_cur_col;
8322}
8323#endif
8324
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008325#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326/*
8327 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8328 * This uses the contents of ScreenLines[] and doesn't change it.
8329 */
8330 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008331screen_draw_rectangle(
8332 int row,
8333 int col,
8334 int height,
8335 int width,
8336 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 int r, c;
8339 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008340#ifdef FEAT_MBYTE
8341 int max_off;
8342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008344 /* Can't use ScreenLines unless initialized */
8345 if (ScreenLines == NULL)
8346 return;
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 if (invert)
8349 screen_char_attr = HL_INVERSE;
8350 for (r = row; r < row + height; ++r)
8351 {
8352 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008353#ifdef FEAT_MBYTE
8354 max_off = off + screen_Columns;
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 for (c = col; c < col + width; ++c)
8357 {
8358#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008359 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
8361 screen_char_2(off + c, r, c);
8362 ++c;
8363 }
8364 else
8365#endif
8366 {
8367 screen_char(off + c, r, c);
8368#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008369 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 ++c;
8371#endif
8372 }
8373 }
8374 }
8375 screen_char_attr = 0;
8376}
8377#endif
8378
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008379#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380/*
8381 * Redraw the characters for a vertically split window.
8382 */
8383 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008384redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 int col;
8387 int width;
8388
8389# ifdef FEAT_CLIPBOARD
8390 clip_may_clear_selection(row, end - 1);
8391# endif
8392
8393 if (wp == NULL)
8394 {
8395 col = 0;
8396 width = Columns;
8397 }
8398 else
8399 {
8400 col = wp->w_wincol;
8401 width = wp->w_width;
8402 }
8403 screen_draw_rectangle(row, col, end - row, width, FALSE);
8404}
8405#endif
8406
8407/*
8408 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8409 * with character 'c1' in first column followed by 'c2' in the other columns.
8410 * Use attributes 'attr'.
8411 */
8412 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008413screen_fill(
8414 int start_row,
8415 int end_row,
8416 int start_col,
8417 int end_col,
8418 int c1,
8419 int c2,
8420 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 int row;
8423 int col;
8424 int off;
8425 int end_off;
8426 int did_delete;
8427 int c;
8428 int norm_term;
8429#if defined(FEAT_GUI) || defined(UNIX)
8430 int force_next = FALSE;
8431#endif
8432
8433 if (end_row > screen_Rows) /* safety check */
8434 end_row = screen_Rows;
8435 if (end_col > screen_Columns) /* safety check */
8436 end_col = screen_Columns;
8437 if (ScreenLines == NULL
8438 || start_row >= end_row
8439 || start_col >= end_col) /* nothing to do */
8440 return;
8441
8442 /* it's a "normal" terminal when not in a GUI or cterm */
8443 norm_term = (
8444#ifdef FEAT_GUI
8445 !gui.in_use &&
8446#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008447 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 for (row = start_row; row < end_row; ++row)
8449 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008450#ifdef FEAT_MBYTE
8451 if (has_mbyte
8452# ifdef FEAT_GUI
8453 && !gui.in_use
8454# endif
8455 )
8456 {
8457 /* When drawing over the right halve of a double-wide char clear
8458 * out the left halve. When drawing over the left halve of a
8459 * double wide-char clear out the right halve. Only needed in a
8460 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008461 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008462 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008463 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008464 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008465 }
8466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 /*
8468 * Try to use delete-line termcap code, when no attributes or in a
8469 * "normal" terminal, where a bold/italic space is just a
8470 * space.
8471 */
8472 did_delete = FALSE;
8473 if (c2 == ' '
8474 && end_col == Columns
8475 && can_clear(T_CE)
8476 && (attr == 0
8477 || (norm_term
8478 && attr <= HL_ALL
8479 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8480 {
8481 /*
8482 * check if we really need to clear something
8483 */
8484 col = start_col;
8485 if (c1 != ' ') /* don't clear first char */
8486 ++col;
8487
8488 off = LineOffset[row] + col;
8489 end_off = LineOffset[row] + end_col;
8490
8491 /* skip blanks (used often, keep it fast!) */
8492#ifdef FEAT_MBYTE
8493 if (enc_utf8)
8494 while (off < end_off && ScreenLines[off] == ' '
8495 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8496 ++off;
8497 else
8498#endif
8499 while (off < end_off && ScreenLines[off] == ' '
8500 && ScreenAttrs[off] == 0)
8501 ++off;
8502 if (off < end_off) /* something to be cleared */
8503 {
8504 col = off - LineOffset[row];
8505 screen_stop_highlight();
8506 term_windgoto(row, col);/* clear rest of this screen line */
8507 out_str(T_CE);
8508 screen_start(); /* don't know where cursor is now */
8509 col = end_col - col;
8510 while (col--) /* clear chars in ScreenLines */
8511 {
8512 ScreenLines[off] = ' ';
8513#ifdef FEAT_MBYTE
8514 if (enc_utf8)
8515 ScreenLinesUC[off] = 0;
8516#endif
8517 ScreenAttrs[off] = 0;
8518 ++off;
8519 }
8520 }
8521 did_delete = TRUE; /* the chars are cleared now */
8522 }
8523
8524 off = LineOffset[row] + start_col;
8525 c = c1;
8526 for (col = start_col; col < end_col; ++col)
8527 {
8528 if (ScreenLines[off] != c
8529#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008530 || (enc_utf8 && (int)ScreenLinesUC[off]
8531 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#endif
8533 || ScreenAttrs[off] != attr
8534#if defined(FEAT_GUI) || defined(UNIX)
8535 || force_next
8536#endif
8537 )
8538 {
8539#if defined(FEAT_GUI) || defined(UNIX)
8540 /* The bold trick may make a single row of pixels appear in
8541 * the next character. When a bold character is removed, the
8542 * next character should be redrawn too. This happens for our
8543 * own GUI and for some xterms. */
8544 if (
8545# ifdef FEAT_GUI
8546 gui.in_use
8547# endif
8548# if defined(FEAT_GUI) && defined(UNIX)
8549 ||
8550# endif
8551# ifdef UNIX
8552 term_is_xterm
8553# endif
8554 )
8555 {
8556 if (ScreenLines[off] != ' '
8557 && (ScreenAttrs[off] > HL_ALL
8558 || ScreenAttrs[off] & HL_BOLD))
8559 force_next = TRUE;
8560 else
8561 force_next = FALSE;
8562 }
8563#endif
8564 ScreenLines[off] = c;
8565#ifdef FEAT_MBYTE
8566 if (enc_utf8)
8567 {
8568 if (c >= 0x80)
8569 {
8570 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008571 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 else
8574 ScreenLinesUC[off] = 0;
8575 }
8576#endif
8577 ScreenAttrs[off] = attr;
8578 if (!did_delete || c != ' ')
8579 screen_char(off, row, col);
8580 }
8581 ++off;
8582 if (col == start_col)
8583 {
8584 if (did_delete)
8585 break;
8586 c = c2;
8587 }
8588 }
8589 if (end_col == Columns)
8590 LineWraps[row] = FALSE;
8591 if (row == Rows - 1) /* overwritten the command line */
8592 {
8593 redraw_cmdline = TRUE;
8594 if (c1 == ' ' && c2 == ' ')
8595 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008596 if (start_col == 0)
8597 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 }
8599 }
8600}
8601
8602/*
8603 * Check if there should be a delay. Used before clearing or redrawing the
8604 * screen or the command line.
8605 */
8606 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008607check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8610 && !did_wait_return
8611 && emsg_silent == 0)
8612 {
8613 out_flush();
8614 ui_delay(1000L, TRUE);
8615 emsg_on_display = FALSE;
8616 if (check_msg_scroll)
8617 msg_scroll = FALSE;
8618 }
8619}
8620
8621/*
8622 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008623 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 * Returns TRUE if there is a valid screen to write to.
8625 * Returns FALSE when starting up and screen not initialized yet.
8626 */
8627 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008628screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008630 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 return (ScreenLines != NULL);
8632}
8633
8634/*
8635 * Resize the shell to Rows and Columns.
8636 * Allocate ScreenLines[] and associated items.
8637 *
8638 * There may be some time between setting Rows and Columns and (re)allocating
8639 * ScreenLines[]. This happens when starting up and when (manually) changing
8640 * the shell size. Always use screen_Rows and screen_Columns to access items
8641 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8642 * final size of the shell is needed.
8643 */
8644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008645screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 int new_row, old_row;
8648#ifdef FEAT_GUI
8649 int old_Rows;
8650#endif
8651 win_T *wp;
8652 int outofmem = FALSE;
8653 int len;
8654 schar_T *new_ScreenLines;
8655#ifdef FEAT_MBYTE
8656 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008657 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008659 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660#endif
8661 sattr_T *new_ScreenAttrs;
8662 unsigned *new_LineOffset;
8663 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008664#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008665 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008666 tabpage_T *tp;
8667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008669 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008670#ifdef FEAT_AUTOCMD
8671 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008673retry:
8674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 /*
8676 * Allocation of the screen buffers is done only when the size changes and
8677 * when Rows and Columns have been set and we have started doing full
8678 * screen stuff.
8679 */
8680 if ((ScreenLines != NULL
8681 && Rows == screen_Rows
8682 && Columns == screen_Columns
8683#ifdef FEAT_MBYTE
8684 && enc_utf8 == (ScreenLinesUC != NULL)
8685 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008686 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687#endif
8688 )
8689 || Rows == 0
8690 || Columns == 0
8691 || (!full_screen && ScreenLines == NULL))
8692 return;
8693
8694 /*
8695 * It's possible that we produce an out-of-memory message below, which
8696 * will cause this function to be called again. To break the loop, just
8697 * return here.
8698 */
8699 if (entered)
8700 return;
8701 entered = TRUE;
8702
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008703 /*
8704 * Note that the window sizes are updated before reallocating the arrays,
8705 * thus we must not redraw here!
8706 */
8707 ++RedrawingDisabled;
8708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 win_new_shellsize(); /* fit the windows in the new sized shell */
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 comp_col(); /* recompute columns for shown command and ruler */
8712
8713 /*
8714 * We're changing the size of the screen.
8715 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8716 * - Move lines from the old arrays into the new arrays, clear extra
8717 * lines (unless the screen is going to be cleared).
8718 * - Free the old arrays.
8719 *
8720 * If anything fails, make ScreenLines NULL, so we don't do anything!
8721 * Continuing with the old ScreenLines may result in a crash, because the
8722 * size is wrong.
8723 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008724 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008726#ifdef FEAT_AUTOCMD
8727 if (aucmd_win != NULL)
8728 win_free_lsize(aucmd_win);
8729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730
8731 new_ScreenLines = (schar_T *)lalloc((long_u)(
8732 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8733#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008734 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 if (enc_utf8)
8736 {
8737 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8738 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008740 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8742 }
8743 if (enc_dbcs == DBCS_JPNU)
8744 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8745 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8746#endif
8747 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8748 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8749 new_LineOffset = (unsigned *)lalloc((long_u)(
8750 Rows * sizeof(unsigned)), FALSE);
8751 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008752#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008753 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008756 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 {
8758 if (win_alloc_lines(wp) == FAIL)
8759 {
8760 outofmem = TRUE;
8761#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008762 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763#endif
8764 }
8765 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008766#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008767 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8768 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008769 outofmem = TRUE;
8770#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008771#ifdef FEAT_WINDOWS
8772give_up:
8773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008775#ifdef FEAT_MBYTE
8776 for (i = 0; i < p_mco; ++i)
8777 if (new_ScreenLinesC[i] == NULL)
8778 break;
8779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 if (new_ScreenLines == NULL
8781#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008782 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8784#endif
8785 || new_ScreenAttrs == NULL
8786 || new_LineOffset == NULL
8787 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008788#ifdef FEAT_WINDOWS
8789 || new_TabPageIdxs == NULL
8790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 || outofmem)
8792 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008793 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008794 {
8795 /* guess the size */
8796 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8797
8798 /* Remember we did this to avoid getting outofmem messages over
8799 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008800 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 vim_free(new_ScreenLines);
8803 new_ScreenLines = NULL;
8804#ifdef FEAT_MBYTE
8805 vim_free(new_ScreenLinesUC);
8806 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008807 for (i = 0; i < p_mco; ++i)
8808 {
8809 vim_free(new_ScreenLinesC[i]);
8810 new_ScreenLinesC[i] = NULL;
8811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 vim_free(new_ScreenLines2);
8813 new_ScreenLines2 = NULL;
8814#endif
8815 vim_free(new_ScreenAttrs);
8816 new_ScreenAttrs = NULL;
8817 vim_free(new_LineOffset);
8818 new_LineOffset = NULL;
8819 vim_free(new_LineWraps);
8820 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008821#ifdef FEAT_WINDOWS
8822 vim_free(new_TabPageIdxs);
8823 new_TabPageIdxs = NULL;
8824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826 else
8827 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008828 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 for (new_row = 0; new_row < Rows; ++new_row)
8831 {
8832 new_LineOffset[new_row] = new_row * Columns;
8833 new_LineWraps[new_row] = FALSE;
8834
8835 /*
8836 * If the screen is not going to be cleared, copy as much as
8837 * possible from the old screen to the new one and clear the rest
8838 * (used when resizing the window at the "--more--" prompt or when
8839 * executing an external command, for the GUI).
8840 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008841 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
8843 (void)vim_memset(new_ScreenLines + new_row * Columns,
8844 ' ', (size_t)Columns * sizeof(schar_T));
8845#ifdef FEAT_MBYTE
8846 if (enc_utf8)
8847 {
8848 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8849 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008850 for (i = 0; i < p_mco; ++i)
8851 (void)vim_memset(new_ScreenLinesC[i]
8852 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 0, (size_t)Columns * sizeof(u8char_T));
8854 }
8855 if (enc_dbcs == DBCS_JPNU)
8856 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8857 0, (size_t)Columns * sizeof(schar_T));
8858#endif
8859 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8860 0, (size_t)Columns * sizeof(sattr_T));
8861 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008862 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 {
8864 if (screen_Columns < Columns)
8865 len = screen_Columns;
8866 else
8867 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008868#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008869 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008870 * may be invalid now. Also when p_mco changes. */
8871 if (!(enc_utf8 && ScreenLinesUC == NULL)
8872 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008873#endif
8874 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8875 ScreenLines + LineOffset[old_row],
8876 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008878 if (enc_utf8 && ScreenLinesUC != NULL
8879 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8882 ScreenLinesUC + LineOffset[old_row],
8883 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008884 for (i = 0; i < p_mco; ++i)
8885 mch_memmove(new_ScreenLinesC[i]
8886 + new_LineOffset[new_row],
8887 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 (size_t)len * sizeof(u8char_T));
8889 }
8890 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8891 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8892 ScreenLines2 + LineOffset[old_row],
8893 (size_t)len * sizeof(schar_T));
8894#endif
8895 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8896 ScreenAttrs + LineOffset[old_row],
8897 (size_t)len * sizeof(sattr_T));
8898 }
8899 }
8900 }
8901 /* Use the last line of the screen for the current line. */
8902 current_ScreenLine = new_ScreenLines + Rows * Columns;
8903 }
8904
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008905 free_screenlines();
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 ScreenLines = new_ScreenLines;
8908#ifdef FEAT_MBYTE
8909 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
8911 ScreenLinesC[i] = new_ScreenLinesC[i];
8912 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 ScreenLines2 = new_ScreenLines2;
8914#endif
8915 ScreenAttrs = new_ScreenAttrs;
8916 LineOffset = new_LineOffset;
8917 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008918#ifdef FEAT_WINDOWS
8919 TabPageIdxs = new_TabPageIdxs;
8920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921
8922 /* It's important that screen_Rows and screen_Columns reflect the actual
8923 * size of ScreenLines[]. Set them before calling anything. */
8924#ifdef FEAT_GUI
8925 old_Rows = screen_Rows;
8926#endif
8927 screen_Rows = Rows;
8928 screen_Columns = Columns;
8929
8930 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008931 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 screenclear2();
8933
8934#ifdef FEAT_GUI
8935 else if (gui.in_use
8936 && !gui.starting
8937 && ScreenLines != NULL
8938 && old_Rows != Rows)
8939 {
8940 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8941 /*
8942 * Adjust the position of the cursor, for when executing an external
8943 * command.
8944 */
8945 if (msg_row >= Rows) /* Rows got smaller */
8946 msg_row = Rows - 1; /* put cursor at last row */
8947 else if (Rows > old_Rows) /* Rows got bigger */
8948 msg_row += Rows - old_Rows; /* put cursor in same place */
8949 if (msg_col >= Columns) /* Columns got smaller */
8950 msg_col = Columns - 1; /* put cursor at last column */
8951 }
8952#endif
8953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008955 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008956
8957#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008958 /*
8959 * Do not apply autocommands more than 3 times to avoid an endless loop
8960 * in case applying autocommands always changes Rows or Columns.
8961 */
8962 if (starting == 0 && ++retry_count <= 3)
8963 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008964 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008965 /* In rare cases, autocommands may have altered Rows or Columns,
8966 * jump back to check if we need to allocate the screen again. */
8967 goto retry;
8968 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
8972 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008973free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008974{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008975#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008976 int i;
8977
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008978 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008979 for (i = 0; i < Screen_mco; ++i)
8980 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008981 vim_free(ScreenLines2);
8982#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008983 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008984 vim_free(ScreenAttrs);
8985 vim_free(LineOffset);
8986 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008987#ifdef FEAT_WINDOWS
8988 vim_free(TabPageIdxs);
8989#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008990}
8991
8992 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008993screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 check_for_delay(FALSE);
8996 screenalloc(FALSE); /* allocate screen buffers if size changed */
8997 screenclear2(); /* clear the screen */
8998}
8999
9000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009001screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002{
9003 int i;
9004
9005 if (starting == NO_SCREEN || ScreenLines == NULL
9006#ifdef FEAT_GUI
9007 || (gui.in_use && gui.starting)
9008#endif
9009 )
9010 return;
9011
9012#ifdef FEAT_GUI
9013 if (!gui.in_use)
9014#endif
9015 screen_attr = -1; /* force setting the Normal colors */
9016 screen_stop_highlight(); /* don't want highlighting here */
9017
9018#ifdef FEAT_CLIPBOARD
9019 /* disable selection without redrawing it */
9020 clip_scroll_selection(9999);
9021#endif
9022
9023 /* blank out ScreenLines */
9024 for (i = 0; i < Rows; ++i)
9025 {
9026 lineclear(LineOffset[i], (int)Columns);
9027 LineWraps[i] = FALSE;
9028 }
9029
9030 if (can_clear(T_CL))
9031 {
9032 out_str(T_CL); /* clear the display */
9033 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009034 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 }
9036 else
9037 {
9038 /* can't clear the screen, mark all chars with invalid attributes */
9039 for (i = 0; i < Rows; ++i)
9040 lineinvalid(LineOffset[i], (int)Columns);
9041 clear_cmdline = TRUE;
9042 }
9043
9044 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9045
9046 win_rest_invalid(firstwin);
9047 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009048#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009049 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 if (must_redraw == CLEAR) /* no need to clear again */
9052 must_redraw = NOT_VALID;
9053 compute_cmdrow();
9054 msg_row = cmdline_row; /* put cursor on last line for messages */
9055 msg_col = 0;
9056 screen_start(); /* don't know where cursor is now */
9057 msg_scrolled = 0; /* can't scroll back */
9058 msg_didany = FALSE;
9059 msg_didout = FALSE;
9060}
9061
9062/*
9063 * Clear one line in ScreenLines.
9064 */
9065 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009066lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9069#ifdef FEAT_MBYTE
9070 if (enc_utf8)
9071 (void)vim_memset(ScreenLinesUC + off, 0,
9072 (size_t)width * sizeof(u8char_T));
9073#endif
9074 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9075}
9076
9077/*
9078 * Mark one line in ScreenLines invalid by setting the attributes to an
9079 * invalid value.
9080 */
9081 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009082lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9085}
9086
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009087#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088/*
9089 * Copy part of a Screenline for vertically split window "wp".
9090 */
9091 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009092linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 unsigned off_to = LineOffset[to] + wp->w_wincol;
9095 unsigned off_from = LineOffset[from] + wp->w_wincol;
9096
9097 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9098 wp->w_width * sizeof(schar_T));
9099# ifdef FEAT_MBYTE
9100 if (enc_utf8)
9101 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009102 int i;
9103
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9105 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009106 for (i = 0; i < p_mco; ++i)
9107 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9108 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 }
9110 if (enc_dbcs == DBCS_JPNU)
9111 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9112 wp->w_width * sizeof(schar_T));
9113# endif
9114 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9115 wp->w_width * sizeof(sattr_T));
9116}
9117#endif
9118
9119/*
9120 * Return TRUE if clearing with term string "p" would work.
9121 * It can't work when the string is empty or it won't set the right background.
9122 */
9123 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009124can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 return (*p != NUL && (t_colors <= 1
9127#ifdef FEAT_GUI
9128 || gui.in_use
9129#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009130#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009131 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009132 || (!p_tgc && cterm_normal_bg_color == 0)
9133#else
9134 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009135#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009136 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138
9139/*
9140 * Reset cursor position. Use whenever cursor was moved because of outputting
9141 * something directly to the screen (shell commands) or a terminal control
9142 * code.
9143 */
9144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009145screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 screen_cur_row = screen_cur_col = 9999;
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * Move the cursor to position "row","col" in the screen.
9152 * This tries to find the most efficient way to move, minimizing the number of
9153 * characters sent to the terminal.
9154 */
9155 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009156windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009158 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 int i;
9160 int plan;
9161 int cost;
9162 int wouldbe_col;
9163 int noinvcurs;
9164 char_u *bs;
9165 int goto_cost;
9166 int attr;
9167
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009168#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9170
9171#define PLAN_LE 1
9172#define PLAN_CR 2
9173#define PLAN_NL 3
9174#define PLAN_WRITE 4
9175 /* Can't use ScreenLines unless initialized */
9176 if (ScreenLines == NULL)
9177 return;
9178
9179 if (col != screen_cur_col || row != screen_cur_row)
9180 {
9181 /* Check for valid position. */
9182 if (row < 0) /* window without text lines? */
9183 row = 0;
9184 if (row >= screen_Rows)
9185 row = screen_Rows - 1;
9186 if (col >= screen_Columns)
9187 col = screen_Columns - 1;
9188
9189 /* check if no cursor movement is allowed in highlight mode */
9190 if (screen_attr && *T_MS == NUL)
9191 noinvcurs = HIGHL_COST;
9192 else
9193 noinvcurs = 0;
9194 goto_cost = GOTO_COST + noinvcurs;
9195
9196 /*
9197 * Plan how to do the positioning:
9198 * 1. Use CR to move it to column 0, same row.
9199 * 2. Use T_LE to move it a few columns to the left.
9200 * 3. Use NL to move a few lines down, column 0.
9201 * 4. Move a few columns to the right with T_ND or by writing chars.
9202 *
9203 * Don't do this if the cursor went beyond the last column, the cursor
9204 * position is unknown then (some terminals wrap, some don't )
9205 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009206 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * characters to move the cursor to the right.
9208 */
9209 if (row >= screen_cur_row && screen_cur_col < Columns)
9210 {
9211 /*
9212 * If the cursor is in the same row, bigger col, we can use CR
9213 * or T_LE.
9214 */
9215 bs = NULL; /* init for GCC */
9216 attr = screen_attr;
9217 if (row == screen_cur_row && col < screen_cur_col)
9218 {
9219 /* "le" is preferred over "bc", because "bc" is obsolete */
9220 if (*T_LE)
9221 bs = T_LE; /* "cursor left" */
9222 else
9223 bs = T_BC; /* "backspace character (old) */
9224 if (*bs)
9225 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9226 else
9227 cost = 999;
9228 if (col + 1 < cost) /* using CR is less characters */
9229 {
9230 plan = PLAN_CR;
9231 wouldbe_col = 0;
9232 cost = 1; /* CR is just one character */
9233 }
9234 else
9235 {
9236 plan = PLAN_LE;
9237 wouldbe_col = col;
9238 }
9239 if (noinvcurs) /* will stop highlighting */
9240 {
9241 cost += noinvcurs;
9242 attr = 0;
9243 }
9244 }
9245
9246 /*
9247 * If the cursor is above where we want to be, we can use CR LF.
9248 */
9249 else if (row > screen_cur_row)
9250 {
9251 plan = PLAN_NL;
9252 wouldbe_col = 0;
9253 cost = (row - screen_cur_row) * 2; /* CR LF */
9254 if (noinvcurs) /* will stop highlighting */
9255 {
9256 cost += noinvcurs;
9257 attr = 0;
9258 }
9259 }
9260
9261 /*
9262 * If the cursor is in the same row, smaller col, just use write.
9263 */
9264 else
9265 {
9266 plan = PLAN_WRITE;
9267 wouldbe_col = screen_cur_col;
9268 cost = 0;
9269 }
9270
9271 /*
9272 * Check if any characters that need to be written have the
9273 * correct attributes. Also avoid UTF-8 characters.
9274 */
9275 i = col - wouldbe_col;
9276 if (i > 0)
9277 cost += i;
9278 if (cost < goto_cost && i > 0)
9279 {
9280 /*
9281 * Check if the attributes are correct without additionally
9282 * stopping highlighting.
9283 */
9284 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9285 while (i && *p++ == attr)
9286 --i;
9287 if (i != 0)
9288 {
9289 /*
9290 * Try if it works when highlighting is stopped here.
9291 */
9292 if (*--p == 0)
9293 {
9294 cost += noinvcurs;
9295 while (i && *p++ == 0)
9296 --i;
9297 }
9298 if (i != 0)
9299 cost = 999; /* different attributes, don't do it */
9300 }
9301#ifdef FEAT_MBYTE
9302 if (enc_utf8)
9303 {
9304 /* Don't use an UTF-8 char for positioning, it's slow. */
9305 for (i = wouldbe_col; i < col; ++i)
9306 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9307 {
9308 cost = 999;
9309 break;
9310 }
9311 }
9312#endif
9313 }
9314
9315 /*
9316 * We can do it without term_windgoto()!
9317 */
9318 if (cost < goto_cost)
9319 {
9320 if (plan == PLAN_LE)
9321 {
9322 if (noinvcurs)
9323 screen_stop_highlight();
9324 while (screen_cur_col > col)
9325 {
9326 out_str(bs);
9327 --screen_cur_col;
9328 }
9329 }
9330 else if (plan == PLAN_CR)
9331 {
9332 if (noinvcurs)
9333 screen_stop_highlight();
9334 out_char('\r');
9335 screen_cur_col = 0;
9336 }
9337 else if (plan == PLAN_NL)
9338 {
9339 if (noinvcurs)
9340 screen_stop_highlight();
9341 while (screen_cur_row < row)
9342 {
9343 out_char('\n');
9344 ++screen_cur_row;
9345 }
9346 screen_cur_col = 0;
9347 }
9348
9349 i = col - screen_cur_col;
9350 if (i > 0)
9351 {
9352 /*
9353 * Use cursor-right if it's one character only. Avoids
9354 * removing a line of pixels from the last bold char, when
9355 * using the bold trick in the GUI.
9356 */
9357 if (T_ND[0] != NUL && T_ND[1] == NUL)
9358 {
9359 while (i-- > 0)
9360 out_char(*T_ND);
9361 }
9362 else
9363 {
9364 int off;
9365
9366 off = LineOffset[row] + screen_cur_col;
9367 while (i-- > 0)
9368 {
9369 if (ScreenAttrs[off] != screen_attr)
9370 screen_stop_highlight();
9371#ifdef FEAT_MBYTE
9372 out_flush_check();
9373#endif
9374 out_char(ScreenLines[off]);
9375#ifdef FEAT_MBYTE
9376 if (enc_dbcs == DBCS_JPNU
9377 && ScreenLines[off] == 0x8e)
9378 out_char(ScreenLines2[off]);
9379#endif
9380 ++off;
9381 }
9382 }
9383 }
9384 }
9385 }
9386 else
9387 cost = 999;
9388
9389 if (cost >= goto_cost)
9390 {
9391 if (noinvcurs)
9392 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009393 if (row == screen_cur_row && (col > screen_cur_col)
9394 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 term_cursor_right(col - screen_cur_col);
9396 else
9397 term_windgoto(row, col);
9398 }
9399 screen_cur_row = row;
9400 screen_cur_col = col;
9401 }
9402}
9403
9404/*
9405 * Set cursor to its position in the current window.
9406 */
9407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009408setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 if (redrawing())
9411 {
9412 validate_cursor();
9413 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9414 W_WINCOL(curwin) + (
9415#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009416 /* With 'rightleft' set and the cursor on a double-wide
9417 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9419# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009420 (has_mbyte
9421 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9422 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423# endif
9424 1)) :
9425#endif
9426 curwin->w_wcol));
9427 }
9428}
9429
9430
9431/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009432 * Insert 'line_count' lines at 'row' in window 'wp'.
9433 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9434 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 * scrolling.
9436 * Returns FAIL if the lines are not inserted, OK for success.
9437 */
9438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009439win_ins_lines(
9440 win_T *wp,
9441 int row,
9442 int line_count,
9443 int invalid,
9444 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 int did_delete;
9447 int nextrow;
9448 int lastrow;
9449 int retval;
9450
9451 if (invalid)
9452 wp->w_lines_valid = 0;
9453
9454 if (wp->w_height < 5)
9455 return FAIL;
9456
9457 if (line_count > wp->w_height - row)
9458 line_count = wp->w_height - row;
9459
9460 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9461 if (retval != MAYBE)
9462 return retval;
9463
9464 /*
9465 * If there is a next window or a status line, we first try to delete the
9466 * lines at the bottom to avoid messing what is after the window.
9467 * If this fails and there are following windows, don't do anything to avoid
9468 * messing up those windows, better just redraw.
9469 */
9470 did_delete = FALSE;
9471#ifdef FEAT_WINDOWS
9472 if (wp->w_next != NULL || wp->w_status_height)
9473 {
9474 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9475 line_count, (int)Rows, FALSE, NULL) == OK)
9476 did_delete = TRUE;
9477 else if (wp->w_next)
9478 return FAIL;
9479 }
9480#endif
9481 /*
9482 * if no lines deleted, blank the lines that will end up below the window
9483 */
9484 if (!did_delete)
9485 {
9486#ifdef FEAT_WINDOWS
9487 wp->w_redr_status = TRUE;
9488#endif
9489 redraw_cmdline = TRUE;
9490 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9491 lastrow = nextrow + line_count;
9492 if (lastrow > Rows)
9493 lastrow = Rows;
9494 screen_fill(nextrow - line_count, lastrow - line_count,
9495 W_WINCOL(wp), (int)W_ENDCOL(wp),
9496 ' ', ' ', 0);
9497 }
9498
9499 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9500 == FAIL)
9501 {
9502 /* deletion will have messed up other windows */
9503 if (did_delete)
9504 {
9505#ifdef FEAT_WINDOWS
9506 wp->w_redr_status = TRUE;
9507#endif
9508 win_rest_invalid(W_NEXT(wp));
9509 }
9510 return FAIL;
9511 }
9512
9513 return OK;
9514}
9515
9516/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009517 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9519 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9520 * scrolling
9521 * Return OK for success, FAIL if the lines are not deleted.
9522 */
9523 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009524win_del_lines(
9525 win_T *wp,
9526 int row,
9527 int line_count,
9528 int invalid,
9529 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
9531 int retval;
9532
9533 if (invalid)
9534 wp->w_lines_valid = 0;
9535
9536 if (line_count > wp->w_height - row)
9537 line_count = wp->w_height - row;
9538
9539 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9540 if (retval != MAYBE)
9541 return retval;
9542
9543 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9544 (int)Rows, FALSE, NULL) == FAIL)
9545 return FAIL;
9546
9547#ifdef FEAT_WINDOWS
9548 /*
9549 * If there are windows or status lines below, try to put them at the
9550 * correct place. If we can't do that, they have to be redrawn.
9551 */
9552 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9553 {
9554 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9555 line_count, (int)Rows, NULL) == FAIL)
9556 {
9557 wp->w_redr_status = TRUE;
9558 win_rest_invalid(wp->w_next);
9559 }
9560 }
9561 /*
9562 * If this is the last window and there is no status line, redraw the
9563 * command line later.
9564 */
9565 else
9566#endif
9567 redraw_cmdline = TRUE;
9568 return OK;
9569}
9570
9571/*
9572 * Common code for win_ins_lines() and win_del_lines().
9573 * Returns OK or FAIL when the work has been done.
9574 * Returns MAYBE when not finished yet.
9575 */
9576 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009577win_do_lines(
9578 win_T *wp,
9579 int row,
9580 int line_count,
9581 int mayclear,
9582 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583{
9584 int retval;
9585
9586 if (!redrawing() || line_count <= 0)
9587 return FAIL;
9588
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009589 /* When inserting lines would result in loss of command output, just redraw
9590 * the lines. */
9591 if (no_win_do_lines_ins && !del)
9592 return FAIL;
9593
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 /* only a few lines left: redraw is faster */
9595 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009596#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 && wp->w_width == Columns
9598#endif
9599 )
9600 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009601 if (!no_win_do_lines_ins)
9602 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 return FAIL;
9604 }
9605
9606 /*
9607 * Delete all remaining lines
9608 */
9609 if (row + line_count >= wp->w_height)
9610 {
9611 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9612 W_WINCOL(wp), (int)W_ENDCOL(wp),
9613 ' ', ' ', 0);
9614 return OK;
9615 }
9616
9617 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009618 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009620 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009622 if (!no_win_do_lines_ins)
9623 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
9625 /*
9626 * If the terminal can set a scroll region, use that.
9627 * Always do this in a vertically split window. This will redraw from
9628 * ScreenLines[] when t_CV isn't defined. That's faster than using
9629 * win_line().
9630 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009631 * a character in the lower right corner of the scroll region may cause a
9632 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 */
9634 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009635#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 || W_WIDTH(wp) != Columns
9637#endif
9638 )
9639 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009640#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9642#endif
9643 scroll_region_set(wp, row);
9644 if (del)
9645 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9646 wp->w_height - row, FALSE, wp);
9647 else
9648 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9649 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009650#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9652#endif
9653 scroll_region_reset();
9654 return retval;
9655 }
9656
9657#ifdef FEAT_WINDOWS
9658 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9659 return FAIL;
9660#endif
9661
9662 return MAYBE;
9663}
9664
9665/*
9666 * window 'wp' and everything after it is messed up, mark it for redraw
9667 */
9668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009669win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671#ifdef FEAT_WINDOWS
9672 while (wp != NULL)
9673#else
9674 if (wp != NULL)
9675#endif
9676 {
9677 redraw_win_later(wp, NOT_VALID);
9678#ifdef FEAT_WINDOWS
9679 wp->w_redr_status = TRUE;
9680 wp = wp->w_next;
9681#endif
9682 }
9683 redraw_cmdline = TRUE;
9684}
9685
9686/*
9687 * The rest of the routines in this file perform screen manipulations. The
9688 * given operation is performed physically on the screen. The corresponding
9689 * change is also made to the internal screen image. In this way, the editor
9690 * anticipates the effect of editing changes on the appearance of the screen.
9691 * That way, when we call screenupdate a complete redraw isn't usually
9692 * necessary. Another advantage is that we can keep adding code to anticipate
9693 * screen changes, and in the meantime, everything still works.
9694 */
9695
9696/*
9697 * types for inserting or deleting lines
9698 */
9699#define USE_T_CAL 1
9700#define USE_T_CDL 2
9701#define USE_T_AL 3
9702#define USE_T_CE 4
9703#define USE_T_DL 5
9704#define USE_T_SR 6
9705#define USE_NL 7
9706#define USE_T_CD 8
9707#define USE_REDRAW 9
9708
9709/*
9710 * insert lines on the screen and update ScreenLines[]
9711 * 'end' is the line after the scrolled part. Normally it is Rows.
9712 * When scrolling region used 'off' is the offset from the top for the region.
9713 * 'row' and 'end' are relative to the start of the region.
9714 *
9715 * return FAIL for failure, OK for success.
9716 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009717 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009718screen_ins_lines(
9719 int off,
9720 int row,
9721 int line_count,
9722 int end,
9723 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 int i;
9726 int j;
9727 unsigned temp;
9728 int cursor_row;
9729 int type;
9730 int result_empty;
9731 int can_ce = can_clear(T_CE);
9732
9733 /*
9734 * FAIL if
9735 * - there is no valid screen
9736 * - the screen has to be redrawn completely
9737 * - the line count is less than one
9738 * - the line count is more than 'ttyscroll'
9739 */
9740 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9741 return FAIL;
9742
9743 /*
9744 * There are seven ways to insert lines:
9745 * 0. When in a vertically split window and t_CV isn't set, redraw the
9746 * characters from ScreenLines[].
9747 * 1. Use T_CD (clear to end of display) if it exists and the result of
9748 * the insert is just empty lines
9749 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9750 * present or line_count > 1. It looks better if we do all the inserts
9751 * at once.
9752 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9753 * insert is just empty lines and T_CE is not present or line_count >
9754 * 1.
9755 * 4. Use T_AL (insert line) if it exists.
9756 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9757 * just empty lines.
9758 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9759 * just empty lines.
9760 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9761 * the 'da' flag is not set or we have clear line capability.
9762 * 8. redraw the characters from ScreenLines[].
9763 *
9764 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9765 * the scrollbar for the window. It does have insert line, use that if it
9766 * exists.
9767 */
9768 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009769#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9771 type = USE_REDRAW;
9772 else
9773#endif
9774 if (can_clear(T_CD) && result_empty)
9775 type = USE_T_CD;
9776 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9777 type = USE_T_CAL;
9778 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9779 type = USE_T_CDL;
9780 else if (*T_AL != NUL)
9781 type = USE_T_AL;
9782 else if (can_ce && result_empty)
9783 type = USE_T_CE;
9784 else if (*T_DL != NUL && result_empty)
9785 type = USE_T_DL;
9786 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9787 type = USE_T_SR;
9788 else
9789 return FAIL;
9790
9791 /*
9792 * For clearing the lines screen_del_lines() is used. This will also take
9793 * care of t_db if necessary.
9794 */
9795 if (type == USE_T_CD || type == USE_T_CDL ||
9796 type == USE_T_CE || type == USE_T_DL)
9797 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9798
9799 /*
9800 * If text is retained below the screen, first clear or delete as many
9801 * lines at the bottom of the window as are about to be inserted so that
9802 * the deleted lines won't later surface during a screen_del_lines.
9803 */
9804 if (*T_DB)
9805 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9806
9807#ifdef FEAT_CLIPBOARD
9808 /* Remove a modeless selection when inserting lines halfway the screen
9809 * or not the full width of the screen. */
9810 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009811# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 || (wp != NULL && wp->w_width != Columns)
9813# endif
9814 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009815 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 else
9817 clip_scroll_selection(-line_count);
9818#endif
9819
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820#ifdef FEAT_GUI
9821 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9822 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009823 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#endif
9825
9826 if (*T_CCS != NUL) /* cursor relative to region */
9827 cursor_row = row;
9828 else
9829 cursor_row = row + off;
9830
9831 /*
9832 * Shift LineOffset[] line_count down to reflect the inserted lines.
9833 * Clear the inserted lines in ScreenLines[].
9834 */
9835 row += off;
9836 end += off;
9837 for (i = 0; i < line_count; ++i)
9838 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009839#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 if (wp != NULL && wp->w_width != Columns)
9841 {
9842 /* need to copy part of a line */
9843 j = end - 1 - i;
9844 while ((j -= line_count) >= row)
9845 linecopy(j + line_count, j, wp);
9846 j += line_count;
9847 if (can_clear((char_u *)" "))
9848 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9849 else
9850 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9851 LineWraps[j] = FALSE;
9852 }
9853 else
9854#endif
9855 {
9856 j = end - 1 - i;
9857 temp = LineOffset[j];
9858 while ((j -= line_count) >= row)
9859 {
9860 LineOffset[j + line_count] = LineOffset[j];
9861 LineWraps[j + line_count] = LineWraps[j];
9862 }
9863 LineOffset[j + line_count] = temp;
9864 LineWraps[j + line_count] = FALSE;
9865 if (can_clear((char_u *)" "))
9866 lineclear(temp, (int)Columns);
9867 else
9868 lineinvalid(temp, (int)Columns);
9869 }
9870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
9872 screen_stop_highlight();
9873 windgoto(cursor_row, 0);
9874
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009875#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 /* redraw the characters */
9877 if (type == USE_REDRAW)
9878 redraw_block(row, end, wp);
9879 else
9880#endif
9881 if (type == USE_T_CAL)
9882 {
9883 term_append_lines(line_count);
9884 screen_start(); /* don't know where cursor is now */
9885 }
9886 else
9887 {
9888 for (i = 0; i < line_count; i++)
9889 {
9890 if (type == USE_T_AL)
9891 {
9892 if (i && cursor_row != 0)
9893 windgoto(cursor_row, 0);
9894 out_str(T_AL);
9895 }
9896 else /* type == USE_T_SR */
9897 out_str(T_SR);
9898 screen_start(); /* don't know where cursor is now */
9899 }
9900 }
9901
9902 /*
9903 * With scroll-reverse and 'da' flag set we need to clear the lines that
9904 * have been scrolled down into the region.
9905 */
9906 if (type == USE_T_SR && *T_DA)
9907 {
9908 for (i = 0; i < line_count; ++i)
9909 {
9910 windgoto(off + i, 0);
9911 out_str(T_CE);
9912 screen_start(); /* don't know where cursor is now */
9913 }
9914 }
9915
9916#ifdef FEAT_GUI
9917 gui_can_update_cursor();
9918 if (gui.in_use)
9919 out_flush(); /* always flush after a scroll */
9920#endif
9921 return OK;
9922}
9923
9924/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009925 * Delete lines on the screen and update ScreenLines[].
9926 * "end" is the line after the scrolled part. Normally it is Rows.
9927 * When scrolling region used "off" is the offset from the top for the region.
9928 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 *
9930 * Return OK for success, FAIL if the lines are not deleted.
9931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009933screen_del_lines(
9934 int off,
9935 int row,
9936 int line_count,
9937 int end,
9938 int force, /* even when line_count > p_ttyscroll */
9939 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 int j;
9942 int i;
9943 unsigned temp;
9944 int cursor_row;
9945 int cursor_end;
9946 int result_empty; /* result is empty until end of region */
9947 int can_delete; /* deleting line codes can be used */
9948 int type;
9949
9950 /*
9951 * FAIL if
9952 * - there is no valid screen
9953 * - the screen has to be redrawn completely
9954 * - the line count is less than one
9955 * - the line count is more than 'ttyscroll'
9956 */
9957 if (!screen_valid(TRUE) || line_count <= 0 ||
9958 (!force && line_count > p_ttyscroll))
9959 return FAIL;
9960
9961 /*
9962 * Check if the rest of the current region will become empty.
9963 */
9964 result_empty = row + line_count >= end;
9965
9966 /*
9967 * We can delete lines only when 'db' flag not set or when 'ce' option
9968 * available.
9969 */
9970 can_delete = (*T_DB == NUL || can_clear(T_CE));
9971
9972 /*
9973 * There are six ways to delete lines:
9974 * 0. When in a vertically split window and t_CV isn't set, redraw the
9975 * characters from ScreenLines[].
9976 * 1. Use T_CD if it exists and the result is empty.
9977 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9978 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9979 * none of the other ways work.
9980 * 4. Use T_CE (erase line) if the result is empty.
9981 * 5. Use T_DL (delete line) if it exists.
9982 * 6. redraw the characters from ScreenLines[].
9983 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009984#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9986 type = USE_REDRAW;
9987 else
9988#endif
9989 if (can_clear(T_CD) && result_empty)
9990 type = USE_T_CD;
9991#if defined(__BEOS__) && defined(BEOS_DR8)
9992 /*
9993 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9994 * its internal termcap... this works okay for tests which test *T_DB !=
9995 * NUL. It has the disadvantage that the user cannot use any :set t_*
9996 * command to get T_DB (back) to empty_option, only :set term=... will do
9997 * the trick...
9998 * Anyway, this hack will hopefully go away with the next OS release.
9999 * (Olaf Seibert)
10000 */
10001 else if (row == 0 && T_DB == empty_option
10002 && (line_count == 1 || *T_CDL == NUL))
10003#else
10004 else if (row == 0 && (
10005#ifndef AMIGA
10006 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10007 * up, so use delete-line command */
10008 line_count == 1 ||
10009#endif
10010 *T_CDL == NUL))
10011#endif
10012 type = USE_NL;
10013 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10014 type = USE_T_CDL;
10015 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010016#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 && (wp == NULL || wp->w_width == Columns)
10018#endif
10019 )
10020 type = USE_T_CE;
10021 else if (*T_DL != NUL && can_delete)
10022 type = USE_T_DL;
10023 else if (*T_CDL != NUL && can_delete)
10024 type = USE_T_CDL;
10025 else
10026 return FAIL;
10027
10028#ifdef FEAT_CLIPBOARD
10029 /* Remove a modeless selection when deleting lines halfway the screen or
10030 * not the full width of the screen. */
10031 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010032# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 || (wp != NULL && wp->w_width != Columns)
10034# endif
10035 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010036 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else
10038 clip_scroll_selection(line_count);
10039#endif
10040
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041#ifdef FEAT_GUI
10042 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10043 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010044 gui_dont_update_cursor(gui.cursor_row >= row + off
10045 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046#endif
10047
10048 if (*T_CCS != NUL) /* cursor relative to region */
10049 {
10050 cursor_row = row;
10051 cursor_end = end;
10052 }
10053 else
10054 {
10055 cursor_row = row + off;
10056 cursor_end = end + off;
10057 }
10058
10059 /*
10060 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10061 * Clear the inserted lines in ScreenLines[].
10062 */
10063 row += off;
10064 end += off;
10065 for (i = 0; i < line_count; ++i)
10066 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010067#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 if (wp != NULL && wp->w_width != Columns)
10069 {
10070 /* need to copy part of a line */
10071 j = row + i;
10072 while ((j += line_count) <= end - 1)
10073 linecopy(j - line_count, j, wp);
10074 j -= line_count;
10075 if (can_clear((char_u *)" "))
10076 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10077 else
10078 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10079 LineWraps[j] = FALSE;
10080 }
10081 else
10082#endif
10083 {
10084 /* whole width, moving the line pointers is faster */
10085 j = row + i;
10086 temp = LineOffset[j];
10087 while ((j += line_count) <= end - 1)
10088 {
10089 LineOffset[j - line_count] = LineOffset[j];
10090 LineWraps[j - line_count] = LineWraps[j];
10091 }
10092 LineOffset[j - line_count] = temp;
10093 LineWraps[j - line_count] = FALSE;
10094 if (can_clear((char_u *)" "))
10095 lineclear(temp, (int)Columns);
10096 else
10097 lineinvalid(temp, (int)Columns);
10098 }
10099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
10101 screen_stop_highlight();
10102
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010103#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 /* redraw the characters */
10105 if (type == USE_REDRAW)
10106 redraw_block(row, end, wp);
10107 else
10108#endif
10109 if (type == USE_T_CD) /* delete the lines */
10110 {
10111 windgoto(cursor_row, 0);
10112 out_str(T_CD);
10113 screen_start(); /* don't know where cursor is now */
10114 }
10115 else if (type == USE_T_CDL)
10116 {
10117 windgoto(cursor_row, 0);
10118 term_delete_lines(line_count);
10119 screen_start(); /* don't know where cursor is now */
10120 }
10121 /*
10122 * Deleting lines at top of the screen or scroll region: Just scroll
10123 * the whole screen (scroll region) up by outputting newlines on the
10124 * last line.
10125 */
10126 else if (type == USE_NL)
10127 {
10128 windgoto(cursor_end - 1, 0);
10129 for (i = line_count; --i >= 0; )
10130 out_char('\n'); /* cursor will remain on same line */
10131 }
10132 else
10133 {
10134 for (i = line_count; --i >= 0; )
10135 {
10136 if (type == USE_T_DL)
10137 {
10138 windgoto(cursor_row, 0);
10139 out_str(T_DL); /* delete a line */
10140 }
10141 else /* type == USE_T_CE */
10142 {
10143 windgoto(cursor_row + i, 0);
10144 out_str(T_CE); /* erase a line */
10145 }
10146 screen_start(); /* don't know where cursor is now */
10147 }
10148 }
10149
10150 /*
10151 * If the 'db' flag is set, we need to clear the lines that have been
10152 * scrolled up at the bottom of the region.
10153 */
10154 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10155 {
10156 for (i = line_count; i > 0; --i)
10157 {
10158 windgoto(cursor_end - i, 0);
10159 out_str(T_CE); /* erase a line */
10160 screen_start(); /* don't know where cursor is now */
10161 }
10162 }
10163
10164#ifdef FEAT_GUI
10165 gui_can_update_cursor();
10166 if (gui.in_use)
10167 out_flush(); /* always flush after a scroll */
10168#endif
10169
10170 return OK;
10171}
10172
10173/*
10174 * show the current mode and ruler
10175 *
10176 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10177 * If clear_cmdline is FALSE there may be a message there that needs to be
10178 * cleared only if a mode is shown.
10179 * Return the length of the message (0 if no message).
10180 */
10181 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010182showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184 int need_clear;
10185 int length = 0;
10186 int do_mode;
10187 int attr;
10188 int nwr_save;
10189#ifdef FEAT_INS_EXPAND
10190 int sub_attr;
10191#endif
10192
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010193 do_mode = ((p_smd && msg_silent == 0)
10194 && ((State & INSERT)
10195 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010196 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 if (do_mode || Recording)
10198 {
10199 /*
10200 * Don't show mode right now, when not redrawing or inside a mapping.
10201 * Call char_avail() only when we are going to show something, because
10202 * it takes a bit of time.
10203 */
10204 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10205 {
10206 redraw_cmdline = TRUE; /* show mode later */
10207 return 0;
10208 }
10209
10210 nwr_save = need_wait_return;
10211
10212 /* wait a bit before overwriting an important message */
10213 check_for_delay(FALSE);
10214
10215 /* if the cmdline is more than one line high, erase top lines */
10216 need_clear = clear_cmdline;
10217 if (clear_cmdline && cmdline_row < Rows - 1)
10218 msg_clr_cmdline(); /* will reset clear_cmdline */
10219
10220 /* Position on the last line in the window, column 0 */
10221 msg_pos_mode();
10222 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010223 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 if (do_mode)
10225 {
10226 MSG_PUTS_ATTR("--", attr);
10227#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010228 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010229# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010230 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010231# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010232 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010233# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010234 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010235# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 MSG_PUTS_ATTR(" IM", attr);
10237# else
10238 MSG_PUTS_ATTR(" XIM", attr);
10239# endif
10240#endif
10241#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10242 if (gui.in_use)
10243 {
10244 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010245 {
10246 /* HANGUL */
10247 if (enc_utf8)
10248 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10249 else
10250 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 }
10253#endif
10254#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010255 /* CTRL-X in Insert mode */
10256 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 {
10258 /* These messages can get long, avoid a wrap in a narrow
10259 * window. Prefer showing edit_submode_extra. */
10260 length = (Rows - msg_row) * Columns - 3;
10261 if (edit_submode_extra != NULL)
10262 length -= vim_strsize(edit_submode_extra);
10263 if (length > 0)
10264 {
10265 if (edit_submode_pre != NULL)
10266 length -= vim_strsize(edit_submode_pre);
10267 if (length - vim_strsize(edit_submode) > 0)
10268 {
10269 if (edit_submode_pre != NULL)
10270 msg_puts_attr(edit_submode_pre, attr);
10271 msg_puts_attr(edit_submode, attr);
10272 }
10273 if (edit_submode_extra != NULL)
10274 {
10275 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10276 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010277 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 else
10279 sub_attr = attr;
10280 msg_puts_attr(edit_submode_extra, sub_attr);
10281 }
10282 }
10283 length = 0;
10284 }
10285 else
10286#endif
10287 {
10288#ifdef FEAT_VREPLACE
10289 if (State & VREPLACE_FLAG)
10290 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10291 else
10292#endif
10293 if (State & REPLACE_FLAG)
10294 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10295 else if (State & INSERT)
10296 {
10297#ifdef FEAT_RIGHTLEFT
10298 if (p_ri)
10299 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10300#endif
10301 MSG_PUTS_ATTR(_(" INSERT"), attr);
10302 }
10303 else if (restart_edit == 'I')
10304 MSG_PUTS_ATTR(_(" (insert)"), attr);
10305 else if (restart_edit == 'R')
10306 MSG_PUTS_ATTR(_(" (replace)"), attr);
10307 else if (restart_edit == 'V')
10308 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10309#ifdef FEAT_RIGHTLEFT
10310 if (p_hkmap)
10311 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10312# ifdef FEAT_FKMAP
10313 if (p_fkmap)
10314 MSG_PUTS_ATTR(farsi_text_5, attr);
10315# endif
10316#endif
10317#ifdef FEAT_KEYMAP
10318 if (State & LANGMAP)
10319 {
10320# ifdef FEAT_ARABIC
10321 if (curwin->w_p_arab)
10322 MSG_PUTS_ATTR(_(" Arabic"), attr);
10323 else
10324# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010325 if (get_keymap_str(curwin, (char_u *)" (%s)",
10326 NameBuff, MAXPATHL))
10327 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 }
10329#endif
10330 if ((State & INSERT) && p_paste)
10331 MSG_PUTS_ATTR(_(" (paste)"), attr);
10332
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 if (VIsual_active)
10334 {
10335 char *p;
10336
10337 /* Don't concatenate separate words to avoid translation
10338 * problems. */
10339 switch ((VIsual_select ? 4 : 0)
10340 + (VIsual_mode == Ctrl_V) * 2
10341 + (VIsual_mode == 'V'))
10342 {
10343 case 0: p = N_(" VISUAL"); break;
10344 case 1: p = N_(" VISUAL LINE"); break;
10345 case 2: p = N_(" VISUAL BLOCK"); break;
10346 case 4: p = N_(" SELECT"); break;
10347 case 5: p = N_(" SELECT LINE"); break;
10348 default: p = N_(" SELECT BLOCK"); break;
10349 }
10350 MSG_PUTS_ATTR(_(p), attr);
10351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 MSG_PUTS_ATTR(" --", attr);
10353 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010354
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 need_clear = TRUE;
10356 }
10357 if (Recording
10358#ifdef FEAT_INS_EXPAND
10359 && edit_submode == NULL /* otherwise it gets too long */
10360#endif
10361 )
10362 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010363 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 need_clear = TRUE;
10365 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010366
10367 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 if (need_clear || clear_cmdline)
10369 msg_clr_eos();
10370 msg_didout = FALSE; /* overwrite this message */
10371 length = msg_col;
10372 msg_col = 0;
10373 need_wait_return = nwr_save; /* never ask for hit-return for this */
10374 }
10375 else if (clear_cmdline && msg_silent == 0)
10376 /* Clear the whole command line. Will reset "clear_cmdline". */
10377 msg_clr_cmdline();
10378
10379#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 /* In Visual mode the size of the selected area must be redrawn. */
10381 if (VIsual_active)
10382 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383
10384 /* If the last window has no status line, the ruler is after the mode
10385 * message and must be redrawn */
10386 if (redrawing()
10387# ifdef FEAT_WINDOWS
10388 && lastwin->w_status_height == 0
10389# endif
10390 )
10391 win_redr_ruler(lastwin, TRUE);
10392#endif
10393 redraw_cmdline = FALSE;
10394 clear_cmdline = FALSE;
10395
10396 return length;
10397}
10398
10399/*
10400 * Position for a mode message.
10401 */
10402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010403msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
10405 msg_col = 0;
10406 msg_row = Rows - 1;
10407}
10408
10409/*
10410 * Delete mode message. Used when ESC is typed which is expected to end
10411 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010412 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 */
10414 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010415unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010418 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 */
10420 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10421 redraw_cmdline = TRUE; /* delete mode later */
10422 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010423 clearmode();
10424}
10425
10426/*
10427 * Clear the mode message.
10428 */
10429 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010430clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010431{
10432 msg_pos_mode();
10433 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010434 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010435 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436}
10437
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010439recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010440{
10441 MSG_PUTS_ATTR(_("recording"), attr);
10442 if (!shortmess(SHM_RECORDING))
10443 {
10444 char_u s[4];
10445 sprintf((char *)s, " @%c", Recording);
10446 MSG_PUTS_ATTR(s, attr);
10447 }
10448}
10449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010450#if defined(FEAT_WINDOWS)
10451/*
10452 * Draw the tab pages line at the top of the Vim window.
10453 */
10454 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010455draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010456{
10457 int tabcount = 0;
10458 tabpage_T *tp;
10459 int tabwidth;
10460 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010461 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010462 int attr;
10463 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010464 win_T *cwp;
10465 int wincount;
10466 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010467 int c;
10468 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010469 int attr_sel = HL_ATTR(HLF_TPS);
10470 int attr_nosel = HL_ATTR(HLF_TP);
10471 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010472 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010473 int room;
10474 int use_sep_chars = (t_colors < 8
10475#ifdef FEAT_GUI
10476 && !gui.in_use
10477#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010478#ifdef FEAT_TERMGUICOLORS
10479 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010480#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010481 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010483 if (ScreenLines == NULL)
10484 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010485 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010486
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010487#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010488 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010489 if (gui_use_tabline())
10490 {
10491 gui_update_tabline();
10492 return;
10493 }
10494#endif
10495
10496 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010497 return;
10498
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010499#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010500
10501 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10502 for (scol = 0; scol < Columns; ++scol)
10503 TabPageIdxs[scol] = 0;
10504
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010505 /* Use the 'tabline' option if it's set. */
10506 if (*p_tal != NUL)
10507 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010508 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010509
10510 /* Check for an error. If there is one we would loop in redrawing the
10511 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010512 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010513 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010514 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010515 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010516 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010517 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010518 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010519 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010520#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010521 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010522 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010523 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010524
Bram Moolenaar238a5642006-02-21 22:12:05 +000010525 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10526 if (tabwidth < 6)
10527 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010528
Bram Moolenaar238a5642006-02-21 22:12:05 +000010529 attr = attr_nosel;
10530 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010531 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010532 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10533 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010534 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010536
Bram Moolenaar238a5642006-02-21 22:12:05 +000010537 if (tp->tp_topframe == topframe)
10538 attr = attr_sel;
10539 if (use_sep_chars && col > 0)
10540 screen_putchar('|', 0, col++, attr);
10541
10542 if (tp->tp_topframe != topframe)
10543 attr = attr_nosel;
10544
10545 screen_putchar(' ', 0, col++, attr);
10546
10547 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010548 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549 cwp = curwin;
10550 wp = firstwin;
10551 }
10552 else
10553 {
10554 cwp = tp->tp_curwin;
10555 wp = tp->tp_firstwin;
10556 }
10557
10558 modified = FALSE;
10559 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10560 if (bufIsChanged(wp->w_buffer))
10561 modified = TRUE;
10562 if (modified || wincount > 1)
10563 {
10564 if (wincount > 1)
10565 {
10566 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010567 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010568 if (col + len >= Columns - 3)
10569 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010570 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010571#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010572 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010573#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010574 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010575#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010576 );
10577 col += len;
10578 }
10579 if (modified)
10580 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10581 screen_putchar(' ', 0, col++, attr);
10582 }
10583
10584 room = scol - col + tabwidth - 1;
10585 if (room > 0)
10586 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010587 /* Get buffer name in NameBuff[] */
10588 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010589 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590 len = vim_strsize(NameBuff);
10591 p = NameBuff;
10592#ifdef FEAT_MBYTE
10593 if (has_mbyte)
10594 while (len > room)
10595 {
10596 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010597 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010598 }
10599 else
10600#endif
10601 if (len > room)
10602 {
10603 p += len - room;
10604 len = room;
10605 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010606 if (len > Columns - col - 1)
10607 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010609 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010610 col += len;
10611 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010612 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613
10614 /* Store the tab page number in TabPageIdxs[], so that
10615 * jump_to_mouse() knows where each one is. */
10616 ++tabcount;
10617 while (scol < col)
10618 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010619 }
10620
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 if (use_sep_chars)
10622 c = '_';
10623 else
10624 c = ' ';
10625 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010626
10627 /* Put an "X" for closing the current tab if there are several. */
10628 if (first_tabpage->tp_next != NULL)
10629 {
10630 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10631 TabPageIdxs[Columns - 1] = -999;
10632 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010633 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010634
10635 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10636 * set. */
10637 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010638}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010639
10640/*
10641 * Get buffer name for "buf" into NameBuff[].
10642 * Takes care of special buffer names and translates special characters.
10643 */
10644 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010645get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010646{
10647 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010648 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010649 else
10650 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10651 trans_characters(NameBuff, MAXPATHL);
10652}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010653#endif
10654
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10656/*
10657 * Get the character to use in a status line. Get its attributes in "*attr".
10658 */
10659 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010660fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661{
10662 int fill;
10663 if (is_curwin)
10664 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010665 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 fill = fill_stl;
10667 }
10668 else
10669 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010670 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 fill = fill_stlnc;
10672 }
10673 /* Use fill when there is highlighting, and highlighting of current
10674 * window differs, or the fillchars differ, or this is not the
10675 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010677 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 || (fill_stl != fill_stlnc)))
10679 return fill;
10680 if (is_curwin)
10681 return '^';
10682 return '=';
10683}
10684#endif
10685
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010686#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687/*
10688 * Get the character to use in a separator between vertically split windows.
10689 * Get its attributes in "*attr".
10690 */
10691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010692fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010694 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 if (*attr == 0 && fill_vert == ' ')
10696 return '|';
10697 else
10698 return fill_vert;
10699}
10700#endif
10701
10702/*
10703 * Return TRUE if redrawing should currently be done.
10704 */
10705 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010706redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010708#ifdef FEAT_EVAL
10709 if (disable_redraw_for_testing)
10710 return 0;
10711 else
10712#endif
10713 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10715}
10716
10717/*
10718 * Return TRUE if printing messages should currently be done.
10719 */
10720 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010721messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
10723 return (!(p_lz && char_avail() && !KeyTyped));
10724}
10725
10726/*
10727 * Show current status info in ruler and various other places
10728 * If always is FALSE, only show ruler if position has changed.
10729 */
10730 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010731showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
10733 if (!always && !redrawing())
10734 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010735#ifdef FEAT_INS_EXPAND
10736 if (pum_visible())
10737 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010738# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010739 /* Don't redraw right now, do it later. */
10740 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010741# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010742 return;
10743 }
10744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010746 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010747 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010748 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
10751#endif
10752#ifdef FEAT_CMDL_INFO
10753 win_redr_ruler(curwin, always);
10754#endif
10755
10756#ifdef FEAT_TITLE
10757 if (need_maketitle
10758# ifdef FEAT_STL_OPT
10759 || (p_icon && (stl_syntax & STL_IN_ICON))
10760 || (p_title && (stl_syntax & STL_IN_TITLE))
10761# endif
10762 )
10763 maketitle();
10764#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010765#ifdef FEAT_WINDOWS
10766 /* Redraw the tab pages line if needed. */
10767 if (redraw_tabline)
10768 draw_tabline();
10769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770}
10771
10772#ifdef FEAT_CMDL_INFO
10773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010774win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010776#define RULER_BUF_LEN 70
10777 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 int row;
10779 int fillchar;
10780 int attr;
10781 int empty_line = FALSE;
10782 colnr_T virtcol;
10783 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010784 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010786#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 int this_ru_col;
10788 int off = 0;
10789 int width = Columns;
10790# define WITH_OFF(x) x
10791# define WITH_WIDTH(x) x
10792#else
10793# define WITH_OFF(x) 0
10794# define WITH_WIDTH(x) Columns
10795# define this_ru_col ru_col
10796#endif
10797
10798 /* If 'ruler' off or redrawing disabled, don't do anything */
10799 if (!p_ru)
10800 return;
10801
10802 /*
10803 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10804 * after deleting lines, before cursor.lnum is corrected.
10805 */
10806 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10807 return;
10808
10809#ifdef FEAT_INS_EXPAND
10810 /* Don't draw the ruler while doing insert-completion, it might overwrite
10811 * the (long) mode message. */
10812# ifdef FEAT_WINDOWS
10813 if (wp == lastwin && lastwin->w_status_height == 0)
10814# endif
10815 if (edit_submode != NULL)
10816 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010817 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10818 if (pum_visible())
10819 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820#endif
10821
10822#ifdef FEAT_STL_OPT
10823 if (*p_ruf)
10824 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010825 int save_called_emsg = called_emsg;
10826
10827 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010829 if (called_emsg)
10830 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010831 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 return;
10834 }
10835#endif
10836
10837 /*
10838 * Check if not in Insert mode and the line is empty (will show "0-1").
10839 */
10840 if (!(State & INSERT)
10841 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10842 empty_line = TRUE;
10843
10844 /*
10845 * Only draw the ruler when something changed.
10846 */
10847 validate_virtcol_win(wp);
10848 if ( redraw_cmdline
10849 || always
10850 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10851 || wp->w_cursor.col != wp->w_ru_cursor.col
10852 || wp->w_virtcol != wp->w_ru_virtcol
10853#ifdef FEAT_VIRTUALEDIT
10854 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10855#endif
10856 || wp->w_topline != wp->w_ru_topline
10857 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10858#ifdef FEAT_DIFF
10859 || wp->w_topfill != wp->w_ru_topfill
10860#endif
10861 || empty_line != wp->w_ru_empty)
10862 {
10863 cursor_off();
10864#ifdef FEAT_WINDOWS
10865 if (wp->w_status_height)
10866 {
10867 row = W_WINROW(wp) + wp->w_height;
10868 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 off = W_WINCOL(wp);
10870 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 }
10872 else
10873#endif
10874 {
10875 row = Rows - 1;
10876 fillchar = ' ';
10877 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010878#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 width = Columns;
10880 off = 0;
10881#endif
10882 }
10883
10884 /* In list mode virtcol needs to be recomputed */
10885 virtcol = wp->w_virtcol;
10886 if (wp->w_p_list && lcs_tab1 == NUL)
10887 {
10888 wp->w_p_list = FALSE;
10889 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10890 wp->w_p_list = TRUE;
10891 }
10892
10893 /*
10894 * Some sprintfs return the length, some return a pointer.
10895 * To avoid portability problems we use strlen() here.
10896 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010897 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10899 ? 0L
10900 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010901 len = STRLEN(buffer);
10902 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10904 (int)virtcol + 1);
10905
10906 /*
10907 * Add a "50%" if there is room for it.
10908 * On the last line, don't print in the last column (scrolls the
10909 * screen up on some terminals).
10910 */
10911 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010912 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 o = i + vim_strsize(buffer + i + 1);
10914#ifdef FEAT_WINDOWS
10915 if (wp->w_status_height == 0) /* can't use last char of screen */
10916#endif
10917 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010918#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 this_ru_col = ru_col - (Columns - width);
10920 if (this_ru_col < 0)
10921 this_ru_col = 0;
10922#endif
10923 /* Never use more than half the window/screen width, leave the other
10924 * half for the filename. */
10925 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10926 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10927 if (this_ru_col + o < WITH_WIDTH(width))
10928 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010929 /* need at least 3 chars left for get_rel_pos() + NUL */
10930 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 {
10932#ifdef FEAT_MBYTE
10933 if (has_mbyte)
10934 i += (*mb_char2bytes)(fillchar, buffer + i);
10935 else
10936#endif
10937 buffer[i++] = fillchar;
10938 ++o;
10939 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010940 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 }
10942 /* Truncate at window boundary. */
10943#ifdef FEAT_MBYTE
10944 if (has_mbyte)
10945 {
10946 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010947 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 {
10949 o += (*mb_ptr2cells)(buffer + i);
10950 if (this_ru_col + o > WITH_WIDTH(width))
10951 {
10952 buffer[i] = NUL;
10953 break;
10954 }
10955 }
10956 }
10957 else
10958#endif
10959 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10960 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10961
10962 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10963 i = redraw_cmdline;
10964 screen_fill(row, row + 1,
10965 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10966 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10967 fillchar, fillchar, attr);
10968 /* don't redraw the cmdline because of showing the ruler */
10969 redraw_cmdline = i;
10970 wp->w_ru_cursor = wp->w_cursor;
10971 wp->w_ru_virtcol = wp->w_virtcol;
10972 wp->w_ru_empty = empty_line;
10973 wp->w_ru_topline = wp->w_topline;
10974 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10975#ifdef FEAT_DIFF
10976 wp->w_ru_topfill = wp->w_topfill;
10977#endif
10978 }
10979}
10980#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010981
10982#if defined(FEAT_LINEBREAK) || defined(PROTO)
10983/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010984 * Return the width of the 'number' and 'relativenumber' column.
10985 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010986 * Otherwise it depends on 'numberwidth' and the line count.
10987 */
10988 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010989number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010990{
10991 int n;
10992 linenr_T lnum;
10993
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010994 if (wp->w_p_rnu && !wp->w_p_nu)
10995 /* cursor line shows "0" */
10996 lnum = wp->w_height;
10997 else
10998 /* cursor line shows absolute line number */
10999 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011000
Bram Moolenaar6b314672015-03-20 15:42:10 +010011001 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011002 return wp->w_nrwidth_width;
11003 wp->w_nrwidth_line_count = lnum;
11004
11005 n = 0;
11006 do
11007 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011008 lnum /= 10;
11009 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011010 } while (lnum > 0);
11011
11012 /* 'numberwidth' gives the minimal width plus one */
11013 if (n < wp->w_p_nuw - 1)
11014 n = wp->w_p_nuw - 1;
11015
11016 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011017 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011018 return n;
11019}
11020#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011021
11022/*
11023 * Return the current cursor column. This is the actual position on the
11024 * screen. First column is 0.
11025 */
11026 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011027screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011028{
11029 return screen_cur_col;
11030}
11031
11032/*
11033 * Return the current cursor row. This is the actual position on the screen.
11034 * First row is 0.
11035 */
11036 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011037screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011038{
11039 return screen_cur_row;
11040}