blob: b5762145b71223933a04551b0e5ca37fe81c0d97 [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
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
125static 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 +0000126#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
128static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
129static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200131static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200140# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void start_search_hl(void);
142static void end_search_hl(void);
143static void init_search_hl(win_T *wp);
144static void prepare_search_hl(win_T *wp, linenr_T lnum);
145static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
146static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_start_highlight(int attr);
149static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100156#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200160static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200168static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100170#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200185#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
186/* Can limit syntax highlight time to 'redrawtime'. */
187# define SYN_TIME_LIMIT 1
188#endif
189
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200190#ifdef FEAT_RIGHTLEFT
191# define HAS_RIGHTLEFT(x) x
192#else
193# define HAS_RIGHTLEFT(x) FALSE
194#endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Redraw the current window later, with update_screen(type).
198 * Set must_redraw only if not already set to a higher value.
199 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
200 */
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 redraw_win_later(curwin, type);
205}
206
207 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100208redraw_win_later(
209 win_T *wp,
210 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 if (wp->w_redr_type < type)
213 {
214 wp->w_redr_type = type;
215 if (type >= NOT_VALID)
216 wp->w_lines_valid = 0;
217 if (must_redraw < type) /* must_redraw is the maximum of all windows */
218 must_redraw = type;
219 }
220}
221
222/*
223 * Force a complete redraw later. Also resets the highlighting. To be used
224 * after executing a shell command that messes up the screen.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000230#ifdef FEAT_GUI
231 if (gui.in_use)
232 /* Use a code that will reset gui.highlight_mask in
233 * gui_stop_highlight(). */
234 screen_attr = HL_ALL + 1;
235 else
236#endif
237 /* Use attributes that is very unlikely to appear in text. */
238 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
242 * Mark all windows to be redrawn later.
243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 redraw_win_later(wp, type);
252 }
253}
254
255/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000256 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 redraw_buf_later(curbuf, type);
262}
263
264 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100265redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276 void
277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200292#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200293 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 }
296 }
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#ifdef FEAT_MBYTE
314 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200317 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318#endif
319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
332#ifdef FEAT_MBYTE
333 if (enc_utf8)
334 {
335 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenlineUC == NULL)
338 ret = 2;
339 for (i = 0; i < p_mco; ++i)
340 {
341 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
349 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
354#endif
355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367#ifdef FEAT_MBYTE
368 if (enc_utf8)
369 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineC[i] + r * cols,
375 ScreenLinesC[i] + LineOffset[cmdline_row + r],
376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382#endif
383 }
384
385 update_screen(0);
386 ret = 3;
387
388 if (must_redraw == 0)
389 {
390 int off = (int)(current_ScreenLine - ScreenLines);
391
392 /* Restore the text displayed in the command line area. */
393 for (r = 0; r < rows; ++r)
394 {
395 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenattr + r * cols,
400 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401#ifdef FEAT_MBYTE
402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200417 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419 ret = 4;
420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 }
422
423 vim_free(screenline);
424 vim_free(screenattr);
425#ifdef FEAT_MBYTE
426 if (enc_utf8)
427 {
428 vim_free(screenlineUC);
429 for (i = 0; i < p_mco; ++i)
430 vim_free(screenlineC[i]);
431 }
432 if (enc_dbcs == DBCS_JPNU)
433 vim_free(screenline2);
434#endif
435
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200436 /* Show the intro message when appropriate. */
437 maybe_intro_message();
438
439 setcursor();
440
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441 return ret;
442}
443
444/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 * Invoked after an asynchronous callback is called.
446 * If an echo command was used the cursor needs to be put back where
447 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200448 * If "call_update_screen" is FALSE don't call update_screen() when at the
449 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100450 */
451 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200452redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100453{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200454 ++redrawing_for_callback;
455
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100456 if (State == HITRETURN || State == ASKMORE)
457 ; /* do nothing */
458 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redrawing only works when the screen didn't scroll. Don't clear
461 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200462 if (msg_scrolled == 0
463#ifdef FEAT_WILDMENU
464 && wild_menu_showing == 0
465#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200466 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200468 /* Redraw in the same position, so that the user can continue
469 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200470 redrawcmdline_ex(FALSE);
471 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200472 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100473 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200474 /* keep the command line if possible */
475 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 setcursor();
477 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478 cursor_on();
479 out_flush();
480#ifdef FEAT_GUI
481 if (gui.in_use)
482 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200483 /* Don't update the cursor when it is blinking and off to avoid
484 * flicker. */
485 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200486 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487 gui_mch_flush();
488 }
489#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200490
491 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100492}
493
494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Changed something in the current window, at buffer line "lnum", that
496 * requires that line and possibly other lines to be redrawn.
497 * Used when entering/leaving Insert mode with the cursor on a folded line.
498 * Used to remove the "$" from a change command.
499 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
500 * may become invalid and the whole window will have to be redrawn.
501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100503redrawWinline(
504 linenr_T lnum,
505 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507#ifdef FEAT_FOLDING
508 int i;
509#endif
510
511 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
512 curwin->w_redraw_top = lnum;
513 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
514 curwin->w_redraw_bot = lnum;
515 redraw_later(VALID);
516
517#ifdef FEAT_FOLDING
518 if (invalid)
519 {
520 /* A w_lines[] entry for this lnum has become invalid. */
521 i = find_wl_entry(curwin, lnum);
522 if (i >= 0)
523 curwin->w_lines[i].wl_valid = FALSE;
524 }
525#endif
526}
527
528/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
531 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 redraw_curbuf_later(type);
535 update_screen(type);
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Based on the current value of curwin->w_topline, transfer a screenfull
540 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
541 */
542 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200543update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200545 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 win_T *wp;
547 static int did_intro = FALSE;
548#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
549 int did_one;
550#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200551#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200552 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200553 int gui_cursor_col;
554 int gui_cursor_row;
555#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200556 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000558 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (!screen_valid(TRUE))
560 return;
561
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200562 if (type == VALID_NO_UPDATE)
563 {
564 no_update = TRUE;
565 type = 0;
566 }
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (must_redraw)
569 {
570 if (type < must_redraw) /* use maximal type */
571 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000572
573 /* must_redraw is reset here, so that when we run into some weird
574 * reason to redraw while busy redrawing (e.g., asynchronous
575 * scrolling), or update_topline() in win_update() will cause a
576 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 must_redraw = 0;
578 }
579
580 /* Need to update w_lines[]. */
581 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
582 type = NOT_VALID;
583
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000584 /* Postpone the redrawing when it's not needed and when being called
585 * recursively. */
586 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 redraw_later(type); /* remember type for next time */
589 must_redraw = type;
590 if (type > INVERTED_ALL)
591 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
592 return;
593 }
594
595 updating_screen = TRUE;
596#ifdef FEAT_SYN_HL
597 ++display_tick; /* let syntax code know we're in a next round of
598 * display updating */
599#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200600 if (no_update)
601 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 /*
604 * if the screen was scrolled up when displaying a message, scroll it down
605 */
606 if (msg_scrolled)
607 {
608 clear_cmdline = TRUE;
609 if (msg_scrolled > Rows - 5) /* clearing is faster */
610 type = CLEAR;
611 else if (type != CLEAR)
612 {
613 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200614 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
615 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 type = CLEAR;
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (W_WINROW(wp) < msg_scrolled)
620 {
621 if (W_WINROW(wp) + wp->w_height > msg_scrolled
622 && wp->w_redr_type < REDRAW_TOP
623 && wp->w_lines_valid > 0
624 && wp->w_topline == wp->w_lines[0].wl_lnum)
625 {
626 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
627 wp->w_redr_type = REDRAW_TOP;
628 }
629 else
630 {
631 wp->w_redr_type = NOT_VALID;
632#ifdef FEAT_WINDOWS
633 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
634 <= msg_scrolled)
635 wp->w_redr_status = TRUE;
636#endif
637 }
638 }
639 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200640 if (!no_update)
641 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000642#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000643 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
646 msg_scrolled = 0;
647 need_wait_return = FALSE;
648 }
649
650 /* reset cmdline_row now (may have been changed temporarily) */
651 compute_cmdrow();
652
653 /* Check for changed highlighting */
654 if (need_highlight_changed)
655 highlight_changed();
656
657 if (type == CLEAR) /* first clear screen */
658 {
659 screenclear(); /* will reset clear_cmdline */
660 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200661 /* must_redraw may be set indirectly, avoid another redraw later */
662 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664
665 if (clear_cmdline) /* going to clear cmdline (done below) */
666 check_for_delay(FALSE);
667
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000668#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200669 /* Force redraw when width of 'number' or 'relativenumber' column
670 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000671 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200672 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
673 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000674 curwin->w_redr_type = NOT_VALID;
675#endif
676
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 /*
678 * Only start redrawing if there is really something to do.
679 */
680 if (type == INVERTED)
681 update_curswant();
682 if (curwin->w_redr_type < type
683 && !((type == VALID
684 && curwin->w_lines[0].wl_valid
685#ifdef FEAT_DIFF
686 && curwin->w_topfill == curwin->w_old_topfill
687 && curwin->w_botfill == curwin->w_old_botfill
688#endif
689 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000691 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
693 && curwin->w_old_visual_mode == VIsual_mode
694 && (curwin->w_valid & VALID_VIRTCOL)
695 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ))
697 curwin->w_redr_type = type;
698
Bram Moolenaar5a305422006-04-28 22:38:25 +0000699#ifdef FEAT_WINDOWS
700 /* Redraw the tab pages line if needed. */
701 if (redraw_tabline || type >= NOT_VALID)
702 draw_tabline();
703#endif
704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#ifdef FEAT_SYN_HL
706 /*
707 * Correct stored syntax highlighting info for changes in each displayed
708 * buffer. Each buffer must only be done once.
709 */
710 FOR_ALL_WINDOWS(wp)
711 {
712 if (wp->w_buffer->b_mod_set)
713 {
714# ifdef FEAT_WINDOWS
715 win_T *wwp;
716
717 /* Check if we already did this buffer. */
718 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
719 if (wwp->w_buffer == wp->w_buffer)
720 break;
721# endif
722 if (
723# ifdef FEAT_WINDOWS
724 wwp == wp &&
725# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200726 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 syn_stack_apply_changes(wp->w_buffer);
728 }
729 }
730#endif
731
732 /*
733 * Go from top to bottom through the windows, redrawing the ones that need
734 * it.
735 */
736#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
737 did_one = FALSE;
738#endif
739#ifdef FEAT_SEARCH_EXTRA
740 search_hl.rm.regprog = NULL;
741#endif
742 FOR_ALL_WINDOWS(wp)
743 {
744 if (wp->w_redr_type != 0)
745 {
746 cursor_off();
747#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
748 if (!did_one)
749 {
750 did_one = TRUE;
751# ifdef FEAT_SEARCH_EXTRA
752 start_search_hl();
753# endif
754# ifdef FEAT_CLIPBOARD
755 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200756 if (clip_star.available && clip_isautosel_star())
757 clip_update_selection(&clip_star);
758 if (clip_plus.available && clip_isautosel_plus())
759 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760# endif
761#ifdef FEAT_GUI
762 /* Remove the cursor before starting to do anything, because
763 * scrolling may make it difficult to redraw the text under
764 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200765 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200766 {
767 gui_cursor_col = gui.cursor_col;
768 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 }
774#endif
775 win_update(wp);
776 }
777
778#ifdef FEAT_WINDOWS
779 /* redraw status line after the window to minimize cursor movement */
780 if (wp->w_redr_status)
781 {
782 cursor_off();
783 win_redr_status(wp);
784 }
785#endif
786 }
787#if defined(FEAT_SEARCH_EXTRA)
788 end_search_hl();
789#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100790#ifdef FEAT_INS_EXPAND
791 /* May need to redraw the popup menu. */
792 if (pum_visible())
793 pum_redraw();
794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796#ifdef FEAT_WINDOWS
797 /* Reset b_mod_set flags. Going through all windows is probably faster
798 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 wp->w_buffer->b_mod_set = FALSE;
801#else
802 curbuf->b_mod_set = FALSE;
803#endif
804
805 updating_screen = FALSE;
806#ifdef FEAT_GUI
807 gui_may_resize_shell();
808#endif
809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
812 if (clear_cmdline || redraw_cmdline)
813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
823#ifdef FEAT_GUI
824 /* Redraw the cursor and update the scrollbars when all screen updating is
825 * done. */
826 if (gui.in_use)
827 {
828 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200829 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200830 {
831 /* Put the GUI position where the cursor was, gui_update_cursor()
832 * uses that. */
833 gui.col = gui_cursor_col;
834 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200835# ifdef FEAT_MBYTE
836 gui.col = mb_fix_col(gui.col, gui.row);
837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200839 screen_cur_col = gui.col;
840 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 gui_update_scrollbars(FALSE);
843 }
844#endif
845}
846
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100847#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
848/*
849 * Prepare for updating one or more windows.
850 * Caller must check for "updating_screen" already set to avoid recursiveness.
851 */
852 static void
853update_prepare(void)
854{
855 cursor_off();
856 updating_screen = TRUE;
857#ifdef FEAT_GUI
858 /* Remove the cursor before starting to do anything, because scrolling may
859 * make it difficult to redraw the text under it. */
860 if (gui.in_use)
861 gui_undraw_cursor();
862#endif
863#ifdef FEAT_SEARCH_EXTRA
864 start_search_hl();
865#endif
866}
867
868/*
869 * Finish updating one or more windows.
870 */
871 static void
872update_finish(void)
873{
874 if (redraw_cmdline)
875 showmode();
876
877# ifdef FEAT_SEARCH_EXTRA
878 end_search_hl();
879# endif
880
881 updating_screen = FALSE;
882
883# ifdef FEAT_GUI
884 gui_may_resize_shell();
885
886 /* Redraw the cursor and update the scrollbars when all screen updating is
887 * done. */
888 if (gui.in_use)
889 {
890 out_flush(); /* required before updating the cursor */
891 gui_update_cursor(FALSE, FALSE);
892 gui_update_scrollbars(FALSE);
893 }
894# endif
895}
896#endif
897
Bram Moolenaar860cae12010-06-05 23:22:07 +0200898#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200899/*
900 * Return TRUE if the cursor line in window "wp" may be concealed, according
901 * to the 'concealcursor' option.
902 */
903 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100904conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200905{
906 int c;
907
908 if (*wp->w_p_cocu == NUL)
909 return FALSE;
910 if (get_real_state() & VISUAL)
911 c = 'v';
912 else if (State & INSERT)
913 c = 'i';
914 else if (State & NORMAL)
915 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200916 else if (State & CMDLINE)
917 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918 else
919 return FALSE;
920 return vim_strchr(wp->w_p_cocu, c) != NULL;
921}
922
923/*
924 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
925 */
926 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100927conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200928{
929 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
930 {
931 need_cursor_line_redraw = TRUE;
932 /* Need to recompute cursor column, e.g., when starting Visual mode
933 * without concealing. */
934 curs_columns(TRUE);
935 }
936}
937
Bram Moolenaar860cae12010-06-05 23:22:07 +0200938 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100939update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940{
941 int row;
942 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200943#ifdef SYN_TIME_LIMIT
944 proftime_T syntax_tm;
945#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200946
Bram Moolenaar908be432016-05-24 10:51:30 +0200947 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100948 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200949 return;
950
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200952 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200953 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200954#ifdef SYN_TIME_LIMIT
955 /* Set the time limit to 'redrawtime'. */
956 profile_setlimit(p_rdt, &syntax_tm);
957#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958 update_prepare();
959
Bram Moolenaar860cae12010-06-05 23:22:07 +0200960 row = 0;
961 for (j = 0; j < wp->w_lines_valid; ++j)
962 {
963 if (lnum == wp->w_lines[j].wl_lnum)
964 {
965 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200966# ifdef FEAT_SEARCH_EXTRA
967 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200968 start_search_hl();
969 prepare_search_hl(wp, lnum);
970# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200971 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
972#ifdef SYN_TIME_LIMIT
973 &syntax_tm
974#else
975 NULL
976#endif
977 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200978# if defined(FEAT_SEARCH_EXTRA)
979 end_search_hl();
980# endif
981 break;
982 }
983 row += wp->w_lines[j].wl_size;
984 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100985
986 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200987 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200988 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990#endif
991
992#if defined(FEAT_SIGNS) || defined(PROTO)
993 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100994update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 win_T *wp;
997 int doit = FALSE;
998
999# ifdef FEAT_FOLDING
1000 win_foldinfo.fi_level = 0;
1001# endif
1002
1003 /* update/delete a specific mark */
1004 FOR_ALL_WINDOWS(wp)
1005 {
1006 if (buf != NULL && lnum > 0)
1007 {
1008 if (wp->w_buffer == buf && lnum >= wp->w_topline
1009 && lnum < wp->w_botline)
1010 {
1011 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1012 wp->w_redraw_top = lnum;
1013 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1014 wp->w_redraw_bot = lnum;
1015 redraw_win_later(wp, VALID);
1016 }
1017 }
1018 else
1019 redraw_win_later(wp, VALID);
1020 if (wp->w_redr_type != 0)
1021 doit = TRUE;
1022 }
1023
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001024 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001025 * happening (recursive call), messages on the screen or still starting up.
1026 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001027 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001028 || State == ASKMORE || State == HITRETURN
1029 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001030#ifdef FEAT_GUI
1031 || gui.starting
1032#endif
1033 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 return;
1035
1036 /* update all windows that need updating */
1037 update_prepare();
1038
1039# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001040 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 if (wp->w_redr_type != 0)
1043 win_update(wp);
1044 if (wp->w_redr_status)
1045 win_redr_status(wp);
1046 }
1047# else
1048 if (curwin->w_redr_type != 0)
1049 win_update(curwin);
1050# endif
1051
1052 update_finish();
1053}
1054#endif
1055
1056
1057#if defined(FEAT_GUI) || defined(PROTO)
1058/*
1059 * Update a single window, its status line and maybe the command line msg.
1060 * Used for the GUI scrollbar.
1061 */
1062 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001063updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001065 /* return if already busy updating */
1066 if (updating_screen)
1067 return;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 update_prepare();
1070
1071#ifdef FEAT_CLIPBOARD
1072 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001073 if (clip_star.available && clip_isautosel_star())
1074 clip_update_selection(&clip_star);
1075 if (clip_plus.available && clip_isautosel_plus())
1076 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001082 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001083 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001084 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (wp->w_redr_status
1087# ifdef FEAT_CMDL_INFO
1088 || p_ru
1089# endif
1090# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001091 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092# endif
1093 )
1094 win_redr_status(wp);
1095#endif
1096
1097 update_finish();
1098}
1099#endif
1100
1101/*
1102 * Update a single window.
1103 *
1104 * This may cause the windows below it also to be redrawn (when clearing the
1105 * screen or scrolling lines).
1106 *
1107 * How the window is redrawn depends on wp->w_redr_type. Each type also
1108 * implies the one below it.
1109 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001110 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1112 * INVERTED redraw the changed part of the Visual area
1113 * INVERTED_ALL redraw the whole Visual area
1114 * VALID 1. scroll up/down to adjust for a changed w_topline
1115 * 2. update lines at the top when scrolled down
1116 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001117 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 * b_mod_top and b_mod_bot.
1119 * - if wp->w_redraw_top non-zero, redraw lines between
1120 * wp->w_redraw_top and wp->w_redr_bot.
1121 * - continue redrawing when syntax status is invalid.
1122 * 4. if scrolled up, update lines at the bottom.
1123 * This results in three areas that may need updating:
1124 * top: from first row to top_end (when scrolled down)
1125 * mid: from mid_start to mid_end (update inversion or changed text)
1126 * bot: from bot_start to last row (when scrolled up)
1127 */
1128 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001129win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
1131 buf_T *buf = wp->w_buffer;
1132 int type;
1133 int top_end = 0; /* Below last row of the top area that needs
1134 updating. 0 when no top area updating. */
1135 int mid_start = 999;/* first row of the mid area that needs
1136 updating. 999 when no mid area updating. */
1137 int mid_end = 0; /* Below last row of the mid area that needs
1138 updating. 0 when no mid area updating. */
1139 int bot_start = 999;/* first row of the bot area that needs
1140 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int scrolled_down = FALSE; /* TRUE when scrolled down when
1142 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001144 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 int top_to_mod = FALSE; /* redraw above mod_top */
1146#endif
1147
1148 int row; /* current window row to display */
1149 linenr_T lnum; /* current buffer lnum to display */
1150 int idx; /* current index in w_lines[] */
1151 int srow; /* starting row of the current line */
1152
1153 int eof = FALSE; /* if TRUE, we hit the end of the file */
1154 int didline = FALSE; /* if TRUE, we finished the last line */
1155 int i;
1156 long j;
1157 static int recursive = FALSE; /* being called recursively */
1158 int old_botline = wp->w_botline;
1159#ifdef FEAT_FOLDING
1160 long fold_count;
1161#endif
1162#ifdef FEAT_SYN_HL
1163 /* remember what happened to the previous line, to know if
1164 * check_visual_highlight() can be used */
1165#define DID_NONE 1 /* didn't update a line */
1166#define DID_LINE 2 /* updated a normal line */
1167#define DID_FOLD 3 /* updated a folded line */
1168 int did_update = DID_NONE;
1169 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1170#endif
1171 linenr_T mod_top = 0;
1172 linenr_T mod_bot = 0;
1173#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1174 int save_got_int;
1175#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001176#ifdef SYN_TIME_LIMIT
1177 proftime_T syntax_tm;
1178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
1180 type = wp->w_redr_type;
1181
1182 if (type == NOT_VALID)
1183 {
1184#ifdef FEAT_WINDOWS
1185 wp->w_redr_status = TRUE;
1186#endif
1187 wp->w_lines_valid = 0;
1188 }
1189
1190 /* Window is zero-height: nothing to draw. */
1191 if (wp->w_height == 0)
1192 {
1193 wp->w_redr_type = 0;
1194 return;
1195 }
1196
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001197#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 /* Window is zero-width: Only need to draw the separator. */
1199 if (wp->w_width == 0)
1200 {
1201 /* draw the vertical separator right of this window */
1202 draw_vsep_win(wp, 0);
1203 wp->w_redr_type = 0;
1204 return;
1205 }
1206#endif
1207
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001208#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001209 /* If this window contains a terminal, redraw works completely differently.
1210 */
1211 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001212 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001213 wp->w_redr_type = 0;
1214 return;
1215 }
1216#endif
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001219 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001222#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001223 /* Force redraw when width of 'number' or 'relativenumber' column
1224 * changes. */
1225 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001226 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001227 {
1228 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001229 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001230 }
1231 else
1232#endif
1233
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1235 {
1236 /*
1237 * When there are both inserted/deleted lines and specific lines to be
1238 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1239 * everything (only happens when redrawing is off for while).
1240 */
1241 type = NOT_VALID;
1242 }
1243 else
1244 {
1245 /*
1246 * Set mod_top to the first line that needs displaying because of
1247 * changes. Set mod_bot to the first line after the changes.
1248 */
1249 mod_top = wp->w_redraw_top;
1250 if (wp->w_redraw_bot != 0)
1251 mod_bot = wp->w_redraw_bot + 1;
1252 else
1253 mod_bot = 0;
1254 wp->w_redraw_top = 0; /* reset for next time */
1255 wp->w_redraw_bot = 0;
1256 if (buf->b_mod_set)
1257 {
1258 if (mod_top == 0 || mod_top > buf->b_mod_top)
1259 {
1260 mod_top = buf->b_mod_top;
1261#ifdef FEAT_SYN_HL
1262 /* Need to redraw lines above the change that may be included
1263 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001264 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001266 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (mod_top < 1)
1268 mod_top = 1;
1269 }
1270#endif
1271 }
1272 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1273 mod_bot = buf->b_mod_bot;
1274
1275#ifdef FEAT_SEARCH_EXTRA
1276 /* When 'hlsearch' is on and using a multi-line search pattern, a
1277 * change in one line may make the Search highlighting in a
1278 * previous line invalid. Simple solution: redraw all visible
1279 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001280 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001282 if (search_hl.rm.regprog != NULL
1283 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001285 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001286 {
1287 cur = wp->w_match_head;
1288 while (cur != NULL)
1289 {
1290 if (cur->match.regprog != NULL
1291 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001292 {
1293 top_to_mod = TRUE;
1294 break;
1295 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001296 cur = cur->next;
1297 }
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299#endif
1300 }
1301#ifdef FEAT_FOLDING
1302 if (mod_top != 0 && hasAnyFolding(wp))
1303 {
1304 linenr_T lnumt, lnumb;
1305
1306 /*
1307 * A change in a line can cause lines above it to become folded or
1308 * unfolded. Find the top most buffer line that may be affected.
1309 * If the line was previously folded and displayed, get the first
1310 * line of that fold. If the line is folded now, get the first
1311 * folded line. Use the minimum of these two.
1312 */
1313
1314 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1315 * the line below it. If there is no valid entry, use w_topline.
1316 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1317 * to this line. If there is no valid entry, use MAXLNUM. */
1318 lnumt = wp->w_topline;
1319 lnumb = MAXLNUM;
1320 for (i = 0; i < wp->w_lines_valid; ++i)
1321 if (wp->w_lines[i].wl_valid)
1322 {
1323 if (wp->w_lines[i].wl_lastlnum < mod_top)
1324 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1325 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1326 {
1327 lnumb = wp->w_lines[i].wl_lnum;
1328 /* When there is a fold column it might need updating
1329 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001330 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 ++lnumb;
1332 }
1333 }
1334
1335 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1336 if (mod_top > lnumt)
1337 mod_top = lnumt;
1338
1339 /* Now do the same for the bottom line (one above mod_bot). */
1340 --mod_bot;
1341 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1342 ++mod_bot;
1343 if (mod_bot < lnumb)
1344 mod_bot = lnumb;
1345 }
1346#endif
1347
1348 /* When a change starts above w_topline and the end is below
1349 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350 * If the end of the change is above w_topline: do like no change was
1351 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (mod_top != 0 && mod_top < wp->w_topline)
1353 {
1354 if (mod_bot > wp->w_topline)
1355 mod_top = wp->w_topline;
1356#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 top_end = 1;
1359#endif
1360 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001361
1362 /* When line numbers are displayed need to redraw all lines below
1363 * inserted/deleted lines. */
1364 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1365 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367
1368 /*
1369 * When only displaying the lines at the top, set top_end. Used when
1370 * window has scrolled down for msg_scrolled.
1371 */
1372 if (type == REDRAW_TOP)
1373 {
1374 j = 0;
1375 for (i = 0; i < wp->w_lines_valid; ++i)
1376 {
1377 j += wp->w_lines[i].wl_size;
1378 if (j >= wp->w_upd_rows)
1379 {
1380 top_end = j;
1381 break;
1382 }
1383 }
1384 if (top_end == 0)
1385 /* not found (cannot happen?): redraw everything */
1386 type = NOT_VALID;
1387 else
1388 /* top area defined, the rest is VALID */
1389 type = VALID;
1390 }
1391
Bram Moolenaar367329b2007-08-30 11:53:22 +00001392 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001393 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1394 * non-zero and thus not FALSE) will indicate that screenclear() was not
1395 * called. */
1396 if (screen_cleared)
1397 screen_cleared = MAYBE;
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 /*
1400 * If there are no changes on the screen that require a complete redraw,
1401 * handle three cases:
1402 * 1: we are off the top of the screen by a few lines: scroll down
1403 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1404 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1405 * w_lines[] that needs updating.
1406 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001407 if ((type == VALID || type == SOME_VALID
1408 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#ifdef FEAT_DIFF
1410 && !wp->w_botfill && !wp->w_old_botfill
1411#endif
1412 )
1413 {
1414 if (mod_top != 0 && wp->w_topline == mod_top)
1415 {
1416 /*
1417 * w_topline is the first changed line, the scrolling will be done
1418 * further down.
1419 */
1420 }
1421 else if (wp->w_lines[0].wl_valid
1422 && (wp->w_topline < wp->w_lines[0].wl_lnum
1423#ifdef FEAT_DIFF
1424 || (wp->w_topline == wp->w_lines[0].wl_lnum
1425 && wp->w_topfill > wp->w_old_topfill)
1426#endif
1427 ))
1428 {
1429 /*
1430 * New topline is above old topline: May scroll down.
1431 */
1432#ifdef FEAT_FOLDING
1433 if (hasAnyFolding(wp))
1434 {
1435 linenr_T ln;
1436
1437 /* count the number of lines we are off, counting a sequence
1438 * of folded lines as one */
1439 j = 0;
1440 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1441 {
1442 ++j;
1443 if (j >= wp->w_height - 2)
1444 break;
1445 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1446 }
1447 }
1448 else
1449#endif
1450 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1451 if (j < wp->w_height - 2) /* not too far off */
1452 {
1453 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1454#ifdef FEAT_DIFF
1455 /* insert extra lines for previously invisible filler lines */
1456 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1457 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1458 - wp->w_old_topfill;
1459#endif
1460 if (i < wp->w_height - 2) /* less than a screen off */
1461 {
1462 /*
1463 * Try to insert the correct number of lines.
1464 * If not the last window, delete the lines at the bottom.
1465 * win_ins_lines may fail when the terminal can't do it.
1466 */
1467 if (i > 0)
1468 check_for_delay(FALSE);
1469 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1470 {
1471 if (wp->w_lines_valid != 0)
1472 {
1473 /* Need to update rows that are new, stop at the
1474 * first one that scrolled down. */
1475 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
1478 /* Move the entries that were scrolled, disable
1479 * the entries for the lines to be redrawn. */
1480 if ((wp->w_lines_valid += j) > wp->w_height)
1481 wp->w_lines_valid = wp->w_height;
1482 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1483 wp->w_lines[idx] = wp->w_lines[idx - j];
1484 while (idx >= 0)
1485 wp->w_lines[idx--].wl_valid = FALSE;
1486 }
1487 }
1488 else
1489 mid_start = 0; /* redraw all lines */
1490 }
1491 else
1492 mid_start = 0; /* redraw all lines */
1493 }
1494 else
1495 mid_start = 0; /* redraw all lines */
1496 }
1497 else
1498 {
1499 /*
1500 * New topline is at or below old topline: May scroll up.
1501 * When topline didn't change, find first entry in w_lines[] that
1502 * needs updating.
1503 */
1504
1505 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1506 j = -1;
1507 row = 0;
1508 for (i = 0; i < wp->w_lines_valid; i++)
1509 {
1510 if (wp->w_lines[i].wl_valid
1511 && wp->w_lines[i].wl_lnum == wp->w_topline)
1512 {
1513 j = i;
1514 break;
1515 }
1516 row += wp->w_lines[i].wl_size;
1517 }
1518 if (j == -1)
1519 {
1520 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1521 * lines */
1522 mid_start = 0;
1523 }
1524 else
1525 {
1526 /*
1527 * Try to delete the correct number of lines.
1528 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1529 */
1530#ifdef FEAT_DIFF
1531 /* If the topline didn't change, delete old filler lines,
1532 * otherwise delete filler lines of the new topline... */
1533 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1534 row += wp->w_old_topfill;
1535 else
1536 row += diff_check_fill(wp, wp->w_topline);
1537 /* ... but don't delete new filler lines. */
1538 row -= wp->w_topfill;
1539#endif
1540 if (row > 0)
1541 {
1542 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001543 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1544 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 bot_start = wp->w_height - row;
1546 else
1547 mid_start = 0; /* redraw all lines */
1548 }
1549 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1550 {
1551 /*
1552 * Skip the lines (below the deleted lines) that are still
1553 * valid and don't need redrawing. Copy their info
1554 * upwards, to compensate for the deleted lines. Set
1555 * bot_start to the first row that needs redrawing.
1556 */
1557 bot_start = 0;
1558 idx = 0;
1559 for (;;)
1560 {
1561 wp->w_lines[idx] = wp->w_lines[j];
1562 /* stop at line that didn't fit, unless it is still
1563 * valid (no lines deleted) */
1564 if (row > 0 && bot_start + row
1565 + (int)wp->w_lines[j].wl_size > wp->w_height)
1566 {
1567 wp->w_lines_valid = idx + 1;
1568 break;
1569 }
1570 bot_start += wp->w_lines[idx++].wl_size;
1571
1572 /* stop at the last valid entry in w_lines[].wl_size */
1573 if (++j >= wp->w_lines_valid)
1574 {
1575 wp->w_lines_valid = idx;
1576 break;
1577 }
1578 }
1579#ifdef FEAT_DIFF
1580 /* Correct the first entry for filler lines at the top
1581 * when it won't get updated below. */
1582 if (wp->w_p_diff && bot_start > 0)
1583 wp->w_lines[0].wl_size =
1584 plines_win_nofill(wp, wp->w_topline, TRUE)
1585 + wp->w_topfill;
1586#endif
1587 }
1588 }
1589 }
1590
1591 /* When starting redraw in the first line, redraw all lines. When
1592 * there is only one window it's probably faster to clear the screen
1593 * first. */
1594 if (mid_start == 0)
1595 {
1596 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001597 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001598 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001599 /* Clear the screen when it was not done by win_del_lines() or
1600 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1601 * then. */
1602 if (screen_cleared != TRUE)
1603 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001604#ifdef FEAT_WINDOWS
1605 /* The screen was cleared, redraw the tab pages line. */
1606 if (redraw_tabline)
1607 draw_tabline();
1608#endif
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001611
1612 /* When win_del_lines() or win_ins_lines() caused the screen to be
1613 * cleared (only happens for the first window) or when screenclear()
1614 * was called directly above, "must_redraw" will have been set to
1615 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1616 if (screen_cleared == TRUE)
1617 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 else
1620 {
1621 /* Not VALID or INVERTED: redraw all lines. */
1622 mid_start = 0;
1623 mid_end = wp->w_height;
1624 }
1625
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001626 if (type == SOME_VALID)
1627 {
1628 /* SOME_VALID: redraw all lines. */
1629 mid_start = 0;
1630 mid_end = wp->w_height;
1631 type = NOT_VALID;
1632 }
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 /* check if we are updating or removing the inverted part */
1635 if ((VIsual_active && buf == curwin->w_buffer)
1636 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1637 {
1638 linenr_T from, to;
1639
1640 if (VIsual_active)
1641 {
1642 if (VIsual_active
1643 && (VIsual_mode != wp->w_old_visual_mode
1644 || type == INVERTED_ALL))
1645 {
1646 /*
1647 * If the type of Visual selection changed, redraw the whole
1648 * selection. Also when the ownership of the X selection is
1649 * gained or lost.
1650 */
1651 if (curwin->w_cursor.lnum < VIsual.lnum)
1652 {
1653 from = curwin->w_cursor.lnum;
1654 to = VIsual.lnum;
1655 }
1656 else
1657 {
1658 from = VIsual.lnum;
1659 to = curwin->w_cursor.lnum;
1660 }
1661 /* redraw more when the cursor moved as well */
1662 if (wp->w_old_cursor_lnum < from)
1663 from = wp->w_old_cursor_lnum;
1664 if (wp->w_old_cursor_lnum > to)
1665 to = wp->w_old_cursor_lnum;
1666 if (wp->w_old_visual_lnum < from)
1667 from = wp->w_old_visual_lnum;
1668 if (wp->w_old_visual_lnum > to)
1669 to = wp->w_old_visual_lnum;
1670 }
1671 else
1672 {
1673 /*
1674 * Find the line numbers that need to be updated: The lines
1675 * between the old cursor position and the current cursor
1676 * position. Also check if the Visual position changed.
1677 */
1678 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1679 {
1680 from = curwin->w_cursor.lnum;
1681 to = wp->w_old_cursor_lnum;
1682 }
1683 else
1684 {
1685 from = wp->w_old_cursor_lnum;
1686 to = curwin->w_cursor.lnum;
1687 if (from == 0) /* Visual mode just started */
1688 from = to;
1689 }
1690
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001691 if (VIsual.lnum != wp->w_old_visual_lnum
1692 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 if (wp->w_old_visual_lnum < from
1695 && wp->w_old_visual_lnum != 0)
1696 from = wp->w_old_visual_lnum;
1697 if (wp->w_old_visual_lnum > to)
1698 to = wp->w_old_visual_lnum;
1699 if (VIsual.lnum < from)
1700 from = VIsual.lnum;
1701 if (VIsual.lnum > to)
1702 to = VIsual.lnum;
1703 }
1704 }
1705
1706 /*
1707 * If in block mode and changed column or curwin->w_curswant:
1708 * update all lines.
1709 * First compute the actual start and end column.
1710 */
1711 if (VIsual_mode == Ctrl_V)
1712 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001713 colnr_T fromc, toc;
1714#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1715 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar404406a2014-10-09 13:24:43 +02001717 if (curwin->w_p_lbr)
1718 ve_flags = VE_ALL;
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001721#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1722 ve_flags = save_ve_flags;
1723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 ++toc;
1725 if (curwin->w_curswant == MAXCOL)
1726 toc = MAXCOL;
1727
1728 if (fromc != wp->w_old_cursor_fcol
1729 || toc != wp->w_old_cursor_lcol)
1730 {
1731 if (from > VIsual.lnum)
1732 from = VIsual.lnum;
1733 if (to < VIsual.lnum)
1734 to = VIsual.lnum;
1735 }
1736 wp->w_old_cursor_fcol = fromc;
1737 wp->w_old_cursor_lcol = toc;
1738 }
1739 }
1740 else
1741 {
1742 /* Use the line numbers of the old Visual area. */
1743 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1744 {
1745 from = wp->w_old_cursor_lnum;
1746 to = wp->w_old_visual_lnum;
1747 }
1748 else
1749 {
1750 from = wp->w_old_visual_lnum;
1751 to = wp->w_old_cursor_lnum;
1752 }
1753 }
1754
1755 /*
1756 * There is no need to update lines above the top of the window.
1757 */
1758 if (from < wp->w_topline)
1759 from = wp->w_topline;
1760
1761 /*
1762 * If we know the value of w_botline, use it to restrict the update to
1763 * the lines that are visible in the window.
1764 */
1765 if (wp->w_valid & VALID_BOTLINE)
1766 {
1767 if (from >= wp->w_botline)
1768 from = wp->w_botline - 1;
1769 if (to >= wp->w_botline)
1770 to = wp->w_botline - 1;
1771 }
1772
1773 /*
1774 * Find the minimal part to be updated.
1775 * Watch out for scrolling that made entries in w_lines[] invalid.
1776 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1777 * top_end; need to redraw from top_end to the "to" line.
1778 * A middle mouse click with a Visual selection may change the text
1779 * above the Visual area and reset wl_valid, do count these for
1780 * mid_end (in srow).
1781 */
1782 if (mid_start > 0)
1783 {
1784 lnum = wp->w_topline;
1785 idx = 0;
1786 srow = 0;
1787 if (scrolled_down)
1788 mid_start = top_end;
1789 else
1790 mid_start = 0;
1791 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1792 {
1793 if (wp->w_lines[idx].wl_valid)
1794 mid_start += wp->w_lines[idx].wl_size;
1795 else if (!scrolled_down)
1796 srow += wp->w_lines[idx].wl_size;
1797 ++idx;
1798# ifdef FEAT_FOLDING
1799 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1800 lnum = wp->w_lines[idx].wl_lnum;
1801 else
1802# endif
1803 ++lnum;
1804 }
1805 srow += mid_start;
1806 mid_end = wp->w_height;
1807 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1808 {
1809 if (wp->w_lines[idx].wl_valid
1810 && wp->w_lines[idx].wl_lnum >= to + 1)
1811 {
1812 /* Only update until first row of this line */
1813 mid_end = srow;
1814 break;
1815 }
1816 srow += wp->w_lines[idx].wl_size;
1817 }
1818 }
1819 }
1820
1821 if (VIsual_active && buf == curwin->w_buffer)
1822 {
1823 wp->w_old_visual_mode = VIsual_mode;
1824 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1825 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001826 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 wp->w_old_curswant = curwin->w_curswant;
1828 }
1829 else
1830 {
1831 wp->w_old_visual_mode = 0;
1832 wp->w_old_cursor_lnum = 0;
1833 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001834 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
1837#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1838 /* reset got_int, otherwise regexp won't work */
1839 save_got_int = got_int;
1840 got_int = 0;
1841#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001842#ifdef SYN_TIME_LIMIT
1843 /* Set the time limit to 'redrawtime'. */
1844 profile_setlimit(p_rdt, &syntax_tm);
1845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846#ifdef FEAT_FOLDING
1847 win_foldinfo.fi_level = 0;
1848#endif
1849
1850 /*
1851 * Update all the window rows.
1852 */
1853 idx = 0; /* first entry in w_lines[].wl_size */
1854 row = 0;
1855 srow = 0;
1856 lnum = wp->w_topline; /* first line shown in window */
1857 for (;;)
1858 {
1859 /* stop updating when reached the end of the window (check for _past_
1860 * the end of the window is at the end of the loop) */
1861 if (row == wp->w_height)
1862 {
1863 didline = TRUE;
1864 break;
1865 }
1866
1867 /* stop updating when hit the end of the file */
1868 if (lnum > buf->b_ml.ml_line_count)
1869 {
1870 eof = TRUE;
1871 break;
1872 }
1873
1874 /* Remember the starting row of the line that is going to be dealt
1875 * with. It is used further down when the line doesn't fit. */
1876 srow = row;
1877
1878 /*
1879 * Update a line when it is in an area that needs updating, when it
1880 * has changes or w_lines[idx] is invalid.
1881 * bot_start may be halfway a wrapped line after using
1882 * win_del_lines(), check if the current line includes it.
1883 * When syntax folding is being used, the saved syntax states will
1884 * already have been updated, we can't see where the syntax state is
1885 * the same again, just update until the end of the window.
1886 */
1887 if (row < top_end
1888 || (row >= mid_start && row < mid_end)
1889#ifdef FEAT_SEARCH_EXTRA
1890 || top_to_mod
1891#endif
1892 || idx >= wp->w_lines_valid
1893 || (row + wp->w_lines[idx].wl_size > bot_start)
1894 || (mod_top != 0
1895 && (lnum == mod_top
1896 || (lnum >= mod_top
1897 && (lnum < mod_bot
1898#ifdef FEAT_SYN_HL
1899 || did_update == DID_FOLD
1900 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001901 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 && (
1903# ifdef FEAT_FOLDING
1904 (foldmethodIsSyntax(wp)
1905 && hasAnyFolding(wp)) ||
1906# endif
1907 syntax_check_changed(lnum)))
1908#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001909#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001910 /* match in fixed position might need redraw
1911 * if lines were inserted or deleted */
1912 || (wp->w_match_head != NULL
1913 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 )))))
1916 {
1917#ifdef FEAT_SEARCH_EXTRA
1918 if (lnum == mod_top)
1919 top_to_mod = FALSE;
1920#endif
1921
1922 /*
1923 * When at start of changed lines: May scroll following lines
1924 * up or down to minimize redrawing.
1925 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001926 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 */
1928 if (lnum == mod_top
1929 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001930 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 int old_rows = 0;
1933 int new_rows = 0;
1934 int xtra_rows;
1935 linenr_T l;
1936
1937 /* Count the old number of window rows, using w_lines[], which
1938 * should still contain the sizes for the lines as they are
1939 * currently displayed. */
1940 for (i = idx; i < wp->w_lines_valid; ++i)
1941 {
1942 /* Only valid lines have a meaningful wl_lnum. Invalid
1943 * lines are part of the changed area. */
1944 if (wp->w_lines[i].wl_valid
1945 && wp->w_lines[i].wl_lnum == mod_bot)
1946 break;
1947 old_rows += wp->w_lines[i].wl_size;
1948#ifdef FEAT_FOLDING
1949 if (wp->w_lines[i].wl_valid
1950 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1951 {
1952 /* Must have found the last valid entry above mod_bot.
1953 * Add following invalid entries. */
1954 ++i;
1955 while (i < wp->w_lines_valid
1956 && !wp->w_lines[i].wl_valid)
1957 old_rows += wp->w_lines[i++].wl_size;
1958 break;
1959 }
1960#endif
1961 }
1962
1963 if (i >= wp->w_lines_valid)
1964 {
1965 /* We can't find a valid line below the changed lines,
1966 * need to redraw until the end of the window.
1967 * Inserting/deleting lines has no use. */
1968 bot_start = 0;
1969 }
1970 else
1971 {
1972 /* Able to count old number of rows: Count new window
1973 * rows, and may insert/delete lines */
1974 j = idx;
1975 for (l = lnum; l < mod_bot; ++l)
1976 {
1977#ifdef FEAT_FOLDING
1978 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1979 ++new_rows;
1980 else
1981#endif
1982#ifdef FEAT_DIFF
1983 if (l == wp->w_topline)
1984 new_rows += plines_win_nofill(wp, l, TRUE)
1985 + wp->w_topfill;
1986 else
1987#endif
1988 new_rows += plines_win(wp, l, TRUE);
1989 ++j;
1990 if (new_rows > wp->w_height - row - 2)
1991 {
1992 /* it's getting too much, must redraw the rest */
1993 new_rows = 9999;
1994 break;
1995 }
1996 }
1997 xtra_rows = new_rows - old_rows;
1998 if (xtra_rows < 0)
1999 {
2000 /* May scroll text up. If there is not enough
2001 * remaining text or scrolling fails, must redraw the
2002 * rest. If scrolling works, must redraw the text
2003 * below the scrolled text. */
2004 if (row - xtra_rows >= wp->w_height - 2)
2005 mod_bot = MAXLNUM;
2006 else
2007 {
2008 check_for_delay(FALSE);
2009 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002010 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 mod_bot = MAXLNUM;
2012 else
2013 bot_start = wp->w_height + xtra_rows;
2014 }
2015 }
2016 else if (xtra_rows > 0)
2017 {
2018 /* May scroll text down. If there is not enough
2019 * remaining text of scrolling fails, must redraw the
2020 * rest. */
2021 if (row + xtra_rows >= wp->w_height - 2)
2022 mod_bot = MAXLNUM;
2023 else
2024 {
2025 check_for_delay(FALSE);
2026 if (win_ins_lines(wp, row + old_rows,
2027 xtra_rows, FALSE, FALSE) == FAIL)
2028 mod_bot = MAXLNUM;
2029 else if (top_end > row + old_rows)
2030 /* Scrolled the part at the top that requires
2031 * updating down. */
2032 top_end += xtra_rows;
2033 }
2034 }
2035
2036 /* When not updating the rest, may need to move w_lines[]
2037 * entries. */
2038 if (mod_bot != MAXLNUM && i != j)
2039 {
2040 if (j < i)
2041 {
2042 int x = row + new_rows;
2043
2044 /* move entries in w_lines[] upwards */
2045 for (;;)
2046 {
2047 /* stop at last valid entry in w_lines[] */
2048 if (i >= wp->w_lines_valid)
2049 {
2050 wp->w_lines_valid = j;
2051 break;
2052 }
2053 wp->w_lines[j] = wp->w_lines[i];
2054 /* stop at a line that won't fit */
2055 if (x + (int)wp->w_lines[j].wl_size
2056 > wp->w_height)
2057 {
2058 wp->w_lines_valid = j + 1;
2059 break;
2060 }
2061 x += wp->w_lines[j++].wl_size;
2062 ++i;
2063 }
2064 if (bot_start > x)
2065 bot_start = x;
2066 }
2067 else /* j > i */
2068 {
2069 /* move entries in w_lines[] downwards */
2070 j -= i;
2071 wp->w_lines_valid += j;
2072 if (wp->w_lines_valid > wp->w_height)
2073 wp->w_lines_valid = wp->w_height;
2074 for (i = wp->w_lines_valid; i - j >= idx; --i)
2075 wp->w_lines[i] = wp->w_lines[i - j];
2076
2077 /* The w_lines[] entries for inserted lines are
2078 * now invalid, but wl_size may be used above.
2079 * Reset to zero. */
2080 while (i >= idx)
2081 {
2082 wp->w_lines[i].wl_size = 0;
2083 wp->w_lines[i--].wl_valid = FALSE;
2084 }
2085 }
2086 }
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
2089
2090#ifdef FEAT_FOLDING
2091 /*
2092 * When lines are folded, display one line for all of them.
2093 * Otherwise, display normally (can be several display lines when
2094 * 'wrap' is on).
2095 */
2096 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2097 if (fold_count != 0)
2098 {
2099 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2100 ++row;
2101 --fold_count;
2102 wp->w_lines[idx].wl_folded = TRUE;
2103 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2104# ifdef FEAT_SYN_HL
2105 did_update = DID_FOLD;
2106# endif
2107 }
2108 else
2109#endif
2110 if (idx < wp->w_lines_valid
2111 && wp->w_lines[idx].wl_valid
2112 && wp->w_lines[idx].wl_lnum == lnum
2113 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002114 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 && srow + wp->w_lines[idx].wl_size > wp->w_height
2116#ifdef FEAT_DIFF
2117 && diff_check_fill(wp, lnum) == 0
2118#endif
2119 )
2120 {
2121 /* This line is not going to fit. Don't draw anything here,
2122 * will draw "@ " lines below. */
2123 row = wp->w_height + 1;
2124 }
2125 else
2126 {
2127#ifdef FEAT_SEARCH_EXTRA
2128 prepare_search_hl(wp, lnum);
2129#endif
2130#ifdef FEAT_SYN_HL
2131 /* Let the syntax stuff know we skipped a few lines. */
2132 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002133 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 syntax_end_parsing(syntax_last_parsed + 1);
2135#endif
2136
2137 /*
2138 * Display one line.
2139 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002140 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2141#ifdef SYN_TIME_LIMIT
2142 &syntax_tm
2143#else
2144 NULL
2145#endif
2146 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148#ifdef FEAT_FOLDING
2149 wp->w_lines[idx].wl_folded = FALSE;
2150 wp->w_lines[idx].wl_lastlnum = lnum;
2151#endif
2152#ifdef FEAT_SYN_HL
2153 did_update = DID_LINE;
2154 syntax_last_parsed = lnum;
2155#endif
2156 }
2157
2158 wp->w_lines[idx].wl_lnum = lnum;
2159 wp->w_lines[idx].wl_valid = TRUE;
2160 if (row > wp->w_height) /* past end of screen */
2161 {
2162 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002163 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2165 ++idx;
2166 break;
2167 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002168 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 wp->w_lines[idx].wl_size = row - srow;
2170 ++idx;
2171#ifdef FEAT_FOLDING
2172 lnum += fold_count + 1;
2173#else
2174 ++lnum;
2175#endif
2176 }
2177 else
2178 {
2179 /* This line does not need updating, advance to the next one */
2180 row += wp->w_lines[idx++].wl_size;
2181 if (row > wp->w_height) /* past end of screen */
2182 break;
2183#ifdef FEAT_FOLDING
2184 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2185#else
2186 ++lnum;
2187#endif
2188#ifdef FEAT_SYN_HL
2189 did_update = DID_NONE;
2190#endif
2191 }
2192
2193 if (lnum > buf->b_ml.ml_line_count)
2194 {
2195 eof = TRUE;
2196 break;
2197 }
2198 }
2199 /*
2200 * End of loop over all window lines.
2201 */
2202
2203
2204 if (idx > wp->w_lines_valid)
2205 wp->w_lines_valid = idx;
2206
2207#ifdef FEAT_SYN_HL
2208 /*
2209 * Let the syntax stuff know we stop parsing here.
2210 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002211 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 syntax_end_parsing(syntax_last_parsed + 1);
2213#endif
2214
2215 /*
2216 * If we didn't hit the end of the file, and we didn't finish the last
2217 * line we were working on, then the line didn't fit.
2218 */
2219 wp->w_empty_rows = 0;
2220#ifdef FEAT_DIFF
2221 wp->w_filler_rows = 0;
2222#endif
2223 if (!eof && !didline)
2224 {
2225 if (lnum == wp->w_topline)
2226 {
2227 /*
2228 * Single line that does not fit!
2229 * Don't overwrite it, it can be edited.
2230 */
2231 wp->w_botline = lnum + 1;
2232 }
2233#ifdef FEAT_DIFF
2234 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2235 {
2236 /* Window ends in filler lines. */
2237 wp->w_botline = lnum;
2238 wp->w_filler_rows = wp->w_height - srow;
2239 }
2240#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002241 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2242 {
2243 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2244
2245 /*
2246 * Last line isn't finished: Display "@@@" in the last screen line.
2247 */
2248 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002249 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002250 screen_fill(scr_row, scr_row + 1,
2251 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002252 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002253 set_empty_rows(wp, srow);
2254 wp->w_botline = lnum;
2255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2257 {
2258 /*
2259 * Last line isn't finished: Display "@@@" at the end.
2260 */
2261 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2262 W_WINROW(wp) + wp->w_height,
2263 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002264 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 set_empty_rows(wp, srow);
2266 wp->w_botline = lnum;
2267 }
2268 else
2269 {
2270 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2271 wp->w_botline = lnum;
2272 }
2273 }
2274 else
2275 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002276#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 draw_vsep_win(wp, row);
2278#endif
2279 if (eof) /* we hit the end of the file */
2280 {
2281 wp->w_botline = buf->b_ml.ml_line_count + 1;
2282#ifdef FEAT_DIFF
2283 j = diff_check_fill(wp, wp->w_botline);
2284 if (j > 0 && !wp->w_botfill)
2285 {
2286 /*
2287 * Display filler lines at the end of the file
2288 */
2289 if (char2cells(fill_diff) > 1)
2290 i = '-';
2291 else
2292 i = fill_diff;
2293 if (row + j > wp->w_height)
2294 j = wp->w_height - row;
2295 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2296 row += j;
2297 }
2298#endif
2299 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002300 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 wp->w_botline = lnum;
2302
2303 /* make sure the rest of the screen is blank */
2304 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002305 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307
2308 /* Reset the type of redrawing required, the window has been updated. */
2309 wp->w_redr_type = 0;
2310#ifdef FEAT_DIFF
2311 wp->w_old_topfill = wp->w_topfill;
2312 wp->w_old_botfill = wp->w_botfill;
2313#endif
2314
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002315 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 /*
2318 * There is a trick with w_botline. If we invalidate it on each
2319 * change that might modify it, this will cause a lot of expensive
2320 * calls to plines() in update_topline() each time. Therefore the
2321 * value of w_botline is often approximated, and this value is used to
2322 * compute the value of w_topline. If the value of w_botline was
2323 * wrong, check that the value of w_topline is correct (cursor is on
2324 * the visible part of the text). If it's not, we need to redraw
2325 * again. Mostly this just means scrolling up a few lines, so it
2326 * doesn't look too bad. Only do this for the current window (where
2327 * changes are relevant).
2328 */
2329 wp->w_valid |= VALID_BOTLINE;
2330 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2331 {
2332 recursive = TRUE;
2333 curwin->w_valid &= ~VALID_TOPLINE;
2334 update_topline(); /* may invalidate w_botline again */
2335 if (must_redraw != 0)
2336 {
2337 /* Don't update for changes in buffer again. */
2338 i = curbuf->b_mod_set;
2339 curbuf->b_mod_set = FALSE;
2340 win_update(curwin);
2341 must_redraw = 0;
2342 curbuf->b_mod_set = i;
2343 }
2344 recursive = FALSE;
2345 }
2346 }
2347
2348#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2349 /* restore got_int, unless CTRL-C was hit while redrawing */
2350 if (!got_int)
2351 got_int = save_got_int;
2352#endif
2353}
2354
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355/*
2356 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2357 * as the filler character.
2358 */
2359 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002360win_draw_end(
2361 win_T *wp,
2362 int c1,
2363 int c2,
2364 int row,
2365 int endrow,
2366 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
2368#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2369 int n = 0;
2370# define FDC_OFF n
2371#else
2372# define FDC_OFF 0
2373#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002374#ifdef FEAT_FOLDING
2375 int fdc = compute_foldcolumn(wp, 0);
2376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifdef FEAT_RIGHTLEFT
2379 if (wp->w_p_rl)
2380 {
2381 /* No check for cmdline window: should never be right-left. */
2382# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002383 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (n > 0)
2386 {
2387 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002388 if (n > W_WIDTH(wp))
2389 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2391 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002392 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394# endif
2395# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002396 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 int nn = n + 2;
2399
2400 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002401 if (nn > W_WIDTH(wp))
2402 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2404 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002405 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 n = nn;
2407 }
2408# endif
2409 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2410 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002411 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2413 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002414 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else
2417#endif
2418 {
2419#ifdef FEAT_CMDWIN
2420 if (cmdwin_type != 0 && wp == curwin)
2421 {
2422 /* draw the cmdline character in the leftmost column */
2423 n = 1;
2424 if (n > wp->w_width)
2425 n = wp->w_width;
2426 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2427 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002428 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430#endif
2431#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002432 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002434 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /* draw the fold column at the left */
2437 if (nn > W_WIDTH(wp))
2438 nn = W_WIDTH(wp);
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2440 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002441 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 n = nn;
2443 }
2444#endif
2445#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002446 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 int nn = n + 2;
2449
2450 /* draw the sign column after the fold column */
2451 if (nn > W_WIDTH(wp))
2452 nn = W_WIDTH(wp);
2453 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2454 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002455 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 n = nn;
2457 }
2458#endif
2459 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2460 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002461 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 set_empty_rows(wp, row);
2464}
2465
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002467static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468
2469/*
2470 * Advance **color_cols and return TRUE when there are columns to draw.
2471 */
2472 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002473advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002474{
2475 while (**color_cols >= 0 && vcol > **color_cols)
2476 ++*color_cols;
2477 return (**color_cols >= 0);
2478}
2479#endif
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481#ifdef FEAT_FOLDING
2482/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002483 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2484 * space is available for window "wp", minus "col".
2485 */
2486 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002487compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002488{
2489 int fdc = wp->w_p_fdc;
2490 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2491 int wwidth = W_WIDTH(wp);
2492
2493 if (fdc > wwidth - (col + wmw))
2494 fdc = wwidth - (col + wmw);
2495 return fdc;
2496}
2497
2498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * Display one folded line.
2500 */
2501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002502fold_line(
2503 win_T *wp,
2504 long fold_count,
2505 foldinfo_T *foldinfo,
2506 linenr_T lnum,
2507 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002509 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 pos_T *top, *bot;
2511 linenr_T lnume = lnum + fold_count - 1;
2512 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002513 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int col;
2516 int txtcol;
2517 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002518 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
2520 /* Build the fold line:
2521 * 1. Add the cmdwin_type for the command-line window
2522 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002523 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * 4. Compose the text
2525 * 5. Add the text
2526 * 6. set highlighting for the Visual area an other text
2527 */
2528 col = 0;
2529
2530 /*
2531 * 1. Add the cmdwin_type for the command-line window
2532 * Ignores 'rightleft', this window is never right-left.
2533 */
2534#ifdef FEAT_CMDWIN
2535 if (cmdwin_type != 0 && wp == curwin)
2536 {
2537 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002538 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#ifdef FEAT_MBYTE
2540 if (enc_utf8)
2541 ScreenLinesUC[off] = 0;
2542#endif
2543 ++col;
2544 }
2545#endif
2546
2547 /*
2548 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002549 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002551 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (fdc > 0)
2553 {
2554 fill_foldcolumn(buf, wp, TRUE, lnum);
2555#ifdef FEAT_RIGHTLEFT
2556 if (wp->w_p_rl)
2557 {
2558 int i;
2559
2560 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002561 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 /* reverse the fold column */
2563 for (i = 0; i < fdc; ++i)
2564 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2565 }
2566 else
2567#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002568 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 col += fdc;
2570 }
2571
2572#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002573# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2574 for (ri = 0; ri < l; ++ri) \
2575 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2576 else \
2577 for (ri = 0; ri < l; ++ri) \
2578 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002580# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2581 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#endif
2583
Bram Moolenaar64486672010-05-16 15:46:46 +02002584 /* Set all attributes of the 'number' or 'relativenumber' column and the
2585 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002586 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588#ifdef FEAT_SIGNS
2589 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002590 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 len = W_WIDTH(wp) - col;
2593 if (len > 0)
2594 {
2595 if (len > 2)
2596 len = 2;
2597# ifdef FEAT_RIGHTLEFT
2598 if (wp->w_p_rl)
2599 /* the line number isn't reversed */
2600 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002601 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 else
2603# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002604 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 col += len;
2606 }
2607 }
2608#endif
2609
2610 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002611 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002613 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 len = W_WIDTH(wp) - col;
2616 if (len > 0)
2617 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002618 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002619 long num;
2620 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002621
2622 if (len > w + 1)
2623 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002624
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002625 if (wp->w_p_nu && !wp->w_p_rnu)
2626 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002627 num = (long)lnum;
2628 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002629 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002630 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002631 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002632 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002633 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002634 /* 'number' + 'relativenumber': cursor line shows absolute
2635 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002636 num = lnum;
2637 fmt = "%-*ld ";
2638 }
2639 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002640
Bram Moolenaar700e7342013-01-30 12:31:36 +01002641 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_RIGHTLEFT
2643 if (wp->w_p_rl)
2644 /* the line number isn't reversed */
2645 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002646 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 else
2648#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002649 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 col += len;
2651 }
2652 }
2653
2654 /*
2655 * 4. Compose the folded-line string with 'foldtext', if set.
2656 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002657 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 txtcol = col; /* remember where text starts */
2660
2661 /*
2662 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2663 * Right-left text is put in columns 0 - number-col, normal text is put
2664 * in columns number-col - window-width.
2665 */
2666#ifdef FEAT_MBYTE
2667 if (has_mbyte)
2668 {
2669 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670 int u8c, u8cc[MAX_MCO];
2671 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int idx;
2673 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002674 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675# ifdef FEAT_ARABIC
2676 int prev_c = 0; /* previous Arabic character */
2677 int prev_c1 = 0; /* first composing char for prev_c */
2678# endif
2679
2680# ifdef FEAT_RIGHTLEFT
2681 if (wp->w_p_rl)
2682 idx = off;
2683 else
2684# endif
2685 idx = off + col;
2686
2687 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2688 for (p = text; *p != NUL; )
2689 {
2690 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002691 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (col + cells > W_WIDTH(wp)
2693# ifdef FEAT_RIGHTLEFT
2694 - (wp->w_p_rl ? col : 0)
2695# endif
2696 )
2697 break;
2698 ScreenLines[idx] = *p;
2699 if (enc_utf8)
2700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 u8c = utfc_ptr2char(p, u8cc);
2702 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
2704 ScreenLinesUC[idx] = 0;
2705#ifdef FEAT_ARABIC
2706 prev_c = u8c;
2707#endif
2708 }
2709 else
2710 {
2711#ifdef FEAT_ARABIC
2712 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2713 {
2714 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715 int pc, pc1, nc;
2716 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 int firstbyte = *p;
2718
2719 /* The idea of what is the previous and next
2720 * character depends on 'rightleft'. */
2721 if (wp->w_p_rl)
2722 {
2723 pc = prev_c;
2724 pc1 = prev_c1;
2725 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 else
2729 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 prev_c = u8c;
2735
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002736 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 pc, pc1, nc);
2738 ScreenLines[idx] = firstbyte;
2739 }
2740 else
2741 prev_c = u8c;
2742#endif
2743 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002744#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (u8c >= 0x10000)
2746 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2747 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 for (i = 0; i < Screen_mco; ++i)
2751 {
2752 ScreenLinesC[i][idx] = u8cc[i];
2753 if (u8cc[i] == 0)
2754 break;
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 if (cells > 1)
2758 ScreenLines[idx + 1] = 0;
2759 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002760 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2761 /* double-byte single width character */
2762 ScreenLines2[idx] = p[1];
2763 else if (cells > 1)
2764 /* double-width character */
2765 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 col += cells;
2767 idx += cells;
2768 p += c_len;
2769 }
2770 }
2771 else
2772#endif
2773 {
2774 len = (int)STRLEN(text);
2775 if (len > W_WIDTH(wp) - col)
2776 len = W_WIDTH(wp) - col;
2777 if (len > 0)
2778 {
2779#ifdef FEAT_RIGHTLEFT
2780 if (wp->w_p_rl)
2781 STRNCPY(current_ScreenLine, text, len);
2782 else
2783#endif
2784 STRNCPY(current_ScreenLine + col, text, len);
2785 col += len;
2786 }
2787 }
2788
2789 /* Fill the rest of the line with the fold filler */
2790#ifdef FEAT_RIGHTLEFT
2791 if (wp->w_p_rl)
2792 col -= txtcol;
2793#endif
2794 while (col < W_WIDTH(wp)
2795#ifdef FEAT_RIGHTLEFT
2796 - (wp->w_p_rl ? txtcol : 0)
2797#endif
2798 )
2799 {
2800#ifdef FEAT_MBYTE
2801 if (enc_utf8)
2802 {
2803 if (fill_fold >= 0x80)
2804 {
2805 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002806 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002807 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
2809 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002812 ScreenLines[off + col] = fill_fold;
2813 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002814 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002818 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820
2821 if (text != buf)
2822 vim_free(text);
2823
2824 /*
2825 * 6. set highlighting for the Visual area an other text.
2826 * If all folded lines are in the Visual area, highlight the line.
2827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2829 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002830 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* Visual is after curwin->w_cursor */
2833 top = &curwin->w_cursor;
2834 bot = &VIsual;
2835 }
2836 else
2837 {
2838 /* Visual is before curwin->w_cursor */
2839 top = &VIsual;
2840 bot = &curwin->w_cursor;
2841 }
2842 if (lnum >= top->lnum
2843 && lnume <= bot->lnum
2844 && (VIsual_mode != 'v'
2845 || ((lnum > top->lnum
2846 || (lnum == top->lnum
2847 && top->col == 0))
2848 && (lnume < bot->lnum
2849 || (lnume == bot->lnum
2850 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002851 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
2853 if (VIsual_mode == Ctrl_V)
2854 {
2855 /* Visual block mode: highlight the chars part of the block */
2856 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2857 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002858 if (wp->w_old_cursor_lcol != MAXCOL
2859 && wp->w_old_cursor_lcol + txtcol
2860 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 len = wp->w_old_cursor_lcol;
2862 else
2863 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002864 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002865 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
2868 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002871 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002876#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002877 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2878 if (wp->w_p_cc_cols)
2879 {
2880 int i = 0;
2881 int j = wp->w_p_cc_cols[i];
2882 int old_txtcol = txtcol;
2883
2884 while (j > -1)
2885 {
2886 txtcol += j;
2887 if (wp->w_p_wrap)
2888 txtcol -= wp->w_skipcol;
2889 else
2890 txtcol -= wp->w_leftcol;
2891 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2892 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002893 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002894 txtcol = old_txtcol;
2895 j = wp->w_p_cc_cols[++i];
2896 }
2897 }
2898
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002899 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002900 if (wp->w_p_cuc)
2901 {
2902 txtcol += wp->w_virtcol;
2903 if (wp->w_p_wrap)
2904 txtcol -= wp->w_skipcol;
2905 else
2906 txtcol -= wp->w_leftcol;
2907 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2908 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002909 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002910 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002913 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 (int)W_WIDTH(wp), FALSE);
2915
2916 /*
2917 * Update w_cline_height and w_cline_folded if the cursor line was
2918 * updated (saves a call to plines() later).
2919 */
2920 if (wp == curwin
2921 && lnum <= curwin->w_cursor.lnum
2922 && lnume >= curwin->w_cursor.lnum)
2923 {
2924 curwin->w_cline_row = row;
2925 curwin->w_cline_height = 1;
2926 curwin->w_cline_folded = TRUE;
2927 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2928 }
2929}
2930
2931/*
2932 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2933 */
2934 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002935copy_text_attr(
2936 int off,
2937 char_u *buf,
2938 int len,
2939 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002941 int i;
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 mch_memmove(ScreenLines + off, buf, (size_t)len);
2944# ifdef FEAT_MBYTE
2945 if (enc_utf8)
2946 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2947# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002948 for (i = 0; i < len; ++i)
2949 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002954 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
2956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002957fill_foldcolumn(
2958 char_u *p,
2959 win_T *wp,
2960 int closed, /* TRUE of FALSE */
2961 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
2963 int i = 0;
2964 int level;
2965 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002966 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002967 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002970 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 level = win_foldinfo.fi_level;
2973 if (level > 0)
2974 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002975 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 /* If the column is too narrow, we start at the lowest level that
2979 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002980 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (first_level < 1)
2982 first_level = 1;
2983
Bram Moolenaar1c934292015-01-27 16:39:29 +01002984 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 if (win_foldinfo.fi_lnum == lnum
2987 && first_level + i >= win_foldinfo.fi_low_level)
2988 p[i] = '-';
2989 else if (first_level == 1)
2990 p[i] = '|';
2991 else if (first_level + i <= 9)
2992 p[i] = '0' + first_level + i;
2993 else
2994 p[i] = '>';
2995 if (first_level + i == level)
2996 break;
2997 }
2998 }
2999 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003000 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002#endif /* FEAT_FOLDING */
3003
3004/*
3005 * Display line "lnum" of window 'wp' on the screen.
3006 * Start at row "startrow", stop when "endrow" is reached.
3007 * wp->w_virtcol needs to be valid.
3008 *
3009 * Return the number of last row the line occupies.
3010 */
3011 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003012win_line(
3013 win_T *wp,
3014 linenr_T lnum,
3015 int startrow,
3016 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003017 int nochange UNUSED, /* not updating for changed text */
Bram Moolenaar02e177d2017-08-26 23:43:28 +02003018 proftime_T *syntax_tm UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003020 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3022 int c = 0; /* init for GCC */
3023 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003024#ifdef FEAT_LINEBREAK
3025 long vcol_sbr = -1; /* virtual column after showbreak */
3026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 long vcol_prev = -1; /* "vcol" of previous character */
3028 char_u *line; /* current line */
3029 char_u *ptr; /* current position in "line" */
3030 int row; /* row in the window, excl w_winrow */
3031 int screen_row; /* row on the screen, incl w_winrow */
3032
3033 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3034 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003035 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003036 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 int c_extra = NUL; /* extra chars, all the same */
3038 int extra_attr = 0; /* attributes when n_extra != 0 */
3039 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3040 displaying lcs_eol at end-of-line */
3041 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3042 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3043
3044 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3045 int saved_n_extra = 0;
3046 char_u *saved_p_extra = NULL;
3047 int saved_c_extra = 0;
3048 int saved_char_attr = 0;
3049
3050 int n_attr = 0; /* chars with special attr */
3051 int saved_attr2 = 0; /* char_attr saved for n_attr */
3052 int n_attr3 = 0; /* chars with overruling special attr */
3053 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3054
3055 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3056
3057 int fromcol, tocol; /* start/end of inverting */
3058 int fromcol_prev = -2; /* start of inverting after cursor */
3059 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003061 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 pos_T pos;
3063 long v;
3064
3065 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003066 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3068 in this line */
3069 int attr = 0; /* attributes for area highlighting */
3070 int area_attr = 0; /* attributes desired by highlighting */
3071 int search_attr = 0; /* attributes desired by 'hlsearch' */
3072#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003073 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 int syntax_attr = 0; /* attributes desired by syntax */
3075 int has_syntax = FALSE; /* this buffer has syntax highl. */
3076 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003077 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003078 int draw_color_col = FALSE; /* highlight colorcolumn */
3079 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003080#endif
3081#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003082 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003083# define SPWORDLEN 150
3084 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003085 int nextlinecol = 0; /* column where nextline[] starts */
3086 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003087 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003088 int spell_attr = 0; /* attributes desired by spelling */
3089 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003090 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3091 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003092 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003093 static int cap_col = -1; /* column to check for Cap word */
3094 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003095 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096#endif
3097 int extra_check; /* has syntax or linebreak */
3098#ifdef FEAT_MBYTE
3099 int multi_attr = 0; /* attributes desired by multibyte */
3100 int mb_l = 1; /* multi-byte byte length */
3101 int mb_c = 0; /* decoded multi-byte character */
3102 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003103 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104#endif
3105#ifdef FEAT_DIFF
3106 int filler_lines; /* nr of filler lines to be drawn */
3107 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003108 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 int change_start = MAXCOL; /* first col of changed area */
3110 int change_end = -1; /* last col of changed area */
3111#endif
3112 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3113#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003114 int need_showbreak = FALSE; /* overlong line, skipping first x
3115 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003117#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3118 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003120 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003123 matchitem_T *cur; /* points to the match list */
3124 match_T *shl; /* points to search_hl or a match */
3125 int shl_flag; /* flag to indicate whether search_hl
3126 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003127 int pos_inprogress; /* marks that position match search is
3128 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003129 int prevcol_hl_flag; /* flag to indicate whether prevcol
3130 equals startcol of search_hl or one
3131 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#endif
3133#ifdef FEAT_ARABIC
3134 int prev_c = 0; /* previous Arabic character */
3135 int prev_c1 = 0; /* first composing char for prev_c */
3136#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003137#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003138 int did_line_attr = 0;
3139#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003140#ifdef FEAT_TERMINAL
3141 int get_term_attr = FALSE;
3142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 /* draw_state: items that are drawn in sequence: */
3145#define WL_START 0 /* nothing done yet */
3146#ifdef FEAT_CMDWIN
3147# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3148#else
3149# define WL_CMDLINE WL_START
3150#endif
3151#ifdef FEAT_FOLDING
3152# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3153#else
3154# define WL_FOLD WL_CMDLINE
3155#endif
3156#ifdef FEAT_SIGNS
3157# define WL_SIGN WL_FOLD + 1 /* column for signs */
3158#else
3159# define WL_SIGN WL_FOLD /* column for signs */
3160#endif
3161#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003162#ifdef FEAT_LINEBREAK
3163# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003165# define WL_BRI WL_NR
3166#endif
3167#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3168# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3169#else
3170# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172#define WL_LINE WL_SBR + 1 /* text in the line */
3173 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003174#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 int feedback_col = 0;
3176 int feedback_old_attr = -1;
3177#endif
3178
Bram Moolenaar860cae12010-06-05 23:22:07 +02003179#ifdef FEAT_CONCEAL
3180 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003181 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003182 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003183 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003184 int is_concealing = FALSE;
3185 int boguscols = 0; /* nonexistent columns added to force
3186 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003187 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003188 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003189 int match_conc = 0; /* cchar for match functions */
3190 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003191 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003192# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003193# define FIX_FOR_BOGUSCOLS \
3194 { \
3195 n_extra += vcol_off; \
3196 vcol -= vcol_off; \
3197 vcol_off = 0; \
3198 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003199 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003200 boguscols = 0; \
3201 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003202#else
3203# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 if (startrow > endrow) /* past the end already! */
3207 return startrow;
3208
3209 row = startrow;
3210 screen_row = row + W_WINROW(wp);
3211
3212 /*
3213 * To speed up the loop below, set extra_check when there is linebreak,
3214 * trailing white space and/or syntax processing to be done.
3215 */
3216#ifdef FEAT_LINEBREAK
3217 extra_check = wp->w_p_lbr;
3218#else
3219 extra_check = 0;
3220#endif
3221#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003222 if (syntax_present(wp) && !wp->w_s->b_syn_error
3223# ifdef SYN_TIME_LIMIT
3224 && !wp->w_s->b_syn_slow
3225# endif
3226 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 /* Prepare for syntax highlighting in this line. When there is an
3229 * error, stop syntax highlighting. */
3230 save_did_emsg = did_emsg;
3231 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003232 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003234 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 else
3236 {
3237 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003238#ifdef SYN_TIME_LIMIT
3239 if (!wp->w_s->b_syn_slow)
3240#endif
3241 {
3242 has_syntax = TRUE;
3243 extra_check = TRUE;
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003247
3248 /* Check for columns to display for 'colorcolumn'. */
3249 color_cols = wp->w_p_cc_cols;
3250 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003251 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003252#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003253
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003254#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003255 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003256 {
3257 extra_check = TRUE;
3258 get_term_attr = TRUE;
3259 }
3260#endif
3261
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003262#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003263 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003264 && *wp->w_s->b_p_spl != NUL
3265 && wp->w_s->b_langp.ga_len > 0
3266 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003267 {
3268 /* Prepare for spell checking. */
3269 has_spell = TRUE;
3270 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003271
3272 /* Get the start of the next line, so that words that wrap to the next
3273 * line are found too: "et<line-break>al.".
3274 * Trick: skip a few chars for C/shell/Vim comments */
3275 nextline[SPWORDLEN] = NUL;
3276 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3277 {
3278 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3279 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3280 }
3281
3282 /* When a word wrapped from the previous line the start of the current
3283 * line is valid. */
3284 if (lnum == checked_lnum)
3285 cur_checked_col = checked_col;
3286 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003287
3288 /* When there was a sentence end in the previous line may require a
3289 * word starting with capital in this line. In line 1 always check
3290 * the first word. */
3291 if (lnum != capcol_lnum)
3292 cap_col = -1;
3293 if (lnum == 1)
3294 cap_col = 0;
3295 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif
3298
3299 /*
3300 * handle visual active in this window
3301 */
3302 fromcol = -10;
3303 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3305 {
3306 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003307 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 top = &curwin->w_cursor;
3310 bot = &VIsual;
3311 }
3312 else /* Visual is before curwin->w_cursor */
3313 {
3314 top = &VIsual;
3315 bot = &curwin->w_cursor;
3316 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003317 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (VIsual_mode == Ctrl_V) /* block mode */
3319 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003320 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 fromcol = wp->w_old_cursor_fcol;
3323 tocol = wp->w_old_cursor_lcol;
3324 }
3325 }
3326 else /* non-block mode */
3327 {
3328 if (lnum > top->lnum && lnum <= bot->lnum)
3329 fromcol = 0;
3330 else if (lnum == top->lnum)
3331 {
3332 if (VIsual_mode == 'V') /* linewise */
3333 fromcol = 0;
3334 else
3335 {
3336 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3337 if (gchar_pos(top) == NUL)
3338 tocol = fromcol + 1;
3339 }
3340 }
3341 if (VIsual_mode != 'V' && lnum == bot->lnum)
3342 {
3343 if (*p_sel == 'e' && bot->col == 0
3344#ifdef FEAT_VIRTUALEDIT
3345 && bot->coladd == 0
3346#endif
3347 )
3348 {
3349 fromcol = -10;
3350 tocol = MAXCOL;
3351 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003352 else if (bot->col == MAXCOL)
3353 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else
3355 {
3356 pos = *bot;
3357 if (*p_sel == 'e')
3358 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3359 else
3360 {
3361 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3362 ++tocol;
3363 }
3364 }
3365 }
3366 }
3367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 /* Check if the character under the cursor should not be inverted */
3369 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003370#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 )
3374 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /* if inverting in this line set area_highlighting */
3377 if (fromcol >= 0)
3378 {
3379 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003380 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003382 if ((clip_star.available && !clip_star.owned
3383 && clip_isautosel_star())
3384 || (clip_plus.available && !clip_plus.owned
3385 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003386 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#endif
3388 }
3389 }
3390
3391 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003392 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003394 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && wp == curwin
3396 && lnum >= curwin->w_cursor.lnum
3397 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3398 {
3399 if (lnum == curwin->w_cursor.lnum)
3400 getvcol(curwin, &(curwin->w_cursor),
3401 (colnr_T *)&fromcol, NULL, NULL);
3402 else
3403 fromcol = 0;
3404 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3405 {
3406 pos.lnum = lnum;
3407 pos.col = search_match_endcol;
3408 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3409 }
3410 else
3411 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003412 /* do at least one character; happens when past end of line */
3413 if (fromcol == tocol)
3414 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003416 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418
3419#ifdef FEAT_DIFF
3420 filler_lines = diff_check(wp, lnum);
3421 if (filler_lines < 0)
3422 {
3423 if (filler_lines == -1)
3424 {
3425 if (diff_find_change(wp, lnum, &change_start, &change_end))
3426 diff_hlf = HLF_ADD; /* added line */
3427 else if (change_start == 0)
3428 diff_hlf = HLF_TXD; /* changed text */
3429 else
3430 diff_hlf = HLF_CHD; /* changed line */
3431 }
3432 else
3433 diff_hlf = HLF_ADD; /* added line */
3434 filler_lines = 0;
3435 area_highlighting = TRUE;
3436 }
3437 if (lnum == wp->w_topline)
3438 filler_lines = wp->w_topfill;
3439 filler_todo = filler_lines;
3440#endif
3441
3442#ifdef LINE_ATTR
3443# ifdef FEAT_SIGNS
3444 /* If this line has a sign with line highlighting set line_attr. */
3445 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3446 if (v != 0)
3447 line_attr = sign_get_attr((int)v, TRUE);
3448# endif
3449# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3450 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003451 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003452 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453# endif
3454 if (line_attr != 0)
3455 area_highlighting = TRUE;
3456#endif
3457
3458 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3459 ptr = line;
3460
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003461#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003462 if (has_spell)
3463 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003464 /* For checking first word with a capital skip white space. */
3465 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003466 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003467
Bram Moolenaar30abd282005-06-22 22:35:10 +00003468 /* To be able to spell-check over line boundaries copy the end of the
3469 * current line into nextline[]. Above the start of the next line was
3470 * copied to nextline[SPWORDLEN]. */
3471 if (nextline[SPWORDLEN] == NUL)
3472 {
3473 /* No next line or it is empty. */
3474 nextlinecol = MAXCOL;
3475 nextline_idx = 0;
3476 }
3477 else
3478 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003479 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003480 if (v < SPWORDLEN)
3481 {
3482 /* Short line, use it completely and append the start of the
3483 * next line. */
3484 nextlinecol = 0;
3485 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003486 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003487 nextline_idx = v + 1;
3488 }
3489 else
3490 {
3491 /* Long line, use only the last SPWORDLEN bytes. */
3492 nextlinecol = v - SPWORDLEN;
3493 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3494 nextline_idx = SPWORDLEN + 1;
3495 }
3496 }
3497 }
3498#endif
3499
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003500 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003502 if (lcs_space || lcs_trail)
3503 extra_check = TRUE;
3504 /* find start of trailing whitespace */
3505 if (lcs_trail)
3506 {
3507 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003508 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003509 --trailcol;
3510 trailcol += (colnr_T) (ptr - line);
3511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513
3514 /*
3515 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3516 * first character to be displayed.
3517 */
3518 if (wp->w_p_wrap)
3519 v = wp->w_skipcol;
3520 else
3521 v = wp->w_leftcol;
3522 if (v > 0)
3523 {
3524#ifdef FEAT_MBYTE
3525 char_u *prev_ptr = ptr;
3526#endif
3527 while (vcol < v && *ptr != NUL)
3528 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003529 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 vcol += c;
3531#ifdef FEAT_MBYTE
3532 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003534 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003537 /* When:
3538 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003539 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003540 * - 'virtualedit' is set, or
3541 * - the visual mode is active,
3542 * the end of the line may be before the start of the displayed part.
3543 */
3544 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003545#ifdef FEAT_SYN_HL
3546 wp->w_p_cuc || draw_color_col ||
3547#endif
3548#ifdef FEAT_VIRTUALEDIT
3549 virtual_active() ||
3550#endif
3551 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 /* Handle a character that's not completely on the screen: Put ptr at
3557 * that character but skip the first few screen characters. */
3558 if (vcol > v)
3559 {
3560 vcol -= c;
3561#ifdef FEAT_MBYTE
3562 ptr = prev_ptr;
3563#else
3564 --ptr;
3565#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003566 /* If the character fits on the screen, don't need to skip it.
3567 * Except for a TAB. */
3568 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003569#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003570 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003571#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003572 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003573 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575
3576 /*
3577 * Adjust for when the inverted text is before the screen,
3578 * and when the start of the inverted text is before the screen.
3579 */
3580 if (tocol <= vcol)
3581 fromcol = 0;
3582 else if (fromcol >= 0 && fromcol < vcol)
3583 fromcol = vcol;
3584
3585#ifdef FEAT_LINEBREAK
3586 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3587 if (wp->w_p_wrap)
3588 need_showbreak = TRUE;
3589#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003590#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003591 /* When spell checking a word we need to figure out the start of the
3592 * word and if it's badly spelled or not. */
3593 if (has_spell)
3594 {
3595 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003596 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003597 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003598
3599 pos = wp->w_cursor;
3600 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003601 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003602 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003603
3604 /* spell_move_to() may call ml_get() and make "line" invalid */
3605 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3606 ptr = line + linecol;
3607
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003608 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003609 {
3610 /* no bad word found at line start, don't check until end of a
3611 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003612 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003613 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003614 }
3615 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003616 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003617 /* bad word found, use attributes until end of word */
3618 word_end = wp->w_cursor.col + len + 1;
3619
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003620 /* Turn index into actual attributes. */
3621 if (spell_hlf != HLF_COUNT)
3622 spell_attr = highlight_attr[spell_hlf];
3623 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003625
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003626# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003627 /* Need to restart syntax highlighting for this line. */
3628 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003629 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003630# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003631 }
3632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
3634
3635 /*
3636 * Correct highlighting for cursor that can't be disabled.
3637 * Avoids having to check this for each character.
3638 */
3639 if (fromcol >= 0)
3640 {
3641 if (noinvcur)
3642 {
3643 if ((colnr_T)fromcol == wp->w_virtcol)
3644 {
3645 /* highlighting starts at cursor, let it start just after the
3646 * cursor */
3647 fromcol_prev = fromcol;
3648 fromcol = -1;
3649 }
3650 else if ((colnr_T)fromcol < wp->w_virtcol)
3651 /* restart highlighting after the cursor */
3652 fromcol_prev = wp->w_virtcol;
3653 }
3654 if (fromcol >= tocol)
3655 fromcol = -1;
3656 }
3657
3658#ifdef FEAT_SEARCH_EXTRA
3659 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003660 * Handle highlighting the last used search pattern and matches.
3661 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003663 cur = wp->w_match_head;
3664 shl_flag = FALSE;
3665 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003667 if (shl_flag == FALSE)
3668 {
3669 shl = &search_hl;
3670 shl_flag = TRUE;
3671 }
3672 else
3673 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003674 shl->startcol = MAXCOL;
3675 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003677 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003678 v = (long)(ptr - line);
3679 if (cur != NULL)
3680 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003681 next_search_hl(wp, shl, lnum, (colnr_T)v,
3682 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003683
3684 /* Need to get the line again, a multi-line regexp may have made it
3685 * invalid. */
3686 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3687 ptr = line + v;
3688
3689 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003691 if (shl->lnum == lnum)
3692 shl->startcol = shl->rm.startpos[0].col;
3693 else
3694 shl->startcol = 0;
3695 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3696 - shl->rm.startpos[0].lnum)
3697 shl->endcol = shl->rm.endpos[0].col;
3698 else
3699 shl->endcol = MAXCOL;
3700 /* Highlight one character for an empty match. */
3701 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003704 if (has_mbyte && line[shl->endcol] != NUL)
3705 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003708 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003710 if ((long)shl->startcol < v) /* match at leftcol */
3711 {
3712 shl->attr_cur = shl->attr;
3713 search_attr = shl->attr;
3714 }
3715 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003717 if (shl != &search_hl && cur != NULL)
3718 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720#endif
3721
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003722#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003723 /* Cursor line highlighting for 'cursorline' in the current window. Not
3724 * when Visual mode is active, because it's not clear what is selected
3725 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003726 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003727 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003728 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003729 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003730 area_highlighting = TRUE;
3731 }
3732#endif
3733
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003734 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 col = 0;
3736#ifdef FEAT_RIGHTLEFT
3737 if (wp->w_p_rl)
3738 {
3739 /* Rightleft window: process the text in the normal direction, but put
3740 * it in current_ScreenLine[] from right to left. Start at the
3741 * rightmost column of the window. */
3742 col = W_WIDTH(wp) - 1;
3743 off += col;
3744 }
3745#endif
3746
3747 /*
3748 * Repeat for the whole displayed line.
3749 */
3750 for (;;)
3751 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003752#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003753 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /* Skip this quickly when working on the text. */
3756 if (draw_state != WL_LINE)
3757 {
3758#ifdef FEAT_CMDWIN
3759 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3760 {
3761 draw_state = WL_CMDLINE;
3762 if (cmdwin_type != 0 && wp == curwin)
3763 {
3764 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003766 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003767 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 }
3770#endif
3771
3772#ifdef FEAT_FOLDING
3773 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3774 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003775 int fdc = compute_foldcolumn(wp, 0);
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003778 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003780 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003781 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003782 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003783 p_extra_free = alloc(12 + 1);
3784
3785 if (p_extra_free != NULL)
3786 {
3787 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3788 n_extra = fdc;
3789 p_extra_free[n_extra] = NUL;
3790 p_extra = p_extra_free;
3791 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003792 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 }
3796#endif
3797
3798#ifdef FEAT_SIGNS
3799 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3800 {
3801 draw_state = WL_SIGN;
3802 /* Show the sign column when there are any signs in this
3803 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003804 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003806 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003808 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809# endif
3810
3811 /* Draw two cells with the sign value or blank. */
3812 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003813 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 n_extra = 2;
3815
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003816 if (row == startrow
3817#ifdef FEAT_DIFF
3818 + filler_lines && filler_todo <= 0
3819#endif
3820 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
3822 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3823 SIGN_TEXT);
3824# ifdef FEAT_SIGN_ICONS
3825 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3826 SIGN_ICON);
3827 if (gui.in_use && icon_sign != 0)
3828 {
3829 /* Use the image in this position. */
3830 c_extra = SIGN_BYTE;
3831# ifdef FEAT_NETBEANS_INTG
3832 if (buf_signcount(wp->w_buffer, lnum) > 1)
3833 c_extra = MULTISIGN_BYTE;
3834# endif
3835 char_attr = icon_sign;
3836 }
3837 else
3838# endif
3839 if (text_sign != 0)
3840 {
3841 p_extra = sign_get_text(text_sign);
3842 if (p_extra != NULL)
3843 {
3844 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 char_attr = sign_get_attr(text_sign, FALSE);
3848 }
3849 }
3850 }
3851 }
3852#endif
3853
3854 if (draw_state == WL_NR - 1 && n_extra == 0)
3855 {
3856 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003857 /* Display the absolute or relative line number. After the
3858 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3859 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 && (row == startrow
3861#ifdef FEAT_DIFF
3862 + filler_lines
3863#endif
3864 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3865 {
3866 /* Draw the line number (empty space after wrapping). */
3867 if (row == startrow
3868#ifdef FEAT_DIFF
3869 + filler_lines
3870#endif
3871 )
3872 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003873 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003874 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003875
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 if (wp->w_p_nu && !wp->w_p_rnu)
3877 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003878 num = (long)lnum;
3879 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003880 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003881 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003882 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003883 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003885 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003886 num = lnum;
3887 fmt = "%-*ld ";
3888 }
3889 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003890
Bram Moolenaar700e7342013-01-30 12:31:36 +01003891 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003892 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (wp->w_skipcol > 0)
3894 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3895 *p_extra = '-';
3896#ifdef FEAT_RIGHTLEFT
3897 if (wp->w_p_rl) /* reverse line numbers */
3898 rl_mirror(extra);
3899#endif
3900 p_extra = extra;
3901 c_extra = NUL;
3902 }
3903 else
3904 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003905 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003906 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003907#ifdef FEAT_SYN_HL
3908 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003909 * the current line differently.
3910 * TODO: Can we use CursorLine instead of CursorLineNr
3911 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003912 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003914 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 }
3918
Bram Moolenaar597a4222014-06-25 14:39:50 +02003919#ifdef FEAT_LINEBREAK
3920 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3921 && n_extra == 0 && *p_sbr != NUL)
3922 /* draw indent after showbreak value */
3923 draw_state = WL_BRI;
3924 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3925 /* After the showbreak, draw the breakindent */
3926 draw_state = WL_BRI - 1;
3927
3928 /* draw 'breakindent': indent wrapped text accordingly */
3929 if (draw_state == WL_BRI - 1 && n_extra == 0)
3930 {
3931 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003932 /* if need_showbreak is set, breakindent also applies */
3933 if (wp->w_p_bri && n_extra == 0
3934 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003935# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003936 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003937# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003938 )
3939 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003940 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003941# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003942 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003943 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003944 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003945# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003946 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3947 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003948 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003949# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003950 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003951# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003952 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003953 c_extra = ' ';
3954 n_extra = get_breakindent_win(wp,
3955 ml_get_buf(wp->w_buffer, lnum, FALSE));
3956 /* Correct end of highlighted area for 'breakindent',
3957 * required when 'linebreak' is also set. */
3958 if (tocol == vcol)
3959 tocol += n_extra;
3960 }
3961 }
3962#endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3965 if (draw_state == WL_SBR - 1 && n_extra == 0)
3966 {
3967 draw_state = WL_SBR;
3968# ifdef FEAT_DIFF
3969 if (filler_todo > 0)
3970 {
3971 /* Draw "deleted" diff line(s). */
3972 if (char2cells(fill_diff) > 1)
3973 c_extra = '-';
3974 else
3975 c_extra = fill_diff;
3976# ifdef FEAT_RIGHTLEFT
3977 if (wp->w_p_rl)
3978 n_extra = col + 1;
3979 else
3980# endif
3981 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003982 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984# endif
3985# ifdef FEAT_LINEBREAK
3986 if (*p_sbr != NUL && need_showbreak)
3987 {
3988 /* Draw 'showbreak' at the start of each broken line. */
3989 p_extra = p_sbr;
3990 c_extra = NUL;
3991 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003992 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003994 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 /* Correct end of highlighted area for 'showbreak',
3996 * required when 'linebreak' is also set. */
3997 if (tocol == vcol)
3998 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003999#ifdef FEAT_SYN_HL
4000 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004001 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004002 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004003 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006# endif
4007 }
4008#endif
4009
4010 if (draw_state == WL_LINE - 1 && n_extra == 0)
4011 {
4012 draw_state = WL_LINE;
4013 if (saved_n_extra)
4014 {
4015 /* Continue item from end of wrapped line. */
4016 n_extra = saved_n_extra;
4017 c_extra = saved_c_extra;
4018 p_extra = saved_p_extra;
4019 char_attr = saved_char_attr;
4020 }
4021 else
4022 char_attr = 0;
4023 }
4024 }
4025
4026 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004027 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#ifdef FEAT_DIFF
4030 && filler_todo <= 0
4031#endif
4032 )
4033 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004034 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004035 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 /* Pretend we have finished updating the window. Except when
4037 * 'cursorcolumn' is set. */
4038#ifdef FEAT_SYN_HL
4039 if (wp->w_p_cuc)
4040 row = wp->w_cline_row + wp->w_cline_height;
4041 else
4042#endif
4043 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 break;
4045 }
4046
4047 if (draw_state == WL_LINE && area_highlighting)
4048 {
4049 /* handle Visual or match highlighting in this line */
4050 if (vcol == fromcol
4051#ifdef FEAT_MBYTE
4052 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4053 && (*mb_ptr2cells)(ptr) > 1)
4054#endif
4055 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004056 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 && vcol < tocol))
4058 area_attr = attr; /* start highlighting */
4059 else if (area_attr != 0
4060 && (vcol == tocol
4061 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#ifdef FEAT_SEARCH_EXTRA
4065 if (!n_extra)
4066 {
4067 /*
4068 * Check for start/end of search pattern match.
4069 * After end, check for start/end of next match.
4070 * When another match, have to check for start again.
4071 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004072 * Do this for 'search_hl' and the match list (ordered by
4073 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004075 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004076 cur = wp->w_match_head;
4077 shl_flag = FALSE;
4078 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004080 if (shl_flag == FALSE
4081 && ((cur != NULL
4082 && cur->priority > SEARCH_HL_PRIORITY)
4083 || cur == NULL))
4084 {
4085 shl = &search_hl;
4086 shl_flag = TRUE;
4087 }
4088 else
4089 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004090 if (cur != NULL)
4091 cur->pos.cur = 0;
4092 pos_inprogress = TRUE;
4093 while (shl->rm.regprog != NULL
4094 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004096 if (shl->startcol != MAXCOL
4097 && v >= (long)shl->startcol
4098 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004100#ifdef FEAT_MBYTE
4101 int tmp_col = v + MB_PTR2LEN(ptr);
4102
4103 if (shl->endcol < tmp_col)
4104 shl->endcol = tmp_col;
4105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004107#ifdef FEAT_CONCEAL
4108 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4109 == cur->hlg_id)
4110 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004111 has_match_conc =
4112 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004113 match_conc = cur->conceal_char;
4114 }
4115 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004116 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004119 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004122 next_search_hl(wp, shl, lnum, (colnr_T)v,
4123 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004124 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004125 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
4127 /* Need to get the line again, a multi-line regexp
4128 * may have made it invalid. */
4129 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4130 ptr = line + v;
4131
4132 if (shl->lnum == lnum)
4133 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 /* highlight empty match, try again after
4143 * it */
4144#ifdef FEAT_MBYTE
4145 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004146 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004147 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 else
4149#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004150 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /* Loop to check if the match starts at the
4154 * current position */
4155 continue;
4156 }
4157 }
4158 break;
4159 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004160 if (shl != &search_hl && cur != NULL)
4161 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004163
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004164 /* Use attributes from match with highest priority among
4165 * 'search_hl' and the match list. */
4166 search_attr = search_hl.attr_cur;
4167 cur = wp->w_match_head;
4168 shl_flag = FALSE;
4169 while (cur != NULL || shl_flag == FALSE)
4170 {
4171 if (shl_flag == FALSE
4172 && ((cur != NULL
4173 && cur->priority > SEARCH_HL_PRIORITY)
4174 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004175 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004176 shl = &search_hl;
4177 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004178 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004179 else
4180 shl = &cur->hl;
4181 if (shl->attr_cur != 0)
4182 search_attr = shl->attr_cur;
4183 if (shl != &search_hl && cur != NULL)
4184 cur = cur->next;
4185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187#endif
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004190 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004192 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4193 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004195 if (diff_hlf == HLF_TXD && ptr - line > change_end
4196 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004198 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004199 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004200 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004203
4204 /* Decide which of the highlight attributes to use. */
4205 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004207 if (area_attr != 0)
4208 char_attr = hl_combine_attr(line_attr, area_attr);
4209 else if (search_attr != 0)
4210 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 /* Use line_attr when not in the Visual or 'incsearch' area
4212 * (area_attr may be 0 when "noinvcur" is set). */
4213 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004214 || vcol < fromcol || vcol_prev < fromcol_prev
4215 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004216 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004217#else
4218 if (area_attr != 0)
4219 char_attr = area_attr;
4220 else if (search_attr != 0)
4221 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004222#endif
4223 else
4224 {
4225 attr_pri = FALSE;
4226#ifdef FEAT_SYN_HL
4227 if (has_syntax)
4228 char_attr = syntax_attr;
4229 else
4230#endif
4231 char_attr = 0;
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234
4235 /*
4236 * Get the next character to put on the screen.
4237 */
4238 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004239 * The "p_extra" points to the extra stuff that is inserted to
4240 * represent special characters (non-printable stuff) and other
4241 * things. When all characters are the same, c_extra is used.
4242 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4243 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4245 */
4246 if (n_extra > 0)
4247 {
4248 if (c_extra != NUL)
4249 {
4250 c = c_extra;
4251#ifdef FEAT_MBYTE
4252 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004253 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004257 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 else
4260 mb_utf8 = FALSE;
4261#endif
4262 }
4263 else
4264 {
4265 c = *p_extra;
4266#ifdef FEAT_MBYTE
4267 if (has_mbyte)
4268 {
4269 mb_c = c;
4270 if (enc_utf8)
4271 {
4272 /* If the UTF-8 character is more than one byte:
4273 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004274 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mb_utf8 = FALSE;
4276 if (mb_l > n_extra)
4277 mb_l = 1;
4278 else if (mb_l > 1)
4279 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 }
4285 else
4286 {
4287 /* if this is a DBCS character, put it in "mb_c" */
4288 mb_l = MB_BYTE2LEN(c);
4289 if (mb_l >= n_extra)
4290 mb_l = 1;
4291 else if (mb_l > 1)
4292 mb_c = (c << 8) + p_extra[1];
4293 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004294 if (mb_l == 0) /* at the NUL at end-of-line */
4295 mb_l = 1;
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 /* If a double-width char doesn't fit display a '>' in the
4298 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004299 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300# ifdef FEAT_RIGHTLEFT
4301 wp->w_p_rl ? (col <= 0) :
4302# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004303 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 && (*mb_char2cells)(mb_c) == 2)
4305 {
4306 c = '>';
4307 mb_c = c;
4308 mb_l = 1;
4309 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004310 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 /* put the pointer back to output the double-width
4312 * character at the start of the next line. */
4313 ++n_extra;
4314 --p_extra;
4315 }
4316 else
4317 {
4318 n_extra -= mb_l - 1;
4319 p_extra += mb_l - 1;
4320 }
4321 }
4322#endif
4323 ++p_extra;
4324 }
4325 --n_extra;
4326 }
4327 else
4328 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004329#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004330 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004331#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004332
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004333 if (p_extra_free != NULL)
4334 {
4335 vim_free(p_extra_free);
4336 p_extra_free = NULL;
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 /*
4339 * Get a character from the line itself.
4340 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004341 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004342#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004343 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#ifdef FEAT_MBYTE
4346 if (has_mbyte)
4347 {
4348 mb_c = c;
4349 if (enc_utf8)
4350 {
4351 /* If the UTF-8 character is more than one byte: Decode it
4352 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004353 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 mb_utf8 = FALSE;
4355 if (mb_l > 1)
4356 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004357 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 /* Overlong encoded ASCII or ASCII with composing char
4359 * is displayed normally, except a NUL. */
4360 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004361 {
4362 c = mb_c;
4363# ifdef FEAT_LINEBREAK
4364 c0 = mb_c;
4365# endif
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004368
4369 /* At start of the line we can have a composing char.
4370 * Draw it as a space with a composing char. */
4371 if (utf_iscomposing(mb_c))
4372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004373 int i;
4374
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004375 for (i = Screen_mco - 1; i > 0; --i)
4376 u8cc[i] = u8cc[i - 1];
4377 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004378 mb_c = ' ';
4379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381
4382 if ((mb_l == 1 && c >= 0x80)
4383 || (mb_l >= 1 && mb_c == 0)
4384 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004385# ifdef UNICODE16
4386 || mb_c >= 0x10000
4387# endif
4388 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 /*
4391 * Illegal UTF-8 byte: display as <xx>.
4392 * Non-BMP character : display as ? or fullwidth ?.
4393 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004394# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
4398 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004399# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (wp->w_p_rl) /* reverse */
4401 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004404# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 else if (utf_char2cells(mb_c) != 2)
4406 STRCPY(extra, "?");
4407 else
4408 /* 0xff1f in UTF-8: full-width '?' */
4409 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 p_extra = extra;
4413 c = *p_extra;
4414 mb_c = mb_ptr2char_adv(&p_extra);
4415 mb_utf8 = (c >= 0x80);
4416 n_extra = (int)STRLEN(p_extra);
4417 c_extra = NUL;
4418 if (area_attr == 0 && search_attr == 0)
4419 {
4420 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004421 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 saved_attr2 = char_attr; /* save current attr */
4423 }
4424 }
4425 else if (mb_l == 0) /* at the NUL at end-of-line */
4426 mb_l = 1;
4427#ifdef FEAT_ARABIC
4428 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4429 {
4430 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004431 int pc, pc1, nc;
4432 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 /* The idea of what is the previous and next
4435 * character depends on 'rightleft'. */
4436 if (wp->w_p_rl)
4437 {
4438 pc = prev_c;
4439 pc1 = prev_c1;
4440 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004441 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 else
4444 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 prev_c = mb_c;
4450
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004451 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 else
4454 prev_c = mb_c;
4455#endif
4456 }
4457 else /* enc_dbcs */
4458 {
4459 mb_l = MB_BYTE2LEN(c);
4460 if (mb_l == 0) /* at the NUL at end-of-line */
4461 mb_l = 1;
4462 else if (mb_l > 1)
4463 {
4464 /* We assume a second byte below 32 is illegal.
4465 * Hopefully this is OK for all double-byte encodings!
4466 */
4467 if (ptr[1] >= 32)
4468 mb_c = (c << 8) + ptr[1];
4469 else
4470 {
4471 if (ptr[1] == NUL)
4472 {
4473 /* head byte at end of line */
4474 mb_l = 1;
4475 transchar_nonprint(extra, c);
4476 }
4477 else
4478 {
4479 /* illegal tail byte */
4480 mb_l = 2;
4481 STRCPY(extra, "XX");
4482 }
4483 p_extra = extra;
4484 n_extra = (int)STRLEN(extra) - 1;
4485 c_extra = NUL;
4486 c = *p_extra++;
4487 if (area_attr == 0 && search_attr == 0)
4488 {
4489 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 saved_attr2 = char_attr; /* save current attr */
4492 }
4493 mb_c = c;
4494 }
4495 }
4496 }
4497 /* If a double-width char doesn't fit display a '>' in the
4498 * last column; the character is displayed at the start of the
4499 * next line. */
4500 if ((
4501# ifdef FEAT_RIGHTLEFT
4502 wp->w_p_rl ? (col <= 0) :
4503# endif
4504 (col >= W_WIDTH(wp) - 1))
4505 && (*mb_char2cells)(mb_c) == 2)
4506 {
4507 c = '>';
4508 mb_c = c;
4509 mb_utf8 = FALSE;
4510 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /* Put pointer back so that the character will be
4513 * displayed at the start of the next line. */
4514 --ptr;
4515 }
4516 else if (*ptr != NUL)
4517 ptr += mb_l - 1;
4518
4519 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004520 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004521 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004522 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004525 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 c = ' ';
4527 if (area_attr == 0 && search_attr == 0)
4528 {
4529 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004530 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 saved_attr2 = char_attr; /* save current attr */
4532 }
4533 mb_c = c;
4534 mb_utf8 = FALSE;
4535 mb_l = 1;
4536 }
4537
4538 }
4539#endif
4540 ++ptr;
4541
4542 if (extra_check)
4543 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004544#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004545 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004546#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004547
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004548#ifdef FEAT_TERMINAL
4549 if (get_term_attr)
4550 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004551 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004552
4553 if (!attr_pri)
4554 char_attr = syntax_attr;
4555 else
4556 char_attr = hl_combine_attr(syntax_attr, char_attr);
4557 }
4558#endif
4559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004560#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /* Get syntax attribute, unless still at the start of the line
4562 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004563 v = (long)(ptr - line);
4564 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 /* Get the syntax attribute for the character. If there
4567 * is an error, disable syntax highlighting. */
4568 save_did_emsg = did_emsg;
4569 did_emsg = FALSE;
4570
Bram Moolenaar217ad922005-03-20 22:37:15 +00004571 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004572# ifdef FEAT_SPELL
4573 has_spell ? &can_spell :
4574# endif
4575 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004578 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004579 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004580 has_syntax = FALSE;
4581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else
4583 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004584#ifdef SYN_TIME_LIMIT
4585 if (wp->w_s->b_syn_slow)
4586 has_syntax = FALSE;
4587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 /* Need to get the line again, a multi-line regexp may
4590 * have made it invalid. */
4591 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4592 ptr = line + v;
4593
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004594 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004596 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004597 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004598# ifdef FEAT_CONCEAL
4599 /* no concealing past the end of the line, it interferes
4600 * with line highlighting */
4601 if (c == NUL)
4602 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004603 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004604 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004607#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004609#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004610 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004611 * Only do this when there is no syntax highlighting, the
4612 * @Spell cluster is not used or the current syntax item
4613 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004614 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004615 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004616 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004617# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004618 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004619 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004620# endif
4621 if (c != 0 && (
4622# ifdef FEAT_SYN_HL
4623 !has_syntax ||
4624# endif
4625 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004626 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004627 char_u *prev_ptr, *p;
4628 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004629 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004631 if (has_mbyte)
4632 {
4633 prev_ptr = ptr - mb_l;
4634 v -= mb_l - 1;
4635 }
4636 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004637# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004638 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004639
4640 /* Use nextline[] if possible, it has the start of the
4641 * next line concatenated. */
4642 if ((prev_ptr - line) - nextlinecol >= 0)
4643 p = nextline + (prev_ptr - line) - nextlinecol;
4644 else
4645 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004646 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004647 len = spell_check(wp, p, &spell_hlf, &cap_col,
4648 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004649 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004651 /* In Insert mode only highlight a word that
4652 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004653 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 && (State & INSERT) != 0
4655 && wp->w_cursor.lnum == lnum
4656 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004657 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004658 && wp->w_cursor.col < (colnr_T)word_end)
4659 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004660 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004661 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004662 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004663
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004664 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665 && (p - nextline) + len > nextline_idx)
4666 {
4667 /* Remember that the good word continues at the
4668 * start of the next line. */
4669 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004670 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004671 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004672
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004673 /* Turn index into actual attributes. */
4674 if (spell_hlf != HLF_COUNT)
4675 spell_attr = highlight_attr[spell_hlf];
4676
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004677 if (cap_col > 0)
4678 {
4679 if (p != prev_ptr
4680 && (p - nextline) + cap_col >= nextline_idx)
4681 {
4682 /* Remember that the word in the next line
4683 * must start with a capital. */
4684 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004685 cap_col = (int)((p - nextline) + cap_col
4686 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004687 }
4688 else
4689 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004690 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004691 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004692 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 }
4694 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004695 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004696 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697 char_attr = hl_combine_attr(char_attr, spell_attr);
4698 else
4699 char_attr = hl_combine_attr(spell_attr, char_attr);
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#endif
4702#ifdef FEAT_LINEBREAK
4703 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004704 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004706 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004707 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004709# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004710 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004711# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004712 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004714 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004716 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004717
Bram Moolenaar597a4222014-06-25 14:39:50 +02004718 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004719 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004720 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004721 if (c == TAB && n_extra + col > W_WIDTH(wp))
4722 n_extra = (int)wp->w_buffer->b_p_ts
4723 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4724
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004725# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004726 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004729# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004730 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004731 {
4732#ifdef FEAT_CONCEAL
4733 if (c == TAB)
4734 /* See "Tab alignment" below. */
4735 FIX_FOR_BOGUSCOLS;
4736#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004737 if (!wp->w_p_list)
4738 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741#endif
4742
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004743 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4744 */
4745 if (wp->w_p_list
4746 && (((c == 160
4747#ifdef FEAT_MBYTE
4748 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4749#endif
4750 ) && lcs_nbsp)
4751 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4752 {
4753 c = (c == ' ') ? lcs_space : lcs_nbsp;
4754 if (area_attr == 0 && search_attr == 0)
4755 {
4756 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004757 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004758 saved_attr2 = char_attr; /* save current attr */
4759 }
4760#ifdef FEAT_MBYTE
4761 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004762 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004763 {
4764 mb_utf8 = TRUE;
4765 u8cc[0] = 0;
4766 c = 0xc0;
4767 }
4768 else
4769 mb_utf8 = FALSE;
4770#endif
4771 }
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4774 {
4775 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004776 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004779 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 saved_attr2 = char_attr; /* save current attr */
4781 }
4782#ifdef FEAT_MBYTE
4783 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004784 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004788 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 else
4791 mb_utf8 = FALSE;
4792#endif
4793 }
4794 }
4795
4796 /*
4797 * Handling of non-printable characters.
4798 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004799 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 /*
4802 * when getting a character from the file, we may have to
4803 * turn it into something else on the way to putting it
4804 * into "ScreenLines".
4805 */
4806 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4807 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004808 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004809 long vcol_adjusted = vcol; /* removed showbreak length */
4810#ifdef FEAT_LINEBREAK
4811 /* only adjust the tab_len, when at the first column
4812 * after the showbreak value was drawn */
4813 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4814 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004817 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004818 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4819
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004820#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004821 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004822#endif
4823 /* tab amount depends on current column */
4824 n_extra = tab_len;
4825#ifdef FEAT_LINEBREAK
4826 else
4827 {
4828 char_u *p;
4829 int len = n_extra;
4830 int i;
4831 int saved_nextra = n_extra;
4832
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004833#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004834 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004835 /* there are characters to conceal */
4836 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004837 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4838 */
4839 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4840 && n_extra > tab_len)
4841 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004842#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004843
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004844 /* if n_extra > 0, it gives the number of chars, to
4845 * use for a tab, else we need to calculate the width
4846 * for a tab */
4847#ifdef FEAT_MBYTE
4848 len = (tab_len * mb_char2len(lcs_tab2));
4849 if (n_extra > 0)
4850 len += n_extra - tab_len;
4851#endif
4852 c = lcs_tab1;
4853 p = alloc((unsigned)(len + 1));
4854 vim_memset(p, ' ', len);
4855 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004856 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004857 p_extra_free = p;
4858 for (i = 0; i < tab_len; i++)
4859 {
4860#ifdef FEAT_MBYTE
4861 mb_char2bytes(lcs_tab2, p);
4862 p += mb_char2len(lcs_tab2);
4863 n_extra += mb_char2len(lcs_tab2)
4864 - (saved_nextra > 0 ? 1 : 0);
4865#else
4866 p[i] = lcs_tab2;
4867#endif
4868 }
4869 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004870#ifdef FEAT_CONCEAL
4871 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4872 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004873 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004874 n_extra -= vcol_off;
4875#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004876 }
4877#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004878#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004879 {
4880 int vc_saved = vcol_off;
4881
4882 /* Tab alignment should be identical regardless of
4883 * 'conceallevel' value. So tab compensates of all
4884 * previous concealed characters, and thus resets
4885 * vcol_off and boguscols accumulated so far in the
4886 * line. Note that the tab can be longer than
4887 * 'tabstop' when there are concealed characters. */
4888 FIX_FOR_BOGUSCOLS;
4889
4890 /* Make sure, the highlighting for the tab char will be
4891 * correctly set further below (effectively reverts the
4892 * FIX_FOR_BOGSUCOLS macro */
4893 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004894 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004895 tab_len += vc_saved;
4896 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#ifdef FEAT_MBYTE
4899 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4900#endif
4901 if (wp->w_p_list)
4902 {
4903 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004904#ifdef FEAT_LINEBREAK
4905 if (wp->w_p_lbr)
4906 c_extra = NUL; /* using p_extra from above */
4907 else
4908#endif
4909 c_extra = lcs_tab2;
4910 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004911 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 saved_attr2 = char_attr; /* save current attr */
4913#ifdef FEAT_MBYTE
4914 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004915 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004918 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004919 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921#endif
4922 }
4923 else
4924 {
4925 c_extra = ' ';
4926 c = ' ';
4927 }
4928 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004929 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004930 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004931 || ((fromcol >= 0 || fromcol_prev >= 0)
4932 && tocol > vcol
4933 && VIsual_mode != Ctrl_V
4934 && (
4935# ifdef FEAT_RIGHTLEFT
4936 wp->w_p_rl ? (col >= 0) :
4937# endif
4938 (col < W_WIDTH(wp)))
4939 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004940 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004941 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004942 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004944 /* Display a '$' after the line or highlight an extra
4945 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4947 /* For a diff line the highlighting continues after the
4948 * "$". */
4949 if (
4950# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004951 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952# ifdef LINE_ATTR
4953 &&
4954# endif
4955# endif
4956# ifdef LINE_ATTR
4957 line_attr == 0
4958# endif
4959 )
4960#endif
4961 {
4962#ifdef FEAT_VIRTUALEDIT
4963 /* In virtualedit, visual selections may extend
4964 * beyond end of line. */
4965 if (area_highlighting && virtual_active()
4966 && tocol != MAXCOL && vcol < tocol)
4967 n_extra = 0;
4968 else
4969#endif
4970 {
4971 p_extra = at_end_str;
4972 n_extra = 1;
4973 c_extra = NUL;
4974 }
4975 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004976 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004977 c = lcs_eol;
4978 else
4979 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 lcs_eol_one = -1;
4981 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004982 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004984 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 n_attr = 1;
4986 }
4987#ifdef FEAT_MBYTE
4988 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004989 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004992 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004993 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else
4996 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4997#endif
4998 }
4999 else if (c != NUL)
5000 {
5001 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005002 if (n_extra == 0)
5003 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004#ifdef FEAT_RIGHTLEFT
5005 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5006 rl_mirror(p_extra); /* reverse "<12>" */
5007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005009#ifdef FEAT_LINEBREAK
5010 if (wp->w_p_lbr)
5011 {
5012 char_u *p;
5013
5014 c = *p_extra;
5015 p = alloc((unsigned)n_extra + 1);
5016 vim_memset(p, ' ', n_extra);
5017 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5018 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005019 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005020 p_extra_free = p_extra = p;
5021 }
5022 else
5023#endif
5024 {
5025 n_extra = byte2cells(c) - 1;
5026 c = *p_extra++;
5027 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005028 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005031 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 saved_attr2 = char_attr; /* save current attr */
5033 }
5034#ifdef FEAT_MBYTE
5035 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5036#endif
5037 }
5038#ifdef FEAT_VIRTUALEDIT
5039 else if (VIsual_active
5040 && (VIsual_mode == Ctrl_V
5041 || VIsual_mode == 'v')
5042 && virtual_active()
5043 && tocol != MAXCOL
5044 && vcol < tocol
5045 && (
5046# ifdef FEAT_RIGHTLEFT
5047 wp->w_p_rl ? (col >= 0) :
5048# endif
5049 (col < W_WIDTH(wp))))
5050 {
5051 c = ' ';
5052 --ptr; /* put it back at the NUL */
5053 }
5054#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005055#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else if ((
5057# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005058 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 ) && (
5062# ifdef FEAT_RIGHTLEFT
5063 wp->w_p_rl ? (col >= 0) :
5064# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005065 (col
5066# ifdef FEAT_CONCEAL
5067 - boguscols
5068# endif
5069 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 /* Highlight until the right side of the window */
5072 c = ' ';
5073 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005074
5075 /* Remember we do the char for line highlighting. */
5076 ++did_line_attr;
5077
5078 /* don't do search HL for the rest of the line */
5079 if (line_attr != 0 && char_attr == search_attr && col > 0)
5080 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081# ifdef FEAT_DIFF
5082 if (diff_hlf == HLF_TXD)
5083 {
5084 diff_hlf = HLF_CHD;
5085 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005086 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005087 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005088 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5089 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005090 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093# endif
5094 }
5095#endif
5096 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005097
5098#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005099 if ( wp->w_p_cole > 0
5100 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005101 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005102 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005103 && !(lnum_in_visual_area
5104 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005105 {
5106 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005107 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005108 && (syn_get_sub_char() != NUL || match_conc
5109 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005110 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005112 /* First time at this concealed item: display one
5113 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005114 if (match_conc)
5115 c = match_conc;
5116 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117 c = syn_get_sub_char();
5118 else if (lcs_conceal != NUL)
5119 c = lcs_conceal;
5120 else
5121 c = ' ';
5122
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005123 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005125 if (n_extra > 0)
5126 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005127 vcol += n_extra;
5128 if (wp->w_p_wrap && n_extra > 0)
5129 {
5130# ifdef FEAT_RIGHTLEFT
5131 if (wp->w_p_rl)
5132 {
5133 col -= n_extra;
5134 boguscols -= n_extra;
5135 }
5136 else
5137# endif
5138 {
5139 boguscols += n_extra;
5140 col += n_extra;
5141 }
5142 }
5143 n_extra = 0;
5144 n_attr = 0;
5145 }
5146 else if (n_skip == 0)
5147 {
5148 is_concealing = TRUE;
5149 n_skip = 1;
5150 }
5151# ifdef FEAT_MBYTE
5152 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005153 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005154 {
5155 mb_utf8 = TRUE;
5156 u8cc[0] = 0;
5157 c = 0xc0;
5158 }
5159 else
5160 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5161# endif
5162 }
5163 else
5164 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005165 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005166 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005167 }
5168#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005171#ifdef FEAT_CONCEAL
5172 /* In the cursor line and we may be concealing characters: correct
5173 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005174 if (!did_wcol && draw_state == WL_LINE
5175 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005176 && conceal_cursor_line(wp)
5177 && (int)wp->w_virtcol <= vcol + n_skip)
5178 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005179# ifdef FEAT_RIGHTLEFT
5180 if (wp->w_p_rl)
5181 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5182 else
5183# endif
5184 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005185 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 did_wcol = TRUE;
5187 }
5188#endif
5189
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /* Don't override visual selection highlighting. */
5191 if (n_attr > 0
5192 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_attr = extra_attr;
5195
Bram Moolenaar81695252004-12-29 20:58:21 +00005196#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 /* XIM don't send preedit_start and preedit_end, but they send
5198 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5199 * im_is_preediting() here. */
5200 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005201 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 && (State & INSERT)
5203 && !p_imdisable
5204 && im_is_preediting()
5205 && draw_state == WL_LINE)
5206 {
5207 colnr_T tcol;
5208
5209 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005210 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 else
5212 tcol = preedit_end_col;
5213 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5214 {
5215 if (feedback_old_attr < 0)
5216 {
5217 feedback_col = 0;
5218 feedback_old_attr = char_attr;
5219 }
5220 char_attr = im_get_feedback_attr(feedback_col);
5221 if (char_attr < 0)
5222 char_attr = feedback_old_attr;
5223 feedback_col++;
5224 }
5225 else if (feedback_old_attr >= 0)
5226 {
5227 char_attr = feedback_old_attr;
5228 feedback_old_attr = -1;
5229 feedback_col = 0;
5230 }
5231 }
5232#endif
5233 /*
5234 * Handle the case where we are in column 0 but not on the first
5235 * character of the line and the user wants us to show us a
5236 * special character (via 'listchars' option "precedes:<char>".
5237 */
5238 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005239 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5241#ifdef FEAT_DIFF
5242 && filler_todo <= 0
5243#endif
5244 && draw_state > WL_NR
5245 && c != NUL)
5246 {
5247 c = lcs_prec;
5248 lcs_prec_todo = NUL;
5249#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005250 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5251 {
5252 /* Double-width character being overwritten by the "precedes"
5253 * character, need to fill up half the character. */
5254 c_extra = MB_FILLER_CHAR;
5255 n_extra = 1;
5256 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005257 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005260 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005263 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005264 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 else
5267 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5268#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005269 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005272 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 n_attr3 = 1;
5274 }
5275 }
5276
5277 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005278 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005280 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005281#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005282 || did_line_attr == 1
5283#endif
5284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005286#ifdef FEAT_SEARCH_EXTRA
5287 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005288
5289 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005290 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005291 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005292#endif
5293
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005294 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 * highlight match at end of line. If it's beyond the last
5296 * char on the screen, just overwrite that one (tricky!) Not
5297 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005298#ifdef FEAT_SEARCH_EXTRA
5299 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005300 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005301 prevcol_hl_flag = TRUE;
5302 else
5303 {
5304 cur = wp->w_match_head;
5305 while (cur != NULL)
5306 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005307 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005308 {
5309 prevcol_hl_flag = TRUE;
5310 break;
5311 }
5312 cur = cur->next;
5313 }
5314 }
5315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005317 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005318 && (VIsual_mode != Ctrl_V
5319 || lnum == VIsual.lnum
5320 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005321 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#ifdef FEAT_SEARCH_EXTRA
5323 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005324 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005325# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005326 && did_line_attr <= 1
5327# endif
5328 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329#endif
5330 ))
5331 {
5332 int n = 0;
5333
5334#ifdef FEAT_RIGHTLEFT
5335 if (wp->w_p_rl)
5336 {
5337 if (col < 0)
5338 n = 1;
5339 }
5340 else
5341#endif
5342 {
5343 if (col >= W_WIDTH(wp))
5344 n = -1;
5345 }
5346 if (n != 0)
5347 {
5348 /* At the window boundary, highlight the last character
5349 * instead (better than nothing). */
5350 off += n;
5351 col += n;
5352 }
5353 else
5354 {
5355 /* Add a blank character to highlight. */
5356 ScreenLines[off] = ' ';
5357#ifdef FEAT_MBYTE
5358 if (enc_utf8)
5359 ScreenLinesUC[off] = 0;
5360#endif
5361 }
5362#ifdef FEAT_SEARCH_EXTRA
5363 if (area_attr == 0)
5364 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005365 /* Use attributes from match with highest priority among
5366 * 'search_hl' and the match list. */
5367 char_attr = search_hl.attr;
5368 cur = wp->w_match_head;
5369 shl_flag = FALSE;
5370 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005371 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005372 if (shl_flag == FALSE
5373 && ((cur != NULL
5374 && cur->priority > SEARCH_HL_PRIORITY)
5375 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005376 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005377 shl = &search_hl;
5378 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005379 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005380 else
5381 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005382 if ((ptr - line) - 1 == (long)shl->startcol
5383 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005384 char_attr = shl->attr;
5385 if (shl != &search_hl && cur != NULL)
5386 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389#endif
5390 ScreenAttrs[off] = char_attr;
5391#ifdef FEAT_RIGHTLEFT
5392 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005395 --off;
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 else
5398#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005401 ++off;
5402 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005403 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005404#ifdef FEAT_SYN_HL
5405 eol_hl_off = 1;
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar91170f82006-05-05 21:15:17 +00005410 /*
5411 * At end of the text line.
5412 */
5413 if (c == NUL)
5414 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005415#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005416 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5417 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005418 {
5419 /* highlight last char after line */
5420 --col;
5421 --off;
5422 --vcol;
5423 }
5424
Bram Moolenaar1a384422010-07-14 19:53:30 +02005425 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005426 if (wp->w_p_wrap)
5427 v = wp->w_skipcol;
5428 else
5429 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005430
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005431 /* check if line ends before left margin */
5432 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005433 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005434#ifdef FEAT_CONCEAL
5435 /* Get rid of the boguscols now, we want to draw until the right
5436 * edge for 'cursorcolumn'. */
5437 col -= boguscols;
5438 boguscols = 0;
5439#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005440
5441 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005442 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005443
5444 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005445 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5446 && (int)wp->w_virtcol <
5447 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005448 && lnum != wp->w_cursor.lnum)
5449 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005450# ifdef FEAT_RIGHTLEFT
5451 && !wp->w_p_rl
5452# endif
5453 )
5454 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005455 int rightmost_vcol = 0;
5456 int i;
5457
5458 if (wp->w_p_cuc)
5459 rightmost_vcol = wp->w_virtcol;
5460 if (draw_color_col)
5461 /* determine rightmost colorcolumn to possibly draw */
5462 for (i = 0; color_cols[i] >= 0; ++i)
5463 if (rightmost_vcol < color_cols[i])
5464 rightmost_vcol = color_cols[i];
5465
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005466 while (col < W_WIDTH(wp))
5467 {
5468 ScreenLines[off] = ' ';
5469#ifdef FEAT_MBYTE
5470 if (enc_utf8)
5471 ScreenLinesUC[off] = 0;
5472#endif
5473 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005474 if (draw_color_col)
5475 draw_color_col = advance_color_col(VCOL_HLC,
5476 &color_cols);
5477
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005478 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005479 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005480 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005481 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005482 else
5483 ScreenAttrs[off++] = 0;
5484
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005485 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005486 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005487
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005488 ++vcol;
5489 }
5490 }
5491#endif
5492
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005493 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005494 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 row++;
5496
5497 /*
5498 * Update w_cline_height and w_cline_folded if the cursor line was
5499 * updated (saves a call to plines() later).
5500 */
5501 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5502 {
5503 curwin->w_cline_row = startrow;
5504 curwin->w_cline_height = row - startrow;
5505#ifdef FEAT_FOLDING
5506 curwin->w_cline_folded = FALSE;
5507#endif
5508 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5509 }
5510
5511 break;
5512 }
5513
5514 /* line continues beyond line end */
5515 if (lcs_ext
5516 && !wp->w_p_wrap
5517#ifdef FEAT_DIFF
5518 && filler_todo <= 0
5519#endif
5520 && (
5521#ifdef FEAT_RIGHTLEFT
5522 wp->w_p_rl ? col == 0 :
5523#endif
5524 col == W_WIDTH(wp) - 1)
5525 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005526 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5528 {
5529 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005530 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531#ifdef FEAT_MBYTE
5532 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005533 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
5535 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005536 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005537 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 }
5539 else
5540 mb_utf8 = FALSE;
5541#endif
5542 }
5543
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005544#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005545 /* advance to the next 'colorcolumn' */
5546 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005547 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005548
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005549 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005550 * highlight the cursor position itself.
5551 * Also highlight the 'colorcolumn' if it is different than
5552 * 'cursorcolumn' */
5553 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005554 if (draw_state == WL_LINE && !lnum_in_visual_area
5555 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005557 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005558 && lnum != wp->w_cursor.lnum)
5559 {
5560 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005561 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005563 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005564 {
5565 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005566 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005567 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005568 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005569#endif
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 /*
5572 * Store character to be displayed.
5573 * Skip characters that are left of the screen for 'nowrap'.
5574 */
5575 vcol_prev = vcol;
5576 if (draw_state < WL_LINE || n_skip <= 0)
5577 {
5578 /*
5579 * Store the character.
5580 */
5581#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5582 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5583 {
5584 /* A double-wide character is: put first halve in left cell. */
5585 --off;
5586 --col;
5587 }
5588#endif
5589 ScreenLines[off] = c;
5590#ifdef FEAT_MBYTE
5591 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005592 {
5593 if ((mb_c & 0xff00) == 0x8e00)
5594 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 else if (enc_utf8)
5598 {
5599 if (mb_utf8)
5600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 int i;
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005604 if ((c & 0xff) == 0)
5605 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005606 for (i = 0; i < Screen_mco; ++i)
5607 {
5608 ScreenLinesC[i][off] = u8cc[i];
5609 if (u8cc[i] == 0)
5610 break;
5611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613 else
5614 ScreenLinesUC[off] = 0;
5615 }
5616 if (multi_attr)
5617 {
5618 ScreenAttrs[off] = multi_attr;
5619 multi_attr = 0;
5620 }
5621 else
5622#endif
5623 ScreenAttrs[off] = char_attr;
5624
5625#ifdef FEAT_MBYTE
5626 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5627 {
5628 /* Need to fill two screen columns. */
5629 ++off;
5630 ++col;
5631 if (enc_utf8)
5632 /* UTF-8: Put a 0 in the second screen char. */
5633 ScreenLines[off] = 0;
5634 else
5635 /* DBCS: Put second byte in the second screen char. */
5636 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005637 if (draw_state > WL_NR
5638#ifdef FEAT_DIFF
5639 && filler_todo <= 0
5640#endif
5641 )
5642 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 /* When "tocol" is halfway a character, set it to the end of
5644 * the character, otherwise highlighting won't stop. */
5645 if (tocol == vcol)
5646 ++tocol;
5647#ifdef FEAT_RIGHTLEFT
5648 if (wp->w_p_rl)
5649 {
5650 /* now it's time to backup one cell */
5651 --off;
5652 --col;
5653 }
5654#endif
5655 }
5656#endif
5657#ifdef FEAT_RIGHTLEFT
5658 if (wp->w_p_rl)
5659 {
5660 --off;
5661 --col;
5662 }
5663 else
5664#endif
5665 {
5666 ++off;
5667 ++col;
5668 }
5669 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005670#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005671 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005672 {
5673 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005674 ++vcol_off;
5675 if (n_extra > 0)
5676 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005677 if (wp->w_p_wrap)
5678 {
5679 /*
5680 * Special voodoo required if 'wrap' is on.
5681 *
5682 * Advance the column indicator to force the line
5683 * drawing to wrap early. This will make the line
5684 * take up the same screen space when parts are concealed,
5685 * so that cursor line computations aren't messed up.
5686 *
5687 * To avoid the fictitious advance of 'col' causing
5688 * trailing junk to be written out of the screen line
5689 * we are building, 'boguscols' keeps track of the number
5690 * of bad columns we have advanced.
5691 */
5692 if (n_extra > 0)
5693 {
5694 vcol += n_extra;
5695# ifdef FEAT_RIGHTLEFT
5696 if (wp->w_p_rl)
5697 {
5698 col -= n_extra;
5699 boguscols -= n_extra;
5700 }
5701 else
5702# endif
5703 {
5704 col += n_extra;
5705 boguscols += n_extra;
5706 }
5707 n_extra = 0;
5708 n_attr = 0;
5709 }
5710
5711
5712# ifdef FEAT_MBYTE
5713 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5714 {
5715 /* Need to fill two screen columns. */
5716# ifdef FEAT_RIGHTLEFT
5717 if (wp->w_p_rl)
5718 {
5719 --boguscols;
5720 --col;
5721 }
5722 else
5723# endif
5724 {
5725 ++boguscols;
5726 ++col;
5727 }
5728 }
5729# endif
5730
5731# ifdef FEAT_RIGHTLEFT
5732 if (wp->w_p_rl)
5733 {
5734 --boguscols;
5735 --col;
5736 }
5737 else
5738# endif
5739 {
5740 ++boguscols;
5741 ++col;
5742 }
5743 }
5744 else
5745 {
5746 if (n_extra > 0)
5747 {
5748 vcol += n_extra;
5749 n_extra = 0;
5750 n_attr = 0;
5751 }
5752 }
5753
5754 }
5755#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 else
5757 --n_skip;
5758
Bram Moolenaar64486672010-05-16 15:46:46 +02005759 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5760 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005761 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#ifdef FEAT_DIFF
5763 && filler_todo <= 0
5764#endif
5765 )
5766 ++vcol;
5767
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768#ifdef FEAT_SYN_HL
5769 if (vcol_save_attr >= 0)
5770 char_attr = vcol_save_attr;
5771#endif
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 /* restore attributes after "predeces" in 'listchars' */
5774 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5775 char_attr = saved_attr3;
5776
5777 /* restore attributes after last 'listchars' or 'number' char */
5778 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5779 char_attr = saved_attr2;
5780
5781 /*
5782 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005783 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 */
5785 if ((
5786#ifdef FEAT_RIGHTLEFT
5787 wp->w_p_rl ? (col < 0) :
5788#endif
5789 (col >= W_WIDTH(wp)))
5790 && (*ptr != NUL
5791#ifdef FEAT_DIFF
5792 || filler_todo > 0
5793#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005794 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5796 )
5797 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005798#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005799 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005800 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005801 boguscols = 0;
5802#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005803 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005804 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 ++row;
5807 ++screen_row;
5808
5809 /* When not wrapping and finished diff lines, or when displayed
5810 * '$' and highlighting until last column, break here. */
5811 if ((!wp->w_p_wrap
5812#ifdef FEAT_DIFF
5813 && filler_todo <= 0
5814#endif
5815 ) || lcs_eol_one == -1)
5816 break;
5817
5818 /* When the window is too narrow draw all "@" lines. */
5819 if (draw_state != WL_LINE
5820#ifdef FEAT_DIFF
5821 && filler_todo <= 0
5822#endif
5823 )
5824 {
5825 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005826#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 draw_vsep_win(wp, row);
5828#endif
5829 row = endrow;
5830 }
5831
5832 /* When line got too long for screen break here. */
5833 if (row == endrow)
5834 {
5835 ++row;
5836 break;
5837 }
5838
5839 if (screen_cur_row == screen_row - 1
5840#ifdef FEAT_DIFF
5841 && filler_todo <= 0
5842#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005843#ifdef FEAT_WINDOWS
5844 && W_WIDTH(wp) == Columns
5845#endif
5846 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
5848 /* Remember that the line wraps, used for modeless copy. */
5849 LineWraps[screen_row - 1] = TRUE;
5850
5851 /*
5852 * Special trick to make copy/paste of wrapped lines work with
5853 * xterm/screen: write an extra character beyond the end of
5854 * the line. This will work with all terminal types
5855 * (regardless of the xn,am settings).
5856 * Only do this on a fast tty.
5857 * Only do this if the cursor is on the current line
5858 * (something has been written in it).
5859 * Don't do this for the GUI.
5860 * Don't do this for double-width characters.
5861 * Don't do this for a window not at the right screen border.
5862 */
5863 if (p_tf
5864#ifdef FEAT_GUI
5865 && !gui.in_use
5866#endif
5867#ifdef FEAT_MBYTE
5868 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005869 && ((*mb_off2cells)(LineOffset[screen_row],
5870 LineOffset[screen_row] + screen_Columns)
5871 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005873 + (int)Columns - 2,
5874 LineOffset[screen_row] + screen_Columns)
5875 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876#endif
5877 )
5878 {
5879 /* First make sure we are at the end of the screen line,
5880 * then output the same character again to let the
5881 * terminal know about the wrap. If the terminal doesn't
5882 * auto-wrap, we overwrite the character. */
5883 if (screen_cur_col != W_WIDTH(wp))
5884 screen_char(LineOffset[screen_row - 1]
5885 + (unsigned)Columns - 1,
5886 screen_row - 1, (int)(Columns - 1));
5887
5888#ifdef FEAT_MBYTE
5889 /* When there is a multi-byte character, just output a
5890 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005891 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5892 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 out_char(' ');
5894 else
5895#endif
5896 out_char(ScreenLines[LineOffset[screen_row - 1]
5897 + (Columns - 1)]);
5898 /* force a redraw of the first char on the next line */
5899 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5900 screen_start(); /* don't know where cursor is now */
5901 }
5902 }
5903
5904 col = 0;
5905 off = (unsigned)(current_ScreenLine - ScreenLines);
5906#ifdef FEAT_RIGHTLEFT
5907 if (wp->w_p_rl)
5908 {
5909 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5910 off += col;
5911 }
5912#endif
5913
5914 /* reset the drawing state for the start of a wrapped line */
5915 draw_state = WL_START;
5916 saved_n_extra = n_extra;
5917 saved_p_extra = p_extra;
5918 saved_c_extra = c_extra;
5919 saved_char_attr = char_attr;
5920 n_extra = 0;
5921 lcs_prec_todo = lcs_prec;
5922#ifdef FEAT_LINEBREAK
5923# ifdef FEAT_DIFF
5924 if (filler_todo <= 0)
5925# endif
5926 need_showbreak = TRUE;
5927#endif
5928#ifdef FEAT_DIFF
5929 --filler_todo;
5930 /* When the filler lines are actually below the last line of the
5931 * file, don't draw the line itself, break here. */
5932 if (filler_todo == 0 && wp->w_botfill)
5933 break;
5934#endif
5935 }
5936
5937 } /* for every character in the line */
5938
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005939#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005940 /* After an empty line check first word for capital. */
5941 if (*skipwhite(line) == NUL)
5942 {
5943 capcol_lnum = lnum + 1;
5944 cap_col = 0;
5945 }
5946#endif
5947
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005948 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 return row;
5950}
5951
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005952#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005953static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005954
5955/*
5956 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005957 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005958 */
5959 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005960comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005961{
5962 int i;
5963
5964 for (i = 0; i < Screen_mco; ++i)
5965 {
5966 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5967 return TRUE;
5968 if (ScreenLinesC[i][off_from] == 0)
5969 break;
5970 }
5971 return FALSE;
5972}
5973#endif
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975/*
5976 * Check whether the given character needs redrawing:
5977 * - the (first byte of the) character is different
5978 * - the attributes are different
5979 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005980 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
5982 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005983char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 if (cols > 0
5986 && ((ScreenLines[off_from] != ScreenLines[off_to]
5987 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5988
5989#ifdef FEAT_MBYTE
5990 || (enc_dbcs != 0
5991 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5992 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5993 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5994 : (cols > 1 && ScreenLines[off_from + 1]
5995 != ScreenLines[off_to + 1])))
5996 || (enc_utf8
5997 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5998 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005999 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006000 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6001 && ScreenLines[off_from + 1]
6002 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003#endif
6004 ))
6005 return TRUE;
6006 return FALSE;
6007}
6008
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006009#if defined(FEAT_TERMINAL) || defined(PROTO)
6010/*
6011 * Return the index in ScreenLines[] for the current screen line.
6012 */
6013 int
6014screen_get_current_line_off()
6015{
6016 return (int)(current_ScreenLine - ScreenLines);
6017}
6018#endif
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
6021 * Move one "cooked" screen line to the screen, but only the characters that
6022 * have actually changed. Handle insert/delete character.
6023 * "coloff" gives the first column on the screen for this line.
6024 * "endcol" gives the columns where valid characters are.
6025 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6026 * needs to be cleared, negative otherwise.
6027 * "rlflag" is TRUE in a rightleft window:
6028 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6029 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6030 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006032screen_line(
6033 int row,
6034 int coloff,
6035 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006036 int clear_width,
6037 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 unsigned off_from;
6040 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006041#ifdef FEAT_MBYTE
6042 unsigned max_off_from;
6043 unsigned max_off_to;
6044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006046#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 int hl;
6048#endif
6049 int force = FALSE; /* force update rest of the line */
6050 int redraw_this /* bool: does character need redraw? */
6051#ifdef FEAT_GUI
6052 = TRUE /* For GUI when while-loop empty */
6053#endif
6054 ;
6055 int redraw_next; /* redraw_this for next character */
6056#ifdef FEAT_MBYTE
6057 int clear_next = FALSE;
6058 int char_cells; /* 1: normal char */
6059 /* 2: occupies two display cells */
6060# define CHAR_CELLS char_cells
6061#else
6062# define CHAR_CELLS 1
6063#endif
6064
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006065 /* Check for illegal row and col, just in case. */
6066 if (row >= Rows)
6067 row = Rows - 1;
6068 if (endcol > Columns)
6069 endcol = Columns;
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071# ifdef FEAT_CLIPBOARD
6072 clip_may_clear_selection(row, row);
6073# endif
6074
6075 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6076 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006077#ifdef FEAT_MBYTE
6078 max_off_from = off_from + screen_Columns;
6079 max_off_to = LineOffset[row] + screen_Columns;
6080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
6082#ifdef FEAT_RIGHTLEFT
6083 if (rlflag)
6084 {
6085 /* Clear rest first, because it's left of the text. */
6086 if (clear_width > 0)
6087 {
6088 while (col <= endcol && ScreenLines[off_to] == ' '
6089 && ScreenAttrs[off_to] == 0
6090# ifdef FEAT_MBYTE
6091 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6092# endif
6093 )
6094 {
6095 ++off_to;
6096 ++col;
6097 }
6098 if (col <= endcol)
6099 screen_fill(row, row + 1, col + coloff,
6100 endcol + coloff + 1, ' ', ' ', 0);
6101 }
6102 col = endcol + 1;
6103 off_to = LineOffset[row] + col + coloff;
6104 off_from += col;
6105 endcol = (clear_width > 0 ? clear_width : -clear_width);
6106 }
6107#endif /* FEAT_RIGHTLEFT */
6108
6109 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6110
6111 while (col < endcol)
6112 {
6113#ifdef FEAT_MBYTE
6114 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006115 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 else
6117 char_cells = 1;
6118#endif
6119
6120 redraw_this = redraw_next;
6121 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6122 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6123
6124#ifdef FEAT_GUI
6125 /* If the next character was bold, then redraw the current character to
6126 * remove any pixels that might have spilt over into us. This only
6127 * happens in the GUI.
6128 */
6129 if (redraw_next && gui.in_use)
6130 {
6131 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006132 if (hl > HL_ALL)
6133 hl = syn_attr2attr(hl);
6134 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 redraw_this = TRUE;
6136 }
6137#endif
6138
6139 if (redraw_this)
6140 {
6141 /*
6142 * Special handling when 'xs' termcap flag set (hpterm):
6143 * Attributes for characters are stored at the position where the
6144 * cursor is when writing the highlighting code. The
6145 * start-highlighting code must be written with the cursor on the
6146 * first highlighted character. The stop-highlighting code must
6147 * be written with the cursor just after the last highlighted
6148 * character.
6149 * Overwriting a character doesn't remove it's highlighting. Need
6150 * to clear the rest of the line, and force redrawing it
6151 * completely.
6152 */
6153 if ( p_wiv
6154 && !force
6155#ifdef FEAT_GUI
6156 && !gui.in_use
6157#endif
6158 && ScreenAttrs[off_to] != 0
6159 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6160 {
6161 /*
6162 * Need to remove highlighting attributes here.
6163 */
6164 windgoto(row, col + coloff);
6165 out_str(T_CE); /* clear rest of this screen line */
6166 screen_start(); /* don't know where cursor is now */
6167 force = TRUE; /* force redraw of rest of the line */
6168 redraw_next = TRUE; /* or else next char would miss out */
6169
6170 /*
6171 * If the previous character was highlighted, need to stop
6172 * highlighting at this character.
6173 */
6174 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6175 {
6176 screen_attr = ScreenAttrs[off_to - 1];
6177 term_windgoto(row, col + coloff);
6178 screen_stop_highlight();
6179 }
6180 else
6181 screen_attr = 0; /* highlighting has stopped */
6182 }
6183#ifdef FEAT_MBYTE
6184 if (enc_dbcs != 0)
6185 {
6186 /* Check if overwriting a double-byte with a single-byte or
6187 * the other way around requires another character to be
6188 * redrawn. For UTF-8 this isn't needed, because comparing
6189 * ScreenLinesUC[] is sufficient. */
6190 if (char_cells == 1
6191 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006192 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
6194 /* Writing a single-cell character over a double-cell
6195 * character: need to redraw the next cell. */
6196 ScreenLines[off_to + 1] = 0;
6197 redraw_next = TRUE;
6198 }
6199 else if (char_cells == 2
6200 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006201 && (*mb_off2cells)(off_to, max_off_to) == 1
6202 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /* Writing the second half of a double-cell character over
6205 * a double-cell character: need to redraw the second
6206 * cell. */
6207 ScreenLines[off_to + 2] = 0;
6208 redraw_next = TRUE;
6209 }
6210
6211 if (enc_dbcs == DBCS_JPNU)
6212 ScreenLines2[off_to] = ScreenLines2[off_from];
6213 }
6214 /* When writing a single-width character over a double-width
6215 * character and at the end of the redrawn text, need to clear out
6216 * the right halve of the old character.
6217 * Also required when writing the right halve of a double-width
6218 * char over the left halve of an existing one. */
6219 if (has_mbyte && col + char_cells == endcol
6220 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006221 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006223 && (*mb_off2cells)(off_to, max_off_to) == 1
6224 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 clear_next = TRUE;
6226#endif
6227
6228 ScreenLines[off_to] = ScreenLines[off_from];
6229#ifdef FEAT_MBYTE
6230 if (enc_utf8)
6231 {
6232 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6233 if (ScreenLinesUC[off_from] != 0)
6234 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006235 int i;
6236
6237 for (i = 0; i < Screen_mco; ++i)
6238 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
6240 }
6241 if (char_cells == 2)
6242 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6243#endif
6244
6245#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006246 /* The bold trick makes a single column of pixels appear in the
6247 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 * character should be redrawn too. This happens for our own GUI
6249 * and for some xterms. */
6250 if (
6251# ifdef FEAT_GUI
6252 gui.in_use
6253# endif
6254# if defined(FEAT_GUI) && defined(UNIX)
6255 ||
6256# endif
6257# ifdef UNIX
6258 term_is_xterm
6259# endif
6260 )
6261 {
6262 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006263 if (hl > HL_ALL)
6264 hl = syn_attr2attr(hl);
6265 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 redraw_next = TRUE;
6267 }
6268#endif
6269 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6270#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006271 /* For simplicity set the attributes of second half of a
6272 * double-wide character equal to the first half. */
6273 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006275
6276 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 else
6279#endif
6280 screen_char(off_to, row, col + coloff);
6281 }
6282 else if ( p_wiv
6283#ifdef FEAT_GUI
6284 && !gui.in_use
6285#endif
6286 && col + coloff > 0)
6287 {
6288 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6289 {
6290 /*
6291 * Don't output stop-highlight when moving the cursor, it will
6292 * stop the highlighting when it should continue.
6293 */
6294 screen_attr = 0;
6295 }
6296 else if (screen_attr != 0)
6297 screen_stop_highlight();
6298 }
6299
6300 off_to += CHAR_CELLS;
6301 off_from += CHAR_CELLS;
6302 col += CHAR_CELLS;
6303 }
6304
6305#ifdef FEAT_MBYTE
6306 if (clear_next)
6307 {
6308 /* Clear the second half of a double-wide character of which the left
6309 * half was overwritten with a single-wide character. */
6310 ScreenLines[off_to] = ' ';
6311 if (enc_utf8)
6312 ScreenLinesUC[off_to] = 0;
6313 screen_char(off_to, row, col + coloff);
6314 }
6315#endif
6316
6317 if (clear_width > 0
6318#ifdef FEAT_RIGHTLEFT
6319 && !rlflag
6320#endif
6321 )
6322 {
6323#ifdef FEAT_GUI
6324 int startCol = col;
6325#endif
6326
6327 /* blank out the rest of the line */
6328 while (col < clear_width && ScreenLines[off_to] == ' '
6329 && ScreenAttrs[off_to] == 0
6330#ifdef FEAT_MBYTE
6331 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6332#endif
6333 )
6334 {
6335 ++off_to;
6336 ++col;
6337 }
6338 if (col < clear_width)
6339 {
6340#ifdef FEAT_GUI
6341 /*
6342 * In the GUI, clearing the rest of the line may leave pixels
6343 * behind if the first character cleared was bold. Some bold
6344 * fonts spill over the left. In this case we redraw the previous
6345 * character too. If we didn't skip any blanks above, then we
6346 * only redraw if the character wasn't already redrawn anyway.
6347 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006348 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 hl = ScreenAttrs[off_to];
6351 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006352 {
6353 int prev_cells = 1;
6354# ifdef FEAT_MBYTE
6355 if (enc_utf8)
6356 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6357 * that its width is 2. */
6358 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6359 else if (enc_dbcs != 0)
6360 {
6361 /* find previous character by counting from first
6362 * column and get its width. */
6363 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006364 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006365
6366 while (off < off_to)
6367 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006368 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006369 off += prev_cells;
6370 }
6371 }
6372
6373 if (enc_dbcs != 0 && prev_cells > 1)
6374 screen_char_2(off_to - prev_cells, row,
6375 col + coloff - prev_cells);
6376 else
6377# endif
6378 screen_char(off_to - prev_cells, row,
6379 col + coloff - prev_cells);
6380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382#endif
6383 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6384 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006385#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 off_to += clear_width - col;
6387 col = clear_width;
6388#endif
6389 }
6390 }
6391
6392 if (clear_width > 0)
6393 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006394#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 /* For a window that's left of another, draw the separator char. */
6396 if (col + coloff < Columns)
6397 {
6398 int c;
6399
6400 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006401 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006403 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6404 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405# endif
6406 || ScreenAttrs[off_to] != hl)
6407 {
6408 ScreenLines[off_to] = c;
6409 ScreenAttrs[off_to] = hl;
6410# ifdef FEAT_MBYTE
6411 if (enc_utf8)
6412 {
6413 if (c >= 0x80)
6414 {
6415 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006416 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418 else
6419 ScreenLinesUC[off_to] = 0;
6420 }
6421# endif
6422 screen_char(off_to, row, col + coloff);
6423 }
6424 }
6425 else
6426#endif
6427 LineWraps[row] = FALSE;
6428 }
6429}
6430
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006431#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006433 * Mirror text "str" for right-left displaying.
6434 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006437rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438{
6439 char_u *p1, *p2;
6440 int t;
6441
6442 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6443 {
6444 t = *p1;
6445 *p1 = *p2;
6446 *p2 = t;
6447 }
6448}
6449#endif
6450
6451#if defined(FEAT_WINDOWS) || defined(PROTO)
6452/*
6453 * mark all status lines for redraw; used after first :cd
6454 */
6455 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006456status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 win_T *wp;
6459
Bram Moolenaar29323592016-07-24 22:04:11 +02006460 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (wp->w_status_height)
6462 {
6463 wp->w_redr_status = TRUE;
6464 redraw_later(VALID);
6465 }
6466}
6467
6468/*
6469 * mark all status lines of the current buffer for redraw
6470 */
6471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006472status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 win_T *wp;
6475
Bram Moolenaar29323592016-07-24 22:04:11 +02006476 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6478 {
6479 wp->w_redr_status = TRUE;
6480 redraw_later(VALID);
6481 }
6482}
6483
6484/*
6485 * Redraw all status lines that need to be redrawn.
6486 */
6487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006488redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 win_T *wp;
6491
Bram Moolenaar29323592016-07-24 22:04:11 +02006492 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (wp->w_redr_status)
6494 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006495 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006496 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498#endif
6499
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006500#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501/*
6502 * Redraw all status lines at the bottom of frame "frp".
6503 */
6504 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006505win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 if (frp->fr_layout == FR_LEAF)
6508 frp->fr_win->w_redr_status = TRUE;
6509 else if (frp->fr_layout == FR_ROW)
6510 {
6511 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6512 win_redraw_last_status(frp);
6513 }
6514 else /* frp->fr_layout == FR_COL */
6515 {
6516 frp = frp->fr_child;
6517 while (frp->fr_next != NULL)
6518 frp = frp->fr_next;
6519 win_redraw_last_status(frp);
6520 }
6521}
6522#endif
6523
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006524#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525/*
6526 * Draw the verticap separator right of window "wp" starting with line "row".
6527 */
6528 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006529draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 int hl;
6532 int c;
6533
6534 if (wp->w_vsep_width)
6535 {
6536 /* draw the vertical separator right of this window */
6537 c = fillchar_vsep(&hl);
6538 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6539 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6540 c, ' ', hl);
6541 }
6542}
6543#endif
6544
6545#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006546static int status_match_len(expand_T *xp, char_u *s);
6547static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006550 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 */
6552 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006553status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 int len = 0;
6556
6557#ifdef FEAT_MENU
6558 int emenu = (xp->xp_context == EXPAND_MENUS
6559 || xp->xp_context == EXPAND_MENUNAMES);
6560
6561 /* Check for menu separators - replace with '|'. */
6562 if (emenu && menu_is_separator(s))
6563 return 1;
6564#endif
6565
6566 while (*s != NUL)
6567 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006568 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006569 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006570 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572
6573 return len;
6574}
6575
6576/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006577 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006578 * These are backslashes used for escaping. Do show backslashes in help tags.
6579 */
6580 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006581skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006582{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006583 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006584#ifdef FEAT_MENU
6585 || ((xp->xp_context == EXPAND_MENUS
6586 || xp->xp_context == EXPAND_MENUNAMES)
6587 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6588#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006589 )
6590 {
6591#ifndef BACKSLASH_IN_FILENAME
6592 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6593 return 2;
6594#endif
6595 return 1;
6596 }
6597 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006598}
6599
6600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * Show wildchar matches in the status line.
6602 * Show at least the "match" item.
6603 * We start at item 'first_match' in the list and show all matches that fit.
6604 *
6605 * If inversion is possible we use it. Else '=' characters are used.
6606 */
6607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006608win_redr_status_matches(
6609 expand_T *xp,
6610 int num_matches,
6611 char_u **matches, /* list of matches */
6612 int match,
6613 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6616 int row;
6617 char_u *buf;
6618 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006619 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 int fillchar;
6621 int attr;
6622 int i;
6623 int highlight = TRUE;
6624 char_u *selstart = NULL;
6625 int selstart_col = 0;
6626 char_u *selend = NULL;
6627 static int first_match = 0;
6628 int add_left = FALSE;
6629 char_u *s;
6630#ifdef FEAT_MENU
6631 int emenu;
6632#endif
6633#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6634 int l;
6635#endif
6636
6637 if (matches == NULL) /* interrupted completion? */
6638 return;
6639
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006640#ifdef FEAT_MBYTE
6641 if (has_mbyte)
6642 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6643 else
6644#endif
6645 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (buf == NULL)
6647 return;
6648
6649 if (match == -1) /* don't show match but original text */
6650 {
6651 match = 0;
6652 highlight = FALSE;
6653 }
6654 /* count 1 for the ending ">" */
6655 clen = status_match_len(xp, L_MATCH(match)) + 3;
6656 if (match == 0)
6657 first_match = 0;
6658 else if (match < first_match)
6659 {
6660 /* jumping left, as far as we can go */
6661 first_match = match;
6662 add_left = TRUE;
6663 }
6664 else
6665 {
6666 /* check if match fits on the screen */
6667 for (i = first_match; i < match; ++i)
6668 clen += status_match_len(xp, L_MATCH(i)) + 2;
6669 if (first_match > 0)
6670 clen += 2;
6671 /* jumping right, put match at the left */
6672 if ((long)clen > Columns)
6673 {
6674 first_match = match;
6675 /* if showing the last match, we can add some on the left */
6676 clen = 2;
6677 for (i = match; i < num_matches; ++i)
6678 {
6679 clen += status_match_len(xp, L_MATCH(i)) + 2;
6680 if ((long)clen >= Columns)
6681 break;
6682 }
6683 if (i == num_matches)
6684 add_left = TRUE;
6685 }
6686 }
6687 if (add_left)
6688 while (first_match > 0)
6689 {
6690 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6691 if ((long)clen >= Columns)
6692 break;
6693 --first_match;
6694 }
6695
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006696 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698 if (first_match == 0)
6699 {
6700 *buf = NUL;
6701 len = 0;
6702 }
6703 else
6704 {
6705 STRCPY(buf, "< ");
6706 len = 2;
6707 }
6708 clen = len;
6709
6710 i = first_match;
6711 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6712 {
6713 if (i == match)
6714 {
6715 selstart = buf + len;
6716 selstart_col = clen;
6717 }
6718
6719 s = L_MATCH(i);
6720 /* Check for menu separators - replace with '|' */
6721#ifdef FEAT_MENU
6722 emenu = (xp->xp_context == EXPAND_MENUS
6723 || xp->xp_context == EXPAND_MENUNAMES);
6724 if (emenu && menu_is_separator(s))
6725 {
6726 STRCPY(buf + len, transchar('|'));
6727 l = (int)STRLEN(buf + len);
6728 len += l;
6729 clen += l;
6730 }
6731 else
6732#endif
6733 for ( ; *s != NUL; ++s)
6734 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006735 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 clen += ptr2cells(s);
6737#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
6740 STRNCPY(buf + len, s, l);
6741 s += l - 1;
6742 len += l;
6743 }
6744 else
6745#endif
6746 {
6747 STRCPY(buf + len, transchar_byte(*s));
6748 len += (int)STRLEN(buf + len);
6749 }
6750 }
6751 if (i == match)
6752 selend = buf + len;
6753
6754 *(buf + len++) = ' ';
6755 *(buf + len++) = ' ';
6756 clen += 2;
6757 if (++i == num_matches)
6758 break;
6759 }
6760
6761 if (i != num_matches)
6762 {
6763 *(buf + len++) = '>';
6764 ++clen;
6765 }
6766
6767 buf[len] = NUL;
6768
6769 row = cmdline_row - 1;
6770 if (row >= 0)
6771 {
6772 if (wild_menu_showing == 0)
6773 {
6774 if (msg_scrolled > 0)
6775 {
6776 /* Put the wildmenu just above the command line. If there is
6777 * no room, scroll the screen one line up. */
6778 if (cmdline_row == Rows - 1)
6779 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006780 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 ++msg_scrolled;
6782 }
6783 else
6784 {
6785 ++cmdline_row;
6786 ++row;
6787 }
6788 wild_menu_showing = WM_SCROLLED;
6789 }
6790 else
6791 {
6792 /* Create status line if needed by setting 'laststatus' to 2.
6793 * Set 'winminheight' to zero to avoid that the window is
6794 * resized. */
6795 if (lastwin->w_status_height == 0)
6796 {
6797 save_p_ls = p_ls;
6798 save_p_wmh = p_wmh;
6799 p_ls = 2;
6800 p_wmh = 0;
6801 last_status(FALSE);
6802 }
6803 wild_menu_showing = WM_SHOWN;
6804 }
6805 }
6806
6807 screen_puts(buf, row, 0, attr);
6808 if (selstart != NULL && highlight)
6809 {
6810 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006811 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813
6814 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6815 }
6816
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006817#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 win_redraw_last_status(topframe);
6819#else
6820 lastwin->w_redr_status = TRUE;
6821#endif
6822 vim_free(buf);
6823}
6824#endif
6825
6826#if defined(FEAT_WINDOWS) || defined(PROTO)
6827/*
6828 * Redraw the status line of window wp.
6829 *
6830 * If inversion is possible we use it. Else '=' characters are used.
6831 */
6832 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006833win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 int row;
6836 char_u *p;
6837 int len;
6838 int fillchar;
6839 int attr;
6840 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006841 static int busy = FALSE;
6842
6843 /* It's possible to get here recursively when 'statusline' (indirectly)
6844 * invokes ":redrawstatus". Simply ignore the call then. */
6845 if (busy)
6846 return;
6847 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849 wp->w_redr_status = FALSE;
6850 if (wp->w_status_height == 0)
6851 {
6852 /* no status line, can only be last window */
6853 redraw_cmdline = TRUE;
6854 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006855 else if (!redrawing()
6856#ifdef FEAT_INS_EXPAND
6857 /* don't update status line when popup menu is visible and may be
6858 * drawn over it */
6859 || pum_visible()
6860#endif
6861 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 /* Don't redraw right now, do it later. */
6864 wp->w_redr_status = TRUE;
6865 }
6866#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006867 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
6869 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006870 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872#endif
6873 else
6874 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006875 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006877 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 p = NameBuff;
6879 len = (int)STRLEN(p);
6880
Bram Moolenaard85f2712017-07-28 21:51:57 +02006881 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#ifdef FEAT_QUICKFIX
6883 || wp->w_p_pvw
6884#endif
6885 || bufIsChanged(wp->w_buffer)
6886 || wp->w_buffer->b_p_ro)
6887 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006888 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006890 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 len += (int)STRLEN(p + len);
6892 }
6893#ifdef FEAT_QUICKFIX
6894 if (wp->w_p_pvw)
6895 {
6896 STRCPY(p + len, _("[Preview]"));
6897 len += (int)STRLEN(p + len);
6898 }
6899#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006900 if (bufIsChanged(wp->w_buffer)
6901#ifdef FEAT_TERMINAL
6902 && !bt_terminal(wp->w_buffer)
6903#endif
6904 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {
6906 STRCPY(p + len, "[+]");
6907 len += 3;
6908 }
6909 if (wp->w_buffer->b_p_ro)
6910 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006911 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006912 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 }
6914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6916 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6917 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6918 if (this_ru_col <= 1)
6919 {
6920 p = (char_u *)"<"; /* No room for file name! */
6921 len = 1;
6922 }
6923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924#ifdef FEAT_MBYTE
6925 if (has_mbyte)
6926 {
6927 int clen = 0, i;
6928
6929 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006930 clen = mb_string2cells(p, -1);
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 /* Find first character that will fit.
6933 * Going from start to end is much faster for DBCS. */
6934 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006935 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 clen -= (*mb_ptr2cells)(p + i);
6937 len = clen;
6938 if (i > 0)
6939 {
6940 p = p + i - 1;
6941 *p = '<';
6942 ++len;
6943 }
6944
6945 }
6946 else
6947#endif
6948 if (len > this_ru_col - 1)
6949 {
6950 p += len - (this_ru_col - 1);
6951 *p = '<';
6952 len = this_ru_col - 1;
6953 }
6954
6955 row = W_WINROW(wp) + wp->w_height;
6956 screen_puts(p, row, W_WINCOL(wp), attr);
6957 screen_fill(row, row + 1, len + W_WINCOL(wp),
6958 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6959
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006960 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6962 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6963 - 1 + W_WINCOL(wp)), attr);
6964
6965#ifdef FEAT_CMDL_INFO
6966 win_redr_ruler(wp, TRUE);
6967#endif
6968 }
6969
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 /*
6971 * May need to draw the character below the vertical separator.
6972 */
6973 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6974 {
6975 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006976 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 else
6978 fillchar = fillchar_vsep(&attr);
6979 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6980 attr);
6981 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006982 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983}
6984
Bram Moolenaar238a5642006-02-21 22:12:05 +00006985#ifdef FEAT_STL_OPT
6986/*
6987 * Redraw the status line according to 'statusline' and take care of any
6988 * errors encountered.
6989 */
6990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006991redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006992{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006994 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006995
6996 /* When called recursively return. This can happen when the statusline
6997 * contains an expression that triggers a redraw. */
6998 if (entered)
6999 return;
7000 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007001
Bram Moolenaara742e082016-04-05 21:10:38 +02007002 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007003 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007004 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007005 {
7006 /* When there is an error disable the statusline, otherwise the
7007 * display is messed up with errors and a redraw triggers the problem
7008 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009 set_string_option_direct((char_u *)"statusline", -1,
7010 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007011 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007013 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007014 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007015}
7016#endif
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018/*
7019 * Return TRUE if the status line of window "wp" is connected to the status
7020 * line of the window right of it. If not, then it's a vertical separator.
7021 * Only call if (wp->w_vsep_width != 0).
7022 */
7023 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007024stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 frame_T *fr;
7027
7028 fr = wp->w_frame;
7029 while (fr->fr_parent != NULL)
7030 {
7031 if (fr->fr_parent->fr_layout == FR_COL)
7032 {
7033 if (fr->fr_next != NULL)
7034 break;
7035 }
7036 else
7037 {
7038 if (fr->fr_next != NULL)
7039 return TRUE;
7040 }
7041 fr = fr->fr_parent;
7042 }
7043 return FALSE;
7044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046#endif /* FEAT_WINDOWS */
7047
7048#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7049/*
7050 * Get the value to show for the language mappings, active 'keymap'.
7051 */
7052 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007053get_keymap_str(
7054 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007055 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007056 char_u *buf, /* buffer for the result */
7057 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 char_u *p;
7060
7061 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7062 return FALSE;
7063
7064 {
7065#ifdef FEAT_EVAL
7066 buf_T *old_curbuf = curbuf;
7067 win_T *old_curwin = curwin;
7068 char_u *s;
7069
7070 curbuf = wp->w_buffer;
7071 curwin = wp;
7072 STRCPY(buf, "b:keymap_name"); /* must be writable */
7073 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007074 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 --emsg_skip;
7076 curbuf = old_curbuf;
7077 curwin = old_curwin;
7078 if (p == NULL || *p == NUL)
7079#endif
7080 {
7081#ifdef FEAT_KEYMAP
7082 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7083 p = wp->w_buffer->b_p_keymap;
7084 else
7085#endif
7086 p = (char_u *)"lang";
7087 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007088 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 buf[0] = NUL;
7090#ifdef FEAT_EVAL
7091 vim_free(s);
7092#endif
7093 }
7094 return buf[0] != NUL;
7095}
7096#endif
7097
7098#if defined(FEAT_STL_OPT) || defined(PROTO)
7099/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007100 * Redraw the status line or ruler of window "wp".
7101 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 */
7103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007104win_redr_custom(
7105 win_T *wp,
7106 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007108 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 int attr;
7110 int curattr;
7111 int row;
7112 int col = 0;
7113 int maxwidth;
7114 int width;
7115 int n;
7116 int len;
7117 int fillchar;
7118 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007119 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007121 struct stl_hlrec hltab[STL_MAX_ITEM];
7122 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007123 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007124 win_T *ewp;
7125 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
Bram Moolenaar1d633412013-12-11 15:52:01 +01007127 /* There is a tiny chance that this gets called recursively: When
7128 * redrawing a status line triggers redrawing the ruler or tabline.
7129 * Avoid trouble by not allowing recursion. */
7130 if (entered)
7131 return;
7132 entered = TRUE;
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007137 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007138 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007139 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007140 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007141 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142 maxwidth = Columns;
7143# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007144 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 else
7148 {
7149 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007150 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007151 maxwidth = W_WIDTH(wp);
7152
7153 if (draw_ruler)
7154 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007157 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007158 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007159 if (*++stl == '-')
7160 stl++;
7161 if (atoi((char *)stl))
7162 while (VIM_ISDIGIT(*stl))
7163 stl++;
7164 if (*stl++ != '(')
7165 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007167#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 col = ru_col - (Columns - W_WIDTH(wp));
7169 if (col < (W_WIDTH(wp) + 1) / 2)
7170 col = (W_WIDTH(wp) + 1) / 2;
7171#else
7172 col = ru_col;
7173 if (col > (Columns + 1) / 2)
7174 col = (Columns + 1) / 2;
7175#endif
7176 maxwidth = W_WIDTH(wp) - col;
7177#ifdef FEAT_WINDOWS
7178 if (!wp->w_status_height)
7179#endif
7180 {
7181 row = Rows - 1;
7182 --maxwidth; /* writing in last column may cause scrolling */
7183 fillchar = ' ';
7184 attr = 0;
7185 }
7186
7187# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007188 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189# endif
7190 }
7191 else
7192 {
7193 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007196 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007197# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 use_sandbox = was_set_insecurely((char_u *)"statusline",
7199 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007200# endif
7201 }
7202
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007203#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 col += W_WINCOL(wp);
7205#endif
7206 }
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007209 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar61452852011-02-01 18:01:11 +01007211 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7212 * the cursor away and back. */
7213 ewp = wp == NULL ? curwin : wp;
7214 p_crb_save = ewp->w_p_crb;
7215 ewp->w_p_crb = FALSE;
7216
Bram Moolenaar362f3562009-11-03 16:20:34 +00007217 /* Make a copy, because the statusline may include a function call that
7218 * might change the option value and free the memory. */
7219 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007220 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007221 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007222 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007223 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007224 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007226 /* Make all characters printable. */
7227 p = transstr(buf);
7228 if (p != NULL)
7229 {
7230 vim_strncpy(buf, p, sizeof(buf) - 1);
7231 vim_free(p);
7232 }
7233
7234 /* fill up with "fillchar" */
7235 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007236 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238#ifdef FEAT_MBYTE
7239 len += (*mb_char2bytes)(fillchar, buf + len);
7240#else
7241 buf[len++] = fillchar;
7242#endif
7243 ++width;
7244 }
7245 buf[len] = NUL;
7246
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007247 /*
7248 * Draw each snippet with the specified highlighting.
7249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 curattr = attr;
7251 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 screen_puts_len(p, len, row, col, curattr);
7256 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261 else if (hltab[n].userhl < 0)
7262 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#ifdef FEAT_WINDOWS
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007264# ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007265 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7266 && wp->w_status_height != 0)
7267 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007268 else if (wp != NULL && bt_terminal(wp->w_buffer)
7269 && wp->w_status_height != 0)
7270 curattr = highlight_stlterm[hltab[n].userhl - 1];
7271# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007272 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007273 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274#endif
7275 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
7278 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007279
7280 if (wp == NULL)
7281 {
7282 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7283 col = 0;
7284 len = 0;
7285 p = buf;
7286 fillchar = 0;
7287 for (n = 0; tabtab[n].start != NULL; n++)
7288 {
7289 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7290 while (col < len)
7291 TabPageIdxs[col++] = fillchar;
7292 p = tabtab[n].start;
7293 fillchar = tabtab[n].userhl;
7294 }
7295 while (col < Columns)
7296 TabPageIdxs[col++] = fillchar;
7297 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007298
7299theend:
7300 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301}
7302
7303#endif /* FEAT_STL_OPT */
7304
7305/*
7306 * Output a single character directly to the screen and update ScreenLines.
7307 */
7308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007309screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 char_u buf[MB_MAXBYTES + 1];
7312
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007313#ifdef FEAT_MBYTE
7314 if (has_mbyte)
7315 buf[(*mb_char2bytes)(c, buf)] = NUL;
7316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007318 {
7319 buf[0] = c;
7320 buf[1] = NUL;
7321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 screen_puts(buf, row, col, attr);
7323}
7324
7325/*
7326 * Get a single character directly from ScreenLines into "bytes[]".
7327 * Also return its attribute in *attrp;
7328 */
7329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007330screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 unsigned off;
7333
7334 /* safety check */
7335 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7336 {
7337 off = LineOffset[row] + col;
7338 *attrp = ScreenAttrs[off];
7339 bytes[0] = ScreenLines[off];
7340 bytes[1] = NUL;
7341
7342#ifdef FEAT_MBYTE
7343 if (enc_utf8 && ScreenLinesUC[off] != 0)
7344 bytes[utfc_char2bytes(off, bytes)] = NUL;
7345 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7346 {
7347 bytes[0] = ScreenLines[off];
7348 bytes[1] = ScreenLines2[off];
7349 bytes[2] = NUL;
7350 }
7351 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7352 {
7353 bytes[1] = ScreenLines[off + 1];
7354 bytes[2] = NUL;
7355 }
7356#endif
7357 }
7358}
7359
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007360#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007361static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007362
7363/*
7364 * Return TRUE if composing characters for screen posn "off" differs from
7365 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007366 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007367 */
7368 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007369screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007370{
7371 int i;
7372
7373 for (i = 0; i < Screen_mco; ++i)
7374 {
7375 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7376 return TRUE;
7377 if (u8cc[i] == 0)
7378 break;
7379 }
7380 return FALSE;
7381}
7382#endif
7383
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384/*
7385 * Put string '*text' on the screen at position 'row' and 'col', with
7386 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7387 * Note: only outputs within one row, message is truncated at screen boundary!
7388 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7389 */
7390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007391screen_puts(
7392 char_u *text,
7393 int row,
7394 int col,
7395 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 screen_puts_len(text, -1, row, col, attr);
7398}
7399
7400/*
7401 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7402 * a NUL.
7403 */
7404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007405screen_puts_len(
7406 char_u *text,
7407 int textlen,
7408 int row,
7409 int col,
7410 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 unsigned off;
7413 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007414 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 int c;
7416#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007417 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int mbyte_blen = 1;
7419 int mbyte_cells = 1;
7420 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007421 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 int clear_next_cell = FALSE;
7423# ifdef FEAT_ARABIC
7424 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007425 int pc, nc, nc1;
7426 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427# endif
7428#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007429#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7430 int force_redraw_this;
7431 int force_redraw_next = FALSE;
7432#endif
7433 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7436 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007437 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439#ifdef FEAT_MBYTE
7440 /* When drawing over the right halve of a double-wide char clear out the
7441 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007442 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007443# ifdef FEAT_GUI
7444 && !gui.in_use
7445# endif
7446 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007447 {
7448 ScreenLines[off - 1] = ' ';
7449 ScreenAttrs[off - 1] = 0;
7450 if (enc_utf8)
7451 {
7452 ScreenLinesUC[off - 1] = 0;
7453 ScreenLinesC[0][off - 1] = 0;
7454 }
7455 /* redraw the previous cell, make it empty */
7456 screen_char(off - 1, row, col - 1);
7457 /* force the cell at "col" to be redrawn */
7458 force_redraw_next = TRUE;
7459 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007460#endif
7461
Bram Moolenaar367329b2007-08-30 11:53:22 +00007462#ifdef FEAT_MBYTE
7463 max_off = LineOffset[row] + screen_Columns;
7464#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007465 while (col < screen_Columns
7466 && (len < 0 || (int)(ptr - text) < len)
7467 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
7469 c = *ptr;
7470#ifdef FEAT_MBYTE
7471 /* check if this is the first byte of a multibyte */
7472 if (has_mbyte)
7473 {
7474 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007475 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007477 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7479 mbyte_cells = 1;
7480 else if (enc_dbcs != 0)
7481 mbyte_cells = mbyte_blen;
7482 else /* enc_utf8 */
7483 {
7484 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 (int)((text + len) - ptr));
7487 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007490# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 /* Non-BMP character: display as ? or fullwidth ?. */
7492 if (u8c >= 0x10000)
7493 {
7494 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7495 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007496 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499# ifdef FEAT_ARABIC
7500 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7501 {
7502 /* Do Arabic shaping. */
7503 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7504 {
7505 /* Past end of string to be displayed. */
7506 nc = NUL;
7507 nc1 = NUL;
7508 }
7509 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007510 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007511 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7512 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007513 nc1 = pcc[0];
7514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 pc = prev_c;
7516 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007517 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519 else
7520 prev_c = u8c;
7521# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007522 if (col + mbyte_cells > screen_Columns)
7523 {
7524 /* Only 1 cell left, but character requires 2 cells:
7525 * display a '>' in the last column to avoid wrapping. */
7526 c = '>';
7527 mbyte_cells = 1;
7528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 }
7531#endif
7532
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7534 force_redraw_this = force_redraw_next;
7535 force_redraw_next = FALSE;
7536#endif
7537
7538 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539#ifdef FEAT_MBYTE
7540 || (mbyte_cells == 2
7541 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7542 || (enc_dbcs == DBCS_JPNU
7543 && c == 0x8e
7544 && ScreenLines2[off] != ptr[1])
7545 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007546 && (ScreenLinesUC[off] !=
7547 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7548 || (ScreenLinesUC[off] != 0
7549 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550#endif
7551 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 || exmode_active;
7553
7554 if (need_redraw
7555#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7556 || force_redraw_this
7557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 )
7559 {
7560#if defined(FEAT_GUI) || defined(UNIX)
7561 /* The bold trick makes a single row of pixels appear in the next
7562 * character. When a bold character is removed, the next
7563 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007564 * and for some xterms. */
7565 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566# ifdef FEAT_GUI
7567 gui.in_use
7568# endif
7569# if defined(FEAT_GUI) && defined(UNIX)
7570 ||
7571# endif
7572# ifdef UNIX
7573 term_is_xterm
7574# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007575 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007577 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007579 if (n > HL_ALL)
7580 n = syn_attr2attr(n);
7581 if (n & HL_BOLD)
7582 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584#endif
7585#ifdef FEAT_MBYTE
7586 /* When at the end of the text and overwriting a two-cell
7587 * character with a one-cell character, need to clear the next
7588 * cell. Also when overwriting the left halve of a two-cell char
7589 * with the right halve of a two-cell char. Do this only once
7590 * (mb_off2cells() may return 2 on the right halve). */
7591 if (clear_next_cell)
7592 clear_next_cell = FALSE;
7593 else if (has_mbyte
7594 && (len < 0 ? ptr[mbyte_blen] == NUL
7595 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007596 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007598 && (*mb_off2cells)(off, max_off) == 1
7599 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 clear_next_cell = TRUE;
7601
7602 /* Make sure we never leave a second byte of a double-byte behind,
7603 * it confuses mb_off2cells(). */
7604 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007605 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007607 && (*mb_off2cells)(off, max_off) == 1
7608 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 ScreenLines[off + mbyte_blen] = 0;
7610#endif
7611 ScreenLines[off] = c;
7612 ScreenAttrs[off] = attr;
7613#ifdef FEAT_MBYTE
7614 if (enc_utf8)
7615 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007616 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 ScreenLinesUC[off] = 0;
7618 else
7619 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007620 int i;
7621
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007623 for (i = 0; i < Screen_mco; ++i)
7624 {
7625 ScreenLinesC[i][off] = u8cc[i];
7626 if (u8cc[i] == 0)
7627 break;
7628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630 if (mbyte_cells == 2)
7631 {
7632 ScreenLines[off + 1] = 0;
7633 ScreenAttrs[off + 1] = attr;
7634 }
7635 screen_char(off, row, col);
7636 }
7637 else if (mbyte_cells == 2)
7638 {
7639 ScreenLines[off + 1] = ptr[1];
7640 ScreenAttrs[off + 1] = attr;
7641 screen_char_2(off, row, col);
7642 }
7643 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7644 {
7645 ScreenLines2[off] = ptr[1];
7646 screen_char(off, row, col);
7647 }
7648 else
7649#endif
7650 screen_char(off, row, col);
7651 }
7652#ifdef FEAT_MBYTE
7653 if (has_mbyte)
7654 {
7655 off += mbyte_cells;
7656 col += mbyte_cells;
7657 ptr += mbyte_blen;
7658 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007659 {
7660 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007662 len = -1;
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665 else
7666#endif
7667 {
7668 ++off;
7669 ++col;
7670 ++ptr;
7671 }
7672 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007673
7674#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7675 /* If we detected the next character needs to be redrawn, but the text
7676 * doesn't extend up to there, update the character here. */
7677 if (force_redraw_next && col < screen_Columns)
7678 {
7679# ifdef FEAT_MBYTE
7680 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7681 screen_char_2(off, row, col);
7682 else
7683# endif
7684 screen_char(off, row, col);
7685 }
7686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
7689#ifdef FEAT_SEARCH_EXTRA
7690/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007691 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 */
7693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007694start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (p_hls && !no_hlsearch)
7697 {
7698 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007699 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007700# ifdef FEAT_RELTIME
7701 /* Set the time limit to 'redrawtime'. */
7702 profile_setlimit(p_rdt, &search_hl.tm);
7703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 }
7705}
7706
7707/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007708 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 */
7710 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007711end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 if (search_hl.rm.regprog != NULL)
7714 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007715 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 search_hl.rm.regprog = NULL;
7717 }
7718}
7719
7720/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007721 * Init for calling prepare_search_hl().
7722 */
7723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007724init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007725{
7726 matchitem_T *cur;
7727
7728 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7729 * match */
7730 cur = wp->w_match_head;
7731 while (cur != NULL)
7732 {
7733 cur->hl.rm = cur->match;
7734 if (cur->hlg_id == 0)
7735 cur->hl.attr = 0;
7736 else
7737 cur->hl.attr = syn_id2attr(cur->hlg_id);
7738 cur->hl.buf = wp->w_buffer;
7739 cur->hl.lnum = 0;
7740 cur->hl.first_lnum = 0;
7741# ifdef FEAT_RELTIME
7742 /* Set the time limit to 'redrawtime'. */
7743 profile_setlimit(p_rdt, &(cur->hl.tm));
7744# endif
7745 cur = cur->next;
7746 }
7747 search_hl.buf = wp->w_buffer;
7748 search_hl.lnum = 0;
7749 search_hl.first_lnum = 0;
7750 /* time limit is set at the toplevel, for all windows */
7751}
7752
7753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 * Advance to the match in window "wp" line "lnum" or past it.
7755 */
7756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007757prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007759 matchitem_T *cur; /* points to the match list */
7760 match_T *shl; /* points to search_hl or a match */
7761 int shl_flag; /* flag to indicate whether search_hl
7762 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007763 int pos_inprogress; /* marks that position match search is
7764 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 int n;
7766
7767 /*
7768 * When using a multi-line pattern, start searching at the top
7769 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007770 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007772 cur = wp->w_match_head;
7773 shl_flag = FALSE;
7774 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007776 if (shl_flag == FALSE)
7777 {
7778 shl = &search_hl;
7779 shl_flag = TRUE;
7780 }
7781 else
7782 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 if (shl->rm.regprog != NULL
7784 && shl->lnum == 0
7785 && re_multiline(shl->rm.regprog))
7786 {
7787 if (shl->first_lnum == 0)
7788 {
7789# ifdef FEAT_FOLDING
7790 for (shl->first_lnum = lnum;
7791 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7792 if (hasFoldingWin(wp, shl->first_lnum - 1,
7793 NULL, NULL, TRUE, NULL))
7794 break;
7795# else
7796 shl->first_lnum = wp->w_topline;
7797# endif
7798 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007799 if (cur != NULL)
7800 cur->pos.cur = 0;
7801 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007803 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7804 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007806 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7807 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007808 pos_inprogress = cur == NULL || cur->pos.cur == 0
7809 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 if (shl->lnum != 0)
7811 {
7812 shl->first_lnum = shl->lnum
7813 + shl->rm.endpos[0].lnum
7814 - shl->rm.startpos[0].lnum;
7815 n = shl->rm.endpos[0].col;
7816 }
7817 else
7818 {
7819 ++shl->first_lnum;
7820 n = 0;
7821 }
7822 }
7823 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007824 if (shl != &search_hl && cur != NULL)
7825 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827}
7828
7829/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007830 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 * Uses shl->buf.
7832 * Sets shl->lnum and shl->rm contents.
7833 * Note: Assumes a previous match is always before "lnum", unless
7834 * shl->lnum is zero.
7835 * Careful: Any pointers for buffer lines will become invalid.
7836 */
7837 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007838next_search_hl(
7839 win_T *win,
7840 match_T *shl, /* points to search_hl or a match */
7841 linenr_T lnum,
7842 colnr_T mincol, /* minimal column for a match */
7843 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 linenr_T l;
7846 colnr_T matchcol;
7847 long nmatched;
7848
7849 if (shl->lnum != 0)
7850 {
7851 /* Check for three situations:
7852 * 1. If the "lnum" is below a previous match, start a new search.
7853 * 2. If the previous match includes "mincol", use it.
7854 * 3. Continue after the previous match.
7855 */
7856 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7857 if (lnum > l)
7858 shl->lnum = 0;
7859 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7860 return;
7861 }
7862
7863 /*
7864 * Repeat searching for a match until one is found that includes "mincol"
7865 * or none is found in this line.
7866 */
7867 called_emsg = FALSE;
7868 for (;;)
7869 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007870#ifdef FEAT_RELTIME
7871 /* Stop searching after passing the time limit. */
7872 if (profile_passed_limit(&(shl->tm)))
7873 {
7874 shl->lnum = 0; /* no match found in time */
7875 break;
7876 }
7877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 /* Three situations:
7879 * 1. No useful previous match: search from start of line.
7880 * 2. Not Vi compatible or empty match: continue at next character.
7881 * Break the loop if this is beyond the end of the line.
7882 * 3. Vi compatible searching: continue at end of previous match.
7883 */
7884 if (shl->lnum == 0)
7885 matchcol = 0;
7886 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7887 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007888 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007890 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007891
7892 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007893 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007894 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007896 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 shl->lnum = 0;
7898 break;
7899 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007900#ifdef FEAT_MBYTE
7901 if (has_mbyte)
7902 matchcol += mb_ptr2len(ml);
7903 else
7904#endif
7905 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 }
7907 else
7908 matchcol = shl->rm.endpos[0].col;
7909
7910 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007911 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007913 /* Remember whether shl->rm is using a copy of the regprog in
7914 * cur->match. */
7915 int regprog_is_copy = (shl != &search_hl && cur != NULL
7916 && shl == &cur->hl
7917 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007918 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007919
Bram Moolenaarb3414592014-06-17 17:48:32 +02007920 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7921 matchcol,
7922#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007923 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007925 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926#endif
7927 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007928 /* Copy the regprog, in case it got freed and recompiled. */
7929 if (regprog_is_copy)
7930 cur->match.regprog = cur->hl.rm.regprog;
7931
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007932 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007933 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007934 /* Error while handling regexp: stop using this regexp. */
7935 if (shl == &search_hl)
7936 {
7937 /* don't free regprog in the match list, it's a copy */
7938 vim_regfree(shl->rm.regprog);
7939 SET_NO_HLSEARCH(TRUE);
7940 }
7941 shl->rm.regprog = NULL;
7942 shl->lnum = 0;
7943 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7944 message */
7945 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007946 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 }
7948 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007949 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007950 else
7951 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 if (nmatched == 0)
7953 {
7954 shl->lnum = 0; /* no match found */
7955 break;
7956 }
7957 if (shl->rm.startpos[0].lnum > 0
7958 || shl->rm.startpos[0].col >= mincol
7959 || nmatched > 1
7960 || shl->rm.endpos[0].col > mincol)
7961 {
7962 shl->lnum += shl->rm.startpos[0].lnum;
7963 break; /* useful match found */
7964 }
7965 }
7966}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967
Bram Moolenaar85077472016-10-16 14:35:48 +02007968/*
7969 * If there is a match fill "shl" and return one.
7970 * Return zero otherwise.
7971 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007973next_search_hl_pos(
7974 match_T *shl, /* points to a match */
7975 linenr_T lnum,
7976 posmatch_T *posmatch, /* match positions */
7977 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978{
7979 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007980 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7983 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007984 llpos_T *pos = &posmatch->pos[i];
7985
7986 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007988 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007990 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007994 /* if this match comes before the one at "found" then swap
7995 * them */
7996 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007998 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999
Bram Moolenaar85077472016-10-16 14:35:48 +02008000 *pos = posmatch->pos[found];
8001 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008002 }
8003 }
8004 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008005 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 }
8007 }
8008 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008011 colnr_T start = posmatch->pos[found].col == 0
8012 ? 0 : posmatch->pos[found].col - 1;
8013 colnr_T end = posmatch->pos[found].col == 0
8014 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015
Bram Moolenaar85077472016-10-16 14:35:48 +02008016 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008017 shl->rm.startpos[0].lnum = 0;
8018 shl->rm.startpos[0].col = start;
8019 shl->rm.endpos[0].lnum = 0;
8020 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008021 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008022 posmatch->cur = found + 1;
8023 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008025 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008027#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008028
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008030screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032 attrentry_T *aep = NULL;
8033
8034 screen_attr = attr;
8035 if (full_screen
8036#ifdef WIN3264
8037 && termcap_active
8038#endif
8039 )
8040 {
8041#ifdef FEAT_GUI
8042 if (gui.in_use)
8043 {
8044 char buf[20];
8045
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008046 /* The GUI handles this internally. */
8047 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 OUT_STR(buf);
8049 }
8050 else
8051#endif
8052 {
8053 if (attr > HL_ALL) /* special HL attr. */
8054 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008055 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 aep = syn_cterm_attr2entry(attr);
8057 else
8058 aep = syn_term_attr2entry(attr);
8059 if (aep == NULL) /* did ":syntax clear" */
8060 attr = 0;
8061 else
8062 attr = aep->ae_attr;
8063 }
8064 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8065 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008066 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008067#ifdef FEAT_TERMGUICOLORS
8068 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008069 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008070#endif
8071 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008072#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008073 )
8074#endif
8075 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008076 /* If the Normal FG color has BOLD attribute and the new HL
8077 * has a FG color defined, clear BOLD. */
8078 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8080 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008081 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8082 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 out_str(T_US);
8084 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8085 out_str(T_CZH);
8086 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8087 out_str(T_MR);
8088
8089 /*
8090 * Output the color or start string after bold etc., in case the
8091 * bold etc. override the color setting.
8092 */
8093 if (aep != NULL)
8094 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008095#ifdef FEAT_TERMGUICOLORS
8096 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008098 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008099 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008100 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008101 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008106 if (t_colors > 1)
8107 {
8108 if (aep->ae_u.cterm.fg_color)
8109 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8110 if (aep->ae_u.cterm.bg_color)
8111 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8112 }
8113 else
8114 {
8115 if (aep->ae_u.term.start != NULL)
8116 out_str(aep->ae_u.term.start);
8117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119 }
8120 }
8121 }
8122}
8123
8124 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008125screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 int do_ME = FALSE; /* output T_ME code */
8128
8129 if (screen_attr != 0
8130#ifdef WIN3264
8131 && termcap_active
8132#endif
8133 )
8134 {
8135#ifdef FEAT_GUI
8136 if (gui.in_use)
8137 {
8138 char buf[20];
8139
8140 /* use internal GUI code */
8141 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8142 OUT_STR(buf);
8143 }
8144 else
8145#endif
8146 {
8147 if (screen_attr > HL_ALL) /* special HL attr. */
8148 {
8149 attrentry_T *aep;
8150
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
8153 /*
8154 * Assume that t_me restores the original colors!
8155 */
8156 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008157 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008158#ifdef FEAT_TERMGUICOLORS
8159 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008160 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8161 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008162#endif
8163 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008164#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008165 )
8166#endif
8167 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 do_ME = TRUE;
8169 }
8170 else
8171 {
8172 aep = syn_term_attr2entry(screen_attr);
8173 if (aep != NULL && aep->ae_u.term.stop != NULL)
8174 {
8175 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8176 do_ME = TRUE;
8177 else
8178 out_str(aep->ae_u.term.stop);
8179 }
8180 }
8181 if (aep == NULL) /* did ":syntax clear" */
8182 screen_attr = 0;
8183 else
8184 screen_attr = aep->ae_attr;
8185 }
8186
8187 /*
8188 * Often all ending-codes are equal to T_ME. Avoid outputting the
8189 * same sequence several times.
8190 */
8191 if (screen_attr & HL_STANDOUT)
8192 {
8193 if (STRCMP(T_SE, T_ME) == 0)
8194 do_ME = TRUE;
8195 else
8196 out_str(T_SE);
8197 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008198 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 if (STRCMP(T_UE, T_ME) == 0)
8201 do_ME = TRUE;
8202 else
8203 out_str(T_UE);
8204 }
8205 if (screen_attr & HL_ITALIC)
8206 {
8207 if (STRCMP(T_CZR, T_ME) == 0)
8208 do_ME = TRUE;
8209 else
8210 out_str(T_CZR);
8211 }
8212 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8213 out_str(T_ME);
8214
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008215#ifdef FEAT_TERMGUICOLORS
8216 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008218 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008219 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008220 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008221 term_bg_rgb_color(cterm_normal_bg_gui_color);
8222 }
8223 else
8224#endif
8225 {
8226 if (t_colors > 1)
8227 {
8228 /* set Normal cterm colors */
8229 if (cterm_normal_fg_color != 0)
8230 term_fg_color(cterm_normal_fg_color - 1);
8231 if (cterm_normal_bg_color != 0)
8232 term_bg_color(cterm_normal_bg_color - 1);
8233 if (cterm_normal_fg_bold)
8234 out_str(T_MD);
8235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 }
8238 }
8239 screen_attr = 0;
8240}
8241
8242/*
8243 * Reset the colors for a cterm. Used when leaving Vim.
8244 * The machine specific code may override this again.
8245 */
8246 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008247reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008249 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {
8251 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008252#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008253 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8254 || cterm_normal_bg_gui_color != INVALCOLOR)
8255 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008256#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {
8260 out_str(T_OP);
8261 screen_attr = -1;
8262 }
8263 if (cterm_normal_fg_bold)
8264 {
8265 out_str(T_ME);
8266 screen_attr = -1;
8267 }
8268 }
8269}
8270
8271/*
8272 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8273 * using the attributes from ScreenAttrs["off"].
8274 */
8275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008276screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 int attr;
8279
8280 /* Check for illegal values, just in case (could happen just after
8281 * resizing). */
8282 if (row >= screen_Rows || col >= screen_Columns)
8283 return;
8284
Bram Moolenaar494838a2015-02-10 19:20:37 +01008285 /* Outputting a character in the last cell on the screen may scroll the
8286 * screen up. Only do it when the "xn" termcap property is set, otherwise
8287 * mark the character invalid (update it when scrolled up). */
8288 if (*T_XN == NUL
8289 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290#ifdef FEAT_RIGHTLEFT
8291 /* account for first command-line character in rightleft mode */
8292 && !cmdmsg_rl
8293#endif
8294 )
8295 {
8296 ScreenAttrs[off] = (sattr_T)-1;
8297 return;
8298 }
8299
8300 /*
8301 * Stop highlighting first, so it's easier to move the cursor.
8302 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008303#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 if (screen_char_attr != 0)
8305 attr = screen_char_attr;
8306 else
8307#endif
8308 attr = ScreenAttrs[off];
8309 if (screen_attr != attr)
8310 screen_stop_highlight();
8311
8312 windgoto(row, col);
8313
8314 if (screen_attr != attr)
8315 screen_start_highlight(attr);
8316
8317#ifdef FEAT_MBYTE
8318 if (enc_utf8 && ScreenLinesUC[off] != 0)
8319 {
8320 char_u buf[MB_MAXBYTES + 1];
8321
8322 /* Convert UTF-8 character to bytes and write it. */
8323
8324 buf[utfc_char2bytes(off, buf)] = NUL;
8325
8326 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008327 if (utf_ambiguous_width(ScreenLinesUC[off]))
8328 screen_cur_col = 9999;
8329 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 ++screen_cur_col;
8331 }
8332 else
8333#endif
8334 {
8335#ifdef FEAT_MBYTE
8336 out_flush_check();
8337#endif
8338 out_char(ScreenLines[off]);
8339#ifdef FEAT_MBYTE
8340 /* double-byte character in single-width cell */
8341 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8342 out_char(ScreenLines2[off]);
8343#endif
8344 }
8345
8346 screen_cur_col++;
8347}
8348
8349#ifdef FEAT_MBYTE
8350
8351/*
8352 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8353 * on the screen at position 'row' and 'col'.
8354 * The attributes of the first byte is used for all. This is required to
8355 * output the two bytes of a double-byte character with nothing in between.
8356 */
8357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008358screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 /* Check for illegal values (could be wrong when screen was resized). */
8361 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8362 return;
8363
8364 /* Outputting the last character on the screen may scrollup the screen.
8365 * Don't to it! Mark the character invalid (update it when scrolled up) */
8366 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8367 {
8368 ScreenAttrs[off] = (sattr_T)-1;
8369 return;
8370 }
8371
8372 /* Output the first byte normally (positions the cursor), then write the
8373 * second byte directly. */
8374 screen_char(off, row, col);
8375 out_char(ScreenLines[off + 1]);
8376 ++screen_cur_col;
8377}
8378#endif
8379
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008380#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381/*
8382 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8383 * This uses the contents of ScreenLines[] and doesn't change it.
8384 */
8385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008386screen_draw_rectangle(
8387 int row,
8388 int col,
8389 int height,
8390 int width,
8391 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 int r, c;
8394 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008395#ifdef FEAT_MBYTE
8396 int max_off;
8397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008399 /* Can't use ScreenLines unless initialized */
8400 if (ScreenLines == NULL)
8401 return;
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (invert)
8404 screen_char_attr = HL_INVERSE;
8405 for (r = row; r < row + height; ++r)
8406 {
8407 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008408#ifdef FEAT_MBYTE
8409 max_off = off + screen_Columns;
8410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 for (c = col; c < col + width; ++c)
8412 {
8413#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008414 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {
8416 screen_char_2(off + c, r, c);
8417 ++c;
8418 }
8419 else
8420#endif
8421 {
8422 screen_char(off + c, r, c);
8423#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008424 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 ++c;
8426#endif
8427 }
8428 }
8429 }
8430 screen_char_attr = 0;
8431}
8432#endif
8433
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008434#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*
8436 * Redraw the characters for a vertically split window.
8437 */
8438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008439redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 int col;
8442 int width;
8443
8444# ifdef FEAT_CLIPBOARD
8445 clip_may_clear_selection(row, end - 1);
8446# endif
8447
8448 if (wp == NULL)
8449 {
8450 col = 0;
8451 width = Columns;
8452 }
8453 else
8454 {
8455 col = wp->w_wincol;
8456 width = wp->w_width;
8457 }
8458 screen_draw_rectangle(row, col, end - row, width, FALSE);
8459}
8460#endif
8461
8462/*
8463 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8464 * with character 'c1' in first column followed by 'c2' in the other columns.
8465 * Use attributes 'attr'.
8466 */
8467 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008468screen_fill(
8469 int start_row,
8470 int end_row,
8471 int start_col,
8472 int end_col,
8473 int c1,
8474 int c2,
8475 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 int row;
8478 int col;
8479 int off;
8480 int end_off;
8481 int did_delete;
8482 int c;
8483 int norm_term;
8484#if defined(FEAT_GUI) || defined(UNIX)
8485 int force_next = FALSE;
8486#endif
8487
8488 if (end_row > screen_Rows) /* safety check */
8489 end_row = screen_Rows;
8490 if (end_col > screen_Columns) /* safety check */
8491 end_col = screen_Columns;
8492 if (ScreenLines == NULL
8493 || start_row >= end_row
8494 || start_col >= end_col) /* nothing to do */
8495 return;
8496
8497 /* it's a "normal" terminal when not in a GUI or cterm */
8498 norm_term = (
8499#ifdef FEAT_GUI
8500 !gui.in_use &&
8501#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008502 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 for (row = start_row; row < end_row; ++row)
8504 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008505#ifdef FEAT_MBYTE
8506 if (has_mbyte
8507# ifdef FEAT_GUI
8508 && !gui.in_use
8509# endif
8510 )
8511 {
8512 /* When drawing over the right halve of a double-wide char clear
8513 * out the left halve. When drawing over the left halve of a
8514 * double wide-char clear out the right halve. Only needed in a
8515 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008516 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008517 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008518 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008519 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008520 }
8521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 /*
8523 * Try to use delete-line termcap code, when no attributes or in a
8524 * "normal" terminal, where a bold/italic space is just a
8525 * space.
8526 */
8527 did_delete = FALSE;
8528 if (c2 == ' '
8529 && end_col == Columns
8530 && can_clear(T_CE)
8531 && (attr == 0
8532 || (norm_term
8533 && attr <= HL_ALL
8534 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8535 {
8536 /*
8537 * check if we really need to clear something
8538 */
8539 col = start_col;
8540 if (c1 != ' ') /* don't clear first char */
8541 ++col;
8542
8543 off = LineOffset[row] + col;
8544 end_off = LineOffset[row] + end_col;
8545
8546 /* skip blanks (used often, keep it fast!) */
8547#ifdef FEAT_MBYTE
8548 if (enc_utf8)
8549 while (off < end_off && ScreenLines[off] == ' '
8550 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8551 ++off;
8552 else
8553#endif
8554 while (off < end_off && ScreenLines[off] == ' '
8555 && ScreenAttrs[off] == 0)
8556 ++off;
8557 if (off < end_off) /* something to be cleared */
8558 {
8559 col = off - LineOffset[row];
8560 screen_stop_highlight();
8561 term_windgoto(row, col);/* clear rest of this screen line */
8562 out_str(T_CE);
8563 screen_start(); /* don't know where cursor is now */
8564 col = end_col - col;
8565 while (col--) /* clear chars in ScreenLines */
8566 {
8567 ScreenLines[off] = ' ';
8568#ifdef FEAT_MBYTE
8569 if (enc_utf8)
8570 ScreenLinesUC[off] = 0;
8571#endif
8572 ScreenAttrs[off] = 0;
8573 ++off;
8574 }
8575 }
8576 did_delete = TRUE; /* the chars are cleared now */
8577 }
8578
8579 off = LineOffset[row] + start_col;
8580 c = c1;
8581 for (col = start_col; col < end_col; ++col)
8582 {
8583 if (ScreenLines[off] != c
8584#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008585 || (enc_utf8 && (int)ScreenLinesUC[off]
8586 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587#endif
8588 || ScreenAttrs[off] != attr
8589#if defined(FEAT_GUI) || defined(UNIX)
8590 || force_next
8591#endif
8592 )
8593 {
8594#if defined(FEAT_GUI) || defined(UNIX)
8595 /* The bold trick may make a single row of pixels appear in
8596 * the next character. When a bold character is removed, the
8597 * next character should be redrawn too. This happens for our
8598 * own GUI and for some xterms. */
8599 if (
8600# ifdef FEAT_GUI
8601 gui.in_use
8602# endif
8603# if defined(FEAT_GUI) && defined(UNIX)
8604 ||
8605# endif
8606# ifdef UNIX
8607 term_is_xterm
8608# endif
8609 )
8610 {
8611 if (ScreenLines[off] != ' '
8612 && (ScreenAttrs[off] > HL_ALL
8613 || ScreenAttrs[off] & HL_BOLD))
8614 force_next = TRUE;
8615 else
8616 force_next = FALSE;
8617 }
8618#endif
8619 ScreenLines[off] = c;
8620#ifdef FEAT_MBYTE
8621 if (enc_utf8)
8622 {
8623 if (c >= 0x80)
8624 {
8625 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 else
8629 ScreenLinesUC[off] = 0;
8630 }
8631#endif
8632 ScreenAttrs[off] = attr;
8633 if (!did_delete || c != ' ')
8634 screen_char(off, row, col);
8635 }
8636 ++off;
8637 if (col == start_col)
8638 {
8639 if (did_delete)
8640 break;
8641 c = c2;
8642 }
8643 }
8644 if (end_col == Columns)
8645 LineWraps[row] = FALSE;
8646 if (row == Rows - 1) /* overwritten the command line */
8647 {
8648 redraw_cmdline = TRUE;
8649 if (c1 == ' ' && c2 == ' ')
8650 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008651 if (start_col == 0)
8652 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 }
8654 }
8655}
8656
8657/*
8658 * Check if there should be a delay. Used before clearing or redrawing the
8659 * screen or the command line.
8660 */
8661 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008662check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663{
8664 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8665 && !did_wait_return
8666 && emsg_silent == 0)
8667 {
8668 out_flush();
8669 ui_delay(1000L, TRUE);
8670 emsg_on_display = FALSE;
8671 if (check_msg_scroll)
8672 msg_scroll = FALSE;
8673 }
8674}
8675
8676/*
8677 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008678 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 * Returns TRUE if there is a valid screen to write to.
8680 * Returns FALSE when starting up and screen not initialized yet.
8681 */
8682 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008683screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008685 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 return (ScreenLines != NULL);
8687}
8688
8689/*
8690 * Resize the shell to Rows and Columns.
8691 * Allocate ScreenLines[] and associated items.
8692 *
8693 * There may be some time between setting Rows and Columns and (re)allocating
8694 * ScreenLines[]. This happens when starting up and when (manually) changing
8695 * the shell size. Always use screen_Rows and screen_Columns to access items
8696 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8697 * final size of the shell is needed.
8698 */
8699 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008700screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 int new_row, old_row;
8703#ifdef FEAT_GUI
8704 int old_Rows;
8705#endif
8706 win_T *wp;
8707 int outofmem = FALSE;
8708 int len;
8709 schar_T *new_ScreenLines;
8710#ifdef FEAT_MBYTE
8711 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008712 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008714 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715#endif
8716 sattr_T *new_ScreenAttrs;
8717 unsigned *new_LineOffset;
8718 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008719#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008720 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008721 tabpage_T *tp;
8722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008724 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008725#ifdef FEAT_AUTOCMD
8726 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008728retry:
8729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 /*
8731 * Allocation of the screen buffers is done only when the size changes and
8732 * when Rows and Columns have been set and we have started doing full
8733 * screen stuff.
8734 */
8735 if ((ScreenLines != NULL
8736 && Rows == screen_Rows
8737 && Columns == screen_Columns
8738#ifdef FEAT_MBYTE
8739 && enc_utf8 == (ScreenLinesUC != NULL)
8740 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742#endif
8743 )
8744 || Rows == 0
8745 || Columns == 0
8746 || (!full_screen && ScreenLines == NULL))
8747 return;
8748
8749 /*
8750 * It's possible that we produce an out-of-memory message below, which
8751 * will cause this function to be called again. To break the loop, just
8752 * return here.
8753 */
8754 if (entered)
8755 return;
8756 entered = TRUE;
8757
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008758 /*
8759 * Note that the window sizes are updated before reallocating the arrays,
8760 * thus we must not redraw here!
8761 */
8762 ++RedrawingDisabled;
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 win_new_shellsize(); /* fit the windows in the new sized shell */
8765
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 comp_col(); /* recompute columns for shown command and ruler */
8767
8768 /*
8769 * We're changing the size of the screen.
8770 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8771 * - Move lines from the old arrays into the new arrays, clear extra
8772 * lines (unless the screen is going to be cleared).
8773 * - Free the old arrays.
8774 *
8775 * If anything fails, make ScreenLines NULL, so we don't do anything!
8776 * Continuing with the old ScreenLines may result in a crash, because the
8777 * size is wrong.
8778 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008779 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008781#ifdef FEAT_AUTOCMD
8782 if (aucmd_win != NULL)
8783 win_free_lsize(aucmd_win);
8784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 new_ScreenLines = (schar_T *)lalloc((long_u)(
8787 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8788#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008789 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 if (enc_utf8)
8791 {
8792 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8793 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008794 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008795 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8797 }
8798 if (enc_dbcs == DBCS_JPNU)
8799 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8800 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8801#endif
8802 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8803 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8804 new_LineOffset = (unsigned *)lalloc((long_u)(
8805 Rows * sizeof(unsigned)), FALSE);
8806 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008807#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008808 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008811 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 {
8813 if (win_alloc_lines(wp) == FAIL)
8814 {
8815 outofmem = TRUE;
8816#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008817 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818#endif
8819 }
8820 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008821#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008822 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8823 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008824 outofmem = TRUE;
8825#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008826#ifdef FEAT_WINDOWS
8827give_up:
8828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008830#ifdef FEAT_MBYTE
8831 for (i = 0; i < p_mco; ++i)
8832 if (new_ScreenLinesC[i] == NULL)
8833 break;
8834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (new_ScreenLines == NULL
8836#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8839#endif
8840 || new_ScreenAttrs == NULL
8841 || new_LineOffset == NULL
8842 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008843#ifdef FEAT_WINDOWS
8844 || new_TabPageIdxs == NULL
8845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 || outofmem)
8847 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008848 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008849 {
8850 /* guess the size */
8851 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8852
8853 /* Remember we did this to avoid getting outofmem messages over
8854 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008855 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 vim_free(new_ScreenLines);
8858 new_ScreenLines = NULL;
8859#ifdef FEAT_MBYTE
8860 vim_free(new_ScreenLinesUC);
8861 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008862 for (i = 0; i < p_mco; ++i)
8863 {
8864 vim_free(new_ScreenLinesC[i]);
8865 new_ScreenLinesC[i] = NULL;
8866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 vim_free(new_ScreenLines2);
8868 new_ScreenLines2 = NULL;
8869#endif
8870 vim_free(new_ScreenAttrs);
8871 new_ScreenAttrs = NULL;
8872 vim_free(new_LineOffset);
8873 new_LineOffset = NULL;
8874 vim_free(new_LineWraps);
8875 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008876#ifdef FEAT_WINDOWS
8877 vim_free(new_TabPageIdxs);
8878 new_TabPageIdxs = NULL;
8879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
8881 else
8882 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008883 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 for (new_row = 0; new_row < Rows; ++new_row)
8886 {
8887 new_LineOffset[new_row] = new_row * Columns;
8888 new_LineWraps[new_row] = FALSE;
8889
8890 /*
8891 * If the screen is not going to be cleared, copy as much as
8892 * possible from the old screen to the new one and clear the rest
8893 * (used when resizing the window at the "--more--" prompt or when
8894 * executing an external command, for the GUI).
8895 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008896 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 {
8898 (void)vim_memset(new_ScreenLines + new_row * Columns,
8899 ' ', (size_t)Columns * sizeof(schar_T));
8900#ifdef FEAT_MBYTE
8901 if (enc_utf8)
8902 {
8903 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8904 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008905 for (i = 0; i < p_mco; ++i)
8906 (void)vim_memset(new_ScreenLinesC[i]
8907 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 0, (size_t)Columns * sizeof(u8char_T));
8909 }
8910 if (enc_dbcs == DBCS_JPNU)
8911 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8912 0, (size_t)Columns * sizeof(schar_T));
8913#endif
8914 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8915 0, (size_t)Columns * sizeof(sattr_T));
8916 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008917 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
8919 if (screen_Columns < Columns)
8920 len = screen_Columns;
8921 else
8922 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008923#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008924 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008925 * may be invalid now. Also when p_mco changes. */
8926 if (!(enc_utf8 && ScreenLinesUC == NULL)
8927 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008928#endif
8929 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8930 ScreenLines + LineOffset[old_row],
8931 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008933 if (enc_utf8 && ScreenLinesUC != NULL
8934 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
8936 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8937 ScreenLinesUC + LineOffset[old_row],
8938 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008939 for (i = 0; i < p_mco; ++i)
8940 mch_memmove(new_ScreenLinesC[i]
8941 + new_LineOffset[new_row],
8942 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 (size_t)len * sizeof(u8char_T));
8944 }
8945 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8946 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8947 ScreenLines2 + LineOffset[old_row],
8948 (size_t)len * sizeof(schar_T));
8949#endif
8950 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8951 ScreenAttrs + LineOffset[old_row],
8952 (size_t)len * sizeof(sattr_T));
8953 }
8954 }
8955 }
8956 /* Use the last line of the screen for the current line. */
8957 current_ScreenLine = new_ScreenLines + Rows * Columns;
8958 }
8959
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008960 free_screenlines();
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 ScreenLines = new_ScreenLines;
8963#ifdef FEAT_MBYTE
8964 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008965 for (i = 0; i < p_mco; ++i)
8966 ScreenLinesC[i] = new_ScreenLinesC[i];
8967 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 ScreenLines2 = new_ScreenLines2;
8969#endif
8970 ScreenAttrs = new_ScreenAttrs;
8971 LineOffset = new_LineOffset;
8972 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008973#ifdef FEAT_WINDOWS
8974 TabPageIdxs = new_TabPageIdxs;
8975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
8977 /* It's important that screen_Rows and screen_Columns reflect the actual
8978 * size of ScreenLines[]. Set them before calling anything. */
8979#ifdef FEAT_GUI
8980 old_Rows = screen_Rows;
8981#endif
8982 screen_Rows = Rows;
8983 screen_Columns = Columns;
8984
8985 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008986 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 screenclear2();
8988
8989#ifdef FEAT_GUI
8990 else if (gui.in_use
8991 && !gui.starting
8992 && ScreenLines != NULL
8993 && old_Rows != Rows)
8994 {
8995 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8996 /*
8997 * Adjust the position of the cursor, for when executing an external
8998 * command.
8999 */
9000 if (msg_row >= Rows) /* Rows got smaller */
9001 msg_row = Rows - 1; /* put cursor at last row */
9002 else if (Rows > old_Rows) /* Rows got bigger */
9003 msg_row += Rows - old_Rows; /* put cursor in same place */
9004 if (msg_col >= Columns) /* Columns got smaller */
9005 msg_col = Columns - 1; /* put cursor at last column */
9006 }
9007#endif
9008
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009010 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009011
9012#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009013 /*
9014 * Do not apply autocommands more than 3 times to avoid an endless loop
9015 * in case applying autocommands always changes Rows or Columns.
9016 */
9017 if (starting == 0 && ++retry_count <= 3)
9018 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009019 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009020 /* In rare cases, autocommands may have altered Rows or Columns,
9021 * jump back to check if we need to allocate the screen again. */
9022 goto retry;
9023 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025}
9026
9027 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009028free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009029{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009030#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009031 int i;
9032
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009033 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009034 for (i = 0; i < Screen_mco; ++i)
9035 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009036 vim_free(ScreenLines2);
9037#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009038 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009039 vim_free(ScreenAttrs);
9040 vim_free(LineOffset);
9041 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009042#ifdef FEAT_WINDOWS
9043 vim_free(TabPageIdxs);
9044#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009045}
9046
9047 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009048screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049{
9050 check_for_delay(FALSE);
9051 screenalloc(FALSE); /* allocate screen buffers if size changed */
9052 screenclear2(); /* clear the screen */
9053}
9054
9055 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009056screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 int i;
9059
9060 if (starting == NO_SCREEN || ScreenLines == NULL
9061#ifdef FEAT_GUI
9062 || (gui.in_use && gui.starting)
9063#endif
9064 )
9065 return;
9066
9067#ifdef FEAT_GUI
9068 if (!gui.in_use)
9069#endif
9070 screen_attr = -1; /* force setting the Normal colors */
9071 screen_stop_highlight(); /* don't want highlighting here */
9072
9073#ifdef FEAT_CLIPBOARD
9074 /* disable selection without redrawing it */
9075 clip_scroll_selection(9999);
9076#endif
9077
9078 /* blank out ScreenLines */
9079 for (i = 0; i < Rows; ++i)
9080 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009081 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 LineWraps[i] = FALSE;
9083 }
9084
9085 if (can_clear(T_CL))
9086 {
9087 out_str(T_CL); /* clear the display */
9088 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009089 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091 else
9092 {
9093 /* can't clear the screen, mark all chars with invalid attributes */
9094 for (i = 0; i < Rows; ++i)
9095 lineinvalid(LineOffset[i], (int)Columns);
9096 clear_cmdline = TRUE;
9097 }
9098
9099 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9100
9101 win_rest_invalid(firstwin);
9102 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009103#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009104 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (must_redraw == CLEAR) /* no need to clear again */
9107 must_redraw = NOT_VALID;
9108 compute_cmdrow();
9109 msg_row = cmdline_row; /* put cursor on last line for messages */
9110 msg_col = 0;
9111 screen_start(); /* don't know where cursor is now */
9112 msg_scrolled = 0; /* can't scroll back */
9113 msg_didany = FALSE;
9114 msg_didout = FALSE;
9115}
9116
9117/*
9118 * Clear one line in ScreenLines.
9119 */
9120 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009121lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9124#ifdef FEAT_MBYTE
9125 if (enc_utf8)
9126 (void)vim_memset(ScreenLinesUC + off, 0,
9127 (size_t)width * sizeof(u8char_T));
9128#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009129 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
9132/*
9133 * Mark one line in ScreenLines invalid by setting the attributes to an
9134 * invalid value.
9135 */
9136 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009137lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9140}
9141
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009142#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143/*
9144 * Copy part of a Screenline for vertically split window "wp".
9145 */
9146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009147linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148{
9149 unsigned off_to = LineOffset[to] + wp->w_wincol;
9150 unsigned off_from = LineOffset[from] + wp->w_wincol;
9151
9152 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9153 wp->w_width * sizeof(schar_T));
9154# ifdef FEAT_MBYTE
9155 if (enc_utf8)
9156 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009157 int i;
9158
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9160 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009161 for (i = 0; i < p_mco; ++i)
9162 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9163 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
9165 if (enc_dbcs == DBCS_JPNU)
9166 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9167 wp->w_width * sizeof(schar_T));
9168# endif
9169 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9170 wp->w_width * sizeof(sattr_T));
9171}
9172#endif
9173
9174/*
9175 * Return TRUE if clearing with term string "p" would work.
9176 * It can't work when the string is empty or it won't set the right background.
9177 */
9178 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009179can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180{
9181 return (*p != NUL && (t_colors <= 1
9182#ifdef FEAT_GUI
9183 || gui.in_use
9184#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009185#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009186 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009187 || (!p_tgc && cterm_normal_bg_color == 0)
9188#else
9189 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009190#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009191 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
9195 * Reset cursor position. Use whenever cursor was moved because of outputting
9196 * something directly to the screen (shell commands) or a terminal control
9197 * code.
9198 */
9199 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009200screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
9202 screen_cur_row = screen_cur_col = 9999;
9203}
9204
9205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 * Move the cursor to position "row","col" in the screen.
9207 * This tries to find the most efficient way to move, minimizing the number of
9208 * characters sent to the terminal.
9209 */
9210 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009211windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009213 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 int i;
9215 int plan;
9216 int cost;
9217 int wouldbe_col;
9218 int noinvcurs;
9219 char_u *bs;
9220 int goto_cost;
9221 int attr;
9222
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009223#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9225
9226#define PLAN_LE 1
9227#define PLAN_CR 2
9228#define PLAN_NL 3
9229#define PLAN_WRITE 4
9230 /* Can't use ScreenLines unless initialized */
9231 if (ScreenLines == NULL)
9232 return;
9233
9234 if (col != screen_cur_col || row != screen_cur_row)
9235 {
9236 /* Check for valid position. */
9237 if (row < 0) /* window without text lines? */
9238 row = 0;
9239 if (row >= screen_Rows)
9240 row = screen_Rows - 1;
9241 if (col >= screen_Columns)
9242 col = screen_Columns - 1;
9243
9244 /* check if no cursor movement is allowed in highlight mode */
9245 if (screen_attr && *T_MS == NUL)
9246 noinvcurs = HIGHL_COST;
9247 else
9248 noinvcurs = 0;
9249 goto_cost = GOTO_COST + noinvcurs;
9250
9251 /*
9252 * Plan how to do the positioning:
9253 * 1. Use CR to move it to column 0, same row.
9254 * 2. Use T_LE to move it a few columns to the left.
9255 * 3. Use NL to move a few lines down, column 0.
9256 * 4. Move a few columns to the right with T_ND or by writing chars.
9257 *
9258 * Don't do this if the cursor went beyond the last column, the cursor
9259 * position is unknown then (some terminals wrap, some don't )
9260 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009261 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 * characters to move the cursor to the right.
9263 */
9264 if (row >= screen_cur_row && screen_cur_col < Columns)
9265 {
9266 /*
9267 * If the cursor is in the same row, bigger col, we can use CR
9268 * or T_LE.
9269 */
9270 bs = NULL; /* init for GCC */
9271 attr = screen_attr;
9272 if (row == screen_cur_row && col < screen_cur_col)
9273 {
9274 /* "le" is preferred over "bc", because "bc" is obsolete */
9275 if (*T_LE)
9276 bs = T_LE; /* "cursor left" */
9277 else
9278 bs = T_BC; /* "backspace character (old) */
9279 if (*bs)
9280 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9281 else
9282 cost = 999;
9283 if (col + 1 < cost) /* using CR is less characters */
9284 {
9285 plan = PLAN_CR;
9286 wouldbe_col = 0;
9287 cost = 1; /* CR is just one character */
9288 }
9289 else
9290 {
9291 plan = PLAN_LE;
9292 wouldbe_col = col;
9293 }
9294 if (noinvcurs) /* will stop highlighting */
9295 {
9296 cost += noinvcurs;
9297 attr = 0;
9298 }
9299 }
9300
9301 /*
9302 * If the cursor is above where we want to be, we can use CR LF.
9303 */
9304 else if (row > screen_cur_row)
9305 {
9306 plan = PLAN_NL;
9307 wouldbe_col = 0;
9308 cost = (row - screen_cur_row) * 2; /* CR LF */
9309 if (noinvcurs) /* will stop highlighting */
9310 {
9311 cost += noinvcurs;
9312 attr = 0;
9313 }
9314 }
9315
9316 /*
9317 * If the cursor is in the same row, smaller col, just use write.
9318 */
9319 else
9320 {
9321 plan = PLAN_WRITE;
9322 wouldbe_col = screen_cur_col;
9323 cost = 0;
9324 }
9325
9326 /*
9327 * Check if any characters that need to be written have the
9328 * correct attributes. Also avoid UTF-8 characters.
9329 */
9330 i = col - wouldbe_col;
9331 if (i > 0)
9332 cost += i;
9333 if (cost < goto_cost && i > 0)
9334 {
9335 /*
9336 * Check if the attributes are correct without additionally
9337 * stopping highlighting.
9338 */
9339 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9340 while (i && *p++ == attr)
9341 --i;
9342 if (i != 0)
9343 {
9344 /*
9345 * Try if it works when highlighting is stopped here.
9346 */
9347 if (*--p == 0)
9348 {
9349 cost += noinvcurs;
9350 while (i && *p++ == 0)
9351 --i;
9352 }
9353 if (i != 0)
9354 cost = 999; /* different attributes, don't do it */
9355 }
9356#ifdef FEAT_MBYTE
9357 if (enc_utf8)
9358 {
9359 /* Don't use an UTF-8 char for positioning, it's slow. */
9360 for (i = wouldbe_col; i < col; ++i)
9361 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9362 {
9363 cost = 999;
9364 break;
9365 }
9366 }
9367#endif
9368 }
9369
9370 /*
9371 * We can do it without term_windgoto()!
9372 */
9373 if (cost < goto_cost)
9374 {
9375 if (plan == PLAN_LE)
9376 {
9377 if (noinvcurs)
9378 screen_stop_highlight();
9379 while (screen_cur_col > col)
9380 {
9381 out_str(bs);
9382 --screen_cur_col;
9383 }
9384 }
9385 else if (plan == PLAN_CR)
9386 {
9387 if (noinvcurs)
9388 screen_stop_highlight();
9389 out_char('\r');
9390 screen_cur_col = 0;
9391 }
9392 else if (plan == PLAN_NL)
9393 {
9394 if (noinvcurs)
9395 screen_stop_highlight();
9396 while (screen_cur_row < row)
9397 {
9398 out_char('\n');
9399 ++screen_cur_row;
9400 }
9401 screen_cur_col = 0;
9402 }
9403
9404 i = col - screen_cur_col;
9405 if (i > 0)
9406 {
9407 /*
9408 * Use cursor-right if it's one character only. Avoids
9409 * removing a line of pixels from the last bold char, when
9410 * using the bold trick in the GUI.
9411 */
9412 if (T_ND[0] != NUL && T_ND[1] == NUL)
9413 {
9414 while (i-- > 0)
9415 out_char(*T_ND);
9416 }
9417 else
9418 {
9419 int off;
9420
9421 off = LineOffset[row] + screen_cur_col;
9422 while (i-- > 0)
9423 {
9424 if (ScreenAttrs[off] != screen_attr)
9425 screen_stop_highlight();
9426#ifdef FEAT_MBYTE
9427 out_flush_check();
9428#endif
9429 out_char(ScreenLines[off]);
9430#ifdef FEAT_MBYTE
9431 if (enc_dbcs == DBCS_JPNU
9432 && ScreenLines[off] == 0x8e)
9433 out_char(ScreenLines2[off]);
9434#endif
9435 ++off;
9436 }
9437 }
9438 }
9439 }
9440 }
9441 else
9442 cost = 999;
9443
9444 if (cost >= goto_cost)
9445 {
9446 if (noinvcurs)
9447 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009448 if (row == screen_cur_row && (col > screen_cur_col)
9449 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 term_cursor_right(col - screen_cur_col);
9451 else
9452 term_windgoto(row, col);
9453 }
9454 screen_cur_row = row;
9455 screen_cur_col = col;
9456 }
9457}
9458
9459/*
9460 * Set cursor to its position in the current window.
9461 */
9462 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009463setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 if (redrawing())
9466 {
9467 validate_cursor();
9468 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9469 W_WINCOL(curwin) + (
9470#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009471 /* With 'rightleft' set and the cursor on a double-wide
9472 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9474# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009475 (has_mbyte
9476 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9477 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478# endif
9479 1)) :
9480#endif
9481 curwin->w_wcol));
9482 }
9483}
9484
9485
9486/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009487 * Insert 'line_count' lines at 'row' in window 'wp'.
9488 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9489 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 * scrolling.
9491 * Returns FAIL if the lines are not inserted, OK for success.
9492 */
9493 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009494win_ins_lines(
9495 win_T *wp,
9496 int row,
9497 int line_count,
9498 int invalid,
9499 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 int did_delete;
9502 int nextrow;
9503 int lastrow;
9504 int retval;
9505
9506 if (invalid)
9507 wp->w_lines_valid = 0;
9508
9509 if (wp->w_height < 5)
9510 return FAIL;
9511
9512 if (line_count > wp->w_height - row)
9513 line_count = wp->w_height - row;
9514
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009515 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 if (retval != MAYBE)
9517 return retval;
9518
9519 /*
9520 * If there is a next window or a status line, we first try to delete the
9521 * lines at the bottom to avoid messing what is after the window.
9522 * If this fails and there are following windows, don't do anything to avoid
9523 * messing up those windows, better just redraw.
9524 */
9525 did_delete = FALSE;
9526#ifdef FEAT_WINDOWS
9527 if (wp->w_next != NULL || wp->w_status_height)
9528 {
9529 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009530 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 did_delete = TRUE;
9532 else if (wp->w_next)
9533 return FAIL;
9534 }
9535#endif
9536 /*
9537 * if no lines deleted, blank the lines that will end up below the window
9538 */
9539 if (!did_delete)
9540 {
9541#ifdef FEAT_WINDOWS
9542 wp->w_redr_status = TRUE;
9543#endif
9544 redraw_cmdline = TRUE;
9545 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9546 lastrow = nextrow + line_count;
9547 if (lastrow > Rows)
9548 lastrow = Rows;
9549 screen_fill(nextrow - line_count, lastrow - line_count,
9550 W_WINCOL(wp), (int)W_ENDCOL(wp),
9551 ' ', ' ', 0);
9552 }
9553
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009554 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 == FAIL)
9556 {
9557 /* deletion will have messed up other windows */
9558 if (did_delete)
9559 {
9560#ifdef FEAT_WINDOWS
9561 wp->w_redr_status = TRUE;
9562#endif
9563 win_rest_invalid(W_NEXT(wp));
9564 }
9565 return FAIL;
9566 }
9567
9568 return OK;
9569}
9570
9571/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009572 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9574 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9575 * scrolling
9576 * Return OK for success, FAIL if the lines are not deleted.
9577 */
9578 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009579win_del_lines(
9580 win_T *wp,
9581 int row,
9582 int line_count,
9583 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009584 int mayclear,
9585 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 int retval;
9588
9589 if (invalid)
9590 wp->w_lines_valid = 0;
9591
9592 if (line_count > wp->w_height - row)
9593 line_count = wp->w_height - row;
9594
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009595 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 if (retval != MAYBE)
9597 return retval;
9598
9599 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009600 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 return FAIL;
9602
9603#ifdef FEAT_WINDOWS
9604 /*
9605 * If there are windows or status lines below, try to put them at the
9606 * correct place. If we can't do that, they have to be redrawn.
9607 */
9608 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9609 {
9610 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009611 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 {
9613 wp->w_redr_status = TRUE;
9614 win_rest_invalid(wp->w_next);
9615 }
9616 }
9617 /*
9618 * If this is the last window and there is no status line, redraw the
9619 * command line later.
9620 */
9621 else
9622#endif
9623 redraw_cmdline = TRUE;
9624 return OK;
9625}
9626
9627/*
9628 * Common code for win_ins_lines() and win_del_lines().
9629 * Returns OK or FAIL when the work has been done.
9630 * Returns MAYBE when not finished yet.
9631 */
9632 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009633win_do_lines(
9634 win_T *wp,
9635 int row,
9636 int line_count,
9637 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009638 int del,
9639 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
9641 int retval;
9642
9643 if (!redrawing() || line_count <= 0)
9644 return FAIL;
9645
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009646 /* When inserting lines would result in loss of command output, just redraw
9647 * the lines. */
9648 if (no_win_do_lines_ins && !del)
9649 return FAIL;
9650
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 /* only a few lines left: redraw is faster */
9652 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009653#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 && wp->w_width == Columns
9655#endif
9656 )
9657 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009658 if (!no_win_do_lines_ins)
9659 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 return FAIL;
9661 }
9662
9663 /*
9664 * Delete all remaining lines
9665 */
9666 if (row + line_count >= wp->w_height)
9667 {
9668 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9669 W_WINCOL(wp), (int)W_ENDCOL(wp),
9670 ' ', ' ', 0);
9671 return OK;
9672 }
9673
9674 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009675 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009677 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009679 if (!no_win_do_lines_ins)
9680 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681
9682 /*
9683 * If the terminal can set a scroll region, use that.
9684 * Always do this in a vertically split window. This will redraw from
9685 * ScreenLines[] when t_CV isn't defined. That's faster than using
9686 * win_line().
9687 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009688 * a character in the lower right corner of the scroll region may cause a
9689 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 */
9691 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009692#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 || W_WIDTH(wp) != Columns
9694#endif
9695 )
9696 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009697#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9699#endif
9700 scroll_region_set(wp, row);
9701 if (del)
9702 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009703 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 else
9705 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009706 wp->w_height - row, clear_attr, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009707#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9709#endif
9710 scroll_region_reset();
9711 return retval;
9712 }
9713
9714#ifdef FEAT_WINDOWS
9715 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9716 return FAIL;
9717#endif
9718
9719 return MAYBE;
9720}
9721
9722/*
9723 * window 'wp' and everything after it is messed up, mark it for redraw
9724 */
9725 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009726win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
9728#ifdef FEAT_WINDOWS
9729 while (wp != NULL)
9730#else
9731 if (wp != NULL)
9732#endif
9733 {
9734 redraw_win_later(wp, NOT_VALID);
9735#ifdef FEAT_WINDOWS
9736 wp->w_redr_status = TRUE;
9737 wp = wp->w_next;
9738#endif
9739 }
9740 redraw_cmdline = TRUE;
9741}
9742
9743/*
9744 * The rest of the routines in this file perform screen manipulations. The
9745 * given operation is performed physically on the screen. The corresponding
9746 * change is also made to the internal screen image. In this way, the editor
9747 * anticipates the effect of editing changes on the appearance of the screen.
9748 * That way, when we call screenupdate a complete redraw isn't usually
9749 * necessary. Another advantage is that we can keep adding code to anticipate
9750 * screen changes, and in the meantime, everything still works.
9751 */
9752
9753/*
9754 * types for inserting or deleting lines
9755 */
9756#define USE_T_CAL 1
9757#define USE_T_CDL 2
9758#define USE_T_AL 3
9759#define USE_T_CE 4
9760#define USE_T_DL 5
9761#define USE_T_SR 6
9762#define USE_NL 7
9763#define USE_T_CD 8
9764#define USE_REDRAW 9
9765
9766/*
9767 * insert lines on the screen and update ScreenLines[]
9768 * 'end' is the line after the scrolled part. Normally it is Rows.
9769 * When scrolling region used 'off' is the offset from the top for the region.
9770 * 'row' and 'end' are relative to the start of the region.
9771 *
9772 * return FAIL for failure, OK for success.
9773 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009774 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009775screen_ins_lines(
9776 int off,
9777 int row,
9778 int line_count,
9779 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009780 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009781 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 int i;
9784 int j;
9785 unsigned temp;
9786 int cursor_row;
9787 int type;
9788 int result_empty;
9789 int can_ce = can_clear(T_CE);
9790
9791 /*
9792 * FAIL if
9793 * - there is no valid screen
9794 * - the screen has to be redrawn completely
9795 * - the line count is less than one
9796 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009797 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009799 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9800#ifdef FEAT_CLIPBOARD
9801 || (clip_star.state != SELECT_CLEARED
9802 && redrawing_for_callback > 0)
9803#endif
9804 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 return FAIL;
9806
9807 /*
9808 * There are seven ways to insert lines:
9809 * 0. When in a vertically split window and t_CV isn't set, redraw the
9810 * characters from ScreenLines[].
9811 * 1. Use T_CD (clear to end of display) if it exists and the result of
9812 * the insert is just empty lines
9813 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9814 * present or line_count > 1. It looks better if we do all the inserts
9815 * at once.
9816 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9817 * insert is just empty lines and T_CE is not present or line_count >
9818 * 1.
9819 * 4. Use T_AL (insert line) if it exists.
9820 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9821 * just empty lines.
9822 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9823 * just empty lines.
9824 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9825 * the 'da' flag is not set or we have clear line capability.
9826 * 8. redraw the characters from ScreenLines[].
9827 *
9828 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9829 * the scrollbar for the window. It does have insert line, use that if it
9830 * exists.
9831 */
9832 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009833#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9835 type = USE_REDRAW;
9836 else
9837#endif
9838 if (can_clear(T_CD) && result_empty)
9839 type = USE_T_CD;
9840 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9841 type = USE_T_CAL;
9842 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9843 type = USE_T_CDL;
9844 else if (*T_AL != NUL)
9845 type = USE_T_AL;
9846 else if (can_ce && result_empty)
9847 type = USE_T_CE;
9848 else if (*T_DL != NUL && result_empty)
9849 type = USE_T_DL;
9850 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9851 type = USE_T_SR;
9852 else
9853 return FAIL;
9854
9855 /*
9856 * For clearing the lines screen_del_lines() is used. This will also take
9857 * care of t_db if necessary.
9858 */
9859 if (type == USE_T_CD || type == USE_T_CDL ||
9860 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009861 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862
9863 /*
9864 * If text is retained below the screen, first clear or delete as many
9865 * lines at the bottom of the window as are about to be inserted so that
9866 * the deleted lines won't later surface during a screen_del_lines.
9867 */
9868 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009869 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870
9871#ifdef FEAT_CLIPBOARD
9872 /* Remove a modeless selection when inserting lines halfway the screen
9873 * or not the full width of the screen. */
9874 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009875# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 || (wp != NULL && wp->w_width != Columns)
9877# endif
9878 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009879 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 else
9881 clip_scroll_selection(-line_count);
9882#endif
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884#ifdef FEAT_GUI
9885 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9886 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009887 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888#endif
9889
9890 if (*T_CCS != NUL) /* cursor relative to region */
9891 cursor_row = row;
9892 else
9893 cursor_row = row + off;
9894
9895 /*
9896 * Shift LineOffset[] line_count down to reflect the inserted lines.
9897 * Clear the inserted lines in ScreenLines[].
9898 */
9899 row += off;
9900 end += off;
9901 for (i = 0; i < line_count; ++i)
9902 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009903#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (wp != NULL && wp->w_width != Columns)
9905 {
9906 /* need to copy part of a line */
9907 j = end - 1 - i;
9908 while ((j -= line_count) >= row)
9909 linecopy(j + line_count, j, wp);
9910 j += line_count;
9911 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009912 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9913 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 else
9915 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9916 LineWraps[j] = FALSE;
9917 }
9918 else
9919#endif
9920 {
9921 j = end - 1 - i;
9922 temp = LineOffset[j];
9923 while ((j -= line_count) >= row)
9924 {
9925 LineOffset[j + line_count] = LineOffset[j];
9926 LineWraps[j + line_count] = LineWraps[j];
9927 }
9928 LineOffset[j + line_count] = temp;
9929 LineWraps[j + line_count] = FALSE;
9930 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009931 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 else
9933 lineinvalid(temp, (int)Columns);
9934 }
9935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
9937 screen_stop_highlight();
9938 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009939 if (clear_attr != 0)
9940 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009942#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 /* redraw the characters */
9944 if (type == USE_REDRAW)
9945 redraw_block(row, end, wp);
9946 else
9947#endif
9948 if (type == USE_T_CAL)
9949 {
9950 term_append_lines(line_count);
9951 screen_start(); /* don't know where cursor is now */
9952 }
9953 else
9954 {
9955 for (i = 0; i < line_count; i++)
9956 {
9957 if (type == USE_T_AL)
9958 {
9959 if (i && cursor_row != 0)
9960 windgoto(cursor_row, 0);
9961 out_str(T_AL);
9962 }
9963 else /* type == USE_T_SR */
9964 out_str(T_SR);
9965 screen_start(); /* don't know where cursor is now */
9966 }
9967 }
9968
9969 /*
9970 * With scroll-reverse and 'da' flag set we need to clear the lines that
9971 * have been scrolled down into the region.
9972 */
9973 if (type == USE_T_SR && *T_DA)
9974 {
9975 for (i = 0; i < line_count; ++i)
9976 {
9977 windgoto(off + i, 0);
9978 out_str(T_CE);
9979 screen_start(); /* don't know where cursor is now */
9980 }
9981 }
9982
9983#ifdef FEAT_GUI
9984 gui_can_update_cursor();
9985 if (gui.in_use)
9986 out_flush(); /* always flush after a scroll */
9987#endif
9988 return OK;
9989}
9990
9991/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009992 * Delete lines on the screen and update ScreenLines[].
9993 * "end" is the line after the scrolled part. Normally it is Rows.
9994 * When scrolling region used "off" is the offset from the top for the region.
9995 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 *
9997 * Return OK for success, FAIL if the lines are not deleted.
9998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010000screen_del_lines(
10001 int off,
10002 int row,
10003 int line_count,
10004 int end,
10005 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010006 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010007 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008{
10009 int j;
10010 int i;
10011 unsigned temp;
10012 int cursor_row;
10013 int cursor_end;
10014 int result_empty; /* result is empty until end of region */
10015 int can_delete; /* deleting line codes can be used */
10016 int type;
10017
10018 /*
10019 * FAIL if
10020 * - there is no valid screen
10021 * - the screen has to be redrawn completely
10022 * - the line count is less than one
10023 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010024 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010026 if (!screen_valid(TRUE) || line_count <= 0
10027 || (!force && line_count > p_ttyscroll)
10028#ifdef FEAT_CLIPBOARD
10029 || (clip_star.state != SELECT_CLEARED
10030 && redrawing_for_callback > 0)
10031#endif
10032 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 return FAIL;
10034
10035 /*
10036 * Check if the rest of the current region will become empty.
10037 */
10038 result_empty = row + line_count >= end;
10039
10040 /*
10041 * We can delete lines only when 'db' flag not set or when 'ce' option
10042 * available.
10043 */
10044 can_delete = (*T_DB == NUL || can_clear(T_CE));
10045
10046 /*
10047 * There are six ways to delete lines:
10048 * 0. When in a vertically split window and t_CV isn't set, redraw the
10049 * characters from ScreenLines[].
10050 * 1. Use T_CD if it exists and the result is empty.
10051 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10052 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10053 * none of the other ways work.
10054 * 4. Use T_CE (erase line) if the result is empty.
10055 * 5. Use T_DL (delete line) if it exists.
10056 * 6. redraw the characters from ScreenLines[].
10057 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010058#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10060 type = USE_REDRAW;
10061 else
10062#endif
10063 if (can_clear(T_CD) && result_empty)
10064 type = USE_T_CD;
10065#if defined(__BEOS__) && defined(BEOS_DR8)
10066 /*
10067 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10068 * its internal termcap... this works okay for tests which test *T_DB !=
10069 * NUL. It has the disadvantage that the user cannot use any :set t_*
10070 * command to get T_DB (back) to empty_option, only :set term=... will do
10071 * the trick...
10072 * Anyway, this hack will hopefully go away with the next OS release.
10073 * (Olaf Seibert)
10074 */
10075 else if (row == 0 && T_DB == empty_option
10076 && (line_count == 1 || *T_CDL == NUL))
10077#else
10078 else if (row == 0 && (
10079#ifndef AMIGA
10080 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10081 * up, so use delete-line command */
10082 line_count == 1 ||
10083#endif
10084 *T_CDL == NUL))
10085#endif
10086 type = USE_NL;
10087 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10088 type = USE_T_CDL;
10089 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010090#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 && (wp == NULL || wp->w_width == Columns)
10092#endif
10093 )
10094 type = USE_T_CE;
10095 else if (*T_DL != NUL && can_delete)
10096 type = USE_T_DL;
10097 else if (*T_CDL != NUL && can_delete)
10098 type = USE_T_CDL;
10099 else
10100 return FAIL;
10101
10102#ifdef FEAT_CLIPBOARD
10103 /* Remove a modeless selection when deleting lines halfway the screen or
10104 * not the full width of the screen. */
10105 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010106# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 || (wp != NULL && wp->w_width != Columns)
10108# endif
10109 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010110 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 else
10112 clip_scroll_selection(line_count);
10113#endif
10114
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115#ifdef FEAT_GUI
10116 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10117 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010118 gui_dont_update_cursor(gui.cursor_row >= row + off
10119 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#endif
10121
10122 if (*T_CCS != NUL) /* cursor relative to region */
10123 {
10124 cursor_row = row;
10125 cursor_end = end;
10126 }
10127 else
10128 {
10129 cursor_row = row + off;
10130 cursor_end = end + off;
10131 }
10132
10133 /*
10134 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10135 * Clear the inserted lines in ScreenLines[].
10136 */
10137 row += off;
10138 end += off;
10139 for (i = 0; i < line_count; ++i)
10140 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010141#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (wp != NULL && wp->w_width != Columns)
10143 {
10144 /* need to copy part of a line */
10145 j = row + i;
10146 while ((j += line_count) <= end - 1)
10147 linecopy(j - line_count, j, wp);
10148 j -= line_count;
10149 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010150 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10151 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 else
10153 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10154 LineWraps[j] = FALSE;
10155 }
10156 else
10157#endif
10158 {
10159 /* whole width, moving the line pointers is faster */
10160 j = row + i;
10161 temp = LineOffset[j];
10162 while ((j += line_count) <= end - 1)
10163 {
10164 LineOffset[j - line_count] = LineOffset[j];
10165 LineWraps[j - line_count] = LineWraps[j];
10166 }
10167 LineOffset[j - line_count] = temp;
10168 LineWraps[j - line_count] = FALSE;
10169 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010170 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 else
10172 lineinvalid(temp, (int)Columns);
10173 }
10174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010176 if (screen_attr != clear_attr)
10177 screen_stop_highlight();
10178 if (clear_attr != 0)
10179 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010181#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 /* redraw the characters */
10183 if (type == USE_REDRAW)
10184 redraw_block(row, end, wp);
10185 else
10186#endif
10187 if (type == USE_T_CD) /* delete the lines */
10188 {
10189 windgoto(cursor_row, 0);
10190 out_str(T_CD);
10191 screen_start(); /* don't know where cursor is now */
10192 }
10193 else if (type == USE_T_CDL)
10194 {
10195 windgoto(cursor_row, 0);
10196 term_delete_lines(line_count);
10197 screen_start(); /* don't know where cursor is now */
10198 }
10199 /*
10200 * Deleting lines at top of the screen or scroll region: Just scroll
10201 * the whole screen (scroll region) up by outputting newlines on the
10202 * last line.
10203 */
10204 else if (type == USE_NL)
10205 {
10206 windgoto(cursor_end - 1, 0);
10207 for (i = line_count; --i >= 0; )
10208 out_char('\n'); /* cursor will remain on same line */
10209 }
10210 else
10211 {
10212 for (i = line_count; --i >= 0; )
10213 {
10214 if (type == USE_T_DL)
10215 {
10216 windgoto(cursor_row, 0);
10217 out_str(T_DL); /* delete a line */
10218 }
10219 else /* type == USE_T_CE */
10220 {
10221 windgoto(cursor_row + i, 0);
10222 out_str(T_CE); /* erase a line */
10223 }
10224 screen_start(); /* don't know where cursor is now */
10225 }
10226 }
10227
10228 /*
10229 * If the 'db' flag is set, we need to clear the lines that have been
10230 * scrolled up at the bottom of the region.
10231 */
10232 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10233 {
10234 for (i = line_count; i > 0; --i)
10235 {
10236 windgoto(cursor_end - i, 0);
10237 out_str(T_CE); /* erase a line */
10238 screen_start(); /* don't know where cursor is now */
10239 }
10240 }
10241
10242#ifdef FEAT_GUI
10243 gui_can_update_cursor();
10244 if (gui.in_use)
10245 out_flush(); /* always flush after a scroll */
10246#endif
10247
10248 return OK;
10249}
10250
10251/*
10252 * show the current mode and ruler
10253 *
10254 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10255 * If clear_cmdline is FALSE there may be a message there that needs to be
10256 * cleared only if a mode is shown.
10257 * Return the length of the message (0 if no message).
10258 */
10259 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010260showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261{
10262 int need_clear;
10263 int length = 0;
10264 int do_mode;
10265 int attr;
10266 int nwr_save;
10267#ifdef FEAT_INS_EXPAND
10268 int sub_attr;
10269#endif
10270
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010271 do_mode = ((p_smd && msg_silent == 0)
10272 && ((State & INSERT)
10273 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010274 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 if (do_mode || Recording)
10276 {
10277 /*
10278 * Don't show mode right now, when not redrawing or inside a mapping.
10279 * Call char_avail() only when we are going to show something, because
10280 * it takes a bit of time.
10281 */
10282 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10283 {
10284 redraw_cmdline = TRUE; /* show mode later */
10285 return 0;
10286 }
10287
10288 nwr_save = need_wait_return;
10289
10290 /* wait a bit before overwriting an important message */
10291 check_for_delay(FALSE);
10292
10293 /* if the cmdline is more than one line high, erase top lines */
10294 need_clear = clear_cmdline;
10295 if (clear_cmdline && cmdline_row < Rows - 1)
10296 msg_clr_cmdline(); /* will reset clear_cmdline */
10297
10298 /* Position on the last line in the window, column 0 */
10299 msg_pos_mode();
10300 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010301 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 if (do_mode)
10303 {
10304 MSG_PUTS_ATTR("--", attr);
10305#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010306 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010307# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010308 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010309# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010310 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010311# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010312 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010313# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 MSG_PUTS_ATTR(" IM", attr);
10315# else
10316 MSG_PUTS_ATTR(" XIM", attr);
10317# endif
10318#endif
10319#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10320 if (gui.in_use)
10321 {
10322 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010323 {
10324 /* HANGUL */
10325 if (enc_utf8)
10326 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10327 else
10328 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331#endif
10332#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010333 /* CTRL-X in Insert mode */
10334 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 {
10336 /* These messages can get long, avoid a wrap in a narrow
10337 * window. Prefer showing edit_submode_extra. */
10338 length = (Rows - msg_row) * Columns - 3;
10339 if (edit_submode_extra != NULL)
10340 length -= vim_strsize(edit_submode_extra);
10341 if (length > 0)
10342 {
10343 if (edit_submode_pre != NULL)
10344 length -= vim_strsize(edit_submode_pre);
10345 if (length - vim_strsize(edit_submode) > 0)
10346 {
10347 if (edit_submode_pre != NULL)
10348 msg_puts_attr(edit_submode_pre, attr);
10349 msg_puts_attr(edit_submode, attr);
10350 }
10351 if (edit_submode_extra != NULL)
10352 {
10353 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10354 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010355 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 else
10357 sub_attr = attr;
10358 msg_puts_attr(edit_submode_extra, sub_attr);
10359 }
10360 }
10361 length = 0;
10362 }
10363 else
10364#endif
10365 {
10366#ifdef FEAT_VREPLACE
10367 if (State & VREPLACE_FLAG)
10368 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10369 else
10370#endif
10371 if (State & REPLACE_FLAG)
10372 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10373 else if (State & INSERT)
10374 {
10375#ifdef FEAT_RIGHTLEFT
10376 if (p_ri)
10377 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10378#endif
10379 MSG_PUTS_ATTR(_(" INSERT"), attr);
10380 }
10381 else if (restart_edit == 'I')
10382 MSG_PUTS_ATTR(_(" (insert)"), attr);
10383 else if (restart_edit == 'R')
10384 MSG_PUTS_ATTR(_(" (replace)"), attr);
10385 else if (restart_edit == 'V')
10386 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10387#ifdef FEAT_RIGHTLEFT
10388 if (p_hkmap)
10389 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10390# ifdef FEAT_FKMAP
10391 if (p_fkmap)
10392 MSG_PUTS_ATTR(farsi_text_5, attr);
10393# endif
10394#endif
10395#ifdef FEAT_KEYMAP
10396 if (State & LANGMAP)
10397 {
10398# ifdef FEAT_ARABIC
10399 if (curwin->w_p_arab)
10400 MSG_PUTS_ATTR(_(" Arabic"), attr);
10401 else
10402# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010403 if (get_keymap_str(curwin, (char_u *)" (%s)",
10404 NameBuff, MAXPATHL))
10405 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407#endif
10408 if ((State & INSERT) && p_paste)
10409 MSG_PUTS_ATTR(_(" (paste)"), attr);
10410
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 if (VIsual_active)
10412 {
10413 char *p;
10414
10415 /* Don't concatenate separate words to avoid translation
10416 * problems. */
10417 switch ((VIsual_select ? 4 : 0)
10418 + (VIsual_mode == Ctrl_V) * 2
10419 + (VIsual_mode == 'V'))
10420 {
10421 case 0: p = N_(" VISUAL"); break;
10422 case 1: p = N_(" VISUAL LINE"); break;
10423 case 2: p = N_(" VISUAL BLOCK"); break;
10424 case 4: p = N_(" SELECT"); break;
10425 case 5: p = N_(" SELECT LINE"); break;
10426 default: p = N_(" SELECT BLOCK"); break;
10427 }
10428 MSG_PUTS_ATTR(_(p), attr);
10429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 MSG_PUTS_ATTR(" --", attr);
10431 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010432
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 need_clear = TRUE;
10434 }
10435 if (Recording
10436#ifdef FEAT_INS_EXPAND
10437 && edit_submode == NULL /* otherwise it gets too long */
10438#endif
10439 )
10440 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010441 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 need_clear = TRUE;
10443 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010444
10445 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (need_clear || clear_cmdline)
10447 msg_clr_eos();
10448 msg_didout = FALSE; /* overwrite this message */
10449 length = msg_col;
10450 msg_col = 0;
10451 need_wait_return = nwr_save; /* never ask for hit-return for this */
10452 }
10453 else if (clear_cmdline && msg_silent == 0)
10454 /* Clear the whole command line. Will reset "clear_cmdline". */
10455 msg_clr_cmdline();
10456
10457#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 /* In Visual mode the size of the selected area must be redrawn. */
10459 if (VIsual_active)
10460 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461
10462 /* If the last window has no status line, the ruler is after the mode
10463 * message and must be redrawn */
10464 if (redrawing()
10465# ifdef FEAT_WINDOWS
10466 && lastwin->w_status_height == 0
10467# endif
10468 )
10469 win_redr_ruler(lastwin, TRUE);
10470#endif
10471 redraw_cmdline = FALSE;
10472 clear_cmdline = FALSE;
10473
10474 return length;
10475}
10476
10477/*
10478 * Position for a mode message.
10479 */
10480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010481msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 msg_col = 0;
10484 msg_row = Rows - 1;
10485}
10486
10487/*
10488 * Delete mode message. Used when ESC is typed which is expected to end
10489 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010490 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 */
10492 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010493unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010496 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 */
10498 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10499 redraw_cmdline = TRUE; /* delete mode later */
10500 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010501 clearmode();
10502}
10503
10504/*
10505 * Clear the mode message.
10506 */
10507 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010508clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010509{
10510 msg_pos_mode();
10511 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010512 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010513 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514}
10515
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010516 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010517recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010518{
10519 MSG_PUTS_ATTR(_("recording"), attr);
10520 if (!shortmess(SHM_RECORDING))
10521 {
10522 char_u s[4];
10523 sprintf((char *)s, " @%c", Recording);
10524 MSG_PUTS_ATTR(s, attr);
10525 }
10526}
10527
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010528#if defined(FEAT_WINDOWS)
10529/*
10530 * Draw the tab pages line at the top of the Vim window.
10531 */
10532 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010533draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010534{
10535 int tabcount = 0;
10536 tabpage_T *tp;
10537 int tabwidth;
10538 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010539 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010540 int attr;
10541 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010542 win_T *cwp;
10543 int wincount;
10544 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010545 int c;
10546 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010547 int attr_sel = HL_ATTR(HLF_TPS);
10548 int attr_nosel = HL_ATTR(HLF_TP);
10549 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010550 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010551 int room;
10552 int use_sep_chars = (t_colors < 8
10553#ifdef FEAT_GUI
10554 && !gui.in_use
10555#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010556#ifdef FEAT_TERMGUICOLORS
10557 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010558#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010559 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010561 if (ScreenLines == NULL)
10562 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010563 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010565#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010566 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010567 if (gui_use_tabline())
10568 {
10569 gui_update_tabline();
10570 return;
10571 }
10572#endif
10573
10574 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010575 return;
10576
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010577#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010578
10579 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10580 for (scol = 0; scol < Columns; ++scol)
10581 TabPageIdxs[scol] = 0;
10582
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010583 /* Use the 'tabline' option if it's set. */
10584 if (*p_tal != NUL)
10585 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010586 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010587
10588 /* Check for an error. If there is one we would loop in redrawing the
10589 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010590 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010591 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010592 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010593 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010594 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010595 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010596 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010597 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010598#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010599 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010600 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010602
Bram Moolenaar238a5642006-02-21 22:12:05 +000010603 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10604 if (tabwidth < 6)
10605 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010606
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607 attr = attr_nosel;
10608 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010609 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010610 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10611 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010612 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010614
Bram Moolenaar238a5642006-02-21 22:12:05 +000010615 if (tp->tp_topframe == topframe)
10616 attr = attr_sel;
10617 if (use_sep_chars && col > 0)
10618 screen_putchar('|', 0, col++, attr);
10619
10620 if (tp->tp_topframe != topframe)
10621 attr = attr_nosel;
10622
10623 screen_putchar(' ', 0, col++, attr);
10624
10625 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010626 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 cwp = curwin;
10628 wp = firstwin;
10629 }
10630 else
10631 {
10632 cwp = tp->tp_curwin;
10633 wp = tp->tp_firstwin;
10634 }
10635
10636 modified = FALSE;
10637 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10638 if (bufIsChanged(wp->w_buffer))
10639 modified = TRUE;
10640 if (modified || wincount > 1)
10641 {
10642 if (wincount > 1)
10643 {
10644 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010645 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010646 if (col + len >= Columns - 3)
10647 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010648 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010649#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010650 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010651#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010652 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010653#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010654 );
10655 col += len;
10656 }
10657 if (modified)
10658 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10659 screen_putchar(' ', 0, col++, attr);
10660 }
10661
10662 room = scol - col + tabwidth - 1;
10663 if (room > 0)
10664 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010665 /* Get buffer name in NameBuff[] */
10666 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010667 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 len = vim_strsize(NameBuff);
10669 p = NameBuff;
10670#ifdef FEAT_MBYTE
10671 if (has_mbyte)
10672 while (len > room)
10673 {
10674 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010675 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010676 }
10677 else
10678#endif
10679 if (len > room)
10680 {
10681 p += len - room;
10682 len = room;
10683 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010684 if (len > Columns - col - 1)
10685 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010687 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010688 col += len;
10689 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010690 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691
10692 /* Store the tab page number in TabPageIdxs[], so that
10693 * jump_to_mouse() knows where each one is. */
10694 ++tabcount;
10695 while (scol < col)
10696 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010697 }
10698
Bram Moolenaar238a5642006-02-21 22:12:05 +000010699 if (use_sep_chars)
10700 c = '_';
10701 else
10702 c = ' ';
10703 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010704
10705 /* Put an "X" for closing the current tab if there are several. */
10706 if (first_tabpage->tp_next != NULL)
10707 {
10708 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10709 TabPageIdxs[Columns - 1] = -999;
10710 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010711 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010712
10713 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10714 * set. */
10715 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010716}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010717
10718/*
10719 * Get buffer name for "buf" into NameBuff[].
10720 * Takes care of special buffer names and translates special characters.
10721 */
10722 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010723get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010724{
10725 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010726 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010727 else
10728 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10729 trans_characters(NameBuff, MAXPATHL);
10730}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731#endif
10732
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10734/*
10735 * Get the character to use in a status line. Get its attributes in "*attr".
10736 */
10737 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010738fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739{
10740 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010741
10742#ifdef FEAT_TERMINAL
10743 if (bt_terminal(wp->w_buffer))
10744 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010745 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010746 {
10747 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010748 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010749 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010750 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010751 {
10752 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010753 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010754 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010755 }
10756 else
10757#endif
10758 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010760 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 fill = fill_stl;
10762 }
10763 else
10764 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010765 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 fill = fill_stlnc;
10767 }
10768 /* Use fill when there is highlighting, and highlighting of current
10769 * window differs, or the fillchars differ, or this is not the
10770 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010771 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 || (fill_stl != fill_stlnc)))
10774 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010775 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 return '^';
10777 return '=';
10778}
10779#endif
10780
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010781#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782/*
10783 * Get the character to use in a separator between vertically split windows.
10784 * Get its attributes in "*attr".
10785 */
10786 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010787fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 if (*attr == 0 && fill_vert == ' ')
10791 return '|';
10792 else
10793 return fill_vert;
10794}
10795#endif
10796
10797/*
10798 * Return TRUE if redrawing should currently be done.
10799 */
10800 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010801redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010803#ifdef FEAT_EVAL
10804 if (disable_redraw_for_testing)
10805 return 0;
10806 else
10807#endif
10808 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10810}
10811
10812/*
10813 * Return TRUE if printing messages should currently be done.
10814 */
10815 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010816messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
10818 return (!(p_lz && char_avail() && !KeyTyped));
10819}
10820
10821/*
10822 * Show current status info in ruler and various other places
10823 * If always is FALSE, only show ruler if position has changed.
10824 */
10825 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010826showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827{
10828 if (!always && !redrawing())
10829 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010830#ifdef FEAT_INS_EXPAND
10831 if (pum_visible())
10832 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010833# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010834 /* Don't redraw right now, do it later. */
10835 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010836# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010837 return;
10838 }
10839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010841 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010842 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010843 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 else
10846#endif
10847#ifdef FEAT_CMDL_INFO
10848 win_redr_ruler(curwin, always);
10849#endif
10850
10851#ifdef FEAT_TITLE
10852 if (need_maketitle
10853# ifdef FEAT_STL_OPT
10854 || (p_icon && (stl_syntax & STL_IN_ICON))
10855 || (p_title && (stl_syntax & STL_IN_TITLE))
10856# endif
10857 )
10858 maketitle();
10859#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010860#ifdef FEAT_WINDOWS
10861 /* Redraw the tab pages line if needed. */
10862 if (redraw_tabline)
10863 draw_tabline();
10864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865}
10866
10867#ifdef FEAT_CMDL_INFO
10868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010869win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010871#define RULER_BUF_LEN 70
10872 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 int row;
10874 int fillchar;
10875 int attr;
10876 int empty_line = FALSE;
10877 colnr_T virtcol;
10878 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010879 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010881#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 int this_ru_col;
10883 int off = 0;
10884 int width = Columns;
10885# define WITH_OFF(x) x
10886# define WITH_WIDTH(x) x
10887#else
10888# define WITH_OFF(x) 0
10889# define WITH_WIDTH(x) Columns
10890# define this_ru_col ru_col
10891#endif
10892
10893 /* If 'ruler' off or redrawing disabled, don't do anything */
10894 if (!p_ru)
10895 return;
10896
10897 /*
10898 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10899 * after deleting lines, before cursor.lnum is corrected.
10900 */
10901 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10902 return;
10903
10904#ifdef FEAT_INS_EXPAND
10905 /* Don't draw the ruler while doing insert-completion, it might overwrite
10906 * the (long) mode message. */
10907# ifdef FEAT_WINDOWS
10908 if (wp == lastwin && lastwin->w_status_height == 0)
10909# endif
10910 if (edit_submode != NULL)
10911 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010912 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10913 if (pum_visible())
10914 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#endif
10916
10917#ifdef FEAT_STL_OPT
10918 if (*p_ruf)
10919 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010920 int save_called_emsg = called_emsg;
10921
10922 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010924 if (called_emsg)
10925 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010926 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010927 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 return;
10929 }
10930#endif
10931
10932 /*
10933 * Check if not in Insert mode and the line is empty (will show "0-1").
10934 */
10935 if (!(State & INSERT)
10936 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10937 empty_line = TRUE;
10938
10939 /*
10940 * Only draw the ruler when something changed.
10941 */
10942 validate_virtcol_win(wp);
10943 if ( redraw_cmdline
10944 || always
10945 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10946 || wp->w_cursor.col != wp->w_ru_cursor.col
10947 || wp->w_virtcol != wp->w_ru_virtcol
10948#ifdef FEAT_VIRTUALEDIT
10949 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10950#endif
10951 || wp->w_topline != wp->w_ru_topline
10952 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10953#ifdef FEAT_DIFF
10954 || wp->w_topfill != wp->w_ru_topfill
10955#endif
10956 || empty_line != wp->w_ru_empty)
10957 {
10958 cursor_off();
10959#ifdef FEAT_WINDOWS
10960 if (wp->w_status_height)
10961 {
10962 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010963 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 off = W_WINCOL(wp);
10965 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 }
10967 else
10968#endif
10969 {
10970 row = Rows - 1;
10971 fillchar = ' ';
10972 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010973#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 width = Columns;
10975 off = 0;
10976#endif
10977 }
10978
10979 /* In list mode virtcol needs to be recomputed */
10980 virtcol = wp->w_virtcol;
10981 if (wp->w_p_list && lcs_tab1 == NUL)
10982 {
10983 wp->w_p_list = FALSE;
10984 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10985 wp->w_p_list = TRUE;
10986 }
10987
10988 /*
10989 * Some sprintfs return the length, some return a pointer.
10990 * To avoid portability problems we use strlen() here.
10991 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010992 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10994 ? 0L
10995 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010996 len = STRLEN(buffer);
10997 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10999 (int)virtcol + 1);
11000
11001 /*
11002 * Add a "50%" if there is room for it.
11003 * On the last line, don't print in the last column (scrolls the
11004 * screen up on some terminals).
11005 */
11006 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011007 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 o = i + vim_strsize(buffer + i + 1);
11009#ifdef FEAT_WINDOWS
11010 if (wp->w_status_height == 0) /* can't use last char of screen */
11011#endif
11012 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010011013#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 this_ru_col = ru_col - (Columns - width);
11015 if (this_ru_col < 0)
11016 this_ru_col = 0;
11017#endif
11018 /* Never use more than half the window/screen width, leave the other
11019 * half for the filename. */
11020 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
11021 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
11022 if (this_ru_col + o < WITH_WIDTH(width))
11023 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011024 /* need at least 3 chars left for get_rel_pos() + NUL */
11025 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 {
11027#ifdef FEAT_MBYTE
11028 if (has_mbyte)
11029 i += (*mb_char2bytes)(fillchar, buffer + i);
11030 else
11031#endif
11032 buffer[i++] = fillchar;
11033 ++o;
11034 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011035 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 }
11037 /* Truncate at window boundary. */
11038#ifdef FEAT_MBYTE
11039 if (has_mbyte)
11040 {
11041 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011042 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 {
11044 o += (*mb_ptr2cells)(buffer + i);
11045 if (this_ru_col + o > WITH_WIDTH(width))
11046 {
11047 buffer[i] = NUL;
11048 break;
11049 }
11050 }
11051 }
11052 else
11053#endif
11054 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11055 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11056
11057 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11058 i = redraw_cmdline;
11059 screen_fill(row, row + 1,
11060 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11061 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11062 fillchar, fillchar, attr);
11063 /* don't redraw the cmdline because of showing the ruler */
11064 redraw_cmdline = i;
11065 wp->w_ru_cursor = wp->w_cursor;
11066 wp->w_ru_virtcol = wp->w_virtcol;
11067 wp->w_ru_empty = empty_line;
11068 wp->w_ru_topline = wp->w_topline;
11069 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11070#ifdef FEAT_DIFF
11071 wp->w_ru_topfill = wp->w_topfill;
11072#endif
11073 }
11074}
11075#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011076
11077#if defined(FEAT_LINEBREAK) || defined(PROTO)
11078/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011079 * Return the width of the 'number' and 'relativenumber' column.
11080 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011081 * Otherwise it depends on 'numberwidth' and the line count.
11082 */
11083 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011084number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011085{
11086 int n;
11087 linenr_T lnum;
11088
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011089 if (wp->w_p_rnu && !wp->w_p_nu)
11090 /* cursor line shows "0" */
11091 lnum = wp->w_height;
11092 else
11093 /* cursor line shows absolute line number */
11094 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011095
Bram Moolenaar6b314672015-03-20 15:42:10 +010011096 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011097 return wp->w_nrwidth_width;
11098 wp->w_nrwidth_line_count = lnum;
11099
11100 n = 0;
11101 do
11102 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011103 lnum /= 10;
11104 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011105 } while (lnum > 0);
11106
11107 /* 'numberwidth' gives the minimal width plus one */
11108 if (n < wp->w_p_nuw - 1)
11109 n = wp->w_p_nuw - 1;
11110
11111 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011112 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011113 return n;
11114}
11115#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011116
11117/*
11118 * Return the current cursor column. This is the actual position on the
11119 * screen. First column is 0.
11120 */
11121 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011122screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011123{
11124 return screen_cur_col;
11125}
11126
11127/*
11128 * Return the current cursor row. This is the actual position on the screen.
11129 * First row is 0.
11130 */
11131 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011132screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011133{
11134 return screen_cur_row;
11135}