blob: 447eb1e49b4c0c63340a2d907f52e36328d41f50 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
125static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
128static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
129static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200131static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200140# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void start_search_hl(void);
142static void end_search_hl(void);
143static void init_search_hl(win_T *wp);
144static void prepare_search_hl(win_T *wp, linenr_T lnum);
145static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
146static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_start_highlight(int attr);
149static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screenclear2(void);
154static void lineclear(unsigned off, int width);
155static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100156#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100170#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200185#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
186/* Can limit syntax highlight time to 'redrawtime'. */
187# define SYN_TIME_LIMIT 1
188#endif
189
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200190#ifdef FEAT_RIGHTLEFT
191# define HAS_RIGHTLEFT(x) x
192#else
193# define HAS_RIGHTLEFT(x) FALSE
194#endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Redraw the current window later, with update_screen(type).
198 * Set must_redraw only if not already set to a higher value.
199 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
200 */
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 redraw_win_later(curwin, type);
205}
206
207 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100208redraw_win_later(
209 win_T *wp,
210 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 if (wp->w_redr_type < type)
213 {
214 wp->w_redr_type = type;
215 if (type >= NOT_VALID)
216 wp->w_lines_valid = 0;
217 if (must_redraw < type) /* must_redraw is the maximum of all windows */
218 must_redraw = type;
219 }
220}
221
222/*
223 * Force a complete redraw later. Also resets the highlighting. To be used
224 * after executing a shell command that messes up the screen.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000230#ifdef FEAT_GUI
231 if (gui.in_use)
232 /* Use a code that will reset gui.highlight_mask in
233 * gui_stop_highlight(). */
234 screen_attr = HL_ALL + 1;
235 else
236#endif
237 /* Use attributes that is very unlikely to appear in text. */
238 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
242 * Mark all windows to be redrawn later.
243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 redraw_win_later(wp, type);
252 }
253}
254
255/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000256 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 redraw_buf_later(curbuf, type);
262}
263
264 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100265redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276 void
277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200292#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200293 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 }
296 }
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#ifdef FEAT_MBYTE
314 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200317 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318#endif
319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
332#ifdef FEAT_MBYTE
333 if (enc_utf8)
334 {
335 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenlineUC == NULL)
338 ret = 2;
339 for (i = 0; i < p_mco; ++i)
340 {
341 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
349 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
354#endif
355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367#ifdef FEAT_MBYTE
368 if (enc_utf8)
369 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineC[i] + r * cols,
375 ScreenLinesC[i] + LineOffset[cmdline_row + r],
376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382#endif
383 }
384
385 update_screen(0);
386 ret = 3;
387
388 if (must_redraw == 0)
389 {
390 int off = (int)(current_ScreenLine - ScreenLines);
391
392 /* Restore the text displayed in the command line area. */
393 for (r = 0; r < rows; ++r)
394 {
395 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenattr + r * cols,
400 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401#ifdef FEAT_MBYTE
402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200417 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419 ret = 4;
420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 }
422
423 vim_free(screenline);
424 vim_free(screenattr);
425#ifdef FEAT_MBYTE
426 if (enc_utf8)
427 {
428 vim_free(screenlineUC);
429 for (i = 0; i < p_mco; ++i)
430 vim_free(screenlineC[i]);
431 }
432 if (enc_dbcs == DBCS_JPNU)
433 vim_free(screenline2);
434#endif
435
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200436 /* Show the intro message when appropriate. */
437 maybe_intro_message();
438
439 setcursor();
440
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441 return ret;
442}
443
444/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 * Invoked after an asynchronous callback is called.
446 * If an echo command was used the cursor needs to be put back where
447 * it belongs. If highlighting was changed a redraw is needed.
448 */
449 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200450redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100451{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200452 ++redrawing_for_callback;
453
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100454 if (State == HITRETURN || State == ASKMORE)
455 ; /* do nothing */
456 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200457 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200458 /* Redrawing only works when the screen didn't scroll. Don't clear
459 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200460 if (msg_scrolled == 0
461#ifdef FEAT_WILDMENU
462 && wild_menu_showing == 0
463#endif
464 )
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200465 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200466 /* Redraw in the same position, so that the user can continue
467 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200468 redrawcmdline_ex(FALSE);
469 }
Bram Moolenaar144445d2016-07-08 21:41:54 +0200470 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200472 /* keep the command line if possible */
473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
477 out_flush();
478#ifdef FEAT_GUI
479 if (gui.in_use)
480 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200481 /* Don't update the cursor when it is blinking and off to avoid
482 * flicker. */
483 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200484 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100485 gui_mch_flush();
486 }
487#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200488
489 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100490}
491
492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 * Changed something in the current window, at buffer line "lnum", that
494 * requires that line and possibly other lines to be redrawn.
495 * Used when entering/leaving Insert mode with the cursor on a folded line.
496 * Used to remove the "$" from a change command.
497 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
498 * may become invalid and the whole window will have to be redrawn.
499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100501redrawWinline(
502 linenr_T lnum,
503 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505#ifdef FEAT_FOLDING
506 int i;
507#endif
508
509 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
510 curwin->w_redraw_top = lnum;
511 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
512 curwin->w_redraw_bot = lnum;
513 redraw_later(VALID);
514
515#ifdef FEAT_FOLDING
516 if (invalid)
517 {
518 /* A w_lines[] entry for this lnum has become invalid. */
519 i = find_wl_entry(curwin, lnum);
520 if (i >= 0)
521 curwin->w_lines[i].wl_valid = FALSE;
522 }
523#endif
524}
525
526/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200527 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 */
529 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100530update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 redraw_curbuf_later(type);
533 update_screen(type);
534}
535
536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * Based on the current value of curwin->w_topline, transfer a screenfull
538 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
539 */
540 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200541update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200543 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 win_T *wp;
545 static int did_intro = FALSE;
546#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
547 int did_one;
548#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200549#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200550 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200551 int gui_cursor_col;
552 int gui_cursor_row;
553#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000556 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (!screen_valid(TRUE))
558 return;
559
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200560 if (type == VALID_NO_UPDATE)
561 {
562 no_update = TRUE;
563 type = 0;
564 }
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (must_redraw)
567 {
568 if (type < must_redraw) /* use maximal type */
569 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000570
571 /* must_redraw is reset here, so that when we run into some weird
572 * reason to redraw while busy redrawing (e.g., asynchronous
573 * scrolling), or update_topline() in win_update() will cause a
574 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 must_redraw = 0;
576 }
577
578 /* Need to update w_lines[]. */
579 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
580 type = NOT_VALID;
581
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000582 /* Postpone the redrawing when it's not needed and when being called
583 * recursively. */
584 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
586 redraw_later(type); /* remember type for next time */
587 must_redraw = type;
588 if (type > INVERTED_ALL)
589 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
590 return;
591 }
592
593 updating_screen = TRUE;
594#ifdef FEAT_SYN_HL
595 ++display_tick; /* let syntax code know we're in a next round of
596 * display updating */
597#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200598 if (no_update)
599 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 /*
602 * if the screen was scrolled up when displaying a message, scroll it down
603 */
604 if (msg_scrolled)
605 {
606 clear_cmdline = TRUE;
607 if (msg_scrolled > Rows - 5) /* clearing is faster */
608 type = CLEAR;
609 else if (type != CLEAR)
610 {
611 check_for_delay(FALSE);
612 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
613 type = CLEAR;
614 FOR_ALL_WINDOWS(wp)
615 {
616 if (W_WINROW(wp) < msg_scrolled)
617 {
618 if (W_WINROW(wp) + wp->w_height > msg_scrolled
619 && wp->w_redr_type < REDRAW_TOP
620 && wp->w_lines_valid > 0
621 && wp->w_topline == wp->w_lines[0].wl_lnum)
622 {
623 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
624 wp->w_redr_type = REDRAW_TOP;
625 }
626 else
627 {
628 wp->w_redr_type = NOT_VALID;
629#ifdef FEAT_WINDOWS
630 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
631 <= msg_scrolled)
632 wp->w_redr_status = TRUE;
633#endif
634 }
635 }
636 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200637 if (!no_update)
638 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000639#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000640 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643 msg_scrolled = 0;
644 need_wait_return = FALSE;
645 }
646
647 /* reset cmdline_row now (may have been changed temporarily) */
648 compute_cmdrow();
649
650 /* Check for changed highlighting */
651 if (need_highlight_changed)
652 highlight_changed();
653
654 if (type == CLEAR) /* first clear screen */
655 {
656 screenclear(); /* will reset clear_cmdline */
657 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200658 /* must_redraw may be set indirectly, avoid another redraw later */
659 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661
662 if (clear_cmdline) /* going to clear cmdline (done below) */
663 check_for_delay(FALSE);
664
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000665#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200666 /* Force redraw when width of 'number' or 'relativenumber' column
667 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000668 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200669 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
670 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000671 curwin->w_redr_type = NOT_VALID;
672#endif
673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 /*
675 * Only start redrawing if there is really something to do.
676 */
677 if (type == INVERTED)
678 update_curswant();
679 if (curwin->w_redr_type < type
680 && !((type == VALID
681 && curwin->w_lines[0].wl_valid
682#ifdef FEAT_DIFF
683 && curwin->w_topfill == curwin->w_old_topfill
684 && curwin->w_botfill == curwin->w_old_botfill
685#endif
686 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000688 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
690 && curwin->w_old_visual_mode == VIsual_mode
691 && (curwin->w_valid & VALID_VIRTCOL)
692 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ))
694 curwin->w_redr_type = type;
695
Bram Moolenaar5a305422006-04-28 22:38:25 +0000696#ifdef FEAT_WINDOWS
697 /* Redraw the tab pages line if needed. */
698 if (redraw_tabline || type >= NOT_VALID)
699 draw_tabline();
700#endif
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#ifdef FEAT_SYN_HL
703 /*
704 * Correct stored syntax highlighting info for changes in each displayed
705 * buffer. Each buffer must only be done once.
706 */
707 FOR_ALL_WINDOWS(wp)
708 {
709 if (wp->w_buffer->b_mod_set)
710 {
711# ifdef FEAT_WINDOWS
712 win_T *wwp;
713
714 /* Check if we already did this buffer. */
715 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
716 if (wwp->w_buffer == wp->w_buffer)
717 break;
718# endif
719 if (
720# ifdef FEAT_WINDOWS
721 wwp == wp &&
722# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200723 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 syn_stack_apply_changes(wp->w_buffer);
725 }
726 }
727#endif
728
729 /*
730 * Go from top to bottom through the windows, redrawing the ones that need
731 * it.
732 */
733#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
734 did_one = FALSE;
735#endif
736#ifdef FEAT_SEARCH_EXTRA
737 search_hl.rm.regprog = NULL;
738#endif
739 FOR_ALL_WINDOWS(wp)
740 {
741 if (wp->w_redr_type != 0)
742 {
743 cursor_off();
744#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
745 if (!did_one)
746 {
747 did_one = TRUE;
748# ifdef FEAT_SEARCH_EXTRA
749 start_search_hl();
750# endif
751# ifdef FEAT_CLIPBOARD
752 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200753 if (clip_star.available && clip_isautosel_star())
754 clip_update_selection(&clip_star);
755 if (clip_plus.available && clip_isautosel_plus())
756 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757# endif
758#ifdef FEAT_GUI
759 /* Remove the cursor before starting to do anything, because
760 * scrolling may make it difficult to redraw the text under
761 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200762 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200763 {
764 gui_cursor_col = gui.cursor_col;
765 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200767 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#endif
770 }
771#endif
772 win_update(wp);
773 }
774
775#ifdef FEAT_WINDOWS
776 /* redraw status line after the window to minimize cursor movement */
777 if (wp->w_redr_status)
778 {
779 cursor_off();
780 win_redr_status(wp);
781 }
782#endif
783 }
784#if defined(FEAT_SEARCH_EXTRA)
785 end_search_hl();
786#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100787#ifdef FEAT_INS_EXPAND
788 /* May need to redraw the popup menu. */
789 if (pum_visible())
790 pum_redraw();
791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793#ifdef FEAT_WINDOWS
794 /* Reset b_mod_set flags. Going through all windows is probably faster
795 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 wp->w_buffer->b_mod_set = FALSE;
798#else
799 curbuf->b_mod_set = FALSE;
800#endif
801
802 updating_screen = FALSE;
803#ifdef FEAT_GUI
804 gui_may_resize_shell();
805#endif
806
807 /* Clear or redraw the command line. Done last, because scrolling may
808 * mess up the command line. */
809 if (clear_cmdline || redraw_cmdline)
810 showmode();
811
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200812 if (no_update)
813 --no_win_do_lines_ins;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200816 if (!did_intro)
817 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 did_intro = TRUE;
819
820#ifdef FEAT_GUI
821 /* Redraw the cursor and update the scrollbars when all screen updating is
822 * done. */
823 if (gui.in_use)
824 {
825 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200826 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200827 {
828 /* Put the GUI position where the cursor was, gui_update_cursor()
829 * uses that. */
830 gui.col = gui_cursor_col;
831 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200832# ifdef FEAT_MBYTE
833 gui.col = mb_fix_col(gui.col, gui.row);
834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200836 screen_cur_col = gui.col;
837 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_scrollbars(FALSE);
840 }
841#endif
842}
843
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100844#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
845/*
846 * Prepare for updating one or more windows.
847 * Caller must check for "updating_screen" already set to avoid recursiveness.
848 */
849 static void
850update_prepare(void)
851{
852 cursor_off();
853 updating_screen = TRUE;
854#ifdef FEAT_GUI
855 /* Remove the cursor before starting to do anything, because scrolling may
856 * make it difficult to redraw the text under it. */
857 if (gui.in_use)
858 gui_undraw_cursor();
859#endif
860#ifdef FEAT_SEARCH_EXTRA
861 start_search_hl();
862#endif
863}
864
865/*
866 * Finish updating one or more windows.
867 */
868 static void
869update_finish(void)
870{
871 if (redraw_cmdline)
872 showmode();
873
874# ifdef FEAT_SEARCH_EXTRA
875 end_search_hl();
876# endif
877
878 updating_screen = FALSE;
879
880# ifdef FEAT_GUI
881 gui_may_resize_shell();
882
883 /* Redraw the cursor and update the scrollbars when all screen updating is
884 * done. */
885 if (gui.in_use)
886 {
887 out_flush(); /* required before updating the cursor */
888 gui_update_cursor(FALSE, FALSE);
889 gui_update_scrollbars(FALSE);
890 }
891# endif
892}
893#endif
894
Bram Moolenaar860cae12010-06-05 23:22:07 +0200895#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896/*
897 * Return TRUE if the cursor line in window "wp" may be concealed, according
898 * to the 'concealcursor' option.
899 */
900 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100901conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200902{
903 int c;
904
905 if (*wp->w_p_cocu == NUL)
906 return FALSE;
907 if (get_real_state() & VISUAL)
908 c = 'v';
909 else if (State & INSERT)
910 c = 'i';
911 else if (State & NORMAL)
912 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200913 else if (State & CMDLINE)
914 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200915 else
916 return FALSE;
917 return vim_strchr(wp->w_p_cocu, c) != NULL;
918}
919
920/*
921 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
922 */
923 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100924conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200925{
926 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
927 {
928 need_cursor_line_redraw = TRUE;
929 /* Need to recompute cursor column, e.g., when starting Visual mode
930 * without concealing. */
931 curs_columns(TRUE);
932 }
933}
934
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100936update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200937{
938 int row;
939 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200940#ifdef SYN_TIME_LIMIT
941 proftime_T syntax_tm;
942#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100945 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200946 return;
947
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200949 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200951#ifdef SYN_TIME_LIMIT
952 /* Set the time limit to 'redrawtime'. */
953 profile_setlimit(p_rdt, &syntax_tm);
954#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100955 update_prepare();
956
Bram Moolenaar860cae12010-06-05 23:22:07 +0200957 row = 0;
958 for (j = 0; j < wp->w_lines_valid; ++j)
959 {
960 if (lnum == wp->w_lines[j].wl_lnum)
961 {
962 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200963# ifdef FEAT_SEARCH_EXTRA
964 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200965 start_search_hl();
966 prepare_search_hl(wp, lnum);
967# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200968 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
969#ifdef SYN_TIME_LIMIT
970 &syntax_tm
971#else
972 NULL
973#endif
974 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200975# if defined(FEAT_SEARCH_EXTRA)
976 end_search_hl();
977# endif
978 break;
979 }
980 row += wp->w_lines[j].wl_size;
981 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100982
983 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200984 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200985 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987#endif
988
989#if defined(FEAT_SIGNS) || defined(PROTO)
990 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100991update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
993 win_T *wp;
994 int doit = FALSE;
995
996# ifdef FEAT_FOLDING
997 win_foldinfo.fi_level = 0;
998# endif
999
1000 /* update/delete a specific mark */
1001 FOR_ALL_WINDOWS(wp)
1002 {
1003 if (buf != NULL && lnum > 0)
1004 {
1005 if (wp->w_buffer == buf && lnum >= wp->w_topline
1006 && lnum < wp->w_botline)
1007 {
1008 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1009 wp->w_redraw_top = lnum;
1010 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1011 wp->w_redraw_bot = lnum;
1012 redraw_win_later(wp, VALID);
1013 }
1014 }
1015 else
1016 redraw_win_later(wp, VALID);
1017 if (wp->w_redr_type != 0)
1018 doit = TRUE;
1019 }
1020
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001021 /* Return when there is nothing to do, screen updating is already
1022 * happening (recursive call) or still starting up. */
1023 if (!doit || updating_screen
1024#ifdef FEAT_GUI
1025 || gui.starting
1026#endif
1027 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 return;
1029
1030 /* update all windows that need updating */
1031 update_prepare();
1032
1033# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001034 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 if (wp->w_redr_type != 0)
1037 win_update(wp);
1038 if (wp->w_redr_status)
1039 win_redr_status(wp);
1040 }
1041# else
1042 if (curwin->w_redr_type != 0)
1043 win_update(curwin);
1044# endif
1045
1046 update_finish();
1047}
1048#endif
1049
1050
1051#if defined(FEAT_GUI) || defined(PROTO)
1052/*
1053 * Update a single window, its status line and maybe the command line msg.
1054 * Used for the GUI scrollbar.
1055 */
1056 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001057updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001059 /* return if already busy updating */
1060 if (updating_screen)
1061 return;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 update_prepare();
1064
1065#ifdef FEAT_CLIPBOARD
1066 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001067 if (clip_star.available && clip_isautosel_star())
1068 clip_update_selection(&clip_star);
1069 if (clip_plus.available && clip_isautosel_plus())
1070 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001077 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001078 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (wp->w_redr_status
1081# ifdef FEAT_CMDL_INFO
1082 || p_ru
1083# endif
1084# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001085 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086# endif
1087 )
1088 win_redr_status(wp);
1089#endif
1090
1091 update_finish();
1092}
1093#endif
1094
1095/*
1096 * Update a single window.
1097 *
1098 * This may cause the windows below it also to be redrawn (when clearing the
1099 * screen or scrolling lines).
1100 *
1101 * How the window is redrawn depends on wp->w_redr_type. Each type also
1102 * implies the one below it.
1103 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001104 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1106 * INVERTED redraw the changed part of the Visual area
1107 * INVERTED_ALL redraw the whole Visual area
1108 * VALID 1. scroll up/down to adjust for a changed w_topline
1109 * 2. update lines at the top when scrolled down
1110 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001111 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 * b_mod_top and b_mod_bot.
1113 * - if wp->w_redraw_top non-zero, redraw lines between
1114 * wp->w_redraw_top and wp->w_redr_bot.
1115 * - continue redrawing when syntax status is invalid.
1116 * 4. if scrolled up, update lines at the bottom.
1117 * This results in three areas that may need updating:
1118 * top: from first row to top_end (when scrolled down)
1119 * mid: from mid_start to mid_end (update inversion or changed text)
1120 * bot: from bot_start to last row (when scrolled up)
1121 */
1122 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001123win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 buf_T *buf = wp->w_buffer;
1126 int type;
1127 int top_end = 0; /* Below last row of the top area that needs
1128 updating. 0 when no top area updating. */
1129 int mid_start = 999;/* first row of the mid area that needs
1130 updating. 999 when no mid area updating. */
1131 int mid_end = 0; /* Below last row of the mid area that needs
1132 updating. 0 when no mid area updating. */
1133 int bot_start = 999;/* first row of the bot area that needs
1134 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 int scrolled_down = FALSE; /* TRUE when scrolled down when
1136 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001138 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int top_to_mod = FALSE; /* redraw above mod_top */
1140#endif
1141
1142 int row; /* current window row to display */
1143 linenr_T lnum; /* current buffer lnum to display */
1144 int idx; /* current index in w_lines[] */
1145 int srow; /* starting row of the current line */
1146
1147 int eof = FALSE; /* if TRUE, we hit the end of the file */
1148 int didline = FALSE; /* if TRUE, we finished the last line */
1149 int i;
1150 long j;
1151 static int recursive = FALSE; /* being called recursively */
1152 int old_botline = wp->w_botline;
1153#ifdef FEAT_FOLDING
1154 long fold_count;
1155#endif
1156#ifdef FEAT_SYN_HL
1157 /* remember what happened to the previous line, to know if
1158 * check_visual_highlight() can be used */
1159#define DID_NONE 1 /* didn't update a line */
1160#define DID_LINE 2 /* updated a normal line */
1161#define DID_FOLD 3 /* updated a folded line */
1162 int did_update = DID_NONE;
1163 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1164#endif
1165 linenr_T mod_top = 0;
1166 linenr_T mod_bot = 0;
1167#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1168 int save_got_int;
1169#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001170#ifdef SYN_TIME_LIMIT
1171 proftime_T syntax_tm;
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 type = wp->w_redr_type;
1175
1176 if (type == NOT_VALID)
1177 {
1178#ifdef FEAT_WINDOWS
1179 wp->w_redr_status = TRUE;
1180#endif
1181 wp->w_lines_valid = 0;
1182 }
1183
1184 /* Window is zero-height: nothing to draw. */
1185 if (wp->w_height == 0)
1186 {
1187 wp->w_redr_type = 0;
1188 return;
1189 }
1190
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001191#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 /* Window is zero-width: Only need to draw the separator. */
1193 if (wp->w_width == 0)
1194 {
1195 /* draw the vertical separator right of this window */
1196 draw_vsep_win(wp, 0);
1197 wp->w_redr_type = 0;
1198 return;
1199 }
1200#endif
1201
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202#ifdef FEAT_TERMINAL
1203 if (wp->w_buffer->b_term != NULL)
1204 {
1205 /* This window contains a terminal, redraw works completely
1206 * differently. */
1207 term_update_window(wp);
1208 wp->w_redr_type = 0;
1209 return;
1210 }
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001214 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001217#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001218 /* Force redraw when width of 'number' or 'relativenumber' column
1219 * changes. */
1220 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001221 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001222 {
1223 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001224 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001225 }
1226 else
1227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1230 {
1231 /*
1232 * When there are both inserted/deleted lines and specific lines to be
1233 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1234 * everything (only happens when redrawing is off for while).
1235 */
1236 type = NOT_VALID;
1237 }
1238 else
1239 {
1240 /*
1241 * Set mod_top to the first line that needs displaying because of
1242 * changes. Set mod_bot to the first line after the changes.
1243 */
1244 mod_top = wp->w_redraw_top;
1245 if (wp->w_redraw_bot != 0)
1246 mod_bot = wp->w_redraw_bot + 1;
1247 else
1248 mod_bot = 0;
1249 wp->w_redraw_top = 0; /* reset for next time */
1250 wp->w_redraw_bot = 0;
1251 if (buf->b_mod_set)
1252 {
1253 if (mod_top == 0 || mod_top > buf->b_mod_top)
1254 {
1255 mod_top = buf->b_mod_top;
1256#ifdef FEAT_SYN_HL
1257 /* Need to redraw lines above the change that may be included
1258 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (mod_top < 1)
1263 mod_top = 1;
1264 }
1265#endif
1266 }
1267 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1268 mod_bot = buf->b_mod_bot;
1269
1270#ifdef FEAT_SEARCH_EXTRA
1271 /* When 'hlsearch' is on and using a multi-line search pattern, a
1272 * change in one line may make the Search highlighting in a
1273 * previous line invalid. Simple solution: redraw all visible
1274 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001277 if (search_hl.rm.regprog != NULL
1278 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001280 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001281 {
1282 cur = wp->w_match_head;
1283 while (cur != NULL)
1284 {
1285 if (cur->match.regprog != NULL
1286 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001287 {
1288 top_to_mod = TRUE;
1289 break;
1290 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001291 cur = cur->next;
1292 }
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294#endif
1295 }
1296#ifdef FEAT_FOLDING
1297 if (mod_top != 0 && hasAnyFolding(wp))
1298 {
1299 linenr_T lnumt, lnumb;
1300
1301 /*
1302 * A change in a line can cause lines above it to become folded or
1303 * unfolded. Find the top most buffer line that may be affected.
1304 * If the line was previously folded and displayed, get the first
1305 * line of that fold. If the line is folded now, get the first
1306 * folded line. Use the minimum of these two.
1307 */
1308
1309 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1310 * the line below it. If there is no valid entry, use w_topline.
1311 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1312 * to this line. If there is no valid entry, use MAXLNUM. */
1313 lnumt = wp->w_topline;
1314 lnumb = MAXLNUM;
1315 for (i = 0; i < wp->w_lines_valid; ++i)
1316 if (wp->w_lines[i].wl_valid)
1317 {
1318 if (wp->w_lines[i].wl_lastlnum < mod_top)
1319 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1320 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1321 {
1322 lnumb = wp->w_lines[i].wl_lnum;
1323 /* When there is a fold column it might need updating
1324 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001325 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 ++lnumb;
1327 }
1328 }
1329
1330 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1331 if (mod_top > lnumt)
1332 mod_top = lnumt;
1333
1334 /* Now do the same for the bottom line (one above mod_bot). */
1335 --mod_bot;
1336 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1337 ++mod_bot;
1338 if (mod_bot < lnumb)
1339 mod_bot = lnumb;
1340 }
1341#endif
1342
1343 /* When a change starts above w_topline and the end is below
1344 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001345 * If the end of the change is above w_topline: do like no change was
1346 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (mod_top != 0 && mod_top < wp->w_topline)
1348 {
1349 if (mod_bot > wp->w_topline)
1350 mod_top = wp->w_topline;
1351#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 top_end = 1;
1354#endif
1355 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001356
1357 /* When line numbers are displayed need to redraw all lines below
1358 * inserted/deleted lines. */
1359 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1360 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362
1363 /*
1364 * When only displaying the lines at the top, set top_end. Used when
1365 * window has scrolled down for msg_scrolled.
1366 */
1367 if (type == REDRAW_TOP)
1368 {
1369 j = 0;
1370 for (i = 0; i < wp->w_lines_valid; ++i)
1371 {
1372 j += wp->w_lines[i].wl_size;
1373 if (j >= wp->w_upd_rows)
1374 {
1375 top_end = j;
1376 break;
1377 }
1378 }
1379 if (top_end == 0)
1380 /* not found (cannot happen?): redraw everything */
1381 type = NOT_VALID;
1382 else
1383 /* top area defined, the rest is VALID */
1384 type = VALID;
1385 }
1386
Bram Moolenaar367329b2007-08-30 11:53:22 +00001387 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001388 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1389 * non-zero and thus not FALSE) will indicate that screenclear() was not
1390 * called. */
1391 if (screen_cleared)
1392 screen_cleared = MAYBE;
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /*
1395 * If there are no changes on the screen that require a complete redraw,
1396 * handle three cases:
1397 * 1: we are off the top of the screen by a few lines: scroll down
1398 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1399 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1400 * w_lines[] that needs updating.
1401 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001402 if ((type == VALID || type == SOME_VALID
1403 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_DIFF
1405 && !wp->w_botfill && !wp->w_old_botfill
1406#endif
1407 )
1408 {
1409 if (mod_top != 0 && wp->w_topline == mod_top)
1410 {
1411 /*
1412 * w_topline is the first changed line, the scrolling will be done
1413 * further down.
1414 */
1415 }
1416 else if (wp->w_lines[0].wl_valid
1417 && (wp->w_topline < wp->w_lines[0].wl_lnum
1418#ifdef FEAT_DIFF
1419 || (wp->w_topline == wp->w_lines[0].wl_lnum
1420 && wp->w_topfill > wp->w_old_topfill)
1421#endif
1422 ))
1423 {
1424 /*
1425 * New topline is above old topline: May scroll down.
1426 */
1427#ifdef FEAT_FOLDING
1428 if (hasAnyFolding(wp))
1429 {
1430 linenr_T ln;
1431
1432 /* count the number of lines we are off, counting a sequence
1433 * of folded lines as one */
1434 j = 0;
1435 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1436 {
1437 ++j;
1438 if (j >= wp->w_height - 2)
1439 break;
1440 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1441 }
1442 }
1443 else
1444#endif
1445 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1446 if (j < wp->w_height - 2) /* not too far off */
1447 {
1448 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1449#ifdef FEAT_DIFF
1450 /* insert extra lines for previously invisible filler lines */
1451 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1452 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1453 - wp->w_old_topfill;
1454#endif
1455 if (i < wp->w_height - 2) /* less than a screen off */
1456 {
1457 /*
1458 * Try to insert the correct number of lines.
1459 * If not the last window, delete the lines at the bottom.
1460 * win_ins_lines may fail when the terminal can't do it.
1461 */
1462 if (i > 0)
1463 check_for_delay(FALSE);
1464 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1465 {
1466 if (wp->w_lines_valid != 0)
1467 {
1468 /* Need to update rows that are new, stop at the
1469 * first one that scrolled down. */
1470 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 /* Move the entries that were scrolled, disable
1474 * the entries for the lines to be redrawn. */
1475 if ((wp->w_lines_valid += j) > wp->w_height)
1476 wp->w_lines_valid = wp->w_height;
1477 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1478 wp->w_lines[idx] = wp->w_lines[idx - j];
1479 while (idx >= 0)
1480 wp->w_lines[idx--].wl_valid = FALSE;
1481 }
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 mid_start = 0; /* redraw all lines */
1488 }
1489 else
1490 mid_start = 0; /* redraw all lines */
1491 }
1492 else
1493 {
1494 /*
1495 * New topline is at or below old topline: May scroll up.
1496 * When topline didn't change, find first entry in w_lines[] that
1497 * needs updating.
1498 */
1499
1500 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1501 j = -1;
1502 row = 0;
1503 for (i = 0; i < wp->w_lines_valid; i++)
1504 {
1505 if (wp->w_lines[i].wl_valid
1506 && wp->w_lines[i].wl_lnum == wp->w_topline)
1507 {
1508 j = i;
1509 break;
1510 }
1511 row += wp->w_lines[i].wl_size;
1512 }
1513 if (j == -1)
1514 {
1515 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1516 * lines */
1517 mid_start = 0;
1518 }
1519 else
1520 {
1521 /*
1522 * Try to delete the correct number of lines.
1523 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1524 */
1525#ifdef FEAT_DIFF
1526 /* If the topline didn't change, delete old filler lines,
1527 * otherwise delete filler lines of the new topline... */
1528 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1529 row += wp->w_old_topfill;
1530 else
1531 row += diff_check_fill(wp, wp->w_topline);
1532 /* ... but don't delete new filler lines. */
1533 row -= wp->w_topfill;
1534#endif
1535 if (row > 0)
1536 {
1537 check_for_delay(FALSE);
1538 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1539 bot_start = wp->w_height - row;
1540 else
1541 mid_start = 0; /* redraw all lines */
1542 }
1543 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1544 {
1545 /*
1546 * Skip the lines (below the deleted lines) that are still
1547 * valid and don't need redrawing. Copy their info
1548 * upwards, to compensate for the deleted lines. Set
1549 * bot_start to the first row that needs redrawing.
1550 */
1551 bot_start = 0;
1552 idx = 0;
1553 for (;;)
1554 {
1555 wp->w_lines[idx] = wp->w_lines[j];
1556 /* stop at line that didn't fit, unless it is still
1557 * valid (no lines deleted) */
1558 if (row > 0 && bot_start + row
1559 + (int)wp->w_lines[j].wl_size > wp->w_height)
1560 {
1561 wp->w_lines_valid = idx + 1;
1562 break;
1563 }
1564 bot_start += wp->w_lines[idx++].wl_size;
1565
1566 /* stop at the last valid entry in w_lines[].wl_size */
1567 if (++j >= wp->w_lines_valid)
1568 {
1569 wp->w_lines_valid = idx;
1570 break;
1571 }
1572 }
1573#ifdef FEAT_DIFF
1574 /* Correct the first entry for filler lines at the top
1575 * when it won't get updated below. */
1576 if (wp->w_p_diff && bot_start > 0)
1577 wp->w_lines[0].wl_size =
1578 plines_win_nofill(wp, wp->w_topline, TRUE)
1579 + wp->w_topfill;
1580#endif
1581 }
1582 }
1583 }
1584
1585 /* When starting redraw in the first line, redraw all lines. When
1586 * there is only one window it's probably faster to clear the screen
1587 * first. */
1588 if (mid_start == 0)
1589 {
1590 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001591 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001592 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001593 /* Clear the screen when it was not done by win_del_lines() or
1594 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1595 * then. */
1596 if (screen_cleared != TRUE)
1597 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001598#ifdef FEAT_WINDOWS
1599 /* The screen was cleared, redraw the tab pages line. */
1600 if (redraw_tabline)
1601 draw_tabline();
1602#endif
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001605
1606 /* When win_del_lines() or win_ins_lines() caused the screen to be
1607 * cleared (only happens for the first window) or when screenclear()
1608 * was called directly above, "must_redraw" will have been set to
1609 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1610 if (screen_cleared == TRUE)
1611 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 else
1614 {
1615 /* Not VALID or INVERTED: redraw all lines. */
1616 mid_start = 0;
1617 mid_end = wp->w_height;
1618 }
1619
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001620 if (type == SOME_VALID)
1621 {
1622 /* SOME_VALID: redraw all lines. */
1623 mid_start = 0;
1624 mid_end = wp->w_height;
1625 type = NOT_VALID;
1626 }
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* check if we are updating or removing the inverted part */
1629 if ((VIsual_active && buf == curwin->w_buffer)
1630 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1631 {
1632 linenr_T from, to;
1633
1634 if (VIsual_active)
1635 {
1636 if (VIsual_active
1637 && (VIsual_mode != wp->w_old_visual_mode
1638 || type == INVERTED_ALL))
1639 {
1640 /*
1641 * If the type of Visual selection changed, redraw the whole
1642 * selection. Also when the ownership of the X selection is
1643 * gained or lost.
1644 */
1645 if (curwin->w_cursor.lnum < VIsual.lnum)
1646 {
1647 from = curwin->w_cursor.lnum;
1648 to = VIsual.lnum;
1649 }
1650 else
1651 {
1652 from = VIsual.lnum;
1653 to = curwin->w_cursor.lnum;
1654 }
1655 /* redraw more when the cursor moved as well */
1656 if (wp->w_old_cursor_lnum < from)
1657 from = wp->w_old_cursor_lnum;
1658 if (wp->w_old_cursor_lnum > to)
1659 to = wp->w_old_cursor_lnum;
1660 if (wp->w_old_visual_lnum < from)
1661 from = wp->w_old_visual_lnum;
1662 if (wp->w_old_visual_lnum > to)
1663 to = wp->w_old_visual_lnum;
1664 }
1665 else
1666 {
1667 /*
1668 * Find the line numbers that need to be updated: The lines
1669 * between the old cursor position and the current cursor
1670 * position. Also check if the Visual position changed.
1671 */
1672 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1673 {
1674 from = curwin->w_cursor.lnum;
1675 to = wp->w_old_cursor_lnum;
1676 }
1677 else
1678 {
1679 from = wp->w_old_cursor_lnum;
1680 to = curwin->w_cursor.lnum;
1681 if (from == 0) /* Visual mode just started */
1682 from = to;
1683 }
1684
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001685 if (VIsual.lnum != wp->w_old_visual_lnum
1686 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 if (wp->w_old_visual_lnum < from
1689 && wp->w_old_visual_lnum != 0)
1690 from = wp->w_old_visual_lnum;
1691 if (wp->w_old_visual_lnum > to)
1692 to = wp->w_old_visual_lnum;
1693 if (VIsual.lnum < from)
1694 from = VIsual.lnum;
1695 if (VIsual.lnum > to)
1696 to = VIsual.lnum;
1697 }
1698 }
1699
1700 /*
1701 * If in block mode and changed column or curwin->w_curswant:
1702 * update all lines.
1703 * First compute the actual start and end column.
1704 */
1705 if (VIsual_mode == Ctrl_V)
1706 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001707 colnr_T fromc, toc;
1708#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1709 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar404406a2014-10-09 13:24:43 +02001711 if (curwin->w_p_lbr)
1712 ve_flags = VE_ALL;
1713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001715#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1716 ve_flags = save_ve_flags;
1717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 ++toc;
1719 if (curwin->w_curswant == MAXCOL)
1720 toc = MAXCOL;
1721
1722 if (fromc != wp->w_old_cursor_fcol
1723 || toc != wp->w_old_cursor_lcol)
1724 {
1725 if (from > VIsual.lnum)
1726 from = VIsual.lnum;
1727 if (to < VIsual.lnum)
1728 to = VIsual.lnum;
1729 }
1730 wp->w_old_cursor_fcol = fromc;
1731 wp->w_old_cursor_lcol = toc;
1732 }
1733 }
1734 else
1735 {
1736 /* Use the line numbers of the old Visual area. */
1737 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1738 {
1739 from = wp->w_old_cursor_lnum;
1740 to = wp->w_old_visual_lnum;
1741 }
1742 else
1743 {
1744 from = wp->w_old_visual_lnum;
1745 to = wp->w_old_cursor_lnum;
1746 }
1747 }
1748
1749 /*
1750 * There is no need to update lines above the top of the window.
1751 */
1752 if (from < wp->w_topline)
1753 from = wp->w_topline;
1754
1755 /*
1756 * If we know the value of w_botline, use it to restrict the update to
1757 * the lines that are visible in the window.
1758 */
1759 if (wp->w_valid & VALID_BOTLINE)
1760 {
1761 if (from >= wp->w_botline)
1762 from = wp->w_botline - 1;
1763 if (to >= wp->w_botline)
1764 to = wp->w_botline - 1;
1765 }
1766
1767 /*
1768 * Find the minimal part to be updated.
1769 * Watch out for scrolling that made entries in w_lines[] invalid.
1770 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1771 * top_end; need to redraw from top_end to the "to" line.
1772 * A middle mouse click with a Visual selection may change the text
1773 * above the Visual area and reset wl_valid, do count these for
1774 * mid_end (in srow).
1775 */
1776 if (mid_start > 0)
1777 {
1778 lnum = wp->w_topline;
1779 idx = 0;
1780 srow = 0;
1781 if (scrolled_down)
1782 mid_start = top_end;
1783 else
1784 mid_start = 0;
1785 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1786 {
1787 if (wp->w_lines[idx].wl_valid)
1788 mid_start += wp->w_lines[idx].wl_size;
1789 else if (!scrolled_down)
1790 srow += wp->w_lines[idx].wl_size;
1791 ++idx;
1792# ifdef FEAT_FOLDING
1793 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1794 lnum = wp->w_lines[idx].wl_lnum;
1795 else
1796# endif
1797 ++lnum;
1798 }
1799 srow += mid_start;
1800 mid_end = wp->w_height;
1801 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1802 {
1803 if (wp->w_lines[idx].wl_valid
1804 && wp->w_lines[idx].wl_lnum >= to + 1)
1805 {
1806 /* Only update until first row of this line */
1807 mid_end = srow;
1808 break;
1809 }
1810 srow += wp->w_lines[idx].wl_size;
1811 }
1812 }
1813 }
1814
1815 if (VIsual_active && buf == curwin->w_buffer)
1816 {
1817 wp->w_old_visual_mode = VIsual_mode;
1818 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1819 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001820 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 wp->w_old_curswant = curwin->w_curswant;
1822 }
1823 else
1824 {
1825 wp->w_old_visual_mode = 0;
1826 wp->w_old_cursor_lnum = 0;
1827 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001828 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1832 /* reset got_int, otherwise regexp won't work */
1833 save_got_int = got_int;
1834 got_int = 0;
1835#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001836#ifdef SYN_TIME_LIMIT
1837 /* Set the time limit to 'redrawtime'. */
1838 profile_setlimit(p_rdt, &syntax_tm);
1839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841 win_foldinfo.fi_level = 0;
1842#endif
1843
1844 /*
1845 * Update all the window rows.
1846 */
1847 idx = 0; /* first entry in w_lines[].wl_size */
1848 row = 0;
1849 srow = 0;
1850 lnum = wp->w_topline; /* first line shown in window */
1851 for (;;)
1852 {
1853 /* stop updating when reached the end of the window (check for _past_
1854 * the end of the window is at the end of the loop) */
1855 if (row == wp->w_height)
1856 {
1857 didline = TRUE;
1858 break;
1859 }
1860
1861 /* stop updating when hit the end of the file */
1862 if (lnum > buf->b_ml.ml_line_count)
1863 {
1864 eof = TRUE;
1865 break;
1866 }
1867
1868 /* Remember the starting row of the line that is going to be dealt
1869 * with. It is used further down when the line doesn't fit. */
1870 srow = row;
1871
1872 /*
1873 * Update a line when it is in an area that needs updating, when it
1874 * has changes or w_lines[idx] is invalid.
1875 * bot_start may be halfway a wrapped line after using
1876 * win_del_lines(), check if the current line includes it.
1877 * When syntax folding is being used, the saved syntax states will
1878 * already have been updated, we can't see where the syntax state is
1879 * the same again, just update until the end of the window.
1880 */
1881 if (row < top_end
1882 || (row >= mid_start && row < mid_end)
1883#ifdef FEAT_SEARCH_EXTRA
1884 || top_to_mod
1885#endif
1886 || idx >= wp->w_lines_valid
1887 || (row + wp->w_lines[idx].wl_size > bot_start)
1888 || (mod_top != 0
1889 && (lnum == mod_top
1890 || (lnum >= mod_top
1891 && (lnum < mod_bot
1892#ifdef FEAT_SYN_HL
1893 || did_update == DID_FOLD
1894 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001895 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 && (
1897# ifdef FEAT_FOLDING
1898 (foldmethodIsSyntax(wp)
1899 && hasAnyFolding(wp)) ||
1900# endif
1901 syntax_check_changed(lnum)))
1902#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001903#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001904 /* match in fixed position might need redraw
1905 * if lines were inserted or deleted */
1906 || (wp->w_match_head != NULL
1907 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 )))))
1910 {
1911#ifdef FEAT_SEARCH_EXTRA
1912 if (lnum == mod_top)
1913 top_to_mod = FALSE;
1914#endif
1915
1916 /*
1917 * When at start of changed lines: May scroll following lines
1918 * up or down to minimize redrawing.
1919 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001920 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 */
1922 if (lnum == mod_top
1923 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001924 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
1926 int old_rows = 0;
1927 int new_rows = 0;
1928 int xtra_rows;
1929 linenr_T l;
1930
1931 /* Count the old number of window rows, using w_lines[], which
1932 * should still contain the sizes for the lines as they are
1933 * currently displayed. */
1934 for (i = idx; i < wp->w_lines_valid; ++i)
1935 {
1936 /* Only valid lines have a meaningful wl_lnum. Invalid
1937 * lines are part of the changed area. */
1938 if (wp->w_lines[i].wl_valid
1939 && wp->w_lines[i].wl_lnum == mod_bot)
1940 break;
1941 old_rows += wp->w_lines[i].wl_size;
1942#ifdef FEAT_FOLDING
1943 if (wp->w_lines[i].wl_valid
1944 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1945 {
1946 /* Must have found the last valid entry above mod_bot.
1947 * Add following invalid entries. */
1948 ++i;
1949 while (i < wp->w_lines_valid
1950 && !wp->w_lines[i].wl_valid)
1951 old_rows += wp->w_lines[i++].wl_size;
1952 break;
1953 }
1954#endif
1955 }
1956
1957 if (i >= wp->w_lines_valid)
1958 {
1959 /* We can't find a valid line below the changed lines,
1960 * need to redraw until the end of the window.
1961 * Inserting/deleting lines has no use. */
1962 bot_start = 0;
1963 }
1964 else
1965 {
1966 /* Able to count old number of rows: Count new window
1967 * rows, and may insert/delete lines */
1968 j = idx;
1969 for (l = lnum; l < mod_bot; ++l)
1970 {
1971#ifdef FEAT_FOLDING
1972 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1973 ++new_rows;
1974 else
1975#endif
1976#ifdef FEAT_DIFF
1977 if (l == wp->w_topline)
1978 new_rows += plines_win_nofill(wp, l, TRUE)
1979 + wp->w_topfill;
1980 else
1981#endif
1982 new_rows += plines_win(wp, l, TRUE);
1983 ++j;
1984 if (new_rows > wp->w_height - row - 2)
1985 {
1986 /* it's getting too much, must redraw the rest */
1987 new_rows = 9999;
1988 break;
1989 }
1990 }
1991 xtra_rows = new_rows - old_rows;
1992 if (xtra_rows < 0)
1993 {
1994 /* May scroll text up. If there is not enough
1995 * remaining text or scrolling fails, must redraw the
1996 * rest. If scrolling works, must redraw the text
1997 * below the scrolled text. */
1998 if (row - xtra_rows >= wp->w_height - 2)
1999 mod_bot = MAXLNUM;
2000 else
2001 {
2002 check_for_delay(FALSE);
2003 if (win_del_lines(wp, row,
2004 -xtra_rows, FALSE, FALSE) == FAIL)
2005 mod_bot = MAXLNUM;
2006 else
2007 bot_start = wp->w_height + xtra_rows;
2008 }
2009 }
2010 else if (xtra_rows > 0)
2011 {
2012 /* May scroll text down. If there is not enough
2013 * remaining text of scrolling fails, must redraw the
2014 * rest. */
2015 if (row + xtra_rows >= wp->w_height - 2)
2016 mod_bot = MAXLNUM;
2017 else
2018 {
2019 check_for_delay(FALSE);
2020 if (win_ins_lines(wp, row + old_rows,
2021 xtra_rows, FALSE, FALSE) == FAIL)
2022 mod_bot = MAXLNUM;
2023 else if (top_end > row + old_rows)
2024 /* Scrolled the part at the top that requires
2025 * updating down. */
2026 top_end += xtra_rows;
2027 }
2028 }
2029
2030 /* When not updating the rest, may need to move w_lines[]
2031 * entries. */
2032 if (mod_bot != MAXLNUM && i != j)
2033 {
2034 if (j < i)
2035 {
2036 int x = row + new_rows;
2037
2038 /* move entries in w_lines[] upwards */
2039 for (;;)
2040 {
2041 /* stop at last valid entry in w_lines[] */
2042 if (i >= wp->w_lines_valid)
2043 {
2044 wp->w_lines_valid = j;
2045 break;
2046 }
2047 wp->w_lines[j] = wp->w_lines[i];
2048 /* stop at a line that won't fit */
2049 if (x + (int)wp->w_lines[j].wl_size
2050 > wp->w_height)
2051 {
2052 wp->w_lines_valid = j + 1;
2053 break;
2054 }
2055 x += wp->w_lines[j++].wl_size;
2056 ++i;
2057 }
2058 if (bot_start > x)
2059 bot_start = x;
2060 }
2061 else /* j > i */
2062 {
2063 /* move entries in w_lines[] downwards */
2064 j -= i;
2065 wp->w_lines_valid += j;
2066 if (wp->w_lines_valid > wp->w_height)
2067 wp->w_lines_valid = wp->w_height;
2068 for (i = wp->w_lines_valid; i - j >= idx; --i)
2069 wp->w_lines[i] = wp->w_lines[i - j];
2070
2071 /* The w_lines[] entries for inserted lines are
2072 * now invalid, but wl_size may be used above.
2073 * Reset to zero. */
2074 while (i >= idx)
2075 {
2076 wp->w_lines[i].wl_size = 0;
2077 wp->w_lines[i--].wl_valid = FALSE;
2078 }
2079 }
2080 }
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083
2084#ifdef FEAT_FOLDING
2085 /*
2086 * When lines are folded, display one line for all of them.
2087 * Otherwise, display normally (can be several display lines when
2088 * 'wrap' is on).
2089 */
2090 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2091 if (fold_count != 0)
2092 {
2093 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2094 ++row;
2095 --fold_count;
2096 wp->w_lines[idx].wl_folded = TRUE;
2097 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2098# ifdef FEAT_SYN_HL
2099 did_update = DID_FOLD;
2100# endif
2101 }
2102 else
2103#endif
2104 if (idx < wp->w_lines_valid
2105 && wp->w_lines[idx].wl_valid
2106 && wp->w_lines[idx].wl_lnum == lnum
2107 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002108 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 && srow + wp->w_lines[idx].wl_size > wp->w_height
2110#ifdef FEAT_DIFF
2111 && diff_check_fill(wp, lnum) == 0
2112#endif
2113 )
2114 {
2115 /* This line is not going to fit. Don't draw anything here,
2116 * will draw "@ " lines below. */
2117 row = wp->w_height + 1;
2118 }
2119 else
2120 {
2121#ifdef FEAT_SEARCH_EXTRA
2122 prepare_search_hl(wp, lnum);
2123#endif
2124#ifdef FEAT_SYN_HL
2125 /* Let the syntax stuff know we skipped a few lines. */
2126 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002127 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 syntax_end_parsing(syntax_last_parsed + 1);
2129#endif
2130
2131 /*
2132 * Display one line.
2133 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002134 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2135#ifdef SYN_TIME_LIMIT
2136 &syntax_tm
2137#else
2138 NULL
2139#endif
2140 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142#ifdef FEAT_FOLDING
2143 wp->w_lines[idx].wl_folded = FALSE;
2144 wp->w_lines[idx].wl_lastlnum = lnum;
2145#endif
2146#ifdef FEAT_SYN_HL
2147 did_update = DID_LINE;
2148 syntax_last_parsed = lnum;
2149#endif
2150 }
2151
2152 wp->w_lines[idx].wl_lnum = lnum;
2153 wp->w_lines[idx].wl_valid = TRUE;
2154 if (row > wp->w_height) /* past end of screen */
2155 {
2156 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002157 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2159 ++idx;
2160 break;
2161 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002162 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 wp->w_lines[idx].wl_size = row - srow;
2164 ++idx;
2165#ifdef FEAT_FOLDING
2166 lnum += fold_count + 1;
2167#else
2168 ++lnum;
2169#endif
2170 }
2171 else
2172 {
2173 /* This line does not need updating, advance to the next one */
2174 row += wp->w_lines[idx++].wl_size;
2175 if (row > wp->w_height) /* past end of screen */
2176 break;
2177#ifdef FEAT_FOLDING
2178 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2179#else
2180 ++lnum;
2181#endif
2182#ifdef FEAT_SYN_HL
2183 did_update = DID_NONE;
2184#endif
2185 }
2186
2187 if (lnum > buf->b_ml.ml_line_count)
2188 {
2189 eof = TRUE;
2190 break;
2191 }
2192 }
2193 /*
2194 * End of loop over all window lines.
2195 */
2196
2197
2198 if (idx > wp->w_lines_valid)
2199 wp->w_lines_valid = idx;
2200
2201#ifdef FEAT_SYN_HL
2202 /*
2203 * Let the syntax stuff know we stop parsing here.
2204 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002205 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 syntax_end_parsing(syntax_last_parsed + 1);
2207#endif
2208
2209 /*
2210 * If we didn't hit the end of the file, and we didn't finish the last
2211 * line we were working on, then the line didn't fit.
2212 */
2213 wp->w_empty_rows = 0;
2214#ifdef FEAT_DIFF
2215 wp->w_filler_rows = 0;
2216#endif
2217 if (!eof && !didline)
2218 {
2219 if (lnum == wp->w_topline)
2220 {
2221 /*
2222 * Single line that does not fit!
2223 * Don't overwrite it, it can be edited.
2224 */
2225 wp->w_botline = lnum + 1;
2226 }
2227#ifdef FEAT_DIFF
2228 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2229 {
2230 /* Window ends in filler lines. */
2231 wp->w_botline = lnum;
2232 wp->w_filler_rows = wp->w_height - srow;
2233 }
2234#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002235 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2236 {
2237 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2238
2239 /*
2240 * Last line isn't finished: Display "@@@" in the last screen line.
2241 */
2242 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002243 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002244 screen_fill(scr_row, scr_row + 1,
2245 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002246 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002247 set_empty_rows(wp, srow);
2248 wp->w_botline = lnum;
2249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2251 {
2252 /*
2253 * Last line isn't finished: Display "@@@" at the end.
2254 */
2255 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2256 W_WINROW(wp) + wp->w_height,
2257 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002258 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 set_empty_rows(wp, srow);
2260 wp->w_botline = lnum;
2261 }
2262 else
2263 {
2264 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2265 wp->w_botline = lnum;
2266 }
2267 }
2268 else
2269 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002270#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 draw_vsep_win(wp, row);
2272#endif
2273 if (eof) /* we hit the end of the file */
2274 {
2275 wp->w_botline = buf->b_ml.ml_line_count + 1;
2276#ifdef FEAT_DIFF
2277 j = diff_check_fill(wp, wp->w_botline);
2278 if (j > 0 && !wp->w_botfill)
2279 {
2280 /*
2281 * Display filler lines at the end of the file
2282 */
2283 if (char2cells(fill_diff) > 1)
2284 i = '-';
2285 else
2286 i = fill_diff;
2287 if (row + j > wp->w_height)
2288 j = wp->w_height - row;
2289 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2290 row += j;
2291 }
2292#endif
2293 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002294 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp->w_botline = lnum;
2296
2297 /* make sure the rest of the screen is blank */
2298 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002299 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301
2302 /* Reset the type of redrawing required, the window has been updated. */
2303 wp->w_redr_type = 0;
2304#ifdef FEAT_DIFF
2305 wp->w_old_topfill = wp->w_topfill;
2306 wp->w_old_botfill = wp->w_botfill;
2307#endif
2308
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002309 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
2311 /*
2312 * There is a trick with w_botline. If we invalidate it on each
2313 * change that might modify it, this will cause a lot of expensive
2314 * calls to plines() in update_topline() each time. Therefore the
2315 * value of w_botline is often approximated, and this value is used to
2316 * compute the value of w_topline. If the value of w_botline was
2317 * wrong, check that the value of w_topline is correct (cursor is on
2318 * the visible part of the text). If it's not, we need to redraw
2319 * again. Mostly this just means scrolling up a few lines, so it
2320 * doesn't look too bad. Only do this for the current window (where
2321 * changes are relevant).
2322 */
2323 wp->w_valid |= VALID_BOTLINE;
2324 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2325 {
2326 recursive = TRUE;
2327 curwin->w_valid &= ~VALID_TOPLINE;
2328 update_topline(); /* may invalidate w_botline again */
2329 if (must_redraw != 0)
2330 {
2331 /* Don't update for changes in buffer again. */
2332 i = curbuf->b_mod_set;
2333 curbuf->b_mod_set = FALSE;
2334 win_update(curwin);
2335 must_redraw = 0;
2336 curbuf->b_mod_set = i;
2337 }
2338 recursive = FALSE;
2339 }
2340 }
2341
2342#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2343 /* restore got_int, unless CTRL-C was hit while redrawing */
2344 if (!got_int)
2345 got_int = save_got_int;
2346#endif
2347}
2348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349/*
2350 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2351 * as the filler character.
2352 */
2353 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002354win_draw_end(
2355 win_T *wp,
2356 int c1,
2357 int c2,
2358 int row,
2359 int endrow,
2360 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2363 int n = 0;
2364# define FDC_OFF n
2365#else
2366# define FDC_OFF 0
2367#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002368#ifdef FEAT_FOLDING
2369 int fdc = compute_foldcolumn(wp, 0);
2370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372#ifdef FEAT_RIGHTLEFT
2373 if (wp->w_p_rl)
2374 {
2375 /* No check for cmdline window: should never be right-left. */
2376# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002377 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 if (n > 0)
2380 {
2381 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002382 if (n > W_WIDTH(wp))
2383 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2385 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002386 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388# endif
2389# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002390 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
2392 int nn = n + 2;
2393
2394 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002395 if (nn > W_WIDTH(wp))
2396 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2398 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002399 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 n = nn;
2401 }
2402# endif
2403 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2404 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002405 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2407 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002408 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else
2411#endif
2412 {
2413#ifdef FEAT_CMDWIN
2414 if (cmdwin_type != 0 && wp == curwin)
2415 {
2416 /* draw the cmdline character in the leftmost column */
2417 n = 1;
2418 if (n > wp->w_width)
2419 n = wp->w_width;
2420 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2421 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002422 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424#endif
2425#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002426 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002428 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 /* draw the fold column at the left */
2431 if (nn > W_WIDTH(wp))
2432 nn = W_WIDTH(wp);
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002435 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 n = nn;
2437 }
2438#endif
2439#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002440 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 int nn = n + 2;
2443
2444 /* draw the sign column after the fold column */
2445 if (nn > W_WIDTH(wp))
2446 nn = W_WIDTH(wp);
2447 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2448 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002449 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 n = nn;
2451 }
2452#endif
2453 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2454 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002455 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457 set_empty_rows(wp, row);
2458}
2459
Bram Moolenaar1a384422010-07-14 19:53:30 +02002460#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002461static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002462
2463/*
2464 * Advance **color_cols and return TRUE when there are columns to draw.
2465 */
2466 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002467advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002468{
2469 while (**color_cols >= 0 && vcol > **color_cols)
2470 ++*color_cols;
2471 return (**color_cols >= 0);
2472}
2473#endif
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475#ifdef FEAT_FOLDING
2476/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002477 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2478 * space is available for window "wp", minus "col".
2479 */
2480 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002481compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002482{
2483 int fdc = wp->w_p_fdc;
2484 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2485 int wwidth = W_WIDTH(wp);
2486
2487 if (fdc > wwidth - (col + wmw))
2488 fdc = wwidth - (col + wmw);
2489 return fdc;
2490}
2491
2492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 * Display one folded line.
2494 */
2495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002496fold_line(
2497 win_T *wp,
2498 long fold_count,
2499 foldinfo_T *foldinfo,
2500 linenr_T lnum,
2501 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002503 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 pos_T *top, *bot;
2505 linenr_T lnume = lnum + fold_count - 1;
2506 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002507 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 int col;
2510 int txtcol;
2511 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002512 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /* Build the fold line:
2515 * 1. Add the cmdwin_type for the command-line window
2516 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002517 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * 4. Compose the text
2519 * 5. Add the text
2520 * 6. set highlighting for the Visual area an other text
2521 */
2522 col = 0;
2523
2524 /*
2525 * 1. Add the cmdwin_type for the command-line window
2526 * Ignores 'rightleft', this window is never right-left.
2527 */
2528#ifdef FEAT_CMDWIN
2529 if (cmdwin_type != 0 && wp == curwin)
2530 {
2531 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002532 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#ifdef FEAT_MBYTE
2534 if (enc_utf8)
2535 ScreenLinesUC[off] = 0;
2536#endif
2537 ++col;
2538 }
2539#endif
2540
2541 /*
2542 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002543 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002545 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (fdc > 0)
2547 {
2548 fill_foldcolumn(buf, wp, TRUE, lnum);
2549#ifdef FEAT_RIGHTLEFT
2550 if (wp->w_p_rl)
2551 {
2552 int i;
2553
2554 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002555 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 /* reverse the fold column */
2557 for (i = 0; i < fdc; ++i)
2558 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2559 }
2560 else
2561#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002562 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 col += fdc;
2564 }
2565
2566#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002567# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2568 for (ri = 0; ri < l; ++ri) \
2569 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2570 else \
2571 for (ri = 0; ri < l; ++ri) \
2572 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002574# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2575 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
Bram Moolenaar64486672010-05-16 15:46:46 +02002578 /* Set all attributes of the 'number' or 'relativenumber' column and the
2579 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002580 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582#ifdef FEAT_SIGNS
2583 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002584 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 len = W_WIDTH(wp) - col;
2587 if (len > 0)
2588 {
2589 if (len > 2)
2590 len = 2;
2591# ifdef FEAT_RIGHTLEFT
2592 if (wp->w_p_rl)
2593 /* the line number isn't reversed */
2594 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002595 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 else
2597# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002598 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 col += len;
2600 }
2601 }
2602#endif
2603
2604 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002605 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002607 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 len = W_WIDTH(wp) - col;
2610 if (len > 0)
2611 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002612 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002613 long num;
2614 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002615
2616 if (len > w + 1)
2617 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002618
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002619 if (wp->w_p_nu && !wp->w_p_rnu)
2620 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002621 num = (long)lnum;
2622 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002623 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002624 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002625 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002626 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002627 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002628 /* 'number' + 'relativenumber': cursor line shows absolute
2629 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002630 num = lnum;
2631 fmt = "%-*ld ";
2632 }
2633 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002634
Bram Moolenaar700e7342013-01-30 12:31:36 +01002635 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_RIGHTLEFT
2637 if (wp->w_p_rl)
2638 /* the line number isn't reversed */
2639 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002640 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 else
2642#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002643 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 col += len;
2645 }
2646 }
2647
2648 /*
2649 * 4. Compose the folded-line string with 'foldtext', if set.
2650 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002651 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 txtcol = col; /* remember where text starts */
2654
2655 /*
2656 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2657 * Right-left text is put in columns 0 - number-col, normal text is put
2658 * in columns number-col - window-width.
2659 */
2660#ifdef FEAT_MBYTE
2661 if (has_mbyte)
2662 {
2663 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002664 int u8c, u8cc[MAX_MCO];
2665 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int idx;
2667 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669# ifdef FEAT_ARABIC
2670 int prev_c = 0; /* previous Arabic character */
2671 int prev_c1 = 0; /* first composing char for prev_c */
2672# endif
2673
2674# ifdef FEAT_RIGHTLEFT
2675 if (wp->w_p_rl)
2676 idx = off;
2677 else
2678# endif
2679 idx = off + col;
2680
2681 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2682 for (p = text; *p != NUL; )
2683 {
2684 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002685 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (col + cells > W_WIDTH(wp)
2687# ifdef FEAT_RIGHTLEFT
2688 - (wp->w_p_rl ? col : 0)
2689# endif
2690 )
2691 break;
2692 ScreenLines[idx] = *p;
2693 if (enc_utf8)
2694 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002695 u8c = utfc_ptr2char(p, u8cc);
2696 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 ScreenLinesUC[idx] = 0;
2699#ifdef FEAT_ARABIC
2700 prev_c = u8c;
2701#endif
2702 }
2703 else
2704 {
2705#ifdef FEAT_ARABIC
2706 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2707 {
2708 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002709 int pc, pc1, nc;
2710 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 int firstbyte = *p;
2712
2713 /* The idea of what is the previous and next
2714 * character depends on 'rightleft'. */
2715 if (wp->w_p_rl)
2716 {
2717 pc = prev_c;
2718 pc1 = prev_c1;
2719 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002720 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 else
2723 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002724 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 prev_c = u8c;
2729
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 pc, pc1, nc);
2732 ScreenLines[idx] = firstbyte;
2733 }
2734 else
2735 prev_c = u8c;
2736#endif
2737 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002738#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (u8c >= 0x10000)
2740 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2741 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002744 for (i = 0; i < Screen_mco; ++i)
2745 {
2746 ScreenLinesC[i][idx] = u8cc[i];
2747 if (u8cc[i] == 0)
2748 break;
2749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 }
2751 if (cells > 1)
2752 ScreenLines[idx + 1] = 0;
2753 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002754 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2755 /* double-byte single width character */
2756 ScreenLines2[idx] = p[1];
2757 else if (cells > 1)
2758 /* double-width character */
2759 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 col += cells;
2761 idx += cells;
2762 p += c_len;
2763 }
2764 }
2765 else
2766#endif
2767 {
2768 len = (int)STRLEN(text);
2769 if (len > W_WIDTH(wp) - col)
2770 len = W_WIDTH(wp) - col;
2771 if (len > 0)
2772 {
2773#ifdef FEAT_RIGHTLEFT
2774 if (wp->w_p_rl)
2775 STRNCPY(current_ScreenLine, text, len);
2776 else
2777#endif
2778 STRNCPY(current_ScreenLine + col, text, len);
2779 col += len;
2780 }
2781 }
2782
2783 /* Fill the rest of the line with the fold filler */
2784#ifdef FEAT_RIGHTLEFT
2785 if (wp->w_p_rl)
2786 col -= txtcol;
2787#endif
2788 while (col < W_WIDTH(wp)
2789#ifdef FEAT_RIGHTLEFT
2790 - (wp->w_p_rl ? txtcol : 0)
2791#endif
2792 )
2793 {
2794#ifdef FEAT_MBYTE
2795 if (enc_utf8)
2796 {
2797 if (fill_fold >= 0x80)
2798 {
2799 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002800 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002801 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002806 ScreenLines[off + col] = fill_fold;
2807 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002808 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002812 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814
2815 if (text != buf)
2816 vim_free(text);
2817
2818 /*
2819 * 6. set highlighting for the Visual area an other text.
2820 * If all folded lines are in the Visual area, highlight the line.
2821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2823 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002824 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
2826 /* Visual is after curwin->w_cursor */
2827 top = &curwin->w_cursor;
2828 bot = &VIsual;
2829 }
2830 else
2831 {
2832 /* Visual is before curwin->w_cursor */
2833 top = &VIsual;
2834 bot = &curwin->w_cursor;
2835 }
2836 if (lnum >= top->lnum
2837 && lnume <= bot->lnum
2838 && (VIsual_mode != 'v'
2839 || ((lnum > top->lnum
2840 || (lnum == top->lnum
2841 && top->col == 0))
2842 && (lnume < bot->lnum
2843 || (lnume == bot->lnum
2844 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002845 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 if (VIsual_mode == Ctrl_V)
2848 {
2849 /* Visual block mode: highlight the chars part of the block */
2850 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2851 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002852 if (wp->w_old_cursor_lcol != MAXCOL
2853 && wp->w_old_cursor_lcol + txtcol
2854 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 len = wp->w_old_cursor_lcol;
2856 else
2857 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002858 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002859 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861 }
2862 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002865 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002870#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002871 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2872 if (wp->w_p_cc_cols)
2873 {
2874 int i = 0;
2875 int j = wp->w_p_cc_cols[i];
2876 int old_txtcol = txtcol;
2877
2878 while (j > -1)
2879 {
2880 txtcol += j;
2881 if (wp->w_p_wrap)
2882 txtcol -= wp->w_skipcol;
2883 else
2884 txtcol -= wp->w_leftcol;
2885 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2886 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002887 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002888 txtcol = old_txtcol;
2889 j = wp->w_p_cc_cols[++i];
2890 }
2891 }
2892
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002893 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002894 if (wp->w_p_cuc)
2895 {
2896 txtcol += wp->w_virtcol;
2897 if (wp->w_p_wrap)
2898 txtcol -= wp->w_skipcol;
2899 else
2900 txtcol -= wp->w_leftcol;
2901 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2902 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002903 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002904 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002907 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 (int)W_WIDTH(wp), FALSE);
2909
2910 /*
2911 * Update w_cline_height and w_cline_folded if the cursor line was
2912 * updated (saves a call to plines() later).
2913 */
2914 if (wp == curwin
2915 && lnum <= curwin->w_cursor.lnum
2916 && lnume >= curwin->w_cursor.lnum)
2917 {
2918 curwin->w_cline_row = row;
2919 curwin->w_cline_height = 1;
2920 curwin->w_cline_folded = TRUE;
2921 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2922 }
2923}
2924
2925/*
2926 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2927 */
2928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002929copy_text_attr(
2930 int off,
2931 char_u *buf,
2932 int len,
2933 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002935 int i;
2936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 mch_memmove(ScreenLines + off, buf, (size_t)len);
2938# ifdef FEAT_MBYTE
2939 if (enc_utf8)
2940 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2941# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002942 for (i = 0; i < len; ++i)
2943 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944}
2945
2946/*
2947 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002948 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
2950 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002951fill_foldcolumn(
2952 char_u *p,
2953 win_T *wp,
2954 int closed, /* TRUE of FALSE */
2955 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 int i = 0;
2958 int level;
2959 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002960 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002961 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002964 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 level = win_foldinfo.fi_level;
2967 if (level > 0)
2968 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002969 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002970 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 /* If the column is too narrow, we start at the lowest level that
2973 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (first_level < 1)
2976 first_level = 1;
2977
Bram Moolenaar1c934292015-01-27 16:39:29 +01002978 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
2980 if (win_foldinfo.fi_lnum == lnum
2981 && first_level + i >= win_foldinfo.fi_low_level)
2982 p[i] = '-';
2983 else if (first_level == 1)
2984 p[i] = '|';
2985 else if (first_level + i <= 9)
2986 p[i] = '0' + first_level + i;
2987 else
2988 p[i] = '>';
2989 if (first_level + i == level)
2990 break;
2991 }
2992 }
2993 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002994 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995}
2996#endif /* FEAT_FOLDING */
2997
2998/*
2999 * Display line "lnum" of window 'wp' on the screen.
3000 * Start at row "startrow", stop when "endrow" is reached.
3001 * wp->w_virtcol needs to be valid.
3002 *
3003 * Return the number of last row the line occupies.
3004 */
3005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003006win_line(
3007 win_T *wp,
3008 linenr_T lnum,
3009 int startrow,
3010 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003011 int nochange UNUSED, /* not updating for changed text */
3012 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003014 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3016 int c = 0; /* init for GCC */
3017 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003018#ifdef FEAT_LINEBREAK
3019 long vcol_sbr = -1; /* virtual column after showbreak */
3020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 long vcol_prev = -1; /* "vcol" of previous character */
3022 char_u *line; /* current line */
3023 char_u *ptr; /* current position in "line" */
3024 int row; /* row in the window, excl w_winrow */
3025 int screen_row; /* row on the screen, incl w_winrow */
3026
3027 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3028 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003029 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003030 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 int c_extra = NUL; /* extra chars, all the same */
3032 int extra_attr = 0; /* attributes when n_extra != 0 */
3033 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3034 displaying lcs_eol at end-of-line */
3035 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3036 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3037
3038 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3039 int saved_n_extra = 0;
3040 char_u *saved_p_extra = NULL;
3041 int saved_c_extra = 0;
3042 int saved_char_attr = 0;
3043
3044 int n_attr = 0; /* chars with special attr */
3045 int saved_attr2 = 0; /* char_attr saved for n_attr */
3046 int n_attr3 = 0; /* chars with overruling special attr */
3047 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3048
3049 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3050
3051 int fromcol, tocol; /* start/end of inverting */
3052 int fromcol_prev = -2; /* start of inverting after cursor */
3053 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003055 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 pos_T pos;
3057 long v;
3058
3059 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003060 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3062 in this line */
3063 int attr = 0; /* attributes for area highlighting */
3064 int area_attr = 0; /* attributes desired by highlighting */
3065 int search_attr = 0; /* attributes desired by 'hlsearch' */
3066#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003067 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 int syntax_attr = 0; /* attributes desired by syntax */
3069 int has_syntax = FALSE; /* this buffer has syntax highl. */
3070 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003071 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003072 int draw_color_col = FALSE; /* highlight colorcolumn */
3073 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003074#endif
3075#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003076 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003077# define SPWORDLEN 150
3078 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003079 int nextlinecol = 0; /* column where nextline[] starts */
3080 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003082 int spell_attr = 0; /* attributes desired by spelling */
3083 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003084 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3085 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003086 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003087 static int cap_col = -1; /* column to check for Cap word */
3088 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003089 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#endif
3091 int extra_check; /* has syntax or linebreak */
3092#ifdef FEAT_MBYTE
3093 int multi_attr = 0; /* attributes desired by multibyte */
3094 int mb_l = 1; /* multi-byte byte length */
3095 int mb_c = 0; /* decoded multi-byte character */
3096 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003097 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#endif
3099#ifdef FEAT_DIFF
3100 int filler_lines; /* nr of filler lines to be drawn */
3101 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003102 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 int change_start = MAXCOL; /* first col of changed area */
3104 int change_end = -1; /* last col of changed area */
3105#endif
3106 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3107#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003108 int need_showbreak = FALSE; /* overlong line, skipping first x
3109 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003111#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3112 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003114 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003117 matchitem_T *cur; /* points to the match list */
3118 match_T *shl; /* points to search_hl or a match */
3119 int shl_flag; /* flag to indicate whether search_hl
3120 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003121 int pos_inprogress; /* marks that position match search is
3122 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003123 int prevcol_hl_flag; /* flag to indicate whether prevcol
3124 equals startcol of search_hl or one
3125 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127#ifdef FEAT_ARABIC
3128 int prev_c = 0; /* previous Arabic character */
3129 int prev_c1 = 0; /* first composing char for prev_c */
3130#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003131#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003132 int did_line_attr = 0;
3133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /* draw_state: items that are drawn in sequence: */
3136#define WL_START 0 /* nothing done yet */
3137#ifdef FEAT_CMDWIN
3138# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3139#else
3140# define WL_CMDLINE WL_START
3141#endif
3142#ifdef FEAT_FOLDING
3143# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3144#else
3145# define WL_FOLD WL_CMDLINE
3146#endif
3147#ifdef FEAT_SIGNS
3148# define WL_SIGN WL_FOLD + 1 /* column for signs */
3149#else
3150# define WL_SIGN WL_FOLD /* column for signs */
3151#endif
3152#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003153#ifdef FEAT_LINEBREAK
3154# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003156# define WL_BRI WL_NR
3157#endif
3158#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3159# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3160#else
3161# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#endif
3163#define WL_LINE WL_SBR + 1 /* text in the line */
3164 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003165#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 int feedback_col = 0;
3167 int feedback_old_attr = -1;
3168#endif
3169
Bram Moolenaar860cae12010-06-05 23:22:07 +02003170#ifdef FEAT_CONCEAL
3171 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003172 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003173 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003174 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003175 int is_concealing = FALSE;
3176 int boguscols = 0; /* nonexistent columns added to force
3177 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003178 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003179 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003180 int match_conc = 0; /* cchar for match functions */
3181 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003182 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003183# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003184# define FIX_FOR_BOGUSCOLS \
3185 { \
3186 n_extra += vcol_off; \
3187 vcol -= vcol_off; \
3188 vcol_off = 0; \
3189 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003190 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003191 boguscols = 0; \
3192 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003193#else
3194# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 if (startrow > endrow) /* past the end already! */
3198 return startrow;
3199
3200 row = startrow;
3201 screen_row = row + W_WINROW(wp);
3202
3203 /*
3204 * To speed up the loop below, set extra_check when there is linebreak,
3205 * trailing white space and/or syntax processing to be done.
3206 */
3207#ifdef FEAT_LINEBREAK
3208 extra_check = wp->w_p_lbr;
3209#else
3210 extra_check = 0;
3211#endif
3212#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003213 if (syntax_present(wp) && !wp->w_s->b_syn_error
3214# ifdef SYN_TIME_LIMIT
3215 && !wp->w_s->b_syn_slow
3216# endif
3217 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* Prepare for syntax highlighting in this line. When there is an
3220 * error, stop syntax highlighting. */
3221 save_did_emsg = did_emsg;
3222 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003223 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003225 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227 {
3228 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003229#ifdef SYN_TIME_LIMIT
3230 if (!wp->w_s->b_syn_slow)
3231#endif
3232 {
3233 has_syntax = TRUE;
3234 extra_check = TRUE;
3235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003238
3239 /* Check for columns to display for 'colorcolumn'. */
3240 color_cols = wp->w_p_cc_cols;
3241 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003242 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003243#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003246 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003247 && *wp->w_s->b_p_spl != NUL
3248 && wp->w_s->b_langp.ga_len > 0
3249 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003250 {
3251 /* Prepare for spell checking. */
3252 has_spell = TRUE;
3253 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003254
3255 /* Get the start of the next line, so that words that wrap to the next
3256 * line are found too: "et<line-break>al.".
3257 * Trick: skip a few chars for C/shell/Vim comments */
3258 nextline[SPWORDLEN] = NUL;
3259 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3260 {
3261 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3262 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3263 }
3264
3265 /* When a word wrapped from the previous line the start of the current
3266 * line is valid. */
3267 if (lnum == checked_lnum)
3268 cur_checked_col = checked_col;
3269 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003270
3271 /* When there was a sentence end in the previous line may require a
3272 * word starting with capital in this line. In line 1 always check
3273 * the first word. */
3274 if (lnum != capcol_lnum)
3275 cap_col = -1;
3276 if (lnum == 1)
3277 cap_col = 0;
3278 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281
3282 /*
3283 * handle visual active in this window
3284 */
3285 fromcol = -10;
3286 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3288 {
3289 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003290 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 {
3292 top = &curwin->w_cursor;
3293 bot = &VIsual;
3294 }
3295 else /* Visual is before curwin->w_cursor */
3296 {
3297 top = &VIsual;
3298 bot = &curwin->w_cursor;
3299 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003300 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (VIsual_mode == Ctrl_V) /* block mode */
3302 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003303 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 fromcol = wp->w_old_cursor_fcol;
3306 tocol = wp->w_old_cursor_lcol;
3307 }
3308 }
3309 else /* non-block mode */
3310 {
3311 if (lnum > top->lnum && lnum <= bot->lnum)
3312 fromcol = 0;
3313 else if (lnum == top->lnum)
3314 {
3315 if (VIsual_mode == 'V') /* linewise */
3316 fromcol = 0;
3317 else
3318 {
3319 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3320 if (gchar_pos(top) == NUL)
3321 tocol = fromcol + 1;
3322 }
3323 }
3324 if (VIsual_mode != 'V' && lnum == bot->lnum)
3325 {
3326 if (*p_sel == 'e' && bot->col == 0
3327#ifdef FEAT_VIRTUALEDIT
3328 && bot->coladd == 0
3329#endif
3330 )
3331 {
3332 fromcol = -10;
3333 tocol = MAXCOL;
3334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003335 else if (bot->col == MAXCOL)
3336 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 else
3338 {
3339 pos = *bot;
3340 if (*p_sel == 'e')
3341 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3342 else
3343 {
3344 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3345 ++tocol;
3346 }
3347 }
3348 }
3349 }
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 /* Check if the character under the cursor should not be inverted */
3352 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003353#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 )
3357 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 /* if inverting in this line set area_highlighting */
3360 if (fromcol >= 0)
3361 {
3362 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003363 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003365 if ((clip_star.available && !clip_star.owned
3366 && clip_isautosel_star())
3367 || (clip_plus.available && !clip_plus.owned
3368 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003369 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#endif
3371 }
3372 }
3373
3374 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003375 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003377 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 && wp == curwin
3379 && lnum >= curwin->w_cursor.lnum
3380 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3381 {
3382 if (lnum == curwin->w_cursor.lnum)
3383 getvcol(curwin, &(curwin->w_cursor),
3384 (colnr_T *)&fromcol, NULL, NULL);
3385 else
3386 fromcol = 0;
3387 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3388 {
3389 pos.lnum = lnum;
3390 pos.col = search_match_endcol;
3391 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3392 }
3393 else
3394 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003395 /* do at least one character; happens when past end of line */
3396 if (fromcol == tocol)
3397 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003399 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401
3402#ifdef FEAT_DIFF
3403 filler_lines = diff_check(wp, lnum);
3404 if (filler_lines < 0)
3405 {
3406 if (filler_lines == -1)
3407 {
3408 if (diff_find_change(wp, lnum, &change_start, &change_end))
3409 diff_hlf = HLF_ADD; /* added line */
3410 else if (change_start == 0)
3411 diff_hlf = HLF_TXD; /* changed text */
3412 else
3413 diff_hlf = HLF_CHD; /* changed line */
3414 }
3415 else
3416 diff_hlf = HLF_ADD; /* added line */
3417 filler_lines = 0;
3418 area_highlighting = TRUE;
3419 }
3420 if (lnum == wp->w_topline)
3421 filler_lines = wp->w_topfill;
3422 filler_todo = filler_lines;
3423#endif
3424
3425#ifdef LINE_ATTR
3426# ifdef FEAT_SIGNS
3427 /* If this line has a sign with line highlighting set line_attr. */
3428 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3429 if (v != 0)
3430 line_attr = sign_get_attr((int)v, TRUE);
3431# endif
3432# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3433 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003434 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003435 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436# endif
3437 if (line_attr != 0)
3438 area_highlighting = TRUE;
3439#endif
3440
3441 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3442 ptr = line;
3443
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003444#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003445 if (has_spell)
3446 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003447 /* For checking first word with a capital skip white space. */
3448 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003450
Bram Moolenaar30abd282005-06-22 22:35:10 +00003451 /* To be able to spell-check over line boundaries copy the end of the
3452 * current line into nextline[]. Above the start of the next line was
3453 * copied to nextline[SPWORDLEN]. */
3454 if (nextline[SPWORDLEN] == NUL)
3455 {
3456 /* No next line or it is empty. */
3457 nextlinecol = MAXCOL;
3458 nextline_idx = 0;
3459 }
3460 else
3461 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003462 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003463 if (v < SPWORDLEN)
3464 {
3465 /* Short line, use it completely and append the start of the
3466 * next line. */
3467 nextlinecol = 0;
3468 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003469 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003470 nextline_idx = v + 1;
3471 }
3472 else
3473 {
3474 /* Long line, use only the last SPWORDLEN bytes. */
3475 nextlinecol = v - SPWORDLEN;
3476 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3477 nextline_idx = SPWORDLEN + 1;
3478 }
3479 }
3480 }
3481#endif
3482
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003483 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003485 if (lcs_space || lcs_trail)
3486 extra_check = TRUE;
3487 /* find start of trailing whitespace */
3488 if (lcs_trail)
3489 {
3490 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003491 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003492 --trailcol;
3493 trailcol += (colnr_T) (ptr - line);
3494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496
3497 /*
3498 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3499 * first character to be displayed.
3500 */
3501 if (wp->w_p_wrap)
3502 v = wp->w_skipcol;
3503 else
3504 v = wp->w_leftcol;
3505 if (v > 0)
3506 {
3507#ifdef FEAT_MBYTE
3508 char_u *prev_ptr = ptr;
3509#endif
3510 while (vcol < v && *ptr != NUL)
3511 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003512 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 vcol += c;
3514#ifdef FEAT_MBYTE
3515 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003517 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003520 /* When:
3521 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003522 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003523 * - 'virtualedit' is set, or
3524 * - the visual mode is active,
3525 * the end of the line may be before the start of the displayed part.
3526 */
3527 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003528#ifdef FEAT_SYN_HL
3529 wp->w_p_cuc || draw_color_col ||
3530#endif
3531#ifdef FEAT_VIRTUALEDIT
3532 virtual_active() ||
3533#endif
3534 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539 /* Handle a character that's not completely on the screen: Put ptr at
3540 * that character but skip the first few screen characters. */
3541 if (vcol > v)
3542 {
3543 vcol -= c;
3544#ifdef FEAT_MBYTE
3545 ptr = prev_ptr;
3546#else
3547 --ptr;
3548#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003549 /* If the character fits on the screen, don't need to skip it.
3550 * Except for a TAB. */
3551 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003552#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003553 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003554#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003555 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003556 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
3559 /*
3560 * Adjust for when the inverted text is before the screen,
3561 * and when the start of the inverted text is before the screen.
3562 */
3563 if (tocol <= vcol)
3564 fromcol = 0;
3565 else if (fromcol >= 0 && fromcol < vcol)
3566 fromcol = vcol;
3567
3568#ifdef FEAT_LINEBREAK
3569 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3570 if (wp->w_p_wrap)
3571 need_showbreak = TRUE;
3572#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003573#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003574 /* When spell checking a word we need to figure out the start of the
3575 * word and if it's badly spelled or not. */
3576 if (has_spell)
3577 {
3578 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003579 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003580 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003581
3582 pos = wp->w_cursor;
3583 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003584 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003585 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003586
3587 /* spell_move_to() may call ml_get() and make "line" invalid */
3588 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3589 ptr = line + linecol;
3590
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003591 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003592 {
3593 /* no bad word found at line start, don't check until end of a
3594 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003595 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003596 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003597 }
3598 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003599 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003600 /* bad word found, use attributes until end of word */
3601 word_end = wp->w_cursor.col + len + 1;
3602
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003603 /* Turn index into actual attributes. */
3604 if (spell_hlf != HLF_COUNT)
3605 spell_attr = highlight_attr[spell_hlf];
3606 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003607 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003608
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003609# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003610 /* Need to restart syntax highlighting for this line. */
3611 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003612 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003613# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003614 }
3615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617
3618 /*
3619 * Correct highlighting for cursor that can't be disabled.
3620 * Avoids having to check this for each character.
3621 */
3622 if (fromcol >= 0)
3623 {
3624 if (noinvcur)
3625 {
3626 if ((colnr_T)fromcol == wp->w_virtcol)
3627 {
3628 /* highlighting starts at cursor, let it start just after the
3629 * cursor */
3630 fromcol_prev = fromcol;
3631 fromcol = -1;
3632 }
3633 else if ((colnr_T)fromcol < wp->w_virtcol)
3634 /* restart highlighting after the cursor */
3635 fromcol_prev = wp->w_virtcol;
3636 }
3637 if (fromcol >= tocol)
3638 fromcol = -1;
3639 }
3640
3641#ifdef FEAT_SEARCH_EXTRA
3642 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003643 * Handle highlighting the last used search pattern and matches.
3644 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003646 cur = wp->w_match_head;
3647 shl_flag = FALSE;
3648 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003650 if (shl_flag == FALSE)
3651 {
3652 shl = &search_hl;
3653 shl_flag = TRUE;
3654 }
3655 else
3656 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003657 shl->startcol = MAXCOL;
3658 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003660 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003661 v = (long)(ptr - line);
3662 if (cur != NULL)
3663 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003664 next_search_hl(wp, shl, lnum, (colnr_T)v,
3665 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003666
3667 /* Need to get the line again, a multi-line regexp may have made it
3668 * invalid. */
3669 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3670 ptr = line + v;
3671
3672 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003674 if (shl->lnum == lnum)
3675 shl->startcol = shl->rm.startpos[0].col;
3676 else
3677 shl->startcol = 0;
3678 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3679 - shl->rm.startpos[0].lnum)
3680 shl->endcol = shl->rm.endpos[0].col;
3681 else
3682 shl->endcol = MAXCOL;
3683 /* Highlight one character for an empty match. */
3684 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003687 if (has_mbyte && line[shl->endcol] != NUL)
3688 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003691 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003693 if ((long)shl->startcol < v) /* match at leftcol */
3694 {
3695 shl->attr_cur = shl->attr;
3696 search_attr = shl->attr;
3697 }
3698 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003700 if (shl != &search_hl && cur != NULL)
3701 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703#endif
3704
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003705#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003706 /* Cursor line highlighting for 'cursorline' in the current window. Not
3707 * when Visual mode is active, because it's not clear what is selected
3708 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003709 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003710 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003711 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003712 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003713 area_highlighting = TRUE;
3714 }
3715#endif
3716
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003717 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 col = 0;
3719#ifdef FEAT_RIGHTLEFT
3720 if (wp->w_p_rl)
3721 {
3722 /* Rightleft window: process the text in the normal direction, but put
3723 * it in current_ScreenLine[] from right to left. Start at the
3724 * rightmost column of the window. */
3725 col = W_WIDTH(wp) - 1;
3726 off += col;
3727 }
3728#endif
3729
3730 /*
3731 * Repeat for the whole displayed line.
3732 */
3733 for (;;)
3734 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003735#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003736 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 /* Skip this quickly when working on the text. */
3739 if (draw_state != WL_LINE)
3740 {
3741#ifdef FEAT_CMDWIN
3742 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3743 {
3744 draw_state = WL_CMDLINE;
3745 if (cmdwin_type != 0 && wp == curwin)
3746 {
3747 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003749 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003750 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 }
3753#endif
3754
3755#ifdef FEAT_FOLDING
3756 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3757 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003758 int fdc = compute_foldcolumn(wp, 0);
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003761 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003763 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003764 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003765 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003766 p_extra_free = alloc(12 + 1);
3767
3768 if (p_extra_free != NULL)
3769 {
3770 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3771 n_extra = fdc;
3772 p_extra_free[n_extra] = NUL;
3773 p_extra = p_extra_free;
3774 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003775 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779#endif
3780
3781#ifdef FEAT_SIGNS
3782 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3783 {
3784 draw_state = WL_SIGN;
3785 /* Show the sign column when there are any signs in this
3786 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003787 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003789 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003791 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792# endif
3793
3794 /* Draw two cells with the sign value or blank. */
3795 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003796 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 n_extra = 2;
3798
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003799 if (row == startrow
3800#ifdef FEAT_DIFF
3801 + filler_lines && filler_todo <= 0
3802#endif
3803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3806 SIGN_TEXT);
3807# ifdef FEAT_SIGN_ICONS
3808 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3809 SIGN_ICON);
3810 if (gui.in_use && icon_sign != 0)
3811 {
3812 /* Use the image in this position. */
3813 c_extra = SIGN_BYTE;
3814# ifdef FEAT_NETBEANS_INTG
3815 if (buf_signcount(wp->w_buffer, lnum) > 1)
3816 c_extra = MULTISIGN_BYTE;
3817# endif
3818 char_attr = icon_sign;
3819 }
3820 else
3821# endif
3822 if (text_sign != 0)
3823 {
3824 p_extra = sign_get_text(text_sign);
3825 if (p_extra != NULL)
3826 {
3827 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003828 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
3830 char_attr = sign_get_attr(text_sign, FALSE);
3831 }
3832 }
3833 }
3834 }
3835#endif
3836
3837 if (draw_state == WL_NR - 1 && n_extra == 0)
3838 {
3839 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003840 /* Display the absolute or relative line number. After the
3841 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3842 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 && (row == startrow
3844#ifdef FEAT_DIFF
3845 + filler_lines
3846#endif
3847 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3848 {
3849 /* Draw the line number (empty space after wrapping). */
3850 if (row == startrow
3851#ifdef FEAT_DIFF
3852 + filler_lines
3853#endif
3854 )
3855 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003856 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003857 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003858
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003859 if (wp->w_p_nu && !wp->w_p_rnu)
3860 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003861 num = (long)lnum;
3862 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003863 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003864 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003865 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003866 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003867 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003868 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003869 num = lnum;
3870 fmt = "%-*ld ";
3871 }
3872 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003873
Bram Moolenaar700e7342013-01-30 12:31:36 +01003874 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003875 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (wp->w_skipcol > 0)
3877 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3878 *p_extra = '-';
3879#ifdef FEAT_RIGHTLEFT
3880 if (wp->w_p_rl) /* reverse line numbers */
3881 rl_mirror(extra);
3882#endif
3883 p_extra = extra;
3884 c_extra = NUL;
3885 }
3886 else
3887 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003888 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003889 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003890#ifdef FEAT_SYN_HL
3891 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003892 * the current line differently.
3893 * TODO: Can we use CursorLine instead of CursorLineNr
3894 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003895 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003896 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003897 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
3901
Bram Moolenaar597a4222014-06-25 14:39:50 +02003902#ifdef FEAT_LINEBREAK
3903 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3904 && n_extra == 0 && *p_sbr != NUL)
3905 /* draw indent after showbreak value */
3906 draw_state = WL_BRI;
3907 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3908 /* After the showbreak, draw the breakindent */
3909 draw_state = WL_BRI - 1;
3910
3911 /* draw 'breakindent': indent wrapped text accordingly */
3912 if (draw_state == WL_BRI - 1 && n_extra == 0)
3913 {
3914 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003915 /* if need_showbreak is set, breakindent also applies */
3916 if (wp->w_p_bri && n_extra == 0
3917 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003918# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003919 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003920# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003921 )
3922 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003923 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003924# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003925 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003926 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003927 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003928# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003929 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3930 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003931 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003932# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003933 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003934# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003935 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003936 c_extra = ' ';
3937 n_extra = get_breakindent_win(wp,
3938 ml_get_buf(wp->w_buffer, lnum, FALSE));
3939 /* Correct end of highlighted area for 'breakindent',
3940 * required when 'linebreak' is also set. */
3941 if (tocol == vcol)
3942 tocol += n_extra;
3943 }
3944 }
3945#endif
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3948 if (draw_state == WL_SBR - 1 && n_extra == 0)
3949 {
3950 draw_state = WL_SBR;
3951# ifdef FEAT_DIFF
3952 if (filler_todo > 0)
3953 {
3954 /* Draw "deleted" diff line(s). */
3955 if (char2cells(fill_diff) > 1)
3956 c_extra = '-';
3957 else
3958 c_extra = fill_diff;
3959# ifdef FEAT_RIGHTLEFT
3960 if (wp->w_p_rl)
3961 n_extra = col + 1;
3962 else
3963# endif
3964 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003965 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967# endif
3968# ifdef FEAT_LINEBREAK
3969 if (*p_sbr != NUL && need_showbreak)
3970 {
3971 /* Draw 'showbreak' at the start of each broken line. */
3972 p_extra = p_sbr;
3973 c_extra = NUL;
3974 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003975 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003977 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 /* Correct end of highlighted area for 'showbreak',
3979 * required when 'linebreak' is also set. */
3980 if (tocol == vcol)
3981 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003982#ifdef FEAT_SYN_HL
3983 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003984 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003985 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003986 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989# endif
3990 }
3991#endif
3992
3993 if (draw_state == WL_LINE - 1 && n_extra == 0)
3994 {
3995 draw_state = WL_LINE;
3996 if (saved_n_extra)
3997 {
3998 /* Continue item from end of wrapped line. */
3999 n_extra = saved_n_extra;
4000 c_extra = saved_c_extra;
4001 p_extra = saved_p_extra;
4002 char_attr = saved_char_attr;
4003 }
4004 else
4005 char_attr = 0;
4006 }
4007 }
4008
4009 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004010 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004011 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#ifdef FEAT_DIFF
4013 && filler_todo <= 0
4014#endif
4015 )
4016 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004017 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004018 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019 /* Pretend we have finished updating the window. Except when
4020 * 'cursorcolumn' is set. */
4021#ifdef FEAT_SYN_HL
4022 if (wp->w_p_cuc)
4023 row = wp->w_cline_row + wp->w_cline_height;
4024 else
4025#endif
4026 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 break;
4028 }
4029
4030 if (draw_state == WL_LINE && area_highlighting)
4031 {
4032 /* handle Visual or match highlighting in this line */
4033 if (vcol == fromcol
4034#ifdef FEAT_MBYTE
4035 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4036 && (*mb_ptr2cells)(ptr) > 1)
4037#endif
4038 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004039 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 && vcol < tocol))
4041 area_attr = attr; /* start highlighting */
4042 else if (area_attr != 0
4043 && (vcol == tocol
4044 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047#ifdef FEAT_SEARCH_EXTRA
4048 if (!n_extra)
4049 {
4050 /*
4051 * Check for start/end of search pattern match.
4052 * After end, check for start/end of next match.
4053 * When another match, have to check for start again.
4054 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004055 * Do this for 'search_hl' and the match list (ordered by
4056 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004058 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004059 cur = wp->w_match_head;
4060 shl_flag = FALSE;
4061 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004063 if (shl_flag == FALSE
4064 && ((cur != NULL
4065 && cur->priority > SEARCH_HL_PRIORITY)
4066 || cur == NULL))
4067 {
4068 shl = &search_hl;
4069 shl_flag = TRUE;
4070 }
4071 else
4072 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004073 if (cur != NULL)
4074 cur->pos.cur = 0;
4075 pos_inprogress = TRUE;
4076 while (shl->rm.regprog != NULL
4077 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004079 if (shl->startcol != MAXCOL
4080 && v >= (long)shl->startcol
4081 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004083#ifdef FEAT_MBYTE
4084 int tmp_col = v + MB_PTR2LEN(ptr);
4085
4086 if (shl->endcol < tmp_col)
4087 shl->endcol = tmp_col;
4088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004090#ifdef FEAT_CONCEAL
4091 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4092 == cur->hlg_id)
4093 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004094 has_match_conc =
4095 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004096 match_conc = cur->conceal_char;
4097 }
4098 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004099 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004102 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004105 next_search_hl(wp, shl, lnum, (colnr_T)v,
4106 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004107 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004108 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 /* Need to get the line again, a multi-line regexp
4111 * may have made it invalid. */
4112 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4113 ptr = line + v;
4114
4115 if (shl->lnum == lnum)
4116 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004117 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004119 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004121 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004123 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 /* highlight empty match, try again after
4126 * it */
4127#ifdef FEAT_MBYTE
4128 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004129 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004130 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else
4132#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004133 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135
4136 /* Loop to check if the match starts at the
4137 * current position */
4138 continue;
4139 }
4140 }
4141 break;
4142 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004143 if (shl != &search_hl && cur != NULL)
4144 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004146
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004147 /* Use attributes from match with highest priority among
4148 * 'search_hl' and the match list. */
4149 search_attr = search_hl.attr_cur;
4150 cur = wp->w_match_head;
4151 shl_flag = FALSE;
4152 while (cur != NULL || shl_flag == FALSE)
4153 {
4154 if (shl_flag == FALSE
4155 && ((cur != NULL
4156 && cur->priority > SEARCH_HL_PRIORITY)
4157 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004158 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004159 shl = &search_hl;
4160 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004161 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004162 else
4163 shl = &cur->hl;
4164 if (shl->attr_cur != 0)
4165 search_attr = shl->attr_cur;
4166 if (shl != &search_hl && cur != NULL)
4167 cur = cur->next;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 }
4170#endif
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004173 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004175 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4176 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004178 if (diff_hlf == HLF_TXD && ptr - line > change_end
4179 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004181 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004182 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004183 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004186
4187 /* Decide which of the highlight attributes to use. */
4188 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004189#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004190 if (area_attr != 0)
4191 char_attr = hl_combine_attr(line_attr, area_attr);
4192 else if (search_attr != 0)
4193 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194 /* Use line_attr when not in the Visual or 'incsearch' area
4195 * (area_attr may be 0 when "noinvcur" is set). */
4196 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004197 || vcol < fromcol || vcol_prev < fromcol_prev
4198 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004200#else
4201 if (area_attr != 0)
4202 char_attr = area_attr;
4203 else if (search_attr != 0)
4204 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004205#endif
4206 else
4207 {
4208 attr_pri = FALSE;
4209#ifdef FEAT_SYN_HL
4210 if (has_syntax)
4211 char_attr = syntax_attr;
4212 else
4213#endif
4214 char_attr = 0;
4215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 /*
4219 * Get the next character to put on the screen.
4220 */
4221 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004222 * The "p_extra" points to the extra stuff that is inserted to
4223 * represent special characters (non-printable stuff) and other
4224 * things. When all characters are the same, c_extra is used.
4225 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4226 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4228 */
4229 if (n_extra > 0)
4230 {
4231 if (c_extra != NUL)
4232 {
4233 c = c_extra;
4234#ifdef FEAT_MBYTE
4235 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004236 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 {
4238 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004240 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 else
4243 mb_utf8 = FALSE;
4244#endif
4245 }
4246 else
4247 {
4248 c = *p_extra;
4249#ifdef FEAT_MBYTE
4250 if (has_mbyte)
4251 {
4252 mb_c = c;
4253 if (enc_utf8)
4254 {
4255 /* If the UTF-8 character is more than one byte:
4256 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004257 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 mb_utf8 = FALSE;
4259 if (mb_l > n_extra)
4260 mb_l = 1;
4261 else if (mb_l > 1)
4262 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004263 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004265 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 }
4268 else
4269 {
4270 /* if this is a DBCS character, put it in "mb_c" */
4271 mb_l = MB_BYTE2LEN(c);
4272 if (mb_l >= n_extra)
4273 mb_l = 1;
4274 else if (mb_l > 1)
4275 mb_c = (c << 8) + p_extra[1];
4276 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004277 if (mb_l == 0) /* at the NUL at end-of-line */
4278 mb_l = 1;
4279
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /* If a double-width char doesn't fit display a '>' in the
4281 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004282 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283# ifdef FEAT_RIGHTLEFT
4284 wp->w_p_rl ? (col <= 0) :
4285# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004286 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 && (*mb_char2cells)(mb_c) == 2)
4288 {
4289 c = '>';
4290 mb_c = c;
4291 mb_l = 1;
4292 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004293 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 /* put the pointer back to output the double-width
4295 * character at the start of the next line. */
4296 ++n_extra;
4297 --p_extra;
4298 }
4299 else
4300 {
4301 n_extra -= mb_l - 1;
4302 p_extra += mb_l - 1;
4303 }
4304 }
4305#endif
4306 ++p_extra;
4307 }
4308 --n_extra;
4309 }
4310 else
4311 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004312#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004313 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004314#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004315
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004316 if (p_extra_free != NULL)
4317 {
4318 vim_free(p_extra_free);
4319 p_extra_free = NULL;
4320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 /*
4322 * Get a character from the line itself.
4323 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004324 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004325#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004326 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328#ifdef FEAT_MBYTE
4329 if (has_mbyte)
4330 {
4331 mb_c = c;
4332 if (enc_utf8)
4333 {
4334 /* If the UTF-8 character is more than one byte: Decode it
4335 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004336 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 mb_utf8 = FALSE;
4338 if (mb_l > 1)
4339 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004340 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 /* Overlong encoded ASCII or ASCII with composing char
4342 * is displayed normally, except a NUL. */
4343 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004344 {
4345 c = mb_c;
4346# ifdef FEAT_LINEBREAK
4347 c0 = mb_c;
4348# endif
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004351
4352 /* At start of the line we can have a composing char.
4353 * Draw it as a space with a composing char. */
4354 if (utf_iscomposing(mb_c))
4355 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004356 int i;
4357
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004358 for (i = Screen_mco - 1; i > 0; --i)
4359 u8cc[i] = u8cc[i - 1];
4360 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004361 mb_c = ' ';
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364
4365 if ((mb_l == 1 && c >= 0x80)
4366 || (mb_l >= 1 && mb_c == 0)
4367 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004368# ifdef UNICODE16
4369 || mb_c >= 0x10000
4370# endif
4371 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 /*
4374 * Illegal UTF-8 byte: display as <xx>.
4375 * Non-BMP character : display as ? or fullwidth ?.
4376 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004377# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
4381 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004382# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (wp->w_p_rl) /* reverse */
4384 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004387# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else if (utf_char2cells(mb_c) != 2)
4389 STRCPY(extra, "?");
4390 else
4391 /* 0xff1f in UTF-8: full-width '?' */
4392 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 p_extra = extra;
4396 c = *p_extra;
4397 mb_c = mb_ptr2char_adv(&p_extra);
4398 mb_utf8 = (c >= 0x80);
4399 n_extra = (int)STRLEN(p_extra);
4400 c_extra = NUL;
4401 if (area_attr == 0 && search_attr == 0)
4402 {
4403 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004404 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 saved_attr2 = char_attr; /* save current attr */
4406 }
4407 }
4408 else if (mb_l == 0) /* at the NUL at end-of-line */
4409 mb_l = 1;
4410#ifdef FEAT_ARABIC
4411 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4412 {
4413 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 int pc, pc1, nc;
4415 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 /* The idea of what is the previous and next
4418 * character depends on 'rightleft'. */
4419 if (wp->w_p_rl)
4420 {
4421 pc = prev_c;
4422 pc1 = prev_c1;
4423 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004424 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 else
4427 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004428 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004430 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 prev_c = mb_c;
4433
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 else
4437 prev_c = mb_c;
4438#endif
4439 }
4440 else /* enc_dbcs */
4441 {
4442 mb_l = MB_BYTE2LEN(c);
4443 if (mb_l == 0) /* at the NUL at end-of-line */
4444 mb_l = 1;
4445 else if (mb_l > 1)
4446 {
4447 /* We assume a second byte below 32 is illegal.
4448 * Hopefully this is OK for all double-byte encodings!
4449 */
4450 if (ptr[1] >= 32)
4451 mb_c = (c << 8) + ptr[1];
4452 else
4453 {
4454 if (ptr[1] == NUL)
4455 {
4456 /* head byte at end of line */
4457 mb_l = 1;
4458 transchar_nonprint(extra, c);
4459 }
4460 else
4461 {
4462 /* illegal tail byte */
4463 mb_l = 2;
4464 STRCPY(extra, "XX");
4465 }
4466 p_extra = extra;
4467 n_extra = (int)STRLEN(extra) - 1;
4468 c_extra = NUL;
4469 c = *p_extra++;
4470 if (area_attr == 0 && search_attr == 0)
4471 {
4472 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004473 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 saved_attr2 = char_attr; /* save current attr */
4475 }
4476 mb_c = c;
4477 }
4478 }
4479 }
4480 /* If a double-width char doesn't fit display a '>' in the
4481 * last column; the character is displayed at the start of the
4482 * next line. */
4483 if ((
4484# ifdef FEAT_RIGHTLEFT
4485 wp->w_p_rl ? (col <= 0) :
4486# endif
4487 (col >= W_WIDTH(wp) - 1))
4488 && (*mb_char2cells)(mb_c) == 2)
4489 {
4490 c = '>';
4491 mb_c = c;
4492 mb_utf8 = FALSE;
4493 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004494 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /* Put pointer back so that the character will be
4496 * displayed at the start of the next line. */
4497 --ptr;
4498 }
4499 else if (*ptr != NUL)
4500 ptr += mb_l - 1;
4501
4502 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004503 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004504 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004505 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004508 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 c = ' ';
4510 if (area_attr == 0 && search_attr == 0)
4511 {
4512 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004513 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 saved_attr2 = char_attr; /* save current attr */
4515 }
4516 mb_c = c;
4517 mb_utf8 = FALSE;
4518 mb_l = 1;
4519 }
4520
4521 }
4522#endif
4523 ++ptr;
4524
4525 if (extra_check)
4526 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004527#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004528 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004529#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004530
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004531#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 /* Get syntax attribute, unless still at the start of the line
4533 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004534 v = (long)(ptr - line);
4535 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 /* Get the syntax attribute for the character. If there
4538 * is an error, disable syntax highlighting. */
4539 save_did_emsg = did_emsg;
4540 did_emsg = FALSE;
4541
Bram Moolenaar217ad922005-03-20 22:37:15 +00004542 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004543# ifdef FEAT_SPELL
4544 has_spell ? &can_spell :
4545# endif
4546 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004549 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004550 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004551 has_syntax = FALSE;
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
4554 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004555#ifdef SYN_TIME_LIMIT
4556 if (wp->w_s->b_syn_slow)
4557 has_syntax = FALSE;
4558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 /* Need to get the line again, a multi-line regexp may
4561 * have made it invalid. */
4562 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4563 ptr = line + v;
4564
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004565 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004567 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004568 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004569# ifdef FEAT_CONCEAL
4570 /* no concealing past the end of the line, it interferes
4571 * with line highlighting */
4572 if (c == NUL)
4573 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004574 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004575 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004578#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004579
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004580#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004581 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004582 * Only do this when there is no syntax highlighting, the
4583 * @Spell cluster is not used or the current syntax item
4584 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004585 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004586 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004587 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004588# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004589 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004590 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004591# endif
4592 if (c != 0 && (
4593# ifdef FEAT_SYN_HL
4594 !has_syntax ||
4595# endif
4596 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004598 char_u *prev_ptr, *p;
4599 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004600 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004601# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004602 if (has_mbyte)
4603 {
4604 prev_ptr = ptr - mb_l;
4605 v -= mb_l - 1;
4606 }
4607 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004609 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004610
4611 /* Use nextline[] if possible, it has the start of the
4612 * next line concatenated. */
4613 if ((prev_ptr - line) - nextlinecol >= 0)
4614 p = nextline + (prev_ptr - line) - nextlinecol;
4615 else
4616 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004617 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004618 len = spell_check(wp, p, &spell_hlf, &cap_col,
4619 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004620 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004621
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004622 /* In Insert mode only highlight a word that
4623 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004624 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004625 && (State & INSERT) != 0
4626 && wp->w_cursor.lnum == lnum
4627 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004628 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004629 && wp->w_cursor.col < (colnr_T)word_end)
4630 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004631 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004632 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004633 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004634
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004635 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004636 && (p - nextline) + len > nextline_idx)
4637 {
4638 /* Remember that the good word continues at the
4639 * start of the next line. */
4640 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004641 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004642 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004643
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004644 /* Turn index into actual attributes. */
4645 if (spell_hlf != HLF_COUNT)
4646 spell_attr = highlight_attr[spell_hlf];
4647
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004648 if (cap_col > 0)
4649 {
4650 if (p != prev_ptr
4651 && (p - nextline) + cap_col >= nextline_idx)
4652 {
4653 /* Remember that the word in the next line
4654 * must start with a capital. */
4655 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004656 cap_col = (int)((p - nextline) + cap_col
4657 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004658 }
4659 else
4660 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004661 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004662 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004663 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004664 }
4665 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004666 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004667 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004668 char_attr = hl_combine_attr(char_attr, spell_attr);
4669 else
4670 char_attr = hl_combine_attr(spell_attr, char_attr);
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
4673#ifdef FEAT_LINEBREAK
4674 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004675 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004677 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004678 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004680# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004681 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004682# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004683 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004685 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004687 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004688
Bram Moolenaar597a4222014-06-25 14:39:50 +02004689 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004690 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004691 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004692 if (c == TAB && n_extra + col > W_WIDTH(wp))
4693 n_extra = (int)wp->w_buffer->b_p_ts
4694 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4695
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004696# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004697 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004698# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004700# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004701 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004702 {
4703#ifdef FEAT_CONCEAL
4704 if (c == TAB)
4705 /* See "Tab alignment" below. */
4706 FIX_FOR_BOGUSCOLS;
4707#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004708 if (!wp->w_p_list)
4709 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712#endif
4713
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004714 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4715 */
4716 if (wp->w_p_list
4717 && (((c == 160
4718#ifdef FEAT_MBYTE
4719 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4720#endif
4721 ) && lcs_nbsp)
4722 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4723 {
4724 c = (c == ' ') ? lcs_space : lcs_nbsp;
4725 if (area_attr == 0 && search_attr == 0)
4726 {
4727 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004728 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004729 saved_attr2 = char_attr; /* save current attr */
4730 }
4731#ifdef FEAT_MBYTE
4732 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004733 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004734 {
4735 mb_utf8 = TRUE;
4736 u8cc[0] = 0;
4737 c = 0xc0;
4738 }
4739 else
4740 mb_utf8 = FALSE;
4741#endif
4742 }
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4745 {
4746 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004747 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
4749 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004750 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 saved_attr2 = char_attr; /* save current attr */
4752 }
4753#ifdef FEAT_MBYTE
4754 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004755 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004758 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004759 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 else
4762 mb_utf8 = FALSE;
4763#endif
4764 }
4765 }
4766
4767 /*
4768 * Handling of non-printable characters.
4769 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004770 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 /*
4773 * when getting a character from the file, we may have to
4774 * turn it into something else on the way to putting it
4775 * into "ScreenLines".
4776 */
4777 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4778 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004779 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004780 long vcol_adjusted = vcol; /* removed showbreak length */
4781#ifdef FEAT_LINEBREAK
4782 /* only adjust the tab_len, when at the first column
4783 * after the showbreak value was drawn */
4784 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4785 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004788 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004789 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4790
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004791#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004792 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004793#endif
4794 /* tab amount depends on current column */
4795 n_extra = tab_len;
4796#ifdef FEAT_LINEBREAK
4797 else
4798 {
4799 char_u *p;
4800 int len = n_extra;
4801 int i;
4802 int saved_nextra = n_extra;
4803
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004804#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004805 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004806 /* there are characters to conceal */
4807 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004808 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4809 */
4810 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4811 && n_extra > tab_len)
4812 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004813#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004814
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815 /* if n_extra > 0, it gives the number of chars, to
4816 * use for a tab, else we need to calculate the width
4817 * for a tab */
4818#ifdef FEAT_MBYTE
4819 len = (tab_len * mb_char2len(lcs_tab2));
4820 if (n_extra > 0)
4821 len += n_extra - tab_len;
4822#endif
4823 c = lcs_tab1;
4824 p = alloc((unsigned)(len + 1));
4825 vim_memset(p, ' ', len);
4826 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004827 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004828 p_extra_free = p;
4829 for (i = 0; i < tab_len; i++)
4830 {
4831#ifdef FEAT_MBYTE
4832 mb_char2bytes(lcs_tab2, p);
4833 p += mb_char2len(lcs_tab2);
4834 n_extra += mb_char2len(lcs_tab2)
4835 - (saved_nextra > 0 ? 1 : 0);
4836#else
4837 p[i] = lcs_tab2;
4838#endif
4839 }
4840 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004841#ifdef FEAT_CONCEAL
4842 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4843 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004844 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004845 n_extra -= vcol_off;
4846#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004847 }
4848#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004849#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004850 {
4851 int vc_saved = vcol_off;
4852
4853 /* Tab alignment should be identical regardless of
4854 * 'conceallevel' value. So tab compensates of all
4855 * previous concealed characters, and thus resets
4856 * vcol_off and boguscols accumulated so far in the
4857 * line. Note that the tab can be longer than
4858 * 'tabstop' when there are concealed characters. */
4859 FIX_FOR_BOGUSCOLS;
4860
4861 /* Make sure, the highlighting for the tab char will be
4862 * correctly set further below (effectively reverts the
4863 * FIX_FOR_BOGSUCOLS macro */
4864 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004865 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004866 tab_len += vc_saved;
4867 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#ifdef FEAT_MBYTE
4870 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4871#endif
4872 if (wp->w_p_list)
4873 {
4874 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004875#ifdef FEAT_LINEBREAK
4876 if (wp->w_p_lbr)
4877 c_extra = NUL; /* using p_extra from above */
4878 else
4879#endif
4880 c_extra = lcs_tab2;
4881 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004882 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 saved_attr2 = char_attr; /* save current attr */
4884#ifdef FEAT_MBYTE
4885 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004886 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
4888 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004889 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004890 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892#endif
4893 }
4894 else
4895 {
4896 c_extra = ' ';
4897 c = ' ';
4898 }
4899 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004900 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004901 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004902 || ((fromcol >= 0 || fromcol_prev >= 0)
4903 && tocol > vcol
4904 && VIsual_mode != Ctrl_V
4905 && (
4906# ifdef FEAT_RIGHTLEFT
4907 wp->w_p_rl ? (col >= 0) :
4908# endif
4909 (col < W_WIDTH(wp)))
4910 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004911 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004912 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004913 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004915 /* Display a '$' after the line or highlight an extra
4916 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4918 /* For a diff line the highlighting continues after the
4919 * "$". */
4920 if (
4921# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004922 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923# ifdef LINE_ATTR
4924 &&
4925# endif
4926# endif
4927# ifdef LINE_ATTR
4928 line_attr == 0
4929# endif
4930 )
4931#endif
4932 {
4933#ifdef FEAT_VIRTUALEDIT
4934 /* In virtualedit, visual selections may extend
4935 * beyond end of line. */
4936 if (area_highlighting && virtual_active()
4937 && tocol != MAXCOL && vcol < tocol)
4938 n_extra = 0;
4939 else
4940#endif
4941 {
4942 p_extra = at_end_str;
4943 n_extra = 1;
4944 c_extra = NUL;
4945 }
4946 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004947 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004948 c = lcs_eol;
4949 else
4950 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 lcs_eol_one = -1;
4952 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004953 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004955 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 n_attr = 1;
4957 }
4958#ifdef FEAT_MBYTE
4959 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004960 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004963 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004964 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966 else
4967 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4968#endif
4969 }
4970 else if (c != NUL)
4971 {
4972 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004973 if (n_extra == 0)
4974 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975#ifdef FEAT_RIGHTLEFT
4976 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4977 rl_mirror(p_extra); /* reverse "<12>" */
4978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004980#ifdef FEAT_LINEBREAK
4981 if (wp->w_p_lbr)
4982 {
4983 char_u *p;
4984
4985 c = *p_extra;
4986 p = alloc((unsigned)n_extra + 1);
4987 vim_memset(p, ' ', n_extra);
4988 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4989 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004990 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004991 p_extra_free = p_extra = p;
4992 }
4993 else
4994#endif
4995 {
4996 n_extra = byte2cells(c) - 1;
4997 c = *p_extra++;
4998 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004999 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005002 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 saved_attr2 = char_attr; /* save current attr */
5004 }
5005#ifdef FEAT_MBYTE
5006 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5007#endif
5008 }
5009#ifdef FEAT_VIRTUALEDIT
5010 else if (VIsual_active
5011 && (VIsual_mode == Ctrl_V
5012 || VIsual_mode == 'v')
5013 && virtual_active()
5014 && tocol != MAXCOL
5015 && vcol < tocol
5016 && (
5017# ifdef FEAT_RIGHTLEFT
5018 wp->w_p_rl ? (col >= 0) :
5019# endif
5020 (col < W_WIDTH(wp))))
5021 {
5022 c = ' ';
5023 --ptr; /* put it back at the NUL */
5024 }
5025#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005026#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else if ((
5028# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005029 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 ) && (
5033# ifdef FEAT_RIGHTLEFT
5034 wp->w_p_rl ? (col >= 0) :
5035# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005036 (col
5037# ifdef FEAT_CONCEAL
5038 - boguscols
5039# endif
5040 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
5042 /* Highlight until the right side of the window */
5043 c = ' ';
5044 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005045
5046 /* Remember we do the char for line highlighting. */
5047 ++did_line_attr;
5048
5049 /* don't do search HL for the rest of the line */
5050 if (line_attr != 0 && char_attr == search_attr && col > 0)
5051 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052# ifdef FEAT_DIFF
5053 if (diff_hlf == HLF_TXD)
5054 {
5055 diff_hlf = HLF_CHD;
5056 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005057 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005058 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005059 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5060 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005061 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064# endif
5065 }
5066#endif
5067 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005068
5069#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005070 if ( wp->w_p_cole > 0
5071 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005072 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005073 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005074 && !(lnum_in_visual_area
5075 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005076 {
5077 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005078 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005079 && (syn_get_sub_char() != NUL || match_conc
5080 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005081 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005082 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005083 /* First time at this concealed item: display one
5084 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005085 if (match_conc)
5086 c = match_conc;
5087 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005088 c = syn_get_sub_char();
5089 else if (lcs_conceal != NUL)
5090 c = lcs_conceal;
5091 else
5092 c = ' ';
5093
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005094 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005095
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005096 if (n_extra > 0)
5097 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005098 vcol += n_extra;
5099 if (wp->w_p_wrap && n_extra > 0)
5100 {
5101# ifdef FEAT_RIGHTLEFT
5102 if (wp->w_p_rl)
5103 {
5104 col -= n_extra;
5105 boguscols -= n_extra;
5106 }
5107 else
5108# endif
5109 {
5110 boguscols += n_extra;
5111 col += n_extra;
5112 }
5113 }
5114 n_extra = 0;
5115 n_attr = 0;
5116 }
5117 else if (n_skip == 0)
5118 {
5119 is_concealing = TRUE;
5120 n_skip = 1;
5121 }
5122# ifdef FEAT_MBYTE
5123 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005124 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005125 {
5126 mb_utf8 = TRUE;
5127 u8cc[0] = 0;
5128 c = 0xc0;
5129 }
5130 else
5131 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5132# endif
5133 }
5134 else
5135 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005136 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005137 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005138 }
5139#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005142#ifdef FEAT_CONCEAL
5143 /* In the cursor line and we may be concealing characters: correct
5144 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005145 if (!did_wcol && draw_state == WL_LINE
5146 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005147 && conceal_cursor_line(wp)
5148 && (int)wp->w_virtcol <= vcol + n_skip)
5149 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005150# ifdef FEAT_RIGHTLEFT
5151 if (wp->w_p_rl)
5152 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5153 else
5154# endif
5155 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005156 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005157 did_wcol = TRUE;
5158 }
5159#endif
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 /* Don't override visual selection highlighting. */
5162 if (n_attr > 0
5163 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005164 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 char_attr = extra_attr;
5166
Bram Moolenaar81695252004-12-29 20:58:21 +00005167#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /* XIM don't send preedit_start and preedit_end, but they send
5169 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5170 * im_is_preediting() here. */
5171 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005172 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 && (State & INSERT)
5174 && !p_imdisable
5175 && im_is_preediting()
5176 && draw_state == WL_LINE)
5177 {
5178 colnr_T tcol;
5179
5180 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005181 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 else
5183 tcol = preedit_end_col;
5184 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5185 {
5186 if (feedback_old_attr < 0)
5187 {
5188 feedback_col = 0;
5189 feedback_old_attr = char_attr;
5190 }
5191 char_attr = im_get_feedback_attr(feedback_col);
5192 if (char_attr < 0)
5193 char_attr = feedback_old_attr;
5194 feedback_col++;
5195 }
5196 else if (feedback_old_attr >= 0)
5197 {
5198 char_attr = feedback_old_attr;
5199 feedback_old_attr = -1;
5200 feedback_col = 0;
5201 }
5202 }
5203#endif
5204 /*
5205 * Handle the case where we are in column 0 but not on the first
5206 * character of the line and the user wants us to show us a
5207 * special character (via 'listchars' option "precedes:<char>".
5208 */
5209 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005210 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5212#ifdef FEAT_DIFF
5213 && filler_todo <= 0
5214#endif
5215 && draw_state > WL_NR
5216 && c != NUL)
5217 {
5218 c = lcs_prec;
5219 lcs_prec_todo = NUL;
5220#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005221 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5222 {
5223 /* Double-width character being overwritten by the "precedes"
5224 * character, need to fill up half the character. */
5225 c_extra = MB_FILLER_CHAR;
5226 n_extra = 1;
5227 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005228 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005231 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005234 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005235 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237 else
5238 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5239#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005240 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005243 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 n_attr3 = 1;
5245 }
5246 }
5247
5248 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005249 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005251 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005252#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005253 || did_line_attr == 1
5254#endif
5255 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005257#ifdef FEAT_SEARCH_EXTRA
5258 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005259
5260 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005261 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005262 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005263#endif
5264
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005265 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 * highlight match at end of line. If it's beyond the last
5267 * char on the screen, just overwrite that one (tricky!) Not
5268 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005269#ifdef FEAT_SEARCH_EXTRA
5270 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005271 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005272 prevcol_hl_flag = TRUE;
5273 else
5274 {
5275 cur = wp->w_match_head;
5276 while (cur != NULL)
5277 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005278 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005279 {
5280 prevcol_hl_flag = TRUE;
5281 break;
5282 }
5283 cur = cur->next;
5284 }
5285 }
5286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005288 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005289 && (VIsual_mode != Ctrl_V
5290 || lnum == VIsual.lnum
5291 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005292 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#ifdef FEAT_SEARCH_EXTRA
5294 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005295 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005296# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005297 && did_line_attr <= 1
5298# endif
5299 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#endif
5301 ))
5302 {
5303 int n = 0;
5304
5305#ifdef FEAT_RIGHTLEFT
5306 if (wp->w_p_rl)
5307 {
5308 if (col < 0)
5309 n = 1;
5310 }
5311 else
5312#endif
5313 {
5314 if (col >= W_WIDTH(wp))
5315 n = -1;
5316 }
5317 if (n != 0)
5318 {
5319 /* At the window boundary, highlight the last character
5320 * instead (better than nothing). */
5321 off += n;
5322 col += n;
5323 }
5324 else
5325 {
5326 /* Add a blank character to highlight. */
5327 ScreenLines[off] = ' ';
5328#ifdef FEAT_MBYTE
5329 if (enc_utf8)
5330 ScreenLinesUC[off] = 0;
5331#endif
5332 }
5333#ifdef FEAT_SEARCH_EXTRA
5334 if (area_attr == 0)
5335 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005336 /* Use attributes from match with highest priority among
5337 * 'search_hl' and the match list. */
5338 char_attr = search_hl.attr;
5339 cur = wp->w_match_head;
5340 shl_flag = FALSE;
5341 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005342 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005343 if (shl_flag == FALSE
5344 && ((cur != NULL
5345 && cur->priority > SEARCH_HL_PRIORITY)
5346 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005347 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005348 shl = &search_hl;
5349 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005350 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005351 else
5352 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005353 if ((ptr - line) - 1 == (long)shl->startcol
5354 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005355 char_attr = shl->attr;
5356 if (shl != &search_hl && cur != NULL)
5357 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360#endif
5361 ScreenAttrs[off] = char_attr;
5362#ifdef FEAT_RIGHTLEFT
5363 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005366 --off;
5367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 else
5369#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005372 ++off;
5373 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005374 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005375#ifdef FEAT_SYN_HL
5376 eol_hl_off = 1;
5377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaar91170f82006-05-05 21:15:17 +00005381 /*
5382 * At end of the text line.
5383 */
5384 if (c == NUL)
5385 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005386#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005387 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5388 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005389 {
5390 /* highlight last char after line */
5391 --col;
5392 --off;
5393 --vcol;
5394 }
5395
Bram Moolenaar1a384422010-07-14 19:53:30 +02005396 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005397 if (wp->w_p_wrap)
5398 v = wp->w_skipcol;
5399 else
5400 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005401
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005402 /* check if line ends before left margin */
5403 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005404 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005405#ifdef FEAT_CONCEAL
5406 /* Get rid of the boguscols now, we want to draw until the right
5407 * edge for 'cursorcolumn'. */
5408 col -= boguscols;
5409 boguscols = 0;
5410#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005411
5412 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005413 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005414
5415 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005416 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5417 && (int)wp->w_virtcol <
5418 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005419 && lnum != wp->w_cursor.lnum)
5420 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005421# ifdef FEAT_RIGHTLEFT
5422 && !wp->w_p_rl
5423# endif
5424 )
5425 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005426 int rightmost_vcol = 0;
5427 int i;
5428
5429 if (wp->w_p_cuc)
5430 rightmost_vcol = wp->w_virtcol;
5431 if (draw_color_col)
5432 /* determine rightmost colorcolumn to possibly draw */
5433 for (i = 0; color_cols[i] >= 0; ++i)
5434 if (rightmost_vcol < color_cols[i])
5435 rightmost_vcol = color_cols[i];
5436
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005437 while (col < W_WIDTH(wp))
5438 {
5439 ScreenLines[off] = ' ';
5440#ifdef FEAT_MBYTE
5441 if (enc_utf8)
5442 ScreenLinesUC[off] = 0;
5443#endif
5444 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005445 if (draw_color_col)
5446 draw_color_col = advance_color_col(VCOL_HLC,
5447 &color_cols);
5448
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005449 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005450 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005451 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005452 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005453 else
5454 ScreenAttrs[off++] = 0;
5455
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005456 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005457 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459 ++vcol;
5460 }
5461 }
5462#endif
5463
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005464 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005465 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 row++;
5467
5468 /*
5469 * Update w_cline_height and w_cline_folded if the cursor line was
5470 * updated (saves a call to plines() later).
5471 */
5472 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5473 {
5474 curwin->w_cline_row = startrow;
5475 curwin->w_cline_height = row - startrow;
5476#ifdef FEAT_FOLDING
5477 curwin->w_cline_folded = FALSE;
5478#endif
5479 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5480 }
5481
5482 break;
5483 }
5484
5485 /* line continues beyond line end */
5486 if (lcs_ext
5487 && !wp->w_p_wrap
5488#ifdef FEAT_DIFF
5489 && filler_todo <= 0
5490#endif
5491 && (
5492#ifdef FEAT_RIGHTLEFT
5493 wp->w_p_rl ? col == 0 :
5494#endif
5495 col == W_WIDTH(wp) - 1)
5496 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005497 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5499 {
5500 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005501 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#ifdef FEAT_MBYTE
5503 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005504 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005507 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005508 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510 else
5511 mb_utf8 = FALSE;
5512#endif
5513 }
5514
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005515#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005516 /* advance to the next 'colorcolumn' */
5517 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005518 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005519
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005520 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005521 * highlight the cursor position itself.
5522 * Also highlight the 'colorcolumn' if it is different than
5523 * 'cursorcolumn' */
5524 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005525 if (draw_state == WL_LINE && !lnum_in_visual_area
5526 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005527 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005528 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529 && lnum != wp->w_cursor.lnum)
5530 {
5531 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005532 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005533 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005534 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005535 {
5536 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005537 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005538 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005539 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005540#endif
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 /*
5543 * Store character to be displayed.
5544 * Skip characters that are left of the screen for 'nowrap'.
5545 */
5546 vcol_prev = vcol;
5547 if (draw_state < WL_LINE || n_skip <= 0)
5548 {
5549 /*
5550 * Store the character.
5551 */
5552#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5553 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5554 {
5555 /* A double-wide character is: put first halve in left cell. */
5556 --off;
5557 --col;
5558 }
5559#endif
5560 ScreenLines[off] = c;
5561#ifdef FEAT_MBYTE
5562 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005563 {
5564 if ((mb_c & 0xff00) == 0x8e00)
5565 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else if (enc_utf8)
5569 {
5570 if (mb_utf8)
5571 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005572 int i;
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005575 if ((c & 0xff) == 0)
5576 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005577 for (i = 0; i < Screen_mco; ++i)
5578 {
5579 ScreenLinesC[i][off] = u8cc[i];
5580 if (u8cc[i] == 0)
5581 break;
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584 else
5585 ScreenLinesUC[off] = 0;
5586 }
5587 if (multi_attr)
5588 {
5589 ScreenAttrs[off] = multi_attr;
5590 multi_attr = 0;
5591 }
5592 else
5593#endif
5594 ScreenAttrs[off] = char_attr;
5595
5596#ifdef FEAT_MBYTE
5597 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5598 {
5599 /* Need to fill two screen columns. */
5600 ++off;
5601 ++col;
5602 if (enc_utf8)
5603 /* UTF-8: Put a 0 in the second screen char. */
5604 ScreenLines[off] = 0;
5605 else
5606 /* DBCS: Put second byte in the second screen char. */
5607 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005608 if (draw_state > WL_NR
5609#ifdef FEAT_DIFF
5610 && filler_todo <= 0
5611#endif
5612 )
5613 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 /* When "tocol" is halfway a character, set it to the end of
5615 * the character, otherwise highlighting won't stop. */
5616 if (tocol == vcol)
5617 ++tocol;
5618#ifdef FEAT_RIGHTLEFT
5619 if (wp->w_p_rl)
5620 {
5621 /* now it's time to backup one cell */
5622 --off;
5623 --col;
5624 }
5625#endif
5626 }
5627#endif
5628#ifdef FEAT_RIGHTLEFT
5629 if (wp->w_p_rl)
5630 {
5631 --off;
5632 --col;
5633 }
5634 else
5635#endif
5636 {
5637 ++off;
5638 ++col;
5639 }
5640 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005641#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005642 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005643 {
5644 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005645 ++vcol_off;
5646 if (n_extra > 0)
5647 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005648 if (wp->w_p_wrap)
5649 {
5650 /*
5651 * Special voodoo required if 'wrap' is on.
5652 *
5653 * Advance the column indicator to force the line
5654 * drawing to wrap early. This will make the line
5655 * take up the same screen space when parts are concealed,
5656 * so that cursor line computations aren't messed up.
5657 *
5658 * To avoid the fictitious advance of 'col' causing
5659 * trailing junk to be written out of the screen line
5660 * we are building, 'boguscols' keeps track of the number
5661 * of bad columns we have advanced.
5662 */
5663 if (n_extra > 0)
5664 {
5665 vcol += n_extra;
5666# ifdef FEAT_RIGHTLEFT
5667 if (wp->w_p_rl)
5668 {
5669 col -= n_extra;
5670 boguscols -= n_extra;
5671 }
5672 else
5673# endif
5674 {
5675 col += n_extra;
5676 boguscols += n_extra;
5677 }
5678 n_extra = 0;
5679 n_attr = 0;
5680 }
5681
5682
5683# ifdef FEAT_MBYTE
5684 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5685 {
5686 /* Need to fill two screen columns. */
5687# ifdef FEAT_RIGHTLEFT
5688 if (wp->w_p_rl)
5689 {
5690 --boguscols;
5691 --col;
5692 }
5693 else
5694# endif
5695 {
5696 ++boguscols;
5697 ++col;
5698 }
5699 }
5700# endif
5701
5702# ifdef FEAT_RIGHTLEFT
5703 if (wp->w_p_rl)
5704 {
5705 --boguscols;
5706 --col;
5707 }
5708 else
5709# endif
5710 {
5711 ++boguscols;
5712 ++col;
5713 }
5714 }
5715 else
5716 {
5717 if (n_extra > 0)
5718 {
5719 vcol += n_extra;
5720 n_extra = 0;
5721 n_attr = 0;
5722 }
5723 }
5724
5725 }
5726#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 else
5728 --n_skip;
5729
Bram Moolenaar64486672010-05-16 15:46:46 +02005730 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5731 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005732 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#ifdef FEAT_DIFF
5734 && filler_todo <= 0
5735#endif
5736 )
5737 ++vcol;
5738
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005739#ifdef FEAT_SYN_HL
5740 if (vcol_save_attr >= 0)
5741 char_attr = vcol_save_attr;
5742#endif
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 /* restore attributes after "predeces" in 'listchars' */
5745 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5746 char_attr = saved_attr3;
5747
5748 /* restore attributes after last 'listchars' or 'number' char */
5749 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5750 char_attr = saved_attr2;
5751
5752 /*
5753 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005754 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 */
5756 if ((
5757#ifdef FEAT_RIGHTLEFT
5758 wp->w_p_rl ? (col < 0) :
5759#endif
5760 (col >= W_WIDTH(wp)))
5761 && (*ptr != NUL
5762#ifdef FEAT_DIFF
5763 || filler_todo > 0
5764#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005765 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5767 )
5768 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005769#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005770 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005771 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005772 boguscols = 0;
5773#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005774 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005775 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 ++row;
5778 ++screen_row;
5779
5780 /* When not wrapping and finished diff lines, or when displayed
5781 * '$' and highlighting until last column, break here. */
5782 if ((!wp->w_p_wrap
5783#ifdef FEAT_DIFF
5784 && filler_todo <= 0
5785#endif
5786 ) || lcs_eol_one == -1)
5787 break;
5788
5789 /* When the window is too narrow draw all "@" lines. */
5790 if (draw_state != WL_LINE
5791#ifdef FEAT_DIFF
5792 && filler_todo <= 0
5793#endif
5794 )
5795 {
5796 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005797#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 draw_vsep_win(wp, row);
5799#endif
5800 row = endrow;
5801 }
5802
5803 /* When line got too long for screen break here. */
5804 if (row == endrow)
5805 {
5806 ++row;
5807 break;
5808 }
5809
5810 if (screen_cur_row == screen_row - 1
5811#ifdef FEAT_DIFF
5812 && filler_todo <= 0
5813#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005814#ifdef FEAT_WINDOWS
5815 && W_WIDTH(wp) == Columns
5816#endif
5817 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
5819 /* Remember that the line wraps, used for modeless copy. */
5820 LineWraps[screen_row - 1] = TRUE;
5821
5822 /*
5823 * Special trick to make copy/paste of wrapped lines work with
5824 * xterm/screen: write an extra character beyond the end of
5825 * the line. This will work with all terminal types
5826 * (regardless of the xn,am settings).
5827 * Only do this on a fast tty.
5828 * Only do this if the cursor is on the current line
5829 * (something has been written in it).
5830 * Don't do this for the GUI.
5831 * Don't do this for double-width characters.
5832 * Don't do this for a window not at the right screen border.
5833 */
5834 if (p_tf
5835#ifdef FEAT_GUI
5836 && !gui.in_use
5837#endif
5838#ifdef FEAT_MBYTE
5839 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005840 && ((*mb_off2cells)(LineOffset[screen_row],
5841 LineOffset[screen_row] + screen_Columns)
5842 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005844 + (int)Columns - 2,
5845 LineOffset[screen_row] + screen_Columns)
5846 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847#endif
5848 )
5849 {
5850 /* First make sure we are at the end of the screen line,
5851 * then output the same character again to let the
5852 * terminal know about the wrap. If the terminal doesn't
5853 * auto-wrap, we overwrite the character. */
5854 if (screen_cur_col != W_WIDTH(wp))
5855 screen_char(LineOffset[screen_row - 1]
5856 + (unsigned)Columns - 1,
5857 screen_row - 1, (int)(Columns - 1));
5858
5859#ifdef FEAT_MBYTE
5860 /* When there is a multi-byte character, just output a
5861 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005862 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5863 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 out_char(' ');
5865 else
5866#endif
5867 out_char(ScreenLines[LineOffset[screen_row - 1]
5868 + (Columns - 1)]);
5869 /* force a redraw of the first char on the next line */
5870 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5871 screen_start(); /* don't know where cursor is now */
5872 }
5873 }
5874
5875 col = 0;
5876 off = (unsigned)(current_ScreenLine - ScreenLines);
5877#ifdef FEAT_RIGHTLEFT
5878 if (wp->w_p_rl)
5879 {
5880 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5881 off += col;
5882 }
5883#endif
5884
5885 /* reset the drawing state for the start of a wrapped line */
5886 draw_state = WL_START;
5887 saved_n_extra = n_extra;
5888 saved_p_extra = p_extra;
5889 saved_c_extra = c_extra;
5890 saved_char_attr = char_attr;
5891 n_extra = 0;
5892 lcs_prec_todo = lcs_prec;
5893#ifdef FEAT_LINEBREAK
5894# ifdef FEAT_DIFF
5895 if (filler_todo <= 0)
5896# endif
5897 need_showbreak = TRUE;
5898#endif
5899#ifdef FEAT_DIFF
5900 --filler_todo;
5901 /* When the filler lines are actually below the last line of the
5902 * file, don't draw the line itself, break here. */
5903 if (filler_todo == 0 && wp->w_botfill)
5904 break;
5905#endif
5906 }
5907
5908 } /* for every character in the line */
5909
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005910#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005911 /* After an empty line check first word for capital. */
5912 if (*skipwhite(line) == NUL)
5913 {
5914 capcol_lnum = lnum + 1;
5915 cap_col = 0;
5916 }
5917#endif
5918
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005919 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 return row;
5921}
5922
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005923#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005924static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005925
5926/*
5927 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005928 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005929 */
5930 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005931comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005932{
5933 int i;
5934
5935 for (i = 0; i < Screen_mco; ++i)
5936 {
5937 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5938 return TRUE;
5939 if (ScreenLinesC[i][off_from] == 0)
5940 break;
5941 }
5942 return FALSE;
5943}
5944#endif
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946/*
5947 * Check whether the given character needs redrawing:
5948 * - the (first byte of the) character is different
5949 * - the attributes are different
5950 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005951 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 */
5953 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005954char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 if (cols > 0
5957 && ((ScreenLines[off_from] != ScreenLines[off_to]
5958 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5959
5960#ifdef FEAT_MBYTE
5961 || (enc_dbcs != 0
5962 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5963 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5964 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5965 : (cols > 1 && ScreenLines[off_from + 1]
5966 != ScreenLines[off_to + 1])))
5967 || (enc_utf8
5968 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5969 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005970 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005971 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5972 && ScreenLines[off_from + 1]
5973 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
5975 ))
5976 return TRUE;
5977 return FALSE;
5978}
5979
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005980#if defined(FEAT_TERMINAL) || defined(PROTO)
5981/*
5982 * Return the index in ScreenLines[] for the current screen line.
5983 */
5984 int
5985screen_get_current_line_off()
5986{
5987 return (int)(current_ScreenLine - ScreenLines);
5988}
5989#endif
5990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991/*
5992 * Move one "cooked" screen line to the screen, but only the characters that
5993 * have actually changed. Handle insert/delete character.
5994 * "coloff" gives the first column on the screen for this line.
5995 * "endcol" gives the columns where valid characters are.
5996 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5997 * needs to be cleared, negative otherwise.
5998 * "rlflag" is TRUE in a rightleft window:
5999 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6000 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6001 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006002 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006003screen_line(
6004 int row,
6005 int coloff,
6006 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006007 int clear_width,
6008 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 unsigned off_from;
6011 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006012#ifdef FEAT_MBYTE
6013 unsigned max_off_from;
6014 unsigned max_off_to;
6015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006017#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 int hl;
6019#endif
6020 int force = FALSE; /* force update rest of the line */
6021 int redraw_this /* bool: does character need redraw? */
6022#ifdef FEAT_GUI
6023 = TRUE /* For GUI when while-loop empty */
6024#endif
6025 ;
6026 int redraw_next; /* redraw_this for next character */
6027#ifdef FEAT_MBYTE
6028 int clear_next = FALSE;
6029 int char_cells; /* 1: normal char */
6030 /* 2: occupies two display cells */
6031# define CHAR_CELLS char_cells
6032#else
6033# define CHAR_CELLS 1
6034#endif
6035
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006036 /* Check for illegal row and col, just in case. */
6037 if (row >= Rows)
6038 row = Rows - 1;
6039 if (endcol > Columns)
6040 endcol = Columns;
6041
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042# ifdef FEAT_CLIPBOARD
6043 clip_may_clear_selection(row, row);
6044# endif
6045
6046 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6047 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006048#ifdef FEAT_MBYTE
6049 max_off_from = off_from + screen_Columns;
6050 max_off_to = LineOffset[row] + screen_Columns;
6051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053#ifdef FEAT_RIGHTLEFT
6054 if (rlflag)
6055 {
6056 /* Clear rest first, because it's left of the text. */
6057 if (clear_width > 0)
6058 {
6059 while (col <= endcol && ScreenLines[off_to] == ' '
6060 && ScreenAttrs[off_to] == 0
6061# ifdef FEAT_MBYTE
6062 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6063# endif
6064 )
6065 {
6066 ++off_to;
6067 ++col;
6068 }
6069 if (col <= endcol)
6070 screen_fill(row, row + 1, col + coloff,
6071 endcol + coloff + 1, ' ', ' ', 0);
6072 }
6073 col = endcol + 1;
6074 off_to = LineOffset[row] + col + coloff;
6075 off_from += col;
6076 endcol = (clear_width > 0 ? clear_width : -clear_width);
6077 }
6078#endif /* FEAT_RIGHTLEFT */
6079
6080 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6081
6082 while (col < endcol)
6083 {
6084#ifdef FEAT_MBYTE
6085 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006086 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 else
6088 char_cells = 1;
6089#endif
6090
6091 redraw_this = redraw_next;
6092 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6093 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6094
6095#ifdef FEAT_GUI
6096 /* If the next character was bold, then redraw the current character to
6097 * remove any pixels that might have spilt over into us. This only
6098 * happens in the GUI.
6099 */
6100 if (redraw_next && gui.in_use)
6101 {
6102 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006103 if (hl > HL_ALL)
6104 hl = syn_attr2attr(hl);
6105 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 redraw_this = TRUE;
6107 }
6108#endif
6109
6110 if (redraw_this)
6111 {
6112 /*
6113 * Special handling when 'xs' termcap flag set (hpterm):
6114 * Attributes for characters are stored at the position where the
6115 * cursor is when writing the highlighting code. The
6116 * start-highlighting code must be written with the cursor on the
6117 * first highlighted character. The stop-highlighting code must
6118 * be written with the cursor just after the last highlighted
6119 * character.
6120 * Overwriting a character doesn't remove it's highlighting. Need
6121 * to clear the rest of the line, and force redrawing it
6122 * completely.
6123 */
6124 if ( p_wiv
6125 && !force
6126#ifdef FEAT_GUI
6127 && !gui.in_use
6128#endif
6129 && ScreenAttrs[off_to] != 0
6130 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6131 {
6132 /*
6133 * Need to remove highlighting attributes here.
6134 */
6135 windgoto(row, col + coloff);
6136 out_str(T_CE); /* clear rest of this screen line */
6137 screen_start(); /* don't know where cursor is now */
6138 force = TRUE; /* force redraw of rest of the line */
6139 redraw_next = TRUE; /* or else next char would miss out */
6140
6141 /*
6142 * If the previous character was highlighted, need to stop
6143 * highlighting at this character.
6144 */
6145 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6146 {
6147 screen_attr = ScreenAttrs[off_to - 1];
6148 term_windgoto(row, col + coloff);
6149 screen_stop_highlight();
6150 }
6151 else
6152 screen_attr = 0; /* highlighting has stopped */
6153 }
6154#ifdef FEAT_MBYTE
6155 if (enc_dbcs != 0)
6156 {
6157 /* Check if overwriting a double-byte with a single-byte or
6158 * the other way around requires another character to be
6159 * redrawn. For UTF-8 this isn't needed, because comparing
6160 * ScreenLinesUC[] is sufficient. */
6161 if (char_cells == 1
6162 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006163 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
6165 /* Writing a single-cell character over a double-cell
6166 * character: need to redraw the next cell. */
6167 ScreenLines[off_to + 1] = 0;
6168 redraw_next = TRUE;
6169 }
6170 else if (char_cells == 2
6171 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006172 && (*mb_off2cells)(off_to, max_off_to) == 1
6173 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 /* Writing the second half of a double-cell character over
6176 * a double-cell character: need to redraw the second
6177 * cell. */
6178 ScreenLines[off_to + 2] = 0;
6179 redraw_next = TRUE;
6180 }
6181
6182 if (enc_dbcs == DBCS_JPNU)
6183 ScreenLines2[off_to] = ScreenLines2[off_from];
6184 }
6185 /* When writing a single-width character over a double-width
6186 * character and at the end of the redrawn text, need to clear out
6187 * the right halve of the old character.
6188 * Also required when writing the right halve of a double-width
6189 * char over the left halve of an existing one. */
6190 if (has_mbyte && col + char_cells == endcol
6191 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006192 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006194 && (*mb_off2cells)(off_to, max_off_to) == 1
6195 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 clear_next = TRUE;
6197#endif
6198
6199 ScreenLines[off_to] = ScreenLines[off_from];
6200#ifdef FEAT_MBYTE
6201 if (enc_utf8)
6202 {
6203 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6204 if (ScreenLinesUC[off_from] != 0)
6205 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006206 int i;
6207
6208 for (i = 0; i < Screen_mco; ++i)
6209 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
6211 }
6212 if (char_cells == 2)
6213 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6214#endif
6215
6216#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006217 /* The bold trick makes a single column of pixels appear in the
6218 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * character should be redrawn too. This happens for our own GUI
6220 * and for some xterms. */
6221 if (
6222# ifdef FEAT_GUI
6223 gui.in_use
6224# endif
6225# if defined(FEAT_GUI) && defined(UNIX)
6226 ||
6227# endif
6228# ifdef UNIX
6229 term_is_xterm
6230# endif
6231 )
6232 {
6233 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006234 if (hl > HL_ALL)
6235 hl = syn_attr2attr(hl);
6236 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 redraw_next = TRUE;
6238 }
6239#endif
6240 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6241#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006242 /* For simplicity set the attributes of second half of a
6243 * double-wide character equal to the first half. */
6244 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006246
6247 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 else
6250#endif
6251 screen_char(off_to, row, col + coloff);
6252 }
6253 else if ( p_wiv
6254#ifdef FEAT_GUI
6255 && !gui.in_use
6256#endif
6257 && col + coloff > 0)
6258 {
6259 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6260 {
6261 /*
6262 * Don't output stop-highlight when moving the cursor, it will
6263 * stop the highlighting when it should continue.
6264 */
6265 screen_attr = 0;
6266 }
6267 else if (screen_attr != 0)
6268 screen_stop_highlight();
6269 }
6270
6271 off_to += CHAR_CELLS;
6272 off_from += CHAR_CELLS;
6273 col += CHAR_CELLS;
6274 }
6275
6276#ifdef FEAT_MBYTE
6277 if (clear_next)
6278 {
6279 /* Clear the second half of a double-wide character of which the left
6280 * half was overwritten with a single-wide character. */
6281 ScreenLines[off_to] = ' ';
6282 if (enc_utf8)
6283 ScreenLinesUC[off_to] = 0;
6284 screen_char(off_to, row, col + coloff);
6285 }
6286#endif
6287
6288 if (clear_width > 0
6289#ifdef FEAT_RIGHTLEFT
6290 && !rlflag
6291#endif
6292 )
6293 {
6294#ifdef FEAT_GUI
6295 int startCol = col;
6296#endif
6297
6298 /* blank out the rest of the line */
6299 while (col < clear_width && ScreenLines[off_to] == ' '
6300 && ScreenAttrs[off_to] == 0
6301#ifdef FEAT_MBYTE
6302 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6303#endif
6304 )
6305 {
6306 ++off_to;
6307 ++col;
6308 }
6309 if (col < clear_width)
6310 {
6311#ifdef FEAT_GUI
6312 /*
6313 * In the GUI, clearing the rest of the line may leave pixels
6314 * behind if the first character cleared was bold. Some bold
6315 * fonts spill over the left. In this case we redraw the previous
6316 * character too. If we didn't skip any blanks above, then we
6317 * only redraw if the character wasn't already redrawn anyway.
6318 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006319 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321 hl = ScreenAttrs[off_to];
6322 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006323 {
6324 int prev_cells = 1;
6325# ifdef FEAT_MBYTE
6326 if (enc_utf8)
6327 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6328 * that its width is 2. */
6329 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6330 else if (enc_dbcs != 0)
6331 {
6332 /* find previous character by counting from first
6333 * column and get its width. */
6334 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006335 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006336
6337 while (off < off_to)
6338 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006339 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006340 off += prev_cells;
6341 }
6342 }
6343
6344 if (enc_dbcs != 0 && prev_cells > 1)
6345 screen_char_2(off_to - prev_cells, row,
6346 col + coloff - prev_cells);
6347 else
6348# endif
6349 screen_char(off_to - prev_cells, row,
6350 col + coloff - prev_cells);
6351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353#endif
6354 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6355 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006356#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 off_to += clear_width - col;
6358 col = clear_width;
6359#endif
6360 }
6361 }
6362
6363 if (clear_width > 0)
6364 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006365#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 /* For a window that's left of another, draw the separator char. */
6367 if (col + coloff < Columns)
6368 {
6369 int c;
6370
6371 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006372 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006374 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6375 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376# endif
6377 || ScreenAttrs[off_to] != hl)
6378 {
6379 ScreenLines[off_to] = c;
6380 ScreenAttrs[off_to] = hl;
6381# ifdef FEAT_MBYTE
6382 if (enc_utf8)
6383 {
6384 if (c >= 0x80)
6385 {
6386 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006387 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 }
6389 else
6390 ScreenLinesUC[off_to] = 0;
6391 }
6392# endif
6393 screen_char(off_to, row, col + coloff);
6394 }
6395 }
6396 else
6397#endif
6398 LineWraps[row] = FALSE;
6399 }
6400}
6401
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006402#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006404 * Mirror text "str" for right-left displaying.
6405 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006408rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 char_u *p1, *p2;
6411 int t;
6412
6413 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6414 {
6415 t = *p1;
6416 *p1 = *p2;
6417 *p2 = t;
6418 }
6419}
6420#endif
6421
6422#if defined(FEAT_WINDOWS) || defined(PROTO)
6423/*
6424 * mark all status lines for redraw; used after first :cd
6425 */
6426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006427status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428{
6429 win_T *wp;
6430
Bram Moolenaar29323592016-07-24 22:04:11 +02006431 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (wp->w_status_height)
6433 {
6434 wp->w_redr_status = TRUE;
6435 redraw_later(VALID);
6436 }
6437}
6438
6439/*
6440 * mark all status lines of the current buffer for redraw
6441 */
6442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006443status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 win_T *wp;
6446
Bram Moolenaar29323592016-07-24 22:04:11 +02006447 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6449 {
6450 wp->w_redr_status = TRUE;
6451 redraw_later(VALID);
6452 }
6453}
6454
6455/*
6456 * Redraw all status lines that need to be redrawn.
6457 */
6458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006459redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 win_T *wp;
6462
Bram Moolenaar29323592016-07-24 22:04:11 +02006463 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 if (wp->w_redr_status)
6465 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006466 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006467 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468}
6469#endif
6470
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006471#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * Redraw all status lines at the bottom of frame "frp".
6474 */
6475 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006476win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 if (frp->fr_layout == FR_LEAF)
6479 frp->fr_win->w_redr_status = TRUE;
6480 else if (frp->fr_layout == FR_ROW)
6481 {
6482 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6483 win_redraw_last_status(frp);
6484 }
6485 else /* frp->fr_layout == FR_COL */
6486 {
6487 frp = frp->fr_child;
6488 while (frp->fr_next != NULL)
6489 frp = frp->fr_next;
6490 win_redraw_last_status(frp);
6491 }
6492}
6493#endif
6494
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006495#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496/*
6497 * Draw the verticap separator right of window "wp" starting with line "row".
6498 */
6499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006500draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 int hl;
6503 int c;
6504
6505 if (wp->w_vsep_width)
6506 {
6507 /* draw the vertical separator right of this window */
6508 c = fillchar_vsep(&hl);
6509 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6510 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6511 c, ' ', hl);
6512 }
6513}
6514#endif
6515
6516#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006517static int status_match_len(expand_T *xp, char_u *s);
6518static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
6520/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006521 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 */
6523 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006524status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 int len = 0;
6527
6528#ifdef FEAT_MENU
6529 int emenu = (xp->xp_context == EXPAND_MENUS
6530 || xp->xp_context == EXPAND_MENUNAMES);
6531
6532 /* Check for menu separators - replace with '|'. */
6533 if (emenu && menu_is_separator(s))
6534 return 1;
6535#endif
6536
6537 while (*s != NUL)
6538 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006539 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006540 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006541 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
6543
6544 return len;
6545}
6546
6547/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006548 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006549 * These are backslashes used for escaping. Do show backslashes in help tags.
6550 */
6551 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006552skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006553{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006554 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006555#ifdef FEAT_MENU
6556 || ((xp->xp_context == EXPAND_MENUS
6557 || xp->xp_context == EXPAND_MENUNAMES)
6558 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6559#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006560 )
6561 {
6562#ifndef BACKSLASH_IN_FILENAME
6563 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6564 return 2;
6565#endif
6566 return 1;
6567 }
6568 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006569}
6570
6571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 * Show wildchar matches in the status line.
6573 * Show at least the "match" item.
6574 * We start at item 'first_match' in the list and show all matches that fit.
6575 *
6576 * If inversion is possible we use it. Else '=' characters are used.
6577 */
6578 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006579win_redr_status_matches(
6580 expand_T *xp,
6581 int num_matches,
6582 char_u **matches, /* list of matches */
6583 int match,
6584 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6587 int row;
6588 char_u *buf;
6589 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006590 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 int fillchar;
6592 int attr;
6593 int i;
6594 int highlight = TRUE;
6595 char_u *selstart = NULL;
6596 int selstart_col = 0;
6597 char_u *selend = NULL;
6598 static int first_match = 0;
6599 int add_left = FALSE;
6600 char_u *s;
6601#ifdef FEAT_MENU
6602 int emenu;
6603#endif
6604#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6605 int l;
6606#endif
6607
6608 if (matches == NULL) /* interrupted completion? */
6609 return;
6610
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006611#ifdef FEAT_MBYTE
6612 if (has_mbyte)
6613 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6614 else
6615#endif
6616 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (buf == NULL)
6618 return;
6619
6620 if (match == -1) /* don't show match but original text */
6621 {
6622 match = 0;
6623 highlight = FALSE;
6624 }
6625 /* count 1 for the ending ">" */
6626 clen = status_match_len(xp, L_MATCH(match)) + 3;
6627 if (match == 0)
6628 first_match = 0;
6629 else if (match < first_match)
6630 {
6631 /* jumping left, as far as we can go */
6632 first_match = match;
6633 add_left = TRUE;
6634 }
6635 else
6636 {
6637 /* check if match fits on the screen */
6638 for (i = first_match; i < match; ++i)
6639 clen += status_match_len(xp, L_MATCH(i)) + 2;
6640 if (first_match > 0)
6641 clen += 2;
6642 /* jumping right, put match at the left */
6643 if ((long)clen > Columns)
6644 {
6645 first_match = match;
6646 /* if showing the last match, we can add some on the left */
6647 clen = 2;
6648 for (i = match; i < num_matches; ++i)
6649 {
6650 clen += status_match_len(xp, L_MATCH(i)) + 2;
6651 if ((long)clen >= Columns)
6652 break;
6653 }
6654 if (i == num_matches)
6655 add_left = TRUE;
6656 }
6657 }
6658 if (add_left)
6659 while (first_match > 0)
6660 {
6661 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6662 if ((long)clen >= Columns)
6663 break;
6664 --first_match;
6665 }
6666
6667 fillchar = fillchar_status(&attr, TRUE);
6668
6669 if (first_match == 0)
6670 {
6671 *buf = NUL;
6672 len = 0;
6673 }
6674 else
6675 {
6676 STRCPY(buf, "< ");
6677 len = 2;
6678 }
6679 clen = len;
6680
6681 i = first_match;
6682 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6683 {
6684 if (i == match)
6685 {
6686 selstart = buf + len;
6687 selstart_col = clen;
6688 }
6689
6690 s = L_MATCH(i);
6691 /* Check for menu separators - replace with '|' */
6692#ifdef FEAT_MENU
6693 emenu = (xp->xp_context == EXPAND_MENUS
6694 || xp->xp_context == EXPAND_MENUNAMES);
6695 if (emenu && menu_is_separator(s))
6696 {
6697 STRCPY(buf + len, transchar('|'));
6698 l = (int)STRLEN(buf + len);
6699 len += l;
6700 clen += l;
6701 }
6702 else
6703#endif
6704 for ( ; *s != NUL; ++s)
6705 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006706 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 clen += ptr2cells(s);
6708#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006709 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
6711 STRNCPY(buf + len, s, l);
6712 s += l - 1;
6713 len += l;
6714 }
6715 else
6716#endif
6717 {
6718 STRCPY(buf + len, transchar_byte(*s));
6719 len += (int)STRLEN(buf + len);
6720 }
6721 }
6722 if (i == match)
6723 selend = buf + len;
6724
6725 *(buf + len++) = ' ';
6726 *(buf + len++) = ' ';
6727 clen += 2;
6728 if (++i == num_matches)
6729 break;
6730 }
6731
6732 if (i != num_matches)
6733 {
6734 *(buf + len++) = '>';
6735 ++clen;
6736 }
6737
6738 buf[len] = NUL;
6739
6740 row = cmdline_row - 1;
6741 if (row >= 0)
6742 {
6743 if (wild_menu_showing == 0)
6744 {
6745 if (msg_scrolled > 0)
6746 {
6747 /* Put the wildmenu just above the command line. If there is
6748 * no room, scroll the screen one line up. */
6749 if (cmdline_row == Rows - 1)
6750 {
6751 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6752 ++msg_scrolled;
6753 }
6754 else
6755 {
6756 ++cmdline_row;
6757 ++row;
6758 }
6759 wild_menu_showing = WM_SCROLLED;
6760 }
6761 else
6762 {
6763 /* Create status line if needed by setting 'laststatus' to 2.
6764 * Set 'winminheight' to zero to avoid that the window is
6765 * resized. */
6766 if (lastwin->w_status_height == 0)
6767 {
6768 save_p_ls = p_ls;
6769 save_p_wmh = p_wmh;
6770 p_ls = 2;
6771 p_wmh = 0;
6772 last_status(FALSE);
6773 }
6774 wild_menu_showing = WM_SHOWN;
6775 }
6776 }
6777
6778 screen_puts(buf, row, 0, attr);
6779 if (selstart != NULL && highlight)
6780 {
6781 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006782 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784
6785 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6786 }
6787
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006788#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 win_redraw_last_status(topframe);
6790#else
6791 lastwin->w_redr_status = TRUE;
6792#endif
6793 vim_free(buf);
6794}
6795#endif
6796
6797#if defined(FEAT_WINDOWS) || defined(PROTO)
6798/*
6799 * Redraw the status line of window wp.
6800 *
6801 * If inversion is possible we use it. Else '=' characters are used.
6802 */
6803 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006804win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int row;
6807 char_u *p;
6808 int len;
6809 int fillchar;
6810 int attr;
6811 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006812 static int busy = FALSE;
6813
6814 /* It's possible to get here recursively when 'statusline' (indirectly)
6815 * invokes ":redrawstatus". Simply ignore the call then. */
6816 if (busy)
6817 return;
6818 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819
6820 wp->w_redr_status = FALSE;
6821 if (wp->w_status_height == 0)
6822 {
6823 /* no status line, can only be last window */
6824 redraw_cmdline = TRUE;
6825 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006826 else if (!redrawing()
6827#ifdef FEAT_INS_EXPAND
6828 /* don't update status line when popup menu is visible and may be
6829 * drawn over it */
6830 || pum_visible()
6831#endif
6832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
6834 /* Don't redraw right now, do it later. */
6835 wp->w_redr_status = TRUE;
6836 }
6837#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006838 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {
6840 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006841 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843#endif
6844 else
6845 {
6846 fillchar = fillchar_status(&attr, wp == curwin);
6847
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006848 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 p = NameBuff;
6850 len = (int)STRLEN(p);
6851
6852 if (wp->w_buffer->b_help
6853#ifdef FEAT_QUICKFIX
6854 || wp->w_p_pvw
6855#endif
6856 || bufIsChanged(wp->w_buffer)
6857 || wp->w_buffer->b_p_ro)
6858 *(p + len++) = ' ';
6859 if (wp->w_buffer->b_help)
6860 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006861 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 len += (int)STRLEN(p + len);
6863 }
6864#ifdef FEAT_QUICKFIX
6865 if (wp->w_p_pvw)
6866 {
6867 STRCPY(p + len, _("[Preview]"));
6868 len += (int)STRLEN(p + len);
6869 }
6870#endif
6871 if (bufIsChanged(wp->w_buffer))
6872 {
6873 STRCPY(p + len, "[+]");
6874 len += 3;
6875 }
6876 if (wp->w_buffer->b_p_ro)
6877 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006878 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006879 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6883 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6884 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6885 if (this_ru_col <= 1)
6886 {
6887 p = (char_u *)"<"; /* No room for file name! */
6888 len = 1;
6889 }
6890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#ifdef FEAT_MBYTE
6892 if (has_mbyte)
6893 {
6894 int clen = 0, i;
6895
6896 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006897 clen = mb_string2cells(p, -1);
6898
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 /* Find first character that will fit.
6900 * Going from start to end is much faster for DBCS. */
6901 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006902 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 clen -= (*mb_ptr2cells)(p + i);
6904 len = clen;
6905 if (i > 0)
6906 {
6907 p = p + i - 1;
6908 *p = '<';
6909 ++len;
6910 }
6911
6912 }
6913 else
6914#endif
6915 if (len > this_ru_col - 1)
6916 {
6917 p += len - (this_ru_col - 1);
6918 *p = '<';
6919 len = this_ru_col - 1;
6920 }
6921
6922 row = W_WINROW(wp) + wp->w_height;
6923 screen_puts(p, row, W_WINCOL(wp), attr);
6924 screen_fill(row, row + 1, len + W_WINCOL(wp),
6925 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6926
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006927 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6929 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6930 - 1 + W_WINCOL(wp)), attr);
6931
6932#ifdef FEAT_CMDL_INFO
6933 win_redr_ruler(wp, TRUE);
6934#endif
6935 }
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 /*
6938 * May need to draw the character below the vertical separator.
6939 */
6940 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6941 {
6942 if (stl_connected(wp))
6943 fillchar = fillchar_status(&attr, wp == curwin);
6944 else
6945 fillchar = fillchar_vsep(&attr);
6946 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6947 attr);
6948 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006949 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950}
6951
Bram Moolenaar238a5642006-02-21 22:12:05 +00006952#ifdef FEAT_STL_OPT
6953/*
6954 * Redraw the status line according to 'statusline' and take care of any
6955 * errors encountered.
6956 */
6957 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006958redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006959{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006960 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006961 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006962
6963 /* When called recursively return. This can happen when the statusline
6964 * contains an expression that triggers a redraw. */
6965 if (entered)
6966 return;
6967 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006968
Bram Moolenaara742e082016-04-05 21:10:38 +02006969 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006970 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006971 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006972 {
6973 /* When there is an error disable the statusline, otherwise the
6974 * display is messed up with errors and a redraw triggers the problem
6975 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006976 set_string_option_direct((char_u *)"statusline", -1,
6977 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006978 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006979 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006980 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006981 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006982}
6983#endif
6984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985/*
6986 * Return TRUE if the status line of window "wp" is connected to the status
6987 * line of the window right of it. If not, then it's a vertical separator.
6988 * Only call if (wp->w_vsep_width != 0).
6989 */
6990 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006991stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 frame_T *fr;
6994
6995 fr = wp->w_frame;
6996 while (fr->fr_parent != NULL)
6997 {
6998 if (fr->fr_parent->fr_layout == FR_COL)
6999 {
7000 if (fr->fr_next != NULL)
7001 break;
7002 }
7003 else
7004 {
7005 if (fr->fr_next != NULL)
7006 return TRUE;
7007 }
7008 fr = fr->fr_parent;
7009 }
7010 return FALSE;
7011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013#endif /* FEAT_WINDOWS */
7014
7015#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7016/*
7017 * Get the value to show for the language mappings, active 'keymap'.
7018 */
7019 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007020get_keymap_str(
7021 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007022 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007023 char_u *buf, /* buffer for the result */
7024 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 char_u *p;
7027
7028 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7029 return FALSE;
7030
7031 {
7032#ifdef FEAT_EVAL
7033 buf_T *old_curbuf = curbuf;
7034 win_T *old_curwin = curwin;
7035 char_u *s;
7036
7037 curbuf = wp->w_buffer;
7038 curwin = wp;
7039 STRCPY(buf, "b:keymap_name"); /* must be writable */
7040 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007041 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 --emsg_skip;
7043 curbuf = old_curbuf;
7044 curwin = old_curwin;
7045 if (p == NULL || *p == NUL)
7046#endif
7047 {
7048#ifdef FEAT_KEYMAP
7049 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7050 p = wp->w_buffer->b_p_keymap;
7051 else
7052#endif
7053 p = (char_u *)"lang";
7054 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007055 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 buf[0] = NUL;
7057#ifdef FEAT_EVAL
7058 vim_free(s);
7059#endif
7060 }
7061 return buf[0] != NUL;
7062}
7063#endif
7064
7065#if defined(FEAT_STL_OPT) || defined(PROTO)
7066/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007067 * Redraw the status line or ruler of window "wp".
7068 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 */
7070 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007071win_redr_custom(
7072 win_T *wp,
7073 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007075 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 int attr;
7077 int curattr;
7078 int row;
7079 int col = 0;
7080 int maxwidth;
7081 int width;
7082 int n;
7083 int len;
7084 int fillchar;
7085 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007086 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007088 struct stl_hlrec hltab[STL_MAX_ITEM];
7089 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007090 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007091 win_T *ewp;
7092 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar1d633412013-12-11 15:52:01 +01007094 /* There is a tiny chance that this gets called recursively: When
7095 * redrawing a status line triggers redrawing the ruler or tabline.
7096 * Avoid trouble by not allowing recursion. */
7097 if (entered)
7098 return;
7099 entered = TRUE;
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007102 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007104 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007105 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007106 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007107 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007108 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007109 maxwidth = Columns;
7110# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007111 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007114 else
7115 {
7116 row = W_WINROW(wp) + wp->w_height;
7117 fillchar = fillchar_status(&attr, wp == curwin);
7118 maxwidth = W_WIDTH(wp);
7119
7120 if (draw_ruler)
7121 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007122 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007123 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007124 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007125 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007126 if (*++stl == '-')
7127 stl++;
7128 if (atoi((char *)stl))
7129 while (VIM_ISDIGIT(*stl))
7130 stl++;
7131 if (*stl++ != '(')
7132 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007134#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 col = ru_col - (Columns - W_WIDTH(wp));
7136 if (col < (W_WIDTH(wp) + 1) / 2)
7137 col = (W_WIDTH(wp) + 1) / 2;
7138#else
7139 col = ru_col;
7140 if (col > (Columns + 1) / 2)
7141 col = (Columns + 1) / 2;
7142#endif
7143 maxwidth = W_WIDTH(wp) - col;
7144#ifdef FEAT_WINDOWS
7145 if (!wp->w_status_height)
7146#endif
7147 {
7148 row = Rows - 1;
7149 --maxwidth; /* writing in last column may cause scrolling */
7150 fillchar = ' ';
7151 attr = 0;
7152 }
7153
7154# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007155 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156# endif
7157 }
7158 else
7159 {
7160 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007161 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007162 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007163 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007165 use_sandbox = was_set_insecurely((char_u *)"statusline",
7166 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007167# endif
7168 }
7169
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007170#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007171 col += W_WINCOL(wp);
7172#endif
7173 }
7174
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007176 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar61452852011-02-01 18:01:11 +01007178 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7179 * the cursor away and back. */
7180 ewp = wp == NULL ? curwin : wp;
7181 p_crb_save = ewp->w_p_crb;
7182 ewp->w_p_crb = FALSE;
7183
Bram Moolenaar362f3562009-11-03 16:20:34 +00007184 /* Make a copy, because the statusline may include a function call that
7185 * might change the option value and free the memory. */
7186 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007187 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007188 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007190 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007191 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007193 /* Make all characters printable. */
7194 p = transstr(buf);
7195 if (p != NULL)
7196 {
7197 vim_strncpy(buf, p, sizeof(buf) - 1);
7198 vim_free(p);
7199 }
7200
7201 /* fill up with "fillchar" */
7202 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007203 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {
7205#ifdef FEAT_MBYTE
7206 len += (*mb_char2bytes)(fillchar, buf + len);
7207#else
7208 buf[len++] = fillchar;
7209#endif
7210 ++width;
7211 }
7212 buf[len] = NUL;
7213
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007214 /*
7215 * Draw each snippet with the specified highlighting.
7216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 curattr = attr;
7218 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007219 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007221 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 screen_puts_len(p, len, row, col, curattr);
7223 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007224 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007226 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007228 else if (hltab[n].userhl < 0)
7229 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007231 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007232 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233#endif
7234 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007235 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007238
7239 if (wp == NULL)
7240 {
7241 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7242 col = 0;
7243 len = 0;
7244 p = buf;
7245 fillchar = 0;
7246 for (n = 0; tabtab[n].start != NULL; n++)
7247 {
7248 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7249 while (col < len)
7250 TabPageIdxs[col++] = fillchar;
7251 p = tabtab[n].start;
7252 fillchar = tabtab[n].userhl;
7253 }
7254 while (col < Columns)
7255 TabPageIdxs[col++] = fillchar;
7256 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007257
7258theend:
7259 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262#endif /* FEAT_STL_OPT */
7263
7264/*
7265 * Output a single character directly to the screen and update ScreenLines.
7266 */
7267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007268screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 char_u buf[MB_MAXBYTES + 1];
7271
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007272#ifdef FEAT_MBYTE
7273 if (has_mbyte)
7274 buf[(*mb_char2bytes)(c, buf)] = NUL;
7275 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007277 {
7278 buf[0] = c;
7279 buf[1] = NUL;
7280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 screen_puts(buf, row, col, attr);
7282}
7283
7284/*
7285 * Get a single character directly from ScreenLines into "bytes[]".
7286 * Also return its attribute in *attrp;
7287 */
7288 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007289screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 unsigned off;
7292
7293 /* safety check */
7294 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7295 {
7296 off = LineOffset[row] + col;
7297 *attrp = ScreenAttrs[off];
7298 bytes[0] = ScreenLines[off];
7299 bytes[1] = NUL;
7300
7301#ifdef FEAT_MBYTE
7302 if (enc_utf8 && ScreenLinesUC[off] != 0)
7303 bytes[utfc_char2bytes(off, bytes)] = NUL;
7304 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7305 {
7306 bytes[0] = ScreenLines[off];
7307 bytes[1] = ScreenLines2[off];
7308 bytes[2] = NUL;
7309 }
7310 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7311 {
7312 bytes[1] = ScreenLines[off + 1];
7313 bytes[2] = NUL;
7314 }
7315#endif
7316 }
7317}
7318
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007319#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007320static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007321
7322/*
7323 * Return TRUE if composing characters for screen posn "off" differs from
7324 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007325 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007326 */
7327 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007328screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007329{
7330 int i;
7331
7332 for (i = 0; i < Screen_mco; ++i)
7333 {
7334 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7335 return TRUE;
7336 if (u8cc[i] == 0)
7337 break;
7338 }
7339 return FALSE;
7340}
7341#endif
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343/*
7344 * Put string '*text' on the screen at position 'row' and 'col', with
7345 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7346 * Note: only outputs within one row, message is truncated at screen boundary!
7347 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7348 */
7349 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007350screen_puts(
7351 char_u *text,
7352 int row,
7353 int col,
7354 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 screen_puts_len(text, -1, row, col, attr);
7357}
7358
7359/*
7360 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7361 * a NUL.
7362 */
7363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007364screen_puts_len(
7365 char_u *text,
7366 int textlen,
7367 int row,
7368 int col,
7369 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371 unsigned off;
7372 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007373 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 int c;
7375#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007376 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 int mbyte_blen = 1;
7378 int mbyte_cells = 1;
7379 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007380 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 int clear_next_cell = FALSE;
7382# ifdef FEAT_ARABIC
7383 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007384 int pc, nc, nc1;
7385 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386# endif
7387#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007388#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7389 int force_redraw_this;
7390 int force_redraw_next = FALSE;
7391#endif
7392 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
7394 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7395 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007396 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
Bram Moolenaarc236c162008-07-13 17:41:49 +00007398#ifdef FEAT_MBYTE
7399 /* When drawing over the right halve of a double-wide char clear out the
7400 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007401 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007402# ifdef FEAT_GUI
7403 && !gui.in_use
7404# endif
7405 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007406 {
7407 ScreenLines[off - 1] = ' ';
7408 ScreenAttrs[off - 1] = 0;
7409 if (enc_utf8)
7410 {
7411 ScreenLinesUC[off - 1] = 0;
7412 ScreenLinesC[0][off - 1] = 0;
7413 }
7414 /* redraw the previous cell, make it empty */
7415 screen_char(off - 1, row, col - 1);
7416 /* force the cell at "col" to be redrawn */
7417 force_redraw_next = TRUE;
7418 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007419#endif
7420
Bram Moolenaar367329b2007-08-30 11:53:22 +00007421#ifdef FEAT_MBYTE
7422 max_off = LineOffset[row] + screen_Columns;
7423#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007424 while (col < screen_Columns
7425 && (len < 0 || (int)(ptr - text) < len)
7426 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
7428 c = *ptr;
7429#ifdef FEAT_MBYTE
7430 /* check if this is the first byte of a multibyte */
7431 if (has_mbyte)
7432 {
7433 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007434 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007436 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7438 mbyte_cells = 1;
7439 else if (enc_dbcs != 0)
7440 mbyte_cells = mbyte_blen;
7441 else /* enc_utf8 */
7442 {
7443 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007444 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 (int)((text + len) - ptr));
7446 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007447 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007449# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 /* Non-BMP character: display as ? or fullwidth ?. */
7451 if (u8c >= 0x10000)
7452 {
7453 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7454 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007455 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458# ifdef FEAT_ARABIC
7459 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7460 {
7461 /* Do Arabic shaping. */
7462 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7463 {
7464 /* Past end of string to be displayed. */
7465 nc = NUL;
7466 nc1 = NUL;
7467 }
7468 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007469 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007470 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7471 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007472 nc1 = pcc[0];
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 pc = prev_c;
7475 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007476 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
7478 else
7479 prev_c = u8c;
7480# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007481 if (col + mbyte_cells > screen_Columns)
7482 {
7483 /* Only 1 cell left, but character requires 2 cells:
7484 * display a '>' in the last column to avoid wrapping. */
7485 c = '>';
7486 mbyte_cells = 1;
7487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489 }
7490#endif
7491
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007492#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7493 force_redraw_this = force_redraw_next;
7494 force_redraw_next = FALSE;
7495#endif
7496
7497 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498#ifdef FEAT_MBYTE
7499 || (mbyte_cells == 2
7500 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7501 || (enc_dbcs == DBCS_JPNU
7502 && c == 0x8e
7503 && ScreenLines2[off] != ptr[1])
7504 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007505 && (ScreenLinesUC[off] !=
7506 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7507 || (ScreenLinesUC[off] != 0
7508 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#endif
7510 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007511 || exmode_active;
7512
7513 if (need_redraw
7514#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7515 || force_redraw_this
7516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 )
7518 {
7519#if defined(FEAT_GUI) || defined(UNIX)
7520 /* The bold trick makes a single row of pixels appear in the next
7521 * character. When a bold character is removed, the next
7522 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007523 * and for some xterms. */
7524 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525# ifdef FEAT_GUI
7526 gui.in_use
7527# endif
7528# if defined(FEAT_GUI) && defined(UNIX)
7529 ||
7530# endif
7531# ifdef UNIX
7532 term_is_xterm
7533# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007534 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007536 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007538 if (n > HL_ALL)
7539 n = syn_attr2attr(n);
7540 if (n & HL_BOLD)
7541 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 }
7543#endif
7544#ifdef FEAT_MBYTE
7545 /* When at the end of the text and overwriting a two-cell
7546 * character with a one-cell character, need to clear the next
7547 * cell. Also when overwriting the left halve of a two-cell char
7548 * with the right halve of a two-cell char. Do this only once
7549 * (mb_off2cells() may return 2 on the right halve). */
7550 if (clear_next_cell)
7551 clear_next_cell = FALSE;
7552 else if (has_mbyte
7553 && (len < 0 ? ptr[mbyte_blen] == NUL
7554 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007557 && (*mb_off2cells)(off, max_off) == 1
7558 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 clear_next_cell = TRUE;
7560
7561 /* Make sure we never leave a second byte of a double-byte behind,
7562 * it confuses mb_off2cells(). */
7563 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007566 && (*mb_off2cells)(off, max_off) == 1
7567 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ScreenLines[off + mbyte_blen] = 0;
7569#endif
7570 ScreenLines[off] = c;
7571 ScreenAttrs[off] = attr;
7572#ifdef FEAT_MBYTE
7573 if (enc_utf8)
7574 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007575 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 ScreenLinesUC[off] = 0;
7577 else
7578 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007579 int i;
7580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007582 for (i = 0; i < Screen_mco; ++i)
7583 {
7584 ScreenLinesC[i][off] = u8cc[i];
7585 if (u8cc[i] == 0)
7586 break;
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 }
7589 if (mbyte_cells == 2)
7590 {
7591 ScreenLines[off + 1] = 0;
7592 ScreenAttrs[off + 1] = attr;
7593 }
7594 screen_char(off, row, col);
7595 }
7596 else if (mbyte_cells == 2)
7597 {
7598 ScreenLines[off + 1] = ptr[1];
7599 ScreenAttrs[off + 1] = attr;
7600 screen_char_2(off, row, col);
7601 }
7602 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7603 {
7604 ScreenLines2[off] = ptr[1];
7605 screen_char(off, row, col);
7606 }
7607 else
7608#endif
7609 screen_char(off, row, col);
7610 }
7611#ifdef FEAT_MBYTE
7612 if (has_mbyte)
7613 {
7614 off += mbyte_cells;
7615 col += mbyte_cells;
7616 ptr += mbyte_blen;
7617 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007618 {
7619 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007621 len = -1;
7622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 else
7625#endif
7626 {
7627 ++off;
7628 ++col;
7629 ++ptr;
7630 }
7631 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632
7633#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7634 /* If we detected the next character needs to be redrawn, but the text
7635 * doesn't extend up to there, update the character here. */
7636 if (force_redraw_next && col < screen_Columns)
7637 {
7638# ifdef FEAT_MBYTE
7639 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7640 screen_char_2(off, row, col);
7641 else
7642# endif
7643 screen_char(off, row, col);
7644 }
7645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646}
7647
7648#ifdef FEAT_SEARCH_EXTRA
7649/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007650 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 */
7652 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007653start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 if (p_hls && !no_hlsearch)
7656 {
7657 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007658 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007659# ifdef FEAT_RELTIME
7660 /* Set the time limit to 'redrawtime'. */
7661 profile_setlimit(p_rdt, &search_hl.tm);
7662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664}
7665
7666/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007667 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
7669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007670end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
7672 if (search_hl.rm.regprog != NULL)
7673 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007674 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 search_hl.rm.regprog = NULL;
7676 }
7677}
7678
7679/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007680 * Init for calling prepare_search_hl().
7681 */
7682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007683init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007684{
7685 matchitem_T *cur;
7686
7687 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7688 * match */
7689 cur = wp->w_match_head;
7690 while (cur != NULL)
7691 {
7692 cur->hl.rm = cur->match;
7693 if (cur->hlg_id == 0)
7694 cur->hl.attr = 0;
7695 else
7696 cur->hl.attr = syn_id2attr(cur->hlg_id);
7697 cur->hl.buf = wp->w_buffer;
7698 cur->hl.lnum = 0;
7699 cur->hl.first_lnum = 0;
7700# ifdef FEAT_RELTIME
7701 /* Set the time limit to 'redrawtime'. */
7702 profile_setlimit(p_rdt, &(cur->hl.tm));
7703# endif
7704 cur = cur->next;
7705 }
7706 search_hl.buf = wp->w_buffer;
7707 search_hl.lnum = 0;
7708 search_hl.first_lnum = 0;
7709 /* time limit is set at the toplevel, for all windows */
7710}
7711
7712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 * Advance to the match in window "wp" line "lnum" or past it.
7714 */
7715 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007716prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007718 matchitem_T *cur; /* points to the match list */
7719 match_T *shl; /* points to search_hl or a match */
7720 int shl_flag; /* flag to indicate whether search_hl
7721 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007722 int pos_inprogress; /* marks that position match search is
7723 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 int n;
7725
7726 /*
7727 * When using a multi-line pattern, start searching at the top
7728 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007729 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007731 cur = wp->w_match_head;
7732 shl_flag = FALSE;
7733 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007735 if (shl_flag == FALSE)
7736 {
7737 shl = &search_hl;
7738 shl_flag = TRUE;
7739 }
7740 else
7741 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 if (shl->rm.regprog != NULL
7743 && shl->lnum == 0
7744 && re_multiline(shl->rm.regprog))
7745 {
7746 if (shl->first_lnum == 0)
7747 {
7748# ifdef FEAT_FOLDING
7749 for (shl->first_lnum = lnum;
7750 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7751 if (hasFoldingWin(wp, shl->first_lnum - 1,
7752 NULL, NULL, TRUE, NULL))
7753 break;
7754# else
7755 shl->first_lnum = wp->w_topline;
7756# endif
7757 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007758 if (cur != NULL)
7759 cur->pos.cur = 0;
7760 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007762 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7763 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007765 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7766 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007767 pos_inprogress = cur == NULL || cur->pos.cur == 0
7768 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 if (shl->lnum != 0)
7770 {
7771 shl->first_lnum = shl->lnum
7772 + shl->rm.endpos[0].lnum
7773 - shl->rm.startpos[0].lnum;
7774 n = shl->rm.endpos[0].col;
7775 }
7776 else
7777 {
7778 ++shl->first_lnum;
7779 n = 0;
7780 }
7781 }
7782 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007783 if (shl != &search_hl && cur != NULL)
7784 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
7786}
7787
7788/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007789 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 * Uses shl->buf.
7791 * Sets shl->lnum and shl->rm contents.
7792 * Note: Assumes a previous match is always before "lnum", unless
7793 * shl->lnum is zero.
7794 * Careful: Any pointers for buffer lines will become invalid.
7795 */
7796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797next_search_hl(
7798 win_T *win,
7799 match_T *shl, /* points to search_hl or a match */
7800 linenr_T lnum,
7801 colnr_T mincol, /* minimal column for a match */
7802 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 linenr_T l;
7805 colnr_T matchcol;
7806 long nmatched;
7807
7808 if (shl->lnum != 0)
7809 {
7810 /* Check for three situations:
7811 * 1. If the "lnum" is below a previous match, start a new search.
7812 * 2. If the previous match includes "mincol", use it.
7813 * 3. Continue after the previous match.
7814 */
7815 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7816 if (lnum > l)
7817 shl->lnum = 0;
7818 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7819 return;
7820 }
7821
7822 /*
7823 * Repeat searching for a match until one is found that includes "mincol"
7824 * or none is found in this line.
7825 */
7826 called_emsg = FALSE;
7827 for (;;)
7828 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007829#ifdef FEAT_RELTIME
7830 /* Stop searching after passing the time limit. */
7831 if (profile_passed_limit(&(shl->tm)))
7832 {
7833 shl->lnum = 0; /* no match found in time */
7834 break;
7835 }
7836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 /* Three situations:
7838 * 1. No useful previous match: search from start of line.
7839 * 2. Not Vi compatible or empty match: continue at next character.
7840 * Break the loop if this is beyond the end of the line.
7841 * 3. Vi compatible searching: continue at end of previous match.
7842 */
7843 if (shl->lnum == 0)
7844 matchcol = 0;
7845 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7846 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007847 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007849 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007850
7851 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007852 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007853 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007855 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 shl->lnum = 0;
7857 break;
7858 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007859#ifdef FEAT_MBYTE
7860 if (has_mbyte)
7861 matchcol += mb_ptr2len(ml);
7862 else
7863#endif
7864 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 }
7866 else
7867 matchcol = shl->rm.endpos[0].col;
7868
7869 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007870 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007872 /* Remember whether shl->rm is using a copy of the regprog in
7873 * cur->match. */
7874 int regprog_is_copy = (shl != &search_hl && cur != NULL
7875 && shl == &cur->hl
7876 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007877 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007878
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7880 matchcol,
7881#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007882 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007883#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007884 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007885#endif
7886 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007887 /* Copy the regprog, in case it got freed and recompiled. */
7888 if (regprog_is_copy)
7889 cur->match.regprog = cur->hl.rm.regprog;
7890
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007891 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007892 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007893 /* Error while handling regexp: stop using this regexp. */
7894 if (shl == &search_hl)
7895 {
7896 /* don't free regprog in the match list, it's a copy */
7897 vim_regfree(shl->rm.regprog);
7898 SET_NO_HLSEARCH(TRUE);
7899 }
7900 shl->rm.regprog = NULL;
7901 shl->lnum = 0;
7902 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7903 message */
7904 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007905 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007906 }
7907 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007908 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007909 else
7910 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 if (nmatched == 0)
7912 {
7913 shl->lnum = 0; /* no match found */
7914 break;
7915 }
7916 if (shl->rm.startpos[0].lnum > 0
7917 || shl->rm.startpos[0].col >= mincol
7918 || nmatched > 1
7919 || shl->rm.endpos[0].col > mincol)
7920 {
7921 shl->lnum += shl->rm.startpos[0].lnum;
7922 break; /* useful match found */
7923 }
7924 }
7925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926
Bram Moolenaar85077472016-10-16 14:35:48 +02007927/*
7928 * If there is a match fill "shl" and return one.
7929 * Return zero otherwise.
7930 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007931 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007932next_search_hl_pos(
7933 match_T *shl, /* points to a match */
7934 linenr_T lnum,
7935 posmatch_T *posmatch, /* match positions */
7936 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007937{
7938 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007939 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007940
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7942 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007943 llpos_T *pos = &posmatch->pos[i];
7944
7945 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007947 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007949 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007951 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007952 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007953 /* if this match comes before the one at "found" then swap
7954 * them */
7955 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007957 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007958
Bram Moolenaar85077472016-10-16 14:35:48 +02007959 *pos = posmatch->pos[found];
7960 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 }
7962 }
7963 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007964 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007965 }
7966 }
7967 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007968 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007970 colnr_T start = posmatch->pos[found].col == 0
7971 ? 0 : posmatch->pos[found].col - 1;
7972 colnr_T end = posmatch->pos[found].col == 0
7973 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976 shl->rm.startpos[0].lnum = 0;
7977 shl->rm.startpos[0].col = start;
7978 shl->rm.endpos[0].lnum = 0;
7979 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007980 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 posmatch->cur = found + 1;
7982 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007984 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007986#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007989screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 attrentry_T *aep = NULL;
7992
7993 screen_attr = attr;
7994 if (full_screen
7995#ifdef WIN3264
7996 && termcap_active
7997#endif
7998 )
7999 {
8000#ifdef FEAT_GUI
8001 if (gui.in_use)
8002 {
8003 char buf[20];
8004
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008005 /* The GUI handles this internally. */
8006 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 OUT_STR(buf);
8008 }
8009 else
8010#endif
8011 {
8012 if (attr > HL_ALL) /* special HL attr. */
8013 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008014 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 aep = syn_cterm_attr2entry(attr);
8016 else
8017 aep = syn_term_attr2entry(attr);
8018 if (aep == NULL) /* did ":syntax clear" */
8019 attr = 0;
8020 else
8021 attr = aep->ae_attr;
8022 }
8023 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8024 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008025 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008026#ifdef FEAT_TERMGUICOLORS
8027 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008028 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008029#endif
8030 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008031#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 )
8033#endif
8034 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008035 /* If the Normal FG color has BOLD attribute and the new HL
8036 * has a FG color defined, clear BOLD. */
8037 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8039 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008040 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8041 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 out_str(T_US);
8043 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8044 out_str(T_CZH);
8045 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8046 out_str(T_MR);
8047
8048 /*
8049 * Output the color or start string after bold etc., in case the
8050 * bold etc. override the color setting.
8051 */
8052 if (aep != NULL)
8053 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008054#ifdef FEAT_TERMGUICOLORS
8055 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008057 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008058 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008059 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008060 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
8062 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008065 if (t_colors > 1)
8066 {
8067 if (aep->ae_u.cterm.fg_color)
8068 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8069 if (aep->ae_u.cterm.bg_color)
8070 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8071 }
8072 else
8073 {
8074 if (aep->ae_u.term.start != NULL)
8075 out_str(aep->ae_u.term.start);
8076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 }
8079 }
8080 }
8081}
8082
8083 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008084screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 int do_ME = FALSE; /* output T_ME code */
8087
8088 if (screen_attr != 0
8089#ifdef WIN3264
8090 && termcap_active
8091#endif
8092 )
8093 {
8094#ifdef FEAT_GUI
8095 if (gui.in_use)
8096 {
8097 char buf[20];
8098
8099 /* use internal GUI code */
8100 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8101 OUT_STR(buf);
8102 }
8103 else
8104#endif
8105 {
8106 if (screen_attr > HL_ALL) /* special HL attr. */
8107 {
8108 attrentry_T *aep;
8109
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008110 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
8112 /*
8113 * Assume that t_me restores the original colors!
8114 */
8115 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008116 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008117#ifdef FEAT_TERMGUICOLORS
8118 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008119 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8120 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008121#endif
8122 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008123#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008124 )
8125#endif
8126 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 do_ME = TRUE;
8128 }
8129 else
8130 {
8131 aep = syn_term_attr2entry(screen_attr);
8132 if (aep != NULL && aep->ae_u.term.stop != NULL)
8133 {
8134 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8135 do_ME = TRUE;
8136 else
8137 out_str(aep->ae_u.term.stop);
8138 }
8139 }
8140 if (aep == NULL) /* did ":syntax clear" */
8141 screen_attr = 0;
8142 else
8143 screen_attr = aep->ae_attr;
8144 }
8145
8146 /*
8147 * Often all ending-codes are equal to T_ME. Avoid outputting the
8148 * same sequence several times.
8149 */
8150 if (screen_attr & HL_STANDOUT)
8151 {
8152 if (STRCMP(T_SE, T_ME) == 0)
8153 do_ME = TRUE;
8154 else
8155 out_str(T_SE);
8156 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008157 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
8159 if (STRCMP(T_UE, T_ME) == 0)
8160 do_ME = TRUE;
8161 else
8162 out_str(T_UE);
8163 }
8164 if (screen_attr & HL_ITALIC)
8165 {
8166 if (STRCMP(T_CZR, T_ME) == 0)
8167 do_ME = TRUE;
8168 else
8169 out_str(T_CZR);
8170 }
8171 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8172 out_str(T_ME);
8173
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008174#ifdef FEAT_TERMGUICOLORS
8175 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008177 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008178 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008179 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008180 term_bg_rgb_color(cterm_normal_bg_gui_color);
8181 }
8182 else
8183#endif
8184 {
8185 if (t_colors > 1)
8186 {
8187 /* set Normal cterm colors */
8188 if (cterm_normal_fg_color != 0)
8189 term_fg_color(cterm_normal_fg_color - 1);
8190 if (cterm_normal_bg_color != 0)
8191 term_bg_color(cterm_normal_bg_color - 1);
8192 if (cterm_normal_fg_bold)
8193 out_str(T_MD);
8194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 }
8196 }
8197 }
8198 screen_attr = 0;
8199}
8200
8201/*
8202 * Reset the colors for a cterm. Used when leaving Vim.
8203 * The machine specific code may override this again.
8204 */
8205 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008206reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008208 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
8210 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008211#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008212 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8213 || cterm_normal_bg_gui_color != INVALCOLOR)
8214 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008215#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 out_str(T_OP);
8220 screen_attr = -1;
8221 }
8222 if (cterm_normal_fg_bold)
8223 {
8224 out_str(T_ME);
8225 screen_attr = -1;
8226 }
8227 }
8228}
8229
8230/*
8231 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8232 * using the attributes from ScreenAttrs["off"].
8233 */
8234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008235screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 int attr;
8238
8239 /* Check for illegal values, just in case (could happen just after
8240 * resizing). */
8241 if (row >= screen_Rows || col >= screen_Columns)
8242 return;
8243
Bram Moolenaar494838a2015-02-10 19:20:37 +01008244 /* Outputting a character in the last cell on the screen may scroll the
8245 * screen up. Only do it when the "xn" termcap property is set, otherwise
8246 * mark the character invalid (update it when scrolled up). */
8247 if (*T_XN == NUL
8248 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#ifdef FEAT_RIGHTLEFT
8250 /* account for first command-line character in rightleft mode */
8251 && !cmdmsg_rl
8252#endif
8253 )
8254 {
8255 ScreenAttrs[off] = (sattr_T)-1;
8256 return;
8257 }
8258
8259 /*
8260 * Stop highlighting first, so it's easier to move the cursor.
8261 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008262#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 if (screen_char_attr != 0)
8264 attr = screen_char_attr;
8265 else
8266#endif
8267 attr = ScreenAttrs[off];
8268 if (screen_attr != attr)
8269 screen_stop_highlight();
8270
8271 windgoto(row, col);
8272
8273 if (screen_attr != attr)
8274 screen_start_highlight(attr);
8275
8276#ifdef FEAT_MBYTE
8277 if (enc_utf8 && ScreenLinesUC[off] != 0)
8278 {
8279 char_u buf[MB_MAXBYTES + 1];
8280
8281 /* Convert UTF-8 character to bytes and write it. */
8282
8283 buf[utfc_char2bytes(off, buf)] = NUL;
8284
8285 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008286 if (utf_ambiguous_width(ScreenLinesUC[off]))
8287 screen_cur_col = 9999;
8288 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 ++screen_cur_col;
8290 }
8291 else
8292#endif
8293 {
8294#ifdef FEAT_MBYTE
8295 out_flush_check();
8296#endif
8297 out_char(ScreenLines[off]);
8298#ifdef FEAT_MBYTE
8299 /* double-byte character in single-width cell */
8300 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8301 out_char(ScreenLines2[off]);
8302#endif
8303 }
8304
8305 screen_cur_col++;
8306}
8307
8308#ifdef FEAT_MBYTE
8309
8310/*
8311 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8312 * on the screen at position 'row' and 'col'.
8313 * The attributes of the first byte is used for all. This is required to
8314 * output the two bytes of a double-byte character with nothing in between.
8315 */
8316 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008317screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318{
8319 /* Check for illegal values (could be wrong when screen was resized). */
8320 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8321 return;
8322
8323 /* Outputting the last character on the screen may scrollup the screen.
8324 * Don't to it! Mark the character invalid (update it when scrolled up) */
8325 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8326 {
8327 ScreenAttrs[off] = (sattr_T)-1;
8328 return;
8329 }
8330
8331 /* Output the first byte normally (positions the cursor), then write the
8332 * second byte directly. */
8333 screen_char(off, row, col);
8334 out_char(ScreenLines[off + 1]);
8335 ++screen_cur_col;
8336}
8337#endif
8338
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008339#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340/*
8341 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8342 * This uses the contents of ScreenLines[] and doesn't change it.
8343 */
8344 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008345screen_draw_rectangle(
8346 int row,
8347 int col,
8348 int height,
8349 int width,
8350 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 int r, c;
8353 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008354#ifdef FEAT_MBYTE
8355 int max_off;
8356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008358 /* Can't use ScreenLines unless initialized */
8359 if (ScreenLines == NULL)
8360 return;
8361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 if (invert)
8363 screen_char_attr = HL_INVERSE;
8364 for (r = row; r < row + height; ++r)
8365 {
8366 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008367#ifdef FEAT_MBYTE
8368 max_off = off + screen_Columns;
8369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 for (c = col; c < col + width; ++c)
8371 {
8372#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008373 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {
8375 screen_char_2(off + c, r, c);
8376 ++c;
8377 }
8378 else
8379#endif
8380 {
8381 screen_char(off + c, r, c);
8382#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008383 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 ++c;
8385#endif
8386 }
8387 }
8388 }
8389 screen_char_attr = 0;
8390}
8391#endif
8392
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008393#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394/*
8395 * Redraw the characters for a vertically split window.
8396 */
8397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008398redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 int col;
8401 int width;
8402
8403# ifdef FEAT_CLIPBOARD
8404 clip_may_clear_selection(row, end - 1);
8405# endif
8406
8407 if (wp == NULL)
8408 {
8409 col = 0;
8410 width = Columns;
8411 }
8412 else
8413 {
8414 col = wp->w_wincol;
8415 width = wp->w_width;
8416 }
8417 screen_draw_rectangle(row, col, end - row, width, FALSE);
8418}
8419#endif
8420
8421/*
8422 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8423 * with character 'c1' in first column followed by 'c2' in the other columns.
8424 * Use attributes 'attr'.
8425 */
8426 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008427screen_fill(
8428 int start_row,
8429 int end_row,
8430 int start_col,
8431 int end_col,
8432 int c1,
8433 int c2,
8434 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
8436 int row;
8437 int col;
8438 int off;
8439 int end_off;
8440 int did_delete;
8441 int c;
8442 int norm_term;
8443#if defined(FEAT_GUI) || defined(UNIX)
8444 int force_next = FALSE;
8445#endif
8446
8447 if (end_row > screen_Rows) /* safety check */
8448 end_row = screen_Rows;
8449 if (end_col > screen_Columns) /* safety check */
8450 end_col = screen_Columns;
8451 if (ScreenLines == NULL
8452 || start_row >= end_row
8453 || start_col >= end_col) /* nothing to do */
8454 return;
8455
8456 /* it's a "normal" terminal when not in a GUI or cterm */
8457 norm_term = (
8458#ifdef FEAT_GUI
8459 !gui.in_use &&
8460#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008461 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 for (row = start_row; row < end_row; ++row)
8463 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008464#ifdef FEAT_MBYTE
8465 if (has_mbyte
8466# ifdef FEAT_GUI
8467 && !gui.in_use
8468# endif
8469 )
8470 {
8471 /* When drawing over the right halve of a double-wide char clear
8472 * out the left halve. When drawing over the left halve of a
8473 * double wide-char clear out the right halve. Only needed in a
8474 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008475 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008476 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008477 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008478 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008479 }
8480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 /*
8482 * Try to use delete-line termcap code, when no attributes or in a
8483 * "normal" terminal, where a bold/italic space is just a
8484 * space.
8485 */
8486 did_delete = FALSE;
8487 if (c2 == ' '
8488 && end_col == Columns
8489 && can_clear(T_CE)
8490 && (attr == 0
8491 || (norm_term
8492 && attr <= HL_ALL
8493 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8494 {
8495 /*
8496 * check if we really need to clear something
8497 */
8498 col = start_col;
8499 if (c1 != ' ') /* don't clear first char */
8500 ++col;
8501
8502 off = LineOffset[row] + col;
8503 end_off = LineOffset[row] + end_col;
8504
8505 /* skip blanks (used often, keep it fast!) */
8506#ifdef FEAT_MBYTE
8507 if (enc_utf8)
8508 while (off < end_off && ScreenLines[off] == ' '
8509 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8510 ++off;
8511 else
8512#endif
8513 while (off < end_off && ScreenLines[off] == ' '
8514 && ScreenAttrs[off] == 0)
8515 ++off;
8516 if (off < end_off) /* something to be cleared */
8517 {
8518 col = off - LineOffset[row];
8519 screen_stop_highlight();
8520 term_windgoto(row, col);/* clear rest of this screen line */
8521 out_str(T_CE);
8522 screen_start(); /* don't know where cursor is now */
8523 col = end_col - col;
8524 while (col--) /* clear chars in ScreenLines */
8525 {
8526 ScreenLines[off] = ' ';
8527#ifdef FEAT_MBYTE
8528 if (enc_utf8)
8529 ScreenLinesUC[off] = 0;
8530#endif
8531 ScreenAttrs[off] = 0;
8532 ++off;
8533 }
8534 }
8535 did_delete = TRUE; /* the chars are cleared now */
8536 }
8537
8538 off = LineOffset[row] + start_col;
8539 c = c1;
8540 for (col = start_col; col < end_col; ++col)
8541 {
8542 if (ScreenLines[off] != c
8543#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008544 || (enc_utf8 && (int)ScreenLinesUC[off]
8545 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#endif
8547 || ScreenAttrs[off] != attr
8548#if defined(FEAT_GUI) || defined(UNIX)
8549 || force_next
8550#endif
8551 )
8552 {
8553#if defined(FEAT_GUI) || defined(UNIX)
8554 /* The bold trick may make a single row of pixels appear in
8555 * the next character. When a bold character is removed, the
8556 * next character should be redrawn too. This happens for our
8557 * own GUI and for some xterms. */
8558 if (
8559# ifdef FEAT_GUI
8560 gui.in_use
8561# endif
8562# if defined(FEAT_GUI) && defined(UNIX)
8563 ||
8564# endif
8565# ifdef UNIX
8566 term_is_xterm
8567# endif
8568 )
8569 {
8570 if (ScreenLines[off] != ' '
8571 && (ScreenAttrs[off] > HL_ALL
8572 || ScreenAttrs[off] & HL_BOLD))
8573 force_next = TRUE;
8574 else
8575 force_next = FALSE;
8576 }
8577#endif
8578 ScreenLines[off] = c;
8579#ifdef FEAT_MBYTE
8580 if (enc_utf8)
8581 {
8582 if (c >= 0x80)
8583 {
8584 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008585 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 else
8588 ScreenLinesUC[off] = 0;
8589 }
8590#endif
8591 ScreenAttrs[off] = attr;
8592 if (!did_delete || c != ' ')
8593 screen_char(off, row, col);
8594 }
8595 ++off;
8596 if (col == start_col)
8597 {
8598 if (did_delete)
8599 break;
8600 c = c2;
8601 }
8602 }
8603 if (end_col == Columns)
8604 LineWraps[row] = FALSE;
8605 if (row == Rows - 1) /* overwritten the command line */
8606 {
8607 redraw_cmdline = TRUE;
8608 if (c1 == ' ' && c2 == ' ')
8609 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008610 if (start_col == 0)
8611 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613 }
8614}
8615
8616/*
8617 * Check if there should be a delay. Used before clearing or redrawing the
8618 * screen or the command line.
8619 */
8620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008621check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8624 && !did_wait_return
8625 && emsg_silent == 0)
8626 {
8627 out_flush();
8628 ui_delay(1000L, TRUE);
8629 emsg_on_display = FALSE;
8630 if (check_msg_scroll)
8631 msg_scroll = FALSE;
8632 }
8633}
8634
8635/*
8636 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008637 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 * Returns TRUE if there is a valid screen to write to.
8639 * Returns FALSE when starting up and screen not initialized yet.
8640 */
8641 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008642screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008644 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 return (ScreenLines != NULL);
8646}
8647
8648/*
8649 * Resize the shell to Rows and Columns.
8650 * Allocate ScreenLines[] and associated items.
8651 *
8652 * There may be some time between setting Rows and Columns and (re)allocating
8653 * ScreenLines[]. This happens when starting up and when (manually) changing
8654 * the shell size. Always use screen_Rows and screen_Columns to access items
8655 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8656 * final size of the shell is needed.
8657 */
8658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008659screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
8661 int new_row, old_row;
8662#ifdef FEAT_GUI
8663 int old_Rows;
8664#endif
8665 win_T *wp;
8666 int outofmem = FALSE;
8667 int len;
8668 schar_T *new_ScreenLines;
8669#ifdef FEAT_MBYTE
8670 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008671 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008673 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674#endif
8675 sattr_T *new_ScreenAttrs;
8676 unsigned *new_LineOffset;
8677 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008678#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008679 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008680 tabpage_T *tp;
8681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008683 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008684#ifdef FEAT_AUTOCMD
8685 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008687retry:
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 /*
8690 * Allocation of the screen buffers is done only when the size changes and
8691 * when Rows and Columns have been set and we have started doing full
8692 * screen stuff.
8693 */
8694 if ((ScreenLines != NULL
8695 && Rows == screen_Rows
8696 && Columns == screen_Columns
8697#ifdef FEAT_MBYTE
8698 && enc_utf8 == (ScreenLinesUC != NULL)
8699 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008700 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701#endif
8702 )
8703 || Rows == 0
8704 || Columns == 0
8705 || (!full_screen && ScreenLines == NULL))
8706 return;
8707
8708 /*
8709 * It's possible that we produce an out-of-memory message below, which
8710 * will cause this function to be called again. To break the loop, just
8711 * return here.
8712 */
8713 if (entered)
8714 return;
8715 entered = TRUE;
8716
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008717 /*
8718 * Note that the window sizes are updated before reallocating the arrays,
8719 * thus we must not redraw here!
8720 */
8721 ++RedrawingDisabled;
8722
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 win_new_shellsize(); /* fit the windows in the new sized shell */
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 comp_col(); /* recompute columns for shown command and ruler */
8726
8727 /*
8728 * We're changing the size of the screen.
8729 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8730 * - Move lines from the old arrays into the new arrays, clear extra
8731 * lines (unless the screen is going to be cleared).
8732 * - Free the old arrays.
8733 *
8734 * If anything fails, make ScreenLines NULL, so we don't do anything!
8735 * Continuing with the old ScreenLines may result in a crash, because the
8736 * size is wrong.
8737 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008738 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008740#ifdef FEAT_AUTOCMD
8741 if (aucmd_win != NULL)
8742 win_free_lsize(aucmd_win);
8743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
8745 new_ScreenLines = (schar_T *)lalloc((long_u)(
8746 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8747#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008748 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 if (enc_utf8)
8750 {
8751 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8752 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008753 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008754 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8756 }
8757 if (enc_dbcs == DBCS_JPNU)
8758 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8759 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8760#endif
8761 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8762 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8763 new_LineOffset = (unsigned *)lalloc((long_u)(
8764 Rows * sizeof(unsigned)), FALSE);
8765 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008766#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008767 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008770 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {
8772 if (win_alloc_lines(wp) == FAIL)
8773 {
8774 outofmem = TRUE;
8775#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008776 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777#endif
8778 }
8779 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008780#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008781 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8782 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008783 outofmem = TRUE;
8784#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008785#ifdef FEAT_WINDOWS
8786give_up:
8787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008789#ifdef FEAT_MBYTE
8790 for (i = 0; i < p_mco; ++i)
8791 if (new_ScreenLinesC[i] == NULL)
8792 break;
8793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (new_ScreenLines == NULL
8795#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008796 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8798#endif
8799 || new_ScreenAttrs == NULL
8800 || new_LineOffset == NULL
8801 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008802#ifdef FEAT_WINDOWS
8803 || new_TabPageIdxs == NULL
8804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 || outofmem)
8806 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008807 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008808 {
8809 /* guess the size */
8810 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8811
8812 /* Remember we did this to avoid getting outofmem messages over
8813 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008814 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 vim_free(new_ScreenLines);
8817 new_ScreenLines = NULL;
8818#ifdef FEAT_MBYTE
8819 vim_free(new_ScreenLinesUC);
8820 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008821 for (i = 0; i < p_mco; ++i)
8822 {
8823 vim_free(new_ScreenLinesC[i]);
8824 new_ScreenLinesC[i] = NULL;
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 vim_free(new_ScreenLines2);
8827 new_ScreenLines2 = NULL;
8828#endif
8829 vim_free(new_ScreenAttrs);
8830 new_ScreenAttrs = NULL;
8831 vim_free(new_LineOffset);
8832 new_LineOffset = NULL;
8833 vim_free(new_LineWraps);
8834 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008835#ifdef FEAT_WINDOWS
8836 vim_free(new_TabPageIdxs);
8837 new_TabPageIdxs = NULL;
8838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 }
8840 else
8841 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008842 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 for (new_row = 0; new_row < Rows; ++new_row)
8845 {
8846 new_LineOffset[new_row] = new_row * Columns;
8847 new_LineWraps[new_row] = FALSE;
8848
8849 /*
8850 * If the screen is not going to be cleared, copy as much as
8851 * possible from the old screen to the new one and clear the rest
8852 * (used when resizing the window at the "--more--" prompt or when
8853 * executing an external command, for the GUI).
8854 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008855 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
8857 (void)vim_memset(new_ScreenLines + new_row * Columns,
8858 ' ', (size_t)Columns * sizeof(schar_T));
8859#ifdef FEAT_MBYTE
8860 if (enc_utf8)
8861 {
8862 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8863 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008864 for (i = 0; i < p_mco; ++i)
8865 (void)vim_memset(new_ScreenLinesC[i]
8866 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 0, (size_t)Columns * sizeof(u8char_T));
8868 }
8869 if (enc_dbcs == DBCS_JPNU)
8870 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8871 0, (size_t)Columns * sizeof(schar_T));
8872#endif
8873 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8874 0, (size_t)Columns * sizeof(sattr_T));
8875 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008876 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
8878 if (screen_Columns < Columns)
8879 len = screen_Columns;
8880 else
8881 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008882#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008883 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008884 * may be invalid now. Also when p_mco changes. */
8885 if (!(enc_utf8 && ScreenLinesUC == NULL)
8886 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008887#endif
8888 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8889 ScreenLines + LineOffset[old_row],
8890 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008892 if (enc_utf8 && ScreenLinesUC != NULL
8893 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
8895 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8896 ScreenLinesUC + LineOffset[old_row],
8897 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008898 for (i = 0; i < p_mco; ++i)
8899 mch_memmove(new_ScreenLinesC[i]
8900 + new_LineOffset[new_row],
8901 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 (size_t)len * sizeof(u8char_T));
8903 }
8904 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8905 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8906 ScreenLines2 + LineOffset[old_row],
8907 (size_t)len * sizeof(schar_T));
8908#endif
8909 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8910 ScreenAttrs + LineOffset[old_row],
8911 (size_t)len * sizeof(sattr_T));
8912 }
8913 }
8914 }
8915 /* Use the last line of the screen for the current line. */
8916 current_ScreenLine = new_ScreenLines + Rows * Columns;
8917 }
8918
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008919 free_screenlines();
8920
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 ScreenLines = new_ScreenLines;
8922#ifdef FEAT_MBYTE
8923 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 for (i = 0; i < p_mco; ++i)
8925 ScreenLinesC[i] = new_ScreenLinesC[i];
8926 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 ScreenLines2 = new_ScreenLines2;
8928#endif
8929 ScreenAttrs = new_ScreenAttrs;
8930 LineOffset = new_LineOffset;
8931 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008932#ifdef FEAT_WINDOWS
8933 TabPageIdxs = new_TabPageIdxs;
8934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
8936 /* It's important that screen_Rows and screen_Columns reflect the actual
8937 * size of ScreenLines[]. Set them before calling anything. */
8938#ifdef FEAT_GUI
8939 old_Rows = screen_Rows;
8940#endif
8941 screen_Rows = Rows;
8942 screen_Columns = Columns;
8943
8944 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008945 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 screenclear2();
8947
8948#ifdef FEAT_GUI
8949 else if (gui.in_use
8950 && !gui.starting
8951 && ScreenLines != NULL
8952 && old_Rows != Rows)
8953 {
8954 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8955 /*
8956 * Adjust the position of the cursor, for when executing an external
8957 * command.
8958 */
8959 if (msg_row >= Rows) /* Rows got smaller */
8960 msg_row = Rows - 1; /* put cursor at last row */
8961 else if (Rows > old_Rows) /* Rows got bigger */
8962 msg_row += Rows - old_Rows; /* put cursor in same place */
8963 if (msg_col >= Columns) /* Columns got smaller */
8964 msg_col = Columns - 1; /* put cursor at last column */
8965 }
8966#endif
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008969 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008970
8971#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008972 /*
8973 * Do not apply autocommands more than 3 times to avoid an endless loop
8974 * in case applying autocommands always changes Rows or Columns.
8975 */
8976 if (starting == 0 && ++retry_count <= 3)
8977 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008978 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008979 /* In rare cases, autocommands may have altered Rows or Columns,
8980 * jump back to check if we need to allocate the screen again. */
8981 goto retry;
8982 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008987free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008988{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008989#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008990 int i;
8991
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008992 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008993 for (i = 0; i < Screen_mco; ++i)
8994 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008995 vim_free(ScreenLines2);
8996#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008997 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008998 vim_free(ScreenAttrs);
8999 vim_free(LineOffset);
9000 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009001#ifdef FEAT_WINDOWS
9002 vim_free(TabPageIdxs);
9003#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009004}
9005
9006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009007screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 check_for_delay(FALSE);
9010 screenalloc(FALSE); /* allocate screen buffers if size changed */
9011 screenclear2(); /* clear the screen */
9012}
9013
9014 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009015screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
9017 int i;
9018
9019 if (starting == NO_SCREEN || ScreenLines == NULL
9020#ifdef FEAT_GUI
9021 || (gui.in_use && gui.starting)
9022#endif
9023 )
9024 return;
9025
9026#ifdef FEAT_GUI
9027 if (!gui.in_use)
9028#endif
9029 screen_attr = -1; /* force setting the Normal colors */
9030 screen_stop_highlight(); /* don't want highlighting here */
9031
9032#ifdef FEAT_CLIPBOARD
9033 /* disable selection without redrawing it */
9034 clip_scroll_selection(9999);
9035#endif
9036
9037 /* blank out ScreenLines */
9038 for (i = 0; i < Rows; ++i)
9039 {
9040 lineclear(LineOffset[i], (int)Columns);
9041 LineWraps[i] = FALSE;
9042 }
9043
9044 if (can_clear(T_CL))
9045 {
9046 out_str(T_CL); /* clear the display */
9047 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009048 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050 else
9051 {
9052 /* can't clear the screen, mark all chars with invalid attributes */
9053 for (i = 0; i < Rows; ++i)
9054 lineinvalid(LineOffset[i], (int)Columns);
9055 clear_cmdline = TRUE;
9056 }
9057
9058 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9059
9060 win_rest_invalid(firstwin);
9061 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009062#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009063 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (must_redraw == CLEAR) /* no need to clear again */
9066 must_redraw = NOT_VALID;
9067 compute_cmdrow();
9068 msg_row = cmdline_row; /* put cursor on last line for messages */
9069 msg_col = 0;
9070 screen_start(); /* don't know where cursor is now */
9071 msg_scrolled = 0; /* can't scroll back */
9072 msg_didany = FALSE;
9073 msg_didout = FALSE;
9074}
9075
9076/*
9077 * Clear one line in ScreenLines.
9078 */
9079 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009080lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9083#ifdef FEAT_MBYTE
9084 if (enc_utf8)
9085 (void)vim_memset(ScreenLinesUC + off, 0,
9086 (size_t)width * sizeof(u8char_T));
9087#endif
9088 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9089}
9090
9091/*
9092 * Mark one line in ScreenLines invalid by setting the attributes to an
9093 * invalid value.
9094 */
9095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009096lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9099}
9100
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009101#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102/*
9103 * Copy part of a Screenline for vertically split window "wp".
9104 */
9105 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009106linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 unsigned off_to = LineOffset[to] + wp->w_wincol;
9109 unsigned off_from = LineOffset[from] + wp->w_wincol;
9110
9111 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9112 wp->w_width * sizeof(schar_T));
9113# ifdef FEAT_MBYTE
9114 if (enc_utf8)
9115 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009116 int i;
9117
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9119 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009120 for (i = 0; i < p_mco; ++i)
9121 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9122 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 }
9124 if (enc_dbcs == DBCS_JPNU)
9125 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9126 wp->w_width * sizeof(schar_T));
9127# endif
9128 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9129 wp->w_width * sizeof(sattr_T));
9130}
9131#endif
9132
9133/*
9134 * Return TRUE if clearing with term string "p" would work.
9135 * It can't work when the string is empty or it won't set the right background.
9136 */
9137 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009138can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 return (*p != NUL && (t_colors <= 1
9141#ifdef FEAT_GUI
9142 || gui.in_use
9143#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009144#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009145 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009146 || (!p_tgc && cterm_normal_bg_color == 0)
9147#else
9148 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009149#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009150 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151}
9152
9153/*
9154 * Reset cursor position. Use whenever cursor was moved because of outputting
9155 * something directly to the screen (shell commands) or a terminal control
9156 * code.
9157 */
9158 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009159screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
9161 screen_cur_row = screen_cur_col = 9999;
9162}
9163
9164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 * Move the cursor to position "row","col" in the screen.
9166 * This tries to find the most efficient way to move, minimizing the number of
9167 * characters sent to the terminal.
9168 */
9169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009170windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009172 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 int i;
9174 int plan;
9175 int cost;
9176 int wouldbe_col;
9177 int noinvcurs;
9178 char_u *bs;
9179 int goto_cost;
9180 int attr;
9181
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009182#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9184
9185#define PLAN_LE 1
9186#define PLAN_CR 2
9187#define PLAN_NL 3
9188#define PLAN_WRITE 4
9189 /* Can't use ScreenLines unless initialized */
9190 if (ScreenLines == NULL)
9191 return;
9192
9193 if (col != screen_cur_col || row != screen_cur_row)
9194 {
9195 /* Check for valid position. */
9196 if (row < 0) /* window without text lines? */
9197 row = 0;
9198 if (row >= screen_Rows)
9199 row = screen_Rows - 1;
9200 if (col >= screen_Columns)
9201 col = screen_Columns - 1;
9202
9203 /* check if no cursor movement is allowed in highlight mode */
9204 if (screen_attr && *T_MS == NUL)
9205 noinvcurs = HIGHL_COST;
9206 else
9207 noinvcurs = 0;
9208 goto_cost = GOTO_COST + noinvcurs;
9209
9210 /*
9211 * Plan how to do the positioning:
9212 * 1. Use CR to move it to column 0, same row.
9213 * 2. Use T_LE to move it a few columns to the left.
9214 * 3. Use NL to move a few lines down, column 0.
9215 * 4. Move a few columns to the right with T_ND or by writing chars.
9216 *
9217 * Don't do this if the cursor went beyond the last column, the cursor
9218 * position is unknown then (some terminals wrap, some don't )
9219 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009220 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 * characters to move the cursor to the right.
9222 */
9223 if (row >= screen_cur_row && screen_cur_col < Columns)
9224 {
9225 /*
9226 * If the cursor is in the same row, bigger col, we can use CR
9227 * or T_LE.
9228 */
9229 bs = NULL; /* init for GCC */
9230 attr = screen_attr;
9231 if (row == screen_cur_row && col < screen_cur_col)
9232 {
9233 /* "le" is preferred over "bc", because "bc" is obsolete */
9234 if (*T_LE)
9235 bs = T_LE; /* "cursor left" */
9236 else
9237 bs = T_BC; /* "backspace character (old) */
9238 if (*bs)
9239 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9240 else
9241 cost = 999;
9242 if (col + 1 < cost) /* using CR is less characters */
9243 {
9244 plan = PLAN_CR;
9245 wouldbe_col = 0;
9246 cost = 1; /* CR is just one character */
9247 }
9248 else
9249 {
9250 plan = PLAN_LE;
9251 wouldbe_col = col;
9252 }
9253 if (noinvcurs) /* will stop highlighting */
9254 {
9255 cost += noinvcurs;
9256 attr = 0;
9257 }
9258 }
9259
9260 /*
9261 * If the cursor is above where we want to be, we can use CR LF.
9262 */
9263 else if (row > screen_cur_row)
9264 {
9265 plan = PLAN_NL;
9266 wouldbe_col = 0;
9267 cost = (row - screen_cur_row) * 2; /* CR LF */
9268 if (noinvcurs) /* will stop highlighting */
9269 {
9270 cost += noinvcurs;
9271 attr = 0;
9272 }
9273 }
9274
9275 /*
9276 * If the cursor is in the same row, smaller col, just use write.
9277 */
9278 else
9279 {
9280 plan = PLAN_WRITE;
9281 wouldbe_col = screen_cur_col;
9282 cost = 0;
9283 }
9284
9285 /*
9286 * Check if any characters that need to be written have the
9287 * correct attributes. Also avoid UTF-8 characters.
9288 */
9289 i = col - wouldbe_col;
9290 if (i > 0)
9291 cost += i;
9292 if (cost < goto_cost && i > 0)
9293 {
9294 /*
9295 * Check if the attributes are correct without additionally
9296 * stopping highlighting.
9297 */
9298 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9299 while (i && *p++ == attr)
9300 --i;
9301 if (i != 0)
9302 {
9303 /*
9304 * Try if it works when highlighting is stopped here.
9305 */
9306 if (*--p == 0)
9307 {
9308 cost += noinvcurs;
9309 while (i && *p++ == 0)
9310 --i;
9311 }
9312 if (i != 0)
9313 cost = 999; /* different attributes, don't do it */
9314 }
9315#ifdef FEAT_MBYTE
9316 if (enc_utf8)
9317 {
9318 /* Don't use an UTF-8 char for positioning, it's slow. */
9319 for (i = wouldbe_col; i < col; ++i)
9320 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9321 {
9322 cost = 999;
9323 break;
9324 }
9325 }
9326#endif
9327 }
9328
9329 /*
9330 * We can do it without term_windgoto()!
9331 */
9332 if (cost < goto_cost)
9333 {
9334 if (plan == PLAN_LE)
9335 {
9336 if (noinvcurs)
9337 screen_stop_highlight();
9338 while (screen_cur_col > col)
9339 {
9340 out_str(bs);
9341 --screen_cur_col;
9342 }
9343 }
9344 else if (plan == PLAN_CR)
9345 {
9346 if (noinvcurs)
9347 screen_stop_highlight();
9348 out_char('\r');
9349 screen_cur_col = 0;
9350 }
9351 else if (plan == PLAN_NL)
9352 {
9353 if (noinvcurs)
9354 screen_stop_highlight();
9355 while (screen_cur_row < row)
9356 {
9357 out_char('\n');
9358 ++screen_cur_row;
9359 }
9360 screen_cur_col = 0;
9361 }
9362
9363 i = col - screen_cur_col;
9364 if (i > 0)
9365 {
9366 /*
9367 * Use cursor-right if it's one character only. Avoids
9368 * removing a line of pixels from the last bold char, when
9369 * using the bold trick in the GUI.
9370 */
9371 if (T_ND[0] != NUL && T_ND[1] == NUL)
9372 {
9373 while (i-- > 0)
9374 out_char(*T_ND);
9375 }
9376 else
9377 {
9378 int off;
9379
9380 off = LineOffset[row] + screen_cur_col;
9381 while (i-- > 0)
9382 {
9383 if (ScreenAttrs[off] != screen_attr)
9384 screen_stop_highlight();
9385#ifdef FEAT_MBYTE
9386 out_flush_check();
9387#endif
9388 out_char(ScreenLines[off]);
9389#ifdef FEAT_MBYTE
9390 if (enc_dbcs == DBCS_JPNU
9391 && ScreenLines[off] == 0x8e)
9392 out_char(ScreenLines2[off]);
9393#endif
9394 ++off;
9395 }
9396 }
9397 }
9398 }
9399 }
9400 else
9401 cost = 999;
9402
9403 if (cost >= goto_cost)
9404 {
9405 if (noinvcurs)
9406 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009407 if (row == screen_cur_row && (col > screen_cur_col)
9408 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 term_cursor_right(col - screen_cur_col);
9410 else
9411 term_windgoto(row, col);
9412 }
9413 screen_cur_row = row;
9414 screen_cur_col = col;
9415 }
9416}
9417
9418/*
9419 * Set cursor to its position in the current window.
9420 */
9421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009422setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424 if (redrawing())
9425 {
9426 validate_cursor();
9427 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9428 W_WINCOL(curwin) + (
9429#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009430 /* With 'rightleft' set and the cursor on a double-wide
9431 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9433# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009434 (has_mbyte
9435 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9436 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437# endif
9438 1)) :
9439#endif
9440 curwin->w_wcol));
9441 }
9442}
9443
9444
9445/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009446 * Insert 'line_count' lines at 'row' in window 'wp'.
9447 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9448 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 * scrolling.
9450 * Returns FAIL if the lines are not inserted, OK for success.
9451 */
9452 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009453win_ins_lines(
9454 win_T *wp,
9455 int row,
9456 int line_count,
9457 int invalid,
9458 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 int did_delete;
9461 int nextrow;
9462 int lastrow;
9463 int retval;
9464
9465 if (invalid)
9466 wp->w_lines_valid = 0;
9467
9468 if (wp->w_height < 5)
9469 return FAIL;
9470
9471 if (line_count > wp->w_height - row)
9472 line_count = wp->w_height - row;
9473
9474 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9475 if (retval != MAYBE)
9476 return retval;
9477
9478 /*
9479 * If there is a next window or a status line, we first try to delete the
9480 * lines at the bottom to avoid messing what is after the window.
9481 * If this fails and there are following windows, don't do anything to avoid
9482 * messing up those windows, better just redraw.
9483 */
9484 did_delete = FALSE;
9485#ifdef FEAT_WINDOWS
9486 if (wp->w_next != NULL || wp->w_status_height)
9487 {
9488 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9489 line_count, (int)Rows, FALSE, NULL) == OK)
9490 did_delete = TRUE;
9491 else if (wp->w_next)
9492 return FAIL;
9493 }
9494#endif
9495 /*
9496 * if no lines deleted, blank the lines that will end up below the window
9497 */
9498 if (!did_delete)
9499 {
9500#ifdef FEAT_WINDOWS
9501 wp->w_redr_status = TRUE;
9502#endif
9503 redraw_cmdline = TRUE;
9504 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9505 lastrow = nextrow + line_count;
9506 if (lastrow > Rows)
9507 lastrow = Rows;
9508 screen_fill(nextrow - line_count, lastrow - line_count,
9509 W_WINCOL(wp), (int)W_ENDCOL(wp),
9510 ' ', ' ', 0);
9511 }
9512
9513 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9514 == FAIL)
9515 {
9516 /* deletion will have messed up other windows */
9517 if (did_delete)
9518 {
9519#ifdef FEAT_WINDOWS
9520 wp->w_redr_status = TRUE;
9521#endif
9522 win_rest_invalid(W_NEXT(wp));
9523 }
9524 return FAIL;
9525 }
9526
9527 return OK;
9528}
9529
9530/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009531 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9533 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9534 * scrolling
9535 * Return OK for success, FAIL if the lines are not deleted.
9536 */
9537 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009538win_del_lines(
9539 win_T *wp,
9540 int row,
9541 int line_count,
9542 int invalid,
9543 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544{
9545 int retval;
9546
9547 if (invalid)
9548 wp->w_lines_valid = 0;
9549
9550 if (line_count > wp->w_height - row)
9551 line_count = wp->w_height - row;
9552
9553 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9554 if (retval != MAYBE)
9555 return retval;
9556
9557 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9558 (int)Rows, FALSE, NULL) == FAIL)
9559 return FAIL;
9560
9561#ifdef FEAT_WINDOWS
9562 /*
9563 * If there are windows or status lines below, try to put them at the
9564 * correct place. If we can't do that, they have to be redrawn.
9565 */
9566 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9567 {
9568 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9569 line_count, (int)Rows, NULL) == FAIL)
9570 {
9571 wp->w_redr_status = TRUE;
9572 win_rest_invalid(wp->w_next);
9573 }
9574 }
9575 /*
9576 * If this is the last window and there is no status line, redraw the
9577 * command line later.
9578 */
9579 else
9580#endif
9581 redraw_cmdline = TRUE;
9582 return OK;
9583}
9584
9585/*
9586 * Common code for win_ins_lines() and win_del_lines().
9587 * Returns OK or FAIL when the work has been done.
9588 * Returns MAYBE when not finished yet.
9589 */
9590 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009591win_do_lines(
9592 win_T *wp,
9593 int row,
9594 int line_count,
9595 int mayclear,
9596 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 int retval;
9599
9600 if (!redrawing() || line_count <= 0)
9601 return FAIL;
9602
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009603 /* When inserting lines would result in loss of command output, just redraw
9604 * the lines. */
9605 if (no_win_do_lines_ins && !del)
9606 return FAIL;
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 /* only a few lines left: redraw is faster */
9609 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009610#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 && wp->w_width == Columns
9612#endif
9613 )
9614 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009615 if (!no_win_do_lines_ins)
9616 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 return FAIL;
9618 }
9619
9620 /*
9621 * Delete all remaining lines
9622 */
9623 if (row + line_count >= wp->w_height)
9624 {
9625 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9626 W_WINCOL(wp), (int)W_ENDCOL(wp),
9627 ' ', ' ', 0);
9628 return OK;
9629 }
9630
9631 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009632 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009634 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009636 if (!no_win_do_lines_ins)
9637 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 /*
9640 * If the terminal can set a scroll region, use that.
9641 * Always do this in a vertically split window. This will redraw from
9642 * ScreenLines[] when t_CV isn't defined. That's faster than using
9643 * win_line().
9644 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009645 * a character in the lower right corner of the scroll region may cause a
9646 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 */
9648 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009649#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 || W_WIDTH(wp) != Columns
9651#endif
9652 )
9653 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009654#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9656#endif
9657 scroll_region_set(wp, row);
9658 if (del)
9659 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9660 wp->w_height - row, FALSE, wp);
9661 else
9662 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9663 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009664#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9666#endif
9667 scroll_region_reset();
9668 return retval;
9669 }
9670
9671#ifdef FEAT_WINDOWS
9672 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9673 return FAIL;
9674#endif
9675
9676 return MAYBE;
9677}
9678
9679/*
9680 * window 'wp' and everything after it is messed up, mark it for redraw
9681 */
9682 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009683win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685#ifdef FEAT_WINDOWS
9686 while (wp != NULL)
9687#else
9688 if (wp != NULL)
9689#endif
9690 {
9691 redraw_win_later(wp, NOT_VALID);
9692#ifdef FEAT_WINDOWS
9693 wp->w_redr_status = TRUE;
9694 wp = wp->w_next;
9695#endif
9696 }
9697 redraw_cmdline = TRUE;
9698}
9699
9700/*
9701 * The rest of the routines in this file perform screen manipulations. The
9702 * given operation is performed physically on the screen. The corresponding
9703 * change is also made to the internal screen image. In this way, the editor
9704 * anticipates the effect of editing changes on the appearance of the screen.
9705 * That way, when we call screenupdate a complete redraw isn't usually
9706 * necessary. Another advantage is that we can keep adding code to anticipate
9707 * screen changes, and in the meantime, everything still works.
9708 */
9709
9710/*
9711 * types for inserting or deleting lines
9712 */
9713#define USE_T_CAL 1
9714#define USE_T_CDL 2
9715#define USE_T_AL 3
9716#define USE_T_CE 4
9717#define USE_T_DL 5
9718#define USE_T_SR 6
9719#define USE_NL 7
9720#define USE_T_CD 8
9721#define USE_REDRAW 9
9722
9723/*
9724 * insert lines on the screen and update ScreenLines[]
9725 * 'end' is the line after the scrolled part. Normally it is Rows.
9726 * When scrolling region used 'off' is the offset from the top for the region.
9727 * 'row' and 'end' are relative to the start of the region.
9728 *
9729 * return FAIL for failure, OK for success.
9730 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009731 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009732screen_ins_lines(
9733 int off,
9734 int row,
9735 int line_count,
9736 int end,
9737 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739 int i;
9740 int j;
9741 unsigned temp;
9742 int cursor_row;
9743 int type;
9744 int result_empty;
9745 int can_ce = can_clear(T_CE);
9746
9747 /*
9748 * FAIL if
9749 * - there is no valid screen
9750 * - the screen has to be redrawn completely
9751 * - the line count is less than one
9752 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009753 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009755 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9756#ifdef FEAT_CLIPBOARD
9757 || (clip_star.state != SELECT_CLEARED
9758 && redrawing_for_callback > 0)
9759#endif
9760 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 return FAIL;
9762
9763 /*
9764 * There are seven ways to insert lines:
9765 * 0. When in a vertically split window and t_CV isn't set, redraw the
9766 * characters from ScreenLines[].
9767 * 1. Use T_CD (clear to end of display) if it exists and the result of
9768 * the insert is just empty lines
9769 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9770 * present or line_count > 1. It looks better if we do all the inserts
9771 * at once.
9772 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9773 * insert is just empty lines and T_CE is not present or line_count >
9774 * 1.
9775 * 4. Use T_AL (insert line) if it exists.
9776 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9777 * just empty lines.
9778 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9779 * just empty lines.
9780 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9781 * the 'da' flag is not set or we have clear line capability.
9782 * 8. redraw the characters from ScreenLines[].
9783 *
9784 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9785 * the scrollbar for the window. It does have insert line, use that if it
9786 * exists.
9787 */
9788 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009789#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9791 type = USE_REDRAW;
9792 else
9793#endif
9794 if (can_clear(T_CD) && result_empty)
9795 type = USE_T_CD;
9796 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9797 type = USE_T_CAL;
9798 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9799 type = USE_T_CDL;
9800 else if (*T_AL != NUL)
9801 type = USE_T_AL;
9802 else if (can_ce && result_empty)
9803 type = USE_T_CE;
9804 else if (*T_DL != NUL && result_empty)
9805 type = USE_T_DL;
9806 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9807 type = USE_T_SR;
9808 else
9809 return FAIL;
9810
9811 /*
9812 * For clearing the lines screen_del_lines() is used. This will also take
9813 * care of t_db if necessary.
9814 */
9815 if (type == USE_T_CD || type == USE_T_CDL ||
9816 type == USE_T_CE || type == USE_T_DL)
9817 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9818
9819 /*
9820 * If text is retained below the screen, first clear or delete as many
9821 * lines at the bottom of the window as are about to be inserted so that
9822 * the deleted lines won't later surface during a screen_del_lines.
9823 */
9824 if (*T_DB)
9825 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9826
9827#ifdef FEAT_CLIPBOARD
9828 /* Remove a modeless selection when inserting lines halfway the screen
9829 * or not the full width of the screen. */
9830 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009831# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 || (wp != NULL && wp->w_width != Columns)
9833# endif
9834 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009835 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 else
9837 clip_scroll_selection(-line_count);
9838#endif
9839
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840#ifdef FEAT_GUI
9841 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9842 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009843 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844#endif
9845
9846 if (*T_CCS != NUL) /* cursor relative to region */
9847 cursor_row = row;
9848 else
9849 cursor_row = row + off;
9850
9851 /*
9852 * Shift LineOffset[] line_count down to reflect the inserted lines.
9853 * Clear the inserted lines in ScreenLines[].
9854 */
9855 row += off;
9856 end += off;
9857 for (i = 0; i < line_count; ++i)
9858 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009859#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 if (wp != NULL && wp->w_width != Columns)
9861 {
9862 /* need to copy part of a line */
9863 j = end - 1 - i;
9864 while ((j -= line_count) >= row)
9865 linecopy(j + line_count, j, wp);
9866 j += line_count;
9867 if (can_clear((char_u *)" "))
9868 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9869 else
9870 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9871 LineWraps[j] = FALSE;
9872 }
9873 else
9874#endif
9875 {
9876 j = end - 1 - i;
9877 temp = LineOffset[j];
9878 while ((j -= line_count) >= row)
9879 {
9880 LineOffset[j + line_count] = LineOffset[j];
9881 LineWraps[j + line_count] = LineWraps[j];
9882 }
9883 LineOffset[j + line_count] = temp;
9884 LineWraps[j + line_count] = FALSE;
9885 if (can_clear((char_u *)" "))
9886 lineclear(temp, (int)Columns);
9887 else
9888 lineinvalid(temp, (int)Columns);
9889 }
9890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
9892 screen_stop_highlight();
9893 windgoto(cursor_row, 0);
9894
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009895#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 /* redraw the characters */
9897 if (type == USE_REDRAW)
9898 redraw_block(row, end, wp);
9899 else
9900#endif
9901 if (type == USE_T_CAL)
9902 {
9903 term_append_lines(line_count);
9904 screen_start(); /* don't know where cursor is now */
9905 }
9906 else
9907 {
9908 for (i = 0; i < line_count; i++)
9909 {
9910 if (type == USE_T_AL)
9911 {
9912 if (i && cursor_row != 0)
9913 windgoto(cursor_row, 0);
9914 out_str(T_AL);
9915 }
9916 else /* type == USE_T_SR */
9917 out_str(T_SR);
9918 screen_start(); /* don't know where cursor is now */
9919 }
9920 }
9921
9922 /*
9923 * With scroll-reverse and 'da' flag set we need to clear the lines that
9924 * have been scrolled down into the region.
9925 */
9926 if (type == USE_T_SR && *T_DA)
9927 {
9928 for (i = 0; i < line_count; ++i)
9929 {
9930 windgoto(off + i, 0);
9931 out_str(T_CE);
9932 screen_start(); /* don't know where cursor is now */
9933 }
9934 }
9935
9936#ifdef FEAT_GUI
9937 gui_can_update_cursor();
9938 if (gui.in_use)
9939 out_flush(); /* always flush after a scroll */
9940#endif
9941 return OK;
9942}
9943
9944/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009945 * Delete lines on the screen and update ScreenLines[].
9946 * "end" is the line after the scrolled part. Normally it is Rows.
9947 * When scrolling region used "off" is the offset from the top for the region.
9948 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 *
9950 * Return OK for success, FAIL if the lines are not deleted.
9951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009953screen_del_lines(
9954 int off,
9955 int row,
9956 int line_count,
9957 int end,
9958 int force, /* even when line_count > p_ttyscroll */
9959 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960{
9961 int j;
9962 int i;
9963 unsigned temp;
9964 int cursor_row;
9965 int cursor_end;
9966 int result_empty; /* result is empty until end of region */
9967 int can_delete; /* deleting line codes can be used */
9968 int type;
9969
9970 /*
9971 * FAIL if
9972 * - there is no valid screen
9973 * - the screen has to be redrawn completely
9974 * - the line count is less than one
9975 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009976 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009978 if (!screen_valid(TRUE) || line_count <= 0
9979 || (!force && line_count > p_ttyscroll)
9980#ifdef FEAT_CLIPBOARD
9981 || (clip_star.state != SELECT_CLEARED
9982 && redrawing_for_callback > 0)
9983#endif
9984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 return FAIL;
9986
9987 /*
9988 * Check if the rest of the current region will become empty.
9989 */
9990 result_empty = row + line_count >= end;
9991
9992 /*
9993 * We can delete lines only when 'db' flag not set or when 'ce' option
9994 * available.
9995 */
9996 can_delete = (*T_DB == NUL || can_clear(T_CE));
9997
9998 /*
9999 * There are six ways to delete lines:
10000 * 0. When in a vertically split window and t_CV isn't set, redraw the
10001 * characters from ScreenLines[].
10002 * 1. Use T_CD if it exists and the result is empty.
10003 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10004 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10005 * none of the other ways work.
10006 * 4. Use T_CE (erase line) if the result is empty.
10007 * 5. Use T_DL (delete line) if it exists.
10008 * 6. redraw the characters from ScreenLines[].
10009 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010010#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10012 type = USE_REDRAW;
10013 else
10014#endif
10015 if (can_clear(T_CD) && result_empty)
10016 type = USE_T_CD;
10017#if defined(__BEOS__) && defined(BEOS_DR8)
10018 /*
10019 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10020 * its internal termcap... this works okay for tests which test *T_DB !=
10021 * NUL. It has the disadvantage that the user cannot use any :set t_*
10022 * command to get T_DB (back) to empty_option, only :set term=... will do
10023 * the trick...
10024 * Anyway, this hack will hopefully go away with the next OS release.
10025 * (Olaf Seibert)
10026 */
10027 else if (row == 0 && T_DB == empty_option
10028 && (line_count == 1 || *T_CDL == NUL))
10029#else
10030 else if (row == 0 && (
10031#ifndef AMIGA
10032 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10033 * up, so use delete-line command */
10034 line_count == 1 ||
10035#endif
10036 *T_CDL == NUL))
10037#endif
10038 type = USE_NL;
10039 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10040 type = USE_T_CDL;
10041 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010042#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 && (wp == NULL || wp->w_width == Columns)
10044#endif
10045 )
10046 type = USE_T_CE;
10047 else if (*T_DL != NUL && can_delete)
10048 type = USE_T_DL;
10049 else if (*T_CDL != NUL && can_delete)
10050 type = USE_T_CDL;
10051 else
10052 return FAIL;
10053
10054#ifdef FEAT_CLIPBOARD
10055 /* Remove a modeless selection when deleting lines halfway the screen or
10056 * not the full width of the screen. */
10057 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010058# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 || (wp != NULL && wp->w_width != Columns)
10060# endif
10061 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010062 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else
10064 clip_scroll_selection(line_count);
10065#endif
10066
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#ifdef FEAT_GUI
10068 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10069 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010070 gui_dont_update_cursor(gui.cursor_row >= row + off
10071 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072#endif
10073
10074 if (*T_CCS != NUL) /* cursor relative to region */
10075 {
10076 cursor_row = row;
10077 cursor_end = end;
10078 }
10079 else
10080 {
10081 cursor_row = row + off;
10082 cursor_end = end + off;
10083 }
10084
10085 /*
10086 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10087 * Clear the inserted lines in ScreenLines[].
10088 */
10089 row += off;
10090 end += off;
10091 for (i = 0; i < line_count; ++i)
10092 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010093#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 if (wp != NULL && wp->w_width != Columns)
10095 {
10096 /* need to copy part of a line */
10097 j = row + i;
10098 while ((j += line_count) <= end - 1)
10099 linecopy(j - line_count, j, wp);
10100 j -= line_count;
10101 if (can_clear((char_u *)" "))
10102 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10103 else
10104 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10105 LineWraps[j] = FALSE;
10106 }
10107 else
10108#endif
10109 {
10110 /* whole width, moving the line pointers is faster */
10111 j = row + i;
10112 temp = LineOffset[j];
10113 while ((j += line_count) <= end - 1)
10114 {
10115 LineOffset[j - line_count] = LineOffset[j];
10116 LineWraps[j - line_count] = LineWraps[j];
10117 }
10118 LineOffset[j - line_count] = temp;
10119 LineWraps[j - line_count] = FALSE;
10120 if (can_clear((char_u *)" "))
10121 lineclear(temp, (int)Columns);
10122 else
10123 lineinvalid(temp, (int)Columns);
10124 }
10125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
10127 screen_stop_highlight();
10128
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010129#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 /* redraw the characters */
10131 if (type == USE_REDRAW)
10132 redraw_block(row, end, wp);
10133 else
10134#endif
10135 if (type == USE_T_CD) /* delete the lines */
10136 {
10137 windgoto(cursor_row, 0);
10138 out_str(T_CD);
10139 screen_start(); /* don't know where cursor is now */
10140 }
10141 else if (type == USE_T_CDL)
10142 {
10143 windgoto(cursor_row, 0);
10144 term_delete_lines(line_count);
10145 screen_start(); /* don't know where cursor is now */
10146 }
10147 /*
10148 * Deleting lines at top of the screen or scroll region: Just scroll
10149 * the whole screen (scroll region) up by outputting newlines on the
10150 * last line.
10151 */
10152 else if (type == USE_NL)
10153 {
10154 windgoto(cursor_end - 1, 0);
10155 for (i = line_count; --i >= 0; )
10156 out_char('\n'); /* cursor will remain on same line */
10157 }
10158 else
10159 {
10160 for (i = line_count; --i >= 0; )
10161 {
10162 if (type == USE_T_DL)
10163 {
10164 windgoto(cursor_row, 0);
10165 out_str(T_DL); /* delete a line */
10166 }
10167 else /* type == USE_T_CE */
10168 {
10169 windgoto(cursor_row + i, 0);
10170 out_str(T_CE); /* erase a line */
10171 }
10172 screen_start(); /* don't know where cursor is now */
10173 }
10174 }
10175
10176 /*
10177 * If the 'db' flag is set, we need to clear the lines that have been
10178 * scrolled up at the bottom of the region.
10179 */
10180 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10181 {
10182 for (i = line_count; i > 0; --i)
10183 {
10184 windgoto(cursor_end - i, 0);
10185 out_str(T_CE); /* erase a line */
10186 screen_start(); /* don't know where cursor is now */
10187 }
10188 }
10189
10190#ifdef FEAT_GUI
10191 gui_can_update_cursor();
10192 if (gui.in_use)
10193 out_flush(); /* always flush after a scroll */
10194#endif
10195
10196 return OK;
10197}
10198
10199/*
10200 * show the current mode and ruler
10201 *
10202 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10203 * If clear_cmdline is FALSE there may be a message there that needs to be
10204 * cleared only if a mode is shown.
10205 * Return the length of the message (0 if no message).
10206 */
10207 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010208showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209{
10210 int need_clear;
10211 int length = 0;
10212 int do_mode;
10213 int attr;
10214 int nwr_save;
10215#ifdef FEAT_INS_EXPAND
10216 int sub_attr;
10217#endif
10218
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010219 do_mode = ((p_smd && msg_silent == 0)
10220 && ((State & INSERT)
10221 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010222 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (do_mode || Recording)
10224 {
10225 /*
10226 * Don't show mode right now, when not redrawing or inside a mapping.
10227 * Call char_avail() only when we are going to show something, because
10228 * it takes a bit of time.
10229 */
10230 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10231 {
10232 redraw_cmdline = TRUE; /* show mode later */
10233 return 0;
10234 }
10235
10236 nwr_save = need_wait_return;
10237
10238 /* wait a bit before overwriting an important message */
10239 check_for_delay(FALSE);
10240
10241 /* if the cmdline is more than one line high, erase top lines */
10242 need_clear = clear_cmdline;
10243 if (clear_cmdline && cmdline_row < Rows - 1)
10244 msg_clr_cmdline(); /* will reset clear_cmdline */
10245
10246 /* Position on the last line in the window, column 0 */
10247 msg_pos_mode();
10248 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010249 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (do_mode)
10251 {
10252 MSG_PUTS_ATTR("--", attr);
10253#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010254 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010255# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010256 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010257# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010258 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010259# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010260 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010261# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 MSG_PUTS_ATTR(" IM", attr);
10263# else
10264 MSG_PUTS_ATTR(" XIM", attr);
10265# endif
10266#endif
10267#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10268 if (gui.in_use)
10269 {
10270 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010271 {
10272 /* HANGUL */
10273 if (enc_utf8)
10274 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10275 else
10276 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 }
10279#endif
10280#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010281 /* CTRL-X in Insert mode */
10282 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 {
10284 /* These messages can get long, avoid a wrap in a narrow
10285 * window. Prefer showing edit_submode_extra. */
10286 length = (Rows - msg_row) * Columns - 3;
10287 if (edit_submode_extra != NULL)
10288 length -= vim_strsize(edit_submode_extra);
10289 if (length > 0)
10290 {
10291 if (edit_submode_pre != NULL)
10292 length -= vim_strsize(edit_submode_pre);
10293 if (length - vim_strsize(edit_submode) > 0)
10294 {
10295 if (edit_submode_pre != NULL)
10296 msg_puts_attr(edit_submode_pre, attr);
10297 msg_puts_attr(edit_submode, attr);
10298 }
10299 if (edit_submode_extra != NULL)
10300 {
10301 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10302 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010303 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 else
10305 sub_attr = attr;
10306 msg_puts_attr(edit_submode_extra, sub_attr);
10307 }
10308 }
10309 length = 0;
10310 }
10311 else
10312#endif
10313 {
10314#ifdef FEAT_VREPLACE
10315 if (State & VREPLACE_FLAG)
10316 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10317 else
10318#endif
10319 if (State & REPLACE_FLAG)
10320 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10321 else if (State & INSERT)
10322 {
10323#ifdef FEAT_RIGHTLEFT
10324 if (p_ri)
10325 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10326#endif
10327 MSG_PUTS_ATTR(_(" INSERT"), attr);
10328 }
10329 else if (restart_edit == 'I')
10330 MSG_PUTS_ATTR(_(" (insert)"), attr);
10331 else if (restart_edit == 'R')
10332 MSG_PUTS_ATTR(_(" (replace)"), attr);
10333 else if (restart_edit == 'V')
10334 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10335#ifdef FEAT_RIGHTLEFT
10336 if (p_hkmap)
10337 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10338# ifdef FEAT_FKMAP
10339 if (p_fkmap)
10340 MSG_PUTS_ATTR(farsi_text_5, attr);
10341# endif
10342#endif
10343#ifdef FEAT_KEYMAP
10344 if (State & LANGMAP)
10345 {
10346# ifdef FEAT_ARABIC
10347 if (curwin->w_p_arab)
10348 MSG_PUTS_ATTR(_(" Arabic"), attr);
10349 else
10350# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010351 if (get_keymap_str(curwin, (char_u *)" (%s)",
10352 NameBuff, MAXPATHL))
10353 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 }
10355#endif
10356 if ((State & INSERT) && p_paste)
10357 MSG_PUTS_ATTR(_(" (paste)"), attr);
10358
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 if (VIsual_active)
10360 {
10361 char *p;
10362
10363 /* Don't concatenate separate words to avoid translation
10364 * problems. */
10365 switch ((VIsual_select ? 4 : 0)
10366 + (VIsual_mode == Ctrl_V) * 2
10367 + (VIsual_mode == 'V'))
10368 {
10369 case 0: p = N_(" VISUAL"); break;
10370 case 1: p = N_(" VISUAL LINE"); break;
10371 case 2: p = N_(" VISUAL BLOCK"); break;
10372 case 4: p = N_(" SELECT"); break;
10373 case 5: p = N_(" SELECT LINE"); break;
10374 default: p = N_(" SELECT BLOCK"); break;
10375 }
10376 MSG_PUTS_ATTR(_(p), attr);
10377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 MSG_PUTS_ATTR(" --", attr);
10379 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010380
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 need_clear = TRUE;
10382 }
10383 if (Recording
10384#ifdef FEAT_INS_EXPAND
10385 && edit_submode == NULL /* otherwise it gets too long */
10386#endif
10387 )
10388 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010389 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 need_clear = TRUE;
10391 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010392
10393 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 if (need_clear || clear_cmdline)
10395 msg_clr_eos();
10396 msg_didout = FALSE; /* overwrite this message */
10397 length = msg_col;
10398 msg_col = 0;
10399 need_wait_return = nwr_save; /* never ask for hit-return for this */
10400 }
10401 else if (clear_cmdline && msg_silent == 0)
10402 /* Clear the whole command line. Will reset "clear_cmdline". */
10403 msg_clr_cmdline();
10404
10405#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 /* In Visual mode the size of the selected area must be redrawn. */
10407 if (VIsual_active)
10408 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409
10410 /* If the last window has no status line, the ruler is after the mode
10411 * message and must be redrawn */
10412 if (redrawing()
10413# ifdef FEAT_WINDOWS
10414 && lastwin->w_status_height == 0
10415# endif
10416 )
10417 win_redr_ruler(lastwin, TRUE);
10418#endif
10419 redraw_cmdline = FALSE;
10420 clear_cmdline = FALSE;
10421
10422 return length;
10423}
10424
10425/*
10426 * Position for a mode message.
10427 */
10428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010429msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430{
10431 msg_col = 0;
10432 msg_row = Rows - 1;
10433}
10434
10435/*
10436 * Delete mode message. Used when ESC is typed which is expected to end
10437 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010438 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 */
10440 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010441unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010444 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 */
10446 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10447 redraw_cmdline = TRUE; /* delete mode later */
10448 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010449 clearmode();
10450}
10451
10452/*
10453 * Clear the mode message.
10454 */
10455 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010456clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010457{
10458 msg_pos_mode();
10459 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010460 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010461 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462}
10463
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010464 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010465recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010466{
10467 MSG_PUTS_ATTR(_("recording"), attr);
10468 if (!shortmess(SHM_RECORDING))
10469 {
10470 char_u s[4];
10471 sprintf((char *)s, " @%c", Recording);
10472 MSG_PUTS_ATTR(s, attr);
10473 }
10474}
10475
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010476#if defined(FEAT_WINDOWS)
10477/*
10478 * Draw the tab pages line at the top of the Vim window.
10479 */
10480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010481draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010482{
10483 int tabcount = 0;
10484 tabpage_T *tp;
10485 int tabwidth;
10486 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010487 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010488 int attr;
10489 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010490 win_T *cwp;
10491 int wincount;
10492 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010493 int c;
10494 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010495 int attr_sel = HL_ATTR(HLF_TPS);
10496 int attr_nosel = HL_ATTR(HLF_TP);
10497 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010498 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010499 int room;
10500 int use_sep_chars = (t_colors < 8
10501#ifdef FEAT_GUI
10502 && !gui.in_use
10503#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010504#ifdef FEAT_TERMGUICOLORS
10505 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010506#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010507 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010508
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010509 if (ScreenLines == NULL)
10510 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010511 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010512
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010513#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010514 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010515 if (gui_use_tabline())
10516 {
10517 gui_update_tabline();
10518 return;
10519 }
10520#endif
10521
10522 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010523 return;
10524
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010525#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010526
10527 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10528 for (scol = 0; scol < Columns; ++scol)
10529 TabPageIdxs[scol] = 0;
10530
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010531 /* Use the 'tabline' option if it's set. */
10532 if (*p_tal != NUL)
10533 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010534 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010535
10536 /* Check for an error. If there is one we would loop in redrawing the
10537 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010538 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010539 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010540 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010541 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010542 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010543 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010544 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010545 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010546#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010547 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010548 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010549 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010550
Bram Moolenaar238a5642006-02-21 22:12:05 +000010551 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10552 if (tabwidth < 6)
10553 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010554
Bram Moolenaar238a5642006-02-21 22:12:05 +000010555 attr = attr_nosel;
10556 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010557 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010558 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10559 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010560 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010561 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010562
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 if (tp->tp_topframe == topframe)
10564 attr = attr_sel;
10565 if (use_sep_chars && col > 0)
10566 screen_putchar('|', 0, col++, attr);
10567
10568 if (tp->tp_topframe != topframe)
10569 attr = attr_nosel;
10570
10571 screen_putchar(' ', 0, col++, attr);
10572
10573 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010574 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010575 cwp = curwin;
10576 wp = firstwin;
10577 }
10578 else
10579 {
10580 cwp = tp->tp_curwin;
10581 wp = tp->tp_firstwin;
10582 }
10583
10584 modified = FALSE;
10585 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10586 if (bufIsChanged(wp->w_buffer))
10587 modified = TRUE;
10588 if (modified || wincount > 1)
10589 {
10590 if (wincount > 1)
10591 {
10592 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010593 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010594 if (col + len >= Columns - 3)
10595 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010597#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010598 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010599#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010600 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010601#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010602 );
10603 col += len;
10604 }
10605 if (modified)
10606 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10607 screen_putchar(' ', 0, col++, attr);
10608 }
10609
10610 room = scol - col + tabwidth - 1;
10611 if (room > 0)
10612 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010613 /* Get buffer name in NameBuff[] */
10614 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010615 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010616 len = vim_strsize(NameBuff);
10617 p = NameBuff;
10618#ifdef FEAT_MBYTE
10619 if (has_mbyte)
10620 while (len > room)
10621 {
10622 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010623 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 }
10625 else
10626#endif
10627 if (len > room)
10628 {
10629 p += len - room;
10630 len = room;
10631 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010632 if (len > Columns - col - 1)
10633 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010635 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010636 col += len;
10637 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639
10640 /* Store the tab page number in TabPageIdxs[], so that
10641 * jump_to_mouse() knows where each one is. */
10642 ++tabcount;
10643 while (scol < col)
10644 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010645 }
10646
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 if (use_sep_chars)
10648 c = '_';
10649 else
10650 c = ' ';
10651 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010652
10653 /* Put an "X" for closing the current tab if there are several. */
10654 if (first_tabpage->tp_next != NULL)
10655 {
10656 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10657 TabPageIdxs[Columns - 1] = -999;
10658 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010659 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010660
10661 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10662 * set. */
10663 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010664}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010665
10666/*
10667 * Get buffer name for "buf" into NameBuff[].
10668 * Takes care of special buffer names and translates special characters.
10669 */
10670 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010671get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010672{
10673 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010674 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010675 else
10676 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10677 trans_characters(NameBuff, MAXPATHL);
10678}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010679#endif
10680
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10682/*
10683 * Get the character to use in a status line. Get its attributes in "*attr".
10684 */
10685 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010686fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 int fill;
10689 if (is_curwin)
10690 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010691 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 fill = fill_stl;
10693 }
10694 else
10695 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010696 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 fill = fill_stlnc;
10698 }
10699 /* Use fill when there is highlighting, and highlighting of current
10700 * window differs, or the fillchars differ, or this is not the
10701 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010702 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010703 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 || (fill_stl != fill_stlnc)))
10705 return fill;
10706 if (is_curwin)
10707 return '^';
10708 return '=';
10709}
10710#endif
10711
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010712#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713/*
10714 * Get the character to use in a separator between vertically split windows.
10715 * Get its attributes in "*attr".
10716 */
10717 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010718fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010720 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 if (*attr == 0 && fill_vert == ' ')
10722 return '|';
10723 else
10724 return fill_vert;
10725}
10726#endif
10727
10728/*
10729 * Return TRUE if redrawing should currently be done.
10730 */
10731 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010732redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010734#ifdef FEAT_EVAL
10735 if (disable_redraw_for_testing)
10736 return 0;
10737 else
10738#endif
10739 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10741}
10742
10743/*
10744 * Return TRUE if printing messages should currently be done.
10745 */
10746 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010747messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749 return (!(p_lz && char_avail() && !KeyTyped));
10750}
10751
10752/*
10753 * Show current status info in ruler and various other places
10754 * If always is FALSE, only show ruler if position has changed.
10755 */
10756 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010757showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758{
10759 if (!always && !redrawing())
10760 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010761#ifdef FEAT_INS_EXPAND
10762 if (pum_visible())
10763 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010764# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010765 /* Don't redraw right now, do it later. */
10766 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010767# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010768 return;
10769 }
10770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010772 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010773 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010774 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 else
10777#endif
10778#ifdef FEAT_CMDL_INFO
10779 win_redr_ruler(curwin, always);
10780#endif
10781
10782#ifdef FEAT_TITLE
10783 if (need_maketitle
10784# ifdef FEAT_STL_OPT
10785 || (p_icon && (stl_syntax & STL_IN_ICON))
10786 || (p_title && (stl_syntax & STL_IN_TITLE))
10787# endif
10788 )
10789 maketitle();
10790#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010791#ifdef FEAT_WINDOWS
10792 /* Redraw the tab pages line if needed. */
10793 if (redraw_tabline)
10794 draw_tabline();
10795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796}
10797
10798#ifdef FEAT_CMDL_INFO
10799 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010800win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010802#define RULER_BUF_LEN 70
10803 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 int row;
10805 int fillchar;
10806 int attr;
10807 int empty_line = FALSE;
10808 colnr_T virtcol;
10809 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010810 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010812#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813 int this_ru_col;
10814 int off = 0;
10815 int width = Columns;
10816# define WITH_OFF(x) x
10817# define WITH_WIDTH(x) x
10818#else
10819# define WITH_OFF(x) 0
10820# define WITH_WIDTH(x) Columns
10821# define this_ru_col ru_col
10822#endif
10823
10824 /* If 'ruler' off or redrawing disabled, don't do anything */
10825 if (!p_ru)
10826 return;
10827
10828 /*
10829 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10830 * after deleting lines, before cursor.lnum is corrected.
10831 */
10832 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10833 return;
10834
10835#ifdef FEAT_INS_EXPAND
10836 /* Don't draw the ruler while doing insert-completion, it might overwrite
10837 * the (long) mode message. */
10838# ifdef FEAT_WINDOWS
10839 if (wp == lastwin && lastwin->w_status_height == 0)
10840# endif
10841 if (edit_submode != NULL)
10842 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010843 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10844 if (pum_visible())
10845 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846#endif
10847
10848#ifdef FEAT_STL_OPT
10849 if (*p_ruf)
10850 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010851 int save_called_emsg = called_emsg;
10852
10853 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010855 if (called_emsg)
10856 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010857 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010858 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 return;
10860 }
10861#endif
10862
10863 /*
10864 * Check if not in Insert mode and the line is empty (will show "0-1").
10865 */
10866 if (!(State & INSERT)
10867 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10868 empty_line = TRUE;
10869
10870 /*
10871 * Only draw the ruler when something changed.
10872 */
10873 validate_virtcol_win(wp);
10874 if ( redraw_cmdline
10875 || always
10876 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10877 || wp->w_cursor.col != wp->w_ru_cursor.col
10878 || wp->w_virtcol != wp->w_ru_virtcol
10879#ifdef FEAT_VIRTUALEDIT
10880 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10881#endif
10882 || wp->w_topline != wp->w_ru_topline
10883 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10884#ifdef FEAT_DIFF
10885 || wp->w_topfill != wp->w_ru_topfill
10886#endif
10887 || empty_line != wp->w_ru_empty)
10888 {
10889 cursor_off();
10890#ifdef FEAT_WINDOWS
10891 if (wp->w_status_height)
10892 {
10893 row = W_WINROW(wp) + wp->w_height;
10894 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 off = W_WINCOL(wp);
10896 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 }
10898 else
10899#endif
10900 {
10901 row = Rows - 1;
10902 fillchar = ' ';
10903 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010904#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 width = Columns;
10906 off = 0;
10907#endif
10908 }
10909
10910 /* In list mode virtcol needs to be recomputed */
10911 virtcol = wp->w_virtcol;
10912 if (wp->w_p_list && lcs_tab1 == NUL)
10913 {
10914 wp->w_p_list = FALSE;
10915 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10916 wp->w_p_list = TRUE;
10917 }
10918
10919 /*
10920 * Some sprintfs return the length, some return a pointer.
10921 * To avoid portability problems we use strlen() here.
10922 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010923 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10925 ? 0L
10926 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010927 len = STRLEN(buffer);
10928 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10930 (int)virtcol + 1);
10931
10932 /*
10933 * Add a "50%" if there is room for it.
10934 * On the last line, don't print in the last column (scrolls the
10935 * screen up on some terminals).
10936 */
10937 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010938 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 o = i + vim_strsize(buffer + i + 1);
10940#ifdef FEAT_WINDOWS
10941 if (wp->w_status_height == 0) /* can't use last char of screen */
10942#endif
10943 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010944#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 this_ru_col = ru_col - (Columns - width);
10946 if (this_ru_col < 0)
10947 this_ru_col = 0;
10948#endif
10949 /* Never use more than half the window/screen width, leave the other
10950 * half for the filename. */
10951 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10952 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10953 if (this_ru_col + o < WITH_WIDTH(width))
10954 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010955 /* need at least 3 chars left for get_rel_pos() + NUL */
10956 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
10958#ifdef FEAT_MBYTE
10959 if (has_mbyte)
10960 i += (*mb_char2bytes)(fillchar, buffer + i);
10961 else
10962#endif
10963 buffer[i++] = fillchar;
10964 ++o;
10965 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010966 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 }
10968 /* Truncate at window boundary. */
10969#ifdef FEAT_MBYTE
10970 if (has_mbyte)
10971 {
10972 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010973 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 {
10975 o += (*mb_ptr2cells)(buffer + i);
10976 if (this_ru_col + o > WITH_WIDTH(width))
10977 {
10978 buffer[i] = NUL;
10979 break;
10980 }
10981 }
10982 }
10983 else
10984#endif
10985 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10986 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10987
10988 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10989 i = redraw_cmdline;
10990 screen_fill(row, row + 1,
10991 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10992 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10993 fillchar, fillchar, attr);
10994 /* don't redraw the cmdline because of showing the ruler */
10995 redraw_cmdline = i;
10996 wp->w_ru_cursor = wp->w_cursor;
10997 wp->w_ru_virtcol = wp->w_virtcol;
10998 wp->w_ru_empty = empty_line;
10999 wp->w_ru_topline = wp->w_topline;
11000 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11001#ifdef FEAT_DIFF
11002 wp->w_ru_topfill = wp->w_topfill;
11003#endif
11004 }
11005}
11006#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011007
11008#if defined(FEAT_LINEBREAK) || defined(PROTO)
11009/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011010 * Return the width of the 'number' and 'relativenumber' column.
11011 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011012 * Otherwise it depends on 'numberwidth' and the line count.
11013 */
11014 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011015number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011016{
11017 int n;
11018 linenr_T lnum;
11019
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011020 if (wp->w_p_rnu && !wp->w_p_nu)
11021 /* cursor line shows "0" */
11022 lnum = wp->w_height;
11023 else
11024 /* cursor line shows absolute line number */
11025 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011026
Bram Moolenaar6b314672015-03-20 15:42:10 +010011027 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011028 return wp->w_nrwidth_width;
11029 wp->w_nrwidth_line_count = lnum;
11030
11031 n = 0;
11032 do
11033 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011034 lnum /= 10;
11035 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011036 } while (lnum > 0);
11037
11038 /* 'numberwidth' gives the minimal width plus one */
11039 if (n < wp->w_p_nuw - 1)
11040 n = wp->w_p_nuw - 1;
11041
11042 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011043 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011044 return n;
11045}
11046#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011047
11048/*
11049 * Return the current cursor column. This is the actual position on the
11050 * screen. First column is 0.
11051 */
11052 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011053screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011054{
11055 return screen_cur_col;
11056}
11057
11058/*
11059 * Return the current cursor row. This is the actual position on the screen.
11060 * First row is 0.
11061 */
11062 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011063screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011064{
11065 return screen_cur_row;
11066}