blob: ea07d02d422e3371bc77ac14e07ab1f38275c5cb [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 Moolenaar3633cf52017-07-31 22:29:35 +0200168static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100170#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200185#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
186/* Can limit syntax highlight time to 'redrawtime'. */
187# define SYN_TIME_LIMIT 1
188#endif
189
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200190#ifdef FEAT_RIGHTLEFT
191# define HAS_RIGHTLEFT(x) x
192#else
193# define HAS_RIGHTLEFT(x) FALSE
194#endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Redraw the current window later, with update_screen(type).
198 * Set must_redraw only if not already set to a higher value.
199 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
200 */
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 redraw_win_later(curwin, type);
205}
206
207 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100208redraw_win_later(
209 win_T *wp,
210 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 if (wp->w_redr_type < type)
213 {
214 wp->w_redr_type = type;
215 if (type >= NOT_VALID)
216 wp->w_lines_valid = 0;
217 if (must_redraw < type) /* must_redraw is the maximum of all windows */
218 must_redraw = type;
219 }
220}
221
222/*
223 * Force a complete redraw later. Also resets the highlighting. To be used
224 * after executing a shell command that messes up the screen.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000230#ifdef FEAT_GUI
231 if (gui.in_use)
232 /* Use a code that will reset gui.highlight_mask in
233 * gui_stop_highlight(). */
234 screen_attr = HL_ALL + 1;
235 else
236#endif
237 /* Use attributes that is very unlikely to appear in text. */
238 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
242 * Mark all windows to be redrawn later.
243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 redraw_win_later(wp, type);
252 }
253}
254
255/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000256 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 redraw_buf_later(curbuf, type);
262}
263
264 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100265redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276 void
277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200292#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200293 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 }
296 }
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#ifdef FEAT_MBYTE
314 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200317 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318#endif
319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
332#ifdef FEAT_MBYTE
333 if (enc_utf8)
334 {
335 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenlineUC == NULL)
338 ret = 2;
339 for (i = 0; i < p_mco; ++i)
340 {
341 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
349 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
354#endif
355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367#ifdef FEAT_MBYTE
368 if (enc_utf8)
369 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineC[i] + r * cols,
375 ScreenLinesC[i] + LineOffset[cmdline_row + r],
376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382#endif
383 }
384
385 update_screen(0);
386 ret = 3;
387
388 if (must_redraw == 0)
389 {
390 int off = (int)(current_ScreenLine - ScreenLines);
391
392 /* Restore the text displayed in the command line area. */
393 for (r = 0; r < rows; ++r)
394 {
395 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenattr + r * cols,
400 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401#ifdef FEAT_MBYTE
402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200417 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419 ret = 4;
420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 }
422
423 vim_free(screenline);
424 vim_free(screenattr);
425#ifdef FEAT_MBYTE
426 if (enc_utf8)
427 {
428 vim_free(screenlineUC);
429 for (i = 0; i < p_mco; ++i)
430 vim_free(screenlineC[i]);
431 }
432 if (enc_dbcs == DBCS_JPNU)
433 vim_free(screenline2);
434#endif
435
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200436 /* Show the intro message when appropriate. */
437 maybe_intro_message();
438
439 setcursor();
440
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441 return ret;
442}
443
444/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 * Invoked after an asynchronous callback is called.
446 * If an echo command was used the cursor needs to be put back where
447 * it belongs. If highlighting was changed a redraw is needed.
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 Moolenaar63ecdda2017-07-28 22:29:35 +02003133#ifdef FEAT_TERMINAL
3134 int get_term_attr = FALSE;
3135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 /* draw_state: items that are drawn in sequence: */
3138#define WL_START 0 /* nothing done yet */
3139#ifdef FEAT_CMDWIN
3140# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3141#else
3142# define WL_CMDLINE WL_START
3143#endif
3144#ifdef FEAT_FOLDING
3145# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3146#else
3147# define WL_FOLD WL_CMDLINE
3148#endif
3149#ifdef FEAT_SIGNS
3150# define WL_SIGN WL_FOLD + 1 /* column for signs */
3151#else
3152# define WL_SIGN WL_FOLD /* column for signs */
3153#endif
3154#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003155#ifdef FEAT_LINEBREAK
3156# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003158# define WL_BRI WL_NR
3159#endif
3160#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3161# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3162#else
3163# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#endif
3165#define WL_LINE WL_SBR + 1 /* text in the line */
3166 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003167#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int feedback_col = 0;
3169 int feedback_old_attr = -1;
3170#endif
3171
Bram Moolenaar860cae12010-06-05 23:22:07 +02003172#ifdef FEAT_CONCEAL
3173 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003174 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003175 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003176 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003177 int is_concealing = FALSE;
3178 int boguscols = 0; /* nonexistent columns added to force
3179 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003180 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003181 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003182 int match_conc = 0; /* cchar for match functions */
3183 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003184 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003185# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003186# define FIX_FOR_BOGUSCOLS \
3187 { \
3188 n_extra += vcol_off; \
3189 vcol -= vcol_off; \
3190 vcol_off = 0; \
3191 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003192 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003193 boguscols = 0; \
3194 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003195#else
3196# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 if (startrow > endrow) /* past the end already! */
3200 return startrow;
3201
3202 row = startrow;
3203 screen_row = row + W_WINROW(wp);
3204
3205 /*
3206 * To speed up the loop below, set extra_check when there is linebreak,
3207 * trailing white space and/or syntax processing to be done.
3208 */
3209#ifdef FEAT_LINEBREAK
3210 extra_check = wp->w_p_lbr;
3211#else
3212 extra_check = 0;
3213#endif
3214#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003215 if (syntax_present(wp) && !wp->w_s->b_syn_error
3216# ifdef SYN_TIME_LIMIT
3217 && !wp->w_s->b_syn_slow
3218# endif
3219 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 /* Prepare for syntax highlighting in this line. When there is an
3222 * error, stop syntax highlighting. */
3223 save_did_emsg = did_emsg;
3224 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003225 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003227 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
3229 {
3230 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003231#ifdef SYN_TIME_LIMIT
3232 if (!wp->w_s->b_syn_slow)
3233#endif
3234 {
3235 has_syntax = TRUE;
3236 extra_check = TRUE;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 }
3239 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003240
3241 /* Check for columns to display for 'colorcolumn'. */
3242 color_cols = wp->w_p_cc_cols;
3243 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003244 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003245#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003246
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003247#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003248 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003249 {
3250 extra_check = TRUE;
3251 get_term_attr = TRUE;
3252 }
3253#endif
3254
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003255#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003256 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003257 && *wp->w_s->b_p_spl != NUL
3258 && wp->w_s->b_langp.ga_len > 0
3259 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003260 {
3261 /* Prepare for spell checking. */
3262 has_spell = TRUE;
3263 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003264
3265 /* Get the start of the next line, so that words that wrap to the next
3266 * line are found too: "et<line-break>al.".
3267 * Trick: skip a few chars for C/shell/Vim comments */
3268 nextline[SPWORDLEN] = NUL;
3269 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3270 {
3271 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3272 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3273 }
3274
3275 /* When a word wrapped from the previous line the start of the current
3276 * line is valid. */
3277 if (lnum == checked_lnum)
3278 cur_checked_col = checked_col;
3279 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003280
3281 /* When there was a sentence end in the previous line may require a
3282 * word starting with capital in this line. In line 1 always check
3283 * the first word. */
3284 if (lnum != capcol_lnum)
3285 cap_col = -1;
3286 if (lnum == 1)
3287 cap_col = 0;
3288 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291
3292 /*
3293 * handle visual active in this window
3294 */
3295 fromcol = -10;
3296 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3298 {
3299 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003300 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
3302 top = &curwin->w_cursor;
3303 bot = &VIsual;
3304 }
3305 else /* Visual is before curwin->w_cursor */
3306 {
3307 top = &VIsual;
3308 bot = &curwin->w_cursor;
3309 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003310 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (VIsual_mode == Ctrl_V) /* block mode */
3312 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003313 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 fromcol = wp->w_old_cursor_fcol;
3316 tocol = wp->w_old_cursor_lcol;
3317 }
3318 }
3319 else /* non-block mode */
3320 {
3321 if (lnum > top->lnum && lnum <= bot->lnum)
3322 fromcol = 0;
3323 else if (lnum == top->lnum)
3324 {
3325 if (VIsual_mode == 'V') /* linewise */
3326 fromcol = 0;
3327 else
3328 {
3329 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3330 if (gchar_pos(top) == NUL)
3331 tocol = fromcol + 1;
3332 }
3333 }
3334 if (VIsual_mode != 'V' && lnum == bot->lnum)
3335 {
3336 if (*p_sel == 'e' && bot->col == 0
3337#ifdef FEAT_VIRTUALEDIT
3338 && bot->coladd == 0
3339#endif
3340 )
3341 {
3342 fromcol = -10;
3343 tocol = MAXCOL;
3344 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003345 else if (bot->col == MAXCOL)
3346 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 else
3348 {
3349 pos = *bot;
3350 if (*p_sel == 'e')
3351 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3352 else
3353 {
3354 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3355 ++tocol;
3356 }
3357 }
3358 }
3359 }
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 /* Check if the character under the cursor should not be inverted */
3362 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003363#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 )
3367 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 /* if inverting in this line set area_highlighting */
3370 if (fromcol >= 0)
3371 {
3372 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003373 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003375 if ((clip_star.available && !clip_star.owned
3376 && clip_isautosel_star())
3377 || (clip_plus.available && !clip_plus.owned
3378 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003379 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380#endif
3381 }
3382 }
3383
3384 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003385 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003387 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && wp == curwin
3389 && lnum >= curwin->w_cursor.lnum
3390 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3391 {
3392 if (lnum == curwin->w_cursor.lnum)
3393 getvcol(curwin, &(curwin->w_cursor),
3394 (colnr_T *)&fromcol, NULL, NULL);
3395 else
3396 fromcol = 0;
3397 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3398 {
3399 pos.lnum = lnum;
3400 pos.col = search_match_endcol;
3401 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3402 }
3403 else
3404 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003405 /* do at least one character; happens when past end of line */
3406 if (fromcol == tocol)
3407 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003409 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411
3412#ifdef FEAT_DIFF
3413 filler_lines = diff_check(wp, lnum);
3414 if (filler_lines < 0)
3415 {
3416 if (filler_lines == -1)
3417 {
3418 if (diff_find_change(wp, lnum, &change_start, &change_end))
3419 diff_hlf = HLF_ADD; /* added line */
3420 else if (change_start == 0)
3421 diff_hlf = HLF_TXD; /* changed text */
3422 else
3423 diff_hlf = HLF_CHD; /* changed line */
3424 }
3425 else
3426 diff_hlf = HLF_ADD; /* added line */
3427 filler_lines = 0;
3428 area_highlighting = TRUE;
3429 }
3430 if (lnum == wp->w_topline)
3431 filler_lines = wp->w_topfill;
3432 filler_todo = filler_lines;
3433#endif
3434
3435#ifdef LINE_ATTR
3436# ifdef FEAT_SIGNS
3437 /* If this line has a sign with line highlighting set line_attr. */
3438 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3439 if (v != 0)
3440 line_attr = sign_get_attr((int)v, TRUE);
3441# endif
3442# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3443 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003444 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003445 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446# endif
3447 if (line_attr != 0)
3448 area_highlighting = TRUE;
3449#endif
3450
3451 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3452 ptr = line;
3453
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003454#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003455 if (has_spell)
3456 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003457 /* For checking first word with a capital skip white space. */
3458 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003460
Bram Moolenaar30abd282005-06-22 22:35:10 +00003461 /* To be able to spell-check over line boundaries copy the end of the
3462 * current line into nextline[]. Above the start of the next line was
3463 * copied to nextline[SPWORDLEN]. */
3464 if (nextline[SPWORDLEN] == NUL)
3465 {
3466 /* No next line or it is empty. */
3467 nextlinecol = MAXCOL;
3468 nextline_idx = 0;
3469 }
3470 else
3471 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003473 if (v < SPWORDLEN)
3474 {
3475 /* Short line, use it completely and append the start of the
3476 * next line. */
3477 nextlinecol = 0;
3478 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003479 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003480 nextline_idx = v + 1;
3481 }
3482 else
3483 {
3484 /* Long line, use only the last SPWORDLEN bytes. */
3485 nextlinecol = v - SPWORDLEN;
3486 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3487 nextline_idx = SPWORDLEN + 1;
3488 }
3489 }
3490 }
3491#endif
3492
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003493 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003495 if (lcs_space || lcs_trail)
3496 extra_check = TRUE;
3497 /* find start of trailing whitespace */
3498 if (lcs_trail)
3499 {
3500 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003501 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003502 --trailcol;
3503 trailcol += (colnr_T) (ptr - line);
3504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506
3507 /*
3508 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3509 * first character to be displayed.
3510 */
3511 if (wp->w_p_wrap)
3512 v = wp->w_skipcol;
3513 else
3514 v = wp->w_leftcol;
3515 if (v > 0)
3516 {
3517#ifdef FEAT_MBYTE
3518 char_u *prev_ptr = ptr;
3519#endif
3520 while (vcol < v && *ptr != NUL)
3521 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003522 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 vcol += c;
3524#ifdef FEAT_MBYTE
3525 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003527 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003530 /* When:
3531 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003532 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003533 * - 'virtualedit' is set, or
3534 * - the visual mode is active,
3535 * the end of the line may be before the start of the displayed part.
3536 */
3537 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003538#ifdef FEAT_SYN_HL
3539 wp->w_p_cuc || draw_color_col ||
3540#endif
3541#ifdef FEAT_VIRTUALEDIT
3542 virtual_active() ||
3543#endif
3544 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 /* Handle a character that's not completely on the screen: Put ptr at
3550 * that character but skip the first few screen characters. */
3551 if (vcol > v)
3552 {
3553 vcol -= c;
3554#ifdef FEAT_MBYTE
3555 ptr = prev_ptr;
3556#else
3557 --ptr;
3558#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003559 /* If the character fits on the screen, don't need to skip it.
3560 * Except for a TAB. */
3561 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003562#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003563 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003564#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003565 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003566 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 /*
3570 * Adjust for when the inverted text is before the screen,
3571 * and when the start of the inverted text is before the screen.
3572 */
3573 if (tocol <= vcol)
3574 fromcol = 0;
3575 else if (fromcol >= 0 && fromcol < vcol)
3576 fromcol = vcol;
3577
3578#ifdef FEAT_LINEBREAK
3579 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3580 if (wp->w_p_wrap)
3581 need_showbreak = TRUE;
3582#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003583#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003584 /* When spell checking a word we need to figure out the start of the
3585 * word and if it's badly spelled or not. */
3586 if (has_spell)
3587 {
3588 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003589 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003590 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003591
3592 pos = wp->w_cursor;
3593 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003594 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003595 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003596
3597 /* spell_move_to() may call ml_get() and make "line" invalid */
3598 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3599 ptr = line + linecol;
3600
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003601 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003602 {
3603 /* no bad word found at line start, don't check until end of a
3604 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003605 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003606 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003607 }
3608 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003609 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003610 /* bad word found, use attributes until end of word */
3611 word_end = wp->w_cursor.col + len + 1;
3612
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003613 /* Turn index into actual attributes. */
3614 if (spell_hlf != HLF_COUNT)
3615 spell_attr = highlight_attr[spell_hlf];
3616 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003617 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003618
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003619# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003620 /* Need to restart syntax highlighting for this line. */
3621 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003622 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003623# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 }
3625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627
3628 /*
3629 * Correct highlighting for cursor that can't be disabled.
3630 * Avoids having to check this for each character.
3631 */
3632 if (fromcol >= 0)
3633 {
3634 if (noinvcur)
3635 {
3636 if ((colnr_T)fromcol == wp->w_virtcol)
3637 {
3638 /* highlighting starts at cursor, let it start just after the
3639 * cursor */
3640 fromcol_prev = fromcol;
3641 fromcol = -1;
3642 }
3643 else if ((colnr_T)fromcol < wp->w_virtcol)
3644 /* restart highlighting after the cursor */
3645 fromcol_prev = wp->w_virtcol;
3646 }
3647 if (fromcol >= tocol)
3648 fromcol = -1;
3649 }
3650
3651#ifdef FEAT_SEARCH_EXTRA
3652 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003653 * Handle highlighting the last used search pattern and matches.
3654 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003656 cur = wp->w_match_head;
3657 shl_flag = FALSE;
3658 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003660 if (shl_flag == FALSE)
3661 {
3662 shl = &search_hl;
3663 shl_flag = TRUE;
3664 }
3665 else
3666 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003667 shl->startcol = MAXCOL;
3668 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003670 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003671 v = (long)(ptr - line);
3672 if (cur != NULL)
3673 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003674 next_search_hl(wp, shl, lnum, (colnr_T)v,
3675 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003676
3677 /* Need to get the line again, a multi-line regexp may have made it
3678 * invalid. */
3679 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3680 ptr = line + v;
3681
3682 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003684 if (shl->lnum == lnum)
3685 shl->startcol = shl->rm.startpos[0].col;
3686 else
3687 shl->startcol = 0;
3688 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3689 - shl->rm.startpos[0].lnum)
3690 shl->endcol = shl->rm.endpos[0].col;
3691 else
3692 shl->endcol = MAXCOL;
3693 /* Highlight one character for an empty match. */
3694 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003697 if (has_mbyte && line[shl->endcol] != NUL)
3698 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003701 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003703 if ((long)shl->startcol < v) /* match at leftcol */
3704 {
3705 shl->attr_cur = shl->attr;
3706 search_attr = shl->attr;
3707 }
3708 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003710 if (shl != &search_hl && cur != NULL)
3711 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713#endif
3714
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003715#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003716 /* Cursor line highlighting for 'cursorline' in the current window. Not
3717 * when Visual mode is active, because it's not clear what is selected
3718 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003719 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003720 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003721 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003722 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003723 area_highlighting = TRUE;
3724 }
3725#endif
3726
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003727 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 col = 0;
3729#ifdef FEAT_RIGHTLEFT
3730 if (wp->w_p_rl)
3731 {
3732 /* Rightleft window: process the text in the normal direction, but put
3733 * it in current_ScreenLine[] from right to left. Start at the
3734 * rightmost column of the window. */
3735 col = W_WIDTH(wp) - 1;
3736 off += col;
3737 }
3738#endif
3739
3740 /*
3741 * Repeat for the whole displayed line.
3742 */
3743 for (;;)
3744 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003745#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003746 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 /* Skip this quickly when working on the text. */
3749 if (draw_state != WL_LINE)
3750 {
3751#ifdef FEAT_CMDWIN
3752 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3753 {
3754 draw_state = WL_CMDLINE;
3755 if (cmdwin_type != 0 && wp == curwin)
3756 {
3757 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003759 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003760 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763#endif
3764
3765#ifdef FEAT_FOLDING
3766 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3767 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003768 int fdc = compute_foldcolumn(wp, 0);
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003771 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003773 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003774 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003775 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003776 p_extra_free = alloc(12 + 1);
3777
3778 if (p_extra_free != NULL)
3779 {
3780 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3781 n_extra = fdc;
3782 p_extra_free[n_extra] = NUL;
3783 p_extra = p_extra_free;
3784 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003785 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788 }
3789#endif
3790
3791#ifdef FEAT_SIGNS
3792 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3793 {
3794 draw_state = WL_SIGN;
3795 /* Show the sign column when there are any signs in this
3796 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003797 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003799 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003801 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802# endif
3803
3804 /* Draw two cells with the sign value or blank. */
3805 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003806 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 n_extra = 2;
3808
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003809 if (row == startrow
3810#ifdef FEAT_DIFF
3811 + filler_lines && filler_todo <= 0
3812#endif
3813 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3816 SIGN_TEXT);
3817# ifdef FEAT_SIGN_ICONS
3818 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3819 SIGN_ICON);
3820 if (gui.in_use && icon_sign != 0)
3821 {
3822 /* Use the image in this position. */
3823 c_extra = SIGN_BYTE;
3824# ifdef FEAT_NETBEANS_INTG
3825 if (buf_signcount(wp->w_buffer, lnum) > 1)
3826 c_extra = MULTISIGN_BYTE;
3827# endif
3828 char_attr = icon_sign;
3829 }
3830 else
3831# endif
3832 if (text_sign != 0)
3833 {
3834 p_extra = sign_get_text(text_sign);
3835 if (p_extra != NULL)
3836 {
3837 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003838 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 char_attr = sign_get_attr(text_sign, FALSE);
3841 }
3842 }
3843 }
3844 }
3845#endif
3846
3847 if (draw_state == WL_NR - 1 && n_extra == 0)
3848 {
3849 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003850 /* Display the absolute or relative line number. After the
3851 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3852 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 && (row == startrow
3854#ifdef FEAT_DIFF
3855 + filler_lines
3856#endif
3857 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3858 {
3859 /* Draw the line number (empty space after wrapping). */
3860 if (row == startrow
3861#ifdef FEAT_DIFF
3862 + filler_lines
3863#endif
3864 )
3865 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003866 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003867 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003868
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003869 if (wp->w_p_nu && !wp->w_p_rnu)
3870 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003871 num = (long)lnum;
3872 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003873 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003874 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003875 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003876 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003877 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003878 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003879 num = lnum;
3880 fmt = "%-*ld ";
3881 }
3882 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003883
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003885 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (wp->w_skipcol > 0)
3887 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3888 *p_extra = '-';
3889#ifdef FEAT_RIGHTLEFT
3890 if (wp->w_p_rl) /* reverse line numbers */
3891 rl_mirror(extra);
3892#endif
3893 p_extra = extra;
3894 c_extra = NUL;
3895 }
3896 else
3897 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003898 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003899 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003900#ifdef FEAT_SYN_HL
3901 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003902 * the current line differently.
3903 * TODO: Can we use CursorLine instead of CursorLineNr
3904 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003905 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003906 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003907 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 }
3911
Bram Moolenaar597a4222014-06-25 14:39:50 +02003912#ifdef FEAT_LINEBREAK
3913 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3914 && n_extra == 0 && *p_sbr != NUL)
3915 /* draw indent after showbreak value */
3916 draw_state = WL_BRI;
3917 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3918 /* After the showbreak, draw the breakindent */
3919 draw_state = WL_BRI - 1;
3920
3921 /* draw 'breakindent': indent wrapped text accordingly */
3922 if (draw_state == WL_BRI - 1 && n_extra == 0)
3923 {
3924 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003925 /* if need_showbreak is set, breakindent also applies */
3926 if (wp->w_p_bri && n_extra == 0
3927 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003928# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003929 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003930# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003931 )
3932 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003933 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003934# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003935 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003936 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003937 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003938# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003939 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3940 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003941 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003942# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003943 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003944# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003945 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003946 c_extra = ' ';
3947 n_extra = get_breakindent_win(wp,
3948 ml_get_buf(wp->w_buffer, lnum, FALSE));
3949 /* Correct end of highlighted area for 'breakindent',
3950 * required when 'linebreak' is also set. */
3951 if (tocol == vcol)
3952 tocol += n_extra;
3953 }
3954 }
3955#endif
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3958 if (draw_state == WL_SBR - 1 && n_extra == 0)
3959 {
3960 draw_state = WL_SBR;
3961# ifdef FEAT_DIFF
3962 if (filler_todo > 0)
3963 {
3964 /* Draw "deleted" diff line(s). */
3965 if (char2cells(fill_diff) > 1)
3966 c_extra = '-';
3967 else
3968 c_extra = fill_diff;
3969# ifdef FEAT_RIGHTLEFT
3970 if (wp->w_p_rl)
3971 n_extra = col + 1;
3972 else
3973# endif
3974 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003975 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977# endif
3978# ifdef FEAT_LINEBREAK
3979 if (*p_sbr != NUL && need_showbreak)
3980 {
3981 /* Draw 'showbreak' at the start of each broken line. */
3982 p_extra = p_sbr;
3983 c_extra = NUL;
3984 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003985 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003987 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 /* Correct end of highlighted area for 'showbreak',
3989 * required when 'linebreak' is also set. */
3990 if (tocol == vcol)
3991 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003992#ifdef FEAT_SYN_HL
3993 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003994 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003995 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003996 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999# endif
4000 }
4001#endif
4002
4003 if (draw_state == WL_LINE - 1 && n_extra == 0)
4004 {
4005 draw_state = WL_LINE;
4006 if (saved_n_extra)
4007 {
4008 /* Continue item from end of wrapped line. */
4009 n_extra = saved_n_extra;
4010 c_extra = saved_c_extra;
4011 p_extra = saved_p_extra;
4012 char_attr = saved_char_attr;
4013 }
4014 else
4015 char_attr = 0;
4016 }
4017 }
4018
4019 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004020 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#ifdef FEAT_DIFF
4023 && filler_todo <= 0
4024#endif
4025 )
4026 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004027 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004028 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 /* Pretend we have finished updating the window. Except when
4030 * 'cursorcolumn' is set. */
4031#ifdef FEAT_SYN_HL
4032 if (wp->w_p_cuc)
4033 row = wp->w_cline_row + wp->w_cline_height;
4034 else
4035#endif
4036 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
4038 }
4039
4040 if (draw_state == WL_LINE && area_highlighting)
4041 {
4042 /* handle Visual or match highlighting in this line */
4043 if (vcol == fromcol
4044#ifdef FEAT_MBYTE
4045 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4046 && (*mb_ptr2cells)(ptr) > 1)
4047#endif
4048 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004049 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 && vcol < tocol))
4051 area_attr = attr; /* start highlighting */
4052 else if (area_attr != 0
4053 && (vcol == tocol
4054 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057#ifdef FEAT_SEARCH_EXTRA
4058 if (!n_extra)
4059 {
4060 /*
4061 * Check for start/end of search pattern match.
4062 * After end, check for start/end of next match.
4063 * When another match, have to check for start again.
4064 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004065 * Do this for 'search_hl' and the match list (ordered by
4066 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004068 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004069 cur = wp->w_match_head;
4070 shl_flag = FALSE;
4071 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004073 if (shl_flag == FALSE
4074 && ((cur != NULL
4075 && cur->priority > SEARCH_HL_PRIORITY)
4076 || cur == NULL))
4077 {
4078 shl = &search_hl;
4079 shl_flag = TRUE;
4080 }
4081 else
4082 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004083 if (cur != NULL)
4084 cur->pos.cur = 0;
4085 pos_inprogress = TRUE;
4086 while (shl->rm.regprog != NULL
4087 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004089 if (shl->startcol != MAXCOL
4090 && v >= (long)shl->startcol
4091 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004093#ifdef FEAT_MBYTE
4094 int tmp_col = v + MB_PTR2LEN(ptr);
4095
4096 if (shl->endcol < tmp_col)
4097 shl->endcol = tmp_col;
4098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004100#ifdef FEAT_CONCEAL
4101 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4102 == cur->hlg_id)
4103 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004104 has_match_conc =
4105 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004106 match_conc = cur->conceal_char;
4107 }
4108 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004109 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004112 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
4114 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004115 next_search_hl(wp, shl, lnum, (colnr_T)v,
4116 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004117 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004118 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 /* Need to get the line again, a multi-line regexp
4121 * may have made it invalid. */
4122 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4123 ptr = line + v;
4124
4125 if (shl->lnum == lnum)
4126 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004127 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004131 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004133 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
4135 /* highlight empty match, try again after
4136 * it */
4137#ifdef FEAT_MBYTE
4138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004139 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004140 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 else
4142#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004143 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145
4146 /* Loop to check if the match starts at the
4147 * current position */
4148 continue;
4149 }
4150 }
4151 break;
4152 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004153 if (shl != &search_hl && cur != NULL)
4154 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004156
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004157 /* Use attributes from match with highest priority among
4158 * 'search_hl' and the match list. */
4159 search_attr = search_hl.attr_cur;
4160 cur = wp->w_match_head;
4161 shl_flag = FALSE;
4162 while (cur != NULL || shl_flag == FALSE)
4163 {
4164 if (shl_flag == FALSE
4165 && ((cur != NULL
4166 && cur->priority > SEARCH_HL_PRIORITY)
4167 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004168 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004169 shl = &search_hl;
4170 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004171 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004172 else
4173 shl = &cur->hl;
4174 if (shl->attr_cur != 0)
4175 search_attr = shl->attr_cur;
4176 if (shl != &search_hl && cur != NULL)
4177 cur = cur->next;
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180#endif
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004183 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004185 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4186 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004188 if (diff_hlf == HLF_TXD && ptr - line > change_end
4189 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004191 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004192 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004193 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004196
4197 /* Decide which of the highlight attributes to use. */
4198 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004199#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004200 if (area_attr != 0)
4201 char_attr = hl_combine_attr(line_attr, area_attr);
4202 else if (search_attr != 0)
4203 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204 /* Use line_attr when not in the Visual or 'incsearch' area
4205 * (area_attr may be 0 when "noinvcur" is set). */
4206 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004207 || vcol < fromcol || vcol_prev < fromcol_prev
4208 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004209 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004210#else
4211 if (area_attr != 0)
4212 char_attr = area_attr;
4213 else if (search_attr != 0)
4214 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004215#endif
4216 else
4217 {
4218 attr_pri = FALSE;
4219#ifdef FEAT_SYN_HL
4220 if (has_syntax)
4221 char_attr = syntax_attr;
4222 else
4223#endif
4224 char_attr = 0;
4225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227
4228 /*
4229 * Get the next character to put on the screen.
4230 */
4231 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004232 * The "p_extra" points to the extra stuff that is inserted to
4233 * represent special characters (non-printable stuff) and other
4234 * things. When all characters are the same, c_extra is used.
4235 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4236 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4238 */
4239 if (n_extra > 0)
4240 {
4241 if (c_extra != NUL)
4242 {
4243 c = c_extra;
4244#ifdef FEAT_MBYTE
4245 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004246 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 else
4253 mb_utf8 = FALSE;
4254#endif
4255 }
4256 else
4257 {
4258 c = *p_extra;
4259#ifdef FEAT_MBYTE
4260 if (has_mbyte)
4261 {
4262 mb_c = c;
4263 if (enc_utf8)
4264 {
4265 /* If the UTF-8 character is more than one byte:
4266 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004267 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 mb_utf8 = FALSE;
4269 if (mb_l > n_extra)
4270 mb_l = 1;
4271 else if (mb_l > 1)
4272 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004273 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 }
4278 else
4279 {
4280 /* if this is a DBCS character, put it in "mb_c" */
4281 mb_l = MB_BYTE2LEN(c);
4282 if (mb_l >= n_extra)
4283 mb_l = 1;
4284 else if (mb_l > 1)
4285 mb_c = (c << 8) + p_extra[1];
4286 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004287 if (mb_l == 0) /* at the NUL at end-of-line */
4288 mb_l = 1;
4289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 /* If a double-width char doesn't fit display a '>' in the
4291 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004292 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293# ifdef FEAT_RIGHTLEFT
4294 wp->w_p_rl ? (col <= 0) :
4295# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004296 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 && (*mb_char2cells)(mb_c) == 2)
4298 {
4299 c = '>';
4300 mb_c = c;
4301 mb_l = 1;
4302 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004303 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 /* put the pointer back to output the double-width
4305 * character at the start of the next line. */
4306 ++n_extra;
4307 --p_extra;
4308 }
4309 else
4310 {
4311 n_extra -= mb_l - 1;
4312 p_extra += mb_l - 1;
4313 }
4314 }
4315#endif
4316 ++p_extra;
4317 }
4318 --n_extra;
4319 }
4320 else
4321 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004322#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004323 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004324#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004325
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004326 if (p_extra_free != NULL)
4327 {
4328 vim_free(p_extra_free);
4329 p_extra_free = NULL;
4330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 /*
4332 * Get a character from the line itself.
4333 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004334 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004335#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004336 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#ifdef FEAT_MBYTE
4339 if (has_mbyte)
4340 {
4341 mb_c = c;
4342 if (enc_utf8)
4343 {
4344 /* If the UTF-8 character is more than one byte: Decode it
4345 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004346 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 mb_utf8 = FALSE;
4348 if (mb_l > 1)
4349 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* Overlong encoded ASCII or ASCII with composing char
4352 * is displayed normally, except a NUL. */
4353 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004354 {
4355 c = mb_c;
4356# ifdef FEAT_LINEBREAK
4357 c0 = mb_c;
4358# endif
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004361
4362 /* At start of the line we can have a composing char.
4363 * Draw it as a space with a composing char. */
4364 if (utf_iscomposing(mb_c))
4365 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004366 int i;
4367
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004368 for (i = Screen_mco - 1; i > 0; --i)
4369 u8cc[i] = u8cc[i - 1];
4370 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004371 mb_c = ' ';
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 if ((mb_l == 1 && c >= 0x80)
4376 || (mb_l >= 1 && mb_c == 0)
4377 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004378# ifdef UNICODE16
4379 || mb_c >= 0x10000
4380# endif
4381 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 /*
4384 * Illegal UTF-8 byte: display as <xx>.
4385 * Non-BMP character : display as ? or fullwidth ?.
4386 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004387# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004392# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (wp->w_p_rl) /* reverse */
4394 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004395# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004397# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else if (utf_char2cells(mb_c) != 2)
4399 STRCPY(extra, "?");
4400 else
4401 /* 0xff1f in UTF-8: full-width '?' */
4402 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 p_extra = extra;
4406 c = *p_extra;
4407 mb_c = mb_ptr2char_adv(&p_extra);
4408 mb_utf8 = (c >= 0x80);
4409 n_extra = (int)STRLEN(p_extra);
4410 c_extra = NUL;
4411 if (area_attr == 0 && search_attr == 0)
4412 {
4413 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004414 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 saved_attr2 = char_attr; /* save current attr */
4416 }
4417 }
4418 else if (mb_l == 0) /* at the NUL at end-of-line */
4419 mb_l = 1;
4420#ifdef FEAT_ARABIC
4421 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4422 {
4423 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004424 int pc, pc1, nc;
4425 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /* The idea of what is the previous and next
4428 * character depends on 'rightleft'. */
4429 if (wp->w_p_rl)
4430 {
4431 pc = prev_c;
4432 pc1 = prev_c1;
4433 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 else
4437 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004438 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 }
4442 prev_c = mb_c;
4443
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 else
4447 prev_c = mb_c;
4448#endif
4449 }
4450 else /* enc_dbcs */
4451 {
4452 mb_l = MB_BYTE2LEN(c);
4453 if (mb_l == 0) /* at the NUL at end-of-line */
4454 mb_l = 1;
4455 else if (mb_l > 1)
4456 {
4457 /* We assume a second byte below 32 is illegal.
4458 * Hopefully this is OK for all double-byte encodings!
4459 */
4460 if (ptr[1] >= 32)
4461 mb_c = (c << 8) + ptr[1];
4462 else
4463 {
4464 if (ptr[1] == NUL)
4465 {
4466 /* head byte at end of line */
4467 mb_l = 1;
4468 transchar_nonprint(extra, c);
4469 }
4470 else
4471 {
4472 /* illegal tail byte */
4473 mb_l = 2;
4474 STRCPY(extra, "XX");
4475 }
4476 p_extra = extra;
4477 n_extra = (int)STRLEN(extra) - 1;
4478 c_extra = NUL;
4479 c = *p_extra++;
4480 if (area_attr == 0 && search_attr == 0)
4481 {
4482 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004483 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 saved_attr2 = char_attr; /* save current attr */
4485 }
4486 mb_c = c;
4487 }
4488 }
4489 }
4490 /* If a double-width char doesn't fit display a '>' in the
4491 * last column; the character is displayed at the start of the
4492 * next line. */
4493 if ((
4494# ifdef FEAT_RIGHTLEFT
4495 wp->w_p_rl ? (col <= 0) :
4496# endif
4497 (col >= W_WIDTH(wp) - 1))
4498 && (*mb_char2cells)(mb_c) == 2)
4499 {
4500 c = '>';
4501 mb_c = c;
4502 mb_utf8 = FALSE;
4503 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004504 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 /* Put pointer back so that the character will be
4506 * displayed at the start of the next line. */
4507 --ptr;
4508 }
4509 else if (*ptr != NUL)
4510 ptr += mb_l - 1;
4511
4512 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004513 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004514 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004515 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004518 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 c = ' ';
4520 if (area_attr == 0 && search_attr == 0)
4521 {
4522 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 saved_attr2 = char_attr; /* save current attr */
4525 }
4526 mb_c = c;
4527 mb_utf8 = FALSE;
4528 mb_l = 1;
4529 }
4530
4531 }
4532#endif
4533 ++ptr;
4534
4535 if (extra_check)
4536 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004537#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004538 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004539#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004540
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004541#ifdef FEAT_TERMINAL
4542 if (get_term_attr)
4543 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004544 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004545
4546 if (!attr_pri)
4547 char_attr = syntax_attr;
4548 else
4549 char_attr = hl_combine_attr(syntax_attr, char_attr);
4550 }
4551#endif
4552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004553#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /* Get syntax attribute, unless still at the start of the line
4555 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004556 v = (long)(ptr - line);
4557 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
4559 /* Get the syntax attribute for the character. If there
4560 * is an error, disable syntax highlighting. */
4561 save_did_emsg = did_emsg;
4562 did_emsg = FALSE;
4563
Bram Moolenaar217ad922005-03-20 22:37:15 +00004564 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004565# ifdef FEAT_SPELL
4566 has_spell ? &can_spell :
4567# endif
4568 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
4570 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004571 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004572 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004573 has_syntax = FALSE;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 else
4576 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004577#ifdef SYN_TIME_LIMIT
4578 if (wp->w_s->b_syn_slow)
4579 has_syntax = FALSE;
4580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
4582 /* Need to get the line again, a multi-line regexp may
4583 * have made it invalid. */
4584 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4585 ptr = line + v;
4586
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004587 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004589 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004590 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004591# ifdef FEAT_CONCEAL
4592 /* no concealing past the end of the line, it interferes
4593 * with line highlighting */
4594 if (c == NUL)
4595 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004596 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004597 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004600#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004601
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004602#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004603 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004604 * Only do this when there is no syntax highlighting, the
4605 * @Spell cluster is not used or the current syntax item
4606 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004607 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004608 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004609 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004610# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004611 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004612 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004613# endif
4614 if (c != 0 && (
4615# ifdef FEAT_SYN_HL
4616 !has_syntax ||
4617# endif
4618 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004619 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004620 char_u *prev_ptr, *p;
4621 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004622 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004623# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004624 if (has_mbyte)
4625 {
4626 prev_ptr = ptr - mb_l;
4627 v -= mb_l - 1;
4628 }
4629 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004630# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004631 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004632
4633 /* Use nextline[] if possible, it has the start of the
4634 * next line concatenated. */
4635 if ((prev_ptr - line) - nextlinecol >= 0)
4636 p = nextline + (prev_ptr - line) - nextlinecol;
4637 else
4638 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004639 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004640 len = spell_check(wp, p, &spell_hlf, &cap_col,
4641 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004642 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004643
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004644 /* In Insert mode only highlight a word that
4645 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004646 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004647 && (State & INSERT) != 0
4648 && wp->w_cursor.lnum == lnum
4649 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004650 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004651 && wp->w_cursor.col < (colnr_T)word_end)
4652 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004653 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004654 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004656
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004657 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004658 && (p - nextline) + len > nextline_idx)
4659 {
4660 /* Remember that the good word continues at the
4661 * start of the next line. */
4662 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004663 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004664 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004665
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004666 /* Turn index into actual attributes. */
4667 if (spell_hlf != HLF_COUNT)
4668 spell_attr = highlight_attr[spell_hlf];
4669
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004670 if (cap_col > 0)
4671 {
4672 if (p != prev_ptr
4673 && (p - nextline) + cap_col >= nextline_idx)
4674 {
4675 /* Remember that the word in the next line
4676 * must start with a capital. */
4677 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004678 cap_col = (int)((p - nextline) + cap_col
4679 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004680 }
4681 else
4682 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004684 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004685 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004686 }
4687 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004688 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004689 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004690 char_attr = hl_combine_attr(char_attr, spell_attr);
4691 else
4692 char_attr = hl_combine_attr(spell_attr, char_attr);
4693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
4695#ifdef FEAT_LINEBREAK
4696 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004697 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004699 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004700 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004702# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004703 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004704# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004705 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004707 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004709 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004710
Bram Moolenaar597a4222014-06-25 14:39:50 +02004711 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004712 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004713 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004714 if (c == TAB && n_extra + col > W_WIDTH(wp))
4715 n_extra = (int)wp->w_buffer->b_p_ts
4716 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4717
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004718# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004719 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004720# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004722# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004723 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004724 {
4725#ifdef FEAT_CONCEAL
4726 if (c == TAB)
4727 /* See "Tab alignment" below. */
4728 FIX_FOR_BOGUSCOLS;
4729#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004730 if (!wp->w_p_list)
4731 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734#endif
4735
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004736 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4737 */
4738 if (wp->w_p_list
4739 && (((c == 160
4740#ifdef FEAT_MBYTE
4741 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4742#endif
4743 ) && lcs_nbsp)
4744 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4745 {
4746 c = (c == ' ') ? lcs_space : lcs_nbsp;
4747 if (area_attr == 0 && search_attr == 0)
4748 {
4749 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004750 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004751 saved_attr2 = char_attr; /* save current attr */
4752 }
4753#ifdef FEAT_MBYTE
4754 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004755 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004756 {
4757 mb_utf8 = TRUE;
4758 u8cc[0] = 0;
4759 c = 0xc0;
4760 }
4761 else
4762 mb_utf8 = FALSE;
4763#endif
4764 }
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4767 {
4768 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004769 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004772 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 saved_attr2 = char_attr; /* save current attr */
4774 }
4775#ifdef FEAT_MBYTE
4776 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004777 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004780 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 else
4784 mb_utf8 = FALSE;
4785#endif
4786 }
4787 }
4788
4789 /*
4790 * Handling of non-printable characters.
4791 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004792 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 /*
4795 * when getting a character from the file, we may have to
4796 * turn it into something else on the way to putting it
4797 * into "ScreenLines".
4798 */
4799 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4800 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004801 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004802 long vcol_adjusted = vcol; /* removed showbreak length */
4803#ifdef FEAT_LINEBREAK
4804 /* only adjust the tab_len, when at the first column
4805 * after the showbreak value was drawn */
4806 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4807 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004810 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004811 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4812
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004813#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004814 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815#endif
4816 /* tab amount depends on current column */
4817 n_extra = tab_len;
4818#ifdef FEAT_LINEBREAK
4819 else
4820 {
4821 char_u *p;
4822 int len = n_extra;
4823 int i;
4824 int saved_nextra = n_extra;
4825
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004826#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004827 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004828 /* there are characters to conceal */
4829 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004830 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4831 */
4832 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4833 && n_extra > tab_len)
4834 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004835#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004836
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004837 /* if n_extra > 0, it gives the number of chars, to
4838 * use for a tab, else we need to calculate the width
4839 * for a tab */
4840#ifdef FEAT_MBYTE
4841 len = (tab_len * mb_char2len(lcs_tab2));
4842 if (n_extra > 0)
4843 len += n_extra - tab_len;
4844#endif
4845 c = lcs_tab1;
4846 p = alloc((unsigned)(len + 1));
4847 vim_memset(p, ' ', len);
4848 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004849 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004850 p_extra_free = p;
4851 for (i = 0; i < tab_len; i++)
4852 {
4853#ifdef FEAT_MBYTE
4854 mb_char2bytes(lcs_tab2, p);
4855 p += mb_char2len(lcs_tab2);
4856 n_extra += mb_char2len(lcs_tab2)
4857 - (saved_nextra > 0 ? 1 : 0);
4858#else
4859 p[i] = lcs_tab2;
4860#endif
4861 }
4862 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004863#ifdef FEAT_CONCEAL
4864 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4865 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004866 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004867 n_extra -= vcol_off;
4868#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004869 }
4870#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004871#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004872 {
4873 int vc_saved = vcol_off;
4874
4875 /* Tab alignment should be identical regardless of
4876 * 'conceallevel' value. So tab compensates of all
4877 * previous concealed characters, and thus resets
4878 * vcol_off and boguscols accumulated so far in the
4879 * line. Note that the tab can be longer than
4880 * 'tabstop' when there are concealed characters. */
4881 FIX_FOR_BOGUSCOLS;
4882
4883 /* Make sure, the highlighting for the tab char will be
4884 * correctly set further below (effectively reverts the
4885 * FIX_FOR_BOGSUCOLS macro */
4886 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004887 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004888 tab_len += vc_saved;
4889 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#ifdef FEAT_MBYTE
4892 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4893#endif
4894 if (wp->w_p_list)
4895 {
4896 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897#ifdef FEAT_LINEBREAK
4898 if (wp->w_p_lbr)
4899 c_extra = NUL; /* using p_extra from above */
4900 else
4901#endif
4902 c_extra = lcs_tab2;
4903 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004904 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 saved_attr2 = char_attr; /* save current attr */
4906#ifdef FEAT_MBYTE
4907 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004908 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004911 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004912 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914#endif
4915 }
4916 else
4917 {
4918 c_extra = ' ';
4919 c = ' ';
4920 }
4921 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004922 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004923 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004924 || ((fromcol >= 0 || fromcol_prev >= 0)
4925 && tocol > vcol
4926 && VIsual_mode != Ctrl_V
4927 && (
4928# ifdef FEAT_RIGHTLEFT
4929 wp->w_p_rl ? (col >= 0) :
4930# endif
4931 (col < W_WIDTH(wp)))
4932 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004933 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004934 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004935 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004937 /* Display a '$' after the line or highlight an extra
4938 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4940 /* For a diff line the highlighting continues after the
4941 * "$". */
4942 if (
4943# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004944 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945# ifdef LINE_ATTR
4946 &&
4947# endif
4948# endif
4949# ifdef LINE_ATTR
4950 line_attr == 0
4951# endif
4952 )
4953#endif
4954 {
4955#ifdef FEAT_VIRTUALEDIT
4956 /* In virtualedit, visual selections may extend
4957 * beyond end of line. */
4958 if (area_highlighting && virtual_active()
4959 && tocol != MAXCOL && vcol < tocol)
4960 n_extra = 0;
4961 else
4962#endif
4963 {
4964 p_extra = at_end_str;
4965 n_extra = 1;
4966 c_extra = NUL;
4967 }
4968 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004969 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004970 c = lcs_eol;
4971 else
4972 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 lcs_eol_one = -1;
4974 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004975 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004977 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 n_attr = 1;
4979 }
4980#ifdef FEAT_MBYTE
4981 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004982 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004985 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
4989 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4990#endif
4991 }
4992 else if (c != NUL)
4993 {
4994 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004995 if (n_extra == 0)
4996 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997#ifdef FEAT_RIGHTLEFT
4998 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4999 rl_mirror(p_extra); /* reverse "<12>" */
5000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005002#ifdef FEAT_LINEBREAK
5003 if (wp->w_p_lbr)
5004 {
5005 char_u *p;
5006
5007 c = *p_extra;
5008 p = alloc((unsigned)n_extra + 1);
5009 vim_memset(p, ' ', n_extra);
5010 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5011 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005012 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005013 p_extra_free = p_extra = p;
5014 }
5015 else
5016#endif
5017 {
5018 n_extra = byte2cells(c) - 1;
5019 c = *p_extra++;
5020 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005021 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
5023 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005024 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 saved_attr2 = char_attr; /* save current attr */
5026 }
5027#ifdef FEAT_MBYTE
5028 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5029#endif
5030 }
5031#ifdef FEAT_VIRTUALEDIT
5032 else if (VIsual_active
5033 && (VIsual_mode == Ctrl_V
5034 || VIsual_mode == 'v')
5035 && virtual_active()
5036 && tocol != MAXCOL
5037 && vcol < tocol
5038 && (
5039# ifdef FEAT_RIGHTLEFT
5040 wp->w_p_rl ? (col >= 0) :
5041# endif
5042 (col < W_WIDTH(wp))))
5043 {
5044 c = ' ';
5045 --ptr; /* put it back at the NUL */
5046 }
5047#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005048#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 else if ((
5050# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005051 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 ) && (
5055# ifdef FEAT_RIGHTLEFT
5056 wp->w_p_rl ? (col >= 0) :
5057# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005058 (col
5059# ifdef FEAT_CONCEAL
5060 - boguscols
5061# endif
5062 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 /* Highlight until the right side of the window */
5065 c = ' ';
5066 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005067
5068 /* Remember we do the char for line highlighting. */
5069 ++did_line_attr;
5070
5071 /* don't do search HL for the rest of the line */
5072 if (line_attr != 0 && char_attr == search_attr && col > 0)
5073 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074# ifdef FEAT_DIFF
5075 if (diff_hlf == HLF_TXD)
5076 {
5077 diff_hlf = HLF_CHD;
5078 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005079 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005080 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005081 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5082 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005083 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086# endif
5087 }
5088#endif
5089 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005090
5091#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005092 if ( wp->w_p_cole > 0
5093 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005094 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005095 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005096 && !(lnum_in_visual_area
5097 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005098 {
5099 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005100 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005101 && (syn_get_sub_char() != NUL || match_conc
5102 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005103 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005104 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005105 /* First time at this concealed item: display one
5106 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005107 if (match_conc)
5108 c = match_conc;
5109 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005110 c = syn_get_sub_char();
5111 else if (lcs_conceal != NUL)
5112 c = lcs_conceal;
5113 else
5114 c = ' ';
5115
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005116 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005117
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005118 if (n_extra > 0)
5119 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005120 vcol += n_extra;
5121 if (wp->w_p_wrap && n_extra > 0)
5122 {
5123# ifdef FEAT_RIGHTLEFT
5124 if (wp->w_p_rl)
5125 {
5126 col -= n_extra;
5127 boguscols -= n_extra;
5128 }
5129 else
5130# endif
5131 {
5132 boguscols += n_extra;
5133 col += n_extra;
5134 }
5135 }
5136 n_extra = 0;
5137 n_attr = 0;
5138 }
5139 else if (n_skip == 0)
5140 {
5141 is_concealing = TRUE;
5142 n_skip = 1;
5143 }
5144# ifdef FEAT_MBYTE
5145 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005146 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005147 {
5148 mb_utf8 = TRUE;
5149 u8cc[0] = 0;
5150 c = 0xc0;
5151 }
5152 else
5153 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5154# endif
5155 }
5156 else
5157 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005158 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005159 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005160 }
5161#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005164#ifdef FEAT_CONCEAL
5165 /* In the cursor line and we may be concealing characters: correct
5166 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005167 if (!did_wcol && draw_state == WL_LINE
5168 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005169 && conceal_cursor_line(wp)
5170 && (int)wp->w_virtcol <= vcol + n_skip)
5171 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005172# ifdef FEAT_RIGHTLEFT
5173 if (wp->w_p_rl)
5174 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5175 else
5176# endif
5177 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005178 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005179 did_wcol = TRUE;
5180 }
5181#endif
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 /* Don't override visual selection highlighting. */
5184 if (n_attr > 0
5185 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005186 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 char_attr = extra_attr;
5188
Bram Moolenaar81695252004-12-29 20:58:21 +00005189#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 /* XIM don't send preedit_start and preedit_end, but they send
5191 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5192 * im_is_preediting() here. */
5193 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005194 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 && (State & INSERT)
5196 && !p_imdisable
5197 && im_is_preediting()
5198 && draw_state == WL_LINE)
5199 {
5200 colnr_T tcol;
5201
5202 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005203 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 else
5205 tcol = preedit_end_col;
5206 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5207 {
5208 if (feedback_old_attr < 0)
5209 {
5210 feedback_col = 0;
5211 feedback_old_attr = char_attr;
5212 }
5213 char_attr = im_get_feedback_attr(feedback_col);
5214 if (char_attr < 0)
5215 char_attr = feedback_old_attr;
5216 feedback_col++;
5217 }
5218 else if (feedback_old_attr >= 0)
5219 {
5220 char_attr = feedback_old_attr;
5221 feedback_old_attr = -1;
5222 feedback_col = 0;
5223 }
5224 }
5225#endif
5226 /*
5227 * Handle the case where we are in column 0 but not on the first
5228 * character of the line and the user wants us to show us a
5229 * special character (via 'listchars' option "precedes:<char>".
5230 */
5231 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005232 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5234#ifdef FEAT_DIFF
5235 && filler_todo <= 0
5236#endif
5237 && draw_state > WL_NR
5238 && c != NUL)
5239 {
5240 c = lcs_prec;
5241 lcs_prec_todo = NUL;
5242#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005243 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5244 {
5245 /* Double-width character being overwritten by the "precedes"
5246 * character, need to fill up half the character. */
5247 c_extra = MB_FILLER_CHAR;
5248 n_extra = 1;
5249 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005250 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005253 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005256 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005257 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 else
5260 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5261#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005262 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
5264 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005265 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 n_attr3 = 1;
5267 }
5268 }
5269
5270 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005271 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005273 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005274#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005275 || did_line_attr == 1
5276#endif
5277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279#ifdef FEAT_SEARCH_EXTRA
5280 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005281
5282 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005283 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005284 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005285#endif
5286
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005287 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 * highlight match at end of line. If it's beyond the last
5289 * char on the screen, just overwrite that one (tricky!) Not
5290 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005291#ifdef FEAT_SEARCH_EXTRA
5292 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005293 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005294 prevcol_hl_flag = TRUE;
5295 else
5296 {
5297 cur = wp->w_match_head;
5298 while (cur != NULL)
5299 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005300 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005301 {
5302 prevcol_hl_flag = TRUE;
5303 break;
5304 }
5305 cur = cur->next;
5306 }
5307 }
5308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005310 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005311 && (VIsual_mode != Ctrl_V
5312 || lnum == VIsual.lnum
5313 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005314 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_SEARCH_EXTRA
5316 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005317 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005318# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005319 && did_line_attr <= 1
5320# endif
5321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#endif
5323 ))
5324 {
5325 int n = 0;
5326
5327#ifdef FEAT_RIGHTLEFT
5328 if (wp->w_p_rl)
5329 {
5330 if (col < 0)
5331 n = 1;
5332 }
5333 else
5334#endif
5335 {
5336 if (col >= W_WIDTH(wp))
5337 n = -1;
5338 }
5339 if (n != 0)
5340 {
5341 /* At the window boundary, highlight the last character
5342 * instead (better than nothing). */
5343 off += n;
5344 col += n;
5345 }
5346 else
5347 {
5348 /* Add a blank character to highlight. */
5349 ScreenLines[off] = ' ';
5350#ifdef FEAT_MBYTE
5351 if (enc_utf8)
5352 ScreenLinesUC[off] = 0;
5353#endif
5354 }
5355#ifdef FEAT_SEARCH_EXTRA
5356 if (area_attr == 0)
5357 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005358 /* Use attributes from match with highest priority among
5359 * 'search_hl' and the match list. */
5360 char_attr = search_hl.attr;
5361 cur = wp->w_match_head;
5362 shl_flag = FALSE;
5363 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005364 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005365 if (shl_flag == FALSE
5366 && ((cur != NULL
5367 && cur->priority > SEARCH_HL_PRIORITY)
5368 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005369 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005370 shl = &search_hl;
5371 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005372 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005373 else
5374 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005375 if ((ptr - line) - 1 == (long)shl->startcol
5376 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005377 char_attr = shl->attr;
5378 if (shl != &search_hl && cur != NULL)
5379 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382#endif
5383 ScreenAttrs[off] = char_attr;
5384#ifdef FEAT_RIGHTLEFT
5385 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005388 --off;
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else
5391#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005394 ++off;
5395 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005396 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005397#ifdef FEAT_SYN_HL
5398 eol_hl_off = 1;
5399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaar91170f82006-05-05 21:15:17 +00005403 /*
5404 * At end of the text line.
5405 */
5406 if (c == NUL)
5407 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005408#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5410 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005411 {
5412 /* highlight last char after line */
5413 --col;
5414 --off;
5415 --vcol;
5416 }
5417
Bram Moolenaar1a384422010-07-14 19:53:30 +02005418 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005419 if (wp->w_p_wrap)
5420 v = wp->w_skipcol;
5421 else
5422 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005423
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005424 /* check if line ends before left margin */
5425 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005426 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005427#ifdef FEAT_CONCEAL
5428 /* Get rid of the boguscols now, we want to draw until the right
5429 * edge for 'cursorcolumn'. */
5430 col -= boguscols;
5431 boguscols = 0;
5432#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005433
5434 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005435 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005436
5437 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005438 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5439 && (int)wp->w_virtcol <
5440 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005441 && lnum != wp->w_cursor.lnum)
5442 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005443# ifdef FEAT_RIGHTLEFT
5444 && !wp->w_p_rl
5445# endif
5446 )
5447 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005448 int rightmost_vcol = 0;
5449 int i;
5450
5451 if (wp->w_p_cuc)
5452 rightmost_vcol = wp->w_virtcol;
5453 if (draw_color_col)
5454 /* determine rightmost colorcolumn to possibly draw */
5455 for (i = 0; color_cols[i] >= 0; ++i)
5456 if (rightmost_vcol < color_cols[i])
5457 rightmost_vcol = color_cols[i];
5458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459 while (col < W_WIDTH(wp))
5460 {
5461 ScreenLines[off] = ' ';
5462#ifdef FEAT_MBYTE
5463 if (enc_utf8)
5464 ScreenLinesUC[off] = 0;
5465#endif
5466 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005467 if (draw_color_col)
5468 draw_color_col = advance_color_col(VCOL_HLC,
5469 &color_cols);
5470
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005471 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005472 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005473 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005474 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005475 else
5476 ScreenAttrs[off++] = 0;
5477
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005478 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005479 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005480
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005481 ++vcol;
5482 }
5483 }
5484#endif
5485
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005486 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005487 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 row++;
5489
5490 /*
5491 * Update w_cline_height and w_cline_folded if the cursor line was
5492 * updated (saves a call to plines() later).
5493 */
5494 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5495 {
5496 curwin->w_cline_row = startrow;
5497 curwin->w_cline_height = row - startrow;
5498#ifdef FEAT_FOLDING
5499 curwin->w_cline_folded = FALSE;
5500#endif
5501 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5502 }
5503
5504 break;
5505 }
5506
5507 /* line continues beyond line end */
5508 if (lcs_ext
5509 && !wp->w_p_wrap
5510#ifdef FEAT_DIFF
5511 && filler_todo <= 0
5512#endif
5513 && (
5514#ifdef FEAT_RIGHTLEFT
5515 wp->w_p_rl ? col == 0 :
5516#endif
5517 col == W_WIDTH(wp) - 1)
5518 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005519 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5521 {
5522 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005523 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#ifdef FEAT_MBYTE
5525 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005526 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005529 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005530 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532 else
5533 mb_utf8 = FALSE;
5534#endif
5535 }
5536
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005537#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005538 /* advance to the next 'colorcolumn' */
5539 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005540 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005541
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005542 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005543 * highlight the cursor position itself.
5544 * Also highlight the 'colorcolumn' if it is different than
5545 * 'cursorcolumn' */
5546 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005547 if (draw_state == WL_LINE && !lnum_in_visual_area
5548 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005549 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005550 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005551 && lnum != wp->w_cursor.lnum)
5552 {
5553 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005554 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005555 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005556 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005557 {
5558 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005559 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005561 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005562#endif
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 /*
5565 * Store character to be displayed.
5566 * Skip characters that are left of the screen for 'nowrap'.
5567 */
5568 vcol_prev = vcol;
5569 if (draw_state < WL_LINE || n_skip <= 0)
5570 {
5571 /*
5572 * Store the character.
5573 */
5574#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5575 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5576 {
5577 /* A double-wide character is: put first halve in left cell. */
5578 --off;
5579 --col;
5580 }
5581#endif
5582 ScreenLines[off] = c;
5583#ifdef FEAT_MBYTE
5584 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005585 {
5586 if ((mb_c & 0xff00) == 0x8e00)
5587 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 else if (enc_utf8)
5591 {
5592 if (mb_utf8)
5593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005594 int i;
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005597 if ((c & 0xff) == 0)
5598 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005599 for (i = 0; i < Screen_mco; ++i)
5600 {
5601 ScreenLinesC[i][off] = u8cc[i];
5602 if (u8cc[i] == 0)
5603 break;
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606 else
5607 ScreenLinesUC[off] = 0;
5608 }
5609 if (multi_attr)
5610 {
5611 ScreenAttrs[off] = multi_attr;
5612 multi_attr = 0;
5613 }
5614 else
5615#endif
5616 ScreenAttrs[off] = char_attr;
5617
5618#ifdef FEAT_MBYTE
5619 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5620 {
5621 /* Need to fill two screen columns. */
5622 ++off;
5623 ++col;
5624 if (enc_utf8)
5625 /* UTF-8: Put a 0 in the second screen char. */
5626 ScreenLines[off] = 0;
5627 else
5628 /* DBCS: Put second byte in the second screen char. */
5629 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005630 if (draw_state > WL_NR
5631#ifdef FEAT_DIFF
5632 && filler_todo <= 0
5633#endif
5634 )
5635 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 /* When "tocol" is halfway a character, set it to the end of
5637 * the character, otherwise highlighting won't stop. */
5638 if (tocol == vcol)
5639 ++tocol;
5640#ifdef FEAT_RIGHTLEFT
5641 if (wp->w_p_rl)
5642 {
5643 /* now it's time to backup one cell */
5644 --off;
5645 --col;
5646 }
5647#endif
5648 }
5649#endif
5650#ifdef FEAT_RIGHTLEFT
5651 if (wp->w_p_rl)
5652 {
5653 --off;
5654 --col;
5655 }
5656 else
5657#endif
5658 {
5659 ++off;
5660 ++col;
5661 }
5662 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005663#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005664 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005665 {
5666 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005667 ++vcol_off;
5668 if (n_extra > 0)
5669 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005670 if (wp->w_p_wrap)
5671 {
5672 /*
5673 * Special voodoo required if 'wrap' is on.
5674 *
5675 * Advance the column indicator to force the line
5676 * drawing to wrap early. This will make the line
5677 * take up the same screen space when parts are concealed,
5678 * so that cursor line computations aren't messed up.
5679 *
5680 * To avoid the fictitious advance of 'col' causing
5681 * trailing junk to be written out of the screen line
5682 * we are building, 'boguscols' keeps track of the number
5683 * of bad columns we have advanced.
5684 */
5685 if (n_extra > 0)
5686 {
5687 vcol += n_extra;
5688# ifdef FEAT_RIGHTLEFT
5689 if (wp->w_p_rl)
5690 {
5691 col -= n_extra;
5692 boguscols -= n_extra;
5693 }
5694 else
5695# endif
5696 {
5697 col += n_extra;
5698 boguscols += n_extra;
5699 }
5700 n_extra = 0;
5701 n_attr = 0;
5702 }
5703
5704
5705# ifdef FEAT_MBYTE
5706 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5707 {
5708 /* Need to fill two screen columns. */
5709# ifdef FEAT_RIGHTLEFT
5710 if (wp->w_p_rl)
5711 {
5712 --boguscols;
5713 --col;
5714 }
5715 else
5716# endif
5717 {
5718 ++boguscols;
5719 ++col;
5720 }
5721 }
5722# endif
5723
5724# ifdef FEAT_RIGHTLEFT
5725 if (wp->w_p_rl)
5726 {
5727 --boguscols;
5728 --col;
5729 }
5730 else
5731# endif
5732 {
5733 ++boguscols;
5734 ++col;
5735 }
5736 }
5737 else
5738 {
5739 if (n_extra > 0)
5740 {
5741 vcol += n_extra;
5742 n_extra = 0;
5743 n_attr = 0;
5744 }
5745 }
5746
5747 }
5748#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 else
5750 --n_skip;
5751
Bram Moolenaar64486672010-05-16 15:46:46 +02005752 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5753 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005754 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#ifdef FEAT_DIFF
5756 && filler_todo <= 0
5757#endif
5758 )
5759 ++vcol;
5760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005761#ifdef FEAT_SYN_HL
5762 if (vcol_save_attr >= 0)
5763 char_attr = vcol_save_attr;
5764#endif
5765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 /* restore attributes after "predeces" in 'listchars' */
5767 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5768 char_attr = saved_attr3;
5769
5770 /* restore attributes after last 'listchars' or 'number' char */
5771 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5772 char_attr = saved_attr2;
5773
5774 /*
5775 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005776 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 */
5778 if ((
5779#ifdef FEAT_RIGHTLEFT
5780 wp->w_p_rl ? (col < 0) :
5781#endif
5782 (col >= W_WIDTH(wp)))
5783 && (*ptr != NUL
5784#ifdef FEAT_DIFF
5785 || filler_todo > 0
5786#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005787 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5789 )
5790 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005791#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005792 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005793 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005794 boguscols = 0;
5795#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005796 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005797 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 ++row;
5800 ++screen_row;
5801
5802 /* When not wrapping and finished diff lines, or when displayed
5803 * '$' and highlighting until last column, break here. */
5804 if ((!wp->w_p_wrap
5805#ifdef FEAT_DIFF
5806 && filler_todo <= 0
5807#endif
5808 ) || lcs_eol_one == -1)
5809 break;
5810
5811 /* When the window is too narrow draw all "@" lines. */
5812 if (draw_state != WL_LINE
5813#ifdef FEAT_DIFF
5814 && filler_todo <= 0
5815#endif
5816 )
5817 {
5818 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005819#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 draw_vsep_win(wp, row);
5821#endif
5822 row = endrow;
5823 }
5824
5825 /* When line got too long for screen break here. */
5826 if (row == endrow)
5827 {
5828 ++row;
5829 break;
5830 }
5831
5832 if (screen_cur_row == screen_row - 1
5833#ifdef FEAT_DIFF
5834 && filler_todo <= 0
5835#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005836#ifdef FEAT_WINDOWS
5837 && W_WIDTH(wp) == Columns
5838#endif
5839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
5841 /* Remember that the line wraps, used for modeless copy. */
5842 LineWraps[screen_row - 1] = TRUE;
5843
5844 /*
5845 * Special trick to make copy/paste of wrapped lines work with
5846 * xterm/screen: write an extra character beyond the end of
5847 * the line. This will work with all terminal types
5848 * (regardless of the xn,am settings).
5849 * Only do this on a fast tty.
5850 * Only do this if the cursor is on the current line
5851 * (something has been written in it).
5852 * Don't do this for the GUI.
5853 * Don't do this for double-width characters.
5854 * Don't do this for a window not at the right screen border.
5855 */
5856 if (p_tf
5857#ifdef FEAT_GUI
5858 && !gui.in_use
5859#endif
5860#ifdef FEAT_MBYTE
5861 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005862 && ((*mb_off2cells)(LineOffset[screen_row],
5863 LineOffset[screen_row] + screen_Columns)
5864 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005866 + (int)Columns - 2,
5867 LineOffset[screen_row] + screen_Columns)
5868 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#endif
5870 )
5871 {
5872 /* First make sure we are at the end of the screen line,
5873 * then output the same character again to let the
5874 * terminal know about the wrap. If the terminal doesn't
5875 * auto-wrap, we overwrite the character. */
5876 if (screen_cur_col != W_WIDTH(wp))
5877 screen_char(LineOffset[screen_row - 1]
5878 + (unsigned)Columns - 1,
5879 screen_row - 1, (int)(Columns - 1));
5880
5881#ifdef FEAT_MBYTE
5882 /* When there is a multi-byte character, just output a
5883 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005884 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5885 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 out_char(' ');
5887 else
5888#endif
5889 out_char(ScreenLines[LineOffset[screen_row - 1]
5890 + (Columns - 1)]);
5891 /* force a redraw of the first char on the next line */
5892 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5893 screen_start(); /* don't know where cursor is now */
5894 }
5895 }
5896
5897 col = 0;
5898 off = (unsigned)(current_ScreenLine - ScreenLines);
5899#ifdef FEAT_RIGHTLEFT
5900 if (wp->w_p_rl)
5901 {
5902 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5903 off += col;
5904 }
5905#endif
5906
5907 /* reset the drawing state for the start of a wrapped line */
5908 draw_state = WL_START;
5909 saved_n_extra = n_extra;
5910 saved_p_extra = p_extra;
5911 saved_c_extra = c_extra;
5912 saved_char_attr = char_attr;
5913 n_extra = 0;
5914 lcs_prec_todo = lcs_prec;
5915#ifdef FEAT_LINEBREAK
5916# ifdef FEAT_DIFF
5917 if (filler_todo <= 0)
5918# endif
5919 need_showbreak = TRUE;
5920#endif
5921#ifdef FEAT_DIFF
5922 --filler_todo;
5923 /* When the filler lines are actually below the last line of the
5924 * file, don't draw the line itself, break here. */
5925 if (filler_todo == 0 && wp->w_botfill)
5926 break;
5927#endif
5928 }
5929
5930 } /* for every character in the line */
5931
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005932#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005933 /* After an empty line check first word for capital. */
5934 if (*skipwhite(line) == NUL)
5935 {
5936 capcol_lnum = lnum + 1;
5937 cap_col = 0;
5938 }
5939#endif
5940
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005941 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 return row;
5943}
5944
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005945#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005946static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005947
5948/*
5949 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005950 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005951 */
5952 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005953comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005954{
5955 int i;
5956
5957 for (i = 0; i < Screen_mco; ++i)
5958 {
5959 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5960 return TRUE;
5961 if (ScreenLinesC[i][off_from] == 0)
5962 break;
5963 }
5964 return FALSE;
5965}
5966#endif
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968/*
5969 * Check whether the given character needs redrawing:
5970 * - the (first byte of the) character is different
5971 * - the attributes are different
5972 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005973 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 */
5975 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005976char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 if (cols > 0
5979 && ((ScreenLines[off_from] != ScreenLines[off_to]
5980 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5981
5982#ifdef FEAT_MBYTE
5983 || (enc_dbcs != 0
5984 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5985 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5986 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5987 : (cols > 1 && ScreenLines[off_from + 1]
5988 != ScreenLines[off_to + 1])))
5989 || (enc_utf8
5990 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5991 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005992 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005993 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5994 && ScreenLines[off_from + 1]
5995 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#endif
5997 ))
5998 return TRUE;
5999 return FALSE;
6000}
6001
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006002#if defined(FEAT_TERMINAL) || defined(PROTO)
6003/*
6004 * Return the index in ScreenLines[] for the current screen line.
6005 */
6006 int
6007screen_get_current_line_off()
6008{
6009 return (int)(current_ScreenLine - ScreenLines);
6010}
6011#endif
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013/*
6014 * Move one "cooked" screen line to the screen, but only the characters that
6015 * have actually changed. Handle insert/delete character.
6016 * "coloff" gives the first column on the screen for this line.
6017 * "endcol" gives the columns where valid characters are.
6018 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6019 * needs to be cleared, negative otherwise.
6020 * "rlflag" is TRUE in a rightleft window:
6021 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6022 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6023 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006024 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006025screen_line(
6026 int row,
6027 int coloff,
6028 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006029 int clear_width,
6030 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 unsigned off_from;
6033 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006034#ifdef FEAT_MBYTE
6035 unsigned max_off_from;
6036 unsigned max_off_to;
6037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006039#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 int hl;
6041#endif
6042 int force = FALSE; /* force update rest of the line */
6043 int redraw_this /* bool: does character need redraw? */
6044#ifdef FEAT_GUI
6045 = TRUE /* For GUI when while-loop empty */
6046#endif
6047 ;
6048 int redraw_next; /* redraw_this for next character */
6049#ifdef FEAT_MBYTE
6050 int clear_next = FALSE;
6051 int char_cells; /* 1: normal char */
6052 /* 2: occupies two display cells */
6053# define CHAR_CELLS char_cells
6054#else
6055# define CHAR_CELLS 1
6056#endif
6057
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006058 /* Check for illegal row and col, just in case. */
6059 if (row >= Rows)
6060 row = Rows - 1;
6061 if (endcol > Columns)
6062 endcol = Columns;
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064# ifdef FEAT_CLIPBOARD
6065 clip_may_clear_selection(row, row);
6066# endif
6067
6068 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6069 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006070#ifdef FEAT_MBYTE
6071 max_off_from = off_from + screen_Columns;
6072 max_off_to = LineOffset[row] + screen_Columns;
6073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075#ifdef FEAT_RIGHTLEFT
6076 if (rlflag)
6077 {
6078 /* Clear rest first, because it's left of the text. */
6079 if (clear_width > 0)
6080 {
6081 while (col <= endcol && ScreenLines[off_to] == ' '
6082 && ScreenAttrs[off_to] == 0
6083# ifdef FEAT_MBYTE
6084 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6085# endif
6086 )
6087 {
6088 ++off_to;
6089 ++col;
6090 }
6091 if (col <= endcol)
6092 screen_fill(row, row + 1, col + coloff,
6093 endcol + coloff + 1, ' ', ' ', 0);
6094 }
6095 col = endcol + 1;
6096 off_to = LineOffset[row] + col + coloff;
6097 off_from += col;
6098 endcol = (clear_width > 0 ? clear_width : -clear_width);
6099 }
6100#endif /* FEAT_RIGHTLEFT */
6101
6102 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6103
6104 while (col < endcol)
6105 {
6106#ifdef FEAT_MBYTE
6107 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006108 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 else
6110 char_cells = 1;
6111#endif
6112
6113 redraw_this = redraw_next;
6114 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6115 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6116
6117#ifdef FEAT_GUI
6118 /* If the next character was bold, then redraw the current character to
6119 * remove any pixels that might have spilt over into us. This only
6120 * happens in the GUI.
6121 */
6122 if (redraw_next && gui.in_use)
6123 {
6124 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006125 if (hl > HL_ALL)
6126 hl = syn_attr2attr(hl);
6127 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 redraw_this = TRUE;
6129 }
6130#endif
6131
6132 if (redraw_this)
6133 {
6134 /*
6135 * Special handling when 'xs' termcap flag set (hpterm):
6136 * Attributes for characters are stored at the position where the
6137 * cursor is when writing the highlighting code. The
6138 * start-highlighting code must be written with the cursor on the
6139 * first highlighted character. The stop-highlighting code must
6140 * be written with the cursor just after the last highlighted
6141 * character.
6142 * Overwriting a character doesn't remove it's highlighting. Need
6143 * to clear the rest of the line, and force redrawing it
6144 * completely.
6145 */
6146 if ( p_wiv
6147 && !force
6148#ifdef FEAT_GUI
6149 && !gui.in_use
6150#endif
6151 && ScreenAttrs[off_to] != 0
6152 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6153 {
6154 /*
6155 * Need to remove highlighting attributes here.
6156 */
6157 windgoto(row, col + coloff);
6158 out_str(T_CE); /* clear rest of this screen line */
6159 screen_start(); /* don't know where cursor is now */
6160 force = TRUE; /* force redraw of rest of the line */
6161 redraw_next = TRUE; /* or else next char would miss out */
6162
6163 /*
6164 * If the previous character was highlighted, need to stop
6165 * highlighting at this character.
6166 */
6167 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6168 {
6169 screen_attr = ScreenAttrs[off_to - 1];
6170 term_windgoto(row, col + coloff);
6171 screen_stop_highlight();
6172 }
6173 else
6174 screen_attr = 0; /* highlighting has stopped */
6175 }
6176#ifdef FEAT_MBYTE
6177 if (enc_dbcs != 0)
6178 {
6179 /* Check if overwriting a double-byte with a single-byte or
6180 * the other way around requires another character to be
6181 * redrawn. For UTF-8 this isn't needed, because comparing
6182 * ScreenLinesUC[] is sufficient. */
6183 if (char_cells == 1
6184 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006185 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 /* Writing a single-cell character over a double-cell
6188 * character: need to redraw the next cell. */
6189 ScreenLines[off_to + 1] = 0;
6190 redraw_next = TRUE;
6191 }
6192 else if (char_cells == 2
6193 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006194 && (*mb_off2cells)(off_to, max_off_to) == 1
6195 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 /* Writing the second half of a double-cell character over
6198 * a double-cell character: need to redraw the second
6199 * cell. */
6200 ScreenLines[off_to + 2] = 0;
6201 redraw_next = TRUE;
6202 }
6203
6204 if (enc_dbcs == DBCS_JPNU)
6205 ScreenLines2[off_to] = ScreenLines2[off_from];
6206 }
6207 /* When writing a single-width character over a double-width
6208 * character and at the end of the redrawn text, need to clear out
6209 * the right halve of the old character.
6210 * Also required when writing the right halve of a double-width
6211 * char over the left halve of an existing one. */
6212 if (has_mbyte && col + char_cells == endcol
6213 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006214 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006216 && (*mb_off2cells)(off_to, max_off_to) == 1
6217 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 clear_next = TRUE;
6219#endif
6220
6221 ScreenLines[off_to] = ScreenLines[off_from];
6222#ifdef FEAT_MBYTE
6223 if (enc_utf8)
6224 {
6225 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6226 if (ScreenLinesUC[off_from] != 0)
6227 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006228 int i;
6229
6230 for (i = 0; i < Screen_mco; ++i)
6231 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
6233 }
6234 if (char_cells == 2)
6235 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6236#endif
6237
6238#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006239 /* The bold trick makes a single column of pixels appear in the
6240 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 * character should be redrawn too. This happens for our own GUI
6242 * and for some xterms. */
6243 if (
6244# ifdef FEAT_GUI
6245 gui.in_use
6246# endif
6247# if defined(FEAT_GUI) && defined(UNIX)
6248 ||
6249# endif
6250# ifdef UNIX
6251 term_is_xterm
6252# endif
6253 )
6254 {
6255 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006256 if (hl > HL_ALL)
6257 hl = syn_attr2attr(hl);
6258 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 redraw_next = TRUE;
6260 }
6261#endif
6262 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6263#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006264 /* For simplicity set the attributes of second half of a
6265 * double-wide character equal to the first half. */
6266 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006268
6269 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 else
6272#endif
6273 screen_char(off_to, row, col + coloff);
6274 }
6275 else if ( p_wiv
6276#ifdef FEAT_GUI
6277 && !gui.in_use
6278#endif
6279 && col + coloff > 0)
6280 {
6281 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6282 {
6283 /*
6284 * Don't output stop-highlight when moving the cursor, it will
6285 * stop the highlighting when it should continue.
6286 */
6287 screen_attr = 0;
6288 }
6289 else if (screen_attr != 0)
6290 screen_stop_highlight();
6291 }
6292
6293 off_to += CHAR_CELLS;
6294 off_from += CHAR_CELLS;
6295 col += CHAR_CELLS;
6296 }
6297
6298#ifdef FEAT_MBYTE
6299 if (clear_next)
6300 {
6301 /* Clear the second half of a double-wide character of which the left
6302 * half was overwritten with a single-wide character. */
6303 ScreenLines[off_to] = ' ';
6304 if (enc_utf8)
6305 ScreenLinesUC[off_to] = 0;
6306 screen_char(off_to, row, col + coloff);
6307 }
6308#endif
6309
6310 if (clear_width > 0
6311#ifdef FEAT_RIGHTLEFT
6312 && !rlflag
6313#endif
6314 )
6315 {
6316#ifdef FEAT_GUI
6317 int startCol = col;
6318#endif
6319
6320 /* blank out the rest of the line */
6321 while (col < clear_width && ScreenLines[off_to] == ' '
6322 && ScreenAttrs[off_to] == 0
6323#ifdef FEAT_MBYTE
6324 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6325#endif
6326 )
6327 {
6328 ++off_to;
6329 ++col;
6330 }
6331 if (col < clear_width)
6332 {
6333#ifdef FEAT_GUI
6334 /*
6335 * In the GUI, clearing the rest of the line may leave pixels
6336 * behind if the first character cleared was bold. Some bold
6337 * fonts spill over the left. In this case we redraw the previous
6338 * character too. If we didn't skip any blanks above, then we
6339 * only redraw if the character wasn't already redrawn anyway.
6340 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006341 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 hl = ScreenAttrs[off_to];
6344 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006345 {
6346 int prev_cells = 1;
6347# ifdef FEAT_MBYTE
6348 if (enc_utf8)
6349 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6350 * that its width is 2. */
6351 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6352 else if (enc_dbcs != 0)
6353 {
6354 /* find previous character by counting from first
6355 * column and get its width. */
6356 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006357 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006358
6359 while (off < off_to)
6360 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006361 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006362 off += prev_cells;
6363 }
6364 }
6365
6366 if (enc_dbcs != 0 && prev_cells > 1)
6367 screen_char_2(off_to - prev_cells, row,
6368 col + coloff - prev_cells);
6369 else
6370# endif
6371 screen_char(off_to - prev_cells, row,
6372 col + coloff - prev_cells);
6373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 }
6375#endif
6376 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6377 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006378#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 off_to += clear_width - col;
6380 col = clear_width;
6381#endif
6382 }
6383 }
6384
6385 if (clear_width > 0)
6386 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006387#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 /* For a window that's left of another, draw the separator char. */
6389 if (col + coloff < Columns)
6390 {
6391 int c;
6392
6393 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006394 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006396 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6397 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398# endif
6399 || ScreenAttrs[off_to] != hl)
6400 {
6401 ScreenLines[off_to] = c;
6402 ScreenAttrs[off_to] = hl;
6403# ifdef FEAT_MBYTE
6404 if (enc_utf8)
6405 {
6406 if (c >= 0x80)
6407 {
6408 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006409 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411 else
6412 ScreenLinesUC[off_to] = 0;
6413 }
6414# endif
6415 screen_char(off_to, row, col + coloff);
6416 }
6417 }
6418 else
6419#endif
6420 LineWraps[row] = FALSE;
6421 }
6422}
6423
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006424#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006426 * Mirror text "str" for right-left displaying.
6427 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006430rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 char_u *p1, *p2;
6433 int t;
6434
6435 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6436 {
6437 t = *p1;
6438 *p1 = *p2;
6439 *p2 = t;
6440 }
6441}
6442#endif
6443
6444#if defined(FEAT_WINDOWS) || defined(PROTO)
6445/*
6446 * mark all status lines for redraw; used after first :cd
6447 */
6448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006449status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 win_T *wp;
6452
Bram Moolenaar29323592016-07-24 22:04:11 +02006453 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (wp->w_status_height)
6455 {
6456 wp->w_redr_status = TRUE;
6457 redraw_later(VALID);
6458 }
6459}
6460
6461/*
6462 * mark all status lines of the current buffer for redraw
6463 */
6464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006465status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
6467 win_T *wp;
6468
Bram Moolenaar29323592016-07-24 22:04:11 +02006469 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6471 {
6472 wp->w_redr_status = TRUE;
6473 redraw_later(VALID);
6474 }
6475}
6476
6477/*
6478 * Redraw all status lines that need to be redrawn.
6479 */
6480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006481redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 win_T *wp;
6484
Bram Moolenaar29323592016-07-24 22:04:11 +02006485 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (wp->w_redr_status)
6487 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006488 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006489 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490}
6491#endif
6492
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006493#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494/*
6495 * Redraw all status lines at the bottom of frame "frp".
6496 */
6497 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006498win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 if (frp->fr_layout == FR_LEAF)
6501 frp->fr_win->w_redr_status = TRUE;
6502 else if (frp->fr_layout == FR_ROW)
6503 {
6504 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6505 win_redraw_last_status(frp);
6506 }
6507 else /* frp->fr_layout == FR_COL */
6508 {
6509 frp = frp->fr_child;
6510 while (frp->fr_next != NULL)
6511 frp = frp->fr_next;
6512 win_redraw_last_status(frp);
6513 }
6514}
6515#endif
6516
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006517#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * Draw the verticap separator right of window "wp" starting with line "row".
6520 */
6521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006522draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 int hl;
6525 int c;
6526
6527 if (wp->w_vsep_width)
6528 {
6529 /* draw the vertical separator right of this window */
6530 c = fillchar_vsep(&hl);
6531 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6532 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6533 c, ' ', hl);
6534 }
6535}
6536#endif
6537
6538#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006539static int status_match_len(expand_T *xp, char_u *s);
6540static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006543 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 */
6545 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006546status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 int len = 0;
6549
6550#ifdef FEAT_MENU
6551 int emenu = (xp->xp_context == EXPAND_MENUS
6552 || xp->xp_context == EXPAND_MENUNAMES);
6553
6554 /* Check for menu separators - replace with '|'. */
6555 if (emenu && menu_is_separator(s))
6556 return 1;
6557#endif
6558
6559 while (*s != NUL)
6560 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006561 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006562 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006563 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565
6566 return len;
6567}
6568
6569/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006570 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006571 * These are backslashes used for escaping. Do show backslashes in help tags.
6572 */
6573 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006574skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006575{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006576 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006577#ifdef FEAT_MENU
6578 || ((xp->xp_context == EXPAND_MENUS
6579 || xp->xp_context == EXPAND_MENUNAMES)
6580 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6581#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006582 )
6583 {
6584#ifndef BACKSLASH_IN_FILENAME
6585 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6586 return 2;
6587#endif
6588 return 1;
6589 }
6590 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006591}
6592
6593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 * Show wildchar matches in the status line.
6595 * Show at least the "match" item.
6596 * We start at item 'first_match' in the list and show all matches that fit.
6597 *
6598 * If inversion is possible we use it. Else '=' characters are used.
6599 */
6600 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006601win_redr_status_matches(
6602 expand_T *xp,
6603 int num_matches,
6604 char_u **matches, /* list of matches */
6605 int match,
6606 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6609 int row;
6610 char_u *buf;
6611 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006612 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 int fillchar;
6614 int attr;
6615 int i;
6616 int highlight = TRUE;
6617 char_u *selstart = NULL;
6618 int selstart_col = 0;
6619 char_u *selend = NULL;
6620 static int first_match = 0;
6621 int add_left = FALSE;
6622 char_u *s;
6623#ifdef FEAT_MENU
6624 int emenu;
6625#endif
6626#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6627 int l;
6628#endif
6629
6630 if (matches == NULL) /* interrupted completion? */
6631 return;
6632
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006633#ifdef FEAT_MBYTE
6634 if (has_mbyte)
6635 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6636 else
6637#endif
6638 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (buf == NULL)
6640 return;
6641
6642 if (match == -1) /* don't show match but original text */
6643 {
6644 match = 0;
6645 highlight = FALSE;
6646 }
6647 /* count 1 for the ending ">" */
6648 clen = status_match_len(xp, L_MATCH(match)) + 3;
6649 if (match == 0)
6650 first_match = 0;
6651 else if (match < first_match)
6652 {
6653 /* jumping left, as far as we can go */
6654 first_match = match;
6655 add_left = TRUE;
6656 }
6657 else
6658 {
6659 /* check if match fits on the screen */
6660 for (i = first_match; i < match; ++i)
6661 clen += status_match_len(xp, L_MATCH(i)) + 2;
6662 if (first_match > 0)
6663 clen += 2;
6664 /* jumping right, put match at the left */
6665 if ((long)clen > Columns)
6666 {
6667 first_match = match;
6668 /* if showing the last match, we can add some on the left */
6669 clen = 2;
6670 for (i = match; i < num_matches; ++i)
6671 {
6672 clen += status_match_len(xp, L_MATCH(i)) + 2;
6673 if ((long)clen >= Columns)
6674 break;
6675 }
6676 if (i == num_matches)
6677 add_left = TRUE;
6678 }
6679 }
6680 if (add_left)
6681 while (first_match > 0)
6682 {
6683 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6684 if ((long)clen >= Columns)
6685 break;
6686 --first_match;
6687 }
6688
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006689 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (first_match == 0)
6692 {
6693 *buf = NUL;
6694 len = 0;
6695 }
6696 else
6697 {
6698 STRCPY(buf, "< ");
6699 len = 2;
6700 }
6701 clen = len;
6702
6703 i = first_match;
6704 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6705 {
6706 if (i == match)
6707 {
6708 selstart = buf + len;
6709 selstart_col = clen;
6710 }
6711
6712 s = L_MATCH(i);
6713 /* Check for menu separators - replace with '|' */
6714#ifdef FEAT_MENU
6715 emenu = (xp->xp_context == EXPAND_MENUS
6716 || xp->xp_context == EXPAND_MENUNAMES);
6717 if (emenu && menu_is_separator(s))
6718 {
6719 STRCPY(buf + len, transchar('|'));
6720 l = (int)STRLEN(buf + len);
6721 len += l;
6722 clen += l;
6723 }
6724 else
6725#endif
6726 for ( ; *s != NUL; ++s)
6727 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006728 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 clen += ptr2cells(s);
6730#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006731 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
6733 STRNCPY(buf + len, s, l);
6734 s += l - 1;
6735 len += l;
6736 }
6737 else
6738#endif
6739 {
6740 STRCPY(buf + len, transchar_byte(*s));
6741 len += (int)STRLEN(buf + len);
6742 }
6743 }
6744 if (i == match)
6745 selend = buf + len;
6746
6747 *(buf + len++) = ' ';
6748 *(buf + len++) = ' ';
6749 clen += 2;
6750 if (++i == num_matches)
6751 break;
6752 }
6753
6754 if (i != num_matches)
6755 {
6756 *(buf + len++) = '>';
6757 ++clen;
6758 }
6759
6760 buf[len] = NUL;
6761
6762 row = cmdline_row - 1;
6763 if (row >= 0)
6764 {
6765 if (wild_menu_showing == 0)
6766 {
6767 if (msg_scrolled > 0)
6768 {
6769 /* Put the wildmenu just above the command line. If there is
6770 * no room, scroll the screen one line up. */
6771 if (cmdline_row == Rows - 1)
6772 {
6773 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6774 ++msg_scrolled;
6775 }
6776 else
6777 {
6778 ++cmdline_row;
6779 ++row;
6780 }
6781 wild_menu_showing = WM_SCROLLED;
6782 }
6783 else
6784 {
6785 /* Create status line if needed by setting 'laststatus' to 2.
6786 * Set 'winminheight' to zero to avoid that the window is
6787 * resized. */
6788 if (lastwin->w_status_height == 0)
6789 {
6790 save_p_ls = p_ls;
6791 save_p_wmh = p_wmh;
6792 p_ls = 2;
6793 p_wmh = 0;
6794 last_status(FALSE);
6795 }
6796 wild_menu_showing = WM_SHOWN;
6797 }
6798 }
6799
6800 screen_puts(buf, row, 0, attr);
6801 if (selstart != NULL && highlight)
6802 {
6803 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006804 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806
6807 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6808 }
6809
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006810#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 win_redraw_last_status(topframe);
6812#else
6813 lastwin->w_redr_status = TRUE;
6814#endif
6815 vim_free(buf);
6816}
6817#endif
6818
6819#if defined(FEAT_WINDOWS) || defined(PROTO)
6820/*
6821 * Redraw the status line of window wp.
6822 *
6823 * If inversion is possible we use it. Else '=' characters are used.
6824 */
6825 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006826win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 int row;
6829 char_u *p;
6830 int len;
6831 int fillchar;
6832 int attr;
6833 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006834 static int busy = FALSE;
6835
6836 /* It's possible to get here recursively when 'statusline' (indirectly)
6837 * invokes ":redrawstatus". Simply ignore the call then. */
6838 if (busy)
6839 return;
6840 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
6842 wp->w_redr_status = FALSE;
6843 if (wp->w_status_height == 0)
6844 {
6845 /* no status line, can only be last window */
6846 redraw_cmdline = TRUE;
6847 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006848 else if (!redrawing()
6849#ifdef FEAT_INS_EXPAND
6850 /* don't update status line when popup menu is visible and may be
6851 * drawn over it */
6852 || pum_visible()
6853#endif
6854 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
6856 /* Don't redraw right now, do it later. */
6857 wp->w_redr_status = TRUE;
6858 }
6859#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006860 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 {
6862 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006863 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 }
6865#endif
6866 else
6867 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006868 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006870 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 p = NameBuff;
6872 len = (int)STRLEN(p);
6873
Bram Moolenaard85f2712017-07-28 21:51:57 +02006874 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875#ifdef FEAT_QUICKFIX
6876 || wp->w_p_pvw
6877#endif
6878 || bufIsChanged(wp->w_buffer)
6879 || wp->w_buffer->b_p_ro)
6880 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006881 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006883 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 len += (int)STRLEN(p + len);
6885 }
6886#ifdef FEAT_QUICKFIX
6887 if (wp->w_p_pvw)
6888 {
6889 STRCPY(p + len, _("[Preview]"));
6890 len += (int)STRLEN(p + len);
6891 }
6892#endif
6893 if (bufIsChanged(wp->w_buffer))
6894 {
6895 STRCPY(p + len, "[+]");
6896 len += 3;
6897 }
6898 if (wp->w_buffer->b_p_ro)
6899 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006900 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006901 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6905 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6906 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6907 if (this_ru_col <= 1)
6908 {
6909 p = (char_u *)"<"; /* No room for file name! */
6910 len = 1;
6911 }
6912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913#ifdef FEAT_MBYTE
6914 if (has_mbyte)
6915 {
6916 int clen = 0, i;
6917
6918 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006919 clen = mb_string2cells(p, -1);
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 /* Find first character that will fit.
6922 * Going from start to end is much faster for DBCS. */
6923 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006924 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 clen -= (*mb_ptr2cells)(p + i);
6926 len = clen;
6927 if (i > 0)
6928 {
6929 p = p + i - 1;
6930 *p = '<';
6931 ++len;
6932 }
6933
6934 }
6935 else
6936#endif
6937 if (len > this_ru_col - 1)
6938 {
6939 p += len - (this_ru_col - 1);
6940 *p = '<';
6941 len = this_ru_col - 1;
6942 }
6943
6944 row = W_WINROW(wp) + wp->w_height;
6945 screen_puts(p, row, W_WINCOL(wp), attr);
6946 screen_fill(row, row + 1, len + W_WINCOL(wp),
6947 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6948
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006949 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6951 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6952 - 1 + W_WINCOL(wp)), attr);
6953
6954#ifdef FEAT_CMDL_INFO
6955 win_redr_ruler(wp, TRUE);
6956#endif
6957 }
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 /*
6960 * May need to draw the character below the vertical separator.
6961 */
6962 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6963 {
6964 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006965 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else
6967 fillchar = fillchar_vsep(&attr);
6968 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6969 attr);
6970 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006971 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972}
6973
Bram Moolenaar238a5642006-02-21 22:12:05 +00006974#ifdef FEAT_STL_OPT
6975/*
6976 * Redraw the status line according to 'statusline' and take care of any
6977 * errors encountered.
6978 */
6979 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006980redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006981{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006982 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006983 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006984
6985 /* When called recursively return. This can happen when the statusline
6986 * contains an expression that triggers a redraw. */
6987 if (entered)
6988 return;
6989 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006990
Bram Moolenaara742e082016-04-05 21:10:38 +02006991 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006992 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006993 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006994 {
6995 /* When there is an error disable the statusline, otherwise the
6996 * display is messed up with errors and a redraw triggers the problem
6997 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006998 set_string_option_direct((char_u *)"statusline", -1,
6999 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007000 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007001 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007002 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007003 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007004}
7005#endif
7006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007/*
7008 * Return TRUE if the status line of window "wp" is connected to the status
7009 * line of the window right of it. If not, then it's a vertical separator.
7010 * Only call if (wp->w_vsep_width != 0).
7011 */
7012 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007013stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 frame_T *fr;
7016
7017 fr = wp->w_frame;
7018 while (fr->fr_parent != NULL)
7019 {
7020 if (fr->fr_parent->fr_layout == FR_COL)
7021 {
7022 if (fr->fr_next != NULL)
7023 break;
7024 }
7025 else
7026 {
7027 if (fr->fr_next != NULL)
7028 return TRUE;
7029 }
7030 fr = fr->fr_parent;
7031 }
7032 return FALSE;
7033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035#endif /* FEAT_WINDOWS */
7036
7037#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7038/*
7039 * Get the value to show for the language mappings, active 'keymap'.
7040 */
7041 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007042get_keymap_str(
7043 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007044 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007045 char_u *buf, /* buffer for the result */
7046 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 char_u *p;
7049
7050 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7051 return FALSE;
7052
7053 {
7054#ifdef FEAT_EVAL
7055 buf_T *old_curbuf = curbuf;
7056 win_T *old_curwin = curwin;
7057 char_u *s;
7058
7059 curbuf = wp->w_buffer;
7060 curwin = wp;
7061 STRCPY(buf, "b:keymap_name"); /* must be writable */
7062 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007063 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 --emsg_skip;
7065 curbuf = old_curbuf;
7066 curwin = old_curwin;
7067 if (p == NULL || *p == NUL)
7068#endif
7069 {
7070#ifdef FEAT_KEYMAP
7071 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7072 p = wp->w_buffer->b_p_keymap;
7073 else
7074#endif
7075 p = (char_u *)"lang";
7076 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007077 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 buf[0] = NUL;
7079#ifdef FEAT_EVAL
7080 vim_free(s);
7081#endif
7082 }
7083 return buf[0] != NUL;
7084}
7085#endif
7086
7087#if defined(FEAT_STL_OPT) || defined(PROTO)
7088/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007089 * Redraw the status line or ruler of window "wp".
7090 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 */
7092 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007093win_redr_custom(
7094 win_T *wp,
7095 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007097 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 int attr;
7099 int curattr;
7100 int row;
7101 int col = 0;
7102 int maxwidth;
7103 int width;
7104 int n;
7105 int len;
7106 int fillchar;
7107 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007108 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007110 struct stl_hlrec hltab[STL_MAX_ITEM];
7111 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007112 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007113 win_T *ewp;
7114 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
Bram Moolenaar1d633412013-12-11 15:52:01 +01007116 /* There is a tiny chance that this gets called recursively: When
7117 * redrawing a status line triggers redrawing the ruler or tabline.
7118 * Avoid trouble by not allowing recursion. */
7119 if (entered)
7120 return;
7121 entered = TRUE;
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007124 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007126 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007127 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007128 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007129 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007130 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007131 maxwidth = Columns;
7132# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007133 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007136 else
7137 {
7138 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007139 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140 maxwidth = W_WIDTH(wp);
7141
7142 if (draw_ruler)
7143 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007146 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007147 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007148 if (*++stl == '-')
7149 stl++;
7150 if (atoi((char *)stl))
7151 while (VIM_ISDIGIT(*stl))
7152 stl++;
7153 if (*stl++ != '(')
7154 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007155 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007156#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007157 col = ru_col - (Columns - W_WIDTH(wp));
7158 if (col < (W_WIDTH(wp) + 1) / 2)
7159 col = (W_WIDTH(wp) + 1) / 2;
7160#else
7161 col = ru_col;
7162 if (col > (Columns + 1) / 2)
7163 col = (Columns + 1) / 2;
7164#endif
7165 maxwidth = W_WIDTH(wp) - col;
7166#ifdef FEAT_WINDOWS
7167 if (!wp->w_status_height)
7168#endif
7169 {
7170 row = Rows - 1;
7171 --maxwidth; /* writing in last column may cause scrolling */
7172 fillchar = ' ';
7173 attr = 0;
7174 }
7175
7176# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007177 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178# endif
7179 }
7180 else
7181 {
7182 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007185 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007187 use_sandbox = was_set_insecurely((char_u *)"statusline",
7188 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189# endif
7190 }
7191
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007192#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 col += W_WINCOL(wp);
7194#endif
7195 }
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007198 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaar61452852011-02-01 18:01:11 +01007200 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7201 * the cursor away and back. */
7202 ewp = wp == NULL ? curwin : wp;
7203 p_crb_save = ewp->w_p_crb;
7204 ewp->w_p_crb = FALSE;
7205
Bram Moolenaar362f3562009-11-03 16:20:34 +00007206 /* Make a copy, because the statusline may include a function call that
7207 * might change the option value and free the memory. */
7208 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007209 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007210 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007211 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007212 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007213 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007215 /* Make all characters printable. */
7216 p = transstr(buf);
7217 if (p != NULL)
7218 {
7219 vim_strncpy(buf, p, sizeof(buf) - 1);
7220 vim_free(p);
7221 }
7222
7223 /* fill up with "fillchar" */
7224 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007225 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
7227#ifdef FEAT_MBYTE
7228 len += (*mb_char2bytes)(fillchar, buf + len);
7229#else
7230 buf[len++] = fillchar;
7231#endif
7232 ++width;
7233 }
7234 buf[len] = NUL;
7235
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 /*
7237 * Draw each snippet with the specified highlighting.
7238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 curattr = attr;
7240 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007241 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 screen_puts_len(p, len, row, col, curattr);
7245 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007246 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007248 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 else if (hltab[n].userhl < 0)
7251 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007253 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255#endif
7256 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007260
7261 if (wp == NULL)
7262 {
7263 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7264 col = 0;
7265 len = 0;
7266 p = buf;
7267 fillchar = 0;
7268 for (n = 0; tabtab[n].start != NULL; n++)
7269 {
7270 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7271 while (col < len)
7272 TabPageIdxs[col++] = fillchar;
7273 p = tabtab[n].start;
7274 fillchar = tabtab[n].userhl;
7275 }
7276 while (col < Columns)
7277 TabPageIdxs[col++] = fillchar;
7278 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007279
7280theend:
7281 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282}
7283
7284#endif /* FEAT_STL_OPT */
7285
7286/*
7287 * Output a single character directly to the screen and update ScreenLines.
7288 */
7289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007290screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 char_u buf[MB_MAXBYTES + 1];
7293
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007294#ifdef FEAT_MBYTE
7295 if (has_mbyte)
7296 buf[(*mb_char2bytes)(c, buf)] = NUL;
7297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007299 {
7300 buf[0] = c;
7301 buf[1] = NUL;
7302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 screen_puts(buf, row, col, attr);
7304}
7305
7306/*
7307 * Get a single character directly from ScreenLines into "bytes[]".
7308 * Also return its attribute in *attrp;
7309 */
7310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007311screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
7313 unsigned off;
7314
7315 /* safety check */
7316 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7317 {
7318 off = LineOffset[row] + col;
7319 *attrp = ScreenAttrs[off];
7320 bytes[0] = ScreenLines[off];
7321 bytes[1] = NUL;
7322
7323#ifdef FEAT_MBYTE
7324 if (enc_utf8 && ScreenLinesUC[off] != 0)
7325 bytes[utfc_char2bytes(off, bytes)] = NUL;
7326 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7327 {
7328 bytes[0] = ScreenLines[off];
7329 bytes[1] = ScreenLines2[off];
7330 bytes[2] = NUL;
7331 }
7332 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7333 {
7334 bytes[1] = ScreenLines[off + 1];
7335 bytes[2] = NUL;
7336 }
7337#endif
7338 }
7339}
7340
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007341#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007342static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007343
7344/*
7345 * Return TRUE if composing characters for screen posn "off" differs from
7346 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007347 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007348 */
7349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007350screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007351{
7352 int i;
7353
7354 for (i = 0; i < Screen_mco; ++i)
7355 {
7356 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7357 return TRUE;
7358 if (u8cc[i] == 0)
7359 break;
7360 }
7361 return FALSE;
7362}
7363#endif
7364
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365/*
7366 * Put string '*text' on the screen at position 'row' and 'col', with
7367 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7368 * Note: only outputs within one row, message is truncated at screen boundary!
7369 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7370 */
7371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007372screen_puts(
7373 char_u *text,
7374 int row,
7375 int col,
7376 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 screen_puts_len(text, -1, row, col, attr);
7379}
7380
7381/*
7382 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7383 * a NUL.
7384 */
7385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007386screen_puts_len(
7387 char_u *text,
7388 int textlen,
7389 int row,
7390 int col,
7391 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392{
7393 unsigned off;
7394 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007395 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int c;
7397#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007398 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 int mbyte_blen = 1;
7400 int mbyte_cells = 1;
7401 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int clear_next_cell = FALSE;
7404# ifdef FEAT_ARABIC
7405 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007406 int pc, nc, nc1;
7407 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408# endif
7409#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007410#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7411 int force_redraw_this;
7412 int force_redraw_next = FALSE;
7413#endif
7414 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7417 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007418 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaarc236c162008-07-13 17:41:49 +00007420#ifdef FEAT_MBYTE
7421 /* When drawing over the right halve of a double-wide char clear out the
7422 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007423 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007424# ifdef FEAT_GUI
7425 && !gui.in_use
7426# endif
7427 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007428 {
7429 ScreenLines[off - 1] = ' ';
7430 ScreenAttrs[off - 1] = 0;
7431 if (enc_utf8)
7432 {
7433 ScreenLinesUC[off - 1] = 0;
7434 ScreenLinesC[0][off - 1] = 0;
7435 }
7436 /* redraw the previous cell, make it empty */
7437 screen_char(off - 1, row, col - 1);
7438 /* force the cell at "col" to be redrawn */
7439 force_redraw_next = TRUE;
7440 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007441#endif
7442
Bram Moolenaar367329b2007-08-30 11:53:22 +00007443#ifdef FEAT_MBYTE
7444 max_off = LineOffset[row] + screen_Columns;
7445#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007446 while (col < screen_Columns
7447 && (len < 0 || (int)(ptr - text) < len)
7448 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 {
7450 c = *ptr;
7451#ifdef FEAT_MBYTE
7452 /* check if this is the first byte of a multibyte */
7453 if (has_mbyte)
7454 {
7455 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007456 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7460 mbyte_cells = 1;
7461 else if (enc_dbcs != 0)
7462 mbyte_cells = mbyte_blen;
7463 else /* enc_utf8 */
7464 {
7465 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007466 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 (int)((text + len) - ptr));
7468 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007469 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007471# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 /* Non-BMP character: display as ? or fullwidth ?. */
7473 if (u8c >= 0x10000)
7474 {
7475 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7476 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007477 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480# ifdef FEAT_ARABIC
7481 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7482 {
7483 /* Do Arabic shaping. */
7484 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7485 {
7486 /* Past end of string to be displayed. */
7487 nc = NUL;
7488 nc1 = NUL;
7489 }
7490 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007492 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7493 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494 nc1 = pcc[0];
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 pc = prev_c;
7497 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007498 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500 else
7501 prev_c = u8c;
7502# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007503 if (col + mbyte_cells > screen_Columns)
7504 {
7505 /* Only 1 cell left, but character requires 2 cells:
7506 * display a '>' in the last column to avoid wrapping. */
7507 c = '>';
7508 mbyte_cells = 1;
7509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
7511 }
7512#endif
7513
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007514#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7515 force_redraw_this = force_redraw_next;
7516 force_redraw_next = FALSE;
7517#endif
7518
7519 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520#ifdef FEAT_MBYTE
7521 || (mbyte_cells == 2
7522 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7523 || (enc_dbcs == DBCS_JPNU
7524 && c == 0x8e
7525 && ScreenLines2[off] != ptr[1])
7526 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007527 && (ScreenLinesUC[off] !=
7528 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7529 || (ScreenLinesUC[off] != 0
7530 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531#endif
7532 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007533 || exmode_active;
7534
7535 if (need_redraw
7536#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7537 || force_redraw_this
7538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 )
7540 {
7541#if defined(FEAT_GUI) || defined(UNIX)
7542 /* The bold trick makes a single row of pixels appear in the next
7543 * character. When a bold character is removed, the next
7544 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007545 * and for some xterms. */
7546 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547# ifdef FEAT_GUI
7548 gui.in_use
7549# endif
7550# if defined(FEAT_GUI) && defined(UNIX)
7551 ||
7552# endif
7553# ifdef UNIX
7554 term_is_xterm
7555# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007558 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007560 if (n > HL_ALL)
7561 n = syn_attr2attr(n);
7562 if (n & HL_BOLD)
7563 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565#endif
7566#ifdef FEAT_MBYTE
7567 /* When at the end of the text and overwriting a two-cell
7568 * character with a one-cell character, need to clear the next
7569 * cell. Also when overwriting the left halve of a two-cell char
7570 * with the right halve of a two-cell char. Do this only once
7571 * (mb_off2cells() may return 2 on the right halve). */
7572 if (clear_next_cell)
7573 clear_next_cell = FALSE;
7574 else if (has_mbyte
7575 && (len < 0 ? ptr[mbyte_blen] == NUL
7576 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579 && (*mb_off2cells)(off, max_off) == 1
7580 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 clear_next_cell = TRUE;
7582
7583 /* Make sure we never leave a second byte of a double-byte behind,
7584 * it confuses mb_off2cells(). */
7585 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007586 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007588 && (*mb_off2cells)(off, max_off) == 1
7589 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 ScreenLines[off + mbyte_blen] = 0;
7591#endif
7592 ScreenLines[off] = c;
7593 ScreenAttrs[off] = attr;
7594#ifdef FEAT_MBYTE
7595 if (enc_utf8)
7596 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007597 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 ScreenLinesUC[off] = 0;
7599 else
7600 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007601 int i;
7602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 for (i = 0; i < Screen_mco; ++i)
7605 {
7606 ScreenLinesC[i][off] = u8cc[i];
7607 if (u8cc[i] == 0)
7608 break;
7609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611 if (mbyte_cells == 2)
7612 {
7613 ScreenLines[off + 1] = 0;
7614 ScreenAttrs[off + 1] = attr;
7615 }
7616 screen_char(off, row, col);
7617 }
7618 else if (mbyte_cells == 2)
7619 {
7620 ScreenLines[off + 1] = ptr[1];
7621 ScreenAttrs[off + 1] = attr;
7622 screen_char_2(off, row, col);
7623 }
7624 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7625 {
7626 ScreenLines2[off] = ptr[1];
7627 screen_char(off, row, col);
7628 }
7629 else
7630#endif
7631 screen_char(off, row, col);
7632 }
7633#ifdef FEAT_MBYTE
7634 if (has_mbyte)
7635 {
7636 off += mbyte_cells;
7637 col += mbyte_cells;
7638 ptr += mbyte_blen;
7639 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007640 {
7641 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007643 len = -1;
7644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646 else
7647#endif
7648 {
7649 ++off;
7650 ++col;
7651 ++ptr;
7652 }
7653 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007654
7655#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7656 /* If we detected the next character needs to be redrawn, but the text
7657 * doesn't extend up to there, update the character here. */
7658 if (force_redraw_next && col < screen_Columns)
7659 {
7660# ifdef FEAT_MBYTE
7661 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7662 screen_char_2(off, row, col);
7663 else
7664# endif
7665 screen_char(off, row, col);
7666 }
7667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668}
7669
7670#ifdef FEAT_SEARCH_EXTRA
7671/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007672 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 */
7674 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007675start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 if (p_hls && !no_hlsearch)
7678 {
7679 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007680 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007681# ifdef FEAT_RELTIME
7682 /* Set the time limit to 'redrawtime'. */
7683 profile_setlimit(p_rdt, &search_hl.tm);
7684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686}
7687
7688/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007689 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 */
7691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007692end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 if (search_hl.rm.regprog != NULL)
7695 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007696 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 search_hl.rm.regprog = NULL;
7698 }
7699}
7700
7701/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007702 * Init for calling prepare_search_hl().
7703 */
7704 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007705init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007706{
7707 matchitem_T *cur;
7708
7709 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7710 * match */
7711 cur = wp->w_match_head;
7712 while (cur != NULL)
7713 {
7714 cur->hl.rm = cur->match;
7715 if (cur->hlg_id == 0)
7716 cur->hl.attr = 0;
7717 else
7718 cur->hl.attr = syn_id2attr(cur->hlg_id);
7719 cur->hl.buf = wp->w_buffer;
7720 cur->hl.lnum = 0;
7721 cur->hl.first_lnum = 0;
7722# ifdef FEAT_RELTIME
7723 /* Set the time limit to 'redrawtime'. */
7724 profile_setlimit(p_rdt, &(cur->hl.tm));
7725# endif
7726 cur = cur->next;
7727 }
7728 search_hl.buf = wp->w_buffer;
7729 search_hl.lnum = 0;
7730 search_hl.first_lnum = 0;
7731 /* time limit is set at the toplevel, for all windows */
7732}
7733
7734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 * Advance to the match in window "wp" line "lnum" or past it.
7736 */
7737 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007738prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007740 matchitem_T *cur; /* points to the match list */
7741 match_T *shl; /* points to search_hl or a match */
7742 int shl_flag; /* flag to indicate whether search_hl
7743 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007744 int pos_inprogress; /* marks that position match search is
7745 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 int n;
7747
7748 /*
7749 * When using a multi-line pattern, start searching at the top
7750 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007751 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007753 cur = wp->w_match_head;
7754 shl_flag = FALSE;
7755 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 if (shl_flag == FALSE)
7758 {
7759 shl = &search_hl;
7760 shl_flag = TRUE;
7761 }
7762 else
7763 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (shl->rm.regprog != NULL
7765 && shl->lnum == 0
7766 && re_multiline(shl->rm.regprog))
7767 {
7768 if (shl->first_lnum == 0)
7769 {
7770# ifdef FEAT_FOLDING
7771 for (shl->first_lnum = lnum;
7772 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7773 if (hasFoldingWin(wp, shl->first_lnum - 1,
7774 NULL, NULL, TRUE, NULL))
7775 break;
7776# else
7777 shl->first_lnum = wp->w_topline;
7778# endif
7779 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780 if (cur != NULL)
7781 cur->pos.cur = 0;
7782 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007784 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7785 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007787 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7788 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007789 pos_inprogress = cur == NULL || cur->pos.cur == 0
7790 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 if (shl->lnum != 0)
7792 {
7793 shl->first_lnum = shl->lnum
7794 + shl->rm.endpos[0].lnum
7795 - shl->rm.startpos[0].lnum;
7796 n = shl->rm.endpos[0].col;
7797 }
7798 else
7799 {
7800 ++shl->first_lnum;
7801 n = 0;
7802 }
7803 }
7804 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007805 if (shl != &search_hl && cur != NULL)
7806 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 }
7808}
7809
7810/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007811 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 * Uses shl->buf.
7813 * Sets shl->lnum and shl->rm contents.
7814 * Note: Assumes a previous match is always before "lnum", unless
7815 * shl->lnum is zero.
7816 * Careful: Any pointers for buffer lines will become invalid.
7817 */
7818 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007819next_search_hl(
7820 win_T *win,
7821 match_T *shl, /* points to search_hl or a match */
7822 linenr_T lnum,
7823 colnr_T mincol, /* minimal column for a match */
7824 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 linenr_T l;
7827 colnr_T matchcol;
7828 long nmatched;
7829
7830 if (shl->lnum != 0)
7831 {
7832 /* Check for three situations:
7833 * 1. If the "lnum" is below a previous match, start a new search.
7834 * 2. If the previous match includes "mincol", use it.
7835 * 3. Continue after the previous match.
7836 */
7837 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7838 if (lnum > l)
7839 shl->lnum = 0;
7840 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7841 return;
7842 }
7843
7844 /*
7845 * Repeat searching for a match until one is found that includes "mincol"
7846 * or none is found in this line.
7847 */
7848 called_emsg = FALSE;
7849 for (;;)
7850 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007851#ifdef FEAT_RELTIME
7852 /* Stop searching after passing the time limit. */
7853 if (profile_passed_limit(&(shl->tm)))
7854 {
7855 shl->lnum = 0; /* no match found in time */
7856 break;
7857 }
7858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 /* Three situations:
7860 * 1. No useful previous match: search from start of line.
7861 * 2. Not Vi compatible or empty match: continue at next character.
7862 * Break the loop if this is beyond the end of the line.
7863 * 3. Vi compatible searching: continue at end of previous match.
7864 */
7865 if (shl->lnum == 0)
7866 matchcol = 0;
7867 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7868 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007869 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007871 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007872
7873 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007874 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007875 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007877 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 shl->lnum = 0;
7879 break;
7880 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007881#ifdef FEAT_MBYTE
7882 if (has_mbyte)
7883 matchcol += mb_ptr2len(ml);
7884 else
7885#endif
7886 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888 else
7889 matchcol = shl->rm.endpos[0].col;
7890
7891 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007894 /* Remember whether shl->rm is using a copy of the regprog in
7895 * cur->match. */
7896 int regprog_is_copy = (shl != &search_hl && cur != NULL
7897 && shl == &cur->hl
7898 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007899 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007900
Bram Moolenaarb3414592014-06-17 17:48:32 +02007901 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7902 matchcol,
7903#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007904 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007905#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007906 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007907#endif
7908 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007909 /* Copy the regprog, in case it got freed and recompiled. */
7910 if (regprog_is_copy)
7911 cur->match.regprog = cur->hl.rm.regprog;
7912
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007913 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007914 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915 /* Error while handling regexp: stop using this regexp. */
7916 if (shl == &search_hl)
7917 {
7918 /* don't free regprog in the match list, it's a copy */
7919 vim_regfree(shl->rm.regprog);
7920 SET_NO_HLSEARCH(TRUE);
7921 }
7922 shl->rm.regprog = NULL;
7923 shl->lnum = 0;
7924 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7925 message */
7926 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007927 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007928 }
7929 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007930 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007931 else
7932 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 if (nmatched == 0)
7934 {
7935 shl->lnum = 0; /* no match found */
7936 break;
7937 }
7938 if (shl->rm.startpos[0].lnum > 0
7939 || shl->rm.startpos[0].col >= mincol
7940 || nmatched > 1
7941 || shl->rm.endpos[0].col > mincol)
7942 {
7943 shl->lnum += shl->rm.startpos[0].lnum;
7944 break; /* useful match found */
7945 }
7946 }
7947}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
Bram Moolenaar85077472016-10-16 14:35:48 +02007949/*
7950 * If there is a match fill "shl" and return one.
7951 * Return zero otherwise.
7952 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007953 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007954next_search_hl_pos(
7955 match_T *shl, /* points to a match */
7956 linenr_T lnum,
7957 posmatch_T *posmatch, /* match positions */
7958 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959{
7960 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007961 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007962
Bram Moolenaarb3414592014-06-17 17:48:32 +02007963 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7964 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007965 llpos_T *pos = &posmatch->pos[i];
7966
7967 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007968 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007969 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007971 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007972 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007973 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007975 /* if this match comes before the one at "found" then swap
7976 * them */
7977 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007978 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007979 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007980
Bram Moolenaar85077472016-10-16 14:35:48 +02007981 *pos = posmatch->pos[found];
7982 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 }
7984 }
7985 else
Bram Moolenaar85077472016-10-16 14:35:48 +02007986 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 }
7988 }
7989 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02007990 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 {
Bram Moolenaar85077472016-10-16 14:35:48 +02007992 colnr_T start = posmatch->pos[found].col == 0
7993 ? 0 : posmatch->pos[found].col - 1;
7994 colnr_T end = posmatch->pos[found].col == 0
7995 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007996
Bram Moolenaar85077472016-10-16 14:35:48 +02007997 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 shl->rm.startpos[0].lnum = 0;
7999 shl->rm.startpos[0].col = start;
8000 shl->rm.endpos[0].lnum = 0;
8001 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008002 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 posmatch->cur = found + 1;
8004 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008006 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008007}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008008#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008009
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008011screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012{
8013 attrentry_T *aep = NULL;
8014
8015 screen_attr = attr;
8016 if (full_screen
8017#ifdef WIN3264
8018 && termcap_active
8019#endif
8020 )
8021 {
8022#ifdef FEAT_GUI
8023 if (gui.in_use)
8024 {
8025 char buf[20];
8026
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008027 /* The GUI handles this internally. */
8028 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 OUT_STR(buf);
8030 }
8031 else
8032#endif
8033 {
8034 if (attr > HL_ALL) /* special HL attr. */
8035 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 aep = syn_cterm_attr2entry(attr);
8038 else
8039 aep = syn_term_attr2entry(attr);
8040 if (aep == NULL) /* did ":syntax clear" */
8041 attr = 0;
8042 else
8043 attr = aep->ae_attr;
8044 }
8045 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8046 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008047 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008048#ifdef FEAT_TERMGUICOLORS
8049 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008050 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008051#endif
8052 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008053#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008054 )
8055#endif
8056 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008057 /* If the Normal FG color has BOLD attribute and the new HL
8058 * has a FG color defined, clear BOLD. */
8059 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8061 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008062 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8063 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 out_str(T_US);
8065 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8066 out_str(T_CZH);
8067 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8068 out_str(T_MR);
8069
8070 /*
8071 * Output the color or start string after bold etc., in case the
8072 * bold etc. override the color setting.
8073 */
8074 if (aep != NULL)
8075 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008076#ifdef FEAT_TERMGUICOLORS
8077 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008079 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008080 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008081 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008082 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008087 if (t_colors > 1)
8088 {
8089 if (aep->ae_u.cterm.fg_color)
8090 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8091 if (aep->ae_u.cterm.bg_color)
8092 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8093 }
8094 else
8095 {
8096 if (aep->ae_u.term.start != NULL)
8097 out_str(aep->ae_u.term.start);
8098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
8100 }
8101 }
8102 }
8103}
8104
8105 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008106screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107{
8108 int do_ME = FALSE; /* output T_ME code */
8109
8110 if (screen_attr != 0
8111#ifdef WIN3264
8112 && termcap_active
8113#endif
8114 )
8115 {
8116#ifdef FEAT_GUI
8117 if (gui.in_use)
8118 {
8119 char buf[20];
8120
8121 /* use internal GUI code */
8122 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8123 OUT_STR(buf);
8124 }
8125 else
8126#endif
8127 {
8128 if (screen_attr > HL_ALL) /* special HL attr. */
8129 {
8130 attrentry_T *aep;
8131
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008132 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {
8134 /*
8135 * Assume that t_me restores the original colors!
8136 */
8137 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008139#ifdef FEAT_TERMGUICOLORS
8140 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008141 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8142 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008143#endif
8144 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008145#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008146 )
8147#endif
8148 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 do_ME = TRUE;
8150 }
8151 else
8152 {
8153 aep = syn_term_attr2entry(screen_attr);
8154 if (aep != NULL && aep->ae_u.term.stop != NULL)
8155 {
8156 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8157 do_ME = TRUE;
8158 else
8159 out_str(aep->ae_u.term.stop);
8160 }
8161 }
8162 if (aep == NULL) /* did ":syntax clear" */
8163 screen_attr = 0;
8164 else
8165 screen_attr = aep->ae_attr;
8166 }
8167
8168 /*
8169 * Often all ending-codes are equal to T_ME. Avoid outputting the
8170 * same sequence several times.
8171 */
8172 if (screen_attr & HL_STANDOUT)
8173 {
8174 if (STRCMP(T_SE, T_ME) == 0)
8175 do_ME = TRUE;
8176 else
8177 out_str(T_SE);
8178 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008179 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 if (STRCMP(T_UE, T_ME) == 0)
8182 do_ME = TRUE;
8183 else
8184 out_str(T_UE);
8185 }
8186 if (screen_attr & HL_ITALIC)
8187 {
8188 if (STRCMP(T_CZR, T_ME) == 0)
8189 do_ME = TRUE;
8190 else
8191 out_str(T_CZR);
8192 }
8193 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8194 out_str(T_ME);
8195
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008196#ifdef FEAT_TERMGUICOLORS
8197 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008199 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008201 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008202 term_bg_rgb_color(cterm_normal_bg_gui_color);
8203 }
8204 else
8205#endif
8206 {
8207 if (t_colors > 1)
8208 {
8209 /* set Normal cterm colors */
8210 if (cterm_normal_fg_color != 0)
8211 term_fg_color(cterm_normal_fg_color - 1);
8212 if (cterm_normal_bg_color != 0)
8213 term_bg_color(cterm_normal_bg_color - 1);
8214 if (cterm_normal_fg_bold)
8215 out_str(T_MD);
8216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218 }
8219 }
8220 screen_attr = 0;
8221}
8222
8223/*
8224 * Reset the colors for a cterm. Used when leaving Vim.
8225 * The machine specific code may override this again.
8226 */
8227 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008228reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008230 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
8232 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008233#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008234 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8235 || cterm_normal_bg_gui_color != INVALCOLOR)
8236 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008237#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
8241 out_str(T_OP);
8242 screen_attr = -1;
8243 }
8244 if (cterm_normal_fg_bold)
8245 {
8246 out_str(T_ME);
8247 screen_attr = -1;
8248 }
8249 }
8250}
8251
8252/*
8253 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8254 * using the attributes from ScreenAttrs["off"].
8255 */
8256 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008257screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 int attr;
8260
8261 /* Check for illegal values, just in case (could happen just after
8262 * resizing). */
8263 if (row >= screen_Rows || col >= screen_Columns)
8264 return;
8265
Bram Moolenaar494838a2015-02-10 19:20:37 +01008266 /* Outputting a character in the last cell on the screen may scroll the
8267 * screen up. Only do it when the "xn" termcap property is set, otherwise
8268 * mark the character invalid (update it when scrolled up). */
8269 if (*T_XN == NUL
8270 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271#ifdef FEAT_RIGHTLEFT
8272 /* account for first command-line character in rightleft mode */
8273 && !cmdmsg_rl
8274#endif
8275 )
8276 {
8277 ScreenAttrs[off] = (sattr_T)-1;
8278 return;
8279 }
8280
8281 /*
8282 * Stop highlighting first, so it's easier to move the cursor.
8283 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008284#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (screen_char_attr != 0)
8286 attr = screen_char_attr;
8287 else
8288#endif
8289 attr = ScreenAttrs[off];
8290 if (screen_attr != attr)
8291 screen_stop_highlight();
8292
8293 windgoto(row, col);
8294
8295 if (screen_attr != attr)
8296 screen_start_highlight(attr);
8297
8298#ifdef FEAT_MBYTE
8299 if (enc_utf8 && ScreenLinesUC[off] != 0)
8300 {
8301 char_u buf[MB_MAXBYTES + 1];
8302
8303 /* Convert UTF-8 character to bytes and write it. */
8304
8305 buf[utfc_char2bytes(off, buf)] = NUL;
8306
8307 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008308 if (utf_ambiguous_width(ScreenLinesUC[off]))
8309 screen_cur_col = 9999;
8310 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 ++screen_cur_col;
8312 }
8313 else
8314#endif
8315 {
8316#ifdef FEAT_MBYTE
8317 out_flush_check();
8318#endif
8319 out_char(ScreenLines[off]);
8320#ifdef FEAT_MBYTE
8321 /* double-byte character in single-width cell */
8322 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8323 out_char(ScreenLines2[off]);
8324#endif
8325 }
8326
8327 screen_cur_col++;
8328}
8329
8330#ifdef FEAT_MBYTE
8331
8332/*
8333 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8334 * on the screen at position 'row' and 'col'.
8335 * The attributes of the first byte is used for all. This is required to
8336 * output the two bytes of a double-byte character with nothing in between.
8337 */
8338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008339screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 /* Check for illegal values (could be wrong when screen was resized). */
8342 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8343 return;
8344
8345 /* Outputting the last character on the screen may scrollup the screen.
8346 * Don't to it! Mark the character invalid (update it when scrolled up) */
8347 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8348 {
8349 ScreenAttrs[off] = (sattr_T)-1;
8350 return;
8351 }
8352
8353 /* Output the first byte normally (positions the cursor), then write the
8354 * second byte directly. */
8355 screen_char(off, row, col);
8356 out_char(ScreenLines[off + 1]);
8357 ++screen_cur_col;
8358}
8359#endif
8360
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008361#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362/*
8363 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8364 * This uses the contents of ScreenLines[] and doesn't change it.
8365 */
8366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008367screen_draw_rectangle(
8368 int row,
8369 int col,
8370 int height,
8371 int width,
8372 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 int r, c;
8375 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008376#ifdef FEAT_MBYTE
8377 int max_off;
8378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008380 /* Can't use ScreenLines unless initialized */
8381 if (ScreenLines == NULL)
8382 return;
8383
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 if (invert)
8385 screen_char_attr = HL_INVERSE;
8386 for (r = row; r < row + height; ++r)
8387 {
8388 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008389#ifdef FEAT_MBYTE
8390 max_off = off + screen_Columns;
8391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 for (c = col; c < col + width; ++c)
8393 {
8394#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008395 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 screen_char_2(off + c, r, c);
8398 ++c;
8399 }
8400 else
8401#endif
8402 {
8403 screen_char(off + c, r, c);
8404#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008405 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 ++c;
8407#endif
8408 }
8409 }
8410 }
8411 screen_char_attr = 0;
8412}
8413#endif
8414
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008415#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416/*
8417 * Redraw the characters for a vertically split window.
8418 */
8419 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008420redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 int col;
8423 int width;
8424
8425# ifdef FEAT_CLIPBOARD
8426 clip_may_clear_selection(row, end - 1);
8427# endif
8428
8429 if (wp == NULL)
8430 {
8431 col = 0;
8432 width = Columns;
8433 }
8434 else
8435 {
8436 col = wp->w_wincol;
8437 width = wp->w_width;
8438 }
8439 screen_draw_rectangle(row, col, end - row, width, FALSE);
8440}
8441#endif
8442
8443/*
8444 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8445 * with character 'c1' in first column followed by 'c2' in the other columns.
8446 * Use attributes 'attr'.
8447 */
8448 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008449screen_fill(
8450 int start_row,
8451 int end_row,
8452 int start_col,
8453 int end_col,
8454 int c1,
8455 int c2,
8456 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 int row;
8459 int col;
8460 int off;
8461 int end_off;
8462 int did_delete;
8463 int c;
8464 int norm_term;
8465#if defined(FEAT_GUI) || defined(UNIX)
8466 int force_next = FALSE;
8467#endif
8468
8469 if (end_row > screen_Rows) /* safety check */
8470 end_row = screen_Rows;
8471 if (end_col > screen_Columns) /* safety check */
8472 end_col = screen_Columns;
8473 if (ScreenLines == NULL
8474 || start_row >= end_row
8475 || start_col >= end_col) /* nothing to do */
8476 return;
8477
8478 /* it's a "normal" terminal when not in a GUI or cterm */
8479 norm_term = (
8480#ifdef FEAT_GUI
8481 !gui.in_use &&
8482#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008483 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 for (row = start_row; row < end_row; ++row)
8485 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008486#ifdef FEAT_MBYTE
8487 if (has_mbyte
8488# ifdef FEAT_GUI
8489 && !gui.in_use
8490# endif
8491 )
8492 {
8493 /* When drawing over the right halve of a double-wide char clear
8494 * out the left halve. When drawing over the left halve of a
8495 * double wide-char clear out the right halve. Only needed in a
8496 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008497 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008498 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008499 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008500 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008501 }
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 /*
8504 * Try to use delete-line termcap code, when no attributes or in a
8505 * "normal" terminal, where a bold/italic space is just a
8506 * space.
8507 */
8508 did_delete = FALSE;
8509 if (c2 == ' '
8510 && end_col == Columns
8511 && can_clear(T_CE)
8512 && (attr == 0
8513 || (norm_term
8514 && attr <= HL_ALL
8515 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8516 {
8517 /*
8518 * check if we really need to clear something
8519 */
8520 col = start_col;
8521 if (c1 != ' ') /* don't clear first char */
8522 ++col;
8523
8524 off = LineOffset[row] + col;
8525 end_off = LineOffset[row] + end_col;
8526
8527 /* skip blanks (used often, keep it fast!) */
8528#ifdef FEAT_MBYTE
8529 if (enc_utf8)
8530 while (off < end_off && ScreenLines[off] == ' '
8531 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8532 ++off;
8533 else
8534#endif
8535 while (off < end_off && ScreenLines[off] == ' '
8536 && ScreenAttrs[off] == 0)
8537 ++off;
8538 if (off < end_off) /* something to be cleared */
8539 {
8540 col = off - LineOffset[row];
8541 screen_stop_highlight();
8542 term_windgoto(row, col);/* clear rest of this screen line */
8543 out_str(T_CE);
8544 screen_start(); /* don't know where cursor is now */
8545 col = end_col - col;
8546 while (col--) /* clear chars in ScreenLines */
8547 {
8548 ScreenLines[off] = ' ';
8549#ifdef FEAT_MBYTE
8550 if (enc_utf8)
8551 ScreenLinesUC[off] = 0;
8552#endif
8553 ScreenAttrs[off] = 0;
8554 ++off;
8555 }
8556 }
8557 did_delete = TRUE; /* the chars are cleared now */
8558 }
8559
8560 off = LineOffset[row] + start_col;
8561 c = c1;
8562 for (col = start_col; col < end_col; ++col)
8563 {
8564 if (ScreenLines[off] != c
8565#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008566 || (enc_utf8 && (int)ScreenLinesUC[off]
8567 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568#endif
8569 || ScreenAttrs[off] != attr
8570#if defined(FEAT_GUI) || defined(UNIX)
8571 || force_next
8572#endif
8573 )
8574 {
8575#if defined(FEAT_GUI) || defined(UNIX)
8576 /* The bold trick may make a single row of pixels appear in
8577 * the next character. When a bold character is removed, the
8578 * next character should be redrawn too. This happens for our
8579 * own GUI and for some xterms. */
8580 if (
8581# ifdef FEAT_GUI
8582 gui.in_use
8583# endif
8584# if defined(FEAT_GUI) && defined(UNIX)
8585 ||
8586# endif
8587# ifdef UNIX
8588 term_is_xterm
8589# endif
8590 )
8591 {
8592 if (ScreenLines[off] != ' '
8593 && (ScreenAttrs[off] > HL_ALL
8594 || ScreenAttrs[off] & HL_BOLD))
8595 force_next = TRUE;
8596 else
8597 force_next = FALSE;
8598 }
8599#endif
8600 ScreenLines[off] = c;
8601#ifdef FEAT_MBYTE
8602 if (enc_utf8)
8603 {
8604 if (c >= 0x80)
8605 {
8606 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008607 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609 else
8610 ScreenLinesUC[off] = 0;
8611 }
8612#endif
8613 ScreenAttrs[off] = attr;
8614 if (!did_delete || c != ' ')
8615 screen_char(off, row, col);
8616 }
8617 ++off;
8618 if (col == start_col)
8619 {
8620 if (did_delete)
8621 break;
8622 c = c2;
8623 }
8624 }
8625 if (end_col == Columns)
8626 LineWraps[row] = FALSE;
8627 if (row == Rows - 1) /* overwritten the command line */
8628 {
8629 redraw_cmdline = TRUE;
8630 if (c1 == ' ' && c2 == ' ')
8631 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008632 if (start_col == 0)
8633 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 }
8635 }
8636}
8637
8638/*
8639 * Check if there should be a delay. Used before clearing or redrawing the
8640 * screen or the command line.
8641 */
8642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008643check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
8645 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8646 && !did_wait_return
8647 && emsg_silent == 0)
8648 {
8649 out_flush();
8650 ui_delay(1000L, TRUE);
8651 emsg_on_display = FALSE;
8652 if (check_msg_scroll)
8653 msg_scroll = FALSE;
8654 }
8655}
8656
8657/*
8658 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008659 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 * Returns TRUE if there is a valid screen to write to.
8661 * Returns FALSE when starting up and screen not initialized yet.
8662 */
8663 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008664screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008666 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 return (ScreenLines != NULL);
8668}
8669
8670/*
8671 * Resize the shell to Rows and Columns.
8672 * Allocate ScreenLines[] and associated items.
8673 *
8674 * There may be some time between setting Rows and Columns and (re)allocating
8675 * ScreenLines[]. This happens when starting up and when (manually) changing
8676 * the shell size. Always use screen_Rows and screen_Columns to access items
8677 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8678 * final size of the shell is needed.
8679 */
8680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008681screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 int new_row, old_row;
8684#ifdef FEAT_GUI
8685 int old_Rows;
8686#endif
8687 win_T *wp;
8688 int outofmem = FALSE;
8689 int len;
8690 schar_T *new_ScreenLines;
8691#ifdef FEAT_MBYTE
8692 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008693 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008695 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#endif
8697 sattr_T *new_ScreenAttrs;
8698 unsigned *new_LineOffset;
8699 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008700#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008701 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008702 tabpage_T *tp;
8703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008705 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008706#ifdef FEAT_AUTOCMD
8707 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008709retry:
8710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 /*
8712 * Allocation of the screen buffers is done only when the size changes and
8713 * when Rows and Columns have been set and we have started doing full
8714 * screen stuff.
8715 */
8716 if ((ScreenLines != NULL
8717 && Rows == screen_Rows
8718 && Columns == screen_Columns
8719#ifdef FEAT_MBYTE
8720 && enc_utf8 == (ScreenLinesUC != NULL)
8721 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008722 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#endif
8724 )
8725 || Rows == 0
8726 || Columns == 0
8727 || (!full_screen && ScreenLines == NULL))
8728 return;
8729
8730 /*
8731 * It's possible that we produce an out-of-memory message below, which
8732 * will cause this function to be called again. To break the loop, just
8733 * return here.
8734 */
8735 if (entered)
8736 return;
8737 entered = TRUE;
8738
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008739 /*
8740 * Note that the window sizes are updated before reallocating the arrays,
8741 * thus we must not redraw here!
8742 */
8743 ++RedrawingDisabled;
8744
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 win_new_shellsize(); /* fit the windows in the new sized shell */
8746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 comp_col(); /* recompute columns for shown command and ruler */
8748
8749 /*
8750 * We're changing the size of the screen.
8751 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8752 * - Move lines from the old arrays into the new arrays, clear extra
8753 * lines (unless the screen is going to be cleared).
8754 * - Free the old arrays.
8755 *
8756 * If anything fails, make ScreenLines NULL, so we don't do anything!
8757 * Continuing with the old ScreenLines may result in a crash, because the
8758 * size is wrong.
8759 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008760 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008762#ifdef FEAT_AUTOCMD
8763 if (aucmd_win != NULL)
8764 win_free_lsize(aucmd_win);
8765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766
8767 new_ScreenLines = (schar_T *)lalloc((long_u)(
8768 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8769#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008770 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 if (enc_utf8)
8772 {
8773 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8774 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008775 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008776 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8778 }
8779 if (enc_dbcs == DBCS_JPNU)
8780 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8781 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8782#endif
8783 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8784 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8785 new_LineOffset = (unsigned *)lalloc((long_u)(
8786 Rows * sizeof(unsigned)), FALSE);
8787 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008788#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008789 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008792 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
8794 if (win_alloc_lines(wp) == FAIL)
8795 {
8796 outofmem = TRUE;
8797#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008798 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#endif
8800 }
8801 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008802#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008803 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8804 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008805 outofmem = TRUE;
8806#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008807#ifdef FEAT_WINDOWS
8808give_up:
8809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008811#ifdef FEAT_MBYTE
8812 for (i = 0; i < p_mco; ++i)
8813 if (new_ScreenLinesC[i] == NULL)
8814 break;
8815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (new_ScreenLines == NULL
8817#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8820#endif
8821 || new_ScreenAttrs == NULL
8822 || new_LineOffset == NULL
8823 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008824#ifdef FEAT_WINDOWS
8825 || new_TabPageIdxs == NULL
8826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 || outofmem)
8828 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008829 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008830 {
8831 /* guess the size */
8832 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8833
8834 /* Remember we did this to avoid getting outofmem messages over
8835 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008836 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 vim_free(new_ScreenLines);
8839 new_ScreenLines = NULL;
8840#ifdef FEAT_MBYTE
8841 vim_free(new_ScreenLinesUC);
8842 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008843 for (i = 0; i < p_mco; ++i)
8844 {
8845 vim_free(new_ScreenLinesC[i]);
8846 new_ScreenLinesC[i] = NULL;
8847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 vim_free(new_ScreenLines2);
8849 new_ScreenLines2 = NULL;
8850#endif
8851 vim_free(new_ScreenAttrs);
8852 new_ScreenAttrs = NULL;
8853 vim_free(new_LineOffset);
8854 new_LineOffset = NULL;
8855 vim_free(new_LineWraps);
8856 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008857#ifdef FEAT_WINDOWS
8858 vim_free(new_TabPageIdxs);
8859 new_TabPageIdxs = NULL;
8860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 else
8863 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008864 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 for (new_row = 0; new_row < Rows; ++new_row)
8867 {
8868 new_LineOffset[new_row] = new_row * Columns;
8869 new_LineWraps[new_row] = FALSE;
8870
8871 /*
8872 * If the screen is not going to be cleared, copy as much as
8873 * possible from the old screen to the new one and clear the rest
8874 * (used when resizing the window at the "--more--" prompt or when
8875 * executing an external command, for the GUI).
8876 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008877 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
8879 (void)vim_memset(new_ScreenLines + new_row * Columns,
8880 ' ', (size_t)Columns * sizeof(schar_T));
8881#ifdef FEAT_MBYTE
8882 if (enc_utf8)
8883 {
8884 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8885 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 for (i = 0; i < p_mco; ++i)
8887 (void)vim_memset(new_ScreenLinesC[i]
8888 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 0, (size_t)Columns * sizeof(u8char_T));
8890 }
8891 if (enc_dbcs == DBCS_JPNU)
8892 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8893 0, (size_t)Columns * sizeof(schar_T));
8894#endif
8895 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8896 0, (size_t)Columns * sizeof(sattr_T));
8897 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008898 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 if (screen_Columns < Columns)
8901 len = screen_Columns;
8902 else
8903 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008904#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008905 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008906 * may be invalid now. Also when p_mco changes. */
8907 if (!(enc_utf8 && ScreenLinesUC == NULL)
8908 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008909#endif
8910 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8911 ScreenLines + LineOffset[old_row],
8912 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008914 if (enc_utf8 && ScreenLinesUC != NULL
8915 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8918 ScreenLinesUC + LineOffset[old_row],
8919 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008920 for (i = 0; i < p_mco; ++i)
8921 mch_memmove(new_ScreenLinesC[i]
8922 + new_LineOffset[new_row],
8923 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 (size_t)len * sizeof(u8char_T));
8925 }
8926 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8927 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8928 ScreenLines2 + LineOffset[old_row],
8929 (size_t)len * sizeof(schar_T));
8930#endif
8931 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8932 ScreenAttrs + LineOffset[old_row],
8933 (size_t)len * sizeof(sattr_T));
8934 }
8935 }
8936 }
8937 /* Use the last line of the screen for the current line. */
8938 current_ScreenLine = new_ScreenLines + Rows * Columns;
8939 }
8940
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008941 free_screenlines();
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 ScreenLines = new_ScreenLines;
8944#ifdef FEAT_MBYTE
8945 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008946 for (i = 0; i < p_mco; ++i)
8947 ScreenLinesC[i] = new_ScreenLinesC[i];
8948 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 ScreenLines2 = new_ScreenLines2;
8950#endif
8951 ScreenAttrs = new_ScreenAttrs;
8952 LineOffset = new_LineOffset;
8953 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008954#ifdef FEAT_WINDOWS
8955 TabPageIdxs = new_TabPageIdxs;
8956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
8958 /* It's important that screen_Rows and screen_Columns reflect the actual
8959 * size of ScreenLines[]. Set them before calling anything. */
8960#ifdef FEAT_GUI
8961 old_Rows = screen_Rows;
8962#endif
8963 screen_Rows = Rows;
8964 screen_Columns = Columns;
8965
8966 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008967 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 screenclear2();
8969
8970#ifdef FEAT_GUI
8971 else if (gui.in_use
8972 && !gui.starting
8973 && ScreenLines != NULL
8974 && old_Rows != Rows)
8975 {
8976 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8977 /*
8978 * Adjust the position of the cursor, for when executing an external
8979 * command.
8980 */
8981 if (msg_row >= Rows) /* Rows got smaller */
8982 msg_row = Rows - 1; /* put cursor at last row */
8983 else if (Rows > old_Rows) /* Rows got bigger */
8984 msg_row += Rows - old_Rows; /* put cursor in same place */
8985 if (msg_col >= Columns) /* Columns got smaller */
8986 msg_col = Columns - 1; /* put cursor at last column */
8987 }
8988#endif
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008991 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008992
8993#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008994 /*
8995 * Do not apply autocommands more than 3 times to avoid an endless loop
8996 * in case applying autocommands always changes Rows or Columns.
8997 */
8998 if (starting == 0 && ++retry_count <= 3)
8999 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009000 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009001 /* In rare cases, autocommands may have altered Rows or Columns,
9002 * jump back to check if we need to allocate the screen again. */
9003 goto retry;
9004 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
9008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009009free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009010{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009011#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009012 int i;
9013
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009014 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 for (i = 0; i < Screen_mco; ++i)
9016 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009017 vim_free(ScreenLines2);
9018#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009019 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009020 vim_free(ScreenAttrs);
9021 vim_free(LineOffset);
9022 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009023#ifdef FEAT_WINDOWS
9024 vim_free(TabPageIdxs);
9025#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009026}
9027
9028 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009029screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 check_for_delay(FALSE);
9032 screenalloc(FALSE); /* allocate screen buffers if size changed */
9033 screenclear2(); /* clear the screen */
9034}
9035
9036 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009037screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 int i;
9040
9041 if (starting == NO_SCREEN || ScreenLines == NULL
9042#ifdef FEAT_GUI
9043 || (gui.in_use && gui.starting)
9044#endif
9045 )
9046 return;
9047
9048#ifdef FEAT_GUI
9049 if (!gui.in_use)
9050#endif
9051 screen_attr = -1; /* force setting the Normal colors */
9052 screen_stop_highlight(); /* don't want highlighting here */
9053
9054#ifdef FEAT_CLIPBOARD
9055 /* disable selection without redrawing it */
9056 clip_scroll_selection(9999);
9057#endif
9058
9059 /* blank out ScreenLines */
9060 for (i = 0; i < Rows; ++i)
9061 {
9062 lineclear(LineOffset[i], (int)Columns);
9063 LineWraps[i] = FALSE;
9064 }
9065
9066 if (can_clear(T_CL))
9067 {
9068 out_str(T_CL); /* clear the display */
9069 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009070 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 }
9072 else
9073 {
9074 /* can't clear the screen, mark all chars with invalid attributes */
9075 for (i = 0; i < Rows; ++i)
9076 lineinvalid(LineOffset[i], (int)Columns);
9077 clear_cmdline = TRUE;
9078 }
9079
9080 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9081
9082 win_rest_invalid(firstwin);
9083 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009084#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009085 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (must_redraw == CLEAR) /* no need to clear again */
9088 must_redraw = NOT_VALID;
9089 compute_cmdrow();
9090 msg_row = cmdline_row; /* put cursor on last line for messages */
9091 msg_col = 0;
9092 screen_start(); /* don't know where cursor is now */
9093 msg_scrolled = 0; /* can't scroll back */
9094 msg_didany = FALSE;
9095 msg_didout = FALSE;
9096}
9097
9098/*
9099 * Clear one line in ScreenLines.
9100 */
9101 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009102lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9105#ifdef FEAT_MBYTE
9106 if (enc_utf8)
9107 (void)vim_memset(ScreenLinesUC + off, 0,
9108 (size_t)width * sizeof(u8char_T));
9109#endif
9110 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
9111}
9112
9113/*
9114 * Mark one line in ScreenLines invalid by setting the attributes to an
9115 * invalid value.
9116 */
9117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009118lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9121}
9122
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009123#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124/*
9125 * Copy part of a Screenline for vertically split window "wp".
9126 */
9127 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009128linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 unsigned off_to = LineOffset[to] + wp->w_wincol;
9131 unsigned off_from = LineOffset[from] + wp->w_wincol;
9132
9133 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9134 wp->w_width * sizeof(schar_T));
9135# ifdef FEAT_MBYTE
9136 if (enc_utf8)
9137 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009138 int i;
9139
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9141 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009142 for (i = 0; i < p_mco; ++i)
9143 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9144 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 }
9146 if (enc_dbcs == DBCS_JPNU)
9147 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9148 wp->w_width * sizeof(schar_T));
9149# endif
9150 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9151 wp->w_width * sizeof(sattr_T));
9152}
9153#endif
9154
9155/*
9156 * Return TRUE if clearing with term string "p" would work.
9157 * It can't work when the string is empty or it won't set the right background.
9158 */
9159 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009160can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 return (*p != NUL && (t_colors <= 1
9163#ifdef FEAT_GUI
9164 || gui.in_use
9165#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009166#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009167 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009168 || (!p_tgc && cterm_normal_bg_color == 0)
9169#else
9170 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009171#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009172 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
9175/*
9176 * Reset cursor position. Use whenever cursor was moved because of outputting
9177 * something directly to the screen (shell commands) or a terminal control
9178 * code.
9179 */
9180 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009181screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 screen_cur_row = screen_cur_col = 9999;
9184}
9185
9186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 * Move the cursor to position "row","col" in the screen.
9188 * This tries to find the most efficient way to move, minimizing the number of
9189 * characters sent to the terminal.
9190 */
9191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009192windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009194 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 int i;
9196 int plan;
9197 int cost;
9198 int wouldbe_col;
9199 int noinvcurs;
9200 char_u *bs;
9201 int goto_cost;
9202 int attr;
9203
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009204#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9206
9207#define PLAN_LE 1
9208#define PLAN_CR 2
9209#define PLAN_NL 3
9210#define PLAN_WRITE 4
9211 /* Can't use ScreenLines unless initialized */
9212 if (ScreenLines == NULL)
9213 return;
9214
9215 if (col != screen_cur_col || row != screen_cur_row)
9216 {
9217 /* Check for valid position. */
9218 if (row < 0) /* window without text lines? */
9219 row = 0;
9220 if (row >= screen_Rows)
9221 row = screen_Rows - 1;
9222 if (col >= screen_Columns)
9223 col = screen_Columns - 1;
9224
9225 /* check if no cursor movement is allowed in highlight mode */
9226 if (screen_attr && *T_MS == NUL)
9227 noinvcurs = HIGHL_COST;
9228 else
9229 noinvcurs = 0;
9230 goto_cost = GOTO_COST + noinvcurs;
9231
9232 /*
9233 * Plan how to do the positioning:
9234 * 1. Use CR to move it to column 0, same row.
9235 * 2. Use T_LE to move it a few columns to the left.
9236 * 3. Use NL to move a few lines down, column 0.
9237 * 4. Move a few columns to the right with T_ND or by writing chars.
9238 *
9239 * Don't do this if the cursor went beyond the last column, the cursor
9240 * position is unknown then (some terminals wrap, some don't )
9241 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009242 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 * characters to move the cursor to the right.
9244 */
9245 if (row >= screen_cur_row && screen_cur_col < Columns)
9246 {
9247 /*
9248 * If the cursor is in the same row, bigger col, we can use CR
9249 * or T_LE.
9250 */
9251 bs = NULL; /* init for GCC */
9252 attr = screen_attr;
9253 if (row == screen_cur_row && col < screen_cur_col)
9254 {
9255 /* "le" is preferred over "bc", because "bc" is obsolete */
9256 if (*T_LE)
9257 bs = T_LE; /* "cursor left" */
9258 else
9259 bs = T_BC; /* "backspace character (old) */
9260 if (*bs)
9261 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9262 else
9263 cost = 999;
9264 if (col + 1 < cost) /* using CR is less characters */
9265 {
9266 plan = PLAN_CR;
9267 wouldbe_col = 0;
9268 cost = 1; /* CR is just one character */
9269 }
9270 else
9271 {
9272 plan = PLAN_LE;
9273 wouldbe_col = col;
9274 }
9275 if (noinvcurs) /* will stop highlighting */
9276 {
9277 cost += noinvcurs;
9278 attr = 0;
9279 }
9280 }
9281
9282 /*
9283 * If the cursor is above where we want to be, we can use CR LF.
9284 */
9285 else if (row > screen_cur_row)
9286 {
9287 plan = PLAN_NL;
9288 wouldbe_col = 0;
9289 cost = (row - screen_cur_row) * 2; /* CR LF */
9290 if (noinvcurs) /* will stop highlighting */
9291 {
9292 cost += noinvcurs;
9293 attr = 0;
9294 }
9295 }
9296
9297 /*
9298 * If the cursor is in the same row, smaller col, just use write.
9299 */
9300 else
9301 {
9302 plan = PLAN_WRITE;
9303 wouldbe_col = screen_cur_col;
9304 cost = 0;
9305 }
9306
9307 /*
9308 * Check if any characters that need to be written have the
9309 * correct attributes. Also avoid UTF-8 characters.
9310 */
9311 i = col - wouldbe_col;
9312 if (i > 0)
9313 cost += i;
9314 if (cost < goto_cost && i > 0)
9315 {
9316 /*
9317 * Check if the attributes are correct without additionally
9318 * stopping highlighting.
9319 */
9320 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9321 while (i && *p++ == attr)
9322 --i;
9323 if (i != 0)
9324 {
9325 /*
9326 * Try if it works when highlighting is stopped here.
9327 */
9328 if (*--p == 0)
9329 {
9330 cost += noinvcurs;
9331 while (i && *p++ == 0)
9332 --i;
9333 }
9334 if (i != 0)
9335 cost = 999; /* different attributes, don't do it */
9336 }
9337#ifdef FEAT_MBYTE
9338 if (enc_utf8)
9339 {
9340 /* Don't use an UTF-8 char for positioning, it's slow. */
9341 for (i = wouldbe_col; i < col; ++i)
9342 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9343 {
9344 cost = 999;
9345 break;
9346 }
9347 }
9348#endif
9349 }
9350
9351 /*
9352 * We can do it without term_windgoto()!
9353 */
9354 if (cost < goto_cost)
9355 {
9356 if (plan == PLAN_LE)
9357 {
9358 if (noinvcurs)
9359 screen_stop_highlight();
9360 while (screen_cur_col > col)
9361 {
9362 out_str(bs);
9363 --screen_cur_col;
9364 }
9365 }
9366 else if (plan == PLAN_CR)
9367 {
9368 if (noinvcurs)
9369 screen_stop_highlight();
9370 out_char('\r');
9371 screen_cur_col = 0;
9372 }
9373 else if (plan == PLAN_NL)
9374 {
9375 if (noinvcurs)
9376 screen_stop_highlight();
9377 while (screen_cur_row < row)
9378 {
9379 out_char('\n');
9380 ++screen_cur_row;
9381 }
9382 screen_cur_col = 0;
9383 }
9384
9385 i = col - screen_cur_col;
9386 if (i > 0)
9387 {
9388 /*
9389 * Use cursor-right if it's one character only. Avoids
9390 * removing a line of pixels from the last bold char, when
9391 * using the bold trick in the GUI.
9392 */
9393 if (T_ND[0] != NUL && T_ND[1] == NUL)
9394 {
9395 while (i-- > 0)
9396 out_char(*T_ND);
9397 }
9398 else
9399 {
9400 int off;
9401
9402 off = LineOffset[row] + screen_cur_col;
9403 while (i-- > 0)
9404 {
9405 if (ScreenAttrs[off] != screen_attr)
9406 screen_stop_highlight();
9407#ifdef FEAT_MBYTE
9408 out_flush_check();
9409#endif
9410 out_char(ScreenLines[off]);
9411#ifdef FEAT_MBYTE
9412 if (enc_dbcs == DBCS_JPNU
9413 && ScreenLines[off] == 0x8e)
9414 out_char(ScreenLines2[off]);
9415#endif
9416 ++off;
9417 }
9418 }
9419 }
9420 }
9421 }
9422 else
9423 cost = 999;
9424
9425 if (cost >= goto_cost)
9426 {
9427 if (noinvcurs)
9428 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009429 if (row == screen_cur_row && (col > screen_cur_col)
9430 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 term_cursor_right(col - screen_cur_col);
9432 else
9433 term_windgoto(row, col);
9434 }
9435 screen_cur_row = row;
9436 screen_cur_col = col;
9437 }
9438}
9439
9440/*
9441 * Set cursor to its position in the current window.
9442 */
9443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009444setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446 if (redrawing())
9447 {
9448 validate_cursor();
9449 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9450 W_WINCOL(curwin) + (
9451#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009452 /* With 'rightleft' set and the cursor on a double-wide
9453 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9455# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009456 (has_mbyte
9457 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9458 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459# endif
9460 1)) :
9461#endif
9462 curwin->w_wcol));
9463 }
9464}
9465
9466
9467/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009468 * Insert 'line_count' lines at 'row' in window 'wp'.
9469 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9470 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 * scrolling.
9472 * Returns FAIL if the lines are not inserted, OK for success.
9473 */
9474 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009475win_ins_lines(
9476 win_T *wp,
9477 int row,
9478 int line_count,
9479 int invalid,
9480 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
9482 int did_delete;
9483 int nextrow;
9484 int lastrow;
9485 int retval;
9486
9487 if (invalid)
9488 wp->w_lines_valid = 0;
9489
9490 if (wp->w_height < 5)
9491 return FAIL;
9492
9493 if (line_count > wp->w_height - row)
9494 line_count = wp->w_height - row;
9495
9496 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9497 if (retval != MAYBE)
9498 return retval;
9499
9500 /*
9501 * If there is a next window or a status line, we first try to delete the
9502 * lines at the bottom to avoid messing what is after the window.
9503 * If this fails and there are following windows, don't do anything to avoid
9504 * messing up those windows, better just redraw.
9505 */
9506 did_delete = FALSE;
9507#ifdef FEAT_WINDOWS
9508 if (wp->w_next != NULL || wp->w_status_height)
9509 {
9510 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9511 line_count, (int)Rows, FALSE, NULL) == OK)
9512 did_delete = TRUE;
9513 else if (wp->w_next)
9514 return FAIL;
9515 }
9516#endif
9517 /*
9518 * if no lines deleted, blank the lines that will end up below the window
9519 */
9520 if (!did_delete)
9521 {
9522#ifdef FEAT_WINDOWS
9523 wp->w_redr_status = TRUE;
9524#endif
9525 redraw_cmdline = TRUE;
9526 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9527 lastrow = nextrow + line_count;
9528 if (lastrow > Rows)
9529 lastrow = Rows;
9530 screen_fill(nextrow - line_count, lastrow - line_count,
9531 W_WINCOL(wp), (int)W_ENDCOL(wp),
9532 ' ', ' ', 0);
9533 }
9534
9535 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9536 == FAIL)
9537 {
9538 /* deletion will have messed up other windows */
9539 if (did_delete)
9540 {
9541#ifdef FEAT_WINDOWS
9542 wp->w_redr_status = TRUE;
9543#endif
9544 win_rest_invalid(W_NEXT(wp));
9545 }
9546 return FAIL;
9547 }
9548
9549 return OK;
9550}
9551
9552/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009553 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9555 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9556 * scrolling
9557 * Return OK for success, FAIL if the lines are not deleted.
9558 */
9559 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009560win_del_lines(
9561 win_T *wp,
9562 int row,
9563 int line_count,
9564 int invalid,
9565 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 int retval;
9568
9569 if (invalid)
9570 wp->w_lines_valid = 0;
9571
9572 if (line_count > wp->w_height - row)
9573 line_count = wp->w_height - row;
9574
9575 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9576 if (retval != MAYBE)
9577 return retval;
9578
9579 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9580 (int)Rows, FALSE, NULL) == FAIL)
9581 return FAIL;
9582
9583#ifdef FEAT_WINDOWS
9584 /*
9585 * If there are windows or status lines below, try to put them at the
9586 * correct place. If we can't do that, they have to be redrawn.
9587 */
9588 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9589 {
9590 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9591 line_count, (int)Rows, NULL) == FAIL)
9592 {
9593 wp->w_redr_status = TRUE;
9594 win_rest_invalid(wp->w_next);
9595 }
9596 }
9597 /*
9598 * If this is the last window and there is no status line, redraw the
9599 * command line later.
9600 */
9601 else
9602#endif
9603 redraw_cmdline = TRUE;
9604 return OK;
9605}
9606
9607/*
9608 * Common code for win_ins_lines() and win_del_lines().
9609 * Returns OK or FAIL when the work has been done.
9610 * Returns MAYBE when not finished yet.
9611 */
9612 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009613win_do_lines(
9614 win_T *wp,
9615 int row,
9616 int line_count,
9617 int mayclear,
9618 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620 int retval;
9621
9622 if (!redrawing() || line_count <= 0)
9623 return FAIL;
9624
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009625 /* When inserting lines would result in loss of command output, just redraw
9626 * the lines. */
9627 if (no_win_do_lines_ins && !del)
9628 return FAIL;
9629
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 /* only a few lines left: redraw is faster */
9631 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009632#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 && wp->w_width == Columns
9634#endif
9635 )
9636 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009637 if (!no_win_do_lines_ins)
9638 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 return FAIL;
9640 }
9641
9642 /*
9643 * Delete all remaining lines
9644 */
9645 if (row + line_count >= wp->w_height)
9646 {
9647 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9648 W_WINCOL(wp), (int)W_ENDCOL(wp),
9649 ' ', ' ', 0);
9650 return OK;
9651 }
9652
9653 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009654 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009656 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009658 if (!no_win_do_lines_ins)
9659 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660
9661 /*
9662 * If the terminal can set a scroll region, use that.
9663 * Always do this in a vertically split window. This will redraw from
9664 * ScreenLines[] when t_CV isn't defined. That's faster than using
9665 * win_line().
9666 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009667 * a character in the lower right corner of the scroll region may cause a
9668 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 */
9670 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009671#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 || W_WIDTH(wp) != Columns
9673#endif
9674 )
9675 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009676#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9678#endif
9679 scroll_region_set(wp, row);
9680 if (del)
9681 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9682 wp->w_height - row, FALSE, wp);
9683 else
9684 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9685 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009686#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9688#endif
9689 scroll_region_reset();
9690 return retval;
9691 }
9692
9693#ifdef FEAT_WINDOWS
9694 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9695 return FAIL;
9696#endif
9697
9698 return MAYBE;
9699}
9700
9701/*
9702 * window 'wp' and everything after it is messed up, mark it for redraw
9703 */
9704 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009705win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707#ifdef FEAT_WINDOWS
9708 while (wp != NULL)
9709#else
9710 if (wp != NULL)
9711#endif
9712 {
9713 redraw_win_later(wp, NOT_VALID);
9714#ifdef FEAT_WINDOWS
9715 wp->w_redr_status = TRUE;
9716 wp = wp->w_next;
9717#endif
9718 }
9719 redraw_cmdline = TRUE;
9720}
9721
9722/*
9723 * The rest of the routines in this file perform screen manipulations. The
9724 * given operation is performed physically on the screen. The corresponding
9725 * change is also made to the internal screen image. In this way, the editor
9726 * anticipates the effect of editing changes on the appearance of the screen.
9727 * That way, when we call screenupdate a complete redraw isn't usually
9728 * necessary. Another advantage is that we can keep adding code to anticipate
9729 * screen changes, and in the meantime, everything still works.
9730 */
9731
9732/*
9733 * types for inserting or deleting lines
9734 */
9735#define USE_T_CAL 1
9736#define USE_T_CDL 2
9737#define USE_T_AL 3
9738#define USE_T_CE 4
9739#define USE_T_DL 5
9740#define USE_T_SR 6
9741#define USE_NL 7
9742#define USE_T_CD 8
9743#define USE_REDRAW 9
9744
9745/*
9746 * insert lines on the screen and update ScreenLines[]
9747 * 'end' is the line after the scrolled part. Normally it is Rows.
9748 * When scrolling region used 'off' is the offset from the top for the region.
9749 * 'row' and 'end' are relative to the start of the region.
9750 *
9751 * return FAIL for failure, OK for success.
9752 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009753 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009754screen_ins_lines(
9755 int off,
9756 int row,
9757 int line_count,
9758 int end,
9759 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 int i;
9762 int j;
9763 unsigned temp;
9764 int cursor_row;
9765 int type;
9766 int result_empty;
9767 int can_ce = can_clear(T_CE);
9768
9769 /*
9770 * FAIL if
9771 * - there is no valid screen
9772 * - the screen has to be redrawn completely
9773 * - the line count is less than one
9774 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009775 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009777 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9778#ifdef FEAT_CLIPBOARD
9779 || (clip_star.state != SELECT_CLEARED
9780 && redrawing_for_callback > 0)
9781#endif
9782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 return FAIL;
9784
9785 /*
9786 * There are seven ways to insert lines:
9787 * 0. When in a vertically split window and t_CV isn't set, redraw the
9788 * characters from ScreenLines[].
9789 * 1. Use T_CD (clear to end of display) if it exists and the result of
9790 * the insert is just empty lines
9791 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9792 * present or line_count > 1. It looks better if we do all the inserts
9793 * at once.
9794 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9795 * insert is just empty lines and T_CE is not present or line_count >
9796 * 1.
9797 * 4. Use T_AL (insert line) if it exists.
9798 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9799 * just empty lines.
9800 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9801 * just empty lines.
9802 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9803 * the 'da' flag is not set or we have clear line capability.
9804 * 8. redraw the characters from ScreenLines[].
9805 *
9806 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9807 * the scrollbar for the window. It does have insert line, use that if it
9808 * exists.
9809 */
9810 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009811#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9813 type = USE_REDRAW;
9814 else
9815#endif
9816 if (can_clear(T_CD) && result_empty)
9817 type = USE_T_CD;
9818 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9819 type = USE_T_CAL;
9820 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9821 type = USE_T_CDL;
9822 else if (*T_AL != NUL)
9823 type = USE_T_AL;
9824 else if (can_ce && result_empty)
9825 type = USE_T_CE;
9826 else if (*T_DL != NUL && result_empty)
9827 type = USE_T_DL;
9828 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9829 type = USE_T_SR;
9830 else
9831 return FAIL;
9832
9833 /*
9834 * For clearing the lines screen_del_lines() is used. This will also take
9835 * care of t_db if necessary.
9836 */
9837 if (type == USE_T_CD || type == USE_T_CDL ||
9838 type == USE_T_CE || type == USE_T_DL)
9839 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9840
9841 /*
9842 * If text is retained below the screen, first clear or delete as many
9843 * lines at the bottom of the window as are about to be inserted so that
9844 * the deleted lines won't later surface during a screen_del_lines.
9845 */
9846 if (*T_DB)
9847 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9848
9849#ifdef FEAT_CLIPBOARD
9850 /* Remove a modeless selection when inserting lines halfway the screen
9851 * or not the full width of the screen. */
9852 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009853# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 || (wp != NULL && wp->w_width != Columns)
9855# endif
9856 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009857 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 else
9859 clip_scroll_selection(-line_count);
9860#endif
9861
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862#ifdef FEAT_GUI
9863 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9864 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009865 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#endif
9867
9868 if (*T_CCS != NUL) /* cursor relative to region */
9869 cursor_row = row;
9870 else
9871 cursor_row = row + off;
9872
9873 /*
9874 * Shift LineOffset[] line_count down to reflect the inserted lines.
9875 * Clear the inserted lines in ScreenLines[].
9876 */
9877 row += off;
9878 end += off;
9879 for (i = 0; i < line_count; ++i)
9880 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009881#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 if (wp != NULL && wp->w_width != Columns)
9883 {
9884 /* need to copy part of a line */
9885 j = end - 1 - i;
9886 while ((j -= line_count) >= row)
9887 linecopy(j + line_count, j, wp);
9888 j += line_count;
9889 if (can_clear((char_u *)" "))
9890 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9891 else
9892 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9893 LineWraps[j] = FALSE;
9894 }
9895 else
9896#endif
9897 {
9898 j = end - 1 - i;
9899 temp = LineOffset[j];
9900 while ((j -= line_count) >= row)
9901 {
9902 LineOffset[j + line_count] = LineOffset[j];
9903 LineWraps[j + line_count] = LineWraps[j];
9904 }
9905 LineOffset[j + line_count] = temp;
9906 LineWraps[j + line_count] = FALSE;
9907 if (can_clear((char_u *)" "))
9908 lineclear(temp, (int)Columns);
9909 else
9910 lineinvalid(temp, (int)Columns);
9911 }
9912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 screen_stop_highlight();
9915 windgoto(cursor_row, 0);
9916
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009917#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 /* redraw the characters */
9919 if (type == USE_REDRAW)
9920 redraw_block(row, end, wp);
9921 else
9922#endif
9923 if (type == USE_T_CAL)
9924 {
9925 term_append_lines(line_count);
9926 screen_start(); /* don't know where cursor is now */
9927 }
9928 else
9929 {
9930 for (i = 0; i < line_count; i++)
9931 {
9932 if (type == USE_T_AL)
9933 {
9934 if (i && cursor_row != 0)
9935 windgoto(cursor_row, 0);
9936 out_str(T_AL);
9937 }
9938 else /* type == USE_T_SR */
9939 out_str(T_SR);
9940 screen_start(); /* don't know where cursor is now */
9941 }
9942 }
9943
9944 /*
9945 * With scroll-reverse and 'da' flag set we need to clear the lines that
9946 * have been scrolled down into the region.
9947 */
9948 if (type == USE_T_SR && *T_DA)
9949 {
9950 for (i = 0; i < line_count; ++i)
9951 {
9952 windgoto(off + i, 0);
9953 out_str(T_CE);
9954 screen_start(); /* don't know where cursor is now */
9955 }
9956 }
9957
9958#ifdef FEAT_GUI
9959 gui_can_update_cursor();
9960 if (gui.in_use)
9961 out_flush(); /* always flush after a scroll */
9962#endif
9963 return OK;
9964}
9965
9966/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009967 * Delete lines on the screen and update ScreenLines[].
9968 * "end" is the line after the scrolled part. Normally it is Rows.
9969 * When scrolling region used "off" is the offset from the top for the region.
9970 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 *
9972 * Return OK for success, FAIL if the lines are not deleted.
9973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009975screen_del_lines(
9976 int off,
9977 int row,
9978 int line_count,
9979 int end,
9980 int force, /* even when line_count > p_ttyscroll */
9981 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983 int j;
9984 int i;
9985 unsigned temp;
9986 int cursor_row;
9987 int cursor_end;
9988 int result_empty; /* result is empty until end of region */
9989 int can_delete; /* deleting line codes can be used */
9990 int type;
9991
9992 /*
9993 * FAIL if
9994 * - there is no valid screen
9995 * - the screen has to be redrawn completely
9996 * - the line count is less than one
9997 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009998 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010000 if (!screen_valid(TRUE) || line_count <= 0
10001 || (!force && line_count > p_ttyscroll)
10002#ifdef FEAT_CLIPBOARD
10003 || (clip_star.state != SELECT_CLEARED
10004 && redrawing_for_callback > 0)
10005#endif
10006 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 return FAIL;
10008
10009 /*
10010 * Check if the rest of the current region will become empty.
10011 */
10012 result_empty = row + line_count >= end;
10013
10014 /*
10015 * We can delete lines only when 'db' flag not set or when 'ce' option
10016 * available.
10017 */
10018 can_delete = (*T_DB == NUL || can_clear(T_CE));
10019
10020 /*
10021 * There are six ways to delete lines:
10022 * 0. When in a vertically split window and t_CV isn't set, redraw the
10023 * characters from ScreenLines[].
10024 * 1. Use T_CD if it exists and the result is empty.
10025 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10026 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10027 * none of the other ways work.
10028 * 4. Use T_CE (erase line) if the result is empty.
10029 * 5. Use T_DL (delete line) if it exists.
10030 * 6. redraw the characters from ScreenLines[].
10031 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010032#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10034 type = USE_REDRAW;
10035 else
10036#endif
10037 if (can_clear(T_CD) && result_empty)
10038 type = USE_T_CD;
10039#if defined(__BEOS__) && defined(BEOS_DR8)
10040 /*
10041 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10042 * its internal termcap... this works okay for tests which test *T_DB !=
10043 * NUL. It has the disadvantage that the user cannot use any :set t_*
10044 * command to get T_DB (back) to empty_option, only :set term=... will do
10045 * the trick...
10046 * Anyway, this hack will hopefully go away with the next OS release.
10047 * (Olaf Seibert)
10048 */
10049 else if (row == 0 && T_DB == empty_option
10050 && (line_count == 1 || *T_CDL == NUL))
10051#else
10052 else if (row == 0 && (
10053#ifndef AMIGA
10054 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10055 * up, so use delete-line command */
10056 line_count == 1 ||
10057#endif
10058 *T_CDL == NUL))
10059#endif
10060 type = USE_NL;
10061 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10062 type = USE_T_CDL;
10063 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010064#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 && (wp == NULL || wp->w_width == Columns)
10066#endif
10067 )
10068 type = USE_T_CE;
10069 else if (*T_DL != NUL && can_delete)
10070 type = USE_T_DL;
10071 else if (*T_CDL != NUL && can_delete)
10072 type = USE_T_CDL;
10073 else
10074 return FAIL;
10075
10076#ifdef FEAT_CLIPBOARD
10077 /* Remove a modeless selection when deleting lines halfway the screen or
10078 * not the full width of the screen. */
10079 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010080# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 || (wp != NULL && wp->w_width != Columns)
10082# endif
10083 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010084 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 else
10086 clip_scroll_selection(line_count);
10087#endif
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089#ifdef FEAT_GUI
10090 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10091 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010092 gui_dont_update_cursor(gui.cursor_row >= row + off
10093 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#endif
10095
10096 if (*T_CCS != NUL) /* cursor relative to region */
10097 {
10098 cursor_row = row;
10099 cursor_end = end;
10100 }
10101 else
10102 {
10103 cursor_row = row + off;
10104 cursor_end = end + off;
10105 }
10106
10107 /*
10108 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10109 * Clear the inserted lines in ScreenLines[].
10110 */
10111 row += off;
10112 end += off;
10113 for (i = 0; i < line_count; ++i)
10114 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010115#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (wp != NULL && wp->w_width != Columns)
10117 {
10118 /* need to copy part of a line */
10119 j = row + i;
10120 while ((j += line_count) <= end - 1)
10121 linecopy(j - line_count, j, wp);
10122 j -= line_count;
10123 if (can_clear((char_u *)" "))
10124 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
10125 else
10126 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10127 LineWraps[j] = FALSE;
10128 }
10129 else
10130#endif
10131 {
10132 /* whole width, moving the line pointers is faster */
10133 j = row + i;
10134 temp = LineOffset[j];
10135 while ((j += line_count) <= end - 1)
10136 {
10137 LineOffset[j - line_count] = LineOffset[j];
10138 LineWraps[j - line_count] = LineWraps[j];
10139 }
10140 LineOffset[j - line_count] = temp;
10141 LineWraps[j - line_count] = FALSE;
10142 if (can_clear((char_u *)" "))
10143 lineclear(temp, (int)Columns);
10144 else
10145 lineinvalid(temp, (int)Columns);
10146 }
10147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
10149 screen_stop_highlight();
10150
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010151#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 /* redraw the characters */
10153 if (type == USE_REDRAW)
10154 redraw_block(row, end, wp);
10155 else
10156#endif
10157 if (type == USE_T_CD) /* delete the lines */
10158 {
10159 windgoto(cursor_row, 0);
10160 out_str(T_CD);
10161 screen_start(); /* don't know where cursor is now */
10162 }
10163 else if (type == USE_T_CDL)
10164 {
10165 windgoto(cursor_row, 0);
10166 term_delete_lines(line_count);
10167 screen_start(); /* don't know where cursor is now */
10168 }
10169 /*
10170 * Deleting lines at top of the screen or scroll region: Just scroll
10171 * the whole screen (scroll region) up by outputting newlines on the
10172 * last line.
10173 */
10174 else if (type == USE_NL)
10175 {
10176 windgoto(cursor_end - 1, 0);
10177 for (i = line_count; --i >= 0; )
10178 out_char('\n'); /* cursor will remain on same line */
10179 }
10180 else
10181 {
10182 for (i = line_count; --i >= 0; )
10183 {
10184 if (type == USE_T_DL)
10185 {
10186 windgoto(cursor_row, 0);
10187 out_str(T_DL); /* delete a line */
10188 }
10189 else /* type == USE_T_CE */
10190 {
10191 windgoto(cursor_row + i, 0);
10192 out_str(T_CE); /* erase a line */
10193 }
10194 screen_start(); /* don't know where cursor is now */
10195 }
10196 }
10197
10198 /*
10199 * If the 'db' flag is set, we need to clear the lines that have been
10200 * scrolled up at the bottom of the region.
10201 */
10202 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10203 {
10204 for (i = line_count; i > 0; --i)
10205 {
10206 windgoto(cursor_end - i, 0);
10207 out_str(T_CE); /* erase a line */
10208 screen_start(); /* don't know where cursor is now */
10209 }
10210 }
10211
10212#ifdef FEAT_GUI
10213 gui_can_update_cursor();
10214 if (gui.in_use)
10215 out_flush(); /* always flush after a scroll */
10216#endif
10217
10218 return OK;
10219}
10220
10221/*
10222 * show the current mode and ruler
10223 *
10224 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10225 * If clear_cmdline is FALSE there may be a message there that needs to be
10226 * cleared only if a mode is shown.
10227 * Return the length of the message (0 if no message).
10228 */
10229 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010230showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231{
10232 int need_clear;
10233 int length = 0;
10234 int do_mode;
10235 int attr;
10236 int nwr_save;
10237#ifdef FEAT_INS_EXPAND
10238 int sub_attr;
10239#endif
10240
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010241 do_mode = ((p_smd && msg_silent == 0)
10242 && ((State & INSERT)
10243 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010244 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 if (do_mode || Recording)
10246 {
10247 /*
10248 * Don't show mode right now, when not redrawing or inside a mapping.
10249 * Call char_avail() only when we are going to show something, because
10250 * it takes a bit of time.
10251 */
10252 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10253 {
10254 redraw_cmdline = TRUE; /* show mode later */
10255 return 0;
10256 }
10257
10258 nwr_save = need_wait_return;
10259
10260 /* wait a bit before overwriting an important message */
10261 check_for_delay(FALSE);
10262
10263 /* if the cmdline is more than one line high, erase top lines */
10264 need_clear = clear_cmdline;
10265 if (clear_cmdline && cmdline_row < Rows - 1)
10266 msg_clr_cmdline(); /* will reset clear_cmdline */
10267
10268 /* Position on the last line in the window, column 0 */
10269 msg_pos_mode();
10270 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010271 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 if (do_mode)
10273 {
10274 MSG_PUTS_ATTR("--", attr);
10275#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010276 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010277# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010278 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010279# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010280 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010281# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010282 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010283# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 MSG_PUTS_ATTR(" IM", attr);
10285# else
10286 MSG_PUTS_ATTR(" XIM", attr);
10287# endif
10288#endif
10289#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10290 if (gui.in_use)
10291 {
10292 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010293 {
10294 /* HANGUL */
10295 if (enc_utf8)
10296 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10297 else
10298 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 }
10301#endif
10302#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010303 /* CTRL-X in Insert mode */
10304 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
10306 /* These messages can get long, avoid a wrap in a narrow
10307 * window. Prefer showing edit_submode_extra. */
10308 length = (Rows - msg_row) * Columns - 3;
10309 if (edit_submode_extra != NULL)
10310 length -= vim_strsize(edit_submode_extra);
10311 if (length > 0)
10312 {
10313 if (edit_submode_pre != NULL)
10314 length -= vim_strsize(edit_submode_pre);
10315 if (length - vim_strsize(edit_submode) > 0)
10316 {
10317 if (edit_submode_pre != NULL)
10318 msg_puts_attr(edit_submode_pre, attr);
10319 msg_puts_attr(edit_submode, attr);
10320 }
10321 if (edit_submode_extra != NULL)
10322 {
10323 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10324 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010325 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 else
10327 sub_attr = attr;
10328 msg_puts_attr(edit_submode_extra, sub_attr);
10329 }
10330 }
10331 length = 0;
10332 }
10333 else
10334#endif
10335 {
10336#ifdef FEAT_VREPLACE
10337 if (State & VREPLACE_FLAG)
10338 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10339 else
10340#endif
10341 if (State & REPLACE_FLAG)
10342 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10343 else if (State & INSERT)
10344 {
10345#ifdef FEAT_RIGHTLEFT
10346 if (p_ri)
10347 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10348#endif
10349 MSG_PUTS_ATTR(_(" INSERT"), attr);
10350 }
10351 else if (restart_edit == 'I')
10352 MSG_PUTS_ATTR(_(" (insert)"), attr);
10353 else if (restart_edit == 'R')
10354 MSG_PUTS_ATTR(_(" (replace)"), attr);
10355 else if (restart_edit == 'V')
10356 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10357#ifdef FEAT_RIGHTLEFT
10358 if (p_hkmap)
10359 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10360# ifdef FEAT_FKMAP
10361 if (p_fkmap)
10362 MSG_PUTS_ATTR(farsi_text_5, attr);
10363# endif
10364#endif
10365#ifdef FEAT_KEYMAP
10366 if (State & LANGMAP)
10367 {
10368# ifdef FEAT_ARABIC
10369 if (curwin->w_p_arab)
10370 MSG_PUTS_ATTR(_(" Arabic"), attr);
10371 else
10372# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010373 if (get_keymap_str(curwin, (char_u *)" (%s)",
10374 NameBuff, MAXPATHL))
10375 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 }
10377#endif
10378 if ((State & INSERT) && p_paste)
10379 MSG_PUTS_ATTR(_(" (paste)"), attr);
10380
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 if (VIsual_active)
10382 {
10383 char *p;
10384
10385 /* Don't concatenate separate words to avoid translation
10386 * problems. */
10387 switch ((VIsual_select ? 4 : 0)
10388 + (VIsual_mode == Ctrl_V) * 2
10389 + (VIsual_mode == 'V'))
10390 {
10391 case 0: p = N_(" VISUAL"); break;
10392 case 1: p = N_(" VISUAL LINE"); break;
10393 case 2: p = N_(" VISUAL BLOCK"); break;
10394 case 4: p = N_(" SELECT"); break;
10395 case 5: p = N_(" SELECT LINE"); break;
10396 default: p = N_(" SELECT BLOCK"); break;
10397 }
10398 MSG_PUTS_ATTR(_(p), attr);
10399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 MSG_PUTS_ATTR(" --", attr);
10401 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010402
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 need_clear = TRUE;
10404 }
10405 if (Recording
10406#ifdef FEAT_INS_EXPAND
10407 && edit_submode == NULL /* otherwise it gets too long */
10408#endif
10409 )
10410 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010411 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 need_clear = TRUE;
10413 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010414
10415 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (need_clear || clear_cmdline)
10417 msg_clr_eos();
10418 msg_didout = FALSE; /* overwrite this message */
10419 length = msg_col;
10420 msg_col = 0;
10421 need_wait_return = nwr_save; /* never ask for hit-return for this */
10422 }
10423 else if (clear_cmdline && msg_silent == 0)
10424 /* Clear the whole command line. Will reset "clear_cmdline". */
10425 msg_clr_cmdline();
10426
10427#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 /* In Visual mode the size of the selected area must be redrawn. */
10429 if (VIsual_active)
10430 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431
10432 /* If the last window has no status line, the ruler is after the mode
10433 * message and must be redrawn */
10434 if (redrawing()
10435# ifdef FEAT_WINDOWS
10436 && lastwin->w_status_height == 0
10437# endif
10438 )
10439 win_redr_ruler(lastwin, TRUE);
10440#endif
10441 redraw_cmdline = FALSE;
10442 clear_cmdline = FALSE;
10443
10444 return length;
10445}
10446
10447/*
10448 * Position for a mode message.
10449 */
10450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010451msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452{
10453 msg_col = 0;
10454 msg_row = Rows - 1;
10455}
10456
10457/*
10458 * Delete mode message. Used when ESC is typed which is expected to end
10459 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010460 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 */
10462 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010463unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464{
10465 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010466 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 */
10468 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10469 redraw_cmdline = TRUE; /* delete mode later */
10470 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010471 clearmode();
10472}
10473
10474/*
10475 * Clear the mode message.
10476 */
10477 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010478clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010479{
10480 msg_pos_mode();
10481 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010482 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010483 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484}
10485
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010486 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010487recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010488{
10489 MSG_PUTS_ATTR(_("recording"), attr);
10490 if (!shortmess(SHM_RECORDING))
10491 {
10492 char_u s[4];
10493 sprintf((char *)s, " @%c", Recording);
10494 MSG_PUTS_ATTR(s, attr);
10495 }
10496}
10497
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010498#if defined(FEAT_WINDOWS)
10499/*
10500 * Draw the tab pages line at the top of the Vim window.
10501 */
10502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010503draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010504{
10505 int tabcount = 0;
10506 tabpage_T *tp;
10507 int tabwidth;
10508 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010509 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010510 int attr;
10511 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010512 win_T *cwp;
10513 int wincount;
10514 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010515 int c;
10516 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010517 int attr_sel = HL_ATTR(HLF_TPS);
10518 int attr_nosel = HL_ATTR(HLF_TP);
10519 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010520 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010521 int room;
10522 int use_sep_chars = (t_colors < 8
10523#ifdef FEAT_GUI
10524 && !gui.in_use
10525#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010526#ifdef FEAT_TERMGUICOLORS
10527 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010528#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010529 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010530
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010531 if (ScreenLines == NULL)
10532 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010533 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010534
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010535#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010536 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010537 if (gui_use_tabline())
10538 {
10539 gui_update_tabline();
10540 return;
10541 }
10542#endif
10543
10544 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010545 return;
10546
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010547#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010548
10549 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10550 for (scol = 0; scol < Columns; ++scol)
10551 TabPageIdxs[scol] = 0;
10552
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010553 /* Use the 'tabline' option if it's set. */
10554 if (*p_tal != NUL)
10555 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010556 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010557
10558 /* Check for an error. If there is one we would loop in redrawing the
10559 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010560 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010561 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010562 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010564 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010565 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010566 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010567 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010568#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010569 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010570 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010571 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010572
Bram Moolenaar238a5642006-02-21 22:12:05 +000010573 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10574 if (tabwidth < 6)
10575 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010576
Bram Moolenaar238a5642006-02-21 22:12:05 +000010577 attr = attr_nosel;
10578 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010579 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010580 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10581 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010582 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010583 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010584
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585 if (tp->tp_topframe == topframe)
10586 attr = attr_sel;
10587 if (use_sep_chars && col > 0)
10588 screen_putchar('|', 0, col++, attr);
10589
10590 if (tp->tp_topframe != topframe)
10591 attr = attr_nosel;
10592
10593 screen_putchar(' ', 0, col++, attr);
10594
10595 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010596 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010597 cwp = curwin;
10598 wp = firstwin;
10599 }
10600 else
10601 {
10602 cwp = tp->tp_curwin;
10603 wp = tp->tp_firstwin;
10604 }
10605
10606 modified = FALSE;
10607 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10608 if (bufIsChanged(wp->w_buffer))
10609 modified = TRUE;
10610 if (modified || wincount > 1)
10611 {
10612 if (wincount > 1)
10613 {
10614 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010616 if (col + len >= Columns - 3)
10617 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010619#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010620 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010621#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010622 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010623#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 );
10625 col += len;
10626 }
10627 if (modified)
10628 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10629 screen_putchar(' ', 0, col++, attr);
10630 }
10631
10632 room = scol - col + tabwidth - 1;
10633 if (room > 0)
10634 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010635 /* Get buffer name in NameBuff[] */
10636 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010637 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010638 len = vim_strsize(NameBuff);
10639 p = NameBuff;
10640#ifdef FEAT_MBYTE
10641 if (has_mbyte)
10642 while (len > room)
10643 {
10644 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010645 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 }
10647 else
10648#endif
10649 if (len > room)
10650 {
10651 p += len - room;
10652 len = room;
10653 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010654 if (len > Columns - col - 1)
10655 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010657 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010658 col += len;
10659 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010660 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010661
10662 /* Store the tab page number in TabPageIdxs[], so that
10663 * jump_to_mouse() knows where each one is. */
10664 ++tabcount;
10665 while (scol < col)
10666 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010667 }
10668
Bram Moolenaar238a5642006-02-21 22:12:05 +000010669 if (use_sep_chars)
10670 c = '_';
10671 else
10672 c = ' ';
10673 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010674
10675 /* Put an "X" for closing the current tab if there are several. */
10676 if (first_tabpage->tp_next != NULL)
10677 {
10678 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10679 TabPageIdxs[Columns - 1] = -999;
10680 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010681 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010682
10683 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10684 * set. */
10685 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010686}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010687
10688/*
10689 * Get buffer name for "buf" into NameBuff[].
10690 * Takes care of special buffer names and translates special characters.
10691 */
10692 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010693get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010694{
10695 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010696 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010697 else
10698 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10699 trans_characters(NameBuff, MAXPATHL);
10700}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010701#endif
10702
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10704/*
10705 * Get the character to use in a status line. Get its attributes in "*attr".
10706 */
10707 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010708fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
10710 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010711
10712#ifdef FEAT_TERMINAL
10713 if (bt_terminal(wp->w_buffer))
10714 {
10715 *attr = HL_ATTR(HLF_ST);
10716 if (wp == curwin)
10717 fill = fill_stl;
10718 else
10719 fill = fill_stlnc;
10720 }
10721 else
10722#endif
10723 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010725 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 fill = fill_stl;
10727 }
10728 else
10729 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010730 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 fill = fill_stlnc;
10732 }
10733 /* Use fill when there is highlighting, and highlighting of current
10734 * window differs, or the fillchars differ, or this is not the
10735 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010736 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010737 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 || (fill_stl != fill_stlnc)))
10739 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010740 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 return '^';
10742 return '=';
10743}
10744#endif
10745
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010746#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747/*
10748 * Get the character to use in a separator between vertically split windows.
10749 * Get its attributes in "*attr".
10750 */
10751 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010752fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010754 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 if (*attr == 0 && fill_vert == ' ')
10756 return '|';
10757 else
10758 return fill_vert;
10759}
10760#endif
10761
10762/*
10763 * Return TRUE if redrawing should currently be done.
10764 */
10765 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010766redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010768#ifdef FEAT_EVAL
10769 if (disable_redraw_for_testing)
10770 return 0;
10771 else
10772#endif
10773 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10775}
10776
10777/*
10778 * Return TRUE if printing messages should currently be done.
10779 */
10780 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010781messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782{
10783 return (!(p_lz && char_avail() && !KeyTyped));
10784}
10785
10786/*
10787 * Show current status info in ruler and various other places
10788 * If always is FALSE, only show ruler if position has changed.
10789 */
10790 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010791showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
10793 if (!always && !redrawing())
10794 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010795#ifdef FEAT_INS_EXPAND
10796 if (pum_visible())
10797 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010798# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010799 /* Don't redraw right now, do it later. */
10800 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010801# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010802 return;
10803 }
10804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010806 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010807 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010808 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 else
10811#endif
10812#ifdef FEAT_CMDL_INFO
10813 win_redr_ruler(curwin, always);
10814#endif
10815
10816#ifdef FEAT_TITLE
10817 if (need_maketitle
10818# ifdef FEAT_STL_OPT
10819 || (p_icon && (stl_syntax & STL_IN_ICON))
10820 || (p_title && (stl_syntax & STL_IN_TITLE))
10821# endif
10822 )
10823 maketitle();
10824#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010825#ifdef FEAT_WINDOWS
10826 /* Redraw the tab pages line if needed. */
10827 if (redraw_tabline)
10828 draw_tabline();
10829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832#ifdef FEAT_CMDL_INFO
10833 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010834win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010836#define RULER_BUF_LEN 70
10837 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 int row;
10839 int fillchar;
10840 int attr;
10841 int empty_line = FALSE;
10842 colnr_T virtcol;
10843 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010844 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010846#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 int this_ru_col;
10848 int off = 0;
10849 int width = Columns;
10850# define WITH_OFF(x) x
10851# define WITH_WIDTH(x) x
10852#else
10853# define WITH_OFF(x) 0
10854# define WITH_WIDTH(x) Columns
10855# define this_ru_col ru_col
10856#endif
10857
10858 /* If 'ruler' off or redrawing disabled, don't do anything */
10859 if (!p_ru)
10860 return;
10861
10862 /*
10863 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10864 * after deleting lines, before cursor.lnum is corrected.
10865 */
10866 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10867 return;
10868
10869#ifdef FEAT_INS_EXPAND
10870 /* Don't draw the ruler while doing insert-completion, it might overwrite
10871 * the (long) mode message. */
10872# ifdef FEAT_WINDOWS
10873 if (wp == lastwin && lastwin->w_status_height == 0)
10874# endif
10875 if (edit_submode != NULL)
10876 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010877 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10878 if (pum_visible())
10879 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880#endif
10881
10882#ifdef FEAT_STL_OPT
10883 if (*p_ruf)
10884 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010885 int save_called_emsg = called_emsg;
10886
10887 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010889 if (called_emsg)
10890 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010891 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010892 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 return;
10894 }
10895#endif
10896
10897 /*
10898 * Check if not in Insert mode and the line is empty (will show "0-1").
10899 */
10900 if (!(State & INSERT)
10901 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10902 empty_line = TRUE;
10903
10904 /*
10905 * Only draw the ruler when something changed.
10906 */
10907 validate_virtcol_win(wp);
10908 if ( redraw_cmdline
10909 || always
10910 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10911 || wp->w_cursor.col != wp->w_ru_cursor.col
10912 || wp->w_virtcol != wp->w_ru_virtcol
10913#ifdef FEAT_VIRTUALEDIT
10914 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10915#endif
10916 || wp->w_topline != wp->w_ru_topline
10917 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10918#ifdef FEAT_DIFF
10919 || wp->w_topfill != wp->w_ru_topfill
10920#endif
10921 || empty_line != wp->w_ru_empty)
10922 {
10923 cursor_off();
10924#ifdef FEAT_WINDOWS
10925 if (wp->w_status_height)
10926 {
10927 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010928 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 off = W_WINCOL(wp);
10930 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 }
10932 else
10933#endif
10934 {
10935 row = Rows - 1;
10936 fillchar = ' ';
10937 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010938#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 width = Columns;
10940 off = 0;
10941#endif
10942 }
10943
10944 /* In list mode virtcol needs to be recomputed */
10945 virtcol = wp->w_virtcol;
10946 if (wp->w_p_list && lcs_tab1 == NUL)
10947 {
10948 wp->w_p_list = FALSE;
10949 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10950 wp->w_p_list = TRUE;
10951 }
10952
10953 /*
10954 * Some sprintfs return the length, some return a pointer.
10955 * To avoid portability problems we use strlen() here.
10956 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010957 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10959 ? 0L
10960 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010961 len = STRLEN(buffer);
10962 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10964 (int)virtcol + 1);
10965
10966 /*
10967 * Add a "50%" if there is room for it.
10968 * On the last line, don't print in the last column (scrolls the
10969 * screen up on some terminals).
10970 */
10971 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010972 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 o = i + vim_strsize(buffer + i + 1);
10974#ifdef FEAT_WINDOWS
10975 if (wp->w_status_height == 0) /* can't use last char of screen */
10976#endif
10977 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010978#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 this_ru_col = ru_col - (Columns - width);
10980 if (this_ru_col < 0)
10981 this_ru_col = 0;
10982#endif
10983 /* Never use more than half the window/screen width, leave the other
10984 * half for the filename. */
10985 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10986 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10987 if (this_ru_col + o < WITH_WIDTH(width))
10988 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010989 /* need at least 3 chars left for get_rel_pos() + NUL */
10990 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 {
10992#ifdef FEAT_MBYTE
10993 if (has_mbyte)
10994 i += (*mb_char2bytes)(fillchar, buffer + i);
10995 else
10996#endif
10997 buffer[i++] = fillchar;
10998 ++o;
10999 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011000 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 }
11002 /* Truncate at window boundary. */
11003#ifdef FEAT_MBYTE
11004 if (has_mbyte)
11005 {
11006 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011007 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 {
11009 o += (*mb_ptr2cells)(buffer + i);
11010 if (this_ru_col + o > WITH_WIDTH(width))
11011 {
11012 buffer[i] = NUL;
11013 break;
11014 }
11015 }
11016 }
11017 else
11018#endif
11019 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11020 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11021
11022 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11023 i = redraw_cmdline;
11024 screen_fill(row, row + 1,
11025 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11026 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11027 fillchar, fillchar, attr);
11028 /* don't redraw the cmdline because of showing the ruler */
11029 redraw_cmdline = i;
11030 wp->w_ru_cursor = wp->w_cursor;
11031 wp->w_ru_virtcol = wp->w_virtcol;
11032 wp->w_ru_empty = empty_line;
11033 wp->w_ru_topline = wp->w_topline;
11034 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11035#ifdef FEAT_DIFF
11036 wp->w_ru_topfill = wp->w_topfill;
11037#endif
11038 }
11039}
11040#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011041
11042#if defined(FEAT_LINEBREAK) || defined(PROTO)
11043/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011044 * Return the width of the 'number' and 'relativenumber' column.
11045 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011046 * Otherwise it depends on 'numberwidth' and the line count.
11047 */
11048 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011049number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011050{
11051 int n;
11052 linenr_T lnum;
11053
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011054 if (wp->w_p_rnu && !wp->w_p_nu)
11055 /* cursor line shows "0" */
11056 lnum = wp->w_height;
11057 else
11058 /* cursor line shows absolute line number */
11059 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011060
Bram Moolenaar6b314672015-03-20 15:42:10 +010011061 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011062 return wp->w_nrwidth_width;
11063 wp->w_nrwidth_line_count = lnum;
11064
11065 n = 0;
11066 do
11067 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011068 lnum /= 10;
11069 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011070 } while (lnum > 0);
11071
11072 /* 'numberwidth' gives the minimal width plus one */
11073 if (n < wp->w_p_nuw - 1)
11074 n = wp->w_p_nuw - 1;
11075
11076 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011077 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011078 return n;
11079}
11080#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011081
11082/*
11083 * Return the current cursor column. This is the actual position on the
11084 * screen. First column is 0.
11085 */
11086 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011087screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011088{
11089 return screen_cur_col;
11090}
11091
11092/*
11093 * Return the current cursor row. This is the actual position on the screen.
11094 * First row is 0.
11095 */
11096 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011097screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011098{
11099 return screen_cur_row;
11100}