blob: ce9b6e98e8c3f28120f69fa582409b87b813d10f [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
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200110#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
111static int text_to_screenline(win_T *wp, char_u *text, int col);
112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_FOLDING
114static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100115static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200118/* Flag that is set when drawing for a callback, not from the main command
119 * loop. */
120static int redrawing_for_callback = 0;
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122/*
123 * Buffer for one screen line (characters and attributes).
124 */
125static schar_T *current_ScreenLine;
126
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
128static 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 +0000129#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
131static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
132static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200134static int win_line(win_T *, linenr_T, int, int, int nochange);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_start_highlight(int attr);
150static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void lineinvalid(unsigned off, int width);
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 Moolenaarcfce7172017-08-17 20:31:48 +0200159static 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 +0100160static void win_rest_invalid(win_T *wp);
161static void msg_pos_mode(void);
162static void recording_mode(int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void draw_tabline(void);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200164static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200166#ifdef FEAT_MENU
167static void redraw_win_toolbar(win_T *wp);
168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100170static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
172#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100173static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174#endif
175
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176/* Ugly global: overrule attribute used by screen_char() */
177static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200179#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
180/* Can limit syntax highlight time to 'redrawtime'. */
181# define SYN_TIME_LIMIT 1
182#endif
183
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200184#ifdef FEAT_RIGHTLEFT
185# define HAS_RIGHTLEFT(x) x
186#else
187# define HAS_RIGHTLEFT(x) FALSE
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190/*
191 * Redraw the current window later, with update_screen(type).
192 * Set must_redraw only if not already set to a higher value.
193 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
194 */
195 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100196redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 redraw_win_later(curwin, type);
199}
200
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_win_later(
203 win_T *wp,
204 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200206 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 wp->w_redr_type = type;
209 if (type >= NOT_VALID)
210 wp->w_lines_valid = 0;
211 if (must_redraw < type) /* must_redraw is the maximum of all windows */
212 must_redraw = type;
213 }
214}
215
216/*
217 * Force a complete redraw later. Also resets the highlighting. To be used
218 * after executing a shell command that messes up the screen.
219 */
220 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100221redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000224#ifdef FEAT_GUI
225 if (gui.in_use)
226 /* Use a code that will reset gui.highlight_mask in
227 * gui_stop_highlight(). */
228 screen_attr = HL_ALL + 1;
229 else
230#endif
231 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200232 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
236 * Mark all windows to be redrawn later.
237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200292 * Redraw as soon as possible. When the command line is not scrolled redraw
293 * right away and restore what was on the command line.
294 * Return a code indicating what happened.
295 */
296 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100297redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200298{
299 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 int r;
302 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 schar_T *screenline; /* copy from ScreenLines[] */
304 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305#ifdef FEAT_MBYTE
306 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200307 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200309 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310#endif
311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (screenline == NULL || screenattr == NULL)
323 ret = 2;
324#ifdef FEAT_MBYTE
325 if (enc_utf8)
326 {
327 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenlineUC == NULL)
330 ret = 2;
331 for (i = 0; i < p_mco; ++i)
332 {
333 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenlineC[i] == NULL)
336 ret = 2;
337 }
338 }
339 if (enc_dbcs == DBCS_JPNU)
340 {
341 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenline2 == NULL)
344 ret = 2;
345 }
346#endif
347
348 if (ret != 2)
349 {
350 /* Save the text displayed in the command line area. */
351 for (r = 0; r < rows; ++r)
352 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200353 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200355 (size_t)cols * sizeof(schar_T));
356 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359#ifdef FEAT_MBYTE
360 if (enc_utf8)
361 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 mch_memmove(screenlineC[i] + r * cols,
367 ScreenLinesC[i] + LineOffset[cmdline_row + r],
368 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 }
370 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374#endif
375 }
376
377 update_screen(0);
378 ret = 3;
379
380 if (must_redraw == 0)
381 {
382 int off = (int)(current_ScreenLine - ScreenLines);
383
384 /* Restore the text displayed in the command line area. */
385 for (r = 0; r < rows; ++r)
386 {
387 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200388 screenline + r * cols,
389 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenattr + r * cols,
392 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393#ifdef FEAT_MBYTE
394 if (enc_utf8)
395 {
396 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineUC + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 for (i = 0; i < p_mco; ++i)
400 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineC[i] + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 }
404 if (enc_dbcs == DBCS_JPNU)
405 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenline2 + r * cols,
407 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200409 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 ret = 4;
412 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414
415 vim_free(screenline);
416 vim_free(screenattr);
417#ifdef FEAT_MBYTE
418 if (enc_utf8)
419 {
420 vim_free(screenlineUC);
421 for (i = 0; i < p_mco; ++i)
422 vim_free(screenlineC[i]);
423 }
424 if (enc_dbcs == DBCS_JPNU)
425 vim_free(screenline2);
426#endif
427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
435
436/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100437 * Invoked after an asynchronous callback is called.
438 * If an echo command was used the cursor needs to be put back where
439 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440 * If "call_update_screen" is FALSE don't call update_screen() when at the
441 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100442 */
443 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200444redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200446 ++redrawing_for_callback;
447
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100448 if (State == HITRETURN || State == ASKMORE)
449 ; /* do nothing */
450 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200451 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200452 /* Redrawing only works when the screen didn't scroll. Don't clear
453 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454 if (msg_scrolled == 0
455#ifdef FEAT_WILDMENU
456 && wild_menu_showing == 0
457#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200458 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200459 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200460 /* Redraw in the same position, so that the user can continue
461 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200464 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100465 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200466 /* keep the command line if possible */
467 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100468 setcursor();
469 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100472 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200473 /* Don't update the cursor when it is blinking and off to avoid
474 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100475 out_flush_cursor(FALSE, FALSE);
476 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100478 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200479
480 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481}
482
483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 * Changed something in the current window, at buffer line "lnum", that
485 * requires that line and possibly other lines to be redrawn.
486 * Used when entering/leaving Insert mode with the cursor on a folded line.
487 * Used to remove the "$" from a change command.
488 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
489 * may become invalid and the whole window will have to be redrawn.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492redrawWinline(
493 linenr_T lnum,
494 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495{
496#ifdef FEAT_FOLDING
497 int i;
498#endif
499
500 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
501 curwin->w_redraw_top = lnum;
502 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
503 curwin->w_redraw_bot = lnum;
504 redraw_later(VALID);
505
506#ifdef FEAT_FOLDING
507 if (invalid)
508 {
509 /* A w_lines[] entry for this lnum has become invalid. */
510 i = find_wl_entry(curwin, lnum);
511 if (i >= 0)
512 curwin->w_lines[i].wl_valid = FALSE;
513 }
514#endif
515}
516
517/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200518 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 */
520 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100521update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 redraw_curbuf_later(type);
524 update_screen(type);
525}
526
527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Based on the current value of curwin->w_topline, transfer a screenfull
529 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200530 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 win_T *wp;
537 static int did_intro = FALSE;
538#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
539 int did_one;
540#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200541#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200542 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543 int gui_cursor_col;
544 int gui_cursor_row;
545#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000548 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200550 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 if (type == VALID_NO_UPDATE)
553 {
554 no_update = TRUE;
555 type = 0;
556 }
557
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (must_redraw)
559 {
560 if (type < must_redraw) /* use maximal type */
561 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000562
563 /* must_redraw is reset here, so that when we run into some weird
564 * reason to redraw while busy redrawing (e.g., asynchronous
565 * scrolling), or update_topline() in win_update() will cause a
566 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 must_redraw = 0;
568 }
569
570 /* Need to update w_lines[]. */
571 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
572 type = NOT_VALID;
573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Postpone the redrawing when it's not needed and when being called
575 * recursively. */
576 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 {
578 redraw_later(type); /* remember type for next time */
579 must_redraw = type;
580 if (type > INVERTED_ALL)
581 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584
585 updating_screen = TRUE;
586#ifdef FEAT_SYN_HL
587 ++display_tick; /* let syntax code know we're in a next round of
588 * display updating */
589#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200590 if (no_update)
591 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 /*
594 * if the screen was scrolled up when displaying a message, scroll it down
595 */
596 if (msg_scrolled)
597 {
598 clear_cmdline = TRUE;
599 if (msg_scrolled > Rows - 5) /* clearing is faster */
600 type = CLEAR;
601 else if (type != CLEAR)
602 {
603 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200604 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
605 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 type = CLEAR;
607 FOR_ALL_WINDOWS(wp)
608 {
609 if (W_WINROW(wp) < msg_scrolled)
610 {
611 if (W_WINROW(wp) + wp->w_height > msg_scrolled
612 && wp->w_redr_type < REDRAW_TOP
613 && wp->w_lines_valid > 0
614 && wp->w_topline == wp->w_lines[0].wl_lnum)
615 {
616 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
617 wp->w_redr_type = REDRAW_TOP;
618 }
619 else
620 {
621 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200622 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
623 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 }
626 }
627 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200628 if (!no_update)
629 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000630 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 msg_scrolled = 0;
633 need_wait_return = FALSE;
634 }
635
636 /* reset cmdline_row now (may have been changed temporarily) */
637 compute_cmdrow();
638
639 /* Check for changed highlighting */
640 if (need_highlight_changed)
641 highlight_changed();
642
643 if (type == CLEAR) /* first clear screen */
644 {
645 screenclear(); /* will reset clear_cmdline */
646 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200647 /* must_redraw may be set indirectly, avoid another redraw later */
648 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650
651 if (clear_cmdline) /* going to clear cmdline (done below) */
652 check_for_delay(FALSE);
653
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000654#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200655 /* Force redraw when width of 'number' or 'relativenumber' column
656 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000657 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200658 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
659 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000660 curwin->w_redr_type = NOT_VALID;
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Only start redrawing if there is really something to do.
665 */
666 if (type == INVERTED)
667 update_curswant();
668 if (curwin->w_redr_type < type
669 && !((type == VALID
670 && curwin->w_lines[0].wl_valid
671#ifdef FEAT_DIFF
672 && curwin->w_topfill == curwin->w_old_topfill
673 && curwin->w_botfill == curwin->w_old_botfill
674#endif
675 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000677 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
679 && curwin->w_old_visual_mode == VIsual_mode
680 && (curwin->w_valid & VALID_VIRTCOL)
681 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 ))
683 curwin->w_redr_type = type;
684
Bram Moolenaar5a305422006-04-28 22:38:25 +0000685 /* Redraw the tab pages line if needed. */
686 if (redraw_tabline || type >= NOT_VALID)
687 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_SYN_HL
690 /*
691 * Correct stored syntax highlighting info for changes in each displayed
692 * buffer. Each buffer must only be done once.
693 */
694 FOR_ALL_WINDOWS(wp)
695 {
696 if (wp->w_buffer->b_mod_set)
697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 win_T *wwp;
699
700 /* Check if we already did this buffer. */
701 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
702 if (wwp->w_buffer == wp->w_buffer)
703 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200704 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 syn_stack_apply_changes(wp->w_buffer);
706 }
707 }
708#endif
709
710 /*
711 * Go from top to bottom through the windows, redrawing the ones that need
712 * it.
713 */
714#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
715 did_one = FALSE;
716#endif
717#ifdef FEAT_SEARCH_EXTRA
718 search_hl.rm.regprog = NULL;
719#endif
720 FOR_ALL_WINDOWS(wp)
721 {
722 if (wp->w_redr_type != 0)
723 {
724 cursor_off();
725#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
726 if (!did_one)
727 {
728 did_one = TRUE;
729# ifdef FEAT_SEARCH_EXTRA
730 start_search_hl();
731# endif
732# ifdef FEAT_CLIPBOARD
733 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200734 if (clip_star.available && clip_isautosel_star())
735 clip_update_selection(&clip_star);
736 if (clip_plus.available && clip_isautosel_plus())
737 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# endif
739#ifdef FEAT_GUI
740 /* Remove the cursor before starting to do anything, because
741 * scrolling may make it difficult to redraw the text under
742 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200743 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200744 {
745 gui_cursor_col = gui.cursor_col;
746 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200748 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#endif
751 }
752#endif
753 win_update(wp);
754 }
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /* redraw status line after the window to minimize cursor movement */
757 if (wp->w_redr_status)
758 {
759 cursor_off();
760 win_redr_status(wp);
761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763#if defined(FEAT_SEARCH_EXTRA)
764 end_search_hl();
765#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100766#ifdef FEAT_INS_EXPAND
767 /* May need to redraw the popup menu. */
768 if (pum_visible())
769 pum_redraw();
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 /* Reset b_mod_set flags. Going through all windows is probably faster
773 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200774 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
777 updating_screen = FALSE;
778#ifdef FEAT_GUI
779 gui_may_resize_shell();
780#endif
781
782 /* Clear or redraw the command line. Done last, because scrolling may
783 * mess up the command line. */
784 if (clear_cmdline || redraw_cmdline)
785 showmode();
786
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200787 if (no_update)
788 --no_win_do_lines_ins;
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200791 if (!did_intro)
792 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 did_intro = TRUE;
794
795#ifdef FEAT_GUI
796 /* Redraw the cursor and update the scrollbars when all screen updating is
797 * done. */
798 if (gui.in_use)
799 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200800 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200801 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100802 mch_disable_flush();
803 out_flush(); /* required before updating the cursor */
804 mch_enable_flush();
805
Bram Moolenaar144445d2016-07-08 21:41:54 +0200806 /* Put the GUI position where the cursor was, gui_update_cursor()
807 * uses that. */
808 gui.col = gui_cursor_col;
809 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200810# ifdef FEAT_MBYTE
811 gui.col = mb_fix_col(gui.col, gui.row);
812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100814 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200815 screen_cur_col = gui.col;
816 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200817 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100818 else
819 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 gui_update_scrollbars(FALSE);
821 }
822#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200823 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824}
825
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100826#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
827/*
828 * Prepare for updating one or more windows.
829 * Caller must check for "updating_screen" already set to avoid recursiveness.
830 */
831 static void
832update_prepare(void)
833{
834 cursor_off();
835 updating_screen = TRUE;
836#ifdef FEAT_GUI
837 /* Remove the cursor before starting to do anything, because scrolling may
838 * make it difficult to redraw the text under it. */
839 if (gui.in_use)
840 gui_undraw_cursor();
841#endif
842#ifdef FEAT_SEARCH_EXTRA
843 start_search_hl();
844#endif
845}
846
847/*
848 * Finish updating one or more windows.
849 */
850 static void
851update_finish(void)
852{
853 if (redraw_cmdline)
854 showmode();
855
856# ifdef FEAT_SEARCH_EXTRA
857 end_search_hl();
858# endif
859
860 updating_screen = FALSE;
861
862# ifdef FEAT_GUI
863 gui_may_resize_shell();
864
865 /* Redraw the cursor and update the scrollbars when all screen updating is
866 * done. */
867 if (gui.in_use)
868 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100869 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870 gui_update_scrollbars(FALSE);
871 }
872# endif
873}
874#endif
875
Bram Moolenaar860cae12010-06-05 23:22:07 +0200876#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200877/*
878 * Return TRUE if the cursor line in window "wp" may be concealed, according
879 * to the 'concealcursor' option.
880 */
881 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100882conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200883{
884 int c;
885
886 if (*wp->w_p_cocu == NUL)
887 return FALSE;
888 if (get_real_state() & VISUAL)
889 c = 'v';
890 else if (State & INSERT)
891 c = 'i';
892 else if (State & NORMAL)
893 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200894 else if (State & CMDLINE)
895 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896 else
897 return FALSE;
898 return vim_strchr(wp->w_p_cocu, c) != NULL;
899}
900
901/*
902 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
903 */
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
908 {
909 need_cursor_line_redraw = TRUE;
910 /* Need to recompute cursor column, e.g., when starting Visual mode
911 * without concealing. */
912 curs_columns(TRUE);
913 }
914}
915
Bram Moolenaar860cae12010-06-05 23:22:07 +0200916 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100917update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200918{
919 int row;
920 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200921#ifdef SYN_TIME_LIMIT
922 proftime_T syntax_tm;
923#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200924
Bram Moolenaar908be432016-05-24 10:51:30 +0200925 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100926 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200927 return;
928
Bram Moolenaar860cae12010-06-05 23:22:07 +0200929 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200930 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200931 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200932#ifdef SYN_TIME_LIMIT
933 /* Set the time limit to 'redrawtime'. */
934 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200935 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200936#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100937 update_prepare();
938
Bram Moolenaar860cae12010-06-05 23:22:07 +0200939 row = 0;
940 for (j = 0; j < wp->w_lines_valid; ++j)
941 {
942 if (lnum == wp->w_lines[j].wl_lnum)
943 {
944 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200945# ifdef FEAT_SEARCH_EXTRA
946 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200947 start_search_hl();
948 prepare_search_hl(wp, lnum);
949# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200950 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951# if defined(FEAT_SEARCH_EXTRA)
952 end_search_hl();
953# endif
954 break;
955 }
956 row += wp->w_lines[j].wl_size;
957 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100958
959 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200960
961#ifdef SYN_TIME_LIMIT
962 syn_set_timeout(NULL);
963#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200964 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200965 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967#endif
968
969#if defined(FEAT_SIGNS) || defined(PROTO)
970 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
973 win_T *wp;
974 int doit = FALSE;
975
976# ifdef FEAT_FOLDING
977 win_foldinfo.fi_level = 0;
978# endif
979
980 /* update/delete a specific mark */
981 FOR_ALL_WINDOWS(wp)
982 {
983 if (buf != NULL && lnum > 0)
984 {
985 if (wp->w_buffer == buf && lnum >= wp->w_topline
986 && lnum < wp->w_botline)
987 {
988 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
989 wp->w_redraw_top = lnum;
990 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
991 wp->w_redraw_bot = lnum;
992 redraw_win_later(wp, VALID);
993 }
994 }
995 else
996 redraw_win_later(wp, VALID);
997 if (wp->w_redr_type != 0)
998 doit = TRUE;
999 }
1000
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001001 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001002 * happening (recursive call), messages on the screen or still starting up.
1003 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001004 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001005 || State == ASKMORE || State == HITRETURN
1006 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001007#ifdef FEAT_GUI
1008 || gui.starting
1009#endif
1010 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 return;
1012
1013 /* update all windows that need updating */
1014 update_prepare();
1015
Bram Moolenaar29323592016-07-24 22:04:11 +02001016 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 {
1018 if (wp->w_redr_type != 0)
1019 win_update(wp);
1020 if (wp->w_redr_status)
1021 win_redr_status(wp);
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 update_finish();
1025}
1026#endif
1027
1028
1029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * Update a single window, its status line and maybe the command line msg.
1032 * Used for the GUI scrollbar.
1033 */
1034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001035updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001037 /* return if already busy updating */
1038 if (updating_screen)
1039 return;
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 update_prepare();
1042
1043#ifdef FEAT_CLIPBOARD
1044 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001045 if (clip_star.available && clip_isautosel_star())
1046 clip_update_selection(&clip_star);
1047 if (clip_plus.available && clip_isautosel_plus())
1048 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001052
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001053 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001054 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001055 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (wp->w_redr_status
1058# ifdef FEAT_CMDL_INFO
1059 || p_ru
1060# endif
1061# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001062 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063# endif
1064 )
1065 win_redr_status(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 update_finish();
1068}
1069#endif
1070
1071/*
1072 * Update a single window.
1073 *
1074 * This may cause the windows below it also to be redrawn (when clearing the
1075 * screen or scrolling lines).
1076 *
1077 * How the window is redrawn depends on wp->w_redr_type. Each type also
1078 * implies the one below it.
1079 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001080 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1082 * INVERTED redraw the changed part of the Visual area
1083 * INVERTED_ALL redraw the whole Visual area
1084 * VALID 1. scroll up/down to adjust for a changed w_topline
1085 * 2. update lines at the top when scrolled down
1086 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001087 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 * b_mod_top and b_mod_bot.
1089 * - if wp->w_redraw_top non-zero, redraw lines between
1090 * wp->w_redraw_top and wp->w_redr_bot.
1091 * - continue redrawing when syntax status is invalid.
1092 * 4. if scrolled up, update lines at the bottom.
1093 * This results in three areas that may need updating:
1094 * top: from first row to top_end (when scrolled down)
1095 * mid: from mid_start to mid_end (update inversion or changed text)
1096 * bot: from bot_start to last row (when scrolled up)
1097 */
1098 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001099win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
1101 buf_T *buf = wp->w_buffer;
1102 int type;
1103 int top_end = 0; /* Below last row of the top area that needs
1104 updating. 0 when no top area updating. */
1105 int mid_start = 999;/* first row of the mid area that needs
1106 updating. 999 when no mid area updating. */
1107 int mid_end = 0; /* Below last row of the mid area that needs
1108 updating. 0 when no mid area updating. */
1109 int bot_start = 999;/* first row of the bot area that needs
1110 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 int scrolled_down = FALSE; /* TRUE when scrolled down when
1112 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001114 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int top_to_mod = FALSE; /* redraw above mod_top */
1116#endif
1117
1118 int row; /* current window row to display */
1119 linenr_T lnum; /* current buffer lnum to display */
1120 int idx; /* current index in w_lines[] */
1121 int srow; /* starting row of the current line */
1122
1123 int eof = FALSE; /* if TRUE, we hit the end of the file */
1124 int didline = FALSE; /* if TRUE, we finished the last line */
1125 int i;
1126 long j;
1127 static int recursive = FALSE; /* being called recursively */
1128 int old_botline = wp->w_botline;
1129#ifdef FEAT_FOLDING
1130 long fold_count;
1131#endif
1132#ifdef FEAT_SYN_HL
1133 /* remember what happened to the previous line, to know if
1134 * check_visual_highlight() can be used */
1135#define DID_NONE 1 /* didn't update a line */
1136#define DID_LINE 2 /* updated a normal line */
1137#define DID_FOLD 3 /* updated a folded line */
1138 int did_update = DID_NONE;
1139 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1140#endif
1141 linenr_T mod_top = 0;
1142 linenr_T mod_bot = 0;
1143#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1144 int save_got_int;
1145#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001146#ifdef SYN_TIME_LIMIT
1147 proftime_T syntax_tm;
1148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 type = wp->w_redr_type;
1151
1152 if (type == NOT_VALID)
1153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wp->w_lines_valid = 0;
1156 }
1157
1158 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001159 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 wp->w_redr_type = 0;
1162 return;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /* Window is zero-width: Only need to draw the separator. */
1166 if (wp->w_width == 0)
1167 {
1168 /* draw the vertical separator right of this window */
1169 draw_vsep_win(wp, 0);
1170 wp->w_redr_type = 0;
1171 return;
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001174#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175 /* If this window contains a terminal, redraw works completely differently.
1176 */
1177 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001178 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001179 wp->w_redr_type = 0;
1180 return;
1181 }
1182#endif
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001185 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001188#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001189 /* Force redraw when width of 'number' or 'relativenumber' column
1190 * changes. */
1191 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001192 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001193 {
1194 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001195 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001196 }
1197 else
1198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1201 {
1202 /*
1203 * When there are both inserted/deleted lines and specific lines to be
1204 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1205 * everything (only happens when redrawing is off for while).
1206 */
1207 type = NOT_VALID;
1208 }
1209 else
1210 {
1211 /*
1212 * Set mod_top to the first line that needs displaying because of
1213 * changes. Set mod_bot to the first line after the changes.
1214 */
1215 mod_top = wp->w_redraw_top;
1216 if (wp->w_redraw_bot != 0)
1217 mod_bot = wp->w_redraw_bot + 1;
1218 else
1219 mod_bot = 0;
1220 wp->w_redraw_top = 0; /* reset for next time */
1221 wp->w_redraw_bot = 0;
1222 if (buf->b_mod_set)
1223 {
1224 if (mod_top == 0 || mod_top > buf->b_mod_top)
1225 {
1226 mod_top = buf->b_mod_top;
1227#ifdef FEAT_SYN_HL
1228 /* Need to redraw lines above the change that may be included
1229 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001230 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001232 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (mod_top < 1)
1234 mod_top = 1;
1235 }
1236#endif
1237 }
1238 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1239 mod_bot = buf->b_mod_bot;
1240
1241#ifdef FEAT_SEARCH_EXTRA
1242 /* When 'hlsearch' is on and using a multi-line search pattern, a
1243 * change in one line may make the Search highlighting in a
1244 * previous line invalid. Simple solution: redraw all visible
1245 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001246 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001248 if (search_hl.rm.regprog != NULL
1249 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001251 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001252 {
1253 cur = wp->w_match_head;
1254 while (cur != NULL)
1255 {
1256 if (cur->match.regprog != NULL
1257 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001258 {
1259 top_to_mod = TRUE;
1260 break;
1261 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001262 cur = cur->next;
1263 }
1264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#endif
1266 }
1267#ifdef FEAT_FOLDING
1268 if (mod_top != 0 && hasAnyFolding(wp))
1269 {
1270 linenr_T lnumt, lnumb;
1271
1272 /*
1273 * A change in a line can cause lines above it to become folded or
1274 * unfolded. Find the top most buffer line that may be affected.
1275 * If the line was previously folded and displayed, get the first
1276 * line of that fold. If the line is folded now, get the first
1277 * folded line. Use the minimum of these two.
1278 */
1279
1280 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1281 * the line below it. If there is no valid entry, use w_topline.
1282 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1283 * to this line. If there is no valid entry, use MAXLNUM. */
1284 lnumt = wp->w_topline;
1285 lnumb = MAXLNUM;
1286 for (i = 0; i < wp->w_lines_valid; ++i)
1287 if (wp->w_lines[i].wl_valid)
1288 {
1289 if (wp->w_lines[i].wl_lastlnum < mod_top)
1290 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1291 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1292 {
1293 lnumb = wp->w_lines[i].wl_lnum;
1294 /* When there is a fold column it might need updating
1295 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001296 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ++lnumb;
1298 }
1299 }
1300
1301 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1302 if (mod_top > lnumt)
1303 mod_top = lnumt;
1304
1305 /* Now do the same for the bottom line (one above mod_bot). */
1306 --mod_bot;
1307 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1308 ++mod_bot;
1309 if (mod_bot < lnumb)
1310 mod_bot = lnumb;
1311 }
1312#endif
1313
1314 /* When a change starts above w_topline and the end is below
1315 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001316 * If the end of the change is above w_topline: do like no change was
1317 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (mod_top != 0 && mod_top < wp->w_topline)
1319 {
1320 if (mod_bot > wp->w_topline)
1321 mod_top = wp->w_topline;
1322#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001323 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 top_end = 1;
1325#endif
1326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001327
1328 /* When line numbers are displayed need to redraw all lines below
1329 * inserted/deleted lines. */
1330 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1331 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333
1334 /*
1335 * When only displaying the lines at the top, set top_end. Used when
1336 * window has scrolled down for msg_scrolled.
1337 */
1338 if (type == REDRAW_TOP)
1339 {
1340 j = 0;
1341 for (i = 0; i < wp->w_lines_valid; ++i)
1342 {
1343 j += wp->w_lines[i].wl_size;
1344 if (j >= wp->w_upd_rows)
1345 {
1346 top_end = j;
1347 break;
1348 }
1349 }
1350 if (top_end == 0)
1351 /* not found (cannot happen?): redraw everything */
1352 type = NOT_VALID;
1353 else
1354 /* top area defined, the rest is VALID */
1355 type = VALID;
1356 }
1357
Bram Moolenaar367329b2007-08-30 11:53:22 +00001358 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001359 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1360 * non-zero and thus not FALSE) will indicate that screenclear() was not
1361 * called. */
1362 if (screen_cleared)
1363 screen_cleared = MAYBE;
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * If there are no changes on the screen that require a complete redraw,
1367 * handle three cases:
1368 * 1: we are off the top of the screen by a few lines: scroll down
1369 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1370 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1371 * w_lines[] that needs updating.
1372 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001373 if ((type == VALID || type == SOME_VALID
1374 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#ifdef FEAT_DIFF
1376 && !wp->w_botfill && !wp->w_old_botfill
1377#endif
1378 )
1379 {
1380 if (mod_top != 0 && wp->w_topline == mod_top)
1381 {
1382 /*
1383 * w_topline is the first changed line, the scrolling will be done
1384 * further down.
1385 */
1386 }
1387 else if (wp->w_lines[0].wl_valid
1388 && (wp->w_topline < wp->w_lines[0].wl_lnum
1389#ifdef FEAT_DIFF
1390 || (wp->w_topline == wp->w_lines[0].wl_lnum
1391 && wp->w_topfill > wp->w_old_topfill)
1392#endif
1393 ))
1394 {
1395 /*
1396 * New topline is above old topline: May scroll down.
1397 */
1398#ifdef FEAT_FOLDING
1399 if (hasAnyFolding(wp))
1400 {
1401 linenr_T ln;
1402
1403 /* count the number of lines we are off, counting a sequence
1404 * of folded lines as one */
1405 j = 0;
1406 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1407 {
1408 ++j;
1409 if (j >= wp->w_height - 2)
1410 break;
1411 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1412 }
1413 }
1414 else
1415#endif
1416 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1417 if (j < wp->w_height - 2) /* not too far off */
1418 {
1419 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1420#ifdef FEAT_DIFF
1421 /* insert extra lines for previously invisible filler lines */
1422 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1423 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1424 - wp->w_old_topfill;
1425#endif
1426 if (i < wp->w_height - 2) /* less than a screen off */
1427 {
1428 /*
1429 * Try to insert the correct number of lines.
1430 * If not the last window, delete the lines at the bottom.
1431 * win_ins_lines may fail when the terminal can't do it.
1432 */
1433 if (i > 0)
1434 check_for_delay(FALSE);
1435 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1436 {
1437 if (wp->w_lines_valid != 0)
1438 {
1439 /* Need to update rows that are new, stop at the
1440 * first one that scrolled down. */
1441 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 /* Move the entries that were scrolled, disable
1445 * the entries for the lines to be redrawn. */
1446 if ((wp->w_lines_valid += j) > wp->w_height)
1447 wp->w_lines_valid = wp->w_height;
1448 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1449 wp->w_lines[idx] = wp->w_lines[idx - j];
1450 while (idx >= 0)
1451 wp->w_lines[idx--].wl_valid = FALSE;
1452 }
1453 }
1454 else
1455 mid_start = 0; /* redraw all lines */
1456 }
1457 else
1458 mid_start = 0; /* redraw all lines */
1459 }
1460 else
1461 mid_start = 0; /* redraw all lines */
1462 }
1463 else
1464 {
1465 /*
1466 * New topline is at or below old topline: May scroll up.
1467 * When topline didn't change, find first entry in w_lines[] that
1468 * needs updating.
1469 */
1470
1471 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1472 j = -1;
1473 row = 0;
1474 for (i = 0; i < wp->w_lines_valid; i++)
1475 {
1476 if (wp->w_lines[i].wl_valid
1477 && wp->w_lines[i].wl_lnum == wp->w_topline)
1478 {
1479 j = i;
1480 break;
1481 }
1482 row += wp->w_lines[i].wl_size;
1483 }
1484 if (j == -1)
1485 {
1486 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1487 * lines */
1488 mid_start = 0;
1489 }
1490 else
1491 {
1492 /*
1493 * Try to delete the correct number of lines.
1494 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1495 */
1496#ifdef FEAT_DIFF
1497 /* If the topline didn't change, delete old filler lines,
1498 * otherwise delete filler lines of the new topline... */
1499 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1500 row += wp->w_old_topfill;
1501 else
1502 row += diff_check_fill(wp, wp->w_topline);
1503 /* ... but don't delete new filler lines. */
1504 row -= wp->w_topfill;
1505#endif
1506 if (row > 0)
1507 {
1508 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001509 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1510 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 bot_start = wp->w_height - row;
1512 else
1513 mid_start = 0; /* redraw all lines */
1514 }
1515 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1516 {
1517 /*
1518 * Skip the lines (below the deleted lines) that are still
1519 * valid and don't need redrawing. Copy their info
1520 * upwards, to compensate for the deleted lines. Set
1521 * bot_start to the first row that needs redrawing.
1522 */
1523 bot_start = 0;
1524 idx = 0;
1525 for (;;)
1526 {
1527 wp->w_lines[idx] = wp->w_lines[j];
1528 /* stop at line that didn't fit, unless it is still
1529 * valid (no lines deleted) */
1530 if (row > 0 && bot_start + row
1531 + (int)wp->w_lines[j].wl_size > wp->w_height)
1532 {
1533 wp->w_lines_valid = idx + 1;
1534 break;
1535 }
1536 bot_start += wp->w_lines[idx++].wl_size;
1537
1538 /* stop at the last valid entry in w_lines[].wl_size */
1539 if (++j >= wp->w_lines_valid)
1540 {
1541 wp->w_lines_valid = idx;
1542 break;
1543 }
1544 }
1545#ifdef FEAT_DIFF
1546 /* Correct the first entry for filler lines at the top
1547 * when it won't get updated below. */
1548 if (wp->w_p_diff && bot_start > 0)
1549 wp->w_lines[0].wl_size =
1550 plines_win_nofill(wp, wp->w_topline, TRUE)
1551 + wp->w_topfill;
1552#endif
1553 }
1554 }
1555 }
1556
1557 /* When starting redraw in the first line, redraw all lines. When
1558 * there is only one window it's probably faster to clear the screen
1559 * first. */
1560 if (mid_start == 0)
1561 {
1562 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001563 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001564 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001565 /* Clear the screen when it was not done by win_del_lines() or
1566 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1567 * then. */
1568 if (screen_cleared != TRUE)
1569 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001570 /* The screen was cleared, redraw the tab pages line. */
1571 if (redraw_tabline)
1572 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001575
1576 /* When win_del_lines() or win_ins_lines() caused the screen to be
1577 * cleared (only happens for the first window) or when screenclear()
1578 * was called directly above, "must_redraw" will have been set to
1579 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1580 if (screen_cleared == TRUE)
1581 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 else
1584 {
1585 /* Not VALID or INVERTED: redraw all lines. */
1586 mid_start = 0;
1587 mid_end = wp->w_height;
1588 }
1589
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001590 if (type == SOME_VALID)
1591 {
1592 /* SOME_VALID: redraw all lines. */
1593 mid_start = 0;
1594 mid_end = wp->w_height;
1595 type = NOT_VALID;
1596 }
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 /* check if we are updating or removing the inverted part */
1599 if ((VIsual_active && buf == curwin->w_buffer)
1600 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1601 {
1602 linenr_T from, to;
1603
1604 if (VIsual_active)
1605 {
1606 if (VIsual_active
1607 && (VIsual_mode != wp->w_old_visual_mode
1608 || type == INVERTED_ALL))
1609 {
1610 /*
1611 * If the type of Visual selection changed, redraw the whole
1612 * selection. Also when the ownership of the X selection is
1613 * gained or lost.
1614 */
1615 if (curwin->w_cursor.lnum < VIsual.lnum)
1616 {
1617 from = curwin->w_cursor.lnum;
1618 to = VIsual.lnum;
1619 }
1620 else
1621 {
1622 from = VIsual.lnum;
1623 to = curwin->w_cursor.lnum;
1624 }
1625 /* redraw more when the cursor moved as well */
1626 if (wp->w_old_cursor_lnum < from)
1627 from = wp->w_old_cursor_lnum;
1628 if (wp->w_old_cursor_lnum > to)
1629 to = wp->w_old_cursor_lnum;
1630 if (wp->w_old_visual_lnum < from)
1631 from = wp->w_old_visual_lnum;
1632 if (wp->w_old_visual_lnum > to)
1633 to = wp->w_old_visual_lnum;
1634 }
1635 else
1636 {
1637 /*
1638 * Find the line numbers that need to be updated: The lines
1639 * between the old cursor position and the current cursor
1640 * position. Also check if the Visual position changed.
1641 */
1642 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1643 {
1644 from = curwin->w_cursor.lnum;
1645 to = wp->w_old_cursor_lnum;
1646 }
1647 else
1648 {
1649 from = wp->w_old_cursor_lnum;
1650 to = curwin->w_cursor.lnum;
1651 if (from == 0) /* Visual mode just started */
1652 from = to;
1653 }
1654
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001655 if (VIsual.lnum != wp->w_old_visual_lnum
1656 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 if (wp->w_old_visual_lnum < from
1659 && wp->w_old_visual_lnum != 0)
1660 from = wp->w_old_visual_lnum;
1661 if (wp->w_old_visual_lnum > to)
1662 to = wp->w_old_visual_lnum;
1663 if (VIsual.lnum < from)
1664 from = VIsual.lnum;
1665 if (VIsual.lnum > to)
1666 to = VIsual.lnum;
1667 }
1668 }
1669
1670 /*
1671 * If in block mode and changed column or curwin->w_curswant:
1672 * update all lines.
1673 * First compute the actual start and end column.
1674 */
1675 if (VIsual_mode == Ctrl_V)
1676 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001677 colnr_T fromc, toc;
1678#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1679 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar404406a2014-10-09 13:24:43 +02001681 if (curwin->w_p_lbr)
1682 ve_flags = VE_ALL;
1683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001685#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1686 ve_flags = save_ve_flags;
1687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 ++toc;
1689 if (curwin->w_curswant == MAXCOL)
1690 toc = MAXCOL;
1691
1692 if (fromc != wp->w_old_cursor_fcol
1693 || toc != wp->w_old_cursor_lcol)
1694 {
1695 if (from > VIsual.lnum)
1696 from = VIsual.lnum;
1697 if (to < VIsual.lnum)
1698 to = VIsual.lnum;
1699 }
1700 wp->w_old_cursor_fcol = fromc;
1701 wp->w_old_cursor_lcol = toc;
1702 }
1703 }
1704 else
1705 {
1706 /* Use the line numbers of the old Visual area. */
1707 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1708 {
1709 from = wp->w_old_cursor_lnum;
1710 to = wp->w_old_visual_lnum;
1711 }
1712 else
1713 {
1714 from = wp->w_old_visual_lnum;
1715 to = wp->w_old_cursor_lnum;
1716 }
1717 }
1718
1719 /*
1720 * There is no need to update lines above the top of the window.
1721 */
1722 if (from < wp->w_topline)
1723 from = wp->w_topline;
1724
1725 /*
1726 * If we know the value of w_botline, use it to restrict the update to
1727 * the lines that are visible in the window.
1728 */
1729 if (wp->w_valid & VALID_BOTLINE)
1730 {
1731 if (from >= wp->w_botline)
1732 from = wp->w_botline - 1;
1733 if (to >= wp->w_botline)
1734 to = wp->w_botline - 1;
1735 }
1736
1737 /*
1738 * Find the minimal part to be updated.
1739 * Watch out for scrolling that made entries in w_lines[] invalid.
1740 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1741 * top_end; need to redraw from top_end to the "to" line.
1742 * A middle mouse click with a Visual selection may change the text
1743 * above the Visual area and reset wl_valid, do count these for
1744 * mid_end (in srow).
1745 */
1746 if (mid_start > 0)
1747 {
1748 lnum = wp->w_topline;
1749 idx = 0;
1750 srow = 0;
1751 if (scrolled_down)
1752 mid_start = top_end;
1753 else
1754 mid_start = 0;
1755 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1756 {
1757 if (wp->w_lines[idx].wl_valid)
1758 mid_start += wp->w_lines[idx].wl_size;
1759 else if (!scrolled_down)
1760 srow += wp->w_lines[idx].wl_size;
1761 ++idx;
1762# ifdef FEAT_FOLDING
1763 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1764 lnum = wp->w_lines[idx].wl_lnum;
1765 else
1766# endif
1767 ++lnum;
1768 }
1769 srow += mid_start;
1770 mid_end = wp->w_height;
1771 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1772 {
1773 if (wp->w_lines[idx].wl_valid
1774 && wp->w_lines[idx].wl_lnum >= to + 1)
1775 {
1776 /* Only update until first row of this line */
1777 mid_end = srow;
1778 break;
1779 }
1780 srow += wp->w_lines[idx].wl_size;
1781 }
1782 }
1783 }
1784
1785 if (VIsual_active && buf == curwin->w_buffer)
1786 {
1787 wp->w_old_visual_mode = VIsual_mode;
1788 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1789 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001790 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 wp->w_old_curswant = curwin->w_curswant;
1792 }
1793 else
1794 {
1795 wp->w_old_visual_mode = 0;
1796 wp->w_old_cursor_lnum = 0;
1797 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001798 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1802 /* reset got_int, otherwise regexp won't work */
1803 save_got_int = got_int;
1804 got_int = 0;
1805#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001806#ifdef SYN_TIME_LIMIT
1807 /* Set the time limit to 'redrawtime'. */
1808 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001809 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#ifdef FEAT_FOLDING
1812 win_foldinfo.fi_level = 0;
1813#endif
1814
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001815#ifdef FEAT_MENU
1816 /*
1817 * Draw the window toolbar, if there is one.
1818 * TODO: only when needed.
1819 */
1820 if (winbar_height(wp) > 0)
1821 redraw_win_toolbar(wp);
1822#endif
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 /*
1825 * Update all the window rows.
1826 */
1827 idx = 0; /* first entry in w_lines[].wl_size */
1828 row = 0;
1829 srow = 0;
1830 lnum = wp->w_topline; /* first line shown in window */
1831 for (;;)
1832 {
1833 /* stop updating when reached the end of the window (check for _past_
1834 * the end of the window is at the end of the loop) */
1835 if (row == wp->w_height)
1836 {
1837 didline = TRUE;
1838 break;
1839 }
1840
1841 /* stop updating when hit the end of the file */
1842 if (lnum > buf->b_ml.ml_line_count)
1843 {
1844 eof = TRUE;
1845 break;
1846 }
1847
1848 /* Remember the starting row of the line that is going to be dealt
1849 * with. It is used further down when the line doesn't fit. */
1850 srow = row;
1851
1852 /*
1853 * Update a line when it is in an area that needs updating, when it
1854 * has changes or w_lines[idx] is invalid.
1855 * bot_start may be halfway a wrapped line after using
1856 * win_del_lines(), check if the current line includes it.
1857 * When syntax folding is being used, the saved syntax states will
1858 * already have been updated, we can't see where the syntax state is
1859 * the same again, just update until the end of the window.
1860 */
1861 if (row < top_end
1862 || (row >= mid_start && row < mid_end)
1863#ifdef FEAT_SEARCH_EXTRA
1864 || top_to_mod
1865#endif
1866 || idx >= wp->w_lines_valid
1867 || (row + wp->w_lines[idx].wl_size > bot_start)
1868 || (mod_top != 0
1869 && (lnum == mod_top
1870 || (lnum >= mod_top
1871 && (lnum < mod_bot
1872#ifdef FEAT_SYN_HL
1873 || did_update == DID_FOLD
1874 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001875 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 && (
1877# ifdef FEAT_FOLDING
1878 (foldmethodIsSyntax(wp)
1879 && hasAnyFolding(wp)) ||
1880# endif
1881 syntax_check_changed(lnum)))
1882#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001883#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001884 /* match in fixed position might need redraw
1885 * if lines were inserted or deleted */
1886 || (wp->w_match_head != NULL
1887 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 )))))
1890 {
1891#ifdef FEAT_SEARCH_EXTRA
1892 if (lnum == mod_top)
1893 top_to_mod = FALSE;
1894#endif
1895
1896 /*
1897 * When at start of changed lines: May scroll following lines
1898 * up or down to minimize redrawing.
1899 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001900 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 */
1902 if (lnum == mod_top
1903 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001904 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 int old_rows = 0;
1907 int new_rows = 0;
1908 int xtra_rows;
1909 linenr_T l;
1910
1911 /* Count the old number of window rows, using w_lines[], which
1912 * should still contain the sizes for the lines as they are
1913 * currently displayed. */
1914 for (i = idx; i < wp->w_lines_valid; ++i)
1915 {
1916 /* Only valid lines have a meaningful wl_lnum. Invalid
1917 * lines are part of the changed area. */
1918 if (wp->w_lines[i].wl_valid
1919 && wp->w_lines[i].wl_lnum == mod_bot)
1920 break;
1921 old_rows += wp->w_lines[i].wl_size;
1922#ifdef FEAT_FOLDING
1923 if (wp->w_lines[i].wl_valid
1924 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1925 {
1926 /* Must have found the last valid entry above mod_bot.
1927 * Add following invalid entries. */
1928 ++i;
1929 while (i < wp->w_lines_valid
1930 && !wp->w_lines[i].wl_valid)
1931 old_rows += wp->w_lines[i++].wl_size;
1932 break;
1933 }
1934#endif
1935 }
1936
1937 if (i >= wp->w_lines_valid)
1938 {
1939 /* We can't find a valid line below the changed lines,
1940 * need to redraw until the end of the window.
1941 * Inserting/deleting lines has no use. */
1942 bot_start = 0;
1943 }
1944 else
1945 {
1946 /* Able to count old number of rows: Count new window
1947 * rows, and may insert/delete lines */
1948 j = idx;
1949 for (l = lnum; l < mod_bot; ++l)
1950 {
1951#ifdef FEAT_FOLDING
1952 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1953 ++new_rows;
1954 else
1955#endif
1956#ifdef FEAT_DIFF
1957 if (l == wp->w_topline)
1958 new_rows += plines_win_nofill(wp, l, TRUE)
1959 + wp->w_topfill;
1960 else
1961#endif
1962 new_rows += plines_win(wp, l, TRUE);
1963 ++j;
1964 if (new_rows > wp->w_height - row - 2)
1965 {
1966 /* it's getting too much, must redraw the rest */
1967 new_rows = 9999;
1968 break;
1969 }
1970 }
1971 xtra_rows = new_rows - old_rows;
1972 if (xtra_rows < 0)
1973 {
1974 /* May scroll text up. If there is not enough
1975 * remaining text or scrolling fails, must redraw the
1976 * rest. If scrolling works, must redraw the text
1977 * below the scrolled text. */
1978 if (row - xtra_rows >= wp->w_height - 2)
1979 mod_bot = MAXLNUM;
1980 else
1981 {
1982 check_for_delay(FALSE);
1983 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001984 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 mod_bot = MAXLNUM;
1986 else
1987 bot_start = wp->w_height + xtra_rows;
1988 }
1989 }
1990 else if (xtra_rows > 0)
1991 {
1992 /* May scroll text down. If there is not enough
1993 * remaining text of scrolling fails, must redraw the
1994 * rest. */
1995 if (row + xtra_rows >= wp->w_height - 2)
1996 mod_bot = MAXLNUM;
1997 else
1998 {
1999 check_for_delay(FALSE);
2000 if (win_ins_lines(wp, row + old_rows,
2001 xtra_rows, FALSE, FALSE) == FAIL)
2002 mod_bot = MAXLNUM;
2003 else if (top_end > row + old_rows)
2004 /* Scrolled the part at the top that requires
2005 * updating down. */
2006 top_end += xtra_rows;
2007 }
2008 }
2009
2010 /* When not updating the rest, may need to move w_lines[]
2011 * entries. */
2012 if (mod_bot != MAXLNUM && i != j)
2013 {
2014 if (j < i)
2015 {
2016 int x = row + new_rows;
2017
2018 /* move entries in w_lines[] upwards */
2019 for (;;)
2020 {
2021 /* stop at last valid entry in w_lines[] */
2022 if (i >= wp->w_lines_valid)
2023 {
2024 wp->w_lines_valid = j;
2025 break;
2026 }
2027 wp->w_lines[j] = wp->w_lines[i];
2028 /* stop at a line that won't fit */
2029 if (x + (int)wp->w_lines[j].wl_size
2030 > wp->w_height)
2031 {
2032 wp->w_lines_valid = j + 1;
2033 break;
2034 }
2035 x += wp->w_lines[j++].wl_size;
2036 ++i;
2037 }
2038 if (bot_start > x)
2039 bot_start = x;
2040 }
2041 else /* j > i */
2042 {
2043 /* move entries in w_lines[] downwards */
2044 j -= i;
2045 wp->w_lines_valid += j;
2046 if (wp->w_lines_valid > wp->w_height)
2047 wp->w_lines_valid = wp->w_height;
2048 for (i = wp->w_lines_valid; i - j >= idx; --i)
2049 wp->w_lines[i] = wp->w_lines[i - j];
2050
2051 /* The w_lines[] entries for inserted lines are
2052 * now invalid, but wl_size may be used above.
2053 * Reset to zero. */
2054 while (i >= idx)
2055 {
2056 wp->w_lines[i].wl_size = 0;
2057 wp->w_lines[i--].wl_valid = FALSE;
2058 }
2059 }
2060 }
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
2064#ifdef FEAT_FOLDING
2065 /*
2066 * When lines are folded, display one line for all of them.
2067 * Otherwise, display normally (can be several display lines when
2068 * 'wrap' is on).
2069 */
2070 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2071 if (fold_count != 0)
2072 {
2073 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2074 ++row;
2075 --fold_count;
2076 wp->w_lines[idx].wl_folded = TRUE;
2077 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2078# ifdef FEAT_SYN_HL
2079 did_update = DID_FOLD;
2080# endif
2081 }
2082 else
2083#endif
2084 if (idx < wp->w_lines_valid
2085 && wp->w_lines[idx].wl_valid
2086 && wp->w_lines[idx].wl_lnum == lnum
2087 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002088 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 && srow + wp->w_lines[idx].wl_size > wp->w_height
2090#ifdef FEAT_DIFF
2091 && diff_check_fill(wp, lnum) == 0
2092#endif
2093 )
2094 {
2095 /* This line is not going to fit. Don't draw anything here,
2096 * will draw "@ " lines below. */
2097 row = wp->w_height + 1;
2098 }
2099 else
2100 {
2101#ifdef FEAT_SEARCH_EXTRA
2102 prepare_search_hl(wp, lnum);
2103#endif
2104#ifdef FEAT_SYN_HL
2105 /* Let the syntax stuff know we skipped a few lines. */
2106 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002107 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 syntax_end_parsing(syntax_last_parsed + 1);
2109#endif
2110
2111 /*
2112 * Display one line.
2113 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002114 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
2116#ifdef FEAT_FOLDING
2117 wp->w_lines[idx].wl_folded = FALSE;
2118 wp->w_lines[idx].wl_lastlnum = lnum;
2119#endif
2120#ifdef FEAT_SYN_HL
2121 did_update = DID_LINE;
2122 syntax_last_parsed = lnum;
2123#endif
2124 }
2125
2126 wp->w_lines[idx].wl_lnum = lnum;
2127 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002128
2129 /* Past end of the window or end of the screen. Note that after
2130 * resizing wp->w_height may be end up too big. That's a problem
2131 * elsewhere, but prevent a crash here. */
2132 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002135 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2137 ++idx;
2138 break;
2139 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002140 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 wp->w_lines[idx].wl_size = row - srow;
2142 ++idx;
2143#ifdef FEAT_FOLDING
2144 lnum += fold_count + 1;
2145#else
2146 ++lnum;
2147#endif
2148 }
2149 else
2150 {
2151 /* This line does not need updating, advance to the next one */
2152 row += wp->w_lines[idx++].wl_size;
2153 if (row > wp->w_height) /* past end of screen */
2154 break;
2155#ifdef FEAT_FOLDING
2156 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2157#else
2158 ++lnum;
2159#endif
2160#ifdef FEAT_SYN_HL
2161 did_update = DID_NONE;
2162#endif
2163 }
2164
2165 if (lnum > buf->b_ml.ml_line_count)
2166 {
2167 eof = TRUE;
2168 break;
2169 }
2170 }
2171 /*
2172 * End of loop over all window lines.
2173 */
2174
2175
2176 if (idx > wp->w_lines_valid)
2177 wp->w_lines_valid = idx;
2178
2179#ifdef FEAT_SYN_HL
2180 /*
2181 * Let the syntax stuff know we stop parsing here.
2182 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002183 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 syntax_end_parsing(syntax_last_parsed + 1);
2185#endif
2186
2187 /*
2188 * If we didn't hit the end of the file, and we didn't finish the last
2189 * line we were working on, then the line didn't fit.
2190 */
2191 wp->w_empty_rows = 0;
2192#ifdef FEAT_DIFF
2193 wp->w_filler_rows = 0;
2194#endif
2195 if (!eof && !didline)
2196 {
2197 if (lnum == wp->w_topline)
2198 {
2199 /*
2200 * Single line that does not fit!
2201 * Don't overwrite it, it can be edited.
2202 */
2203 wp->w_botline = lnum + 1;
2204 }
2205#ifdef FEAT_DIFF
2206 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2207 {
2208 /* Window ends in filler lines. */
2209 wp->w_botline = lnum;
2210 wp->w_filler_rows = wp->w_height - srow;
2211 }
2212#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002213 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2214 {
2215 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2216
2217 /*
2218 * Last line isn't finished: Display "@@@" in the last screen line.
2219 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002220 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002221 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002222 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002223 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002224 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002225 set_empty_rows(wp, srow);
2226 wp->w_botline = lnum;
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2229 {
2230 /*
2231 * Last line isn't finished: Display "@@@" at the end.
2232 */
2233 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2234 W_WINROW(wp) + wp->w_height,
2235 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002236 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 set_empty_rows(wp, srow);
2238 wp->w_botline = lnum;
2239 }
2240 else
2241 {
2242 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2243 wp->w_botline = lnum;
2244 }
2245 }
2246 else
2247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 if (eof) /* we hit the end of the file */
2250 {
2251 wp->w_botline = buf->b_ml.ml_line_count + 1;
2252#ifdef FEAT_DIFF
2253 j = diff_check_fill(wp, wp->w_botline);
2254 if (j > 0 && !wp->w_botfill)
2255 {
2256 /*
2257 * Display filler lines at the end of the file
2258 */
2259 if (char2cells(fill_diff) > 1)
2260 i = '-';
2261 else
2262 i = fill_diff;
2263 if (row + j > wp->w_height)
2264 j = wp->w_height - row;
2265 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2266 row += j;
2267 }
2268#endif
2269 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002270 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 wp->w_botline = lnum;
2272
2273 /* make sure the rest of the screen is blank */
2274 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002275 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002278#ifdef SYN_TIME_LIMIT
2279 syn_set_timeout(NULL);
2280#endif
2281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 /* Reset the type of redrawing required, the window has been updated. */
2283 wp->w_redr_type = 0;
2284#ifdef FEAT_DIFF
2285 wp->w_old_topfill = wp->w_topfill;
2286 wp->w_old_botfill = wp->w_botfill;
2287#endif
2288
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002289 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 /*
2292 * There is a trick with w_botline. If we invalidate it on each
2293 * change that might modify it, this will cause a lot of expensive
2294 * calls to plines() in update_topline() each time. Therefore the
2295 * value of w_botline is often approximated, and this value is used to
2296 * compute the value of w_topline. If the value of w_botline was
2297 * wrong, check that the value of w_topline is correct (cursor is on
2298 * the visible part of the text). If it's not, we need to redraw
2299 * again. Mostly this just means scrolling up a few lines, so it
2300 * doesn't look too bad. Only do this for the current window (where
2301 * changes are relevant).
2302 */
2303 wp->w_valid |= VALID_BOTLINE;
2304 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2305 {
2306 recursive = TRUE;
2307 curwin->w_valid &= ~VALID_TOPLINE;
2308 update_topline(); /* may invalidate w_botline again */
2309 if (must_redraw != 0)
2310 {
2311 /* Don't update for changes in buffer again. */
2312 i = curbuf->b_mod_set;
2313 curbuf->b_mod_set = FALSE;
2314 win_update(curwin);
2315 must_redraw = 0;
2316 curbuf->b_mod_set = i;
2317 }
2318 recursive = FALSE;
2319 }
2320 }
2321
2322#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2323 /* restore got_int, unless CTRL-C was hit while redrawing */
2324 if (!got_int)
2325 got_int = save_got_int;
2326#endif
2327}
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2331 * as the filler character.
2332 */
2333 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002334win_draw_end(
2335 win_T *wp,
2336 int c1,
2337 int c2,
2338 int row,
2339 int endrow,
2340 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2343 int n = 0;
2344# define FDC_OFF n
2345#else
2346# define FDC_OFF 0
2347#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002348#ifdef FEAT_FOLDING
2349 int fdc = compute_foldcolumn(wp, 0);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352#ifdef FEAT_RIGHTLEFT
2353 if (wp->w_p_rl)
2354 {
2355 /* No check for cmdline window: should never be right-left. */
2356# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002357 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 if (n > 0)
2360 {
2361 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002362 if (n > wp->w_width)
2363 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2365 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002366 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368# endif
2369# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002370 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 int nn = n + 2;
2373
2374 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002375 if (nn > wp->w_width)
2376 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2378 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 n = nn;
2381 }
2382# endif
2383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002384 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2387 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002388 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390 else
2391#endif
2392 {
2393#ifdef FEAT_CMDWIN
2394 if (cmdwin_type != 0 && wp == curwin)
2395 {
2396 /* draw the cmdline character in the leftmost column */
2397 n = 1;
2398 if (n > wp->w_width)
2399 n = wp->w_width;
2400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002401 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404#endif
2405#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002406 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002408 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002411 if (nn > wp->w_width)
2412 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002414 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002415 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 n = nn;
2417 }
2418#endif
2419#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002420 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 int nn = n + 2;
2423
2424 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002425 if (nn > wp->w_width)
2426 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002428 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 n = nn;
2431 }
2432#endif
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002434 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 set_empty_rows(wp, row);
2438}
2439
Bram Moolenaar1a384422010-07-14 19:53:30 +02002440#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002441static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002442
2443/*
2444 * Advance **color_cols and return TRUE when there are columns to draw.
2445 */
2446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002447advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002448{
2449 while (**color_cols >= 0 && vcol > **color_cols)
2450 ++*color_cols;
2451 return (**color_cols >= 0);
2452}
2453#endif
2454
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002455#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2456/*
2457 * Copy "text" to ScreenLines using "attr".
2458 * Returns the next screen column.
2459 */
2460 static int
2461text_to_screenline(win_T *wp, char_u *text, int col)
2462{
2463 int off = (int)(current_ScreenLine - ScreenLines);
2464
2465#ifdef FEAT_MBYTE
2466 if (has_mbyte)
2467 {
2468 int cells;
2469 int u8c, u8cc[MAX_MCO];
2470 int i;
2471 int idx;
2472 int c_len;
2473 char_u *p;
2474# ifdef FEAT_ARABIC
2475 int prev_c = 0; /* previous Arabic character */
2476 int prev_c1 = 0; /* first composing char for prev_c */
2477# endif
2478
2479# ifdef FEAT_RIGHTLEFT
2480 if (wp->w_p_rl)
2481 idx = off;
2482 else
2483# endif
2484 idx = off + col;
2485
2486 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2487 for (p = text; *p != NUL; )
2488 {
2489 cells = (*mb_ptr2cells)(p);
2490 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002491 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002492# ifdef FEAT_RIGHTLEFT
2493 - (wp->w_p_rl ? col : 0)
2494# endif
2495 )
2496 break;
2497 ScreenLines[idx] = *p;
2498 if (enc_utf8)
2499 {
2500 u8c = utfc_ptr2char(p, u8cc);
2501 if (*p < 0x80 && u8cc[0] == 0)
2502 {
2503 ScreenLinesUC[idx] = 0;
2504#ifdef FEAT_ARABIC
2505 prev_c = u8c;
2506#endif
2507 }
2508 else
2509 {
2510#ifdef FEAT_ARABIC
2511 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2512 {
2513 /* Do Arabic shaping. */
2514 int pc, pc1, nc;
2515 int pcc[MAX_MCO];
2516 int firstbyte = *p;
2517
2518 /* The idea of what is the previous and next
2519 * character depends on 'rightleft'. */
2520 if (wp->w_p_rl)
2521 {
2522 pc = prev_c;
2523 pc1 = prev_c1;
2524 nc = utf_ptr2char(p + c_len);
2525 prev_c1 = u8cc[0];
2526 }
2527 else
2528 {
2529 pc = utfc_ptr2char(p + c_len, pcc);
2530 nc = prev_c;
2531 pc1 = pcc[0];
2532 }
2533 prev_c = u8c;
2534
2535 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2536 pc, pc1, nc);
2537 ScreenLines[idx] = firstbyte;
2538 }
2539 else
2540 prev_c = u8c;
2541#endif
2542 /* Non-BMP character: display as ? or fullwidth ?. */
2543#ifdef UNICODE16
2544 if (u8c >= 0x10000)
2545 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2546 else
2547#endif
2548 ScreenLinesUC[idx] = u8c;
2549 for (i = 0; i < Screen_mco; ++i)
2550 {
2551 ScreenLinesC[i][idx] = u8cc[i];
2552 if (u8cc[i] == 0)
2553 break;
2554 }
2555 }
2556 if (cells > 1)
2557 ScreenLines[idx + 1] = 0;
2558 }
2559 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2560 /* double-byte single width character */
2561 ScreenLines2[idx] = p[1];
2562 else if (cells > 1)
2563 /* double-width character */
2564 ScreenLines[idx + 1] = p[1];
2565 col += cells;
2566 idx += cells;
2567 p += c_len;
2568 }
2569 }
2570 else
2571#endif
2572 {
2573 int len = (int)STRLEN(text);
2574
Bram Moolenaar02631462017-09-22 15:20:32 +02002575 if (len > wp->w_width - col)
2576 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002577 if (len > 0)
2578 {
2579#ifdef FEAT_RIGHTLEFT
2580 if (wp->w_p_rl)
2581 STRNCPY(current_ScreenLine, text, len);
2582 else
2583#endif
2584 STRNCPY(current_ScreenLine + col, text, len);
2585 col += len;
2586 }
2587 }
2588 return col;
2589}
2590#endif
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#ifdef FEAT_FOLDING
2593/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002594 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2595 * space is available for window "wp", minus "col".
2596 */
2597 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002598compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002599{
2600 int fdc = wp->w_p_fdc;
2601 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002602 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002603
2604 if (fdc > wwidth - (col + wmw))
2605 fdc = wwidth - (col + wmw);
2606 return fdc;
2607}
2608
2609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 * Display one folded line.
2611 */
2612 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002613fold_line(
2614 win_T *wp,
2615 long fold_count,
2616 foldinfo_T *foldinfo,
2617 linenr_T lnum,
2618 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002620 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 pos_T *top, *bot;
2622 linenr_T lnume = lnum + fold_count - 1;
2623 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002624 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int col;
2627 int txtcol;
2628 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002629 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
2631 /* Build the fold line:
2632 * 1. Add the cmdwin_type for the command-line window
2633 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002634 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * 4. Compose the text
2636 * 5. Add the text
2637 * 6. set highlighting for the Visual area an other text
2638 */
2639 col = 0;
2640
2641 /*
2642 * 1. Add the cmdwin_type for the command-line window
2643 * Ignores 'rightleft', this window is never right-left.
2644 */
2645#ifdef FEAT_CMDWIN
2646 if (cmdwin_type != 0 && wp == curwin)
2647 {
2648 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002649 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#ifdef FEAT_MBYTE
2651 if (enc_utf8)
2652 ScreenLinesUC[off] = 0;
2653#endif
2654 ++col;
2655 }
2656#endif
2657
2658 /*
2659 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002662 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (fdc > 0)
2664 {
2665 fill_foldcolumn(buf, wp, TRUE, lnum);
2666#ifdef FEAT_RIGHTLEFT
2667 if (wp->w_p_rl)
2668 {
2669 int i;
2670
Bram Moolenaar02631462017-09-22 15:20:32 +02002671 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002672 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /* reverse the fold column */
2674 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002675 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 else
2678#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002679 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 col += fdc;
2681 }
2682
2683#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002684# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2685 for (ri = 0; ri < l; ++ri) \
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002687 else \
2688 for (ri = 0; ri < l; ++ri) \
2689 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002691# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2692 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#endif
2694
Bram Moolenaar64486672010-05-16 15:46:46 +02002695 /* Set all attributes of the 'number' or 'relativenumber' column and the
2696 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002697 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699#ifdef FEAT_SIGNS
2700 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002701 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (len > 0)
2705 {
2706 if (len > 2)
2707 len = 2;
2708# ifdef FEAT_RIGHTLEFT
2709 if (wp->w_p_rl)
2710 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002712 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 else
2714# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col += len;
2717 }
2718 }
2719#endif
2720
2721 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002722 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002724 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002726 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (len > 0)
2728 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002729 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002730 long num;
2731 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002732
2733 if (len > w + 1)
2734 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002735
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002736 if (wp->w_p_nu && !wp->w_p_rnu)
2737 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 num = (long)lnum;
2739 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002740 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002741 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002742 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002743 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002744 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002745 /* 'number' + 'relativenumber': cursor line shows absolute
2746 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002747 num = lnum;
2748 fmt = "%-*ld ";
2749 }
2750 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002751
Bram Moolenaar700e7342013-01-30 12:31:36 +01002752 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753#ifdef FEAT_RIGHTLEFT
2754 if (wp->w_p_rl)
2755 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002757 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
2759#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002760 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 col += len;
2762 }
2763 }
2764
2765 /*
2766 * 4. Compose the folded-line string with 'foldtext', if set.
2767 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002768 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 txtcol = col; /* remember where text starts */
2771
2772 /*
2773 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2774 * Right-left text is put in columns 0 - number-col, normal text is put
2775 * in columns number-col - window-width.
2776 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002777 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /* Fill the rest of the line with the fold filler */
2780#ifdef FEAT_RIGHTLEFT
2781 if (wp->w_p_rl)
2782 col -= txtcol;
2783#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002784 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#ifdef FEAT_RIGHTLEFT
2786 - (wp->w_p_rl ? txtcol : 0)
2787#endif
2788 )
2789 {
2790#ifdef FEAT_MBYTE
2791 if (enc_utf8)
2792 {
2793 if (fill_fold >= 0x80)
2794 {
2795 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002796 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002797 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002802 ScreenLines[off + col] = fill_fold;
2803 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002808 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
2811 if (text != buf)
2812 vim_free(text);
2813
2814 /*
2815 * 6. set highlighting for the Visual area an other text.
2816 * If all folded lines are in the Visual area, highlight the line.
2817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2819 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002820 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
2822 /* Visual is after curwin->w_cursor */
2823 top = &curwin->w_cursor;
2824 bot = &VIsual;
2825 }
2826 else
2827 {
2828 /* Visual is before curwin->w_cursor */
2829 top = &VIsual;
2830 bot = &curwin->w_cursor;
2831 }
2832 if (lnum >= top->lnum
2833 && lnume <= bot->lnum
2834 && (VIsual_mode != 'v'
2835 || ((lnum > top->lnum
2836 || (lnum == top->lnum
2837 && top->col == 0))
2838 && (lnume < bot->lnum
2839 || (lnume == bot->lnum
2840 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002841 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
2843 if (VIsual_mode == Ctrl_V)
2844 {
2845 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002846 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002848 if (wp->w_old_cursor_lcol != MAXCOL
2849 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002850 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 len = wp->w_old_cursor_lcol;
2852 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002854 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002855 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 }
2858 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002861 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002866#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002867 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2868 if (wp->w_p_cc_cols)
2869 {
2870 int i = 0;
2871 int j = wp->w_p_cc_cols[i];
2872 int old_txtcol = txtcol;
2873
2874 while (j > -1)
2875 {
2876 txtcol += j;
2877 if (wp->w_p_wrap)
2878 txtcol -= wp->w_skipcol;
2879 else
2880 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002881 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002882 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002883 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002884 txtcol = old_txtcol;
2885 j = wp->w_p_cc_cols[++i];
2886 }
2887 }
2888
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002889 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002890 if (wp->w_p_cuc)
2891 {
2892 txtcol += wp->w_virtcol;
2893 if (wp->w_p_wrap)
2894 txtcol -= wp->w_skipcol;
2895 else
2896 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002897 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002898 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002899 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002900 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2904 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
2906 /*
2907 * Update w_cline_height and w_cline_folded if the cursor line was
2908 * updated (saves a call to plines() later).
2909 */
2910 if (wp == curwin
2911 && lnum <= curwin->w_cursor.lnum
2912 && lnume >= curwin->w_cursor.lnum)
2913 {
2914 curwin->w_cline_row = row;
2915 curwin->w_cline_height = 1;
2916 curwin->w_cline_folded = TRUE;
2917 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2918 }
2919}
2920
2921/*
2922 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2923 */
2924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002925copy_text_attr(
2926 int off,
2927 char_u *buf,
2928 int len,
2929 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002931 int i;
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 mch_memmove(ScreenLines + off, buf, (size_t)len);
2934# ifdef FEAT_MBYTE
2935 if (enc_utf8)
2936 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2937# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002938 for (i = 0; i < len; ++i)
2939 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
2943 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002944 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
2946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002947fill_foldcolumn(
2948 char_u *p,
2949 win_T *wp,
2950 int closed, /* TRUE of FALSE */
2951 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 int i = 0;
2954 int level;
2955 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002956 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002957 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002960 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 level = win_foldinfo.fi_level;
2963 if (level > 0)
2964 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002965 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /* If the column is too narrow, we start at the lowest level that
2969 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (first_level < 1)
2972 first_level = 1;
2973
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
2976 if (win_foldinfo.fi_lnum == lnum
2977 && first_level + i >= win_foldinfo.fi_low_level)
2978 p[i] = '-';
2979 else if (first_level == 1)
2980 p[i] = '|';
2981 else if (first_level + i <= 9)
2982 p[i] = '0' + first_level + i;
2983 else
2984 p[i] = '>';
2985 if (first_level + i == level)
2986 break;
2987 }
2988 }
2989 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002990 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992#endif /* FEAT_FOLDING */
2993
2994/*
2995 * Display line "lnum" of window 'wp' on the screen.
2996 * Start at row "startrow", stop when "endrow" is reached.
2997 * wp->w_virtcol needs to be valid.
2998 *
2999 * Return the number of last row the line occupies.
3000 */
3001 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003002win_line(
3003 win_T *wp,
3004 linenr_T lnum,
3005 int startrow,
3006 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003007 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003009 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3011 int c = 0; /* init for GCC */
3012 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003013#ifdef FEAT_LINEBREAK
3014 long vcol_sbr = -1; /* virtual column after showbreak */
3015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 long vcol_prev = -1; /* "vcol" of previous character */
3017 char_u *line; /* current line */
3018 char_u *ptr; /* current position in "line" */
3019 int row; /* row in the window, excl w_winrow */
3020 int screen_row; /* row on the screen, incl w_winrow */
3021
3022 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3023 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003024 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003025 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 int c_extra = NUL; /* extra chars, all the same */
3027 int extra_attr = 0; /* attributes when n_extra != 0 */
3028 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3029 displaying lcs_eol at end-of-line */
3030 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3031 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3032
3033 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3034 int saved_n_extra = 0;
3035 char_u *saved_p_extra = NULL;
3036 int saved_c_extra = 0;
3037 int saved_char_attr = 0;
3038
3039 int n_attr = 0; /* chars with special attr */
3040 int saved_attr2 = 0; /* char_attr saved for n_attr */
3041 int n_attr3 = 0; /* chars with overruling special attr */
3042 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3043
3044 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3045
3046 int fromcol, tocol; /* start/end of inverting */
3047 int fromcol_prev = -2; /* start of inverting after cursor */
3048 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003050 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 pos_T pos;
3052 long v;
3053
3054 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003055 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3057 in this line */
3058 int attr = 0; /* attributes for area highlighting */
3059 int area_attr = 0; /* attributes desired by highlighting */
3060 int search_attr = 0; /* attributes desired by 'hlsearch' */
3061#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003062 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 int syntax_attr = 0; /* attributes desired by syntax */
3064 int has_syntax = FALSE; /* this buffer has syntax highl. */
3065 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003066 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003067 int draw_color_col = FALSE; /* highlight colorcolumn */
3068 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003069#endif
3070#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003071 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003072# define SPWORDLEN 150
3073 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003074 int nextlinecol = 0; /* column where nextline[] starts */
3075 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003076 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003077 int spell_attr = 0; /* attributes desired by spelling */
3078 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003079 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3080 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003082 static int cap_col = -1; /* column to check for Cap word */
3083 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003084 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#endif
3086 int extra_check; /* has syntax or linebreak */
3087#ifdef FEAT_MBYTE
3088 int multi_attr = 0; /* attributes desired by multibyte */
3089 int mb_l = 1; /* multi-byte byte length */
3090 int mb_c = 0; /* decoded multi-byte character */
3091 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003092 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
3094#ifdef FEAT_DIFF
3095 int filler_lines; /* nr of filler lines to be drawn */
3096 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003097 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int change_start = MAXCOL; /* first col of changed area */
3099 int change_end = -1; /* last col of changed area */
3100#endif
3101 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3102#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003103 int need_showbreak = FALSE; /* overlong line, skipping first x
3104 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003106#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003107 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003109 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#endif
3111#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003112 matchitem_T *cur; /* points to the match list */
3113 match_T *shl; /* points to search_hl or a match */
3114 int shl_flag; /* flag to indicate whether search_hl
3115 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003116 int pos_inprogress; /* marks that position match search is
3117 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003118 int prevcol_hl_flag; /* flag to indicate whether prevcol
3119 equals startcol of search_hl or one
3120 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122#ifdef FEAT_ARABIC
3123 int prev_c = 0; /* previous Arabic character */
3124 int prev_c1 = 0; /* first composing char for prev_c */
3125#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003126#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003127 int did_line_attr = 0;
3128#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003129#ifdef FEAT_TERMINAL
3130 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003131 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /* draw_state: items that are drawn in sequence: */
3135#define WL_START 0 /* nothing done yet */
3136#ifdef FEAT_CMDWIN
3137# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3138#else
3139# define WL_CMDLINE WL_START
3140#endif
3141#ifdef FEAT_FOLDING
3142# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3143#else
3144# define WL_FOLD WL_CMDLINE
3145#endif
3146#ifdef FEAT_SIGNS
3147# define WL_SIGN WL_FOLD + 1 /* column for signs */
3148#else
3149# define WL_SIGN WL_FOLD /* column for signs */
3150#endif
3151#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003152#ifdef FEAT_LINEBREAK
3153# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003155# define WL_BRI WL_NR
3156#endif
3157#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3158# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3159#else
3160# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#define WL_LINE WL_SBR + 1 /* text in the line */
3163 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003164#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int feedback_col = 0;
3166 int feedback_old_attr = -1;
3167#endif
3168
Bram Moolenaar860cae12010-06-05 23:22:07 +02003169#ifdef FEAT_CONCEAL
3170 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003171 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003172 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003173 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003174 int is_concealing = FALSE;
3175 int boguscols = 0; /* nonexistent columns added to force
3176 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003177 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003178 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003179 int match_conc = 0; /* cchar for match functions */
3180 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003181 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003182# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003183# define FIX_FOR_BOGUSCOLS \
3184 { \
3185 n_extra += vcol_off; \
3186 vcol -= vcol_off; \
3187 vcol_off = 0; \
3188 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003189 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003190 boguscols = 0; \
3191 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003192#else
3193# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 if (startrow > endrow) /* past the end already! */
3197 return startrow;
3198
3199 row = startrow;
3200 screen_row = row + W_WINROW(wp);
3201
3202 /*
3203 * To speed up the loop below, set extra_check when there is linebreak,
3204 * trailing white space and/or syntax processing to be done.
3205 */
3206#ifdef FEAT_LINEBREAK
3207 extra_check = wp->w_p_lbr;
3208#else
3209 extra_check = 0;
3210#endif
3211#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003212 if (syntax_present(wp) && !wp->w_s->b_syn_error
3213# ifdef SYN_TIME_LIMIT
3214 && !wp->w_s->b_syn_slow
3215# endif
3216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
3218 /* Prepare for syntax highlighting in this line. When there is an
3219 * error, stop syntax highlighting. */
3220 save_did_emsg = did_emsg;
3221 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003222 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
3226 {
3227 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003228#ifdef SYN_TIME_LIMIT
3229 if (!wp->w_s->b_syn_slow)
3230#endif
3231 {
3232 has_syntax = TRUE;
3233 extra_check = TRUE;
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003237
3238 /* Check for columns to display for 'colorcolumn'. */
3239 color_cols = wp->w_p_cc_cols;
3240 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003241 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003242#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003243
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003244#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003245 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003246 {
3247 extra_check = TRUE;
3248 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003249 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003250 }
3251#endif
3252
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003253#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003254 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003255 && *wp->w_s->b_p_spl != NUL
3256 && wp->w_s->b_langp.ga_len > 0
3257 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003258 {
3259 /* Prepare for spell checking. */
3260 has_spell = TRUE;
3261 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003262
3263 /* Get the start of the next line, so that words that wrap to the next
3264 * line are found too: "et<line-break>al.".
3265 * Trick: skip a few chars for C/shell/Vim comments */
3266 nextline[SPWORDLEN] = NUL;
3267 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3268 {
3269 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3270 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3271 }
3272
3273 /* When a word wrapped from the previous line the start of the current
3274 * line is valid. */
3275 if (lnum == checked_lnum)
3276 cur_checked_col = checked_col;
3277 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003278
3279 /* When there was a sentence end in the previous line may require a
3280 * word starting with capital in this line. In line 1 always check
3281 * the first word. */
3282 if (lnum != capcol_lnum)
3283 cap_col = -1;
3284 if (lnum == 1)
3285 cap_col = 0;
3286 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#endif
3289
3290 /*
3291 * handle visual active in this window
3292 */
3293 fromcol = -10;
3294 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3296 {
3297 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003298 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 top = &curwin->w_cursor;
3301 bot = &VIsual;
3302 }
3303 else /* Visual is before curwin->w_cursor */
3304 {
3305 top = &VIsual;
3306 bot = &curwin->w_cursor;
3307 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003308 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (VIsual_mode == Ctrl_V) /* block mode */
3310 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003311 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 fromcol = wp->w_old_cursor_fcol;
3314 tocol = wp->w_old_cursor_lcol;
3315 }
3316 }
3317 else /* non-block mode */
3318 {
3319 if (lnum > top->lnum && lnum <= bot->lnum)
3320 fromcol = 0;
3321 else if (lnum == top->lnum)
3322 {
3323 if (VIsual_mode == 'V') /* linewise */
3324 fromcol = 0;
3325 else
3326 {
3327 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3328 if (gchar_pos(top) == NUL)
3329 tocol = fromcol + 1;
3330 }
3331 }
3332 if (VIsual_mode != 'V' && lnum == bot->lnum)
3333 {
3334 if (*p_sel == 'e' && bot->col == 0
3335#ifdef FEAT_VIRTUALEDIT
3336 && bot->coladd == 0
3337#endif
3338 )
3339 {
3340 fromcol = -10;
3341 tocol = MAXCOL;
3342 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003343 else if (bot->col == MAXCOL)
3344 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346 {
3347 pos = *bot;
3348 if (*p_sel == 'e')
3349 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3350 else
3351 {
3352 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3353 ++tocol;
3354 }
3355 }
3356 }
3357 }
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 /* Check if the character under the cursor should not be inverted */
3360 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003361#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 )
3365 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 /* if inverting in this line set area_highlighting */
3368 if (fromcol >= 0)
3369 {
3370 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003371 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003373 if ((clip_star.available && !clip_star.owned
3374 && clip_isautosel_star())
3375 || (clip_plus.available && !clip_plus.owned
3376 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003377 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
3379 }
3380 }
3381
3382 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003383 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003385 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 && wp == curwin
3387 && lnum >= curwin->w_cursor.lnum
3388 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3389 {
3390 if (lnum == curwin->w_cursor.lnum)
3391 getvcol(curwin, &(curwin->w_cursor),
3392 (colnr_T *)&fromcol, NULL, NULL);
3393 else
3394 fromcol = 0;
3395 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3396 {
3397 pos.lnum = lnum;
3398 pos.col = search_match_endcol;
3399 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3400 }
3401 else
3402 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003403 /* do at least one character; happens when past end of line */
3404 if (fromcol == tocol)
3405 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003407 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409
3410#ifdef FEAT_DIFF
3411 filler_lines = diff_check(wp, lnum);
3412 if (filler_lines < 0)
3413 {
3414 if (filler_lines == -1)
3415 {
3416 if (diff_find_change(wp, lnum, &change_start, &change_end))
3417 diff_hlf = HLF_ADD; /* added line */
3418 else if (change_start == 0)
3419 diff_hlf = HLF_TXD; /* changed text */
3420 else
3421 diff_hlf = HLF_CHD; /* changed line */
3422 }
3423 else
3424 diff_hlf = HLF_ADD; /* added line */
3425 filler_lines = 0;
3426 area_highlighting = TRUE;
3427 }
3428 if (lnum == wp->w_topline)
3429 filler_lines = wp->w_topfill;
3430 filler_todo = filler_lines;
3431#endif
3432
3433#ifdef LINE_ATTR
3434# ifdef FEAT_SIGNS
3435 /* If this line has a sign with line highlighting set line_attr. */
3436 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3437 if (v != 0)
3438 line_attr = sign_get_attr((int)v, TRUE);
3439# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003440# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003442 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003443 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444# endif
3445 if (line_attr != 0)
3446 area_highlighting = TRUE;
3447#endif
3448
3449 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3450 ptr = line;
3451
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003452#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003453 if (has_spell)
3454 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003455 /* For checking first word with a capital skip white space. */
3456 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003457 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003458
Bram Moolenaar30abd282005-06-22 22:35:10 +00003459 /* To be able to spell-check over line boundaries copy the end of the
3460 * current line into nextline[]. Above the start of the next line was
3461 * copied to nextline[SPWORDLEN]. */
3462 if (nextline[SPWORDLEN] == NUL)
3463 {
3464 /* No next line or it is empty. */
3465 nextlinecol = MAXCOL;
3466 nextline_idx = 0;
3467 }
3468 else
3469 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003470 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003471 if (v < SPWORDLEN)
3472 {
3473 /* Short line, use it completely and append the start of the
3474 * next line. */
3475 nextlinecol = 0;
3476 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003477 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003478 nextline_idx = v + 1;
3479 }
3480 else
3481 {
3482 /* Long line, use only the last SPWORDLEN bytes. */
3483 nextlinecol = v - SPWORDLEN;
3484 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3485 nextline_idx = SPWORDLEN + 1;
3486 }
3487 }
3488 }
3489#endif
3490
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003491 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003493 if (lcs_space || lcs_trail)
3494 extra_check = TRUE;
3495 /* find start of trailing whitespace */
3496 if (lcs_trail)
3497 {
3498 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003499 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003500 --trailcol;
3501 trailcol += (colnr_T) (ptr - line);
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504
3505 /*
3506 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3507 * first character to be displayed.
3508 */
3509 if (wp->w_p_wrap)
3510 v = wp->w_skipcol;
3511 else
3512 v = wp->w_leftcol;
3513 if (v > 0)
3514 {
3515#ifdef FEAT_MBYTE
3516 char_u *prev_ptr = ptr;
3517#endif
3518 while (vcol < v && *ptr != NUL)
3519 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003520 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 vcol += c;
3522#ifdef FEAT_MBYTE
3523 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003525 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003528 /* When:
3529 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003530 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003531 * - 'virtualedit' is set, or
3532 * - the visual mode is active,
3533 * the end of the line may be before the start of the displayed part.
3534 */
3535 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003536#ifdef FEAT_SYN_HL
3537 wp->w_p_cuc || draw_color_col ||
3538#endif
3539#ifdef FEAT_VIRTUALEDIT
3540 virtual_active() ||
3541#endif
3542 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 /* Handle a character that's not completely on the screen: Put ptr at
3548 * that character but skip the first few screen characters. */
3549 if (vcol > v)
3550 {
3551 vcol -= c;
3552#ifdef FEAT_MBYTE
3553 ptr = prev_ptr;
3554#else
3555 --ptr;
3556#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003557 /* If the character fits on the screen, don't need to skip it.
3558 * Except for a TAB. */
3559 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003560#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003561 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003562#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003563 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003564 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567 /*
3568 * Adjust for when the inverted text is before the screen,
3569 * and when the start of the inverted text is before the screen.
3570 */
3571 if (tocol <= vcol)
3572 fromcol = 0;
3573 else if (fromcol >= 0 && fromcol < vcol)
3574 fromcol = vcol;
3575
3576#ifdef FEAT_LINEBREAK
3577 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3578 if (wp->w_p_wrap)
3579 need_showbreak = TRUE;
3580#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003581#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003582 /* When spell checking a word we need to figure out the start of the
3583 * word and if it's badly spelled or not. */
3584 if (has_spell)
3585 {
3586 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003587 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003588 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589
3590 pos = wp->w_cursor;
3591 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003592 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003593 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003594
3595 /* spell_move_to() may call ml_get() and make "line" invalid */
3596 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3597 ptr = line + linecol;
3598
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003599 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003600 {
3601 /* no bad word found at line start, don't check until end of a
3602 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003603 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003604 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003605 }
3606 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003607 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003608 /* bad word found, use attributes until end of word */
3609 word_end = wp->w_cursor.col + len + 1;
3610
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003611 /* Turn index into actual attributes. */
3612 if (spell_hlf != HLF_COUNT)
3613 spell_attr = highlight_attr[spell_hlf];
3614 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003615 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003617# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003618 /* Need to restart syntax highlighting for this line. */
3619 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003620 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003621# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003622 }
3623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625
3626 /*
3627 * Correct highlighting for cursor that can't be disabled.
3628 * Avoids having to check this for each character.
3629 */
3630 if (fromcol >= 0)
3631 {
3632 if (noinvcur)
3633 {
3634 if ((colnr_T)fromcol == wp->w_virtcol)
3635 {
3636 /* highlighting starts at cursor, let it start just after the
3637 * cursor */
3638 fromcol_prev = fromcol;
3639 fromcol = -1;
3640 }
3641 else if ((colnr_T)fromcol < wp->w_virtcol)
3642 /* restart highlighting after the cursor */
3643 fromcol_prev = wp->w_virtcol;
3644 }
3645 if (fromcol >= tocol)
3646 fromcol = -1;
3647 }
3648
3649#ifdef FEAT_SEARCH_EXTRA
3650 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003651 * Handle highlighting the last used search pattern and matches.
3652 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003654 cur = wp->w_match_head;
3655 shl_flag = FALSE;
3656 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003658 if (shl_flag == FALSE)
3659 {
3660 shl = &search_hl;
3661 shl_flag = TRUE;
3662 }
3663 else
3664 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003665 shl->startcol = MAXCOL;
3666 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003668 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003669 v = (long)(ptr - line);
3670 if (cur != NULL)
3671 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003672 next_search_hl(wp, shl, lnum, (colnr_T)v,
3673 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674
3675 /* Need to get the line again, a multi-line regexp may have made it
3676 * invalid. */
3677 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3678 ptr = line + v;
3679
3680 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003682 if (shl->lnum == lnum)
3683 shl->startcol = shl->rm.startpos[0].col;
3684 else
3685 shl->startcol = 0;
3686 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3687 - shl->rm.startpos[0].lnum)
3688 shl->endcol = shl->rm.endpos[0].col;
3689 else
3690 shl->endcol = MAXCOL;
3691 /* Highlight one character for an empty match. */
3692 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003695 if (has_mbyte && line[shl->endcol] != NUL)
3696 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3697 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003699 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003701 if ((long)shl->startcol < v) /* match at leftcol */
3702 {
3703 shl->attr_cur = shl->attr;
3704 search_attr = shl->attr;
3705 }
3706 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003708 if (shl != &search_hl && cur != NULL)
3709 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711#endif
3712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003713#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003714 /* Cursor line highlighting for 'cursorline' in the current window. Not
3715 * when Visual mode is active, because it's not clear what is selected
3716 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003717 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003718 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003719 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003720 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003721 area_highlighting = TRUE;
3722 }
3723#endif
3724
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003725 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 col = 0;
3727#ifdef FEAT_RIGHTLEFT
3728 if (wp->w_p_rl)
3729 {
3730 /* Rightleft window: process the text in the normal direction, but put
3731 * it in current_ScreenLine[] from right to left. Start at the
3732 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003733 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 off += col;
3735 }
3736#endif
3737
3738 /*
3739 * Repeat for the whole displayed line.
3740 */
3741 for (;;)
3742 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003743#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003744 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 /* Skip this quickly when working on the text. */
3747 if (draw_state != WL_LINE)
3748 {
3749#ifdef FEAT_CMDWIN
3750 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3751 {
3752 draw_state = WL_CMDLINE;
3753 if (cmdwin_type != 0 && wp == curwin)
3754 {
3755 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003757 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003758 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 }
3761#endif
3762
3763#ifdef FEAT_FOLDING
3764 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3765 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003766 int fdc = compute_foldcolumn(wp, 0);
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003769 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003771 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003772 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003773 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003774 p_extra_free = alloc(12 + 1);
3775
3776 if (p_extra_free != NULL)
3777 {
3778 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3779 n_extra = fdc;
3780 p_extra_free[n_extra] = NUL;
3781 p_extra = p_extra_free;
3782 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003783 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 }
3787#endif
3788
3789#ifdef FEAT_SIGNS
3790 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3791 {
3792 draw_state = WL_SIGN;
3793 /* Show the sign column when there are any signs in this
3794 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003795 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003797 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003799 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800# endif
3801
3802 /* Draw two cells with the sign value or blank. */
3803 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003804 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 n_extra = 2;
3806
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003807 if (row == startrow
3808#ifdef FEAT_DIFF
3809 + filler_lines && filler_todo <= 0
3810#endif
3811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3814 SIGN_TEXT);
3815# ifdef FEAT_SIGN_ICONS
3816 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3817 SIGN_ICON);
3818 if (gui.in_use && icon_sign != 0)
3819 {
3820 /* Use the image in this position. */
3821 c_extra = SIGN_BYTE;
3822# ifdef FEAT_NETBEANS_INTG
3823 if (buf_signcount(wp->w_buffer, lnum) > 1)
3824 c_extra = MULTISIGN_BYTE;
3825# endif
3826 char_attr = icon_sign;
3827 }
3828 else
3829# endif
3830 if (text_sign != 0)
3831 {
3832 p_extra = sign_get_text(text_sign);
3833 if (p_extra != NULL)
3834 {
3835 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003836 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 char_attr = sign_get_attr(text_sign, FALSE);
3839 }
3840 }
3841 }
3842 }
3843#endif
3844
3845 if (draw_state == WL_NR - 1 && n_extra == 0)
3846 {
3847 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003848 /* Display the absolute or relative line number. After the
3849 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3850 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 && (row == startrow
3852#ifdef FEAT_DIFF
3853 + filler_lines
3854#endif
3855 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3856 {
3857 /* Draw the line number (empty space after wrapping). */
3858 if (row == startrow
3859#ifdef FEAT_DIFF
3860 + filler_lines
3861#endif
3862 )
3863 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003864 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003865 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003866
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003867 if (wp->w_p_nu && !wp->w_p_rnu)
3868 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003869 num = (long)lnum;
3870 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003871 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003872 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003873 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003874 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003875 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003877 num = lnum;
3878 fmt = "%-*ld ";
3879 }
3880 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003881
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003883 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (wp->w_skipcol > 0)
3885 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3886 *p_extra = '-';
3887#ifdef FEAT_RIGHTLEFT
3888 if (wp->w_p_rl) /* reverse line numbers */
3889 rl_mirror(extra);
3890#endif
3891 p_extra = extra;
3892 c_extra = NUL;
3893 }
3894 else
3895 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003896 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003897 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003898#ifdef FEAT_SYN_HL
3899 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003900 * the current line differently.
3901 * TODO: Can we use CursorLine instead of CursorLineNr
3902 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003903 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003904 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003905 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909
Bram Moolenaar597a4222014-06-25 14:39:50 +02003910#ifdef FEAT_LINEBREAK
3911 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3912 && n_extra == 0 && *p_sbr != NUL)
3913 /* draw indent after showbreak value */
3914 draw_state = WL_BRI;
3915 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3916 /* After the showbreak, draw the breakindent */
3917 draw_state = WL_BRI - 1;
3918
3919 /* draw 'breakindent': indent wrapped text accordingly */
3920 if (draw_state == WL_BRI - 1 && n_extra == 0)
3921 {
3922 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003923 /* if need_showbreak is set, breakindent also applies */
3924 if (wp->w_p_bri && n_extra == 0
3925 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003926# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003927 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003928# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003929 )
3930 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003931 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003932# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003933 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003934 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003935 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003936# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003937 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3938 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003940# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003941 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003942# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003943 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003944 c_extra = ' ';
3945 n_extra = get_breakindent_win(wp,
3946 ml_get_buf(wp->w_buffer, lnum, FALSE));
3947 /* Correct end of highlighted area for 'breakindent',
3948 * required when 'linebreak' is also set. */
3949 if (tocol == vcol)
3950 tocol += n_extra;
3951 }
3952 }
3953#endif
3954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3956 if (draw_state == WL_SBR - 1 && n_extra == 0)
3957 {
3958 draw_state = WL_SBR;
3959# ifdef FEAT_DIFF
3960 if (filler_todo > 0)
3961 {
3962 /* Draw "deleted" diff line(s). */
3963 if (char2cells(fill_diff) > 1)
3964 c_extra = '-';
3965 else
3966 c_extra = fill_diff;
3967# ifdef FEAT_RIGHTLEFT
3968 if (wp->w_p_rl)
3969 n_extra = col + 1;
3970 else
3971# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003972 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003973 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975# endif
3976# ifdef FEAT_LINEBREAK
3977 if (*p_sbr != NUL && need_showbreak)
3978 {
3979 /* Draw 'showbreak' at the start of each broken line. */
3980 p_extra = p_sbr;
3981 c_extra = NUL;
3982 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003983 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003985 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 /* Correct end of highlighted area for 'showbreak',
3987 * required when 'linebreak' is also set. */
3988 if (tocol == vcol)
3989 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003990#ifdef FEAT_SYN_HL
3991 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003992 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003993 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003994 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997# endif
3998 }
3999#endif
4000
4001 if (draw_state == WL_LINE - 1 && n_extra == 0)
4002 {
4003 draw_state = WL_LINE;
4004 if (saved_n_extra)
4005 {
4006 /* Continue item from end of wrapped line. */
4007 n_extra = saved_n_extra;
4008 c_extra = saved_c_extra;
4009 p_extra = saved_p_extra;
4010 char_attr = saved_char_attr;
4011 }
4012 else
4013 char_attr = 0;
4014 }
4015 }
4016
4017 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004018 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#ifdef FEAT_DIFF
4021 && filler_todo <= 0
4022#endif
4023 )
4024 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004025 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004026 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004027 /* Pretend we have finished updating the window. Except when
4028 * 'cursorcolumn' is set. */
4029#ifdef FEAT_SYN_HL
4030 if (wp->w_p_cuc)
4031 row = wp->w_cline_row + wp->w_cline_height;
4032 else
4033#endif
4034 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036 }
4037
4038 if (draw_state == WL_LINE && area_highlighting)
4039 {
4040 /* handle Visual or match highlighting in this line */
4041 if (vcol == fromcol
4042#ifdef FEAT_MBYTE
4043 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4044 && (*mb_ptr2cells)(ptr) > 1)
4045#endif
4046 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004047 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 && vcol < tocol))
4049 area_attr = attr; /* start highlighting */
4050 else if (area_attr != 0
4051 && (vcol == tocol
4052 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055#ifdef FEAT_SEARCH_EXTRA
4056 if (!n_extra)
4057 {
4058 /*
4059 * Check for start/end of search pattern match.
4060 * After end, check for start/end of next match.
4061 * When another match, have to check for start again.
4062 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004063 * Do this for 'search_hl' and the match list (ordered by
4064 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004066 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004067 cur = wp->w_match_head;
4068 shl_flag = FALSE;
4069 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004071 if (shl_flag == FALSE
4072 && ((cur != NULL
4073 && cur->priority > SEARCH_HL_PRIORITY)
4074 || cur == NULL))
4075 {
4076 shl = &search_hl;
4077 shl_flag = TRUE;
4078 }
4079 else
4080 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004081 if (cur != NULL)
4082 cur->pos.cur = 0;
4083 pos_inprogress = TRUE;
4084 while (shl->rm.regprog != NULL
4085 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004087 if (shl->startcol != MAXCOL
4088 && v >= (long)shl->startcol
4089 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004091#ifdef FEAT_MBYTE
4092 int tmp_col = v + MB_PTR2LEN(ptr);
4093
4094 if (shl->endcol < tmp_col)
4095 shl->endcol = tmp_col;
4096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004098#ifdef FEAT_CONCEAL
4099 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4100 == cur->hlg_id)
4101 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004102 has_match_conc =
4103 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004104 match_conc = cur->conceal_char;
4105 }
4106 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004107 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004110 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
4112 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004113 next_search_hl(wp, shl, lnum, (colnr_T)v,
4114 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004115 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004116 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 /* Need to get the line again, a multi-line regexp
4119 * may have made it invalid. */
4120 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4121 ptr = line + v;
4122
4123 if (shl->lnum == lnum)
4124 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004125 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004127 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004131 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
4133 /* highlight empty match, try again after
4134 * it */
4135#ifdef FEAT_MBYTE
4136 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004137 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
4140#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004141 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143
4144 /* Loop to check if the match starts at the
4145 * current position */
4146 continue;
4147 }
4148 }
4149 break;
4150 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004151 if (shl != &search_hl && cur != NULL)
4152 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004154
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004155 /* Use attributes from match with highest priority among
4156 * 'search_hl' and the match list. */
4157 search_attr = search_hl.attr_cur;
4158 cur = wp->w_match_head;
4159 shl_flag = FALSE;
4160 while (cur != NULL || shl_flag == FALSE)
4161 {
4162 if (shl_flag == FALSE
4163 && ((cur != NULL
4164 && cur->priority > SEARCH_HL_PRIORITY)
4165 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004166 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004167 shl = &search_hl;
4168 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004169 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004170 else
4171 shl = &cur->hl;
4172 if (shl->attr_cur != 0)
4173 search_attr = shl->attr_cur;
4174 if (shl != &search_hl && cur != NULL)
4175 cur = cur->next;
4176 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004177 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004178 if (*ptr == NUL && (did_line_attr >= 1
4179 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004180 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182#endif
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004187 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4188 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004190 if (diff_hlf == HLF_TXD && ptr - line > change_end
4191 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004193 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004194 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004195 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004198
4199 /* Decide which of the highlight attributes to use. */
4200 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004201#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004202 if (area_attr != 0)
4203 char_attr = hl_combine_attr(line_attr, area_attr);
4204 else if (search_attr != 0)
4205 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004206 /* Use line_attr when not in the Visual or 'incsearch' area
4207 * (area_attr may be 0 when "noinvcur" is set). */
4208 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004209 || vcol < fromcol || vcol_prev < fromcol_prev
4210 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004211 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004212#else
4213 if (area_attr != 0)
4214 char_attr = area_attr;
4215 else if (search_attr != 0)
4216 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004217#endif
4218 else
4219 {
4220 attr_pri = FALSE;
4221#ifdef FEAT_SYN_HL
4222 if (has_syntax)
4223 char_attr = syntax_attr;
4224 else
4225#endif
4226 char_attr = 0;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229
4230 /*
4231 * Get the next character to put on the screen.
4232 */
4233 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004234 * The "p_extra" points to the extra stuff that is inserted to
4235 * represent special characters (non-printable stuff) and other
4236 * things. When all characters are the same, c_extra is used.
4237 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4238 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4240 */
4241 if (n_extra > 0)
4242 {
4243 if (c_extra != NUL)
4244 {
4245 c = c_extra;
4246#ifdef FEAT_MBYTE
4247 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004248 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004251 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004252 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 else
4255 mb_utf8 = FALSE;
4256#endif
4257 }
4258 else
4259 {
4260 c = *p_extra;
4261#ifdef FEAT_MBYTE
4262 if (has_mbyte)
4263 {
4264 mb_c = c;
4265 if (enc_utf8)
4266 {
4267 /* If the UTF-8 character is more than one byte:
4268 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004269 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 mb_utf8 = FALSE;
4271 if (mb_l > n_extra)
4272 mb_l = 1;
4273 else if (mb_l > 1)
4274 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004275 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004277 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280 else
4281 {
4282 /* if this is a DBCS character, put it in "mb_c" */
4283 mb_l = MB_BYTE2LEN(c);
4284 if (mb_l >= n_extra)
4285 mb_l = 1;
4286 else if (mb_l > 1)
4287 mb_c = (c << 8) + p_extra[1];
4288 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004289 if (mb_l == 0) /* at the NUL at end-of-line */
4290 mb_l = 1;
4291
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 /* If a double-width char doesn't fit display a '>' in the
4293 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004294 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295# ifdef FEAT_RIGHTLEFT
4296 wp->w_p_rl ? (col <= 0) :
4297# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004298 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 && (*mb_char2cells)(mb_c) == 2)
4300 {
4301 c = '>';
4302 mb_c = c;
4303 mb_l = 1;
4304 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004305 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 /* put the pointer back to output the double-width
4307 * character at the start of the next line. */
4308 ++n_extra;
4309 --p_extra;
4310 }
4311 else
4312 {
4313 n_extra -= mb_l - 1;
4314 p_extra += mb_l - 1;
4315 }
4316 }
4317#endif
4318 ++p_extra;
4319 }
4320 --n_extra;
4321 }
4322 else
4323 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004325 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004326#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004327
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004328 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004329 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /*
4331 * Get a character from the line itself.
4332 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004333 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004334#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004335 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#ifdef FEAT_MBYTE
4338 if (has_mbyte)
4339 {
4340 mb_c = c;
4341 if (enc_utf8)
4342 {
4343 /* If the UTF-8 character is more than one byte: Decode it
4344 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004345 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 mb_utf8 = FALSE;
4347 if (mb_l > 1)
4348 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004349 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 /* Overlong encoded ASCII or ASCII with composing char
4351 * is displayed normally, except a NUL. */
4352 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004353 {
4354 c = mb_c;
4355# ifdef FEAT_LINEBREAK
4356 c0 = mb_c;
4357# endif
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004360
4361 /* At start of the line we can have a composing char.
4362 * Draw it as a space with a composing char. */
4363 if (utf_iscomposing(mb_c))
4364 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004365 int i;
4366
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004367 for (i = Screen_mco - 1; i > 0; --i)
4368 u8cc[i] = u8cc[i - 1];
4369 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004370 mb_c = ' ';
4371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373
4374 if ((mb_l == 1 && c >= 0x80)
4375 || (mb_l >= 1 && mb_c == 0)
4376 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004377# ifdef UNICODE16
4378 || mb_c >= 0x10000
4379# endif
4380 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 /*
4383 * Illegal UTF-8 byte: display as <xx>.
4384 * Non-BMP character : display as ? or fullwidth ?.
4385 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004386# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 if (wp->w_p_rl) /* reverse */
4393 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004396# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 else if (utf_char2cells(mb_c) != 2)
4398 STRCPY(extra, "?");
4399 else
4400 /* 0xff1f in UTF-8: full-width '?' */
4401 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 p_extra = extra;
4405 c = *p_extra;
4406 mb_c = mb_ptr2char_adv(&p_extra);
4407 mb_utf8 = (c >= 0x80);
4408 n_extra = (int)STRLEN(p_extra);
4409 c_extra = NUL;
4410 if (area_attr == 0 && search_attr == 0)
4411 {
4412 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004413 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 saved_attr2 = char_attr; /* save current attr */
4415 }
4416 }
4417 else if (mb_l == 0) /* at the NUL at end-of-line */
4418 mb_l = 1;
4419#ifdef FEAT_ARABIC
4420 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4421 {
4422 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004423 int pc, pc1, nc;
4424 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 /* The idea of what is the previous and next
4427 * character depends on 'rightleft'. */
4428 if (wp->w_p_rl)
4429 {
4430 pc = prev_c;
4431 pc1 = prev_c1;
4432 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 else
4436 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004437 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 prev_c = mb_c;
4442
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004443 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 else
4446 prev_c = mb_c;
4447#endif
4448 }
4449 else /* enc_dbcs */
4450 {
4451 mb_l = MB_BYTE2LEN(c);
4452 if (mb_l == 0) /* at the NUL at end-of-line */
4453 mb_l = 1;
4454 else if (mb_l > 1)
4455 {
4456 /* We assume a second byte below 32 is illegal.
4457 * Hopefully this is OK for all double-byte encodings!
4458 */
4459 if (ptr[1] >= 32)
4460 mb_c = (c << 8) + ptr[1];
4461 else
4462 {
4463 if (ptr[1] == NUL)
4464 {
4465 /* head byte at end of line */
4466 mb_l = 1;
4467 transchar_nonprint(extra, c);
4468 }
4469 else
4470 {
4471 /* illegal tail byte */
4472 mb_l = 2;
4473 STRCPY(extra, "XX");
4474 }
4475 p_extra = extra;
4476 n_extra = (int)STRLEN(extra) - 1;
4477 c_extra = NUL;
4478 c = *p_extra++;
4479 if (area_attr == 0 && search_attr == 0)
4480 {
4481 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004482 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 saved_attr2 = char_attr; /* save current attr */
4484 }
4485 mb_c = c;
4486 }
4487 }
4488 }
4489 /* If a double-width char doesn't fit display a '>' in the
4490 * last column; the character is displayed at the start of the
4491 * next line. */
4492 if ((
4493# ifdef FEAT_RIGHTLEFT
4494 wp->w_p_rl ? (col <= 0) :
4495# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004496 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 && (*mb_char2cells)(mb_c) == 2)
4498 {
4499 c = '>';
4500 mb_c = c;
4501 mb_utf8 = FALSE;
4502 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004503 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /* Put pointer back so that the character will be
4505 * displayed at the start of the next line. */
4506 --ptr;
4507 }
4508 else if (*ptr != NUL)
4509 ptr += mb_l - 1;
4510
4511 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004512 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004513 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004514 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004517 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 c = ' ';
4519 if (area_attr == 0 && search_attr == 0)
4520 {
4521 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004522 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 saved_attr2 = char_attr; /* save current attr */
4524 }
4525 mb_c = c;
4526 mb_utf8 = FALSE;
4527 mb_l = 1;
4528 }
4529
4530 }
4531#endif
4532 ++ptr;
4533
4534 if (extra_check)
4535 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004536#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004537 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004538#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004539
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004540#ifdef FEAT_TERMINAL
4541 if (get_term_attr)
4542 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004543 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004544
4545 if (!attr_pri)
4546 char_attr = syntax_attr;
4547 else
4548 char_attr = hl_combine_attr(syntax_attr, char_attr);
4549 }
4550#endif
4551
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004552#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 /* Get syntax attribute, unless still at the start of the line
4554 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004555 v = (long)(ptr - line);
4556 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 /* Get the syntax attribute for the character. If there
4559 * is an error, disable syntax highlighting. */
4560 save_did_emsg = did_emsg;
4561 did_emsg = FALSE;
4562
Bram Moolenaar217ad922005-03-20 22:37:15 +00004563 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004564# ifdef FEAT_SPELL
4565 has_spell ? &can_spell :
4566# endif
4567 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004570 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004571 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004572 has_syntax = FALSE;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 else
4575 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004576#ifdef SYN_TIME_LIMIT
4577 if (wp->w_s->b_syn_slow)
4578 has_syntax = FALSE;
4579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581 /* Need to get the line again, a multi-line regexp may
4582 * have made it invalid. */
4583 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4584 ptr = line + v;
4585
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004586 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004588 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004589 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004590# ifdef FEAT_CONCEAL
4591 /* no concealing past the end of the line, it interferes
4592 * with line highlighting */
4593 if (c == NUL)
4594 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004595 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004596 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004599#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004600
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004601#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004602 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004603 * Only do this when there is no syntax highlighting, the
4604 * @Spell cluster is not used or the current syntax item
4605 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004606 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004608 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004609# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004610 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004611 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004612# endif
4613 if (c != 0 && (
4614# ifdef FEAT_SYN_HL
4615 !has_syntax ||
4616# endif
4617 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004618 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004619 char_u *prev_ptr, *p;
4620 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004621 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004622# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004623 if (has_mbyte)
4624 {
4625 prev_ptr = ptr - mb_l;
4626 v -= mb_l - 1;
4627 }
4628 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004629# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004630 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004631
4632 /* Use nextline[] if possible, it has the start of the
4633 * next line concatenated. */
4634 if ((prev_ptr - line) - nextlinecol >= 0)
4635 p = nextline + (prev_ptr - line) - nextlinecol;
4636 else
4637 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004638 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004639 len = spell_check(wp, p, &spell_hlf, &cap_col,
4640 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004641 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004642
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004643 /* In Insert mode only highlight a word that
4644 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004645 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004646 && (State & INSERT) != 0
4647 && wp->w_cursor.lnum == lnum
4648 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004649 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004650 && wp->w_cursor.col < (colnr_T)word_end)
4651 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004652 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004653 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004654 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004655
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004656 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004657 && (p - nextline) + len > nextline_idx)
4658 {
4659 /* Remember that the good word continues at the
4660 * start of the next line. */
4661 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004662 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004663 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004664
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004665 /* Turn index into actual attributes. */
4666 if (spell_hlf != HLF_COUNT)
4667 spell_attr = highlight_attr[spell_hlf];
4668
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004669 if (cap_col > 0)
4670 {
4671 if (p != prev_ptr
4672 && (p - nextline) + cap_col >= nextline_idx)
4673 {
4674 /* Remember that the word in the next line
4675 * must start with a capital. */
4676 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004677 cap_col = (int)((p - nextline) + cap_col
4678 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004679 }
4680 else
4681 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004682 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004683 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004684 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004685 }
4686 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004687 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004688 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004689 char_attr = hl_combine_attr(char_attr, spell_attr);
4690 else
4691 char_attr = hl_combine_attr(spell_attr, char_attr);
4692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#endif
4694#ifdef FEAT_LINEBREAK
4695 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004698 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004699 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004701# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004702 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004703# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004704 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004706 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004708 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004709
Bram Moolenaar597a4222014-06-25 14:39:50 +02004710 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004711 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004712 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004713 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004714 n_extra = (int)wp->w_buffer->b_p_ts
4715 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4716
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004717# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004718 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004719# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004721# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004722 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004723 {
4724#ifdef FEAT_CONCEAL
4725 if (c == TAB)
4726 /* See "Tab alignment" below. */
4727 FIX_FOR_BOGUSCOLS;
4728#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004729 if (!wp->w_p_list)
4730 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733#endif
4734
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004735 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4736 */
4737 if (wp->w_p_list
4738 && (((c == 160
4739#ifdef FEAT_MBYTE
4740 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4741#endif
4742 ) && lcs_nbsp)
4743 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4744 {
4745 c = (c == ' ') ? lcs_space : lcs_nbsp;
4746 if (area_attr == 0 && search_attr == 0)
4747 {
4748 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004749 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004750 saved_attr2 = char_attr; /* save current attr */
4751 }
4752#ifdef FEAT_MBYTE
4753 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004754 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004755 {
4756 mb_utf8 = TRUE;
4757 u8cc[0] = 0;
4758 c = 0xc0;
4759 }
4760 else
4761 mb_utf8 = FALSE;
4762#endif
4763 }
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4766 {
4767 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004768 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004771 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 saved_attr2 = char_attr; /* save current attr */
4773 }
4774#ifdef FEAT_MBYTE
4775 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004776 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004779 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004780 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else
4783 mb_utf8 = FALSE;
4784#endif
4785 }
4786 }
4787
4788 /*
4789 * Handling of non-printable characters.
4790 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004791 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 /*
4794 * when getting a character from the file, we may have to
4795 * turn it into something else on the way to putting it
4796 * into "ScreenLines".
4797 */
4798 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4799 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004800 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004801 long vcol_adjusted = vcol; /* removed showbreak length */
4802#ifdef FEAT_LINEBREAK
4803 /* only adjust the tab_len, when at the first column
4804 * after the showbreak value was drawn */
4805 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4806 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004809 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004810 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4811
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004812#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004813 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004814#endif
4815 /* tab amount depends on current column */
4816 n_extra = tab_len;
4817#ifdef FEAT_LINEBREAK
4818 else
4819 {
4820 char_u *p;
4821 int len = n_extra;
4822 int i;
4823 int saved_nextra = n_extra;
4824
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004825#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004826 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004827 /* there are characters to conceal */
4828 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004829 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4830 */
4831 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4832 && n_extra > tab_len)
4833 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004834#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004835
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004836 /* if n_extra > 0, it gives the number of chars, to
4837 * use for a tab, else we need to calculate the width
4838 * for a tab */
4839#ifdef FEAT_MBYTE
4840 len = (tab_len * mb_char2len(lcs_tab2));
4841 if (n_extra > 0)
4842 len += n_extra - tab_len;
4843#endif
4844 c = lcs_tab1;
4845 p = alloc((unsigned)(len + 1));
4846 vim_memset(p, ' ', len);
4847 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004848 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004849 p_extra_free = p;
4850 for (i = 0; i < tab_len; i++)
4851 {
4852#ifdef FEAT_MBYTE
4853 mb_char2bytes(lcs_tab2, p);
4854 p += mb_char2len(lcs_tab2);
4855 n_extra += mb_char2len(lcs_tab2)
4856 - (saved_nextra > 0 ? 1 : 0);
4857#else
4858 p[i] = lcs_tab2;
4859#endif
4860 }
4861 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004862#ifdef FEAT_CONCEAL
4863 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4864 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004865 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004866 n_extra -= vcol_off;
4867#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004868 }
4869#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004870#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004871 {
4872 int vc_saved = vcol_off;
4873
4874 /* Tab alignment should be identical regardless of
4875 * 'conceallevel' value. So tab compensates of all
4876 * previous concealed characters, and thus resets
4877 * vcol_off and boguscols accumulated so far in the
4878 * line. Note that the tab can be longer than
4879 * 'tabstop' when there are concealed characters. */
4880 FIX_FOR_BOGUSCOLS;
4881
4882 /* Make sure, the highlighting for the tab char will be
4883 * correctly set further below (effectively reverts the
4884 * FIX_FOR_BOGSUCOLS macro */
4885 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004886 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004887 tab_len += vc_saved;
4888 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#ifdef FEAT_MBYTE
4891 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4892#endif
4893 if (wp->w_p_list)
4894 {
4895 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004896#ifdef FEAT_LINEBREAK
4897 if (wp->w_p_lbr)
4898 c_extra = NUL; /* using p_extra from above */
4899 else
4900#endif
4901 c_extra = lcs_tab2;
4902 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004903 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 saved_attr2 = char_attr; /* save current attr */
4905#ifdef FEAT_MBYTE
4906 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004907 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004910 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004911 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913#endif
4914 }
4915 else
4916 {
4917 c_extra = ' ';
4918 c = ' ';
4919 }
4920 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004921 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004922 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004923 || ((fromcol >= 0 || fromcol_prev >= 0)
4924 && tocol > vcol
4925 && VIsual_mode != Ctrl_V
4926 && (
4927# ifdef FEAT_RIGHTLEFT
4928 wp->w_p_rl ? (col >= 0) :
4929# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004930 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004931 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004932 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004933 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004934 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004936 /* Display a '$' after the line or highlight an extra
4937 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4939 /* For a diff line the highlighting continues after the
4940 * "$". */
4941 if (
4942# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004943 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944# ifdef LINE_ATTR
4945 &&
4946# endif
4947# endif
4948# ifdef LINE_ATTR
4949 line_attr == 0
4950# endif
4951 )
4952#endif
4953 {
4954#ifdef FEAT_VIRTUALEDIT
4955 /* In virtualedit, visual selections may extend
4956 * beyond end of line. */
4957 if (area_highlighting && virtual_active()
4958 && tocol != MAXCOL && vcol < tocol)
4959 n_extra = 0;
4960 else
4961#endif
4962 {
4963 p_extra = at_end_str;
4964 n_extra = 1;
4965 c_extra = NUL;
4966 }
4967 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004968 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004969 c = lcs_eol;
4970 else
4971 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 lcs_eol_one = -1;
4973 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004974 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004976 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 n_attr = 1;
4978 }
4979#ifdef FEAT_MBYTE
4980 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004981 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004984 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004985 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
4988 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4989#endif
4990 }
4991 else if (c != NUL)
4992 {
4993 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004994 if (n_extra == 0)
4995 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996#ifdef FEAT_RIGHTLEFT
4997 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4998 rl_mirror(p_extra); /* reverse "<12>" */
4999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005001#ifdef FEAT_LINEBREAK
5002 if (wp->w_p_lbr)
5003 {
5004 char_u *p;
5005
5006 c = *p_extra;
5007 p = alloc((unsigned)n_extra + 1);
5008 vim_memset(p, ' ', n_extra);
5009 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5010 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005011 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005012 p_extra_free = p_extra = p;
5013 }
5014 else
5015#endif
5016 {
5017 n_extra = byte2cells(c) - 1;
5018 c = *p_extra++;
5019 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005020 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005023 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 saved_attr2 = char_attr; /* save current attr */
5025 }
5026#ifdef FEAT_MBYTE
5027 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5028#endif
5029 }
5030#ifdef FEAT_VIRTUALEDIT
5031 else if (VIsual_active
5032 && (VIsual_mode == Ctrl_V
5033 || VIsual_mode == 'v')
5034 && virtual_active()
5035 && tocol != MAXCOL
5036 && vcol < tocol
5037 && (
5038# ifdef FEAT_RIGHTLEFT
5039 wp->w_p_rl ? (col >= 0) :
5040# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005041 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 c = ' ';
5044 --ptr; /* put it back at the NUL */
5045 }
5046#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005047#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 else if ((
5049# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005050 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005052# ifdef FEAT_TERMINAL
5053 term_attr != 0 ||
5054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 ) && (
5057# ifdef FEAT_RIGHTLEFT
5058 wp->w_p_rl ? (col >= 0) :
5059# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005060 (col
5061# ifdef FEAT_CONCEAL
5062 - boguscols
5063# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005064 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 /* Highlight until the right side of the window */
5067 c = ' ';
5068 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005069
5070 /* Remember we do the char for line highlighting. */
5071 ++did_line_attr;
5072
5073 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005074 if (line_attr != 0 && char_attr == search_attr
5075 && (did_line_attr > 1
5076 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005077 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078# ifdef FEAT_DIFF
5079 if (diff_hlf == HLF_TXD)
5080 {
5081 diff_hlf = HLF_CHD;
5082 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005083 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005084 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005085 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5086 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005087 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005091# ifdef FEAT_TERMINAL
5092 if (term_attr != 0)
5093 {
5094 char_attr = term_attr;
5095 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5096 char_attr = hl_combine_attr(char_attr,
5097 HL_ATTR(HLF_CUL));
5098 }
5099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101#endif
5102 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005103
5104#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005105 if ( wp->w_p_cole > 0
5106 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005107 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005108 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005109 && !(lnum_in_visual_area
5110 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005111 {
5112 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005113 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005114 && (syn_get_sub_char() != NUL || match_conc
5115 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005116 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005118 /* First time at this concealed item: display one
5119 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005120 if (match_conc)
5121 c = match_conc;
5122 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005123 c = syn_get_sub_char();
5124 else if (lcs_conceal != NUL)
5125 c = lcs_conceal;
5126 else
5127 c = ' ';
5128
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005129 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005130
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005131 if (n_extra > 0)
5132 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005133 vcol += n_extra;
5134 if (wp->w_p_wrap && n_extra > 0)
5135 {
5136# ifdef FEAT_RIGHTLEFT
5137 if (wp->w_p_rl)
5138 {
5139 col -= n_extra;
5140 boguscols -= n_extra;
5141 }
5142 else
5143# endif
5144 {
5145 boguscols += n_extra;
5146 col += n_extra;
5147 }
5148 }
5149 n_extra = 0;
5150 n_attr = 0;
5151 }
5152 else if (n_skip == 0)
5153 {
5154 is_concealing = TRUE;
5155 n_skip = 1;
5156 }
5157# ifdef FEAT_MBYTE
5158 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005159 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005160 {
5161 mb_utf8 = TRUE;
5162 u8cc[0] = 0;
5163 c = 0xc0;
5164 }
5165 else
5166 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5167# endif
5168 }
5169 else
5170 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005171 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005172 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005173 }
5174#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005177#ifdef FEAT_CONCEAL
5178 /* In the cursor line and we may be concealing characters: correct
5179 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005180 if (!did_wcol && draw_state == WL_LINE
5181 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005182 && conceal_cursor_line(wp)
5183 && (int)wp->w_virtcol <= vcol + n_skip)
5184 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005185# ifdef FEAT_RIGHTLEFT
5186 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005187 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005188 else
5189# endif
5190 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005191 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005192 did_wcol = TRUE;
5193 }
5194#endif
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 /* Don't override visual selection highlighting. */
5197 if (n_attr > 0
5198 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005199 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 char_attr = extra_attr;
5201
Bram Moolenaar81695252004-12-29 20:58:21 +00005202#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 /* XIM don't send preedit_start and preedit_end, but they send
5204 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5205 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005206 if (p_imst == IM_ON_THE_SPOT
5207 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005208 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 && (State & INSERT)
5210 && !p_imdisable
5211 && im_is_preediting()
5212 && draw_state == WL_LINE)
5213 {
5214 colnr_T tcol;
5215
5216 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005217 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 else
5219 tcol = preedit_end_col;
5220 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5221 {
5222 if (feedback_old_attr < 0)
5223 {
5224 feedback_col = 0;
5225 feedback_old_attr = char_attr;
5226 }
5227 char_attr = im_get_feedback_attr(feedback_col);
5228 if (char_attr < 0)
5229 char_attr = feedback_old_attr;
5230 feedback_col++;
5231 }
5232 else if (feedback_old_attr >= 0)
5233 {
5234 char_attr = feedback_old_attr;
5235 feedback_old_attr = -1;
5236 feedback_col = 0;
5237 }
5238 }
5239#endif
5240 /*
5241 * Handle the case where we are in column 0 but not on the first
5242 * character of the line and the user wants us to show us a
5243 * special character (via 'listchars' option "precedes:<char>".
5244 */
5245 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005246 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5248#ifdef FEAT_DIFF
5249 && filler_todo <= 0
5250#endif
5251 && draw_state > WL_NR
5252 && c != NUL)
5253 {
5254 c = lcs_prec;
5255 lcs_prec_todo = NUL;
5256#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005257 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5258 {
5259 /* Double-width character being overwritten by the "precedes"
5260 * character, need to fill up half the character. */
5261 c_extra = MB_FILLER_CHAR;
5262 n_extra = 1;
5263 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005264 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005267 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005270 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005271 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273 else
5274 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5275#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005276 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005279 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 n_attr3 = 1;
5281 }
5282 }
5283
5284 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005285 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005287 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005288#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005289 || did_line_attr == 1
5290#endif
5291 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005293#ifdef FEAT_SEARCH_EXTRA
5294 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005295
5296 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005297 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005298 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005299#endif
5300
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005301 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 * highlight match at end of line. If it's beyond the last
5303 * char on the screen, just overwrite that one (tricky!) Not
5304 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005305#ifdef FEAT_SEARCH_EXTRA
5306 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005307 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005308 prevcol_hl_flag = TRUE;
5309 else
5310 {
5311 cur = wp->w_match_head;
5312 while (cur != NULL)
5313 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005314 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005315 {
5316 prevcol_hl_flag = TRUE;
5317 break;
5318 }
5319 cur = cur->next;
5320 }
5321 }
5322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005324 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005325 && (VIsual_mode != Ctrl_V
5326 || lnum == VIsual.lnum
5327 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005328 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329#ifdef FEAT_SEARCH_EXTRA
5330 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005331 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005332# ifdef FEAT_SYN_HL
5333 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5334 && !(wp == curwin && VIsual_active))
5335# endif
5336# ifdef FEAT_DIFF
5337 && diff_hlf == (hlf_T)0
5338# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005339# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005340 && did_line_attr <= 1
5341# endif
5342 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#endif
5344 ))
5345 {
5346 int n = 0;
5347
5348#ifdef FEAT_RIGHTLEFT
5349 if (wp->w_p_rl)
5350 {
5351 if (col < 0)
5352 n = 1;
5353 }
5354 else
5355#endif
5356 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005357 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 n = -1;
5359 }
5360 if (n != 0)
5361 {
5362 /* At the window boundary, highlight the last character
5363 * instead (better than nothing). */
5364 off += n;
5365 col += n;
5366 }
5367 else
5368 {
5369 /* Add a blank character to highlight. */
5370 ScreenLines[off] = ' ';
5371#ifdef FEAT_MBYTE
5372 if (enc_utf8)
5373 ScreenLinesUC[off] = 0;
5374#endif
5375 }
5376#ifdef FEAT_SEARCH_EXTRA
5377 if (area_attr == 0)
5378 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005379 /* Use attributes from match with highest priority among
5380 * 'search_hl' and the match list. */
5381 char_attr = search_hl.attr;
5382 cur = wp->w_match_head;
5383 shl_flag = FALSE;
5384 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005385 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005386 if (shl_flag == FALSE
5387 && ((cur != NULL
5388 && cur->priority > SEARCH_HL_PRIORITY)
5389 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005390 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005391 shl = &search_hl;
5392 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005393 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005394 else
5395 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005396 if ((ptr - line) - 1 == (long)shl->startcol
5397 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005398 char_attr = shl->attr;
5399 if (shl != &search_hl && cur != NULL)
5400 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403#endif
5404 ScreenAttrs[off] = char_attr;
5405#ifdef FEAT_RIGHTLEFT
5406 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005409 --off;
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 else
5412#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005415 ++off;
5416 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005417 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005418#ifdef FEAT_SYN_HL
5419 eol_hl_off = 1;
5420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaar91170f82006-05-05 21:15:17 +00005424 /*
5425 * At end of the text line.
5426 */
5427 if (c == NUL)
5428 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005429#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005430 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5431 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005432 {
5433 /* highlight last char after line */
5434 --col;
5435 --off;
5436 --vcol;
5437 }
5438
Bram Moolenaar1a384422010-07-14 19:53:30 +02005439 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005440 if (wp->w_p_wrap)
5441 v = wp->w_skipcol;
5442 else
5443 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005444
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005445 /* check if line ends before left margin */
5446 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005447 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005448#ifdef FEAT_CONCEAL
5449 /* Get rid of the boguscols now, we want to draw until the right
5450 * edge for 'cursorcolumn'. */
5451 col -= boguscols;
5452 boguscols = 0;
5453#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005454
5455 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005456 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005457
5458 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005459 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5460 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005461 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005462 && lnum != wp->w_cursor.lnum)
5463 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005464# ifdef FEAT_RIGHTLEFT
5465 && !wp->w_p_rl
5466# endif
5467 )
5468 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005469 int rightmost_vcol = 0;
5470 int i;
5471
5472 if (wp->w_p_cuc)
5473 rightmost_vcol = wp->w_virtcol;
5474 if (draw_color_col)
5475 /* determine rightmost colorcolumn to possibly draw */
5476 for (i = 0; color_cols[i] >= 0; ++i)
5477 if (rightmost_vcol < color_cols[i])
5478 rightmost_vcol = color_cols[i];
5479
Bram Moolenaar02631462017-09-22 15:20:32 +02005480 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005481 {
5482 ScreenLines[off] = ' ';
5483#ifdef FEAT_MBYTE
5484 if (enc_utf8)
5485 ScreenLinesUC[off] = 0;
5486#endif
5487 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005488 if (draw_color_col)
5489 draw_color_col = advance_color_col(VCOL_HLC,
5490 &color_cols);
5491
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005492 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005493 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005494 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005495 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005496 else
5497 ScreenAttrs[off++] = 0;
5498
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005499 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005500 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005501
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005502 ++vcol;
5503 }
5504 }
5505#endif
5506
Bram Moolenaar53f81742017-09-22 14:35:51 +02005507 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005508 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 row++;
5510
5511 /*
5512 * Update w_cline_height and w_cline_folded if the cursor line was
5513 * updated (saves a call to plines() later).
5514 */
5515 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5516 {
5517 curwin->w_cline_row = startrow;
5518 curwin->w_cline_height = row - startrow;
5519#ifdef FEAT_FOLDING
5520 curwin->w_cline_folded = FALSE;
5521#endif
5522 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5523 }
5524
5525 break;
5526 }
5527
5528 /* line continues beyond line end */
5529 if (lcs_ext
5530 && !wp->w_p_wrap
5531#ifdef FEAT_DIFF
5532 && filler_todo <= 0
5533#endif
5534 && (
5535#ifdef FEAT_RIGHTLEFT
5536 wp->w_p_rl ? col == 0 :
5537#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005538 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005540 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5542 {
5543 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005544 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#ifdef FEAT_MBYTE
5546 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005547 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005550 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005551 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 else
5554 mb_utf8 = FALSE;
5555#endif
5556 }
5557
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005558#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005559 /* advance to the next 'colorcolumn' */
5560 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005561 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005563 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005564 * highlight the cursor position itself.
5565 * Also highlight the 'colorcolumn' if it is different than
5566 * 'cursorcolumn' */
5567 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005568 if (draw_state == WL_LINE && !lnum_in_visual_area
5569 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005570 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005571 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005572 && lnum != wp->w_cursor.lnum)
5573 {
5574 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005575 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005576 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005577 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578 {
5579 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005580 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005581 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005582 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005583#endif
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 /*
5586 * Store character to be displayed.
5587 * Skip characters that are left of the screen for 'nowrap'.
5588 */
5589 vcol_prev = vcol;
5590 if (draw_state < WL_LINE || n_skip <= 0)
5591 {
5592 /*
5593 * Store the character.
5594 */
5595#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5596 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5597 {
5598 /* A double-wide character is: put first halve in left cell. */
5599 --off;
5600 --col;
5601 }
5602#endif
5603 ScreenLines[off] = c;
5604#ifdef FEAT_MBYTE
5605 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005606 {
5607 if ((mb_c & 0xff00) == 0x8e00)
5608 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 else if (enc_utf8)
5612 {
5613 if (mb_utf8)
5614 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005615 int i;
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005618 if ((c & 0xff) == 0)
5619 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005620 for (i = 0; i < Screen_mco; ++i)
5621 {
5622 ScreenLinesC[i][off] = u8cc[i];
5623 if (u8cc[i] == 0)
5624 break;
5625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else
5628 ScreenLinesUC[off] = 0;
5629 }
5630 if (multi_attr)
5631 {
5632 ScreenAttrs[off] = multi_attr;
5633 multi_attr = 0;
5634 }
5635 else
5636#endif
5637 ScreenAttrs[off] = char_attr;
5638
5639#ifdef FEAT_MBYTE
5640 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5641 {
5642 /* Need to fill two screen columns. */
5643 ++off;
5644 ++col;
5645 if (enc_utf8)
5646 /* UTF-8: Put a 0 in the second screen char. */
5647 ScreenLines[off] = 0;
5648 else
5649 /* DBCS: Put second byte in the second screen char. */
5650 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005651 if (draw_state > WL_NR
5652#ifdef FEAT_DIFF
5653 && filler_todo <= 0
5654#endif
5655 )
5656 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 /* When "tocol" is halfway a character, set it to the end of
5658 * the character, otherwise highlighting won't stop. */
5659 if (tocol == vcol)
5660 ++tocol;
5661#ifdef FEAT_RIGHTLEFT
5662 if (wp->w_p_rl)
5663 {
5664 /* now it's time to backup one cell */
5665 --off;
5666 --col;
5667 }
5668#endif
5669 }
5670#endif
5671#ifdef FEAT_RIGHTLEFT
5672 if (wp->w_p_rl)
5673 {
5674 --off;
5675 --col;
5676 }
5677 else
5678#endif
5679 {
5680 ++off;
5681 ++col;
5682 }
5683 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005684#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005685 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005686 {
5687 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005688 ++vcol_off;
5689 if (n_extra > 0)
5690 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005691 if (wp->w_p_wrap)
5692 {
5693 /*
5694 * Special voodoo required if 'wrap' is on.
5695 *
5696 * Advance the column indicator to force the line
5697 * drawing to wrap early. This will make the line
5698 * take up the same screen space when parts are concealed,
5699 * so that cursor line computations aren't messed up.
5700 *
5701 * To avoid the fictitious advance of 'col' causing
5702 * trailing junk to be written out of the screen line
5703 * we are building, 'boguscols' keeps track of the number
5704 * of bad columns we have advanced.
5705 */
5706 if (n_extra > 0)
5707 {
5708 vcol += n_extra;
5709# ifdef FEAT_RIGHTLEFT
5710 if (wp->w_p_rl)
5711 {
5712 col -= n_extra;
5713 boguscols -= n_extra;
5714 }
5715 else
5716# endif
5717 {
5718 col += n_extra;
5719 boguscols += n_extra;
5720 }
5721 n_extra = 0;
5722 n_attr = 0;
5723 }
5724
5725
5726# ifdef FEAT_MBYTE
5727 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5728 {
5729 /* Need to fill two screen columns. */
5730# ifdef FEAT_RIGHTLEFT
5731 if (wp->w_p_rl)
5732 {
5733 --boguscols;
5734 --col;
5735 }
5736 else
5737# endif
5738 {
5739 ++boguscols;
5740 ++col;
5741 }
5742 }
5743# endif
5744
5745# ifdef FEAT_RIGHTLEFT
5746 if (wp->w_p_rl)
5747 {
5748 --boguscols;
5749 --col;
5750 }
5751 else
5752# endif
5753 {
5754 ++boguscols;
5755 ++col;
5756 }
5757 }
5758 else
5759 {
5760 if (n_extra > 0)
5761 {
5762 vcol += n_extra;
5763 n_extra = 0;
5764 n_attr = 0;
5765 }
5766 }
5767
5768 }
5769#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 else
5771 --n_skip;
5772
Bram Moolenaar64486672010-05-16 15:46:46 +02005773 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5774 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005775 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#ifdef FEAT_DIFF
5777 && filler_todo <= 0
5778#endif
5779 )
5780 ++vcol;
5781
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005782#ifdef FEAT_SYN_HL
5783 if (vcol_save_attr >= 0)
5784 char_attr = vcol_save_attr;
5785#endif
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 /* restore attributes after "predeces" in 'listchars' */
5788 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5789 char_attr = saved_attr3;
5790
5791 /* restore attributes after last 'listchars' or 'number' char */
5792 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5793 char_attr = saved_attr2;
5794
5795 /*
5796 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005797 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 */
5799 if ((
5800#ifdef FEAT_RIGHTLEFT
5801 wp->w_p_rl ? (col < 0) :
5802#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005803 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 && (*ptr != NUL
5805#ifdef FEAT_DIFF
5806 || filler_todo > 0
5807#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005808 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5810 )
5811 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005812#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005813 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005814 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005815 boguscols = 0;
5816#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005817 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005818 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 ++row;
5821 ++screen_row;
5822
5823 /* When not wrapping and finished diff lines, or when displayed
5824 * '$' and highlighting until last column, break here. */
5825 if ((!wp->w_p_wrap
5826#ifdef FEAT_DIFF
5827 && filler_todo <= 0
5828#endif
5829 ) || lcs_eol_one == -1)
5830 break;
5831
5832 /* When the window is too narrow draw all "@" lines. */
5833 if (draw_state != WL_LINE
5834#ifdef FEAT_DIFF
5835 && filler_todo <= 0
5836#endif
5837 )
5838 {
5839 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 row = endrow;
5842 }
5843
5844 /* When line got too long for screen break here. */
5845 if (row == endrow)
5846 {
5847 ++row;
5848 break;
5849 }
5850
5851 if (screen_cur_row == screen_row - 1
5852#ifdef FEAT_DIFF
5853 && filler_todo <= 0
5854#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005855 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
5857 /* Remember that the line wraps, used for modeless copy. */
5858 LineWraps[screen_row - 1] = TRUE;
5859
5860 /*
5861 * Special trick to make copy/paste of wrapped lines work with
5862 * xterm/screen: write an extra character beyond the end of
5863 * the line. This will work with all terminal types
5864 * (regardless of the xn,am settings).
5865 * Only do this on a fast tty.
5866 * Only do this if the cursor is on the current line
5867 * (something has been written in it).
5868 * Don't do this for the GUI.
5869 * Don't do this for double-width characters.
5870 * Don't do this for a window not at the right screen border.
5871 */
5872 if (p_tf
5873#ifdef FEAT_GUI
5874 && !gui.in_use
5875#endif
5876#ifdef FEAT_MBYTE
5877 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005878 && ((*mb_off2cells)(LineOffset[screen_row],
5879 LineOffset[screen_row] + screen_Columns)
5880 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005882 + (int)Columns - 2,
5883 LineOffset[screen_row] + screen_Columns)
5884 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#endif
5886 )
5887 {
5888 /* First make sure we are at the end of the screen line,
5889 * then output the same character again to let the
5890 * terminal know about the wrap. If the terminal doesn't
5891 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005892 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 screen_char(LineOffset[screen_row - 1]
5894 + (unsigned)Columns - 1,
5895 screen_row - 1, (int)(Columns - 1));
5896
5897#ifdef FEAT_MBYTE
5898 /* When there is a multi-byte character, just output a
5899 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005900 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5901 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 out_char(' ');
5903 else
5904#endif
5905 out_char(ScreenLines[LineOffset[screen_row - 1]
5906 + (Columns - 1)]);
5907 /* force a redraw of the first char on the next line */
5908 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5909 screen_start(); /* don't know where cursor is now */
5910 }
5911 }
5912
5913 col = 0;
5914 off = (unsigned)(current_ScreenLine - ScreenLines);
5915#ifdef FEAT_RIGHTLEFT
5916 if (wp->w_p_rl)
5917 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005918 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 off += col;
5920 }
5921#endif
5922
5923 /* reset the drawing state for the start of a wrapped line */
5924 draw_state = WL_START;
5925 saved_n_extra = n_extra;
5926 saved_p_extra = p_extra;
5927 saved_c_extra = c_extra;
5928 saved_char_attr = char_attr;
5929 n_extra = 0;
5930 lcs_prec_todo = lcs_prec;
5931#ifdef FEAT_LINEBREAK
5932# ifdef FEAT_DIFF
5933 if (filler_todo <= 0)
5934# endif
5935 need_showbreak = TRUE;
5936#endif
5937#ifdef FEAT_DIFF
5938 --filler_todo;
5939 /* When the filler lines are actually below the last line of the
5940 * file, don't draw the line itself, break here. */
5941 if (filler_todo == 0 && wp->w_botfill)
5942 break;
5943#endif
5944 }
5945
5946 } /* for every character in the line */
5947
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005948#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005949 /* After an empty line check first word for capital. */
5950 if (*skipwhite(line) == NUL)
5951 {
5952 capcol_lnum = lnum + 1;
5953 cap_col = 0;
5954 }
5955#endif
5956
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005957 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 return row;
5959}
5960
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005961#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005962static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005963
5964/*
5965 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005966 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005967 */
5968 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005969comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005970{
5971 int i;
5972
5973 for (i = 0; i < Screen_mco; ++i)
5974 {
5975 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5976 return TRUE;
5977 if (ScreenLinesC[i][off_from] == 0)
5978 break;
5979 }
5980 return FALSE;
5981}
5982#endif
5983
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984/*
5985 * Check whether the given character needs redrawing:
5986 * - the (first byte of the) character is different
5987 * - the attributes are different
5988 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005989 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005992char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 if (cols > 0
5995 && ((ScreenLines[off_from] != ScreenLines[off_to]
5996 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5997
5998#ifdef FEAT_MBYTE
5999 || (enc_dbcs != 0
6000 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6001 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6002 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6003 : (cols > 1 && ScreenLines[off_from + 1]
6004 != ScreenLines[off_to + 1])))
6005 || (enc_utf8
6006 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6007 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006008 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006009 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6010 && ScreenLines[off_from + 1]
6011 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012#endif
6013 ))
6014 return TRUE;
6015 return FALSE;
6016}
6017
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006018#if defined(FEAT_TERMINAL) || defined(PROTO)
6019/*
6020 * Return the index in ScreenLines[] for the current screen line.
6021 */
6022 int
6023screen_get_current_line_off()
6024{
6025 return (int)(current_ScreenLine - ScreenLines);
6026}
6027#endif
6028
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029/*
6030 * Move one "cooked" screen line to the screen, but only the characters that
6031 * have actually changed. Handle insert/delete character.
6032 * "coloff" gives the first column on the screen for this line.
6033 * "endcol" gives the columns where valid characters are.
6034 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6035 * needs to be cleared, negative otherwise.
6036 * "rlflag" is TRUE in a rightleft window:
6037 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6038 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6039 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006040 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006041screen_line(
6042 int row,
6043 int coloff,
6044 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006045 int clear_width,
6046 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 unsigned off_from;
6049 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006050#ifdef FEAT_MBYTE
6051 unsigned max_off_from;
6052 unsigned max_off_to;
6053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 int force = FALSE; /* force update rest of the line */
6057 int redraw_this /* bool: does character need redraw? */
6058#ifdef FEAT_GUI
6059 = TRUE /* For GUI when while-loop empty */
6060#endif
6061 ;
6062 int redraw_next; /* redraw_this for next character */
6063#ifdef FEAT_MBYTE
6064 int clear_next = FALSE;
6065 int char_cells; /* 1: normal char */
6066 /* 2: occupies two display cells */
6067# define CHAR_CELLS char_cells
6068#else
6069# define CHAR_CELLS 1
6070#endif
6071
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006072 /* Check for illegal row and col, just in case. */
6073 if (row >= Rows)
6074 row = Rows - 1;
6075 if (endcol > Columns)
6076 endcol = Columns;
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078# ifdef FEAT_CLIPBOARD
6079 clip_may_clear_selection(row, row);
6080# endif
6081
6082 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6083 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006084#ifdef FEAT_MBYTE
6085 max_off_from = off_from + screen_Columns;
6086 max_off_to = LineOffset[row] + screen_Columns;
6087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
6089#ifdef FEAT_RIGHTLEFT
6090 if (rlflag)
6091 {
6092 /* Clear rest first, because it's left of the text. */
6093 if (clear_width > 0)
6094 {
6095 while (col <= endcol && ScreenLines[off_to] == ' '
6096 && ScreenAttrs[off_to] == 0
6097# ifdef FEAT_MBYTE
6098 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6099# endif
6100 )
6101 {
6102 ++off_to;
6103 ++col;
6104 }
6105 if (col <= endcol)
6106 screen_fill(row, row + 1, col + coloff,
6107 endcol + coloff + 1, ' ', ' ', 0);
6108 }
6109 col = endcol + 1;
6110 off_to = LineOffset[row] + col + coloff;
6111 off_from += col;
6112 endcol = (clear_width > 0 ? clear_width : -clear_width);
6113 }
6114#endif /* FEAT_RIGHTLEFT */
6115
6116 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6117
6118 while (col < endcol)
6119 {
6120#ifdef FEAT_MBYTE
6121 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006122 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 else
6124 char_cells = 1;
6125#endif
6126
6127 redraw_this = redraw_next;
6128 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6129 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6130
6131#ifdef FEAT_GUI
6132 /* If the next character was bold, then redraw the current character to
6133 * remove any pixels that might have spilt over into us. This only
6134 * happens in the GUI.
6135 */
6136 if (redraw_next && gui.in_use)
6137 {
6138 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006139 if (hl > HL_ALL)
6140 hl = syn_attr2attr(hl);
6141 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 redraw_this = TRUE;
6143 }
6144#endif
6145
6146 if (redraw_this)
6147 {
6148 /*
6149 * Special handling when 'xs' termcap flag set (hpterm):
6150 * Attributes for characters are stored at the position where the
6151 * cursor is when writing the highlighting code. The
6152 * start-highlighting code must be written with the cursor on the
6153 * first highlighted character. The stop-highlighting code must
6154 * be written with the cursor just after the last highlighted
6155 * character.
6156 * Overwriting a character doesn't remove it's highlighting. Need
6157 * to clear the rest of the line, and force redrawing it
6158 * completely.
6159 */
6160 if ( p_wiv
6161 && !force
6162#ifdef FEAT_GUI
6163 && !gui.in_use
6164#endif
6165 && ScreenAttrs[off_to] != 0
6166 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6167 {
6168 /*
6169 * Need to remove highlighting attributes here.
6170 */
6171 windgoto(row, col + coloff);
6172 out_str(T_CE); /* clear rest of this screen line */
6173 screen_start(); /* don't know where cursor is now */
6174 force = TRUE; /* force redraw of rest of the line */
6175 redraw_next = TRUE; /* or else next char would miss out */
6176
6177 /*
6178 * If the previous character was highlighted, need to stop
6179 * highlighting at this character.
6180 */
6181 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6182 {
6183 screen_attr = ScreenAttrs[off_to - 1];
6184 term_windgoto(row, col + coloff);
6185 screen_stop_highlight();
6186 }
6187 else
6188 screen_attr = 0; /* highlighting has stopped */
6189 }
6190#ifdef FEAT_MBYTE
6191 if (enc_dbcs != 0)
6192 {
6193 /* Check if overwriting a double-byte with a single-byte or
6194 * the other way around requires another character to be
6195 * redrawn. For UTF-8 this isn't needed, because comparing
6196 * ScreenLinesUC[] is sufficient. */
6197 if (char_cells == 1
6198 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
6201 /* Writing a single-cell character over a double-cell
6202 * character: need to redraw the next cell. */
6203 ScreenLines[off_to + 1] = 0;
6204 redraw_next = TRUE;
6205 }
6206 else if (char_cells == 2
6207 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006208 && (*mb_off2cells)(off_to, max_off_to) == 1
6209 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
6211 /* Writing the second half of a double-cell character over
6212 * a double-cell character: need to redraw the second
6213 * cell. */
6214 ScreenLines[off_to + 2] = 0;
6215 redraw_next = TRUE;
6216 }
6217
6218 if (enc_dbcs == DBCS_JPNU)
6219 ScreenLines2[off_to] = ScreenLines2[off_from];
6220 }
6221 /* When writing a single-width character over a double-width
6222 * character and at the end of the redrawn text, need to clear out
6223 * the right halve of the old character.
6224 * Also required when writing the right halve of a double-width
6225 * char over the left halve of an existing one. */
6226 if (has_mbyte && col + char_cells == endcol
6227 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006228 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006230 && (*mb_off2cells)(off_to, max_off_to) == 1
6231 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 clear_next = TRUE;
6233#endif
6234
6235 ScreenLines[off_to] = ScreenLines[off_from];
6236#ifdef FEAT_MBYTE
6237 if (enc_utf8)
6238 {
6239 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6240 if (ScreenLinesUC[off_from] != 0)
6241 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006242 int i;
6243
6244 for (i = 0; i < Screen_mco; ++i)
6245 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247 }
6248 if (char_cells == 2)
6249 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6250#endif
6251
6252#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006253 /* The bold trick makes a single column of pixels appear in the
6254 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 * character should be redrawn too. This happens for our own GUI
6256 * and for some xterms. */
6257 if (
6258# ifdef FEAT_GUI
6259 gui.in_use
6260# endif
6261# if defined(FEAT_GUI) && defined(UNIX)
6262 ||
6263# endif
6264# ifdef UNIX
6265 term_is_xterm
6266# endif
6267 )
6268 {
6269 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006270 if (hl > HL_ALL)
6271 hl = syn_attr2attr(hl);
6272 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 redraw_next = TRUE;
6274 }
6275#endif
6276 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6277#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006278 /* For simplicity set the attributes of second half of a
6279 * double-wide character equal to the first half. */
6280 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006282
6283 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 else
6286#endif
6287 screen_char(off_to, row, col + coloff);
6288 }
6289 else if ( p_wiv
6290#ifdef FEAT_GUI
6291 && !gui.in_use
6292#endif
6293 && col + coloff > 0)
6294 {
6295 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6296 {
6297 /*
6298 * Don't output stop-highlight when moving the cursor, it will
6299 * stop the highlighting when it should continue.
6300 */
6301 screen_attr = 0;
6302 }
6303 else if (screen_attr != 0)
6304 screen_stop_highlight();
6305 }
6306
6307 off_to += CHAR_CELLS;
6308 off_from += CHAR_CELLS;
6309 col += CHAR_CELLS;
6310 }
6311
6312#ifdef FEAT_MBYTE
6313 if (clear_next)
6314 {
6315 /* Clear the second half of a double-wide character of which the left
6316 * half was overwritten with a single-wide character. */
6317 ScreenLines[off_to] = ' ';
6318 if (enc_utf8)
6319 ScreenLinesUC[off_to] = 0;
6320 screen_char(off_to, row, col + coloff);
6321 }
6322#endif
6323
6324 if (clear_width > 0
6325#ifdef FEAT_RIGHTLEFT
6326 && !rlflag
6327#endif
6328 )
6329 {
6330#ifdef FEAT_GUI
6331 int startCol = col;
6332#endif
6333
6334 /* blank out the rest of the line */
6335 while (col < clear_width && ScreenLines[off_to] == ' '
6336 && ScreenAttrs[off_to] == 0
6337#ifdef FEAT_MBYTE
6338 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6339#endif
6340 )
6341 {
6342 ++off_to;
6343 ++col;
6344 }
6345 if (col < clear_width)
6346 {
6347#ifdef FEAT_GUI
6348 /*
6349 * In the GUI, clearing the rest of the line may leave pixels
6350 * behind if the first character cleared was bold. Some bold
6351 * fonts spill over the left. In this case we redraw the previous
6352 * character too. If we didn't skip any blanks above, then we
6353 * only redraw if the character wasn't already redrawn anyway.
6354 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006355 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 {
6357 hl = ScreenAttrs[off_to];
6358 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006359 {
6360 int prev_cells = 1;
6361# ifdef FEAT_MBYTE
6362 if (enc_utf8)
6363 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6364 * that its width is 2. */
6365 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6366 else if (enc_dbcs != 0)
6367 {
6368 /* find previous character by counting from first
6369 * column and get its width. */
6370 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006372
6373 while (off < off_to)
6374 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006375 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006376 off += prev_cells;
6377 }
6378 }
6379
6380 if (enc_dbcs != 0 && prev_cells > 1)
6381 screen_char_2(off_to - prev_cells, row,
6382 col + coloff - prev_cells);
6383 else
6384# endif
6385 screen_char(off_to - prev_cells, row,
6386 col + coloff - prev_cells);
6387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389#endif
6390 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6391 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 off_to += clear_width - col;
6393 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 }
6395 }
6396
6397 if (clear_width > 0)
6398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 /* For a window that's left of another, draw the separator char. */
6400 if (col + coloff < Columns)
6401 {
6402 int c;
6403
6404 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006405 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006406#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006407 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6408 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 || ScreenAttrs[off_to] != hl)
6411 {
6412 ScreenLines[off_to] = c;
6413 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006414#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (enc_utf8)
6416 {
6417 if (c >= 0x80)
6418 {
6419 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006420 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 }
6422 else
6423 ScreenLinesUC[off_to] = 0;
6424 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 screen_char(off_to, row, col + coloff);
6427 }
6428 }
6429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 LineWraps[row] = FALSE;
6431 }
6432}
6433
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006434#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 * Mirror text "str" for right-left displaying.
6437 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006440rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 char_u *p1, *p2;
6443 int t;
6444
6445 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6446 {
6447 t = *p1;
6448 *p1 = *p2;
6449 *p2 = t;
6450 }
6451}
6452#endif
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454/*
6455 * mark all status lines for redraw; used after first :cd
6456 */
6457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006458status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 win_T *wp;
6461
Bram Moolenaar29323592016-07-24 22:04:11 +02006462 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (wp->w_status_height)
6464 {
6465 wp->w_redr_status = TRUE;
6466 redraw_later(VALID);
6467 }
6468}
6469
6470/*
6471 * mark all status lines of the current buffer for redraw
6472 */
6473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006474status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 win_T *wp;
6477
Bram Moolenaar29323592016-07-24 22:04:11 +02006478 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6480 {
6481 wp->w_redr_status = TRUE;
6482 redraw_later(VALID);
6483 }
6484}
6485
6486/*
6487 * Redraw all status lines that need to be redrawn.
6488 */
6489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006490redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 win_T *wp;
6493
Bram Moolenaar29323592016-07-24 22:04:11 +02006494 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (wp->w_redr_status)
6496 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006497 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006498 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
Bram Moolenaar4033c552017-09-16 20:54:51 +02006501#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * Redraw all status lines at the bottom of frame "frp".
6504 */
6505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006506win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 if (frp->fr_layout == FR_LEAF)
6509 frp->fr_win->w_redr_status = TRUE;
6510 else if (frp->fr_layout == FR_ROW)
6511 {
6512 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6513 win_redraw_last_status(frp);
6514 }
6515 else /* frp->fr_layout == FR_COL */
6516 {
6517 frp = frp->fr_child;
6518 while (frp->fr_next != NULL)
6519 frp = frp->fr_next;
6520 win_redraw_last_status(frp);
6521 }
6522}
6523#endif
6524
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
6544#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006545static int status_match_len(expand_T *xp, char_u *s);
6546static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006549 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 */
6551 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006552status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 int len = 0;
6555
6556#ifdef FEAT_MENU
6557 int emenu = (xp->xp_context == EXPAND_MENUS
6558 || xp->xp_context == EXPAND_MENUNAMES);
6559
6560 /* Check for menu separators - replace with '|'. */
6561 if (emenu && menu_is_separator(s))
6562 return 1;
6563#endif
6564
6565 while (*s != NUL)
6566 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006567 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006568 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006569 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 }
6571
6572 return len;
6573}
6574
6575/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006576 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006577 * These are backslashes used for escaping. Do show backslashes in help tags.
6578 */
6579 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006580skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006581{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006582 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006583#ifdef FEAT_MENU
6584 || ((xp->xp_context == EXPAND_MENUS
6585 || xp->xp_context == EXPAND_MENUNAMES)
6586 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6587#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006588 )
6589 {
6590#ifndef BACKSLASH_IN_FILENAME
6591 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6592 return 2;
6593#endif
6594 return 1;
6595 }
6596 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006597}
6598
6599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 * Show wildchar matches in the status line.
6601 * Show at least the "match" item.
6602 * We start at item 'first_match' in the list and show all matches that fit.
6603 *
6604 * If inversion is possible we use it. Else '=' characters are used.
6605 */
6606 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006607win_redr_status_matches(
6608 expand_T *xp,
6609 int num_matches,
6610 char_u **matches, /* list of matches */
6611 int match,
6612 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6615 int row;
6616 char_u *buf;
6617 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006618 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 int fillchar;
6620 int attr;
6621 int i;
6622 int highlight = TRUE;
6623 char_u *selstart = NULL;
6624 int selstart_col = 0;
6625 char_u *selend = NULL;
6626 static int first_match = 0;
6627 int add_left = FALSE;
6628 char_u *s;
6629#ifdef FEAT_MENU
6630 int emenu;
6631#endif
6632#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6633 int l;
6634#endif
6635
6636 if (matches == NULL) /* interrupted completion? */
6637 return;
6638
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006639#ifdef FEAT_MBYTE
6640 if (has_mbyte)
6641 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6642 else
6643#endif
6644 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (buf == NULL)
6646 return;
6647
6648 if (match == -1) /* don't show match but original text */
6649 {
6650 match = 0;
6651 highlight = FALSE;
6652 }
6653 /* count 1 for the ending ">" */
6654 clen = status_match_len(xp, L_MATCH(match)) + 3;
6655 if (match == 0)
6656 first_match = 0;
6657 else if (match < first_match)
6658 {
6659 /* jumping left, as far as we can go */
6660 first_match = match;
6661 add_left = TRUE;
6662 }
6663 else
6664 {
6665 /* check if match fits on the screen */
6666 for (i = first_match; i < match; ++i)
6667 clen += status_match_len(xp, L_MATCH(i)) + 2;
6668 if (first_match > 0)
6669 clen += 2;
6670 /* jumping right, put match at the left */
6671 if ((long)clen > Columns)
6672 {
6673 first_match = match;
6674 /* if showing the last match, we can add some on the left */
6675 clen = 2;
6676 for (i = match; i < num_matches; ++i)
6677 {
6678 clen += status_match_len(xp, L_MATCH(i)) + 2;
6679 if ((long)clen >= Columns)
6680 break;
6681 }
6682 if (i == num_matches)
6683 add_left = TRUE;
6684 }
6685 }
6686 if (add_left)
6687 while (first_match > 0)
6688 {
6689 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6690 if ((long)clen >= Columns)
6691 break;
6692 --first_match;
6693 }
6694
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006695 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 if (first_match == 0)
6698 {
6699 *buf = NUL;
6700 len = 0;
6701 }
6702 else
6703 {
6704 STRCPY(buf, "< ");
6705 len = 2;
6706 }
6707 clen = len;
6708
6709 i = first_match;
6710 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6711 {
6712 if (i == match)
6713 {
6714 selstart = buf + len;
6715 selstart_col = clen;
6716 }
6717
6718 s = L_MATCH(i);
6719 /* Check for menu separators - replace with '|' */
6720#ifdef FEAT_MENU
6721 emenu = (xp->xp_context == EXPAND_MENUS
6722 || xp->xp_context == EXPAND_MENUNAMES);
6723 if (emenu && menu_is_separator(s))
6724 {
6725 STRCPY(buf + len, transchar('|'));
6726 l = (int)STRLEN(buf + len);
6727 len += l;
6728 clen += l;
6729 }
6730 else
6731#endif
6732 for ( ; *s != NUL; ++s)
6733 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006734 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 clen += ptr2cells(s);
6736#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006737 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
6739 STRNCPY(buf + len, s, l);
6740 s += l - 1;
6741 len += l;
6742 }
6743 else
6744#endif
6745 {
6746 STRCPY(buf + len, transchar_byte(*s));
6747 len += (int)STRLEN(buf + len);
6748 }
6749 }
6750 if (i == match)
6751 selend = buf + len;
6752
6753 *(buf + len++) = ' ';
6754 *(buf + len++) = ' ';
6755 clen += 2;
6756 if (++i == num_matches)
6757 break;
6758 }
6759
6760 if (i != num_matches)
6761 {
6762 *(buf + len++) = '>';
6763 ++clen;
6764 }
6765
6766 buf[len] = NUL;
6767
6768 row = cmdline_row - 1;
6769 if (row >= 0)
6770 {
6771 if (wild_menu_showing == 0)
6772 {
6773 if (msg_scrolled > 0)
6774 {
6775 /* Put the wildmenu just above the command line. If there is
6776 * no room, scroll the screen one line up. */
6777 if (cmdline_row == Rows - 1)
6778 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006779 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 ++msg_scrolled;
6781 }
6782 else
6783 {
6784 ++cmdline_row;
6785 ++row;
6786 }
6787 wild_menu_showing = WM_SCROLLED;
6788 }
6789 else
6790 {
6791 /* Create status line if needed by setting 'laststatus' to 2.
6792 * Set 'winminheight' to zero to avoid that the window is
6793 * resized. */
6794 if (lastwin->w_status_height == 0)
6795 {
6796 save_p_ls = p_ls;
6797 save_p_wmh = p_wmh;
6798 p_ls = 2;
6799 p_wmh = 0;
6800 last_status(FALSE);
6801 }
6802 wild_menu_showing = WM_SHOWN;
6803 }
6804 }
6805
6806 screen_puts(buf, row, 0, attr);
6807 if (selstart != NULL && highlight)
6808 {
6809 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006810 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812
6813 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6814 }
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 vim_free(buf);
6818}
6819#endif
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821/*
6822 * Redraw the status line of window wp.
6823 *
6824 * If inversion is possible we use it. Else '=' characters are used.
6825 */
6826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006827win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 int row;
6830 char_u *p;
6831 int len;
6832 int fillchar;
6833 int attr;
6834 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006835 static int busy = FALSE;
6836
6837 /* It's possible to get here recursively when 'statusline' (indirectly)
6838 * invokes ":redrawstatus". Simply ignore the call then. */
6839 if (busy)
6840 return;
6841 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843 wp->w_redr_status = FALSE;
6844 if (wp->w_status_height == 0)
6845 {
6846 /* no status line, can only be last window */
6847 redraw_cmdline = TRUE;
6848 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006849 else if (!redrawing()
6850#ifdef FEAT_INS_EXPAND
6851 /* don't update status line when popup menu is visible and may be
6852 * drawn over it */
6853 || pum_visible()
6854#endif
6855 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 {
6857 /* Don't redraw right now, do it later. */
6858 wp->w_redr_status = TRUE;
6859 }
6860#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006861 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006864 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866#endif
6867 else
6868 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006869 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006871 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 p = NameBuff;
6873 len = (int)STRLEN(p);
6874
Bram Moolenaard85f2712017-07-28 21:51:57 +02006875 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#ifdef FEAT_QUICKFIX
6877 || wp->w_p_pvw
6878#endif
6879 || bufIsChanged(wp->w_buffer)
6880 || wp->w_buffer->b_p_ro)
6881 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006882 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006884 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 len += (int)STRLEN(p + len);
6886 }
6887#ifdef FEAT_QUICKFIX
6888 if (wp->w_p_pvw)
6889 {
6890 STRCPY(p + len, _("[Preview]"));
6891 len += (int)STRLEN(p + len);
6892 }
6893#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006894 if (bufIsChanged(wp->w_buffer)
6895#ifdef FEAT_TERMINAL
6896 && !bt_terminal(wp->w_buffer)
6897#endif
6898 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
6900 STRCPY(p + len, "[+]");
6901 len += 3;
6902 }
6903 if (wp->w_buffer->b_p_ro)
6904 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006905 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006906 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908
Bram Moolenaar02631462017-09-22 15:20:32 +02006909 this_ru_col = ru_col - (Columns - wp->w_width);
6910 if (this_ru_col < (wp->w_width + 1) / 2)
6911 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 if (this_ru_col <= 1)
6913 {
6914 p = (char_u *)"<"; /* No room for file name! */
6915 len = 1;
6916 }
6917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#ifdef FEAT_MBYTE
6919 if (has_mbyte)
6920 {
6921 int clen = 0, i;
6922
6923 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006924 clen = mb_string2cells(p, -1);
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Find first character that will fit.
6927 * Going from start to end is much faster for DBCS. */
6928 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006929 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 clen -= (*mb_ptr2cells)(p + i);
6931 len = clen;
6932 if (i > 0)
6933 {
6934 p = p + i - 1;
6935 *p = '<';
6936 ++len;
6937 }
6938
6939 }
6940 else
6941#endif
6942 if (len > this_ru_col - 1)
6943 {
6944 p += len - (this_ru_col - 1);
6945 *p = '<';
6946 len = this_ru_col - 1;
6947 }
6948
6949 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006950 screen_puts(p, row, wp->w_wincol, attr);
6951 screen_fill(row, row + 1, len + wp->w_wincol,
6952 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006954 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6956 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006957 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
6959#ifdef FEAT_CMDL_INFO
6960 win_redr_ruler(wp, TRUE);
6961#endif
6962 }
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 /*
6965 * May need to draw the character below the vertical separator.
6966 */
6967 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6968 {
6969 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006970 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 else
6972 fillchar = fillchar_vsep(&attr);
6973 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6974 attr);
6975 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006976 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977}
6978
Bram Moolenaar238a5642006-02-21 22:12:05 +00006979#ifdef FEAT_STL_OPT
6980/*
6981 * Redraw the status line according to 'statusline' and take care of any
6982 * errors encountered.
6983 */
6984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006985redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006986{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006987 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006988 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006989
6990 /* When called recursively return. This can happen when the statusline
6991 * contains an expression that triggers a redraw. */
6992 if (entered)
6993 return;
6994 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006995
Bram Moolenaara742e082016-04-05 21:10:38 +02006996 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006997 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006998 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006999 {
7000 /* When there is an error disable the statusline, otherwise the
7001 * display is messed up with errors and a redraw triggers the problem
7002 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007003 set_string_option_direct((char_u *)"statusline", -1,
7004 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007005 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007007 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007008 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009}
7010#endif
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012/*
7013 * Return TRUE if the status line of window "wp" is connected to the status
7014 * line of the window right of it. If not, then it's a vertical separator.
7015 * Only call if (wp->w_vsep_width != 0).
7016 */
7017 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007018stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 frame_T *fr;
7021
7022 fr = wp->w_frame;
7023 while (fr->fr_parent != NULL)
7024 {
7025 if (fr->fr_parent->fr_layout == FR_COL)
7026 {
7027 if (fr->fr_next != NULL)
7028 break;
7029 }
7030 else
7031 {
7032 if (fr->fr_next != NULL)
7033 return TRUE;
7034 }
7035 fr = fr->fr_parent;
7036 }
7037 return FALSE;
7038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041/*
7042 * Get the value to show for the language mappings, active 'keymap'.
7043 */
7044 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007045get_keymap_str(
7046 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007047 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007048 char_u *buf, /* buffer for the result */
7049 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
7051 char_u *p;
7052
7053 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7054 return FALSE;
7055
7056 {
7057#ifdef FEAT_EVAL
7058 buf_T *old_curbuf = curbuf;
7059 win_T *old_curwin = curwin;
7060 char_u *s;
7061
7062 curbuf = wp->w_buffer;
7063 curwin = wp;
7064 STRCPY(buf, "b:keymap_name"); /* must be writable */
7065 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007066 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 --emsg_skip;
7068 curbuf = old_curbuf;
7069 curwin = old_curwin;
7070 if (p == NULL || *p == NUL)
7071#endif
7072 {
7073#ifdef FEAT_KEYMAP
7074 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7075 p = wp->w_buffer->b_p_keymap;
7076 else
7077#endif
7078 p = (char_u *)"lang";
7079 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007080 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 buf[0] = NUL;
7082#ifdef FEAT_EVAL
7083 vim_free(s);
7084#endif
7085 }
7086 return buf[0] != NUL;
7087}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
7089#if defined(FEAT_STL_OPT) || defined(PROTO)
7090/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007091 * Redraw the status line or ruler of window "wp".
7092 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 */
7094 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007095win_redr_custom(
7096 win_T *wp,
7097 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007099 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int attr;
7101 int curattr;
7102 int row;
7103 int col = 0;
7104 int maxwidth;
7105 int width;
7106 int n;
7107 int len;
7108 int fillchar;
7109 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007110 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007112 struct stl_hlrec hltab[STL_MAX_ITEM];
7113 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007114 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007115 win_T *ewp;
7116 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar1d633412013-12-11 15:52:01 +01007118 /* There is a tiny chance that this gets called recursively: When
7119 * redrawing a status line triggers redrawing the ruler or tabline.
7120 * Avoid trouble by not allowing recursion. */
7121 if (entered)
7122 return;
7123 entered = TRUE;
7124
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007126 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007129 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007130 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007131 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007132 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 maxwidth = Columns;
7134# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007135 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 else
7139 {
7140 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007141 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007142 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143
7144 if (draw_ruler)
7145 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007146 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007148 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007149 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007150 if (*++stl == '-')
7151 stl++;
7152 if (atoi((char *)stl))
7153 while (VIM_ISDIGIT(*stl))
7154 stl++;
7155 if (*stl++ != '(')
7156 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007158 col = ru_col - (Columns - wp->w_width);
7159 if (col < (wp->w_width + 1) / 2)
7160 col = (wp->w_width + 1) / 2;
7161 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163 {
7164 row = Rows - 1;
7165 --maxwidth; /* writing in last column may cause scrolling */
7166 fillchar = ' ';
7167 attr = 0;
7168 }
7169
7170# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007171 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007172# endif
7173 }
7174 else
7175 {
7176 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007177 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007179 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007181 use_sandbox = was_set_insecurely((char_u *)"statusline",
7182 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183# endif
7184 }
7185
Bram Moolenaar53f81742017-09-22 14:35:51 +02007186 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187 }
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007190 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar61452852011-02-01 18:01:11 +01007192 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7193 * the cursor away and back. */
7194 ewp = wp == NULL ? curwin : wp;
7195 p_crb_save = ewp->w_p_crb;
7196 ewp->w_p_crb = FALSE;
7197
Bram Moolenaar362f3562009-11-03 16:20:34 +00007198 /* Make a copy, because the statusline may include a function call that
7199 * might change the option value and free the memory. */
7200 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007201 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007202 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007203 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007204 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007205 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007207 /* Make all characters printable. */
7208 p = transstr(buf);
7209 if (p != NULL)
7210 {
7211 vim_strncpy(buf, p, sizeof(buf) - 1);
7212 vim_free(p);
7213 }
7214
7215 /* fill up with "fillchar" */
7216 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007217 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
7219#ifdef FEAT_MBYTE
7220 len += (*mb_char2bytes)(fillchar, buf + len);
7221#else
7222 buf[len++] = fillchar;
7223#endif
7224 ++width;
7225 }
7226 buf[len] = NUL;
7227
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007228 /*
7229 * Draw each snippet with the specified highlighting.
7230 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 curattr = attr;
7232 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007233 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007235 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 screen_puts_len(p, len, row, col, curattr);
7237 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007238 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007240 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007242 else if (hltab[n].userhl < 0)
7243 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007244#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007245 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7246 && wp->w_status_height != 0)
7247 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007248 else if (wp != NULL && bt_terminal(wp->w_buffer)
7249 && wp->w_status_height != 0)
7250 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007251#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007252 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258
7259 if (wp == NULL)
7260 {
7261 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7262 col = 0;
7263 len = 0;
7264 p = buf;
7265 fillchar = 0;
7266 for (n = 0; tabtab[n].start != NULL; n++)
7267 {
7268 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7269 while (col < len)
7270 TabPageIdxs[col++] = fillchar;
7271 p = tabtab[n].start;
7272 fillchar = tabtab[n].userhl;
7273 }
7274 while (col < Columns)
7275 TabPageIdxs[col++] = fillchar;
7276 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007277
7278theend:
7279 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280}
7281
7282#endif /* FEAT_STL_OPT */
7283
7284/*
7285 * Output a single character directly to the screen and update ScreenLines.
7286 */
7287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 char_u buf[MB_MAXBYTES + 1];
7291
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007292#ifdef FEAT_MBYTE
7293 if (has_mbyte)
7294 buf[(*mb_char2bytes)(c, buf)] = NUL;
7295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007297 {
7298 buf[0] = c;
7299 buf[1] = NUL;
7300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 screen_puts(buf, row, col, attr);
7302}
7303
7304/*
7305 * Get a single character directly from ScreenLines into "bytes[]".
7306 * Also return its attribute in *attrp;
7307 */
7308 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007309screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 unsigned off;
7312
7313 /* safety check */
7314 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7315 {
7316 off = LineOffset[row] + col;
7317 *attrp = ScreenAttrs[off];
7318 bytes[0] = ScreenLines[off];
7319 bytes[1] = NUL;
7320
7321#ifdef FEAT_MBYTE
7322 if (enc_utf8 && ScreenLinesUC[off] != 0)
7323 bytes[utfc_char2bytes(off, bytes)] = NUL;
7324 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7325 {
7326 bytes[0] = ScreenLines[off];
7327 bytes[1] = ScreenLines2[off];
7328 bytes[2] = NUL;
7329 }
7330 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7331 {
7332 bytes[1] = ScreenLines[off + 1];
7333 bytes[2] = NUL;
7334 }
7335#endif
7336 }
7337}
7338
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007339#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007340static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007341
7342/*
7343 * Return TRUE if composing characters for screen posn "off" differs from
7344 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007345 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007346 */
7347 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007348screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007349{
7350 int i;
7351
7352 for (i = 0; i < Screen_mco; ++i)
7353 {
7354 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7355 return TRUE;
7356 if (u8cc[i] == 0)
7357 break;
7358 }
7359 return FALSE;
7360}
7361#endif
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363/*
7364 * Put string '*text' on the screen at position 'row' and 'col', with
7365 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7366 * Note: only outputs within one row, message is truncated at screen boundary!
7367 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7368 */
7369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007370screen_puts(
7371 char_u *text,
7372 int row,
7373 int col,
7374 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 screen_puts_len(text, -1, row, col, attr);
7377}
7378
7379/*
7380 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7381 * a NUL.
7382 */
7383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007384screen_puts_len(
7385 char_u *text,
7386 int textlen,
7387 int row,
7388 int col,
7389 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 unsigned off;
7392 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007393 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int c;
7395#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007396 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 int mbyte_blen = 1;
7398 int mbyte_cells = 1;
7399 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007400 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 int clear_next_cell = FALSE;
7402# ifdef FEAT_ARABIC
7403 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404 int pc, nc, nc1;
7405 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406# endif
7407#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007408#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7409 int force_redraw_this;
7410 int force_redraw_next = FALSE;
7411#endif
7412 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7415 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007416 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
Bram Moolenaarc236c162008-07-13 17:41:49 +00007418#ifdef FEAT_MBYTE
7419 /* When drawing over the right halve of a double-wide char clear out the
7420 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007421 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007422# ifdef FEAT_GUI
7423 && !gui.in_use
7424# endif
7425 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007426 {
7427 ScreenLines[off - 1] = ' ';
7428 ScreenAttrs[off - 1] = 0;
7429 if (enc_utf8)
7430 {
7431 ScreenLinesUC[off - 1] = 0;
7432 ScreenLinesC[0][off - 1] = 0;
7433 }
7434 /* redraw the previous cell, make it empty */
7435 screen_char(off - 1, row, col - 1);
7436 /* force the cell at "col" to be redrawn */
7437 force_redraw_next = TRUE;
7438 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439#endif
7440
Bram Moolenaar367329b2007-08-30 11:53:22 +00007441#ifdef FEAT_MBYTE
7442 max_off = LineOffset[row] + screen_Columns;
7443#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007444 while (col < screen_Columns
7445 && (len < 0 || (int)(ptr - text) < len)
7446 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
7448 c = *ptr;
7449#ifdef FEAT_MBYTE
7450 /* check if this is the first byte of a multibyte */
7451 if (has_mbyte)
7452 {
7453 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007454 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007456 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7458 mbyte_cells = 1;
7459 else if (enc_dbcs != 0)
7460 mbyte_cells = mbyte_blen;
7461 else /* enc_utf8 */
7462 {
7463 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007464 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 (int)((text + len) - ptr));
7466 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007467 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007469# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 /* Non-BMP character: display as ? or fullwidth ?. */
7471 if (u8c >= 0x10000)
7472 {
7473 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7474 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007475 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478# ifdef FEAT_ARABIC
7479 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7480 {
7481 /* Do Arabic shaping. */
7482 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7483 {
7484 /* Past end of string to be displayed. */
7485 nc = NUL;
7486 nc1 = NUL;
7487 }
7488 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007489 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007490 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7491 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007492 nc1 = pcc[0];
7493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 pc = prev_c;
7495 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007496 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498 else
7499 prev_c = u8c;
7500# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007501 if (col + mbyte_cells > screen_Columns)
7502 {
7503 /* Only 1 cell left, but character requires 2 cells:
7504 * display a '>' in the last column to avoid wrapping. */
7505 c = '>';
7506 mbyte_cells = 1;
7507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509 }
7510#endif
7511
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007512#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7513 force_redraw_this = force_redraw_next;
7514 force_redraw_next = FALSE;
7515#endif
7516
7517 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#ifdef FEAT_MBYTE
7519 || (mbyte_cells == 2
7520 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7521 || (enc_dbcs == DBCS_JPNU
7522 && c == 0x8e
7523 && ScreenLines2[off] != ptr[1])
7524 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007525 && (ScreenLinesUC[off] !=
7526 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7527 || (ScreenLinesUC[off] != 0
7528 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529#endif
7530 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007531 || exmode_active;
7532
7533 if (need_redraw
7534#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7535 || force_redraw_this
7536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 )
7538 {
7539#if defined(FEAT_GUI) || defined(UNIX)
7540 /* The bold trick makes a single row of pixels appear in the next
7541 * character. When a bold character is removed, the next
7542 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007543 * and for some xterms. */
7544 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545# ifdef FEAT_GUI
7546 gui.in_use
7547# endif
7548# if defined(FEAT_GUI) && defined(UNIX)
7549 ||
7550# endif
7551# ifdef UNIX
7552 term_is_xterm
7553# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007558 if (n > HL_ALL)
7559 n = syn_attr2attr(n);
7560 if (n & HL_BOLD)
7561 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 }
7563#endif
7564#ifdef FEAT_MBYTE
7565 /* When at the end of the text and overwriting a two-cell
7566 * character with a one-cell character, need to clear the next
7567 * cell. Also when overwriting the left halve of a two-cell char
7568 * with the right halve of a two-cell char. Do this only once
7569 * (mb_off2cells() may return 2 on the right halve). */
7570 if (clear_next_cell)
7571 clear_next_cell = FALSE;
7572 else if (has_mbyte
7573 && (len < 0 ? ptr[mbyte_blen] == NUL
7574 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007575 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577 && (*mb_off2cells)(off, max_off) == 1
7578 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 clear_next_cell = TRUE;
7580
7581 /* Make sure we never leave a second byte of a double-byte behind,
7582 * it confuses mb_off2cells(). */
7583 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007584 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007586 && (*mb_off2cells)(off, max_off) == 1
7587 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 ScreenLines[off + mbyte_blen] = 0;
7589#endif
7590 ScreenLines[off] = c;
7591 ScreenAttrs[off] = attr;
7592#ifdef FEAT_MBYTE
7593 if (enc_utf8)
7594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007595 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 ScreenLinesUC[off] = 0;
7597 else
7598 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 int i;
7600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 for (i = 0; i < Screen_mco; ++i)
7603 {
7604 ScreenLinesC[i][off] = u8cc[i];
7605 if (u8cc[i] == 0)
7606 break;
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 if (mbyte_cells == 2)
7610 {
7611 ScreenLines[off + 1] = 0;
7612 ScreenAttrs[off + 1] = attr;
7613 }
7614 screen_char(off, row, col);
7615 }
7616 else if (mbyte_cells == 2)
7617 {
7618 ScreenLines[off + 1] = ptr[1];
7619 ScreenAttrs[off + 1] = attr;
7620 screen_char_2(off, row, col);
7621 }
7622 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7623 {
7624 ScreenLines2[off] = ptr[1];
7625 screen_char(off, row, col);
7626 }
7627 else
7628#endif
7629 screen_char(off, row, col);
7630 }
7631#ifdef FEAT_MBYTE
7632 if (has_mbyte)
7633 {
7634 off += mbyte_cells;
7635 col += mbyte_cells;
7636 ptr += mbyte_blen;
7637 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007638 {
7639 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007641 len = -1;
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
7644 else
7645#endif
7646 {
7647 ++off;
7648 ++col;
7649 ++ptr;
7650 }
7651 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652
7653#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7654 /* If we detected the next character needs to be redrawn, but the text
7655 * doesn't extend up to there, update the character here. */
7656 if (force_redraw_next && col < screen_Columns)
7657 {
7658# ifdef FEAT_MBYTE
7659 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7660 screen_char_2(off, row, col);
7661 else
7662# endif
7663 screen_char(off, row, col);
7664 }
7665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668#ifdef FEAT_SEARCH_EXTRA
7669/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007670 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 */
7672 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007673start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 if (p_hls && !no_hlsearch)
7676 {
7677 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007678 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007679# ifdef FEAT_RELTIME
7680 /* Set the time limit to 'redrawtime'. */
7681 profile_setlimit(p_rdt, &search_hl.tm);
7682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
7684}
7685
7686/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007687 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 */
7689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007690end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 if (search_hl.rm.regprog != NULL)
7693 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007694 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 search_hl.rm.regprog = NULL;
7696 }
7697}
7698
7699/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007700 * Init for calling prepare_search_hl().
7701 */
7702 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007703init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007704{
7705 matchitem_T *cur;
7706
7707 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7708 * match */
7709 cur = wp->w_match_head;
7710 while (cur != NULL)
7711 {
7712 cur->hl.rm = cur->match;
7713 if (cur->hlg_id == 0)
7714 cur->hl.attr = 0;
7715 else
7716 cur->hl.attr = syn_id2attr(cur->hlg_id);
7717 cur->hl.buf = wp->w_buffer;
7718 cur->hl.lnum = 0;
7719 cur->hl.first_lnum = 0;
7720# ifdef FEAT_RELTIME
7721 /* Set the time limit to 'redrawtime'. */
7722 profile_setlimit(p_rdt, &(cur->hl.tm));
7723# endif
7724 cur = cur->next;
7725 }
7726 search_hl.buf = wp->w_buffer;
7727 search_hl.lnum = 0;
7728 search_hl.first_lnum = 0;
7729 /* time limit is set at the toplevel, for all windows */
7730}
7731
7732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 * Advance to the match in window "wp" line "lnum" or past it.
7734 */
7735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007736prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007738 matchitem_T *cur; /* points to the match list */
7739 match_T *shl; /* points to search_hl or a match */
7740 int shl_flag; /* flag to indicate whether search_hl
7741 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007742 int pos_inprogress; /* marks that position match search is
7743 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 int n;
7745
7746 /*
7747 * When using a multi-line pattern, start searching at the top
7748 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007749 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007751 cur = wp->w_match_head;
7752 shl_flag = FALSE;
7753 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007755 if (shl_flag == FALSE)
7756 {
7757 shl = &search_hl;
7758 shl_flag = TRUE;
7759 }
7760 else
7761 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (shl->rm.regprog != NULL
7763 && shl->lnum == 0
7764 && re_multiline(shl->rm.regprog))
7765 {
7766 if (shl->first_lnum == 0)
7767 {
7768# ifdef FEAT_FOLDING
7769 for (shl->first_lnum = lnum;
7770 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7771 if (hasFoldingWin(wp, shl->first_lnum - 1,
7772 NULL, NULL, TRUE, NULL))
7773 break;
7774# else
7775 shl->first_lnum = wp->w_topline;
7776# endif
7777 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007778 if (cur != NULL)
7779 cur->pos.cur = 0;
7780 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007782 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7783 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007785 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7786 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007787 pos_inprogress = cur == NULL || cur->pos.cur == 0
7788 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 if (shl->lnum != 0)
7790 {
7791 shl->first_lnum = shl->lnum
7792 + shl->rm.endpos[0].lnum
7793 - shl->rm.startpos[0].lnum;
7794 n = shl->rm.endpos[0].col;
7795 }
7796 else
7797 {
7798 ++shl->first_lnum;
7799 n = 0;
7800 }
7801 }
7802 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007803 if (shl != &search_hl && cur != NULL)
7804 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
7806}
7807
7808/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007809 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 * Uses shl->buf.
7811 * Sets shl->lnum and shl->rm contents.
7812 * Note: Assumes a previous match is always before "lnum", unless
7813 * shl->lnum is zero.
7814 * Careful: Any pointers for buffer lines will become invalid.
7815 */
7816 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007817next_search_hl(
7818 win_T *win,
7819 match_T *shl, /* points to search_hl or a match */
7820 linenr_T lnum,
7821 colnr_T mincol, /* minimal column for a match */
7822 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 linenr_T l;
7825 colnr_T matchcol;
7826 long nmatched;
7827
7828 if (shl->lnum != 0)
7829 {
7830 /* Check for three situations:
7831 * 1. If the "lnum" is below a previous match, start a new search.
7832 * 2. If the previous match includes "mincol", use it.
7833 * 3. Continue after the previous match.
7834 */
7835 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7836 if (lnum > l)
7837 shl->lnum = 0;
7838 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7839 return;
7840 }
7841
7842 /*
7843 * Repeat searching for a match until one is found that includes "mincol"
7844 * or none is found in this line.
7845 */
7846 called_emsg = FALSE;
7847 for (;;)
7848 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007849#ifdef FEAT_RELTIME
7850 /* Stop searching after passing the time limit. */
7851 if (profile_passed_limit(&(shl->tm)))
7852 {
7853 shl->lnum = 0; /* no match found in time */
7854 break;
7855 }
7856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 /* Three situations:
7858 * 1. No useful previous match: search from start of line.
7859 * 2. Not Vi compatible or empty match: continue at next character.
7860 * Break the loop if this is beyond the end of the line.
7861 * 3. Vi compatible searching: continue at end of previous match.
7862 */
7863 if (shl->lnum == 0)
7864 matchcol = 0;
7865 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7866 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007867 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007869 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007870
7871 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007872 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007873 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007875 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 shl->lnum = 0;
7877 break;
7878 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007879#ifdef FEAT_MBYTE
7880 if (has_mbyte)
7881 matchcol += mb_ptr2len(ml);
7882 else
7883#endif
7884 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886 else
7887 matchcol = shl->rm.endpos[0].col;
7888
7889 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007890 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007892 /* Remember whether shl->rm is using a copy of the regprog in
7893 * cur->match. */
7894 int regprog_is_copy = (shl != &search_hl && cur != NULL
7895 && shl == &cur->hl
7896 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007897 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007898
Bram Moolenaarb3414592014-06-17 17:48:32 +02007899 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7900 matchcol,
7901#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007902 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007903#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007904 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905#endif
7906 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007907 /* Copy the regprog, in case it got freed and recompiled. */
7908 if (regprog_is_copy)
7909 cur->match.regprog = cur->hl.rm.regprog;
7910
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007911 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007912 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007913 /* Error while handling regexp: stop using this regexp. */
7914 if (shl == &search_hl)
7915 {
7916 /* don't free regprog in the match list, it's a copy */
7917 vim_regfree(shl->rm.regprog);
7918 SET_NO_HLSEARCH(TRUE);
7919 }
7920 shl->rm.regprog = NULL;
7921 shl->lnum = 0;
7922 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7923 message */
7924 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007925 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007926 }
7927 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007929 else
7930 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (nmatched == 0)
7932 {
7933 shl->lnum = 0; /* no match found */
7934 break;
7935 }
7936 if (shl->rm.startpos[0].lnum > 0
7937 || shl->rm.startpos[0].col >= mincol
7938 || nmatched > 1
7939 || shl->rm.endpos[0].col > mincol)
7940 {
7941 shl->lnum += shl->rm.startpos[0].lnum;
7942 break; /* useful match found */
7943 }
7944 }
7945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
Bram Moolenaar85077472016-10-16 14:35:48 +02007947/*
7948 * If there is a match fill "shl" and return one.
7949 * Return zero otherwise.
7950 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007952next_search_hl_pos(
7953 match_T *shl, /* points to a match */
7954 linenr_T lnum,
7955 posmatch_T *posmatch, /* match positions */
7956 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957{
7958 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007959 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7962 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007963 llpos_T *pos = &posmatch->pos[i];
7964
7965 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007967 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007969 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007971 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007973 /* if this match comes before the one at "found" then swap
7974 * them */
7975 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007977 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978
Bram Moolenaar85077472016-10-16 14:35:48 +02007979 *pos = posmatch->pos[found];
7980 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 }
7982 }
7983 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007984 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 }
7986 }
7987 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007988 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007990 colnr_T start = posmatch->pos[found].col == 0
7991 ? 0 : posmatch->pos[found].col - 1;
7992 colnr_T end = posmatch->pos[found].col == 0
7993 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007994
Bram Moolenaar85077472016-10-16 14:35:48 +02007995 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996 shl->rm.startpos[0].lnum = 0;
7997 shl->rm.startpos[0].col = start;
7998 shl->rm.endpos[0].lnum = 0;
7999 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008000 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008001 posmatch->cur = found + 1;
8002 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008004 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008006#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008009screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 attrentry_T *aep = NULL;
8012
8013 screen_attr = attr;
8014 if (full_screen
8015#ifdef WIN3264
8016 && termcap_active
8017#endif
8018 )
8019 {
8020#ifdef FEAT_GUI
8021 if (gui.in_use)
8022 {
8023 char buf[20];
8024
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008025 /* The GUI handles this internally. */
8026 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 OUT_STR(buf);
8028 }
8029 else
8030#endif
8031 {
8032 if (attr > HL_ALL) /* special HL attr. */
8033 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008034 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 aep = syn_cterm_attr2entry(attr);
8036 else
8037 aep = syn_term_attr2entry(attr);
8038 if (aep == NULL) /* did ":syntax clear" */
8039 attr = 0;
8040 else
8041 attr = aep->ae_attr;
8042 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008043 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008045 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008046#ifdef FEAT_TERMGUICOLORS
8047 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008048 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008049#endif
8050 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008051#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008052 )
8053#endif
8054 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008055 /* If the Normal FG color has BOLD attribute and the new HL
8056 * has a FG color defined, clear BOLD. */
8057 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008058 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008060 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008061 out_str(T_UCS);
8062 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008063 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8064 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008066 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008068 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008070 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008071 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
8073 /*
8074 * Output the color or start string after bold etc., in case the
8075 * bold etc. override the color setting.
8076 */
8077 if (aep != NULL)
8078 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008079#ifdef FEAT_TERMGUICOLORS
8080 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008082 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008083 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008084 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008085 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008090 if (t_colors > 1)
8091 {
8092 if (aep->ae_u.cterm.fg_color)
8093 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8094 if (aep->ae_u.cterm.bg_color)
8095 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8096 }
8097 else
8098 {
8099 if (aep->ae_u.term.start != NULL)
8100 out_str(aep->ae_u.term.start);
8101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 }
8104 }
8105 }
8106}
8107
8108 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008109screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
8111 int do_ME = FALSE; /* output T_ME code */
8112
8113 if (screen_attr != 0
8114#ifdef WIN3264
8115 && termcap_active
8116#endif
8117 )
8118 {
8119#ifdef FEAT_GUI
8120 if (gui.in_use)
8121 {
8122 char buf[20];
8123
8124 /* use internal GUI code */
8125 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8126 OUT_STR(buf);
8127 }
8128 else
8129#endif
8130 {
8131 if (screen_attr > HL_ALL) /* special HL attr. */
8132 {
8133 attrentry_T *aep;
8134
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008135 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {
8137 /*
8138 * Assume that t_me restores the original colors!
8139 */
8140 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008141 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008142#ifdef FEAT_TERMGUICOLORS
8143 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008144 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8145 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008146#endif
8147 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008148#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008149 )
8150#endif
8151 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 do_ME = TRUE;
8153 }
8154 else
8155 {
8156 aep = syn_term_attr2entry(screen_attr);
8157 if (aep != NULL && aep->ae_u.term.stop != NULL)
8158 {
8159 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8160 do_ME = TRUE;
8161 else
8162 out_str(aep->ae_u.term.stop);
8163 }
8164 }
8165 if (aep == NULL) /* did ":syntax clear" */
8166 screen_attr = 0;
8167 else
8168 screen_attr = aep->ae_attr;
8169 }
8170
8171 /*
8172 * Often all ending-codes are equal to T_ME. Avoid outputting the
8173 * same sequence several times.
8174 */
8175 if (screen_attr & HL_STANDOUT)
8176 {
8177 if (STRCMP(T_SE, T_ME) == 0)
8178 do_ME = TRUE;
8179 else
8180 out_str(T_SE);
8181 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008182 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008183 {
8184 if (STRCMP(T_UCE, T_ME) == 0)
8185 do_ME = TRUE;
8186 else
8187 out_str(T_UCE);
8188 }
8189 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008190 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
8192 if (STRCMP(T_UE, T_ME) == 0)
8193 do_ME = TRUE;
8194 else
8195 out_str(T_UE);
8196 }
8197 if (screen_attr & HL_ITALIC)
8198 {
8199 if (STRCMP(T_CZR, T_ME) == 0)
8200 do_ME = TRUE;
8201 else
8202 out_str(T_CZR);
8203 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008204 if (screen_attr & HL_STRIKETHROUGH)
8205 {
8206 if (STRCMP(T_STE, T_ME) == 0)
8207 do_ME = TRUE;
8208 else
8209 out_str(T_STE);
8210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8212 out_str(T_ME);
8213
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008214#ifdef FEAT_TERMGUICOLORS
8215 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008217 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008218 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008219 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008220 term_bg_rgb_color(cterm_normal_bg_gui_color);
8221 }
8222 else
8223#endif
8224 {
8225 if (t_colors > 1)
8226 {
8227 /* set Normal cterm colors */
8228 if (cterm_normal_fg_color != 0)
8229 term_fg_color(cterm_normal_fg_color - 1);
8230 if (cterm_normal_bg_color != 0)
8231 term_bg_color(cterm_normal_bg_color - 1);
8232 if (cterm_normal_fg_bold)
8233 out_str(T_MD);
8234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
8236 }
8237 }
8238 screen_attr = 0;
8239}
8240
8241/*
8242 * Reset the colors for a cterm. Used when leaving Vim.
8243 * The machine specific code may override this again.
8244 */
8245 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008246reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008248 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
8250 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008251#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008252 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8253 || cterm_normal_bg_gui_color != INVALCOLOR)
8254 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008255#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {
8259 out_str(T_OP);
8260 screen_attr = -1;
8261 }
8262 if (cterm_normal_fg_bold)
8263 {
8264 out_str(T_ME);
8265 screen_attr = -1;
8266 }
8267 }
8268}
8269
8270/*
8271 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8272 * using the attributes from ScreenAttrs["off"].
8273 */
8274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008275screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
8277 int attr;
8278
8279 /* Check for illegal values, just in case (could happen just after
8280 * resizing). */
8281 if (row >= screen_Rows || col >= screen_Columns)
8282 return;
8283
Bram Moolenaar494838a2015-02-10 19:20:37 +01008284 /* Outputting a character in the last cell on the screen may scroll the
8285 * screen up. Only do it when the "xn" termcap property is set, otherwise
8286 * mark the character invalid (update it when scrolled up). */
8287 if (*T_XN == NUL
8288 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289#ifdef FEAT_RIGHTLEFT
8290 /* account for first command-line character in rightleft mode */
8291 && !cmdmsg_rl
8292#endif
8293 )
8294 {
8295 ScreenAttrs[off] = (sattr_T)-1;
8296 return;
8297 }
8298
8299 /*
8300 * Stop highlighting first, so it's easier to move the cursor.
8301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (screen_char_attr != 0)
8303 attr = screen_char_attr;
8304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 attr = ScreenAttrs[off];
8306 if (screen_attr != attr)
8307 screen_stop_highlight();
8308
8309 windgoto(row, col);
8310
8311 if (screen_attr != attr)
8312 screen_start_highlight(attr);
8313
8314#ifdef FEAT_MBYTE
8315 if (enc_utf8 && ScreenLinesUC[off] != 0)
8316 {
8317 char_u buf[MB_MAXBYTES + 1];
8318
Bram Moolenaarcb070082016-04-02 22:14:51 +02008319 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008320 {
8321 if (*p_ambw == 'd'
8322# ifdef FEAT_GUI
8323 && !gui.in_use
8324# endif
8325 )
8326 {
8327 /* Clear the two screen cells. If the character is actually
8328 * single width it won't change the second cell. */
8329 out_str((char_u *)" ");
8330 term_windgoto(row, col);
8331 }
8332 /* not sure where the cursor is after drawing the ambiguous width
8333 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008334 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008335 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008336 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008338
8339 /* Convert the UTF-8 character to bytes and write it. */
8340 buf[utfc_char2bytes(off, buf)] = NUL;
8341 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343 else
8344#endif
8345 {
8346#ifdef FEAT_MBYTE
8347 out_flush_check();
8348#endif
8349 out_char(ScreenLines[off]);
8350#ifdef FEAT_MBYTE
8351 /* double-byte character in single-width cell */
8352 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8353 out_char(ScreenLines2[off]);
8354#endif
8355 }
8356
8357 screen_cur_col++;
8358}
8359
8360#ifdef FEAT_MBYTE
8361
8362/*
8363 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8364 * on the screen at position 'row' and 'col'.
8365 * The attributes of the first byte is used for all. This is required to
8366 * output the two bytes of a double-byte character with nothing in between.
8367 */
8368 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008369screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 /* Check for illegal values (could be wrong when screen was resized). */
8372 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8373 return;
8374
8375 /* Outputting the last character on the screen may scrollup the screen.
8376 * Don't to it! Mark the character invalid (update it when scrolled up) */
8377 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8378 {
8379 ScreenAttrs[off] = (sattr_T)-1;
8380 return;
8381 }
8382
8383 /* Output the first byte normally (positions the cursor), then write the
8384 * second byte directly. */
8385 screen_char(off, row, col);
8386 out_char(ScreenLines[off + 1]);
8387 ++screen_cur_col;
8388}
8389#endif
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391/*
8392 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8393 * This uses the contents of ScreenLines[] and doesn't change it.
8394 */
8395 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008396screen_draw_rectangle(
8397 int row,
8398 int col,
8399 int height,
8400 int width,
8401 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int r, c;
8404 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008405#ifdef FEAT_MBYTE
8406 int max_off;
8407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008409 /* Can't use ScreenLines unless initialized */
8410 if (ScreenLines == NULL)
8411 return;
8412
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 if (invert)
8414 screen_char_attr = HL_INVERSE;
8415 for (r = row; r < row + height; ++r)
8416 {
8417 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008418#ifdef FEAT_MBYTE
8419 max_off = off + screen_Columns;
8420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 for (c = col; c < col + width; ++c)
8422 {
8423#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008424 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 screen_char_2(off + c, r, c);
8427 ++c;
8428 }
8429 else
8430#endif
8431 {
8432 screen_char(off + c, r, c);
8433#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008434 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 ++c;
8436#endif
8437 }
8438 }
8439 }
8440 screen_char_attr = 0;
8441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443/*
8444 * Redraw the characters for a vertically split window.
8445 */
8446 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008447redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448{
8449 int col;
8450 int width;
8451
8452# ifdef FEAT_CLIPBOARD
8453 clip_may_clear_selection(row, end - 1);
8454# endif
8455
8456 if (wp == NULL)
8457 {
8458 col = 0;
8459 width = Columns;
8460 }
8461 else
8462 {
8463 col = wp->w_wincol;
8464 width = wp->w_width;
8465 }
8466 screen_draw_rectangle(row, col, end - row, width, FALSE);
8467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008469 static void
8470space_to_screenline(int off, int attr)
8471{
8472 ScreenLines[off] = ' ';
8473 ScreenAttrs[off] = attr;
8474# ifdef FEAT_MBYTE
8475 if (enc_utf8)
8476 ScreenLinesUC[off] = 0;
8477# endif
8478}
8479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480/*
8481 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8482 * with character 'c1' in first column followed by 'c2' in the other columns.
8483 * Use attributes 'attr'.
8484 */
8485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008486screen_fill(
8487 int start_row,
8488 int end_row,
8489 int start_col,
8490 int end_col,
8491 int c1,
8492 int c2,
8493 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 int row;
8496 int col;
8497 int off;
8498 int end_off;
8499 int did_delete;
8500 int c;
8501 int norm_term;
8502#if defined(FEAT_GUI) || defined(UNIX)
8503 int force_next = FALSE;
8504#endif
8505
8506 if (end_row > screen_Rows) /* safety check */
8507 end_row = screen_Rows;
8508 if (end_col > screen_Columns) /* safety check */
8509 end_col = screen_Columns;
8510 if (ScreenLines == NULL
8511 || start_row >= end_row
8512 || start_col >= end_col) /* nothing to do */
8513 return;
8514
8515 /* it's a "normal" terminal when not in a GUI or cterm */
8516 norm_term = (
8517#ifdef FEAT_GUI
8518 !gui.in_use &&
8519#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008520 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 for (row = start_row; row < end_row; ++row)
8522 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008523#ifdef FEAT_MBYTE
8524 if (has_mbyte
8525# ifdef FEAT_GUI
8526 && !gui.in_use
8527# endif
8528 )
8529 {
8530 /* When drawing over the right halve of a double-wide char clear
8531 * out the left halve. When drawing over the left halve of a
8532 * double wide-char clear out the right halve. Only needed in a
8533 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008534 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008535 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008536 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008537 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008538 }
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 /*
8541 * Try to use delete-line termcap code, when no attributes or in a
8542 * "normal" terminal, where a bold/italic space is just a
8543 * space.
8544 */
8545 did_delete = FALSE;
8546 if (c2 == ' '
8547 && end_col == Columns
8548 && can_clear(T_CE)
8549 && (attr == 0
8550 || (norm_term
8551 && attr <= HL_ALL
8552 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8553 {
8554 /*
8555 * check if we really need to clear something
8556 */
8557 col = start_col;
8558 if (c1 != ' ') /* don't clear first char */
8559 ++col;
8560
8561 off = LineOffset[row] + col;
8562 end_off = LineOffset[row] + end_col;
8563
8564 /* skip blanks (used often, keep it fast!) */
8565#ifdef FEAT_MBYTE
8566 if (enc_utf8)
8567 while (off < end_off && ScreenLines[off] == ' '
8568 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8569 ++off;
8570 else
8571#endif
8572 while (off < end_off && ScreenLines[off] == ' '
8573 && ScreenAttrs[off] == 0)
8574 ++off;
8575 if (off < end_off) /* something to be cleared */
8576 {
8577 col = off - LineOffset[row];
8578 screen_stop_highlight();
8579 term_windgoto(row, col);/* clear rest of this screen line */
8580 out_str(T_CE);
8581 screen_start(); /* don't know where cursor is now */
8582 col = end_col - col;
8583 while (col--) /* clear chars in ScreenLines */
8584 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008585 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 ++off;
8587 }
8588 }
8589 did_delete = TRUE; /* the chars are cleared now */
8590 }
8591
8592 off = LineOffset[row] + start_col;
8593 c = c1;
8594 for (col = start_col; col < end_col; ++col)
8595 {
8596 if (ScreenLines[off] != c
8597#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008598 || (enc_utf8 && (int)ScreenLinesUC[off]
8599 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600#endif
8601 || ScreenAttrs[off] != attr
8602#if defined(FEAT_GUI) || defined(UNIX)
8603 || force_next
8604#endif
8605 )
8606 {
8607#if defined(FEAT_GUI) || defined(UNIX)
8608 /* The bold trick may make a single row of pixels appear in
8609 * the next character. When a bold character is removed, the
8610 * next character should be redrawn too. This happens for our
8611 * own GUI and for some xterms. */
8612 if (
8613# ifdef FEAT_GUI
8614 gui.in_use
8615# endif
8616# if defined(FEAT_GUI) && defined(UNIX)
8617 ||
8618# endif
8619# ifdef UNIX
8620 term_is_xterm
8621# endif
8622 )
8623 {
8624 if (ScreenLines[off] != ' '
8625 && (ScreenAttrs[off] > HL_ALL
8626 || ScreenAttrs[off] & HL_BOLD))
8627 force_next = TRUE;
8628 else
8629 force_next = FALSE;
8630 }
8631#endif
8632 ScreenLines[off] = c;
8633#ifdef FEAT_MBYTE
8634 if (enc_utf8)
8635 {
8636 if (c >= 0x80)
8637 {
8638 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008639 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else
8642 ScreenLinesUC[off] = 0;
8643 }
8644#endif
8645 ScreenAttrs[off] = attr;
8646 if (!did_delete || c != ' ')
8647 screen_char(off, row, col);
8648 }
8649 ++off;
8650 if (col == start_col)
8651 {
8652 if (did_delete)
8653 break;
8654 c = c2;
8655 }
8656 }
8657 if (end_col == Columns)
8658 LineWraps[row] = FALSE;
8659 if (row == Rows - 1) /* overwritten the command line */
8660 {
8661 redraw_cmdline = TRUE;
8662 if (c1 == ' ' && c2 == ' ')
8663 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008664 if (start_col == 0)
8665 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667 }
8668}
8669
8670/*
8671 * Check if there should be a delay. Used before clearing or redrawing the
8672 * screen or the command line.
8673 */
8674 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008675check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8678 && !did_wait_return
8679 && emsg_silent == 0)
8680 {
8681 out_flush();
8682 ui_delay(1000L, TRUE);
8683 emsg_on_display = FALSE;
8684 if (check_msg_scroll)
8685 msg_scroll = FALSE;
8686 }
8687}
8688
8689/*
8690 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008691 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 * Returns TRUE if there is a valid screen to write to.
8693 * Returns FALSE when starting up and screen not initialized yet.
8694 */
8695 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008696screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008698 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 return (ScreenLines != NULL);
8700}
8701
8702/*
8703 * Resize the shell to Rows and Columns.
8704 * Allocate ScreenLines[] and associated items.
8705 *
8706 * There may be some time between setting Rows and Columns and (re)allocating
8707 * ScreenLines[]. This happens when starting up and when (manually) changing
8708 * the shell size. Always use screen_Rows and screen_Columns to access items
8709 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8710 * final size of the shell is needed.
8711 */
8712 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008713screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 int new_row, old_row;
8716#ifdef FEAT_GUI
8717 int old_Rows;
8718#endif
8719 win_T *wp;
8720 int outofmem = FALSE;
8721 int len;
8722 schar_T *new_ScreenLines;
8723#ifdef FEAT_MBYTE
8724 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008725 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008727 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#endif
8729 sattr_T *new_ScreenAttrs;
8730 unsigned *new_LineOffset;
8731 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008732 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008733 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008735 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008736#ifdef FEAT_AUTOCMD
8737 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008739retry:
8740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 /*
8742 * Allocation of the screen buffers is done only when the size changes and
8743 * when Rows and Columns have been set and we have started doing full
8744 * screen stuff.
8745 */
8746 if ((ScreenLines != NULL
8747 && Rows == screen_Rows
8748 && Columns == screen_Columns
8749#ifdef FEAT_MBYTE
8750 && enc_utf8 == (ScreenLinesUC != NULL)
8751 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008752 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753#endif
8754 )
8755 || Rows == 0
8756 || Columns == 0
8757 || (!full_screen && ScreenLines == NULL))
8758 return;
8759
8760 /*
8761 * It's possible that we produce an out-of-memory message below, which
8762 * will cause this function to be called again. To break the loop, just
8763 * return here.
8764 */
8765 if (entered)
8766 return;
8767 entered = TRUE;
8768
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008769 /*
8770 * Note that the window sizes are updated before reallocating the arrays,
8771 * thus we must not redraw here!
8772 */
8773 ++RedrawingDisabled;
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 win_new_shellsize(); /* fit the windows in the new sized shell */
8776
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 comp_col(); /* recompute columns for shown command and ruler */
8778
8779 /*
8780 * We're changing the size of the screen.
8781 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8782 * - Move lines from the old arrays into the new arrays, clear extra
8783 * lines (unless the screen is going to be cleared).
8784 * - Free the old arrays.
8785 *
8786 * If anything fails, make ScreenLines NULL, so we don't do anything!
8787 * Continuing with the old ScreenLines may result in a crash, because the
8788 * size is wrong.
8789 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008790 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008792#ifdef FEAT_AUTOCMD
8793 if (aucmd_win != NULL)
8794 win_free_lsize(aucmd_win);
8795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797 new_ScreenLines = (schar_T *)lalloc((long_u)(
8798 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8799#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008800 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 if (enc_utf8)
8802 {
8803 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8804 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008805 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008806 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8808 }
8809 if (enc_dbcs == DBCS_JPNU)
8810 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8811 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8812#endif
8813 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8814 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8815 new_LineOffset = (unsigned *)lalloc((long_u)(
8816 Rows * sizeof(unsigned)), FALSE);
8817 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008818 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008820 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
8822 if (win_alloc_lines(wp) == FAIL)
8823 {
8824 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008825 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
8827 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008828#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008829 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8830 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008831 outofmem = TRUE;
8832#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008833give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835#ifdef FEAT_MBYTE
8836 for (i = 0; i < p_mco; ++i)
8837 if (new_ScreenLinesC[i] == NULL)
8838 break;
8839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (new_ScreenLines == NULL
8841#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008842 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8844#endif
8845 || new_ScreenAttrs == NULL
8846 || new_LineOffset == NULL
8847 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008848 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 || outofmem)
8850 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008851 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008852 {
8853 /* guess the size */
8854 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8855
8856 /* Remember we did this to avoid getting outofmem messages over
8857 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008858 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008859 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008860 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008862 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008864 VIM_CLEAR(new_ScreenLinesC[i]);
8865 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008867 VIM_CLEAR(new_ScreenAttrs);
8868 VIM_CLEAR(new_LineOffset);
8869 VIM_CLEAR(new_LineWraps);
8870 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 else
8873 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008874 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 for (new_row = 0; new_row < Rows; ++new_row)
8877 {
8878 new_LineOffset[new_row] = new_row * Columns;
8879 new_LineWraps[new_row] = FALSE;
8880
8881 /*
8882 * If the screen is not going to be cleared, copy as much as
8883 * possible from the old screen to the new one and clear the rest
8884 * (used when resizing the window at the "--more--" prompt or when
8885 * executing an external command, for the GUI).
8886 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008887 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 {
8889 (void)vim_memset(new_ScreenLines + new_row * Columns,
8890 ' ', (size_t)Columns * sizeof(schar_T));
8891#ifdef FEAT_MBYTE
8892 if (enc_utf8)
8893 {
8894 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8895 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008896 for (i = 0; i < p_mco; ++i)
8897 (void)vim_memset(new_ScreenLinesC[i]
8898 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 0, (size_t)Columns * sizeof(u8char_T));
8900 }
8901 if (enc_dbcs == DBCS_JPNU)
8902 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8903 0, (size_t)Columns * sizeof(schar_T));
8904#endif
8905 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8906 0, (size_t)Columns * sizeof(sattr_T));
8907 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008908 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 {
8910 if (screen_Columns < Columns)
8911 len = screen_Columns;
8912 else
8913 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008914#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008915 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008916 * may be invalid now. Also when p_mco changes. */
8917 if (!(enc_utf8 && ScreenLinesUC == NULL)
8918 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008919#endif
8920 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8921 ScreenLines + LineOffset[old_row],
8922 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 if (enc_utf8 && ScreenLinesUC != NULL
8925 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
8927 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8928 ScreenLinesUC + LineOffset[old_row],
8929 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 for (i = 0; i < p_mco; ++i)
8931 mch_memmove(new_ScreenLinesC[i]
8932 + new_LineOffset[new_row],
8933 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 (size_t)len * sizeof(u8char_T));
8935 }
8936 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8937 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8938 ScreenLines2 + LineOffset[old_row],
8939 (size_t)len * sizeof(schar_T));
8940#endif
8941 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8942 ScreenAttrs + LineOffset[old_row],
8943 (size_t)len * sizeof(sattr_T));
8944 }
8945 }
8946 }
8947 /* Use the last line of the screen for the current line. */
8948 current_ScreenLine = new_ScreenLines + Rows * Columns;
8949 }
8950
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008951 free_screenlines();
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 ScreenLines = new_ScreenLines;
8954#ifdef FEAT_MBYTE
8955 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 for (i = 0; i < p_mco; ++i)
8957 ScreenLinesC[i] = new_ScreenLinesC[i];
8958 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 ScreenLines2 = new_ScreenLines2;
8960#endif
8961 ScreenAttrs = new_ScreenAttrs;
8962 LineOffset = new_LineOffset;
8963 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008964 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 /* It's important that screen_Rows and screen_Columns reflect the actual
8967 * size of ScreenLines[]. Set them before calling anything. */
8968#ifdef FEAT_GUI
8969 old_Rows = screen_Rows;
8970#endif
8971 screen_Rows = Rows;
8972 screen_Columns = Columns;
8973
8974 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008975 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 screenclear2();
8977
8978#ifdef FEAT_GUI
8979 else if (gui.in_use
8980 && !gui.starting
8981 && ScreenLines != NULL
8982 && old_Rows != Rows)
8983 {
8984 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8985 /*
8986 * Adjust the position of the cursor, for when executing an external
8987 * command.
8988 */
8989 if (msg_row >= Rows) /* Rows got smaller */
8990 msg_row = Rows - 1; /* put cursor at last row */
8991 else if (Rows > old_Rows) /* Rows got bigger */
8992 msg_row += Rows - old_Rows; /* put cursor in same place */
8993 if (msg_col >= Columns) /* Columns got smaller */
8994 msg_col = Columns - 1; /* put cursor at last column */
8995 }
8996#endif
8997
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008999 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009000
9001#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009002 /*
9003 * Do not apply autocommands more than 3 times to avoid an endless loop
9004 * in case applying autocommands always changes Rows or Columns.
9005 */
9006 if (starting == 0 && ++retry_count <= 3)
9007 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009008 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009009 /* In rare cases, autocommands may have altered Rows or Columns,
9010 * jump back to check if we need to allocate the screen again. */
9011 goto retry;
9012 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
9016 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009017free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009018{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009019#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009020 int i;
9021
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009022 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009023 for (i = 0; i < Screen_mco; ++i)
9024 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009025 vim_free(ScreenLines2);
9026#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009027 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009028 vim_free(ScreenAttrs);
9029 vim_free(LineOffset);
9030 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009031 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009032}
9033
9034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009035screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 check_for_delay(FALSE);
9038 screenalloc(FALSE); /* allocate screen buffers if size changed */
9039 screenclear2(); /* clear the screen */
9040}
9041
9042 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009043screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 int i;
9046
9047 if (starting == NO_SCREEN || ScreenLines == NULL
9048#ifdef FEAT_GUI
9049 || (gui.in_use && gui.starting)
9050#endif
9051 )
9052 return;
9053
9054#ifdef FEAT_GUI
9055 if (!gui.in_use)
9056#endif
9057 screen_attr = -1; /* force setting the Normal colors */
9058 screen_stop_highlight(); /* don't want highlighting here */
9059
9060#ifdef FEAT_CLIPBOARD
9061 /* disable selection without redrawing it */
9062 clip_scroll_selection(9999);
9063#endif
9064
9065 /* blank out ScreenLines */
9066 for (i = 0; i < Rows; ++i)
9067 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009068 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 LineWraps[i] = FALSE;
9070 }
9071
9072 if (can_clear(T_CL))
9073 {
9074 out_str(T_CL); /* clear the display */
9075 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009076 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
9078 else
9079 {
9080 /* can't clear the screen, mark all chars with invalid attributes */
9081 for (i = 0; i < Rows; ++i)
9082 lineinvalid(LineOffset[i], (int)Columns);
9083 clear_cmdline = TRUE;
9084 }
9085
9086 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9087
9088 win_rest_invalid(firstwin);
9089 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009090 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (must_redraw == CLEAR) /* no need to clear again */
9092 must_redraw = NOT_VALID;
9093 compute_cmdrow();
9094 msg_row = cmdline_row; /* put cursor on last line for messages */
9095 msg_col = 0;
9096 screen_start(); /* don't know where cursor is now */
9097 msg_scrolled = 0; /* can't scroll back */
9098 msg_didany = FALSE;
9099 msg_didout = FALSE;
9100}
9101
9102/*
9103 * Clear one line in ScreenLines.
9104 */
9105 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009106lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9109#ifdef FEAT_MBYTE
9110 if (enc_utf8)
9111 (void)vim_memset(ScreenLinesUC + off, 0,
9112 (size_t)width * sizeof(u8char_T));
9113#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009114 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115}
9116
9117/*
9118 * Mark one line in ScreenLines invalid by setting the attributes to an
9119 * invalid value.
9120 */
9121 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009122lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9125}
9126
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127/*
9128 * Copy part of a Screenline for vertically split window "wp".
9129 */
9130 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009131linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 unsigned off_to = LineOffset[to] + wp->w_wincol;
9134 unsigned off_from = LineOffset[from] + wp->w_wincol;
9135
9136 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9137 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009138#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 if (enc_utf8)
9140 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009141 int i;
9142
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9144 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009145 for (i = 0; i < p_mco; ++i)
9146 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9147 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 }
9149 if (enc_dbcs == DBCS_JPNU)
9150 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9151 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9154 wp->w_width * sizeof(sattr_T));
9155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
9157/*
9158 * Return TRUE if clearing with term string "p" would work.
9159 * It can't work when the string is empty or it won't set the right background.
9160 */
9161 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009162can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 return (*p != NUL && (t_colors <= 1
9165#ifdef FEAT_GUI
9166 || gui.in_use
9167#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009168#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009169 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009170 || (!p_tgc && cterm_normal_bg_color == 0)
9171#else
9172 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009173#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009174 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175}
9176
9177/*
9178 * Reset cursor position. Use whenever cursor was moved because of outputting
9179 * something directly to the screen (shell commands) or a terminal control
9180 * code.
9181 */
9182 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009183screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 screen_cur_row = screen_cur_col = 9999;
9186}
9187
9188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 * Move the cursor to position "row","col" in the screen.
9190 * This tries to find the most efficient way to move, minimizing the number of
9191 * characters sent to the terminal.
9192 */
9193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009194windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009196 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 int i;
9198 int plan;
9199 int cost;
9200 int wouldbe_col;
9201 int noinvcurs;
9202 char_u *bs;
9203 int goto_cost;
9204 int attr;
9205
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009206#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9208
9209#define PLAN_LE 1
9210#define PLAN_CR 2
9211#define PLAN_NL 3
9212#define PLAN_WRITE 4
9213 /* Can't use ScreenLines unless initialized */
9214 if (ScreenLines == NULL)
9215 return;
9216
9217 if (col != screen_cur_col || row != screen_cur_row)
9218 {
9219 /* Check for valid position. */
9220 if (row < 0) /* window without text lines? */
9221 row = 0;
9222 if (row >= screen_Rows)
9223 row = screen_Rows - 1;
9224 if (col >= screen_Columns)
9225 col = screen_Columns - 1;
9226
9227 /* check if no cursor movement is allowed in highlight mode */
9228 if (screen_attr && *T_MS == NUL)
9229 noinvcurs = HIGHL_COST;
9230 else
9231 noinvcurs = 0;
9232 goto_cost = GOTO_COST + noinvcurs;
9233
9234 /*
9235 * Plan how to do the positioning:
9236 * 1. Use CR to move it to column 0, same row.
9237 * 2. Use T_LE to move it a few columns to the left.
9238 * 3. Use NL to move a few lines down, column 0.
9239 * 4. Move a few columns to the right with T_ND or by writing chars.
9240 *
9241 * Don't do this if the cursor went beyond the last column, the cursor
9242 * position is unknown then (some terminals wrap, some don't )
9243 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009244 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 * characters to move the cursor to the right.
9246 */
9247 if (row >= screen_cur_row && screen_cur_col < Columns)
9248 {
9249 /*
9250 * If the cursor is in the same row, bigger col, we can use CR
9251 * or T_LE.
9252 */
9253 bs = NULL; /* init for GCC */
9254 attr = screen_attr;
9255 if (row == screen_cur_row && col < screen_cur_col)
9256 {
9257 /* "le" is preferred over "bc", because "bc" is obsolete */
9258 if (*T_LE)
9259 bs = T_LE; /* "cursor left" */
9260 else
9261 bs = T_BC; /* "backspace character (old) */
9262 if (*bs)
9263 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9264 else
9265 cost = 999;
9266 if (col + 1 < cost) /* using CR is less characters */
9267 {
9268 plan = PLAN_CR;
9269 wouldbe_col = 0;
9270 cost = 1; /* CR is just one character */
9271 }
9272 else
9273 {
9274 plan = PLAN_LE;
9275 wouldbe_col = col;
9276 }
9277 if (noinvcurs) /* will stop highlighting */
9278 {
9279 cost += noinvcurs;
9280 attr = 0;
9281 }
9282 }
9283
9284 /*
9285 * If the cursor is above where we want to be, we can use CR LF.
9286 */
9287 else if (row > screen_cur_row)
9288 {
9289 plan = PLAN_NL;
9290 wouldbe_col = 0;
9291 cost = (row - screen_cur_row) * 2; /* CR LF */
9292 if (noinvcurs) /* will stop highlighting */
9293 {
9294 cost += noinvcurs;
9295 attr = 0;
9296 }
9297 }
9298
9299 /*
9300 * If the cursor is in the same row, smaller col, just use write.
9301 */
9302 else
9303 {
9304 plan = PLAN_WRITE;
9305 wouldbe_col = screen_cur_col;
9306 cost = 0;
9307 }
9308
9309 /*
9310 * Check if any characters that need to be written have the
9311 * correct attributes. Also avoid UTF-8 characters.
9312 */
9313 i = col - wouldbe_col;
9314 if (i > 0)
9315 cost += i;
9316 if (cost < goto_cost && i > 0)
9317 {
9318 /*
9319 * Check if the attributes are correct without additionally
9320 * stopping highlighting.
9321 */
9322 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9323 while (i && *p++ == attr)
9324 --i;
9325 if (i != 0)
9326 {
9327 /*
9328 * Try if it works when highlighting is stopped here.
9329 */
9330 if (*--p == 0)
9331 {
9332 cost += noinvcurs;
9333 while (i && *p++ == 0)
9334 --i;
9335 }
9336 if (i != 0)
9337 cost = 999; /* different attributes, don't do it */
9338 }
9339#ifdef FEAT_MBYTE
9340 if (enc_utf8)
9341 {
9342 /* Don't use an UTF-8 char for positioning, it's slow. */
9343 for (i = wouldbe_col; i < col; ++i)
9344 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9345 {
9346 cost = 999;
9347 break;
9348 }
9349 }
9350#endif
9351 }
9352
9353 /*
9354 * We can do it without term_windgoto()!
9355 */
9356 if (cost < goto_cost)
9357 {
9358 if (plan == PLAN_LE)
9359 {
9360 if (noinvcurs)
9361 screen_stop_highlight();
9362 while (screen_cur_col > col)
9363 {
9364 out_str(bs);
9365 --screen_cur_col;
9366 }
9367 }
9368 else if (plan == PLAN_CR)
9369 {
9370 if (noinvcurs)
9371 screen_stop_highlight();
9372 out_char('\r');
9373 screen_cur_col = 0;
9374 }
9375 else if (plan == PLAN_NL)
9376 {
9377 if (noinvcurs)
9378 screen_stop_highlight();
9379 while (screen_cur_row < row)
9380 {
9381 out_char('\n');
9382 ++screen_cur_row;
9383 }
9384 screen_cur_col = 0;
9385 }
9386
9387 i = col - screen_cur_col;
9388 if (i > 0)
9389 {
9390 /*
9391 * Use cursor-right if it's one character only. Avoids
9392 * removing a line of pixels from the last bold char, when
9393 * using the bold trick in the GUI.
9394 */
9395 if (T_ND[0] != NUL && T_ND[1] == NUL)
9396 {
9397 while (i-- > 0)
9398 out_char(*T_ND);
9399 }
9400 else
9401 {
9402 int off;
9403
9404 off = LineOffset[row] + screen_cur_col;
9405 while (i-- > 0)
9406 {
9407 if (ScreenAttrs[off] != screen_attr)
9408 screen_stop_highlight();
9409#ifdef FEAT_MBYTE
9410 out_flush_check();
9411#endif
9412 out_char(ScreenLines[off]);
9413#ifdef FEAT_MBYTE
9414 if (enc_dbcs == DBCS_JPNU
9415 && ScreenLines[off] == 0x8e)
9416 out_char(ScreenLines2[off]);
9417#endif
9418 ++off;
9419 }
9420 }
9421 }
9422 }
9423 }
9424 else
9425 cost = 999;
9426
9427 if (cost >= goto_cost)
9428 {
9429 if (noinvcurs)
9430 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009431 if (row == screen_cur_row && (col > screen_cur_col)
9432 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 term_cursor_right(col - screen_cur_col);
9434 else
9435 term_windgoto(row, col);
9436 }
9437 screen_cur_row = row;
9438 screen_cur_col = col;
9439 }
9440}
9441
9442/*
9443 * Set cursor to its position in the current window.
9444 */
9445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009446setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 if (redrawing())
9449 {
9450 validate_cursor();
9451 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009452 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009454 /* With 'rightleft' set and the cursor on a double-wide
9455 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009456 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009458 (has_mbyte
9459 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9460 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461# endif
9462 1)) :
9463#endif
9464 curwin->w_wcol));
9465 }
9466}
9467
9468
9469/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009470 * Insert 'line_count' lines at 'row' in window 'wp'.
9471 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9472 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 * scrolling.
9474 * Returns FAIL if the lines are not inserted, OK for success.
9475 */
9476 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009477win_ins_lines(
9478 win_T *wp,
9479 int row,
9480 int line_count,
9481 int invalid,
9482 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484 int did_delete;
9485 int nextrow;
9486 int lastrow;
9487 int retval;
9488
9489 if (invalid)
9490 wp->w_lines_valid = 0;
9491
9492 if (wp->w_height < 5)
9493 return FAIL;
9494
9495 if (line_count > wp->w_height - row)
9496 line_count = wp->w_height - row;
9497
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009498 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 if (retval != MAYBE)
9500 return retval;
9501
9502 /*
9503 * If there is a next window or a status line, we first try to delete the
9504 * lines at the bottom to avoid messing what is after the window.
9505 * If this fails and there are following windows, don't do anything to avoid
9506 * messing up those windows, better just redraw.
9507 */
9508 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (wp->w_next != NULL || wp->w_status_height)
9510 {
9511 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009512 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 did_delete = TRUE;
9514 else if (wp->w_next)
9515 return FAIL;
9516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 /*
9518 * if no lines deleted, blank the lines that will end up below the window
9519 */
9520 if (!did_delete)
9521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009524 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 lastrow = nextrow + line_count;
9526 if (lastrow > Rows)
9527 lastrow = Rows;
9528 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009529 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 ' ', ' ', 0);
9531 }
9532
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009533 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 == FAIL)
9535 {
9536 /* deletion will have messed up other windows */
9537 if (did_delete)
9538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 win_rest_invalid(W_NEXT(wp));
9541 }
9542 return FAIL;
9543 }
9544
9545 return OK;
9546}
9547
9548/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009549 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9551 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9552 * scrolling
9553 * Return OK for success, FAIL if the lines are not deleted.
9554 */
9555 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009556win_del_lines(
9557 win_T *wp,
9558 int row,
9559 int line_count,
9560 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009561 int mayclear,
9562 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 int retval;
9565
9566 if (invalid)
9567 wp->w_lines_valid = 0;
9568
9569 if (line_count > wp->w_height - row)
9570 line_count = wp->w_height - row;
9571
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009572 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 if (retval != MAYBE)
9574 return retval;
9575
9576 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009577 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 return FAIL;
9579
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 /*
9581 * If there are windows or status lines below, try to put them at the
9582 * correct place. If we can't do that, they have to be redrawn.
9583 */
9584 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9585 {
9586 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009587 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
9589 wp->w_redr_status = TRUE;
9590 win_rest_invalid(wp->w_next);
9591 }
9592 }
9593 /*
9594 * If this is the last window and there is no status line, redraw the
9595 * command line later.
9596 */
9597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 redraw_cmdline = TRUE;
9599 return OK;
9600}
9601
9602/*
9603 * Common code for win_ins_lines() and win_del_lines().
9604 * Returns OK or FAIL when the work has been done.
9605 * Returns MAYBE when not finished yet.
9606 */
9607 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009608win_do_lines(
9609 win_T *wp,
9610 int row,
9611 int line_count,
9612 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009613 int del,
9614 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 int retval;
9617
9618 if (!redrawing() || line_count <= 0)
9619 return FAIL;
9620
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009621 /* When inserting lines would result in loss of command output, just redraw
9622 * the lines. */
9623 if (no_win_do_lines_ins && !del)
9624 return FAIL;
9625
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009627 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009629 if (!no_win_do_lines_ins)
9630 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 return FAIL;
9632 }
9633
9634 /*
9635 * Delete all remaining lines
9636 */
9637 if (row + line_count >= wp->w_height)
9638 {
9639 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009640 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 ' ', ' ', 0);
9642 return OK;
9643 }
9644
9645 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009646 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009648 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009650 if (!no_win_do_lines_ins)
9651 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652
9653 /*
9654 * If the terminal can set a scroll region, use that.
9655 * Always do this in a vertically split window. This will redraw from
9656 * ScreenLines[] when t_CV isn't defined. That's faster than using
9657 * win_line().
9658 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009659 * a character in the lower right corner of the scroll region may cause a
9660 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009662 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 scroll_region_set(wp, row);
9666 if (del)
9667 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009668 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 else
9670 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009671 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 scroll_region_reset();
9674 return retval;
9675 }
9676
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679
9680 return MAYBE;
9681}
9682
9683/*
9684 * window 'wp' and everything after it is messed up, mark it for redraw
9685 */
9686 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009687win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 {
9691 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 wp->w_redr_status = TRUE;
9693 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 }
9695 redraw_cmdline = TRUE;
9696}
9697
9698/*
9699 * The rest of the routines in this file perform screen manipulations. The
9700 * given operation is performed physically on the screen. The corresponding
9701 * change is also made to the internal screen image. In this way, the editor
9702 * anticipates the effect of editing changes on the appearance of the screen.
9703 * That way, when we call screenupdate a complete redraw isn't usually
9704 * necessary. Another advantage is that we can keep adding code to anticipate
9705 * screen changes, and in the meantime, everything still works.
9706 */
9707
9708/*
9709 * types for inserting or deleting lines
9710 */
9711#define USE_T_CAL 1
9712#define USE_T_CDL 2
9713#define USE_T_AL 3
9714#define USE_T_CE 4
9715#define USE_T_DL 5
9716#define USE_T_SR 6
9717#define USE_NL 7
9718#define USE_T_CD 8
9719#define USE_REDRAW 9
9720
9721/*
9722 * insert lines on the screen and update ScreenLines[]
9723 * 'end' is the line after the scrolled part. Normally it is Rows.
9724 * When scrolling region used 'off' is the offset from the top for the region.
9725 * 'row' and 'end' are relative to the start of the region.
9726 *
9727 * return FAIL for failure, OK for success.
9728 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009729 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009730screen_ins_lines(
9731 int off,
9732 int row,
9733 int line_count,
9734 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009735 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009736 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738 int i;
9739 int j;
9740 unsigned temp;
9741 int cursor_row;
9742 int type;
9743 int result_empty;
9744 int can_ce = can_clear(T_CE);
9745
9746 /*
9747 * FAIL if
9748 * - there is no valid screen
9749 * - the screen has to be redrawn completely
9750 * - the line count is less than one
9751 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009752 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009754 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9755#ifdef FEAT_CLIPBOARD
9756 || (clip_star.state != SELECT_CLEARED
9757 && redrawing_for_callback > 0)
9758#endif
9759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 return FAIL;
9761
9762 /*
9763 * There are seven ways to insert lines:
9764 * 0. When in a vertically split window and t_CV isn't set, redraw the
9765 * characters from ScreenLines[].
9766 * 1. Use T_CD (clear to end of display) if it exists and the result of
9767 * the insert is just empty lines
9768 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9769 * present or line_count > 1. It looks better if we do all the inserts
9770 * at once.
9771 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9772 * insert is just empty lines and T_CE is not present or line_count >
9773 * 1.
9774 * 4. Use T_AL (insert line) if it exists.
9775 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9776 * just empty lines.
9777 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9778 * just empty lines.
9779 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9780 * the 'da' flag is not set or we have clear line capability.
9781 * 8. redraw the characters from ScreenLines[].
9782 *
9783 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9784 * the scrollbar for the window. It does have insert line, use that if it
9785 * exists.
9786 */
9787 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9789 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009790 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 type = USE_T_CD;
9792 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9793 type = USE_T_CAL;
9794 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9795 type = USE_T_CDL;
9796 else if (*T_AL != NUL)
9797 type = USE_T_AL;
9798 else if (can_ce && result_empty)
9799 type = USE_T_CE;
9800 else if (*T_DL != NUL && result_empty)
9801 type = USE_T_DL;
9802 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9803 type = USE_T_SR;
9804 else
9805 return FAIL;
9806
9807 /*
9808 * For clearing the lines screen_del_lines() is used. This will also take
9809 * care of t_db if necessary.
9810 */
9811 if (type == USE_T_CD || type == USE_T_CDL ||
9812 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009813 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814
9815 /*
9816 * If text is retained below the screen, first clear or delete as many
9817 * lines at the bottom of the window as are about to be inserted so that
9818 * the deleted lines won't later surface during a screen_del_lines.
9819 */
9820 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009821 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
9823#ifdef FEAT_CLIPBOARD
9824 /* Remove a modeless selection when inserting lines halfway the screen
9825 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009826 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009827 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else
9829 clip_scroll_selection(-line_count);
9830#endif
9831
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#ifdef FEAT_GUI
9833 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9834 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009835 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836#endif
9837
9838 if (*T_CCS != NUL) /* cursor relative to region */
9839 cursor_row = row;
9840 else
9841 cursor_row = row + off;
9842
9843 /*
9844 * Shift LineOffset[] line_count down to reflect the inserted lines.
9845 * Clear the inserted lines in ScreenLines[].
9846 */
9847 row += off;
9848 end += off;
9849 for (i = 0; i < line_count; ++i)
9850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 if (wp != NULL && wp->w_width != Columns)
9852 {
9853 /* need to copy part of a line */
9854 j = end - 1 - i;
9855 while ((j -= line_count) >= row)
9856 linecopy(j + line_count, j, wp);
9857 j += line_count;
9858 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009859 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9860 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 else
9862 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9863 LineWraps[j] = FALSE;
9864 }
9865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
9867 j = end - 1 - i;
9868 temp = LineOffset[j];
9869 while ((j -= line_count) >= row)
9870 {
9871 LineOffset[j + line_count] = LineOffset[j];
9872 LineWraps[j + line_count] = LineWraps[j];
9873 }
9874 LineOffset[j + line_count] = temp;
9875 LineWraps[j + line_count] = FALSE;
9876 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009877 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 else
9879 lineinvalid(temp, (int)Columns);
9880 }
9881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
9883 screen_stop_highlight();
9884 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009885 if (clear_attr != 0)
9886 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 /* redraw the characters */
9889 if (type == USE_REDRAW)
9890 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009891 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 {
9893 term_append_lines(line_count);
9894 screen_start(); /* don't know where cursor is now */
9895 }
9896 else
9897 {
9898 for (i = 0; i < line_count; i++)
9899 {
9900 if (type == USE_T_AL)
9901 {
9902 if (i && cursor_row != 0)
9903 windgoto(cursor_row, 0);
9904 out_str(T_AL);
9905 }
9906 else /* type == USE_T_SR */
9907 out_str(T_SR);
9908 screen_start(); /* don't know where cursor is now */
9909 }
9910 }
9911
9912 /*
9913 * With scroll-reverse and 'da' flag set we need to clear the lines that
9914 * have been scrolled down into the region.
9915 */
9916 if (type == USE_T_SR && *T_DA)
9917 {
9918 for (i = 0; i < line_count; ++i)
9919 {
9920 windgoto(off + i, 0);
9921 out_str(T_CE);
9922 screen_start(); /* don't know where cursor is now */
9923 }
9924 }
9925
9926#ifdef FEAT_GUI
9927 gui_can_update_cursor();
9928 if (gui.in_use)
9929 out_flush(); /* always flush after a scroll */
9930#endif
9931 return OK;
9932}
9933
9934/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009935 * Delete lines on the screen and update ScreenLines[].
9936 * "end" is the line after the scrolled part. Normally it is Rows.
9937 * When scrolling region used "off" is the offset from the top for the region.
9938 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 *
9940 * Return OK for success, FAIL if the lines are not deleted.
9941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009943screen_del_lines(
9944 int off,
9945 int row,
9946 int line_count,
9947 int end,
9948 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009949 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009950 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952 int j;
9953 int i;
9954 unsigned temp;
9955 int cursor_row;
9956 int cursor_end;
9957 int result_empty; /* result is empty until end of region */
9958 int can_delete; /* deleting line codes can be used */
9959 int type;
9960
9961 /*
9962 * FAIL if
9963 * - there is no valid screen
9964 * - the screen has to be redrawn completely
9965 * - the line count is less than one
9966 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009967 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009969 if (!screen_valid(TRUE) || line_count <= 0
9970 || (!force && line_count > p_ttyscroll)
9971#ifdef FEAT_CLIPBOARD
9972 || (clip_star.state != SELECT_CLEARED
9973 && redrawing_for_callback > 0)
9974#endif
9975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 return FAIL;
9977
9978 /*
9979 * Check if the rest of the current region will become empty.
9980 */
9981 result_empty = row + line_count >= end;
9982
9983 /*
9984 * We can delete lines only when 'db' flag not set or when 'ce' option
9985 * available.
9986 */
9987 can_delete = (*T_DB == NUL || can_clear(T_CE));
9988
9989 /*
9990 * There are six ways to delete lines:
9991 * 0. When in a vertically split window and t_CV isn't set, redraw the
9992 * characters from ScreenLines[].
9993 * 1. Use T_CD if it exists and the result is empty.
9994 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9995 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9996 * none of the other ways work.
9997 * 4. Use T_CE (erase line) if the result is empty.
9998 * 5. Use T_DL (delete line) if it exists.
9999 * 6. redraw the characters from ScreenLines[].
10000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10002 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010003 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 type = USE_T_CD;
10005#if defined(__BEOS__) && defined(BEOS_DR8)
10006 /*
10007 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10008 * its internal termcap... this works okay for tests which test *T_DB !=
10009 * NUL. It has the disadvantage that the user cannot use any :set t_*
10010 * command to get T_DB (back) to empty_option, only :set term=... will do
10011 * the trick...
10012 * Anyway, this hack will hopefully go away with the next OS release.
10013 * (Olaf Seibert)
10014 */
10015 else if (row == 0 && T_DB == empty_option
10016 && (line_count == 1 || *T_CDL == NUL))
10017#else
10018 else if (row == 0 && (
10019#ifndef AMIGA
10020 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10021 * up, so use delete-line command */
10022 line_count == 1 ||
10023#endif
10024 *T_CDL == NUL))
10025#endif
10026 type = USE_NL;
10027 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10028 type = USE_T_CDL;
10029 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010030 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 type = USE_T_CE;
10032 else if (*T_DL != NUL && can_delete)
10033 type = USE_T_DL;
10034 else if (*T_CDL != NUL && can_delete)
10035 type = USE_T_CDL;
10036 else
10037 return FAIL;
10038
10039#ifdef FEAT_CLIPBOARD
10040 /* Remove a modeless selection when deleting lines halfway the screen or
10041 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010042 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010043 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else
10045 clip_scroll_selection(line_count);
10046#endif
10047
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#ifdef FEAT_GUI
10049 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10050 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010051 gui_dont_update_cursor(gui.cursor_row >= row + off
10052 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053#endif
10054
10055 if (*T_CCS != NUL) /* cursor relative to region */
10056 {
10057 cursor_row = row;
10058 cursor_end = end;
10059 }
10060 else
10061 {
10062 cursor_row = row + off;
10063 cursor_end = end + off;
10064 }
10065
10066 /*
10067 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10068 * Clear the inserted lines in ScreenLines[].
10069 */
10070 row += off;
10071 end += off;
10072 for (i = 0; i < line_count; ++i)
10073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 if (wp != NULL && wp->w_width != Columns)
10075 {
10076 /* need to copy part of a line */
10077 j = row + i;
10078 while ((j += line_count) <= end - 1)
10079 linecopy(j - line_count, j, wp);
10080 j -= line_count;
10081 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010082 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10083 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 else
10085 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10086 LineWraps[j] = FALSE;
10087 }
10088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
10090 /* whole width, moving the line pointers is faster */
10091 j = row + i;
10092 temp = LineOffset[j];
10093 while ((j += line_count) <= end - 1)
10094 {
10095 LineOffset[j - line_count] = LineOffset[j];
10096 LineWraps[j - line_count] = LineWraps[j];
10097 }
10098 LineOffset[j - line_count] = temp;
10099 LineWraps[j - line_count] = FALSE;
10100 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010101 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 else
10103 lineinvalid(temp, (int)Columns);
10104 }
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010107 if (screen_attr != clear_attr)
10108 screen_stop_highlight();
10109 if (clear_attr != 0)
10110 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 /* redraw the characters */
10113 if (type == USE_REDRAW)
10114 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010115 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 {
10117 windgoto(cursor_row, 0);
10118 out_str(T_CD);
10119 screen_start(); /* don't know where cursor is now */
10120 }
10121 else if (type == USE_T_CDL)
10122 {
10123 windgoto(cursor_row, 0);
10124 term_delete_lines(line_count);
10125 screen_start(); /* don't know where cursor is now */
10126 }
10127 /*
10128 * Deleting lines at top of the screen or scroll region: Just scroll
10129 * the whole screen (scroll region) up by outputting newlines on the
10130 * last line.
10131 */
10132 else if (type == USE_NL)
10133 {
10134 windgoto(cursor_end - 1, 0);
10135 for (i = line_count; --i >= 0; )
10136 out_char('\n'); /* cursor will remain on same line */
10137 }
10138 else
10139 {
10140 for (i = line_count; --i >= 0; )
10141 {
10142 if (type == USE_T_DL)
10143 {
10144 windgoto(cursor_row, 0);
10145 out_str(T_DL); /* delete a line */
10146 }
10147 else /* type == USE_T_CE */
10148 {
10149 windgoto(cursor_row + i, 0);
10150 out_str(T_CE); /* erase a line */
10151 }
10152 screen_start(); /* don't know where cursor is now */
10153 }
10154 }
10155
10156 /*
10157 * If the 'db' flag is set, we need to clear the lines that have been
10158 * scrolled up at the bottom of the region.
10159 */
10160 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10161 {
10162 for (i = line_count; i > 0; --i)
10163 {
10164 windgoto(cursor_end - i, 0);
10165 out_str(T_CE); /* erase a line */
10166 screen_start(); /* don't know where cursor is now */
10167 }
10168 }
10169
10170#ifdef FEAT_GUI
10171 gui_can_update_cursor();
10172 if (gui.in_use)
10173 out_flush(); /* always flush after a scroll */
10174#endif
10175
10176 return OK;
10177}
10178
10179/*
10180 * show the current mode and ruler
10181 *
10182 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10183 * If clear_cmdline is FALSE there may be a message there that needs to be
10184 * cleared only if a mode is shown.
10185 * Return the length of the message (0 if no message).
10186 */
10187 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010188showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 int need_clear;
10191 int length = 0;
10192 int do_mode;
10193 int attr;
10194 int nwr_save;
10195#ifdef FEAT_INS_EXPAND
10196 int sub_attr;
10197#endif
10198
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010199 do_mode = ((p_smd && msg_silent == 0)
10200 && ((State & INSERT)
10201 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010202 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 if (do_mode || Recording)
10204 {
10205 /*
10206 * Don't show mode right now, when not redrawing or inside a mapping.
10207 * Call char_avail() only when we are going to show something, because
10208 * it takes a bit of time.
10209 */
10210 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10211 {
10212 redraw_cmdline = TRUE; /* show mode later */
10213 return 0;
10214 }
10215
10216 nwr_save = need_wait_return;
10217
10218 /* wait a bit before overwriting an important message */
10219 check_for_delay(FALSE);
10220
10221 /* if the cmdline is more than one line high, erase top lines */
10222 need_clear = clear_cmdline;
10223 if (clear_cmdline && cmdline_row < Rows - 1)
10224 msg_clr_cmdline(); /* will reset clear_cmdline */
10225
10226 /* Position on the last line in the window, column 0 */
10227 msg_pos_mode();
10228 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010229 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (do_mode)
10231 {
10232 MSG_PUTS_ATTR("--", attr);
10233#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010234 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010235# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010236 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010237# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010238 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010239# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010240 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010241# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 MSG_PUTS_ATTR(" IM", attr);
10243# else
10244 MSG_PUTS_ATTR(" XIM", attr);
10245# endif
10246#endif
10247#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10248 if (gui.in_use)
10249 {
10250 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010251 {
10252 /* HANGUL */
10253 if (enc_utf8)
10254 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10255 else
10256 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 }
10259#endif
10260#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010261 /* CTRL-X in Insert mode */
10262 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 {
10264 /* These messages can get long, avoid a wrap in a narrow
10265 * window. Prefer showing edit_submode_extra. */
10266 length = (Rows - msg_row) * Columns - 3;
10267 if (edit_submode_extra != NULL)
10268 length -= vim_strsize(edit_submode_extra);
10269 if (length > 0)
10270 {
10271 if (edit_submode_pre != NULL)
10272 length -= vim_strsize(edit_submode_pre);
10273 if (length - vim_strsize(edit_submode) > 0)
10274 {
10275 if (edit_submode_pre != NULL)
10276 msg_puts_attr(edit_submode_pre, attr);
10277 msg_puts_attr(edit_submode, attr);
10278 }
10279 if (edit_submode_extra != NULL)
10280 {
10281 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10282 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010283 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 else
10285 sub_attr = attr;
10286 msg_puts_attr(edit_submode_extra, sub_attr);
10287 }
10288 }
10289 length = 0;
10290 }
10291 else
10292#endif
10293 {
10294#ifdef FEAT_VREPLACE
10295 if (State & VREPLACE_FLAG)
10296 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10297 else
10298#endif
10299 if (State & REPLACE_FLAG)
10300 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10301 else if (State & INSERT)
10302 {
10303#ifdef FEAT_RIGHTLEFT
10304 if (p_ri)
10305 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10306#endif
10307 MSG_PUTS_ATTR(_(" INSERT"), attr);
10308 }
10309 else if (restart_edit == 'I')
10310 MSG_PUTS_ATTR(_(" (insert)"), attr);
10311 else if (restart_edit == 'R')
10312 MSG_PUTS_ATTR(_(" (replace)"), attr);
10313 else if (restart_edit == 'V')
10314 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10315#ifdef FEAT_RIGHTLEFT
10316 if (p_hkmap)
10317 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10318# ifdef FEAT_FKMAP
10319 if (p_fkmap)
10320 MSG_PUTS_ATTR(farsi_text_5, attr);
10321# endif
10322#endif
10323#ifdef FEAT_KEYMAP
10324 if (State & LANGMAP)
10325 {
10326# ifdef FEAT_ARABIC
10327 if (curwin->w_p_arab)
10328 MSG_PUTS_ATTR(_(" Arabic"), attr);
10329 else
10330# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010331 if (get_keymap_str(curwin, (char_u *)" (%s)",
10332 NameBuff, MAXPATHL))
10333 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 }
10335#endif
10336 if ((State & INSERT) && p_paste)
10337 MSG_PUTS_ATTR(_(" (paste)"), attr);
10338
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (VIsual_active)
10340 {
10341 char *p;
10342
10343 /* Don't concatenate separate words to avoid translation
10344 * problems. */
10345 switch ((VIsual_select ? 4 : 0)
10346 + (VIsual_mode == Ctrl_V) * 2
10347 + (VIsual_mode == 'V'))
10348 {
10349 case 0: p = N_(" VISUAL"); break;
10350 case 1: p = N_(" VISUAL LINE"); break;
10351 case 2: p = N_(" VISUAL BLOCK"); break;
10352 case 4: p = N_(" SELECT"); break;
10353 case 5: p = N_(" SELECT LINE"); break;
10354 default: p = N_(" SELECT BLOCK"); break;
10355 }
10356 MSG_PUTS_ATTR(_(p), attr);
10357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 MSG_PUTS_ATTR(" --", attr);
10359 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010360
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 need_clear = TRUE;
10362 }
10363 if (Recording
10364#ifdef FEAT_INS_EXPAND
10365 && edit_submode == NULL /* otherwise it gets too long */
10366#endif
10367 )
10368 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010369 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 need_clear = TRUE;
10371 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010372
10373 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 if (need_clear || clear_cmdline)
10375 msg_clr_eos();
10376 msg_didout = FALSE; /* overwrite this message */
10377 length = msg_col;
10378 msg_col = 0;
10379 need_wait_return = nwr_save; /* never ask for hit-return for this */
10380 }
10381 else if (clear_cmdline && msg_silent == 0)
10382 /* Clear the whole command line. Will reset "clear_cmdline". */
10383 msg_clr_cmdline();
10384
10385#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 /* In Visual mode the size of the selected area must be redrawn. */
10387 if (VIsual_active)
10388 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389
10390 /* If the last window has no status line, the ruler is after the mode
10391 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010392 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 win_redr_ruler(lastwin, TRUE);
10394#endif
10395 redraw_cmdline = FALSE;
10396 clear_cmdline = FALSE;
10397
10398 return length;
10399}
10400
10401/*
10402 * Position for a mode message.
10403 */
10404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010405msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 msg_col = 0;
10408 msg_row = Rows - 1;
10409}
10410
10411/*
10412 * Delete mode message. Used when ESC is typed which is expected to end
10413 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010414 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 */
10416 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010417unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418{
10419 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010420 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 */
10422 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10423 redraw_cmdline = TRUE; /* delete mode later */
10424 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010425 clearmode();
10426}
10427
10428/*
10429 * Clear the mode message.
10430 */
10431 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010432clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010433{
10434 msg_pos_mode();
10435 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010436 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010437 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438}
10439
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010440 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010441recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010442{
10443 MSG_PUTS_ATTR(_("recording"), attr);
10444 if (!shortmess(SHM_RECORDING))
10445 {
10446 char_u s[4];
10447 sprintf((char *)s, " @%c", Recording);
10448 MSG_PUTS_ATTR(s, attr);
10449 }
10450}
10451
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010452/*
10453 * Draw the tab pages line at the top of the Vim window.
10454 */
10455 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010456draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010457{
10458 int tabcount = 0;
10459 tabpage_T *tp;
10460 int tabwidth;
10461 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010462 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010463 int attr;
10464 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010465 win_T *cwp;
10466 int wincount;
10467 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010468 int c;
10469 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010470 int attr_sel = HL_ATTR(HLF_TPS);
10471 int attr_nosel = HL_ATTR(HLF_TP);
10472 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010473 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010474 int room;
10475 int use_sep_chars = (t_colors < 8
10476#ifdef FEAT_GUI
10477 && !gui.in_use
10478#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010479#ifdef FEAT_TERMGUICOLORS
10480 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010481#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010482 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010483
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010484 if (ScreenLines == NULL)
10485 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010486 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010488#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010489 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010490 if (gui_use_tabline())
10491 {
10492 gui_update_tabline();
10493 return;
10494 }
10495#endif
10496
10497 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498 return;
10499
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010500#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010501
10502 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10503 for (scol = 0; scol < Columns; ++scol)
10504 TabPageIdxs[scol] = 0;
10505
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010506 /* Use the 'tabline' option if it's set. */
10507 if (*p_tal != NUL)
10508 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010509 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010510
10511 /* Check for an error. If there is one we would loop in redrawing the
10512 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010513 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010514 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010515 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010516 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010517 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010518 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010519 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010520 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010521#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010522 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010523 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010524 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010525
Bram Moolenaar238a5642006-02-21 22:12:05 +000010526 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10527 if (tabwidth < 6)
10528 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010529
Bram Moolenaar238a5642006-02-21 22:12:05 +000010530 attr = attr_nosel;
10531 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010532 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010533 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10534 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010535 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010536 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010537
Bram Moolenaar238a5642006-02-21 22:12:05 +000010538 if (tp->tp_topframe == topframe)
10539 attr = attr_sel;
10540 if (use_sep_chars && col > 0)
10541 screen_putchar('|', 0, col++, attr);
10542
10543 if (tp->tp_topframe != topframe)
10544 attr = attr_nosel;
10545
10546 screen_putchar(' ', 0, col++, attr);
10547
10548 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010549 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010550 cwp = curwin;
10551 wp = firstwin;
10552 }
10553 else
10554 {
10555 cwp = tp->tp_curwin;
10556 wp = tp->tp_firstwin;
10557 }
10558
10559 modified = FALSE;
10560 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10561 if (bufIsChanged(wp->w_buffer))
10562 modified = TRUE;
10563 if (modified || wincount > 1)
10564 {
10565 if (wincount > 1)
10566 {
10567 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010568 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010569 if (col + len >= Columns - 3)
10570 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010571 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010572#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010573 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010574#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010575 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010576#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010577 );
10578 col += len;
10579 }
10580 if (modified)
10581 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10582 screen_putchar(' ', 0, col++, attr);
10583 }
10584
10585 room = scol - col + tabwidth - 1;
10586 if (room > 0)
10587 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010588 /* Get buffer name in NameBuff[] */
10589 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010590 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591 len = vim_strsize(NameBuff);
10592 p = NameBuff;
10593#ifdef FEAT_MBYTE
10594 if (has_mbyte)
10595 while (len > room)
10596 {
10597 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010598 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010599 }
10600 else
10601#endif
10602 if (len > room)
10603 {
10604 p += len - room;
10605 len = room;
10606 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010607 if (len > Columns - col - 1)
10608 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010609
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010610 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010611 col += len;
10612 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010613 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010614
10615 /* Store the tab page number in TabPageIdxs[], so that
10616 * jump_to_mouse() knows where each one is. */
10617 ++tabcount;
10618 while (scol < col)
10619 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010620 }
10621
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622 if (use_sep_chars)
10623 c = '_';
10624 else
10625 c = ' ';
10626 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010627
10628 /* Put an "X" for closing the current tab if there are several. */
10629 if (first_tabpage->tp_next != NULL)
10630 {
10631 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10632 TabPageIdxs[Columns - 1] = -999;
10633 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010634 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010635
10636 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10637 * set. */
10638 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010639}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010640
10641/*
10642 * Get buffer name for "buf" into NameBuff[].
10643 * Takes care of special buffer names and translates special characters.
10644 */
10645 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010646get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010647{
10648 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010649 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010650 else
10651 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10652 trans_characters(NameBuff, MAXPATHL);
10653}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010654
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655/*
10656 * Get the character to use in a status line. Get its attributes in "*attr".
10657 */
10658 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010659fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
10661 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010662
10663#ifdef FEAT_TERMINAL
10664 if (bt_terminal(wp->w_buffer))
10665 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010666 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010667 {
10668 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010669 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010670 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010671 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010672 {
10673 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010674 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010675 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010676 }
10677 else
10678#endif
10679 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010681 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 fill = fill_stl;
10683 }
10684 else
10685 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010686 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687 fill = fill_stlnc;
10688 }
10689 /* Use fill when there is highlighting, and highlighting of current
10690 * window differs, or the fillchars differ, or this is not the
10691 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010692 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010693 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 || (fill_stl != fill_stlnc)))
10695 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010696 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 return '^';
10698 return '=';
10699}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701/*
10702 * Get the character to use in a separator between vertically split windows.
10703 * Get its attributes in "*attr".
10704 */
10705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010706fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010708 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 if (*attr == 0 && fill_vert == ' ')
10710 return '|';
10711 else
10712 return fill_vert;
10713}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714
10715/*
10716 * Return TRUE if redrawing should currently be done.
10717 */
10718 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010719redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010721#ifdef FEAT_EVAL
10722 if (disable_redraw_for_testing)
10723 return 0;
10724 else
10725#endif
10726 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10728}
10729
10730/*
10731 * Return TRUE if printing messages should currently be done.
10732 */
10733 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010734messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736 return (!(p_lz && char_avail() && !KeyTyped));
10737}
10738
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010739#ifdef FEAT_MENU
10740/*
10741 * Draw the window toolbar.
10742 */
10743 static void
10744redraw_win_toolbar(win_T *wp)
10745{
10746 vimmenu_T *menu;
10747 int item_idx = 0;
10748 int item_count = 0;
10749 int col = 0;
10750 int next_col;
10751 int off = (int)(current_ScreenLine - ScreenLines);
10752 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10753 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10754
10755 vim_free(wp->w_winbar_items);
10756 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10757 ++item_count;
10758 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10759 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10760
10761 /* TODO: use fewer spaces if there is not enough room */
10762 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010763 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010764 {
10765 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010766 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010767 break;
10768 if (col > 1)
10769 {
10770 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010771 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010772 break;
10773 }
10774
10775 wp->w_winbar_items[item_idx].wb_startcol = col;
10776 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010777 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010778 break;
10779
10780 next_col = text_to_screenline(wp, menu->name, col);
10781 while (col < next_col)
10782 {
10783 ScreenAttrs[off + col] = button_attr;
10784 ++col;
10785 }
10786 wp->w_winbar_items[item_idx].wb_endcol = col;
10787 wp->w_winbar_items[item_idx].wb_menu = menu;
10788 ++item_idx;
10789
Bram Moolenaar02631462017-09-22 15:20:32 +020010790 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010791 break;
10792 space_to_screenline(off + col, button_attr);
10793 ++col;
10794 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010795 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010796 {
10797 space_to_screenline(off + col, fill_attr);
10798 ++col;
10799 }
10800 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10801
Bram Moolenaar02631462017-09-22 15:20:32 +020010802 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10803 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010804}
10805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806/*
10807 * Show current status info in ruler and various other places
10808 * If always is FALSE, only show ruler if position has changed.
10809 */
10810 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010811showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
10813 if (!always && !redrawing())
10814 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010815#ifdef FEAT_INS_EXPAND
10816 if (pum_visible())
10817 {
10818 /* Don't redraw right now, do it later. */
10819 curwin->w_redr_status = TRUE;
10820 return;
10821 }
10822#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010823#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010824 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010825 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010826 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 else
10829#endif
10830#ifdef FEAT_CMDL_INFO
10831 win_redr_ruler(curwin, always);
10832#endif
10833
10834#ifdef FEAT_TITLE
10835 if (need_maketitle
10836# ifdef FEAT_STL_OPT
10837 || (p_icon && (stl_syntax & STL_IN_ICON))
10838 || (p_title && (stl_syntax & STL_IN_TITLE))
10839# endif
10840 )
10841 maketitle();
10842#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010843 /* Redraw the tab pages line if needed. */
10844 if (redraw_tabline)
10845 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846}
10847
10848#ifdef FEAT_CMDL_INFO
10849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010850win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010852#define RULER_BUF_LEN 70
10853 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 int row;
10855 int fillchar;
10856 int attr;
10857 int empty_line = FALSE;
10858 colnr_T virtcol;
10859 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010860 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 int this_ru_col;
10863 int off = 0;
10864 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865
10866 /* If 'ruler' off or redrawing disabled, don't do anything */
10867 if (!p_ru)
10868 return;
10869
10870 /*
10871 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10872 * after deleting lines, before cursor.lnum is corrected.
10873 */
10874 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10875 return;
10876
10877#ifdef FEAT_INS_EXPAND
10878 /* Don't draw the ruler while doing insert-completion, it might overwrite
10879 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 if (edit_submode != NULL)
10882 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010883 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10884 if (pum_visible())
10885 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886#endif
10887
10888#ifdef FEAT_STL_OPT
10889 if (*p_ruf)
10890 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010891 int save_called_emsg = called_emsg;
10892
10893 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010895 if (called_emsg)
10896 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010897 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010898 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 return;
10900 }
10901#endif
10902
10903 /*
10904 * Check if not in Insert mode and the line is empty (will show "0-1").
10905 */
10906 if (!(State & INSERT)
10907 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10908 empty_line = TRUE;
10909
10910 /*
10911 * Only draw the ruler when something changed.
10912 */
10913 validate_virtcol_win(wp);
10914 if ( redraw_cmdline
10915 || always
10916 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10917 || wp->w_cursor.col != wp->w_ru_cursor.col
10918 || wp->w_virtcol != wp->w_ru_virtcol
10919#ifdef FEAT_VIRTUALEDIT
10920 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10921#endif
10922 || wp->w_topline != wp->w_ru_topline
10923 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10924#ifdef FEAT_DIFF
10925 || wp->w_topfill != wp->w_ru_topfill
10926#endif
10927 || empty_line != wp->w_ru_empty)
10928 {
10929 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 if (wp->w_status_height)
10931 {
10932 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010933 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010934 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010935 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 {
10939 row = Rows - 1;
10940 fillchar = ' ';
10941 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 width = Columns;
10943 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 }
10945
10946 /* In list mode virtcol needs to be recomputed */
10947 virtcol = wp->w_virtcol;
10948 if (wp->w_p_list && lcs_tab1 == NUL)
10949 {
10950 wp->w_p_list = FALSE;
10951 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10952 wp->w_p_list = TRUE;
10953 }
10954
10955 /*
10956 * Some sprintfs return the length, some return a pointer.
10957 * To avoid portability problems we use strlen() here.
10958 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010959 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10961 ? 0L
10962 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010963 len = STRLEN(buffer);
10964 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10966 (int)virtcol + 1);
10967
10968 /*
10969 * Add a "50%" if there is room for it.
10970 * On the last line, don't print in the last column (scrolls the
10971 * screen up on some terminals).
10972 */
10973 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010974 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 this_ru_col = ru_col - (Columns - width);
10979 if (this_ru_col < 0)
10980 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 /* Never use more than half the window/screen width, leave the other
10982 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010983 if (this_ru_col < (width + 1) / 2)
10984 this_ru_col = (width + 1) / 2;
10985 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010987 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010988 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 {
10990#ifdef FEAT_MBYTE
10991 if (has_mbyte)
10992 i += (*mb_char2bytes)(fillchar, buffer + i);
10993 else
10994#endif
10995 buffer[i++] = fillchar;
10996 ++o;
10997 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010998 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 }
11000 /* Truncate at window boundary. */
11001#ifdef FEAT_MBYTE
11002 if (has_mbyte)
11003 {
11004 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011005 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 {
11007 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011008 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 {
11010 buffer[i] = NUL;
11011 break;
11012 }
11013 }
11014 }
11015 else
11016#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011017 if (this_ru_col + (int)STRLEN(buffer) > width)
11018 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019
Bram Moolenaar4033c552017-09-16 20:54:51 +020011020 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 i = redraw_cmdline;
11022 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011023 this_ru_col + off + (int)STRLEN(buffer),
11024 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 fillchar, fillchar, attr);
11026 /* don't redraw the cmdline because of showing the ruler */
11027 redraw_cmdline = i;
11028 wp->w_ru_cursor = wp->w_cursor;
11029 wp->w_ru_virtcol = wp->w_virtcol;
11030 wp->w_ru_empty = empty_line;
11031 wp->w_ru_topline = wp->w_topline;
11032 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11033#ifdef FEAT_DIFF
11034 wp->w_ru_topfill = wp->w_topfill;
11035#endif
11036 }
11037}
11038#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011039
11040#if defined(FEAT_LINEBREAK) || defined(PROTO)
11041/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011042 * Return the width of the 'number' and 'relativenumber' column.
11043 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011044 * Otherwise it depends on 'numberwidth' and the line count.
11045 */
11046 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011047number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011048{
11049 int n;
11050 linenr_T lnum;
11051
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011052 if (wp->w_p_rnu && !wp->w_p_nu)
11053 /* cursor line shows "0" */
11054 lnum = wp->w_height;
11055 else
11056 /* cursor line shows absolute line number */
11057 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011058
Bram Moolenaar6b314672015-03-20 15:42:10 +010011059 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011060 return wp->w_nrwidth_width;
11061 wp->w_nrwidth_line_count = lnum;
11062
11063 n = 0;
11064 do
11065 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011066 lnum /= 10;
11067 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011068 } while (lnum > 0);
11069
11070 /* 'numberwidth' gives the minimal width plus one */
11071 if (n < wp->w_p_nuw - 1)
11072 n = wp->w_p_nuw - 1;
11073
11074 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011075 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011076 return n;
11077}
11078#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011079
11080/*
11081 * Return the current cursor column. This is the actual position on the
11082 * screen. First column is 0.
11083 */
11084 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011085screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011086{
11087 return screen_cur_col;
11088}
11089
11090/*
11091 * Return the current cursor row. This is the actual position on the screen.
11092 * First row is 0.
11093 */
11094 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011095screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011096{
11097 return screen_cur_row;
11098}