blob: 7d62042cb763f428a11e32201843a31b1e7eb041 [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
Bram Moolenaard85f2712017-07-28 21:51:57 +02001203 /* If this window contains a terminal, redraw works completely differently.
1204 */
1205 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001206 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001207 wp->w_redr_type = 0;
1208 return;
1209 }
1210#endif
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001213 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
1215
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001216#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001217 /* Force redraw when width of 'number' or 'relativenumber' column
1218 * changes. */
1219 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001220 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001221 {
1222 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001224 }
1225 else
1226#endif
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1229 {
1230 /*
1231 * When there are both inserted/deleted lines and specific lines to be
1232 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1233 * everything (only happens when redrawing is off for while).
1234 */
1235 type = NOT_VALID;
1236 }
1237 else
1238 {
1239 /*
1240 * Set mod_top to the first line that needs displaying because of
1241 * changes. Set mod_bot to the first line after the changes.
1242 */
1243 mod_top = wp->w_redraw_top;
1244 if (wp->w_redraw_bot != 0)
1245 mod_bot = wp->w_redraw_bot + 1;
1246 else
1247 mod_bot = 0;
1248 wp->w_redraw_top = 0; /* reset for next time */
1249 wp->w_redraw_bot = 0;
1250 if (buf->b_mod_set)
1251 {
1252 if (mod_top == 0 || mod_top > buf->b_mod_top)
1253 {
1254 mod_top = buf->b_mod_top;
1255#ifdef FEAT_SYN_HL
1256 /* Need to redraw lines above the change that may be included
1257 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001260 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (mod_top < 1)
1262 mod_top = 1;
1263 }
1264#endif
1265 }
1266 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1267 mod_bot = buf->b_mod_bot;
1268
1269#ifdef FEAT_SEARCH_EXTRA
1270 /* When 'hlsearch' is on and using a multi-line search pattern, a
1271 * change in one line may make the Search highlighting in a
1272 * previous line invalid. Simple solution: redraw all visible
1273 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001274 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001276 if (search_hl.rm.regprog != NULL
1277 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001279 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001280 {
1281 cur = wp->w_match_head;
1282 while (cur != NULL)
1283 {
1284 if (cur->match.regprog != NULL
1285 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001286 {
1287 top_to_mod = TRUE;
1288 break;
1289 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001290 cur = cur->next;
1291 }
1292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#endif
1294 }
1295#ifdef FEAT_FOLDING
1296 if (mod_top != 0 && hasAnyFolding(wp))
1297 {
1298 linenr_T lnumt, lnumb;
1299
1300 /*
1301 * A change in a line can cause lines above it to become folded or
1302 * unfolded. Find the top most buffer line that may be affected.
1303 * If the line was previously folded and displayed, get the first
1304 * line of that fold. If the line is folded now, get the first
1305 * folded line. Use the minimum of these two.
1306 */
1307
1308 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1309 * the line below it. If there is no valid entry, use w_topline.
1310 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1311 * to this line. If there is no valid entry, use MAXLNUM. */
1312 lnumt = wp->w_topline;
1313 lnumb = MAXLNUM;
1314 for (i = 0; i < wp->w_lines_valid; ++i)
1315 if (wp->w_lines[i].wl_valid)
1316 {
1317 if (wp->w_lines[i].wl_lastlnum < mod_top)
1318 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1319 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1320 {
1321 lnumb = wp->w_lines[i].wl_lnum;
1322 /* When there is a fold column it might need updating
1323 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001324 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 ++lnumb;
1326 }
1327 }
1328
1329 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1330 if (mod_top > lnumt)
1331 mod_top = lnumt;
1332
1333 /* Now do the same for the bottom line (one above mod_bot). */
1334 --mod_bot;
1335 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1336 ++mod_bot;
1337 if (mod_bot < lnumb)
1338 mod_bot = lnumb;
1339 }
1340#endif
1341
1342 /* When a change starts above w_topline and the end is below
1343 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344 * If the end of the change is above w_topline: do like no change was
1345 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (mod_top != 0 && mod_top < wp->w_topline)
1347 {
1348 if (mod_bot > wp->w_topline)
1349 mod_top = wp->w_topline;
1350#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001351 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 top_end = 1;
1353#endif
1354 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001355
1356 /* When line numbers are displayed need to redraw all lines below
1357 * inserted/deleted lines. */
1358 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1359 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361
1362 /*
1363 * When only displaying the lines at the top, set top_end. Used when
1364 * window has scrolled down for msg_scrolled.
1365 */
1366 if (type == REDRAW_TOP)
1367 {
1368 j = 0;
1369 for (i = 0; i < wp->w_lines_valid; ++i)
1370 {
1371 j += wp->w_lines[i].wl_size;
1372 if (j >= wp->w_upd_rows)
1373 {
1374 top_end = j;
1375 break;
1376 }
1377 }
1378 if (top_end == 0)
1379 /* not found (cannot happen?): redraw everything */
1380 type = NOT_VALID;
1381 else
1382 /* top area defined, the rest is VALID */
1383 type = VALID;
1384 }
1385
Bram Moolenaar367329b2007-08-30 11:53:22 +00001386 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001387 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1388 * non-zero and thus not FALSE) will indicate that screenclear() was not
1389 * called. */
1390 if (screen_cleared)
1391 screen_cleared = MAYBE;
1392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 /*
1394 * If there are no changes on the screen that require a complete redraw,
1395 * handle three cases:
1396 * 1: we are off the top of the screen by a few lines: scroll down
1397 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1398 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1399 * w_lines[] that needs updating.
1400 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001401 if ((type == VALID || type == SOME_VALID
1402 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403#ifdef FEAT_DIFF
1404 && !wp->w_botfill && !wp->w_old_botfill
1405#endif
1406 )
1407 {
1408 if (mod_top != 0 && wp->w_topline == mod_top)
1409 {
1410 /*
1411 * w_topline is the first changed line, the scrolling will be done
1412 * further down.
1413 */
1414 }
1415 else if (wp->w_lines[0].wl_valid
1416 && (wp->w_topline < wp->w_lines[0].wl_lnum
1417#ifdef FEAT_DIFF
1418 || (wp->w_topline == wp->w_lines[0].wl_lnum
1419 && wp->w_topfill > wp->w_old_topfill)
1420#endif
1421 ))
1422 {
1423 /*
1424 * New topline is above old topline: May scroll down.
1425 */
1426#ifdef FEAT_FOLDING
1427 if (hasAnyFolding(wp))
1428 {
1429 linenr_T ln;
1430
1431 /* count the number of lines we are off, counting a sequence
1432 * of folded lines as one */
1433 j = 0;
1434 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1435 {
1436 ++j;
1437 if (j >= wp->w_height - 2)
1438 break;
1439 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1440 }
1441 }
1442 else
1443#endif
1444 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1445 if (j < wp->w_height - 2) /* not too far off */
1446 {
1447 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1448#ifdef FEAT_DIFF
1449 /* insert extra lines for previously invisible filler lines */
1450 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1451 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1452 - wp->w_old_topfill;
1453#endif
1454 if (i < wp->w_height - 2) /* less than a screen off */
1455 {
1456 /*
1457 * Try to insert the correct number of lines.
1458 * If not the last window, delete the lines at the bottom.
1459 * win_ins_lines may fail when the terminal can't do it.
1460 */
1461 if (i > 0)
1462 check_for_delay(FALSE);
1463 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1464 {
1465 if (wp->w_lines_valid != 0)
1466 {
1467 /* Need to update rows that are new, stop at the
1468 * first one that scrolled down. */
1469 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
1472 /* Move the entries that were scrolled, disable
1473 * the entries for the lines to be redrawn. */
1474 if ((wp->w_lines_valid += j) > wp->w_height)
1475 wp->w_lines_valid = wp->w_height;
1476 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1477 wp->w_lines[idx] = wp->w_lines[idx - j];
1478 while (idx >= 0)
1479 wp->w_lines[idx--].wl_valid = FALSE;
1480 }
1481 }
1482 else
1483 mid_start = 0; /* redraw all lines */
1484 }
1485 else
1486 mid_start = 0; /* redraw all lines */
1487 }
1488 else
1489 mid_start = 0; /* redraw all lines */
1490 }
1491 else
1492 {
1493 /*
1494 * New topline is at or below old topline: May scroll up.
1495 * When topline didn't change, find first entry in w_lines[] that
1496 * needs updating.
1497 */
1498
1499 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1500 j = -1;
1501 row = 0;
1502 for (i = 0; i < wp->w_lines_valid; i++)
1503 {
1504 if (wp->w_lines[i].wl_valid
1505 && wp->w_lines[i].wl_lnum == wp->w_topline)
1506 {
1507 j = i;
1508 break;
1509 }
1510 row += wp->w_lines[i].wl_size;
1511 }
1512 if (j == -1)
1513 {
1514 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1515 * lines */
1516 mid_start = 0;
1517 }
1518 else
1519 {
1520 /*
1521 * Try to delete the correct number of lines.
1522 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1523 */
1524#ifdef FEAT_DIFF
1525 /* If the topline didn't change, delete old filler lines,
1526 * otherwise delete filler lines of the new topline... */
1527 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1528 row += wp->w_old_topfill;
1529 else
1530 row += diff_check_fill(wp, wp->w_topline);
1531 /* ... but don't delete new filler lines. */
1532 row -= wp->w_topfill;
1533#endif
1534 if (row > 0)
1535 {
1536 check_for_delay(FALSE);
1537 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1538 bot_start = wp->w_height - row;
1539 else
1540 mid_start = 0; /* redraw all lines */
1541 }
1542 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1543 {
1544 /*
1545 * Skip the lines (below the deleted lines) that are still
1546 * valid and don't need redrawing. Copy their info
1547 * upwards, to compensate for the deleted lines. Set
1548 * bot_start to the first row that needs redrawing.
1549 */
1550 bot_start = 0;
1551 idx = 0;
1552 for (;;)
1553 {
1554 wp->w_lines[idx] = wp->w_lines[j];
1555 /* stop at line that didn't fit, unless it is still
1556 * valid (no lines deleted) */
1557 if (row > 0 && bot_start + row
1558 + (int)wp->w_lines[j].wl_size > wp->w_height)
1559 {
1560 wp->w_lines_valid = idx + 1;
1561 break;
1562 }
1563 bot_start += wp->w_lines[idx++].wl_size;
1564
1565 /* stop at the last valid entry in w_lines[].wl_size */
1566 if (++j >= wp->w_lines_valid)
1567 {
1568 wp->w_lines_valid = idx;
1569 break;
1570 }
1571 }
1572#ifdef FEAT_DIFF
1573 /* Correct the first entry for filler lines at the top
1574 * when it won't get updated below. */
1575 if (wp->w_p_diff && bot_start > 0)
1576 wp->w_lines[0].wl_size =
1577 plines_win_nofill(wp, wp->w_topline, TRUE)
1578 + wp->w_topfill;
1579#endif
1580 }
1581 }
1582 }
1583
1584 /* When starting redraw in the first line, redraw all lines. When
1585 * there is only one window it's probably faster to clear the screen
1586 * first. */
1587 if (mid_start == 0)
1588 {
1589 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001590 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001591 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001592 /* Clear the screen when it was not done by win_del_lines() or
1593 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1594 * then. */
1595 if (screen_cleared != TRUE)
1596 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001597#ifdef FEAT_WINDOWS
1598 /* The screen was cleared, redraw the tab pages line. */
1599 if (redraw_tabline)
1600 draw_tabline();
1601#endif
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001604
1605 /* When win_del_lines() or win_ins_lines() caused the screen to be
1606 * cleared (only happens for the first window) or when screenclear()
1607 * was called directly above, "must_redraw" will have been set to
1608 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1609 if (screen_cleared == TRUE)
1610 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 else
1613 {
1614 /* Not VALID or INVERTED: redraw all lines. */
1615 mid_start = 0;
1616 mid_end = wp->w_height;
1617 }
1618
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001619 if (type == SOME_VALID)
1620 {
1621 /* SOME_VALID: redraw all lines. */
1622 mid_start = 0;
1623 mid_end = wp->w_height;
1624 type = NOT_VALID;
1625 }
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /* check if we are updating or removing the inverted part */
1628 if ((VIsual_active && buf == curwin->w_buffer)
1629 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1630 {
1631 linenr_T from, to;
1632
1633 if (VIsual_active)
1634 {
1635 if (VIsual_active
1636 && (VIsual_mode != wp->w_old_visual_mode
1637 || type == INVERTED_ALL))
1638 {
1639 /*
1640 * If the type of Visual selection changed, redraw the whole
1641 * selection. Also when the ownership of the X selection is
1642 * gained or lost.
1643 */
1644 if (curwin->w_cursor.lnum < VIsual.lnum)
1645 {
1646 from = curwin->w_cursor.lnum;
1647 to = VIsual.lnum;
1648 }
1649 else
1650 {
1651 from = VIsual.lnum;
1652 to = curwin->w_cursor.lnum;
1653 }
1654 /* redraw more when the cursor moved as well */
1655 if (wp->w_old_cursor_lnum < from)
1656 from = wp->w_old_cursor_lnum;
1657 if (wp->w_old_cursor_lnum > to)
1658 to = wp->w_old_cursor_lnum;
1659 if (wp->w_old_visual_lnum < from)
1660 from = wp->w_old_visual_lnum;
1661 if (wp->w_old_visual_lnum > to)
1662 to = wp->w_old_visual_lnum;
1663 }
1664 else
1665 {
1666 /*
1667 * Find the line numbers that need to be updated: The lines
1668 * between the old cursor position and the current cursor
1669 * position. Also check if the Visual position changed.
1670 */
1671 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1672 {
1673 from = curwin->w_cursor.lnum;
1674 to = wp->w_old_cursor_lnum;
1675 }
1676 else
1677 {
1678 from = wp->w_old_cursor_lnum;
1679 to = curwin->w_cursor.lnum;
1680 if (from == 0) /* Visual mode just started */
1681 from = to;
1682 }
1683
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001684 if (VIsual.lnum != wp->w_old_visual_lnum
1685 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 if (wp->w_old_visual_lnum < from
1688 && wp->w_old_visual_lnum != 0)
1689 from = wp->w_old_visual_lnum;
1690 if (wp->w_old_visual_lnum > to)
1691 to = wp->w_old_visual_lnum;
1692 if (VIsual.lnum < from)
1693 from = VIsual.lnum;
1694 if (VIsual.lnum > to)
1695 to = VIsual.lnum;
1696 }
1697 }
1698
1699 /*
1700 * If in block mode and changed column or curwin->w_curswant:
1701 * update all lines.
1702 * First compute the actual start and end column.
1703 */
1704 if (VIsual_mode == Ctrl_V)
1705 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001706 colnr_T fromc, toc;
1707#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1708 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar404406a2014-10-09 13:24:43 +02001710 if (curwin->w_p_lbr)
1711 ve_flags = VE_ALL;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001714#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1715 ve_flags = save_ve_flags;
1716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 ++toc;
1718 if (curwin->w_curswant == MAXCOL)
1719 toc = MAXCOL;
1720
1721 if (fromc != wp->w_old_cursor_fcol
1722 || toc != wp->w_old_cursor_lcol)
1723 {
1724 if (from > VIsual.lnum)
1725 from = VIsual.lnum;
1726 if (to < VIsual.lnum)
1727 to = VIsual.lnum;
1728 }
1729 wp->w_old_cursor_fcol = fromc;
1730 wp->w_old_cursor_lcol = toc;
1731 }
1732 }
1733 else
1734 {
1735 /* Use the line numbers of the old Visual area. */
1736 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1737 {
1738 from = wp->w_old_cursor_lnum;
1739 to = wp->w_old_visual_lnum;
1740 }
1741 else
1742 {
1743 from = wp->w_old_visual_lnum;
1744 to = wp->w_old_cursor_lnum;
1745 }
1746 }
1747
1748 /*
1749 * There is no need to update lines above the top of the window.
1750 */
1751 if (from < wp->w_topline)
1752 from = wp->w_topline;
1753
1754 /*
1755 * If we know the value of w_botline, use it to restrict the update to
1756 * the lines that are visible in the window.
1757 */
1758 if (wp->w_valid & VALID_BOTLINE)
1759 {
1760 if (from >= wp->w_botline)
1761 from = wp->w_botline - 1;
1762 if (to >= wp->w_botline)
1763 to = wp->w_botline - 1;
1764 }
1765
1766 /*
1767 * Find the minimal part to be updated.
1768 * Watch out for scrolling that made entries in w_lines[] invalid.
1769 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1770 * top_end; need to redraw from top_end to the "to" line.
1771 * A middle mouse click with a Visual selection may change the text
1772 * above the Visual area and reset wl_valid, do count these for
1773 * mid_end (in srow).
1774 */
1775 if (mid_start > 0)
1776 {
1777 lnum = wp->w_topline;
1778 idx = 0;
1779 srow = 0;
1780 if (scrolled_down)
1781 mid_start = top_end;
1782 else
1783 mid_start = 0;
1784 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1785 {
1786 if (wp->w_lines[idx].wl_valid)
1787 mid_start += wp->w_lines[idx].wl_size;
1788 else if (!scrolled_down)
1789 srow += wp->w_lines[idx].wl_size;
1790 ++idx;
1791# ifdef FEAT_FOLDING
1792 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1793 lnum = wp->w_lines[idx].wl_lnum;
1794 else
1795# endif
1796 ++lnum;
1797 }
1798 srow += mid_start;
1799 mid_end = wp->w_height;
1800 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1801 {
1802 if (wp->w_lines[idx].wl_valid
1803 && wp->w_lines[idx].wl_lnum >= to + 1)
1804 {
1805 /* Only update until first row of this line */
1806 mid_end = srow;
1807 break;
1808 }
1809 srow += wp->w_lines[idx].wl_size;
1810 }
1811 }
1812 }
1813
1814 if (VIsual_active && buf == curwin->w_buffer)
1815 {
1816 wp->w_old_visual_mode = VIsual_mode;
1817 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1818 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001819 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 wp->w_old_curswant = curwin->w_curswant;
1821 }
1822 else
1823 {
1824 wp->w_old_visual_mode = 0;
1825 wp->w_old_cursor_lnum = 0;
1826 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001827 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1831 /* reset got_int, otherwise regexp won't work */
1832 save_got_int = got_int;
1833 got_int = 0;
1834#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001835#ifdef SYN_TIME_LIMIT
1836 /* Set the time limit to 'redrawtime'. */
1837 profile_setlimit(p_rdt, &syntax_tm);
1838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_FOLDING
1840 win_foldinfo.fi_level = 0;
1841#endif
1842
1843 /*
1844 * Update all the window rows.
1845 */
1846 idx = 0; /* first entry in w_lines[].wl_size */
1847 row = 0;
1848 srow = 0;
1849 lnum = wp->w_topline; /* first line shown in window */
1850 for (;;)
1851 {
1852 /* stop updating when reached the end of the window (check for _past_
1853 * the end of the window is at the end of the loop) */
1854 if (row == wp->w_height)
1855 {
1856 didline = TRUE;
1857 break;
1858 }
1859
1860 /* stop updating when hit the end of the file */
1861 if (lnum > buf->b_ml.ml_line_count)
1862 {
1863 eof = TRUE;
1864 break;
1865 }
1866
1867 /* Remember the starting row of the line that is going to be dealt
1868 * with. It is used further down when the line doesn't fit. */
1869 srow = row;
1870
1871 /*
1872 * Update a line when it is in an area that needs updating, when it
1873 * has changes or w_lines[idx] is invalid.
1874 * bot_start may be halfway a wrapped line after using
1875 * win_del_lines(), check if the current line includes it.
1876 * When syntax folding is being used, the saved syntax states will
1877 * already have been updated, we can't see where the syntax state is
1878 * the same again, just update until the end of the window.
1879 */
1880 if (row < top_end
1881 || (row >= mid_start && row < mid_end)
1882#ifdef FEAT_SEARCH_EXTRA
1883 || top_to_mod
1884#endif
1885 || idx >= wp->w_lines_valid
1886 || (row + wp->w_lines[idx].wl_size > bot_start)
1887 || (mod_top != 0
1888 && (lnum == mod_top
1889 || (lnum >= mod_top
1890 && (lnum < mod_bot
1891#ifdef FEAT_SYN_HL
1892 || did_update == DID_FOLD
1893 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001894 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 && (
1896# ifdef FEAT_FOLDING
1897 (foldmethodIsSyntax(wp)
1898 && hasAnyFolding(wp)) ||
1899# endif
1900 syntax_check_changed(lnum)))
1901#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001903 /* match in fixed position might need redraw
1904 * if lines were inserted or deleted */
1905 || (wp->w_match_head != NULL
1906 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 )))))
1909 {
1910#ifdef FEAT_SEARCH_EXTRA
1911 if (lnum == mod_top)
1912 top_to_mod = FALSE;
1913#endif
1914
1915 /*
1916 * When at start of changed lines: May scroll following lines
1917 * up or down to minimize redrawing.
1918 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001919 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 */
1921 if (lnum == mod_top
1922 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001923 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 int old_rows = 0;
1926 int new_rows = 0;
1927 int xtra_rows;
1928 linenr_T l;
1929
1930 /* Count the old number of window rows, using w_lines[], which
1931 * should still contain the sizes for the lines as they are
1932 * currently displayed. */
1933 for (i = idx; i < wp->w_lines_valid; ++i)
1934 {
1935 /* Only valid lines have a meaningful wl_lnum. Invalid
1936 * lines are part of the changed area. */
1937 if (wp->w_lines[i].wl_valid
1938 && wp->w_lines[i].wl_lnum == mod_bot)
1939 break;
1940 old_rows += wp->w_lines[i].wl_size;
1941#ifdef FEAT_FOLDING
1942 if (wp->w_lines[i].wl_valid
1943 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1944 {
1945 /* Must have found the last valid entry above mod_bot.
1946 * Add following invalid entries. */
1947 ++i;
1948 while (i < wp->w_lines_valid
1949 && !wp->w_lines[i].wl_valid)
1950 old_rows += wp->w_lines[i++].wl_size;
1951 break;
1952 }
1953#endif
1954 }
1955
1956 if (i >= wp->w_lines_valid)
1957 {
1958 /* We can't find a valid line below the changed lines,
1959 * need to redraw until the end of the window.
1960 * Inserting/deleting lines has no use. */
1961 bot_start = 0;
1962 }
1963 else
1964 {
1965 /* Able to count old number of rows: Count new window
1966 * rows, and may insert/delete lines */
1967 j = idx;
1968 for (l = lnum; l < mod_bot; ++l)
1969 {
1970#ifdef FEAT_FOLDING
1971 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1972 ++new_rows;
1973 else
1974#endif
1975#ifdef FEAT_DIFF
1976 if (l == wp->w_topline)
1977 new_rows += plines_win_nofill(wp, l, TRUE)
1978 + wp->w_topfill;
1979 else
1980#endif
1981 new_rows += plines_win(wp, l, TRUE);
1982 ++j;
1983 if (new_rows > wp->w_height - row - 2)
1984 {
1985 /* it's getting too much, must redraw the rest */
1986 new_rows = 9999;
1987 break;
1988 }
1989 }
1990 xtra_rows = new_rows - old_rows;
1991 if (xtra_rows < 0)
1992 {
1993 /* May scroll text up. If there is not enough
1994 * remaining text or scrolling fails, must redraw the
1995 * rest. If scrolling works, must redraw the text
1996 * below the scrolled text. */
1997 if (row - xtra_rows >= wp->w_height - 2)
1998 mod_bot = MAXLNUM;
1999 else
2000 {
2001 check_for_delay(FALSE);
2002 if (win_del_lines(wp, row,
2003 -xtra_rows, FALSE, FALSE) == FAIL)
2004 mod_bot = MAXLNUM;
2005 else
2006 bot_start = wp->w_height + xtra_rows;
2007 }
2008 }
2009 else if (xtra_rows > 0)
2010 {
2011 /* May scroll text down. If there is not enough
2012 * remaining text of scrolling fails, must redraw the
2013 * rest. */
2014 if (row + xtra_rows >= wp->w_height - 2)
2015 mod_bot = MAXLNUM;
2016 else
2017 {
2018 check_for_delay(FALSE);
2019 if (win_ins_lines(wp, row + old_rows,
2020 xtra_rows, FALSE, FALSE) == FAIL)
2021 mod_bot = MAXLNUM;
2022 else if (top_end > row + old_rows)
2023 /* Scrolled the part at the top that requires
2024 * updating down. */
2025 top_end += xtra_rows;
2026 }
2027 }
2028
2029 /* When not updating the rest, may need to move w_lines[]
2030 * entries. */
2031 if (mod_bot != MAXLNUM && i != j)
2032 {
2033 if (j < i)
2034 {
2035 int x = row + new_rows;
2036
2037 /* move entries in w_lines[] upwards */
2038 for (;;)
2039 {
2040 /* stop at last valid entry in w_lines[] */
2041 if (i >= wp->w_lines_valid)
2042 {
2043 wp->w_lines_valid = j;
2044 break;
2045 }
2046 wp->w_lines[j] = wp->w_lines[i];
2047 /* stop at a line that won't fit */
2048 if (x + (int)wp->w_lines[j].wl_size
2049 > wp->w_height)
2050 {
2051 wp->w_lines_valid = j + 1;
2052 break;
2053 }
2054 x += wp->w_lines[j++].wl_size;
2055 ++i;
2056 }
2057 if (bot_start > x)
2058 bot_start = x;
2059 }
2060 else /* j > i */
2061 {
2062 /* move entries in w_lines[] downwards */
2063 j -= i;
2064 wp->w_lines_valid += j;
2065 if (wp->w_lines_valid > wp->w_height)
2066 wp->w_lines_valid = wp->w_height;
2067 for (i = wp->w_lines_valid; i - j >= idx; --i)
2068 wp->w_lines[i] = wp->w_lines[i - j];
2069
2070 /* The w_lines[] entries for inserted lines are
2071 * now invalid, but wl_size may be used above.
2072 * Reset to zero. */
2073 while (i >= idx)
2074 {
2075 wp->w_lines[i].wl_size = 0;
2076 wp->w_lines[i--].wl_valid = FALSE;
2077 }
2078 }
2079 }
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083#ifdef FEAT_FOLDING
2084 /*
2085 * When lines are folded, display one line for all of them.
2086 * Otherwise, display normally (can be several display lines when
2087 * 'wrap' is on).
2088 */
2089 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2090 if (fold_count != 0)
2091 {
2092 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2093 ++row;
2094 --fold_count;
2095 wp->w_lines[idx].wl_folded = TRUE;
2096 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2097# ifdef FEAT_SYN_HL
2098 did_update = DID_FOLD;
2099# endif
2100 }
2101 else
2102#endif
2103 if (idx < wp->w_lines_valid
2104 && wp->w_lines[idx].wl_valid
2105 && wp->w_lines[idx].wl_lnum == lnum
2106 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002107 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 && srow + wp->w_lines[idx].wl_size > wp->w_height
2109#ifdef FEAT_DIFF
2110 && diff_check_fill(wp, lnum) == 0
2111#endif
2112 )
2113 {
2114 /* This line is not going to fit. Don't draw anything here,
2115 * will draw "@ " lines below. */
2116 row = wp->w_height + 1;
2117 }
2118 else
2119 {
2120#ifdef FEAT_SEARCH_EXTRA
2121 prepare_search_hl(wp, lnum);
2122#endif
2123#ifdef FEAT_SYN_HL
2124 /* Let the syntax stuff know we skipped a few lines. */
2125 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002126 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 syntax_end_parsing(syntax_last_parsed + 1);
2128#endif
2129
2130 /*
2131 * Display one line.
2132 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002133 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2134#ifdef SYN_TIME_LIMIT
2135 &syntax_tm
2136#else
2137 NULL
2138#endif
2139 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141#ifdef FEAT_FOLDING
2142 wp->w_lines[idx].wl_folded = FALSE;
2143 wp->w_lines[idx].wl_lastlnum = lnum;
2144#endif
2145#ifdef FEAT_SYN_HL
2146 did_update = DID_LINE;
2147 syntax_last_parsed = lnum;
2148#endif
2149 }
2150
2151 wp->w_lines[idx].wl_lnum = lnum;
2152 wp->w_lines[idx].wl_valid = TRUE;
2153 if (row > wp->w_height) /* past end of screen */
2154 {
2155 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002156 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2158 ++idx;
2159 break;
2160 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002161 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 wp->w_lines[idx].wl_size = row - srow;
2163 ++idx;
2164#ifdef FEAT_FOLDING
2165 lnum += fold_count + 1;
2166#else
2167 ++lnum;
2168#endif
2169 }
2170 else
2171 {
2172 /* This line does not need updating, advance to the next one */
2173 row += wp->w_lines[idx++].wl_size;
2174 if (row > wp->w_height) /* past end of screen */
2175 break;
2176#ifdef FEAT_FOLDING
2177 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2178#else
2179 ++lnum;
2180#endif
2181#ifdef FEAT_SYN_HL
2182 did_update = DID_NONE;
2183#endif
2184 }
2185
2186 if (lnum > buf->b_ml.ml_line_count)
2187 {
2188 eof = TRUE;
2189 break;
2190 }
2191 }
2192 /*
2193 * End of loop over all window lines.
2194 */
2195
2196
2197 if (idx > wp->w_lines_valid)
2198 wp->w_lines_valid = idx;
2199
2200#ifdef FEAT_SYN_HL
2201 /*
2202 * Let the syntax stuff know we stop parsing here.
2203 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002204 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 syntax_end_parsing(syntax_last_parsed + 1);
2206#endif
2207
2208 /*
2209 * If we didn't hit the end of the file, and we didn't finish the last
2210 * line we were working on, then the line didn't fit.
2211 */
2212 wp->w_empty_rows = 0;
2213#ifdef FEAT_DIFF
2214 wp->w_filler_rows = 0;
2215#endif
2216 if (!eof && !didline)
2217 {
2218 if (lnum == wp->w_topline)
2219 {
2220 /*
2221 * Single line that does not fit!
2222 * Don't overwrite it, it can be edited.
2223 */
2224 wp->w_botline = lnum + 1;
2225 }
2226#ifdef FEAT_DIFF
2227 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2228 {
2229 /* Window ends in filler lines. */
2230 wp->w_botline = lnum;
2231 wp->w_filler_rows = wp->w_height - srow;
2232 }
2233#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002234 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2235 {
2236 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2237
2238 /*
2239 * Last line isn't finished: Display "@@@" in the last screen line.
2240 */
2241 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002242 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002243 screen_fill(scr_row, scr_row + 1,
2244 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002245 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002246 set_empty_rows(wp, srow);
2247 wp->w_botline = lnum;
2248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2250 {
2251 /*
2252 * Last line isn't finished: Display "@@@" at the end.
2253 */
2254 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2255 W_WINROW(wp) + wp->w_height,
2256 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002257 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 set_empty_rows(wp, srow);
2259 wp->w_botline = lnum;
2260 }
2261 else
2262 {
2263 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2264 wp->w_botline = lnum;
2265 }
2266 }
2267 else
2268 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002269#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 draw_vsep_win(wp, row);
2271#endif
2272 if (eof) /* we hit the end of the file */
2273 {
2274 wp->w_botline = buf->b_ml.ml_line_count + 1;
2275#ifdef FEAT_DIFF
2276 j = diff_check_fill(wp, wp->w_botline);
2277 if (j > 0 && !wp->w_botfill)
2278 {
2279 /*
2280 * Display filler lines at the end of the file
2281 */
2282 if (char2cells(fill_diff) > 1)
2283 i = '-';
2284 else
2285 i = fill_diff;
2286 if (row + j > wp->w_height)
2287 j = wp->w_height - row;
2288 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2289 row += j;
2290 }
2291#endif
2292 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002293 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 wp->w_botline = lnum;
2295
2296 /* make sure the rest of the screen is blank */
2297 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002298 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300
2301 /* Reset the type of redrawing required, the window has been updated. */
2302 wp->w_redr_type = 0;
2303#ifdef FEAT_DIFF
2304 wp->w_old_topfill = wp->w_topfill;
2305 wp->w_old_botfill = wp->w_botfill;
2306#endif
2307
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002308 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 /*
2311 * There is a trick with w_botline. If we invalidate it on each
2312 * change that might modify it, this will cause a lot of expensive
2313 * calls to plines() in update_topline() each time. Therefore the
2314 * value of w_botline is often approximated, and this value is used to
2315 * compute the value of w_topline. If the value of w_botline was
2316 * wrong, check that the value of w_topline is correct (cursor is on
2317 * the visible part of the text). If it's not, we need to redraw
2318 * again. Mostly this just means scrolling up a few lines, so it
2319 * doesn't look too bad. Only do this for the current window (where
2320 * changes are relevant).
2321 */
2322 wp->w_valid |= VALID_BOTLINE;
2323 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2324 {
2325 recursive = TRUE;
2326 curwin->w_valid &= ~VALID_TOPLINE;
2327 update_topline(); /* may invalidate w_botline again */
2328 if (must_redraw != 0)
2329 {
2330 /* Don't update for changes in buffer again. */
2331 i = curbuf->b_mod_set;
2332 curbuf->b_mod_set = FALSE;
2333 win_update(curwin);
2334 must_redraw = 0;
2335 curbuf->b_mod_set = i;
2336 }
2337 recursive = FALSE;
2338 }
2339 }
2340
2341#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2342 /* restore got_int, unless CTRL-C was hit while redrawing */
2343 if (!got_int)
2344 got_int = save_got_int;
2345#endif
2346}
2347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348/*
2349 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2350 * as the filler character.
2351 */
2352 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002353win_draw_end(
2354 win_T *wp,
2355 int c1,
2356 int c2,
2357 int row,
2358 int endrow,
2359 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2362 int n = 0;
2363# define FDC_OFF n
2364#else
2365# define FDC_OFF 0
2366#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002367#ifdef FEAT_FOLDING
2368 int fdc = compute_foldcolumn(wp, 0);
2369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371#ifdef FEAT_RIGHTLEFT
2372 if (wp->w_p_rl)
2373 {
2374 /* No check for cmdline window: should never be right-left. */
2375# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002376 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 if (n > 0)
2379 {
2380 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002381 if (n > W_WIDTH(wp))
2382 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2384 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002385 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387# endif
2388# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002389 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
2391 int nn = n + 2;
2392
2393 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002394 if (nn > W_WIDTH(wp))
2395 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2397 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002398 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 n = nn;
2400 }
2401# endif
2402 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2403 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002404 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2406 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002407 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else
2410#endif
2411 {
2412#ifdef FEAT_CMDWIN
2413 if (cmdwin_type != 0 && wp == curwin)
2414 {
2415 /* draw the cmdline character in the leftmost column */
2416 n = 1;
2417 if (n > wp->w_width)
2418 n = wp->w_width;
2419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2420 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423#endif
2424#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002425 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002427 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 /* draw the fold column at the left */
2430 if (nn > W_WIDTH(wp))
2431 nn = W_WIDTH(wp);
2432 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2433 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002434 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 n = nn;
2436 }
2437#endif
2438#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002439 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441 int nn = n + 2;
2442
2443 /* draw the sign column after the fold column */
2444 if (nn > W_WIDTH(wp))
2445 nn = W_WIDTH(wp);
2446 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2447 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002448 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 n = nn;
2450 }
2451#endif
2452 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2453 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002454 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456 set_empty_rows(wp, row);
2457}
2458
Bram Moolenaar1a384422010-07-14 19:53:30 +02002459#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002460static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002461
2462/*
2463 * Advance **color_cols and return TRUE when there are columns to draw.
2464 */
2465 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002466advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002467{
2468 while (**color_cols >= 0 && vcol > **color_cols)
2469 ++*color_cols;
2470 return (**color_cols >= 0);
2471}
2472#endif
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#ifdef FEAT_FOLDING
2475/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002476 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2477 * space is available for window "wp", minus "col".
2478 */
2479 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002480compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002481{
2482 int fdc = wp->w_p_fdc;
2483 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2484 int wwidth = W_WIDTH(wp);
2485
2486 if (fdc > wwidth - (col + wmw))
2487 fdc = wwidth - (col + wmw);
2488 return fdc;
2489}
2490
2491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 * Display one folded line.
2493 */
2494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002495fold_line(
2496 win_T *wp,
2497 long fold_count,
2498 foldinfo_T *foldinfo,
2499 linenr_T lnum,
2500 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002502 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 pos_T *top, *bot;
2504 linenr_T lnume = lnum + fold_count - 1;
2505 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002506 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 int col;
2509 int txtcol;
2510 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002511 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 /* Build the fold line:
2514 * 1. Add the cmdwin_type for the command-line window
2515 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002516 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * 4. Compose the text
2518 * 5. Add the text
2519 * 6. set highlighting for the Visual area an other text
2520 */
2521 col = 0;
2522
2523 /*
2524 * 1. Add the cmdwin_type for the command-line window
2525 * Ignores 'rightleft', this window is never right-left.
2526 */
2527#ifdef FEAT_CMDWIN
2528 if (cmdwin_type != 0 && wp == curwin)
2529 {
2530 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002531 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#ifdef FEAT_MBYTE
2533 if (enc_utf8)
2534 ScreenLinesUC[off] = 0;
2535#endif
2536 ++col;
2537 }
2538#endif
2539
2540 /*
2541 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002542 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002544 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (fdc > 0)
2546 {
2547 fill_foldcolumn(buf, wp, TRUE, lnum);
2548#ifdef FEAT_RIGHTLEFT
2549 if (wp->w_p_rl)
2550 {
2551 int i;
2552
2553 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002554 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 /* reverse the fold column */
2556 for (i = 0; i < fdc; ++i)
2557 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2558 }
2559 else
2560#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002561 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 col += fdc;
2563 }
2564
2565#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002566# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2567 for (ri = 0; ri < l; ++ri) \
2568 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2569 else \
2570 for (ri = 0; ri < l; ++ri) \
2571 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002573# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2574 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575#endif
2576
Bram Moolenaar64486672010-05-16 15:46:46 +02002577 /* Set all attributes of the 'number' or 'relativenumber' column and the
2578 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002579 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581#ifdef FEAT_SIGNS
2582 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002583 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
2585 len = W_WIDTH(wp) - col;
2586 if (len > 0)
2587 {
2588 if (len > 2)
2589 len = 2;
2590# ifdef FEAT_RIGHTLEFT
2591 if (wp->w_p_rl)
2592 /* the line number isn't reversed */
2593 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002594 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 else
2596# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002597 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 col += len;
2599 }
2600 }
2601#endif
2602
2603 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002604 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002606 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
2608 len = W_WIDTH(wp) - col;
2609 if (len > 0)
2610 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002611 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002612 long num;
2613 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002614
2615 if (len > w + 1)
2616 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002617
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002618 if (wp->w_p_nu && !wp->w_p_rnu)
2619 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002620 num = (long)lnum;
2621 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002622 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002623 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002624 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002625 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002626 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002627 /* 'number' + 'relativenumber': cursor line shows absolute
2628 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002629 num = lnum;
2630 fmt = "%-*ld ";
2631 }
2632 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002633
Bram Moolenaar700e7342013-01-30 12:31:36 +01002634 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#ifdef FEAT_RIGHTLEFT
2636 if (wp->w_p_rl)
2637 /* the line number isn't reversed */
2638 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002639 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 else
2641#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002642 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 col += len;
2644 }
2645 }
2646
2647 /*
2648 * 4. Compose the folded-line string with 'foldtext', if set.
2649 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002650 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 txtcol = col; /* remember where text starts */
2653
2654 /*
2655 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2656 * Right-left text is put in columns 0 - number-col, normal text is put
2657 * in columns number-col - window-width.
2658 */
2659#ifdef FEAT_MBYTE
2660 if (has_mbyte)
2661 {
2662 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002663 int u8c, u8cc[MAX_MCO];
2664 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 int idx;
2666 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002667 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668# ifdef FEAT_ARABIC
2669 int prev_c = 0; /* previous Arabic character */
2670 int prev_c1 = 0; /* first composing char for prev_c */
2671# endif
2672
2673# ifdef FEAT_RIGHTLEFT
2674 if (wp->w_p_rl)
2675 idx = off;
2676 else
2677# endif
2678 idx = off + col;
2679
2680 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2681 for (p = text; *p != NUL; )
2682 {
2683 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002684 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (col + cells > W_WIDTH(wp)
2686# ifdef FEAT_RIGHTLEFT
2687 - (wp->w_p_rl ? col : 0)
2688# endif
2689 )
2690 break;
2691 ScreenLines[idx] = *p;
2692 if (enc_utf8)
2693 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002694 u8c = utfc_ptr2char(p, u8cc);
2695 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 ScreenLinesUC[idx] = 0;
2698#ifdef FEAT_ARABIC
2699 prev_c = u8c;
2700#endif
2701 }
2702 else
2703 {
2704#ifdef FEAT_ARABIC
2705 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2706 {
2707 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002708 int pc, pc1, nc;
2709 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 int firstbyte = *p;
2711
2712 /* The idea of what is the previous and next
2713 * character depends on 'rightleft'. */
2714 if (wp->w_p_rl)
2715 {
2716 pc = prev_c;
2717 pc1 = prev_c1;
2718 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002719 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else
2722 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002723 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002725 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
2727 prev_c = u8c;
2728
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 pc, pc1, nc);
2731 ScreenLines[idx] = firstbyte;
2732 }
2733 else
2734 prev_c = u8c;
2735#endif
2736 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002737#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (u8c >= 0x10000)
2739 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2740 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002743 for (i = 0; i < Screen_mco; ++i)
2744 {
2745 ScreenLinesC[i][idx] = u8cc[i];
2746 if (u8cc[i] == 0)
2747 break;
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 if (cells > 1)
2751 ScreenLines[idx + 1] = 0;
2752 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002753 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2754 /* double-byte single width character */
2755 ScreenLines2[idx] = p[1];
2756 else if (cells > 1)
2757 /* double-width character */
2758 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 col += cells;
2760 idx += cells;
2761 p += c_len;
2762 }
2763 }
2764 else
2765#endif
2766 {
2767 len = (int)STRLEN(text);
2768 if (len > W_WIDTH(wp) - col)
2769 len = W_WIDTH(wp) - col;
2770 if (len > 0)
2771 {
2772#ifdef FEAT_RIGHTLEFT
2773 if (wp->w_p_rl)
2774 STRNCPY(current_ScreenLine, text, len);
2775 else
2776#endif
2777 STRNCPY(current_ScreenLine + col, text, len);
2778 col += len;
2779 }
2780 }
2781
2782 /* Fill the rest of the line with the fold filler */
2783#ifdef FEAT_RIGHTLEFT
2784 if (wp->w_p_rl)
2785 col -= txtcol;
2786#endif
2787 while (col < W_WIDTH(wp)
2788#ifdef FEAT_RIGHTLEFT
2789 - (wp->w_p_rl ? txtcol : 0)
2790#endif
2791 )
2792 {
2793#ifdef FEAT_MBYTE
2794 if (enc_utf8)
2795 {
2796 if (fill_fold >= 0x80)
2797 {
2798 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002799 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002805 ScreenLines[off + col] = fill_fold;
2806 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002807 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002809 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002811 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813
2814 if (text != buf)
2815 vim_free(text);
2816
2817 /*
2818 * 6. set highlighting for the Visual area an other text.
2819 * If all folded lines are in the Visual area, highlight the line.
2820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2822 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002823 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
2825 /* Visual is after curwin->w_cursor */
2826 top = &curwin->w_cursor;
2827 bot = &VIsual;
2828 }
2829 else
2830 {
2831 /* Visual is before curwin->w_cursor */
2832 top = &VIsual;
2833 bot = &curwin->w_cursor;
2834 }
2835 if (lnum >= top->lnum
2836 && lnume <= bot->lnum
2837 && (VIsual_mode != 'v'
2838 || ((lnum > top->lnum
2839 || (lnum == top->lnum
2840 && top->col == 0))
2841 && (lnume < bot->lnum
2842 || (lnume == bot->lnum
2843 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002844 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 if (VIsual_mode == Ctrl_V)
2847 {
2848 /* Visual block mode: highlight the chars part of the block */
2849 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2850 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002851 if (wp->w_old_cursor_lcol != MAXCOL
2852 && wp->w_old_cursor_lcol + txtcol
2853 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 len = wp->w_old_cursor_lcol;
2855 else
2856 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002857 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002858 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
2860 }
2861 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002864 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002869#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002870 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2871 if (wp->w_p_cc_cols)
2872 {
2873 int i = 0;
2874 int j = wp->w_p_cc_cols[i];
2875 int old_txtcol = txtcol;
2876
2877 while (j > -1)
2878 {
2879 txtcol += j;
2880 if (wp->w_p_wrap)
2881 txtcol -= wp->w_skipcol;
2882 else
2883 txtcol -= wp->w_leftcol;
2884 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2885 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002886 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002887 txtcol = old_txtcol;
2888 j = wp->w_p_cc_cols[++i];
2889 }
2890 }
2891
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002892 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002893 if (wp->w_p_cuc)
2894 {
2895 txtcol += wp->w_virtcol;
2896 if (wp->w_p_wrap)
2897 txtcol -= wp->w_skipcol;
2898 else
2899 txtcol -= wp->w_leftcol;
2900 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2901 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002902 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002903 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002906 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 (int)W_WIDTH(wp), FALSE);
2908
2909 /*
2910 * Update w_cline_height and w_cline_folded if the cursor line was
2911 * updated (saves a call to plines() later).
2912 */
2913 if (wp == curwin
2914 && lnum <= curwin->w_cursor.lnum
2915 && lnume >= curwin->w_cursor.lnum)
2916 {
2917 curwin->w_cline_row = row;
2918 curwin->w_cline_height = 1;
2919 curwin->w_cline_folded = TRUE;
2920 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2921 }
2922}
2923
2924/*
2925 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2926 */
2927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002928copy_text_attr(
2929 int off,
2930 char_u *buf,
2931 int len,
2932 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002934 int i;
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 mch_memmove(ScreenLines + off, buf, (size_t)len);
2937# ifdef FEAT_MBYTE
2938 if (enc_utf8)
2939 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2940# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002941 for (i = 0; i < len; ++i)
2942 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943}
2944
2945/*
2946 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002947 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 */
2949 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002950fill_foldcolumn(
2951 char_u *p,
2952 win_T *wp,
2953 int closed, /* TRUE of FALSE */
2954 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 int i = 0;
2957 int level;
2958 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002959 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002960 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002963 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 level = win_foldinfo.fi_level;
2966 if (level > 0)
2967 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002968 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002969 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 /* If the column is too narrow, we start at the lowest level that
2972 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002973 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (first_level < 1)
2975 first_level = 1;
2976
Bram Moolenaar1c934292015-01-27 16:39:29 +01002977 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 if (win_foldinfo.fi_lnum == lnum
2980 && first_level + i >= win_foldinfo.fi_low_level)
2981 p[i] = '-';
2982 else if (first_level == 1)
2983 p[i] = '|';
2984 else if (first_level + i <= 9)
2985 p[i] = '0' + first_level + i;
2986 else
2987 p[i] = '>';
2988 if (first_level + i == level)
2989 break;
2990 }
2991 }
2992 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002993 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995#endif /* FEAT_FOLDING */
2996
2997/*
2998 * Display line "lnum" of window 'wp' on the screen.
2999 * Start at row "startrow", stop when "endrow" is reached.
3000 * wp->w_virtcol needs to be valid.
3001 *
3002 * Return the number of last row the line occupies.
3003 */
3004 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003005win_line(
3006 win_T *wp,
3007 linenr_T lnum,
3008 int startrow,
3009 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003010 int nochange UNUSED, /* not updating for changed text */
3011 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003013 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3015 int c = 0; /* init for GCC */
3016 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003017#ifdef FEAT_LINEBREAK
3018 long vcol_sbr = -1; /* virtual column after showbreak */
3019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 long vcol_prev = -1; /* "vcol" of previous character */
3021 char_u *line; /* current line */
3022 char_u *ptr; /* current position in "line" */
3023 int row; /* row in the window, excl w_winrow */
3024 int screen_row; /* row on the screen, incl w_winrow */
3025
3026 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3027 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003028 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003029 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 int c_extra = NUL; /* extra chars, all the same */
3031 int extra_attr = 0; /* attributes when n_extra != 0 */
3032 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3033 displaying lcs_eol at end-of-line */
3034 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3035 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3036
3037 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3038 int saved_n_extra = 0;
3039 char_u *saved_p_extra = NULL;
3040 int saved_c_extra = 0;
3041 int saved_char_attr = 0;
3042
3043 int n_attr = 0; /* chars with special attr */
3044 int saved_attr2 = 0; /* char_attr saved for n_attr */
3045 int n_attr3 = 0; /* chars with overruling special attr */
3046 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3047
3048 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3049
3050 int fromcol, tocol; /* start/end of inverting */
3051 int fromcol_prev = -2; /* start of inverting after cursor */
3052 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003054 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 pos_T pos;
3056 long v;
3057
3058 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003059 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3061 in this line */
3062 int attr = 0; /* attributes for area highlighting */
3063 int area_attr = 0; /* attributes desired by highlighting */
3064 int search_attr = 0; /* attributes desired by 'hlsearch' */
3065#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003066 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 int syntax_attr = 0; /* attributes desired by syntax */
3068 int has_syntax = FALSE; /* this buffer has syntax highl. */
3069 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003070 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003071 int draw_color_col = FALSE; /* highlight colorcolumn */
3072 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003073#endif
3074#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003075 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003076# define SPWORDLEN 150
3077 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003078 int nextlinecol = 0; /* column where nextline[] starts */
3079 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003080 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003081 int spell_attr = 0; /* attributes desired by spelling */
3082 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003083 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3084 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003086 static int cap_col = -1; /* column to check for Cap word */
3087 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003088 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089#endif
3090 int extra_check; /* has syntax or linebreak */
3091#ifdef FEAT_MBYTE
3092 int multi_attr = 0; /* attributes desired by multibyte */
3093 int mb_l = 1; /* multi-byte byte length */
3094 int mb_c = 0; /* decoded multi-byte character */
3095 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003096 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097#endif
3098#ifdef FEAT_DIFF
3099 int filler_lines; /* nr of filler lines to be drawn */
3100 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003101 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 int change_start = MAXCOL; /* first col of changed area */
3103 int change_end = -1; /* last col of changed area */
3104#endif
3105 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3106#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003107 int need_showbreak = FALSE; /* overlong line, skipping first x
3108 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003110#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3111 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003113 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
3115#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003116 matchitem_T *cur; /* points to the match list */
3117 match_T *shl; /* points to search_hl or a match */
3118 int shl_flag; /* flag to indicate whether search_hl
3119 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003120 int pos_inprogress; /* marks that position match search is
3121 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003122 int prevcol_hl_flag; /* flag to indicate whether prevcol
3123 equals startcol of search_hl or one
3124 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#endif
3126#ifdef FEAT_ARABIC
3127 int prev_c = 0; /* previous Arabic character */
3128 int prev_c1 = 0; /* first composing char for prev_c */
3129#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003130#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003131 int did_line_attr = 0;
3132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /* draw_state: items that are drawn in sequence: */
3135#define WL_START 0 /* nothing done yet */
3136#ifdef FEAT_CMDWIN
3137# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3138#else
3139# define WL_CMDLINE WL_START
3140#endif
3141#ifdef FEAT_FOLDING
3142# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3143#else
3144# define WL_FOLD WL_CMDLINE
3145#endif
3146#ifdef FEAT_SIGNS
3147# define WL_SIGN WL_FOLD + 1 /* column for signs */
3148#else
3149# define WL_SIGN WL_FOLD /* column for signs */
3150#endif
3151#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003152#ifdef FEAT_LINEBREAK
3153# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003155# define WL_BRI WL_NR
3156#endif
3157#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3158# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3159#else
3160# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#define WL_LINE WL_SBR + 1 /* text in the line */
3163 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003164#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int feedback_col = 0;
3166 int feedback_old_attr = -1;
3167#endif
3168
Bram Moolenaar860cae12010-06-05 23:22:07 +02003169#ifdef FEAT_CONCEAL
3170 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003171 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003172 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003173 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003174 int is_concealing = FALSE;
3175 int boguscols = 0; /* nonexistent columns added to force
3176 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003177 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003178 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003179 int match_conc = 0; /* cchar for match functions */
3180 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003181 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003182# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003183# define FIX_FOR_BOGUSCOLS \
3184 { \
3185 n_extra += vcol_off; \
3186 vcol -= vcol_off; \
3187 vcol_off = 0; \
3188 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003189 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003190 boguscols = 0; \
3191 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003192#else
3193# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 if (startrow > endrow) /* past the end already! */
3197 return startrow;
3198
3199 row = startrow;
3200 screen_row = row + W_WINROW(wp);
3201
3202 /*
3203 * To speed up the loop below, set extra_check when there is linebreak,
3204 * trailing white space and/or syntax processing to be done.
3205 */
3206#ifdef FEAT_LINEBREAK
3207 extra_check = wp->w_p_lbr;
3208#else
3209 extra_check = 0;
3210#endif
3211#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003212 if (syntax_present(wp) && !wp->w_s->b_syn_error
3213# ifdef SYN_TIME_LIMIT
3214 && !wp->w_s->b_syn_slow
3215# endif
3216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
3218 /* Prepare for syntax highlighting in this line. When there is an
3219 * error, stop syntax highlighting. */
3220 save_did_emsg = did_emsg;
3221 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003222 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003224 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
3226 {
3227 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003228#ifdef SYN_TIME_LIMIT
3229 if (!wp->w_s->b_syn_slow)
3230#endif
3231 {
3232 has_syntax = TRUE;
3233 extra_check = TRUE;
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
3236 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003237
3238 /* Check for columns to display for 'colorcolumn'. */
3239 color_cols = wp->w_p_cc_cols;
3240 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003241 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003242#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003243
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003244#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003245 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003246 && *wp->w_s->b_p_spl != NUL
3247 && wp->w_s->b_langp.ga_len > 0
3248 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003249 {
3250 /* Prepare for spell checking. */
3251 has_spell = TRUE;
3252 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003253
3254 /* Get the start of the next line, so that words that wrap to the next
3255 * line are found too: "et<line-break>al.".
3256 * Trick: skip a few chars for C/shell/Vim comments */
3257 nextline[SPWORDLEN] = NUL;
3258 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3259 {
3260 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3261 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3262 }
3263
3264 /* When a word wrapped from the previous line the start of the current
3265 * line is valid. */
3266 if (lnum == checked_lnum)
3267 cur_checked_col = checked_col;
3268 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003269
3270 /* When there was a sentence end in the previous line may require a
3271 * word starting with capital in this line. In line 1 always check
3272 * the first word. */
3273 if (lnum != capcol_lnum)
3274 cap_col = -1;
3275 if (lnum == 1)
3276 cap_col = 0;
3277 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280
3281 /*
3282 * handle visual active in this window
3283 */
3284 fromcol = -10;
3285 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3287 {
3288 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003289 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
3291 top = &curwin->w_cursor;
3292 bot = &VIsual;
3293 }
3294 else /* Visual is before curwin->w_cursor */
3295 {
3296 top = &VIsual;
3297 bot = &curwin->w_cursor;
3298 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003299 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (VIsual_mode == Ctrl_V) /* block mode */
3301 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003302 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 fromcol = wp->w_old_cursor_fcol;
3305 tocol = wp->w_old_cursor_lcol;
3306 }
3307 }
3308 else /* non-block mode */
3309 {
3310 if (lnum > top->lnum && lnum <= bot->lnum)
3311 fromcol = 0;
3312 else if (lnum == top->lnum)
3313 {
3314 if (VIsual_mode == 'V') /* linewise */
3315 fromcol = 0;
3316 else
3317 {
3318 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3319 if (gchar_pos(top) == NUL)
3320 tocol = fromcol + 1;
3321 }
3322 }
3323 if (VIsual_mode != 'V' && lnum == bot->lnum)
3324 {
3325 if (*p_sel == 'e' && bot->col == 0
3326#ifdef FEAT_VIRTUALEDIT
3327 && bot->coladd == 0
3328#endif
3329 )
3330 {
3331 fromcol = -10;
3332 tocol = MAXCOL;
3333 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003334 else if (bot->col == MAXCOL)
3335 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 else
3337 {
3338 pos = *bot;
3339 if (*p_sel == 'e')
3340 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3341 else
3342 {
3343 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3344 ++tocol;
3345 }
3346 }
3347 }
3348 }
3349
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 /* Check if the character under the cursor should not be inverted */
3351 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003352#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 )
3356 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358 /* if inverting in this line set area_highlighting */
3359 if (fromcol >= 0)
3360 {
3361 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003362 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003364 if ((clip_star.available && !clip_star.owned
3365 && clip_isautosel_star())
3366 || (clip_plus.available && !clip_plus.owned
3367 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003368 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
3370 }
3371 }
3372
3373 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003374 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003376 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 && wp == curwin
3378 && lnum >= curwin->w_cursor.lnum
3379 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3380 {
3381 if (lnum == curwin->w_cursor.lnum)
3382 getvcol(curwin, &(curwin->w_cursor),
3383 (colnr_T *)&fromcol, NULL, NULL);
3384 else
3385 fromcol = 0;
3386 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3387 {
3388 pos.lnum = lnum;
3389 pos.col = search_match_endcol;
3390 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3391 }
3392 else
3393 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003394 /* do at least one character; happens when past end of line */
3395 if (fromcol == tocol)
3396 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003398 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400
3401#ifdef FEAT_DIFF
3402 filler_lines = diff_check(wp, lnum);
3403 if (filler_lines < 0)
3404 {
3405 if (filler_lines == -1)
3406 {
3407 if (diff_find_change(wp, lnum, &change_start, &change_end))
3408 diff_hlf = HLF_ADD; /* added line */
3409 else if (change_start == 0)
3410 diff_hlf = HLF_TXD; /* changed text */
3411 else
3412 diff_hlf = HLF_CHD; /* changed line */
3413 }
3414 else
3415 diff_hlf = HLF_ADD; /* added line */
3416 filler_lines = 0;
3417 area_highlighting = TRUE;
3418 }
3419 if (lnum == wp->w_topline)
3420 filler_lines = wp->w_topfill;
3421 filler_todo = filler_lines;
3422#endif
3423
3424#ifdef LINE_ATTR
3425# ifdef FEAT_SIGNS
3426 /* If this line has a sign with line highlighting set line_attr. */
3427 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3428 if (v != 0)
3429 line_attr = sign_get_attr((int)v, TRUE);
3430# endif
3431# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3432 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003433 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003434 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435# endif
3436 if (line_attr != 0)
3437 area_highlighting = TRUE;
3438#endif
3439
3440 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3441 ptr = line;
3442
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003443#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003444 if (has_spell)
3445 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003446 /* For checking first word with a capital skip white space. */
3447 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003448 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003449
Bram Moolenaar30abd282005-06-22 22:35:10 +00003450 /* To be able to spell-check over line boundaries copy the end of the
3451 * current line into nextline[]. Above the start of the next line was
3452 * copied to nextline[SPWORDLEN]. */
3453 if (nextline[SPWORDLEN] == NUL)
3454 {
3455 /* No next line or it is empty. */
3456 nextlinecol = MAXCOL;
3457 nextline_idx = 0;
3458 }
3459 else
3460 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003462 if (v < SPWORDLEN)
3463 {
3464 /* Short line, use it completely and append the start of the
3465 * next line. */
3466 nextlinecol = 0;
3467 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003468 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003469 nextline_idx = v + 1;
3470 }
3471 else
3472 {
3473 /* Long line, use only the last SPWORDLEN bytes. */
3474 nextlinecol = v - SPWORDLEN;
3475 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3476 nextline_idx = SPWORDLEN + 1;
3477 }
3478 }
3479 }
3480#endif
3481
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003482 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003484 if (lcs_space || lcs_trail)
3485 extra_check = TRUE;
3486 /* find start of trailing whitespace */
3487 if (lcs_trail)
3488 {
3489 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003490 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003491 --trailcol;
3492 trailcol += (colnr_T) (ptr - line);
3493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
3495
3496 /*
3497 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3498 * first character to be displayed.
3499 */
3500 if (wp->w_p_wrap)
3501 v = wp->w_skipcol;
3502 else
3503 v = wp->w_leftcol;
3504 if (v > 0)
3505 {
3506#ifdef FEAT_MBYTE
3507 char_u *prev_ptr = ptr;
3508#endif
3509 while (vcol < v && *ptr != NUL)
3510 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003511 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 vcol += c;
3513#ifdef FEAT_MBYTE
3514 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003516 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003519 /* When:
3520 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003521 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003522 * - 'virtualedit' is set, or
3523 * - the visual mode is active,
3524 * the end of the line may be before the start of the displayed part.
3525 */
3526 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003527#ifdef FEAT_SYN_HL
3528 wp->w_p_cuc || draw_color_col ||
3529#endif
3530#ifdef FEAT_VIRTUALEDIT
3531 virtual_active() ||
3532#endif
3533 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 /* Handle a character that's not completely on the screen: Put ptr at
3539 * that character but skip the first few screen characters. */
3540 if (vcol > v)
3541 {
3542 vcol -= c;
3543#ifdef FEAT_MBYTE
3544 ptr = prev_ptr;
3545#else
3546 --ptr;
3547#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003548 /* If the character fits on the screen, don't need to skip it.
3549 * Except for a TAB. */
3550 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003551#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003552 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003553#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003554 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003555 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557
3558 /*
3559 * Adjust for when the inverted text is before the screen,
3560 * and when the start of the inverted text is before the screen.
3561 */
3562 if (tocol <= vcol)
3563 fromcol = 0;
3564 else if (fromcol >= 0 && fromcol < vcol)
3565 fromcol = vcol;
3566
3567#ifdef FEAT_LINEBREAK
3568 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3569 if (wp->w_p_wrap)
3570 need_showbreak = TRUE;
3571#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003572#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003573 /* When spell checking a word we need to figure out the start of the
3574 * word and if it's badly spelled or not. */
3575 if (has_spell)
3576 {
3577 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003578 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003579 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003580
3581 pos = wp->w_cursor;
3582 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003583 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003584 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003585
3586 /* spell_move_to() may call ml_get() and make "line" invalid */
3587 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3588 ptr = line + linecol;
3589
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003590 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003591 {
3592 /* no bad word found at line start, don't check until end of a
3593 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003594 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003595 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003596 }
3597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003598 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003599 /* bad word found, use attributes until end of word */
3600 word_end = wp->w_cursor.col + len + 1;
3601
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003602 /* Turn index into actual attributes. */
3603 if (spell_hlf != HLF_COUNT)
3604 spell_attr = highlight_attr[spell_hlf];
3605 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003606 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003607
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003608# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003609 /* Need to restart syntax highlighting for this line. */
3610 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003611 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003612# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003613 }
3614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616
3617 /*
3618 * Correct highlighting for cursor that can't be disabled.
3619 * Avoids having to check this for each character.
3620 */
3621 if (fromcol >= 0)
3622 {
3623 if (noinvcur)
3624 {
3625 if ((colnr_T)fromcol == wp->w_virtcol)
3626 {
3627 /* highlighting starts at cursor, let it start just after the
3628 * cursor */
3629 fromcol_prev = fromcol;
3630 fromcol = -1;
3631 }
3632 else if ((colnr_T)fromcol < wp->w_virtcol)
3633 /* restart highlighting after the cursor */
3634 fromcol_prev = wp->w_virtcol;
3635 }
3636 if (fromcol >= tocol)
3637 fromcol = -1;
3638 }
3639
3640#ifdef FEAT_SEARCH_EXTRA
3641 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003642 * Handle highlighting the last used search pattern and matches.
3643 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003645 cur = wp->w_match_head;
3646 shl_flag = FALSE;
3647 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003649 if (shl_flag == FALSE)
3650 {
3651 shl = &search_hl;
3652 shl_flag = TRUE;
3653 }
3654 else
3655 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003656 shl->startcol = MAXCOL;
3657 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003659 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003660 v = (long)(ptr - line);
3661 if (cur != NULL)
3662 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003663 next_search_hl(wp, shl, lnum, (colnr_T)v,
3664 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003665
3666 /* Need to get the line again, a multi-line regexp may have made it
3667 * invalid. */
3668 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3669 ptr = line + v;
3670
3671 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003673 if (shl->lnum == lnum)
3674 shl->startcol = shl->rm.startpos[0].col;
3675 else
3676 shl->startcol = 0;
3677 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3678 - shl->rm.startpos[0].lnum)
3679 shl->endcol = shl->rm.endpos[0].col;
3680 else
3681 shl->endcol = MAXCOL;
3682 /* Highlight one character for an empty match. */
3683 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003686 if (has_mbyte && line[shl->endcol] != NUL)
3687 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003690 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003692 if ((long)shl->startcol < v) /* match at leftcol */
3693 {
3694 shl->attr_cur = shl->attr;
3695 search_attr = shl->attr;
3696 }
3697 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003699 if (shl != &search_hl && cur != NULL)
3700 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702#endif
3703
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003704#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003705 /* Cursor line highlighting for 'cursorline' in the current window. Not
3706 * when Visual mode is active, because it's not clear what is selected
3707 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003708 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003709 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003710 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003711 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003712 area_highlighting = TRUE;
3713 }
3714#endif
3715
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003716 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 col = 0;
3718#ifdef FEAT_RIGHTLEFT
3719 if (wp->w_p_rl)
3720 {
3721 /* Rightleft window: process the text in the normal direction, but put
3722 * it in current_ScreenLine[] from right to left. Start at the
3723 * rightmost column of the window. */
3724 col = W_WIDTH(wp) - 1;
3725 off += col;
3726 }
3727#endif
3728
3729 /*
3730 * Repeat for the whole displayed line.
3731 */
3732 for (;;)
3733 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003734#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003735 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 /* Skip this quickly when working on the text. */
3738 if (draw_state != WL_LINE)
3739 {
3740#ifdef FEAT_CMDWIN
3741 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3742 {
3743 draw_state = WL_CMDLINE;
3744 if (cmdwin_type != 0 && wp == curwin)
3745 {
3746 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003748 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003749 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 }
3752#endif
3753
3754#ifdef FEAT_FOLDING
3755 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3756 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003757 int fdc = compute_foldcolumn(wp, 0);
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003760 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003762 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003763 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003764 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003765 p_extra_free = alloc(12 + 1);
3766
3767 if (p_extra_free != NULL)
3768 {
3769 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3770 n_extra = fdc;
3771 p_extra_free[n_extra] = NUL;
3772 p_extra = p_extra_free;
3773 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003774 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
3777 }
3778#endif
3779
3780#ifdef FEAT_SIGNS
3781 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3782 {
3783 draw_state = WL_SIGN;
3784 /* Show the sign column when there are any signs in this
3785 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003786 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003788 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003790 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791# endif
3792
3793 /* Draw two cells with the sign value or blank. */
3794 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003795 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 n_extra = 2;
3797
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003798 if (row == startrow
3799#ifdef FEAT_DIFF
3800 + filler_lines && filler_todo <= 0
3801#endif
3802 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3805 SIGN_TEXT);
3806# ifdef FEAT_SIGN_ICONS
3807 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3808 SIGN_ICON);
3809 if (gui.in_use && icon_sign != 0)
3810 {
3811 /* Use the image in this position. */
3812 c_extra = SIGN_BYTE;
3813# ifdef FEAT_NETBEANS_INTG
3814 if (buf_signcount(wp->w_buffer, lnum) > 1)
3815 c_extra = MULTISIGN_BYTE;
3816# endif
3817 char_attr = icon_sign;
3818 }
3819 else
3820# endif
3821 if (text_sign != 0)
3822 {
3823 p_extra = sign_get_text(text_sign);
3824 if (p_extra != NULL)
3825 {
3826 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003827 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829 char_attr = sign_get_attr(text_sign, FALSE);
3830 }
3831 }
3832 }
3833 }
3834#endif
3835
3836 if (draw_state == WL_NR - 1 && n_extra == 0)
3837 {
3838 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003839 /* Display the absolute or relative line number. After the
3840 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3841 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 && (row == startrow
3843#ifdef FEAT_DIFF
3844 + filler_lines
3845#endif
3846 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3847 {
3848 /* Draw the line number (empty space after wrapping). */
3849 if (row == startrow
3850#ifdef FEAT_DIFF
3851 + filler_lines
3852#endif
3853 )
3854 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003855 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003856 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003857
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003858 if (wp->w_p_nu && !wp->w_p_rnu)
3859 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003860 num = (long)lnum;
3861 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003862 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003863 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003864 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003865 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003866 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003867 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003868 num = lnum;
3869 fmt = "%-*ld ";
3870 }
3871 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003872
Bram Moolenaar700e7342013-01-30 12:31:36 +01003873 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003874 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (wp->w_skipcol > 0)
3876 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3877 *p_extra = '-';
3878#ifdef FEAT_RIGHTLEFT
3879 if (wp->w_p_rl) /* reverse line numbers */
3880 rl_mirror(extra);
3881#endif
3882 p_extra = extra;
3883 c_extra = NUL;
3884 }
3885 else
3886 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003887 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003888 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003889#ifdef FEAT_SYN_HL
3890 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003891 * the current line differently.
3892 * TODO: Can we use CursorLine instead of CursorLineNr
3893 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003894 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003895 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003896 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899 }
3900
Bram Moolenaar597a4222014-06-25 14:39:50 +02003901#ifdef FEAT_LINEBREAK
3902 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3903 && n_extra == 0 && *p_sbr != NUL)
3904 /* draw indent after showbreak value */
3905 draw_state = WL_BRI;
3906 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3907 /* After the showbreak, draw the breakindent */
3908 draw_state = WL_BRI - 1;
3909
3910 /* draw 'breakindent': indent wrapped text accordingly */
3911 if (draw_state == WL_BRI - 1 && n_extra == 0)
3912 {
3913 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003914 /* if need_showbreak is set, breakindent also applies */
3915 if (wp->w_p_bri && n_extra == 0
3916 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003917# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003918 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003919# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003920 )
3921 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003922 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003923# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003924 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003925 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003926 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003927# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003928 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3929 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003930 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003931# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003932 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003933# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003934 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003935 c_extra = ' ';
3936 n_extra = get_breakindent_win(wp,
3937 ml_get_buf(wp->w_buffer, lnum, FALSE));
3938 /* Correct end of highlighted area for 'breakindent',
3939 * required when 'linebreak' is also set. */
3940 if (tocol == vcol)
3941 tocol += n_extra;
3942 }
3943 }
3944#endif
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3947 if (draw_state == WL_SBR - 1 && n_extra == 0)
3948 {
3949 draw_state = WL_SBR;
3950# ifdef FEAT_DIFF
3951 if (filler_todo > 0)
3952 {
3953 /* Draw "deleted" diff line(s). */
3954 if (char2cells(fill_diff) > 1)
3955 c_extra = '-';
3956 else
3957 c_extra = fill_diff;
3958# ifdef FEAT_RIGHTLEFT
3959 if (wp->w_p_rl)
3960 n_extra = col + 1;
3961 else
3962# endif
3963 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003964 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966# endif
3967# ifdef FEAT_LINEBREAK
3968 if (*p_sbr != NUL && need_showbreak)
3969 {
3970 /* Draw 'showbreak' at the start of each broken line. */
3971 p_extra = p_sbr;
3972 c_extra = NUL;
3973 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003974 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003976 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 /* Correct end of highlighted area for 'showbreak',
3978 * required when 'linebreak' is also set. */
3979 if (tocol == vcol)
3980 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003981#ifdef FEAT_SYN_HL
3982 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003983 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003984 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003985 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988# endif
3989 }
3990#endif
3991
3992 if (draw_state == WL_LINE - 1 && n_extra == 0)
3993 {
3994 draw_state = WL_LINE;
3995 if (saved_n_extra)
3996 {
3997 /* Continue item from end of wrapped line. */
3998 n_extra = saved_n_extra;
3999 c_extra = saved_c_extra;
4000 p_extra = saved_p_extra;
4001 char_attr = saved_char_attr;
4002 }
4003 else
4004 char_attr = 0;
4005 }
4006 }
4007
4008 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004009 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#ifdef FEAT_DIFF
4012 && filler_todo <= 0
4013#endif
4014 )
4015 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004016 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004017 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 /* Pretend we have finished updating the window. Except when
4019 * 'cursorcolumn' is set. */
4020#ifdef FEAT_SYN_HL
4021 if (wp->w_p_cuc)
4022 row = wp->w_cline_row + wp->w_cline_height;
4023 else
4024#endif
4025 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 break;
4027 }
4028
4029 if (draw_state == WL_LINE && area_highlighting)
4030 {
4031 /* handle Visual or match highlighting in this line */
4032 if (vcol == fromcol
4033#ifdef FEAT_MBYTE
4034 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4035 && (*mb_ptr2cells)(ptr) > 1)
4036#endif
4037 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004038 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 && vcol < tocol))
4040 area_attr = attr; /* start highlighting */
4041 else if (area_attr != 0
4042 && (vcol == tocol
4043 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046#ifdef FEAT_SEARCH_EXTRA
4047 if (!n_extra)
4048 {
4049 /*
4050 * Check for start/end of search pattern match.
4051 * After end, check for start/end of next match.
4052 * When another match, have to check for start again.
4053 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004054 * Do this for 'search_hl' and the match list (ordered by
4055 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004057 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004058 cur = wp->w_match_head;
4059 shl_flag = FALSE;
4060 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004062 if (shl_flag == FALSE
4063 && ((cur != NULL
4064 && cur->priority > SEARCH_HL_PRIORITY)
4065 || cur == NULL))
4066 {
4067 shl = &search_hl;
4068 shl_flag = TRUE;
4069 }
4070 else
4071 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004072 if (cur != NULL)
4073 cur->pos.cur = 0;
4074 pos_inprogress = TRUE;
4075 while (shl->rm.regprog != NULL
4076 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004078 if (shl->startcol != MAXCOL
4079 && v >= (long)shl->startcol
4080 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004082#ifdef FEAT_MBYTE
4083 int tmp_col = v + MB_PTR2LEN(ptr);
4084
4085 if (shl->endcol < tmp_col)
4086 shl->endcol = tmp_col;
4087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004089#ifdef FEAT_CONCEAL
4090 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4091 == cur->hlg_id)
4092 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004093 has_match_conc =
4094 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004095 match_conc = cur->conceal_char;
4096 }
4097 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004098 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004101 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004104 next_search_hl(wp, shl, lnum, (colnr_T)v,
4105 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004106 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004107 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109 /* Need to get the line again, a multi-line regexp
4110 * may have made it invalid. */
4111 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4112 ptr = line + v;
4113
4114 if (shl->lnum == lnum)
4115 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004116 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004118 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004120 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004122 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
4124 /* highlight empty match, try again after
4125 * it */
4126#ifdef FEAT_MBYTE
4127 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004128 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 else
4131#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134
4135 /* Loop to check if the match starts at the
4136 * current position */
4137 continue;
4138 }
4139 }
4140 break;
4141 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004142 if (shl != &search_hl && cur != NULL)
4143 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004145
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004146 /* Use attributes from match with highest priority among
4147 * 'search_hl' and the match list. */
4148 search_attr = search_hl.attr_cur;
4149 cur = wp->w_match_head;
4150 shl_flag = FALSE;
4151 while (cur != NULL || shl_flag == FALSE)
4152 {
4153 if (shl_flag == FALSE
4154 && ((cur != NULL
4155 && cur->priority > SEARCH_HL_PRIORITY)
4156 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004157 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004158 shl = &search_hl;
4159 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004160 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004161 else
4162 shl = &cur->hl;
4163 if (shl->attr_cur != 0)
4164 search_attr = shl->attr_cur;
4165 if (shl != &search_hl && cur != NULL)
4166 cur = cur->next;
4167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 }
4169#endif
4170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004172 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004174 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4175 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004177 if (diff_hlf == HLF_TXD && ptr - line > change_end
4178 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004180 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004181 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004182 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004185
4186 /* Decide which of the highlight attributes to use. */
4187 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004188#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004189 if (area_attr != 0)
4190 char_attr = hl_combine_attr(line_attr, area_attr);
4191 else if (search_attr != 0)
4192 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004193 /* Use line_attr when not in the Visual or 'incsearch' area
4194 * (area_attr may be 0 when "noinvcur" is set). */
4195 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004196 || vcol < fromcol || vcol_prev < fromcol_prev
4197 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004198 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004199#else
4200 if (area_attr != 0)
4201 char_attr = area_attr;
4202 else if (search_attr != 0)
4203 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204#endif
4205 else
4206 {
4207 attr_pri = FALSE;
4208#ifdef FEAT_SYN_HL
4209 if (has_syntax)
4210 char_attr = syntax_attr;
4211 else
4212#endif
4213 char_attr = 0;
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216
4217 /*
4218 * Get the next character to put on the screen.
4219 */
4220 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004221 * The "p_extra" points to the extra stuff that is inserted to
4222 * represent special characters (non-printable stuff) and other
4223 * things. When all characters are the same, c_extra is used.
4224 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4225 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4227 */
4228 if (n_extra > 0)
4229 {
4230 if (c_extra != NUL)
4231 {
4232 c = c_extra;
4233#ifdef FEAT_MBYTE
4234 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004235 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004239 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 else
4242 mb_utf8 = FALSE;
4243#endif
4244 }
4245 else
4246 {
4247 c = *p_extra;
4248#ifdef FEAT_MBYTE
4249 if (has_mbyte)
4250 {
4251 mb_c = c;
4252 if (enc_utf8)
4253 {
4254 /* If the UTF-8 character is more than one byte:
4255 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004256 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 mb_utf8 = FALSE;
4258 if (mb_l > n_extra)
4259 mb_l = 1;
4260 else if (mb_l > 1)
4261 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004262 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004264 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 }
4267 else
4268 {
4269 /* if this is a DBCS character, put it in "mb_c" */
4270 mb_l = MB_BYTE2LEN(c);
4271 if (mb_l >= n_extra)
4272 mb_l = 1;
4273 else if (mb_l > 1)
4274 mb_c = (c << 8) + p_extra[1];
4275 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004276 if (mb_l == 0) /* at the NUL at end-of-line */
4277 mb_l = 1;
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 /* If a double-width char doesn't fit display a '>' in the
4280 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004281 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282# ifdef FEAT_RIGHTLEFT
4283 wp->w_p_rl ? (col <= 0) :
4284# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004285 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 && (*mb_char2cells)(mb_c) == 2)
4287 {
4288 c = '>';
4289 mb_c = c;
4290 mb_l = 1;
4291 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004292 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 /* put the pointer back to output the double-width
4294 * character at the start of the next line. */
4295 ++n_extra;
4296 --p_extra;
4297 }
4298 else
4299 {
4300 n_extra -= mb_l - 1;
4301 p_extra += mb_l - 1;
4302 }
4303 }
4304#endif
4305 ++p_extra;
4306 }
4307 --n_extra;
4308 }
4309 else
4310 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004311#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004312 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004313#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004314
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004315 if (p_extra_free != NULL)
4316 {
4317 vim_free(p_extra_free);
4318 p_extra_free = NULL;
4319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 /*
4321 * Get a character from the line itself.
4322 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004323 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004325 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#ifdef FEAT_MBYTE
4328 if (has_mbyte)
4329 {
4330 mb_c = c;
4331 if (enc_utf8)
4332 {
4333 /* If the UTF-8 character is more than one byte: Decode it
4334 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004335 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 mb_utf8 = FALSE;
4337 if (mb_l > 1)
4338 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004339 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /* Overlong encoded ASCII or ASCII with composing char
4341 * is displayed normally, except a NUL. */
4342 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004343 {
4344 c = mb_c;
4345# ifdef FEAT_LINEBREAK
4346 c0 = mb_c;
4347# endif
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004350
4351 /* At start of the line we can have a composing char.
4352 * Draw it as a space with a composing char. */
4353 if (utf_iscomposing(mb_c))
4354 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004355 int i;
4356
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004357 for (i = Screen_mco - 1; i > 0; --i)
4358 u8cc[i] = u8cc[i - 1];
4359 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004360 mb_c = ' ';
4361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363
4364 if ((mb_l == 1 && c >= 0x80)
4365 || (mb_l >= 1 && mb_c == 0)
4366 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004367# ifdef UNICODE16
4368 || mb_c >= 0x10000
4369# endif
4370 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
4372 /*
4373 * Illegal UTF-8 byte: display as <xx>.
4374 * Non-BMP character : display as ? or fullwidth ?.
4375 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004376# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004381# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (wp->w_p_rl) /* reverse */
4383 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004384# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004386# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 else if (utf_char2cells(mb_c) != 2)
4388 STRCPY(extra, "?");
4389 else
4390 /* 0xff1f in UTF-8: full-width '?' */
4391 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004392# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 p_extra = extra;
4395 c = *p_extra;
4396 mb_c = mb_ptr2char_adv(&p_extra);
4397 mb_utf8 = (c >= 0x80);
4398 n_extra = (int)STRLEN(p_extra);
4399 c_extra = NUL;
4400 if (area_attr == 0 && search_attr == 0)
4401 {
4402 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004403 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 saved_attr2 = char_attr; /* save current attr */
4405 }
4406 }
4407 else if (mb_l == 0) /* at the NUL at end-of-line */
4408 mb_l = 1;
4409#ifdef FEAT_ARABIC
4410 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4411 {
4412 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004413 int pc, pc1, nc;
4414 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 /* The idea of what is the previous and next
4417 * character depends on 'rightleft'. */
4418 if (wp->w_p_rl)
4419 {
4420 pc = prev_c;
4421 pc1 = prev_c1;
4422 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004423 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 else
4426 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004427 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 prev_c = mb_c;
4432
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 else
4436 prev_c = mb_c;
4437#endif
4438 }
4439 else /* enc_dbcs */
4440 {
4441 mb_l = MB_BYTE2LEN(c);
4442 if (mb_l == 0) /* at the NUL at end-of-line */
4443 mb_l = 1;
4444 else if (mb_l > 1)
4445 {
4446 /* We assume a second byte below 32 is illegal.
4447 * Hopefully this is OK for all double-byte encodings!
4448 */
4449 if (ptr[1] >= 32)
4450 mb_c = (c << 8) + ptr[1];
4451 else
4452 {
4453 if (ptr[1] == NUL)
4454 {
4455 /* head byte at end of line */
4456 mb_l = 1;
4457 transchar_nonprint(extra, c);
4458 }
4459 else
4460 {
4461 /* illegal tail byte */
4462 mb_l = 2;
4463 STRCPY(extra, "XX");
4464 }
4465 p_extra = extra;
4466 n_extra = (int)STRLEN(extra) - 1;
4467 c_extra = NUL;
4468 c = *p_extra++;
4469 if (area_attr == 0 && search_attr == 0)
4470 {
4471 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 saved_attr2 = char_attr; /* save current attr */
4474 }
4475 mb_c = c;
4476 }
4477 }
4478 }
4479 /* If a double-width char doesn't fit display a '>' in the
4480 * last column; the character is displayed at the start of the
4481 * next line. */
4482 if ((
4483# ifdef FEAT_RIGHTLEFT
4484 wp->w_p_rl ? (col <= 0) :
4485# endif
4486 (col >= W_WIDTH(wp) - 1))
4487 && (*mb_char2cells)(mb_c) == 2)
4488 {
4489 c = '>';
4490 mb_c = c;
4491 mb_utf8 = FALSE;
4492 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004493 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /* Put pointer back so that the character will be
4495 * displayed at the start of the next line. */
4496 --ptr;
4497 }
4498 else if (*ptr != NUL)
4499 ptr += mb_l - 1;
4500
4501 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004502 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004503 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004504 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004507 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 c = ' ';
4509 if (area_attr == 0 && search_attr == 0)
4510 {
4511 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004512 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 saved_attr2 = char_attr; /* save current attr */
4514 }
4515 mb_c = c;
4516 mb_utf8 = FALSE;
4517 mb_l = 1;
4518 }
4519
4520 }
4521#endif
4522 ++ptr;
4523
4524 if (extra_check)
4525 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004526#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004527 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004528#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004529
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004530#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /* Get syntax attribute, unless still at the start of the line
4532 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004533 v = (long)(ptr - line);
4534 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 /* Get the syntax attribute for the character. If there
4537 * is an error, disable syntax highlighting. */
4538 save_did_emsg = did_emsg;
4539 did_emsg = FALSE;
4540
Bram Moolenaar217ad922005-03-20 22:37:15 +00004541 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004542# ifdef FEAT_SPELL
4543 has_spell ? &can_spell :
4544# endif
4545 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
4547 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004548 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004549 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004550 has_syntax = FALSE;
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else
4553 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004554#ifdef SYN_TIME_LIMIT
4555 if (wp->w_s->b_syn_slow)
4556 has_syntax = FALSE;
4557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 /* Need to get the line again, a multi-line regexp may
4560 * have made it invalid. */
4561 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4562 ptr = line + v;
4563
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004564 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004566 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004567 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004568# ifdef FEAT_CONCEAL
4569 /* no concealing past the end of the line, it interferes
4570 * with line highlighting */
4571 if (c == NUL)
4572 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004573 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004574 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004575# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004577#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004578
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004579#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004580 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004581 * Only do this when there is no syntax highlighting, the
4582 * @Spell cluster is not used or the current syntax item
4583 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004584 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004585 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004586 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004587# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004588 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004589 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004590# endif
4591 if (c != 0 && (
4592# ifdef FEAT_SYN_HL
4593 !has_syntax ||
4594# endif
4595 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004596 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004597 char_u *prev_ptr, *p;
4598 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004599 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004600# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004601 if (has_mbyte)
4602 {
4603 prev_ptr = ptr - mb_l;
4604 v -= mb_l - 1;
4605 }
4606 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004608 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004609
4610 /* Use nextline[] if possible, it has the start of the
4611 * next line concatenated. */
4612 if ((prev_ptr - line) - nextlinecol >= 0)
4613 p = nextline + (prev_ptr - line) - nextlinecol;
4614 else
4615 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004616 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004617 len = spell_check(wp, p, &spell_hlf, &cap_col,
4618 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004619 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004620
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004621 /* In Insert mode only highlight a word that
4622 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004623 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004624 && (State & INSERT) != 0
4625 && wp->w_cursor.lnum == lnum
4626 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004627 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004628 && wp->w_cursor.col < (colnr_T)word_end)
4629 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004630 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004631 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004632 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004633
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004634 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004635 && (p - nextline) + len > nextline_idx)
4636 {
4637 /* Remember that the good word continues at the
4638 * start of the next line. */
4639 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004640 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004641 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004642
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004643 /* Turn index into actual attributes. */
4644 if (spell_hlf != HLF_COUNT)
4645 spell_attr = highlight_attr[spell_hlf];
4646
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004647 if (cap_col > 0)
4648 {
4649 if (p != prev_ptr
4650 && (p - nextline) + cap_col >= nextline_idx)
4651 {
4652 /* Remember that the word in the next line
4653 * must start with a capital. */
4654 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004655 cap_col = (int)((p - nextline) + cap_col
4656 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004657 }
4658 else
4659 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004660 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004661 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004662 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004663 }
4664 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004665 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004666 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004667 char_attr = hl_combine_attr(char_attr, spell_attr);
4668 else
4669 char_attr = hl_combine_attr(spell_attr, char_attr);
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#endif
4672#ifdef FEAT_LINEBREAK
4673 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004674 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004676 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004677 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004679# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004680 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004681# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004682 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004684 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004686 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004687
Bram Moolenaar597a4222014-06-25 14:39:50 +02004688 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004689 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004690 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004691 if (c == TAB && n_extra + col > W_WIDTH(wp))
4692 n_extra = (int)wp->w_buffer->b_p_ts
4693 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4694
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004695# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004696 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004697# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004699# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004700 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004701 {
4702#ifdef FEAT_CONCEAL
4703 if (c == TAB)
4704 /* See "Tab alignment" below. */
4705 FIX_FOR_BOGUSCOLS;
4706#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004707 if (!wp->w_p_list)
4708 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
4711#endif
4712
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004713 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4714 */
4715 if (wp->w_p_list
4716 && (((c == 160
4717#ifdef FEAT_MBYTE
4718 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4719#endif
4720 ) && lcs_nbsp)
4721 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4722 {
4723 c = (c == ' ') ? lcs_space : lcs_nbsp;
4724 if (area_attr == 0 && search_attr == 0)
4725 {
4726 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004727 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004728 saved_attr2 = char_attr; /* save current attr */
4729 }
4730#ifdef FEAT_MBYTE
4731 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004732 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004733 {
4734 mb_utf8 = TRUE;
4735 u8cc[0] = 0;
4736 c = 0xc0;
4737 }
4738 else
4739 mb_utf8 = FALSE;
4740#endif
4741 }
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4744 {
4745 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004746 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004749 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 saved_attr2 = char_attr; /* save current attr */
4751 }
4752#ifdef FEAT_MBYTE
4753 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004754 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004757 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004758 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 else
4761 mb_utf8 = FALSE;
4762#endif
4763 }
4764 }
4765
4766 /*
4767 * Handling of non-printable characters.
4768 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004769 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 /*
4772 * when getting a character from the file, we may have to
4773 * turn it into something else on the way to putting it
4774 * into "ScreenLines".
4775 */
4776 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4777 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004778 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004779 long vcol_adjusted = vcol; /* removed showbreak length */
4780#ifdef FEAT_LINEBREAK
4781 /* only adjust the tab_len, when at the first column
4782 * after the showbreak value was drawn */
4783 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4784 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004787 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004788 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4789
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004790#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004791 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004792#endif
4793 /* tab amount depends on current column */
4794 n_extra = tab_len;
4795#ifdef FEAT_LINEBREAK
4796 else
4797 {
4798 char_u *p;
4799 int len = n_extra;
4800 int i;
4801 int saved_nextra = n_extra;
4802
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004803#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004804 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004805 /* there are characters to conceal */
4806 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004807 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4808 */
4809 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4810 && n_extra > tab_len)
4811 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004812#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004813
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004814 /* if n_extra > 0, it gives the number of chars, to
4815 * use for a tab, else we need to calculate the width
4816 * for a tab */
4817#ifdef FEAT_MBYTE
4818 len = (tab_len * mb_char2len(lcs_tab2));
4819 if (n_extra > 0)
4820 len += n_extra - tab_len;
4821#endif
4822 c = lcs_tab1;
4823 p = alloc((unsigned)(len + 1));
4824 vim_memset(p, ' ', len);
4825 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004826 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004827 p_extra_free = p;
4828 for (i = 0; i < tab_len; i++)
4829 {
4830#ifdef FEAT_MBYTE
4831 mb_char2bytes(lcs_tab2, p);
4832 p += mb_char2len(lcs_tab2);
4833 n_extra += mb_char2len(lcs_tab2)
4834 - (saved_nextra > 0 ? 1 : 0);
4835#else
4836 p[i] = lcs_tab2;
4837#endif
4838 }
4839 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004840#ifdef FEAT_CONCEAL
4841 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4842 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004843 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004844 n_extra -= vcol_off;
4845#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004846 }
4847#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004848#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004849 {
4850 int vc_saved = vcol_off;
4851
4852 /* Tab alignment should be identical regardless of
4853 * 'conceallevel' value. So tab compensates of all
4854 * previous concealed characters, and thus resets
4855 * vcol_off and boguscols accumulated so far in the
4856 * line. Note that the tab can be longer than
4857 * 'tabstop' when there are concealed characters. */
4858 FIX_FOR_BOGUSCOLS;
4859
4860 /* Make sure, the highlighting for the tab char will be
4861 * correctly set further below (effectively reverts the
4862 * FIX_FOR_BOGSUCOLS macro */
4863 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004864 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004865 tab_len += vc_saved;
4866 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#ifdef FEAT_MBYTE
4869 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4870#endif
4871 if (wp->w_p_list)
4872 {
4873 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004874#ifdef FEAT_LINEBREAK
4875 if (wp->w_p_lbr)
4876 c_extra = NUL; /* using p_extra from above */
4877 else
4878#endif
4879 c_extra = lcs_tab2;
4880 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004881 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 saved_attr2 = char_attr; /* save current attr */
4883#ifdef FEAT_MBYTE
4884 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004885 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
4887 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004888 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004889 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891#endif
4892 }
4893 else
4894 {
4895 c_extra = ' ';
4896 c = ' ';
4897 }
4898 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004899 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004900 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004901 || ((fromcol >= 0 || fromcol_prev >= 0)
4902 && tocol > vcol
4903 && VIsual_mode != Ctrl_V
4904 && (
4905# ifdef FEAT_RIGHTLEFT
4906 wp->w_p_rl ? (col >= 0) :
4907# endif
4908 (col < W_WIDTH(wp)))
4909 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004910 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004911 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004912 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004914 /* Display a '$' after the line or highlight an extra
4915 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4917 /* For a diff line the highlighting continues after the
4918 * "$". */
4919 if (
4920# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004921 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922# ifdef LINE_ATTR
4923 &&
4924# endif
4925# endif
4926# ifdef LINE_ATTR
4927 line_attr == 0
4928# endif
4929 )
4930#endif
4931 {
4932#ifdef FEAT_VIRTUALEDIT
4933 /* In virtualedit, visual selections may extend
4934 * beyond end of line. */
4935 if (area_highlighting && virtual_active()
4936 && tocol != MAXCOL && vcol < tocol)
4937 n_extra = 0;
4938 else
4939#endif
4940 {
4941 p_extra = at_end_str;
4942 n_extra = 1;
4943 c_extra = NUL;
4944 }
4945 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004946 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004947 c = lcs_eol;
4948 else
4949 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 lcs_eol_one = -1;
4951 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004952 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004954 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 n_attr = 1;
4956 }
4957#ifdef FEAT_MBYTE
4958 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004959 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004962 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004963 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 else
4966 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4967#endif
4968 }
4969 else if (c != NUL)
4970 {
4971 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004972 if (n_extra == 0)
4973 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#ifdef FEAT_RIGHTLEFT
4975 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4976 rl_mirror(p_extra); /* reverse "<12>" */
4977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004979#ifdef FEAT_LINEBREAK
4980 if (wp->w_p_lbr)
4981 {
4982 char_u *p;
4983
4984 c = *p_extra;
4985 p = alloc((unsigned)n_extra + 1);
4986 vim_memset(p, ' ', n_extra);
4987 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4988 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004989 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004990 p_extra_free = p_extra = p;
4991 }
4992 else
4993#endif
4994 {
4995 n_extra = byte2cells(c) - 1;
4996 c = *p_extra++;
4997 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004998 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005001 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 saved_attr2 = char_attr; /* save current attr */
5003 }
5004#ifdef FEAT_MBYTE
5005 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5006#endif
5007 }
5008#ifdef FEAT_VIRTUALEDIT
5009 else if (VIsual_active
5010 && (VIsual_mode == Ctrl_V
5011 || VIsual_mode == 'v')
5012 && virtual_active()
5013 && tocol != MAXCOL
5014 && vcol < tocol
5015 && (
5016# ifdef FEAT_RIGHTLEFT
5017 wp->w_p_rl ? (col >= 0) :
5018# endif
5019 (col < W_WIDTH(wp))))
5020 {
5021 c = ' ';
5022 --ptr; /* put it back at the NUL */
5023 }
5024#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005025#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else if ((
5027# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005028 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 ) && (
5032# ifdef FEAT_RIGHTLEFT
5033 wp->w_p_rl ? (col >= 0) :
5034# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005035 (col
5036# ifdef FEAT_CONCEAL
5037 - boguscols
5038# endif
5039 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 /* Highlight until the right side of the window */
5042 c = ' ';
5043 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005044
5045 /* Remember we do the char for line highlighting. */
5046 ++did_line_attr;
5047
5048 /* don't do search HL for the rest of the line */
5049 if (line_attr != 0 && char_attr == search_attr && col > 0)
5050 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051# ifdef FEAT_DIFF
5052 if (diff_hlf == HLF_TXD)
5053 {
5054 diff_hlf = HLF_CHD;
5055 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005056 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005057 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5059 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005060 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063# endif
5064 }
5065#endif
5066 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005067
5068#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005069 if ( wp->w_p_cole > 0
5070 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005071 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005072 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005073 && !(lnum_in_visual_area
5074 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005075 {
5076 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005077 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005078 && (syn_get_sub_char() != NUL || match_conc
5079 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005080 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005081 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005082 /* First time at this concealed item: display one
5083 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005084 if (match_conc)
5085 c = match_conc;
5086 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005087 c = syn_get_sub_char();
5088 else if (lcs_conceal != NUL)
5089 c = lcs_conceal;
5090 else
5091 c = ' ';
5092
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005093 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005094
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005095 if (n_extra > 0)
5096 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005097 vcol += n_extra;
5098 if (wp->w_p_wrap && n_extra > 0)
5099 {
5100# ifdef FEAT_RIGHTLEFT
5101 if (wp->w_p_rl)
5102 {
5103 col -= n_extra;
5104 boguscols -= n_extra;
5105 }
5106 else
5107# endif
5108 {
5109 boguscols += n_extra;
5110 col += n_extra;
5111 }
5112 }
5113 n_extra = 0;
5114 n_attr = 0;
5115 }
5116 else if (n_skip == 0)
5117 {
5118 is_concealing = TRUE;
5119 n_skip = 1;
5120 }
5121# ifdef FEAT_MBYTE
5122 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005123 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005124 {
5125 mb_utf8 = TRUE;
5126 u8cc[0] = 0;
5127 c = 0xc0;
5128 }
5129 else
5130 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5131# endif
5132 }
5133 else
5134 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005135 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005136 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005137 }
5138#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005141#ifdef FEAT_CONCEAL
5142 /* In the cursor line and we may be concealing characters: correct
5143 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005144 if (!did_wcol && draw_state == WL_LINE
5145 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005146 && conceal_cursor_line(wp)
5147 && (int)wp->w_virtcol <= vcol + n_skip)
5148 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005149# ifdef FEAT_RIGHTLEFT
5150 if (wp->w_p_rl)
5151 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5152 else
5153# endif
5154 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005155 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005156 did_wcol = TRUE;
5157 }
5158#endif
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 /* Don't override visual selection highlighting. */
5161 if (n_attr > 0
5162 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005163 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 char_attr = extra_attr;
5165
Bram Moolenaar81695252004-12-29 20:58:21 +00005166#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 /* XIM don't send preedit_start and preedit_end, but they send
5168 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5169 * im_is_preediting() here. */
5170 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005171 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 && (State & INSERT)
5173 && !p_imdisable
5174 && im_is_preediting()
5175 && draw_state == WL_LINE)
5176 {
5177 colnr_T tcol;
5178
5179 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005180 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 else
5182 tcol = preedit_end_col;
5183 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5184 {
5185 if (feedback_old_attr < 0)
5186 {
5187 feedback_col = 0;
5188 feedback_old_attr = char_attr;
5189 }
5190 char_attr = im_get_feedback_attr(feedback_col);
5191 if (char_attr < 0)
5192 char_attr = feedback_old_attr;
5193 feedback_col++;
5194 }
5195 else if (feedback_old_attr >= 0)
5196 {
5197 char_attr = feedback_old_attr;
5198 feedback_old_attr = -1;
5199 feedback_col = 0;
5200 }
5201 }
5202#endif
5203 /*
5204 * Handle the case where we are in column 0 but not on the first
5205 * character of the line and the user wants us to show us a
5206 * special character (via 'listchars' option "precedes:<char>".
5207 */
5208 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005209 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5211#ifdef FEAT_DIFF
5212 && filler_todo <= 0
5213#endif
5214 && draw_state > WL_NR
5215 && c != NUL)
5216 {
5217 c = lcs_prec;
5218 lcs_prec_todo = NUL;
5219#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005220 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5221 {
5222 /* Double-width character being overwritten by the "precedes"
5223 * character, need to fill up half the character. */
5224 c_extra = MB_FILLER_CHAR;
5225 n_extra = 1;
5226 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005227 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005230 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005233 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005234 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236 else
5237 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5238#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005239 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005242 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 n_attr3 = 1;
5244 }
5245 }
5246
5247 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005248 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005250 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005251#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005252 || did_line_attr == 1
5253#endif
5254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005256#ifdef FEAT_SEARCH_EXTRA
5257 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005258
5259 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005260 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005261 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005262#endif
5263
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005264 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * highlight match at end of line. If it's beyond the last
5266 * char on the screen, just overwrite that one (tricky!) Not
5267 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005268#ifdef FEAT_SEARCH_EXTRA
5269 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005270 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005271 prevcol_hl_flag = TRUE;
5272 else
5273 {
5274 cur = wp->w_match_head;
5275 while (cur != NULL)
5276 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005277 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005278 {
5279 prevcol_hl_flag = TRUE;
5280 break;
5281 }
5282 cur = cur->next;
5283 }
5284 }
5285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005287 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005288 && (VIsual_mode != Ctrl_V
5289 || lnum == VIsual.lnum
5290 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005291 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#ifdef FEAT_SEARCH_EXTRA
5293 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005294 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005295# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005296 && did_line_attr <= 1
5297# endif
5298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299#endif
5300 ))
5301 {
5302 int n = 0;
5303
5304#ifdef FEAT_RIGHTLEFT
5305 if (wp->w_p_rl)
5306 {
5307 if (col < 0)
5308 n = 1;
5309 }
5310 else
5311#endif
5312 {
5313 if (col >= W_WIDTH(wp))
5314 n = -1;
5315 }
5316 if (n != 0)
5317 {
5318 /* At the window boundary, highlight the last character
5319 * instead (better than nothing). */
5320 off += n;
5321 col += n;
5322 }
5323 else
5324 {
5325 /* Add a blank character to highlight. */
5326 ScreenLines[off] = ' ';
5327#ifdef FEAT_MBYTE
5328 if (enc_utf8)
5329 ScreenLinesUC[off] = 0;
5330#endif
5331 }
5332#ifdef FEAT_SEARCH_EXTRA
5333 if (area_attr == 0)
5334 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005335 /* Use attributes from match with highest priority among
5336 * 'search_hl' and the match list. */
5337 char_attr = search_hl.attr;
5338 cur = wp->w_match_head;
5339 shl_flag = FALSE;
5340 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005341 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005342 if (shl_flag == FALSE
5343 && ((cur != NULL
5344 && cur->priority > SEARCH_HL_PRIORITY)
5345 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005346 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005347 shl = &search_hl;
5348 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005349 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005350 else
5351 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005352 if ((ptr - line) - 1 == (long)shl->startcol
5353 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005354 char_attr = shl->attr;
5355 if (shl != &search_hl && cur != NULL)
5356 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359#endif
5360 ScreenAttrs[off] = char_attr;
5361#ifdef FEAT_RIGHTLEFT
5362 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005365 --off;
5366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 else
5368#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005371 ++off;
5372 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005373 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005374#ifdef FEAT_SYN_HL
5375 eol_hl_off = 1;
5376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar91170f82006-05-05 21:15:17 +00005380 /*
5381 * At end of the text line.
5382 */
5383 if (c == NUL)
5384 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005385#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005386 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5387 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005388 {
5389 /* highlight last char after line */
5390 --col;
5391 --off;
5392 --vcol;
5393 }
5394
Bram Moolenaar1a384422010-07-14 19:53:30 +02005395 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005396 if (wp->w_p_wrap)
5397 v = wp->w_skipcol;
5398 else
5399 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005400
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005401 /* check if line ends before left margin */
5402 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005403 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005404#ifdef FEAT_CONCEAL
5405 /* Get rid of the boguscols now, we want to draw until the right
5406 * edge for 'cursorcolumn'. */
5407 col -= boguscols;
5408 boguscols = 0;
5409#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005410
5411 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005412 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005413
5414 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005415 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5416 && (int)wp->w_virtcol <
5417 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005418 && lnum != wp->w_cursor.lnum)
5419 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005420# ifdef FEAT_RIGHTLEFT
5421 && !wp->w_p_rl
5422# endif
5423 )
5424 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005425 int rightmost_vcol = 0;
5426 int i;
5427
5428 if (wp->w_p_cuc)
5429 rightmost_vcol = wp->w_virtcol;
5430 if (draw_color_col)
5431 /* determine rightmost colorcolumn to possibly draw */
5432 for (i = 0; color_cols[i] >= 0; ++i)
5433 if (rightmost_vcol < color_cols[i])
5434 rightmost_vcol = color_cols[i];
5435
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005436 while (col < W_WIDTH(wp))
5437 {
5438 ScreenLines[off] = ' ';
5439#ifdef FEAT_MBYTE
5440 if (enc_utf8)
5441 ScreenLinesUC[off] = 0;
5442#endif
5443 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005444 if (draw_color_col)
5445 draw_color_col = advance_color_col(VCOL_HLC,
5446 &color_cols);
5447
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005448 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005449 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005450 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005451 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005452 else
5453 ScreenAttrs[off++] = 0;
5454
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005455 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005456 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005457
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005458 ++vcol;
5459 }
5460 }
5461#endif
5462
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005463 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005464 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 row++;
5466
5467 /*
5468 * Update w_cline_height and w_cline_folded if the cursor line was
5469 * updated (saves a call to plines() later).
5470 */
5471 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5472 {
5473 curwin->w_cline_row = startrow;
5474 curwin->w_cline_height = row - startrow;
5475#ifdef FEAT_FOLDING
5476 curwin->w_cline_folded = FALSE;
5477#endif
5478 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5479 }
5480
5481 break;
5482 }
5483
5484 /* line continues beyond line end */
5485 if (lcs_ext
5486 && !wp->w_p_wrap
5487#ifdef FEAT_DIFF
5488 && filler_todo <= 0
5489#endif
5490 && (
5491#ifdef FEAT_RIGHTLEFT
5492 wp->w_p_rl ? col == 0 :
5493#endif
5494 col == W_WIDTH(wp) - 1)
5495 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005496 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5498 {
5499 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005500 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#ifdef FEAT_MBYTE
5502 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005503 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005506 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005507 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 else
5510 mb_utf8 = FALSE;
5511#endif
5512 }
5513
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005514#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 /* advance to the next 'colorcolumn' */
5516 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005517 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005518
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005519 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005520 * highlight the cursor position itself.
5521 * Also highlight the 'colorcolumn' if it is different than
5522 * 'cursorcolumn' */
5523 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005524 if (draw_state == WL_LINE && !lnum_in_visual_area
5525 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005526 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005527 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005528 && lnum != wp->w_cursor.lnum)
5529 {
5530 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005531 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005533 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005534 {
5535 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005536 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005537 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005538 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005539#endif
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /*
5542 * Store character to be displayed.
5543 * Skip characters that are left of the screen for 'nowrap'.
5544 */
5545 vcol_prev = vcol;
5546 if (draw_state < WL_LINE || n_skip <= 0)
5547 {
5548 /*
5549 * Store the character.
5550 */
5551#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5552 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5553 {
5554 /* A double-wide character is: put first halve in left cell. */
5555 --off;
5556 --col;
5557 }
5558#endif
5559 ScreenLines[off] = c;
5560#ifdef FEAT_MBYTE
5561 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005562 {
5563 if ((mb_c & 0xff00) == 0x8e00)
5564 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 else if (enc_utf8)
5568 {
5569 if (mb_utf8)
5570 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005571 int i;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005574 if ((c & 0xff) == 0)
5575 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005576 for (i = 0; i < Screen_mco; ++i)
5577 {
5578 ScreenLinesC[i][off] = u8cc[i];
5579 if (u8cc[i] == 0)
5580 break;
5581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else
5584 ScreenLinesUC[off] = 0;
5585 }
5586 if (multi_attr)
5587 {
5588 ScreenAttrs[off] = multi_attr;
5589 multi_attr = 0;
5590 }
5591 else
5592#endif
5593 ScreenAttrs[off] = char_attr;
5594
5595#ifdef FEAT_MBYTE
5596 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5597 {
5598 /* Need to fill two screen columns. */
5599 ++off;
5600 ++col;
5601 if (enc_utf8)
5602 /* UTF-8: Put a 0 in the second screen char. */
5603 ScreenLines[off] = 0;
5604 else
5605 /* DBCS: Put second byte in the second screen char. */
5606 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005607 if (draw_state > WL_NR
5608#ifdef FEAT_DIFF
5609 && filler_todo <= 0
5610#endif
5611 )
5612 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 /* When "tocol" is halfway a character, set it to the end of
5614 * the character, otherwise highlighting won't stop. */
5615 if (tocol == vcol)
5616 ++tocol;
5617#ifdef FEAT_RIGHTLEFT
5618 if (wp->w_p_rl)
5619 {
5620 /* now it's time to backup one cell */
5621 --off;
5622 --col;
5623 }
5624#endif
5625 }
5626#endif
5627#ifdef FEAT_RIGHTLEFT
5628 if (wp->w_p_rl)
5629 {
5630 --off;
5631 --col;
5632 }
5633 else
5634#endif
5635 {
5636 ++off;
5637 ++col;
5638 }
5639 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005640#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005641 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005642 {
5643 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005644 ++vcol_off;
5645 if (n_extra > 0)
5646 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005647 if (wp->w_p_wrap)
5648 {
5649 /*
5650 * Special voodoo required if 'wrap' is on.
5651 *
5652 * Advance the column indicator to force the line
5653 * drawing to wrap early. This will make the line
5654 * take up the same screen space when parts are concealed,
5655 * so that cursor line computations aren't messed up.
5656 *
5657 * To avoid the fictitious advance of 'col' causing
5658 * trailing junk to be written out of the screen line
5659 * we are building, 'boguscols' keeps track of the number
5660 * of bad columns we have advanced.
5661 */
5662 if (n_extra > 0)
5663 {
5664 vcol += n_extra;
5665# ifdef FEAT_RIGHTLEFT
5666 if (wp->w_p_rl)
5667 {
5668 col -= n_extra;
5669 boguscols -= n_extra;
5670 }
5671 else
5672# endif
5673 {
5674 col += n_extra;
5675 boguscols += n_extra;
5676 }
5677 n_extra = 0;
5678 n_attr = 0;
5679 }
5680
5681
5682# ifdef FEAT_MBYTE
5683 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5684 {
5685 /* Need to fill two screen columns. */
5686# ifdef FEAT_RIGHTLEFT
5687 if (wp->w_p_rl)
5688 {
5689 --boguscols;
5690 --col;
5691 }
5692 else
5693# endif
5694 {
5695 ++boguscols;
5696 ++col;
5697 }
5698 }
5699# endif
5700
5701# ifdef FEAT_RIGHTLEFT
5702 if (wp->w_p_rl)
5703 {
5704 --boguscols;
5705 --col;
5706 }
5707 else
5708# endif
5709 {
5710 ++boguscols;
5711 ++col;
5712 }
5713 }
5714 else
5715 {
5716 if (n_extra > 0)
5717 {
5718 vcol += n_extra;
5719 n_extra = 0;
5720 n_attr = 0;
5721 }
5722 }
5723
5724 }
5725#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 else
5727 --n_skip;
5728
Bram Moolenaar64486672010-05-16 15:46:46 +02005729 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5730 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005731 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732#ifdef FEAT_DIFF
5733 && filler_todo <= 0
5734#endif
5735 )
5736 ++vcol;
5737
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005738#ifdef FEAT_SYN_HL
5739 if (vcol_save_attr >= 0)
5740 char_attr = vcol_save_attr;
5741#endif
5742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 /* restore attributes after "predeces" in 'listchars' */
5744 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5745 char_attr = saved_attr3;
5746
5747 /* restore attributes after last 'listchars' or 'number' char */
5748 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5749 char_attr = saved_attr2;
5750
5751 /*
5752 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005753 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 */
5755 if ((
5756#ifdef FEAT_RIGHTLEFT
5757 wp->w_p_rl ? (col < 0) :
5758#endif
5759 (col >= W_WIDTH(wp)))
5760 && (*ptr != NUL
5761#ifdef FEAT_DIFF
5762 || filler_todo > 0
5763#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005764 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5766 )
5767 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005768#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005769 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005770 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005771 boguscols = 0;
5772#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005773 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005774 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 ++row;
5777 ++screen_row;
5778
5779 /* When not wrapping and finished diff lines, or when displayed
5780 * '$' and highlighting until last column, break here. */
5781 if ((!wp->w_p_wrap
5782#ifdef FEAT_DIFF
5783 && filler_todo <= 0
5784#endif
5785 ) || lcs_eol_one == -1)
5786 break;
5787
5788 /* When the window is too narrow draw all "@" lines. */
5789 if (draw_state != WL_LINE
5790#ifdef FEAT_DIFF
5791 && filler_todo <= 0
5792#endif
5793 )
5794 {
5795 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005796#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 draw_vsep_win(wp, row);
5798#endif
5799 row = endrow;
5800 }
5801
5802 /* When line got too long for screen break here. */
5803 if (row == endrow)
5804 {
5805 ++row;
5806 break;
5807 }
5808
5809 if (screen_cur_row == screen_row - 1
5810#ifdef FEAT_DIFF
5811 && filler_todo <= 0
5812#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005813#ifdef FEAT_WINDOWS
5814 && W_WIDTH(wp) == Columns
5815#endif
5816 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
5818 /* Remember that the line wraps, used for modeless copy. */
5819 LineWraps[screen_row - 1] = TRUE;
5820
5821 /*
5822 * Special trick to make copy/paste of wrapped lines work with
5823 * xterm/screen: write an extra character beyond the end of
5824 * the line. This will work with all terminal types
5825 * (regardless of the xn,am settings).
5826 * Only do this on a fast tty.
5827 * Only do this if the cursor is on the current line
5828 * (something has been written in it).
5829 * Don't do this for the GUI.
5830 * Don't do this for double-width characters.
5831 * Don't do this for a window not at the right screen border.
5832 */
5833 if (p_tf
5834#ifdef FEAT_GUI
5835 && !gui.in_use
5836#endif
5837#ifdef FEAT_MBYTE
5838 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005839 && ((*mb_off2cells)(LineOffset[screen_row],
5840 LineOffset[screen_row] + screen_Columns)
5841 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005843 + (int)Columns - 2,
5844 LineOffset[screen_row] + screen_Columns)
5845 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#endif
5847 )
5848 {
5849 /* First make sure we are at the end of the screen line,
5850 * then output the same character again to let the
5851 * terminal know about the wrap. If the terminal doesn't
5852 * auto-wrap, we overwrite the character. */
5853 if (screen_cur_col != W_WIDTH(wp))
5854 screen_char(LineOffset[screen_row - 1]
5855 + (unsigned)Columns - 1,
5856 screen_row - 1, (int)(Columns - 1));
5857
5858#ifdef FEAT_MBYTE
5859 /* When there is a multi-byte character, just output a
5860 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005861 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5862 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 out_char(' ');
5864 else
5865#endif
5866 out_char(ScreenLines[LineOffset[screen_row - 1]
5867 + (Columns - 1)]);
5868 /* force a redraw of the first char on the next line */
5869 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5870 screen_start(); /* don't know where cursor is now */
5871 }
5872 }
5873
5874 col = 0;
5875 off = (unsigned)(current_ScreenLine - ScreenLines);
5876#ifdef FEAT_RIGHTLEFT
5877 if (wp->w_p_rl)
5878 {
5879 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5880 off += col;
5881 }
5882#endif
5883
5884 /* reset the drawing state for the start of a wrapped line */
5885 draw_state = WL_START;
5886 saved_n_extra = n_extra;
5887 saved_p_extra = p_extra;
5888 saved_c_extra = c_extra;
5889 saved_char_attr = char_attr;
5890 n_extra = 0;
5891 lcs_prec_todo = lcs_prec;
5892#ifdef FEAT_LINEBREAK
5893# ifdef FEAT_DIFF
5894 if (filler_todo <= 0)
5895# endif
5896 need_showbreak = TRUE;
5897#endif
5898#ifdef FEAT_DIFF
5899 --filler_todo;
5900 /* When the filler lines are actually below the last line of the
5901 * file, don't draw the line itself, break here. */
5902 if (filler_todo == 0 && wp->w_botfill)
5903 break;
5904#endif
5905 }
5906
5907 } /* for every character in the line */
5908
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005909#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005910 /* After an empty line check first word for capital. */
5911 if (*skipwhite(line) == NUL)
5912 {
5913 capcol_lnum = lnum + 1;
5914 cap_col = 0;
5915 }
5916#endif
5917
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005918 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 return row;
5920}
5921
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005922#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005923static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005924
5925/*
5926 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005927 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005928 */
5929 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005930comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005931{
5932 int i;
5933
5934 for (i = 0; i < Screen_mco; ++i)
5935 {
5936 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5937 return TRUE;
5938 if (ScreenLinesC[i][off_from] == 0)
5939 break;
5940 }
5941 return FALSE;
5942}
5943#endif
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945/*
5946 * Check whether the given character needs redrawing:
5947 * - the (first byte of the) character is different
5948 * - the attributes are different
5949 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005950 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005953char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 if (cols > 0
5956 && ((ScreenLines[off_from] != ScreenLines[off_to]
5957 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5958
5959#ifdef FEAT_MBYTE
5960 || (enc_dbcs != 0
5961 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5962 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5963 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5964 : (cols > 1 && ScreenLines[off_from + 1]
5965 != ScreenLines[off_to + 1])))
5966 || (enc_utf8
5967 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5968 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005969 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005970 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5971 && ScreenLines[off_from + 1]
5972 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#endif
5974 ))
5975 return TRUE;
5976 return FALSE;
5977}
5978
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005979#if defined(FEAT_TERMINAL) || defined(PROTO)
5980/*
5981 * Return the index in ScreenLines[] for the current screen line.
5982 */
5983 int
5984screen_get_current_line_off()
5985{
5986 return (int)(current_ScreenLine - ScreenLines);
5987}
5988#endif
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990/*
5991 * Move one "cooked" screen line to the screen, but only the characters that
5992 * have actually changed. Handle insert/delete character.
5993 * "coloff" gives the first column on the screen for this line.
5994 * "endcol" gives the columns where valid characters are.
5995 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5996 * needs to be cleared, negative otherwise.
5997 * "rlflag" is TRUE in a rightleft window:
5998 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5999 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6000 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006001 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006002screen_line(
6003 int row,
6004 int coloff,
6005 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006006 int clear_width,
6007 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 unsigned off_from;
6010 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006011#ifdef FEAT_MBYTE
6012 unsigned max_off_from;
6013 unsigned max_off_to;
6014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006016#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 int hl;
6018#endif
6019 int force = FALSE; /* force update rest of the line */
6020 int redraw_this /* bool: does character need redraw? */
6021#ifdef FEAT_GUI
6022 = TRUE /* For GUI when while-loop empty */
6023#endif
6024 ;
6025 int redraw_next; /* redraw_this for next character */
6026#ifdef FEAT_MBYTE
6027 int clear_next = FALSE;
6028 int char_cells; /* 1: normal char */
6029 /* 2: occupies two display cells */
6030# define CHAR_CELLS char_cells
6031#else
6032# define CHAR_CELLS 1
6033#endif
6034
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006035 /* Check for illegal row and col, just in case. */
6036 if (row >= Rows)
6037 row = Rows - 1;
6038 if (endcol > Columns)
6039 endcol = Columns;
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041# ifdef FEAT_CLIPBOARD
6042 clip_may_clear_selection(row, row);
6043# endif
6044
6045 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6046 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006047#ifdef FEAT_MBYTE
6048 max_off_from = off_from + screen_Columns;
6049 max_off_to = LineOffset[row] + screen_Columns;
6050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
6052#ifdef FEAT_RIGHTLEFT
6053 if (rlflag)
6054 {
6055 /* Clear rest first, because it's left of the text. */
6056 if (clear_width > 0)
6057 {
6058 while (col <= endcol && ScreenLines[off_to] == ' '
6059 && ScreenAttrs[off_to] == 0
6060# ifdef FEAT_MBYTE
6061 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6062# endif
6063 )
6064 {
6065 ++off_to;
6066 ++col;
6067 }
6068 if (col <= endcol)
6069 screen_fill(row, row + 1, col + coloff,
6070 endcol + coloff + 1, ' ', ' ', 0);
6071 }
6072 col = endcol + 1;
6073 off_to = LineOffset[row] + col + coloff;
6074 off_from += col;
6075 endcol = (clear_width > 0 ? clear_width : -clear_width);
6076 }
6077#endif /* FEAT_RIGHTLEFT */
6078
6079 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6080
6081 while (col < endcol)
6082 {
6083#ifdef FEAT_MBYTE
6084 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006085 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 else
6087 char_cells = 1;
6088#endif
6089
6090 redraw_this = redraw_next;
6091 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6092 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6093
6094#ifdef FEAT_GUI
6095 /* If the next character was bold, then redraw the current character to
6096 * remove any pixels that might have spilt over into us. This only
6097 * happens in the GUI.
6098 */
6099 if (redraw_next && gui.in_use)
6100 {
6101 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006102 if (hl > HL_ALL)
6103 hl = syn_attr2attr(hl);
6104 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 redraw_this = TRUE;
6106 }
6107#endif
6108
6109 if (redraw_this)
6110 {
6111 /*
6112 * Special handling when 'xs' termcap flag set (hpterm):
6113 * Attributes for characters are stored at the position where the
6114 * cursor is when writing the highlighting code. The
6115 * start-highlighting code must be written with the cursor on the
6116 * first highlighted character. The stop-highlighting code must
6117 * be written with the cursor just after the last highlighted
6118 * character.
6119 * Overwriting a character doesn't remove it's highlighting. Need
6120 * to clear the rest of the line, and force redrawing it
6121 * completely.
6122 */
6123 if ( p_wiv
6124 && !force
6125#ifdef FEAT_GUI
6126 && !gui.in_use
6127#endif
6128 && ScreenAttrs[off_to] != 0
6129 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6130 {
6131 /*
6132 * Need to remove highlighting attributes here.
6133 */
6134 windgoto(row, col + coloff);
6135 out_str(T_CE); /* clear rest of this screen line */
6136 screen_start(); /* don't know where cursor is now */
6137 force = TRUE; /* force redraw of rest of the line */
6138 redraw_next = TRUE; /* or else next char would miss out */
6139
6140 /*
6141 * If the previous character was highlighted, need to stop
6142 * highlighting at this character.
6143 */
6144 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6145 {
6146 screen_attr = ScreenAttrs[off_to - 1];
6147 term_windgoto(row, col + coloff);
6148 screen_stop_highlight();
6149 }
6150 else
6151 screen_attr = 0; /* highlighting has stopped */
6152 }
6153#ifdef FEAT_MBYTE
6154 if (enc_dbcs != 0)
6155 {
6156 /* Check if overwriting a double-byte with a single-byte or
6157 * the other way around requires another character to be
6158 * redrawn. For UTF-8 this isn't needed, because comparing
6159 * ScreenLinesUC[] is sufficient. */
6160 if (char_cells == 1
6161 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006162 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 {
6164 /* Writing a single-cell character over a double-cell
6165 * character: need to redraw the next cell. */
6166 ScreenLines[off_to + 1] = 0;
6167 redraw_next = TRUE;
6168 }
6169 else if (char_cells == 2
6170 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006171 && (*mb_off2cells)(off_to, max_off_to) == 1
6172 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
6174 /* Writing the second half of a double-cell character over
6175 * a double-cell character: need to redraw the second
6176 * cell. */
6177 ScreenLines[off_to + 2] = 0;
6178 redraw_next = TRUE;
6179 }
6180
6181 if (enc_dbcs == DBCS_JPNU)
6182 ScreenLines2[off_to] = ScreenLines2[off_from];
6183 }
6184 /* When writing a single-width character over a double-width
6185 * character and at the end of the redrawn text, need to clear out
6186 * the right halve of the old character.
6187 * Also required when writing the right halve of a double-width
6188 * char over the left halve of an existing one. */
6189 if (has_mbyte && col + char_cells == endcol
6190 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006191 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006193 && (*mb_off2cells)(off_to, max_off_to) == 1
6194 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 clear_next = TRUE;
6196#endif
6197
6198 ScreenLines[off_to] = ScreenLines[off_from];
6199#ifdef FEAT_MBYTE
6200 if (enc_utf8)
6201 {
6202 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6203 if (ScreenLinesUC[off_from] != 0)
6204 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006205 int i;
6206
6207 for (i = 0; i < Screen_mco; ++i)
6208 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 }
6211 if (char_cells == 2)
6212 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6213#endif
6214
6215#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006216 /* The bold trick makes a single column of pixels appear in the
6217 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 * character should be redrawn too. This happens for our own GUI
6219 * and for some xterms. */
6220 if (
6221# ifdef FEAT_GUI
6222 gui.in_use
6223# endif
6224# if defined(FEAT_GUI) && defined(UNIX)
6225 ||
6226# endif
6227# ifdef UNIX
6228 term_is_xterm
6229# endif
6230 )
6231 {
6232 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006233 if (hl > HL_ALL)
6234 hl = syn_attr2attr(hl);
6235 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 redraw_next = TRUE;
6237 }
6238#endif
6239 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6240#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006241 /* For simplicity set the attributes of second half of a
6242 * double-wide character equal to the first half. */
6243 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006245
6246 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 else
6249#endif
6250 screen_char(off_to, row, col + coloff);
6251 }
6252 else if ( p_wiv
6253#ifdef FEAT_GUI
6254 && !gui.in_use
6255#endif
6256 && col + coloff > 0)
6257 {
6258 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6259 {
6260 /*
6261 * Don't output stop-highlight when moving the cursor, it will
6262 * stop the highlighting when it should continue.
6263 */
6264 screen_attr = 0;
6265 }
6266 else if (screen_attr != 0)
6267 screen_stop_highlight();
6268 }
6269
6270 off_to += CHAR_CELLS;
6271 off_from += CHAR_CELLS;
6272 col += CHAR_CELLS;
6273 }
6274
6275#ifdef FEAT_MBYTE
6276 if (clear_next)
6277 {
6278 /* Clear the second half of a double-wide character of which the left
6279 * half was overwritten with a single-wide character. */
6280 ScreenLines[off_to] = ' ';
6281 if (enc_utf8)
6282 ScreenLinesUC[off_to] = 0;
6283 screen_char(off_to, row, col + coloff);
6284 }
6285#endif
6286
6287 if (clear_width > 0
6288#ifdef FEAT_RIGHTLEFT
6289 && !rlflag
6290#endif
6291 )
6292 {
6293#ifdef FEAT_GUI
6294 int startCol = col;
6295#endif
6296
6297 /* blank out the rest of the line */
6298 while (col < clear_width && ScreenLines[off_to] == ' '
6299 && ScreenAttrs[off_to] == 0
6300#ifdef FEAT_MBYTE
6301 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6302#endif
6303 )
6304 {
6305 ++off_to;
6306 ++col;
6307 }
6308 if (col < clear_width)
6309 {
6310#ifdef FEAT_GUI
6311 /*
6312 * In the GUI, clearing the rest of the line may leave pixels
6313 * behind if the first character cleared was bold. Some bold
6314 * fonts spill over the left. In this case we redraw the previous
6315 * character too. If we didn't skip any blanks above, then we
6316 * only redraw if the character wasn't already redrawn anyway.
6317 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006318 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
6320 hl = ScreenAttrs[off_to];
6321 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006322 {
6323 int prev_cells = 1;
6324# ifdef FEAT_MBYTE
6325 if (enc_utf8)
6326 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6327 * that its width is 2. */
6328 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6329 else if (enc_dbcs != 0)
6330 {
6331 /* find previous character by counting from first
6332 * column and get its width. */
6333 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006334 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006335
6336 while (off < off_to)
6337 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006338 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006339 off += prev_cells;
6340 }
6341 }
6342
6343 if (enc_dbcs != 0 && prev_cells > 1)
6344 screen_char_2(off_to - prev_cells, row,
6345 col + coloff - prev_cells);
6346 else
6347# endif
6348 screen_char(off_to - prev_cells, row,
6349 col + coloff - prev_cells);
6350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
6352#endif
6353 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6354 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006355#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 off_to += clear_width - col;
6357 col = clear_width;
6358#endif
6359 }
6360 }
6361
6362 if (clear_width > 0)
6363 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006364#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 /* For a window that's left of another, draw the separator char. */
6366 if (col + coloff < Columns)
6367 {
6368 int c;
6369
6370 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006371 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006373 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6374 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375# endif
6376 || ScreenAttrs[off_to] != hl)
6377 {
6378 ScreenLines[off_to] = c;
6379 ScreenAttrs[off_to] = hl;
6380# ifdef FEAT_MBYTE
6381 if (enc_utf8)
6382 {
6383 if (c >= 0x80)
6384 {
6385 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006386 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
6388 else
6389 ScreenLinesUC[off_to] = 0;
6390 }
6391# endif
6392 screen_char(off_to, row, col + coloff);
6393 }
6394 }
6395 else
6396#endif
6397 LineWraps[row] = FALSE;
6398 }
6399}
6400
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006401#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006403 * Mirror text "str" for right-left displaying.
6404 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006407rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 char_u *p1, *p2;
6410 int t;
6411
6412 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6413 {
6414 t = *p1;
6415 *p1 = *p2;
6416 *p2 = t;
6417 }
6418}
6419#endif
6420
6421#if defined(FEAT_WINDOWS) || defined(PROTO)
6422/*
6423 * mark all status lines for redraw; used after first :cd
6424 */
6425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006426status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 win_T *wp;
6429
Bram Moolenaar29323592016-07-24 22:04:11 +02006430 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (wp->w_status_height)
6432 {
6433 wp->w_redr_status = TRUE;
6434 redraw_later(VALID);
6435 }
6436}
6437
6438/*
6439 * mark all status lines of the current buffer for redraw
6440 */
6441 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006442status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 win_T *wp;
6445
Bram Moolenaar29323592016-07-24 22:04:11 +02006446 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6448 {
6449 wp->w_redr_status = TRUE;
6450 redraw_later(VALID);
6451 }
6452}
6453
6454/*
6455 * Redraw all status lines that need to be redrawn.
6456 */
6457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006458redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 win_T *wp;
6461
Bram Moolenaar29323592016-07-24 22:04:11 +02006462 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (wp->w_redr_status)
6464 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006465 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006466 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467}
6468#endif
6469
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006470#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471/*
6472 * Redraw all status lines at the bottom of frame "frp".
6473 */
6474 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006475win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 if (frp->fr_layout == FR_LEAF)
6478 frp->fr_win->w_redr_status = TRUE;
6479 else if (frp->fr_layout == FR_ROW)
6480 {
6481 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6482 win_redraw_last_status(frp);
6483 }
6484 else /* frp->fr_layout == FR_COL */
6485 {
6486 frp = frp->fr_child;
6487 while (frp->fr_next != NULL)
6488 frp = frp->fr_next;
6489 win_redraw_last_status(frp);
6490 }
6491}
6492#endif
6493
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006494#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495/*
6496 * Draw the verticap separator right of window "wp" starting with line "row".
6497 */
6498 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006499draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
6501 int hl;
6502 int c;
6503
6504 if (wp->w_vsep_width)
6505 {
6506 /* draw the vertical separator right of this window */
6507 c = fillchar_vsep(&hl);
6508 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6509 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6510 c, ' ', hl);
6511 }
6512}
6513#endif
6514
6515#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006516static int status_match_len(expand_T *xp, char_u *s);
6517static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006520 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 */
6522 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006523status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 int len = 0;
6526
6527#ifdef FEAT_MENU
6528 int emenu = (xp->xp_context == EXPAND_MENUS
6529 || xp->xp_context == EXPAND_MENUNAMES);
6530
6531 /* Check for menu separators - replace with '|'. */
6532 if (emenu && menu_is_separator(s))
6533 return 1;
6534#endif
6535
6536 while (*s != NUL)
6537 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006538 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006539 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006540 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
6542
6543 return len;
6544}
6545
6546/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006547 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006548 * These are backslashes used for escaping. Do show backslashes in help tags.
6549 */
6550 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006551skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006552{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006553 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006554#ifdef FEAT_MENU
6555 || ((xp->xp_context == EXPAND_MENUS
6556 || xp->xp_context == EXPAND_MENUNAMES)
6557 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6558#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006559 )
6560 {
6561#ifndef BACKSLASH_IN_FILENAME
6562 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6563 return 2;
6564#endif
6565 return 1;
6566 }
6567 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006568}
6569
6570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 * Show wildchar matches in the status line.
6572 * Show at least the "match" item.
6573 * We start at item 'first_match' in the list and show all matches that fit.
6574 *
6575 * If inversion is possible we use it. Else '=' characters are used.
6576 */
6577 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006578win_redr_status_matches(
6579 expand_T *xp,
6580 int num_matches,
6581 char_u **matches, /* list of matches */
6582 int match,
6583 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6586 int row;
6587 char_u *buf;
6588 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006589 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 int fillchar;
6591 int attr;
6592 int i;
6593 int highlight = TRUE;
6594 char_u *selstart = NULL;
6595 int selstart_col = 0;
6596 char_u *selend = NULL;
6597 static int first_match = 0;
6598 int add_left = FALSE;
6599 char_u *s;
6600#ifdef FEAT_MENU
6601 int emenu;
6602#endif
6603#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6604 int l;
6605#endif
6606
6607 if (matches == NULL) /* interrupted completion? */
6608 return;
6609
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006610#ifdef FEAT_MBYTE
6611 if (has_mbyte)
6612 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6613 else
6614#endif
6615 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (buf == NULL)
6617 return;
6618
6619 if (match == -1) /* don't show match but original text */
6620 {
6621 match = 0;
6622 highlight = FALSE;
6623 }
6624 /* count 1 for the ending ">" */
6625 clen = status_match_len(xp, L_MATCH(match)) + 3;
6626 if (match == 0)
6627 first_match = 0;
6628 else if (match < first_match)
6629 {
6630 /* jumping left, as far as we can go */
6631 first_match = match;
6632 add_left = TRUE;
6633 }
6634 else
6635 {
6636 /* check if match fits on the screen */
6637 for (i = first_match; i < match; ++i)
6638 clen += status_match_len(xp, L_MATCH(i)) + 2;
6639 if (first_match > 0)
6640 clen += 2;
6641 /* jumping right, put match at the left */
6642 if ((long)clen > Columns)
6643 {
6644 first_match = match;
6645 /* if showing the last match, we can add some on the left */
6646 clen = 2;
6647 for (i = match; i < num_matches; ++i)
6648 {
6649 clen += status_match_len(xp, L_MATCH(i)) + 2;
6650 if ((long)clen >= Columns)
6651 break;
6652 }
6653 if (i == num_matches)
6654 add_left = TRUE;
6655 }
6656 }
6657 if (add_left)
6658 while (first_match > 0)
6659 {
6660 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6661 if ((long)clen >= Columns)
6662 break;
6663 --first_match;
6664 }
6665
6666 fillchar = fillchar_status(&attr, TRUE);
6667
6668 if (first_match == 0)
6669 {
6670 *buf = NUL;
6671 len = 0;
6672 }
6673 else
6674 {
6675 STRCPY(buf, "< ");
6676 len = 2;
6677 }
6678 clen = len;
6679
6680 i = first_match;
6681 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6682 {
6683 if (i == match)
6684 {
6685 selstart = buf + len;
6686 selstart_col = clen;
6687 }
6688
6689 s = L_MATCH(i);
6690 /* Check for menu separators - replace with '|' */
6691#ifdef FEAT_MENU
6692 emenu = (xp->xp_context == EXPAND_MENUS
6693 || xp->xp_context == EXPAND_MENUNAMES);
6694 if (emenu && menu_is_separator(s))
6695 {
6696 STRCPY(buf + len, transchar('|'));
6697 l = (int)STRLEN(buf + len);
6698 len += l;
6699 clen += l;
6700 }
6701 else
6702#endif
6703 for ( ; *s != NUL; ++s)
6704 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006705 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 clen += ptr2cells(s);
6707#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006708 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 {
6710 STRNCPY(buf + len, s, l);
6711 s += l - 1;
6712 len += l;
6713 }
6714 else
6715#endif
6716 {
6717 STRCPY(buf + len, transchar_byte(*s));
6718 len += (int)STRLEN(buf + len);
6719 }
6720 }
6721 if (i == match)
6722 selend = buf + len;
6723
6724 *(buf + len++) = ' ';
6725 *(buf + len++) = ' ';
6726 clen += 2;
6727 if (++i == num_matches)
6728 break;
6729 }
6730
6731 if (i != num_matches)
6732 {
6733 *(buf + len++) = '>';
6734 ++clen;
6735 }
6736
6737 buf[len] = NUL;
6738
6739 row = cmdline_row - 1;
6740 if (row >= 0)
6741 {
6742 if (wild_menu_showing == 0)
6743 {
6744 if (msg_scrolled > 0)
6745 {
6746 /* Put the wildmenu just above the command line. If there is
6747 * no room, scroll the screen one line up. */
6748 if (cmdline_row == Rows - 1)
6749 {
6750 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6751 ++msg_scrolled;
6752 }
6753 else
6754 {
6755 ++cmdline_row;
6756 ++row;
6757 }
6758 wild_menu_showing = WM_SCROLLED;
6759 }
6760 else
6761 {
6762 /* Create status line if needed by setting 'laststatus' to 2.
6763 * Set 'winminheight' to zero to avoid that the window is
6764 * resized. */
6765 if (lastwin->w_status_height == 0)
6766 {
6767 save_p_ls = p_ls;
6768 save_p_wmh = p_wmh;
6769 p_ls = 2;
6770 p_wmh = 0;
6771 last_status(FALSE);
6772 }
6773 wild_menu_showing = WM_SHOWN;
6774 }
6775 }
6776
6777 screen_puts(buf, row, 0, attr);
6778 if (selstart != NULL && highlight)
6779 {
6780 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006781 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 }
6783
6784 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6785 }
6786
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006787#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 win_redraw_last_status(topframe);
6789#else
6790 lastwin->w_redr_status = TRUE;
6791#endif
6792 vim_free(buf);
6793}
6794#endif
6795
6796#if defined(FEAT_WINDOWS) || defined(PROTO)
6797/*
6798 * Redraw the status line of window wp.
6799 *
6800 * If inversion is possible we use it. Else '=' characters are used.
6801 */
6802 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006803win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 int row;
6806 char_u *p;
6807 int len;
6808 int fillchar;
6809 int attr;
6810 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006811 static int busy = FALSE;
6812
6813 /* It's possible to get here recursively when 'statusline' (indirectly)
6814 * invokes ":redrawstatus". Simply ignore the call then. */
6815 if (busy)
6816 return;
6817 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 wp->w_redr_status = FALSE;
6820 if (wp->w_status_height == 0)
6821 {
6822 /* no status line, can only be last window */
6823 redraw_cmdline = TRUE;
6824 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006825 else if (!redrawing()
6826#ifdef FEAT_INS_EXPAND
6827 /* don't update status line when popup menu is visible and may be
6828 * drawn over it */
6829 || pum_visible()
6830#endif
6831 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {
6833 /* Don't redraw right now, do it later. */
6834 wp->w_redr_status = TRUE;
6835 }
6836#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006837 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {
6839 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006840 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842#endif
6843 else
6844 {
6845 fillchar = fillchar_status(&attr, wp == curwin);
6846
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006847 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 p = NameBuff;
6849 len = (int)STRLEN(p);
6850
Bram Moolenaard85f2712017-07-28 21:51:57 +02006851 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852#ifdef FEAT_QUICKFIX
6853 || wp->w_p_pvw
6854#endif
6855 || bufIsChanged(wp->w_buffer)
6856 || wp->w_buffer->b_p_ro)
6857 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006858 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006860 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 len += (int)STRLEN(p + len);
6862 }
6863#ifdef FEAT_QUICKFIX
6864 if (wp->w_p_pvw)
6865 {
6866 STRCPY(p + len, _("[Preview]"));
6867 len += (int)STRLEN(p + len);
6868 }
6869#endif
6870 if (bufIsChanged(wp->w_buffer))
6871 {
6872 STRCPY(p + len, "[+]");
6873 len += 3;
6874 }
6875 if (wp->w_buffer->b_p_ro)
6876 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006877 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006878 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6882 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6883 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6884 if (this_ru_col <= 1)
6885 {
6886 p = (char_u *)"<"; /* No room for file name! */
6887 len = 1;
6888 }
6889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890#ifdef FEAT_MBYTE
6891 if (has_mbyte)
6892 {
6893 int clen = 0, i;
6894
6895 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006896 clen = mb_string2cells(p, -1);
6897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 /* Find first character that will fit.
6899 * Going from start to end is much faster for DBCS. */
6900 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006901 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 clen -= (*mb_ptr2cells)(p + i);
6903 len = clen;
6904 if (i > 0)
6905 {
6906 p = p + i - 1;
6907 *p = '<';
6908 ++len;
6909 }
6910
6911 }
6912 else
6913#endif
6914 if (len > this_ru_col - 1)
6915 {
6916 p += len - (this_ru_col - 1);
6917 *p = '<';
6918 len = this_ru_col - 1;
6919 }
6920
6921 row = W_WINROW(wp) + wp->w_height;
6922 screen_puts(p, row, W_WINCOL(wp), attr);
6923 screen_fill(row, row + 1, len + W_WINCOL(wp),
6924 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6925
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006926 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6928 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6929 - 1 + W_WINCOL(wp)), attr);
6930
6931#ifdef FEAT_CMDL_INFO
6932 win_redr_ruler(wp, TRUE);
6933#endif
6934 }
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 /*
6937 * May need to draw the character below the vertical separator.
6938 */
6939 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6940 {
6941 if (stl_connected(wp))
6942 fillchar = fillchar_status(&attr, wp == curwin);
6943 else
6944 fillchar = fillchar_vsep(&attr);
6945 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6946 attr);
6947 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006948 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949}
6950
Bram Moolenaar238a5642006-02-21 22:12:05 +00006951#ifdef FEAT_STL_OPT
6952/*
6953 * Redraw the status line according to 'statusline' and take care of any
6954 * errors encountered.
6955 */
6956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006957redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006958{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006959 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006960 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006961
6962 /* When called recursively return. This can happen when the statusline
6963 * contains an expression that triggers a redraw. */
6964 if (entered)
6965 return;
6966 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006967
Bram Moolenaara742e082016-04-05 21:10:38 +02006968 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006969 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006970 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006971 {
6972 /* When there is an error disable the statusline, otherwise the
6973 * display is messed up with errors and a redraw triggers the problem
6974 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006975 set_string_option_direct((char_u *)"statusline", -1,
6976 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006977 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006978 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006979 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006980 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006981}
6982#endif
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984/*
6985 * Return TRUE if the status line of window "wp" is connected to the status
6986 * line of the window right of it. If not, then it's a vertical separator.
6987 * Only call if (wp->w_vsep_width != 0).
6988 */
6989 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006990stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 frame_T *fr;
6993
6994 fr = wp->w_frame;
6995 while (fr->fr_parent != NULL)
6996 {
6997 if (fr->fr_parent->fr_layout == FR_COL)
6998 {
6999 if (fr->fr_next != NULL)
7000 break;
7001 }
7002 else
7003 {
7004 if (fr->fr_next != NULL)
7005 return TRUE;
7006 }
7007 fr = fr->fr_parent;
7008 }
7009 return FALSE;
7010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012#endif /* FEAT_WINDOWS */
7013
7014#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7015/*
7016 * Get the value to show for the language mappings, active 'keymap'.
7017 */
7018 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007019get_keymap_str(
7020 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007021 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007022 char_u *buf, /* buffer for the result */
7023 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 char_u *p;
7026
7027 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7028 return FALSE;
7029
7030 {
7031#ifdef FEAT_EVAL
7032 buf_T *old_curbuf = curbuf;
7033 win_T *old_curwin = curwin;
7034 char_u *s;
7035
7036 curbuf = wp->w_buffer;
7037 curwin = wp;
7038 STRCPY(buf, "b:keymap_name"); /* must be writable */
7039 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007040 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 --emsg_skip;
7042 curbuf = old_curbuf;
7043 curwin = old_curwin;
7044 if (p == NULL || *p == NUL)
7045#endif
7046 {
7047#ifdef FEAT_KEYMAP
7048 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7049 p = wp->w_buffer->b_p_keymap;
7050 else
7051#endif
7052 p = (char_u *)"lang";
7053 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007054 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 buf[0] = NUL;
7056#ifdef FEAT_EVAL
7057 vim_free(s);
7058#endif
7059 }
7060 return buf[0] != NUL;
7061}
7062#endif
7063
7064#if defined(FEAT_STL_OPT) || defined(PROTO)
7065/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007066 * Redraw the status line or ruler of window "wp".
7067 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 */
7069 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007070win_redr_custom(
7071 win_T *wp,
7072 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007074 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 int attr;
7076 int curattr;
7077 int row;
7078 int col = 0;
7079 int maxwidth;
7080 int width;
7081 int n;
7082 int len;
7083 int fillchar;
7084 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007085 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007087 struct stl_hlrec hltab[STL_MAX_ITEM];
7088 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007090 win_T *ewp;
7091 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar1d633412013-12-11 15:52:01 +01007093 /* There is a tiny chance that this gets called recursively: When
7094 * redrawing a status line triggers redrawing the ruler or tabline.
7095 * Avoid trouble by not allowing recursion. */
7096 if (entered)
7097 return;
7098 entered = TRUE;
7099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007101 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007103 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007104 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007105 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007106 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007107 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007108 maxwidth = Columns;
7109# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007110 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007113 else
7114 {
7115 row = W_WINROW(wp) + wp->w_height;
7116 fillchar = fillchar_status(&attr, wp == curwin);
7117 maxwidth = W_WIDTH(wp);
7118
7119 if (draw_ruler)
7120 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007121 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007122 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007123 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007125 if (*++stl == '-')
7126 stl++;
7127 if (atoi((char *)stl))
7128 while (VIM_ISDIGIT(*stl))
7129 stl++;
7130 if (*stl++ != '(')
7131 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007132 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007133#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134 col = ru_col - (Columns - W_WIDTH(wp));
7135 if (col < (W_WIDTH(wp) + 1) / 2)
7136 col = (W_WIDTH(wp) + 1) / 2;
7137#else
7138 col = ru_col;
7139 if (col > (Columns + 1) / 2)
7140 col = (Columns + 1) / 2;
7141#endif
7142 maxwidth = W_WIDTH(wp) - col;
7143#ifdef FEAT_WINDOWS
7144 if (!wp->w_status_height)
7145#endif
7146 {
7147 row = Rows - 1;
7148 --maxwidth; /* writing in last column may cause scrolling */
7149 fillchar = ' ';
7150 attr = 0;
7151 }
7152
7153# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007154 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007155# endif
7156 }
7157 else
7158 {
7159 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007163# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007164 use_sandbox = was_set_insecurely((char_u *)"statusline",
7165 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166# endif
7167 }
7168
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007169#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007170 col += W_WINCOL(wp);
7171#endif
7172 }
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007175 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
Bram Moolenaar61452852011-02-01 18:01:11 +01007177 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7178 * the cursor away and back. */
7179 ewp = wp == NULL ? curwin : wp;
7180 p_crb_save = ewp->w_p_crb;
7181 ewp->w_p_crb = FALSE;
7182
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 /* Make a copy, because the statusline may include a function call that
7184 * might change the option value and free the memory. */
7185 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007186 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007187 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007188 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007190 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007192 /* Make all characters printable. */
7193 p = transstr(buf);
7194 if (p != NULL)
7195 {
7196 vim_strncpy(buf, p, sizeof(buf) - 1);
7197 vim_free(p);
7198 }
7199
7200 /* fill up with "fillchar" */
7201 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007202 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
7204#ifdef FEAT_MBYTE
7205 len += (*mb_char2bytes)(fillchar, buf + len);
7206#else
7207 buf[len++] = fillchar;
7208#endif
7209 ++width;
7210 }
7211 buf[len] = NUL;
7212
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007213 /*
7214 * Draw each snippet with the specified highlighting.
7215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 curattr = attr;
7217 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007218 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007220 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 screen_puts_len(p, len, row, col, curattr);
7222 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007223 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007225 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007227 else if (hltab[n].userhl < 0)
7228 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007230 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007231 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232#endif
7233 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
7236 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007237
7238 if (wp == NULL)
7239 {
7240 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7241 col = 0;
7242 len = 0;
7243 p = buf;
7244 fillchar = 0;
7245 for (n = 0; tabtab[n].start != NULL; n++)
7246 {
7247 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7248 while (col < len)
7249 TabPageIdxs[col++] = fillchar;
7250 p = tabtab[n].start;
7251 fillchar = tabtab[n].userhl;
7252 }
7253 while (col < Columns)
7254 TabPageIdxs[col++] = fillchar;
7255 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007256
7257theend:
7258 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260
7261#endif /* FEAT_STL_OPT */
7262
7263/*
7264 * Output a single character directly to the screen and update ScreenLines.
7265 */
7266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007267screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 char_u buf[MB_MAXBYTES + 1];
7270
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007271#ifdef FEAT_MBYTE
7272 if (has_mbyte)
7273 buf[(*mb_char2bytes)(c, buf)] = NUL;
7274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007276 {
7277 buf[0] = c;
7278 buf[1] = NUL;
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 screen_puts(buf, row, col, attr);
7281}
7282
7283/*
7284 * Get a single character directly from ScreenLines into "bytes[]".
7285 * Also return its attribute in *attrp;
7286 */
7287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007288screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 unsigned off;
7291
7292 /* safety check */
7293 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7294 {
7295 off = LineOffset[row] + col;
7296 *attrp = ScreenAttrs[off];
7297 bytes[0] = ScreenLines[off];
7298 bytes[1] = NUL;
7299
7300#ifdef FEAT_MBYTE
7301 if (enc_utf8 && ScreenLinesUC[off] != 0)
7302 bytes[utfc_char2bytes(off, bytes)] = NUL;
7303 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7304 {
7305 bytes[0] = ScreenLines[off];
7306 bytes[1] = ScreenLines2[off];
7307 bytes[2] = NUL;
7308 }
7309 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7310 {
7311 bytes[1] = ScreenLines[off + 1];
7312 bytes[2] = NUL;
7313 }
7314#endif
7315 }
7316}
7317
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007318#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007319static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007320
7321/*
7322 * Return TRUE if composing characters for screen posn "off" differs from
7323 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007324 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007325 */
7326 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007327screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007328{
7329 int i;
7330
7331 for (i = 0; i < Screen_mco; ++i)
7332 {
7333 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7334 return TRUE;
7335 if (u8cc[i] == 0)
7336 break;
7337 }
7338 return FALSE;
7339}
7340#endif
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342/*
7343 * Put string '*text' on the screen at position 'row' and 'col', with
7344 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7345 * Note: only outputs within one row, message is truncated at screen boundary!
7346 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7347 */
7348 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007349screen_puts(
7350 char_u *text,
7351 int row,
7352 int col,
7353 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
7355 screen_puts_len(text, -1, row, col, attr);
7356}
7357
7358/*
7359 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7360 * a NUL.
7361 */
7362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007363screen_puts_len(
7364 char_u *text,
7365 int textlen,
7366 int row,
7367 int col,
7368 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
7370 unsigned off;
7371 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007372 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 int c;
7374#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007375 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 int mbyte_blen = 1;
7377 int mbyte_cells = 1;
7378 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007379 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 int clear_next_cell = FALSE;
7381# ifdef FEAT_ARABIC
7382 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007383 int pc, nc, nc1;
7384 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385# endif
7386#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007387#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7388 int force_redraw_this;
7389 int force_redraw_next = FALSE;
7390#endif
7391 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
7393 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7394 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007395 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaarc236c162008-07-13 17:41:49 +00007397#ifdef FEAT_MBYTE
7398 /* When drawing over the right halve of a double-wide char clear out the
7399 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007400 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007401# ifdef FEAT_GUI
7402 && !gui.in_use
7403# endif
7404 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007405 {
7406 ScreenLines[off - 1] = ' ';
7407 ScreenAttrs[off - 1] = 0;
7408 if (enc_utf8)
7409 {
7410 ScreenLinesUC[off - 1] = 0;
7411 ScreenLinesC[0][off - 1] = 0;
7412 }
7413 /* redraw the previous cell, make it empty */
7414 screen_char(off - 1, row, col - 1);
7415 /* force the cell at "col" to be redrawn */
7416 force_redraw_next = TRUE;
7417 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007418#endif
7419
Bram Moolenaar367329b2007-08-30 11:53:22 +00007420#ifdef FEAT_MBYTE
7421 max_off = LineOffset[row] + screen_Columns;
7422#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007423 while (col < screen_Columns
7424 && (len < 0 || (int)(ptr - text) < len)
7425 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
7427 c = *ptr;
7428#ifdef FEAT_MBYTE
7429 /* check if this is the first byte of a multibyte */
7430 if (has_mbyte)
7431 {
7432 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007433 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007435 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7437 mbyte_cells = 1;
7438 else if (enc_dbcs != 0)
7439 mbyte_cells = mbyte_blen;
7440 else /* enc_utf8 */
7441 {
7442 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007443 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 (int)((text + len) - ptr));
7445 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007446 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007448# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 /* Non-BMP character: display as ? or fullwidth ?. */
7450 if (u8c >= 0x10000)
7451 {
7452 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7453 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007454 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457# ifdef FEAT_ARABIC
7458 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7459 {
7460 /* Do Arabic shaping. */
7461 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7462 {
7463 /* Past end of string to be displayed. */
7464 nc = NUL;
7465 nc1 = NUL;
7466 }
7467 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007468 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007469 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7470 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007471 nc1 = pcc[0];
7472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 pc = prev_c;
7474 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007475 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477 else
7478 prev_c = u8c;
7479# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007480 if (col + mbyte_cells > screen_Columns)
7481 {
7482 /* Only 1 cell left, but character requires 2 cells:
7483 * display a '>' in the last column to avoid wrapping. */
7484 c = '>';
7485 mbyte_cells = 1;
7486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
7488 }
7489#endif
7490
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007491#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7492 force_redraw_this = force_redraw_next;
7493 force_redraw_next = FALSE;
7494#endif
7495
7496 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497#ifdef FEAT_MBYTE
7498 || (mbyte_cells == 2
7499 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7500 || (enc_dbcs == DBCS_JPNU
7501 && c == 0x8e
7502 && ScreenLines2[off] != ptr[1])
7503 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007504 && (ScreenLinesUC[off] !=
7505 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7506 || (ScreenLinesUC[off] != 0
7507 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508#endif
7509 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007510 || exmode_active;
7511
7512 if (need_redraw
7513#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7514 || force_redraw_this
7515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 )
7517 {
7518#if defined(FEAT_GUI) || defined(UNIX)
7519 /* The bold trick makes a single row of pixels appear in the next
7520 * character. When a bold character is removed, the next
7521 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007522 * and for some xterms. */
7523 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524# ifdef FEAT_GUI
7525 gui.in_use
7526# endif
7527# if defined(FEAT_GUI) && defined(UNIX)
7528 ||
7529# endif
7530# ifdef UNIX
7531 term_is_xterm
7532# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007537 if (n > HL_ALL)
7538 n = syn_attr2attr(n);
7539 if (n & HL_BOLD)
7540 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542#endif
7543#ifdef FEAT_MBYTE
7544 /* When at the end of the text and overwriting a two-cell
7545 * character with a one-cell character, need to clear the next
7546 * cell. Also when overwriting the left halve of a two-cell char
7547 * with the right halve of a two-cell char. Do this only once
7548 * (mb_off2cells() may return 2 on the right halve). */
7549 if (clear_next_cell)
7550 clear_next_cell = FALSE;
7551 else if (has_mbyte
7552 && (len < 0 ? ptr[mbyte_blen] == NUL
7553 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007554 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007556 && (*mb_off2cells)(off, max_off) == 1
7557 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 clear_next_cell = TRUE;
7559
7560 /* Make sure we never leave a second byte of a double-byte behind,
7561 * it confuses mb_off2cells(). */
7562 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007563 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007565 && (*mb_off2cells)(off, max_off) == 1
7566 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ScreenLines[off + mbyte_blen] = 0;
7568#endif
7569 ScreenLines[off] = c;
7570 ScreenAttrs[off] = attr;
7571#ifdef FEAT_MBYTE
7572 if (enc_utf8)
7573 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007574 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 ScreenLinesUC[off] = 0;
7576 else
7577 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007578 int i;
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007581 for (i = 0; i < Screen_mco; ++i)
7582 {
7583 ScreenLinesC[i][off] = u8cc[i];
7584 if (u8cc[i] == 0)
7585 break;
7586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 if (mbyte_cells == 2)
7589 {
7590 ScreenLines[off + 1] = 0;
7591 ScreenAttrs[off + 1] = attr;
7592 }
7593 screen_char(off, row, col);
7594 }
7595 else if (mbyte_cells == 2)
7596 {
7597 ScreenLines[off + 1] = ptr[1];
7598 ScreenAttrs[off + 1] = attr;
7599 screen_char_2(off, row, col);
7600 }
7601 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7602 {
7603 ScreenLines2[off] = ptr[1];
7604 screen_char(off, row, col);
7605 }
7606 else
7607#endif
7608 screen_char(off, row, col);
7609 }
7610#ifdef FEAT_MBYTE
7611 if (has_mbyte)
7612 {
7613 off += mbyte_cells;
7614 col += mbyte_cells;
7615 ptr += mbyte_blen;
7616 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007617 {
7618 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007620 len = -1;
7621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
7623 else
7624#endif
7625 {
7626 ++off;
7627 ++col;
7628 ++ptr;
7629 }
7630 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007631
7632#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7633 /* If we detected the next character needs to be redrawn, but the text
7634 * doesn't extend up to there, update the character here. */
7635 if (force_redraw_next && col < screen_Columns)
7636 {
7637# ifdef FEAT_MBYTE
7638 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7639 screen_char_2(off, row, col);
7640 else
7641# endif
7642 screen_char(off, row, col);
7643 }
7644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645}
7646
7647#ifdef FEAT_SEARCH_EXTRA
7648/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007649 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 */
7651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007652start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 if (p_hls && !no_hlsearch)
7655 {
7656 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007657 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007658# ifdef FEAT_RELTIME
7659 /* Set the time limit to 'redrawtime'. */
7660 profile_setlimit(p_rdt, &search_hl.tm);
7661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663}
7664
7665/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007666 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 */
7668 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007669end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 if (search_hl.rm.regprog != NULL)
7672 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007673 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 search_hl.rm.regprog = NULL;
7675 }
7676}
7677
7678/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007679 * Init for calling prepare_search_hl().
7680 */
7681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007682init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007683{
7684 matchitem_T *cur;
7685
7686 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7687 * match */
7688 cur = wp->w_match_head;
7689 while (cur != NULL)
7690 {
7691 cur->hl.rm = cur->match;
7692 if (cur->hlg_id == 0)
7693 cur->hl.attr = 0;
7694 else
7695 cur->hl.attr = syn_id2attr(cur->hlg_id);
7696 cur->hl.buf = wp->w_buffer;
7697 cur->hl.lnum = 0;
7698 cur->hl.first_lnum = 0;
7699# ifdef FEAT_RELTIME
7700 /* Set the time limit to 'redrawtime'. */
7701 profile_setlimit(p_rdt, &(cur->hl.tm));
7702# endif
7703 cur = cur->next;
7704 }
7705 search_hl.buf = wp->w_buffer;
7706 search_hl.lnum = 0;
7707 search_hl.first_lnum = 0;
7708 /* time limit is set at the toplevel, for all windows */
7709}
7710
7711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 * Advance to the match in window "wp" line "lnum" or past it.
7713 */
7714 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007715prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007717 matchitem_T *cur; /* points to the match list */
7718 match_T *shl; /* points to search_hl or a match */
7719 int shl_flag; /* flag to indicate whether search_hl
7720 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007721 int pos_inprogress; /* marks that position match search is
7722 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 int n;
7724
7725 /*
7726 * When using a multi-line pattern, start searching at the top
7727 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007728 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007730 cur = wp->w_match_head;
7731 shl_flag = FALSE;
7732 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007734 if (shl_flag == FALSE)
7735 {
7736 shl = &search_hl;
7737 shl_flag = TRUE;
7738 }
7739 else
7740 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (shl->rm.regprog != NULL
7742 && shl->lnum == 0
7743 && re_multiline(shl->rm.regprog))
7744 {
7745 if (shl->first_lnum == 0)
7746 {
7747# ifdef FEAT_FOLDING
7748 for (shl->first_lnum = lnum;
7749 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7750 if (hasFoldingWin(wp, shl->first_lnum - 1,
7751 NULL, NULL, TRUE, NULL))
7752 break;
7753# else
7754 shl->first_lnum = wp->w_topline;
7755# endif
7756 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007757 if (cur != NULL)
7758 cur->pos.cur = 0;
7759 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007761 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7762 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007764 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7765 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007766 pos_inprogress = cur == NULL || cur->pos.cur == 0
7767 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (shl->lnum != 0)
7769 {
7770 shl->first_lnum = shl->lnum
7771 + shl->rm.endpos[0].lnum
7772 - shl->rm.startpos[0].lnum;
7773 n = shl->rm.endpos[0].col;
7774 }
7775 else
7776 {
7777 ++shl->first_lnum;
7778 n = 0;
7779 }
7780 }
7781 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007782 if (shl != &search_hl && cur != NULL)
7783 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785}
7786
7787/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007788 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 * Uses shl->buf.
7790 * Sets shl->lnum and shl->rm contents.
7791 * Note: Assumes a previous match is always before "lnum", unless
7792 * shl->lnum is zero.
7793 * Careful: Any pointers for buffer lines will become invalid.
7794 */
7795 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007796next_search_hl(
7797 win_T *win,
7798 match_T *shl, /* points to search_hl or a match */
7799 linenr_T lnum,
7800 colnr_T mincol, /* minimal column for a match */
7801 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 linenr_T l;
7804 colnr_T matchcol;
7805 long nmatched;
7806
7807 if (shl->lnum != 0)
7808 {
7809 /* Check for three situations:
7810 * 1. If the "lnum" is below a previous match, start a new search.
7811 * 2. If the previous match includes "mincol", use it.
7812 * 3. Continue after the previous match.
7813 */
7814 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7815 if (lnum > l)
7816 shl->lnum = 0;
7817 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7818 return;
7819 }
7820
7821 /*
7822 * Repeat searching for a match until one is found that includes "mincol"
7823 * or none is found in this line.
7824 */
7825 called_emsg = FALSE;
7826 for (;;)
7827 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007828#ifdef FEAT_RELTIME
7829 /* Stop searching after passing the time limit. */
7830 if (profile_passed_limit(&(shl->tm)))
7831 {
7832 shl->lnum = 0; /* no match found in time */
7833 break;
7834 }
7835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 /* Three situations:
7837 * 1. No useful previous match: search from start of line.
7838 * 2. Not Vi compatible or empty match: continue at next character.
7839 * Break the loop if this is beyond the end of the line.
7840 * 3. Vi compatible searching: continue at end of previous match.
7841 */
7842 if (shl->lnum == 0)
7843 matchcol = 0;
7844 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7845 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007846 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007848 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007849
7850 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007851 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007852 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007854 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 shl->lnum = 0;
7856 break;
7857 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007858#ifdef FEAT_MBYTE
7859 if (has_mbyte)
7860 matchcol += mb_ptr2len(ml);
7861 else
7862#endif
7863 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 else
7866 matchcol = shl->rm.endpos[0].col;
7867
7868 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007869 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007871 /* Remember whether shl->rm is using a copy of the regprog in
7872 * cur->match. */
7873 int regprog_is_copy = (shl != &search_hl && cur != NULL
7874 && shl == &cur->hl
7875 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007876 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007877
Bram Moolenaarb3414592014-06-17 17:48:32 +02007878 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7879 matchcol,
7880#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007881 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007883 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007884#endif
7885 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007886 /* Copy the regprog, in case it got freed and recompiled. */
7887 if (regprog_is_copy)
7888 cur->match.regprog = cur->hl.rm.regprog;
7889
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007890 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007891 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892 /* Error while handling regexp: stop using this regexp. */
7893 if (shl == &search_hl)
7894 {
7895 /* don't free regprog in the match list, it's a copy */
7896 vim_regfree(shl->rm.regprog);
7897 SET_NO_HLSEARCH(TRUE);
7898 }
7899 shl->rm.regprog = NULL;
7900 shl->lnum = 0;
7901 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7902 message */
7903 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007904 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905 }
7906 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007907 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007908 else
7909 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 if (nmatched == 0)
7911 {
7912 shl->lnum = 0; /* no match found */
7913 break;
7914 }
7915 if (shl->rm.startpos[0].lnum > 0
7916 || shl->rm.startpos[0].col >= mincol
7917 || nmatched > 1
7918 || shl->rm.endpos[0].col > mincol)
7919 {
7920 shl->lnum += shl->rm.startpos[0].lnum;
7921 break; /* useful match found */
7922 }
7923 }
7924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
Bram Moolenaar85077472016-10-16 14:35:48 +02007926/*
7927 * If there is a match fill "shl" and return one.
7928 * Return zero otherwise.
7929 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007931next_search_hl_pos(
7932 match_T *shl, /* points to a match */
7933 linenr_T lnum,
7934 posmatch_T *posmatch, /* match positions */
7935 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007936{
7937 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007938 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007939
Bram Moolenaarb3414592014-06-17 17:48:32 +02007940 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7941 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007942 llpos_T *pos = &posmatch->pos[i];
7943
7944 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007946 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007948 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007949 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007950 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007951 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007952 /* if this match comes before the one at "found" then swap
7953 * them */
7954 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007956 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957
Bram Moolenaar85077472016-10-16 14:35:48 +02007958 *pos = posmatch->pos[found];
7959 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007960 }
7961 }
7962 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007963 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 }
7965 }
7966 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007967 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007969 colnr_T start = posmatch->pos[found].col == 0
7970 ? 0 : posmatch->pos[found].col - 1;
7971 colnr_T end = posmatch->pos[found].col == 0
7972 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007973
Bram Moolenaar85077472016-10-16 14:35:48 +02007974 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 shl->rm.startpos[0].lnum = 0;
7976 shl->rm.startpos[0].col = start;
7977 shl->rm.endpos[0].lnum = 0;
7978 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007979 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02007980 posmatch->cur = found + 1;
7981 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 }
Bram Moolenaar85077472016-10-16 14:35:48 +02007983 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007985#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007986
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007988screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 attrentry_T *aep = NULL;
7991
7992 screen_attr = attr;
7993 if (full_screen
7994#ifdef WIN3264
7995 && termcap_active
7996#endif
7997 )
7998 {
7999#ifdef FEAT_GUI
8000 if (gui.in_use)
8001 {
8002 char buf[20];
8003
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008004 /* The GUI handles this internally. */
8005 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 OUT_STR(buf);
8007 }
8008 else
8009#endif
8010 {
8011 if (attr > HL_ALL) /* special HL attr. */
8012 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008013 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 aep = syn_cterm_attr2entry(attr);
8015 else
8016 aep = syn_term_attr2entry(attr);
8017 if (aep == NULL) /* did ":syntax clear" */
8018 attr = 0;
8019 else
8020 attr = aep->ae_attr;
8021 }
8022 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8023 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008024 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008025#ifdef FEAT_TERMGUICOLORS
8026 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008027 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008028#endif
8029 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008030#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008031 )
8032#endif
8033 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008034 /* If the Normal FG color has BOLD attribute and the new HL
8035 * has a FG color defined, clear BOLD. */
8036 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8038 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008039 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8040 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 out_str(T_US);
8042 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8043 out_str(T_CZH);
8044 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8045 out_str(T_MR);
8046
8047 /*
8048 * Output the color or start string after bold etc., in case the
8049 * bold etc. override the color setting.
8050 */
8051 if (aep != NULL)
8052 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008053#ifdef FEAT_TERMGUICOLORS
8054 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008056 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008057 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008058 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008059 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008064 if (t_colors > 1)
8065 {
8066 if (aep->ae_u.cterm.fg_color)
8067 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8068 if (aep->ae_u.cterm.bg_color)
8069 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8070 }
8071 else
8072 {
8073 if (aep->ae_u.term.start != NULL)
8074 out_str(aep->ae_u.term.start);
8075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 }
8077 }
8078 }
8079 }
8080}
8081
8082 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 int do_ME = FALSE; /* output T_ME code */
8086
8087 if (screen_attr != 0
8088#ifdef WIN3264
8089 && termcap_active
8090#endif
8091 )
8092 {
8093#ifdef FEAT_GUI
8094 if (gui.in_use)
8095 {
8096 char buf[20];
8097
8098 /* use internal GUI code */
8099 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8100 OUT_STR(buf);
8101 }
8102 else
8103#endif
8104 {
8105 if (screen_attr > HL_ALL) /* special HL attr. */
8106 {
8107 attrentry_T *aep;
8108
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008109 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {
8111 /*
8112 * Assume that t_me restores the original colors!
8113 */
8114 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008115 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008116#ifdef FEAT_TERMGUICOLORS
8117 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008118 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8119 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008120#endif
8121 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008122#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008123 )
8124#endif
8125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 do_ME = TRUE;
8127 }
8128 else
8129 {
8130 aep = syn_term_attr2entry(screen_attr);
8131 if (aep != NULL && aep->ae_u.term.stop != NULL)
8132 {
8133 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8134 do_ME = TRUE;
8135 else
8136 out_str(aep->ae_u.term.stop);
8137 }
8138 }
8139 if (aep == NULL) /* did ":syntax clear" */
8140 screen_attr = 0;
8141 else
8142 screen_attr = aep->ae_attr;
8143 }
8144
8145 /*
8146 * Often all ending-codes are equal to T_ME. Avoid outputting the
8147 * same sequence several times.
8148 */
8149 if (screen_attr & HL_STANDOUT)
8150 {
8151 if (STRCMP(T_SE, T_ME) == 0)
8152 do_ME = TRUE;
8153 else
8154 out_str(T_SE);
8155 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008156 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
8158 if (STRCMP(T_UE, T_ME) == 0)
8159 do_ME = TRUE;
8160 else
8161 out_str(T_UE);
8162 }
8163 if (screen_attr & HL_ITALIC)
8164 {
8165 if (STRCMP(T_CZR, T_ME) == 0)
8166 do_ME = TRUE;
8167 else
8168 out_str(T_CZR);
8169 }
8170 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8171 out_str(T_ME);
8172
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008173#ifdef FEAT_TERMGUICOLORS
8174 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008176 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008177 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008178 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008179 term_bg_rgb_color(cterm_normal_bg_gui_color);
8180 }
8181 else
8182#endif
8183 {
8184 if (t_colors > 1)
8185 {
8186 /* set Normal cterm colors */
8187 if (cterm_normal_fg_color != 0)
8188 term_fg_color(cterm_normal_fg_color - 1);
8189 if (cterm_normal_bg_color != 0)
8190 term_bg_color(cterm_normal_bg_color - 1);
8191 if (cterm_normal_fg_bold)
8192 out_str(T_MD);
8193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 }
8195 }
8196 }
8197 screen_attr = 0;
8198}
8199
8200/*
8201 * Reset the colors for a cterm. Used when leaving Vim.
8202 * The machine specific code may override this again.
8203 */
8204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008205reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008207 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008210#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008211 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8212 || cterm_normal_bg_gui_color != INVALCOLOR)
8213 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008214#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
8218 out_str(T_OP);
8219 screen_attr = -1;
8220 }
8221 if (cterm_normal_fg_bold)
8222 {
8223 out_str(T_ME);
8224 screen_attr = -1;
8225 }
8226 }
8227}
8228
8229/*
8230 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8231 * using the attributes from ScreenAttrs["off"].
8232 */
8233 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008234screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 int attr;
8237
8238 /* Check for illegal values, just in case (could happen just after
8239 * resizing). */
8240 if (row >= screen_Rows || col >= screen_Columns)
8241 return;
8242
Bram Moolenaar494838a2015-02-10 19:20:37 +01008243 /* Outputting a character in the last cell on the screen may scroll the
8244 * screen up. Only do it when the "xn" termcap property is set, otherwise
8245 * mark the character invalid (update it when scrolled up). */
8246 if (*T_XN == NUL
8247 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248#ifdef FEAT_RIGHTLEFT
8249 /* account for first command-line character in rightleft mode */
8250 && !cmdmsg_rl
8251#endif
8252 )
8253 {
8254 ScreenAttrs[off] = (sattr_T)-1;
8255 return;
8256 }
8257
8258 /*
8259 * Stop highlighting first, so it's easier to move the cursor.
8260 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008261#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (screen_char_attr != 0)
8263 attr = screen_char_attr;
8264 else
8265#endif
8266 attr = ScreenAttrs[off];
8267 if (screen_attr != attr)
8268 screen_stop_highlight();
8269
8270 windgoto(row, col);
8271
8272 if (screen_attr != attr)
8273 screen_start_highlight(attr);
8274
8275#ifdef FEAT_MBYTE
8276 if (enc_utf8 && ScreenLinesUC[off] != 0)
8277 {
8278 char_u buf[MB_MAXBYTES + 1];
8279
8280 /* Convert UTF-8 character to bytes and write it. */
8281
8282 buf[utfc_char2bytes(off, buf)] = NUL;
8283
8284 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008285 if (utf_ambiguous_width(ScreenLinesUC[off]))
8286 screen_cur_col = 9999;
8287 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 ++screen_cur_col;
8289 }
8290 else
8291#endif
8292 {
8293#ifdef FEAT_MBYTE
8294 out_flush_check();
8295#endif
8296 out_char(ScreenLines[off]);
8297#ifdef FEAT_MBYTE
8298 /* double-byte character in single-width cell */
8299 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8300 out_char(ScreenLines2[off]);
8301#endif
8302 }
8303
8304 screen_cur_col++;
8305}
8306
8307#ifdef FEAT_MBYTE
8308
8309/*
8310 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8311 * on the screen at position 'row' and 'col'.
8312 * The attributes of the first byte is used for all. This is required to
8313 * output the two bytes of a double-byte character with nothing in between.
8314 */
8315 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008316screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 /* Check for illegal values (could be wrong when screen was resized). */
8319 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8320 return;
8321
8322 /* Outputting the last character on the screen may scrollup the screen.
8323 * Don't to it! Mark the character invalid (update it when scrolled up) */
8324 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8325 {
8326 ScreenAttrs[off] = (sattr_T)-1;
8327 return;
8328 }
8329
8330 /* Output the first byte normally (positions the cursor), then write the
8331 * second byte directly. */
8332 screen_char(off, row, col);
8333 out_char(ScreenLines[off + 1]);
8334 ++screen_cur_col;
8335}
8336#endif
8337
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008338#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339/*
8340 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8341 * This uses the contents of ScreenLines[] and doesn't change it.
8342 */
8343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008344screen_draw_rectangle(
8345 int row,
8346 int col,
8347 int height,
8348 int width,
8349 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 int r, c;
8352 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008353#ifdef FEAT_MBYTE
8354 int max_off;
8355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008357 /* Can't use ScreenLines unless initialized */
8358 if (ScreenLines == NULL)
8359 return;
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (invert)
8362 screen_char_attr = HL_INVERSE;
8363 for (r = row; r < row + height; ++r)
8364 {
8365 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008366#ifdef FEAT_MBYTE
8367 max_off = off + screen_Columns;
8368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 for (c = col; c < col + width; ++c)
8370 {
8371#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008372 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
8374 screen_char_2(off + c, r, c);
8375 ++c;
8376 }
8377 else
8378#endif
8379 {
8380 screen_char(off + c, r, c);
8381#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008382 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 ++c;
8384#endif
8385 }
8386 }
8387 }
8388 screen_char_attr = 0;
8389}
8390#endif
8391
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008392#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393/*
8394 * Redraw the characters for a vertically split window.
8395 */
8396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008397redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 int col;
8400 int width;
8401
8402# ifdef FEAT_CLIPBOARD
8403 clip_may_clear_selection(row, end - 1);
8404# endif
8405
8406 if (wp == NULL)
8407 {
8408 col = 0;
8409 width = Columns;
8410 }
8411 else
8412 {
8413 col = wp->w_wincol;
8414 width = wp->w_width;
8415 }
8416 screen_draw_rectangle(row, col, end - row, width, FALSE);
8417}
8418#endif
8419
8420/*
8421 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8422 * with character 'c1' in first column followed by 'c2' in the other columns.
8423 * Use attributes 'attr'.
8424 */
8425 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008426screen_fill(
8427 int start_row,
8428 int end_row,
8429 int start_col,
8430 int end_col,
8431 int c1,
8432 int c2,
8433 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
8435 int row;
8436 int col;
8437 int off;
8438 int end_off;
8439 int did_delete;
8440 int c;
8441 int norm_term;
8442#if defined(FEAT_GUI) || defined(UNIX)
8443 int force_next = FALSE;
8444#endif
8445
8446 if (end_row > screen_Rows) /* safety check */
8447 end_row = screen_Rows;
8448 if (end_col > screen_Columns) /* safety check */
8449 end_col = screen_Columns;
8450 if (ScreenLines == NULL
8451 || start_row >= end_row
8452 || start_col >= end_col) /* nothing to do */
8453 return;
8454
8455 /* it's a "normal" terminal when not in a GUI or cterm */
8456 norm_term = (
8457#ifdef FEAT_GUI
8458 !gui.in_use &&
8459#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008460 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 for (row = start_row; row < end_row; ++row)
8462 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008463#ifdef FEAT_MBYTE
8464 if (has_mbyte
8465# ifdef FEAT_GUI
8466 && !gui.in_use
8467# endif
8468 )
8469 {
8470 /* When drawing over the right halve of a double-wide char clear
8471 * out the left halve. When drawing over the left halve of a
8472 * double wide-char clear out the right halve. Only needed in a
8473 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008474 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008475 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008476 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008477 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008478 }
8479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 /*
8481 * Try to use delete-line termcap code, when no attributes or in a
8482 * "normal" terminal, where a bold/italic space is just a
8483 * space.
8484 */
8485 did_delete = FALSE;
8486 if (c2 == ' '
8487 && end_col == Columns
8488 && can_clear(T_CE)
8489 && (attr == 0
8490 || (norm_term
8491 && attr <= HL_ALL
8492 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8493 {
8494 /*
8495 * check if we really need to clear something
8496 */
8497 col = start_col;
8498 if (c1 != ' ') /* don't clear first char */
8499 ++col;
8500
8501 off = LineOffset[row] + col;
8502 end_off = LineOffset[row] + end_col;
8503
8504 /* skip blanks (used often, keep it fast!) */
8505#ifdef FEAT_MBYTE
8506 if (enc_utf8)
8507 while (off < end_off && ScreenLines[off] == ' '
8508 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8509 ++off;
8510 else
8511#endif
8512 while (off < end_off && ScreenLines[off] == ' '
8513 && ScreenAttrs[off] == 0)
8514 ++off;
8515 if (off < end_off) /* something to be cleared */
8516 {
8517 col = off - LineOffset[row];
8518 screen_stop_highlight();
8519 term_windgoto(row, col);/* clear rest of this screen line */
8520 out_str(T_CE);
8521 screen_start(); /* don't know where cursor is now */
8522 col = end_col - col;
8523 while (col--) /* clear chars in ScreenLines */
8524 {
8525 ScreenLines[off] = ' ';
8526#ifdef FEAT_MBYTE
8527 if (enc_utf8)
8528 ScreenLinesUC[off] = 0;
8529#endif
8530 ScreenAttrs[off] = 0;
8531 ++off;
8532 }
8533 }
8534 did_delete = TRUE; /* the chars are cleared now */
8535 }
8536
8537 off = LineOffset[row] + start_col;
8538 c = c1;
8539 for (col = start_col; col < end_col; ++col)
8540 {
8541 if (ScreenLines[off] != c
8542#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008543 || (enc_utf8 && (int)ScreenLinesUC[off]
8544 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545#endif
8546 || ScreenAttrs[off] != attr
8547#if defined(FEAT_GUI) || defined(UNIX)
8548 || force_next
8549#endif
8550 )
8551 {
8552#if defined(FEAT_GUI) || defined(UNIX)
8553 /* The bold trick may make a single row of pixels appear in
8554 * the next character. When a bold character is removed, the
8555 * next character should be redrawn too. This happens for our
8556 * own GUI and for some xterms. */
8557 if (
8558# ifdef FEAT_GUI
8559 gui.in_use
8560# endif
8561# if defined(FEAT_GUI) && defined(UNIX)
8562 ||
8563# endif
8564# ifdef UNIX
8565 term_is_xterm
8566# endif
8567 )
8568 {
8569 if (ScreenLines[off] != ' '
8570 && (ScreenAttrs[off] > HL_ALL
8571 || ScreenAttrs[off] & HL_BOLD))
8572 force_next = TRUE;
8573 else
8574 force_next = FALSE;
8575 }
8576#endif
8577 ScreenLines[off] = c;
8578#ifdef FEAT_MBYTE
8579 if (enc_utf8)
8580 {
8581 if (c >= 0x80)
8582 {
8583 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008584 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 }
8586 else
8587 ScreenLinesUC[off] = 0;
8588 }
8589#endif
8590 ScreenAttrs[off] = attr;
8591 if (!did_delete || c != ' ')
8592 screen_char(off, row, col);
8593 }
8594 ++off;
8595 if (col == start_col)
8596 {
8597 if (did_delete)
8598 break;
8599 c = c2;
8600 }
8601 }
8602 if (end_col == Columns)
8603 LineWraps[row] = FALSE;
8604 if (row == Rows - 1) /* overwritten the command line */
8605 {
8606 redraw_cmdline = TRUE;
8607 if (c1 == ' ' && c2 == ' ')
8608 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008609 if (start_col == 0)
8610 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
8612 }
8613}
8614
8615/*
8616 * Check if there should be a delay. Used before clearing or redrawing the
8617 * screen or the command line.
8618 */
8619 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008620check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8623 && !did_wait_return
8624 && emsg_silent == 0)
8625 {
8626 out_flush();
8627 ui_delay(1000L, TRUE);
8628 emsg_on_display = FALSE;
8629 if (check_msg_scroll)
8630 msg_scroll = FALSE;
8631 }
8632}
8633
8634/*
8635 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008636 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 * Returns TRUE if there is a valid screen to write to.
8638 * Returns FALSE when starting up and screen not initialized yet.
8639 */
8640 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008641screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008643 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 return (ScreenLines != NULL);
8645}
8646
8647/*
8648 * Resize the shell to Rows and Columns.
8649 * Allocate ScreenLines[] and associated items.
8650 *
8651 * There may be some time between setting Rows and Columns and (re)allocating
8652 * ScreenLines[]. This happens when starting up and when (manually) changing
8653 * the shell size. Always use screen_Rows and screen_Columns to access items
8654 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8655 * final size of the shell is needed.
8656 */
8657 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008658screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 int new_row, old_row;
8661#ifdef FEAT_GUI
8662 int old_Rows;
8663#endif
8664 win_T *wp;
8665 int outofmem = FALSE;
8666 int len;
8667 schar_T *new_ScreenLines;
8668#ifdef FEAT_MBYTE
8669 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008670 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008672 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673#endif
8674 sattr_T *new_ScreenAttrs;
8675 unsigned *new_LineOffset;
8676 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008677#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008678 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008679 tabpage_T *tp;
8680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008682 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008683#ifdef FEAT_AUTOCMD
8684 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008686retry:
8687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 /*
8689 * Allocation of the screen buffers is done only when the size changes and
8690 * when Rows and Columns have been set and we have started doing full
8691 * screen stuff.
8692 */
8693 if ((ScreenLines != NULL
8694 && Rows == screen_Rows
8695 && Columns == screen_Columns
8696#ifdef FEAT_MBYTE
8697 && enc_utf8 == (ScreenLinesUC != NULL)
8698 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008699 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700#endif
8701 )
8702 || Rows == 0
8703 || Columns == 0
8704 || (!full_screen && ScreenLines == NULL))
8705 return;
8706
8707 /*
8708 * It's possible that we produce an out-of-memory message below, which
8709 * will cause this function to be called again. To break the loop, just
8710 * return here.
8711 */
8712 if (entered)
8713 return;
8714 entered = TRUE;
8715
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008716 /*
8717 * Note that the window sizes are updated before reallocating the arrays,
8718 * thus we must not redraw here!
8719 */
8720 ++RedrawingDisabled;
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 win_new_shellsize(); /* fit the windows in the new sized shell */
8723
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 comp_col(); /* recompute columns for shown command and ruler */
8725
8726 /*
8727 * We're changing the size of the screen.
8728 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8729 * - Move lines from the old arrays into the new arrays, clear extra
8730 * lines (unless the screen is going to be cleared).
8731 * - Free the old arrays.
8732 *
8733 * If anything fails, make ScreenLines NULL, so we don't do anything!
8734 * Continuing with the old ScreenLines may result in a crash, because the
8735 * size is wrong.
8736 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008737 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008739#ifdef FEAT_AUTOCMD
8740 if (aucmd_win != NULL)
8741 win_free_lsize(aucmd_win);
8742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
8744 new_ScreenLines = (schar_T *)lalloc((long_u)(
8745 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8746#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008747 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (enc_utf8)
8749 {
8750 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8751 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008752 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008753 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8755 }
8756 if (enc_dbcs == DBCS_JPNU)
8757 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8758 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8759#endif
8760 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8761 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8762 new_LineOffset = (unsigned *)lalloc((long_u)(
8763 Rows * sizeof(unsigned)), FALSE);
8764 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008765#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008766 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008769 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 {
8771 if (win_alloc_lines(wp) == FAIL)
8772 {
8773 outofmem = TRUE;
8774#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008775 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776#endif
8777 }
8778 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008779#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008780 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8781 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008782 outofmem = TRUE;
8783#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008784#ifdef FEAT_WINDOWS
8785give_up:
8786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008788#ifdef FEAT_MBYTE
8789 for (i = 0; i < p_mco; ++i)
8790 if (new_ScreenLinesC[i] == NULL)
8791 break;
8792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 if (new_ScreenLines == NULL
8794#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008795 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8797#endif
8798 || new_ScreenAttrs == NULL
8799 || new_LineOffset == NULL
8800 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008801#ifdef FEAT_WINDOWS
8802 || new_TabPageIdxs == NULL
8803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 || outofmem)
8805 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008806 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008807 {
8808 /* guess the size */
8809 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8810
8811 /* Remember we did this to avoid getting outofmem messages over
8812 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008813 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 vim_free(new_ScreenLines);
8816 new_ScreenLines = NULL;
8817#ifdef FEAT_MBYTE
8818 vim_free(new_ScreenLinesUC);
8819 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008820 for (i = 0; i < p_mco; ++i)
8821 {
8822 vim_free(new_ScreenLinesC[i]);
8823 new_ScreenLinesC[i] = NULL;
8824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 vim_free(new_ScreenLines2);
8826 new_ScreenLines2 = NULL;
8827#endif
8828 vim_free(new_ScreenAttrs);
8829 new_ScreenAttrs = NULL;
8830 vim_free(new_LineOffset);
8831 new_LineOffset = NULL;
8832 vim_free(new_LineWraps);
8833 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008834#ifdef FEAT_WINDOWS
8835 vim_free(new_TabPageIdxs);
8836 new_TabPageIdxs = NULL;
8837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 }
8839 else
8840 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008841 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 for (new_row = 0; new_row < Rows; ++new_row)
8844 {
8845 new_LineOffset[new_row] = new_row * Columns;
8846 new_LineWraps[new_row] = FALSE;
8847
8848 /*
8849 * If the screen is not going to be cleared, copy as much as
8850 * possible from the old screen to the new one and clear the rest
8851 * (used when resizing the window at the "--more--" prompt or when
8852 * executing an external command, for the GUI).
8853 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008854 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
8856 (void)vim_memset(new_ScreenLines + new_row * Columns,
8857 ' ', (size_t)Columns * sizeof(schar_T));
8858#ifdef FEAT_MBYTE
8859 if (enc_utf8)
8860 {
8861 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8862 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 for (i = 0; i < p_mco; ++i)
8864 (void)vim_memset(new_ScreenLinesC[i]
8865 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 0, (size_t)Columns * sizeof(u8char_T));
8867 }
8868 if (enc_dbcs == DBCS_JPNU)
8869 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8870 0, (size_t)Columns * sizeof(schar_T));
8871#endif
8872 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8873 0, (size_t)Columns * sizeof(sattr_T));
8874 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008875 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {
8877 if (screen_Columns < Columns)
8878 len = screen_Columns;
8879 else
8880 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008881#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008882 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008883 * may be invalid now. Also when p_mco changes. */
8884 if (!(enc_utf8 && ScreenLinesUC == NULL)
8885 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008886#endif
8887 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8888 ScreenLines + LineOffset[old_row],
8889 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008891 if (enc_utf8 && ScreenLinesUC != NULL
8892 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 {
8894 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8895 ScreenLinesUC + LineOffset[old_row],
8896 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008897 for (i = 0; i < p_mco; ++i)
8898 mch_memmove(new_ScreenLinesC[i]
8899 + new_LineOffset[new_row],
8900 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 (size_t)len * sizeof(u8char_T));
8902 }
8903 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8904 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8905 ScreenLines2 + LineOffset[old_row],
8906 (size_t)len * sizeof(schar_T));
8907#endif
8908 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8909 ScreenAttrs + LineOffset[old_row],
8910 (size_t)len * sizeof(sattr_T));
8911 }
8912 }
8913 }
8914 /* Use the last line of the screen for the current line. */
8915 current_ScreenLine = new_ScreenLines + Rows * Columns;
8916 }
8917
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008918 free_screenlines();
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 ScreenLines = new_ScreenLines;
8921#ifdef FEAT_MBYTE
8922 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008923 for (i = 0; i < p_mco; ++i)
8924 ScreenLinesC[i] = new_ScreenLinesC[i];
8925 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 ScreenLines2 = new_ScreenLines2;
8927#endif
8928 ScreenAttrs = new_ScreenAttrs;
8929 LineOffset = new_LineOffset;
8930 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008931#ifdef FEAT_WINDOWS
8932 TabPageIdxs = new_TabPageIdxs;
8933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
8935 /* It's important that screen_Rows and screen_Columns reflect the actual
8936 * size of ScreenLines[]. Set them before calling anything. */
8937#ifdef FEAT_GUI
8938 old_Rows = screen_Rows;
8939#endif
8940 screen_Rows = Rows;
8941 screen_Columns = Columns;
8942
8943 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008944 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 screenclear2();
8946
8947#ifdef FEAT_GUI
8948 else if (gui.in_use
8949 && !gui.starting
8950 && ScreenLines != NULL
8951 && old_Rows != Rows)
8952 {
8953 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8954 /*
8955 * Adjust the position of the cursor, for when executing an external
8956 * command.
8957 */
8958 if (msg_row >= Rows) /* Rows got smaller */
8959 msg_row = Rows - 1; /* put cursor at last row */
8960 else if (Rows > old_Rows) /* Rows got bigger */
8961 msg_row += Rows - old_Rows; /* put cursor in same place */
8962 if (msg_col >= Columns) /* Columns got smaller */
8963 msg_col = Columns - 1; /* put cursor at last column */
8964 }
8965#endif
8966
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008968 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008969
8970#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008971 /*
8972 * Do not apply autocommands more than 3 times to avoid an endless loop
8973 * in case applying autocommands always changes Rows or Columns.
8974 */
8975 if (starting == 0 && ++retry_count <= 3)
8976 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008977 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008978 /* In rare cases, autocommands may have altered Rows or Columns,
8979 * jump back to check if we need to allocate the screen again. */
8980 goto retry;
8981 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984
8985 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008986free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008987{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008988#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 int i;
8990
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008991 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008992 for (i = 0; i < Screen_mco; ++i)
8993 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008994 vim_free(ScreenLines2);
8995#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008996 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008997 vim_free(ScreenAttrs);
8998 vim_free(LineOffset);
8999 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009000#ifdef FEAT_WINDOWS
9001 vim_free(TabPageIdxs);
9002#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009003}
9004
9005 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009006screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 check_for_delay(FALSE);
9009 screenalloc(FALSE); /* allocate screen buffers if size changed */
9010 screenclear2(); /* clear the screen */
9011}
9012
9013 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009014screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015{
9016 int i;
9017
9018 if (starting == NO_SCREEN || ScreenLines == NULL
9019#ifdef FEAT_GUI
9020 || (gui.in_use && gui.starting)
9021#endif
9022 )
9023 return;
9024
9025#ifdef FEAT_GUI
9026 if (!gui.in_use)
9027#endif
9028 screen_attr = -1; /* force setting the Normal colors */
9029 screen_stop_highlight(); /* don't want highlighting here */
9030
9031#ifdef FEAT_CLIPBOARD
9032 /* disable selection without redrawing it */
9033 clip_scroll_selection(9999);
9034#endif
9035
9036 /* blank out ScreenLines */
9037 for (i = 0; i < Rows; ++i)
9038 {
9039 lineclear(LineOffset[i], (int)Columns);
9040 LineWraps[i] = FALSE;
9041 }
9042
9043 if (can_clear(T_CL))
9044 {
9045 out_str(T_CL); /* clear the display */
9046 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009047 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 }
9049 else
9050 {
9051 /* can't clear the screen, mark all chars with invalid attributes */
9052 for (i = 0; i < Rows; ++i)
9053 lineinvalid(LineOffset[i], (int)Columns);
9054 clear_cmdline = TRUE;
9055 }
9056
9057 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9058
9059 win_rest_invalid(firstwin);
9060 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009061#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009062 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 if (must_redraw == CLEAR) /* no need to clear again */
9065 must_redraw = NOT_VALID;
9066 compute_cmdrow();
9067 msg_row = cmdline_row; /* put cursor on last line for messages */
9068 msg_col = 0;
9069 screen_start(); /* don't know where cursor is now */
9070 msg_scrolled = 0; /* can't scroll back */
9071 msg_didany = FALSE;
9072 msg_didout = FALSE;
9073}
9074
9075/*
9076 * Clear one line in ScreenLines.
9077 */
9078 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009079lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9082#ifdef FEAT_MBYTE
9083 if (enc_utf8)
9084 (void)vim_memset(ScreenLinesUC + off, 0,
9085 (size_t)width * sizeof(u8char_T));
9086#endif
9087 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9088}
9089
9090/*
9091 * Mark one line in ScreenLines invalid by setting the attributes to an
9092 * invalid value.
9093 */
9094 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009095lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9098}
9099
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009100#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101/*
9102 * Copy part of a Screenline for vertically split window "wp".
9103 */
9104 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009105linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 unsigned off_to = LineOffset[to] + wp->w_wincol;
9108 unsigned off_from = LineOffset[from] + wp->w_wincol;
9109
9110 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9111 wp->w_width * sizeof(schar_T));
9112# ifdef FEAT_MBYTE
9113 if (enc_utf8)
9114 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009115 int i;
9116
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9118 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009119 for (i = 0; i < p_mco; ++i)
9120 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9121 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
9123 if (enc_dbcs == DBCS_JPNU)
9124 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9125 wp->w_width * sizeof(schar_T));
9126# endif
9127 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9128 wp->w_width * sizeof(sattr_T));
9129}
9130#endif
9131
9132/*
9133 * Return TRUE if clearing with term string "p" would work.
9134 * It can't work when the string is empty or it won't set the right background.
9135 */
9136 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009137can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 return (*p != NUL && (t_colors <= 1
9140#ifdef FEAT_GUI
9141 || gui.in_use
9142#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009143#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009144 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009145 || (!p_tgc && cterm_normal_bg_color == 0)
9146#else
9147 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009148#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009149 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150}
9151
9152/*
9153 * Reset cursor position. Use whenever cursor was moved because of outputting
9154 * something directly to the screen (shell commands) or a terminal control
9155 * code.
9156 */
9157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009158screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160 screen_cur_row = screen_cur_col = 9999;
9161}
9162
9163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 * Move the cursor to position "row","col" in the screen.
9165 * This tries to find the most efficient way to move, minimizing the number of
9166 * characters sent to the terminal.
9167 */
9168 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009169windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009171 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 int i;
9173 int plan;
9174 int cost;
9175 int wouldbe_col;
9176 int noinvcurs;
9177 char_u *bs;
9178 int goto_cost;
9179 int attr;
9180
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009181#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9183
9184#define PLAN_LE 1
9185#define PLAN_CR 2
9186#define PLAN_NL 3
9187#define PLAN_WRITE 4
9188 /* Can't use ScreenLines unless initialized */
9189 if (ScreenLines == NULL)
9190 return;
9191
9192 if (col != screen_cur_col || row != screen_cur_row)
9193 {
9194 /* Check for valid position. */
9195 if (row < 0) /* window without text lines? */
9196 row = 0;
9197 if (row >= screen_Rows)
9198 row = screen_Rows - 1;
9199 if (col >= screen_Columns)
9200 col = screen_Columns - 1;
9201
9202 /* check if no cursor movement is allowed in highlight mode */
9203 if (screen_attr && *T_MS == NUL)
9204 noinvcurs = HIGHL_COST;
9205 else
9206 noinvcurs = 0;
9207 goto_cost = GOTO_COST + noinvcurs;
9208
9209 /*
9210 * Plan how to do the positioning:
9211 * 1. Use CR to move it to column 0, same row.
9212 * 2. Use T_LE to move it a few columns to the left.
9213 * 3. Use NL to move a few lines down, column 0.
9214 * 4. Move a few columns to the right with T_ND or by writing chars.
9215 *
9216 * Don't do this if the cursor went beyond the last column, the cursor
9217 * position is unknown then (some terminals wrap, some don't )
9218 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009219 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 * characters to move the cursor to the right.
9221 */
9222 if (row >= screen_cur_row && screen_cur_col < Columns)
9223 {
9224 /*
9225 * If the cursor is in the same row, bigger col, we can use CR
9226 * or T_LE.
9227 */
9228 bs = NULL; /* init for GCC */
9229 attr = screen_attr;
9230 if (row == screen_cur_row && col < screen_cur_col)
9231 {
9232 /* "le" is preferred over "bc", because "bc" is obsolete */
9233 if (*T_LE)
9234 bs = T_LE; /* "cursor left" */
9235 else
9236 bs = T_BC; /* "backspace character (old) */
9237 if (*bs)
9238 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9239 else
9240 cost = 999;
9241 if (col + 1 < cost) /* using CR is less characters */
9242 {
9243 plan = PLAN_CR;
9244 wouldbe_col = 0;
9245 cost = 1; /* CR is just one character */
9246 }
9247 else
9248 {
9249 plan = PLAN_LE;
9250 wouldbe_col = col;
9251 }
9252 if (noinvcurs) /* will stop highlighting */
9253 {
9254 cost += noinvcurs;
9255 attr = 0;
9256 }
9257 }
9258
9259 /*
9260 * If the cursor is above where we want to be, we can use CR LF.
9261 */
9262 else if (row > screen_cur_row)
9263 {
9264 plan = PLAN_NL;
9265 wouldbe_col = 0;
9266 cost = (row - screen_cur_row) * 2; /* CR LF */
9267 if (noinvcurs) /* will stop highlighting */
9268 {
9269 cost += noinvcurs;
9270 attr = 0;
9271 }
9272 }
9273
9274 /*
9275 * If the cursor is in the same row, smaller col, just use write.
9276 */
9277 else
9278 {
9279 plan = PLAN_WRITE;
9280 wouldbe_col = screen_cur_col;
9281 cost = 0;
9282 }
9283
9284 /*
9285 * Check if any characters that need to be written have the
9286 * correct attributes. Also avoid UTF-8 characters.
9287 */
9288 i = col - wouldbe_col;
9289 if (i > 0)
9290 cost += i;
9291 if (cost < goto_cost && i > 0)
9292 {
9293 /*
9294 * Check if the attributes are correct without additionally
9295 * stopping highlighting.
9296 */
9297 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9298 while (i && *p++ == attr)
9299 --i;
9300 if (i != 0)
9301 {
9302 /*
9303 * Try if it works when highlighting is stopped here.
9304 */
9305 if (*--p == 0)
9306 {
9307 cost += noinvcurs;
9308 while (i && *p++ == 0)
9309 --i;
9310 }
9311 if (i != 0)
9312 cost = 999; /* different attributes, don't do it */
9313 }
9314#ifdef FEAT_MBYTE
9315 if (enc_utf8)
9316 {
9317 /* Don't use an UTF-8 char for positioning, it's slow. */
9318 for (i = wouldbe_col; i < col; ++i)
9319 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9320 {
9321 cost = 999;
9322 break;
9323 }
9324 }
9325#endif
9326 }
9327
9328 /*
9329 * We can do it without term_windgoto()!
9330 */
9331 if (cost < goto_cost)
9332 {
9333 if (plan == PLAN_LE)
9334 {
9335 if (noinvcurs)
9336 screen_stop_highlight();
9337 while (screen_cur_col > col)
9338 {
9339 out_str(bs);
9340 --screen_cur_col;
9341 }
9342 }
9343 else if (plan == PLAN_CR)
9344 {
9345 if (noinvcurs)
9346 screen_stop_highlight();
9347 out_char('\r');
9348 screen_cur_col = 0;
9349 }
9350 else if (plan == PLAN_NL)
9351 {
9352 if (noinvcurs)
9353 screen_stop_highlight();
9354 while (screen_cur_row < row)
9355 {
9356 out_char('\n');
9357 ++screen_cur_row;
9358 }
9359 screen_cur_col = 0;
9360 }
9361
9362 i = col - screen_cur_col;
9363 if (i > 0)
9364 {
9365 /*
9366 * Use cursor-right if it's one character only. Avoids
9367 * removing a line of pixels from the last bold char, when
9368 * using the bold trick in the GUI.
9369 */
9370 if (T_ND[0] != NUL && T_ND[1] == NUL)
9371 {
9372 while (i-- > 0)
9373 out_char(*T_ND);
9374 }
9375 else
9376 {
9377 int off;
9378
9379 off = LineOffset[row] + screen_cur_col;
9380 while (i-- > 0)
9381 {
9382 if (ScreenAttrs[off] != screen_attr)
9383 screen_stop_highlight();
9384#ifdef FEAT_MBYTE
9385 out_flush_check();
9386#endif
9387 out_char(ScreenLines[off]);
9388#ifdef FEAT_MBYTE
9389 if (enc_dbcs == DBCS_JPNU
9390 && ScreenLines[off] == 0x8e)
9391 out_char(ScreenLines2[off]);
9392#endif
9393 ++off;
9394 }
9395 }
9396 }
9397 }
9398 }
9399 else
9400 cost = 999;
9401
9402 if (cost >= goto_cost)
9403 {
9404 if (noinvcurs)
9405 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009406 if (row == screen_cur_row && (col > screen_cur_col)
9407 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 term_cursor_right(col - screen_cur_col);
9409 else
9410 term_windgoto(row, col);
9411 }
9412 screen_cur_row = row;
9413 screen_cur_col = col;
9414 }
9415}
9416
9417/*
9418 * Set cursor to its position in the current window.
9419 */
9420 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009421setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 if (redrawing())
9424 {
9425 validate_cursor();
9426 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9427 W_WINCOL(curwin) + (
9428#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009429 /* With 'rightleft' set and the cursor on a double-wide
9430 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9432# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009433 (has_mbyte
9434 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9435 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436# endif
9437 1)) :
9438#endif
9439 curwin->w_wcol));
9440 }
9441}
9442
9443
9444/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009445 * Insert 'line_count' lines at 'row' in window 'wp'.
9446 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9447 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 * scrolling.
9449 * Returns FAIL if the lines are not inserted, OK for success.
9450 */
9451 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009452win_ins_lines(
9453 win_T *wp,
9454 int row,
9455 int line_count,
9456 int invalid,
9457 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459 int did_delete;
9460 int nextrow;
9461 int lastrow;
9462 int retval;
9463
9464 if (invalid)
9465 wp->w_lines_valid = 0;
9466
9467 if (wp->w_height < 5)
9468 return FAIL;
9469
9470 if (line_count > wp->w_height - row)
9471 line_count = wp->w_height - row;
9472
9473 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9474 if (retval != MAYBE)
9475 return retval;
9476
9477 /*
9478 * If there is a next window or a status line, we first try to delete the
9479 * lines at the bottom to avoid messing what is after the window.
9480 * If this fails and there are following windows, don't do anything to avoid
9481 * messing up those windows, better just redraw.
9482 */
9483 did_delete = FALSE;
9484#ifdef FEAT_WINDOWS
9485 if (wp->w_next != NULL || wp->w_status_height)
9486 {
9487 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9488 line_count, (int)Rows, FALSE, NULL) == OK)
9489 did_delete = TRUE;
9490 else if (wp->w_next)
9491 return FAIL;
9492 }
9493#endif
9494 /*
9495 * if no lines deleted, blank the lines that will end up below the window
9496 */
9497 if (!did_delete)
9498 {
9499#ifdef FEAT_WINDOWS
9500 wp->w_redr_status = TRUE;
9501#endif
9502 redraw_cmdline = TRUE;
9503 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9504 lastrow = nextrow + line_count;
9505 if (lastrow > Rows)
9506 lastrow = Rows;
9507 screen_fill(nextrow - line_count, lastrow - line_count,
9508 W_WINCOL(wp), (int)W_ENDCOL(wp),
9509 ' ', ' ', 0);
9510 }
9511
9512 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9513 == FAIL)
9514 {
9515 /* deletion will have messed up other windows */
9516 if (did_delete)
9517 {
9518#ifdef FEAT_WINDOWS
9519 wp->w_redr_status = TRUE;
9520#endif
9521 win_rest_invalid(W_NEXT(wp));
9522 }
9523 return FAIL;
9524 }
9525
9526 return OK;
9527}
9528
9529/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009530 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9532 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9533 * scrolling
9534 * Return OK for success, FAIL if the lines are not deleted.
9535 */
9536 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009537win_del_lines(
9538 win_T *wp,
9539 int row,
9540 int line_count,
9541 int invalid,
9542 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 int retval;
9545
9546 if (invalid)
9547 wp->w_lines_valid = 0;
9548
9549 if (line_count > wp->w_height - row)
9550 line_count = wp->w_height - row;
9551
9552 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9553 if (retval != MAYBE)
9554 return retval;
9555
9556 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9557 (int)Rows, FALSE, NULL) == FAIL)
9558 return FAIL;
9559
9560#ifdef FEAT_WINDOWS
9561 /*
9562 * If there are windows or status lines below, try to put them at the
9563 * correct place. If we can't do that, they have to be redrawn.
9564 */
9565 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9566 {
9567 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9568 line_count, (int)Rows, NULL) == FAIL)
9569 {
9570 wp->w_redr_status = TRUE;
9571 win_rest_invalid(wp->w_next);
9572 }
9573 }
9574 /*
9575 * If this is the last window and there is no status line, redraw the
9576 * command line later.
9577 */
9578 else
9579#endif
9580 redraw_cmdline = TRUE;
9581 return OK;
9582}
9583
9584/*
9585 * Common code for win_ins_lines() and win_del_lines().
9586 * Returns OK or FAIL when the work has been done.
9587 * Returns MAYBE when not finished yet.
9588 */
9589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009590win_do_lines(
9591 win_T *wp,
9592 int row,
9593 int line_count,
9594 int mayclear,
9595 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 int retval;
9598
9599 if (!redrawing() || line_count <= 0)
9600 return FAIL;
9601
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009602 /* When inserting lines would result in loss of command output, just redraw
9603 * the lines. */
9604 if (no_win_do_lines_ins && !del)
9605 return FAIL;
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 /* only a few lines left: redraw is faster */
9608 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009609#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 && wp->w_width == Columns
9611#endif
9612 )
9613 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009614 if (!no_win_do_lines_ins)
9615 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 return FAIL;
9617 }
9618
9619 /*
9620 * Delete all remaining lines
9621 */
9622 if (row + line_count >= wp->w_height)
9623 {
9624 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9625 W_WINCOL(wp), (int)W_ENDCOL(wp),
9626 ' ', ' ', 0);
9627 return OK;
9628 }
9629
9630 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009631 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009633 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009635 if (!no_win_do_lines_ins)
9636 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637
9638 /*
9639 * If the terminal can set a scroll region, use that.
9640 * Always do this in a vertically split window. This will redraw from
9641 * ScreenLines[] when t_CV isn't defined. That's faster than using
9642 * win_line().
9643 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009644 * a character in the lower right corner of the scroll region may cause a
9645 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 */
9647 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009648#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 || W_WIDTH(wp) != Columns
9650#endif
9651 )
9652 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009653#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9655#endif
9656 scroll_region_set(wp, row);
9657 if (del)
9658 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9659 wp->w_height - row, FALSE, wp);
9660 else
9661 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9662 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009663#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9665#endif
9666 scroll_region_reset();
9667 return retval;
9668 }
9669
9670#ifdef FEAT_WINDOWS
9671 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9672 return FAIL;
9673#endif
9674
9675 return MAYBE;
9676}
9677
9678/*
9679 * window 'wp' and everything after it is messed up, mark it for redraw
9680 */
9681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009682win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684#ifdef FEAT_WINDOWS
9685 while (wp != NULL)
9686#else
9687 if (wp != NULL)
9688#endif
9689 {
9690 redraw_win_later(wp, NOT_VALID);
9691#ifdef FEAT_WINDOWS
9692 wp->w_redr_status = TRUE;
9693 wp = wp->w_next;
9694#endif
9695 }
9696 redraw_cmdline = TRUE;
9697}
9698
9699/*
9700 * The rest of the routines in this file perform screen manipulations. The
9701 * given operation is performed physically on the screen. The corresponding
9702 * change is also made to the internal screen image. In this way, the editor
9703 * anticipates the effect of editing changes on the appearance of the screen.
9704 * That way, when we call screenupdate a complete redraw isn't usually
9705 * necessary. Another advantage is that we can keep adding code to anticipate
9706 * screen changes, and in the meantime, everything still works.
9707 */
9708
9709/*
9710 * types for inserting or deleting lines
9711 */
9712#define USE_T_CAL 1
9713#define USE_T_CDL 2
9714#define USE_T_AL 3
9715#define USE_T_CE 4
9716#define USE_T_DL 5
9717#define USE_T_SR 6
9718#define USE_NL 7
9719#define USE_T_CD 8
9720#define USE_REDRAW 9
9721
9722/*
9723 * insert lines on the screen and update ScreenLines[]
9724 * 'end' is the line after the scrolled part. Normally it is Rows.
9725 * When scrolling region used 'off' is the offset from the top for the region.
9726 * 'row' and 'end' are relative to the start of the region.
9727 *
9728 * return FAIL for failure, OK for success.
9729 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009730 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009731screen_ins_lines(
9732 int off,
9733 int row,
9734 int line_count,
9735 int end,
9736 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738 int i;
9739 int j;
9740 unsigned temp;
9741 int cursor_row;
9742 int type;
9743 int result_empty;
9744 int can_ce = can_clear(T_CE);
9745
9746 /*
9747 * FAIL if
9748 * - there is no valid screen
9749 * - the screen has to be redrawn completely
9750 * - the line count is less than one
9751 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009752 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009754 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9755#ifdef FEAT_CLIPBOARD
9756 || (clip_star.state != SELECT_CLEARED
9757 && redrawing_for_callback > 0)
9758#endif
9759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 return FAIL;
9761
9762 /*
9763 * There are seven ways to insert lines:
9764 * 0. When in a vertically split window and t_CV isn't set, redraw the
9765 * characters from ScreenLines[].
9766 * 1. Use T_CD (clear to end of display) if it exists and the result of
9767 * the insert is just empty lines
9768 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9769 * present or line_count > 1. It looks better if we do all the inserts
9770 * at once.
9771 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9772 * insert is just empty lines and T_CE is not present or line_count >
9773 * 1.
9774 * 4. Use T_AL (insert line) if it exists.
9775 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9776 * just empty lines.
9777 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9778 * just empty lines.
9779 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9780 * the 'da' flag is not set or we have clear line capability.
9781 * 8. redraw the characters from ScreenLines[].
9782 *
9783 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9784 * the scrollbar for the window. It does have insert line, use that if it
9785 * exists.
9786 */
9787 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009788#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9790 type = USE_REDRAW;
9791 else
9792#endif
9793 if (can_clear(T_CD) && result_empty)
9794 type = USE_T_CD;
9795 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9796 type = USE_T_CAL;
9797 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9798 type = USE_T_CDL;
9799 else if (*T_AL != NUL)
9800 type = USE_T_AL;
9801 else if (can_ce && result_empty)
9802 type = USE_T_CE;
9803 else if (*T_DL != NUL && result_empty)
9804 type = USE_T_DL;
9805 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9806 type = USE_T_SR;
9807 else
9808 return FAIL;
9809
9810 /*
9811 * For clearing the lines screen_del_lines() is used. This will also take
9812 * care of t_db if necessary.
9813 */
9814 if (type == USE_T_CD || type == USE_T_CDL ||
9815 type == USE_T_CE || type == USE_T_DL)
9816 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9817
9818 /*
9819 * If text is retained below the screen, first clear or delete as many
9820 * lines at the bottom of the window as are about to be inserted so that
9821 * the deleted lines won't later surface during a screen_del_lines.
9822 */
9823 if (*T_DB)
9824 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9825
9826#ifdef FEAT_CLIPBOARD
9827 /* Remove a modeless selection when inserting lines halfway the screen
9828 * or not the full width of the screen. */
9829 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009830# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 || (wp != NULL && wp->w_width != Columns)
9832# endif
9833 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009834 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 else
9836 clip_scroll_selection(-line_count);
9837#endif
9838
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839#ifdef FEAT_GUI
9840 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9841 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009842 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843#endif
9844
9845 if (*T_CCS != NUL) /* cursor relative to region */
9846 cursor_row = row;
9847 else
9848 cursor_row = row + off;
9849
9850 /*
9851 * Shift LineOffset[] line_count down to reflect the inserted lines.
9852 * Clear the inserted lines in ScreenLines[].
9853 */
9854 row += off;
9855 end += off;
9856 for (i = 0; i < line_count; ++i)
9857 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009858#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 if (wp != NULL && wp->w_width != Columns)
9860 {
9861 /* need to copy part of a line */
9862 j = end - 1 - i;
9863 while ((j -= line_count) >= row)
9864 linecopy(j + line_count, j, wp);
9865 j += line_count;
9866 if (can_clear((char_u *)" "))
9867 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9868 else
9869 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9870 LineWraps[j] = FALSE;
9871 }
9872 else
9873#endif
9874 {
9875 j = end - 1 - i;
9876 temp = LineOffset[j];
9877 while ((j -= line_count) >= row)
9878 {
9879 LineOffset[j + line_count] = LineOffset[j];
9880 LineWraps[j + line_count] = LineWraps[j];
9881 }
9882 LineOffset[j + line_count] = temp;
9883 LineWraps[j + line_count] = FALSE;
9884 if (can_clear((char_u *)" "))
9885 lineclear(temp, (int)Columns);
9886 else
9887 lineinvalid(temp, (int)Columns);
9888 }
9889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 screen_stop_highlight();
9892 windgoto(cursor_row, 0);
9893
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009894#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 /* redraw the characters */
9896 if (type == USE_REDRAW)
9897 redraw_block(row, end, wp);
9898 else
9899#endif
9900 if (type == USE_T_CAL)
9901 {
9902 term_append_lines(line_count);
9903 screen_start(); /* don't know where cursor is now */
9904 }
9905 else
9906 {
9907 for (i = 0; i < line_count; i++)
9908 {
9909 if (type == USE_T_AL)
9910 {
9911 if (i && cursor_row != 0)
9912 windgoto(cursor_row, 0);
9913 out_str(T_AL);
9914 }
9915 else /* type == USE_T_SR */
9916 out_str(T_SR);
9917 screen_start(); /* don't know where cursor is now */
9918 }
9919 }
9920
9921 /*
9922 * With scroll-reverse and 'da' flag set we need to clear the lines that
9923 * have been scrolled down into the region.
9924 */
9925 if (type == USE_T_SR && *T_DA)
9926 {
9927 for (i = 0; i < line_count; ++i)
9928 {
9929 windgoto(off + i, 0);
9930 out_str(T_CE);
9931 screen_start(); /* don't know where cursor is now */
9932 }
9933 }
9934
9935#ifdef FEAT_GUI
9936 gui_can_update_cursor();
9937 if (gui.in_use)
9938 out_flush(); /* always flush after a scroll */
9939#endif
9940 return OK;
9941}
9942
9943/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009944 * Delete lines on the screen and update ScreenLines[].
9945 * "end" is the line after the scrolled part. Normally it is Rows.
9946 * When scrolling region used "off" is the offset from the top for the region.
9947 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 *
9949 * Return OK for success, FAIL if the lines are not deleted.
9950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009952screen_del_lines(
9953 int off,
9954 int row,
9955 int line_count,
9956 int end,
9957 int force, /* even when line_count > p_ttyscroll */
9958 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960 int j;
9961 int i;
9962 unsigned temp;
9963 int cursor_row;
9964 int cursor_end;
9965 int result_empty; /* result is empty until end of region */
9966 int can_delete; /* deleting line codes can be used */
9967 int type;
9968
9969 /*
9970 * FAIL if
9971 * - there is no valid screen
9972 * - the screen has to be redrawn completely
9973 * - the line count is less than one
9974 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009975 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009977 if (!screen_valid(TRUE) || line_count <= 0
9978 || (!force && line_count > p_ttyscroll)
9979#ifdef FEAT_CLIPBOARD
9980 || (clip_star.state != SELECT_CLEARED
9981 && redrawing_for_callback > 0)
9982#endif
9983 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 return FAIL;
9985
9986 /*
9987 * Check if the rest of the current region will become empty.
9988 */
9989 result_empty = row + line_count >= end;
9990
9991 /*
9992 * We can delete lines only when 'db' flag not set or when 'ce' option
9993 * available.
9994 */
9995 can_delete = (*T_DB == NUL || can_clear(T_CE));
9996
9997 /*
9998 * There are six ways to delete lines:
9999 * 0. When in a vertically split window and t_CV isn't set, redraw the
10000 * characters from ScreenLines[].
10001 * 1. Use T_CD if it exists and the result is empty.
10002 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10003 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10004 * none of the other ways work.
10005 * 4. Use T_CE (erase line) if the result is empty.
10006 * 5. Use T_DL (delete line) if it exists.
10007 * 6. redraw the characters from ScreenLines[].
10008 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010009#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10011 type = USE_REDRAW;
10012 else
10013#endif
10014 if (can_clear(T_CD) && result_empty)
10015 type = USE_T_CD;
10016#if defined(__BEOS__) && defined(BEOS_DR8)
10017 /*
10018 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10019 * its internal termcap... this works okay for tests which test *T_DB !=
10020 * NUL. It has the disadvantage that the user cannot use any :set t_*
10021 * command to get T_DB (back) to empty_option, only :set term=... will do
10022 * the trick...
10023 * Anyway, this hack will hopefully go away with the next OS release.
10024 * (Olaf Seibert)
10025 */
10026 else if (row == 0 && T_DB == empty_option
10027 && (line_count == 1 || *T_CDL == NUL))
10028#else
10029 else if (row == 0 && (
10030#ifndef AMIGA
10031 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10032 * up, so use delete-line command */
10033 line_count == 1 ||
10034#endif
10035 *T_CDL == NUL))
10036#endif
10037 type = USE_NL;
10038 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10039 type = USE_T_CDL;
10040 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010041#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 && (wp == NULL || wp->w_width == Columns)
10043#endif
10044 )
10045 type = USE_T_CE;
10046 else if (*T_DL != NUL && can_delete)
10047 type = USE_T_DL;
10048 else if (*T_CDL != NUL && can_delete)
10049 type = USE_T_CDL;
10050 else
10051 return FAIL;
10052
10053#ifdef FEAT_CLIPBOARD
10054 /* Remove a modeless selection when deleting lines halfway the screen or
10055 * not the full width of the screen. */
10056 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010057# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 || (wp != NULL && wp->w_width != Columns)
10059# endif
10060 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010061 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 else
10063 clip_scroll_selection(line_count);
10064#endif
10065
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066#ifdef FEAT_GUI
10067 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10068 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010069 gui_dont_update_cursor(gui.cursor_row >= row + off
10070 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071#endif
10072
10073 if (*T_CCS != NUL) /* cursor relative to region */
10074 {
10075 cursor_row = row;
10076 cursor_end = end;
10077 }
10078 else
10079 {
10080 cursor_row = row + off;
10081 cursor_end = end + off;
10082 }
10083
10084 /*
10085 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10086 * Clear the inserted lines in ScreenLines[].
10087 */
10088 row += off;
10089 end += off;
10090 for (i = 0; i < line_count; ++i)
10091 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010092#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (wp != NULL && wp->w_width != Columns)
10094 {
10095 /* need to copy part of a line */
10096 j = row + i;
10097 while ((j += line_count) <= end - 1)
10098 linecopy(j - line_count, j, wp);
10099 j -= line_count;
10100 if (can_clear((char_u *)" "))
10101 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10102 else
10103 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10104 LineWraps[j] = FALSE;
10105 }
10106 else
10107#endif
10108 {
10109 /* whole width, moving the line pointers is faster */
10110 j = row + i;
10111 temp = LineOffset[j];
10112 while ((j += line_count) <= end - 1)
10113 {
10114 LineOffset[j - line_count] = LineOffset[j];
10115 LineWraps[j - line_count] = LineWraps[j];
10116 }
10117 LineOffset[j - line_count] = temp;
10118 LineWraps[j - line_count] = FALSE;
10119 if (can_clear((char_u *)" "))
10120 lineclear(temp, (int)Columns);
10121 else
10122 lineinvalid(temp, (int)Columns);
10123 }
10124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
10126 screen_stop_highlight();
10127
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010128#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 /* redraw the characters */
10130 if (type == USE_REDRAW)
10131 redraw_block(row, end, wp);
10132 else
10133#endif
10134 if (type == USE_T_CD) /* delete the lines */
10135 {
10136 windgoto(cursor_row, 0);
10137 out_str(T_CD);
10138 screen_start(); /* don't know where cursor is now */
10139 }
10140 else if (type == USE_T_CDL)
10141 {
10142 windgoto(cursor_row, 0);
10143 term_delete_lines(line_count);
10144 screen_start(); /* don't know where cursor is now */
10145 }
10146 /*
10147 * Deleting lines at top of the screen or scroll region: Just scroll
10148 * the whole screen (scroll region) up by outputting newlines on the
10149 * last line.
10150 */
10151 else if (type == USE_NL)
10152 {
10153 windgoto(cursor_end - 1, 0);
10154 for (i = line_count; --i >= 0; )
10155 out_char('\n'); /* cursor will remain on same line */
10156 }
10157 else
10158 {
10159 for (i = line_count; --i >= 0; )
10160 {
10161 if (type == USE_T_DL)
10162 {
10163 windgoto(cursor_row, 0);
10164 out_str(T_DL); /* delete a line */
10165 }
10166 else /* type == USE_T_CE */
10167 {
10168 windgoto(cursor_row + i, 0);
10169 out_str(T_CE); /* erase a line */
10170 }
10171 screen_start(); /* don't know where cursor is now */
10172 }
10173 }
10174
10175 /*
10176 * If the 'db' flag is set, we need to clear the lines that have been
10177 * scrolled up at the bottom of the region.
10178 */
10179 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10180 {
10181 for (i = line_count; i > 0; --i)
10182 {
10183 windgoto(cursor_end - i, 0);
10184 out_str(T_CE); /* erase a line */
10185 screen_start(); /* don't know where cursor is now */
10186 }
10187 }
10188
10189#ifdef FEAT_GUI
10190 gui_can_update_cursor();
10191 if (gui.in_use)
10192 out_flush(); /* always flush after a scroll */
10193#endif
10194
10195 return OK;
10196}
10197
10198/*
10199 * show the current mode and ruler
10200 *
10201 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10202 * If clear_cmdline is FALSE there may be a message there that needs to be
10203 * cleared only if a mode is shown.
10204 * Return the length of the message (0 if no message).
10205 */
10206 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010207showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208{
10209 int need_clear;
10210 int length = 0;
10211 int do_mode;
10212 int attr;
10213 int nwr_save;
10214#ifdef FEAT_INS_EXPAND
10215 int sub_attr;
10216#endif
10217
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010218 do_mode = ((p_smd && msg_silent == 0)
10219 && ((State & INSERT)
10220 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010221 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (do_mode || Recording)
10223 {
10224 /*
10225 * Don't show mode right now, when not redrawing or inside a mapping.
10226 * Call char_avail() only when we are going to show something, because
10227 * it takes a bit of time.
10228 */
10229 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10230 {
10231 redraw_cmdline = TRUE; /* show mode later */
10232 return 0;
10233 }
10234
10235 nwr_save = need_wait_return;
10236
10237 /* wait a bit before overwriting an important message */
10238 check_for_delay(FALSE);
10239
10240 /* if the cmdline is more than one line high, erase top lines */
10241 need_clear = clear_cmdline;
10242 if (clear_cmdline && cmdline_row < Rows - 1)
10243 msg_clr_cmdline(); /* will reset clear_cmdline */
10244
10245 /* Position on the last line in the window, column 0 */
10246 msg_pos_mode();
10247 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010248 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 if (do_mode)
10250 {
10251 MSG_PUTS_ATTR("--", attr);
10252#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010253 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010254# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010255 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010256# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010257 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010258# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010259 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010260# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 MSG_PUTS_ATTR(" IM", attr);
10262# else
10263 MSG_PUTS_ATTR(" XIM", attr);
10264# endif
10265#endif
10266#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10267 if (gui.in_use)
10268 {
10269 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010270 {
10271 /* HANGUL */
10272 if (enc_utf8)
10273 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10274 else
10275 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 }
10278#endif
10279#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010280 /* CTRL-X in Insert mode */
10281 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 {
10283 /* These messages can get long, avoid a wrap in a narrow
10284 * window. Prefer showing edit_submode_extra. */
10285 length = (Rows - msg_row) * Columns - 3;
10286 if (edit_submode_extra != NULL)
10287 length -= vim_strsize(edit_submode_extra);
10288 if (length > 0)
10289 {
10290 if (edit_submode_pre != NULL)
10291 length -= vim_strsize(edit_submode_pre);
10292 if (length - vim_strsize(edit_submode) > 0)
10293 {
10294 if (edit_submode_pre != NULL)
10295 msg_puts_attr(edit_submode_pre, attr);
10296 msg_puts_attr(edit_submode, attr);
10297 }
10298 if (edit_submode_extra != NULL)
10299 {
10300 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10301 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010302 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 else
10304 sub_attr = attr;
10305 msg_puts_attr(edit_submode_extra, sub_attr);
10306 }
10307 }
10308 length = 0;
10309 }
10310 else
10311#endif
10312 {
10313#ifdef FEAT_VREPLACE
10314 if (State & VREPLACE_FLAG)
10315 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10316 else
10317#endif
10318 if (State & REPLACE_FLAG)
10319 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10320 else if (State & INSERT)
10321 {
10322#ifdef FEAT_RIGHTLEFT
10323 if (p_ri)
10324 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10325#endif
10326 MSG_PUTS_ATTR(_(" INSERT"), attr);
10327 }
10328 else if (restart_edit == 'I')
10329 MSG_PUTS_ATTR(_(" (insert)"), attr);
10330 else if (restart_edit == 'R')
10331 MSG_PUTS_ATTR(_(" (replace)"), attr);
10332 else if (restart_edit == 'V')
10333 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10334#ifdef FEAT_RIGHTLEFT
10335 if (p_hkmap)
10336 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10337# ifdef FEAT_FKMAP
10338 if (p_fkmap)
10339 MSG_PUTS_ATTR(farsi_text_5, attr);
10340# endif
10341#endif
10342#ifdef FEAT_KEYMAP
10343 if (State & LANGMAP)
10344 {
10345# ifdef FEAT_ARABIC
10346 if (curwin->w_p_arab)
10347 MSG_PUTS_ATTR(_(" Arabic"), attr);
10348 else
10349# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010350 if (get_keymap_str(curwin, (char_u *)" (%s)",
10351 NameBuff, MAXPATHL))
10352 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354#endif
10355 if ((State & INSERT) && p_paste)
10356 MSG_PUTS_ATTR(_(" (paste)"), attr);
10357
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 if (VIsual_active)
10359 {
10360 char *p;
10361
10362 /* Don't concatenate separate words to avoid translation
10363 * problems. */
10364 switch ((VIsual_select ? 4 : 0)
10365 + (VIsual_mode == Ctrl_V) * 2
10366 + (VIsual_mode == 'V'))
10367 {
10368 case 0: p = N_(" VISUAL"); break;
10369 case 1: p = N_(" VISUAL LINE"); break;
10370 case 2: p = N_(" VISUAL BLOCK"); break;
10371 case 4: p = N_(" SELECT"); break;
10372 case 5: p = N_(" SELECT LINE"); break;
10373 default: p = N_(" SELECT BLOCK"); break;
10374 }
10375 MSG_PUTS_ATTR(_(p), attr);
10376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 MSG_PUTS_ATTR(" --", attr);
10378 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010379
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 need_clear = TRUE;
10381 }
10382 if (Recording
10383#ifdef FEAT_INS_EXPAND
10384 && edit_submode == NULL /* otherwise it gets too long */
10385#endif
10386 )
10387 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010388 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 need_clear = TRUE;
10390 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010391
10392 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 if (need_clear || clear_cmdline)
10394 msg_clr_eos();
10395 msg_didout = FALSE; /* overwrite this message */
10396 length = msg_col;
10397 msg_col = 0;
10398 need_wait_return = nwr_save; /* never ask for hit-return for this */
10399 }
10400 else if (clear_cmdline && msg_silent == 0)
10401 /* Clear the whole command line. Will reset "clear_cmdline". */
10402 msg_clr_cmdline();
10403
10404#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 /* In Visual mode the size of the selected area must be redrawn. */
10406 if (VIsual_active)
10407 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408
10409 /* If the last window has no status line, the ruler is after the mode
10410 * message and must be redrawn */
10411 if (redrawing()
10412# ifdef FEAT_WINDOWS
10413 && lastwin->w_status_height == 0
10414# endif
10415 )
10416 win_redr_ruler(lastwin, TRUE);
10417#endif
10418 redraw_cmdline = FALSE;
10419 clear_cmdline = FALSE;
10420
10421 return length;
10422}
10423
10424/*
10425 * Position for a mode message.
10426 */
10427 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010428msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
10430 msg_col = 0;
10431 msg_row = Rows - 1;
10432}
10433
10434/*
10435 * Delete mode message. Used when ESC is typed which is expected to end
10436 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010437 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 */
10439 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010440unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010443 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 */
10445 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10446 redraw_cmdline = TRUE; /* delete mode later */
10447 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010448 clearmode();
10449}
10450
10451/*
10452 * Clear the mode message.
10453 */
10454 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010455clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010456{
10457 msg_pos_mode();
10458 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010459 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010460 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461}
10462
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010463 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010464recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010465{
10466 MSG_PUTS_ATTR(_("recording"), attr);
10467 if (!shortmess(SHM_RECORDING))
10468 {
10469 char_u s[4];
10470 sprintf((char *)s, " @%c", Recording);
10471 MSG_PUTS_ATTR(s, attr);
10472 }
10473}
10474
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010475#if defined(FEAT_WINDOWS)
10476/*
10477 * Draw the tab pages line at the top of the Vim window.
10478 */
10479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010480draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010481{
10482 int tabcount = 0;
10483 tabpage_T *tp;
10484 int tabwidth;
10485 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010486 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010487 int attr;
10488 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010489 win_T *cwp;
10490 int wincount;
10491 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010492 int c;
10493 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010494 int attr_sel = HL_ATTR(HLF_TPS);
10495 int attr_nosel = HL_ATTR(HLF_TP);
10496 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010497 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010498 int room;
10499 int use_sep_chars = (t_colors < 8
10500#ifdef FEAT_GUI
10501 && !gui.in_use
10502#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010503#ifdef FEAT_TERMGUICOLORS
10504 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010505#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010506 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010507
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010508 if (ScreenLines == NULL)
10509 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010510 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010511
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010512#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010513 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010514 if (gui_use_tabline())
10515 {
10516 gui_update_tabline();
10517 return;
10518 }
10519#endif
10520
10521 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010522 return;
10523
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010524#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010525
10526 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10527 for (scol = 0; scol < Columns; ++scol)
10528 TabPageIdxs[scol] = 0;
10529
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010530 /* Use the 'tabline' option if it's set. */
10531 if (*p_tal != NUL)
10532 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010533 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010534
10535 /* Check for an error. If there is one we would loop in redrawing the
10536 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010537 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010538 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010539 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010540 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010541 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010542 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010543 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010544 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010545#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010546 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010547 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010548 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010549
Bram Moolenaar238a5642006-02-21 22:12:05 +000010550 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10551 if (tabwidth < 6)
10552 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010553
Bram Moolenaar238a5642006-02-21 22:12:05 +000010554 attr = attr_nosel;
10555 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010556 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010557 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10558 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010559 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010560 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010561
Bram Moolenaar238a5642006-02-21 22:12:05 +000010562 if (tp->tp_topframe == topframe)
10563 attr = attr_sel;
10564 if (use_sep_chars && col > 0)
10565 screen_putchar('|', 0, col++, attr);
10566
10567 if (tp->tp_topframe != topframe)
10568 attr = attr_nosel;
10569
10570 screen_putchar(' ', 0, col++, attr);
10571
10572 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010573 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010574 cwp = curwin;
10575 wp = firstwin;
10576 }
10577 else
10578 {
10579 cwp = tp->tp_curwin;
10580 wp = tp->tp_firstwin;
10581 }
10582
10583 modified = FALSE;
10584 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10585 if (bufIsChanged(wp->w_buffer))
10586 modified = TRUE;
10587 if (modified || wincount > 1)
10588 {
10589 if (wincount > 1)
10590 {
10591 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010593 if (col + len >= Columns - 3)
10594 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010596#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010597 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010598#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010599 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010600#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601 );
10602 col += len;
10603 }
10604 if (modified)
10605 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10606 screen_putchar(' ', 0, col++, attr);
10607 }
10608
10609 room = scol - col + tabwidth - 1;
10610 if (room > 0)
10611 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010612 /* Get buffer name in NameBuff[] */
10613 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010614 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010615 len = vim_strsize(NameBuff);
10616 p = NameBuff;
10617#ifdef FEAT_MBYTE
10618 if (has_mbyte)
10619 while (len > room)
10620 {
10621 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010622 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 }
10624 else
10625#endif
10626 if (len > room)
10627 {
10628 p += len - room;
10629 len = room;
10630 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010631 if (len > Columns - col - 1)
10632 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010634 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010635 col += len;
10636 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010637 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010638
10639 /* Store the tab page number in TabPageIdxs[], so that
10640 * jump_to_mouse() knows where each one is. */
10641 ++tabcount;
10642 while (scol < col)
10643 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010644 }
10645
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 if (use_sep_chars)
10647 c = '_';
10648 else
10649 c = ' ';
10650 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010651
10652 /* Put an "X" for closing the current tab if there are several. */
10653 if (first_tabpage->tp_next != NULL)
10654 {
10655 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10656 TabPageIdxs[Columns - 1] = -999;
10657 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010658 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010659
10660 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10661 * set. */
10662 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010663}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010664
10665/*
10666 * Get buffer name for "buf" into NameBuff[].
10667 * Takes care of special buffer names and translates special characters.
10668 */
10669 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010670get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010671{
10672 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010673 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010674 else
10675 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10676 trans_characters(NameBuff, MAXPATHL);
10677}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010678#endif
10679
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10681/*
10682 * Get the character to use in a status line. Get its attributes in "*attr".
10683 */
10684 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010685fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686{
10687 int fill;
10688 if (is_curwin)
10689 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010690 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 fill = fill_stl;
10692 }
10693 else
10694 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010695 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 fill = fill_stlnc;
10697 }
10698 /* Use fill when there is highlighting, and highlighting of current
10699 * window differs, or the fillchars differ, or this is not the
10700 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010701 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaara1f4cb92016-11-06 15:25:42 +010010702 || !is_curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 || (fill_stl != fill_stlnc)))
10704 return fill;
10705 if (is_curwin)
10706 return '^';
10707 return '=';
10708}
10709#endif
10710
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010711#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712/*
10713 * Get the character to use in a separator between vertically split windows.
10714 * Get its attributes in "*attr".
10715 */
10716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010717fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010719 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 if (*attr == 0 && fill_vert == ' ')
10721 return '|';
10722 else
10723 return fill_vert;
10724}
10725#endif
10726
10727/*
10728 * Return TRUE if redrawing should currently be done.
10729 */
10730 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010731redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010733#ifdef FEAT_EVAL
10734 if (disable_redraw_for_testing)
10735 return 0;
10736 else
10737#endif
10738 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10740}
10741
10742/*
10743 * Return TRUE if printing messages should currently be done.
10744 */
10745 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010746messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747{
10748 return (!(p_lz && char_avail() && !KeyTyped));
10749}
10750
10751/*
10752 * Show current status info in ruler and various other places
10753 * If always is FALSE, only show ruler if position has changed.
10754 */
10755 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010756showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758 if (!always && !redrawing())
10759 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010760#ifdef FEAT_INS_EXPAND
10761 if (pum_visible())
10762 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010763# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010764 /* Don't redraw right now, do it later. */
10765 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010766# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010767 return;
10768 }
10769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010771 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010772 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010773 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 else
10776#endif
10777#ifdef FEAT_CMDL_INFO
10778 win_redr_ruler(curwin, always);
10779#endif
10780
10781#ifdef FEAT_TITLE
10782 if (need_maketitle
10783# ifdef FEAT_STL_OPT
10784 || (p_icon && (stl_syntax & STL_IN_ICON))
10785 || (p_title && (stl_syntax & STL_IN_TITLE))
10786# endif
10787 )
10788 maketitle();
10789#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010790#ifdef FEAT_WINDOWS
10791 /* Redraw the tab pages line if needed. */
10792 if (redraw_tabline)
10793 draw_tabline();
10794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795}
10796
10797#ifdef FEAT_CMDL_INFO
10798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010799win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010801#define RULER_BUF_LEN 70
10802 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 int row;
10804 int fillchar;
10805 int attr;
10806 int empty_line = FALSE;
10807 colnr_T virtcol;
10808 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010809 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010811#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 int this_ru_col;
10813 int off = 0;
10814 int width = Columns;
10815# define WITH_OFF(x) x
10816# define WITH_WIDTH(x) x
10817#else
10818# define WITH_OFF(x) 0
10819# define WITH_WIDTH(x) Columns
10820# define this_ru_col ru_col
10821#endif
10822
10823 /* If 'ruler' off or redrawing disabled, don't do anything */
10824 if (!p_ru)
10825 return;
10826
10827 /*
10828 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10829 * after deleting lines, before cursor.lnum is corrected.
10830 */
10831 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10832 return;
10833
10834#ifdef FEAT_INS_EXPAND
10835 /* Don't draw the ruler while doing insert-completion, it might overwrite
10836 * the (long) mode message. */
10837# ifdef FEAT_WINDOWS
10838 if (wp == lastwin && lastwin->w_status_height == 0)
10839# endif
10840 if (edit_submode != NULL)
10841 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010842 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10843 if (pum_visible())
10844 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#endif
10846
10847#ifdef FEAT_STL_OPT
10848 if (*p_ruf)
10849 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010850 int save_called_emsg = called_emsg;
10851
10852 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010854 if (called_emsg)
10855 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010856 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010857 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 return;
10859 }
10860#endif
10861
10862 /*
10863 * Check if not in Insert mode and the line is empty (will show "0-1").
10864 */
10865 if (!(State & INSERT)
10866 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10867 empty_line = TRUE;
10868
10869 /*
10870 * Only draw the ruler when something changed.
10871 */
10872 validate_virtcol_win(wp);
10873 if ( redraw_cmdline
10874 || always
10875 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10876 || wp->w_cursor.col != wp->w_ru_cursor.col
10877 || wp->w_virtcol != wp->w_ru_virtcol
10878#ifdef FEAT_VIRTUALEDIT
10879 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10880#endif
10881 || wp->w_topline != wp->w_ru_topline
10882 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10883#ifdef FEAT_DIFF
10884 || wp->w_topfill != wp->w_ru_topfill
10885#endif
10886 || empty_line != wp->w_ru_empty)
10887 {
10888 cursor_off();
10889#ifdef FEAT_WINDOWS
10890 if (wp->w_status_height)
10891 {
10892 row = W_WINROW(wp) + wp->w_height;
10893 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 off = W_WINCOL(wp);
10895 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 }
10897 else
10898#endif
10899 {
10900 row = Rows - 1;
10901 fillchar = ' ';
10902 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010903#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 width = Columns;
10905 off = 0;
10906#endif
10907 }
10908
10909 /* In list mode virtcol needs to be recomputed */
10910 virtcol = wp->w_virtcol;
10911 if (wp->w_p_list && lcs_tab1 == NUL)
10912 {
10913 wp->w_p_list = FALSE;
10914 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10915 wp->w_p_list = TRUE;
10916 }
10917
10918 /*
10919 * Some sprintfs return the length, some return a pointer.
10920 * To avoid portability problems we use strlen() here.
10921 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010922 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10924 ? 0L
10925 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010926 len = STRLEN(buffer);
10927 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10929 (int)virtcol + 1);
10930
10931 /*
10932 * Add a "50%" if there is room for it.
10933 * On the last line, don't print in the last column (scrolls the
10934 * screen up on some terminals).
10935 */
10936 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010937 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 o = i + vim_strsize(buffer + i + 1);
10939#ifdef FEAT_WINDOWS
10940 if (wp->w_status_height == 0) /* can't use last char of screen */
10941#endif
10942 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010943#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 this_ru_col = ru_col - (Columns - width);
10945 if (this_ru_col < 0)
10946 this_ru_col = 0;
10947#endif
10948 /* Never use more than half the window/screen width, leave the other
10949 * half for the filename. */
10950 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10951 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10952 if (this_ru_col + o < WITH_WIDTH(width))
10953 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010954 /* need at least 3 chars left for get_rel_pos() + NUL */
10955 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 {
10957#ifdef FEAT_MBYTE
10958 if (has_mbyte)
10959 i += (*mb_char2bytes)(fillchar, buffer + i);
10960 else
10961#endif
10962 buffer[i++] = fillchar;
10963 ++o;
10964 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010965 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 }
10967 /* Truncate at window boundary. */
10968#ifdef FEAT_MBYTE
10969 if (has_mbyte)
10970 {
10971 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010972 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 {
10974 o += (*mb_ptr2cells)(buffer + i);
10975 if (this_ru_col + o > WITH_WIDTH(width))
10976 {
10977 buffer[i] = NUL;
10978 break;
10979 }
10980 }
10981 }
10982 else
10983#endif
10984 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10985 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10986
10987 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10988 i = redraw_cmdline;
10989 screen_fill(row, row + 1,
10990 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10991 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10992 fillchar, fillchar, attr);
10993 /* don't redraw the cmdline because of showing the ruler */
10994 redraw_cmdline = i;
10995 wp->w_ru_cursor = wp->w_cursor;
10996 wp->w_ru_virtcol = wp->w_virtcol;
10997 wp->w_ru_empty = empty_line;
10998 wp->w_ru_topline = wp->w_topline;
10999 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11000#ifdef FEAT_DIFF
11001 wp->w_ru_topfill = wp->w_topfill;
11002#endif
11003 }
11004}
11005#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011006
11007#if defined(FEAT_LINEBREAK) || defined(PROTO)
11008/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011009 * Return the width of the 'number' and 'relativenumber' column.
11010 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011011 * Otherwise it depends on 'numberwidth' and the line count.
11012 */
11013 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011014number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011015{
11016 int n;
11017 linenr_T lnum;
11018
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011019 if (wp->w_p_rnu && !wp->w_p_nu)
11020 /* cursor line shows "0" */
11021 lnum = wp->w_height;
11022 else
11023 /* cursor line shows absolute line number */
11024 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011025
Bram Moolenaar6b314672015-03-20 15:42:10 +010011026 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011027 return wp->w_nrwidth_width;
11028 wp->w_nrwidth_line_count = lnum;
11029
11030 n = 0;
11031 do
11032 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011033 lnum /= 10;
11034 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011035 } while (lnum > 0);
11036
11037 /* 'numberwidth' gives the minimal width plus one */
11038 if (n < wp->w_p_nuw - 1)
11039 n = wp->w_p_nuw - 1;
11040
11041 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011042 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011043 return n;
11044}
11045#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011046
11047/*
11048 * Return the current cursor column. This is the actual position on the
11049 * screen. First column is 0.
11050 */
11051 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011052screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011053{
11054 return screen_cur_col;
11055}
11056
11057/*
11058 * Return the current cursor row. This is the actual position on the screen.
11059 * First row is 0.
11060 */
11061 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011062screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011063{
11064 return screen_cur_row;
11065}