blob: 54ba9578874d6d4e7f14ff35566943076d415cef [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
125static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
128static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
129static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200131static int win_line(win_T *, linenr_T, int, int, int nochange, proftime_T *syntax_tm);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100133#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100134static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200140# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void start_search_hl(void);
142static void end_search_hl(void);
143static void init_search_hl(win_T *wp);
144static void prepare_search_hl(win_T *wp, linenr_T lnum);
145static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
146static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_start_highlight(int attr);
149static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100156#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100157static void linecopy(int to, int from, win_T *wp);
158static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200160static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100161static void win_rest_invalid(win_T *wp);
162static void msg_pos_mode(void);
163static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200168static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100170#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100180#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200185#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
186/* Can limit syntax highlight time to 'redrawtime'. */
187# define SYN_TIME_LIMIT 1
188#endif
189
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200190#ifdef FEAT_RIGHTLEFT
191# define HAS_RIGHTLEFT(x) x
192#else
193# define HAS_RIGHTLEFT(x) FALSE
194#endif
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
197 * Redraw the current window later, with update_screen(type).
198 * Set must_redraw only if not already set to a higher value.
199 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
200 */
201 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100202redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 redraw_win_later(curwin, type);
205}
206
207 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100208redraw_win_later(
209 win_T *wp,
210 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 if (wp->w_redr_type < type)
213 {
214 wp->w_redr_type = type;
215 if (type >= NOT_VALID)
216 wp->w_lines_valid = 0;
217 if (must_redraw < type) /* must_redraw is the maximum of all windows */
218 must_redraw = type;
219 }
220}
221
222/*
223 * Force a complete redraw later. Also resets the highlighting. To be used
224 * after executing a shell command that messes up the screen.
225 */
226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000230#ifdef FEAT_GUI
231 if (gui.in_use)
232 /* Use a code that will reset gui.highlight_mask in
233 * gui_stop_highlight(). */
234 screen_attr = HL_ALL + 1;
235 else
236#endif
237 /* Use attributes that is very unlikely to appear in text. */
238 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239}
240
241/*
242 * Mark all windows to be redrawn later.
243 */
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 redraw_win_later(wp, type);
252 }
253}
254
255/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000256 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 */
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 redraw_buf_later(curbuf, type);
262}
263
264 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100265redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
267 win_T *wp;
268
269 FOR_ALL_WINDOWS(wp)
270 {
271 if (wp->w_buffer == buf)
272 redraw_win_later(wp, type);
273 }
274}
275
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200276 void
277redraw_buf_and_status_later(buf_T *buf, int type)
278{
279 win_T *wp;
280
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200281#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200282 if (wild_menu_showing != 0)
283 /* Don't redraw while the command line completion is displayed, it
284 * would disappear. */
285 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200286#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200287 FOR_ALL_WINDOWS(wp)
288 {
289 if (wp->w_buffer == buf)
290 {
291 redraw_win_later(wp, type);
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200292#ifdef FEAT_WINDOWS
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200293 wp->w_redr_status = TRUE;
Bram Moolenaar66c0e702017-04-30 20:46:32 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 }
296 }
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300 * Redraw as soon as possible. When the command line is not scrolled redraw
301 * right away and restore what was on the command line.
302 * Return a code indicating what happened.
303 */
304 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100305redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306{
307 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200308 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 int r;
310 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200311 schar_T *screenline; /* copy from ScreenLines[] */
312 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313#ifdef FEAT_MBYTE
314 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200317 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318#endif
319
320 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200321 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 return ret;
323
324 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenline == NULL || screenattr == NULL)
331 ret = 2;
332#ifdef FEAT_MBYTE
333 if (enc_utf8)
334 {
335 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenlineUC == NULL)
338 ret = 2;
339 for (i = 0; i < p_mco; ++i)
340 {
341 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200342 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
349 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
354#endif
355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367#ifdef FEAT_MBYTE
368 if (enc_utf8)
369 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineC[i] + r * cols,
375 ScreenLinesC[i] + LineOffset[cmdline_row + r],
376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 }
378 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382#endif
383 }
384
385 update_screen(0);
386 ret = 3;
387
388 if (must_redraw == 0)
389 {
390 int off = (int)(current_ScreenLine - ScreenLines);
391
392 /* Restore the text displayed in the command line area. */
393 for (r = 0; r < rows; ++r)
394 {
395 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200399 screenattr + r * cols,
400 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200401#ifdef FEAT_MBYTE
402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200417 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200418 }
419 ret = 4;
420 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 }
422
423 vim_free(screenline);
424 vim_free(screenattr);
425#ifdef FEAT_MBYTE
426 if (enc_utf8)
427 {
428 vim_free(screenlineUC);
429 for (i = 0; i < p_mco; ++i)
430 vim_free(screenlineC[i]);
431 }
432 if (enc_dbcs == DBCS_JPNU)
433 vim_free(screenline2);
434#endif
435
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200436 /* Show the intro message when appropriate. */
437 maybe_intro_message();
438
439 setcursor();
440
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441 return ret;
442}
443
444/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 * Invoked after an asynchronous callback is called.
446 * If an echo command was used the cursor needs to be put back where
447 * it belongs. If highlighting was changed a redraw is needed.
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);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200612 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
613 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 type = CLEAR;
615 FOR_ALL_WINDOWS(wp)
616 {
617 if (W_WINROW(wp) < msg_scrolled)
618 {
619 if (W_WINROW(wp) + wp->w_height > msg_scrolled
620 && wp->w_redr_type < REDRAW_TOP
621 && wp->w_lines_valid > 0
622 && wp->w_topline == wp->w_lines[0].wl_lnum)
623 {
624 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
625 wp->w_redr_type = REDRAW_TOP;
626 }
627 else
628 {
629 wp->w_redr_type = NOT_VALID;
630#ifdef FEAT_WINDOWS
631 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
632 <= msg_scrolled)
633 wp->w_redr_status = TRUE;
634#endif
635 }
636 }
637 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200638 if (!no_update)
639 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000640#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000641 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
644 msg_scrolled = 0;
645 need_wait_return = FALSE;
646 }
647
648 /* reset cmdline_row now (may have been changed temporarily) */
649 compute_cmdrow();
650
651 /* Check for changed highlighting */
652 if (need_highlight_changed)
653 highlight_changed();
654
655 if (type == CLEAR) /* first clear screen */
656 {
657 screenclear(); /* will reset clear_cmdline */
658 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200659 /* must_redraw may be set indirectly, avoid another redraw later */
660 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662
663 if (clear_cmdline) /* going to clear cmdline (done below) */
664 check_for_delay(FALSE);
665
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000666#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200667 /* Force redraw when width of 'number' or 'relativenumber' column
668 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000669 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200670 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
671 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672 curwin->w_redr_type = NOT_VALID;
673#endif
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /*
676 * Only start redrawing if there is really something to do.
677 */
678 if (type == INVERTED)
679 update_curswant();
680 if (curwin->w_redr_type < type
681 && !((type == VALID
682 && curwin->w_lines[0].wl_valid
683#ifdef FEAT_DIFF
684 && curwin->w_topfill == curwin->w_old_topfill
685 && curwin->w_botfill == curwin->w_old_botfill
686#endif
687 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000689 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
691 && curwin->w_old_visual_mode == VIsual_mode
692 && (curwin->w_valid & VALID_VIRTCOL)
693 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ))
695 curwin->w_redr_type = type;
696
Bram Moolenaar5a305422006-04-28 22:38:25 +0000697#ifdef FEAT_WINDOWS
698 /* Redraw the tab pages line if needed. */
699 if (redraw_tabline || type >= NOT_VALID)
700 draw_tabline();
701#endif
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703#ifdef FEAT_SYN_HL
704 /*
705 * Correct stored syntax highlighting info for changes in each displayed
706 * buffer. Each buffer must only be done once.
707 */
708 FOR_ALL_WINDOWS(wp)
709 {
710 if (wp->w_buffer->b_mod_set)
711 {
712# ifdef FEAT_WINDOWS
713 win_T *wwp;
714
715 /* Check if we already did this buffer. */
716 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
717 if (wwp->w_buffer == wp->w_buffer)
718 break;
719# endif
720 if (
721# ifdef FEAT_WINDOWS
722 wwp == wp &&
723# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200724 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 syn_stack_apply_changes(wp->w_buffer);
726 }
727 }
728#endif
729
730 /*
731 * Go from top to bottom through the windows, redrawing the ones that need
732 * it.
733 */
734#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
735 did_one = FALSE;
736#endif
737#ifdef FEAT_SEARCH_EXTRA
738 search_hl.rm.regprog = NULL;
739#endif
740 FOR_ALL_WINDOWS(wp)
741 {
742 if (wp->w_redr_type != 0)
743 {
744 cursor_off();
745#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
746 if (!did_one)
747 {
748 did_one = TRUE;
749# ifdef FEAT_SEARCH_EXTRA
750 start_search_hl();
751# endif
752# ifdef FEAT_CLIPBOARD
753 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200754 if (clip_star.available && clip_isautosel_star())
755 clip_update_selection(&clip_star);
756 if (clip_plus.available && clip_isautosel_plus())
757 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758# endif
759#ifdef FEAT_GUI
760 /* Remove the cursor before starting to do anything, because
761 * scrolling may make it difficult to redraw the text under
762 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200763 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200764 {
765 gui_cursor_col = gui.cursor_col;
766 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200768 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771 }
772#endif
773 win_update(wp);
774 }
775
776#ifdef FEAT_WINDOWS
777 /* redraw status line after the window to minimize cursor movement */
778 if (wp->w_redr_status)
779 {
780 cursor_off();
781 win_redr_status(wp);
782 }
783#endif
784 }
785#if defined(FEAT_SEARCH_EXTRA)
786 end_search_hl();
787#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100788#ifdef FEAT_INS_EXPAND
789 /* May need to redraw the popup menu. */
790 if (pum_visible())
791 pum_redraw();
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794#ifdef FEAT_WINDOWS
795 /* Reset b_mod_set flags. Going through all windows is probably faster
796 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200797 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 wp->w_buffer->b_mod_set = FALSE;
799#else
800 curbuf->b_mod_set = FALSE;
801#endif
802
803 updating_screen = FALSE;
804#ifdef FEAT_GUI
805 gui_may_resize_shell();
806#endif
807
808 /* Clear or redraw the command line. Done last, because scrolling may
809 * mess up the command line. */
810 if (clear_cmdline || redraw_cmdline)
811 showmode();
812
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200813 if (no_update)
814 --no_win_do_lines_ins;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200817 if (!did_intro)
818 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 did_intro = TRUE;
820
821#ifdef FEAT_GUI
822 /* Redraw the cursor and update the scrollbars when all screen updating is
823 * done. */
824 if (gui.in_use)
825 {
826 out_flush(); /* required before updating the cursor */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200827 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200828 {
829 /* Put the GUI position where the cursor was, gui_update_cursor()
830 * uses that. */
831 gui.col = gui_cursor_col;
832 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200833# ifdef FEAT_MBYTE
834 gui.col = mb_fix_col(gui.col, gui.row);
835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200837 screen_cur_col = gui.col;
838 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 gui_update_scrollbars(FALSE);
841 }
842#endif
843}
844
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100845#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
846/*
847 * Prepare for updating one or more windows.
848 * Caller must check for "updating_screen" already set to avoid recursiveness.
849 */
850 static void
851update_prepare(void)
852{
853 cursor_off();
854 updating_screen = TRUE;
855#ifdef FEAT_GUI
856 /* Remove the cursor before starting to do anything, because scrolling may
857 * make it difficult to redraw the text under it. */
858 if (gui.in_use)
859 gui_undraw_cursor();
860#endif
861#ifdef FEAT_SEARCH_EXTRA
862 start_search_hl();
863#endif
864}
865
866/*
867 * Finish updating one or more windows.
868 */
869 static void
870update_finish(void)
871{
872 if (redraw_cmdline)
873 showmode();
874
875# ifdef FEAT_SEARCH_EXTRA
876 end_search_hl();
877# endif
878
879 updating_screen = FALSE;
880
881# ifdef FEAT_GUI
882 gui_may_resize_shell();
883
884 /* Redraw the cursor and update the scrollbars when all screen updating is
885 * done. */
886 if (gui.in_use)
887 {
888 out_flush(); /* required before updating the cursor */
889 gui_update_cursor(FALSE, FALSE);
890 gui_update_scrollbars(FALSE);
891 }
892# endif
893}
894#endif
895
Bram Moolenaar860cae12010-06-05 23:22:07 +0200896#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200897/*
898 * Return TRUE if the cursor line in window "wp" may be concealed, according
899 * to the 'concealcursor' option.
900 */
901 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100902conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200903{
904 int c;
905
906 if (*wp->w_p_cocu == NUL)
907 return FALSE;
908 if (get_real_state() & VISUAL)
909 c = 'v';
910 else if (State & INSERT)
911 c = 'i';
912 else if (State & NORMAL)
913 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200914 else if (State & CMDLINE)
915 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200916 else
917 return FALSE;
918 return vim_strchr(wp->w_p_cocu, c) != NULL;
919}
920
921/*
922 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
923 */
924 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100925conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200926{
927 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
928 {
929 need_cursor_line_redraw = TRUE;
930 /* Need to recompute cursor column, e.g., when starting Visual mode
931 * without concealing. */
932 curs_columns(TRUE);
933 }
934}
935
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100937update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200938{
939 int row;
940 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200941#ifdef SYN_TIME_LIMIT
942 proftime_T syntax_tm;
943#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200944
Bram Moolenaar908be432016-05-24 10:51:30 +0200945 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100946 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200947 return;
948
Bram Moolenaar860cae12010-06-05 23:22:07 +0200949 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200950 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200952#ifdef SYN_TIME_LIMIT
953 /* Set the time limit to 'redrawtime'. */
954 profile_setlimit(p_rdt, &syntax_tm);
955#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100956 update_prepare();
957
Bram Moolenaar860cae12010-06-05 23:22:07 +0200958 row = 0;
959 for (j = 0; j < wp->w_lines_valid; ++j)
960 {
961 if (lnum == wp->w_lines[j].wl_lnum)
962 {
963 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200964# ifdef FEAT_SEARCH_EXTRA
965 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200966 start_search_hl();
967 prepare_search_hl(wp, lnum);
968# endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200969 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE,
970#ifdef SYN_TIME_LIMIT
971 &syntax_tm
972#else
973 NULL
974#endif
975 );
Bram Moolenaar860cae12010-06-05 23:22:07 +0200976# if defined(FEAT_SEARCH_EXTRA)
977 end_search_hl();
978# endif
979 break;
980 }
981 row += wp->w_lines[j].wl_size;
982 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100983
984 update_finish();
Bram Moolenaar860cae12010-06-05 23:22:07 +0200985 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200986 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988#endif
989
990#if defined(FEAT_SIGNS) || defined(PROTO)
991 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100992update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 win_T *wp;
995 int doit = FALSE;
996
997# ifdef FEAT_FOLDING
998 win_foldinfo.fi_level = 0;
999# endif
1000
1001 /* update/delete a specific mark */
1002 FOR_ALL_WINDOWS(wp)
1003 {
1004 if (buf != NULL && lnum > 0)
1005 {
1006 if (wp->w_buffer == buf && lnum >= wp->w_topline
1007 && lnum < wp->w_botline)
1008 {
1009 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1010 wp->w_redraw_top = lnum;
1011 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1012 wp->w_redraw_bot = lnum;
1013 redraw_win_later(wp, VALID);
1014 }
1015 }
1016 else
1017 redraw_win_later(wp, VALID);
1018 if (wp->w_redr_type != 0)
1019 doit = TRUE;
1020 }
1021
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001022 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001023 * happening (recursive call), messages on the screen or still starting up.
1024 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001026 || State == ASKMORE || State == HITRETURN
1027 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001028#ifdef FEAT_GUI
1029 || gui.starting
1030#endif
1031 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 return;
1033
1034 /* update all windows that need updating */
1035 update_prepare();
1036
1037# ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001038 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 if (wp->w_redr_type != 0)
1041 win_update(wp);
1042 if (wp->w_redr_status)
1043 win_redr_status(wp);
1044 }
1045# else
1046 if (curwin->w_redr_type != 0)
1047 win_update(curwin);
1048# endif
1049
1050 update_finish();
1051}
1052#endif
1053
1054
1055#if defined(FEAT_GUI) || defined(PROTO)
1056/*
1057 * Update a single window, its status line and maybe the command line msg.
1058 * Used for the GUI scrollbar.
1059 */
1060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001061updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001063 /* return if already busy updating */
1064 if (updating_screen)
1065 return;
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 update_prepare();
1068
1069#ifdef FEAT_CLIPBOARD
1070 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001071 if (clip_star.available && clip_isautosel_star())
1072 clip_update_selection(&clip_star);
1073 if (clip_plus.available && clip_isautosel_plus())
1074 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001081 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001082 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (wp->w_redr_status
1085# ifdef FEAT_CMDL_INFO
1086 || p_ru
1087# endif
1088# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001089 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090# endif
1091 )
1092 win_redr_status(wp);
1093#endif
1094
1095 update_finish();
1096}
1097#endif
1098
1099/*
1100 * Update a single window.
1101 *
1102 * This may cause the windows below it also to be redrawn (when clearing the
1103 * screen or scrolling lines).
1104 *
1105 * How the window is redrawn depends on wp->w_redr_type. Each type also
1106 * implies the one below it.
1107 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001108 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1110 * INVERTED redraw the changed part of the Visual area
1111 * INVERTED_ALL redraw the whole Visual area
1112 * VALID 1. scroll up/down to adjust for a changed w_topline
1113 * 2. update lines at the top when scrolled down
1114 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001115 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * b_mod_top and b_mod_bot.
1117 * - if wp->w_redraw_top non-zero, redraw lines between
1118 * wp->w_redraw_top and wp->w_redr_bot.
1119 * - continue redrawing when syntax status is invalid.
1120 * 4. if scrolled up, update lines at the bottom.
1121 * This results in three areas that may need updating:
1122 * top: from first row to top_end (when scrolled down)
1123 * mid: from mid_start to mid_end (update inversion or changed text)
1124 * bot: from bot_start to last row (when scrolled up)
1125 */
1126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001127win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 buf_T *buf = wp->w_buffer;
1130 int type;
1131 int top_end = 0; /* Below last row of the top area that needs
1132 updating. 0 when no top area updating. */
1133 int mid_start = 999;/* first row of the mid area that needs
1134 updating. 999 when no mid area updating. */
1135 int mid_end = 0; /* Below last row of the mid area that needs
1136 updating. 0 when no mid area updating. */
1137 int bot_start = 999;/* first row of the bot area that needs
1138 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int scrolled_down = FALSE; /* TRUE when scrolled down when
1140 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001142 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 int top_to_mod = FALSE; /* redraw above mod_top */
1144#endif
1145
1146 int row; /* current window row to display */
1147 linenr_T lnum; /* current buffer lnum to display */
1148 int idx; /* current index in w_lines[] */
1149 int srow; /* starting row of the current line */
1150
1151 int eof = FALSE; /* if TRUE, we hit the end of the file */
1152 int didline = FALSE; /* if TRUE, we finished the last line */
1153 int i;
1154 long j;
1155 static int recursive = FALSE; /* being called recursively */
1156 int old_botline = wp->w_botline;
1157#ifdef FEAT_FOLDING
1158 long fold_count;
1159#endif
1160#ifdef FEAT_SYN_HL
1161 /* remember what happened to the previous line, to know if
1162 * check_visual_highlight() can be used */
1163#define DID_NONE 1 /* didn't update a line */
1164#define DID_LINE 2 /* updated a normal line */
1165#define DID_FOLD 3 /* updated a folded line */
1166 int did_update = DID_NONE;
1167 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1168#endif
1169 linenr_T mod_top = 0;
1170 linenr_T mod_bot = 0;
1171#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1172 int save_got_int;
1173#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001174#ifdef SYN_TIME_LIMIT
1175 proftime_T syntax_tm;
1176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 type = wp->w_redr_type;
1179
1180 if (type == NOT_VALID)
1181 {
1182#ifdef FEAT_WINDOWS
1183 wp->w_redr_status = TRUE;
1184#endif
1185 wp->w_lines_valid = 0;
1186 }
1187
1188 /* Window is zero-height: nothing to draw. */
1189 if (wp->w_height == 0)
1190 {
1191 wp->w_redr_type = 0;
1192 return;
1193 }
1194
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001195#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 /* Window is zero-width: Only need to draw the separator. */
1197 if (wp->w_width == 0)
1198 {
1199 /* draw the vertical separator right of this window */
1200 draw_vsep_win(wp, 0);
1201 wp->w_redr_type = 0;
1202 return;
1203 }
1204#endif
1205
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001206#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +02001207 /* If this window contains a terminal, redraw works completely differently.
1208 */
1209 if (term_update_window(wp) == OK)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001210 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001211 wp->w_redr_type = 0;
1212 return;
1213 }
1214#endif
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001217 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#endif
1219
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001220#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001221 /* Force redraw when width of 'number' or 'relativenumber' column
1222 * changes. */
1223 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001224 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001225 {
1226 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001227 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001228 }
1229 else
1230#endif
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1233 {
1234 /*
1235 * When there are both inserted/deleted lines and specific lines to be
1236 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1237 * everything (only happens when redrawing is off for while).
1238 */
1239 type = NOT_VALID;
1240 }
1241 else
1242 {
1243 /*
1244 * Set mod_top to the first line that needs displaying because of
1245 * changes. Set mod_bot to the first line after the changes.
1246 */
1247 mod_top = wp->w_redraw_top;
1248 if (wp->w_redraw_bot != 0)
1249 mod_bot = wp->w_redraw_bot + 1;
1250 else
1251 mod_bot = 0;
1252 wp->w_redraw_top = 0; /* reset for next time */
1253 wp->w_redraw_bot = 0;
1254 if (buf->b_mod_set)
1255 {
1256 if (mod_top == 0 || mod_top > buf->b_mod_top)
1257 {
1258 mod_top = buf->b_mod_top;
1259#ifdef FEAT_SYN_HL
1260 /* Need to redraw lines above the change that may be included
1261 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001262 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001264 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (mod_top < 1)
1266 mod_top = 1;
1267 }
1268#endif
1269 }
1270 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1271 mod_bot = buf->b_mod_bot;
1272
1273#ifdef FEAT_SEARCH_EXTRA
1274 /* When 'hlsearch' is on and using a multi-line search pattern, a
1275 * change in one line may make the Search highlighting in a
1276 * previous line invalid. Simple solution: redraw all visible
1277 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001278 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001280 if (search_hl.rm.regprog != NULL
1281 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001283 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001284 {
1285 cur = wp->w_match_head;
1286 while (cur != NULL)
1287 {
1288 if (cur->match.regprog != NULL
1289 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001290 {
1291 top_to_mod = TRUE;
1292 break;
1293 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001294 cur = cur->next;
1295 }
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297#endif
1298 }
1299#ifdef FEAT_FOLDING
1300 if (mod_top != 0 && hasAnyFolding(wp))
1301 {
1302 linenr_T lnumt, lnumb;
1303
1304 /*
1305 * A change in a line can cause lines above it to become folded or
1306 * unfolded. Find the top most buffer line that may be affected.
1307 * If the line was previously folded and displayed, get the first
1308 * line of that fold. If the line is folded now, get the first
1309 * folded line. Use the minimum of these two.
1310 */
1311
1312 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1313 * the line below it. If there is no valid entry, use w_topline.
1314 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1315 * to this line. If there is no valid entry, use MAXLNUM. */
1316 lnumt = wp->w_topline;
1317 lnumb = MAXLNUM;
1318 for (i = 0; i < wp->w_lines_valid; ++i)
1319 if (wp->w_lines[i].wl_valid)
1320 {
1321 if (wp->w_lines[i].wl_lastlnum < mod_top)
1322 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1323 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1324 {
1325 lnumb = wp->w_lines[i].wl_lnum;
1326 /* When there is a fold column it might need updating
1327 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001328 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 ++lnumb;
1330 }
1331 }
1332
1333 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1334 if (mod_top > lnumt)
1335 mod_top = lnumt;
1336
1337 /* Now do the same for the bottom line (one above mod_bot). */
1338 --mod_bot;
1339 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1340 ++mod_bot;
1341 if (mod_bot < lnumb)
1342 mod_bot = lnumb;
1343 }
1344#endif
1345
1346 /* When a change starts above w_topline and the end is below
1347 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348 * If the end of the change is above w_topline: do like no change was
1349 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (mod_top != 0 && mod_top < wp->w_topline)
1351 {
1352 if (mod_bot > wp->w_topline)
1353 mod_top = wp->w_topline;
1354#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001355 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 top_end = 1;
1357#endif
1358 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001359
1360 /* When line numbers are displayed need to redraw all lines below
1361 * inserted/deleted lines. */
1362 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1363 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365
1366 /*
1367 * When only displaying the lines at the top, set top_end. Used when
1368 * window has scrolled down for msg_scrolled.
1369 */
1370 if (type == REDRAW_TOP)
1371 {
1372 j = 0;
1373 for (i = 0; i < wp->w_lines_valid; ++i)
1374 {
1375 j += wp->w_lines[i].wl_size;
1376 if (j >= wp->w_upd_rows)
1377 {
1378 top_end = j;
1379 break;
1380 }
1381 }
1382 if (top_end == 0)
1383 /* not found (cannot happen?): redraw everything */
1384 type = NOT_VALID;
1385 else
1386 /* top area defined, the rest is VALID */
1387 type = VALID;
1388 }
1389
Bram Moolenaar367329b2007-08-30 11:53:22 +00001390 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001391 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1392 * non-zero and thus not FALSE) will indicate that screenclear() was not
1393 * called. */
1394 if (screen_cleared)
1395 screen_cleared = MAYBE;
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /*
1398 * If there are no changes on the screen that require a complete redraw,
1399 * handle three cases:
1400 * 1: we are off the top of the screen by a few lines: scroll down
1401 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1402 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1403 * w_lines[] that needs updating.
1404 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001405 if ((type == VALID || type == SOME_VALID
1406 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#ifdef FEAT_DIFF
1408 && !wp->w_botfill && !wp->w_old_botfill
1409#endif
1410 )
1411 {
1412 if (mod_top != 0 && wp->w_topline == mod_top)
1413 {
1414 /*
1415 * w_topline is the first changed line, the scrolling will be done
1416 * further down.
1417 */
1418 }
1419 else if (wp->w_lines[0].wl_valid
1420 && (wp->w_topline < wp->w_lines[0].wl_lnum
1421#ifdef FEAT_DIFF
1422 || (wp->w_topline == wp->w_lines[0].wl_lnum
1423 && wp->w_topfill > wp->w_old_topfill)
1424#endif
1425 ))
1426 {
1427 /*
1428 * New topline is above old topline: May scroll down.
1429 */
1430#ifdef FEAT_FOLDING
1431 if (hasAnyFolding(wp))
1432 {
1433 linenr_T ln;
1434
1435 /* count the number of lines we are off, counting a sequence
1436 * of folded lines as one */
1437 j = 0;
1438 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1439 {
1440 ++j;
1441 if (j >= wp->w_height - 2)
1442 break;
1443 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1444 }
1445 }
1446 else
1447#endif
1448 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1449 if (j < wp->w_height - 2) /* not too far off */
1450 {
1451 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1452#ifdef FEAT_DIFF
1453 /* insert extra lines for previously invisible filler lines */
1454 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1455 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1456 - wp->w_old_topfill;
1457#endif
1458 if (i < wp->w_height - 2) /* less than a screen off */
1459 {
1460 /*
1461 * Try to insert the correct number of lines.
1462 * If not the last window, delete the lines at the bottom.
1463 * win_ins_lines may fail when the terminal can't do it.
1464 */
1465 if (i > 0)
1466 check_for_delay(FALSE);
1467 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1468 {
1469 if (wp->w_lines_valid != 0)
1470 {
1471 /* Need to update rows that are new, stop at the
1472 * first one that scrolled down. */
1473 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 /* Move the entries that were scrolled, disable
1477 * the entries for the lines to be redrawn. */
1478 if ((wp->w_lines_valid += j) > wp->w_height)
1479 wp->w_lines_valid = wp->w_height;
1480 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1481 wp->w_lines[idx] = wp->w_lines[idx - j];
1482 while (idx >= 0)
1483 wp->w_lines[idx--].wl_valid = FALSE;
1484 }
1485 }
1486 else
1487 mid_start = 0; /* redraw all lines */
1488 }
1489 else
1490 mid_start = 0; /* redraw all lines */
1491 }
1492 else
1493 mid_start = 0; /* redraw all lines */
1494 }
1495 else
1496 {
1497 /*
1498 * New topline is at or below old topline: May scroll up.
1499 * When topline didn't change, find first entry in w_lines[] that
1500 * needs updating.
1501 */
1502
1503 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1504 j = -1;
1505 row = 0;
1506 for (i = 0; i < wp->w_lines_valid; i++)
1507 {
1508 if (wp->w_lines[i].wl_valid
1509 && wp->w_lines[i].wl_lnum == wp->w_topline)
1510 {
1511 j = i;
1512 break;
1513 }
1514 row += wp->w_lines[i].wl_size;
1515 }
1516 if (j == -1)
1517 {
1518 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1519 * lines */
1520 mid_start = 0;
1521 }
1522 else
1523 {
1524 /*
1525 * Try to delete the correct number of lines.
1526 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1527 */
1528#ifdef FEAT_DIFF
1529 /* If the topline didn't change, delete old filler lines,
1530 * otherwise delete filler lines of the new topline... */
1531 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1532 row += wp->w_old_topfill;
1533 else
1534 row += diff_check_fill(wp, wp->w_topline);
1535 /* ... but don't delete new filler lines. */
1536 row -= wp->w_topfill;
1537#endif
1538 if (row > 0)
1539 {
1540 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001541 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1542 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 bot_start = wp->w_height - row;
1544 else
1545 mid_start = 0; /* redraw all lines */
1546 }
1547 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1548 {
1549 /*
1550 * Skip the lines (below the deleted lines) that are still
1551 * valid and don't need redrawing. Copy their info
1552 * upwards, to compensate for the deleted lines. Set
1553 * bot_start to the first row that needs redrawing.
1554 */
1555 bot_start = 0;
1556 idx = 0;
1557 for (;;)
1558 {
1559 wp->w_lines[idx] = wp->w_lines[j];
1560 /* stop at line that didn't fit, unless it is still
1561 * valid (no lines deleted) */
1562 if (row > 0 && bot_start + row
1563 + (int)wp->w_lines[j].wl_size > wp->w_height)
1564 {
1565 wp->w_lines_valid = idx + 1;
1566 break;
1567 }
1568 bot_start += wp->w_lines[idx++].wl_size;
1569
1570 /* stop at the last valid entry in w_lines[].wl_size */
1571 if (++j >= wp->w_lines_valid)
1572 {
1573 wp->w_lines_valid = idx;
1574 break;
1575 }
1576 }
1577#ifdef FEAT_DIFF
1578 /* Correct the first entry for filler lines at the top
1579 * when it won't get updated below. */
1580 if (wp->w_p_diff && bot_start > 0)
1581 wp->w_lines[0].wl_size =
1582 plines_win_nofill(wp, wp->w_topline, TRUE)
1583 + wp->w_topfill;
1584#endif
1585 }
1586 }
1587 }
1588
1589 /* When starting redraw in the first line, redraw all lines. When
1590 * there is only one window it's probably faster to clear the screen
1591 * first. */
1592 if (mid_start == 0)
1593 {
1594 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001595 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001596 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001597 /* Clear the screen when it was not done by win_del_lines() or
1598 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1599 * then. */
1600 if (screen_cleared != TRUE)
1601 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001602#ifdef FEAT_WINDOWS
1603 /* The screen was cleared, redraw the tab pages line. */
1604 if (redraw_tabline)
1605 draw_tabline();
1606#endif
1607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001609
1610 /* When win_del_lines() or win_ins_lines() caused the screen to be
1611 * cleared (only happens for the first window) or when screenclear()
1612 * was called directly above, "must_redraw" will have been set to
1613 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1614 if (screen_cleared == TRUE)
1615 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617 else
1618 {
1619 /* Not VALID or INVERTED: redraw all lines. */
1620 mid_start = 0;
1621 mid_end = wp->w_height;
1622 }
1623
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001624 if (type == SOME_VALID)
1625 {
1626 /* SOME_VALID: redraw all lines. */
1627 mid_start = 0;
1628 mid_end = wp->w_height;
1629 type = NOT_VALID;
1630 }
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 /* check if we are updating or removing the inverted part */
1633 if ((VIsual_active && buf == curwin->w_buffer)
1634 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1635 {
1636 linenr_T from, to;
1637
1638 if (VIsual_active)
1639 {
1640 if (VIsual_active
1641 && (VIsual_mode != wp->w_old_visual_mode
1642 || type == INVERTED_ALL))
1643 {
1644 /*
1645 * If the type of Visual selection changed, redraw the whole
1646 * selection. Also when the ownership of the X selection is
1647 * gained or lost.
1648 */
1649 if (curwin->w_cursor.lnum < VIsual.lnum)
1650 {
1651 from = curwin->w_cursor.lnum;
1652 to = VIsual.lnum;
1653 }
1654 else
1655 {
1656 from = VIsual.lnum;
1657 to = curwin->w_cursor.lnum;
1658 }
1659 /* redraw more when the cursor moved as well */
1660 if (wp->w_old_cursor_lnum < from)
1661 from = wp->w_old_cursor_lnum;
1662 if (wp->w_old_cursor_lnum > to)
1663 to = wp->w_old_cursor_lnum;
1664 if (wp->w_old_visual_lnum < from)
1665 from = wp->w_old_visual_lnum;
1666 if (wp->w_old_visual_lnum > to)
1667 to = wp->w_old_visual_lnum;
1668 }
1669 else
1670 {
1671 /*
1672 * Find the line numbers that need to be updated: The lines
1673 * between the old cursor position and the current cursor
1674 * position. Also check if the Visual position changed.
1675 */
1676 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1677 {
1678 from = curwin->w_cursor.lnum;
1679 to = wp->w_old_cursor_lnum;
1680 }
1681 else
1682 {
1683 from = wp->w_old_cursor_lnum;
1684 to = curwin->w_cursor.lnum;
1685 if (from == 0) /* Visual mode just started */
1686 from = to;
1687 }
1688
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001689 if (VIsual.lnum != wp->w_old_visual_lnum
1690 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 if (wp->w_old_visual_lnum < from
1693 && wp->w_old_visual_lnum != 0)
1694 from = wp->w_old_visual_lnum;
1695 if (wp->w_old_visual_lnum > to)
1696 to = wp->w_old_visual_lnum;
1697 if (VIsual.lnum < from)
1698 from = VIsual.lnum;
1699 if (VIsual.lnum > to)
1700 to = VIsual.lnum;
1701 }
1702 }
1703
1704 /*
1705 * If in block mode and changed column or curwin->w_curswant:
1706 * update all lines.
1707 * First compute the actual start and end column.
1708 */
1709 if (VIsual_mode == Ctrl_V)
1710 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001711 colnr_T fromc, toc;
1712#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1713 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar404406a2014-10-09 13:24:43 +02001715 if (curwin->w_p_lbr)
1716 ve_flags = VE_ALL;
1717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001719#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1720 ve_flags = save_ve_flags;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 ++toc;
1723 if (curwin->w_curswant == MAXCOL)
1724 toc = MAXCOL;
1725
1726 if (fromc != wp->w_old_cursor_fcol
1727 || toc != wp->w_old_cursor_lcol)
1728 {
1729 if (from > VIsual.lnum)
1730 from = VIsual.lnum;
1731 if (to < VIsual.lnum)
1732 to = VIsual.lnum;
1733 }
1734 wp->w_old_cursor_fcol = fromc;
1735 wp->w_old_cursor_lcol = toc;
1736 }
1737 }
1738 else
1739 {
1740 /* Use the line numbers of the old Visual area. */
1741 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1742 {
1743 from = wp->w_old_cursor_lnum;
1744 to = wp->w_old_visual_lnum;
1745 }
1746 else
1747 {
1748 from = wp->w_old_visual_lnum;
1749 to = wp->w_old_cursor_lnum;
1750 }
1751 }
1752
1753 /*
1754 * There is no need to update lines above the top of the window.
1755 */
1756 if (from < wp->w_topline)
1757 from = wp->w_topline;
1758
1759 /*
1760 * If we know the value of w_botline, use it to restrict the update to
1761 * the lines that are visible in the window.
1762 */
1763 if (wp->w_valid & VALID_BOTLINE)
1764 {
1765 if (from >= wp->w_botline)
1766 from = wp->w_botline - 1;
1767 if (to >= wp->w_botline)
1768 to = wp->w_botline - 1;
1769 }
1770
1771 /*
1772 * Find the minimal part to be updated.
1773 * Watch out for scrolling that made entries in w_lines[] invalid.
1774 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1775 * top_end; need to redraw from top_end to the "to" line.
1776 * A middle mouse click with a Visual selection may change the text
1777 * above the Visual area and reset wl_valid, do count these for
1778 * mid_end (in srow).
1779 */
1780 if (mid_start > 0)
1781 {
1782 lnum = wp->w_topline;
1783 idx = 0;
1784 srow = 0;
1785 if (scrolled_down)
1786 mid_start = top_end;
1787 else
1788 mid_start = 0;
1789 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1790 {
1791 if (wp->w_lines[idx].wl_valid)
1792 mid_start += wp->w_lines[idx].wl_size;
1793 else if (!scrolled_down)
1794 srow += wp->w_lines[idx].wl_size;
1795 ++idx;
1796# ifdef FEAT_FOLDING
1797 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1798 lnum = wp->w_lines[idx].wl_lnum;
1799 else
1800# endif
1801 ++lnum;
1802 }
1803 srow += mid_start;
1804 mid_end = wp->w_height;
1805 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1806 {
1807 if (wp->w_lines[idx].wl_valid
1808 && wp->w_lines[idx].wl_lnum >= to + 1)
1809 {
1810 /* Only update until first row of this line */
1811 mid_end = srow;
1812 break;
1813 }
1814 srow += wp->w_lines[idx].wl_size;
1815 }
1816 }
1817 }
1818
1819 if (VIsual_active && buf == curwin->w_buffer)
1820 {
1821 wp->w_old_visual_mode = VIsual_mode;
1822 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1823 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001824 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 wp->w_old_curswant = curwin->w_curswant;
1826 }
1827 else
1828 {
1829 wp->w_old_visual_mode = 0;
1830 wp->w_old_cursor_lnum = 0;
1831 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001832 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1836 /* reset got_int, otherwise regexp won't work */
1837 save_got_int = got_int;
1838 got_int = 0;
1839#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001840#ifdef SYN_TIME_LIMIT
1841 /* Set the time limit to 'redrawtime'. */
1842 profile_setlimit(p_rdt, &syntax_tm);
1843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#ifdef FEAT_FOLDING
1845 win_foldinfo.fi_level = 0;
1846#endif
1847
1848 /*
1849 * Update all the window rows.
1850 */
1851 idx = 0; /* first entry in w_lines[].wl_size */
1852 row = 0;
1853 srow = 0;
1854 lnum = wp->w_topline; /* first line shown in window */
1855 for (;;)
1856 {
1857 /* stop updating when reached the end of the window (check for _past_
1858 * the end of the window is at the end of the loop) */
1859 if (row == wp->w_height)
1860 {
1861 didline = TRUE;
1862 break;
1863 }
1864
1865 /* stop updating when hit the end of the file */
1866 if (lnum > buf->b_ml.ml_line_count)
1867 {
1868 eof = TRUE;
1869 break;
1870 }
1871
1872 /* Remember the starting row of the line that is going to be dealt
1873 * with. It is used further down when the line doesn't fit. */
1874 srow = row;
1875
1876 /*
1877 * Update a line when it is in an area that needs updating, when it
1878 * has changes or w_lines[idx] is invalid.
1879 * bot_start may be halfway a wrapped line after using
1880 * win_del_lines(), check if the current line includes it.
1881 * When syntax folding is being used, the saved syntax states will
1882 * already have been updated, we can't see where the syntax state is
1883 * the same again, just update until the end of the window.
1884 */
1885 if (row < top_end
1886 || (row >= mid_start && row < mid_end)
1887#ifdef FEAT_SEARCH_EXTRA
1888 || top_to_mod
1889#endif
1890 || idx >= wp->w_lines_valid
1891 || (row + wp->w_lines[idx].wl_size > bot_start)
1892 || (mod_top != 0
1893 && (lnum == mod_top
1894 || (lnum >= mod_top
1895 && (lnum < mod_bot
1896#ifdef FEAT_SYN_HL
1897 || did_update == DID_FOLD
1898 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001899 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 && (
1901# ifdef FEAT_FOLDING
1902 (foldmethodIsSyntax(wp)
1903 && hasAnyFolding(wp)) ||
1904# endif
1905 syntax_check_changed(lnum)))
1906#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001907#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001908 /* match in fixed position might need redraw
1909 * if lines were inserted or deleted */
1910 || (wp->w_match_head != NULL
1911 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 )))))
1914 {
1915#ifdef FEAT_SEARCH_EXTRA
1916 if (lnum == mod_top)
1917 top_to_mod = FALSE;
1918#endif
1919
1920 /*
1921 * When at start of changed lines: May scroll following lines
1922 * up or down to minimize redrawing.
1923 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001924 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 if (lnum == mod_top
1927 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001928 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
1930 int old_rows = 0;
1931 int new_rows = 0;
1932 int xtra_rows;
1933 linenr_T l;
1934
1935 /* Count the old number of window rows, using w_lines[], which
1936 * should still contain the sizes for the lines as they are
1937 * currently displayed. */
1938 for (i = idx; i < wp->w_lines_valid; ++i)
1939 {
1940 /* Only valid lines have a meaningful wl_lnum. Invalid
1941 * lines are part of the changed area. */
1942 if (wp->w_lines[i].wl_valid
1943 && wp->w_lines[i].wl_lnum == mod_bot)
1944 break;
1945 old_rows += wp->w_lines[i].wl_size;
1946#ifdef FEAT_FOLDING
1947 if (wp->w_lines[i].wl_valid
1948 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1949 {
1950 /* Must have found the last valid entry above mod_bot.
1951 * Add following invalid entries. */
1952 ++i;
1953 while (i < wp->w_lines_valid
1954 && !wp->w_lines[i].wl_valid)
1955 old_rows += wp->w_lines[i++].wl_size;
1956 break;
1957 }
1958#endif
1959 }
1960
1961 if (i >= wp->w_lines_valid)
1962 {
1963 /* We can't find a valid line below the changed lines,
1964 * need to redraw until the end of the window.
1965 * Inserting/deleting lines has no use. */
1966 bot_start = 0;
1967 }
1968 else
1969 {
1970 /* Able to count old number of rows: Count new window
1971 * rows, and may insert/delete lines */
1972 j = idx;
1973 for (l = lnum; l < mod_bot; ++l)
1974 {
1975#ifdef FEAT_FOLDING
1976 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1977 ++new_rows;
1978 else
1979#endif
1980#ifdef FEAT_DIFF
1981 if (l == wp->w_topline)
1982 new_rows += plines_win_nofill(wp, l, TRUE)
1983 + wp->w_topfill;
1984 else
1985#endif
1986 new_rows += plines_win(wp, l, TRUE);
1987 ++j;
1988 if (new_rows > wp->w_height - row - 2)
1989 {
1990 /* it's getting too much, must redraw the rest */
1991 new_rows = 9999;
1992 break;
1993 }
1994 }
1995 xtra_rows = new_rows - old_rows;
1996 if (xtra_rows < 0)
1997 {
1998 /* May scroll text up. If there is not enough
1999 * remaining text or scrolling fails, must redraw the
2000 * rest. If scrolling works, must redraw the text
2001 * below the scrolled text. */
2002 if (row - xtra_rows >= wp->w_height - 2)
2003 mod_bot = MAXLNUM;
2004 else
2005 {
2006 check_for_delay(FALSE);
2007 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002008 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 mod_bot = MAXLNUM;
2010 else
2011 bot_start = wp->w_height + xtra_rows;
2012 }
2013 }
2014 else if (xtra_rows > 0)
2015 {
2016 /* May scroll text down. If there is not enough
2017 * remaining text of scrolling fails, must redraw the
2018 * rest. */
2019 if (row + xtra_rows >= wp->w_height - 2)
2020 mod_bot = MAXLNUM;
2021 else
2022 {
2023 check_for_delay(FALSE);
2024 if (win_ins_lines(wp, row + old_rows,
2025 xtra_rows, FALSE, FALSE) == FAIL)
2026 mod_bot = MAXLNUM;
2027 else if (top_end > row + old_rows)
2028 /* Scrolled the part at the top that requires
2029 * updating down. */
2030 top_end += xtra_rows;
2031 }
2032 }
2033
2034 /* When not updating the rest, may need to move w_lines[]
2035 * entries. */
2036 if (mod_bot != MAXLNUM && i != j)
2037 {
2038 if (j < i)
2039 {
2040 int x = row + new_rows;
2041
2042 /* move entries in w_lines[] upwards */
2043 for (;;)
2044 {
2045 /* stop at last valid entry in w_lines[] */
2046 if (i >= wp->w_lines_valid)
2047 {
2048 wp->w_lines_valid = j;
2049 break;
2050 }
2051 wp->w_lines[j] = wp->w_lines[i];
2052 /* stop at a line that won't fit */
2053 if (x + (int)wp->w_lines[j].wl_size
2054 > wp->w_height)
2055 {
2056 wp->w_lines_valid = j + 1;
2057 break;
2058 }
2059 x += wp->w_lines[j++].wl_size;
2060 ++i;
2061 }
2062 if (bot_start > x)
2063 bot_start = x;
2064 }
2065 else /* j > i */
2066 {
2067 /* move entries in w_lines[] downwards */
2068 j -= i;
2069 wp->w_lines_valid += j;
2070 if (wp->w_lines_valid > wp->w_height)
2071 wp->w_lines_valid = wp->w_height;
2072 for (i = wp->w_lines_valid; i - j >= idx; --i)
2073 wp->w_lines[i] = wp->w_lines[i - j];
2074
2075 /* The w_lines[] entries for inserted lines are
2076 * now invalid, but wl_size may be used above.
2077 * Reset to zero. */
2078 while (i >= idx)
2079 {
2080 wp->w_lines[i].wl_size = 0;
2081 wp->w_lines[i--].wl_valid = FALSE;
2082 }
2083 }
2084 }
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087
2088#ifdef FEAT_FOLDING
2089 /*
2090 * When lines are folded, display one line for all of them.
2091 * Otherwise, display normally (can be several display lines when
2092 * 'wrap' is on).
2093 */
2094 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2095 if (fold_count != 0)
2096 {
2097 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2098 ++row;
2099 --fold_count;
2100 wp->w_lines[idx].wl_folded = TRUE;
2101 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2102# ifdef FEAT_SYN_HL
2103 did_update = DID_FOLD;
2104# endif
2105 }
2106 else
2107#endif
2108 if (idx < wp->w_lines_valid
2109 && wp->w_lines[idx].wl_valid
2110 && wp->w_lines[idx].wl_lnum == lnum
2111 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002112 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 && srow + wp->w_lines[idx].wl_size > wp->w_height
2114#ifdef FEAT_DIFF
2115 && diff_check_fill(wp, lnum) == 0
2116#endif
2117 )
2118 {
2119 /* This line is not going to fit. Don't draw anything here,
2120 * will draw "@ " lines below. */
2121 row = wp->w_height + 1;
2122 }
2123 else
2124 {
2125#ifdef FEAT_SEARCH_EXTRA
2126 prepare_search_hl(wp, lnum);
2127#endif
2128#ifdef FEAT_SYN_HL
2129 /* Let the syntax stuff know we skipped a few lines. */
2130 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002131 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 syntax_end_parsing(syntax_last_parsed + 1);
2133#endif
2134
2135 /*
2136 * Display one line.
2137 */
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002138 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0,
2139#ifdef SYN_TIME_LIMIT
2140 &syntax_tm
2141#else
2142 NULL
2143#endif
2144 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146#ifdef FEAT_FOLDING
2147 wp->w_lines[idx].wl_folded = FALSE;
2148 wp->w_lines[idx].wl_lastlnum = lnum;
2149#endif
2150#ifdef FEAT_SYN_HL
2151 did_update = DID_LINE;
2152 syntax_last_parsed = lnum;
2153#endif
2154 }
2155
2156 wp->w_lines[idx].wl_lnum = lnum;
2157 wp->w_lines[idx].wl_valid = TRUE;
2158 if (row > wp->w_height) /* past end of screen */
2159 {
2160 /* we may need the size of that too long line later on */
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 = plines_win(wp, lnum, TRUE);
2163 ++idx;
2164 break;
2165 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002166 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 wp->w_lines[idx].wl_size = row - srow;
2168 ++idx;
2169#ifdef FEAT_FOLDING
2170 lnum += fold_count + 1;
2171#else
2172 ++lnum;
2173#endif
2174 }
2175 else
2176 {
2177 /* This line does not need updating, advance to the next one */
2178 row += wp->w_lines[idx++].wl_size;
2179 if (row > wp->w_height) /* past end of screen */
2180 break;
2181#ifdef FEAT_FOLDING
2182 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2183#else
2184 ++lnum;
2185#endif
2186#ifdef FEAT_SYN_HL
2187 did_update = DID_NONE;
2188#endif
2189 }
2190
2191 if (lnum > buf->b_ml.ml_line_count)
2192 {
2193 eof = TRUE;
2194 break;
2195 }
2196 }
2197 /*
2198 * End of loop over all window lines.
2199 */
2200
2201
2202 if (idx > wp->w_lines_valid)
2203 wp->w_lines_valid = idx;
2204
2205#ifdef FEAT_SYN_HL
2206 /*
2207 * Let the syntax stuff know we stop parsing here.
2208 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002209 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 syntax_end_parsing(syntax_last_parsed + 1);
2211#endif
2212
2213 /*
2214 * If we didn't hit the end of the file, and we didn't finish the last
2215 * line we were working on, then the line didn't fit.
2216 */
2217 wp->w_empty_rows = 0;
2218#ifdef FEAT_DIFF
2219 wp->w_filler_rows = 0;
2220#endif
2221 if (!eof && !didline)
2222 {
2223 if (lnum == wp->w_topline)
2224 {
2225 /*
2226 * Single line that does not fit!
2227 * Don't overwrite it, it can be edited.
2228 */
2229 wp->w_botline = lnum + 1;
2230 }
2231#ifdef FEAT_DIFF
2232 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2233 {
2234 /* Window ends in filler lines. */
2235 wp->w_botline = lnum;
2236 wp->w_filler_rows = wp->w_height - srow;
2237 }
2238#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002239 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2240 {
2241 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2242
2243 /*
2244 * Last line isn't finished: Display "@@@" in the last screen line.
2245 */
2246 screen_puts_len((char_u *)"@@", 2, scr_row, W_WINCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002247 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002248 screen_fill(scr_row, scr_row + 1,
2249 (int)W_WINCOL(wp) + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002250 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002251 set_empty_rows(wp, srow);
2252 wp->w_botline = lnum;
2253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2255 {
2256 /*
2257 * Last line isn't finished: Display "@@@" at the end.
2258 */
2259 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2260 W_WINROW(wp) + wp->w_height,
2261 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002262 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 set_empty_rows(wp, srow);
2264 wp->w_botline = lnum;
2265 }
2266 else
2267 {
2268 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2269 wp->w_botline = lnum;
2270 }
2271 }
2272 else
2273 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002274#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 draw_vsep_win(wp, row);
2276#endif
2277 if (eof) /* we hit the end of the file */
2278 {
2279 wp->w_botline = buf->b_ml.ml_line_count + 1;
2280#ifdef FEAT_DIFF
2281 j = diff_check_fill(wp, wp->w_botline);
2282 if (j > 0 && !wp->w_botfill)
2283 {
2284 /*
2285 * Display filler lines at the end of the file
2286 */
2287 if (char2cells(fill_diff) > 1)
2288 i = '-';
2289 else
2290 i = fill_diff;
2291 if (row + j > wp->w_height)
2292 j = wp->w_height - row;
2293 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2294 row += j;
2295 }
2296#endif
2297 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002298 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 wp->w_botline = lnum;
2300
2301 /* make sure the rest of the screen is blank */
2302 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002303 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305
2306 /* Reset the type of redrawing required, the window has been updated. */
2307 wp->w_redr_type = 0;
2308#ifdef FEAT_DIFF
2309 wp->w_old_topfill = wp->w_topfill;
2310 wp->w_old_botfill = wp->w_botfill;
2311#endif
2312
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002313 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 /*
2316 * There is a trick with w_botline. If we invalidate it on each
2317 * change that might modify it, this will cause a lot of expensive
2318 * calls to plines() in update_topline() each time. Therefore the
2319 * value of w_botline is often approximated, and this value is used to
2320 * compute the value of w_topline. If the value of w_botline was
2321 * wrong, check that the value of w_topline is correct (cursor is on
2322 * the visible part of the text). If it's not, we need to redraw
2323 * again. Mostly this just means scrolling up a few lines, so it
2324 * doesn't look too bad. Only do this for the current window (where
2325 * changes are relevant).
2326 */
2327 wp->w_valid |= VALID_BOTLINE;
2328 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2329 {
2330 recursive = TRUE;
2331 curwin->w_valid &= ~VALID_TOPLINE;
2332 update_topline(); /* may invalidate w_botline again */
2333 if (must_redraw != 0)
2334 {
2335 /* Don't update for changes in buffer again. */
2336 i = curbuf->b_mod_set;
2337 curbuf->b_mod_set = FALSE;
2338 win_update(curwin);
2339 must_redraw = 0;
2340 curbuf->b_mod_set = i;
2341 }
2342 recursive = FALSE;
2343 }
2344 }
2345
2346#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2347 /* restore got_int, unless CTRL-C was hit while redrawing */
2348 if (!got_int)
2349 got_int = save_got_int;
2350#endif
2351}
2352
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353/*
2354 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2355 * as the filler character.
2356 */
2357 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002358win_draw_end(
2359 win_T *wp,
2360 int c1,
2361 int c2,
2362 int row,
2363 int endrow,
2364 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2367 int n = 0;
2368# define FDC_OFF n
2369#else
2370# define FDC_OFF 0
2371#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002372#ifdef FEAT_FOLDING
2373 int fdc = compute_foldcolumn(wp, 0);
2374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376#ifdef FEAT_RIGHTLEFT
2377 if (wp->w_p_rl)
2378 {
2379 /* No check for cmdline window: should never be right-left. */
2380# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002381 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 if (n > 0)
2384 {
2385 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002386 if (n > W_WIDTH(wp))
2387 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2389 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392# endif
2393# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002394 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
2396 int nn = n + 2;
2397
2398 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002399 if (nn > W_WIDTH(wp))
2400 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2402 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002403 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 n = nn;
2405 }
2406# endif
2407 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2408 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002409 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2411 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002412 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 else
2415#endif
2416 {
2417#ifdef FEAT_CMDWIN
2418 if (cmdwin_type != 0 && wp == curwin)
2419 {
2420 /* draw the cmdline character in the leftmost column */
2421 n = 1;
2422 if (n > wp->w_width)
2423 n = wp->w_width;
2424 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2425 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002426 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428#endif
2429#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002430 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002432 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 /* draw the fold column at the left */
2435 if (nn > W_WIDTH(wp))
2436 nn = W_WIDTH(wp);
2437 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2438 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002439 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 n = nn;
2441 }
2442#endif
2443#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002444 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 int nn = n + 2;
2447
2448 /* draw the sign column after the fold column */
2449 if (nn > W_WIDTH(wp))
2450 nn = W_WIDTH(wp);
2451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2452 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002453 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 n = nn;
2455 }
2456#endif
2457 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2458 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002459 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 set_empty_rows(wp, row);
2462}
2463
Bram Moolenaar1a384422010-07-14 19:53:30 +02002464#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002465static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466
2467/*
2468 * Advance **color_cols and return TRUE when there are columns to draw.
2469 */
2470 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002471advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002472{
2473 while (**color_cols >= 0 && vcol > **color_cols)
2474 ++*color_cols;
2475 return (**color_cols >= 0);
2476}
2477#endif
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479#ifdef FEAT_FOLDING
2480/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002481 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2482 * space is available for window "wp", minus "col".
2483 */
2484 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002485compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002486{
2487 int fdc = wp->w_p_fdc;
2488 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2489 int wwidth = W_WIDTH(wp);
2490
2491 if (fdc > wwidth - (col + wmw))
2492 fdc = wwidth - (col + wmw);
2493 return fdc;
2494}
2495
2496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 * Display one folded line.
2498 */
2499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002500fold_line(
2501 win_T *wp,
2502 long fold_count,
2503 foldinfo_T *foldinfo,
2504 linenr_T lnum,
2505 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002507 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 pos_T *top, *bot;
2509 linenr_T lnume = lnum + fold_count - 1;
2510 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002511 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int col;
2514 int txtcol;
2515 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002516 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 /* Build the fold line:
2519 * 1. Add the cmdwin_type for the command-line window
2520 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002521 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * 4. Compose the text
2523 * 5. Add the text
2524 * 6. set highlighting for the Visual area an other text
2525 */
2526 col = 0;
2527
2528 /*
2529 * 1. Add the cmdwin_type for the command-line window
2530 * Ignores 'rightleft', this window is never right-left.
2531 */
2532#ifdef FEAT_CMDWIN
2533 if (cmdwin_type != 0 && wp == curwin)
2534 {
2535 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002536 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#ifdef FEAT_MBYTE
2538 if (enc_utf8)
2539 ScreenLinesUC[off] = 0;
2540#endif
2541 ++col;
2542 }
2543#endif
2544
2545 /*
2546 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002547 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002549 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (fdc > 0)
2551 {
2552 fill_foldcolumn(buf, wp, TRUE, lnum);
2553#ifdef FEAT_RIGHTLEFT
2554 if (wp->w_p_rl)
2555 {
2556 int i;
2557
2558 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002559 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 /* reverse the fold column */
2561 for (i = 0; i < fdc; ++i)
2562 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2563 }
2564 else
2565#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002566 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 col += fdc;
2568 }
2569
2570#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002571# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2572 for (ri = 0; ri < l; ++ri) \
2573 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2574 else \
2575 for (ri = 0; ri < l; ++ri) \
2576 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002578# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2579 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#endif
2581
Bram Moolenaar64486672010-05-16 15:46:46 +02002582 /* Set all attributes of the 'number' or 'relativenumber' column and the
2583 * text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002584 RL_MEMSET(col, HL_ATTR(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586#ifdef FEAT_SIGNS
2587 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002588 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
2590 len = W_WIDTH(wp) - col;
2591 if (len > 0)
2592 {
2593 if (len > 2)
2594 len = 2;
2595# ifdef FEAT_RIGHTLEFT
2596 if (wp->w_p_rl)
2597 /* the line number isn't reversed */
2598 copy_text_attr(off + W_WIDTH(wp) - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002599 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 else
2601# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002602 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 col += len;
2604 }
2605 }
2606#endif
2607
2608 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002609 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002611 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 len = W_WIDTH(wp) - col;
2614 if (len > 0)
2615 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002616 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002617 long num;
2618 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002619
2620 if (len > w + 1)
2621 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002622
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002623 if (wp->w_p_nu && !wp->w_p_rnu)
2624 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002625 num = (long)lnum;
2626 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002627 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002628 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002629 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002630 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002631 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002632 /* 'number' + 'relativenumber': cursor line shows absolute
2633 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002634 num = lnum;
2635 fmt = "%-*ld ";
2636 }
2637 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002638
Bram Moolenaar700e7342013-01-30 12:31:36 +01002639 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#ifdef FEAT_RIGHTLEFT
2641 if (wp->w_p_rl)
2642 /* the line number isn't reversed */
2643 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002644 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else
2646#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002647 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 col += len;
2649 }
2650 }
2651
2652 /*
2653 * 4. Compose the folded-line string with 'foldtext', if set.
2654 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002655 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 txtcol = col; /* remember where text starts */
2658
2659 /*
2660 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2661 * Right-left text is put in columns 0 - number-col, normal text is put
2662 * in columns number-col - window-width.
2663 */
2664#ifdef FEAT_MBYTE
2665 if (has_mbyte)
2666 {
2667 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002668 int u8c, u8cc[MAX_MCO];
2669 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 int idx;
2671 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002672 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673# ifdef FEAT_ARABIC
2674 int prev_c = 0; /* previous Arabic character */
2675 int prev_c1 = 0; /* first composing char for prev_c */
2676# endif
2677
2678# ifdef FEAT_RIGHTLEFT
2679 if (wp->w_p_rl)
2680 idx = off;
2681 else
2682# endif
2683 idx = off + col;
2684
2685 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2686 for (p = text; *p != NUL; )
2687 {
2688 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002689 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (col + cells > W_WIDTH(wp)
2691# ifdef FEAT_RIGHTLEFT
2692 - (wp->w_p_rl ? col : 0)
2693# endif
2694 )
2695 break;
2696 ScreenLines[idx] = *p;
2697 if (enc_utf8)
2698 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 u8c = utfc_ptr2char(p, u8cc);
2700 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
2702 ScreenLinesUC[idx] = 0;
2703#ifdef FEAT_ARABIC
2704 prev_c = u8c;
2705#endif
2706 }
2707 else
2708 {
2709#ifdef FEAT_ARABIC
2710 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2711 {
2712 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 int pc, pc1, nc;
2714 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int firstbyte = *p;
2716
2717 /* The idea of what is the previous and next
2718 * character depends on 'rightleft'. */
2719 if (wp->w_p_rl)
2720 {
2721 pc = prev_c;
2722 pc1 = prev_c1;
2723 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002724 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
2726 else
2727 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002728 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 prev_c = u8c;
2733
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002734 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 pc, pc1, nc);
2736 ScreenLines[idx] = firstbyte;
2737 }
2738 else
2739 prev_c = u8c;
2740#endif
2741 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002742#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (u8c >= 0x10000)
2744 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2745 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002748 for (i = 0; i < Screen_mco; ++i)
2749 {
2750 ScreenLinesC[i][idx] = u8cc[i];
2751 if (u8cc[i] == 0)
2752 break;
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 if (cells > 1)
2756 ScreenLines[idx + 1] = 0;
2757 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002758 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2759 /* double-byte single width character */
2760 ScreenLines2[idx] = p[1];
2761 else if (cells > 1)
2762 /* double-width character */
2763 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 col += cells;
2765 idx += cells;
2766 p += c_len;
2767 }
2768 }
2769 else
2770#endif
2771 {
2772 len = (int)STRLEN(text);
2773 if (len > W_WIDTH(wp) - col)
2774 len = W_WIDTH(wp) - col;
2775 if (len > 0)
2776 {
2777#ifdef FEAT_RIGHTLEFT
2778 if (wp->w_p_rl)
2779 STRNCPY(current_ScreenLine, text, len);
2780 else
2781#endif
2782 STRNCPY(current_ScreenLine + col, text, len);
2783 col += len;
2784 }
2785 }
2786
2787 /* Fill the rest of the line with the fold filler */
2788#ifdef FEAT_RIGHTLEFT
2789 if (wp->w_p_rl)
2790 col -= txtcol;
2791#endif
2792 while (col < W_WIDTH(wp)
2793#ifdef FEAT_RIGHTLEFT
2794 - (wp->w_p_rl ? txtcol : 0)
2795#endif
2796 )
2797 {
2798#ifdef FEAT_MBYTE
2799 if (enc_utf8)
2800 {
2801 if (fill_fold >= 0x80)
2802 {
2803 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002804 ScreenLinesC[0][off + col] = 0;
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002805 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002810 ScreenLines[off + col] = fill_fold;
2811 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002812 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002816 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 }
2818
2819 if (text != buf)
2820 vim_free(text);
2821
2822 /*
2823 * 6. set highlighting for the Visual area an other text.
2824 * If all folded lines are in the Visual area, highlight the line.
2825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2827 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002828 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
2830 /* Visual is after curwin->w_cursor */
2831 top = &curwin->w_cursor;
2832 bot = &VIsual;
2833 }
2834 else
2835 {
2836 /* Visual is before curwin->w_cursor */
2837 top = &VIsual;
2838 bot = &curwin->w_cursor;
2839 }
2840 if (lnum >= top->lnum
2841 && lnume <= bot->lnum
2842 && (VIsual_mode != 'v'
2843 || ((lnum > top->lnum
2844 || (lnum == top->lnum
2845 && top->col == 0))
2846 && (lnume < bot->lnum
2847 || (lnume == bot->lnum
2848 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002849 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 {
2851 if (VIsual_mode == Ctrl_V)
2852 {
2853 /* Visual block mode: highlight the chars part of the block */
2854 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2855 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002856 if (wp->w_old_cursor_lcol != MAXCOL
2857 && wp->w_old_cursor_lcol + txtcol
2858 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 len = wp->w_old_cursor_lcol;
2860 else
2861 len = W_WIDTH(wp) - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002862 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002863 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 }
2866 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 /* Set all attributes of the text */
Bram Moolenaar8820b482017-03-16 17:23:31 +01002869 RL_MEMSET(txtcol, HL_ATTR(HLF_V), W_WIDTH(wp) - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002874#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002875 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2876 if (wp->w_p_cc_cols)
2877 {
2878 int i = 0;
2879 int j = wp->w_p_cc_cols[i];
2880 int old_txtcol = txtcol;
2881
2882 while (j > -1)
2883 {
2884 txtcol += j;
2885 if (wp->w_p_wrap)
2886 txtcol -= wp->w_skipcol;
2887 else
2888 txtcol -= wp->w_leftcol;
2889 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2890 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002891 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002892 txtcol = old_txtcol;
2893 j = wp->w_p_cc_cols[++i];
2894 }
2895 }
2896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002897 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002898 if (wp->w_p_cuc)
2899 {
2900 txtcol += wp->w_virtcol;
2901 if (wp->w_p_wrap)
2902 txtcol -= wp->w_skipcol;
2903 else
2904 txtcol -= wp->w_leftcol;
2905 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2906 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002907 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002908 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02002911 screen_line(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 (int)W_WIDTH(wp), FALSE);
2913
2914 /*
2915 * Update w_cline_height and w_cline_folded if the cursor line was
2916 * updated (saves a call to plines() later).
2917 */
2918 if (wp == curwin
2919 && lnum <= curwin->w_cursor.lnum
2920 && lnume >= curwin->w_cursor.lnum)
2921 {
2922 curwin->w_cline_row = row;
2923 curwin->w_cline_height = 1;
2924 curwin->w_cline_folded = TRUE;
2925 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2926 }
2927}
2928
2929/*
2930 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2931 */
2932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002933copy_text_attr(
2934 int off,
2935 char_u *buf,
2936 int len,
2937 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002939 int i;
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 mch_memmove(ScreenLines + off, buf, (size_t)len);
2942# ifdef FEAT_MBYTE
2943 if (enc_utf8)
2944 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2945# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002946 for (i = 0; i < len; ++i)
2947 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948}
2949
2950/*
2951 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002952 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002955fill_foldcolumn(
2956 char_u *p,
2957 win_T *wp,
2958 int closed, /* TRUE of FALSE */
2959 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 int i = 0;
2962 int level;
2963 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002964 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002965 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002968 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 level = win_foldinfo.fi_level;
2971 if (level > 0)
2972 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002973 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 /* If the column is too narrow, we start at the lowest level that
2977 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002978 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (first_level < 1)
2980 first_level = 1;
2981
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
2984 if (win_foldinfo.fi_lnum == lnum
2985 && first_level + i >= win_foldinfo.fi_low_level)
2986 p[i] = '-';
2987 else if (first_level == 1)
2988 p[i] = '|';
2989 else if (first_level + i <= 9)
2990 p[i] = '0' + first_level + i;
2991 else
2992 p[i] = '>';
2993 if (first_level + i == level)
2994 break;
2995 }
2996 }
2997 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002998 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000#endif /* FEAT_FOLDING */
3001
3002/*
3003 * Display line "lnum" of window 'wp' on the screen.
3004 * Start at row "startrow", stop when "endrow" is reached.
3005 * wp->w_virtcol needs to be valid.
3006 *
3007 * Return the number of last row the line occupies.
3008 */
3009 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003010win_line(
3011 win_T *wp,
3012 linenr_T lnum,
3013 int startrow,
3014 int endrow,
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003015 int nochange UNUSED, /* not updating for changed text */
3016 proftime_T *syntax_tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003018 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3020 int c = 0; /* init for GCC */
3021 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003022#ifdef FEAT_LINEBREAK
3023 long vcol_sbr = -1; /* virtual column after showbreak */
3024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 long vcol_prev = -1; /* "vcol" of previous character */
3026 char_u *line; /* current line */
3027 char_u *ptr; /* current position in "line" */
3028 int row; /* row in the window, excl w_winrow */
3029 int screen_row; /* row on the screen, incl w_winrow */
3030
3031 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3032 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003033 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003034 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int c_extra = NUL; /* extra chars, all the same */
3036 int extra_attr = 0; /* attributes when n_extra != 0 */
3037 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3038 displaying lcs_eol at end-of-line */
3039 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3040 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3041
3042 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3043 int saved_n_extra = 0;
3044 char_u *saved_p_extra = NULL;
3045 int saved_c_extra = 0;
3046 int saved_char_attr = 0;
3047
3048 int n_attr = 0; /* chars with special attr */
3049 int saved_attr2 = 0; /* char_attr saved for n_attr */
3050 int n_attr3 = 0; /* chars with overruling special attr */
3051 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3052
3053 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3054
3055 int fromcol, tocol; /* start/end of inverting */
3056 int fromcol_prev = -2; /* start of inverting after cursor */
3057 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003059 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 pos_T pos;
3061 long v;
3062
3063 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003064 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3066 in this line */
3067 int attr = 0; /* attributes for area highlighting */
3068 int area_attr = 0; /* attributes desired by highlighting */
3069 int search_attr = 0; /* attributes desired by 'hlsearch' */
3070#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003071 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 int syntax_attr = 0; /* attributes desired by syntax */
3073 int has_syntax = FALSE; /* this buffer has syntax highl. */
3074 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003075 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003076 int draw_color_col = FALSE; /* highlight colorcolumn */
3077 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003078#endif
3079#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003080 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003081# define SPWORDLEN 150
3082 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003083 int nextlinecol = 0; /* column where nextline[] starts */
3084 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003085 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003086 int spell_attr = 0; /* attributes desired by spelling */
3087 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003088 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3089 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003090 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003091 static int cap_col = -1; /* column to check for Cap word */
3092 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003093 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#endif
3095 int extra_check; /* has syntax or linebreak */
3096#ifdef FEAT_MBYTE
3097 int multi_attr = 0; /* attributes desired by multibyte */
3098 int mb_l = 1; /* multi-byte byte length */
3099 int mb_c = 0; /* decoded multi-byte character */
3100 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003101 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
3103#ifdef FEAT_DIFF
3104 int filler_lines; /* nr of filler lines to be drawn */
3105 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003106 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 int change_start = MAXCOL; /* first col of changed area */
3108 int change_end = -1; /* last col of changed area */
3109#endif
3110 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3111#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003112 int need_showbreak = FALSE; /* overlong line, skipping first x
3113 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003115#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3116 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003118 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003121 matchitem_T *cur; /* points to the match list */
3122 match_T *shl; /* points to search_hl or a match */
3123 int shl_flag; /* flag to indicate whether search_hl
3124 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003125 int pos_inprogress; /* marks that position match search is
3126 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003127 int prevcol_hl_flag; /* flag to indicate whether prevcol
3128 equals startcol of search_hl or one
3129 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130#endif
3131#ifdef FEAT_ARABIC
3132 int prev_c = 0; /* previous Arabic character */
3133 int prev_c1 = 0; /* first composing char for prev_c */
3134#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003135#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003136 int did_line_attr = 0;
3137#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003138#ifdef FEAT_TERMINAL
3139 int get_term_attr = FALSE;
3140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
3142 /* draw_state: items that are drawn in sequence: */
3143#define WL_START 0 /* nothing done yet */
3144#ifdef FEAT_CMDWIN
3145# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3146#else
3147# define WL_CMDLINE WL_START
3148#endif
3149#ifdef FEAT_FOLDING
3150# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3151#else
3152# define WL_FOLD WL_CMDLINE
3153#endif
3154#ifdef FEAT_SIGNS
3155# define WL_SIGN WL_FOLD + 1 /* column for signs */
3156#else
3157# define WL_SIGN WL_FOLD /* column for signs */
3158#endif
3159#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003160#ifdef FEAT_LINEBREAK
3161# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003163# define WL_BRI WL_NR
3164#endif
3165#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3166# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3167#else
3168# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
3170#define WL_LINE WL_SBR + 1 /* text in the line */
3171 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003172#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int feedback_col = 0;
3174 int feedback_old_attr = -1;
3175#endif
3176
Bram Moolenaar860cae12010-06-05 23:22:07 +02003177#ifdef FEAT_CONCEAL
3178 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003179 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003180 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003181 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003182 int is_concealing = FALSE;
3183 int boguscols = 0; /* nonexistent columns added to force
3184 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003185 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003186 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003187 int match_conc = 0; /* cchar for match functions */
3188 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003189 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003190# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003191# define FIX_FOR_BOGUSCOLS \
3192 { \
3193 n_extra += vcol_off; \
3194 vcol -= vcol_off; \
3195 vcol_off = 0; \
3196 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003197 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003198 boguscols = 0; \
3199 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003200#else
3201# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 if (startrow > endrow) /* past the end already! */
3205 return startrow;
3206
3207 row = startrow;
3208 screen_row = row + W_WINROW(wp);
3209
3210 /*
3211 * To speed up the loop below, set extra_check when there is linebreak,
3212 * trailing white space and/or syntax processing to be done.
3213 */
3214#ifdef FEAT_LINEBREAK
3215 extra_check = wp->w_p_lbr;
3216#else
3217 extra_check = 0;
3218#endif
3219#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003220 if (syntax_present(wp) && !wp->w_s->b_syn_error
3221# ifdef SYN_TIME_LIMIT
3222 && !wp->w_s->b_syn_slow
3223# endif
3224 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 /* Prepare for syntax highlighting in this line. When there is an
3227 * error, stop syntax highlighting. */
3228 save_did_emsg = did_emsg;
3229 did_emsg = FALSE;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003230 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003232 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
3234 {
3235 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003236#ifdef SYN_TIME_LIMIT
3237 if (!wp->w_s->b_syn_slow)
3238#endif
3239 {
3240 has_syntax = TRUE;
3241 extra_check = TRUE;
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003245
3246 /* Check for columns to display for 'colorcolumn'. */
3247 color_cols = wp->w_p_cc_cols;
3248 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003249 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003250#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003251
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003252#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003253 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003254 {
3255 extra_check = TRUE;
3256 get_term_attr = TRUE;
3257 }
3258#endif
3259
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003260#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003261 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003262 && *wp->w_s->b_p_spl != NUL
3263 && wp->w_s->b_langp.ga_len > 0
3264 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003265 {
3266 /* Prepare for spell checking. */
3267 has_spell = TRUE;
3268 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003269
3270 /* Get the start of the next line, so that words that wrap to the next
3271 * line are found too: "et<line-break>al.".
3272 * Trick: skip a few chars for C/shell/Vim comments */
3273 nextline[SPWORDLEN] = NUL;
3274 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3275 {
3276 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3277 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3278 }
3279
3280 /* When a word wrapped from the previous line the start of the current
3281 * line is valid. */
3282 if (lnum == checked_lnum)
3283 cur_checked_col = checked_col;
3284 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003285
3286 /* When there was a sentence end in the previous line may require a
3287 * word starting with capital in this line. In line 1 always check
3288 * the first word. */
3289 if (lnum != capcol_lnum)
3290 cap_col = -1;
3291 if (lnum == 1)
3292 cap_col = 0;
3293 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#endif
3296
3297 /*
3298 * handle visual active in this window
3299 */
3300 fromcol = -10;
3301 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3303 {
3304 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003305 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 top = &curwin->w_cursor;
3308 bot = &VIsual;
3309 }
3310 else /* Visual is before curwin->w_cursor */
3311 {
3312 top = &VIsual;
3313 bot = &curwin->w_cursor;
3314 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003315 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 if (VIsual_mode == Ctrl_V) /* block mode */
3317 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003318 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320 fromcol = wp->w_old_cursor_fcol;
3321 tocol = wp->w_old_cursor_lcol;
3322 }
3323 }
3324 else /* non-block mode */
3325 {
3326 if (lnum > top->lnum && lnum <= bot->lnum)
3327 fromcol = 0;
3328 else if (lnum == top->lnum)
3329 {
3330 if (VIsual_mode == 'V') /* linewise */
3331 fromcol = 0;
3332 else
3333 {
3334 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3335 if (gchar_pos(top) == NUL)
3336 tocol = fromcol + 1;
3337 }
3338 }
3339 if (VIsual_mode != 'V' && lnum == bot->lnum)
3340 {
3341 if (*p_sel == 'e' && bot->col == 0
3342#ifdef FEAT_VIRTUALEDIT
3343 && bot->coladd == 0
3344#endif
3345 )
3346 {
3347 fromcol = -10;
3348 tocol = MAXCOL;
3349 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003350 else if (bot->col == MAXCOL)
3351 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
3353 {
3354 pos = *bot;
3355 if (*p_sel == 'e')
3356 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3357 else
3358 {
3359 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3360 ++tocol;
3361 }
3362 }
3363 }
3364 }
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 /* Check if the character under the cursor should not be inverted */
3367 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003368#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 )
3372 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 /* if inverting in this line set area_highlighting */
3375 if (fromcol >= 0)
3376 {
3377 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003378 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003380 if ((clip_star.available && !clip_star.owned
3381 && clip_isautosel_star())
3382 || (clip_plus.available && !clip_plus.owned
3383 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003384 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#endif
3386 }
3387 }
3388
3389 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003390 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003392 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && wp == curwin
3394 && lnum >= curwin->w_cursor.lnum
3395 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3396 {
3397 if (lnum == curwin->w_cursor.lnum)
3398 getvcol(curwin, &(curwin->w_cursor),
3399 (colnr_T *)&fromcol, NULL, NULL);
3400 else
3401 fromcol = 0;
3402 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3403 {
3404 pos.lnum = lnum;
3405 pos.col = search_match_endcol;
3406 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3407 }
3408 else
3409 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003410 /* do at least one character; happens when past end of line */
3411 if (fromcol == tocol)
3412 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003414 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416
3417#ifdef FEAT_DIFF
3418 filler_lines = diff_check(wp, lnum);
3419 if (filler_lines < 0)
3420 {
3421 if (filler_lines == -1)
3422 {
3423 if (diff_find_change(wp, lnum, &change_start, &change_end))
3424 diff_hlf = HLF_ADD; /* added line */
3425 else if (change_start == 0)
3426 diff_hlf = HLF_TXD; /* changed text */
3427 else
3428 diff_hlf = HLF_CHD; /* changed line */
3429 }
3430 else
3431 diff_hlf = HLF_ADD; /* added line */
3432 filler_lines = 0;
3433 area_highlighting = TRUE;
3434 }
3435 if (lnum == wp->w_topline)
3436 filler_lines = wp->w_topfill;
3437 filler_todo = filler_lines;
3438#endif
3439
3440#ifdef LINE_ATTR
3441# ifdef FEAT_SIGNS
3442 /* If this line has a sign with line highlighting set line_attr. */
3443 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3444 if (v != 0)
3445 line_attr = sign_get_attr((int)v, TRUE);
3446# endif
3447# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3448 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003449 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003450 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451# endif
3452 if (line_attr != 0)
3453 area_highlighting = TRUE;
3454#endif
3455
3456 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3457 ptr = line;
3458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003459#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003460 if (has_spell)
3461 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003462 /* For checking first word with a capital skip white space. */
3463 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003464 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003465
Bram Moolenaar30abd282005-06-22 22:35:10 +00003466 /* To be able to spell-check over line boundaries copy the end of the
3467 * current line into nextline[]. Above the start of the next line was
3468 * copied to nextline[SPWORDLEN]. */
3469 if (nextline[SPWORDLEN] == NUL)
3470 {
3471 /* No next line or it is empty. */
3472 nextlinecol = MAXCOL;
3473 nextline_idx = 0;
3474 }
3475 else
3476 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003477 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003478 if (v < SPWORDLEN)
3479 {
3480 /* Short line, use it completely and append the start of the
3481 * next line. */
3482 nextlinecol = 0;
3483 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003484 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003485 nextline_idx = v + 1;
3486 }
3487 else
3488 {
3489 /* Long line, use only the last SPWORDLEN bytes. */
3490 nextlinecol = v - SPWORDLEN;
3491 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3492 nextline_idx = SPWORDLEN + 1;
3493 }
3494 }
3495 }
3496#endif
3497
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003498 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003500 if (lcs_space || lcs_trail)
3501 extra_check = TRUE;
3502 /* find start of trailing whitespace */
3503 if (lcs_trail)
3504 {
3505 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003506 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003507 --trailcol;
3508 trailcol += (colnr_T) (ptr - line);
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511
3512 /*
3513 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3514 * first character to be displayed.
3515 */
3516 if (wp->w_p_wrap)
3517 v = wp->w_skipcol;
3518 else
3519 v = wp->w_leftcol;
3520 if (v > 0)
3521 {
3522#ifdef FEAT_MBYTE
3523 char_u *prev_ptr = ptr;
3524#endif
3525 while (vcol < v && *ptr != NUL)
3526 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003527 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 vcol += c;
3529#ifdef FEAT_MBYTE
3530 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003532 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003535 /* When:
3536 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003537 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003538 * - 'virtualedit' is set, or
3539 * - the visual mode is active,
3540 * the end of the line may be before the start of the displayed part.
3541 */
3542 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003543#ifdef FEAT_SYN_HL
3544 wp->w_p_cuc || draw_color_col ||
3545#endif
3546#ifdef FEAT_VIRTUALEDIT
3547 virtual_active() ||
3548#endif
3549 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
3554 /* Handle a character that's not completely on the screen: Put ptr at
3555 * that character but skip the first few screen characters. */
3556 if (vcol > v)
3557 {
3558 vcol -= c;
3559#ifdef FEAT_MBYTE
3560 ptr = prev_ptr;
3561#else
3562 --ptr;
3563#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003564 /* If the character fits on the screen, don't need to skip it.
3565 * Except for a TAB. */
3566 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003567#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003568 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003569#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003570 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003571 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573
3574 /*
3575 * Adjust for when the inverted text is before the screen,
3576 * and when the start of the inverted text is before the screen.
3577 */
3578 if (tocol <= vcol)
3579 fromcol = 0;
3580 else if (fromcol >= 0 && fromcol < vcol)
3581 fromcol = vcol;
3582
3583#ifdef FEAT_LINEBREAK
3584 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3585 if (wp->w_p_wrap)
3586 need_showbreak = TRUE;
3587#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003588#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003589 /* When spell checking a word we need to figure out the start of the
3590 * word and if it's badly spelled or not. */
3591 if (has_spell)
3592 {
3593 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003594 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003595 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003596
3597 pos = wp->w_cursor;
3598 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003599 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003600 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003601
3602 /* spell_move_to() may call ml_get() and make "line" invalid */
3603 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3604 ptr = line + linecol;
3605
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003606 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003607 {
3608 /* no bad word found at line start, don't check until end of a
3609 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003610 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003611 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003612 }
3613 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003614 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003615 /* bad word found, use attributes until end of word */
3616 word_end = wp->w_cursor.col + len + 1;
3617
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003618 /* Turn index into actual attributes. */
3619 if (spell_hlf != HLF_COUNT)
3620 spell_attr = highlight_attr[spell_hlf];
3621 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003622 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003623
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003624# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003625 /* Need to restart syntax highlighting for this line. */
3626 if (has_syntax)
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003627 syntax_start(wp, lnum, syntax_tm);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003628# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003629 }
3630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632
3633 /*
3634 * Correct highlighting for cursor that can't be disabled.
3635 * Avoids having to check this for each character.
3636 */
3637 if (fromcol >= 0)
3638 {
3639 if (noinvcur)
3640 {
3641 if ((colnr_T)fromcol == wp->w_virtcol)
3642 {
3643 /* highlighting starts at cursor, let it start just after the
3644 * cursor */
3645 fromcol_prev = fromcol;
3646 fromcol = -1;
3647 }
3648 else if ((colnr_T)fromcol < wp->w_virtcol)
3649 /* restart highlighting after the cursor */
3650 fromcol_prev = wp->w_virtcol;
3651 }
3652 if (fromcol >= tocol)
3653 fromcol = -1;
3654 }
3655
3656#ifdef FEAT_SEARCH_EXTRA
3657 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003658 * Handle highlighting the last used search pattern and matches.
3659 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003661 cur = wp->w_match_head;
3662 shl_flag = FALSE;
3663 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003665 if (shl_flag == FALSE)
3666 {
3667 shl = &search_hl;
3668 shl_flag = TRUE;
3669 }
3670 else
3671 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003672 shl->startcol = MAXCOL;
3673 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003675 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003676 v = (long)(ptr - line);
3677 if (cur != NULL)
3678 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003679 next_search_hl(wp, shl, lnum, (colnr_T)v,
3680 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003681
3682 /* Need to get the line again, a multi-line regexp may have made it
3683 * invalid. */
3684 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3685 ptr = line + v;
3686
3687 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003689 if (shl->lnum == lnum)
3690 shl->startcol = shl->rm.startpos[0].col;
3691 else
3692 shl->startcol = 0;
3693 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3694 - shl->rm.startpos[0].lnum)
3695 shl->endcol = shl->rm.endpos[0].col;
3696 else
3697 shl->endcol = MAXCOL;
3698 /* Highlight one character for an empty match. */
3699 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003702 if (has_mbyte && line[shl->endcol] != NUL)
3703 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003706 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003708 if ((long)shl->startcol < v) /* match at leftcol */
3709 {
3710 shl->attr_cur = shl->attr;
3711 search_attr = shl->attr;
3712 }
3713 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003715 if (shl != &search_hl && cur != NULL)
3716 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718#endif
3719
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003720#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003721 /* Cursor line highlighting for 'cursorline' in the current window. Not
3722 * when Visual mode is active, because it's not clear what is selected
3723 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003724 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003725 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003726 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003727 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003728 area_highlighting = TRUE;
3729 }
3730#endif
3731
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003732 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 col = 0;
3734#ifdef FEAT_RIGHTLEFT
3735 if (wp->w_p_rl)
3736 {
3737 /* Rightleft window: process the text in the normal direction, but put
3738 * it in current_ScreenLine[] from right to left. Start at the
3739 * rightmost column of the window. */
3740 col = W_WIDTH(wp) - 1;
3741 off += col;
3742 }
3743#endif
3744
3745 /*
3746 * Repeat for the whole displayed line.
3747 */
3748 for (;;)
3749 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003750#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003751 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 /* Skip this quickly when working on the text. */
3754 if (draw_state != WL_LINE)
3755 {
3756#ifdef FEAT_CMDWIN
3757 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3758 {
3759 draw_state = WL_CMDLINE;
3760 if (cmdwin_type != 0 && wp == curwin)
3761 {
3762 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003764 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003765 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 }
3768#endif
3769
3770#ifdef FEAT_FOLDING
3771 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3772 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003773 int fdc = compute_foldcolumn(wp, 0);
3774
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003776 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003778 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003779 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003780 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003781 p_extra_free = alloc(12 + 1);
3782
3783 if (p_extra_free != NULL)
3784 {
3785 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3786 n_extra = fdc;
3787 p_extra_free[n_extra] = NUL;
3788 p_extra = p_extra_free;
3789 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003790 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 }
3794#endif
3795
3796#ifdef FEAT_SIGNS
3797 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3798 {
3799 draw_state = WL_SIGN;
3800 /* Show the sign column when there are any signs in this
3801 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003802 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003804 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003806 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807# endif
3808
3809 /* Draw two cells with the sign value or blank. */
3810 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003811 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 n_extra = 2;
3813
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003814 if (row == startrow
3815#ifdef FEAT_DIFF
3816 + filler_lines && filler_todo <= 0
3817#endif
3818 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
3820 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3821 SIGN_TEXT);
3822# ifdef FEAT_SIGN_ICONS
3823 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3824 SIGN_ICON);
3825 if (gui.in_use && icon_sign != 0)
3826 {
3827 /* Use the image in this position. */
3828 c_extra = SIGN_BYTE;
3829# ifdef FEAT_NETBEANS_INTG
3830 if (buf_signcount(wp->w_buffer, lnum) > 1)
3831 c_extra = MULTISIGN_BYTE;
3832# endif
3833 char_attr = icon_sign;
3834 }
3835 else
3836# endif
3837 if (text_sign != 0)
3838 {
3839 p_extra = sign_get_text(text_sign);
3840 if (p_extra != NULL)
3841 {
3842 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 char_attr = sign_get_attr(text_sign, FALSE);
3846 }
3847 }
3848 }
3849 }
3850#endif
3851
3852 if (draw_state == WL_NR - 1 && n_extra == 0)
3853 {
3854 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003855 /* Display the absolute or relative line number. After the
3856 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3857 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 && (row == startrow
3859#ifdef FEAT_DIFF
3860 + filler_lines
3861#endif
3862 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3863 {
3864 /* Draw the line number (empty space after wrapping). */
3865 if (row == startrow
3866#ifdef FEAT_DIFF
3867 + filler_lines
3868#endif
3869 )
3870 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003871 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003872 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003873
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003874 if (wp->w_p_nu && !wp->w_p_rnu)
3875 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003876 num = (long)lnum;
3877 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003878 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003879 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003880 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003881 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003882 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003883 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003884 num = lnum;
3885 fmt = "%-*ld ";
3886 }
3887 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003888
Bram Moolenaar700e7342013-01-30 12:31:36 +01003889 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003890 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (wp->w_skipcol > 0)
3892 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3893 *p_extra = '-';
3894#ifdef FEAT_RIGHTLEFT
3895 if (wp->w_p_rl) /* reverse line numbers */
3896 rl_mirror(extra);
3897#endif
3898 p_extra = extra;
3899 c_extra = NUL;
3900 }
3901 else
3902 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003903 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003904 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003905#ifdef FEAT_SYN_HL
3906 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003907 * the current line differently.
3908 * TODO: Can we use CursorLine instead of CursorLineNr
3909 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003910 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003911 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003912 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 }
3916
Bram Moolenaar597a4222014-06-25 14:39:50 +02003917#ifdef FEAT_LINEBREAK
3918 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3919 && n_extra == 0 && *p_sbr != NUL)
3920 /* draw indent after showbreak value */
3921 draw_state = WL_BRI;
3922 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3923 /* After the showbreak, draw the breakindent */
3924 draw_state = WL_BRI - 1;
3925
3926 /* draw 'breakindent': indent wrapped text accordingly */
3927 if (draw_state == WL_BRI - 1 && n_extra == 0)
3928 {
3929 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003930 /* if need_showbreak is set, breakindent also applies */
3931 if (wp->w_p_bri && n_extra == 0
3932 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003933# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003934 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003935# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003936 )
3937 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003938 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003939# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003940 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003941 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003942 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003943# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003944 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3945 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003946 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003947# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003948 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003949# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003950 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003951 c_extra = ' ';
3952 n_extra = get_breakindent_win(wp,
3953 ml_get_buf(wp->w_buffer, lnum, FALSE));
3954 /* Correct end of highlighted area for 'breakindent',
3955 * required when 'linebreak' is also set. */
3956 if (tocol == vcol)
3957 tocol += n_extra;
3958 }
3959 }
3960#endif
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3963 if (draw_state == WL_SBR - 1 && n_extra == 0)
3964 {
3965 draw_state = WL_SBR;
3966# ifdef FEAT_DIFF
3967 if (filler_todo > 0)
3968 {
3969 /* Draw "deleted" diff line(s). */
3970 if (char2cells(fill_diff) > 1)
3971 c_extra = '-';
3972 else
3973 c_extra = fill_diff;
3974# ifdef FEAT_RIGHTLEFT
3975 if (wp->w_p_rl)
3976 n_extra = col + 1;
3977 else
3978# endif
3979 n_extra = W_WIDTH(wp) - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003980 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982# endif
3983# ifdef FEAT_LINEBREAK
3984 if (*p_sbr != NUL && need_showbreak)
3985 {
3986 /* Draw 'showbreak' at the start of each broken line. */
3987 p_extra = p_sbr;
3988 c_extra = NUL;
3989 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01003990 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003992 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 /* Correct end of highlighted area for 'showbreak',
3994 * required when 'linebreak' is also set. */
3995 if (tocol == vcol)
3996 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003997#ifdef FEAT_SYN_HL
3998 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003999 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004000 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004001 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004# endif
4005 }
4006#endif
4007
4008 if (draw_state == WL_LINE - 1 && n_extra == 0)
4009 {
4010 draw_state = WL_LINE;
4011 if (saved_n_extra)
4012 {
4013 /* Continue item from end of wrapped line. */
4014 n_extra = saved_n_extra;
4015 c_extra = saved_c_extra;
4016 p_extra = saved_p_extra;
4017 char_attr = saved_char_attr;
4018 }
4019 else
4020 char_attr = 0;
4021 }
4022 }
4023
4024 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004025 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027#ifdef FEAT_DIFF
4028 && filler_todo <= 0
4029#endif
4030 )
4031 {
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02004032 screen_line(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004033 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034 /* Pretend we have finished updating the window. Except when
4035 * 'cursorcolumn' is set. */
4036#ifdef FEAT_SYN_HL
4037 if (wp->w_p_cuc)
4038 row = wp->w_cline_row + wp->w_cline_height;
4039 else
4040#endif
4041 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 break;
4043 }
4044
4045 if (draw_state == WL_LINE && area_highlighting)
4046 {
4047 /* handle Visual or match highlighting in this line */
4048 if (vcol == fromcol
4049#ifdef FEAT_MBYTE
4050 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4051 && (*mb_ptr2cells)(ptr) > 1)
4052#endif
4053 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004054 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 && vcol < tocol))
4056 area_attr = attr; /* start highlighting */
4057 else if (area_attr != 0
4058 && (vcol == tocol
4059 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062#ifdef FEAT_SEARCH_EXTRA
4063 if (!n_extra)
4064 {
4065 /*
4066 * Check for start/end of search pattern match.
4067 * After end, check for start/end of next match.
4068 * When another match, have to check for start again.
4069 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004070 * Do this for 'search_hl' and the match list (ordered by
4071 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004073 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004074 cur = wp->w_match_head;
4075 shl_flag = FALSE;
4076 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004078 if (shl_flag == FALSE
4079 && ((cur != NULL
4080 && cur->priority > SEARCH_HL_PRIORITY)
4081 || cur == NULL))
4082 {
4083 shl = &search_hl;
4084 shl_flag = TRUE;
4085 }
4086 else
4087 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004088 if (cur != NULL)
4089 cur->pos.cur = 0;
4090 pos_inprogress = TRUE;
4091 while (shl->rm.regprog != NULL
4092 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004094 if (shl->startcol != MAXCOL
4095 && v >= (long)shl->startcol
4096 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004098#ifdef FEAT_MBYTE
4099 int tmp_col = v + MB_PTR2LEN(ptr);
4100
4101 if (shl->endcol < tmp_col)
4102 shl->endcol = tmp_col;
4103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004105#ifdef FEAT_CONCEAL
4106 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4107 == cur->hlg_id)
4108 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004109 has_match_conc =
4110 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004111 match_conc = cur->conceal_char;
4112 }
4113 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004114 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004117 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
4119 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004120 next_search_hl(wp, shl, lnum, (colnr_T)v,
4121 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004122 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004123 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 /* Need to get the line again, a multi-line regexp
4126 * may have made it invalid. */
4127 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4128 ptr = line + v;
4129
4130 if (shl->lnum == lnum)
4131 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004136 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004138 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 {
4140 /* highlight empty match, try again after
4141 * it */
4142#ifdef FEAT_MBYTE
4143 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004144 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004145 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 else
4147#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004148 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150
4151 /* Loop to check if the match starts at the
4152 * current position */
4153 continue;
4154 }
4155 }
4156 break;
4157 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004158 if (shl != &search_hl && cur != NULL)
4159 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004161
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004162 /* Use attributes from match with highest priority among
4163 * 'search_hl' and the match list. */
4164 search_attr = search_hl.attr_cur;
4165 cur = wp->w_match_head;
4166 shl_flag = FALSE;
4167 while (cur != NULL || shl_flag == FALSE)
4168 {
4169 if (shl_flag == FALSE
4170 && ((cur != NULL
4171 && cur->priority > SEARCH_HL_PRIORITY)
4172 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004173 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004174 shl = &search_hl;
4175 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004176 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004177 else
4178 shl = &cur->hl;
4179 if (shl->attr_cur != 0)
4180 search_attr = shl->attr_cur;
4181 if (shl != &search_hl && cur != NULL)
4182 cur = cur->next;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185#endif
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004188 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004190 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4191 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004193 if (diff_hlf == HLF_TXD && ptr - line > change_end
4194 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004196 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004197 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004198 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004201
4202 /* Decide which of the highlight attributes to use. */
4203 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004204#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004205 if (area_attr != 0)
4206 char_attr = hl_combine_attr(line_attr, area_attr);
4207 else if (search_attr != 0)
4208 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004209 /* Use line_attr when not in the Visual or 'incsearch' area
4210 * (area_attr may be 0 when "noinvcur" is set). */
4211 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004212 || vcol < fromcol || vcol_prev < fromcol_prev
4213 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004214 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004215#else
4216 if (area_attr != 0)
4217 char_attr = area_attr;
4218 else if (search_attr != 0)
4219 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220#endif
4221 else
4222 {
4223 attr_pri = FALSE;
4224#ifdef FEAT_SYN_HL
4225 if (has_syntax)
4226 char_attr = syntax_attr;
4227 else
4228#endif
4229 char_attr = 0;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232
4233 /*
4234 * Get the next character to put on the screen.
4235 */
4236 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004237 * The "p_extra" points to the extra stuff that is inserted to
4238 * represent special characters (non-printable stuff) and other
4239 * things. When all characters are the same, c_extra is used.
4240 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4241 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4243 */
4244 if (n_extra > 0)
4245 {
4246 if (c_extra != NUL)
4247 {
4248 c = c_extra;
4249#ifdef FEAT_MBYTE
4250 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004251 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004254 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004255 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 else
4258 mb_utf8 = FALSE;
4259#endif
4260 }
4261 else
4262 {
4263 c = *p_extra;
4264#ifdef FEAT_MBYTE
4265 if (has_mbyte)
4266 {
4267 mb_c = c;
4268 if (enc_utf8)
4269 {
4270 /* If the UTF-8 character is more than one byte:
4271 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004272 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 mb_utf8 = FALSE;
4274 if (mb_l > n_extra)
4275 mb_l = 1;
4276 else if (mb_l > 1)
4277 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004278 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004280 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 }
4283 else
4284 {
4285 /* if this is a DBCS character, put it in "mb_c" */
4286 mb_l = MB_BYTE2LEN(c);
4287 if (mb_l >= n_extra)
4288 mb_l = 1;
4289 else if (mb_l > 1)
4290 mb_c = (c << 8) + p_extra[1];
4291 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004292 if (mb_l == 0) /* at the NUL at end-of-line */
4293 mb_l = 1;
4294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 /* If a double-width char doesn't fit display a '>' in the
4296 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004297 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298# ifdef FEAT_RIGHTLEFT
4299 wp->w_p_rl ? (col <= 0) :
4300# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004301 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 && (*mb_char2cells)(mb_c) == 2)
4303 {
4304 c = '>';
4305 mb_c = c;
4306 mb_l = 1;
4307 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004308 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 /* put the pointer back to output the double-width
4310 * character at the start of the next line. */
4311 ++n_extra;
4312 --p_extra;
4313 }
4314 else
4315 {
4316 n_extra -= mb_l - 1;
4317 p_extra += mb_l - 1;
4318 }
4319 }
4320#endif
4321 ++p_extra;
4322 }
4323 --n_extra;
4324 }
4325 else
4326 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004327#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004328 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004329#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004330
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004331 if (p_extra_free != NULL)
4332 {
4333 vim_free(p_extra_free);
4334 p_extra_free = NULL;
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 /*
4337 * Get a character from the line itself.
4338 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004339 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004340#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004341 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343#ifdef FEAT_MBYTE
4344 if (has_mbyte)
4345 {
4346 mb_c = c;
4347 if (enc_utf8)
4348 {
4349 /* If the UTF-8 character is more than one byte: Decode it
4350 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004351 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 mb_utf8 = FALSE;
4353 if (mb_l > 1)
4354 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004355 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 /* Overlong encoded ASCII or ASCII with composing char
4357 * is displayed normally, except a NUL. */
4358 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004359 {
4360 c = mb_c;
4361# ifdef FEAT_LINEBREAK
4362 c0 = mb_c;
4363# endif
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004366
4367 /* At start of the line we can have a composing char.
4368 * Draw it as a space with a composing char. */
4369 if (utf_iscomposing(mb_c))
4370 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004371 int i;
4372
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004373 for (i = Screen_mco - 1; i > 0; --i)
4374 u8cc[i] = u8cc[i - 1];
4375 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004376 mb_c = ' ';
4377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 if ((mb_l == 1 && c >= 0x80)
4381 || (mb_l >= 1 && mb_c == 0)
4382 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004383# ifdef UNICODE16
4384 || mb_c >= 0x10000
4385# endif
4386 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 /*
4389 * Illegal UTF-8 byte: display as <xx>.
4390 * Non-BMP character : display as ? or fullwidth ?.
4391 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004392# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
4396 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004397# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 if (wp->w_p_rl) /* reverse */
4399 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004402# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else if (utf_char2cells(mb_c) != 2)
4404 STRCPY(extra, "?");
4405 else
4406 /* 0xff1f in UTF-8: full-width '?' */
4407 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 p_extra = extra;
4411 c = *p_extra;
4412 mb_c = mb_ptr2char_adv(&p_extra);
4413 mb_utf8 = (c >= 0x80);
4414 n_extra = (int)STRLEN(p_extra);
4415 c_extra = NUL;
4416 if (area_attr == 0 && search_attr == 0)
4417 {
4418 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004419 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 saved_attr2 = char_attr; /* save current attr */
4421 }
4422 }
4423 else if (mb_l == 0) /* at the NUL at end-of-line */
4424 mb_l = 1;
4425#ifdef FEAT_ARABIC
4426 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4427 {
4428 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 int pc, pc1, nc;
4430 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /* The idea of what is the previous and next
4433 * character depends on 'rightleft'. */
4434 if (wp->w_p_rl)
4435 {
4436 pc = prev_c;
4437 pc1 = prev_c1;
4438 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004439 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004443 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 prev_c = mb_c;
4448
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004449 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451 else
4452 prev_c = mb_c;
4453#endif
4454 }
4455 else /* enc_dbcs */
4456 {
4457 mb_l = MB_BYTE2LEN(c);
4458 if (mb_l == 0) /* at the NUL at end-of-line */
4459 mb_l = 1;
4460 else if (mb_l > 1)
4461 {
4462 /* We assume a second byte below 32 is illegal.
4463 * Hopefully this is OK for all double-byte encodings!
4464 */
4465 if (ptr[1] >= 32)
4466 mb_c = (c << 8) + ptr[1];
4467 else
4468 {
4469 if (ptr[1] == NUL)
4470 {
4471 /* head byte at end of line */
4472 mb_l = 1;
4473 transchar_nonprint(extra, c);
4474 }
4475 else
4476 {
4477 /* illegal tail byte */
4478 mb_l = 2;
4479 STRCPY(extra, "XX");
4480 }
4481 p_extra = extra;
4482 n_extra = (int)STRLEN(extra) - 1;
4483 c_extra = NUL;
4484 c = *p_extra++;
4485 if (area_attr == 0 && search_attr == 0)
4486 {
4487 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004488 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 saved_attr2 = char_attr; /* save current attr */
4490 }
4491 mb_c = c;
4492 }
4493 }
4494 }
4495 /* If a double-width char doesn't fit display a '>' in the
4496 * last column; the character is displayed at the start of the
4497 * next line. */
4498 if ((
4499# ifdef FEAT_RIGHTLEFT
4500 wp->w_p_rl ? (col <= 0) :
4501# endif
4502 (col >= W_WIDTH(wp) - 1))
4503 && (*mb_char2cells)(mb_c) == 2)
4504 {
4505 c = '>';
4506 mb_c = c;
4507 mb_utf8 = FALSE;
4508 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004509 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 /* Put pointer back so that the character will be
4511 * displayed at the start of the next line. */
4512 --ptr;
4513 }
4514 else if (*ptr != NUL)
4515 ptr += mb_l - 1;
4516
4517 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004518 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004519 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004520 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004523 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 c = ' ';
4525 if (area_attr == 0 && search_attr == 0)
4526 {
4527 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004528 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 saved_attr2 = char_attr; /* save current attr */
4530 }
4531 mb_c = c;
4532 mb_utf8 = FALSE;
4533 mb_l = 1;
4534 }
4535
4536 }
4537#endif
4538 ++ptr;
4539
4540 if (extra_check)
4541 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004542#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004543 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004544#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004545
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004546#ifdef FEAT_TERMINAL
4547 if (get_term_attr)
4548 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004549 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004550
4551 if (!attr_pri)
4552 char_attr = syntax_attr;
4553 else
4554 char_attr = hl_combine_attr(syntax_attr, char_attr);
4555 }
4556#endif
4557
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004558#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 /* Get syntax attribute, unless still at the start of the line
4560 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004561 v = (long)(ptr - line);
4562 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 /* Get the syntax attribute for the character. If there
4565 * is an error, disable syntax highlighting. */
4566 save_did_emsg = did_emsg;
4567 did_emsg = FALSE;
4568
Bram Moolenaar217ad922005-03-20 22:37:15 +00004569 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004570# ifdef FEAT_SPELL
4571 has_spell ? &can_spell :
4572# endif
4573 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004576 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004577 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004578 has_syntax = FALSE;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 else
4581 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004582#ifdef SYN_TIME_LIMIT
4583 if (wp->w_s->b_syn_slow)
4584 has_syntax = FALSE;
4585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /* Need to get the line again, a multi-line regexp may
4588 * have made it invalid. */
4589 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4590 ptr = line + v;
4591
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004592 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004594 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004595 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004596# ifdef FEAT_CONCEAL
4597 /* no concealing past the end of the line, it interferes
4598 * with line highlighting */
4599 if (c == NUL)
4600 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004601 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004602 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004605#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004606
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004607#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004608 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004609 * Only do this when there is no syntax highlighting, the
4610 * @Spell cluster is not used or the current syntax item
4611 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004612 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004613 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004614 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004615# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004616 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004617 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004618# endif
4619 if (c != 0 && (
4620# ifdef FEAT_SYN_HL
4621 !has_syntax ||
4622# endif
4623 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004624 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004625 char_u *prev_ptr, *p;
4626 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004627 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004628# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004629 if (has_mbyte)
4630 {
4631 prev_ptr = ptr - mb_l;
4632 v -= mb_l - 1;
4633 }
4634 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004635# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004636 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004637
4638 /* Use nextline[] if possible, it has the start of the
4639 * next line concatenated. */
4640 if ((prev_ptr - line) - nextlinecol >= 0)
4641 p = nextline + (prev_ptr - line) - nextlinecol;
4642 else
4643 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004644 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004645 len = spell_check(wp, p, &spell_hlf, &cap_col,
4646 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004647 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004648
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004649 /* In Insert mode only highlight a word that
4650 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004651 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004652 && (State & INSERT) != 0
4653 && wp->w_cursor.lnum == lnum
4654 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004655 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004656 && wp->w_cursor.col < (colnr_T)word_end)
4657 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004658 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004659 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004660 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004661
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004662 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004663 && (p - nextline) + len > nextline_idx)
4664 {
4665 /* Remember that the good word continues at the
4666 * start of the next line. */
4667 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004668 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004669 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004670
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004671 /* Turn index into actual attributes. */
4672 if (spell_hlf != HLF_COUNT)
4673 spell_attr = highlight_attr[spell_hlf];
4674
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004675 if (cap_col > 0)
4676 {
4677 if (p != prev_ptr
4678 && (p - nextline) + cap_col >= nextline_idx)
4679 {
4680 /* Remember that the word in the next line
4681 * must start with a capital. */
4682 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 cap_col = (int)((p - nextline) + cap_col
4684 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004685 }
4686 else
4687 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004688 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004689 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004690 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004691 }
4692 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004693 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004694 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004695 char_attr = hl_combine_attr(char_attr, spell_attr);
4696 else
4697 char_attr = hl_combine_attr(spell_attr, char_attr);
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#endif
4700#ifdef FEAT_LINEBREAK
4701 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004702 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004704 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004705 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004707# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004708 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004709# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004710 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004712 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004714 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004715
Bram Moolenaar597a4222014-06-25 14:39:50 +02004716 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004717 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004718 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004719 if (c == TAB && n_extra + col > W_WIDTH(wp))
4720 n_extra = (int)wp->w_buffer->b_p_ts
4721 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4722
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004723# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004724 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004725# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004727# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004728 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004729 {
4730#ifdef FEAT_CONCEAL
4731 if (c == TAB)
4732 /* See "Tab alignment" below. */
4733 FIX_FOR_BOGUSCOLS;
4734#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004735 if (!wp->w_p_list)
4736 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
4739#endif
4740
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004741 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4742 */
4743 if (wp->w_p_list
4744 && (((c == 160
4745#ifdef FEAT_MBYTE
4746 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4747#endif
4748 ) && lcs_nbsp)
4749 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4750 {
4751 c = (c == ' ') ? lcs_space : lcs_nbsp;
4752 if (area_attr == 0 && search_attr == 0)
4753 {
4754 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004755 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004756 saved_attr2 = char_attr; /* save current attr */
4757 }
4758#ifdef FEAT_MBYTE
4759 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004760 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004761 {
4762 mb_utf8 = TRUE;
4763 u8cc[0] = 0;
4764 c = 0xc0;
4765 }
4766 else
4767 mb_utf8 = FALSE;
4768#endif
4769 }
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4772 {
4773 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004774 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004777 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 saved_attr2 = char_attr; /* save current attr */
4779 }
4780#ifdef FEAT_MBYTE
4781 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004782 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004785 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004786 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 else
4789 mb_utf8 = FALSE;
4790#endif
4791 }
4792 }
4793
4794 /*
4795 * Handling of non-printable characters.
4796 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004797 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 /*
4800 * when getting a character from the file, we may have to
4801 * turn it into something else on the way to putting it
4802 * into "ScreenLines".
4803 */
4804 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4805 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004806 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004807 long vcol_adjusted = vcol; /* removed showbreak length */
4808#ifdef FEAT_LINEBREAK
4809 /* only adjust the tab_len, when at the first column
4810 * after the showbreak value was drawn */
4811 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4812 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004815 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004816 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4817
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004818#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004819 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004820#endif
4821 /* tab amount depends on current column */
4822 n_extra = tab_len;
4823#ifdef FEAT_LINEBREAK
4824 else
4825 {
4826 char_u *p;
4827 int len = n_extra;
4828 int i;
4829 int saved_nextra = n_extra;
4830
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004831#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004832 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004833 /* there are characters to conceal */
4834 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004835 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4836 */
4837 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4838 && n_extra > tab_len)
4839 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004840#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004841
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004842 /* if n_extra > 0, it gives the number of chars, to
4843 * use for a tab, else we need to calculate the width
4844 * for a tab */
4845#ifdef FEAT_MBYTE
4846 len = (tab_len * mb_char2len(lcs_tab2));
4847 if (n_extra > 0)
4848 len += n_extra - tab_len;
4849#endif
4850 c = lcs_tab1;
4851 p = alloc((unsigned)(len + 1));
4852 vim_memset(p, ' ', len);
4853 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004854 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004855 p_extra_free = p;
4856 for (i = 0; i < tab_len; i++)
4857 {
4858#ifdef FEAT_MBYTE
4859 mb_char2bytes(lcs_tab2, p);
4860 p += mb_char2len(lcs_tab2);
4861 n_extra += mb_char2len(lcs_tab2)
4862 - (saved_nextra > 0 ? 1 : 0);
4863#else
4864 p[i] = lcs_tab2;
4865#endif
4866 }
4867 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004868#ifdef FEAT_CONCEAL
4869 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4870 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004871 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004872 n_extra -= vcol_off;
4873#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004874 }
4875#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004876#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004877 {
4878 int vc_saved = vcol_off;
4879
4880 /* Tab alignment should be identical regardless of
4881 * 'conceallevel' value. So tab compensates of all
4882 * previous concealed characters, and thus resets
4883 * vcol_off and boguscols accumulated so far in the
4884 * line. Note that the tab can be longer than
4885 * 'tabstop' when there are concealed characters. */
4886 FIX_FOR_BOGUSCOLS;
4887
4888 /* Make sure, the highlighting for the tab char will be
4889 * correctly set further below (effectively reverts the
4890 * FIX_FOR_BOGSUCOLS macro */
4891 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004892 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004893 tab_len += vc_saved;
4894 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896#ifdef FEAT_MBYTE
4897 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4898#endif
4899 if (wp->w_p_list)
4900 {
4901 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902#ifdef FEAT_LINEBREAK
4903 if (wp->w_p_lbr)
4904 c_extra = NUL; /* using p_extra from above */
4905 else
4906#endif
4907 c_extra = lcs_tab2;
4908 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004909 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 saved_attr2 = char_attr; /* save current attr */
4911#ifdef FEAT_MBYTE
4912 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004913 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
4915 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004916 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004917 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919#endif
4920 }
4921 else
4922 {
4923 c_extra = ' ';
4924 c = ' ';
4925 }
4926 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004927 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004928 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004929 || ((fromcol >= 0 || fromcol_prev >= 0)
4930 && tocol > vcol
4931 && VIsual_mode != Ctrl_V
4932 && (
4933# ifdef FEAT_RIGHTLEFT
4934 wp->w_p_rl ? (col >= 0) :
4935# endif
4936 (col < W_WIDTH(wp)))
4937 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004938 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004939 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004940 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004942 /* Display a '$' after the line or highlight an extra
4943 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4945 /* For a diff line the highlighting continues after the
4946 * "$". */
4947 if (
4948# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004949 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950# ifdef LINE_ATTR
4951 &&
4952# endif
4953# endif
4954# ifdef LINE_ATTR
4955 line_attr == 0
4956# endif
4957 )
4958#endif
4959 {
4960#ifdef FEAT_VIRTUALEDIT
4961 /* In virtualedit, visual selections may extend
4962 * beyond end of line. */
4963 if (area_highlighting && virtual_active()
4964 && tocol != MAXCOL && vcol < tocol)
4965 n_extra = 0;
4966 else
4967#endif
4968 {
4969 p_extra = at_end_str;
4970 n_extra = 1;
4971 c_extra = NUL;
4972 }
4973 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004974 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004975 c = lcs_eol;
4976 else
4977 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 lcs_eol_one = -1;
4979 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004980 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004982 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 n_attr = 1;
4984 }
4985#ifdef FEAT_MBYTE
4986 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004987 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004990 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004991 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
4994 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4995#endif
4996 }
4997 else if (c != NUL)
4998 {
4999 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005000 if (n_extra == 0)
5001 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#ifdef FEAT_RIGHTLEFT
5003 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5004 rl_mirror(p_extra); /* reverse "<12>" */
5005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005007#ifdef FEAT_LINEBREAK
5008 if (wp->w_p_lbr)
5009 {
5010 char_u *p;
5011
5012 c = *p_extra;
5013 p = alloc((unsigned)n_extra + 1);
5014 vim_memset(p, ' ', n_extra);
5015 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5016 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005017 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018 p_extra_free = p_extra = p;
5019 }
5020 else
5021#endif
5022 {
5023 n_extra = byte2cells(c) - 1;
5024 c = *p_extra++;
5025 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005026 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005029 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 saved_attr2 = char_attr; /* save current attr */
5031 }
5032#ifdef FEAT_MBYTE
5033 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5034#endif
5035 }
5036#ifdef FEAT_VIRTUALEDIT
5037 else if (VIsual_active
5038 && (VIsual_mode == Ctrl_V
5039 || VIsual_mode == 'v')
5040 && virtual_active()
5041 && tocol != MAXCOL
5042 && vcol < tocol
5043 && (
5044# ifdef FEAT_RIGHTLEFT
5045 wp->w_p_rl ? (col >= 0) :
5046# endif
5047 (col < W_WIDTH(wp))))
5048 {
5049 c = ' ';
5050 --ptr; /* put it back at the NUL */
5051 }
5052#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005053#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else if ((
5055# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005056 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 ) && (
5060# ifdef FEAT_RIGHTLEFT
5061 wp->w_p_rl ? (col >= 0) :
5062# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005063 (col
5064# ifdef FEAT_CONCEAL
5065 - boguscols
5066# endif
5067 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 {
5069 /* Highlight until the right side of the window */
5070 c = ' ';
5071 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005072
5073 /* Remember we do the char for line highlighting. */
5074 ++did_line_attr;
5075
5076 /* don't do search HL for the rest of the line */
5077 if (line_attr != 0 && char_attr == search_attr && col > 0)
5078 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079# ifdef FEAT_DIFF
5080 if (diff_hlf == HLF_TXD)
5081 {
5082 diff_hlf = HLF_CHD;
5083 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005084 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005085 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005086 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5087 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005088 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091# endif
5092 }
5093#endif
5094 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005095
5096#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005097 if ( wp->w_p_cole > 0
5098 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005099 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005100 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005101 && !(lnum_in_visual_area
5102 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005103 {
5104 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005105 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005106 && (syn_get_sub_char() != NUL || match_conc
5107 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005108 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005109 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005110 /* First time at this concealed item: display one
5111 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005112 if (match_conc)
5113 c = match_conc;
5114 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005115 c = syn_get_sub_char();
5116 else if (lcs_conceal != NUL)
5117 c = lcs_conceal;
5118 else
5119 c = ' ';
5120
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005121 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005122
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005123 if (n_extra > 0)
5124 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005125 vcol += n_extra;
5126 if (wp->w_p_wrap && n_extra > 0)
5127 {
5128# ifdef FEAT_RIGHTLEFT
5129 if (wp->w_p_rl)
5130 {
5131 col -= n_extra;
5132 boguscols -= n_extra;
5133 }
5134 else
5135# endif
5136 {
5137 boguscols += n_extra;
5138 col += n_extra;
5139 }
5140 }
5141 n_extra = 0;
5142 n_attr = 0;
5143 }
5144 else if (n_skip == 0)
5145 {
5146 is_concealing = TRUE;
5147 n_skip = 1;
5148 }
5149# ifdef FEAT_MBYTE
5150 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005151 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005152 {
5153 mb_utf8 = TRUE;
5154 u8cc[0] = 0;
5155 c = 0xc0;
5156 }
5157 else
5158 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5159# endif
5160 }
5161 else
5162 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005163 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005164 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005165 }
5166#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005169#ifdef FEAT_CONCEAL
5170 /* In the cursor line and we may be concealing characters: correct
5171 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005172 if (!did_wcol && draw_state == WL_LINE
5173 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005174 && conceal_cursor_line(wp)
5175 && (int)wp->w_virtcol <= vcol + n_skip)
5176 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005177# ifdef FEAT_RIGHTLEFT
5178 if (wp->w_p_rl)
5179 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5180 else
5181# endif
5182 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005183 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005184 did_wcol = TRUE;
5185 }
5186#endif
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 /* Don't override visual selection highlighting. */
5189 if (n_attr > 0
5190 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005191 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 char_attr = extra_attr;
5193
Bram Moolenaar81695252004-12-29 20:58:21 +00005194#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /* XIM don't send preedit_start and preedit_end, but they send
5196 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5197 * im_is_preediting() here. */
5198 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005199 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 && (State & INSERT)
5201 && !p_imdisable
5202 && im_is_preediting()
5203 && draw_state == WL_LINE)
5204 {
5205 colnr_T tcol;
5206
5207 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005208 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 else
5210 tcol = preedit_end_col;
5211 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5212 {
5213 if (feedback_old_attr < 0)
5214 {
5215 feedback_col = 0;
5216 feedback_old_attr = char_attr;
5217 }
5218 char_attr = im_get_feedback_attr(feedback_col);
5219 if (char_attr < 0)
5220 char_attr = feedback_old_attr;
5221 feedback_col++;
5222 }
5223 else if (feedback_old_attr >= 0)
5224 {
5225 char_attr = feedback_old_attr;
5226 feedback_old_attr = -1;
5227 feedback_col = 0;
5228 }
5229 }
5230#endif
5231 /*
5232 * Handle the case where we are in column 0 but not on the first
5233 * character of the line and the user wants us to show us a
5234 * special character (via 'listchars' option "precedes:<char>".
5235 */
5236 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005237 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5239#ifdef FEAT_DIFF
5240 && filler_todo <= 0
5241#endif
5242 && draw_state > WL_NR
5243 && c != NUL)
5244 {
5245 c = lcs_prec;
5246 lcs_prec_todo = NUL;
5247#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005248 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5249 {
5250 /* Double-width character being overwritten by the "precedes"
5251 * character, need to fill up half the character. */
5252 c_extra = MB_FILLER_CHAR;
5253 n_extra = 1;
5254 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005255 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005258 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005261 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005262 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 else
5265 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5266#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005267 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005270 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 n_attr3 = 1;
5272 }
5273 }
5274
5275 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005276 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005278 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005279#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005280 || did_line_attr == 1
5281#endif
5282 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005284#ifdef FEAT_SEARCH_EXTRA
5285 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005286
5287 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005288 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005289 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005290#endif
5291
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005292 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 * highlight match at end of line. If it's beyond the last
5294 * char on the screen, just overwrite that one (tricky!) Not
5295 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005296#ifdef FEAT_SEARCH_EXTRA
5297 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005298 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005299 prevcol_hl_flag = TRUE;
5300 else
5301 {
5302 cur = wp->w_match_head;
5303 while (cur != NULL)
5304 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005305 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005306 {
5307 prevcol_hl_flag = TRUE;
5308 break;
5309 }
5310 cur = cur->next;
5311 }
5312 }
5313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005315 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005316 && (VIsual_mode != Ctrl_V
5317 || lnum == VIsual.lnum
5318 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005319 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320#ifdef FEAT_SEARCH_EXTRA
5321 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005322 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005323# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005324 && did_line_attr <= 1
5325# endif
5326 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#endif
5328 ))
5329 {
5330 int n = 0;
5331
5332#ifdef FEAT_RIGHTLEFT
5333 if (wp->w_p_rl)
5334 {
5335 if (col < 0)
5336 n = 1;
5337 }
5338 else
5339#endif
5340 {
5341 if (col >= W_WIDTH(wp))
5342 n = -1;
5343 }
5344 if (n != 0)
5345 {
5346 /* At the window boundary, highlight the last character
5347 * instead (better than nothing). */
5348 off += n;
5349 col += n;
5350 }
5351 else
5352 {
5353 /* Add a blank character to highlight. */
5354 ScreenLines[off] = ' ';
5355#ifdef FEAT_MBYTE
5356 if (enc_utf8)
5357 ScreenLinesUC[off] = 0;
5358#endif
5359 }
5360#ifdef FEAT_SEARCH_EXTRA
5361 if (area_attr == 0)
5362 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005363 /* Use attributes from match with highest priority among
5364 * 'search_hl' and the match list. */
5365 char_attr = search_hl.attr;
5366 cur = wp->w_match_head;
5367 shl_flag = FALSE;
5368 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005369 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005370 if (shl_flag == FALSE
5371 && ((cur != NULL
5372 && cur->priority > SEARCH_HL_PRIORITY)
5373 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005374 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005375 shl = &search_hl;
5376 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005377 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005378 else
5379 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005380 if ((ptr - line) - 1 == (long)shl->startcol
5381 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005382 char_attr = shl->attr;
5383 if (shl != &search_hl && cur != NULL)
5384 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387#endif
5388 ScreenAttrs[off] = char_attr;
5389#ifdef FEAT_RIGHTLEFT
5390 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005393 --off;
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else
5396#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005399 ++off;
5400 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005401 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005402#ifdef FEAT_SYN_HL
5403 eol_hl_off = 1;
5404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
Bram Moolenaar91170f82006-05-05 21:15:17 +00005408 /*
5409 * At end of the text line.
5410 */
5411 if (c == NUL)
5412 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005413#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005414 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5415 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005416 {
5417 /* highlight last char after line */
5418 --col;
5419 --off;
5420 --vcol;
5421 }
5422
Bram Moolenaar1a384422010-07-14 19:53:30 +02005423 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005424 if (wp->w_p_wrap)
5425 v = wp->w_skipcol;
5426 else
5427 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005428
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005429 /* check if line ends before left margin */
5430 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005431 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005432#ifdef FEAT_CONCEAL
5433 /* Get rid of the boguscols now, we want to draw until the right
5434 * edge for 'cursorcolumn'. */
5435 col -= boguscols;
5436 boguscols = 0;
5437#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005438
5439 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005440 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005441
5442 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005443 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5444 && (int)wp->w_virtcol <
5445 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005446 && lnum != wp->w_cursor.lnum)
5447 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005448# ifdef FEAT_RIGHTLEFT
5449 && !wp->w_p_rl
5450# endif
5451 )
5452 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005453 int rightmost_vcol = 0;
5454 int i;
5455
5456 if (wp->w_p_cuc)
5457 rightmost_vcol = wp->w_virtcol;
5458 if (draw_color_col)
5459 /* determine rightmost colorcolumn to possibly draw */
5460 for (i = 0; color_cols[i] >= 0; ++i)
5461 if (rightmost_vcol < color_cols[i])
5462 rightmost_vcol = color_cols[i];
5463
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005464 while (col < W_WIDTH(wp))
5465 {
5466 ScreenLines[off] = ' ';
5467#ifdef FEAT_MBYTE
5468 if (enc_utf8)
5469 ScreenLinesUC[off] = 0;
5470#endif
5471 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005472 if (draw_color_col)
5473 draw_color_col = advance_color_col(VCOL_HLC,
5474 &color_cols);
5475
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005476 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005477 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005478 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005479 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005480 else
5481 ScreenAttrs[off++] = 0;
5482
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005483 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005484 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005485
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005486 ++vcol;
5487 }
5488 }
5489#endif
5490
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005491 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005492 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 row++;
5494
5495 /*
5496 * Update w_cline_height and w_cline_folded if the cursor line was
5497 * updated (saves a call to plines() later).
5498 */
5499 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5500 {
5501 curwin->w_cline_row = startrow;
5502 curwin->w_cline_height = row - startrow;
5503#ifdef FEAT_FOLDING
5504 curwin->w_cline_folded = FALSE;
5505#endif
5506 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5507 }
5508
5509 break;
5510 }
5511
5512 /* line continues beyond line end */
5513 if (lcs_ext
5514 && !wp->w_p_wrap
5515#ifdef FEAT_DIFF
5516 && filler_todo <= 0
5517#endif
5518 && (
5519#ifdef FEAT_RIGHTLEFT
5520 wp->w_p_rl ? col == 0 :
5521#endif
5522 col == W_WIDTH(wp) - 1)
5523 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005524 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5526 {
5527 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005528 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#ifdef FEAT_MBYTE
5530 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005531 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005534 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005535 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 }
5537 else
5538 mb_utf8 = FALSE;
5539#endif
5540 }
5541
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005542#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005543 /* advance to the next 'colorcolumn' */
5544 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005545 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005546
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005547 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005548 * highlight the cursor position itself.
5549 * Also highlight the 'colorcolumn' if it is different than
5550 * 'cursorcolumn' */
5551 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005552 if (draw_state == WL_LINE && !lnum_in_visual_area
5553 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005554 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005555 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005556 && lnum != wp->w_cursor.lnum)
5557 {
5558 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005559 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005560 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005561 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562 {
5563 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005564 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005565 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005566 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005567#endif
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 /*
5570 * Store character to be displayed.
5571 * Skip characters that are left of the screen for 'nowrap'.
5572 */
5573 vcol_prev = vcol;
5574 if (draw_state < WL_LINE || n_skip <= 0)
5575 {
5576 /*
5577 * Store the character.
5578 */
5579#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5580 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5581 {
5582 /* A double-wide character is: put first halve in left cell. */
5583 --off;
5584 --col;
5585 }
5586#endif
5587 ScreenLines[off] = c;
5588#ifdef FEAT_MBYTE
5589 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005590 {
5591 if ((mb_c & 0xff00) == 0x8e00)
5592 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 else if (enc_utf8)
5596 {
5597 if (mb_utf8)
5598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005599 int i;
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005602 if ((c & 0xff) == 0)
5603 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005604 for (i = 0; i < Screen_mco; ++i)
5605 {
5606 ScreenLinesC[i][off] = u8cc[i];
5607 if (u8cc[i] == 0)
5608 break;
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611 else
5612 ScreenLinesUC[off] = 0;
5613 }
5614 if (multi_attr)
5615 {
5616 ScreenAttrs[off] = multi_attr;
5617 multi_attr = 0;
5618 }
5619 else
5620#endif
5621 ScreenAttrs[off] = char_attr;
5622
5623#ifdef FEAT_MBYTE
5624 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5625 {
5626 /* Need to fill two screen columns. */
5627 ++off;
5628 ++col;
5629 if (enc_utf8)
5630 /* UTF-8: Put a 0 in the second screen char. */
5631 ScreenLines[off] = 0;
5632 else
5633 /* DBCS: Put second byte in the second screen char. */
5634 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005635 if (draw_state > WL_NR
5636#ifdef FEAT_DIFF
5637 && filler_todo <= 0
5638#endif
5639 )
5640 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 /* When "tocol" is halfway a character, set it to the end of
5642 * the character, otherwise highlighting won't stop. */
5643 if (tocol == vcol)
5644 ++tocol;
5645#ifdef FEAT_RIGHTLEFT
5646 if (wp->w_p_rl)
5647 {
5648 /* now it's time to backup one cell */
5649 --off;
5650 --col;
5651 }
5652#endif
5653 }
5654#endif
5655#ifdef FEAT_RIGHTLEFT
5656 if (wp->w_p_rl)
5657 {
5658 --off;
5659 --col;
5660 }
5661 else
5662#endif
5663 {
5664 ++off;
5665 ++col;
5666 }
5667 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005668#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005669 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005670 {
5671 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005672 ++vcol_off;
5673 if (n_extra > 0)
5674 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005675 if (wp->w_p_wrap)
5676 {
5677 /*
5678 * Special voodoo required if 'wrap' is on.
5679 *
5680 * Advance the column indicator to force the line
5681 * drawing to wrap early. This will make the line
5682 * take up the same screen space when parts are concealed,
5683 * so that cursor line computations aren't messed up.
5684 *
5685 * To avoid the fictitious advance of 'col' causing
5686 * trailing junk to be written out of the screen line
5687 * we are building, 'boguscols' keeps track of the number
5688 * of bad columns we have advanced.
5689 */
5690 if (n_extra > 0)
5691 {
5692 vcol += n_extra;
5693# ifdef FEAT_RIGHTLEFT
5694 if (wp->w_p_rl)
5695 {
5696 col -= n_extra;
5697 boguscols -= n_extra;
5698 }
5699 else
5700# endif
5701 {
5702 col += n_extra;
5703 boguscols += n_extra;
5704 }
5705 n_extra = 0;
5706 n_attr = 0;
5707 }
5708
5709
5710# ifdef FEAT_MBYTE
5711 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5712 {
5713 /* Need to fill two screen columns. */
5714# ifdef FEAT_RIGHTLEFT
5715 if (wp->w_p_rl)
5716 {
5717 --boguscols;
5718 --col;
5719 }
5720 else
5721# endif
5722 {
5723 ++boguscols;
5724 ++col;
5725 }
5726 }
5727# endif
5728
5729# ifdef FEAT_RIGHTLEFT
5730 if (wp->w_p_rl)
5731 {
5732 --boguscols;
5733 --col;
5734 }
5735 else
5736# endif
5737 {
5738 ++boguscols;
5739 ++col;
5740 }
5741 }
5742 else
5743 {
5744 if (n_extra > 0)
5745 {
5746 vcol += n_extra;
5747 n_extra = 0;
5748 n_attr = 0;
5749 }
5750 }
5751
5752 }
5753#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 else
5755 --n_skip;
5756
Bram Moolenaar64486672010-05-16 15:46:46 +02005757 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5758 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005759 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760#ifdef FEAT_DIFF
5761 && filler_todo <= 0
5762#endif
5763 )
5764 ++vcol;
5765
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005766#ifdef FEAT_SYN_HL
5767 if (vcol_save_attr >= 0)
5768 char_attr = vcol_save_attr;
5769#endif
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 /* restore attributes after "predeces" in 'listchars' */
5772 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5773 char_attr = saved_attr3;
5774
5775 /* restore attributes after last 'listchars' or 'number' char */
5776 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5777 char_attr = saved_attr2;
5778
5779 /*
5780 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005781 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 */
5783 if ((
5784#ifdef FEAT_RIGHTLEFT
5785 wp->w_p_rl ? (col < 0) :
5786#endif
5787 (col >= W_WIDTH(wp)))
5788 && (*ptr != NUL
5789#ifdef FEAT_DIFF
5790 || filler_todo > 0
5791#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005792 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5794 )
5795 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005796#ifdef FEAT_CONCEAL
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005797 screen_line(screen_row, W_WINCOL(wp), col - boguscols,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005798 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005799 boguscols = 0;
5800#else
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02005801 screen_line(screen_row, W_WINCOL(wp), col,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02005802 (int)W_WIDTH(wp), HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 ++row;
5805 ++screen_row;
5806
5807 /* When not wrapping and finished diff lines, or when displayed
5808 * '$' and highlighting until last column, break here. */
5809 if ((!wp->w_p_wrap
5810#ifdef FEAT_DIFF
5811 && filler_todo <= 0
5812#endif
5813 ) || lcs_eol_one == -1)
5814 break;
5815
5816 /* When the window is too narrow draw all "@" lines. */
5817 if (draw_state != WL_LINE
5818#ifdef FEAT_DIFF
5819 && filler_todo <= 0
5820#endif
5821 )
5822 {
5823 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005824#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 draw_vsep_win(wp, row);
5826#endif
5827 row = endrow;
5828 }
5829
5830 /* When line got too long for screen break here. */
5831 if (row == endrow)
5832 {
5833 ++row;
5834 break;
5835 }
5836
5837 if (screen_cur_row == screen_row - 1
5838#ifdef FEAT_DIFF
5839 && filler_todo <= 0
5840#endif
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005841#ifdef FEAT_WINDOWS
5842 && W_WIDTH(wp) == Columns
5843#endif
5844 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
5846 /* Remember that the line wraps, used for modeless copy. */
5847 LineWraps[screen_row - 1] = TRUE;
5848
5849 /*
5850 * Special trick to make copy/paste of wrapped lines work with
5851 * xterm/screen: write an extra character beyond the end of
5852 * the line. This will work with all terminal types
5853 * (regardless of the xn,am settings).
5854 * Only do this on a fast tty.
5855 * Only do this if the cursor is on the current line
5856 * (something has been written in it).
5857 * Don't do this for the GUI.
5858 * Don't do this for double-width characters.
5859 * Don't do this for a window not at the right screen border.
5860 */
5861 if (p_tf
5862#ifdef FEAT_GUI
5863 && !gui.in_use
5864#endif
5865#ifdef FEAT_MBYTE
5866 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005867 && ((*mb_off2cells)(LineOffset[screen_row],
5868 LineOffset[screen_row] + screen_Columns)
5869 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005871 + (int)Columns - 2,
5872 LineOffset[screen_row] + screen_Columns)
5873 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#endif
5875 )
5876 {
5877 /* First make sure we are at the end of the screen line,
5878 * then output the same character again to let the
5879 * terminal know about the wrap. If the terminal doesn't
5880 * auto-wrap, we overwrite the character. */
5881 if (screen_cur_col != W_WIDTH(wp))
5882 screen_char(LineOffset[screen_row - 1]
5883 + (unsigned)Columns - 1,
5884 screen_row - 1, (int)(Columns - 1));
5885
5886#ifdef FEAT_MBYTE
5887 /* When there is a multi-byte character, just output a
5888 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005889 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5890 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 out_char(' ');
5892 else
5893#endif
5894 out_char(ScreenLines[LineOffset[screen_row - 1]
5895 + (Columns - 1)]);
5896 /* force a redraw of the first char on the next line */
5897 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5898 screen_start(); /* don't know where cursor is now */
5899 }
5900 }
5901
5902 col = 0;
5903 off = (unsigned)(current_ScreenLine - ScreenLines);
5904#ifdef FEAT_RIGHTLEFT
5905 if (wp->w_p_rl)
5906 {
5907 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5908 off += col;
5909 }
5910#endif
5911
5912 /* reset the drawing state for the start of a wrapped line */
5913 draw_state = WL_START;
5914 saved_n_extra = n_extra;
5915 saved_p_extra = p_extra;
5916 saved_c_extra = c_extra;
5917 saved_char_attr = char_attr;
5918 n_extra = 0;
5919 lcs_prec_todo = lcs_prec;
5920#ifdef FEAT_LINEBREAK
5921# ifdef FEAT_DIFF
5922 if (filler_todo <= 0)
5923# endif
5924 need_showbreak = TRUE;
5925#endif
5926#ifdef FEAT_DIFF
5927 --filler_todo;
5928 /* When the filler lines are actually below the last line of the
5929 * file, don't draw the line itself, break here. */
5930 if (filler_todo == 0 && wp->w_botfill)
5931 break;
5932#endif
5933 }
5934
5935 } /* for every character in the line */
5936
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005937#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005938 /* After an empty line check first word for capital. */
5939 if (*skipwhite(line) == NUL)
5940 {
5941 capcol_lnum = lnum + 1;
5942 cap_col = 0;
5943 }
5944#endif
5945
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005946 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 return row;
5948}
5949
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005950#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005951static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005952
5953/*
5954 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005955 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005956 */
5957 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005958comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005959{
5960 int i;
5961
5962 for (i = 0; i < Screen_mco; ++i)
5963 {
5964 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5965 return TRUE;
5966 if (ScreenLinesC[i][off_from] == 0)
5967 break;
5968 }
5969 return FALSE;
5970}
5971#endif
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973/*
5974 * Check whether the given character needs redrawing:
5975 * - the (first byte of the) character is different
5976 * - the attributes are different
5977 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005978 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 */
5980 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005981char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 if (cols > 0
5984 && ((ScreenLines[off_from] != ScreenLines[off_to]
5985 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5986
5987#ifdef FEAT_MBYTE
5988 || (enc_dbcs != 0
5989 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5990 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5991 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5992 : (cols > 1 && ScreenLines[off_from + 1]
5993 != ScreenLines[off_to + 1])))
5994 || (enc_utf8
5995 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5996 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005997 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005998 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5999 && ScreenLines[off_from + 1]
6000 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001#endif
6002 ))
6003 return TRUE;
6004 return FALSE;
6005}
6006
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006007#if defined(FEAT_TERMINAL) || defined(PROTO)
6008/*
6009 * Return the index in ScreenLines[] for the current screen line.
6010 */
6011 int
6012screen_get_current_line_off()
6013{
6014 return (int)(current_ScreenLine - ScreenLines);
6015}
6016#endif
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018/*
6019 * Move one "cooked" screen line to the screen, but only the characters that
6020 * have actually changed. Handle insert/delete character.
6021 * "coloff" gives the first column on the screen for this line.
6022 * "endcol" gives the columns where valid characters are.
6023 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6024 * needs to be cleared, negative otherwise.
6025 * "rlflag" is TRUE in a rightleft window:
6026 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6027 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6028 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006030screen_line(
6031 int row,
6032 int coloff,
6033 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006034 int clear_width,
6035 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
6037 unsigned off_from;
6038 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006039#ifdef FEAT_MBYTE
6040 unsigned max_off_from;
6041 unsigned max_off_to;
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006044#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 int hl;
6046#endif
6047 int force = FALSE; /* force update rest of the line */
6048 int redraw_this /* bool: does character need redraw? */
6049#ifdef FEAT_GUI
6050 = TRUE /* For GUI when while-loop empty */
6051#endif
6052 ;
6053 int redraw_next; /* redraw_this for next character */
6054#ifdef FEAT_MBYTE
6055 int clear_next = FALSE;
6056 int char_cells; /* 1: normal char */
6057 /* 2: occupies two display cells */
6058# define CHAR_CELLS char_cells
6059#else
6060# define CHAR_CELLS 1
6061#endif
6062
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006063 /* Check for illegal row and col, just in case. */
6064 if (row >= Rows)
6065 row = Rows - 1;
6066 if (endcol > Columns)
6067 endcol = Columns;
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069# ifdef FEAT_CLIPBOARD
6070 clip_may_clear_selection(row, row);
6071# endif
6072
6073 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6074 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006075#ifdef FEAT_MBYTE
6076 max_off_from = off_from + screen_Columns;
6077 max_off_to = LineOffset[row] + screen_Columns;
6078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079
6080#ifdef FEAT_RIGHTLEFT
6081 if (rlflag)
6082 {
6083 /* Clear rest first, because it's left of the text. */
6084 if (clear_width > 0)
6085 {
6086 while (col <= endcol && ScreenLines[off_to] == ' '
6087 && ScreenAttrs[off_to] == 0
6088# ifdef FEAT_MBYTE
6089 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6090# endif
6091 )
6092 {
6093 ++off_to;
6094 ++col;
6095 }
6096 if (col <= endcol)
6097 screen_fill(row, row + 1, col + coloff,
6098 endcol + coloff + 1, ' ', ' ', 0);
6099 }
6100 col = endcol + 1;
6101 off_to = LineOffset[row] + col + coloff;
6102 off_from += col;
6103 endcol = (clear_width > 0 ? clear_width : -clear_width);
6104 }
6105#endif /* FEAT_RIGHTLEFT */
6106
6107 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6108
6109 while (col < endcol)
6110 {
6111#ifdef FEAT_MBYTE
6112 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006113 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 else
6115 char_cells = 1;
6116#endif
6117
6118 redraw_this = redraw_next;
6119 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6120 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6121
6122#ifdef FEAT_GUI
6123 /* If the next character was bold, then redraw the current character to
6124 * remove any pixels that might have spilt over into us. This only
6125 * happens in the GUI.
6126 */
6127 if (redraw_next && gui.in_use)
6128 {
6129 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006130 if (hl > HL_ALL)
6131 hl = syn_attr2attr(hl);
6132 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 redraw_this = TRUE;
6134 }
6135#endif
6136
6137 if (redraw_this)
6138 {
6139 /*
6140 * Special handling when 'xs' termcap flag set (hpterm):
6141 * Attributes for characters are stored at the position where the
6142 * cursor is when writing the highlighting code. The
6143 * start-highlighting code must be written with the cursor on the
6144 * first highlighted character. The stop-highlighting code must
6145 * be written with the cursor just after the last highlighted
6146 * character.
6147 * Overwriting a character doesn't remove it's highlighting. Need
6148 * to clear the rest of the line, and force redrawing it
6149 * completely.
6150 */
6151 if ( p_wiv
6152 && !force
6153#ifdef FEAT_GUI
6154 && !gui.in_use
6155#endif
6156 && ScreenAttrs[off_to] != 0
6157 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6158 {
6159 /*
6160 * Need to remove highlighting attributes here.
6161 */
6162 windgoto(row, col + coloff);
6163 out_str(T_CE); /* clear rest of this screen line */
6164 screen_start(); /* don't know where cursor is now */
6165 force = TRUE; /* force redraw of rest of the line */
6166 redraw_next = TRUE; /* or else next char would miss out */
6167
6168 /*
6169 * If the previous character was highlighted, need to stop
6170 * highlighting at this character.
6171 */
6172 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6173 {
6174 screen_attr = ScreenAttrs[off_to - 1];
6175 term_windgoto(row, col + coloff);
6176 screen_stop_highlight();
6177 }
6178 else
6179 screen_attr = 0; /* highlighting has stopped */
6180 }
6181#ifdef FEAT_MBYTE
6182 if (enc_dbcs != 0)
6183 {
6184 /* Check if overwriting a double-byte with a single-byte or
6185 * the other way around requires another character to be
6186 * redrawn. For UTF-8 this isn't needed, because comparing
6187 * ScreenLinesUC[] is sufficient. */
6188 if (char_cells == 1
6189 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006190 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 {
6192 /* Writing a single-cell character over a double-cell
6193 * character: need to redraw the next cell. */
6194 ScreenLines[off_to + 1] = 0;
6195 redraw_next = TRUE;
6196 }
6197 else if (char_cells == 2
6198 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 && (*mb_off2cells)(off_to, max_off_to) == 1
6200 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
6202 /* Writing the second half of a double-cell character over
6203 * a double-cell character: need to redraw the second
6204 * cell. */
6205 ScreenLines[off_to + 2] = 0;
6206 redraw_next = TRUE;
6207 }
6208
6209 if (enc_dbcs == DBCS_JPNU)
6210 ScreenLines2[off_to] = ScreenLines2[off_from];
6211 }
6212 /* When writing a single-width character over a double-width
6213 * character and at the end of the redrawn text, need to clear out
6214 * the right halve of the old character.
6215 * Also required when writing the right halve of a double-width
6216 * char over the left halve of an existing one. */
6217 if (has_mbyte && col + char_cells == endcol
6218 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006219 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006221 && (*mb_off2cells)(off_to, max_off_to) == 1
6222 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 clear_next = TRUE;
6224#endif
6225
6226 ScreenLines[off_to] = ScreenLines[off_from];
6227#ifdef FEAT_MBYTE
6228 if (enc_utf8)
6229 {
6230 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6231 if (ScreenLinesUC[off_from] != 0)
6232 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006233 int i;
6234
6235 for (i = 0; i < Screen_mco; ++i)
6236 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238 }
6239 if (char_cells == 2)
6240 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6241#endif
6242
6243#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006244 /* The bold trick makes a single column of pixels appear in the
6245 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * character should be redrawn too. This happens for our own GUI
6247 * and for some xterms. */
6248 if (
6249# ifdef FEAT_GUI
6250 gui.in_use
6251# endif
6252# if defined(FEAT_GUI) && defined(UNIX)
6253 ||
6254# endif
6255# ifdef UNIX
6256 term_is_xterm
6257# endif
6258 )
6259 {
6260 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006261 if (hl > HL_ALL)
6262 hl = syn_attr2attr(hl);
6263 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 redraw_next = TRUE;
6265 }
6266#endif
6267 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6268#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006269 /* For simplicity set the attributes of second half of a
6270 * double-wide character equal to the first half. */
6271 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006273
6274 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 else
6277#endif
6278 screen_char(off_to, row, col + coloff);
6279 }
6280 else if ( p_wiv
6281#ifdef FEAT_GUI
6282 && !gui.in_use
6283#endif
6284 && col + coloff > 0)
6285 {
6286 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6287 {
6288 /*
6289 * Don't output stop-highlight when moving the cursor, it will
6290 * stop the highlighting when it should continue.
6291 */
6292 screen_attr = 0;
6293 }
6294 else if (screen_attr != 0)
6295 screen_stop_highlight();
6296 }
6297
6298 off_to += CHAR_CELLS;
6299 off_from += CHAR_CELLS;
6300 col += CHAR_CELLS;
6301 }
6302
6303#ifdef FEAT_MBYTE
6304 if (clear_next)
6305 {
6306 /* Clear the second half of a double-wide character of which the left
6307 * half was overwritten with a single-wide character. */
6308 ScreenLines[off_to] = ' ';
6309 if (enc_utf8)
6310 ScreenLinesUC[off_to] = 0;
6311 screen_char(off_to, row, col + coloff);
6312 }
6313#endif
6314
6315 if (clear_width > 0
6316#ifdef FEAT_RIGHTLEFT
6317 && !rlflag
6318#endif
6319 )
6320 {
6321#ifdef FEAT_GUI
6322 int startCol = col;
6323#endif
6324
6325 /* blank out the rest of the line */
6326 while (col < clear_width && ScreenLines[off_to] == ' '
6327 && ScreenAttrs[off_to] == 0
6328#ifdef FEAT_MBYTE
6329 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6330#endif
6331 )
6332 {
6333 ++off_to;
6334 ++col;
6335 }
6336 if (col < clear_width)
6337 {
6338#ifdef FEAT_GUI
6339 /*
6340 * In the GUI, clearing the rest of the line may leave pixels
6341 * behind if the first character cleared was bold. Some bold
6342 * fonts spill over the left. In this case we redraw the previous
6343 * character too. If we didn't skip any blanks above, then we
6344 * only redraw if the character wasn't already redrawn anyway.
6345 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006346 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 {
6348 hl = ScreenAttrs[off_to];
6349 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006350 {
6351 int prev_cells = 1;
6352# ifdef FEAT_MBYTE
6353 if (enc_utf8)
6354 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6355 * that its width is 2. */
6356 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6357 else if (enc_dbcs != 0)
6358 {
6359 /* find previous character by counting from first
6360 * column and get its width. */
6361 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006362 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006363
6364 while (off < off_to)
6365 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006366 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006367 off += prev_cells;
6368 }
6369 }
6370
6371 if (enc_dbcs != 0 && prev_cells > 1)
6372 screen_char_2(off_to - prev_cells, row,
6373 col + coloff - prev_cells);
6374 else
6375# endif
6376 screen_char(off_to - prev_cells, row,
6377 col + coloff - prev_cells);
6378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380#endif
6381 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6382 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006383#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 off_to += clear_width - col;
6385 col = clear_width;
6386#endif
6387 }
6388 }
6389
6390 if (clear_width > 0)
6391 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006392#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 /* For a window that's left of another, draw the separator char. */
6394 if (col + coloff < Columns)
6395 {
6396 int c;
6397
6398 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006399 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006401 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6402 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# endif
6404 || ScreenAttrs[off_to] != hl)
6405 {
6406 ScreenLines[off_to] = c;
6407 ScreenAttrs[off_to] = hl;
6408# ifdef FEAT_MBYTE
6409 if (enc_utf8)
6410 {
6411 if (c >= 0x80)
6412 {
6413 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006414 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416 else
6417 ScreenLinesUC[off_to] = 0;
6418 }
6419# endif
6420 screen_char(off_to, row, col + coloff);
6421 }
6422 }
6423 else
6424#endif
6425 LineWraps[row] = FALSE;
6426 }
6427}
6428
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006429#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006431 * Mirror text "str" for right-left displaying.
6432 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006434 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006435rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char_u *p1, *p2;
6438 int t;
6439
6440 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6441 {
6442 t = *p1;
6443 *p1 = *p2;
6444 *p2 = t;
6445 }
6446}
6447#endif
6448
6449#if defined(FEAT_WINDOWS) || defined(PROTO)
6450/*
6451 * mark all status lines for redraw; used after first :cd
6452 */
6453 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006454status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 win_T *wp;
6457
Bram Moolenaar29323592016-07-24 22:04:11 +02006458 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (wp->w_status_height)
6460 {
6461 wp->w_redr_status = TRUE;
6462 redraw_later(VALID);
6463 }
6464}
6465
6466/*
6467 * mark all status lines of the current buffer for redraw
6468 */
6469 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006470status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 win_T *wp;
6473
Bram Moolenaar29323592016-07-24 22:04:11 +02006474 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6476 {
6477 wp->w_redr_status = TRUE;
6478 redraw_later(VALID);
6479 }
6480}
6481
6482/*
6483 * Redraw all status lines that need to be redrawn.
6484 */
6485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006486redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 win_T *wp;
6489
Bram Moolenaar29323592016-07-24 22:04:11 +02006490 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (wp->w_redr_status)
6492 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006493 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006494 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495}
6496#endif
6497
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006498#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
6500 * Redraw all status lines at the bottom of frame "frp".
6501 */
6502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006503win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 if (frp->fr_layout == FR_LEAF)
6506 frp->fr_win->w_redr_status = TRUE;
6507 else if (frp->fr_layout == FR_ROW)
6508 {
6509 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6510 win_redraw_last_status(frp);
6511 }
6512 else /* frp->fr_layout == FR_COL */
6513 {
6514 frp = frp->fr_child;
6515 while (frp->fr_next != NULL)
6516 frp = frp->fr_next;
6517 win_redraw_last_status(frp);
6518 }
6519}
6520#endif
6521
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006522#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523/*
6524 * Draw the verticap separator right of window "wp" starting with line "row".
6525 */
6526 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006527draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 int hl;
6530 int c;
6531
6532 if (wp->w_vsep_width)
6533 {
6534 /* draw the vertical separator right of this window */
6535 c = fillchar_vsep(&hl);
6536 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6537 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6538 c, ' ', hl);
6539 }
6540}
6541#endif
6542
6543#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006544static int status_match_len(expand_T *xp, char_u *s);
6545static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006548 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 */
6550 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006551status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 int len = 0;
6554
6555#ifdef FEAT_MENU
6556 int emenu = (xp->xp_context == EXPAND_MENUS
6557 || xp->xp_context == EXPAND_MENUNAMES);
6558
6559 /* Check for menu separators - replace with '|'. */
6560 if (emenu && menu_is_separator(s))
6561 return 1;
6562#endif
6563
6564 while (*s != NUL)
6565 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006566 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006567 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006568 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570
6571 return len;
6572}
6573
6574/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006575 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006576 * These are backslashes used for escaping. Do show backslashes in help tags.
6577 */
6578 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006579skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006580{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006581 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006582#ifdef FEAT_MENU
6583 || ((xp->xp_context == EXPAND_MENUS
6584 || xp->xp_context == EXPAND_MENUNAMES)
6585 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6586#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006587 )
6588 {
6589#ifndef BACKSLASH_IN_FILENAME
6590 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6591 return 2;
6592#endif
6593 return 1;
6594 }
6595 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006596}
6597
6598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 * Show wildchar matches in the status line.
6600 * Show at least the "match" item.
6601 * We start at item 'first_match' in the list and show all matches that fit.
6602 *
6603 * If inversion is possible we use it. Else '=' characters are used.
6604 */
6605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006606win_redr_status_matches(
6607 expand_T *xp,
6608 int num_matches,
6609 char_u **matches, /* list of matches */
6610 int match,
6611 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6614 int row;
6615 char_u *buf;
6616 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006617 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 int fillchar;
6619 int attr;
6620 int i;
6621 int highlight = TRUE;
6622 char_u *selstart = NULL;
6623 int selstart_col = 0;
6624 char_u *selend = NULL;
6625 static int first_match = 0;
6626 int add_left = FALSE;
6627 char_u *s;
6628#ifdef FEAT_MENU
6629 int emenu;
6630#endif
6631#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6632 int l;
6633#endif
6634
6635 if (matches == NULL) /* interrupted completion? */
6636 return;
6637
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006638#ifdef FEAT_MBYTE
6639 if (has_mbyte)
6640 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6641 else
6642#endif
6643 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (buf == NULL)
6645 return;
6646
6647 if (match == -1) /* don't show match but original text */
6648 {
6649 match = 0;
6650 highlight = FALSE;
6651 }
6652 /* count 1 for the ending ">" */
6653 clen = status_match_len(xp, L_MATCH(match)) + 3;
6654 if (match == 0)
6655 first_match = 0;
6656 else if (match < first_match)
6657 {
6658 /* jumping left, as far as we can go */
6659 first_match = match;
6660 add_left = TRUE;
6661 }
6662 else
6663 {
6664 /* check if match fits on the screen */
6665 for (i = first_match; i < match; ++i)
6666 clen += status_match_len(xp, L_MATCH(i)) + 2;
6667 if (first_match > 0)
6668 clen += 2;
6669 /* jumping right, put match at the left */
6670 if ((long)clen > Columns)
6671 {
6672 first_match = match;
6673 /* if showing the last match, we can add some on the left */
6674 clen = 2;
6675 for (i = match; i < num_matches; ++i)
6676 {
6677 clen += status_match_len(xp, L_MATCH(i)) + 2;
6678 if ((long)clen >= Columns)
6679 break;
6680 }
6681 if (i == num_matches)
6682 add_left = TRUE;
6683 }
6684 }
6685 if (add_left)
6686 while (first_match > 0)
6687 {
6688 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6689 if ((long)clen >= Columns)
6690 break;
6691 --first_match;
6692 }
6693
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006694 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696 if (first_match == 0)
6697 {
6698 *buf = NUL;
6699 len = 0;
6700 }
6701 else
6702 {
6703 STRCPY(buf, "< ");
6704 len = 2;
6705 }
6706 clen = len;
6707
6708 i = first_match;
6709 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6710 {
6711 if (i == match)
6712 {
6713 selstart = buf + len;
6714 selstart_col = clen;
6715 }
6716
6717 s = L_MATCH(i);
6718 /* Check for menu separators - replace with '|' */
6719#ifdef FEAT_MENU
6720 emenu = (xp->xp_context == EXPAND_MENUS
6721 || xp->xp_context == EXPAND_MENUNAMES);
6722 if (emenu && menu_is_separator(s))
6723 {
6724 STRCPY(buf + len, transchar('|'));
6725 l = (int)STRLEN(buf + len);
6726 len += l;
6727 clen += l;
6728 }
6729 else
6730#endif
6731 for ( ; *s != NUL; ++s)
6732 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006733 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 clen += ptr2cells(s);
6735#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006736 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 STRNCPY(buf + len, s, l);
6739 s += l - 1;
6740 len += l;
6741 }
6742 else
6743#endif
6744 {
6745 STRCPY(buf + len, transchar_byte(*s));
6746 len += (int)STRLEN(buf + len);
6747 }
6748 }
6749 if (i == match)
6750 selend = buf + len;
6751
6752 *(buf + len++) = ' ';
6753 *(buf + len++) = ' ';
6754 clen += 2;
6755 if (++i == num_matches)
6756 break;
6757 }
6758
6759 if (i != num_matches)
6760 {
6761 *(buf + len++) = '>';
6762 ++clen;
6763 }
6764
6765 buf[len] = NUL;
6766
6767 row = cmdline_row - 1;
6768 if (row >= 0)
6769 {
6770 if (wild_menu_showing == 0)
6771 {
6772 if (msg_scrolled > 0)
6773 {
6774 /* Put the wildmenu just above the command line. If there is
6775 * no room, scroll the screen one line up. */
6776 if (cmdline_row == Rows - 1)
6777 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006778 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 ++msg_scrolled;
6780 }
6781 else
6782 {
6783 ++cmdline_row;
6784 ++row;
6785 }
6786 wild_menu_showing = WM_SCROLLED;
6787 }
6788 else
6789 {
6790 /* Create status line if needed by setting 'laststatus' to 2.
6791 * Set 'winminheight' to zero to avoid that the window is
6792 * resized. */
6793 if (lastwin->w_status_height == 0)
6794 {
6795 save_p_ls = p_ls;
6796 save_p_wmh = p_wmh;
6797 p_ls = 2;
6798 p_wmh = 0;
6799 last_status(FALSE);
6800 }
6801 wild_menu_showing = WM_SHOWN;
6802 }
6803 }
6804
6805 screen_puts(buf, row, 0, attr);
6806 if (selstart != NULL && highlight)
6807 {
6808 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006809 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811
6812 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6813 }
6814
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006815#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 win_redraw_last_status(topframe);
6817#else
6818 lastwin->w_redr_status = TRUE;
6819#endif
6820 vim_free(buf);
6821}
6822#endif
6823
6824#if defined(FEAT_WINDOWS) || defined(PROTO)
6825/*
6826 * Redraw the status line of window wp.
6827 *
6828 * If inversion is possible we use it. Else '=' characters are used.
6829 */
6830 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006831win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 int row;
6834 char_u *p;
6835 int len;
6836 int fillchar;
6837 int attr;
6838 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006839 static int busy = FALSE;
6840
6841 /* It's possible to get here recursively when 'statusline' (indirectly)
6842 * invokes ":redrawstatus". Simply ignore the call then. */
6843 if (busy)
6844 return;
6845 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
6847 wp->w_redr_status = FALSE;
6848 if (wp->w_status_height == 0)
6849 {
6850 /* no status line, can only be last window */
6851 redraw_cmdline = TRUE;
6852 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006853 else if (!redrawing()
6854#ifdef FEAT_INS_EXPAND
6855 /* don't update status line when popup menu is visible and may be
6856 * drawn over it */
6857 || pum_visible()
6858#endif
6859 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 /* Don't redraw right now, do it later. */
6862 wp->w_redr_status = TRUE;
6863 }
6864#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006865 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
6867 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006868 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870#endif
6871 else
6872 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006873 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006875 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 p = NameBuff;
6877 len = (int)STRLEN(p);
6878
Bram Moolenaard85f2712017-07-28 21:51:57 +02006879 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880#ifdef FEAT_QUICKFIX
6881 || wp->w_p_pvw
6882#endif
6883 || bufIsChanged(wp->w_buffer)
6884 || wp->w_buffer->b_p_ro)
6885 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006886 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006888 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 len += (int)STRLEN(p + len);
6890 }
6891#ifdef FEAT_QUICKFIX
6892 if (wp->w_p_pvw)
6893 {
6894 STRCPY(p + len, _("[Preview]"));
6895 len += (int)STRLEN(p + len);
6896 }
6897#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006898 if (bufIsChanged(wp->w_buffer)
6899#ifdef FEAT_TERMINAL
6900 && !bt_terminal(wp->w_buffer)
6901#endif
6902 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
6904 STRCPY(p + len, "[+]");
6905 len += 3;
6906 }
6907 if (wp->w_buffer->b_p_ro)
6908 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006909 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006910 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6914 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6915 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6916 if (this_ru_col <= 1)
6917 {
6918 p = (char_u *)"<"; /* No room for file name! */
6919 len = 1;
6920 }
6921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922#ifdef FEAT_MBYTE
6923 if (has_mbyte)
6924 {
6925 int clen = 0, i;
6926
6927 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006928 clen = mb_string2cells(p, -1);
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /* Find first character that will fit.
6931 * Going from start to end is much faster for DBCS. */
6932 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006933 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 clen -= (*mb_ptr2cells)(p + i);
6935 len = clen;
6936 if (i > 0)
6937 {
6938 p = p + i - 1;
6939 *p = '<';
6940 ++len;
6941 }
6942
6943 }
6944 else
6945#endif
6946 if (len > this_ru_col - 1)
6947 {
6948 p += len - (this_ru_col - 1);
6949 *p = '<';
6950 len = this_ru_col - 1;
6951 }
6952
6953 row = W_WINROW(wp) + wp->w_height;
6954 screen_puts(p, row, W_WINCOL(wp), attr);
6955 screen_fill(row, row + 1, len + W_WINCOL(wp),
6956 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6957
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006958 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6960 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6961 - 1 + W_WINCOL(wp)), attr);
6962
6963#ifdef FEAT_CMDL_INFO
6964 win_redr_ruler(wp, TRUE);
6965#endif
6966 }
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 /*
6969 * May need to draw the character below the vertical separator.
6970 */
6971 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6972 {
6973 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006974 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 else
6976 fillchar = fillchar_vsep(&attr);
6977 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6978 attr);
6979 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006980 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981}
6982
Bram Moolenaar238a5642006-02-21 22:12:05 +00006983#ifdef FEAT_STL_OPT
6984/*
6985 * Redraw the status line according to 'statusline' and take care of any
6986 * errors encountered.
6987 */
6988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006989redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006990{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006992 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993
6994 /* When called recursively return. This can happen when the statusline
6995 * contains an expression that triggers a redraw. */
6996 if (entered)
6997 return;
6998 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006999
Bram Moolenaara742e082016-04-05 21:10:38 +02007000 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007001 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007002 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007003 {
7004 /* When there is an error disable the statusline, otherwise the
7005 * display is messed up with errors and a redraw triggers the problem
7006 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007 set_string_option_direct((char_u *)"statusline", -1,
7008 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007009 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007010 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007011 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007013}
7014#endif
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016/*
7017 * Return TRUE if the status line of window "wp" is connected to the status
7018 * line of the window right of it. If not, then it's a vertical separator.
7019 * Only call if (wp->w_vsep_width != 0).
7020 */
7021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007022stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 frame_T *fr;
7025
7026 fr = wp->w_frame;
7027 while (fr->fr_parent != NULL)
7028 {
7029 if (fr->fr_parent->fr_layout == FR_COL)
7030 {
7031 if (fr->fr_next != NULL)
7032 break;
7033 }
7034 else
7035 {
7036 if (fr->fr_next != NULL)
7037 return TRUE;
7038 }
7039 fr = fr->fr_parent;
7040 }
7041 return FALSE;
7042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
7044#endif /* FEAT_WINDOWS */
7045
7046#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
7047/*
7048 * Get the value to show for the language mappings, active 'keymap'.
7049 */
7050 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007051get_keymap_str(
7052 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007053 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007054 char_u *buf, /* buffer for the result */
7055 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 char_u *p;
7058
7059 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7060 return FALSE;
7061
7062 {
7063#ifdef FEAT_EVAL
7064 buf_T *old_curbuf = curbuf;
7065 win_T *old_curwin = curwin;
7066 char_u *s;
7067
7068 curbuf = wp->w_buffer;
7069 curwin = wp;
7070 STRCPY(buf, "b:keymap_name"); /* must be writable */
7071 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007072 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 --emsg_skip;
7074 curbuf = old_curbuf;
7075 curwin = old_curwin;
7076 if (p == NULL || *p == NUL)
7077#endif
7078 {
7079#ifdef FEAT_KEYMAP
7080 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7081 p = wp->w_buffer->b_p_keymap;
7082 else
7083#endif
7084 p = (char_u *)"lang";
7085 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007086 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 buf[0] = NUL;
7088#ifdef FEAT_EVAL
7089 vim_free(s);
7090#endif
7091 }
7092 return buf[0] != NUL;
7093}
7094#endif
7095
7096#if defined(FEAT_STL_OPT) || defined(PROTO)
7097/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007098 * Redraw the status line or ruler of window "wp".
7099 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 */
7101 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007102win_redr_custom(
7103 win_T *wp,
7104 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007106 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 int attr;
7108 int curattr;
7109 int row;
7110 int col = 0;
7111 int maxwidth;
7112 int width;
7113 int n;
7114 int len;
7115 int fillchar;
7116 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007117 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007119 struct stl_hlrec hltab[STL_MAX_ITEM];
7120 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007121 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007122 win_T *ewp;
7123 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
Bram Moolenaar1d633412013-12-11 15:52:01 +01007125 /* There is a tiny chance that this gets called recursively: When
7126 * redrawing a status line triggers redrawing the ruler or tabline.
7127 * Avoid trouble by not allowing recursion. */
7128 if (entered)
7129 return;
7130 entered = TRUE;
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007133 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007135 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007136 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007137 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007138 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007139 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140 maxwidth = Columns;
7141# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007142 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 else
7146 {
7147 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007148 fillchar = fillchar_status(&attr, wp);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007149 maxwidth = W_WIDTH(wp);
7150
7151 if (draw_ruler)
7152 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007156 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007157 if (*++stl == '-')
7158 stl++;
7159 if (atoi((char *)stl))
7160 while (VIM_ISDIGIT(*stl))
7161 stl++;
7162 if (*stl++ != '(')
7163 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007165#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007166 col = ru_col - (Columns - W_WIDTH(wp));
7167 if (col < (W_WIDTH(wp) + 1) / 2)
7168 col = (W_WIDTH(wp) + 1) / 2;
7169#else
7170 col = ru_col;
7171 if (col > (Columns + 1) / 2)
7172 col = (Columns + 1) / 2;
7173#endif
7174 maxwidth = W_WIDTH(wp) - col;
7175#ifdef FEAT_WINDOWS
7176 if (!wp->w_status_height)
7177#endif
7178 {
7179 row = Rows - 1;
7180 --maxwidth; /* writing in last column may cause scrolling */
7181 fillchar = ' ';
7182 attr = 0;
7183 }
7184
7185# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007186 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007187# endif
7188 }
7189 else
7190 {
7191 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007194 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007196 use_sandbox = was_set_insecurely((char_u *)"statusline",
7197 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198# endif
7199 }
7200
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007201#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 col += W_WINCOL(wp);
7203#endif
7204 }
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007207 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaar61452852011-02-01 18:01:11 +01007209 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7210 * the cursor away and back. */
7211 ewp = wp == NULL ? curwin : wp;
7212 p_crb_save = ewp->w_p_crb;
7213 ewp->w_p_crb = FALSE;
7214
Bram Moolenaar362f3562009-11-03 16:20:34 +00007215 /* Make a copy, because the statusline may include a function call that
7216 * might change the option value and free the memory. */
7217 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007218 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007219 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007220 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007221 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007222 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007224 /* Make all characters printable. */
7225 p = transstr(buf);
7226 if (p != NULL)
7227 {
7228 vim_strncpy(buf, p, sizeof(buf) - 1);
7229 vim_free(p);
7230 }
7231
7232 /* fill up with "fillchar" */
7233 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007234 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
7236#ifdef FEAT_MBYTE
7237 len += (*mb_char2bytes)(fillchar, buf + len);
7238#else
7239 buf[len++] = fillchar;
7240#endif
7241 ++width;
7242 }
7243 buf[len] = NUL;
7244
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007245 /*
7246 * Draw each snippet with the specified highlighting.
7247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 curattr = attr;
7249 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007252 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 screen_puts_len(p, len, row, col, curattr);
7254 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007255 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007257 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007259 else if (hltab[n].userhl < 0)
7260 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261#ifdef FEAT_WINDOWS
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007262# ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007263 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7264 && wp->w_status_height != 0)
7265 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007266 else if (wp != NULL && bt_terminal(wp->w_buffer)
7267 && wp->w_status_height != 0)
7268 curattr = highlight_stlterm[hltab[n].userhl - 1];
7269# endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007270 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007271 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272#endif
7273 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007274 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007277
7278 if (wp == NULL)
7279 {
7280 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7281 col = 0;
7282 len = 0;
7283 p = buf;
7284 fillchar = 0;
7285 for (n = 0; tabtab[n].start != NULL; n++)
7286 {
7287 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7288 while (col < len)
7289 TabPageIdxs[col++] = fillchar;
7290 p = tabtab[n].start;
7291 fillchar = tabtab[n].userhl;
7292 }
7293 while (col < Columns)
7294 TabPageIdxs[col++] = fillchar;
7295 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007296
7297theend:
7298 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299}
7300
7301#endif /* FEAT_STL_OPT */
7302
7303/*
7304 * Output a single character directly to the screen and update ScreenLines.
7305 */
7306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007307screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 char_u buf[MB_MAXBYTES + 1];
7310
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007311#ifdef FEAT_MBYTE
7312 if (has_mbyte)
7313 buf[(*mb_char2bytes)(c, buf)] = NUL;
7314 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007316 {
7317 buf[0] = c;
7318 buf[1] = NUL;
7319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 screen_puts(buf, row, col, attr);
7321}
7322
7323/*
7324 * Get a single character directly from ScreenLines into "bytes[]".
7325 * Also return its attribute in *attrp;
7326 */
7327 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007328screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 unsigned off;
7331
7332 /* safety check */
7333 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7334 {
7335 off = LineOffset[row] + col;
7336 *attrp = ScreenAttrs[off];
7337 bytes[0] = ScreenLines[off];
7338 bytes[1] = NUL;
7339
7340#ifdef FEAT_MBYTE
7341 if (enc_utf8 && ScreenLinesUC[off] != 0)
7342 bytes[utfc_char2bytes(off, bytes)] = NUL;
7343 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7344 {
7345 bytes[0] = ScreenLines[off];
7346 bytes[1] = ScreenLines2[off];
7347 bytes[2] = NUL;
7348 }
7349 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7350 {
7351 bytes[1] = ScreenLines[off + 1];
7352 bytes[2] = NUL;
7353 }
7354#endif
7355 }
7356}
7357
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007358#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007359static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007360
7361/*
7362 * Return TRUE if composing characters for screen posn "off" differs from
7363 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007364 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007365 */
7366 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007367screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007368{
7369 int i;
7370
7371 for (i = 0; i < Screen_mco; ++i)
7372 {
7373 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7374 return TRUE;
7375 if (u8cc[i] == 0)
7376 break;
7377 }
7378 return FALSE;
7379}
7380#endif
7381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382/*
7383 * Put string '*text' on the screen at position 'row' and 'col', with
7384 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7385 * Note: only outputs within one row, message is truncated at screen boundary!
7386 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7387 */
7388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007389screen_puts(
7390 char_u *text,
7391 int row,
7392 int col,
7393 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 screen_puts_len(text, -1, row, col, attr);
7396}
7397
7398/*
7399 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7400 * a NUL.
7401 */
7402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007403screen_puts_len(
7404 char_u *text,
7405 int textlen,
7406 int row,
7407 int col,
7408 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 unsigned off;
7411 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007412 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 int c;
7414#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007415 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 int mbyte_blen = 1;
7417 int mbyte_cells = 1;
7418 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007419 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int clear_next_cell = FALSE;
7421# ifdef FEAT_ARABIC
7422 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007423 int pc, nc, nc1;
7424 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425# endif
7426#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007427#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7428 int force_redraw_this;
7429 int force_redraw_next = FALSE;
7430#endif
7431 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
7433 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7434 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007435 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
Bram Moolenaarc236c162008-07-13 17:41:49 +00007437#ifdef FEAT_MBYTE
7438 /* When drawing over the right halve of a double-wide char clear out the
7439 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007440 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007441# ifdef FEAT_GUI
7442 && !gui.in_use
7443# endif
7444 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007445 {
7446 ScreenLines[off - 1] = ' ';
7447 ScreenAttrs[off - 1] = 0;
7448 if (enc_utf8)
7449 {
7450 ScreenLinesUC[off - 1] = 0;
7451 ScreenLinesC[0][off - 1] = 0;
7452 }
7453 /* redraw the previous cell, make it empty */
7454 screen_char(off - 1, row, col - 1);
7455 /* force the cell at "col" to be redrawn */
7456 force_redraw_next = TRUE;
7457 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007458#endif
7459
Bram Moolenaar367329b2007-08-30 11:53:22 +00007460#ifdef FEAT_MBYTE
7461 max_off = LineOffset[row] + screen_Columns;
7462#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007463 while (col < screen_Columns
7464 && (len < 0 || (int)(ptr - text) < len)
7465 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
7467 c = *ptr;
7468#ifdef FEAT_MBYTE
7469 /* check if this is the first byte of a multibyte */
7470 if (has_mbyte)
7471 {
7472 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007473 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007475 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7477 mbyte_cells = 1;
7478 else if (enc_dbcs != 0)
7479 mbyte_cells = mbyte_blen;
7480 else /* enc_utf8 */
7481 {
7482 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007483 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 (int)((text + len) - ptr));
7485 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007488# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 /* Non-BMP character: display as ? or fullwidth ?. */
7490 if (u8c >= 0x10000)
7491 {
7492 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7493 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007494 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497# ifdef FEAT_ARABIC
7498 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7499 {
7500 /* Do Arabic shaping. */
7501 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7502 {
7503 /* Past end of string to be displayed. */
7504 nc = NUL;
7505 nc1 = NUL;
7506 }
7507 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007508 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007509 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7510 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007511 nc1 = pcc[0];
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 pc = prev_c;
7514 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007515 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
7518 prev_c = u8c;
7519# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007520 if (col + mbyte_cells > screen_Columns)
7521 {
7522 /* Only 1 cell left, but character requires 2 cells:
7523 * display a '>' in the last column to avoid wrapping. */
7524 c = '>';
7525 mbyte_cells = 1;
7526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 }
7528 }
7529#endif
7530
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007531#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7532 force_redraw_this = force_redraw_next;
7533 force_redraw_next = FALSE;
7534#endif
7535
7536 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537#ifdef FEAT_MBYTE
7538 || (mbyte_cells == 2
7539 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7540 || (enc_dbcs == DBCS_JPNU
7541 && c == 0x8e
7542 && ScreenLines2[off] != ptr[1])
7543 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007544 && (ScreenLinesUC[off] !=
7545 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7546 || (ScreenLinesUC[off] != 0
7547 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548#endif
7549 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 || exmode_active;
7551
7552 if (need_redraw
7553#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7554 || force_redraw_this
7555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 )
7557 {
7558#if defined(FEAT_GUI) || defined(UNIX)
7559 /* The bold trick makes a single row of pixels appear in the next
7560 * character. When a bold character is removed, the next
7561 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007562 * and for some xterms. */
7563 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564# ifdef FEAT_GUI
7565 gui.in_use
7566# endif
7567# if defined(FEAT_GUI) && defined(UNIX)
7568 ||
7569# endif
7570# ifdef UNIX
7571 term_is_xterm
7572# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007573 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007575 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007577 if (n > HL_ALL)
7578 n = syn_attr2attr(n);
7579 if (n & HL_BOLD)
7580 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 }
7582#endif
7583#ifdef FEAT_MBYTE
7584 /* When at the end of the text and overwriting a two-cell
7585 * character with a one-cell character, need to clear the next
7586 * cell. Also when overwriting the left halve of a two-cell char
7587 * with the right halve of a two-cell char. Do this only once
7588 * (mb_off2cells() may return 2 on the right halve). */
7589 if (clear_next_cell)
7590 clear_next_cell = FALSE;
7591 else if (has_mbyte
7592 && (len < 0 ? ptr[mbyte_blen] == NUL
7593 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007594 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007596 && (*mb_off2cells)(off, max_off) == 1
7597 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 clear_next_cell = TRUE;
7599
7600 /* Make sure we never leave a second byte of a double-byte behind,
7601 * it confuses mb_off2cells(). */
7602 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007603 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007605 && (*mb_off2cells)(off, max_off) == 1
7606 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 ScreenLines[off + mbyte_blen] = 0;
7608#endif
7609 ScreenLines[off] = c;
7610 ScreenAttrs[off] = attr;
7611#ifdef FEAT_MBYTE
7612 if (enc_utf8)
7613 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007614 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 ScreenLinesUC[off] = 0;
7616 else
7617 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007618 int i;
7619
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007621 for (i = 0; i < Screen_mco; ++i)
7622 {
7623 ScreenLinesC[i][off] = u8cc[i];
7624 if (u8cc[i] == 0)
7625 break;
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628 if (mbyte_cells == 2)
7629 {
7630 ScreenLines[off + 1] = 0;
7631 ScreenAttrs[off + 1] = attr;
7632 }
7633 screen_char(off, row, col);
7634 }
7635 else if (mbyte_cells == 2)
7636 {
7637 ScreenLines[off + 1] = ptr[1];
7638 ScreenAttrs[off + 1] = attr;
7639 screen_char_2(off, row, col);
7640 }
7641 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7642 {
7643 ScreenLines2[off] = ptr[1];
7644 screen_char(off, row, col);
7645 }
7646 else
7647#endif
7648 screen_char(off, row, col);
7649 }
7650#ifdef FEAT_MBYTE
7651 if (has_mbyte)
7652 {
7653 off += mbyte_cells;
7654 col += mbyte_cells;
7655 ptr += mbyte_blen;
7656 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007657 {
7658 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007660 len = -1;
7661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 }
7663 else
7664#endif
7665 {
7666 ++off;
7667 ++col;
7668 ++ptr;
7669 }
7670 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007671
7672#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7673 /* If we detected the next character needs to be redrawn, but the text
7674 * doesn't extend up to there, update the character here. */
7675 if (force_redraw_next && col < screen_Columns)
7676 {
7677# ifdef FEAT_MBYTE
7678 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7679 screen_char_2(off, row, col);
7680 else
7681# endif
7682 screen_char(off, row, col);
7683 }
7684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687#ifdef FEAT_SEARCH_EXTRA
7688/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007689 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 */
7691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007692start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 if (p_hls && !no_hlsearch)
7695 {
7696 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007697 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007698# ifdef FEAT_RELTIME
7699 /* Set the time limit to 'redrawtime'. */
7700 profile_setlimit(p_rdt, &search_hl.tm);
7701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703}
7704
7705/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007706 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 */
7708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007709end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 if (search_hl.rm.regprog != NULL)
7712 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007713 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 search_hl.rm.regprog = NULL;
7715 }
7716}
7717
7718/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007719 * Init for calling prepare_search_hl().
7720 */
7721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007722init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007723{
7724 matchitem_T *cur;
7725
7726 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7727 * match */
7728 cur = wp->w_match_head;
7729 while (cur != NULL)
7730 {
7731 cur->hl.rm = cur->match;
7732 if (cur->hlg_id == 0)
7733 cur->hl.attr = 0;
7734 else
7735 cur->hl.attr = syn_id2attr(cur->hlg_id);
7736 cur->hl.buf = wp->w_buffer;
7737 cur->hl.lnum = 0;
7738 cur->hl.first_lnum = 0;
7739# ifdef FEAT_RELTIME
7740 /* Set the time limit to 'redrawtime'. */
7741 profile_setlimit(p_rdt, &(cur->hl.tm));
7742# endif
7743 cur = cur->next;
7744 }
7745 search_hl.buf = wp->w_buffer;
7746 search_hl.lnum = 0;
7747 search_hl.first_lnum = 0;
7748 /* time limit is set at the toplevel, for all windows */
7749}
7750
7751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 * Advance to the match in window "wp" line "lnum" or past it.
7753 */
7754 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007755prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007757 matchitem_T *cur; /* points to the match list */
7758 match_T *shl; /* points to search_hl or a match */
7759 int shl_flag; /* flag to indicate whether search_hl
7760 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007761 int pos_inprogress; /* marks that position match search is
7762 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 int n;
7764
7765 /*
7766 * When using a multi-line pattern, start searching at the top
7767 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007768 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007770 cur = wp->w_match_head;
7771 shl_flag = FALSE;
7772 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007774 if (shl_flag == FALSE)
7775 {
7776 shl = &search_hl;
7777 shl_flag = TRUE;
7778 }
7779 else
7780 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (shl->rm.regprog != NULL
7782 && shl->lnum == 0
7783 && re_multiline(shl->rm.regprog))
7784 {
7785 if (shl->first_lnum == 0)
7786 {
7787# ifdef FEAT_FOLDING
7788 for (shl->first_lnum = lnum;
7789 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7790 if (hasFoldingWin(wp, shl->first_lnum - 1,
7791 NULL, NULL, TRUE, NULL))
7792 break;
7793# else
7794 shl->first_lnum = wp->w_topline;
7795# endif
7796 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007797 if (cur != NULL)
7798 cur->pos.cur = 0;
7799 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007801 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7802 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007804 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7805 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806 pos_inprogress = cur == NULL || cur->pos.cur == 0
7807 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 if (shl->lnum != 0)
7809 {
7810 shl->first_lnum = shl->lnum
7811 + shl->rm.endpos[0].lnum
7812 - shl->rm.startpos[0].lnum;
7813 n = shl->rm.endpos[0].col;
7814 }
7815 else
7816 {
7817 ++shl->first_lnum;
7818 n = 0;
7819 }
7820 }
7821 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007822 if (shl != &search_hl && cur != NULL)
7823 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825}
7826
7827/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007828 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 * Uses shl->buf.
7830 * Sets shl->lnum and shl->rm contents.
7831 * Note: Assumes a previous match is always before "lnum", unless
7832 * shl->lnum is zero.
7833 * Careful: Any pointers for buffer lines will become invalid.
7834 */
7835 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007836next_search_hl(
7837 win_T *win,
7838 match_T *shl, /* points to search_hl or a match */
7839 linenr_T lnum,
7840 colnr_T mincol, /* minimal column for a match */
7841 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 linenr_T l;
7844 colnr_T matchcol;
7845 long nmatched;
7846
7847 if (shl->lnum != 0)
7848 {
7849 /* Check for three situations:
7850 * 1. If the "lnum" is below a previous match, start a new search.
7851 * 2. If the previous match includes "mincol", use it.
7852 * 3. Continue after the previous match.
7853 */
7854 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7855 if (lnum > l)
7856 shl->lnum = 0;
7857 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7858 return;
7859 }
7860
7861 /*
7862 * Repeat searching for a match until one is found that includes "mincol"
7863 * or none is found in this line.
7864 */
7865 called_emsg = FALSE;
7866 for (;;)
7867 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007868#ifdef FEAT_RELTIME
7869 /* Stop searching after passing the time limit. */
7870 if (profile_passed_limit(&(shl->tm)))
7871 {
7872 shl->lnum = 0; /* no match found in time */
7873 break;
7874 }
7875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 /* Three situations:
7877 * 1. No useful previous match: search from start of line.
7878 * 2. Not Vi compatible or empty match: continue at next character.
7879 * Break the loop if this is beyond the end of the line.
7880 * 3. Vi compatible searching: continue at end of previous match.
7881 */
7882 if (shl->lnum == 0)
7883 matchcol = 0;
7884 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7885 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007886 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007888 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007889
7890 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007891 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007892 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007894 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 shl->lnum = 0;
7896 break;
7897 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007898#ifdef FEAT_MBYTE
7899 if (has_mbyte)
7900 matchcol += mb_ptr2len(ml);
7901 else
7902#endif
7903 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 }
7905 else
7906 matchcol = shl->rm.endpos[0].col;
7907
7908 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007909 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007911 /* Remember whether shl->rm is using a copy of the regprog in
7912 * cur->match. */
7913 int regprog_is_copy = (shl != &search_hl && cur != NULL
7914 && shl == &cur->hl
7915 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007916 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007917
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7919 matchcol,
7920#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007921 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007923 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007924#endif
7925 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007926 /* Copy the regprog, in case it got freed and recompiled. */
7927 if (regprog_is_copy)
7928 cur->match.regprog = cur->hl.rm.regprog;
7929
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007930 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007931 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007932 /* Error while handling regexp: stop using this regexp. */
7933 if (shl == &search_hl)
7934 {
7935 /* don't free regprog in the match list, it's a copy */
7936 vim_regfree(shl->rm.regprog);
7937 SET_NO_HLSEARCH(TRUE);
7938 }
7939 shl->rm.regprog = NULL;
7940 shl->lnum = 0;
7941 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7942 message */
7943 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007944 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945 }
7946 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007947 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007948 else
7949 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (nmatched == 0)
7951 {
7952 shl->lnum = 0; /* no match found */
7953 break;
7954 }
7955 if (shl->rm.startpos[0].lnum > 0
7956 || shl->rm.startpos[0].col >= mincol
7957 || nmatched > 1
7958 || shl->rm.endpos[0].col > mincol)
7959 {
7960 shl->lnum += shl->rm.startpos[0].lnum;
7961 break; /* useful match found */
7962 }
7963 }
7964}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965
Bram Moolenaar85077472016-10-16 14:35:48 +02007966/*
7967 * If there is a match fill "shl" and return one.
7968 * Return zero otherwise.
7969 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007971next_search_hl_pos(
7972 match_T *shl, /* points to a match */
7973 linenr_T lnum,
7974 posmatch_T *posmatch, /* match positions */
7975 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976{
7977 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02007978 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979
Bram Moolenaarb3414592014-06-17 17:48:32 +02007980 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7981 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007982 llpos_T *pos = &posmatch->pos[i];
7983
7984 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02007986 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007987 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007988 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007989 {
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 /* if this match comes before the one at "found" then swap
7993 * them */
7994 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007995 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02007996 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997
Bram Moolenaar85077472016-10-16 14:35:48 +02007998 *pos = posmatch->pos[found];
7999 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 }
8001 }
8002 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008003 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 }
8005 }
8006 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008007 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008008 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008009 colnr_T start = posmatch->pos[found].col == 0
8010 ? 0 : posmatch->pos[found].col - 1;
8011 colnr_T end = posmatch->pos[found].col == 0
8012 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013
Bram Moolenaar85077472016-10-16 14:35:48 +02008014 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015 shl->rm.startpos[0].lnum = 0;
8016 shl->rm.startpos[0].col = start;
8017 shl->rm.endpos[0].lnum = 0;
8018 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008019 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008020 posmatch->cur = found + 1;
8021 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008022 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008023 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008025#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008028screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 attrentry_T *aep = NULL;
8031
8032 screen_attr = attr;
8033 if (full_screen
8034#ifdef WIN3264
8035 && termcap_active
8036#endif
8037 )
8038 {
8039#ifdef FEAT_GUI
8040 if (gui.in_use)
8041 {
8042 char buf[20];
8043
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008044 /* The GUI handles this internally. */
8045 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 OUT_STR(buf);
8047 }
8048 else
8049#endif
8050 {
8051 if (attr > HL_ALL) /* special HL attr. */
8052 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008053 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 aep = syn_cterm_attr2entry(attr);
8055 else
8056 aep = syn_term_attr2entry(attr);
8057 if (aep == NULL) /* did ":syntax clear" */
8058 attr = 0;
8059 else
8060 attr = aep->ae_attr;
8061 }
8062 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
8063 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008064 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008065#ifdef FEAT_TERMGUICOLORS
8066 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008067 (aep->ae_u.cterm.fg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008068#endif
8069 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008070#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008071 )
8072#endif
8073 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008074 /* If the Normal FG color has BOLD attribute and the new HL
8075 * has a FG color defined, clear BOLD. */
8076 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
8078 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008079 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
8080 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 out_str(T_US);
8082 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
8083 out_str(T_CZH);
8084 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
8085 out_str(T_MR);
8086
8087 /*
8088 * Output the color or start string after bold etc., in case the
8089 * bold etc. override the color setting.
8090 */
8091 if (aep != NULL)
8092 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008093#ifdef FEAT_TERMGUICOLORS
8094 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008096 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008097 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008098 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008099 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008104 if (t_colors > 1)
8105 {
8106 if (aep->ae_u.cterm.fg_color)
8107 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8108 if (aep->ae_u.cterm.bg_color)
8109 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8110 }
8111 else
8112 {
8113 if (aep->ae_u.term.start != NULL)
8114 out_str(aep->ae_u.term.start);
8115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 }
8118 }
8119 }
8120}
8121
8122 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008123screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 int do_ME = FALSE; /* output T_ME code */
8126
8127 if (screen_attr != 0
8128#ifdef WIN3264
8129 && termcap_active
8130#endif
8131 )
8132 {
8133#ifdef FEAT_GUI
8134 if (gui.in_use)
8135 {
8136 char buf[20];
8137
8138 /* use internal GUI code */
8139 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8140 OUT_STR(buf);
8141 }
8142 else
8143#endif
8144 {
8145 if (screen_attr > HL_ALL) /* special HL attr. */
8146 {
8147 attrentry_T *aep;
8148
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008149 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 /*
8152 * Assume that t_me restores the original colors!
8153 */
8154 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008155 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008156#ifdef FEAT_TERMGUICOLORS
8157 (p_tgc ?
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008158 (aep->ae_u.cterm.fg_rgb != INVALCOLOR
8159 || aep->ae_u.cterm.bg_rgb != INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008160#endif
8161 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008162#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008163 )
8164#endif
8165 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 do_ME = TRUE;
8167 }
8168 else
8169 {
8170 aep = syn_term_attr2entry(screen_attr);
8171 if (aep != NULL && aep->ae_u.term.stop != NULL)
8172 {
8173 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8174 do_ME = TRUE;
8175 else
8176 out_str(aep->ae_u.term.stop);
8177 }
8178 }
8179 if (aep == NULL) /* did ":syntax clear" */
8180 screen_attr = 0;
8181 else
8182 screen_attr = aep->ae_attr;
8183 }
8184
8185 /*
8186 * Often all ending-codes are equal to T_ME. Avoid outputting the
8187 * same sequence several times.
8188 */
8189 if (screen_attr & HL_STANDOUT)
8190 {
8191 if (STRCMP(T_SE, T_ME) == 0)
8192 do_ME = TRUE;
8193 else
8194 out_str(T_SE);
8195 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008196 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {
8198 if (STRCMP(T_UE, T_ME) == 0)
8199 do_ME = TRUE;
8200 else
8201 out_str(T_UE);
8202 }
8203 if (screen_attr & HL_ITALIC)
8204 {
8205 if (STRCMP(T_CZR, T_ME) == 0)
8206 do_ME = TRUE;
8207 else
8208 out_str(T_CZR);
8209 }
8210 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8211 out_str(T_ME);
8212
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008213#ifdef FEAT_TERMGUICOLORS
8214 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008216 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008217 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008218 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008219 term_bg_rgb_color(cterm_normal_bg_gui_color);
8220 }
8221 else
8222#endif
8223 {
8224 if (t_colors > 1)
8225 {
8226 /* set Normal cterm colors */
8227 if (cterm_normal_fg_color != 0)
8228 term_fg_color(cterm_normal_fg_color - 1);
8229 if (cterm_normal_bg_color != 0)
8230 term_bg_color(cterm_normal_bg_color - 1);
8231 if (cterm_normal_fg_bold)
8232 out_str(T_MD);
8233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235 }
8236 }
8237 screen_attr = 0;
8238}
8239
8240/*
8241 * Reset the colors for a cterm. Used when leaving Vim.
8242 * The machine specific code may override this again.
8243 */
8244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008245reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008250#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008251 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8252 || cterm_normal_bg_gui_color != INVALCOLOR)
8253 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008254#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
8258 out_str(T_OP);
8259 screen_attr = -1;
8260 }
8261 if (cterm_normal_fg_bold)
8262 {
8263 out_str(T_ME);
8264 screen_attr = -1;
8265 }
8266 }
8267}
8268
8269/*
8270 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8271 * using the attributes from ScreenAttrs["off"].
8272 */
8273 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008274screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275{
8276 int attr;
8277
8278 /* Check for illegal values, just in case (could happen just after
8279 * resizing). */
8280 if (row >= screen_Rows || col >= screen_Columns)
8281 return;
8282
Bram Moolenaar494838a2015-02-10 19:20:37 +01008283 /* Outputting a character in the last cell on the screen may scroll the
8284 * screen up. Only do it when the "xn" termcap property is set, otherwise
8285 * mark the character invalid (update it when scrolled up). */
8286 if (*T_XN == NUL
8287 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288#ifdef FEAT_RIGHTLEFT
8289 /* account for first command-line character in rightleft mode */
8290 && !cmdmsg_rl
8291#endif
8292 )
8293 {
8294 ScreenAttrs[off] = (sattr_T)-1;
8295 return;
8296 }
8297
8298 /*
8299 * Stop highlighting first, so it's easier to move the cursor.
8300 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008301#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (screen_char_attr != 0)
8303 attr = screen_char_attr;
8304 else
8305#endif
8306 attr = ScreenAttrs[off];
8307 if (screen_attr != attr)
8308 screen_stop_highlight();
8309
8310 windgoto(row, col);
8311
8312 if (screen_attr != attr)
8313 screen_start_highlight(attr);
8314
8315#ifdef FEAT_MBYTE
8316 if (enc_utf8 && ScreenLinesUC[off] != 0)
8317 {
8318 char_u buf[MB_MAXBYTES + 1];
8319
8320 /* Convert UTF-8 character to bytes and write it. */
8321
8322 buf[utfc_char2bytes(off, buf)] = NUL;
8323
8324 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008325 if (utf_ambiguous_width(ScreenLinesUC[off]))
8326 screen_cur_col = 9999;
8327 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 ++screen_cur_col;
8329 }
8330 else
8331#endif
8332 {
8333#ifdef FEAT_MBYTE
8334 out_flush_check();
8335#endif
8336 out_char(ScreenLines[off]);
8337#ifdef FEAT_MBYTE
8338 /* double-byte character in single-width cell */
8339 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8340 out_char(ScreenLines2[off]);
8341#endif
8342 }
8343
8344 screen_cur_col++;
8345}
8346
8347#ifdef FEAT_MBYTE
8348
8349/*
8350 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8351 * on the screen at position 'row' and 'col'.
8352 * The attributes of the first byte is used for all. This is required to
8353 * output the two bytes of a double-byte character with nothing in between.
8354 */
8355 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008356screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
8358 /* Check for illegal values (could be wrong when screen was resized). */
8359 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8360 return;
8361
8362 /* Outputting the last character on the screen may scrollup the screen.
8363 * Don't to it! Mark the character invalid (update it when scrolled up) */
8364 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8365 {
8366 ScreenAttrs[off] = (sattr_T)-1;
8367 return;
8368 }
8369
8370 /* Output the first byte normally (positions the cursor), then write the
8371 * second byte directly. */
8372 screen_char(off, row, col);
8373 out_char(ScreenLines[off + 1]);
8374 ++screen_cur_col;
8375}
8376#endif
8377
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008378#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379/*
8380 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8381 * This uses the contents of ScreenLines[] and doesn't change it.
8382 */
8383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008384screen_draw_rectangle(
8385 int row,
8386 int col,
8387 int height,
8388 int width,
8389 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 int r, c;
8392 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008393#ifdef FEAT_MBYTE
8394 int max_off;
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008397 /* Can't use ScreenLines unless initialized */
8398 if (ScreenLines == NULL)
8399 return;
8400
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 if (invert)
8402 screen_char_attr = HL_INVERSE;
8403 for (r = row; r < row + height; ++r)
8404 {
8405 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008406#ifdef FEAT_MBYTE
8407 max_off = off + screen_Columns;
8408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 for (c = col; c < col + width; ++c)
8410 {
8411#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008412 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {
8414 screen_char_2(off + c, r, c);
8415 ++c;
8416 }
8417 else
8418#endif
8419 {
8420 screen_char(off + c, r, c);
8421#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008422 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 ++c;
8424#endif
8425 }
8426 }
8427 }
8428 screen_char_attr = 0;
8429}
8430#endif
8431
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008432#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * Redraw the characters for a vertically split window.
8435 */
8436 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008437redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 int col;
8440 int width;
8441
8442# ifdef FEAT_CLIPBOARD
8443 clip_may_clear_selection(row, end - 1);
8444# endif
8445
8446 if (wp == NULL)
8447 {
8448 col = 0;
8449 width = Columns;
8450 }
8451 else
8452 {
8453 col = wp->w_wincol;
8454 width = wp->w_width;
8455 }
8456 screen_draw_rectangle(row, col, end - row, width, FALSE);
8457}
8458#endif
8459
8460/*
8461 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8462 * with character 'c1' in first column followed by 'c2' in the other columns.
8463 * Use attributes 'attr'.
8464 */
8465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008466screen_fill(
8467 int start_row,
8468 int end_row,
8469 int start_col,
8470 int end_col,
8471 int c1,
8472 int c2,
8473 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
8475 int row;
8476 int col;
8477 int off;
8478 int end_off;
8479 int did_delete;
8480 int c;
8481 int norm_term;
8482#if defined(FEAT_GUI) || defined(UNIX)
8483 int force_next = FALSE;
8484#endif
8485
8486 if (end_row > screen_Rows) /* safety check */
8487 end_row = screen_Rows;
8488 if (end_col > screen_Columns) /* safety check */
8489 end_col = screen_Columns;
8490 if (ScreenLines == NULL
8491 || start_row >= end_row
8492 || start_col >= end_col) /* nothing to do */
8493 return;
8494
8495 /* it's a "normal" terminal when not in a GUI or cterm */
8496 norm_term = (
8497#ifdef FEAT_GUI
8498 !gui.in_use &&
8499#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008500 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 for (row = start_row; row < end_row; ++row)
8502 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008503#ifdef FEAT_MBYTE
8504 if (has_mbyte
8505# ifdef FEAT_GUI
8506 && !gui.in_use
8507# endif
8508 )
8509 {
8510 /* When drawing over the right halve of a double-wide char clear
8511 * out the left halve. When drawing over the left halve of a
8512 * double wide-char clear out the right halve. Only needed in a
8513 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008514 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008515 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008516 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008517 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008518 }
8519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 /*
8521 * Try to use delete-line termcap code, when no attributes or in a
8522 * "normal" terminal, where a bold/italic space is just a
8523 * space.
8524 */
8525 did_delete = FALSE;
8526 if (c2 == ' '
8527 && end_col == Columns
8528 && can_clear(T_CE)
8529 && (attr == 0
8530 || (norm_term
8531 && attr <= HL_ALL
8532 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8533 {
8534 /*
8535 * check if we really need to clear something
8536 */
8537 col = start_col;
8538 if (c1 != ' ') /* don't clear first char */
8539 ++col;
8540
8541 off = LineOffset[row] + col;
8542 end_off = LineOffset[row] + end_col;
8543
8544 /* skip blanks (used often, keep it fast!) */
8545#ifdef FEAT_MBYTE
8546 if (enc_utf8)
8547 while (off < end_off && ScreenLines[off] == ' '
8548 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8549 ++off;
8550 else
8551#endif
8552 while (off < end_off && ScreenLines[off] == ' '
8553 && ScreenAttrs[off] == 0)
8554 ++off;
8555 if (off < end_off) /* something to be cleared */
8556 {
8557 col = off - LineOffset[row];
8558 screen_stop_highlight();
8559 term_windgoto(row, col);/* clear rest of this screen line */
8560 out_str(T_CE);
8561 screen_start(); /* don't know where cursor is now */
8562 col = end_col - col;
8563 while (col--) /* clear chars in ScreenLines */
8564 {
8565 ScreenLines[off] = ' ';
8566#ifdef FEAT_MBYTE
8567 if (enc_utf8)
8568 ScreenLinesUC[off] = 0;
8569#endif
8570 ScreenAttrs[off] = 0;
8571 ++off;
8572 }
8573 }
8574 did_delete = TRUE; /* the chars are cleared now */
8575 }
8576
8577 off = LineOffset[row] + start_col;
8578 c = c1;
8579 for (col = start_col; col < end_col; ++col)
8580 {
8581 if (ScreenLines[off] != c
8582#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008583 || (enc_utf8 && (int)ScreenLinesUC[off]
8584 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585#endif
8586 || ScreenAttrs[off] != attr
8587#if defined(FEAT_GUI) || defined(UNIX)
8588 || force_next
8589#endif
8590 )
8591 {
8592#if defined(FEAT_GUI) || defined(UNIX)
8593 /* The bold trick may make a single row of pixels appear in
8594 * the next character. When a bold character is removed, the
8595 * next character should be redrawn too. This happens for our
8596 * own GUI and for some xterms. */
8597 if (
8598# ifdef FEAT_GUI
8599 gui.in_use
8600# endif
8601# if defined(FEAT_GUI) && defined(UNIX)
8602 ||
8603# endif
8604# ifdef UNIX
8605 term_is_xterm
8606# endif
8607 )
8608 {
8609 if (ScreenLines[off] != ' '
8610 && (ScreenAttrs[off] > HL_ALL
8611 || ScreenAttrs[off] & HL_BOLD))
8612 force_next = TRUE;
8613 else
8614 force_next = FALSE;
8615 }
8616#endif
8617 ScreenLines[off] = c;
8618#ifdef FEAT_MBYTE
8619 if (enc_utf8)
8620 {
8621 if (c >= 0x80)
8622 {
8623 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008624 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626 else
8627 ScreenLinesUC[off] = 0;
8628 }
8629#endif
8630 ScreenAttrs[off] = attr;
8631 if (!did_delete || c != ' ')
8632 screen_char(off, row, col);
8633 }
8634 ++off;
8635 if (col == start_col)
8636 {
8637 if (did_delete)
8638 break;
8639 c = c2;
8640 }
8641 }
8642 if (end_col == Columns)
8643 LineWraps[row] = FALSE;
8644 if (row == Rows - 1) /* overwritten the command line */
8645 {
8646 redraw_cmdline = TRUE;
8647 if (c1 == ' ' && c2 == ' ')
8648 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008649 if (start_col == 0)
8650 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652 }
8653}
8654
8655/*
8656 * Check if there should be a delay. Used before clearing or redrawing the
8657 * screen or the command line.
8658 */
8659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008660check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8663 && !did_wait_return
8664 && emsg_silent == 0)
8665 {
8666 out_flush();
8667 ui_delay(1000L, TRUE);
8668 emsg_on_display = FALSE;
8669 if (check_msg_scroll)
8670 msg_scroll = FALSE;
8671 }
8672}
8673
8674/*
8675 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008676 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 * Returns TRUE if there is a valid screen to write to.
8678 * Returns FALSE when starting up and screen not initialized yet.
8679 */
8680 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008681screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008683 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 return (ScreenLines != NULL);
8685}
8686
8687/*
8688 * Resize the shell to Rows and Columns.
8689 * Allocate ScreenLines[] and associated items.
8690 *
8691 * There may be some time between setting Rows and Columns and (re)allocating
8692 * ScreenLines[]. This happens when starting up and when (manually) changing
8693 * the shell size. Always use screen_Rows and screen_Columns to access items
8694 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8695 * final size of the shell is needed.
8696 */
8697 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008698screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699{
8700 int new_row, old_row;
8701#ifdef FEAT_GUI
8702 int old_Rows;
8703#endif
8704 win_T *wp;
8705 int outofmem = FALSE;
8706 int len;
8707 schar_T *new_ScreenLines;
8708#ifdef FEAT_MBYTE
8709 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008710 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008712 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713#endif
8714 sattr_T *new_ScreenAttrs;
8715 unsigned *new_LineOffset;
8716 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008717#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008718 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008719 tabpage_T *tp;
8720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008722 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008723#ifdef FEAT_AUTOCMD
8724 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008726retry:
8727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 /*
8729 * Allocation of the screen buffers is done only when the size changes and
8730 * when Rows and Columns have been set and we have started doing full
8731 * screen stuff.
8732 */
8733 if ((ScreenLines != NULL
8734 && Rows == screen_Rows
8735 && Columns == screen_Columns
8736#ifdef FEAT_MBYTE
8737 && enc_utf8 == (ScreenLinesUC != NULL)
8738 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008739 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740#endif
8741 )
8742 || Rows == 0
8743 || Columns == 0
8744 || (!full_screen && ScreenLines == NULL))
8745 return;
8746
8747 /*
8748 * It's possible that we produce an out-of-memory message below, which
8749 * will cause this function to be called again. To break the loop, just
8750 * return here.
8751 */
8752 if (entered)
8753 return;
8754 entered = TRUE;
8755
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008756 /*
8757 * Note that the window sizes are updated before reallocating the arrays,
8758 * thus we must not redraw here!
8759 */
8760 ++RedrawingDisabled;
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 win_new_shellsize(); /* fit the windows in the new sized shell */
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 comp_col(); /* recompute columns for shown command and ruler */
8765
8766 /*
8767 * We're changing the size of the screen.
8768 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8769 * - Move lines from the old arrays into the new arrays, clear extra
8770 * lines (unless the screen is going to be cleared).
8771 * - Free the old arrays.
8772 *
8773 * If anything fails, make ScreenLines NULL, so we don't do anything!
8774 * Continuing with the old ScreenLines may result in a crash, because the
8775 * size is wrong.
8776 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008777 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008779#ifdef FEAT_AUTOCMD
8780 if (aucmd_win != NULL)
8781 win_free_lsize(aucmd_win);
8782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 new_ScreenLines = (schar_T *)lalloc((long_u)(
8785 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8786#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008787 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 if (enc_utf8)
8789 {
8790 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8791 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008792 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008793 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8795 }
8796 if (enc_dbcs == DBCS_JPNU)
8797 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8798 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8799#endif
8800 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8801 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8802 new_LineOffset = (unsigned *)lalloc((long_u)(
8803 Rows * sizeof(unsigned)), FALSE);
8804 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008805#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008806 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008809 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
8811 if (win_alloc_lines(wp) == FAIL)
8812 {
8813 outofmem = TRUE;
8814#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008815 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816#endif
8817 }
8818 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008819#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008820 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8821 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008822 outofmem = TRUE;
8823#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008824#ifdef FEAT_WINDOWS
8825give_up:
8826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008828#ifdef FEAT_MBYTE
8829 for (i = 0; i < p_mco; ++i)
8830 if (new_ScreenLinesC[i] == NULL)
8831 break;
8832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (new_ScreenLines == NULL
8834#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8837#endif
8838 || new_ScreenAttrs == NULL
8839 || new_LineOffset == NULL
8840 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008841#ifdef FEAT_WINDOWS
8842 || new_TabPageIdxs == NULL
8843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 || outofmem)
8845 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008846 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008847 {
8848 /* guess the size */
8849 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8850
8851 /* Remember we did this to avoid getting outofmem messages over
8852 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008853 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 vim_free(new_ScreenLines);
8856 new_ScreenLines = NULL;
8857#ifdef FEAT_MBYTE
8858 vim_free(new_ScreenLinesUC);
8859 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008860 for (i = 0; i < p_mco; ++i)
8861 {
8862 vim_free(new_ScreenLinesC[i]);
8863 new_ScreenLinesC[i] = NULL;
8864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 vim_free(new_ScreenLines2);
8866 new_ScreenLines2 = NULL;
8867#endif
8868 vim_free(new_ScreenAttrs);
8869 new_ScreenAttrs = NULL;
8870 vim_free(new_LineOffset);
8871 new_LineOffset = NULL;
8872 vim_free(new_LineWraps);
8873 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008874#ifdef FEAT_WINDOWS
8875 vim_free(new_TabPageIdxs);
8876 new_TabPageIdxs = NULL;
8877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879 else
8880 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008881 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 for (new_row = 0; new_row < Rows; ++new_row)
8884 {
8885 new_LineOffset[new_row] = new_row * Columns;
8886 new_LineWraps[new_row] = FALSE;
8887
8888 /*
8889 * If the screen is not going to be cleared, copy as much as
8890 * possible from the old screen to the new one and clear the rest
8891 * (used when resizing the window at the "--more--" prompt or when
8892 * executing an external command, for the GUI).
8893 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008894 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 (void)vim_memset(new_ScreenLines + new_row * Columns,
8897 ' ', (size_t)Columns * sizeof(schar_T));
8898#ifdef FEAT_MBYTE
8899 if (enc_utf8)
8900 {
8901 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8902 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008903 for (i = 0; i < p_mco; ++i)
8904 (void)vim_memset(new_ScreenLinesC[i]
8905 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 0, (size_t)Columns * sizeof(u8char_T));
8907 }
8908 if (enc_dbcs == DBCS_JPNU)
8909 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8910 0, (size_t)Columns * sizeof(schar_T));
8911#endif
8912 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8913 0, (size_t)Columns * sizeof(sattr_T));
8914 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008915 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 if (screen_Columns < Columns)
8918 len = screen_Columns;
8919 else
8920 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008921#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008922 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008923 * may be invalid now. Also when p_mco changes. */
8924 if (!(enc_utf8 && ScreenLinesUC == NULL)
8925 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008926#endif
8927 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8928 ScreenLines + LineOffset[old_row],
8929 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008931 if (enc_utf8 && ScreenLinesUC != NULL
8932 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 {
8934 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8935 ScreenLinesUC + LineOffset[old_row],
8936 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008937 for (i = 0; i < p_mco; ++i)
8938 mch_memmove(new_ScreenLinesC[i]
8939 + new_LineOffset[new_row],
8940 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 (size_t)len * sizeof(u8char_T));
8942 }
8943 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8944 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8945 ScreenLines2 + LineOffset[old_row],
8946 (size_t)len * sizeof(schar_T));
8947#endif
8948 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8949 ScreenAttrs + LineOffset[old_row],
8950 (size_t)len * sizeof(sattr_T));
8951 }
8952 }
8953 }
8954 /* Use the last line of the screen for the current line. */
8955 current_ScreenLine = new_ScreenLines + Rows * Columns;
8956 }
8957
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008958 free_screenlines();
8959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 ScreenLines = new_ScreenLines;
8961#ifdef FEAT_MBYTE
8962 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 for (i = 0; i < p_mco; ++i)
8964 ScreenLinesC[i] = new_ScreenLinesC[i];
8965 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 ScreenLines2 = new_ScreenLines2;
8967#endif
8968 ScreenAttrs = new_ScreenAttrs;
8969 LineOffset = new_LineOffset;
8970 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008971#ifdef FEAT_WINDOWS
8972 TabPageIdxs = new_TabPageIdxs;
8973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 /* It's important that screen_Rows and screen_Columns reflect the actual
8976 * size of ScreenLines[]. Set them before calling anything. */
8977#ifdef FEAT_GUI
8978 old_Rows = screen_Rows;
8979#endif
8980 screen_Rows = Rows;
8981 screen_Columns = Columns;
8982
8983 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008984 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 screenclear2();
8986
8987#ifdef FEAT_GUI
8988 else if (gui.in_use
8989 && !gui.starting
8990 && ScreenLines != NULL
8991 && old_Rows != Rows)
8992 {
8993 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8994 /*
8995 * Adjust the position of the cursor, for when executing an external
8996 * command.
8997 */
8998 if (msg_row >= Rows) /* Rows got smaller */
8999 msg_row = Rows - 1; /* put cursor at last row */
9000 else if (Rows > old_Rows) /* Rows got bigger */
9001 msg_row += Rows - old_Rows; /* put cursor in same place */
9002 if (msg_col >= Columns) /* Columns got smaller */
9003 msg_col = Columns - 1; /* put cursor at last column */
9004 }
9005#endif
9006
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009008 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009009
9010#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009011 /*
9012 * Do not apply autocommands more than 3 times to avoid an endless loop
9013 * in case applying autocommands always changes Rows or Columns.
9014 */
9015 if (starting == 0 && ++retry_count <= 3)
9016 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009017 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009018 /* In rare cases, autocommands may have altered Rows or Columns,
9019 * jump back to check if we need to allocate the screen again. */
9020 goto retry;
9021 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009026free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009027{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009028#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009029 int i;
9030
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009031 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009032 for (i = 0; i < Screen_mco; ++i)
9033 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009034 vim_free(ScreenLines2);
9035#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009036 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009037 vim_free(ScreenAttrs);
9038 vim_free(LineOffset);
9039 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009040#ifdef FEAT_WINDOWS
9041 vim_free(TabPageIdxs);
9042#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009043}
9044
9045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009046screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 check_for_delay(FALSE);
9049 screenalloc(FALSE); /* allocate screen buffers if size changed */
9050 screenclear2(); /* clear the screen */
9051}
9052
9053 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009054screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 int i;
9057
9058 if (starting == NO_SCREEN || ScreenLines == NULL
9059#ifdef FEAT_GUI
9060 || (gui.in_use && gui.starting)
9061#endif
9062 )
9063 return;
9064
9065#ifdef FEAT_GUI
9066 if (!gui.in_use)
9067#endif
9068 screen_attr = -1; /* force setting the Normal colors */
9069 screen_stop_highlight(); /* don't want highlighting here */
9070
9071#ifdef FEAT_CLIPBOARD
9072 /* disable selection without redrawing it */
9073 clip_scroll_selection(9999);
9074#endif
9075
9076 /* blank out ScreenLines */
9077 for (i = 0; i < Rows; ++i)
9078 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009079 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 LineWraps[i] = FALSE;
9081 }
9082
9083 if (can_clear(T_CL))
9084 {
9085 out_str(T_CL); /* clear the display */
9086 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009087 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
9089 else
9090 {
9091 /* can't clear the screen, mark all chars with invalid attributes */
9092 for (i = 0; i < Rows; ++i)
9093 lineinvalid(LineOffset[i], (int)Columns);
9094 clear_cmdline = TRUE;
9095 }
9096
9097 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9098
9099 win_rest_invalid(firstwin);
9100 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009101#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009102 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00009103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 if (must_redraw == CLEAR) /* no need to clear again */
9105 must_redraw = NOT_VALID;
9106 compute_cmdrow();
9107 msg_row = cmdline_row; /* put cursor on last line for messages */
9108 msg_col = 0;
9109 screen_start(); /* don't know where cursor is now */
9110 msg_scrolled = 0; /* can't scroll back */
9111 msg_didany = FALSE;
9112 msg_didout = FALSE;
9113}
9114
9115/*
9116 * Clear one line in ScreenLines.
9117 */
9118 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009119lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9122#ifdef FEAT_MBYTE
9123 if (enc_utf8)
9124 (void)vim_memset(ScreenLinesUC + off, 0,
9125 (size_t)width * sizeof(u8char_T));
9126#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009127 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
9130/*
9131 * Mark one line in ScreenLines invalid by setting the attributes to an
9132 * invalid value.
9133 */
9134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009135lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9138}
9139
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009140#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141/*
9142 * Copy part of a Screenline for vertically split window "wp".
9143 */
9144 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009145linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 unsigned off_to = LineOffset[to] + wp->w_wincol;
9148 unsigned off_from = LineOffset[from] + wp->w_wincol;
9149
9150 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9151 wp->w_width * sizeof(schar_T));
9152# ifdef FEAT_MBYTE
9153 if (enc_utf8)
9154 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009155 int i;
9156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9158 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009159 for (i = 0; i < p_mco; ++i)
9160 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9161 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 if (enc_dbcs == DBCS_JPNU)
9164 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9165 wp->w_width * sizeof(schar_T));
9166# endif
9167 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9168 wp->w_width * sizeof(sattr_T));
9169}
9170#endif
9171
9172/*
9173 * Return TRUE if clearing with term string "p" would work.
9174 * It can't work when the string is empty or it won't set the right background.
9175 */
9176 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009177can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 return (*p != NUL && (t_colors <= 1
9180#ifdef FEAT_GUI
9181 || gui.in_use
9182#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009183#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009184 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009185 || (!p_tgc && cterm_normal_bg_color == 0)
9186#else
9187 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009188#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009189 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
9193 * Reset cursor position. Use whenever cursor was moved because of outputting
9194 * something directly to the screen (shell commands) or a terminal control
9195 * code.
9196 */
9197 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009198screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 screen_cur_row = screen_cur_col = 9999;
9201}
9202
9203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 * Move the cursor to position "row","col" in the screen.
9205 * This tries to find the most efficient way to move, minimizing the number of
9206 * characters sent to the terminal.
9207 */
9208 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009209windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009211 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 int i;
9213 int plan;
9214 int cost;
9215 int wouldbe_col;
9216 int noinvcurs;
9217 char_u *bs;
9218 int goto_cost;
9219 int attr;
9220
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009221#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9223
9224#define PLAN_LE 1
9225#define PLAN_CR 2
9226#define PLAN_NL 3
9227#define PLAN_WRITE 4
9228 /* Can't use ScreenLines unless initialized */
9229 if (ScreenLines == NULL)
9230 return;
9231
9232 if (col != screen_cur_col || row != screen_cur_row)
9233 {
9234 /* Check for valid position. */
9235 if (row < 0) /* window without text lines? */
9236 row = 0;
9237 if (row >= screen_Rows)
9238 row = screen_Rows - 1;
9239 if (col >= screen_Columns)
9240 col = screen_Columns - 1;
9241
9242 /* check if no cursor movement is allowed in highlight mode */
9243 if (screen_attr && *T_MS == NUL)
9244 noinvcurs = HIGHL_COST;
9245 else
9246 noinvcurs = 0;
9247 goto_cost = GOTO_COST + noinvcurs;
9248
9249 /*
9250 * Plan how to do the positioning:
9251 * 1. Use CR to move it to column 0, same row.
9252 * 2. Use T_LE to move it a few columns to the left.
9253 * 3. Use NL to move a few lines down, column 0.
9254 * 4. Move a few columns to the right with T_ND or by writing chars.
9255 *
9256 * Don't do this if the cursor went beyond the last column, the cursor
9257 * position is unknown then (some terminals wrap, some don't )
9258 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009259 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 * characters to move the cursor to the right.
9261 */
9262 if (row >= screen_cur_row && screen_cur_col < Columns)
9263 {
9264 /*
9265 * If the cursor is in the same row, bigger col, we can use CR
9266 * or T_LE.
9267 */
9268 bs = NULL; /* init for GCC */
9269 attr = screen_attr;
9270 if (row == screen_cur_row && col < screen_cur_col)
9271 {
9272 /* "le" is preferred over "bc", because "bc" is obsolete */
9273 if (*T_LE)
9274 bs = T_LE; /* "cursor left" */
9275 else
9276 bs = T_BC; /* "backspace character (old) */
9277 if (*bs)
9278 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9279 else
9280 cost = 999;
9281 if (col + 1 < cost) /* using CR is less characters */
9282 {
9283 plan = PLAN_CR;
9284 wouldbe_col = 0;
9285 cost = 1; /* CR is just one character */
9286 }
9287 else
9288 {
9289 plan = PLAN_LE;
9290 wouldbe_col = col;
9291 }
9292 if (noinvcurs) /* will stop highlighting */
9293 {
9294 cost += noinvcurs;
9295 attr = 0;
9296 }
9297 }
9298
9299 /*
9300 * If the cursor is above where we want to be, we can use CR LF.
9301 */
9302 else if (row > screen_cur_row)
9303 {
9304 plan = PLAN_NL;
9305 wouldbe_col = 0;
9306 cost = (row - screen_cur_row) * 2; /* CR LF */
9307 if (noinvcurs) /* will stop highlighting */
9308 {
9309 cost += noinvcurs;
9310 attr = 0;
9311 }
9312 }
9313
9314 /*
9315 * If the cursor is in the same row, smaller col, just use write.
9316 */
9317 else
9318 {
9319 plan = PLAN_WRITE;
9320 wouldbe_col = screen_cur_col;
9321 cost = 0;
9322 }
9323
9324 /*
9325 * Check if any characters that need to be written have the
9326 * correct attributes. Also avoid UTF-8 characters.
9327 */
9328 i = col - wouldbe_col;
9329 if (i > 0)
9330 cost += i;
9331 if (cost < goto_cost && i > 0)
9332 {
9333 /*
9334 * Check if the attributes are correct without additionally
9335 * stopping highlighting.
9336 */
9337 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9338 while (i && *p++ == attr)
9339 --i;
9340 if (i != 0)
9341 {
9342 /*
9343 * Try if it works when highlighting is stopped here.
9344 */
9345 if (*--p == 0)
9346 {
9347 cost += noinvcurs;
9348 while (i && *p++ == 0)
9349 --i;
9350 }
9351 if (i != 0)
9352 cost = 999; /* different attributes, don't do it */
9353 }
9354#ifdef FEAT_MBYTE
9355 if (enc_utf8)
9356 {
9357 /* Don't use an UTF-8 char for positioning, it's slow. */
9358 for (i = wouldbe_col; i < col; ++i)
9359 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9360 {
9361 cost = 999;
9362 break;
9363 }
9364 }
9365#endif
9366 }
9367
9368 /*
9369 * We can do it without term_windgoto()!
9370 */
9371 if (cost < goto_cost)
9372 {
9373 if (plan == PLAN_LE)
9374 {
9375 if (noinvcurs)
9376 screen_stop_highlight();
9377 while (screen_cur_col > col)
9378 {
9379 out_str(bs);
9380 --screen_cur_col;
9381 }
9382 }
9383 else if (plan == PLAN_CR)
9384 {
9385 if (noinvcurs)
9386 screen_stop_highlight();
9387 out_char('\r');
9388 screen_cur_col = 0;
9389 }
9390 else if (plan == PLAN_NL)
9391 {
9392 if (noinvcurs)
9393 screen_stop_highlight();
9394 while (screen_cur_row < row)
9395 {
9396 out_char('\n');
9397 ++screen_cur_row;
9398 }
9399 screen_cur_col = 0;
9400 }
9401
9402 i = col - screen_cur_col;
9403 if (i > 0)
9404 {
9405 /*
9406 * Use cursor-right if it's one character only. Avoids
9407 * removing a line of pixels from the last bold char, when
9408 * using the bold trick in the GUI.
9409 */
9410 if (T_ND[0] != NUL && T_ND[1] == NUL)
9411 {
9412 while (i-- > 0)
9413 out_char(*T_ND);
9414 }
9415 else
9416 {
9417 int off;
9418
9419 off = LineOffset[row] + screen_cur_col;
9420 while (i-- > 0)
9421 {
9422 if (ScreenAttrs[off] != screen_attr)
9423 screen_stop_highlight();
9424#ifdef FEAT_MBYTE
9425 out_flush_check();
9426#endif
9427 out_char(ScreenLines[off]);
9428#ifdef FEAT_MBYTE
9429 if (enc_dbcs == DBCS_JPNU
9430 && ScreenLines[off] == 0x8e)
9431 out_char(ScreenLines2[off]);
9432#endif
9433 ++off;
9434 }
9435 }
9436 }
9437 }
9438 }
9439 else
9440 cost = 999;
9441
9442 if (cost >= goto_cost)
9443 {
9444 if (noinvcurs)
9445 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009446 if (row == screen_cur_row && (col > screen_cur_col)
9447 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 term_cursor_right(col - screen_cur_col);
9449 else
9450 term_windgoto(row, col);
9451 }
9452 screen_cur_row = row;
9453 screen_cur_col = col;
9454 }
9455}
9456
9457/*
9458 * Set cursor to its position in the current window.
9459 */
9460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009461setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
9463 if (redrawing())
9464 {
9465 validate_cursor();
9466 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9467 W_WINCOL(curwin) + (
9468#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009469 /* With 'rightleft' set and the cursor on a double-wide
9470 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9472# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009473 (has_mbyte
9474 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9475 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476# endif
9477 1)) :
9478#endif
9479 curwin->w_wcol));
9480 }
9481}
9482
9483
9484/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009485 * Insert 'line_count' lines at 'row' in window 'wp'.
9486 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9487 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * scrolling.
9489 * Returns FAIL if the lines are not inserted, OK for success.
9490 */
9491 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009492win_ins_lines(
9493 win_T *wp,
9494 int row,
9495 int line_count,
9496 int invalid,
9497 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 int did_delete;
9500 int nextrow;
9501 int lastrow;
9502 int retval;
9503
9504 if (invalid)
9505 wp->w_lines_valid = 0;
9506
9507 if (wp->w_height < 5)
9508 return FAIL;
9509
9510 if (line_count > wp->w_height - row)
9511 line_count = wp->w_height - row;
9512
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009513 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 if (retval != MAYBE)
9515 return retval;
9516
9517 /*
9518 * If there is a next window or a status line, we first try to delete the
9519 * lines at the bottom to avoid messing what is after the window.
9520 * If this fails and there are following windows, don't do anything to avoid
9521 * messing up those windows, better just redraw.
9522 */
9523 did_delete = FALSE;
9524#ifdef FEAT_WINDOWS
9525 if (wp->w_next != NULL || wp->w_status_height)
9526 {
9527 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009528 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 did_delete = TRUE;
9530 else if (wp->w_next)
9531 return FAIL;
9532 }
9533#endif
9534 /*
9535 * if no lines deleted, blank the lines that will end up below the window
9536 */
9537 if (!did_delete)
9538 {
9539#ifdef FEAT_WINDOWS
9540 wp->w_redr_status = TRUE;
9541#endif
9542 redraw_cmdline = TRUE;
9543 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9544 lastrow = nextrow + line_count;
9545 if (lastrow > Rows)
9546 lastrow = Rows;
9547 screen_fill(nextrow - line_count, lastrow - line_count,
9548 W_WINCOL(wp), (int)W_ENDCOL(wp),
9549 ' ', ' ', 0);
9550 }
9551
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009552 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 == FAIL)
9554 {
9555 /* deletion will have messed up other windows */
9556 if (did_delete)
9557 {
9558#ifdef FEAT_WINDOWS
9559 wp->w_redr_status = TRUE;
9560#endif
9561 win_rest_invalid(W_NEXT(wp));
9562 }
9563 return FAIL;
9564 }
9565
9566 return OK;
9567}
9568
9569/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009570 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9572 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9573 * scrolling
9574 * Return OK for success, FAIL if the lines are not deleted.
9575 */
9576 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009577win_del_lines(
9578 win_T *wp,
9579 int row,
9580 int line_count,
9581 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009582 int mayclear,
9583 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 int retval;
9586
9587 if (invalid)
9588 wp->w_lines_valid = 0;
9589
9590 if (line_count > wp->w_height - row)
9591 line_count = wp->w_height - row;
9592
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009593 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 if (retval != MAYBE)
9595 return retval;
9596
9597 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009598 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 return FAIL;
9600
9601#ifdef FEAT_WINDOWS
9602 /*
9603 * If there are windows or status lines below, try to put them at the
9604 * correct place. If we can't do that, they have to be redrawn.
9605 */
9606 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9607 {
9608 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009609 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 {
9611 wp->w_redr_status = TRUE;
9612 win_rest_invalid(wp->w_next);
9613 }
9614 }
9615 /*
9616 * If this is the last window and there is no status line, redraw the
9617 * command line later.
9618 */
9619 else
9620#endif
9621 redraw_cmdline = TRUE;
9622 return OK;
9623}
9624
9625/*
9626 * Common code for win_ins_lines() and win_del_lines().
9627 * Returns OK or FAIL when the work has been done.
9628 * Returns MAYBE when not finished yet.
9629 */
9630 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009631win_do_lines(
9632 win_T *wp,
9633 int row,
9634 int line_count,
9635 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009636 int del,
9637 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 int retval;
9640
9641 if (!redrawing() || line_count <= 0)
9642 return FAIL;
9643
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009644 /* When inserting lines would result in loss of command output, just redraw
9645 * the lines. */
9646 if (no_win_do_lines_ins && !del)
9647 return FAIL;
9648
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 /* only a few lines left: redraw is faster */
9650 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009651#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 && wp->w_width == Columns
9653#endif
9654 )
9655 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009656 if (!no_win_do_lines_ins)
9657 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 return FAIL;
9659 }
9660
9661 /*
9662 * Delete all remaining lines
9663 */
9664 if (row + line_count >= wp->w_height)
9665 {
9666 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9667 W_WINCOL(wp), (int)W_ENDCOL(wp),
9668 ' ', ' ', 0);
9669 return OK;
9670 }
9671
9672 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009673 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009675 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009677 if (!no_win_do_lines_ins)
9678 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679
9680 /*
9681 * If the terminal can set a scroll region, use that.
9682 * Always do this in a vertically split window. This will redraw from
9683 * ScreenLines[] when t_CV isn't defined. That's faster than using
9684 * win_line().
9685 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009686 * a character in the lower right corner of the scroll region may cause a
9687 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 */
9689 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009690#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 || W_WIDTH(wp) != Columns
9692#endif
9693 )
9694 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009695#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9697#endif
9698 scroll_region_set(wp, row);
9699 if (del)
9700 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009701 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 else
9703 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009704 wp->w_height - row, clear_attr, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009705#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9707#endif
9708 scroll_region_reset();
9709 return retval;
9710 }
9711
9712#ifdef FEAT_WINDOWS
9713 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9714 return FAIL;
9715#endif
9716
9717 return MAYBE;
9718}
9719
9720/*
9721 * window 'wp' and everything after it is messed up, mark it for redraw
9722 */
9723 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009724win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726#ifdef FEAT_WINDOWS
9727 while (wp != NULL)
9728#else
9729 if (wp != NULL)
9730#endif
9731 {
9732 redraw_win_later(wp, NOT_VALID);
9733#ifdef FEAT_WINDOWS
9734 wp->w_redr_status = TRUE;
9735 wp = wp->w_next;
9736#endif
9737 }
9738 redraw_cmdline = TRUE;
9739}
9740
9741/*
9742 * The rest of the routines in this file perform screen manipulations. The
9743 * given operation is performed physically on the screen. The corresponding
9744 * change is also made to the internal screen image. In this way, the editor
9745 * anticipates the effect of editing changes on the appearance of the screen.
9746 * That way, when we call screenupdate a complete redraw isn't usually
9747 * necessary. Another advantage is that we can keep adding code to anticipate
9748 * screen changes, and in the meantime, everything still works.
9749 */
9750
9751/*
9752 * types for inserting or deleting lines
9753 */
9754#define USE_T_CAL 1
9755#define USE_T_CDL 2
9756#define USE_T_AL 3
9757#define USE_T_CE 4
9758#define USE_T_DL 5
9759#define USE_T_SR 6
9760#define USE_NL 7
9761#define USE_T_CD 8
9762#define USE_REDRAW 9
9763
9764/*
9765 * insert lines on the screen and update ScreenLines[]
9766 * 'end' is the line after the scrolled part. Normally it is Rows.
9767 * When scrolling region used 'off' is the offset from the top for the region.
9768 * 'row' and 'end' are relative to the start of the region.
9769 *
9770 * return FAIL for failure, OK for success.
9771 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009772 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009773screen_ins_lines(
9774 int off,
9775 int row,
9776 int line_count,
9777 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009778 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009779 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781 int i;
9782 int j;
9783 unsigned temp;
9784 int cursor_row;
9785 int type;
9786 int result_empty;
9787 int can_ce = can_clear(T_CE);
9788
9789 /*
9790 * FAIL if
9791 * - there is no valid screen
9792 * - the screen has to be redrawn completely
9793 * - the line count is less than one
9794 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009795 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009797 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9798#ifdef FEAT_CLIPBOARD
9799 || (clip_star.state != SELECT_CLEARED
9800 && redrawing_for_callback > 0)
9801#endif
9802 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 return FAIL;
9804
9805 /*
9806 * There are seven ways to insert lines:
9807 * 0. When in a vertically split window and t_CV isn't set, redraw the
9808 * characters from ScreenLines[].
9809 * 1. Use T_CD (clear to end of display) if it exists and the result of
9810 * the insert is just empty lines
9811 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9812 * present or line_count > 1. It looks better if we do all the inserts
9813 * at once.
9814 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9815 * insert is just empty lines and T_CE is not present or line_count >
9816 * 1.
9817 * 4. Use T_AL (insert line) if it exists.
9818 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9819 * just empty lines.
9820 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9821 * just empty lines.
9822 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9823 * the 'da' flag is not set or we have clear line capability.
9824 * 8. redraw the characters from ScreenLines[].
9825 *
9826 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9827 * the scrollbar for the window. It does have insert line, use that if it
9828 * exists.
9829 */
9830 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009831#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9833 type = USE_REDRAW;
9834 else
9835#endif
9836 if (can_clear(T_CD) && result_empty)
9837 type = USE_T_CD;
9838 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9839 type = USE_T_CAL;
9840 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9841 type = USE_T_CDL;
9842 else if (*T_AL != NUL)
9843 type = USE_T_AL;
9844 else if (can_ce && result_empty)
9845 type = USE_T_CE;
9846 else if (*T_DL != NUL && result_empty)
9847 type = USE_T_DL;
9848 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9849 type = USE_T_SR;
9850 else
9851 return FAIL;
9852
9853 /*
9854 * For clearing the lines screen_del_lines() is used. This will also take
9855 * care of t_db if necessary.
9856 */
9857 if (type == USE_T_CD || type == USE_T_CDL ||
9858 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009859 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
9861 /*
9862 * If text is retained below the screen, first clear or delete as many
9863 * lines at the bottom of the window as are about to be inserted so that
9864 * the deleted lines won't later surface during a screen_del_lines.
9865 */
9866 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009867 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
9869#ifdef FEAT_CLIPBOARD
9870 /* Remove a modeless selection when inserting lines halfway the screen
9871 * or not the full width of the screen. */
9872 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009873# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 || (wp != NULL && wp->w_width != Columns)
9875# endif
9876 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009877 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 else
9879 clip_scroll_selection(-line_count);
9880#endif
9881
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882#ifdef FEAT_GUI
9883 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9884 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009885 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886#endif
9887
9888 if (*T_CCS != NUL) /* cursor relative to region */
9889 cursor_row = row;
9890 else
9891 cursor_row = row + off;
9892
9893 /*
9894 * Shift LineOffset[] line_count down to reflect the inserted lines.
9895 * Clear the inserted lines in ScreenLines[].
9896 */
9897 row += off;
9898 end += off;
9899 for (i = 0; i < line_count; ++i)
9900 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009901#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 if (wp != NULL && wp->w_width != Columns)
9903 {
9904 /* need to copy part of a line */
9905 j = end - 1 - i;
9906 while ((j -= line_count) >= row)
9907 linecopy(j + line_count, j, wp);
9908 j += line_count;
9909 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009910 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9911 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 else
9913 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9914 LineWraps[j] = FALSE;
9915 }
9916 else
9917#endif
9918 {
9919 j = end - 1 - i;
9920 temp = LineOffset[j];
9921 while ((j -= line_count) >= row)
9922 {
9923 LineOffset[j + line_count] = LineOffset[j];
9924 LineWraps[j + line_count] = LineWraps[j];
9925 }
9926 LineOffset[j + line_count] = temp;
9927 LineWraps[j + line_count] = FALSE;
9928 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009929 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 else
9931 lineinvalid(temp, (int)Columns);
9932 }
9933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 screen_stop_highlight();
9936 windgoto(cursor_row, 0);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009937 if (clear_attr != 0)
9938 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009940#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 /* redraw the characters */
9942 if (type == USE_REDRAW)
9943 redraw_block(row, end, wp);
9944 else
9945#endif
9946 if (type == USE_T_CAL)
9947 {
9948 term_append_lines(line_count);
9949 screen_start(); /* don't know where cursor is now */
9950 }
9951 else
9952 {
9953 for (i = 0; i < line_count; i++)
9954 {
9955 if (type == USE_T_AL)
9956 {
9957 if (i && cursor_row != 0)
9958 windgoto(cursor_row, 0);
9959 out_str(T_AL);
9960 }
9961 else /* type == USE_T_SR */
9962 out_str(T_SR);
9963 screen_start(); /* don't know where cursor is now */
9964 }
9965 }
9966
9967 /*
9968 * With scroll-reverse and 'da' flag set we need to clear the lines that
9969 * have been scrolled down into the region.
9970 */
9971 if (type == USE_T_SR && *T_DA)
9972 {
9973 for (i = 0; i < line_count; ++i)
9974 {
9975 windgoto(off + i, 0);
9976 out_str(T_CE);
9977 screen_start(); /* don't know where cursor is now */
9978 }
9979 }
9980
9981#ifdef FEAT_GUI
9982 gui_can_update_cursor();
9983 if (gui.in_use)
9984 out_flush(); /* always flush after a scroll */
9985#endif
9986 return OK;
9987}
9988
9989/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009990 * Delete lines on the screen and update ScreenLines[].
9991 * "end" is the line after the scrolled part. Normally it is Rows.
9992 * When scrolling region used "off" is the offset from the top for the region.
9993 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 *
9995 * Return OK for success, FAIL if the lines are not deleted.
9996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009998screen_del_lines(
9999 int off,
10000 int row,
10001 int line_count,
10002 int end,
10003 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010004 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010005 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006{
10007 int j;
10008 int i;
10009 unsigned temp;
10010 int cursor_row;
10011 int cursor_end;
10012 int result_empty; /* result is empty until end of region */
10013 int can_delete; /* deleting line codes can be used */
10014 int type;
10015
10016 /*
10017 * FAIL if
10018 * - there is no valid screen
10019 * - the screen has to be redrawn completely
10020 * - the line count is less than one
10021 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010022 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010024 if (!screen_valid(TRUE) || line_count <= 0
10025 || (!force && line_count > p_ttyscroll)
10026#ifdef FEAT_CLIPBOARD
10027 || (clip_star.state != SELECT_CLEARED
10028 && redrawing_for_callback > 0)
10029#endif
10030 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 return FAIL;
10032
10033 /*
10034 * Check if the rest of the current region will become empty.
10035 */
10036 result_empty = row + line_count >= end;
10037
10038 /*
10039 * We can delete lines only when 'db' flag not set or when 'ce' option
10040 * available.
10041 */
10042 can_delete = (*T_DB == NUL || can_clear(T_CE));
10043
10044 /*
10045 * There are six ways to delete lines:
10046 * 0. When in a vertically split window and t_CV isn't set, redraw the
10047 * characters from ScreenLines[].
10048 * 1. Use T_CD if it exists and the result is empty.
10049 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10050 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10051 * none of the other ways work.
10052 * 4. Use T_CE (erase line) if the result is empty.
10053 * 5. Use T_DL (delete line) if it exists.
10054 * 6. redraw the characters from ScreenLines[].
10055 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010056#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10058 type = USE_REDRAW;
10059 else
10060#endif
10061 if (can_clear(T_CD) && result_empty)
10062 type = USE_T_CD;
10063#if defined(__BEOS__) && defined(BEOS_DR8)
10064 /*
10065 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10066 * its internal termcap... this works okay for tests which test *T_DB !=
10067 * NUL. It has the disadvantage that the user cannot use any :set t_*
10068 * command to get T_DB (back) to empty_option, only :set term=... will do
10069 * the trick...
10070 * Anyway, this hack will hopefully go away with the next OS release.
10071 * (Olaf Seibert)
10072 */
10073 else if (row == 0 && T_DB == empty_option
10074 && (line_count == 1 || *T_CDL == NUL))
10075#else
10076 else if (row == 0 && (
10077#ifndef AMIGA
10078 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10079 * up, so use delete-line command */
10080 line_count == 1 ||
10081#endif
10082 *T_CDL == NUL))
10083#endif
10084 type = USE_NL;
10085 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10086 type = USE_T_CDL;
10087 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010088#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 && (wp == NULL || wp->w_width == Columns)
10090#endif
10091 )
10092 type = USE_T_CE;
10093 else if (*T_DL != NUL && can_delete)
10094 type = USE_T_DL;
10095 else if (*T_CDL != NUL && can_delete)
10096 type = USE_T_CDL;
10097 else
10098 return FAIL;
10099
10100#ifdef FEAT_CLIPBOARD
10101 /* Remove a modeless selection when deleting lines halfway the screen or
10102 * not the full width of the screen. */
10103 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010104# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 || (wp != NULL && wp->w_width != Columns)
10106# endif
10107 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010108 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
10110 clip_scroll_selection(line_count);
10111#endif
10112
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113#ifdef FEAT_GUI
10114 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10115 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010116 gui_dont_update_cursor(gui.cursor_row >= row + off
10117 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#endif
10119
10120 if (*T_CCS != NUL) /* cursor relative to region */
10121 {
10122 cursor_row = row;
10123 cursor_end = end;
10124 }
10125 else
10126 {
10127 cursor_row = row + off;
10128 cursor_end = end + off;
10129 }
10130
10131 /*
10132 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10133 * Clear the inserted lines in ScreenLines[].
10134 */
10135 row += off;
10136 end += off;
10137 for (i = 0; i < line_count; ++i)
10138 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010139#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (wp != NULL && wp->w_width != Columns)
10141 {
10142 /* need to copy part of a line */
10143 j = row + i;
10144 while ((j += line_count) <= end - 1)
10145 linecopy(j - line_count, j, wp);
10146 j -= line_count;
10147 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010148 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10149 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
10151 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10152 LineWraps[j] = FALSE;
10153 }
10154 else
10155#endif
10156 {
10157 /* whole width, moving the line pointers is faster */
10158 j = row + i;
10159 temp = LineOffset[j];
10160 while ((j += line_count) <= end - 1)
10161 {
10162 LineOffset[j - line_count] = LineOffset[j];
10163 LineWraps[j - line_count] = LineWraps[j];
10164 }
10165 LineOffset[j - line_count] = temp;
10166 LineWraps[j - line_count] = FALSE;
10167 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010168 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
10170 lineinvalid(temp, (int)Columns);
10171 }
10172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010174 if (screen_attr != clear_attr)
10175 screen_stop_highlight();
10176 if (clear_attr != 0)
10177 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010179#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 /* redraw the characters */
10181 if (type == USE_REDRAW)
10182 redraw_block(row, end, wp);
10183 else
10184#endif
10185 if (type == USE_T_CD) /* delete the lines */
10186 {
10187 windgoto(cursor_row, 0);
10188 out_str(T_CD);
10189 screen_start(); /* don't know where cursor is now */
10190 }
10191 else if (type == USE_T_CDL)
10192 {
10193 windgoto(cursor_row, 0);
10194 term_delete_lines(line_count);
10195 screen_start(); /* don't know where cursor is now */
10196 }
10197 /*
10198 * Deleting lines at top of the screen or scroll region: Just scroll
10199 * the whole screen (scroll region) up by outputting newlines on the
10200 * last line.
10201 */
10202 else if (type == USE_NL)
10203 {
10204 windgoto(cursor_end - 1, 0);
10205 for (i = line_count; --i >= 0; )
10206 out_char('\n'); /* cursor will remain on same line */
10207 }
10208 else
10209 {
10210 for (i = line_count; --i >= 0; )
10211 {
10212 if (type == USE_T_DL)
10213 {
10214 windgoto(cursor_row, 0);
10215 out_str(T_DL); /* delete a line */
10216 }
10217 else /* type == USE_T_CE */
10218 {
10219 windgoto(cursor_row + i, 0);
10220 out_str(T_CE); /* erase a line */
10221 }
10222 screen_start(); /* don't know where cursor is now */
10223 }
10224 }
10225
10226 /*
10227 * If the 'db' flag is set, we need to clear the lines that have been
10228 * scrolled up at the bottom of the region.
10229 */
10230 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10231 {
10232 for (i = line_count; i > 0; --i)
10233 {
10234 windgoto(cursor_end - i, 0);
10235 out_str(T_CE); /* erase a line */
10236 screen_start(); /* don't know where cursor is now */
10237 }
10238 }
10239
10240#ifdef FEAT_GUI
10241 gui_can_update_cursor();
10242 if (gui.in_use)
10243 out_flush(); /* always flush after a scroll */
10244#endif
10245
10246 return OK;
10247}
10248
10249/*
10250 * show the current mode and ruler
10251 *
10252 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10253 * If clear_cmdline is FALSE there may be a message there that needs to be
10254 * cleared only if a mode is shown.
10255 * Return the length of the message (0 if no message).
10256 */
10257 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010258showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 int need_clear;
10261 int length = 0;
10262 int do_mode;
10263 int attr;
10264 int nwr_save;
10265#ifdef FEAT_INS_EXPAND
10266 int sub_attr;
10267#endif
10268
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010269 do_mode = ((p_smd && msg_silent == 0)
10270 && ((State & INSERT)
10271 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010272 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 if (do_mode || Recording)
10274 {
10275 /*
10276 * Don't show mode right now, when not redrawing or inside a mapping.
10277 * Call char_avail() only when we are going to show something, because
10278 * it takes a bit of time.
10279 */
10280 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10281 {
10282 redraw_cmdline = TRUE; /* show mode later */
10283 return 0;
10284 }
10285
10286 nwr_save = need_wait_return;
10287
10288 /* wait a bit before overwriting an important message */
10289 check_for_delay(FALSE);
10290
10291 /* if the cmdline is more than one line high, erase top lines */
10292 need_clear = clear_cmdline;
10293 if (clear_cmdline && cmdline_row < Rows - 1)
10294 msg_clr_cmdline(); /* will reset clear_cmdline */
10295
10296 /* Position on the last line in the window, column 0 */
10297 msg_pos_mode();
10298 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010299 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 if (do_mode)
10301 {
10302 MSG_PUTS_ATTR("--", attr);
10303#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010304 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010305# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010306 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010307# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010308 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010309# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010310 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010311# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 MSG_PUTS_ATTR(" IM", attr);
10313# else
10314 MSG_PUTS_ATTR(" XIM", attr);
10315# endif
10316#endif
10317#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10318 if (gui.in_use)
10319 {
10320 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010321 {
10322 /* HANGUL */
10323 if (enc_utf8)
10324 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10325 else
10326 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 }
10329#endif
10330#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010331 /* CTRL-X in Insert mode */
10332 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 {
10334 /* These messages can get long, avoid a wrap in a narrow
10335 * window. Prefer showing edit_submode_extra. */
10336 length = (Rows - msg_row) * Columns - 3;
10337 if (edit_submode_extra != NULL)
10338 length -= vim_strsize(edit_submode_extra);
10339 if (length > 0)
10340 {
10341 if (edit_submode_pre != NULL)
10342 length -= vim_strsize(edit_submode_pre);
10343 if (length - vim_strsize(edit_submode) > 0)
10344 {
10345 if (edit_submode_pre != NULL)
10346 msg_puts_attr(edit_submode_pre, attr);
10347 msg_puts_attr(edit_submode, attr);
10348 }
10349 if (edit_submode_extra != NULL)
10350 {
10351 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10352 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010353 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 else
10355 sub_attr = attr;
10356 msg_puts_attr(edit_submode_extra, sub_attr);
10357 }
10358 }
10359 length = 0;
10360 }
10361 else
10362#endif
10363 {
10364#ifdef FEAT_VREPLACE
10365 if (State & VREPLACE_FLAG)
10366 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10367 else
10368#endif
10369 if (State & REPLACE_FLAG)
10370 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10371 else if (State & INSERT)
10372 {
10373#ifdef FEAT_RIGHTLEFT
10374 if (p_ri)
10375 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10376#endif
10377 MSG_PUTS_ATTR(_(" INSERT"), attr);
10378 }
10379 else if (restart_edit == 'I')
10380 MSG_PUTS_ATTR(_(" (insert)"), attr);
10381 else if (restart_edit == 'R')
10382 MSG_PUTS_ATTR(_(" (replace)"), attr);
10383 else if (restart_edit == 'V')
10384 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10385#ifdef FEAT_RIGHTLEFT
10386 if (p_hkmap)
10387 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10388# ifdef FEAT_FKMAP
10389 if (p_fkmap)
10390 MSG_PUTS_ATTR(farsi_text_5, attr);
10391# endif
10392#endif
10393#ifdef FEAT_KEYMAP
10394 if (State & LANGMAP)
10395 {
10396# ifdef FEAT_ARABIC
10397 if (curwin->w_p_arab)
10398 MSG_PUTS_ATTR(_(" Arabic"), attr);
10399 else
10400# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010401 if (get_keymap_str(curwin, (char_u *)" (%s)",
10402 NameBuff, MAXPATHL))
10403 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
10405#endif
10406 if ((State & INSERT) && p_paste)
10407 MSG_PUTS_ATTR(_(" (paste)"), attr);
10408
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 if (VIsual_active)
10410 {
10411 char *p;
10412
10413 /* Don't concatenate separate words to avoid translation
10414 * problems. */
10415 switch ((VIsual_select ? 4 : 0)
10416 + (VIsual_mode == Ctrl_V) * 2
10417 + (VIsual_mode == 'V'))
10418 {
10419 case 0: p = N_(" VISUAL"); break;
10420 case 1: p = N_(" VISUAL LINE"); break;
10421 case 2: p = N_(" VISUAL BLOCK"); break;
10422 case 4: p = N_(" SELECT"); break;
10423 case 5: p = N_(" SELECT LINE"); break;
10424 default: p = N_(" SELECT BLOCK"); break;
10425 }
10426 MSG_PUTS_ATTR(_(p), attr);
10427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 MSG_PUTS_ATTR(" --", attr);
10429 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010430
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 need_clear = TRUE;
10432 }
10433 if (Recording
10434#ifdef FEAT_INS_EXPAND
10435 && edit_submode == NULL /* otherwise it gets too long */
10436#endif
10437 )
10438 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010439 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 need_clear = TRUE;
10441 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010442
10443 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 if (need_clear || clear_cmdline)
10445 msg_clr_eos();
10446 msg_didout = FALSE; /* overwrite this message */
10447 length = msg_col;
10448 msg_col = 0;
10449 need_wait_return = nwr_save; /* never ask for hit-return for this */
10450 }
10451 else if (clear_cmdline && msg_silent == 0)
10452 /* Clear the whole command line. Will reset "clear_cmdline". */
10453 msg_clr_cmdline();
10454
10455#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 /* In Visual mode the size of the selected area must be redrawn. */
10457 if (VIsual_active)
10458 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460 /* If the last window has no status line, the ruler is after the mode
10461 * message and must be redrawn */
10462 if (redrawing()
10463# ifdef FEAT_WINDOWS
10464 && lastwin->w_status_height == 0
10465# endif
10466 )
10467 win_redr_ruler(lastwin, TRUE);
10468#endif
10469 redraw_cmdline = FALSE;
10470 clear_cmdline = FALSE;
10471
10472 return length;
10473}
10474
10475/*
10476 * Position for a mode message.
10477 */
10478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010479msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480{
10481 msg_col = 0;
10482 msg_row = Rows - 1;
10483}
10484
10485/*
10486 * Delete mode message. Used when ESC is typed which is expected to end
10487 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010488 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 */
10490 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010491unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010494 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 */
10496 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10497 redraw_cmdline = TRUE; /* delete mode later */
10498 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010499 clearmode();
10500}
10501
10502/*
10503 * Clear the mode message.
10504 */
10505 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010506clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010507{
10508 msg_pos_mode();
10509 if (Recording)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010510 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010511 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512}
10513
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010514 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010515recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010516{
10517 MSG_PUTS_ATTR(_("recording"), attr);
10518 if (!shortmess(SHM_RECORDING))
10519 {
10520 char_u s[4];
10521 sprintf((char *)s, " @%c", Recording);
10522 MSG_PUTS_ATTR(s, attr);
10523 }
10524}
10525
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010526#if defined(FEAT_WINDOWS)
10527/*
10528 * Draw the tab pages line at the top of the Vim window.
10529 */
10530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010531draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010532{
10533 int tabcount = 0;
10534 tabpage_T *tp;
10535 int tabwidth;
10536 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010537 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010538 int attr;
10539 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010540 win_T *cwp;
10541 int wincount;
10542 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010543 int c;
10544 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010545 int attr_sel = HL_ATTR(HLF_TPS);
10546 int attr_nosel = HL_ATTR(HLF_TP);
10547 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010548 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010549 int room;
10550 int use_sep_chars = (t_colors < 8
10551#ifdef FEAT_GUI
10552 && !gui.in_use
10553#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010554#ifdef FEAT_TERMGUICOLORS
10555 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010556#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010557 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010558
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010559 if (ScreenLines == NULL)
10560 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010561 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010562
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010563#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010564 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010565 if (gui_use_tabline())
10566 {
10567 gui_update_tabline();
10568 return;
10569 }
10570#endif
10571
10572 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010573 return;
10574
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010575#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010576
10577 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10578 for (scol = 0; scol < Columns; ++scol)
10579 TabPageIdxs[scol] = 0;
10580
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010581 /* Use the 'tabline' option if it's set. */
10582 if (*p_tal != NUL)
10583 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010584 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010585
10586 /* Check for an error. If there is one we would loop in redrawing the
10587 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010588 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010589 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010590 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010591 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010592 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010593 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010594 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010596#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010597 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010598 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010599 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010600
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10602 if (tabwidth < 6)
10603 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010604
Bram Moolenaar238a5642006-02-21 22:12:05 +000010605 attr = attr_nosel;
10606 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010607 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010608 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10609 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010610 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010611 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010612
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 if (tp->tp_topframe == topframe)
10614 attr = attr_sel;
10615 if (use_sep_chars && col > 0)
10616 screen_putchar('|', 0, col++, attr);
10617
10618 if (tp->tp_topframe != topframe)
10619 attr = attr_nosel;
10620
10621 screen_putchar(' ', 0, col++, attr);
10622
10623 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010624 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 cwp = curwin;
10626 wp = firstwin;
10627 }
10628 else
10629 {
10630 cwp = tp->tp_curwin;
10631 wp = tp->tp_firstwin;
10632 }
10633
10634 modified = FALSE;
10635 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10636 if (bufIsChanged(wp->w_buffer))
10637 modified = TRUE;
10638 if (modified || wincount > 1)
10639 {
10640 if (wincount > 1)
10641 {
10642 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010643 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010644 if (col + len >= Columns - 3)
10645 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010647#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010648 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010649#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010650 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010651#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010652 );
10653 col += len;
10654 }
10655 if (modified)
10656 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10657 screen_putchar(' ', 0, col++, attr);
10658 }
10659
10660 room = scol - col + tabwidth - 1;
10661 if (room > 0)
10662 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010663 /* Get buffer name in NameBuff[] */
10664 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010665 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010666 len = vim_strsize(NameBuff);
10667 p = NameBuff;
10668#ifdef FEAT_MBYTE
10669 if (has_mbyte)
10670 while (len > room)
10671 {
10672 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010673 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 }
10675 else
10676#endif
10677 if (len > room)
10678 {
10679 p += len - room;
10680 len = room;
10681 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010682 if (len > Columns - col - 1)
10683 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010685 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010686 col += len;
10687 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010688 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689
10690 /* Store the tab page number in TabPageIdxs[], so that
10691 * jump_to_mouse() knows where each one is. */
10692 ++tabcount;
10693 while (scol < col)
10694 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010695 }
10696
Bram Moolenaar238a5642006-02-21 22:12:05 +000010697 if (use_sep_chars)
10698 c = '_';
10699 else
10700 c = ' ';
10701 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010702
10703 /* Put an "X" for closing the current tab if there are several. */
10704 if (first_tabpage->tp_next != NULL)
10705 {
10706 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10707 TabPageIdxs[Columns - 1] = -999;
10708 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010709 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010710
10711 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10712 * set. */
10713 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010714}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010715
10716/*
10717 * Get buffer name for "buf" into NameBuff[].
10718 * Takes care of special buffer names and translates special characters.
10719 */
10720 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010721get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010722{
10723 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010724 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010725 else
10726 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10727 trans_characters(NameBuff, MAXPATHL);
10728}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010729#endif
10730
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10732/*
10733 * Get the character to use in a status line. Get its attributes in "*attr".
10734 */
10735 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010736fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010739
10740#ifdef FEAT_TERMINAL
10741 if (bt_terminal(wp->w_buffer))
10742 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010743 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010744 {
10745 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010746 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010747 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010748 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010749 {
10750 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010751 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010752 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010753 }
10754 else
10755#endif
10756 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010758 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 fill = fill_stl;
10760 }
10761 else
10762 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010763 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 fill = fill_stlnc;
10765 }
10766 /* Use fill when there is highlighting, and highlighting of current
10767 * window differs, or the fillchars differ, or this is not the
10768 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010769 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 || (fill_stl != fill_stlnc)))
10772 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 return '^';
10775 return '=';
10776}
10777#endif
10778
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010779#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780/*
10781 * Get the character to use in a separator between vertically split windows.
10782 * Get its attributes in "*attr".
10783 */
10784 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010785fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010787 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 if (*attr == 0 && fill_vert == ' ')
10789 return '|';
10790 else
10791 return fill_vert;
10792}
10793#endif
10794
10795/*
10796 * Return TRUE if redrawing should currently be done.
10797 */
10798 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010799redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010801#ifdef FEAT_EVAL
10802 if (disable_redraw_for_testing)
10803 return 0;
10804 else
10805#endif
10806 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10808}
10809
10810/*
10811 * Return TRUE if printing messages should currently be done.
10812 */
10813 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010814messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815{
10816 return (!(p_lz && char_avail() && !KeyTyped));
10817}
10818
10819/*
10820 * Show current status info in ruler and various other places
10821 * If always is FALSE, only show ruler if position has changed.
10822 */
10823 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010824showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 if (!always && !redrawing())
10827 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010828#ifdef FEAT_INS_EXPAND
10829 if (pum_visible())
10830 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010831# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010832 /* Don't redraw right now, do it later. */
10833 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010834# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010835 return;
10836 }
10837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010839 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010840 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010841 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 else
10844#endif
10845#ifdef FEAT_CMDL_INFO
10846 win_redr_ruler(curwin, always);
10847#endif
10848
10849#ifdef FEAT_TITLE
10850 if (need_maketitle
10851# ifdef FEAT_STL_OPT
10852 || (p_icon && (stl_syntax & STL_IN_ICON))
10853 || (p_title && (stl_syntax & STL_IN_TITLE))
10854# endif
10855 )
10856 maketitle();
10857#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010858#ifdef FEAT_WINDOWS
10859 /* Redraw the tab pages line if needed. */
10860 if (redraw_tabline)
10861 draw_tabline();
10862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863}
10864
10865#ifdef FEAT_CMDL_INFO
10866 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010867win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010869#define RULER_BUF_LEN 70
10870 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 int row;
10872 int fillchar;
10873 int attr;
10874 int empty_line = FALSE;
10875 colnr_T virtcol;
10876 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010877 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010879#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 int this_ru_col;
10881 int off = 0;
10882 int width = Columns;
10883# define WITH_OFF(x) x
10884# define WITH_WIDTH(x) x
10885#else
10886# define WITH_OFF(x) 0
10887# define WITH_WIDTH(x) Columns
10888# define this_ru_col ru_col
10889#endif
10890
10891 /* If 'ruler' off or redrawing disabled, don't do anything */
10892 if (!p_ru)
10893 return;
10894
10895 /*
10896 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10897 * after deleting lines, before cursor.lnum is corrected.
10898 */
10899 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10900 return;
10901
10902#ifdef FEAT_INS_EXPAND
10903 /* Don't draw the ruler while doing insert-completion, it might overwrite
10904 * the (long) mode message. */
10905# ifdef FEAT_WINDOWS
10906 if (wp == lastwin && lastwin->w_status_height == 0)
10907# endif
10908 if (edit_submode != NULL)
10909 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010910 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10911 if (pum_visible())
10912 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913#endif
10914
10915#ifdef FEAT_STL_OPT
10916 if (*p_ruf)
10917 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010918 int save_called_emsg = called_emsg;
10919
10920 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010922 if (called_emsg)
10923 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010924 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010925 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 return;
10927 }
10928#endif
10929
10930 /*
10931 * Check if not in Insert mode and the line is empty (will show "0-1").
10932 */
10933 if (!(State & INSERT)
10934 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10935 empty_line = TRUE;
10936
10937 /*
10938 * Only draw the ruler when something changed.
10939 */
10940 validate_virtcol_win(wp);
10941 if ( redraw_cmdline
10942 || always
10943 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10944 || wp->w_cursor.col != wp->w_ru_cursor.col
10945 || wp->w_virtcol != wp->w_ru_virtcol
10946#ifdef FEAT_VIRTUALEDIT
10947 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10948#endif
10949 || wp->w_topline != wp->w_ru_topline
10950 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10951#ifdef FEAT_DIFF
10952 || wp->w_topfill != wp->w_ru_topfill
10953#endif
10954 || empty_line != wp->w_ru_empty)
10955 {
10956 cursor_off();
10957#ifdef FEAT_WINDOWS
10958 if (wp->w_status_height)
10959 {
10960 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010961 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 off = W_WINCOL(wp);
10963 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 }
10965 else
10966#endif
10967 {
10968 row = Rows - 1;
10969 fillchar = ' ';
10970 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010971#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 width = Columns;
10973 off = 0;
10974#endif
10975 }
10976
10977 /* In list mode virtcol needs to be recomputed */
10978 virtcol = wp->w_virtcol;
10979 if (wp->w_p_list && lcs_tab1 == NUL)
10980 {
10981 wp->w_p_list = FALSE;
10982 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10983 wp->w_p_list = TRUE;
10984 }
10985
10986 /*
10987 * Some sprintfs return the length, some return a pointer.
10988 * To avoid portability problems we use strlen() here.
10989 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010990 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10992 ? 0L
10993 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010994 len = STRLEN(buffer);
10995 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10997 (int)virtcol + 1);
10998
10999 /*
11000 * Add a "50%" if there is room for it.
11001 * On the last line, don't print in the last column (scrolls the
11002 * screen up on some terminals).
11003 */
11004 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011005 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 o = i + vim_strsize(buffer + i + 1);
11007#ifdef FEAT_WINDOWS
11008 if (wp->w_status_height == 0) /* can't use last char of screen */
11009#endif
11010 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010011011#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 this_ru_col = ru_col - (Columns - width);
11013 if (this_ru_col < 0)
11014 this_ru_col = 0;
11015#endif
11016 /* Never use more than half the window/screen width, leave the other
11017 * half for the filename. */
11018 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
11019 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
11020 if (this_ru_col + o < WITH_WIDTH(width))
11021 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011022 /* need at least 3 chars left for get_rel_pos() + NUL */
11023 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 {
11025#ifdef FEAT_MBYTE
11026 if (has_mbyte)
11027 i += (*mb_char2bytes)(fillchar, buffer + i);
11028 else
11029#endif
11030 buffer[i++] = fillchar;
11031 ++o;
11032 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011033 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035 /* Truncate at window boundary. */
11036#ifdef FEAT_MBYTE
11037 if (has_mbyte)
11038 {
11039 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011040 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 {
11042 o += (*mb_ptr2cells)(buffer + i);
11043 if (this_ru_col + o > WITH_WIDTH(width))
11044 {
11045 buffer[i] = NUL;
11046 break;
11047 }
11048 }
11049 }
11050 else
11051#endif
11052 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
11053 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
11054
11055 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
11056 i = redraw_cmdline;
11057 screen_fill(row, row + 1,
11058 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
11059 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
11060 fillchar, fillchar, attr);
11061 /* don't redraw the cmdline because of showing the ruler */
11062 redraw_cmdline = i;
11063 wp->w_ru_cursor = wp->w_cursor;
11064 wp->w_ru_virtcol = wp->w_virtcol;
11065 wp->w_ru_empty = empty_line;
11066 wp->w_ru_topline = wp->w_topline;
11067 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11068#ifdef FEAT_DIFF
11069 wp->w_ru_topfill = wp->w_topfill;
11070#endif
11071 }
11072}
11073#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011074
11075#if defined(FEAT_LINEBREAK) || defined(PROTO)
11076/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011077 * Return the width of the 'number' and 'relativenumber' column.
11078 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011079 * Otherwise it depends on 'numberwidth' and the line count.
11080 */
11081 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011082number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011083{
11084 int n;
11085 linenr_T lnum;
11086
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011087 if (wp->w_p_rnu && !wp->w_p_nu)
11088 /* cursor line shows "0" */
11089 lnum = wp->w_height;
11090 else
11091 /* cursor line shows absolute line number */
11092 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011093
Bram Moolenaar6b314672015-03-20 15:42:10 +010011094 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011095 return wp->w_nrwidth_width;
11096 wp->w_nrwidth_line_count = lnum;
11097
11098 n = 0;
11099 do
11100 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011101 lnum /= 10;
11102 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011103 } while (lnum > 0);
11104
11105 /* 'numberwidth' gives the minimal width plus one */
11106 if (n < wp->w_p_nuw - 1)
11107 n = wp->w_p_nuw - 1;
11108
11109 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011110 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011111 return n;
11112}
11113#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011114
11115/*
11116 * Return the current cursor column. This is the actual position on the
11117 * screen. First column is 0.
11118 */
11119 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011120screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011121{
11122 return screen_cur_col;
11123}
11124
11125/*
11126 * Return the current cursor row. This is the actual position on the screen.
11127 * First row is 0.
11128 */
11129 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011130screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011131{
11132 return screen_cur_row;
11133}