blob: 4ddded9bfc47e99368e2f9f8ddb588feb0debc4d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static 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 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200425 else if (State & (NORMAL | INSERT))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100426 {
427 update_screen(0);
428 setcursor();
429 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 cursor_on();
431 out_flush();
432#ifdef FEAT_GUI
433 if (gui.in_use)
434 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 * Based on the current value of curwin->w_topline, transfer a screenfull
490 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
491 */
492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 win_T *wp;
496 static int did_intro = FALSE;
497#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
498 int did_one;
499#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200500#ifdef FEAT_GUI
501 int gui_cursor_col;
502 int gui_cursor_row;
503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000505 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (!screen_valid(TRUE))
507 return;
508
509 if (must_redraw)
510 {
511 if (type < must_redraw) /* use maximal type */
512 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000513
514 /* must_redraw is reset here, so that when we run into some weird
515 * reason to redraw while busy redrawing (e.g., asynchronous
516 * scrolling), or update_topline() in win_update() will cause a
517 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 must_redraw = 0;
519 }
520
521 /* Need to update w_lines[]. */
522 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
523 type = NOT_VALID;
524
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000525 /* Postpone the redrawing when it's not needed and when being called
526 * recursively. */
527 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
529 redraw_later(type); /* remember type for next time */
530 must_redraw = type;
531 if (type > INVERTED_ALL)
532 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
533 return;
534 }
535
536 updating_screen = TRUE;
537#ifdef FEAT_SYN_HL
538 ++display_tick; /* let syntax code know we're in a next round of
539 * display updating */
540#endif
541
542 /*
543 * if the screen was scrolled up when displaying a message, scroll it down
544 */
545 if (msg_scrolled)
546 {
547 clear_cmdline = TRUE;
548 if (msg_scrolled > Rows - 5) /* clearing is faster */
549 type = CLEAR;
550 else if (type != CLEAR)
551 {
552 check_for_delay(FALSE);
553 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
554 type = CLEAR;
555 FOR_ALL_WINDOWS(wp)
556 {
557 if (W_WINROW(wp) < msg_scrolled)
558 {
559 if (W_WINROW(wp) + wp->w_height > msg_scrolled
560 && wp->w_redr_type < REDRAW_TOP
561 && wp->w_lines_valid > 0
562 && wp->w_topline == wp->w_lines[0].wl_lnum)
563 {
564 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
565 wp->w_redr_type = REDRAW_TOP;
566 }
567 else
568 {
569 wp->w_redr_type = NOT_VALID;
570#ifdef FEAT_WINDOWS
571 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
572 <= msg_scrolled)
573 wp->w_redr_status = TRUE;
574#endif
575 }
576 }
577 }
578 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000579#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000580 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583 msg_scrolled = 0;
584 need_wait_return = FALSE;
585 }
586
587 /* reset cmdline_row now (may have been changed temporarily) */
588 compute_cmdrow();
589
590 /* Check for changed highlighting */
591 if (need_highlight_changed)
592 highlight_changed();
593
594 if (type == CLEAR) /* first clear screen */
595 {
596 screenclear(); /* will reset clear_cmdline */
597 type = NOT_VALID;
598 }
599
600 if (clear_cmdline) /* going to clear cmdline (done below) */
601 check_for_delay(FALSE);
602
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000603#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200604 /* Force redraw when width of 'number' or 'relativenumber' column
605 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000606 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200607 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
608 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000609 curwin->w_redr_type = NOT_VALID;
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 /*
613 * Only start redrawing if there is really something to do.
614 */
615 if (type == INVERTED)
616 update_curswant();
617 if (curwin->w_redr_type < type
618 && !((type == VALID
619 && curwin->w_lines[0].wl_valid
620#ifdef FEAT_DIFF
621 && curwin->w_topfill == curwin->w_old_topfill
622 && curwin->w_botfill == curwin->w_old_botfill
623#endif
624 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000626 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
628 && curwin->w_old_visual_mode == VIsual_mode
629 && (curwin->w_valid & VALID_VIRTCOL)
630 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 ))
632 curwin->w_redr_type = type;
633
Bram Moolenaar5a305422006-04-28 22:38:25 +0000634#ifdef FEAT_WINDOWS
635 /* Redraw the tab pages line if needed. */
636 if (redraw_tabline || type >= NOT_VALID)
637 draw_tabline();
638#endif
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_SYN_HL
641 /*
642 * Correct stored syntax highlighting info for changes in each displayed
643 * buffer. Each buffer must only be done once.
644 */
645 FOR_ALL_WINDOWS(wp)
646 {
647 if (wp->w_buffer->b_mod_set)
648 {
649# ifdef FEAT_WINDOWS
650 win_T *wwp;
651
652 /* Check if we already did this buffer. */
653 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
654 if (wwp->w_buffer == wp->w_buffer)
655 break;
656# endif
657 if (
658# ifdef FEAT_WINDOWS
659 wwp == wp &&
660# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200661 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 syn_stack_apply_changes(wp->w_buffer);
663 }
664 }
665#endif
666
667 /*
668 * Go from top to bottom through the windows, redrawing the ones that need
669 * it.
670 */
671#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
672 did_one = FALSE;
673#endif
674#ifdef FEAT_SEARCH_EXTRA
675 search_hl.rm.regprog = NULL;
676#endif
677 FOR_ALL_WINDOWS(wp)
678 {
679 if (wp->w_redr_type != 0)
680 {
681 cursor_off();
682#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
683 if (!did_one)
684 {
685 did_one = TRUE;
686# ifdef FEAT_SEARCH_EXTRA
687 start_search_hl();
688# endif
689# ifdef FEAT_CLIPBOARD
690 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200691 if (clip_star.available && clip_isautosel_star())
692 clip_update_selection(&clip_star);
693 if (clip_plus.available && clip_isautosel_plus())
694 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695# endif
696#ifdef FEAT_GUI
697 /* Remove the cursor before starting to do anything, because
698 * scrolling may make it difficult to redraw the text under
699 * it. */
700 if (gui.in_use)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200701 {
702 gui_cursor_col = gui.cursor_col;
703 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 gui_undraw_cursor();
Bram Moolenaar144445d2016-07-08 21:41:54 +0200705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#endif
707 }
708#endif
709 win_update(wp);
710 }
711
712#ifdef FEAT_WINDOWS
713 /* redraw status line after the window to minimize cursor movement */
714 if (wp->w_redr_status)
715 {
716 cursor_off();
717 win_redr_status(wp);
718 }
719#endif
720 }
721#if defined(FEAT_SEARCH_EXTRA)
722 end_search_hl();
723#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100724#ifdef FEAT_INS_EXPAND
725 /* May need to redraw the popup menu. */
726 if (pum_visible())
727 pum_redraw();
728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730#ifdef FEAT_WINDOWS
731 /* Reset b_mod_set flags. Going through all windows is probably faster
732 * than going through all buffers (there could be many buffers). */
733 for (wp = firstwin; wp != NULL; wp = wp->w_next)
734 wp->w_buffer->b_mod_set = FALSE;
735#else
736 curbuf->b_mod_set = FALSE;
737#endif
738
739 updating_screen = FALSE;
740#ifdef FEAT_GUI
741 gui_may_resize_shell();
742#endif
743
744 /* Clear or redraw the command line. Done last, because scrolling may
745 * mess up the command line. */
746 if (clear_cmdline || redraw_cmdline)
747 showmode();
748
749 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200750 if (!did_intro)
751 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 did_intro = TRUE;
753
754#ifdef FEAT_GUI
755 /* Redraw the cursor and update the scrollbars when all screen updating is
756 * done. */
757 if (gui.in_use)
758 {
759 out_flush(); /* required before updating the cursor */
760 if (did_one)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200761 {
762 /* Put the GUI position where the cursor was, gui_update_cursor()
763 * uses that. */
764 gui.col = gui_cursor_col;
765 gui.row = gui_cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 gui_update_scrollbars(FALSE);
769 }
770#endif
771}
772
Bram Moolenaar860cae12010-06-05 23:22:07 +0200773#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200774/*
775 * Return TRUE if the cursor line in window "wp" may be concealed, according
776 * to the 'concealcursor' option.
777 */
778 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100779conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200780{
781 int c;
782
783 if (*wp->w_p_cocu == NUL)
784 return FALSE;
785 if (get_real_state() & VISUAL)
786 c = 'v';
787 else if (State & INSERT)
788 c = 'i';
789 else if (State & NORMAL)
790 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200791 else if (State & CMDLINE)
792 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200793 else
794 return FALSE;
795 return vim_strchr(wp->w_p_cocu, c) != NULL;
796}
797
798/*
799 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
800 */
801 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100802conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200803{
804 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
805 {
806 need_cursor_line_redraw = TRUE;
807 /* Need to recompute cursor column, e.g., when starting Visual mode
808 * without concealing. */
809 curs_columns(TRUE);
810 }
811}
812
Bram Moolenaar860cae12010-06-05 23:22:07 +0200813 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100814update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200815{
816 int row;
817 int j;
818
Bram Moolenaar908be432016-05-24 10:51:30 +0200819 /* Don't do anything if the screen structures are (not yet) valid. */
820 if (!screen_valid(TRUE))
821 return;
822
Bram Moolenaar860cae12010-06-05 23:22:07 +0200823 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200824 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200825 {
826# ifdef FEAT_GUI
827 /* Remove the cursor before starting to do anything, because scrolling
828 * may make it difficult to redraw the text under it. */
829 if (gui.in_use)
830 gui_undraw_cursor();
831# endif
832 row = 0;
833 for (j = 0; j < wp->w_lines_valid; ++j)
834 {
835 if (lnum == wp->w_lines[j].wl_lnum)
836 {
837 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200838# ifdef FEAT_SEARCH_EXTRA
839 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200840 start_search_hl();
841 prepare_search_hl(wp, lnum);
842# endif
843 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
844# if defined(FEAT_SEARCH_EXTRA)
845 end_search_hl();
846# endif
847 break;
848 }
849 row += wp->w_lines[j].wl_size;
850 }
851# ifdef FEAT_GUI
852 /* Redraw the cursor */
853 if (gui.in_use)
854 {
855 out_flush(); /* required before updating the cursor */
856 gui_update_cursor(FALSE, FALSE);
857 }
858# endif
859 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200860 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200861}
862#endif
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100865static void update_prepare(void);
866static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868/*
869 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000870 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 */
872 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100873update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 cursor_off();
876 updating_screen = TRUE;
877#ifdef FEAT_GUI
878 /* Remove the cursor before starting to do anything, because scrolling may
879 * make it difficult to redraw the text under it. */
880 if (gui.in_use)
881 gui_undraw_cursor();
882#endif
883#ifdef FEAT_SEARCH_EXTRA
884 start_search_hl();
885#endif
886}
887
888/*
889 * Finish updating one or more windows.
890 */
891 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100892update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893{
894 if (redraw_cmdline)
895 showmode();
896
897# ifdef FEAT_SEARCH_EXTRA
898 end_search_hl();
899# endif
900
901 updating_screen = FALSE;
902
903# ifdef FEAT_GUI
904 gui_may_resize_shell();
905
906 /* Redraw the cursor and update the scrollbars when all screen updating is
907 * done. */
908 if (gui.in_use)
909 {
910 out_flush(); /* required before updating the cursor */
911 gui_update_cursor(FALSE, FALSE);
912 gui_update_scrollbars(FALSE);
913 }
914# endif
915}
916#endif
917
918#if defined(FEAT_SIGNS) || defined(PROTO)
919 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100920update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 win_T *wp;
923 int doit = FALSE;
924
925# ifdef FEAT_FOLDING
926 win_foldinfo.fi_level = 0;
927# endif
928
929 /* update/delete a specific mark */
930 FOR_ALL_WINDOWS(wp)
931 {
932 if (buf != NULL && lnum > 0)
933 {
934 if (wp->w_buffer == buf && lnum >= wp->w_topline
935 && lnum < wp->w_botline)
936 {
937 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
938 wp->w_redraw_top = lnum;
939 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
940 wp->w_redraw_bot = lnum;
941 redraw_win_later(wp, VALID);
942 }
943 }
944 else
945 redraw_win_later(wp, VALID);
946 if (wp->w_redr_type != 0)
947 doit = TRUE;
948 }
949
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100950 /* Return when there is nothing to do, screen updating is already
951 * happening (recursive call) or still starting up. */
952 if (!doit || updating_screen
953#ifdef FEAT_GUI
954 || gui.starting
955#endif
956 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 return;
958
959 /* update all windows that need updating */
960 update_prepare();
961
962# ifdef FEAT_WINDOWS
963 for (wp = firstwin; wp; wp = wp->w_next)
964 {
965 if (wp->w_redr_type != 0)
966 win_update(wp);
967 if (wp->w_redr_status)
968 win_redr_status(wp);
969 }
970# else
971 if (curwin->w_redr_type != 0)
972 win_update(curwin);
973# endif
974
975 update_finish();
976}
977#endif
978
979
980#if defined(FEAT_GUI) || defined(PROTO)
981/*
982 * Update a single window, its status line and maybe the command line msg.
983 * Used for the GUI scrollbar.
984 */
985 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100986updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000988 /* return if already busy updating */
989 if (updating_screen)
990 return;
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 update_prepare();
993
994#ifdef FEAT_CLIPBOARD
995 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200996 if (clip_star.available && clip_isautosel_star())
997 clip_update_selection(&clip_star);
998 if (clip_plus.available && clip_isautosel_plus())
999 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001005 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001006 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001007 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (wp->w_redr_status
1010# ifdef FEAT_CMDL_INFO
1011 || p_ru
1012# endif
1013# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001014 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015# endif
1016 )
1017 win_redr_status(wp);
1018#endif
1019
1020 update_finish();
1021}
1022#endif
1023
1024/*
1025 * Update a single window.
1026 *
1027 * This may cause the windows below it also to be redrawn (when clearing the
1028 * screen or scrolling lines).
1029 *
1030 * How the window is redrawn depends on wp->w_redr_type. Each type also
1031 * implies the one below it.
1032 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001033 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1035 * INVERTED redraw the changed part of the Visual area
1036 * INVERTED_ALL redraw the whole Visual area
1037 * VALID 1. scroll up/down to adjust for a changed w_topline
1038 * 2. update lines at the top when scrolled down
1039 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001040 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 * b_mod_top and b_mod_bot.
1042 * - if wp->w_redraw_top non-zero, redraw lines between
1043 * wp->w_redraw_top and wp->w_redr_bot.
1044 * - continue redrawing when syntax status is invalid.
1045 * 4. if scrolled up, update lines at the bottom.
1046 * This results in three areas that may need updating:
1047 * top: from first row to top_end (when scrolled down)
1048 * mid: from mid_start to mid_end (update inversion or changed text)
1049 * bot: from bot_start to last row (when scrolled up)
1050 */
1051 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001052win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 buf_T *buf = wp->w_buffer;
1055 int type;
1056 int top_end = 0; /* Below last row of the top area that needs
1057 updating. 0 when no top area updating. */
1058 int mid_start = 999;/* first row of the mid area that needs
1059 updating. 999 when no mid area updating. */
1060 int mid_end = 0; /* Below last row of the mid area that needs
1061 updating. 0 when no mid area updating. */
1062 int bot_start = 999;/* first row of the bot area that needs
1063 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 int scrolled_down = FALSE; /* TRUE when scrolled down when
1065 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001067 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 int top_to_mod = FALSE; /* redraw above mod_top */
1069#endif
1070
1071 int row; /* current window row to display */
1072 linenr_T lnum; /* current buffer lnum to display */
1073 int idx; /* current index in w_lines[] */
1074 int srow; /* starting row of the current line */
1075
1076 int eof = FALSE; /* if TRUE, we hit the end of the file */
1077 int didline = FALSE; /* if TRUE, we finished the last line */
1078 int i;
1079 long j;
1080 static int recursive = FALSE; /* being called recursively */
1081 int old_botline = wp->w_botline;
1082#ifdef FEAT_FOLDING
1083 long fold_count;
1084#endif
1085#ifdef FEAT_SYN_HL
1086 /* remember what happened to the previous line, to know if
1087 * check_visual_highlight() can be used */
1088#define DID_NONE 1 /* didn't update a line */
1089#define DID_LINE 2 /* updated a normal line */
1090#define DID_FOLD 3 /* updated a folded line */
1091 int did_update = DID_NONE;
1092 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1093#endif
1094 linenr_T mod_top = 0;
1095 linenr_T mod_bot = 0;
1096#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1097 int save_got_int;
1098#endif
1099
1100 type = wp->w_redr_type;
1101
1102 if (type == NOT_VALID)
1103 {
1104#ifdef FEAT_WINDOWS
1105 wp->w_redr_status = TRUE;
1106#endif
1107 wp->w_lines_valid = 0;
1108 }
1109
1110 /* Window is zero-height: nothing to draw. */
1111 if (wp->w_height == 0)
1112 {
1113 wp->w_redr_type = 0;
1114 return;
1115 }
1116
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001117#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 /* Window is zero-width: Only need to draw the separator. */
1119 if (wp->w_width == 0)
1120 {
1121 /* draw the vertical separator right of this window */
1122 draw_vsep_win(wp, 0);
1123 wp->w_redr_type = 0;
1124 return;
1125 }
1126#endif
1127
1128#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001129 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#endif
1131
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001132#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001133 /* Force redraw when width of 'number' or 'relativenumber' column
1134 * changes. */
1135 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001136 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001137 {
1138 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001139 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001140 }
1141 else
1142#endif
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1145 {
1146 /*
1147 * When there are both inserted/deleted lines and specific lines to be
1148 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1149 * everything (only happens when redrawing is off for while).
1150 */
1151 type = NOT_VALID;
1152 }
1153 else
1154 {
1155 /*
1156 * Set mod_top to the first line that needs displaying because of
1157 * changes. Set mod_bot to the first line after the changes.
1158 */
1159 mod_top = wp->w_redraw_top;
1160 if (wp->w_redraw_bot != 0)
1161 mod_bot = wp->w_redraw_bot + 1;
1162 else
1163 mod_bot = 0;
1164 wp->w_redraw_top = 0; /* reset for next time */
1165 wp->w_redraw_bot = 0;
1166 if (buf->b_mod_set)
1167 {
1168 if (mod_top == 0 || mod_top > buf->b_mod_top)
1169 {
1170 mod_top = buf->b_mod_top;
1171#ifdef FEAT_SYN_HL
1172 /* Need to redraw lines above the change that may be included
1173 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001174 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001176 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (mod_top < 1)
1178 mod_top = 1;
1179 }
1180#endif
1181 }
1182 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1183 mod_bot = buf->b_mod_bot;
1184
1185#ifdef FEAT_SEARCH_EXTRA
1186 /* When 'hlsearch' is on and using a multi-line search pattern, a
1187 * change in one line may make the Search highlighting in a
1188 * previous line invalid. Simple solution: redraw all visible
1189 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001190 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001192 if (search_hl.rm.regprog != NULL
1193 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001195 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001196 {
1197 cur = wp->w_match_head;
1198 while (cur != NULL)
1199 {
1200 if (cur->match.regprog != NULL
1201 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001202 {
1203 top_to_mod = TRUE;
1204 break;
1205 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001206 cur = cur->next;
1207 }
1208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif
1210 }
1211#ifdef FEAT_FOLDING
1212 if (mod_top != 0 && hasAnyFolding(wp))
1213 {
1214 linenr_T lnumt, lnumb;
1215
1216 /*
1217 * A change in a line can cause lines above it to become folded or
1218 * unfolded. Find the top most buffer line that may be affected.
1219 * If the line was previously folded and displayed, get the first
1220 * line of that fold. If the line is folded now, get the first
1221 * folded line. Use the minimum of these two.
1222 */
1223
1224 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1225 * the line below it. If there is no valid entry, use w_topline.
1226 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1227 * to this line. If there is no valid entry, use MAXLNUM. */
1228 lnumt = wp->w_topline;
1229 lnumb = MAXLNUM;
1230 for (i = 0; i < wp->w_lines_valid; ++i)
1231 if (wp->w_lines[i].wl_valid)
1232 {
1233 if (wp->w_lines[i].wl_lastlnum < mod_top)
1234 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1235 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1236 {
1237 lnumb = wp->w_lines[i].wl_lnum;
1238 /* When there is a fold column it might need updating
1239 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001240 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 ++lnumb;
1242 }
1243 }
1244
1245 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1246 if (mod_top > lnumt)
1247 mod_top = lnumt;
1248
1249 /* Now do the same for the bottom line (one above mod_bot). */
1250 --mod_bot;
1251 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1252 ++mod_bot;
1253 if (mod_bot < lnumb)
1254 mod_bot = lnumb;
1255 }
1256#endif
1257
1258 /* When a change starts above w_topline and the end is below
1259 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001260 * If the end of the change is above w_topline: do like no change was
1261 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (mod_top != 0 && mod_top < wp->w_topline)
1263 {
1264 if (mod_bot > wp->w_topline)
1265 mod_top = wp->w_topline;
1266#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001267 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 top_end = 1;
1269#endif
1270 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001271
1272 /* When line numbers are displayed need to redraw all lines below
1273 * inserted/deleted lines. */
1274 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1275 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
1277
1278 /*
1279 * When only displaying the lines at the top, set top_end. Used when
1280 * window has scrolled down for msg_scrolled.
1281 */
1282 if (type == REDRAW_TOP)
1283 {
1284 j = 0;
1285 for (i = 0; i < wp->w_lines_valid; ++i)
1286 {
1287 j += wp->w_lines[i].wl_size;
1288 if (j >= wp->w_upd_rows)
1289 {
1290 top_end = j;
1291 break;
1292 }
1293 }
1294 if (top_end == 0)
1295 /* not found (cannot happen?): redraw everything */
1296 type = NOT_VALID;
1297 else
1298 /* top area defined, the rest is VALID */
1299 type = VALID;
1300 }
1301
Bram Moolenaar367329b2007-08-30 11:53:22 +00001302 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001303 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1304 * non-zero and thus not FALSE) will indicate that screenclear() was not
1305 * called. */
1306 if (screen_cleared)
1307 screen_cleared = MAYBE;
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 /*
1310 * If there are no changes on the screen that require a complete redraw,
1311 * handle three cases:
1312 * 1: we are off the top of the screen by a few lines: scroll down
1313 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1314 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1315 * w_lines[] that needs updating.
1316 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001317 if ((type == VALID || type == SOME_VALID
1318 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319#ifdef FEAT_DIFF
1320 && !wp->w_botfill && !wp->w_old_botfill
1321#endif
1322 )
1323 {
1324 if (mod_top != 0 && wp->w_topline == mod_top)
1325 {
1326 /*
1327 * w_topline is the first changed line, the scrolling will be done
1328 * further down.
1329 */
1330 }
1331 else if (wp->w_lines[0].wl_valid
1332 && (wp->w_topline < wp->w_lines[0].wl_lnum
1333#ifdef FEAT_DIFF
1334 || (wp->w_topline == wp->w_lines[0].wl_lnum
1335 && wp->w_topfill > wp->w_old_topfill)
1336#endif
1337 ))
1338 {
1339 /*
1340 * New topline is above old topline: May scroll down.
1341 */
1342#ifdef FEAT_FOLDING
1343 if (hasAnyFolding(wp))
1344 {
1345 linenr_T ln;
1346
1347 /* count the number of lines we are off, counting a sequence
1348 * of folded lines as one */
1349 j = 0;
1350 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1351 {
1352 ++j;
1353 if (j >= wp->w_height - 2)
1354 break;
1355 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1356 }
1357 }
1358 else
1359#endif
1360 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1361 if (j < wp->w_height - 2) /* not too far off */
1362 {
1363 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1364#ifdef FEAT_DIFF
1365 /* insert extra lines for previously invisible filler lines */
1366 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1367 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1368 - wp->w_old_topfill;
1369#endif
1370 if (i < wp->w_height - 2) /* less than a screen off */
1371 {
1372 /*
1373 * Try to insert the correct number of lines.
1374 * If not the last window, delete the lines at the bottom.
1375 * win_ins_lines may fail when the terminal can't do it.
1376 */
1377 if (i > 0)
1378 check_for_delay(FALSE);
1379 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1380 {
1381 if (wp->w_lines_valid != 0)
1382 {
1383 /* Need to update rows that are new, stop at the
1384 * first one that scrolled down. */
1385 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 /* Move the entries that were scrolled, disable
1389 * the entries for the lines to be redrawn. */
1390 if ((wp->w_lines_valid += j) > wp->w_height)
1391 wp->w_lines_valid = wp->w_height;
1392 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1393 wp->w_lines[idx] = wp->w_lines[idx - j];
1394 while (idx >= 0)
1395 wp->w_lines[idx--].wl_valid = FALSE;
1396 }
1397 }
1398 else
1399 mid_start = 0; /* redraw all lines */
1400 }
1401 else
1402 mid_start = 0; /* redraw all lines */
1403 }
1404 else
1405 mid_start = 0; /* redraw all lines */
1406 }
1407 else
1408 {
1409 /*
1410 * New topline is at or below old topline: May scroll up.
1411 * When topline didn't change, find first entry in w_lines[] that
1412 * needs updating.
1413 */
1414
1415 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1416 j = -1;
1417 row = 0;
1418 for (i = 0; i < wp->w_lines_valid; i++)
1419 {
1420 if (wp->w_lines[i].wl_valid
1421 && wp->w_lines[i].wl_lnum == wp->w_topline)
1422 {
1423 j = i;
1424 break;
1425 }
1426 row += wp->w_lines[i].wl_size;
1427 }
1428 if (j == -1)
1429 {
1430 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1431 * lines */
1432 mid_start = 0;
1433 }
1434 else
1435 {
1436 /*
1437 * Try to delete the correct number of lines.
1438 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1439 */
1440#ifdef FEAT_DIFF
1441 /* If the topline didn't change, delete old filler lines,
1442 * otherwise delete filler lines of the new topline... */
1443 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1444 row += wp->w_old_topfill;
1445 else
1446 row += diff_check_fill(wp, wp->w_topline);
1447 /* ... but don't delete new filler lines. */
1448 row -= wp->w_topfill;
1449#endif
1450 if (row > 0)
1451 {
1452 check_for_delay(FALSE);
1453 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1454 bot_start = wp->w_height - row;
1455 else
1456 mid_start = 0; /* redraw all lines */
1457 }
1458 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1459 {
1460 /*
1461 * Skip the lines (below the deleted lines) that are still
1462 * valid and don't need redrawing. Copy their info
1463 * upwards, to compensate for the deleted lines. Set
1464 * bot_start to the first row that needs redrawing.
1465 */
1466 bot_start = 0;
1467 idx = 0;
1468 for (;;)
1469 {
1470 wp->w_lines[idx] = wp->w_lines[j];
1471 /* stop at line that didn't fit, unless it is still
1472 * valid (no lines deleted) */
1473 if (row > 0 && bot_start + row
1474 + (int)wp->w_lines[j].wl_size > wp->w_height)
1475 {
1476 wp->w_lines_valid = idx + 1;
1477 break;
1478 }
1479 bot_start += wp->w_lines[idx++].wl_size;
1480
1481 /* stop at the last valid entry in w_lines[].wl_size */
1482 if (++j >= wp->w_lines_valid)
1483 {
1484 wp->w_lines_valid = idx;
1485 break;
1486 }
1487 }
1488#ifdef FEAT_DIFF
1489 /* Correct the first entry for filler lines at the top
1490 * when it won't get updated below. */
1491 if (wp->w_p_diff && bot_start > 0)
1492 wp->w_lines[0].wl_size =
1493 plines_win_nofill(wp, wp->w_topline, TRUE)
1494 + wp->w_topfill;
1495#endif
1496 }
1497 }
1498 }
1499
1500 /* When starting redraw in the first line, redraw all lines. When
1501 * there is only one window it's probably faster to clear the screen
1502 * first. */
1503 if (mid_start == 0)
1504 {
1505 mid_end = wp->w_height;
1506 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001507 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001508 /* Clear the screen when it was not done by win_del_lines() or
1509 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1510 * then. */
1511 if (screen_cleared != TRUE)
1512 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001513#ifdef FEAT_WINDOWS
1514 /* The screen was cleared, redraw the tab pages line. */
1515 if (redraw_tabline)
1516 draw_tabline();
1517#endif
1518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001520
1521 /* When win_del_lines() or win_ins_lines() caused the screen to be
1522 * cleared (only happens for the first window) or when screenclear()
1523 * was called directly above, "must_redraw" will have been set to
1524 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1525 if (screen_cleared == TRUE)
1526 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528 else
1529 {
1530 /* Not VALID or INVERTED: redraw all lines. */
1531 mid_start = 0;
1532 mid_end = wp->w_height;
1533 }
1534
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001535 if (type == SOME_VALID)
1536 {
1537 /* SOME_VALID: redraw all lines. */
1538 mid_start = 0;
1539 mid_end = wp->w_height;
1540 type = NOT_VALID;
1541 }
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /* check if we are updating or removing the inverted part */
1544 if ((VIsual_active && buf == curwin->w_buffer)
1545 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1546 {
1547 linenr_T from, to;
1548
1549 if (VIsual_active)
1550 {
1551 if (VIsual_active
1552 && (VIsual_mode != wp->w_old_visual_mode
1553 || type == INVERTED_ALL))
1554 {
1555 /*
1556 * If the type of Visual selection changed, redraw the whole
1557 * selection. Also when the ownership of the X selection is
1558 * gained or lost.
1559 */
1560 if (curwin->w_cursor.lnum < VIsual.lnum)
1561 {
1562 from = curwin->w_cursor.lnum;
1563 to = VIsual.lnum;
1564 }
1565 else
1566 {
1567 from = VIsual.lnum;
1568 to = curwin->w_cursor.lnum;
1569 }
1570 /* redraw more when the cursor moved as well */
1571 if (wp->w_old_cursor_lnum < from)
1572 from = wp->w_old_cursor_lnum;
1573 if (wp->w_old_cursor_lnum > to)
1574 to = wp->w_old_cursor_lnum;
1575 if (wp->w_old_visual_lnum < from)
1576 from = wp->w_old_visual_lnum;
1577 if (wp->w_old_visual_lnum > to)
1578 to = wp->w_old_visual_lnum;
1579 }
1580 else
1581 {
1582 /*
1583 * Find the line numbers that need to be updated: The lines
1584 * between the old cursor position and the current cursor
1585 * position. Also check if the Visual position changed.
1586 */
1587 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1588 {
1589 from = curwin->w_cursor.lnum;
1590 to = wp->w_old_cursor_lnum;
1591 }
1592 else
1593 {
1594 from = wp->w_old_cursor_lnum;
1595 to = curwin->w_cursor.lnum;
1596 if (from == 0) /* Visual mode just started */
1597 from = to;
1598 }
1599
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001600 if (VIsual.lnum != wp->w_old_visual_lnum
1601 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 if (wp->w_old_visual_lnum < from
1604 && wp->w_old_visual_lnum != 0)
1605 from = wp->w_old_visual_lnum;
1606 if (wp->w_old_visual_lnum > to)
1607 to = wp->w_old_visual_lnum;
1608 if (VIsual.lnum < from)
1609 from = VIsual.lnum;
1610 if (VIsual.lnum > to)
1611 to = VIsual.lnum;
1612 }
1613 }
1614
1615 /*
1616 * If in block mode and changed column or curwin->w_curswant:
1617 * update all lines.
1618 * First compute the actual start and end column.
1619 */
1620 if (VIsual_mode == Ctrl_V)
1621 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001622 colnr_T fromc, toc;
1623#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1624 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaar404406a2014-10-09 13:24:43 +02001626 if (curwin->w_p_lbr)
1627 ve_flags = VE_ALL;
1628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001630#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1631 ve_flags = save_ve_flags;
1632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 ++toc;
1634 if (curwin->w_curswant == MAXCOL)
1635 toc = MAXCOL;
1636
1637 if (fromc != wp->w_old_cursor_fcol
1638 || toc != wp->w_old_cursor_lcol)
1639 {
1640 if (from > VIsual.lnum)
1641 from = VIsual.lnum;
1642 if (to < VIsual.lnum)
1643 to = VIsual.lnum;
1644 }
1645 wp->w_old_cursor_fcol = fromc;
1646 wp->w_old_cursor_lcol = toc;
1647 }
1648 }
1649 else
1650 {
1651 /* Use the line numbers of the old Visual area. */
1652 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1653 {
1654 from = wp->w_old_cursor_lnum;
1655 to = wp->w_old_visual_lnum;
1656 }
1657 else
1658 {
1659 from = wp->w_old_visual_lnum;
1660 to = wp->w_old_cursor_lnum;
1661 }
1662 }
1663
1664 /*
1665 * There is no need to update lines above the top of the window.
1666 */
1667 if (from < wp->w_topline)
1668 from = wp->w_topline;
1669
1670 /*
1671 * If we know the value of w_botline, use it to restrict the update to
1672 * the lines that are visible in the window.
1673 */
1674 if (wp->w_valid & VALID_BOTLINE)
1675 {
1676 if (from >= wp->w_botline)
1677 from = wp->w_botline - 1;
1678 if (to >= wp->w_botline)
1679 to = wp->w_botline - 1;
1680 }
1681
1682 /*
1683 * Find the minimal part to be updated.
1684 * Watch out for scrolling that made entries in w_lines[] invalid.
1685 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1686 * top_end; need to redraw from top_end to the "to" line.
1687 * A middle mouse click with a Visual selection may change the text
1688 * above the Visual area and reset wl_valid, do count these for
1689 * mid_end (in srow).
1690 */
1691 if (mid_start > 0)
1692 {
1693 lnum = wp->w_topline;
1694 idx = 0;
1695 srow = 0;
1696 if (scrolled_down)
1697 mid_start = top_end;
1698 else
1699 mid_start = 0;
1700 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1701 {
1702 if (wp->w_lines[idx].wl_valid)
1703 mid_start += wp->w_lines[idx].wl_size;
1704 else if (!scrolled_down)
1705 srow += wp->w_lines[idx].wl_size;
1706 ++idx;
1707# ifdef FEAT_FOLDING
1708 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1709 lnum = wp->w_lines[idx].wl_lnum;
1710 else
1711# endif
1712 ++lnum;
1713 }
1714 srow += mid_start;
1715 mid_end = wp->w_height;
1716 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1717 {
1718 if (wp->w_lines[idx].wl_valid
1719 && wp->w_lines[idx].wl_lnum >= to + 1)
1720 {
1721 /* Only update until first row of this line */
1722 mid_end = srow;
1723 break;
1724 }
1725 srow += wp->w_lines[idx].wl_size;
1726 }
1727 }
1728 }
1729
1730 if (VIsual_active && buf == curwin->w_buffer)
1731 {
1732 wp->w_old_visual_mode = VIsual_mode;
1733 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1734 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001735 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 wp->w_old_curswant = curwin->w_curswant;
1737 }
1738 else
1739 {
1740 wp->w_old_visual_mode = 0;
1741 wp->w_old_cursor_lnum = 0;
1742 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001743 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1747 /* reset got_int, otherwise regexp won't work */
1748 save_got_int = got_int;
1749 got_int = 0;
1750#endif
1751#ifdef FEAT_FOLDING
1752 win_foldinfo.fi_level = 0;
1753#endif
1754
1755 /*
1756 * Update all the window rows.
1757 */
1758 idx = 0; /* first entry in w_lines[].wl_size */
1759 row = 0;
1760 srow = 0;
1761 lnum = wp->w_topline; /* first line shown in window */
1762 for (;;)
1763 {
1764 /* stop updating when reached the end of the window (check for _past_
1765 * the end of the window is at the end of the loop) */
1766 if (row == wp->w_height)
1767 {
1768 didline = TRUE;
1769 break;
1770 }
1771
1772 /* stop updating when hit the end of the file */
1773 if (lnum > buf->b_ml.ml_line_count)
1774 {
1775 eof = TRUE;
1776 break;
1777 }
1778
1779 /* Remember the starting row of the line that is going to be dealt
1780 * with. It is used further down when the line doesn't fit. */
1781 srow = row;
1782
1783 /*
1784 * Update a line when it is in an area that needs updating, when it
1785 * has changes or w_lines[idx] is invalid.
1786 * bot_start may be halfway a wrapped line after using
1787 * win_del_lines(), check if the current line includes it.
1788 * When syntax folding is being used, the saved syntax states will
1789 * already have been updated, we can't see where the syntax state is
1790 * the same again, just update until the end of the window.
1791 */
1792 if (row < top_end
1793 || (row >= mid_start && row < mid_end)
1794#ifdef FEAT_SEARCH_EXTRA
1795 || top_to_mod
1796#endif
1797 || idx >= wp->w_lines_valid
1798 || (row + wp->w_lines[idx].wl_size > bot_start)
1799 || (mod_top != 0
1800 && (lnum == mod_top
1801 || (lnum >= mod_top
1802 && (lnum < mod_bot
1803#ifdef FEAT_SYN_HL
1804 || did_update == DID_FOLD
1805 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001806 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 && (
1808# ifdef FEAT_FOLDING
1809 (foldmethodIsSyntax(wp)
1810 && hasAnyFolding(wp)) ||
1811# endif
1812 syntax_check_changed(lnum)))
1813#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001814#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001815 /* match in fixed position might need redraw
1816 * if lines were inserted or deleted */
1817 || (wp->w_match_head != NULL
1818 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 )))))
1821 {
1822#ifdef FEAT_SEARCH_EXTRA
1823 if (lnum == mod_top)
1824 top_to_mod = FALSE;
1825#endif
1826
1827 /*
1828 * When at start of changed lines: May scroll following lines
1829 * up or down to minimize redrawing.
1830 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001831 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
1833 if (lnum == mod_top
1834 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001835 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 int old_rows = 0;
1838 int new_rows = 0;
1839 int xtra_rows;
1840 linenr_T l;
1841
1842 /* Count the old number of window rows, using w_lines[], which
1843 * should still contain the sizes for the lines as they are
1844 * currently displayed. */
1845 for (i = idx; i < wp->w_lines_valid; ++i)
1846 {
1847 /* Only valid lines have a meaningful wl_lnum. Invalid
1848 * lines are part of the changed area. */
1849 if (wp->w_lines[i].wl_valid
1850 && wp->w_lines[i].wl_lnum == mod_bot)
1851 break;
1852 old_rows += wp->w_lines[i].wl_size;
1853#ifdef FEAT_FOLDING
1854 if (wp->w_lines[i].wl_valid
1855 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1856 {
1857 /* Must have found the last valid entry above mod_bot.
1858 * Add following invalid entries. */
1859 ++i;
1860 while (i < wp->w_lines_valid
1861 && !wp->w_lines[i].wl_valid)
1862 old_rows += wp->w_lines[i++].wl_size;
1863 break;
1864 }
1865#endif
1866 }
1867
1868 if (i >= wp->w_lines_valid)
1869 {
1870 /* We can't find a valid line below the changed lines,
1871 * need to redraw until the end of the window.
1872 * Inserting/deleting lines has no use. */
1873 bot_start = 0;
1874 }
1875 else
1876 {
1877 /* Able to count old number of rows: Count new window
1878 * rows, and may insert/delete lines */
1879 j = idx;
1880 for (l = lnum; l < mod_bot; ++l)
1881 {
1882#ifdef FEAT_FOLDING
1883 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1884 ++new_rows;
1885 else
1886#endif
1887#ifdef FEAT_DIFF
1888 if (l == wp->w_topline)
1889 new_rows += plines_win_nofill(wp, l, TRUE)
1890 + wp->w_topfill;
1891 else
1892#endif
1893 new_rows += plines_win(wp, l, TRUE);
1894 ++j;
1895 if (new_rows > wp->w_height - row - 2)
1896 {
1897 /* it's getting too much, must redraw the rest */
1898 new_rows = 9999;
1899 break;
1900 }
1901 }
1902 xtra_rows = new_rows - old_rows;
1903 if (xtra_rows < 0)
1904 {
1905 /* May scroll text up. If there is not enough
1906 * remaining text or scrolling fails, must redraw the
1907 * rest. If scrolling works, must redraw the text
1908 * below the scrolled text. */
1909 if (row - xtra_rows >= wp->w_height - 2)
1910 mod_bot = MAXLNUM;
1911 else
1912 {
1913 check_for_delay(FALSE);
1914 if (win_del_lines(wp, row,
1915 -xtra_rows, FALSE, FALSE) == FAIL)
1916 mod_bot = MAXLNUM;
1917 else
1918 bot_start = wp->w_height + xtra_rows;
1919 }
1920 }
1921 else if (xtra_rows > 0)
1922 {
1923 /* May scroll text down. If there is not enough
1924 * remaining text of scrolling fails, must redraw the
1925 * rest. */
1926 if (row + xtra_rows >= wp->w_height - 2)
1927 mod_bot = MAXLNUM;
1928 else
1929 {
1930 check_for_delay(FALSE);
1931 if (win_ins_lines(wp, row + old_rows,
1932 xtra_rows, FALSE, FALSE) == FAIL)
1933 mod_bot = MAXLNUM;
1934 else if (top_end > row + old_rows)
1935 /* Scrolled the part at the top that requires
1936 * updating down. */
1937 top_end += xtra_rows;
1938 }
1939 }
1940
1941 /* When not updating the rest, may need to move w_lines[]
1942 * entries. */
1943 if (mod_bot != MAXLNUM && i != j)
1944 {
1945 if (j < i)
1946 {
1947 int x = row + new_rows;
1948
1949 /* move entries in w_lines[] upwards */
1950 for (;;)
1951 {
1952 /* stop at last valid entry in w_lines[] */
1953 if (i >= wp->w_lines_valid)
1954 {
1955 wp->w_lines_valid = j;
1956 break;
1957 }
1958 wp->w_lines[j] = wp->w_lines[i];
1959 /* stop at a line that won't fit */
1960 if (x + (int)wp->w_lines[j].wl_size
1961 > wp->w_height)
1962 {
1963 wp->w_lines_valid = j + 1;
1964 break;
1965 }
1966 x += wp->w_lines[j++].wl_size;
1967 ++i;
1968 }
1969 if (bot_start > x)
1970 bot_start = x;
1971 }
1972 else /* j > i */
1973 {
1974 /* move entries in w_lines[] downwards */
1975 j -= i;
1976 wp->w_lines_valid += j;
1977 if (wp->w_lines_valid > wp->w_height)
1978 wp->w_lines_valid = wp->w_height;
1979 for (i = wp->w_lines_valid; i - j >= idx; --i)
1980 wp->w_lines[i] = wp->w_lines[i - j];
1981
1982 /* The w_lines[] entries for inserted lines are
1983 * now invalid, but wl_size may be used above.
1984 * Reset to zero. */
1985 while (i >= idx)
1986 {
1987 wp->w_lines[i].wl_size = 0;
1988 wp->w_lines[i--].wl_valid = FALSE;
1989 }
1990 }
1991 }
1992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 }
1994
1995#ifdef FEAT_FOLDING
1996 /*
1997 * When lines are folded, display one line for all of them.
1998 * Otherwise, display normally (can be several display lines when
1999 * 'wrap' is on).
2000 */
2001 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2002 if (fold_count != 0)
2003 {
2004 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2005 ++row;
2006 --fold_count;
2007 wp->w_lines[idx].wl_folded = TRUE;
2008 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2009# ifdef FEAT_SYN_HL
2010 did_update = DID_FOLD;
2011# endif
2012 }
2013 else
2014#endif
2015 if (idx < wp->w_lines_valid
2016 && wp->w_lines[idx].wl_valid
2017 && wp->w_lines[idx].wl_lnum == lnum
2018 && lnum > wp->w_topline
2019 && !(dy_flags & DY_LASTLINE)
2020 && srow + wp->w_lines[idx].wl_size > wp->w_height
2021#ifdef FEAT_DIFF
2022 && diff_check_fill(wp, lnum) == 0
2023#endif
2024 )
2025 {
2026 /* This line is not going to fit. Don't draw anything here,
2027 * will draw "@ " lines below. */
2028 row = wp->w_height + 1;
2029 }
2030 else
2031 {
2032#ifdef FEAT_SEARCH_EXTRA
2033 prepare_search_hl(wp, lnum);
2034#endif
2035#ifdef FEAT_SYN_HL
2036 /* Let the syntax stuff know we skipped a few lines. */
2037 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002038 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 syntax_end_parsing(syntax_last_parsed + 1);
2040#endif
2041
2042 /*
2043 * Display one line.
2044 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002045 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047#ifdef FEAT_FOLDING
2048 wp->w_lines[idx].wl_folded = FALSE;
2049 wp->w_lines[idx].wl_lastlnum = lnum;
2050#endif
2051#ifdef FEAT_SYN_HL
2052 did_update = DID_LINE;
2053 syntax_last_parsed = lnum;
2054#endif
2055 }
2056
2057 wp->w_lines[idx].wl_lnum = lnum;
2058 wp->w_lines[idx].wl_valid = TRUE;
2059 if (row > wp->w_height) /* past end of screen */
2060 {
2061 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002062 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2064 ++idx;
2065 break;
2066 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002067 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 wp->w_lines[idx].wl_size = row - srow;
2069 ++idx;
2070#ifdef FEAT_FOLDING
2071 lnum += fold_count + 1;
2072#else
2073 ++lnum;
2074#endif
2075 }
2076 else
2077 {
2078 /* This line does not need updating, advance to the next one */
2079 row += wp->w_lines[idx++].wl_size;
2080 if (row > wp->w_height) /* past end of screen */
2081 break;
2082#ifdef FEAT_FOLDING
2083 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2084#else
2085 ++lnum;
2086#endif
2087#ifdef FEAT_SYN_HL
2088 did_update = DID_NONE;
2089#endif
2090 }
2091
2092 if (lnum > buf->b_ml.ml_line_count)
2093 {
2094 eof = TRUE;
2095 break;
2096 }
2097 }
2098 /*
2099 * End of loop over all window lines.
2100 */
2101
2102
2103 if (idx > wp->w_lines_valid)
2104 wp->w_lines_valid = idx;
2105
2106#ifdef FEAT_SYN_HL
2107 /*
2108 * Let the syntax stuff know we stop parsing here.
2109 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002110 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 syntax_end_parsing(syntax_last_parsed + 1);
2112#endif
2113
2114 /*
2115 * If we didn't hit the end of the file, and we didn't finish the last
2116 * line we were working on, then the line didn't fit.
2117 */
2118 wp->w_empty_rows = 0;
2119#ifdef FEAT_DIFF
2120 wp->w_filler_rows = 0;
2121#endif
2122 if (!eof && !didline)
2123 {
2124 if (lnum == wp->w_topline)
2125 {
2126 /*
2127 * Single line that does not fit!
2128 * Don't overwrite it, it can be edited.
2129 */
2130 wp->w_botline = lnum + 1;
2131 }
2132#ifdef FEAT_DIFF
2133 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2134 {
2135 /* Window ends in filler lines. */
2136 wp->w_botline = lnum;
2137 wp->w_filler_rows = wp->w_height - srow;
2138 }
2139#endif
2140 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2141 {
2142 /*
2143 * Last line isn't finished: Display "@@@" at the end.
2144 */
2145 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2146 W_WINROW(wp) + wp->w_height,
2147 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2148 '@', '@', hl_attr(HLF_AT));
2149 set_empty_rows(wp, srow);
2150 wp->w_botline = lnum;
2151 }
2152 else
2153 {
2154 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2155 wp->w_botline = lnum;
2156 }
2157 }
2158 else
2159 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002160#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 draw_vsep_win(wp, row);
2162#endif
2163 if (eof) /* we hit the end of the file */
2164 {
2165 wp->w_botline = buf->b_ml.ml_line_count + 1;
2166#ifdef FEAT_DIFF
2167 j = diff_check_fill(wp, wp->w_botline);
2168 if (j > 0 && !wp->w_botfill)
2169 {
2170 /*
2171 * Display filler lines at the end of the file
2172 */
2173 if (char2cells(fill_diff) > 1)
2174 i = '-';
2175 else
2176 i = fill_diff;
2177 if (row + j > wp->w_height)
2178 j = wp->w_height - row;
2179 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2180 row += j;
2181 }
2182#endif
2183 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002184 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 wp->w_botline = lnum;
2186
2187 /* make sure the rest of the screen is blank */
2188 /* put '~'s on rows that aren't part of the file. */
2189 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2190 }
2191
2192 /* Reset the type of redrawing required, the window has been updated. */
2193 wp->w_redr_type = 0;
2194#ifdef FEAT_DIFF
2195 wp->w_old_topfill = wp->w_topfill;
2196 wp->w_old_botfill = wp->w_botfill;
2197#endif
2198
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002199 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 /*
2202 * There is a trick with w_botline. If we invalidate it on each
2203 * change that might modify it, this will cause a lot of expensive
2204 * calls to plines() in update_topline() each time. Therefore the
2205 * value of w_botline is often approximated, and this value is used to
2206 * compute the value of w_topline. If the value of w_botline was
2207 * wrong, check that the value of w_topline is correct (cursor is on
2208 * the visible part of the text). If it's not, we need to redraw
2209 * again. Mostly this just means scrolling up a few lines, so it
2210 * doesn't look too bad. Only do this for the current window (where
2211 * changes are relevant).
2212 */
2213 wp->w_valid |= VALID_BOTLINE;
2214 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2215 {
2216 recursive = TRUE;
2217 curwin->w_valid &= ~VALID_TOPLINE;
2218 update_topline(); /* may invalidate w_botline again */
2219 if (must_redraw != 0)
2220 {
2221 /* Don't update for changes in buffer again. */
2222 i = curbuf->b_mod_set;
2223 curbuf->b_mod_set = FALSE;
2224 win_update(curwin);
2225 must_redraw = 0;
2226 curbuf->b_mod_set = i;
2227 }
2228 recursive = FALSE;
2229 }
2230 }
2231
2232#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2233 /* restore got_int, unless CTRL-C was hit while redrawing */
2234 if (!got_int)
2235 got_int = save_got_int;
2236#endif
2237}
2238
2239#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002240static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
2242/*
2243 * Return TRUE when window "wp" has a column to draw signs in.
2244 */
2245 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002246draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
2248 return (wp->w_buffer->b_signlist != NULL
2249# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002250 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251# endif
2252 );
2253}
2254#endif
2255
2256/*
2257 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2258 * as the filler character.
2259 */
2260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002261win_draw_end(
2262 win_T *wp,
2263 int c1,
2264 int c2,
2265 int row,
2266 int endrow,
2267 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2270 int n = 0;
2271# define FDC_OFF n
2272#else
2273# define FDC_OFF 0
2274#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002275#ifdef FEAT_FOLDING
2276 int fdc = compute_foldcolumn(wp, 0);
2277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279#ifdef FEAT_RIGHTLEFT
2280 if (wp->w_p_rl)
2281 {
2282 /* No check for cmdline window: should never be right-left. */
2283# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002284 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 if (n > 0)
2287 {
2288 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002289 if (n > W_WIDTH(wp))
2290 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2292 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2293 ' ', ' ', hl_attr(HLF_FC));
2294 }
2295# endif
2296# ifdef FEAT_SIGNS
2297 if (draw_signcolumn(wp))
2298 {
2299 int nn = n + 2;
2300
2301 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002302 if (nn > W_WIDTH(wp))
2303 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2305 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2306 ' ', ' ', hl_attr(HLF_SC));
2307 n = nn;
2308 }
2309# endif
2310 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2311 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2312 c2, c2, hl_attr(hl));
2313 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2314 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2315 c1, c2, hl_attr(hl));
2316 }
2317 else
2318#endif
2319 {
2320#ifdef FEAT_CMDWIN
2321 if (cmdwin_type != 0 && wp == curwin)
2322 {
2323 /* draw the cmdline character in the leftmost column */
2324 n = 1;
2325 if (n > wp->w_width)
2326 n = wp->w_width;
2327 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2328 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2329 cmdwin_type, ' ', hl_attr(HLF_AT));
2330 }
2331#endif
2332#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002333 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002335 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /* draw the fold column at the left */
2338 if (nn > W_WIDTH(wp))
2339 nn = W_WIDTH(wp);
2340 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2341 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2342 ' ', ' ', hl_attr(HLF_FC));
2343 n = nn;
2344 }
2345#endif
2346#ifdef FEAT_SIGNS
2347 if (draw_signcolumn(wp))
2348 {
2349 int nn = n + 2;
2350
2351 /* draw the sign column after the fold column */
2352 if (nn > W_WIDTH(wp))
2353 nn = W_WIDTH(wp);
2354 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2355 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2356 ' ', ' ', hl_attr(HLF_SC));
2357 n = nn;
2358 }
2359#endif
2360 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2361 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2362 c1, c2, hl_attr(hl));
2363 }
2364 set_empty_rows(wp, row);
2365}
2366
Bram Moolenaar1a384422010-07-14 19:53:30 +02002367#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002368static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002369
2370/*
2371 * Advance **color_cols and return TRUE when there are columns to draw.
2372 */
2373 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002374advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002375{
2376 while (**color_cols >= 0 && vcol > **color_cols)
2377 ++*color_cols;
2378 return (**color_cols >= 0);
2379}
2380#endif
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_FOLDING
2383/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002384 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2385 * space is available for window "wp", minus "col".
2386 */
2387 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002388compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002389{
2390 int fdc = wp->w_p_fdc;
2391 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2392 int wwidth = W_WIDTH(wp);
2393
2394 if (fdc > wwidth - (col + wmw))
2395 fdc = wwidth - (col + wmw);
2396 return fdc;
2397}
2398
2399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 * Display one folded line.
2401 */
2402 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002403fold_line(
2404 win_T *wp,
2405 long fold_count,
2406 foldinfo_T *foldinfo,
2407 linenr_T lnum,
2408 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 char_u buf[51];
2411 pos_T *top, *bot;
2412 linenr_T lnume = lnum + fold_count - 1;
2413 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002414 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 int col;
2417 int txtcol;
2418 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002419 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 /* Build the fold line:
2422 * 1. Add the cmdwin_type for the command-line window
2423 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002424 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * 4. Compose the text
2426 * 5. Add the text
2427 * 6. set highlighting for the Visual area an other text
2428 */
2429 col = 0;
2430
2431 /*
2432 * 1. Add the cmdwin_type for the command-line window
2433 * Ignores 'rightleft', this window is never right-left.
2434 */
2435#ifdef FEAT_CMDWIN
2436 if (cmdwin_type != 0 && wp == curwin)
2437 {
2438 ScreenLines[off] = cmdwin_type;
2439 ScreenAttrs[off] = hl_attr(HLF_AT);
2440#ifdef FEAT_MBYTE
2441 if (enc_utf8)
2442 ScreenLinesUC[off] = 0;
2443#endif
2444 ++col;
2445 }
2446#endif
2447
2448 /*
2449 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002450 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002452 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (fdc > 0)
2454 {
2455 fill_foldcolumn(buf, wp, TRUE, lnum);
2456#ifdef FEAT_RIGHTLEFT
2457 if (wp->w_p_rl)
2458 {
2459 int i;
2460
2461 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2462 hl_attr(HLF_FC));
2463 /* reverse the fold column */
2464 for (i = 0; i < fdc; ++i)
2465 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2466 }
2467 else
2468#endif
2469 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2470 col += fdc;
2471 }
2472
2473#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002474# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2475 for (ri = 0; ri < l; ++ri) \
2476 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2477 else \
2478 for (ri = 0; ri < l; ++ri) \
2479 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002481# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2482 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#endif
2484
Bram Moolenaar64486672010-05-16 15:46:46 +02002485 /* Set all attributes of the 'number' or 'relativenumber' column and the
2486 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002487 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489#ifdef FEAT_SIGNS
2490 /* If signs are being displayed, add two spaces. */
2491 if (draw_signcolumn(wp))
2492 {
2493 len = W_WIDTH(wp) - col;
2494 if (len > 0)
2495 {
2496 if (len > 2)
2497 len = 2;
2498# ifdef FEAT_RIGHTLEFT
2499 if (wp->w_p_rl)
2500 /* the line number isn't reversed */
2501 copy_text_attr(off + W_WIDTH(wp) - len - col,
2502 (char_u *)" ", len, hl_attr(HLF_FL));
2503 else
2504# endif
2505 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2506 col += len;
2507 }
2508 }
2509#endif
2510
2511 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002512 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002514 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 len = W_WIDTH(wp) - col;
2517 if (len > 0)
2518 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002519 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002520 long num;
2521 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002522
2523 if (len > w + 1)
2524 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002525
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002526 if (wp->w_p_nu && !wp->w_p_rnu)
2527 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002528 num = (long)lnum;
2529 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002530 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002531 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002532 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002533 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002534 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002535 /* 'number' + 'relativenumber': cursor line shows absolute
2536 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002537 num = lnum;
2538 fmt = "%-*ld ";
2539 }
2540 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002541
Bram Moolenaar700e7342013-01-30 12:31:36 +01002542 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef FEAT_RIGHTLEFT
2544 if (wp->w_p_rl)
2545 /* the line number isn't reversed */
2546 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2547 hl_attr(HLF_FL));
2548 else
2549#endif
2550 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2551 col += len;
2552 }
2553 }
2554
2555 /*
2556 * 4. Compose the folded-line string with 'foldtext', if set.
2557 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002558 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 txtcol = col; /* remember where text starts */
2561
2562 /*
2563 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2564 * Right-left text is put in columns 0 - number-col, normal text is put
2565 * in columns number-col - window-width.
2566 */
2567#ifdef FEAT_MBYTE
2568 if (has_mbyte)
2569 {
2570 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 int u8c, u8cc[MAX_MCO];
2572 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 int idx;
2574 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002575 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576# ifdef FEAT_ARABIC
2577 int prev_c = 0; /* previous Arabic character */
2578 int prev_c1 = 0; /* first composing char for prev_c */
2579# endif
2580
2581# ifdef FEAT_RIGHTLEFT
2582 if (wp->w_p_rl)
2583 idx = off;
2584 else
2585# endif
2586 idx = off + col;
2587
2588 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2589 for (p = text; *p != NUL; )
2590 {
2591 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002592 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (col + cells > W_WIDTH(wp)
2594# ifdef FEAT_RIGHTLEFT
2595 - (wp->w_p_rl ? col : 0)
2596# endif
2597 )
2598 break;
2599 ScreenLines[idx] = *p;
2600 if (enc_utf8)
2601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 u8c = utfc_ptr2char(p, u8cc);
2603 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 ScreenLinesUC[idx] = 0;
2606#ifdef FEAT_ARABIC
2607 prev_c = u8c;
2608#endif
2609 }
2610 else
2611 {
2612#ifdef FEAT_ARABIC
2613 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2614 {
2615 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002616 int pc, pc1, nc;
2617 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 int firstbyte = *p;
2619
2620 /* The idea of what is the previous and next
2621 * character depends on 'rightleft'. */
2622 if (wp->w_p_rl)
2623 {
2624 pc = prev_c;
2625 pc1 = prev_c1;
2626 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002627 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 }
2629 else
2630 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002631 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002633 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 prev_c = u8c;
2636
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002637 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 pc, pc1, nc);
2639 ScreenLines[idx] = firstbyte;
2640 }
2641 else
2642 prev_c = u8c;
2643#endif
2644 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002645#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (u8c >= 0x10000)
2647 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2648 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002651 for (i = 0; i < Screen_mco; ++i)
2652 {
2653 ScreenLinesC[i][idx] = u8cc[i];
2654 if (u8cc[i] == 0)
2655 break;
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658 if (cells > 1)
2659 ScreenLines[idx + 1] = 0;
2660 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002661 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2662 /* double-byte single width character */
2663 ScreenLines2[idx] = p[1];
2664 else if (cells > 1)
2665 /* double-width character */
2666 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 col += cells;
2668 idx += cells;
2669 p += c_len;
2670 }
2671 }
2672 else
2673#endif
2674 {
2675 len = (int)STRLEN(text);
2676 if (len > W_WIDTH(wp) - col)
2677 len = W_WIDTH(wp) - col;
2678 if (len > 0)
2679 {
2680#ifdef FEAT_RIGHTLEFT
2681 if (wp->w_p_rl)
2682 STRNCPY(current_ScreenLine, text, len);
2683 else
2684#endif
2685 STRNCPY(current_ScreenLine + col, text, len);
2686 col += len;
2687 }
2688 }
2689
2690 /* Fill the rest of the line with the fold filler */
2691#ifdef FEAT_RIGHTLEFT
2692 if (wp->w_p_rl)
2693 col -= txtcol;
2694#endif
2695 while (col < W_WIDTH(wp)
2696#ifdef FEAT_RIGHTLEFT
2697 - (wp->w_p_rl ? txtcol : 0)
2698#endif
2699 )
2700 {
2701#ifdef FEAT_MBYTE
2702 if (enc_utf8)
2703 {
2704 if (fill_fold >= 0x80)
2705 {
2706 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002707 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 else
2710 ScreenLinesUC[off + col] = 0;
2711 }
2712#endif
2713 ScreenLines[off + col++] = fill_fold;
2714 }
2715
2716 if (text != buf)
2717 vim_free(text);
2718
2719 /*
2720 * 6. set highlighting for the Visual area an other text.
2721 * If all folded lines are in the Visual area, highlight the line.
2722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2724 {
2725 if (ltoreq(curwin->w_cursor, VIsual))
2726 {
2727 /* Visual is after curwin->w_cursor */
2728 top = &curwin->w_cursor;
2729 bot = &VIsual;
2730 }
2731 else
2732 {
2733 /* Visual is before curwin->w_cursor */
2734 top = &VIsual;
2735 bot = &curwin->w_cursor;
2736 }
2737 if (lnum >= top->lnum
2738 && lnume <= bot->lnum
2739 && (VIsual_mode != 'v'
2740 || ((lnum > top->lnum
2741 || (lnum == top->lnum
2742 && top->col == 0))
2743 && (lnume < bot->lnum
2744 || (lnume == bot->lnum
2745 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002746 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
2748 if (VIsual_mode == Ctrl_V)
2749 {
2750 /* Visual block mode: highlight the chars part of the block */
2751 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2752 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002753 if (wp->w_old_cursor_lcol != MAXCOL
2754 && wp->w_old_cursor_lcol + txtcol
2755 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 len = wp->w_old_cursor_lcol;
2757 else
2758 len = W_WIDTH(wp) - txtcol;
2759 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002760 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 }
2763 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002766 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002771#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002772 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2773 if (wp->w_p_cc_cols)
2774 {
2775 int i = 0;
2776 int j = wp->w_p_cc_cols[i];
2777 int old_txtcol = txtcol;
2778
2779 while (j > -1)
2780 {
2781 txtcol += j;
2782 if (wp->w_p_wrap)
2783 txtcol -= wp->w_skipcol;
2784 else
2785 txtcol -= wp->w_leftcol;
2786 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2787 ScreenAttrs[off + txtcol] = hl_combine_attr(
2788 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2789 txtcol = old_txtcol;
2790 j = wp->w_p_cc_cols[++i];
2791 }
2792 }
2793
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002794 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002795 if (wp->w_p_cuc)
2796 {
2797 txtcol += wp->w_virtcol;
2798 if (wp->w_p_wrap)
2799 txtcol -= wp->w_skipcol;
2800 else
2801 txtcol -= wp->w_leftcol;
2802 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2803 ScreenAttrs[off + txtcol] = hl_combine_attr(
2804 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2805 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2809 (int)W_WIDTH(wp), FALSE);
2810
2811 /*
2812 * Update w_cline_height and w_cline_folded if the cursor line was
2813 * updated (saves a call to plines() later).
2814 */
2815 if (wp == curwin
2816 && lnum <= curwin->w_cursor.lnum
2817 && lnume >= curwin->w_cursor.lnum)
2818 {
2819 curwin->w_cline_row = row;
2820 curwin->w_cline_height = 1;
2821 curwin->w_cline_folded = TRUE;
2822 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2823 }
2824}
2825
2826/*
2827 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2828 */
2829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002830copy_text_attr(
2831 int off,
2832 char_u *buf,
2833 int len,
2834 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002836 int i;
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 mch_memmove(ScreenLines + off, buf, (size_t)len);
2839# ifdef FEAT_MBYTE
2840 if (enc_utf8)
2841 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2842# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002843 for (i = 0; i < len; ++i)
2844 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847/*
2848 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002849 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
2851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002852fill_foldcolumn(
2853 char_u *p,
2854 win_T *wp,
2855 int closed, /* TRUE of FALSE */
2856 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 int i = 0;
2859 int level;
2860 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002861 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002862 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002865 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 level = win_foldinfo.fi_level;
2868 if (level > 0)
2869 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002870 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002871 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 /* If the column is too narrow, we start at the lowest level that
2874 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002875 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (first_level < 1)
2877 first_level = 1;
2878
Bram Moolenaar1c934292015-01-27 16:39:29 +01002879 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
2881 if (win_foldinfo.fi_lnum == lnum
2882 && first_level + i >= win_foldinfo.fi_low_level)
2883 p[i] = '-';
2884 else if (first_level == 1)
2885 p[i] = '|';
2886 else if (first_level + i <= 9)
2887 p[i] = '0' + first_level + i;
2888 else
2889 p[i] = '>';
2890 if (first_level + i == level)
2891 break;
2892 }
2893 }
2894 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002895 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897#endif /* FEAT_FOLDING */
2898
2899/*
2900 * Display line "lnum" of window 'wp' on the screen.
2901 * Start at row "startrow", stop when "endrow" is reached.
2902 * wp->w_virtcol needs to be valid.
2903 *
2904 * Return the number of last row the line occupies.
2905 */
2906 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002907win_line(
2908 win_T *wp,
2909 linenr_T lnum,
2910 int startrow,
2911 int endrow,
2912 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 int col; /* visual column on screen */
2915 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2916 int c = 0; /* init for GCC */
2917 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002918#ifdef FEAT_LINEBREAK
2919 long vcol_sbr = -1; /* virtual column after showbreak */
2920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 long vcol_prev = -1; /* "vcol" of previous character */
2922 char_u *line; /* current line */
2923 char_u *ptr; /* current position in "line" */
2924 int row; /* row in the window, excl w_winrow */
2925 int screen_row; /* row on the screen, incl w_winrow */
2926
2927 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2928 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002929 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002930 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 int c_extra = NUL; /* extra chars, all the same */
2932 int extra_attr = 0; /* attributes when n_extra != 0 */
2933 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2934 displaying lcs_eol at end-of-line */
2935 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2936 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2937
2938 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2939 int saved_n_extra = 0;
2940 char_u *saved_p_extra = NULL;
2941 int saved_c_extra = 0;
2942 int saved_char_attr = 0;
2943
2944 int n_attr = 0; /* chars with special attr */
2945 int saved_attr2 = 0; /* char_attr saved for n_attr */
2946 int n_attr3 = 0; /* chars with overruling special attr */
2947 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2948
2949 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2950
2951 int fromcol, tocol; /* start/end of inverting */
2952 int fromcol_prev = -2; /* start of inverting after cursor */
2953 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002955 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 pos_T pos;
2957 long v;
2958
2959 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002960 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2962 in this line */
2963 int attr = 0; /* attributes for area highlighting */
2964 int area_attr = 0; /* attributes desired by highlighting */
2965 int search_attr = 0; /* attributes desired by 'hlsearch' */
2966#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002967 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 int syntax_attr = 0; /* attributes desired by syntax */
2969 int has_syntax = FALSE; /* this buffer has syntax highl. */
2970 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002971 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002972 int draw_color_col = FALSE; /* highlight colorcolumn */
2973 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002974#endif
2975#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002976 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002977# define SPWORDLEN 150
2978 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002979 int nextlinecol = 0; /* column where nextline[] starts */
2980 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002981 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002982 int spell_attr = 0; /* attributes desired by spelling */
2983 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002984 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2985 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002986 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002987 static int cap_col = -1; /* column to check for Cap word */
2988 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002989 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#endif
2991 int extra_check; /* has syntax or linebreak */
2992#ifdef FEAT_MBYTE
2993 int multi_attr = 0; /* attributes desired by multibyte */
2994 int mb_l = 1; /* multi-byte byte length */
2995 int mb_c = 0; /* decoded multi-byte character */
2996 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002997 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
2999#ifdef FEAT_DIFF
3000 int filler_lines; /* nr of filler lines to be drawn */
3001 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003002 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 int change_start = MAXCOL; /* first col of changed area */
3004 int change_end = -1; /* last col of changed area */
3005#endif
3006 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3007#ifdef FEAT_LINEBREAK
3008 int need_showbreak = FALSE;
3009#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003010#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
3011 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003013 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#endif
3015#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003016 matchitem_T *cur; /* points to the match list */
3017 match_T *shl; /* points to search_hl or a match */
3018 int shl_flag; /* flag to indicate whether search_hl
3019 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003020 int pos_inprogress; /* marks that position match search is
3021 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003022 int prevcol_hl_flag; /* flag to indicate whether prevcol
3023 equals startcol of search_hl or one
3024 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025#endif
3026#ifdef FEAT_ARABIC
3027 int prev_c = 0; /* previous Arabic character */
3028 int prev_c1 = 0; /* first composing char for prev_c */
3029#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003030#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003031 int did_line_attr = 0;
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 /* draw_state: items that are drawn in sequence: */
3035#define WL_START 0 /* nothing done yet */
3036#ifdef FEAT_CMDWIN
3037# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3038#else
3039# define WL_CMDLINE WL_START
3040#endif
3041#ifdef FEAT_FOLDING
3042# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3043#else
3044# define WL_FOLD WL_CMDLINE
3045#endif
3046#ifdef FEAT_SIGNS
3047# define WL_SIGN WL_FOLD + 1 /* column for signs */
3048#else
3049# define WL_SIGN WL_FOLD /* column for signs */
3050#endif
3051#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003052#ifdef FEAT_LINEBREAK
3053# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003055# define WL_BRI WL_NR
3056#endif
3057#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3058# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3059#else
3060# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#endif
3062#define WL_LINE WL_SBR + 1 /* text in the line */
3063 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003064#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 int feedback_col = 0;
3066 int feedback_old_attr = -1;
3067#endif
3068
Bram Moolenaar860cae12010-06-05 23:22:07 +02003069#ifdef FEAT_CONCEAL
3070 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003071 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003072 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003073 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003074 int is_concealing = FALSE;
3075 int boguscols = 0; /* nonexistent columns added to force
3076 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003077 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003078 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003079 int match_conc = 0; /* cchar for match functions */
3080 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003081 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003082# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003083# define FIX_FOR_BOGUSCOLS \
3084 { \
3085 n_extra += vcol_off; \
3086 vcol -= vcol_off; \
3087 vcol_off = 0; \
3088 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003089 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003090 boguscols = 0; \
3091 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003092#else
3093# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 if (startrow > endrow) /* past the end already! */
3097 return startrow;
3098
3099 row = startrow;
3100 screen_row = row + W_WINROW(wp);
3101
3102 /*
3103 * To speed up the loop below, set extra_check when there is linebreak,
3104 * trailing white space and/or syntax processing to be done.
3105 */
3106#ifdef FEAT_LINEBREAK
3107 extra_check = wp->w_p_lbr;
3108#else
3109 extra_check = 0;
3110#endif
3111#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003112 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
3114 /* Prepare for syntax highlighting in this line. When there is an
3115 * error, stop syntax highlighting. */
3116 save_did_emsg = did_emsg;
3117 did_emsg = FALSE;
3118 syntax_start(wp, lnum);
3119 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003120 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else
3122 {
3123 did_emsg = save_did_emsg;
3124 has_syntax = TRUE;
3125 extra_check = TRUE;
3126 }
3127 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003128
3129 /* Check for columns to display for 'colorcolumn'. */
3130 color_cols = wp->w_p_cc_cols;
3131 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003132 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003133#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003134
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003135#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003136 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003137 && *wp->w_s->b_p_spl != NUL
3138 && wp->w_s->b_langp.ga_len > 0
3139 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140 {
3141 /* Prepare for spell checking. */
3142 has_spell = TRUE;
3143 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003144
3145 /* Get the start of the next line, so that words that wrap to the next
3146 * line are found too: "et<line-break>al.".
3147 * Trick: skip a few chars for C/shell/Vim comments */
3148 nextline[SPWORDLEN] = NUL;
3149 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3150 {
3151 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3152 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3153 }
3154
3155 /* When a word wrapped from the previous line the start of the current
3156 * line is valid. */
3157 if (lnum == checked_lnum)
3158 cur_checked_col = checked_col;
3159 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003160
3161 /* When there was a sentence end in the previous line may require a
3162 * word starting with capital in this line. In line 1 always check
3163 * the first word. */
3164 if (lnum != capcol_lnum)
3165 cap_col = -1;
3166 if (lnum == 1)
3167 cap_col = 0;
3168 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
3171
3172 /*
3173 * handle visual active in this window
3174 */
3175 fromcol = -10;
3176 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3178 {
3179 /* Visual is after curwin->w_cursor */
3180 if (ltoreq(curwin->w_cursor, VIsual))
3181 {
3182 top = &curwin->w_cursor;
3183 bot = &VIsual;
3184 }
3185 else /* Visual is before curwin->w_cursor */
3186 {
3187 top = &VIsual;
3188 bot = &curwin->w_cursor;
3189 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003190 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 if (VIsual_mode == Ctrl_V) /* block mode */
3192 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003193 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 fromcol = wp->w_old_cursor_fcol;
3196 tocol = wp->w_old_cursor_lcol;
3197 }
3198 }
3199 else /* non-block mode */
3200 {
3201 if (lnum > top->lnum && lnum <= bot->lnum)
3202 fromcol = 0;
3203 else if (lnum == top->lnum)
3204 {
3205 if (VIsual_mode == 'V') /* linewise */
3206 fromcol = 0;
3207 else
3208 {
3209 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3210 if (gchar_pos(top) == NUL)
3211 tocol = fromcol + 1;
3212 }
3213 }
3214 if (VIsual_mode != 'V' && lnum == bot->lnum)
3215 {
3216 if (*p_sel == 'e' && bot->col == 0
3217#ifdef FEAT_VIRTUALEDIT
3218 && bot->coladd == 0
3219#endif
3220 )
3221 {
3222 fromcol = -10;
3223 tocol = MAXCOL;
3224 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003225 else if (bot->col == MAXCOL)
3226 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 else
3228 {
3229 pos = *bot;
3230 if (*p_sel == 'e')
3231 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3232 else
3233 {
3234 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3235 ++tocol;
3236 }
3237 }
3238 }
3239 }
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /* Check if the character under the cursor should not be inverted */
3242 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003243#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 )
3247 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249 /* if inverting in this line set area_highlighting */
3250 if (fromcol >= 0)
3251 {
3252 area_highlighting = TRUE;
3253 attr = hl_attr(HLF_V);
3254#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003255 if ((clip_star.available && !clip_star.owned
3256 && clip_isautosel_star())
3257 || (clip_plus.available && !clip_plus.owned
3258 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 attr = hl_attr(HLF_VNC);
3260#endif
3261 }
3262 }
3263
3264 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003265 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003267 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 && wp == curwin
3269 && lnum >= curwin->w_cursor.lnum
3270 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3271 {
3272 if (lnum == curwin->w_cursor.lnum)
3273 getvcol(curwin, &(curwin->w_cursor),
3274 (colnr_T *)&fromcol, NULL, NULL);
3275 else
3276 fromcol = 0;
3277 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3278 {
3279 pos.lnum = lnum;
3280 pos.col = search_match_endcol;
3281 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3282 }
3283 else
3284 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003285 /* do at least one character; happens when past end of line */
3286 if (fromcol == tocol)
3287 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 area_highlighting = TRUE;
3289 attr = hl_attr(HLF_I);
3290 }
3291
3292#ifdef FEAT_DIFF
3293 filler_lines = diff_check(wp, lnum);
3294 if (filler_lines < 0)
3295 {
3296 if (filler_lines == -1)
3297 {
3298 if (diff_find_change(wp, lnum, &change_start, &change_end))
3299 diff_hlf = HLF_ADD; /* added line */
3300 else if (change_start == 0)
3301 diff_hlf = HLF_TXD; /* changed text */
3302 else
3303 diff_hlf = HLF_CHD; /* changed line */
3304 }
3305 else
3306 diff_hlf = HLF_ADD; /* added line */
3307 filler_lines = 0;
3308 area_highlighting = TRUE;
3309 }
3310 if (lnum == wp->w_topline)
3311 filler_lines = wp->w_topfill;
3312 filler_todo = filler_lines;
3313#endif
3314
3315#ifdef LINE_ATTR
3316# ifdef FEAT_SIGNS
3317 /* If this line has a sign with line highlighting set line_attr. */
3318 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3319 if (v != 0)
3320 line_attr = sign_get_attr((int)v, TRUE);
3321# endif
3322# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3323 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003324 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 line_attr = hl_attr(HLF_L);
3326# endif
3327 if (line_attr != 0)
3328 area_highlighting = TRUE;
3329#endif
3330
3331 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3332 ptr = line;
3333
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003334#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003335 if (has_spell)
3336 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003337 /* For checking first word with a capital skip white space. */
3338 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003339 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003340
Bram Moolenaar30abd282005-06-22 22:35:10 +00003341 /* To be able to spell-check over line boundaries copy the end of the
3342 * current line into nextline[]. Above the start of the next line was
3343 * copied to nextline[SPWORDLEN]. */
3344 if (nextline[SPWORDLEN] == NUL)
3345 {
3346 /* No next line or it is empty. */
3347 nextlinecol = MAXCOL;
3348 nextline_idx = 0;
3349 }
3350 else
3351 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003352 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003353 if (v < SPWORDLEN)
3354 {
3355 /* Short line, use it completely and append the start of the
3356 * next line. */
3357 nextlinecol = 0;
3358 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003359 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003360 nextline_idx = v + 1;
3361 }
3362 else
3363 {
3364 /* Long line, use only the last SPWORDLEN bytes. */
3365 nextlinecol = v - SPWORDLEN;
3366 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3367 nextline_idx = SPWORDLEN + 1;
3368 }
3369 }
3370 }
3371#endif
3372
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003373 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003375 if (lcs_space || lcs_trail)
3376 extra_check = TRUE;
3377 /* find start of trailing whitespace */
3378 if (lcs_trail)
3379 {
3380 trailcol = (colnr_T)STRLEN(ptr);
3381 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3382 --trailcol;
3383 trailcol += (colnr_T) (ptr - line);
3384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386
3387 /*
3388 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3389 * first character to be displayed.
3390 */
3391 if (wp->w_p_wrap)
3392 v = wp->w_skipcol;
3393 else
3394 v = wp->w_leftcol;
3395 if (v > 0)
3396 {
3397#ifdef FEAT_MBYTE
3398 char_u *prev_ptr = ptr;
3399#endif
3400 while (vcol < v && *ptr != NUL)
3401 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003402 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 vcol += c;
3404#ifdef FEAT_MBYTE
3405 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003407 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003410 /* When:
3411 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003412 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003413 * - 'virtualedit' is set, or
3414 * - the visual mode is active,
3415 * the end of the line may be before the start of the displayed part.
3416 */
3417 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003418#ifdef FEAT_SYN_HL
3419 wp->w_p_cuc || draw_color_col ||
3420#endif
3421#ifdef FEAT_VIRTUALEDIT
3422 virtual_active() ||
3423#endif
3424 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 /* Handle a character that's not completely on the screen: Put ptr at
3430 * that character but skip the first few screen characters. */
3431 if (vcol > v)
3432 {
3433 vcol -= c;
3434#ifdef FEAT_MBYTE
3435 ptr = prev_ptr;
3436#else
3437 --ptr;
3438#endif
3439 n_skip = v - vcol;
3440 }
3441
3442 /*
3443 * Adjust for when the inverted text is before the screen,
3444 * and when the start of the inverted text is before the screen.
3445 */
3446 if (tocol <= vcol)
3447 fromcol = 0;
3448 else if (fromcol >= 0 && fromcol < vcol)
3449 fromcol = vcol;
3450
3451#ifdef FEAT_LINEBREAK
3452 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3453 if (wp->w_p_wrap)
3454 need_showbreak = TRUE;
3455#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003456#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003457 /* When spell checking a word we need to figure out the start of the
3458 * word and if it's badly spelled or not. */
3459 if (has_spell)
3460 {
3461 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003462 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003463 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003464
3465 pos = wp->w_cursor;
3466 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003467 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003468 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003469
3470 /* spell_move_to() may call ml_get() and make "line" invalid */
3471 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3472 ptr = line + linecol;
3473
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003474 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003475 {
3476 /* no bad word found at line start, don't check until end of a
3477 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003478 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003479 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003480 }
3481 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003482 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003483 /* bad word found, use attributes until end of word */
3484 word_end = wp->w_cursor.col + len + 1;
3485
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003486 /* Turn index into actual attributes. */
3487 if (spell_hlf != HLF_COUNT)
3488 spell_attr = highlight_attr[spell_hlf];
3489 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003490 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003491
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003492# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003493 /* Need to restart syntax highlighting for this line. */
3494 if (has_syntax)
3495 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003496# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003497 }
3498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500
3501 /*
3502 * Correct highlighting for cursor that can't be disabled.
3503 * Avoids having to check this for each character.
3504 */
3505 if (fromcol >= 0)
3506 {
3507 if (noinvcur)
3508 {
3509 if ((colnr_T)fromcol == wp->w_virtcol)
3510 {
3511 /* highlighting starts at cursor, let it start just after the
3512 * cursor */
3513 fromcol_prev = fromcol;
3514 fromcol = -1;
3515 }
3516 else if ((colnr_T)fromcol < wp->w_virtcol)
3517 /* restart highlighting after the cursor */
3518 fromcol_prev = wp->w_virtcol;
3519 }
3520 if (fromcol >= tocol)
3521 fromcol = -1;
3522 }
3523
3524#ifdef FEAT_SEARCH_EXTRA
3525 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003526 * Handle highlighting the last used search pattern and matches.
3527 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003529 cur = wp->w_match_head;
3530 shl_flag = FALSE;
3531 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003533 if (shl_flag == FALSE)
3534 {
3535 shl = &search_hl;
3536 shl_flag = TRUE;
3537 }
3538 else
3539 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003540 shl->startcol = MAXCOL;
3541 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003543 v = (long)(ptr - line);
3544 if (cur != NULL)
3545 cur->pos.cur = 0;
3546 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3547
3548 /* Need to get the line again, a multi-line regexp may have made it
3549 * invalid. */
3550 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3551 ptr = line + v;
3552
3553 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003555 if (shl->lnum == lnum)
3556 shl->startcol = shl->rm.startpos[0].col;
3557 else
3558 shl->startcol = 0;
3559 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3560 - shl->rm.startpos[0].lnum)
3561 shl->endcol = shl->rm.endpos[0].col;
3562 else
3563 shl->endcol = MAXCOL;
3564 /* Highlight one character for an empty match. */
3565 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003568 if (has_mbyte && line[shl->endcol] != NUL)
3569 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003572 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003574 if ((long)shl->startcol < v) /* match at leftcol */
3575 {
3576 shl->attr_cur = shl->attr;
3577 search_attr = shl->attr;
3578 }
3579 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003581 if (shl != &search_hl && cur != NULL)
3582 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
3584#endif
3585
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003586#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003587 /* Cursor line highlighting for 'cursorline' in the current window. Not
3588 * when Visual mode is active, because it's not clear what is selected
3589 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003590 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003591 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003592 {
3593 line_attr = hl_attr(HLF_CUL);
3594 area_highlighting = TRUE;
3595 }
3596#endif
3597
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003598 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 col = 0;
3600#ifdef FEAT_RIGHTLEFT
3601 if (wp->w_p_rl)
3602 {
3603 /* Rightleft window: process the text in the normal direction, but put
3604 * it in current_ScreenLine[] from right to left. Start at the
3605 * rightmost column of the window. */
3606 col = W_WIDTH(wp) - 1;
3607 off += col;
3608 }
3609#endif
3610
3611 /*
3612 * Repeat for the whole displayed line.
3613 */
3614 for (;;)
3615 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003616#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003617 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /* Skip this quickly when working on the text. */
3620 if (draw_state != WL_LINE)
3621 {
3622#ifdef FEAT_CMDWIN
3623 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3624 {
3625 draw_state = WL_CMDLINE;
3626 if (cmdwin_type != 0 && wp == curwin)
3627 {
3628 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003630 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 char_attr = hl_attr(HLF_AT);
3632 }
3633 }
3634#endif
3635
3636#ifdef FEAT_FOLDING
3637 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3638 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003639 int fdc = compute_foldcolumn(wp, 0);
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003642 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 /* Draw the 'foldcolumn'. */
3645 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003646 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003648 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 c_extra = NUL;
3650 char_attr = hl_attr(HLF_FC);
3651 }
3652 }
3653#endif
3654
3655#ifdef FEAT_SIGNS
3656 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3657 {
3658 draw_state = WL_SIGN;
3659 /* Show the sign column when there are any signs in this
3660 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003661 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003663 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003665 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666# endif
3667
3668 /* Draw two cells with the sign value or blank. */
3669 c_extra = ' ';
3670 char_attr = hl_attr(HLF_SC);
3671 n_extra = 2;
3672
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003673 if (row == startrow
3674#ifdef FEAT_DIFF
3675 + filler_lines && filler_todo <= 0
3676#endif
3677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
3679 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3680 SIGN_TEXT);
3681# ifdef FEAT_SIGN_ICONS
3682 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3683 SIGN_ICON);
3684 if (gui.in_use && icon_sign != 0)
3685 {
3686 /* Use the image in this position. */
3687 c_extra = SIGN_BYTE;
3688# ifdef FEAT_NETBEANS_INTG
3689 if (buf_signcount(wp->w_buffer, lnum) > 1)
3690 c_extra = MULTISIGN_BYTE;
3691# endif
3692 char_attr = icon_sign;
3693 }
3694 else
3695# endif
3696 if (text_sign != 0)
3697 {
3698 p_extra = sign_get_text(text_sign);
3699 if (p_extra != NULL)
3700 {
3701 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704 char_attr = sign_get_attr(text_sign, FALSE);
3705 }
3706 }
3707 }
3708 }
3709#endif
3710
3711 if (draw_state == WL_NR - 1 && n_extra == 0)
3712 {
3713 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003714 /* Display the absolute or relative line number. After the
3715 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3716 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 && (row == startrow
3718#ifdef FEAT_DIFF
3719 + filler_lines
3720#endif
3721 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3722 {
3723 /* Draw the line number (empty space after wrapping). */
3724 if (row == startrow
3725#ifdef FEAT_DIFF
3726 + filler_lines
3727#endif
3728 )
3729 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003730 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003731 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003732
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003733 if (wp->w_p_nu && !wp->w_p_rnu)
3734 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003735 num = (long)lnum;
3736 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003737 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003738 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003739 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003740 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003741 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003742 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003743 num = lnum;
3744 fmt = "%-*ld ";
3745 }
3746 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003747
Bram Moolenaar700e7342013-01-30 12:31:36 +01003748 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003749 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (wp->w_skipcol > 0)
3751 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3752 *p_extra = '-';
3753#ifdef FEAT_RIGHTLEFT
3754 if (wp->w_p_rl) /* reverse line numbers */
3755 rl_mirror(extra);
3756#endif
3757 p_extra = extra;
3758 c_extra = NUL;
3759 }
3760 else
3761 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003762 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003764#ifdef FEAT_SYN_HL
3765 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003766 * the current line differently.
3767 * TODO: Can we use CursorLine instead of CursorLineNr
3768 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003769 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003770 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003771 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
3774 }
3775
Bram Moolenaar597a4222014-06-25 14:39:50 +02003776#ifdef FEAT_LINEBREAK
3777 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3778 && n_extra == 0 && *p_sbr != NUL)
3779 /* draw indent after showbreak value */
3780 draw_state = WL_BRI;
3781 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3782 /* After the showbreak, draw the breakindent */
3783 draw_state = WL_BRI - 1;
3784
3785 /* draw 'breakindent': indent wrapped text accordingly */
3786 if (draw_state == WL_BRI - 1 && n_extra == 0)
3787 {
3788 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003789 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003790# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003791 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003792# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003793 )
3794 {
3795 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003796# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003797 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003798 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003799 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003800# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003801 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3802 char_attr = hl_combine_attr(char_attr,
3803 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003804# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003805 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003806# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003807 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003808 c_extra = ' ';
3809 n_extra = get_breakindent_win(wp,
3810 ml_get_buf(wp->w_buffer, lnum, FALSE));
3811 /* Correct end of highlighted area for 'breakindent',
3812 * required when 'linebreak' is also set. */
3813 if (tocol == vcol)
3814 tocol += n_extra;
3815 }
3816 }
3817#endif
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3820 if (draw_state == WL_SBR - 1 && n_extra == 0)
3821 {
3822 draw_state = WL_SBR;
3823# ifdef FEAT_DIFF
3824 if (filler_todo > 0)
3825 {
3826 /* Draw "deleted" diff line(s). */
3827 if (char2cells(fill_diff) > 1)
3828 c_extra = '-';
3829 else
3830 c_extra = fill_diff;
3831# ifdef FEAT_RIGHTLEFT
3832 if (wp->w_p_rl)
3833 n_extra = col + 1;
3834 else
3835# endif
3836 n_extra = W_WIDTH(wp) - col;
3837 char_attr = hl_attr(HLF_DED);
3838 }
3839# endif
3840# ifdef FEAT_LINEBREAK
3841 if (*p_sbr != NUL && need_showbreak)
3842 {
3843 /* Draw 'showbreak' at the start of each broken line. */
3844 p_extra = p_sbr;
3845 c_extra = NUL;
3846 n_extra = (int)STRLEN(p_sbr);
3847 char_attr = hl_attr(HLF_AT);
3848 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003849 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 /* Correct end of highlighted area for 'showbreak',
3851 * required when 'linebreak' is also set. */
3852 if (tocol == vcol)
3853 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003854#ifdef FEAT_SYN_HL
3855 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003856 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003857 char_attr = hl_combine_attr(char_attr,
3858 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861# endif
3862 }
3863#endif
3864
3865 if (draw_state == WL_LINE - 1 && n_extra == 0)
3866 {
3867 draw_state = WL_LINE;
3868 if (saved_n_extra)
3869 {
3870 /* Continue item from end of wrapped line. */
3871 n_extra = saved_n_extra;
3872 c_extra = saved_c_extra;
3873 p_extra = saved_p_extra;
3874 char_attr = saved_char_attr;
3875 }
3876 else
3877 char_attr = 0;
3878 }
3879 }
3880
3881 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003882 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003883 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884#ifdef FEAT_DIFF
3885 && filler_todo <= 0
3886#endif
3887 )
3888 {
3889 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3890 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003891 /* Pretend we have finished updating the window. Except when
3892 * 'cursorcolumn' is set. */
3893#ifdef FEAT_SYN_HL
3894 if (wp->w_p_cuc)
3895 row = wp->w_cline_row + wp->w_cline_height;
3896 else
3897#endif
3898 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 break;
3900 }
3901
3902 if (draw_state == WL_LINE && area_highlighting)
3903 {
3904 /* handle Visual or match highlighting in this line */
3905 if (vcol == fromcol
3906#ifdef FEAT_MBYTE
3907 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3908 && (*mb_ptr2cells)(ptr) > 1)
3909#endif
3910 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003911 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 && vcol < tocol))
3913 area_attr = attr; /* start highlighting */
3914 else if (area_attr != 0
3915 && (vcol == tocol
3916 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919#ifdef FEAT_SEARCH_EXTRA
3920 if (!n_extra)
3921 {
3922 /*
3923 * Check for start/end of search pattern match.
3924 * After end, check for start/end of next match.
3925 * When another match, have to check for start again.
3926 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003927 * Do this for 'search_hl' and the match list (ordered by
3928 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003930 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003931 cur = wp->w_match_head;
3932 shl_flag = FALSE;
3933 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003935 if (shl_flag == FALSE
3936 && ((cur != NULL
3937 && cur->priority > SEARCH_HL_PRIORITY)
3938 || cur == NULL))
3939 {
3940 shl = &search_hl;
3941 shl_flag = TRUE;
3942 }
3943 else
3944 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003945 if (cur != NULL)
3946 cur->pos.cur = 0;
3947 pos_inprogress = TRUE;
3948 while (shl->rm.regprog != NULL
3949 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003951 if (shl->startcol != MAXCOL
3952 && v >= (long)shl->startcol
3953 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003955#ifdef FEAT_MBYTE
3956 int tmp_col = v + MB_PTR2LEN(ptr);
3957
3958 if (shl->endcol < tmp_col)
3959 shl->endcol = tmp_col;
3960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003962#ifdef FEAT_CONCEAL
3963 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3964 == cur->hlg_id)
3965 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003966 has_match_conc =
3967 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003968 match_conc = cur->conceal_char;
3969 }
3970 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003971 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003974 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
3976 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003977#ifdef FEAT_CONCEAL
3978 prev_syntax_id = 0;
3979#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003980 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3981 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003982 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984 /* Need to get the line again, a multi-line regexp
3985 * may have made it invalid. */
3986 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3987 ptr = line + v;
3988
3989 if (shl->lnum == lnum)
3990 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003991 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003993 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003997 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
3999 /* highlight empty match, try again after
4000 * it */
4001#ifdef FEAT_MBYTE
4002 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004003 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004004 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 else
4006#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004007 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009
4010 /* Loop to check if the match starts at the
4011 * current position */
4012 continue;
4013 }
4014 }
4015 break;
4016 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004017 if (shl != &search_hl && cur != NULL)
4018 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004020
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004021 /* Use attributes from match with highest priority among
4022 * 'search_hl' and the match list. */
4023 search_attr = search_hl.attr_cur;
4024 cur = wp->w_match_head;
4025 shl_flag = FALSE;
4026 while (cur != NULL || shl_flag == FALSE)
4027 {
4028 if (shl_flag == FALSE
4029 && ((cur != NULL
4030 && cur->priority > SEARCH_HL_PRIORITY)
4031 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004032 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004033 shl = &search_hl;
4034 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004035 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004036 else
4037 shl = &cur->hl;
4038 if (shl->attr_cur != 0)
4039 search_attr = shl->attr_cur;
4040 if (shl != &search_hl && cur != NULL)
4041 cur = cur->next;
4042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044#endif
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004047 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004049 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4050 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004052 if (diff_hlf == HLF_TXD && ptr - line > change_end
4053 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004055 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004056 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4057 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004060
4061 /* Decide which of the highlight attributes to use. */
4062 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004063#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004064 if (area_attr != 0)
4065 char_attr = hl_combine_attr(line_attr, area_attr);
4066 else if (search_attr != 0)
4067 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004068 /* Use line_attr when not in the Visual or 'incsearch' area
4069 * (area_attr may be 0 when "noinvcur" is set). */
4070 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004071 || vcol < fromcol || vcol_prev < fromcol_prev
4072 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004073 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004074#else
4075 if (area_attr != 0)
4076 char_attr = area_attr;
4077 else if (search_attr != 0)
4078 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004079#endif
4080 else
4081 {
4082 attr_pri = FALSE;
4083#ifdef FEAT_SYN_HL
4084 if (has_syntax)
4085 char_attr = syntax_attr;
4086 else
4087#endif
4088 char_attr = 0;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091
4092 /*
4093 * Get the next character to put on the screen.
4094 */
4095 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004096 * The "p_extra" points to the extra stuff that is inserted to
4097 * represent special characters (non-printable stuff) and other
4098 * things. When all characters are the same, c_extra is used.
4099 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4100 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4102 */
4103 if (n_extra > 0)
4104 {
4105 if (c_extra != NUL)
4106 {
4107 c = c_extra;
4108#ifdef FEAT_MBYTE
4109 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4110 if (enc_utf8 && (*mb_char2len)(c) > 1)
4111 {
4112 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004113 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004114 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 else
4117 mb_utf8 = FALSE;
4118#endif
4119 }
4120 else
4121 {
4122 c = *p_extra;
4123#ifdef FEAT_MBYTE
4124 if (has_mbyte)
4125 {
4126 mb_c = c;
4127 if (enc_utf8)
4128 {
4129 /* If the UTF-8 character is more than one byte:
4130 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004131 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 mb_utf8 = FALSE;
4133 if (mb_l > n_extra)
4134 mb_l = 1;
4135 else if (mb_l > 1)
4136 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004137 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004139 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 }
4142 else
4143 {
4144 /* if this is a DBCS character, put it in "mb_c" */
4145 mb_l = MB_BYTE2LEN(c);
4146 if (mb_l >= n_extra)
4147 mb_l = 1;
4148 else if (mb_l > 1)
4149 mb_c = (c << 8) + p_extra[1];
4150 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004151 if (mb_l == 0) /* at the NUL at end-of-line */
4152 mb_l = 1;
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 /* If a double-width char doesn't fit display a '>' in the
4155 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004156 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157# ifdef FEAT_RIGHTLEFT
4158 wp->w_p_rl ? (col <= 0) :
4159# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004160 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 && (*mb_char2cells)(mb_c) == 2)
4162 {
4163 c = '>';
4164 mb_c = c;
4165 mb_l = 1;
4166 mb_utf8 = FALSE;
4167 multi_attr = hl_attr(HLF_AT);
4168 /* put the pointer back to output the double-width
4169 * character at the start of the next line. */
4170 ++n_extra;
4171 --p_extra;
4172 }
4173 else
4174 {
4175 n_extra -= mb_l - 1;
4176 p_extra += mb_l - 1;
4177 }
4178 }
4179#endif
4180 ++p_extra;
4181 }
4182 --n_extra;
4183 }
4184 else
4185 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004186 if (p_extra_free != NULL)
4187 {
4188 vim_free(p_extra_free);
4189 p_extra_free = NULL;
4190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /*
4192 * Get a character from the line itself.
4193 */
4194 c = *ptr;
4195#ifdef FEAT_MBYTE
4196 if (has_mbyte)
4197 {
4198 mb_c = c;
4199 if (enc_utf8)
4200 {
4201 /* If the UTF-8 character is more than one byte: Decode it
4202 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004203 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 mb_utf8 = FALSE;
4205 if (mb_l > 1)
4206 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004207 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 /* Overlong encoded ASCII or ASCII with composing char
4209 * is displayed normally, except a NUL. */
4210 if (mb_c < 0x80)
4211 c = mb_c;
4212 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004213
4214 /* At start of the line we can have a composing char.
4215 * Draw it as a space with a composing char. */
4216 if (utf_iscomposing(mb_c))
4217 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004218 int i;
4219
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004220 for (i = Screen_mco - 1; i > 0; --i)
4221 u8cc[i] = u8cc[i - 1];
4222 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004223 mb_c = ' ';
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 }
4226
4227 if ((mb_l == 1 && c >= 0x80)
4228 || (mb_l >= 1 && mb_c == 0)
4229 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004230# ifdef UNICODE16
4231 || mb_c >= 0x10000
4232# endif
4233 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
4235 /*
4236 * Illegal UTF-8 byte: display as <xx>.
4237 * Non-BMP character : display as ? or fullwidth ?.
4238 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004239# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004241# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (wp->w_p_rl) /* reverse */
4246 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004249# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else if (utf_char2cells(mb_c) != 2)
4251 STRCPY(extra, "?");
4252 else
4253 /* 0xff1f in UTF-8: full-width '?' */
4254 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 p_extra = extra;
4258 c = *p_extra;
4259 mb_c = mb_ptr2char_adv(&p_extra);
4260 mb_utf8 = (c >= 0x80);
4261 n_extra = (int)STRLEN(p_extra);
4262 c_extra = NUL;
4263 if (area_attr == 0 && search_attr == 0)
4264 {
4265 n_attr = n_extra + 1;
4266 extra_attr = hl_attr(HLF_8);
4267 saved_attr2 = char_attr; /* save current attr */
4268 }
4269 }
4270 else if (mb_l == 0) /* at the NUL at end-of-line */
4271 mb_l = 1;
4272#ifdef FEAT_ARABIC
4273 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4274 {
4275 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004276 int pc, pc1, nc;
4277 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 /* The idea of what is the previous and next
4280 * character depends on 'rightleft'. */
4281 if (wp->w_p_rl)
4282 {
4283 pc = prev_c;
4284 pc1 = prev_c1;
4285 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004286 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 else
4289 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004290 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004292 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 prev_c = mb_c;
4295
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004296 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 else
4299 prev_c = mb_c;
4300#endif
4301 }
4302 else /* enc_dbcs */
4303 {
4304 mb_l = MB_BYTE2LEN(c);
4305 if (mb_l == 0) /* at the NUL at end-of-line */
4306 mb_l = 1;
4307 else if (mb_l > 1)
4308 {
4309 /* We assume a second byte below 32 is illegal.
4310 * Hopefully this is OK for all double-byte encodings!
4311 */
4312 if (ptr[1] >= 32)
4313 mb_c = (c << 8) + ptr[1];
4314 else
4315 {
4316 if (ptr[1] == NUL)
4317 {
4318 /* head byte at end of line */
4319 mb_l = 1;
4320 transchar_nonprint(extra, c);
4321 }
4322 else
4323 {
4324 /* illegal tail byte */
4325 mb_l = 2;
4326 STRCPY(extra, "XX");
4327 }
4328 p_extra = extra;
4329 n_extra = (int)STRLEN(extra) - 1;
4330 c_extra = NUL;
4331 c = *p_extra++;
4332 if (area_attr == 0 && search_attr == 0)
4333 {
4334 n_attr = n_extra + 1;
4335 extra_attr = hl_attr(HLF_8);
4336 saved_attr2 = char_attr; /* save current attr */
4337 }
4338 mb_c = c;
4339 }
4340 }
4341 }
4342 /* If a double-width char doesn't fit display a '>' in the
4343 * last column; the character is displayed at the start of the
4344 * next line. */
4345 if ((
4346# ifdef FEAT_RIGHTLEFT
4347 wp->w_p_rl ? (col <= 0) :
4348# endif
4349 (col >= W_WIDTH(wp) - 1))
4350 && (*mb_char2cells)(mb_c) == 2)
4351 {
4352 c = '>';
4353 mb_c = c;
4354 mb_utf8 = FALSE;
4355 mb_l = 1;
4356 multi_attr = hl_attr(HLF_AT);
4357 /* Put pointer back so that the character will be
4358 * displayed at the start of the next line. */
4359 --ptr;
4360 }
4361 else if (*ptr != NUL)
4362 ptr += mb_l - 1;
4363
4364 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004365 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004366 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004367 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004370 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 c = ' ';
4372 if (area_attr == 0 && search_attr == 0)
4373 {
4374 n_attr = n_extra + 1;
4375 extra_attr = hl_attr(HLF_AT);
4376 saved_attr2 = char_attr; /* save current attr */
4377 }
4378 mb_c = c;
4379 mb_utf8 = FALSE;
4380 mb_l = 1;
4381 }
4382
4383 }
4384#endif
4385 ++ptr;
4386
4387 if (extra_check)
4388 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004389#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004390 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004391#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004393#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 /* Get syntax attribute, unless still at the start of the line
4395 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004396 v = (long)(ptr - line);
4397 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
4399 /* Get the syntax attribute for the character. If there
4400 * is an error, disable syntax highlighting. */
4401 save_did_emsg = did_emsg;
4402 did_emsg = FALSE;
4403
Bram Moolenaar217ad922005-03-20 22:37:15 +00004404 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004405# ifdef FEAT_SPELL
4406 has_spell ? &can_spell :
4407# endif
4408 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004411 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004412 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004413 has_syntax = FALSE;
4414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 else
4416 did_emsg = save_did_emsg;
4417
4418 /* Need to get the line again, a multi-line regexp may
4419 * have made it invalid. */
4420 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4421 ptr = line + v;
4422
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004423 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004425 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004426 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004427# ifdef FEAT_CONCEAL
4428 /* no concealing past the end of the line, it interferes
4429 * with line highlighting */
4430 if (c == NUL)
4431 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004432 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004433 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004436#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004437
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004438#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004439 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004440 * Only do this when there is no syntax highlighting, the
4441 * @Spell cluster is not used or the current syntax item
4442 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004443 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004444 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004445 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004446# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004447 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004448 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004449# endif
4450 if (c != 0 && (
4451# ifdef FEAT_SYN_HL
4452 !has_syntax ||
4453# endif
4454 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004455 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004456 char_u *prev_ptr, *p;
4457 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004458 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004459# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004460 if (has_mbyte)
4461 {
4462 prev_ptr = ptr - mb_l;
4463 v -= mb_l - 1;
4464 }
4465 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004466# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004467 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004468
4469 /* Use nextline[] if possible, it has the start of the
4470 * next line concatenated. */
4471 if ((prev_ptr - line) - nextlinecol >= 0)
4472 p = nextline + (prev_ptr - line) - nextlinecol;
4473 else
4474 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004475 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004476 len = spell_check(wp, p, &spell_hlf, &cap_col,
4477 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004478 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004479
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004480 /* In Insert mode only highlight a word that
4481 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004482 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004483 && (State & INSERT) != 0
4484 && wp->w_cursor.lnum == lnum
4485 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004486 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004487 && wp->w_cursor.col < (colnr_T)word_end)
4488 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004489 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004490 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004491 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004492
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004493 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004494 && (p - nextline) + len > nextline_idx)
4495 {
4496 /* Remember that the good word continues at the
4497 * start of the next line. */
4498 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004499 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004500 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004501
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004502 /* Turn index into actual attributes. */
4503 if (spell_hlf != HLF_COUNT)
4504 spell_attr = highlight_attr[spell_hlf];
4505
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004506 if (cap_col > 0)
4507 {
4508 if (p != prev_ptr
4509 && (p - nextline) + cap_col >= nextline_idx)
4510 {
4511 /* Remember that the word in the next line
4512 * must start with a capital. */
4513 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004514 cap_col = (int)((p - nextline) + cap_col
4515 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004516 }
4517 else
4518 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004519 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004520 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004521 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004522 }
4523 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004524 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004525 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004526 char_attr = hl_combine_attr(char_attr, spell_attr);
4527 else
4528 char_attr = hl_combine_attr(spell_attr, char_attr);
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#endif
4531#ifdef FEAT_LINEBREAK
4532 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004533 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004535 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004537# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004538 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004539# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004540 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004542 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004544 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004545
Bram Moolenaar597a4222014-06-25 14:39:50 +02004546 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004547 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004548 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004549 if (c == TAB && n_extra + col > W_WIDTH(wp))
4550 n_extra = (int)wp->w_buffer->b_p_ts
4551 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4552
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004553# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004554 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004555# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004559 {
4560#ifdef FEAT_CONCEAL
4561 if (c == TAB)
4562 /* See "Tab alignment" below. */
4563 FIX_FOR_BOGUSCOLS;
4564#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004565 if (!wp->w_p_list)
4566 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569#endif
4570
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004571 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4572 */
4573 if (wp->w_p_list
4574 && (((c == 160
4575#ifdef FEAT_MBYTE
4576 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4577#endif
4578 ) && lcs_nbsp)
4579 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4580 {
4581 c = (c == ' ') ? lcs_space : lcs_nbsp;
4582 if (area_attr == 0 && search_attr == 0)
4583 {
4584 n_attr = 1;
4585 extra_attr = hl_attr(HLF_8);
4586 saved_attr2 = char_attr; /* save current attr */
4587 }
4588#ifdef FEAT_MBYTE
4589 mb_c = c;
4590 if (enc_utf8 && (*mb_char2len)(c) > 1)
4591 {
4592 mb_utf8 = TRUE;
4593 u8cc[0] = 0;
4594 c = 0xc0;
4595 }
4596 else
4597 mb_utf8 = FALSE;
4598#endif
4599 }
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4602 {
4603 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004604 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 n_attr = 1;
4607 extra_attr = hl_attr(HLF_8);
4608 saved_attr2 = char_attr; /* save current attr */
4609 }
4610#ifdef FEAT_MBYTE
4611 mb_c = c;
4612 if (enc_utf8 && (*mb_char2len)(c) > 1)
4613 {
4614 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004615 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004616 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 else
4619 mb_utf8 = FALSE;
4620#endif
4621 }
4622 }
4623
4624 /*
4625 * Handling of non-printable characters.
4626 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004627 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 /*
4630 * when getting a character from the file, we may have to
4631 * turn it into something else on the way to putting it
4632 * into "ScreenLines".
4633 */
4634 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4635 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004636 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004637 long vcol_adjusted = vcol; /* removed showbreak length */
4638#ifdef FEAT_LINEBREAK
4639 /* only adjust the tab_len, when at the first column
4640 * after the showbreak value was drawn */
4641 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4642 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004645 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004646 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4647
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004648#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004649 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004650#endif
4651 /* tab amount depends on current column */
4652 n_extra = tab_len;
4653#ifdef FEAT_LINEBREAK
4654 else
4655 {
4656 char_u *p;
4657 int len = n_extra;
4658 int i;
4659 int saved_nextra = n_extra;
4660
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004661#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004662 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004663 /* there are characters to conceal */
4664 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004665 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4666 */
4667 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4668 && n_extra > tab_len)
4669 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004670#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004671
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004672 /* if n_extra > 0, it gives the number of chars, to
4673 * use for a tab, else we need to calculate the width
4674 * for a tab */
4675#ifdef FEAT_MBYTE
4676 len = (tab_len * mb_char2len(lcs_tab2));
4677 if (n_extra > 0)
4678 len += n_extra - tab_len;
4679#endif
4680 c = lcs_tab1;
4681 p = alloc((unsigned)(len + 1));
4682 vim_memset(p, ' ', len);
4683 p[len] = NUL;
4684 p_extra_free = p;
4685 for (i = 0; i < tab_len; i++)
4686 {
4687#ifdef FEAT_MBYTE
4688 mb_char2bytes(lcs_tab2, p);
4689 p += mb_char2len(lcs_tab2);
4690 n_extra += mb_char2len(lcs_tab2)
4691 - (saved_nextra > 0 ? 1 : 0);
4692#else
4693 p[i] = lcs_tab2;
4694#endif
4695 }
4696 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004697#ifdef FEAT_CONCEAL
4698 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4699 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004700 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004701 n_extra -= vcol_off;
4702#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004703 }
4704#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004705#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004706 {
4707 int vc_saved = vcol_off;
4708
4709 /* Tab alignment should be identical regardless of
4710 * 'conceallevel' value. So tab compensates of all
4711 * previous concealed characters, and thus resets
4712 * vcol_off and boguscols accumulated so far in the
4713 * line. Note that the tab can be longer than
4714 * 'tabstop' when there are concealed characters. */
4715 FIX_FOR_BOGUSCOLS;
4716
4717 /* Make sure, the highlighting for the tab char will be
4718 * correctly set further below (effectively reverts the
4719 * FIX_FOR_BOGSUCOLS macro */
4720 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004721 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004722 tab_len += vc_saved;
4723 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725#ifdef FEAT_MBYTE
4726 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4727#endif
4728 if (wp->w_p_list)
4729 {
4730 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004731#ifdef FEAT_LINEBREAK
4732 if (wp->w_p_lbr)
4733 c_extra = NUL; /* using p_extra from above */
4734 else
4735#endif
4736 c_extra = lcs_tab2;
4737 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 extra_attr = hl_attr(HLF_8);
4739 saved_attr2 = char_attr; /* save current attr */
4740#ifdef FEAT_MBYTE
4741 mb_c = c;
4742 if (enc_utf8 && (*mb_char2len)(c) > 1)
4743 {
4744 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004745 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004746 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748#endif
4749 }
4750 else
4751 {
4752 c_extra = ' ';
4753 c = ' ';
4754 }
4755 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004756 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004757 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004758 || ((fromcol >= 0 || fromcol_prev >= 0)
4759 && tocol > vcol
4760 && VIsual_mode != Ctrl_V
4761 && (
4762# ifdef FEAT_RIGHTLEFT
4763 wp->w_p_rl ? (col >= 0) :
4764# endif
4765 (col < W_WIDTH(wp)))
4766 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004767 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004768 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004769 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004771 /* Display a '$' after the line or highlight an extra
4772 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4774 /* For a diff line the highlighting continues after the
4775 * "$". */
4776 if (
4777# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004778 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779# ifdef LINE_ATTR
4780 &&
4781# endif
4782# endif
4783# ifdef LINE_ATTR
4784 line_attr == 0
4785# endif
4786 )
4787#endif
4788 {
4789#ifdef FEAT_VIRTUALEDIT
4790 /* In virtualedit, visual selections may extend
4791 * beyond end of line. */
4792 if (area_highlighting && virtual_active()
4793 && tocol != MAXCOL && vcol < tocol)
4794 n_extra = 0;
4795 else
4796#endif
4797 {
4798 p_extra = at_end_str;
4799 n_extra = 1;
4800 c_extra = NUL;
4801 }
4802 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004803 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004804 c = lcs_eol;
4805 else
4806 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 lcs_eol_one = -1;
4808 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004809 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 extra_attr = hl_attr(HLF_AT);
4812 n_attr = 1;
4813 }
4814#ifdef FEAT_MBYTE
4815 mb_c = c;
4816 if (enc_utf8 && (*mb_char2len)(c) > 1)
4817 {
4818 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004819 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004820 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 else
4823 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4824#endif
4825 }
4826 else if (c != NUL)
4827 {
4828 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004829 if (n_extra == 0)
4830 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#ifdef FEAT_RIGHTLEFT
4832 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4833 rl_mirror(p_extra); /* reverse "<12>" */
4834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004836#ifdef FEAT_LINEBREAK
4837 if (wp->w_p_lbr)
4838 {
4839 char_u *p;
4840
4841 c = *p_extra;
4842 p = alloc((unsigned)n_extra + 1);
4843 vim_memset(p, ' ', n_extra);
4844 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4845 p[n_extra] = NUL;
4846 p_extra_free = p_extra = p;
4847 }
4848 else
4849#endif
4850 {
4851 n_extra = byte2cells(c) - 1;
4852 c = *p_extra++;
4853 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004854 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
4856 n_attr = n_extra + 1;
4857 extra_attr = hl_attr(HLF_8);
4858 saved_attr2 = char_attr; /* save current attr */
4859 }
4860#ifdef FEAT_MBYTE
4861 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4862#endif
4863 }
4864#ifdef FEAT_VIRTUALEDIT
4865 else if (VIsual_active
4866 && (VIsual_mode == Ctrl_V
4867 || VIsual_mode == 'v')
4868 && virtual_active()
4869 && tocol != MAXCOL
4870 && vcol < tocol
4871 && (
4872# ifdef FEAT_RIGHTLEFT
4873 wp->w_p_rl ? (col >= 0) :
4874# endif
4875 (col < W_WIDTH(wp))))
4876 {
4877 c = ' ';
4878 --ptr; /* put it back at the NUL */
4879 }
4880#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004881#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 else if ((
4883# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004884 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 ) && (
4888# ifdef FEAT_RIGHTLEFT
4889 wp->w_p_rl ? (col >= 0) :
4890# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004891 (col
4892# ifdef FEAT_CONCEAL
4893 - boguscols
4894# endif
4895 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 /* Highlight until the right side of the window */
4898 c = ' ';
4899 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004900
4901 /* Remember we do the char for line highlighting. */
4902 ++did_line_attr;
4903
4904 /* don't do search HL for the rest of the line */
4905 if (line_attr != 0 && char_attr == search_attr && col > 0)
4906 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907# ifdef FEAT_DIFF
4908 if (diff_hlf == HLF_TXD)
4909 {
4910 diff_hlf = HLF_CHD;
4911 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004914 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4915 char_attr = hl_combine_attr(char_attr,
4916 hl_attr(HLF_CUL));
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919# endif
4920 }
4921#endif
4922 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004923
4924#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004925 if ( wp->w_p_cole > 0
4926 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004927 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004928 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004929 && !(lnum_in_visual_area
4930 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931 {
4932 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004933 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004934 && (syn_get_sub_char() != NUL || match_conc
4935 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004936 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004937 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004938 /* First time at this concealed item: display one
4939 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004940 if (match_conc)
4941 c = match_conc;
4942 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004943 c = syn_get_sub_char();
4944 else if (lcs_conceal != NUL)
4945 c = lcs_conceal;
4946 else
4947 c = ' ';
4948
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004949 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004950
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004951 if (n_extra > 0)
4952 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004953 vcol += n_extra;
4954 if (wp->w_p_wrap && n_extra > 0)
4955 {
4956# ifdef FEAT_RIGHTLEFT
4957 if (wp->w_p_rl)
4958 {
4959 col -= n_extra;
4960 boguscols -= n_extra;
4961 }
4962 else
4963# endif
4964 {
4965 boguscols += n_extra;
4966 col += n_extra;
4967 }
4968 }
4969 n_extra = 0;
4970 n_attr = 0;
4971 }
4972 else if (n_skip == 0)
4973 {
4974 is_concealing = TRUE;
4975 n_skip = 1;
4976 }
4977# ifdef FEAT_MBYTE
4978 mb_c = c;
4979 if (enc_utf8 && (*mb_char2len)(c) > 1)
4980 {
4981 mb_utf8 = TRUE;
4982 u8cc[0] = 0;
4983 c = 0xc0;
4984 }
4985 else
4986 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4987# endif
4988 }
4989 else
4990 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004991 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004992 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004993 }
4994#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004997#ifdef FEAT_CONCEAL
4998 /* In the cursor line and we may be concealing characters: correct
4999 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005000 if (!did_wcol && draw_state == WL_LINE
5001 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005002 && conceal_cursor_line(wp)
5003 && (int)wp->w_virtcol <= vcol + n_skip)
5004 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005005# ifdef FEAT_RIGHTLEFT
5006 if (wp->w_p_rl)
5007 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
5008 else
5009# endif
5010 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005011 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005012 did_wcol = TRUE;
5013 }
5014#endif
5015
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 /* Don't override visual selection highlighting. */
5017 if (n_attr > 0
5018 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005019 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 char_attr = extra_attr;
5021
Bram Moolenaar81695252004-12-29 20:58:21 +00005022#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 /* XIM don't send preedit_start and preedit_end, but they send
5024 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5025 * im_is_preediting() here. */
5026 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005027 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 && (State & INSERT)
5029 && !p_imdisable
5030 && im_is_preediting()
5031 && draw_state == WL_LINE)
5032 {
5033 colnr_T tcol;
5034
5035 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005036 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 else
5038 tcol = preedit_end_col;
5039 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5040 {
5041 if (feedback_old_attr < 0)
5042 {
5043 feedback_col = 0;
5044 feedback_old_attr = char_attr;
5045 }
5046 char_attr = im_get_feedback_attr(feedback_col);
5047 if (char_attr < 0)
5048 char_attr = feedback_old_attr;
5049 feedback_col++;
5050 }
5051 else if (feedback_old_attr >= 0)
5052 {
5053 char_attr = feedback_old_attr;
5054 feedback_old_attr = -1;
5055 feedback_col = 0;
5056 }
5057 }
5058#endif
5059 /*
5060 * Handle the case where we are in column 0 but not on the first
5061 * character of the line and the user wants us to show us a
5062 * special character (via 'listchars' option "precedes:<char>".
5063 */
5064 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005065 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5067#ifdef FEAT_DIFF
5068 && filler_todo <= 0
5069#endif
5070 && draw_state > WL_NR
5071 && c != NUL)
5072 {
5073 c = lcs_prec;
5074 lcs_prec_todo = NUL;
5075#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005076 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5077 {
5078 /* Double-width character being overwritten by the "precedes"
5079 * character, need to fill up half the character. */
5080 c_extra = MB_FILLER_CHAR;
5081 n_extra = 1;
5082 n_attr = 2;
5083 extra_attr = hl_attr(HLF_AT);
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 mb_c = c;
5086 if (enc_utf8 && (*mb_char2len)(c) > 1)
5087 {
5088 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005089 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005090 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 else
5093 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5094#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005095 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 saved_attr3 = char_attr; /* save current attr */
5098 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5099 n_attr3 = 1;
5100 }
5101 }
5102
5103 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005104 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005106 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005107#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005108 || did_line_attr == 1
5109#endif
5110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005112#ifdef FEAT_SEARCH_EXTRA
5113 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005114
5115 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005116 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005117 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005118#endif
5119
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005120 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 * highlight match at end of line. If it's beyond the last
5122 * char on the screen, just overwrite that one (tricky!) Not
5123 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005124#ifdef FEAT_SEARCH_EXTRA
5125 prevcol_hl_flag = FALSE;
5126 if (prevcol == (long)search_hl.startcol)
5127 prevcol_hl_flag = TRUE;
5128 else
5129 {
5130 cur = wp->w_match_head;
5131 while (cur != NULL)
5132 {
5133 if (prevcol == (long)cur->hl.startcol)
5134 {
5135 prevcol_hl_flag = TRUE;
5136 break;
5137 }
5138 cur = cur->next;
5139 }
5140 }
5141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005143 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005144 && (VIsual_mode != Ctrl_V
5145 || lnum == VIsual.lnum
5146 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005147 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148#ifdef FEAT_SEARCH_EXTRA
5149 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005150 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005151# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005152 && did_line_attr <= 1
5153# endif
5154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155#endif
5156 ))
5157 {
5158 int n = 0;
5159
5160#ifdef FEAT_RIGHTLEFT
5161 if (wp->w_p_rl)
5162 {
5163 if (col < 0)
5164 n = 1;
5165 }
5166 else
5167#endif
5168 {
5169 if (col >= W_WIDTH(wp))
5170 n = -1;
5171 }
5172 if (n != 0)
5173 {
5174 /* At the window boundary, highlight the last character
5175 * instead (better than nothing). */
5176 off += n;
5177 col += n;
5178 }
5179 else
5180 {
5181 /* Add a blank character to highlight. */
5182 ScreenLines[off] = ' ';
5183#ifdef FEAT_MBYTE
5184 if (enc_utf8)
5185 ScreenLinesUC[off] = 0;
5186#endif
5187 }
5188#ifdef FEAT_SEARCH_EXTRA
5189 if (area_attr == 0)
5190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005191 /* Use attributes from match with highest priority among
5192 * 'search_hl' and the match list. */
5193 char_attr = search_hl.attr;
5194 cur = wp->w_match_head;
5195 shl_flag = FALSE;
5196 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005197 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005198 if (shl_flag == FALSE
5199 && ((cur != NULL
5200 && cur->priority > SEARCH_HL_PRIORITY)
5201 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005202 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005203 shl = &search_hl;
5204 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005205 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005206 else
5207 shl = &cur->hl;
5208 if ((ptr - line) - 1 == (long)shl->startcol)
5209 char_attr = shl->attr;
5210 if (shl != &search_hl && cur != NULL)
5211 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214#endif
5215 ScreenAttrs[off] = char_attr;
5216#ifdef FEAT_RIGHTLEFT
5217 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005220 --off;
5221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
5223#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005226 ++off;
5227 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005228 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005229#ifdef FEAT_SYN_HL
5230 eol_hl_off = 1;
5231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar91170f82006-05-05 21:15:17 +00005235 /*
5236 * At end of the text line.
5237 */
5238 if (c == NUL)
5239 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005240#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005241 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5242 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005243 {
5244 /* highlight last char after line */
5245 --col;
5246 --off;
5247 --vcol;
5248 }
5249
Bram Moolenaar1a384422010-07-14 19:53:30 +02005250 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005251 if (wp->w_p_wrap)
5252 v = wp->w_skipcol;
5253 else
5254 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005255
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005256 /* check if line ends before left margin */
5257 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005258 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005259#ifdef FEAT_CONCEAL
5260 /* Get rid of the boguscols now, we want to draw until the right
5261 * edge for 'cursorcolumn'. */
5262 col -= boguscols;
5263 boguscols = 0;
5264#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005265
5266 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005267 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005268
5269 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005270 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5271 && (int)wp->w_virtcol <
5272 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005273 && lnum != wp->w_cursor.lnum)
5274 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005275# ifdef FEAT_RIGHTLEFT
5276 && !wp->w_p_rl
5277# endif
5278 )
5279 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005280 int rightmost_vcol = 0;
5281 int i;
5282
5283 if (wp->w_p_cuc)
5284 rightmost_vcol = wp->w_virtcol;
5285 if (draw_color_col)
5286 /* determine rightmost colorcolumn to possibly draw */
5287 for (i = 0; color_cols[i] >= 0; ++i)
5288 if (rightmost_vcol < color_cols[i])
5289 rightmost_vcol = color_cols[i];
5290
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005291 while (col < W_WIDTH(wp))
5292 {
5293 ScreenLines[off] = ' ';
5294#ifdef FEAT_MBYTE
5295 if (enc_utf8)
5296 ScreenLinesUC[off] = 0;
5297#endif
5298 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005299 if (draw_color_col)
5300 draw_color_col = advance_color_col(VCOL_HLC,
5301 &color_cols);
5302
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005303 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005304 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005305 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005306 ScreenAttrs[off++] = hl_attr(HLF_MC);
5307 else
5308 ScreenAttrs[off++] = 0;
5309
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005310 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005311 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005312
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005313 ++vcol;
5314 }
5315 }
5316#endif
5317
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5319 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 row++;
5321
5322 /*
5323 * Update w_cline_height and w_cline_folded if the cursor line was
5324 * updated (saves a call to plines() later).
5325 */
5326 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5327 {
5328 curwin->w_cline_row = startrow;
5329 curwin->w_cline_height = row - startrow;
5330#ifdef FEAT_FOLDING
5331 curwin->w_cline_folded = FALSE;
5332#endif
5333 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5334 }
5335
5336 break;
5337 }
5338
5339 /* line continues beyond line end */
5340 if (lcs_ext
5341 && !wp->w_p_wrap
5342#ifdef FEAT_DIFF
5343 && filler_todo <= 0
5344#endif
5345 && (
5346#ifdef FEAT_RIGHTLEFT
5347 wp->w_p_rl ? col == 0 :
5348#endif
5349 col == W_WIDTH(wp) - 1)
5350 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005351 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5353 {
5354 c = lcs_ext;
5355 char_attr = hl_attr(HLF_AT);
5356#ifdef FEAT_MBYTE
5357 mb_c = c;
5358 if (enc_utf8 && (*mb_char2len)(c) > 1)
5359 {
5360 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005361 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005362 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 else
5365 mb_utf8 = FALSE;
5366#endif
5367 }
5368
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005369#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005370 /* advance to the next 'colorcolumn' */
5371 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005372 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005373
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005374 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005375 * highlight the cursor position itself.
5376 * Also highlight the 'colorcolumn' if it is different than
5377 * 'cursorcolumn' */
5378 vcol_save_attr = -1;
5379 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005380 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005381 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005382 && lnum != wp->w_cursor.lnum)
5383 {
5384 vcol_save_attr = char_attr;
5385 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5386 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005387 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005388 {
5389 vcol_save_attr = char_attr;
5390 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5391 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005392 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005393#endif
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 /*
5396 * Store character to be displayed.
5397 * Skip characters that are left of the screen for 'nowrap'.
5398 */
5399 vcol_prev = vcol;
5400 if (draw_state < WL_LINE || n_skip <= 0)
5401 {
5402 /*
5403 * Store the character.
5404 */
5405#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5406 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5407 {
5408 /* A double-wide character is: put first halve in left cell. */
5409 --off;
5410 --col;
5411 }
5412#endif
5413 ScreenLines[off] = c;
5414#ifdef FEAT_MBYTE
5415 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005416 {
5417 if ((mb_c & 0xff00) == 0x8e00)
5418 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 else if (enc_utf8)
5422 {
5423 if (mb_utf8)
5424 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005425 int i;
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005428 if ((c & 0xff) == 0)
5429 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005430 for (i = 0; i < Screen_mco; ++i)
5431 {
5432 ScreenLinesC[i][off] = u8cc[i];
5433 if (u8cc[i] == 0)
5434 break;
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437 else
5438 ScreenLinesUC[off] = 0;
5439 }
5440 if (multi_attr)
5441 {
5442 ScreenAttrs[off] = multi_attr;
5443 multi_attr = 0;
5444 }
5445 else
5446#endif
5447 ScreenAttrs[off] = char_attr;
5448
5449#ifdef FEAT_MBYTE
5450 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5451 {
5452 /* Need to fill two screen columns. */
5453 ++off;
5454 ++col;
5455 if (enc_utf8)
5456 /* UTF-8: Put a 0 in the second screen char. */
5457 ScreenLines[off] = 0;
5458 else
5459 /* DBCS: Put second byte in the second screen char. */
5460 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005461 if (draw_state > WL_NR
5462#ifdef FEAT_DIFF
5463 && filler_todo <= 0
5464#endif
5465 )
5466 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 /* When "tocol" is halfway a character, set it to the end of
5468 * the character, otherwise highlighting won't stop. */
5469 if (tocol == vcol)
5470 ++tocol;
5471#ifdef FEAT_RIGHTLEFT
5472 if (wp->w_p_rl)
5473 {
5474 /* now it's time to backup one cell */
5475 --off;
5476 --col;
5477 }
5478#endif
5479 }
5480#endif
5481#ifdef FEAT_RIGHTLEFT
5482 if (wp->w_p_rl)
5483 {
5484 --off;
5485 --col;
5486 }
5487 else
5488#endif
5489 {
5490 ++off;
5491 ++col;
5492 }
5493 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005494#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005495 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005496 {
5497 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498 ++vcol_off;
5499 if (n_extra > 0)
5500 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005501 if (wp->w_p_wrap)
5502 {
5503 /*
5504 * Special voodoo required if 'wrap' is on.
5505 *
5506 * Advance the column indicator to force the line
5507 * drawing to wrap early. This will make the line
5508 * take up the same screen space when parts are concealed,
5509 * so that cursor line computations aren't messed up.
5510 *
5511 * To avoid the fictitious advance of 'col' causing
5512 * trailing junk to be written out of the screen line
5513 * we are building, 'boguscols' keeps track of the number
5514 * of bad columns we have advanced.
5515 */
5516 if (n_extra > 0)
5517 {
5518 vcol += n_extra;
5519# ifdef FEAT_RIGHTLEFT
5520 if (wp->w_p_rl)
5521 {
5522 col -= n_extra;
5523 boguscols -= n_extra;
5524 }
5525 else
5526# endif
5527 {
5528 col += n_extra;
5529 boguscols += n_extra;
5530 }
5531 n_extra = 0;
5532 n_attr = 0;
5533 }
5534
5535
5536# ifdef FEAT_MBYTE
5537 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5538 {
5539 /* Need to fill two screen columns. */
5540# ifdef FEAT_RIGHTLEFT
5541 if (wp->w_p_rl)
5542 {
5543 --boguscols;
5544 --col;
5545 }
5546 else
5547# endif
5548 {
5549 ++boguscols;
5550 ++col;
5551 }
5552 }
5553# endif
5554
5555# ifdef FEAT_RIGHTLEFT
5556 if (wp->w_p_rl)
5557 {
5558 --boguscols;
5559 --col;
5560 }
5561 else
5562# endif
5563 {
5564 ++boguscols;
5565 ++col;
5566 }
5567 }
5568 else
5569 {
5570 if (n_extra > 0)
5571 {
5572 vcol += n_extra;
5573 n_extra = 0;
5574 n_attr = 0;
5575 }
5576 }
5577
5578 }
5579#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 else
5581 --n_skip;
5582
Bram Moolenaar64486672010-05-16 15:46:46 +02005583 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5584 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005585 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#ifdef FEAT_DIFF
5587 && filler_todo <= 0
5588#endif
5589 )
5590 ++vcol;
5591
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005592#ifdef FEAT_SYN_HL
5593 if (vcol_save_attr >= 0)
5594 char_attr = vcol_save_attr;
5595#endif
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 /* restore attributes after "predeces" in 'listchars' */
5598 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5599 char_attr = saved_attr3;
5600
5601 /* restore attributes after last 'listchars' or 'number' char */
5602 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5603 char_attr = saved_attr2;
5604
5605 /*
5606 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005607 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 */
5609 if ((
5610#ifdef FEAT_RIGHTLEFT
5611 wp->w_p_rl ? (col < 0) :
5612#endif
5613 (col >= W_WIDTH(wp)))
5614 && (*ptr != NUL
5615#ifdef FEAT_DIFF
5616 || filler_todo > 0
5617#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005618 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5620 )
5621 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005622#ifdef FEAT_CONCEAL
5623 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5624 (int)W_WIDTH(wp), wp->w_p_rl);
5625 boguscols = 0;
5626#else
5627 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5628 (int)W_WIDTH(wp), wp->w_p_rl);
5629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 ++row;
5631 ++screen_row;
5632
5633 /* When not wrapping and finished diff lines, or when displayed
5634 * '$' and highlighting until last column, break here. */
5635 if ((!wp->w_p_wrap
5636#ifdef FEAT_DIFF
5637 && filler_todo <= 0
5638#endif
5639 ) || lcs_eol_one == -1)
5640 break;
5641
5642 /* When the window is too narrow draw all "@" lines. */
5643 if (draw_state != WL_LINE
5644#ifdef FEAT_DIFF
5645 && filler_todo <= 0
5646#endif
5647 )
5648 {
5649 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005650#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 draw_vsep_win(wp, row);
5652#endif
5653 row = endrow;
5654 }
5655
5656 /* When line got too long for screen break here. */
5657 if (row == endrow)
5658 {
5659 ++row;
5660 break;
5661 }
5662
5663 if (screen_cur_row == screen_row - 1
5664#ifdef FEAT_DIFF
5665 && filler_todo <= 0
5666#endif
5667 && W_WIDTH(wp) == Columns)
5668 {
5669 /* Remember that the line wraps, used for modeless copy. */
5670 LineWraps[screen_row - 1] = TRUE;
5671
5672 /*
5673 * Special trick to make copy/paste of wrapped lines work with
5674 * xterm/screen: write an extra character beyond the end of
5675 * the line. This will work with all terminal types
5676 * (regardless of the xn,am settings).
5677 * Only do this on a fast tty.
5678 * Only do this if the cursor is on the current line
5679 * (something has been written in it).
5680 * Don't do this for the GUI.
5681 * Don't do this for double-width characters.
5682 * Don't do this for a window not at the right screen border.
5683 */
5684 if (p_tf
5685#ifdef FEAT_GUI
5686 && !gui.in_use
5687#endif
5688#ifdef FEAT_MBYTE
5689 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005690 && ((*mb_off2cells)(LineOffset[screen_row],
5691 LineOffset[screen_row] + screen_Columns)
5692 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005694 + (int)Columns - 2,
5695 LineOffset[screen_row] + screen_Columns)
5696 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697#endif
5698 )
5699 {
5700 /* First make sure we are at the end of the screen line,
5701 * then output the same character again to let the
5702 * terminal know about the wrap. If the terminal doesn't
5703 * auto-wrap, we overwrite the character. */
5704 if (screen_cur_col != W_WIDTH(wp))
5705 screen_char(LineOffset[screen_row - 1]
5706 + (unsigned)Columns - 1,
5707 screen_row - 1, (int)(Columns - 1));
5708
5709#ifdef FEAT_MBYTE
5710 /* When there is a multi-byte character, just output a
5711 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005712 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5713 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 out_char(' ');
5715 else
5716#endif
5717 out_char(ScreenLines[LineOffset[screen_row - 1]
5718 + (Columns - 1)]);
5719 /* force a redraw of the first char on the next line */
5720 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5721 screen_start(); /* don't know where cursor is now */
5722 }
5723 }
5724
5725 col = 0;
5726 off = (unsigned)(current_ScreenLine - ScreenLines);
5727#ifdef FEAT_RIGHTLEFT
5728 if (wp->w_p_rl)
5729 {
5730 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5731 off += col;
5732 }
5733#endif
5734
5735 /* reset the drawing state for the start of a wrapped line */
5736 draw_state = WL_START;
5737 saved_n_extra = n_extra;
5738 saved_p_extra = p_extra;
5739 saved_c_extra = c_extra;
5740 saved_char_attr = char_attr;
5741 n_extra = 0;
5742 lcs_prec_todo = lcs_prec;
5743#ifdef FEAT_LINEBREAK
5744# ifdef FEAT_DIFF
5745 if (filler_todo <= 0)
5746# endif
5747 need_showbreak = TRUE;
5748#endif
5749#ifdef FEAT_DIFF
5750 --filler_todo;
5751 /* When the filler lines are actually below the last line of the
5752 * file, don't draw the line itself, break here. */
5753 if (filler_todo == 0 && wp->w_botfill)
5754 break;
5755#endif
5756 }
5757
5758 } /* for every character in the line */
5759
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005761 /* After an empty line check first word for capital. */
5762 if (*skipwhite(line) == NUL)
5763 {
5764 capcol_lnum = lnum + 1;
5765 cap_col = 0;
5766 }
5767#endif
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 return row;
5770}
5771
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005772#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005773static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005774
5775/*
5776 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005777 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005778 */
5779 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005780comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005781{
5782 int i;
5783
5784 for (i = 0; i < Screen_mco; ++i)
5785 {
5786 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5787 return TRUE;
5788 if (ScreenLinesC[i][off_from] == 0)
5789 break;
5790 }
5791 return FALSE;
5792}
5793#endif
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795/*
5796 * Check whether the given character needs redrawing:
5797 * - the (first byte of the) character is different
5798 * - the attributes are different
5799 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005800 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
5802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005803char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 if (cols > 0
5806 && ((ScreenLines[off_from] != ScreenLines[off_to]
5807 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5808
5809#ifdef FEAT_MBYTE
5810 || (enc_dbcs != 0
5811 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5812 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5813 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5814 : (cols > 1 && ScreenLines[off_from + 1]
5815 != ScreenLines[off_to + 1])))
5816 || (enc_utf8
5817 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5818 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005819 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005820 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5821 && ScreenLines[off_from + 1]
5822 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#endif
5824 ))
5825 return TRUE;
5826 return FALSE;
5827}
5828
5829/*
5830 * Move one "cooked" screen line to the screen, but only the characters that
5831 * have actually changed. Handle insert/delete character.
5832 * "coloff" gives the first column on the screen for this line.
5833 * "endcol" gives the columns where valid characters are.
5834 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5835 * needs to be cleared, negative otherwise.
5836 * "rlflag" is TRUE in a rightleft window:
5837 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5838 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5839 */
5840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005841screen_line(
5842 int row,
5843 int coloff,
5844 int endcol,
5845 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005847 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005849 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 unsigned off_from;
5852 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005853#ifdef FEAT_MBYTE
5854 unsigned max_off_from;
5855 unsigned max_off_to;
5856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005858#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 int hl;
5860#endif
5861 int force = FALSE; /* force update rest of the line */
5862 int redraw_this /* bool: does character need redraw? */
5863#ifdef FEAT_GUI
5864 = TRUE /* For GUI when while-loop empty */
5865#endif
5866 ;
5867 int redraw_next; /* redraw_this for next character */
5868#ifdef FEAT_MBYTE
5869 int clear_next = FALSE;
5870 int char_cells; /* 1: normal char */
5871 /* 2: occupies two display cells */
5872# define CHAR_CELLS char_cells
5873#else
5874# define CHAR_CELLS 1
5875#endif
5876
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005877 /* Check for illegal row and col, just in case. */
5878 if (row >= Rows)
5879 row = Rows - 1;
5880 if (endcol > Columns)
5881 endcol = Columns;
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883# ifdef FEAT_CLIPBOARD
5884 clip_may_clear_selection(row, row);
5885# endif
5886
5887 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5888 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005889#ifdef FEAT_MBYTE
5890 max_off_from = off_from + screen_Columns;
5891 max_off_to = LineOffset[row] + screen_Columns;
5892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894#ifdef FEAT_RIGHTLEFT
5895 if (rlflag)
5896 {
5897 /* Clear rest first, because it's left of the text. */
5898 if (clear_width > 0)
5899 {
5900 while (col <= endcol && ScreenLines[off_to] == ' '
5901 && ScreenAttrs[off_to] == 0
5902# ifdef FEAT_MBYTE
5903 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5904# endif
5905 )
5906 {
5907 ++off_to;
5908 ++col;
5909 }
5910 if (col <= endcol)
5911 screen_fill(row, row + 1, col + coloff,
5912 endcol + coloff + 1, ' ', ' ', 0);
5913 }
5914 col = endcol + 1;
5915 off_to = LineOffset[row] + col + coloff;
5916 off_from += col;
5917 endcol = (clear_width > 0 ? clear_width : -clear_width);
5918 }
5919#endif /* FEAT_RIGHTLEFT */
5920
5921 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5922
5923 while (col < endcol)
5924 {
5925#ifdef FEAT_MBYTE
5926 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005927 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 else
5929 char_cells = 1;
5930#endif
5931
5932 redraw_this = redraw_next;
5933 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5934 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5935
5936#ifdef FEAT_GUI
5937 /* If the next character was bold, then redraw the current character to
5938 * remove any pixels that might have spilt over into us. This only
5939 * happens in the GUI.
5940 */
5941 if (redraw_next && gui.in_use)
5942 {
5943 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005944 if (hl > HL_ALL)
5945 hl = syn_attr2attr(hl);
5946 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 redraw_this = TRUE;
5948 }
5949#endif
5950
5951 if (redraw_this)
5952 {
5953 /*
5954 * Special handling when 'xs' termcap flag set (hpterm):
5955 * Attributes for characters are stored at the position where the
5956 * cursor is when writing the highlighting code. The
5957 * start-highlighting code must be written with the cursor on the
5958 * first highlighted character. The stop-highlighting code must
5959 * be written with the cursor just after the last highlighted
5960 * character.
5961 * Overwriting a character doesn't remove it's highlighting. Need
5962 * to clear the rest of the line, and force redrawing it
5963 * completely.
5964 */
5965 if ( p_wiv
5966 && !force
5967#ifdef FEAT_GUI
5968 && !gui.in_use
5969#endif
5970 && ScreenAttrs[off_to] != 0
5971 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5972 {
5973 /*
5974 * Need to remove highlighting attributes here.
5975 */
5976 windgoto(row, col + coloff);
5977 out_str(T_CE); /* clear rest of this screen line */
5978 screen_start(); /* don't know where cursor is now */
5979 force = TRUE; /* force redraw of rest of the line */
5980 redraw_next = TRUE; /* or else next char would miss out */
5981
5982 /*
5983 * If the previous character was highlighted, need to stop
5984 * highlighting at this character.
5985 */
5986 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5987 {
5988 screen_attr = ScreenAttrs[off_to - 1];
5989 term_windgoto(row, col + coloff);
5990 screen_stop_highlight();
5991 }
5992 else
5993 screen_attr = 0; /* highlighting has stopped */
5994 }
5995#ifdef FEAT_MBYTE
5996 if (enc_dbcs != 0)
5997 {
5998 /* Check if overwriting a double-byte with a single-byte or
5999 * the other way around requires another character to be
6000 * redrawn. For UTF-8 this isn't needed, because comparing
6001 * ScreenLinesUC[] is sufficient. */
6002 if (char_cells == 1
6003 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006004 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 /* Writing a single-cell character over a double-cell
6007 * character: need to redraw the next cell. */
6008 ScreenLines[off_to + 1] = 0;
6009 redraw_next = TRUE;
6010 }
6011 else if (char_cells == 2
6012 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006013 && (*mb_off2cells)(off_to, max_off_to) == 1
6014 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 /* Writing the second half of a double-cell character over
6017 * a double-cell character: need to redraw the second
6018 * cell. */
6019 ScreenLines[off_to + 2] = 0;
6020 redraw_next = TRUE;
6021 }
6022
6023 if (enc_dbcs == DBCS_JPNU)
6024 ScreenLines2[off_to] = ScreenLines2[off_from];
6025 }
6026 /* When writing a single-width character over a double-width
6027 * character and at the end of the redrawn text, need to clear out
6028 * the right halve of the old character.
6029 * Also required when writing the right halve of a double-width
6030 * char over the left halve of an existing one. */
6031 if (has_mbyte && col + char_cells == endcol
6032 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006033 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006035 && (*mb_off2cells)(off_to, max_off_to) == 1
6036 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 clear_next = TRUE;
6038#endif
6039
6040 ScreenLines[off_to] = ScreenLines[off_from];
6041#ifdef FEAT_MBYTE
6042 if (enc_utf8)
6043 {
6044 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6045 if (ScreenLinesUC[off_from] != 0)
6046 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006047 int i;
6048
6049 for (i = 0; i < Screen_mco; ++i)
6050 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 }
6052 }
6053 if (char_cells == 2)
6054 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6055#endif
6056
6057#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006058 /* The bold trick makes a single column of pixels appear in the
6059 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * character should be redrawn too. This happens for our own GUI
6061 * and for some xterms. */
6062 if (
6063# ifdef FEAT_GUI
6064 gui.in_use
6065# endif
6066# if defined(FEAT_GUI) && defined(UNIX)
6067 ||
6068# endif
6069# ifdef UNIX
6070 term_is_xterm
6071# endif
6072 )
6073 {
6074 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006075 if (hl > HL_ALL)
6076 hl = syn_attr2attr(hl);
6077 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 redraw_next = TRUE;
6079 }
6080#endif
6081 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6082#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006083 /* For simplicity set the attributes of second half of a
6084 * double-wide character equal to the first half. */
6085 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006087
6088 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 else
6091#endif
6092 screen_char(off_to, row, col + coloff);
6093 }
6094 else if ( p_wiv
6095#ifdef FEAT_GUI
6096 && !gui.in_use
6097#endif
6098 && col + coloff > 0)
6099 {
6100 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6101 {
6102 /*
6103 * Don't output stop-highlight when moving the cursor, it will
6104 * stop the highlighting when it should continue.
6105 */
6106 screen_attr = 0;
6107 }
6108 else if (screen_attr != 0)
6109 screen_stop_highlight();
6110 }
6111
6112 off_to += CHAR_CELLS;
6113 off_from += CHAR_CELLS;
6114 col += CHAR_CELLS;
6115 }
6116
6117#ifdef FEAT_MBYTE
6118 if (clear_next)
6119 {
6120 /* Clear the second half of a double-wide character of which the left
6121 * half was overwritten with a single-wide character. */
6122 ScreenLines[off_to] = ' ';
6123 if (enc_utf8)
6124 ScreenLinesUC[off_to] = 0;
6125 screen_char(off_to, row, col + coloff);
6126 }
6127#endif
6128
6129 if (clear_width > 0
6130#ifdef FEAT_RIGHTLEFT
6131 && !rlflag
6132#endif
6133 )
6134 {
6135#ifdef FEAT_GUI
6136 int startCol = col;
6137#endif
6138
6139 /* blank out the rest of the line */
6140 while (col < clear_width && ScreenLines[off_to] == ' '
6141 && ScreenAttrs[off_to] == 0
6142#ifdef FEAT_MBYTE
6143 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6144#endif
6145 )
6146 {
6147 ++off_to;
6148 ++col;
6149 }
6150 if (col < clear_width)
6151 {
6152#ifdef FEAT_GUI
6153 /*
6154 * In the GUI, clearing the rest of the line may leave pixels
6155 * behind if the first character cleared was bold. Some bold
6156 * fonts spill over the left. In this case we redraw the previous
6157 * character too. If we didn't skip any blanks above, then we
6158 * only redraw if the character wasn't already redrawn anyway.
6159 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006160 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 hl = ScreenAttrs[off_to];
6163 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006164 {
6165 int prev_cells = 1;
6166# ifdef FEAT_MBYTE
6167 if (enc_utf8)
6168 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6169 * that its width is 2. */
6170 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6171 else if (enc_dbcs != 0)
6172 {
6173 /* find previous character by counting from first
6174 * column and get its width. */
6175 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006176 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006177
6178 while (off < off_to)
6179 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006180 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006181 off += prev_cells;
6182 }
6183 }
6184
6185 if (enc_dbcs != 0 && prev_cells > 1)
6186 screen_char_2(off_to - prev_cells, row,
6187 col + coloff - prev_cells);
6188 else
6189# endif
6190 screen_char(off_to - prev_cells, row,
6191 col + coloff - prev_cells);
6192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 }
6194#endif
6195 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6196 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006197#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 off_to += clear_width - col;
6199 col = clear_width;
6200#endif
6201 }
6202 }
6203
6204 if (clear_width > 0)
6205 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006206#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 /* For a window that's left of another, draw the separator char. */
6208 if (col + coloff < Columns)
6209 {
6210 int c;
6211
6212 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006213 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006215 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6216 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217# endif
6218 || ScreenAttrs[off_to] != hl)
6219 {
6220 ScreenLines[off_to] = c;
6221 ScreenAttrs[off_to] = hl;
6222# ifdef FEAT_MBYTE
6223 if (enc_utf8)
6224 {
6225 if (c >= 0x80)
6226 {
6227 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006228 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
6230 else
6231 ScreenLinesUC[off_to] = 0;
6232 }
6233# endif
6234 screen_char(off_to, row, col + coloff);
6235 }
6236 }
6237 else
6238#endif
6239 LineWraps[row] = FALSE;
6240 }
6241}
6242
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006243#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006245 * Mirror text "str" for right-left displaying.
6246 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006249rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 char_u *p1, *p2;
6252 int t;
6253
6254 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6255 {
6256 t = *p1;
6257 *p1 = *p2;
6258 *p2 = t;
6259 }
6260}
6261#endif
6262
6263#if defined(FEAT_WINDOWS) || defined(PROTO)
6264/*
6265 * mark all status lines for redraw; used after first :cd
6266 */
6267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006268status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
6270 win_T *wp;
6271
6272 for (wp = firstwin; wp; wp = wp->w_next)
6273 if (wp->w_status_height)
6274 {
6275 wp->w_redr_status = TRUE;
6276 redraw_later(VALID);
6277 }
6278}
6279
6280/*
6281 * mark all status lines of the current buffer for redraw
6282 */
6283 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006284status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 win_T *wp;
6287
6288 for (wp = firstwin; wp; wp = wp->w_next)
6289 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6290 {
6291 wp->w_redr_status = TRUE;
6292 redraw_later(VALID);
6293 }
6294}
6295
6296/*
6297 * Redraw all status lines that need to be redrawn.
6298 */
6299 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006300redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 win_T *wp;
6303
6304 for (wp = firstwin; wp; wp = wp->w_next)
6305 if (wp->w_redr_status)
6306 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006307 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006308 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309}
6310#endif
6311
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006312#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313/*
6314 * Redraw all status lines at the bottom of frame "frp".
6315 */
6316 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006317win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 if (frp->fr_layout == FR_LEAF)
6320 frp->fr_win->w_redr_status = TRUE;
6321 else if (frp->fr_layout == FR_ROW)
6322 {
6323 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6324 win_redraw_last_status(frp);
6325 }
6326 else /* frp->fr_layout == FR_COL */
6327 {
6328 frp = frp->fr_child;
6329 while (frp->fr_next != NULL)
6330 frp = frp->fr_next;
6331 win_redraw_last_status(frp);
6332 }
6333}
6334#endif
6335
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006336#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337/*
6338 * Draw the verticap separator right of window "wp" starting with line "row".
6339 */
6340 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006341draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 int hl;
6344 int c;
6345
6346 if (wp->w_vsep_width)
6347 {
6348 /* draw the vertical separator right of this window */
6349 c = fillchar_vsep(&hl);
6350 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6351 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6352 c, ' ', hl);
6353 }
6354}
6355#endif
6356
6357#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006358static int status_match_len(expand_T *xp, char_u *s);
6359static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006362 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 */
6364 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006365status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 int len = 0;
6368
6369#ifdef FEAT_MENU
6370 int emenu = (xp->xp_context == EXPAND_MENUS
6371 || xp->xp_context == EXPAND_MENUNAMES);
6372
6373 /* Check for menu separators - replace with '|'. */
6374 if (emenu && menu_is_separator(s))
6375 return 1;
6376#endif
6377
6378 while (*s != NUL)
6379 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006380 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006381 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006382 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384
6385 return len;
6386}
6387
6388/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006389 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006390 * These are backslashes used for escaping. Do show backslashes in help tags.
6391 */
6392 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006393skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006394{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006395 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006396#ifdef FEAT_MENU
6397 || ((xp->xp_context == EXPAND_MENUS
6398 || xp->xp_context == EXPAND_MENUNAMES)
6399 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6400#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006401 )
6402 {
6403#ifndef BACKSLASH_IN_FILENAME
6404 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6405 return 2;
6406#endif
6407 return 1;
6408 }
6409 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006410}
6411
6412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 * Show wildchar matches in the status line.
6414 * Show at least the "match" item.
6415 * We start at item 'first_match' in the list and show all matches that fit.
6416 *
6417 * If inversion is possible we use it. Else '=' characters are used.
6418 */
6419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006420win_redr_status_matches(
6421 expand_T *xp,
6422 int num_matches,
6423 char_u **matches, /* list of matches */
6424 int match,
6425 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6428 int row;
6429 char_u *buf;
6430 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006431 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 int fillchar;
6433 int attr;
6434 int i;
6435 int highlight = TRUE;
6436 char_u *selstart = NULL;
6437 int selstart_col = 0;
6438 char_u *selend = NULL;
6439 static int first_match = 0;
6440 int add_left = FALSE;
6441 char_u *s;
6442#ifdef FEAT_MENU
6443 int emenu;
6444#endif
6445#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6446 int l;
6447#endif
6448
6449 if (matches == NULL) /* interrupted completion? */
6450 return;
6451
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006452#ifdef FEAT_MBYTE
6453 if (has_mbyte)
6454 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6455 else
6456#endif
6457 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 if (buf == NULL)
6459 return;
6460
6461 if (match == -1) /* don't show match but original text */
6462 {
6463 match = 0;
6464 highlight = FALSE;
6465 }
6466 /* count 1 for the ending ">" */
6467 clen = status_match_len(xp, L_MATCH(match)) + 3;
6468 if (match == 0)
6469 first_match = 0;
6470 else if (match < first_match)
6471 {
6472 /* jumping left, as far as we can go */
6473 first_match = match;
6474 add_left = TRUE;
6475 }
6476 else
6477 {
6478 /* check if match fits on the screen */
6479 for (i = first_match; i < match; ++i)
6480 clen += status_match_len(xp, L_MATCH(i)) + 2;
6481 if (first_match > 0)
6482 clen += 2;
6483 /* jumping right, put match at the left */
6484 if ((long)clen > Columns)
6485 {
6486 first_match = match;
6487 /* if showing the last match, we can add some on the left */
6488 clen = 2;
6489 for (i = match; i < num_matches; ++i)
6490 {
6491 clen += status_match_len(xp, L_MATCH(i)) + 2;
6492 if ((long)clen >= Columns)
6493 break;
6494 }
6495 if (i == num_matches)
6496 add_left = TRUE;
6497 }
6498 }
6499 if (add_left)
6500 while (first_match > 0)
6501 {
6502 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6503 if ((long)clen >= Columns)
6504 break;
6505 --first_match;
6506 }
6507
6508 fillchar = fillchar_status(&attr, TRUE);
6509
6510 if (first_match == 0)
6511 {
6512 *buf = NUL;
6513 len = 0;
6514 }
6515 else
6516 {
6517 STRCPY(buf, "< ");
6518 len = 2;
6519 }
6520 clen = len;
6521
6522 i = first_match;
6523 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6524 {
6525 if (i == match)
6526 {
6527 selstart = buf + len;
6528 selstart_col = clen;
6529 }
6530
6531 s = L_MATCH(i);
6532 /* Check for menu separators - replace with '|' */
6533#ifdef FEAT_MENU
6534 emenu = (xp->xp_context == EXPAND_MENUS
6535 || xp->xp_context == EXPAND_MENUNAMES);
6536 if (emenu && menu_is_separator(s))
6537 {
6538 STRCPY(buf + len, transchar('|'));
6539 l = (int)STRLEN(buf + len);
6540 len += l;
6541 clen += l;
6542 }
6543 else
6544#endif
6545 for ( ; *s != NUL; ++s)
6546 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006547 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 clen += ptr2cells(s);
6549#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006550 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 STRNCPY(buf + len, s, l);
6553 s += l - 1;
6554 len += l;
6555 }
6556 else
6557#endif
6558 {
6559 STRCPY(buf + len, transchar_byte(*s));
6560 len += (int)STRLEN(buf + len);
6561 }
6562 }
6563 if (i == match)
6564 selend = buf + len;
6565
6566 *(buf + len++) = ' ';
6567 *(buf + len++) = ' ';
6568 clen += 2;
6569 if (++i == num_matches)
6570 break;
6571 }
6572
6573 if (i != num_matches)
6574 {
6575 *(buf + len++) = '>';
6576 ++clen;
6577 }
6578
6579 buf[len] = NUL;
6580
6581 row = cmdline_row - 1;
6582 if (row >= 0)
6583 {
6584 if (wild_menu_showing == 0)
6585 {
6586 if (msg_scrolled > 0)
6587 {
6588 /* Put the wildmenu just above the command line. If there is
6589 * no room, scroll the screen one line up. */
6590 if (cmdline_row == Rows - 1)
6591 {
6592 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6593 ++msg_scrolled;
6594 }
6595 else
6596 {
6597 ++cmdline_row;
6598 ++row;
6599 }
6600 wild_menu_showing = WM_SCROLLED;
6601 }
6602 else
6603 {
6604 /* Create status line if needed by setting 'laststatus' to 2.
6605 * Set 'winminheight' to zero to avoid that the window is
6606 * resized. */
6607 if (lastwin->w_status_height == 0)
6608 {
6609 save_p_ls = p_ls;
6610 save_p_wmh = p_wmh;
6611 p_ls = 2;
6612 p_wmh = 0;
6613 last_status(FALSE);
6614 }
6615 wild_menu_showing = WM_SHOWN;
6616 }
6617 }
6618
6619 screen_puts(buf, row, 0, attr);
6620 if (selstart != NULL && highlight)
6621 {
6622 *selend = NUL;
6623 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6624 }
6625
6626 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6627 }
6628
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006629#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 win_redraw_last_status(topframe);
6631#else
6632 lastwin->w_redr_status = TRUE;
6633#endif
6634 vim_free(buf);
6635}
6636#endif
6637
6638#if defined(FEAT_WINDOWS) || defined(PROTO)
6639/*
6640 * Redraw the status line of window wp.
6641 *
6642 * If inversion is possible we use it. Else '=' characters are used.
6643 */
6644 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006645win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 int row;
6648 char_u *p;
6649 int len;
6650 int fillchar;
6651 int attr;
6652 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006653 static int busy = FALSE;
6654
6655 /* It's possible to get here recursively when 'statusline' (indirectly)
6656 * invokes ":redrawstatus". Simply ignore the call then. */
6657 if (busy)
6658 return;
6659 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 wp->w_redr_status = FALSE;
6662 if (wp->w_status_height == 0)
6663 {
6664 /* no status line, can only be last window */
6665 redraw_cmdline = TRUE;
6666 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006667 else if (!redrawing()
6668#ifdef FEAT_INS_EXPAND
6669 /* don't update status line when popup menu is visible and may be
6670 * drawn over it */
6671 || pum_visible()
6672#endif
6673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 /* Don't redraw right now, do it later. */
6676 wp->w_redr_status = TRUE;
6677 }
6678#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006679 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006682 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684#endif
6685 else
6686 {
6687 fillchar = fillchar_status(&attr, wp == curwin);
6688
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006689 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 p = NameBuff;
6691 len = (int)STRLEN(p);
6692
6693 if (wp->w_buffer->b_help
6694#ifdef FEAT_QUICKFIX
6695 || wp->w_p_pvw
6696#endif
6697 || bufIsChanged(wp->w_buffer)
6698 || wp->w_buffer->b_p_ro)
6699 *(p + len++) = ' ';
6700 if (wp->w_buffer->b_help)
6701 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006702 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 len += (int)STRLEN(p + len);
6704 }
6705#ifdef FEAT_QUICKFIX
6706 if (wp->w_p_pvw)
6707 {
6708 STRCPY(p + len, _("[Preview]"));
6709 len += (int)STRLEN(p + len);
6710 }
6711#endif
6712 if (bufIsChanged(wp->w_buffer))
6713 {
6714 STRCPY(p + len, "[+]");
6715 len += 3;
6716 }
6717 if (wp->w_buffer->b_p_ro)
6718 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006719 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 len += 4;
6721 }
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6724 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6725 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6726 if (this_ru_col <= 1)
6727 {
6728 p = (char_u *)"<"; /* No room for file name! */
6729 len = 1;
6730 }
6731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732#ifdef FEAT_MBYTE
6733 if (has_mbyte)
6734 {
6735 int clen = 0, i;
6736
6737 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006738 clen = mb_string2cells(p, -1);
6739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 /* Find first character that will fit.
6741 * Going from start to end is much faster for DBCS. */
6742 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006743 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 clen -= (*mb_ptr2cells)(p + i);
6745 len = clen;
6746 if (i > 0)
6747 {
6748 p = p + i - 1;
6749 *p = '<';
6750 ++len;
6751 }
6752
6753 }
6754 else
6755#endif
6756 if (len > this_ru_col - 1)
6757 {
6758 p += len - (this_ru_col - 1);
6759 *p = '<';
6760 len = this_ru_col - 1;
6761 }
6762
6763 row = W_WINROW(wp) + wp->w_height;
6764 screen_puts(p, row, W_WINCOL(wp), attr);
6765 screen_fill(row, row + 1, len + W_WINCOL(wp),
6766 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6767
6768 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6769 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6770 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6771 - 1 + W_WINCOL(wp)), attr);
6772
6773#ifdef FEAT_CMDL_INFO
6774 win_redr_ruler(wp, TRUE);
6775#endif
6776 }
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 /*
6779 * May need to draw the character below the vertical separator.
6780 */
6781 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6782 {
6783 if (stl_connected(wp))
6784 fillchar = fillchar_status(&attr, wp == curwin);
6785 else
6786 fillchar = fillchar_vsep(&attr);
6787 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6788 attr);
6789 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006790 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
6792
Bram Moolenaar238a5642006-02-21 22:12:05 +00006793#ifdef FEAT_STL_OPT
6794/*
6795 * Redraw the status line according to 'statusline' and take care of any
6796 * errors encountered.
6797 */
6798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006799redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006800{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006802 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006803
6804 /* When called recursively return. This can happen when the statusline
6805 * contains an expression that triggers a redraw. */
6806 if (entered)
6807 return;
6808 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006809
Bram Moolenaara742e082016-04-05 21:10:38 +02006810 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006811 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006812 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006813 {
6814 /* When there is an error disable the statusline, otherwise the
6815 * display is messed up with errors and a redraw triggers the problem
6816 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006817 set_string_option_direct((char_u *)"statusline", -1,
6818 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006819 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006820 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006821 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006822 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006823}
6824#endif
6825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826/*
6827 * Return TRUE if the status line of window "wp" is connected to the status
6828 * line of the window right of it. If not, then it's a vertical separator.
6829 * Only call if (wp->w_vsep_width != 0).
6830 */
6831 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006832stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 frame_T *fr;
6835
6836 fr = wp->w_frame;
6837 while (fr->fr_parent != NULL)
6838 {
6839 if (fr->fr_parent->fr_layout == FR_COL)
6840 {
6841 if (fr->fr_next != NULL)
6842 break;
6843 }
6844 else
6845 {
6846 if (fr->fr_next != NULL)
6847 return TRUE;
6848 }
6849 fr = fr->fr_parent;
6850 }
6851 return FALSE;
6852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854#endif /* FEAT_WINDOWS */
6855
6856#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6857/*
6858 * Get the value to show for the language mappings, active 'keymap'.
6859 */
6860 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006861get_keymap_str(
6862 win_T *wp,
6863 char_u *buf, /* buffer for the result */
6864 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 char_u *p;
6867
6868 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6869 return FALSE;
6870
6871 {
6872#ifdef FEAT_EVAL
6873 buf_T *old_curbuf = curbuf;
6874 win_T *old_curwin = curwin;
6875 char_u *s;
6876
6877 curbuf = wp->w_buffer;
6878 curwin = wp;
6879 STRCPY(buf, "b:keymap_name"); /* must be writable */
6880 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006881 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 --emsg_skip;
6883 curbuf = old_curbuf;
6884 curwin = old_curwin;
6885 if (p == NULL || *p == NUL)
6886#endif
6887 {
6888#ifdef FEAT_KEYMAP
6889 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6890 p = wp->w_buffer->b_p_keymap;
6891 else
6892#endif
6893 p = (char_u *)"lang";
6894 }
6895 if ((int)(STRLEN(p) + 3) < len)
6896 sprintf((char *)buf, "<%s>", p);
6897 else
6898 buf[0] = NUL;
6899#ifdef FEAT_EVAL
6900 vim_free(s);
6901#endif
6902 }
6903 return buf[0] != NUL;
6904}
6905#endif
6906
6907#if defined(FEAT_STL_OPT) || defined(PROTO)
6908/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006909 * Redraw the status line or ruler of window "wp".
6910 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 */
6912 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006913win_redr_custom(
6914 win_T *wp,
6915 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006917 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 int attr;
6919 int curattr;
6920 int row;
6921 int col = 0;
6922 int maxwidth;
6923 int width;
6924 int n;
6925 int len;
6926 int fillchar;
6927 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006928 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006930 struct stl_hlrec hltab[STL_MAX_ITEM];
6931 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006932 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006933 win_T *ewp;
6934 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaar1d633412013-12-11 15:52:01 +01006936 /* There is a tiny chance that this gets called recursively: When
6937 * redrawing a status line triggers redrawing the ruler or tabline.
6938 * Avoid trouble by not allowing recursion. */
6939 if (entered)
6940 return;
6941 entered = TRUE;
6942
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006944 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006946 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006947 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006948 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006949 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006950 attr = hl_attr(HLF_TPF);
6951 maxwidth = Columns;
6952# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006953 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006956 else
6957 {
6958 row = W_WINROW(wp) + wp->w_height;
6959 fillchar = fillchar_status(&attr, wp == curwin);
6960 maxwidth = W_WIDTH(wp);
6961
6962 if (draw_ruler)
6963 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006964 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006965 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006966 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006967 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006968 if (*++stl == '-')
6969 stl++;
6970 if (atoi((char *)stl))
6971 while (VIM_ISDIGIT(*stl))
6972 stl++;
6973 if (*stl++ != '(')
6974 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006975 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006976#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006977 col = ru_col - (Columns - W_WIDTH(wp));
6978 if (col < (W_WIDTH(wp) + 1) / 2)
6979 col = (W_WIDTH(wp) + 1) / 2;
6980#else
6981 col = ru_col;
6982 if (col > (Columns + 1) / 2)
6983 col = (Columns + 1) / 2;
6984#endif
6985 maxwidth = W_WIDTH(wp) - col;
6986#ifdef FEAT_WINDOWS
6987 if (!wp->w_status_height)
6988#endif
6989 {
6990 row = Rows - 1;
6991 --maxwidth; /* writing in last column may cause scrolling */
6992 fillchar = ' ';
6993 attr = 0;
6994 }
6995
6996# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006997 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006998# endif
6999 }
7000 else
7001 {
7002 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007003 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007004 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007005 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007006# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007007 use_sandbox = was_set_insecurely((char_u *)"statusline",
7008 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007009# endif
7010 }
7011
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007012#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007013 col += W_WINCOL(wp);
7014#endif
7015 }
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007018 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar61452852011-02-01 18:01:11 +01007020 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7021 * the cursor away and back. */
7022 ewp = wp == NULL ? curwin : wp;
7023 p_crb_save = ewp->w_p_crb;
7024 ewp->w_p_crb = FALSE;
7025
Bram Moolenaar362f3562009-11-03 16:20:34 +00007026 /* Make a copy, because the statusline may include a function call that
7027 * might change the option value and free the memory. */
7028 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007029 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007030 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007031 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007032 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007033 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007035 /* Make all characters printable. */
7036 p = transstr(buf);
7037 if (p != NULL)
7038 {
7039 vim_strncpy(buf, p, sizeof(buf) - 1);
7040 vim_free(p);
7041 }
7042
7043 /* fill up with "fillchar" */
7044 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007045 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047#ifdef FEAT_MBYTE
7048 len += (*mb_char2bytes)(fillchar, buf + len);
7049#else
7050 buf[len++] = fillchar;
7051#endif
7052 ++width;
7053 }
7054 buf[len] = NUL;
7055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 /*
7057 * Draw each snippet with the specified highlighting.
7058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 curattr = attr;
7060 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007061 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007063 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 screen_puts_len(p, len, row, col, curattr);
7065 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007066 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007068 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007070 else if (hltab[n].userhl < 0)
7071 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007073 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007074 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#endif
7076 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007077 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007080
7081 if (wp == NULL)
7082 {
7083 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7084 col = 0;
7085 len = 0;
7086 p = buf;
7087 fillchar = 0;
7088 for (n = 0; tabtab[n].start != NULL; n++)
7089 {
7090 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7091 while (col < len)
7092 TabPageIdxs[col++] = fillchar;
7093 p = tabtab[n].start;
7094 fillchar = tabtab[n].userhl;
7095 }
7096 while (col < Columns)
7097 TabPageIdxs[col++] = fillchar;
7098 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007099
7100theend:
7101 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102}
7103
7104#endif /* FEAT_STL_OPT */
7105
7106/*
7107 * Output a single character directly to the screen and update ScreenLines.
7108 */
7109 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007110screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 char_u buf[MB_MAXBYTES + 1];
7113
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007114#ifdef FEAT_MBYTE
7115 if (has_mbyte)
7116 buf[(*mb_char2bytes)(c, buf)] = NUL;
7117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007119 {
7120 buf[0] = c;
7121 buf[1] = NUL;
7122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 screen_puts(buf, row, col, attr);
7124}
7125
7126/*
7127 * Get a single character directly from ScreenLines into "bytes[]".
7128 * Also return its attribute in *attrp;
7129 */
7130 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007131screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
7133 unsigned off;
7134
7135 /* safety check */
7136 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7137 {
7138 off = LineOffset[row] + col;
7139 *attrp = ScreenAttrs[off];
7140 bytes[0] = ScreenLines[off];
7141 bytes[1] = NUL;
7142
7143#ifdef FEAT_MBYTE
7144 if (enc_utf8 && ScreenLinesUC[off] != 0)
7145 bytes[utfc_char2bytes(off, bytes)] = NUL;
7146 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7147 {
7148 bytes[0] = ScreenLines[off];
7149 bytes[1] = ScreenLines2[off];
7150 bytes[2] = NUL;
7151 }
7152 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7153 {
7154 bytes[1] = ScreenLines[off + 1];
7155 bytes[2] = NUL;
7156 }
7157#endif
7158 }
7159}
7160
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007161#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007162static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007163
7164/*
7165 * Return TRUE if composing characters for screen posn "off" differs from
7166 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007167 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007168 */
7169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007170screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007171{
7172 int i;
7173
7174 for (i = 0; i < Screen_mco; ++i)
7175 {
7176 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7177 return TRUE;
7178 if (u8cc[i] == 0)
7179 break;
7180 }
7181 return FALSE;
7182}
7183#endif
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185/*
7186 * Put string '*text' on the screen at position 'row' and 'col', with
7187 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7188 * Note: only outputs within one row, message is truncated at screen boundary!
7189 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7190 */
7191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007192screen_puts(
7193 char_u *text,
7194 int row,
7195 int col,
7196 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 screen_puts_len(text, -1, row, col, attr);
7199}
7200
7201/*
7202 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7203 * a NUL.
7204 */
7205 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007206screen_puts_len(
7207 char_u *text,
7208 int textlen,
7209 int row,
7210 int col,
7211 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 unsigned off;
7214 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007215 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 int c;
7217#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007218 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 int mbyte_blen = 1;
7220 int mbyte_cells = 1;
7221 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007222 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 int clear_next_cell = FALSE;
7224# ifdef FEAT_ARABIC
7225 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007226 int pc, nc, nc1;
7227 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228# endif
7229#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007230#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7231 int force_redraw_this;
7232 int force_redraw_next = FALSE;
7233#endif
7234 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
7236 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7237 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007238 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
Bram Moolenaarc236c162008-07-13 17:41:49 +00007240#ifdef FEAT_MBYTE
7241 /* When drawing over the right halve of a double-wide char clear out the
7242 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007243 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007244# ifdef FEAT_GUI
7245 && !gui.in_use
7246# endif
7247 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007248 {
7249 ScreenLines[off - 1] = ' ';
7250 ScreenAttrs[off - 1] = 0;
7251 if (enc_utf8)
7252 {
7253 ScreenLinesUC[off - 1] = 0;
7254 ScreenLinesC[0][off - 1] = 0;
7255 }
7256 /* redraw the previous cell, make it empty */
7257 screen_char(off - 1, row, col - 1);
7258 /* force the cell at "col" to be redrawn */
7259 force_redraw_next = TRUE;
7260 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007261#endif
7262
Bram Moolenaar367329b2007-08-30 11:53:22 +00007263#ifdef FEAT_MBYTE
7264 max_off = LineOffset[row] + screen_Columns;
7265#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007266 while (col < screen_Columns
7267 && (len < 0 || (int)(ptr - text) < len)
7268 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 c = *ptr;
7271#ifdef FEAT_MBYTE
7272 /* check if this is the first byte of a multibyte */
7273 if (has_mbyte)
7274 {
7275 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007276 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007278 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7280 mbyte_cells = 1;
7281 else if (enc_dbcs != 0)
7282 mbyte_cells = mbyte_blen;
7283 else /* enc_utf8 */
7284 {
7285 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007286 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 (int)((text + len) - ptr));
7288 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007289 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007291# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 /* Non-BMP character: display as ? or fullwidth ?. */
7293 if (u8c >= 0x10000)
7294 {
7295 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7296 if (attr == 0)
7297 attr = hl_attr(HLF_8);
7298 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300# ifdef FEAT_ARABIC
7301 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7302 {
7303 /* Do Arabic shaping. */
7304 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7305 {
7306 /* Past end of string to be displayed. */
7307 nc = NUL;
7308 nc1 = NUL;
7309 }
7310 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007311 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007312 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7313 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007314 nc1 = pcc[0];
7315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 pc = prev_c;
7317 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007318 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320 else
7321 prev_c = u8c;
7322# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007323 if (col + mbyte_cells > screen_Columns)
7324 {
7325 /* Only 1 cell left, but character requires 2 cells:
7326 * display a '>' in the last column to avoid wrapping. */
7327 c = '>';
7328 mbyte_cells = 1;
7329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
7331 }
7332#endif
7333
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007334#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7335 force_redraw_this = force_redraw_next;
7336 force_redraw_next = FALSE;
7337#endif
7338
7339 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340#ifdef FEAT_MBYTE
7341 || (mbyte_cells == 2
7342 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7343 || (enc_dbcs == DBCS_JPNU
7344 && c == 0x8e
7345 && ScreenLines2[off] != ptr[1])
7346 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007347 && (ScreenLinesUC[off] !=
7348 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7349 || (ScreenLinesUC[off] != 0
7350 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351#endif
7352 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007353 || exmode_active;
7354
7355 if (need_redraw
7356#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7357 || force_redraw_this
7358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 )
7360 {
7361#if defined(FEAT_GUI) || defined(UNIX)
7362 /* The bold trick makes a single row of pixels appear in the next
7363 * character. When a bold character is removed, the next
7364 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007365 * and for some xterms. */
7366 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367# ifdef FEAT_GUI
7368 gui.in_use
7369# endif
7370# if defined(FEAT_GUI) && defined(UNIX)
7371 ||
7372# endif
7373# ifdef UNIX
7374 term_is_xterm
7375# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007376 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007378 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007380 if (n > HL_ALL)
7381 n = syn_attr2attr(n);
7382 if (n & HL_BOLD)
7383 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
7385#endif
7386#ifdef FEAT_MBYTE
7387 /* When at the end of the text and overwriting a two-cell
7388 * character with a one-cell character, need to clear the next
7389 * cell. Also when overwriting the left halve of a two-cell char
7390 * with the right halve of a two-cell char. Do this only once
7391 * (mb_off2cells() may return 2 on the right halve). */
7392 if (clear_next_cell)
7393 clear_next_cell = FALSE;
7394 else if (has_mbyte
7395 && (len < 0 ? ptr[mbyte_blen] == NUL
7396 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007397 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007399 && (*mb_off2cells)(off, max_off) == 1
7400 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 clear_next_cell = TRUE;
7402
7403 /* Make sure we never leave a second byte of a double-byte behind,
7404 * it confuses mb_off2cells(). */
7405 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007406 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007408 && (*mb_off2cells)(off, max_off) == 1
7409 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 ScreenLines[off + mbyte_blen] = 0;
7411#endif
7412 ScreenLines[off] = c;
7413 ScreenAttrs[off] = attr;
7414#ifdef FEAT_MBYTE
7415 if (enc_utf8)
7416 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007417 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 ScreenLinesUC[off] = 0;
7419 else
7420 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007421 int i;
7422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007424 for (i = 0; i < Screen_mco; ++i)
7425 {
7426 ScreenLinesC[i][off] = u8cc[i];
7427 if (u8cc[i] == 0)
7428 break;
7429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431 if (mbyte_cells == 2)
7432 {
7433 ScreenLines[off + 1] = 0;
7434 ScreenAttrs[off + 1] = attr;
7435 }
7436 screen_char(off, row, col);
7437 }
7438 else if (mbyte_cells == 2)
7439 {
7440 ScreenLines[off + 1] = ptr[1];
7441 ScreenAttrs[off + 1] = attr;
7442 screen_char_2(off, row, col);
7443 }
7444 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7445 {
7446 ScreenLines2[off] = ptr[1];
7447 screen_char(off, row, col);
7448 }
7449 else
7450#endif
7451 screen_char(off, row, col);
7452 }
7453#ifdef FEAT_MBYTE
7454 if (has_mbyte)
7455 {
7456 off += mbyte_cells;
7457 col += mbyte_cells;
7458 ptr += mbyte_blen;
7459 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007460 {
7461 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007463 len = -1;
7464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 else
7467#endif
7468 {
7469 ++off;
7470 ++col;
7471 ++ptr;
7472 }
7473 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007474
7475#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7476 /* If we detected the next character needs to be redrawn, but the text
7477 * doesn't extend up to there, update the character here. */
7478 if (force_redraw_next && col < screen_Columns)
7479 {
7480# ifdef FEAT_MBYTE
7481 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7482 screen_char_2(off, row, col);
7483 else
7484# endif
7485 screen_char(off, row, col);
7486 }
7487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490#ifdef FEAT_SEARCH_EXTRA
7491/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007492 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 */
7494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007495start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 if (p_hls && !no_hlsearch)
7498 {
7499 last_pat_prog(&search_hl.rm);
7500 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007501# ifdef FEAT_RELTIME
7502 /* Set the time limit to 'redrawtime'. */
7503 profile_setlimit(p_rdt, &search_hl.tm);
7504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506}
7507
7508/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007509 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 */
7511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007512end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 if (search_hl.rm.regprog != NULL)
7515 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007516 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 search_hl.rm.regprog = NULL;
7518 }
7519}
7520
7521/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007522 * Init for calling prepare_search_hl().
7523 */
7524 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007525init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007526{
7527 matchitem_T *cur;
7528
7529 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7530 * match */
7531 cur = wp->w_match_head;
7532 while (cur != NULL)
7533 {
7534 cur->hl.rm = cur->match;
7535 if (cur->hlg_id == 0)
7536 cur->hl.attr = 0;
7537 else
7538 cur->hl.attr = syn_id2attr(cur->hlg_id);
7539 cur->hl.buf = wp->w_buffer;
7540 cur->hl.lnum = 0;
7541 cur->hl.first_lnum = 0;
7542# ifdef FEAT_RELTIME
7543 /* Set the time limit to 'redrawtime'. */
7544 profile_setlimit(p_rdt, &(cur->hl.tm));
7545# endif
7546 cur = cur->next;
7547 }
7548 search_hl.buf = wp->w_buffer;
7549 search_hl.lnum = 0;
7550 search_hl.first_lnum = 0;
7551 /* time limit is set at the toplevel, for all windows */
7552}
7553
7554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 * Advance to the match in window "wp" line "lnum" or past it.
7556 */
7557 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007558prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007560 matchitem_T *cur; /* points to the match list */
7561 match_T *shl; /* points to search_hl or a match */
7562 int shl_flag; /* flag to indicate whether search_hl
7563 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007564 int pos_inprogress; /* marks that position match search is
7565 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 int n;
7567
7568 /*
7569 * When using a multi-line pattern, start searching at the top
7570 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007571 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007573 cur = wp->w_match_head;
7574 shl_flag = FALSE;
7575 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007577 if (shl_flag == FALSE)
7578 {
7579 shl = &search_hl;
7580 shl_flag = TRUE;
7581 }
7582 else
7583 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (shl->rm.regprog != NULL
7585 && shl->lnum == 0
7586 && re_multiline(shl->rm.regprog))
7587 {
7588 if (shl->first_lnum == 0)
7589 {
7590# ifdef FEAT_FOLDING
7591 for (shl->first_lnum = lnum;
7592 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7593 if (hasFoldingWin(wp, shl->first_lnum - 1,
7594 NULL, NULL, TRUE, NULL))
7595 break;
7596# else
7597 shl->first_lnum = wp->w_topline;
7598# endif
7599 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007600 if (cur != NULL)
7601 cur->pos.cur = 0;
7602 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007604 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7605 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007607 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7608 pos_inprogress = cur == NULL || cur->pos.cur == 0
7609 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (shl->lnum != 0)
7611 {
7612 shl->first_lnum = shl->lnum
7613 + shl->rm.endpos[0].lnum
7614 - shl->rm.startpos[0].lnum;
7615 n = shl->rm.endpos[0].col;
7616 }
7617 else
7618 {
7619 ++shl->first_lnum;
7620 n = 0;
7621 }
7622 }
7623 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007624 if (shl != &search_hl && cur != NULL)
7625 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 }
7627}
7628
7629/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007630 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 * Uses shl->buf.
7632 * Sets shl->lnum and shl->rm contents.
7633 * Note: Assumes a previous match is always before "lnum", unless
7634 * shl->lnum is zero.
7635 * Careful: Any pointers for buffer lines will become invalid.
7636 */
7637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007638next_search_hl(
7639 win_T *win,
7640 match_T *shl, /* points to search_hl or a match */
7641 linenr_T lnum,
7642 colnr_T mincol, /* minimal column for a match */
7643 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644{
7645 linenr_T l;
7646 colnr_T matchcol;
7647 long nmatched;
7648
7649 if (shl->lnum != 0)
7650 {
7651 /* Check for three situations:
7652 * 1. If the "lnum" is below a previous match, start a new search.
7653 * 2. If the previous match includes "mincol", use it.
7654 * 3. Continue after the previous match.
7655 */
7656 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7657 if (lnum > l)
7658 shl->lnum = 0;
7659 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7660 return;
7661 }
7662
7663 /*
7664 * Repeat searching for a match until one is found that includes "mincol"
7665 * or none is found in this line.
7666 */
7667 called_emsg = FALSE;
7668 for (;;)
7669 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007670#ifdef FEAT_RELTIME
7671 /* Stop searching after passing the time limit. */
7672 if (profile_passed_limit(&(shl->tm)))
7673 {
7674 shl->lnum = 0; /* no match found in time */
7675 break;
7676 }
7677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 /* Three situations:
7679 * 1. No useful previous match: search from start of line.
7680 * 2. Not Vi compatible or empty match: continue at next character.
7681 * Break the loop if this is beyond the end of the line.
7682 * 3. Vi compatible searching: continue at end of previous match.
7683 */
7684 if (shl->lnum == 0)
7685 matchcol = 0;
7686 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7687 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007688 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007690 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007691
7692 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007693 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007694 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007696 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 shl->lnum = 0;
7698 break;
7699 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007700#ifdef FEAT_MBYTE
7701 if (has_mbyte)
7702 matchcol += mb_ptr2len(ml);
7703 else
7704#endif
7705 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 else
7708 matchcol = shl->rm.endpos[0].col;
7709
7710 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007711 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007713 /* Remember whether shl->rm is using a copy of the regprog in
7714 * cur->match. */
7715 int regprog_is_copy = (shl != &search_hl && cur != NULL
7716 && shl == &cur->hl
7717 && cur->match.regprog == cur->hl.rm.regprog);
7718
Bram Moolenaarb3414592014-06-17 17:48:32 +02007719 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7720 matchcol,
7721#ifdef FEAT_RELTIME
7722 &(shl->tm)
7723#else
7724 NULL
7725#endif
7726 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007727 /* Copy the regprog, in case it got freed and recompiled. */
7728 if (regprog_is_copy)
7729 cur->match.regprog = cur->hl.rm.regprog;
7730
Bram Moolenaarb3414592014-06-17 17:48:32 +02007731 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007732 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007733 /* Error while handling regexp: stop using this regexp. */
7734 if (shl == &search_hl)
7735 {
7736 /* don't free regprog in the match list, it's a copy */
7737 vim_regfree(shl->rm.regprog);
7738 SET_NO_HLSEARCH(TRUE);
7739 }
7740 shl->rm.regprog = NULL;
7741 shl->lnum = 0;
7742 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7743 message */
7744 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007745 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007746 }
7747 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007748 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007749 else
7750 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (nmatched == 0)
7752 {
7753 shl->lnum = 0; /* no match found */
7754 break;
7755 }
7756 if (shl->rm.startpos[0].lnum > 0
7757 || shl->rm.startpos[0].col >= mincol
7758 || nmatched > 1
7759 || shl->rm.endpos[0].col > mincol)
7760 {
7761 shl->lnum += shl->rm.startpos[0].lnum;
7762 break; /* useful match found */
7763 }
7764 }
7765}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
Bram Moolenaarb3414592014-06-17 17:48:32 +02007767 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007768next_search_hl_pos(
7769 match_T *shl, /* points to a match */
7770 linenr_T lnum,
7771 posmatch_T *posmatch, /* match positions */
7772 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007773{
7774 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007775 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007776
7777 shl->lnum = 0;
7778 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7779 {
7780 if (posmatch->pos[i].lnum == 0)
7781 break;
7782 if (posmatch->pos[i].col < mincol)
7783 continue;
7784 if (posmatch->pos[i].lnum == lnum)
7785 {
7786 if (shl->lnum == lnum)
7787 {
7788 /* partially sort positions by column numbers
7789 * on the same line */
7790 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7791 {
7792 llpos_T tmp = posmatch->pos[i];
7793
7794 posmatch->pos[i] = posmatch->pos[bot];
7795 posmatch->pos[bot] = tmp;
7796 }
7797 }
7798 else
7799 {
7800 bot = i;
7801 shl->lnum = lnum;
7802 }
7803 }
7804 }
7805 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007806 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007807 {
7808 colnr_T start = posmatch->pos[bot].col == 0
7809 ? 0 : posmatch->pos[bot].col - 1;
7810 colnr_T end = posmatch->pos[bot].col == 0
7811 ? MAXCOL : start + posmatch->pos[bot].len;
7812
7813 shl->rm.startpos[0].lnum = 0;
7814 shl->rm.startpos[0].col = start;
7815 shl->rm.endpos[0].lnum = 0;
7816 shl->rm.endpos[0].col = end;
7817 posmatch->cur = bot + 1;
7818 return TRUE;
7819 }
7820 return FALSE;
7821}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007822#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007825screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 attrentry_T *aep = NULL;
7828
7829 screen_attr = attr;
7830 if (full_screen
7831#ifdef WIN3264
7832 && termcap_active
7833#endif
7834 )
7835 {
7836#ifdef FEAT_GUI
7837 if (gui.in_use)
7838 {
7839 char buf[20];
7840
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007841 /* The GUI handles this internally. */
7842 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 OUT_STR(buf);
7844 }
7845 else
7846#endif
7847 {
7848 if (attr > HL_ALL) /* special HL attr. */
7849 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007850 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 aep = syn_cterm_attr2entry(attr);
7852 else
7853 aep = syn_term_attr2entry(attr);
7854 if (aep == NULL) /* did ":syntax clear" */
7855 attr = 0;
7856 else
7857 attr = aep->ae_attr;
7858 }
7859 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7860 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007861 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007862#ifdef FEAT_TERMGUICOLORS
7863 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007864 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007865#endif
7866 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007867#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007868 )
7869#endif
7870 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007871 /* If the Normal FG color has BOLD attribute and the new HL
7872 * has a FG color defined, clear BOLD. */
7873 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7875 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007876 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7877 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 out_str(T_US);
7879 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7880 out_str(T_CZH);
7881 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7882 out_str(T_MR);
7883
7884 /*
7885 * Output the color or start string after bold etc., in case the
7886 * bold etc. override the color setting.
7887 */
7888 if (aep != NULL)
7889 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007890#ifdef FEAT_TERMGUICOLORS
7891 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007893 if (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007894 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007895 if (aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007896 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007901 if (t_colors > 1)
7902 {
7903 if (aep->ae_u.cterm.fg_color)
7904 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7905 if (aep->ae_u.cterm.bg_color)
7906 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7907 }
7908 else
7909 {
7910 if (aep->ae_u.term.start != NULL)
7911 out_str(aep->ae_u.term.start);
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 }
7914 }
7915 }
7916 }
7917}
7918
7919 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007920screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922 int do_ME = FALSE; /* output T_ME code */
7923
7924 if (screen_attr != 0
7925#ifdef WIN3264
7926 && termcap_active
7927#endif
7928 )
7929 {
7930#ifdef FEAT_GUI
7931 if (gui.in_use)
7932 {
7933 char buf[20];
7934
7935 /* use internal GUI code */
7936 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7937 OUT_STR(buf);
7938 }
7939 else
7940#endif
7941 {
7942 if (screen_attr > HL_ALL) /* special HL attr. */
7943 {
7944 attrentry_T *aep;
7945
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007946 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
7948 /*
7949 * Assume that t_me restores the original colors!
7950 */
7951 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007952 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007953#ifdef FEAT_TERMGUICOLORS
7954 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007955 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR ||
7956 aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007957#endif
7958 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007959#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007960 )
7961#endif
7962 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 do_ME = TRUE;
7964 }
7965 else
7966 {
7967 aep = syn_term_attr2entry(screen_attr);
7968 if (aep != NULL && aep->ae_u.term.stop != NULL)
7969 {
7970 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7971 do_ME = TRUE;
7972 else
7973 out_str(aep->ae_u.term.stop);
7974 }
7975 }
7976 if (aep == NULL) /* did ":syntax clear" */
7977 screen_attr = 0;
7978 else
7979 screen_attr = aep->ae_attr;
7980 }
7981
7982 /*
7983 * Often all ending-codes are equal to T_ME. Avoid outputting the
7984 * same sequence several times.
7985 */
7986 if (screen_attr & HL_STANDOUT)
7987 {
7988 if (STRCMP(T_SE, T_ME) == 0)
7989 do_ME = TRUE;
7990 else
7991 out_str(T_SE);
7992 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007993 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 if (STRCMP(T_UE, T_ME) == 0)
7996 do_ME = TRUE;
7997 else
7998 out_str(T_UE);
7999 }
8000 if (screen_attr & HL_ITALIC)
8001 {
8002 if (STRCMP(T_CZR, T_ME) == 0)
8003 do_ME = TRUE;
8004 else
8005 out_str(T_CZR);
8006 }
8007 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8008 out_str(T_ME);
8009
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008010#ifdef FEAT_TERMGUICOLORS
8011 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02008013 if (cterm_normal_fg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008014 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar902647d2016-04-22 11:49:06 +02008015 if (cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008016 term_bg_rgb_color(cterm_normal_bg_gui_color);
8017 }
8018 else
8019#endif
8020 {
8021 if (t_colors > 1)
8022 {
8023 /* set Normal cterm colors */
8024 if (cterm_normal_fg_color != 0)
8025 term_fg_color(cterm_normal_fg_color - 1);
8026 if (cterm_normal_bg_color != 0)
8027 term_bg_color(cterm_normal_bg_color - 1);
8028 if (cterm_normal_fg_bold)
8029 out_str(T_MD);
8030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 }
8033 }
8034 screen_attr = 0;
8035}
8036
8037/*
8038 * Reset the colors for a cterm. Used when leaving Vim.
8039 * The machine specific code may override this again.
8040 */
8041 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008042reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008044 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {
8046 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008047#ifdef FEAT_TERMGUICOLORS
8048 if (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02008049 (cterm_normal_fg_gui_color != (long_u)INVALCOLOR
8050 || cterm_normal_bg_gui_color != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008051 (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
8052#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
8056 out_str(T_OP);
8057 screen_attr = -1;
8058 }
8059 if (cterm_normal_fg_bold)
8060 {
8061 out_str(T_ME);
8062 screen_attr = -1;
8063 }
8064 }
8065}
8066
8067/*
8068 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8069 * using the attributes from ScreenAttrs["off"].
8070 */
8071 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008072screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 int attr;
8075
8076 /* Check for illegal values, just in case (could happen just after
8077 * resizing). */
8078 if (row >= screen_Rows || col >= screen_Columns)
8079 return;
8080
Bram Moolenaar494838a2015-02-10 19:20:37 +01008081 /* Outputting a character in the last cell on the screen may scroll the
8082 * screen up. Only do it when the "xn" termcap property is set, otherwise
8083 * mark the character invalid (update it when scrolled up). */
8084 if (*T_XN == NUL
8085 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef FEAT_RIGHTLEFT
8087 /* account for first command-line character in rightleft mode */
8088 && !cmdmsg_rl
8089#endif
8090 )
8091 {
8092 ScreenAttrs[off] = (sattr_T)-1;
8093 return;
8094 }
8095
8096 /*
8097 * Stop highlighting first, so it's easier to move the cursor.
8098 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008099#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (screen_char_attr != 0)
8101 attr = screen_char_attr;
8102 else
8103#endif
8104 attr = ScreenAttrs[off];
8105 if (screen_attr != attr)
8106 screen_stop_highlight();
8107
8108 windgoto(row, col);
8109
8110 if (screen_attr != attr)
8111 screen_start_highlight(attr);
8112
8113#ifdef FEAT_MBYTE
8114 if (enc_utf8 && ScreenLinesUC[off] != 0)
8115 {
8116 char_u buf[MB_MAXBYTES + 1];
8117
8118 /* Convert UTF-8 character to bytes and write it. */
8119
8120 buf[utfc_char2bytes(off, buf)] = NUL;
8121
8122 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008123 if (utf_ambiguous_width(ScreenLinesUC[off]))
8124 screen_cur_col = 9999;
8125 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 ++screen_cur_col;
8127 }
8128 else
8129#endif
8130 {
8131#ifdef FEAT_MBYTE
8132 out_flush_check();
8133#endif
8134 out_char(ScreenLines[off]);
8135#ifdef FEAT_MBYTE
8136 /* double-byte character in single-width cell */
8137 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8138 out_char(ScreenLines2[off]);
8139#endif
8140 }
8141
8142 screen_cur_col++;
8143}
8144
8145#ifdef FEAT_MBYTE
8146
8147/*
8148 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8149 * on the screen at position 'row' and 'col'.
8150 * The attributes of the first byte is used for all. This is required to
8151 * output the two bytes of a double-byte character with nothing in between.
8152 */
8153 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008154screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 /* Check for illegal values (could be wrong when screen was resized). */
8157 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8158 return;
8159
8160 /* Outputting the last character on the screen may scrollup the screen.
8161 * Don't to it! Mark the character invalid (update it when scrolled up) */
8162 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8163 {
8164 ScreenAttrs[off] = (sattr_T)-1;
8165 return;
8166 }
8167
8168 /* Output the first byte normally (positions the cursor), then write the
8169 * second byte directly. */
8170 screen_char(off, row, col);
8171 out_char(ScreenLines[off + 1]);
8172 ++screen_cur_col;
8173}
8174#endif
8175
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008176#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177/*
8178 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8179 * This uses the contents of ScreenLines[] and doesn't change it.
8180 */
8181 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008182screen_draw_rectangle(
8183 int row,
8184 int col,
8185 int height,
8186 int width,
8187 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188{
8189 int r, c;
8190 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008191#ifdef FEAT_MBYTE
8192 int max_off;
8193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008195 /* Can't use ScreenLines unless initialized */
8196 if (ScreenLines == NULL)
8197 return;
8198
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (invert)
8200 screen_char_attr = HL_INVERSE;
8201 for (r = row; r < row + height; ++r)
8202 {
8203 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008204#ifdef FEAT_MBYTE
8205 max_off = off + screen_Columns;
8206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 for (c = col; c < col + width; ++c)
8208 {
8209#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008210 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {
8212 screen_char_2(off + c, r, c);
8213 ++c;
8214 }
8215 else
8216#endif
8217 {
8218 screen_char(off + c, r, c);
8219#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008220 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 ++c;
8222#endif
8223 }
8224 }
8225 }
8226 screen_char_attr = 0;
8227}
8228#endif
8229
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008230#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231/*
8232 * Redraw the characters for a vertically split window.
8233 */
8234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008235redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 int col;
8238 int width;
8239
8240# ifdef FEAT_CLIPBOARD
8241 clip_may_clear_selection(row, end - 1);
8242# endif
8243
8244 if (wp == NULL)
8245 {
8246 col = 0;
8247 width = Columns;
8248 }
8249 else
8250 {
8251 col = wp->w_wincol;
8252 width = wp->w_width;
8253 }
8254 screen_draw_rectangle(row, col, end - row, width, FALSE);
8255}
8256#endif
8257
8258/*
8259 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8260 * with character 'c1' in first column followed by 'c2' in the other columns.
8261 * Use attributes 'attr'.
8262 */
8263 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008264screen_fill(
8265 int start_row,
8266 int end_row,
8267 int start_col,
8268 int end_col,
8269 int c1,
8270 int c2,
8271 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 int row;
8274 int col;
8275 int off;
8276 int end_off;
8277 int did_delete;
8278 int c;
8279 int norm_term;
8280#if defined(FEAT_GUI) || defined(UNIX)
8281 int force_next = FALSE;
8282#endif
8283
8284 if (end_row > screen_Rows) /* safety check */
8285 end_row = screen_Rows;
8286 if (end_col > screen_Columns) /* safety check */
8287 end_col = screen_Columns;
8288 if (ScreenLines == NULL
8289 || start_row >= end_row
8290 || start_col >= end_col) /* nothing to do */
8291 return;
8292
8293 /* it's a "normal" terminal when not in a GUI or cterm */
8294 norm_term = (
8295#ifdef FEAT_GUI
8296 !gui.in_use &&
8297#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008298 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 for (row = start_row; row < end_row; ++row)
8300 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008301#ifdef FEAT_MBYTE
8302 if (has_mbyte
8303# ifdef FEAT_GUI
8304 && !gui.in_use
8305# endif
8306 )
8307 {
8308 /* When drawing over the right halve of a double-wide char clear
8309 * out the left halve. When drawing over the left halve of a
8310 * double wide-char clear out the right halve. Only needed in a
8311 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008312 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008313 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008314 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008315 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008316 }
8317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /*
8319 * Try to use delete-line termcap code, when no attributes or in a
8320 * "normal" terminal, where a bold/italic space is just a
8321 * space.
8322 */
8323 did_delete = FALSE;
8324 if (c2 == ' '
8325 && end_col == Columns
8326 && can_clear(T_CE)
8327 && (attr == 0
8328 || (norm_term
8329 && attr <= HL_ALL
8330 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8331 {
8332 /*
8333 * check if we really need to clear something
8334 */
8335 col = start_col;
8336 if (c1 != ' ') /* don't clear first char */
8337 ++col;
8338
8339 off = LineOffset[row] + col;
8340 end_off = LineOffset[row] + end_col;
8341
8342 /* skip blanks (used often, keep it fast!) */
8343#ifdef FEAT_MBYTE
8344 if (enc_utf8)
8345 while (off < end_off && ScreenLines[off] == ' '
8346 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8347 ++off;
8348 else
8349#endif
8350 while (off < end_off && ScreenLines[off] == ' '
8351 && ScreenAttrs[off] == 0)
8352 ++off;
8353 if (off < end_off) /* something to be cleared */
8354 {
8355 col = off - LineOffset[row];
8356 screen_stop_highlight();
8357 term_windgoto(row, col);/* clear rest of this screen line */
8358 out_str(T_CE);
8359 screen_start(); /* don't know where cursor is now */
8360 col = end_col - col;
8361 while (col--) /* clear chars in ScreenLines */
8362 {
8363 ScreenLines[off] = ' ';
8364#ifdef FEAT_MBYTE
8365 if (enc_utf8)
8366 ScreenLinesUC[off] = 0;
8367#endif
8368 ScreenAttrs[off] = 0;
8369 ++off;
8370 }
8371 }
8372 did_delete = TRUE; /* the chars are cleared now */
8373 }
8374
8375 off = LineOffset[row] + start_col;
8376 c = c1;
8377 for (col = start_col; col < end_col; ++col)
8378 {
8379 if (ScreenLines[off] != c
8380#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008381 || (enc_utf8 && (int)ScreenLinesUC[off]
8382 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#endif
8384 || ScreenAttrs[off] != attr
8385#if defined(FEAT_GUI) || defined(UNIX)
8386 || force_next
8387#endif
8388 )
8389 {
8390#if defined(FEAT_GUI) || defined(UNIX)
8391 /* The bold trick may make a single row of pixels appear in
8392 * the next character. When a bold character is removed, the
8393 * next character should be redrawn too. This happens for our
8394 * own GUI and for some xterms. */
8395 if (
8396# ifdef FEAT_GUI
8397 gui.in_use
8398# endif
8399# if defined(FEAT_GUI) && defined(UNIX)
8400 ||
8401# endif
8402# ifdef UNIX
8403 term_is_xterm
8404# endif
8405 )
8406 {
8407 if (ScreenLines[off] != ' '
8408 && (ScreenAttrs[off] > HL_ALL
8409 || ScreenAttrs[off] & HL_BOLD))
8410 force_next = TRUE;
8411 else
8412 force_next = FALSE;
8413 }
8414#endif
8415 ScreenLines[off] = c;
8416#ifdef FEAT_MBYTE
8417 if (enc_utf8)
8418 {
8419 if (c >= 0x80)
8420 {
8421 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008422 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 }
8424 else
8425 ScreenLinesUC[off] = 0;
8426 }
8427#endif
8428 ScreenAttrs[off] = attr;
8429 if (!did_delete || c != ' ')
8430 screen_char(off, row, col);
8431 }
8432 ++off;
8433 if (col == start_col)
8434 {
8435 if (did_delete)
8436 break;
8437 c = c2;
8438 }
8439 }
8440 if (end_col == Columns)
8441 LineWraps[row] = FALSE;
8442 if (row == Rows - 1) /* overwritten the command line */
8443 {
8444 redraw_cmdline = TRUE;
8445 if (c1 == ' ' && c2 == ' ')
8446 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008447 if (start_col == 0)
8448 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 }
8450 }
8451}
8452
8453/*
8454 * Check if there should be a delay. Used before clearing or redrawing the
8455 * screen or the command line.
8456 */
8457 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008458check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8461 && !did_wait_return
8462 && emsg_silent == 0)
8463 {
8464 out_flush();
8465 ui_delay(1000L, TRUE);
8466 emsg_on_display = FALSE;
8467 if (check_msg_scroll)
8468 msg_scroll = FALSE;
8469 }
8470}
8471
8472/*
8473 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008474 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 * Returns TRUE if there is a valid screen to write to.
8476 * Returns FALSE when starting up and screen not initialized yet.
8477 */
8478 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008479screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008481 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 return (ScreenLines != NULL);
8483}
8484
8485/*
8486 * Resize the shell to Rows and Columns.
8487 * Allocate ScreenLines[] and associated items.
8488 *
8489 * There may be some time between setting Rows and Columns and (re)allocating
8490 * ScreenLines[]. This happens when starting up and when (manually) changing
8491 * the shell size. Always use screen_Rows and screen_Columns to access items
8492 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8493 * final size of the shell is needed.
8494 */
8495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008496screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 int new_row, old_row;
8499#ifdef FEAT_GUI
8500 int old_Rows;
8501#endif
8502 win_T *wp;
8503 int outofmem = FALSE;
8504 int len;
8505 schar_T *new_ScreenLines;
8506#ifdef FEAT_MBYTE
8507 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008508 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008510 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#endif
8512 sattr_T *new_ScreenAttrs;
8513 unsigned *new_LineOffset;
8514 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008515#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008516 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008517 tabpage_T *tp;
8518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008520 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008521#ifdef FEAT_AUTOCMD
8522 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008524retry:
8525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 /*
8527 * Allocation of the screen buffers is done only when the size changes and
8528 * when Rows and Columns have been set and we have started doing full
8529 * screen stuff.
8530 */
8531 if ((ScreenLines != NULL
8532 && Rows == screen_Rows
8533 && Columns == screen_Columns
8534#ifdef FEAT_MBYTE
8535 && enc_utf8 == (ScreenLinesUC != NULL)
8536 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008537 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538#endif
8539 )
8540 || Rows == 0
8541 || Columns == 0
8542 || (!full_screen && ScreenLines == NULL))
8543 return;
8544
8545 /*
8546 * It's possible that we produce an out-of-memory message below, which
8547 * will cause this function to be called again. To break the loop, just
8548 * return here.
8549 */
8550 if (entered)
8551 return;
8552 entered = TRUE;
8553
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008554 /*
8555 * Note that the window sizes are updated before reallocating the arrays,
8556 * thus we must not redraw here!
8557 */
8558 ++RedrawingDisabled;
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 win_new_shellsize(); /* fit the windows in the new sized shell */
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 comp_col(); /* recompute columns for shown command and ruler */
8563
8564 /*
8565 * We're changing the size of the screen.
8566 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8567 * - Move lines from the old arrays into the new arrays, clear extra
8568 * lines (unless the screen is going to be cleared).
8569 * - Free the old arrays.
8570 *
8571 * If anything fails, make ScreenLines NULL, so we don't do anything!
8572 * Continuing with the old ScreenLines may result in a crash, because the
8573 * size is wrong.
8574 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008575 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008577#ifdef FEAT_AUTOCMD
8578 if (aucmd_win != NULL)
8579 win_free_lsize(aucmd_win);
8580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
8582 new_ScreenLines = (schar_T *)lalloc((long_u)(
8583 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8584#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008585 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (enc_utf8)
8587 {
8588 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8589 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008591 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8593 }
8594 if (enc_dbcs == DBCS_JPNU)
8595 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8596 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8597#endif
8598 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8599 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8600 new_LineOffset = (unsigned *)lalloc((long_u)(
8601 Rows * sizeof(unsigned)), FALSE);
8602 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008603#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008604 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008607 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {
8609 if (win_alloc_lines(wp) == FAIL)
8610 {
8611 outofmem = TRUE;
8612#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008613 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#endif
8615 }
8616 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008617#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008618 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8619 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008620 outofmem = TRUE;
8621#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008622#ifdef FEAT_WINDOWS
8623give_up:
8624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626#ifdef FEAT_MBYTE
8627 for (i = 0; i < p_mco; ++i)
8628 if (new_ScreenLinesC[i] == NULL)
8629 break;
8630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 if (new_ScreenLines == NULL
8632#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008633 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8635#endif
8636 || new_ScreenAttrs == NULL
8637 || new_LineOffset == NULL
8638 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008639#ifdef FEAT_WINDOWS
8640 || new_TabPageIdxs == NULL
8641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 || outofmem)
8643 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008644 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008645 {
8646 /* guess the size */
8647 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8648
8649 /* Remember we did this to avoid getting outofmem messages over
8650 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008651 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 vim_free(new_ScreenLines);
8654 new_ScreenLines = NULL;
8655#ifdef FEAT_MBYTE
8656 vim_free(new_ScreenLinesUC);
8657 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008658 for (i = 0; i < p_mco; ++i)
8659 {
8660 vim_free(new_ScreenLinesC[i]);
8661 new_ScreenLinesC[i] = NULL;
8662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 vim_free(new_ScreenLines2);
8664 new_ScreenLines2 = NULL;
8665#endif
8666 vim_free(new_ScreenAttrs);
8667 new_ScreenAttrs = NULL;
8668 vim_free(new_LineOffset);
8669 new_LineOffset = NULL;
8670 vim_free(new_LineWraps);
8671 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008672#ifdef FEAT_WINDOWS
8673 vim_free(new_TabPageIdxs);
8674 new_TabPageIdxs = NULL;
8675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 }
8677 else
8678 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008679 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 for (new_row = 0; new_row < Rows; ++new_row)
8682 {
8683 new_LineOffset[new_row] = new_row * Columns;
8684 new_LineWraps[new_row] = FALSE;
8685
8686 /*
8687 * If the screen is not going to be cleared, copy as much as
8688 * possible from the old screen to the new one and clear the rest
8689 * (used when resizing the window at the "--more--" prompt or when
8690 * executing an external command, for the GUI).
8691 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008692 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 (void)vim_memset(new_ScreenLines + new_row * Columns,
8695 ' ', (size_t)Columns * sizeof(schar_T));
8696#ifdef FEAT_MBYTE
8697 if (enc_utf8)
8698 {
8699 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8700 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008701 for (i = 0; i < p_mco; ++i)
8702 (void)vim_memset(new_ScreenLinesC[i]
8703 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 0, (size_t)Columns * sizeof(u8char_T));
8705 }
8706 if (enc_dbcs == DBCS_JPNU)
8707 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8708 0, (size_t)Columns * sizeof(schar_T));
8709#endif
8710 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8711 0, (size_t)Columns * sizeof(sattr_T));
8712 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008713 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {
8715 if (screen_Columns < Columns)
8716 len = screen_Columns;
8717 else
8718 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008719#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008720 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008721 * may be invalid now. Also when p_mco changes. */
8722 if (!(enc_utf8 && ScreenLinesUC == NULL)
8723 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008724#endif
8725 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8726 ScreenLines + LineOffset[old_row],
8727 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008729 if (enc_utf8 && ScreenLinesUC != NULL
8730 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
8732 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8733 ScreenLinesUC + LineOffset[old_row],
8734 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008735 for (i = 0; i < p_mco; ++i)
8736 mch_memmove(new_ScreenLinesC[i]
8737 + new_LineOffset[new_row],
8738 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 (size_t)len * sizeof(u8char_T));
8740 }
8741 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8742 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8743 ScreenLines2 + LineOffset[old_row],
8744 (size_t)len * sizeof(schar_T));
8745#endif
8746 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8747 ScreenAttrs + LineOffset[old_row],
8748 (size_t)len * sizeof(sattr_T));
8749 }
8750 }
8751 }
8752 /* Use the last line of the screen for the current line. */
8753 current_ScreenLine = new_ScreenLines + Rows * Columns;
8754 }
8755
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008756 free_screenlines();
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 ScreenLines = new_ScreenLines;
8759#ifdef FEAT_MBYTE
8760 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008761 for (i = 0; i < p_mco; ++i)
8762 ScreenLinesC[i] = new_ScreenLinesC[i];
8763 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 ScreenLines2 = new_ScreenLines2;
8765#endif
8766 ScreenAttrs = new_ScreenAttrs;
8767 LineOffset = new_LineOffset;
8768 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008769#ifdef FEAT_WINDOWS
8770 TabPageIdxs = new_TabPageIdxs;
8771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 /* It's important that screen_Rows and screen_Columns reflect the actual
8774 * size of ScreenLines[]. Set them before calling anything. */
8775#ifdef FEAT_GUI
8776 old_Rows = screen_Rows;
8777#endif
8778 screen_Rows = Rows;
8779 screen_Columns = Columns;
8780
8781 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008782 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 screenclear2();
8784
8785#ifdef FEAT_GUI
8786 else if (gui.in_use
8787 && !gui.starting
8788 && ScreenLines != NULL
8789 && old_Rows != Rows)
8790 {
8791 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8792 /*
8793 * Adjust the position of the cursor, for when executing an external
8794 * command.
8795 */
8796 if (msg_row >= Rows) /* Rows got smaller */
8797 msg_row = Rows - 1; /* put cursor at last row */
8798 else if (Rows > old_Rows) /* Rows got bigger */
8799 msg_row += Rows - old_Rows; /* put cursor in same place */
8800 if (msg_col >= Columns) /* Columns got smaller */
8801 msg_col = Columns - 1; /* put cursor at last column */
8802 }
8803#endif
8804
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008806 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008807
8808#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008809 /*
8810 * Do not apply autocommands more than 3 times to avoid an endless loop
8811 * in case applying autocommands always changes Rows or Columns.
8812 */
8813 if (starting == 0 && ++retry_count <= 3)
8814 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008815 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008816 /* In rare cases, autocommands may have altered Rows or Columns,
8817 * jump back to check if we need to allocate the screen again. */
8818 goto retry;
8819 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
8822
8823 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008824free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008825{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008826#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008827 int i;
8828
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008829 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008830 for (i = 0; i < Screen_mco; ++i)
8831 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008832 vim_free(ScreenLines2);
8833#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008835 vim_free(ScreenAttrs);
8836 vim_free(LineOffset);
8837 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008838#ifdef FEAT_WINDOWS
8839 vim_free(TabPageIdxs);
8840#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008841}
8842
8843 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008844screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 check_for_delay(FALSE);
8847 screenalloc(FALSE); /* allocate screen buffers if size changed */
8848 screenclear2(); /* clear the screen */
8849}
8850
8851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008852screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853{
8854 int i;
8855
8856 if (starting == NO_SCREEN || ScreenLines == NULL
8857#ifdef FEAT_GUI
8858 || (gui.in_use && gui.starting)
8859#endif
8860 )
8861 return;
8862
8863#ifdef FEAT_GUI
8864 if (!gui.in_use)
8865#endif
8866 screen_attr = -1; /* force setting the Normal colors */
8867 screen_stop_highlight(); /* don't want highlighting here */
8868
8869#ifdef FEAT_CLIPBOARD
8870 /* disable selection without redrawing it */
8871 clip_scroll_selection(9999);
8872#endif
8873
8874 /* blank out ScreenLines */
8875 for (i = 0; i < Rows; ++i)
8876 {
8877 lineclear(LineOffset[i], (int)Columns);
8878 LineWraps[i] = FALSE;
8879 }
8880
8881 if (can_clear(T_CL))
8882 {
8883 out_str(T_CL); /* clear the display */
8884 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008885 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
8887 else
8888 {
8889 /* can't clear the screen, mark all chars with invalid attributes */
8890 for (i = 0; i < Rows; ++i)
8891 lineinvalid(LineOffset[i], (int)Columns);
8892 clear_cmdline = TRUE;
8893 }
8894
8895 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8896
8897 win_rest_invalid(firstwin);
8898 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008899#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008900 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 if (must_redraw == CLEAR) /* no need to clear again */
8903 must_redraw = NOT_VALID;
8904 compute_cmdrow();
8905 msg_row = cmdline_row; /* put cursor on last line for messages */
8906 msg_col = 0;
8907 screen_start(); /* don't know where cursor is now */
8908 msg_scrolled = 0; /* can't scroll back */
8909 msg_didany = FALSE;
8910 msg_didout = FALSE;
8911}
8912
8913/*
8914 * Clear one line in ScreenLines.
8915 */
8916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008917lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918{
8919 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8920#ifdef FEAT_MBYTE
8921 if (enc_utf8)
8922 (void)vim_memset(ScreenLinesUC + off, 0,
8923 (size_t)width * sizeof(u8char_T));
8924#endif
8925 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8926}
8927
8928/*
8929 * Mark one line in ScreenLines invalid by setting the attributes to an
8930 * invalid value.
8931 */
8932 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008933lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8936}
8937
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008938#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
8940 * Copy part of a Screenline for vertically split window "wp".
8941 */
8942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008943linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
8945 unsigned off_to = LineOffset[to] + wp->w_wincol;
8946 unsigned off_from = LineOffset[from] + wp->w_wincol;
8947
8948 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8949 wp->w_width * sizeof(schar_T));
8950# ifdef FEAT_MBYTE
8951 if (enc_utf8)
8952 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008953 int i;
8954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8956 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008957 for (i = 0; i < p_mco; ++i)
8958 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8959 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 if (enc_dbcs == DBCS_JPNU)
8962 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8963 wp->w_width * sizeof(schar_T));
8964# endif
8965 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8966 wp->w_width * sizeof(sattr_T));
8967}
8968#endif
8969
8970/*
8971 * Return TRUE if clearing with term string "p" would work.
8972 * It can't work when the string is empty or it won't set the right background.
8973 */
8974 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008975can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 return (*p != NUL && (t_colors <= 1
8978#ifdef FEAT_GUI
8979 || gui.in_use
8980#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008981#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard18f6722016-06-17 13:18:49 +02008982 || (p_tgc && cterm_normal_bg_gui_color == (long_u)INVALCOLOR)
8983 || (!p_tgc && cterm_normal_bg_color == 0)
8984#else
8985 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008986#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02008987 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989
8990/*
8991 * Reset cursor position. Use whenever cursor was moved because of outputting
8992 * something directly to the screen (shell commands) or a terminal control
8993 * code.
8994 */
8995 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008996screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 screen_cur_row = screen_cur_col = 9999;
8999}
9000
9001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 * Move the cursor to position "row","col" in the screen.
9003 * This tries to find the most efficient way to move, minimizing the number of
9004 * characters sent to the terminal.
9005 */
9006 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009007windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009009 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 int i;
9011 int plan;
9012 int cost;
9013 int wouldbe_col;
9014 int noinvcurs;
9015 char_u *bs;
9016 int goto_cost;
9017 int attr;
9018
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009019#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9021
9022#define PLAN_LE 1
9023#define PLAN_CR 2
9024#define PLAN_NL 3
9025#define PLAN_WRITE 4
9026 /* Can't use ScreenLines unless initialized */
9027 if (ScreenLines == NULL)
9028 return;
9029
9030 if (col != screen_cur_col || row != screen_cur_row)
9031 {
9032 /* Check for valid position. */
9033 if (row < 0) /* window without text lines? */
9034 row = 0;
9035 if (row >= screen_Rows)
9036 row = screen_Rows - 1;
9037 if (col >= screen_Columns)
9038 col = screen_Columns - 1;
9039
9040 /* check if no cursor movement is allowed in highlight mode */
9041 if (screen_attr && *T_MS == NUL)
9042 noinvcurs = HIGHL_COST;
9043 else
9044 noinvcurs = 0;
9045 goto_cost = GOTO_COST + noinvcurs;
9046
9047 /*
9048 * Plan how to do the positioning:
9049 * 1. Use CR to move it to column 0, same row.
9050 * 2. Use T_LE to move it a few columns to the left.
9051 * 3. Use NL to move a few lines down, column 0.
9052 * 4. Move a few columns to the right with T_ND or by writing chars.
9053 *
9054 * Don't do this if the cursor went beyond the last column, the cursor
9055 * position is unknown then (some terminals wrap, some don't )
9056 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009057 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * characters to move the cursor to the right.
9059 */
9060 if (row >= screen_cur_row && screen_cur_col < Columns)
9061 {
9062 /*
9063 * If the cursor is in the same row, bigger col, we can use CR
9064 * or T_LE.
9065 */
9066 bs = NULL; /* init for GCC */
9067 attr = screen_attr;
9068 if (row == screen_cur_row && col < screen_cur_col)
9069 {
9070 /* "le" is preferred over "bc", because "bc" is obsolete */
9071 if (*T_LE)
9072 bs = T_LE; /* "cursor left" */
9073 else
9074 bs = T_BC; /* "backspace character (old) */
9075 if (*bs)
9076 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9077 else
9078 cost = 999;
9079 if (col + 1 < cost) /* using CR is less characters */
9080 {
9081 plan = PLAN_CR;
9082 wouldbe_col = 0;
9083 cost = 1; /* CR is just one character */
9084 }
9085 else
9086 {
9087 plan = PLAN_LE;
9088 wouldbe_col = col;
9089 }
9090 if (noinvcurs) /* will stop highlighting */
9091 {
9092 cost += noinvcurs;
9093 attr = 0;
9094 }
9095 }
9096
9097 /*
9098 * If the cursor is above where we want to be, we can use CR LF.
9099 */
9100 else if (row > screen_cur_row)
9101 {
9102 plan = PLAN_NL;
9103 wouldbe_col = 0;
9104 cost = (row - screen_cur_row) * 2; /* CR LF */
9105 if (noinvcurs) /* will stop highlighting */
9106 {
9107 cost += noinvcurs;
9108 attr = 0;
9109 }
9110 }
9111
9112 /*
9113 * If the cursor is in the same row, smaller col, just use write.
9114 */
9115 else
9116 {
9117 plan = PLAN_WRITE;
9118 wouldbe_col = screen_cur_col;
9119 cost = 0;
9120 }
9121
9122 /*
9123 * Check if any characters that need to be written have the
9124 * correct attributes. Also avoid UTF-8 characters.
9125 */
9126 i = col - wouldbe_col;
9127 if (i > 0)
9128 cost += i;
9129 if (cost < goto_cost && i > 0)
9130 {
9131 /*
9132 * Check if the attributes are correct without additionally
9133 * stopping highlighting.
9134 */
9135 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9136 while (i && *p++ == attr)
9137 --i;
9138 if (i != 0)
9139 {
9140 /*
9141 * Try if it works when highlighting is stopped here.
9142 */
9143 if (*--p == 0)
9144 {
9145 cost += noinvcurs;
9146 while (i && *p++ == 0)
9147 --i;
9148 }
9149 if (i != 0)
9150 cost = 999; /* different attributes, don't do it */
9151 }
9152#ifdef FEAT_MBYTE
9153 if (enc_utf8)
9154 {
9155 /* Don't use an UTF-8 char for positioning, it's slow. */
9156 for (i = wouldbe_col; i < col; ++i)
9157 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9158 {
9159 cost = 999;
9160 break;
9161 }
9162 }
9163#endif
9164 }
9165
9166 /*
9167 * We can do it without term_windgoto()!
9168 */
9169 if (cost < goto_cost)
9170 {
9171 if (plan == PLAN_LE)
9172 {
9173 if (noinvcurs)
9174 screen_stop_highlight();
9175 while (screen_cur_col > col)
9176 {
9177 out_str(bs);
9178 --screen_cur_col;
9179 }
9180 }
9181 else if (plan == PLAN_CR)
9182 {
9183 if (noinvcurs)
9184 screen_stop_highlight();
9185 out_char('\r');
9186 screen_cur_col = 0;
9187 }
9188 else if (plan == PLAN_NL)
9189 {
9190 if (noinvcurs)
9191 screen_stop_highlight();
9192 while (screen_cur_row < row)
9193 {
9194 out_char('\n');
9195 ++screen_cur_row;
9196 }
9197 screen_cur_col = 0;
9198 }
9199
9200 i = col - screen_cur_col;
9201 if (i > 0)
9202 {
9203 /*
9204 * Use cursor-right if it's one character only. Avoids
9205 * removing a line of pixels from the last bold char, when
9206 * using the bold trick in the GUI.
9207 */
9208 if (T_ND[0] != NUL && T_ND[1] == NUL)
9209 {
9210 while (i-- > 0)
9211 out_char(*T_ND);
9212 }
9213 else
9214 {
9215 int off;
9216
9217 off = LineOffset[row] + screen_cur_col;
9218 while (i-- > 0)
9219 {
9220 if (ScreenAttrs[off] != screen_attr)
9221 screen_stop_highlight();
9222#ifdef FEAT_MBYTE
9223 out_flush_check();
9224#endif
9225 out_char(ScreenLines[off]);
9226#ifdef FEAT_MBYTE
9227 if (enc_dbcs == DBCS_JPNU
9228 && ScreenLines[off] == 0x8e)
9229 out_char(ScreenLines2[off]);
9230#endif
9231 ++off;
9232 }
9233 }
9234 }
9235 }
9236 }
9237 else
9238 cost = 999;
9239
9240 if (cost >= goto_cost)
9241 {
9242 if (noinvcurs)
9243 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009244 if (row == screen_cur_row && (col > screen_cur_col)
9245 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 term_cursor_right(col - screen_cur_col);
9247 else
9248 term_windgoto(row, col);
9249 }
9250 screen_cur_row = row;
9251 screen_cur_col = col;
9252 }
9253}
9254
9255/*
9256 * Set cursor to its position in the current window.
9257 */
9258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009259setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 if (redrawing())
9262 {
9263 validate_cursor();
9264 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9265 W_WINCOL(curwin) + (
9266#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009267 /* With 'rightleft' set and the cursor on a double-wide
9268 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9270# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009271 (has_mbyte
9272 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9273 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274# endif
9275 1)) :
9276#endif
9277 curwin->w_wcol));
9278 }
9279}
9280
9281
9282/*
9283 * insert 'line_count' lines at 'row' in window 'wp'
9284 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9285 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9286 * scrolling.
9287 * Returns FAIL if the lines are not inserted, OK for success.
9288 */
9289 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009290win_ins_lines(
9291 win_T *wp,
9292 int row,
9293 int line_count,
9294 int invalid,
9295 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 int did_delete;
9298 int nextrow;
9299 int lastrow;
9300 int retval;
9301
9302 if (invalid)
9303 wp->w_lines_valid = 0;
9304
9305 if (wp->w_height < 5)
9306 return FAIL;
9307
9308 if (line_count > wp->w_height - row)
9309 line_count = wp->w_height - row;
9310
9311 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9312 if (retval != MAYBE)
9313 return retval;
9314
9315 /*
9316 * If there is a next window or a status line, we first try to delete the
9317 * lines at the bottom to avoid messing what is after the window.
9318 * If this fails and there are following windows, don't do anything to avoid
9319 * messing up those windows, better just redraw.
9320 */
9321 did_delete = FALSE;
9322#ifdef FEAT_WINDOWS
9323 if (wp->w_next != NULL || wp->w_status_height)
9324 {
9325 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9326 line_count, (int)Rows, FALSE, NULL) == OK)
9327 did_delete = TRUE;
9328 else if (wp->w_next)
9329 return FAIL;
9330 }
9331#endif
9332 /*
9333 * if no lines deleted, blank the lines that will end up below the window
9334 */
9335 if (!did_delete)
9336 {
9337#ifdef FEAT_WINDOWS
9338 wp->w_redr_status = TRUE;
9339#endif
9340 redraw_cmdline = TRUE;
9341 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9342 lastrow = nextrow + line_count;
9343 if (lastrow > Rows)
9344 lastrow = Rows;
9345 screen_fill(nextrow - line_count, lastrow - line_count,
9346 W_WINCOL(wp), (int)W_ENDCOL(wp),
9347 ' ', ' ', 0);
9348 }
9349
9350 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9351 == FAIL)
9352 {
9353 /* deletion will have messed up other windows */
9354 if (did_delete)
9355 {
9356#ifdef FEAT_WINDOWS
9357 wp->w_redr_status = TRUE;
9358#endif
9359 win_rest_invalid(W_NEXT(wp));
9360 }
9361 return FAIL;
9362 }
9363
9364 return OK;
9365}
9366
9367/*
9368 * delete "line_count" window lines at "row" in window "wp"
9369 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9370 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9371 * scrolling
9372 * Return OK for success, FAIL if the lines are not deleted.
9373 */
9374 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009375win_del_lines(
9376 win_T *wp,
9377 int row,
9378 int line_count,
9379 int invalid,
9380 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 int retval;
9383
9384 if (invalid)
9385 wp->w_lines_valid = 0;
9386
9387 if (line_count > wp->w_height - row)
9388 line_count = wp->w_height - row;
9389
9390 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9391 if (retval != MAYBE)
9392 return retval;
9393
9394 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9395 (int)Rows, FALSE, NULL) == FAIL)
9396 return FAIL;
9397
9398#ifdef FEAT_WINDOWS
9399 /*
9400 * If there are windows or status lines below, try to put them at the
9401 * correct place. If we can't do that, they have to be redrawn.
9402 */
9403 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9404 {
9405 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9406 line_count, (int)Rows, NULL) == FAIL)
9407 {
9408 wp->w_redr_status = TRUE;
9409 win_rest_invalid(wp->w_next);
9410 }
9411 }
9412 /*
9413 * If this is the last window and there is no status line, redraw the
9414 * command line later.
9415 */
9416 else
9417#endif
9418 redraw_cmdline = TRUE;
9419 return OK;
9420}
9421
9422/*
9423 * Common code for win_ins_lines() and win_del_lines().
9424 * Returns OK or FAIL when the work has been done.
9425 * Returns MAYBE when not finished yet.
9426 */
9427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009428win_do_lines(
9429 win_T *wp,
9430 int row,
9431 int line_count,
9432 int mayclear,
9433 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435 int retval;
9436
9437 if (!redrawing() || line_count <= 0)
9438 return FAIL;
9439
9440 /* only a few lines left: redraw is faster */
9441 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009442#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 && wp->w_width == Columns
9444#endif
9445 )
9446 {
9447 screenclear(); /* will set wp->w_lines_valid to 0 */
9448 return FAIL;
9449 }
9450
9451 /*
9452 * Delete all remaining lines
9453 */
9454 if (row + line_count >= wp->w_height)
9455 {
9456 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9457 W_WINCOL(wp), (int)W_ENDCOL(wp),
9458 ' ', ' ', 0);
9459 return OK;
9460 }
9461
9462 /*
9463 * when scrolling, the message on the command line should be cleared,
9464 * otherwise it will stay there forever.
9465 */
9466 clear_cmdline = TRUE;
9467
9468 /*
9469 * If the terminal can set a scroll region, use that.
9470 * Always do this in a vertically split window. This will redraw from
9471 * ScreenLines[] when t_CV isn't defined. That's faster than using
9472 * win_line().
9473 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009474 * a character in the lower right corner of the scroll region may cause a
9475 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 */
9477 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009478#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 || W_WIDTH(wp) != Columns
9480#endif
9481 )
9482 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009483#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9485#endif
9486 scroll_region_set(wp, row);
9487 if (del)
9488 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9489 wp->w_height - row, FALSE, wp);
9490 else
9491 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9492 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009493#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9495#endif
9496 scroll_region_reset();
9497 return retval;
9498 }
9499
9500#ifdef FEAT_WINDOWS
9501 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9502 return FAIL;
9503#endif
9504
9505 return MAYBE;
9506}
9507
9508/*
9509 * window 'wp' and everything after it is messed up, mark it for redraw
9510 */
9511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009512win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514#ifdef FEAT_WINDOWS
9515 while (wp != NULL)
9516#else
9517 if (wp != NULL)
9518#endif
9519 {
9520 redraw_win_later(wp, NOT_VALID);
9521#ifdef FEAT_WINDOWS
9522 wp->w_redr_status = TRUE;
9523 wp = wp->w_next;
9524#endif
9525 }
9526 redraw_cmdline = TRUE;
9527}
9528
9529/*
9530 * The rest of the routines in this file perform screen manipulations. The
9531 * given operation is performed physically on the screen. The corresponding
9532 * change is also made to the internal screen image. In this way, the editor
9533 * anticipates the effect of editing changes on the appearance of the screen.
9534 * That way, when we call screenupdate a complete redraw isn't usually
9535 * necessary. Another advantage is that we can keep adding code to anticipate
9536 * screen changes, and in the meantime, everything still works.
9537 */
9538
9539/*
9540 * types for inserting or deleting lines
9541 */
9542#define USE_T_CAL 1
9543#define USE_T_CDL 2
9544#define USE_T_AL 3
9545#define USE_T_CE 4
9546#define USE_T_DL 5
9547#define USE_T_SR 6
9548#define USE_NL 7
9549#define USE_T_CD 8
9550#define USE_REDRAW 9
9551
9552/*
9553 * insert lines on the screen and update ScreenLines[]
9554 * 'end' is the line after the scrolled part. Normally it is Rows.
9555 * When scrolling region used 'off' is the offset from the top for the region.
9556 * 'row' and 'end' are relative to the start of the region.
9557 *
9558 * return FAIL for failure, OK for success.
9559 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009560 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009561screen_ins_lines(
9562 int off,
9563 int row,
9564 int line_count,
9565 int end,
9566 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568 int i;
9569 int j;
9570 unsigned temp;
9571 int cursor_row;
9572 int type;
9573 int result_empty;
9574 int can_ce = can_clear(T_CE);
9575
9576 /*
9577 * FAIL if
9578 * - there is no valid screen
9579 * - the screen has to be redrawn completely
9580 * - the line count is less than one
9581 * - the line count is more than 'ttyscroll'
9582 */
9583 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9584 return FAIL;
9585
9586 /*
9587 * There are seven ways to insert lines:
9588 * 0. When in a vertically split window and t_CV isn't set, redraw the
9589 * characters from ScreenLines[].
9590 * 1. Use T_CD (clear to end of display) if it exists and the result of
9591 * the insert is just empty lines
9592 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9593 * present or line_count > 1. It looks better if we do all the inserts
9594 * at once.
9595 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9596 * insert is just empty lines and T_CE is not present or line_count >
9597 * 1.
9598 * 4. Use T_AL (insert line) if it exists.
9599 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9600 * just empty lines.
9601 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9602 * just empty lines.
9603 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9604 * the 'da' flag is not set or we have clear line capability.
9605 * 8. redraw the characters from ScreenLines[].
9606 *
9607 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9608 * the scrollbar for the window. It does have insert line, use that if it
9609 * exists.
9610 */
9611 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009612#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9614 type = USE_REDRAW;
9615 else
9616#endif
9617 if (can_clear(T_CD) && result_empty)
9618 type = USE_T_CD;
9619 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9620 type = USE_T_CAL;
9621 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9622 type = USE_T_CDL;
9623 else if (*T_AL != NUL)
9624 type = USE_T_AL;
9625 else if (can_ce && result_empty)
9626 type = USE_T_CE;
9627 else if (*T_DL != NUL && result_empty)
9628 type = USE_T_DL;
9629 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9630 type = USE_T_SR;
9631 else
9632 return FAIL;
9633
9634 /*
9635 * For clearing the lines screen_del_lines() is used. This will also take
9636 * care of t_db if necessary.
9637 */
9638 if (type == USE_T_CD || type == USE_T_CDL ||
9639 type == USE_T_CE || type == USE_T_DL)
9640 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9641
9642 /*
9643 * If text is retained below the screen, first clear or delete as many
9644 * lines at the bottom of the window as are about to be inserted so that
9645 * the deleted lines won't later surface during a screen_del_lines.
9646 */
9647 if (*T_DB)
9648 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9649
9650#ifdef FEAT_CLIPBOARD
9651 /* Remove a modeless selection when inserting lines halfway the screen
9652 * or not the full width of the screen. */
9653 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009654# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 || (wp != NULL && wp->w_width != Columns)
9656# endif
9657 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009658 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 else
9660 clip_scroll_selection(-line_count);
9661#endif
9662
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663#ifdef FEAT_GUI
9664 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9665 * scrolling is actually carried out. */
9666 gui_dont_update_cursor();
9667#endif
9668
9669 if (*T_CCS != NUL) /* cursor relative to region */
9670 cursor_row = row;
9671 else
9672 cursor_row = row + off;
9673
9674 /*
9675 * Shift LineOffset[] line_count down to reflect the inserted lines.
9676 * Clear the inserted lines in ScreenLines[].
9677 */
9678 row += off;
9679 end += off;
9680 for (i = 0; i < line_count; ++i)
9681 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009682#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 if (wp != NULL && wp->w_width != Columns)
9684 {
9685 /* need to copy part of a line */
9686 j = end - 1 - i;
9687 while ((j -= line_count) >= row)
9688 linecopy(j + line_count, j, wp);
9689 j += line_count;
9690 if (can_clear((char_u *)" "))
9691 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9692 else
9693 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9694 LineWraps[j] = FALSE;
9695 }
9696 else
9697#endif
9698 {
9699 j = end - 1 - i;
9700 temp = LineOffset[j];
9701 while ((j -= line_count) >= row)
9702 {
9703 LineOffset[j + line_count] = LineOffset[j];
9704 LineWraps[j + line_count] = LineWraps[j];
9705 }
9706 LineOffset[j + line_count] = temp;
9707 LineWraps[j + line_count] = FALSE;
9708 if (can_clear((char_u *)" "))
9709 lineclear(temp, (int)Columns);
9710 else
9711 lineinvalid(temp, (int)Columns);
9712 }
9713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
9715 screen_stop_highlight();
9716 windgoto(cursor_row, 0);
9717
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009718#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 /* redraw the characters */
9720 if (type == USE_REDRAW)
9721 redraw_block(row, end, wp);
9722 else
9723#endif
9724 if (type == USE_T_CAL)
9725 {
9726 term_append_lines(line_count);
9727 screen_start(); /* don't know where cursor is now */
9728 }
9729 else
9730 {
9731 for (i = 0; i < line_count; i++)
9732 {
9733 if (type == USE_T_AL)
9734 {
9735 if (i && cursor_row != 0)
9736 windgoto(cursor_row, 0);
9737 out_str(T_AL);
9738 }
9739 else /* type == USE_T_SR */
9740 out_str(T_SR);
9741 screen_start(); /* don't know where cursor is now */
9742 }
9743 }
9744
9745 /*
9746 * With scroll-reverse and 'da' flag set we need to clear the lines that
9747 * have been scrolled down into the region.
9748 */
9749 if (type == USE_T_SR && *T_DA)
9750 {
9751 for (i = 0; i < line_count; ++i)
9752 {
9753 windgoto(off + i, 0);
9754 out_str(T_CE);
9755 screen_start(); /* don't know where cursor is now */
9756 }
9757 }
9758
9759#ifdef FEAT_GUI
9760 gui_can_update_cursor();
9761 if (gui.in_use)
9762 out_flush(); /* always flush after a scroll */
9763#endif
9764 return OK;
9765}
9766
9767/*
9768 * delete lines on the screen and update ScreenLines[]
9769 * 'end' is the line after the scrolled part. Normally it is Rows.
9770 * When scrolling region used 'off' is the offset from the top for the region.
9771 * 'row' and 'end' are relative to the start of the region.
9772 *
9773 * Return OK for success, FAIL if the lines are not deleted.
9774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009776screen_del_lines(
9777 int off,
9778 int row,
9779 int line_count,
9780 int end,
9781 int force, /* even when line_count > p_ttyscroll */
9782 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
9784 int j;
9785 int i;
9786 unsigned temp;
9787 int cursor_row;
9788 int cursor_end;
9789 int result_empty; /* result is empty until end of region */
9790 int can_delete; /* deleting line codes can be used */
9791 int type;
9792
9793 /*
9794 * FAIL if
9795 * - there is no valid screen
9796 * - the screen has to be redrawn completely
9797 * - the line count is less than one
9798 * - the line count is more than 'ttyscroll'
9799 */
9800 if (!screen_valid(TRUE) || line_count <= 0 ||
9801 (!force && line_count > p_ttyscroll))
9802 return FAIL;
9803
9804 /*
9805 * Check if the rest of the current region will become empty.
9806 */
9807 result_empty = row + line_count >= end;
9808
9809 /*
9810 * We can delete lines only when 'db' flag not set or when 'ce' option
9811 * available.
9812 */
9813 can_delete = (*T_DB == NUL || can_clear(T_CE));
9814
9815 /*
9816 * There are six ways to delete lines:
9817 * 0. When in a vertically split window and t_CV isn't set, redraw the
9818 * characters from ScreenLines[].
9819 * 1. Use T_CD if it exists and the result is empty.
9820 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9821 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9822 * none of the other ways work.
9823 * 4. Use T_CE (erase line) if the result is empty.
9824 * 5. Use T_DL (delete line) if it exists.
9825 * 6. redraw the characters from ScreenLines[].
9826 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009827#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9829 type = USE_REDRAW;
9830 else
9831#endif
9832 if (can_clear(T_CD) && result_empty)
9833 type = USE_T_CD;
9834#if defined(__BEOS__) && defined(BEOS_DR8)
9835 /*
9836 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9837 * its internal termcap... this works okay for tests which test *T_DB !=
9838 * NUL. It has the disadvantage that the user cannot use any :set t_*
9839 * command to get T_DB (back) to empty_option, only :set term=... will do
9840 * the trick...
9841 * Anyway, this hack will hopefully go away with the next OS release.
9842 * (Olaf Seibert)
9843 */
9844 else if (row == 0 && T_DB == empty_option
9845 && (line_count == 1 || *T_CDL == NUL))
9846#else
9847 else if (row == 0 && (
9848#ifndef AMIGA
9849 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9850 * up, so use delete-line command */
9851 line_count == 1 ||
9852#endif
9853 *T_CDL == NUL))
9854#endif
9855 type = USE_NL;
9856 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9857 type = USE_T_CDL;
9858 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009859#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 && (wp == NULL || wp->w_width == Columns)
9861#endif
9862 )
9863 type = USE_T_CE;
9864 else if (*T_DL != NUL && can_delete)
9865 type = USE_T_DL;
9866 else if (*T_CDL != NUL && can_delete)
9867 type = USE_T_CDL;
9868 else
9869 return FAIL;
9870
9871#ifdef FEAT_CLIPBOARD
9872 /* Remove a modeless selection when deleting lines halfway the screen or
9873 * not the full width of the screen. */
9874 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009875# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 || (wp != NULL && wp->w_width != Columns)
9877# endif
9878 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009879 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 else
9881 clip_scroll_selection(line_count);
9882#endif
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884#ifdef FEAT_GUI
9885 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9886 * scrolling is actually carried out. */
9887 gui_dont_update_cursor();
9888#endif
9889
9890 if (*T_CCS != NUL) /* cursor relative to region */
9891 {
9892 cursor_row = row;
9893 cursor_end = end;
9894 }
9895 else
9896 {
9897 cursor_row = row + off;
9898 cursor_end = end + off;
9899 }
9900
9901 /*
9902 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9903 * Clear the inserted lines in ScreenLines[].
9904 */
9905 row += off;
9906 end += off;
9907 for (i = 0; i < line_count; ++i)
9908 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009909#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (wp != NULL && wp->w_width != Columns)
9911 {
9912 /* need to copy part of a line */
9913 j = row + i;
9914 while ((j += line_count) <= end - 1)
9915 linecopy(j - line_count, j, wp);
9916 j -= line_count;
9917 if (can_clear((char_u *)" "))
9918 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9919 else
9920 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9921 LineWraps[j] = FALSE;
9922 }
9923 else
9924#endif
9925 {
9926 /* whole width, moving the line pointers is faster */
9927 j = row + i;
9928 temp = LineOffset[j];
9929 while ((j += line_count) <= end - 1)
9930 {
9931 LineOffset[j - line_count] = LineOffset[j];
9932 LineWraps[j - line_count] = LineWraps[j];
9933 }
9934 LineOffset[j - line_count] = temp;
9935 LineWraps[j - line_count] = FALSE;
9936 if (can_clear((char_u *)" "))
9937 lineclear(temp, (int)Columns);
9938 else
9939 lineinvalid(temp, (int)Columns);
9940 }
9941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942
9943 screen_stop_highlight();
9944
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009945#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 /* redraw the characters */
9947 if (type == USE_REDRAW)
9948 redraw_block(row, end, wp);
9949 else
9950#endif
9951 if (type == USE_T_CD) /* delete the lines */
9952 {
9953 windgoto(cursor_row, 0);
9954 out_str(T_CD);
9955 screen_start(); /* don't know where cursor is now */
9956 }
9957 else if (type == USE_T_CDL)
9958 {
9959 windgoto(cursor_row, 0);
9960 term_delete_lines(line_count);
9961 screen_start(); /* don't know where cursor is now */
9962 }
9963 /*
9964 * Deleting lines at top of the screen or scroll region: Just scroll
9965 * the whole screen (scroll region) up by outputting newlines on the
9966 * last line.
9967 */
9968 else if (type == USE_NL)
9969 {
9970 windgoto(cursor_end - 1, 0);
9971 for (i = line_count; --i >= 0; )
9972 out_char('\n'); /* cursor will remain on same line */
9973 }
9974 else
9975 {
9976 for (i = line_count; --i >= 0; )
9977 {
9978 if (type == USE_T_DL)
9979 {
9980 windgoto(cursor_row, 0);
9981 out_str(T_DL); /* delete a line */
9982 }
9983 else /* type == USE_T_CE */
9984 {
9985 windgoto(cursor_row + i, 0);
9986 out_str(T_CE); /* erase a line */
9987 }
9988 screen_start(); /* don't know where cursor is now */
9989 }
9990 }
9991
9992 /*
9993 * If the 'db' flag is set, we need to clear the lines that have been
9994 * scrolled up at the bottom of the region.
9995 */
9996 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9997 {
9998 for (i = line_count; i > 0; --i)
9999 {
10000 windgoto(cursor_end - i, 0);
10001 out_str(T_CE); /* erase a line */
10002 screen_start(); /* don't know where cursor is now */
10003 }
10004 }
10005
10006#ifdef FEAT_GUI
10007 gui_can_update_cursor();
10008 if (gui.in_use)
10009 out_flush(); /* always flush after a scroll */
10010#endif
10011
10012 return OK;
10013}
10014
10015/*
10016 * show the current mode and ruler
10017 *
10018 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10019 * If clear_cmdline is FALSE there may be a message there that needs to be
10020 * cleared only if a mode is shown.
10021 * Return the length of the message (0 if no message).
10022 */
10023 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010024showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
10026 int need_clear;
10027 int length = 0;
10028 int do_mode;
10029 int attr;
10030 int nwr_save;
10031#ifdef FEAT_INS_EXPAND
10032 int sub_attr;
10033#endif
10034
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010035 do_mode = ((p_smd && msg_silent == 0)
10036 && ((State & INSERT)
10037 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010038 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 if (do_mode || Recording)
10040 {
10041 /*
10042 * Don't show mode right now, when not redrawing or inside a mapping.
10043 * Call char_avail() only when we are going to show something, because
10044 * it takes a bit of time.
10045 */
10046 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10047 {
10048 redraw_cmdline = TRUE; /* show mode later */
10049 return 0;
10050 }
10051
10052 nwr_save = need_wait_return;
10053
10054 /* wait a bit before overwriting an important message */
10055 check_for_delay(FALSE);
10056
10057 /* if the cmdline is more than one line high, erase top lines */
10058 need_clear = clear_cmdline;
10059 if (clear_cmdline && cmdline_row < Rows - 1)
10060 msg_clr_cmdline(); /* will reset clear_cmdline */
10061
10062 /* Position on the last line in the window, column 0 */
10063 msg_pos_mode();
10064 cursor_off();
10065 attr = hl_attr(HLF_CM); /* Highlight mode */
10066 if (do_mode)
10067 {
10068 MSG_PUTS_ATTR("--", attr);
10069#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010070 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010071# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010072 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010073# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010074 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010075# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010076 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010077# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 MSG_PUTS_ATTR(" IM", attr);
10079# else
10080 MSG_PUTS_ATTR(" XIM", attr);
10081# endif
10082#endif
10083#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10084 if (gui.in_use)
10085 {
10086 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010087 {
10088 /* HANGUL */
10089 if (enc_utf8)
10090 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10091 else
10092 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 }
10095#endif
10096#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010097 /* CTRL-X in Insert mode */
10098 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
10100 /* These messages can get long, avoid a wrap in a narrow
10101 * window. Prefer showing edit_submode_extra. */
10102 length = (Rows - msg_row) * Columns - 3;
10103 if (edit_submode_extra != NULL)
10104 length -= vim_strsize(edit_submode_extra);
10105 if (length > 0)
10106 {
10107 if (edit_submode_pre != NULL)
10108 length -= vim_strsize(edit_submode_pre);
10109 if (length - vim_strsize(edit_submode) > 0)
10110 {
10111 if (edit_submode_pre != NULL)
10112 msg_puts_attr(edit_submode_pre, attr);
10113 msg_puts_attr(edit_submode, attr);
10114 }
10115 if (edit_submode_extra != NULL)
10116 {
10117 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10118 if ((int)edit_submode_highl < (int)HLF_COUNT)
10119 sub_attr = hl_attr(edit_submode_highl);
10120 else
10121 sub_attr = attr;
10122 msg_puts_attr(edit_submode_extra, sub_attr);
10123 }
10124 }
10125 length = 0;
10126 }
10127 else
10128#endif
10129 {
10130#ifdef FEAT_VREPLACE
10131 if (State & VREPLACE_FLAG)
10132 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10133 else
10134#endif
10135 if (State & REPLACE_FLAG)
10136 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10137 else if (State & INSERT)
10138 {
10139#ifdef FEAT_RIGHTLEFT
10140 if (p_ri)
10141 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10142#endif
10143 MSG_PUTS_ATTR(_(" INSERT"), attr);
10144 }
10145 else if (restart_edit == 'I')
10146 MSG_PUTS_ATTR(_(" (insert)"), attr);
10147 else if (restart_edit == 'R')
10148 MSG_PUTS_ATTR(_(" (replace)"), attr);
10149 else if (restart_edit == 'V')
10150 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10151#ifdef FEAT_RIGHTLEFT
10152 if (p_hkmap)
10153 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10154# ifdef FEAT_FKMAP
10155 if (p_fkmap)
10156 MSG_PUTS_ATTR(farsi_text_5, attr);
10157# endif
10158#endif
10159#ifdef FEAT_KEYMAP
10160 if (State & LANGMAP)
10161 {
10162# ifdef FEAT_ARABIC
10163 if (curwin->w_p_arab)
10164 MSG_PUTS_ATTR(_(" Arabic"), attr);
10165 else
10166# endif
10167 MSG_PUTS_ATTR(_(" (lang)"), attr);
10168 }
10169#endif
10170 if ((State & INSERT) && p_paste)
10171 MSG_PUTS_ATTR(_(" (paste)"), attr);
10172
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 if (VIsual_active)
10174 {
10175 char *p;
10176
10177 /* Don't concatenate separate words to avoid translation
10178 * problems. */
10179 switch ((VIsual_select ? 4 : 0)
10180 + (VIsual_mode == Ctrl_V) * 2
10181 + (VIsual_mode == 'V'))
10182 {
10183 case 0: p = N_(" VISUAL"); break;
10184 case 1: p = N_(" VISUAL LINE"); break;
10185 case 2: p = N_(" VISUAL BLOCK"); break;
10186 case 4: p = N_(" SELECT"); break;
10187 case 5: p = N_(" SELECT LINE"); break;
10188 default: p = N_(" SELECT BLOCK"); break;
10189 }
10190 MSG_PUTS_ATTR(_(p), attr);
10191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 MSG_PUTS_ATTR(" --", attr);
10193 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010194
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 need_clear = TRUE;
10196 }
10197 if (Recording
10198#ifdef FEAT_INS_EXPAND
10199 && edit_submode == NULL /* otherwise it gets too long */
10200#endif
10201 )
10202 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010203 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 need_clear = TRUE;
10205 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010206
10207 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 if (need_clear || clear_cmdline)
10209 msg_clr_eos();
10210 msg_didout = FALSE; /* overwrite this message */
10211 length = msg_col;
10212 msg_col = 0;
10213 need_wait_return = nwr_save; /* never ask for hit-return for this */
10214 }
10215 else if (clear_cmdline && msg_silent == 0)
10216 /* Clear the whole command line. Will reset "clear_cmdline". */
10217 msg_clr_cmdline();
10218
10219#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 /* In Visual mode the size of the selected area must be redrawn. */
10221 if (VIsual_active)
10222 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223
10224 /* If the last window has no status line, the ruler is after the mode
10225 * message and must be redrawn */
10226 if (redrawing()
10227# ifdef FEAT_WINDOWS
10228 && lastwin->w_status_height == 0
10229# endif
10230 )
10231 win_redr_ruler(lastwin, TRUE);
10232#endif
10233 redraw_cmdline = FALSE;
10234 clear_cmdline = FALSE;
10235
10236 return length;
10237}
10238
10239/*
10240 * Position for a mode message.
10241 */
10242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010243msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 msg_col = 0;
10246 msg_row = Rows - 1;
10247}
10248
10249/*
10250 * Delete mode message. Used when ESC is typed which is expected to end
10251 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010252 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 */
10254 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010255unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010258 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 */
10260 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10261 redraw_cmdline = TRUE; /* delete mode later */
10262 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010263 clearmode();
10264}
10265
10266/*
10267 * Clear the mode message.
10268 */
10269 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010270clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010271{
10272 msg_pos_mode();
10273 if (Recording)
10274 recording_mode(hl_attr(HLF_CM));
10275 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276}
10277
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010278 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010279recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010280{
10281 MSG_PUTS_ATTR(_("recording"), attr);
10282 if (!shortmess(SHM_RECORDING))
10283 {
10284 char_u s[4];
10285 sprintf((char *)s, " @%c", Recording);
10286 MSG_PUTS_ATTR(s, attr);
10287 }
10288}
10289
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010290#if defined(FEAT_WINDOWS)
10291/*
10292 * Draw the tab pages line at the top of the Vim window.
10293 */
10294 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010295draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010296{
10297 int tabcount = 0;
10298 tabpage_T *tp;
10299 int tabwidth;
10300 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010301 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010302 int attr;
10303 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010304 win_T *cwp;
10305 int wincount;
10306 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010307 int c;
10308 int len;
10309 int attr_sel = hl_attr(HLF_TPS);
10310 int attr_nosel = hl_attr(HLF_TP);
10311 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010312 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010313 int room;
10314 int use_sep_chars = (t_colors < 8
10315#ifdef FEAT_GUI
10316 && !gui.in_use
10317#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010318#ifdef FEAT_TERMGUICOLORS
10319 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010320#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010321 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010322
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010323 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010324
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010325#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010326 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010327 if (gui_use_tabline())
10328 {
10329 gui_update_tabline();
10330 return;
10331 }
10332#endif
10333
10334 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010335 return;
10336
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010337#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010338
10339 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10340 for (scol = 0; scol < Columns; ++scol)
10341 TabPageIdxs[scol] = 0;
10342
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010343 /* Use the 'tabline' option if it's set. */
10344 if (*p_tal != NUL)
10345 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010346 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010347
10348 /* Check for an error. If there is one we would loop in redrawing the
10349 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010350 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010351 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010352 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010353 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010354 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010355 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010356 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010357 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010358#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010359 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010360 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10361 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010362
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10364 if (tabwidth < 6)
10365 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010366
Bram Moolenaar238a5642006-02-21 22:12:05 +000010367 attr = attr_nosel;
10368 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010369 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010370 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10371 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010372 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010373 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010374
Bram Moolenaar238a5642006-02-21 22:12:05 +000010375 if (tp->tp_topframe == topframe)
10376 attr = attr_sel;
10377 if (use_sep_chars && col > 0)
10378 screen_putchar('|', 0, col++, attr);
10379
10380 if (tp->tp_topframe != topframe)
10381 attr = attr_nosel;
10382
10383 screen_putchar(' ', 0, col++, attr);
10384
10385 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010386 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010387 cwp = curwin;
10388 wp = firstwin;
10389 }
10390 else
10391 {
10392 cwp = tp->tp_curwin;
10393 wp = tp->tp_firstwin;
10394 }
10395
10396 modified = FALSE;
10397 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10398 if (bufIsChanged(wp->w_buffer))
10399 modified = TRUE;
10400 if (modified || wincount > 1)
10401 {
10402 if (wincount > 1)
10403 {
10404 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010405 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010406 if (col + len >= Columns - 3)
10407 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010408 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010409#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010410 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010411#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010412 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010413#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010414 );
10415 col += len;
10416 }
10417 if (modified)
10418 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10419 screen_putchar(' ', 0, col++, attr);
10420 }
10421
10422 room = scol - col + tabwidth - 1;
10423 if (room > 0)
10424 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010425 /* Get buffer name in NameBuff[] */
10426 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010427 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010428 len = vim_strsize(NameBuff);
10429 p = NameBuff;
10430#ifdef FEAT_MBYTE
10431 if (has_mbyte)
10432 while (len > room)
10433 {
10434 len -= ptr2cells(p);
10435 mb_ptr_adv(p);
10436 }
10437 else
10438#endif
10439 if (len > room)
10440 {
10441 p += len - room;
10442 len = room;
10443 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010444 if (len > Columns - col - 1)
10445 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010446
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010447 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010448 col += len;
10449 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010450 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010451
10452 /* Store the tab page number in TabPageIdxs[], so that
10453 * jump_to_mouse() knows where each one is. */
10454 ++tabcount;
10455 while (scol < col)
10456 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010457 }
10458
Bram Moolenaar238a5642006-02-21 22:12:05 +000010459 if (use_sep_chars)
10460 c = '_';
10461 else
10462 c = ' ';
10463 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010464
10465 /* Put an "X" for closing the current tab if there are several. */
10466 if (first_tabpage->tp_next != NULL)
10467 {
10468 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10469 TabPageIdxs[Columns - 1] = -999;
10470 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010471 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010472
10473 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10474 * set. */
10475 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010476}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010477
10478/*
10479 * Get buffer name for "buf" into NameBuff[].
10480 * Takes care of special buffer names and translates special characters.
10481 */
10482 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010483get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010484{
10485 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010486 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010487 else
10488 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10489 trans_characters(NameBuff, MAXPATHL);
10490}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010491#endif
10492
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10494/*
10495 * Get the character to use in a status line. Get its attributes in "*attr".
10496 */
10497 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010498fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500 int fill;
10501 if (is_curwin)
10502 {
10503 *attr = hl_attr(HLF_S);
10504 fill = fill_stl;
10505 }
10506 else
10507 {
10508 *attr = hl_attr(HLF_SNC);
10509 fill = fill_stlnc;
10510 }
10511 /* Use fill when there is highlighting, and highlighting of current
10512 * window differs, or the fillchars differ, or this is not the
10513 * current window */
10514 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10515 || !is_curwin || firstwin == lastwin)
10516 || (fill_stl != fill_stlnc)))
10517 return fill;
10518 if (is_curwin)
10519 return '^';
10520 return '=';
10521}
10522#endif
10523
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010524#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525/*
10526 * Get the character to use in a separator between vertically split windows.
10527 * Get its attributes in "*attr".
10528 */
10529 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010530fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532 *attr = hl_attr(HLF_C);
10533 if (*attr == 0 && fill_vert == ' ')
10534 return '|';
10535 else
10536 return fill_vert;
10537}
10538#endif
10539
10540/*
10541 * Return TRUE if redrawing should currently be done.
10542 */
10543 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010544redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 return (!RedrawingDisabled
10547 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10548}
10549
10550/*
10551 * Return TRUE if printing messages should currently be done.
10552 */
10553 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010554messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555{
10556 return (!(p_lz && char_avail() && !KeyTyped));
10557}
10558
10559/*
10560 * Show current status info in ruler and various other places
10561 * If always is FALSE, only show ruler if position has changed.
10562 */
10563 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010564showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565{
10566 if (!always && !redrawing())
10567 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010568#ifdef FEAT_INS_EXPAND
10569 if (pum_visible())
10570 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010571# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010572 /* Don't redraw right now, do it later. */
10573 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010574# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010575 return;
10576 }
10577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010579 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010580 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010581 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 else
10584#endif
10585#ifdef FEAT_CMDL_INFO
10586 win_redr_ruler(curwin, always);
10587#endif
10588
10589#ifdef FEAT_TITLE
10590 if (need_maketitle
10591# ifdef FEAT_STL_OPT
10592 || (p_icon && (stl_syntax & STL_IN_ICON))
10593 || (p_title && (stl_syntax & STL_IN_TITLE))
10594# endif
10595 )
10596 maketitle();
10597#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010598#ifdef FEAT_WINDOWS
10599 /* Redraw the tab pages line if needed. */
10600 if (redraw_tabline)
10601 draw_tabline();
10602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603}
10604
10605#ifdef FEAT_CMDL_INFO
10606 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010607win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010609#define RULER_BUF_LEN 70
10610 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 int row;
10612 int fillchar;
10613 int attr;
10614 int empty_line = FALSE;
10615 colnr_T virtcol;
10616 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010617 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010619#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 int this_ru_col;
10621 int off = 0;
10622 int width = Columns;
10623# define WITH_OFF(x) x
10624# define WITH_WIDTH(x) x
10625#else
10626# define WITH_OFF(x) 0
10627# define WITH_WIDTH(x) Columns
10628# define this_ru_col ru_col
10629#endif
10630
10631 /* If 'ruler' off or redrawing disabled, don't do anything */
10632 if (!p_ru)
10633 return;
10634
10635 /*
10636 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10637 * after deleting lines, before cursor.lnum is corrected.
10638 */
10639 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10640 return;
10641
10642#ifdef FEAT_INS_EXPAND
10643 /* Don't draw the ruler while doing insert-completion, it might overwrite
10644 * the (long) mode message. */
10645# ifdef FEAT_WINDOWS
10646 if (wp == lastwin && lastwin->w_status_height == 0)
10647# endif
10648 if (edit_submode != NULL)
10649 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010650 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10651 if (pum_visible())
10652 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653#endif
10654
10655#ifdef FEAT_STL_OPT
10656 if (*p_ruf)
10657 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010658 int save_called_emsg = called_emsg;
10659
10660 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010662 if (called_emsg)
10663 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010664 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010665 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 return;
10667 }
10668#endif
10669
10670 /*
10671 * Check if not in Insert mode and the line is empty (will show "0-1").
10672 */
10673 if (!(State & INSERT)
10674 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10675 empty_line = TRUE;
10676
10677 /*
10678 * Only draw the ruler when something changed.
10679 */
10680 validate_virtcol_win(wp);
10681 if ( redraw_cmdline
10682 || always
10683 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10684 || wp->w_cursor.col != wp->w_ru_cursor.col
10685 || wp->w_virtcol != wp->w_ru_virtcol
10686#ifdef FEAT_VIRTUALEDIT
10687 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10688#endif
10689 || wp->w_topline != wp->w_ru_topline
10690 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10691#ifdef FEAT_DIFF
10692 || wp->w_topfill != wp->w_ru_topfill
10693#endif
10694 || empty_line != wp->w_ru_empty)
10695 {
10696 cursor_off();
10697#ifdef FEAT_WINDOWS
10698 if (wp->w_status_height)
10699 {
10700 row = W_WINROW(wp) + wp->w_height;
10701 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 off = W_WINCOL(wp);
10703 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 }
10705 else
10706#endif
10707 {
10708 row = Rows - 1;
10709 fillchar = ' ';
10710 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010711#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 width = Columns;
10713 off = 0;
10714#endif
10715 }
10716
10717 /* In list mode virtcol needs to be recomputed */
10718 virtcol = wp->w_virtcol;
10719 if (wp->w_p_list && lcs_tab1 == NUL)
10720 {
10721 wp->w_p_list = FALSE;
10722 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10723 wp->w_p_list = TRUE;
10724 }
10725
10726 /*
10727 * Some sprintfs return the length, some return a pointer.
10728 * To avoid portability problems we use strlen() here.
10729 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010730 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10732 ? 0L
10733 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010734 len = STRLEN(buffer);
10735 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10737 (int)virtcol + 1);
10738
10739 /*
10740 * Add a "50%" if there is room for it.
10741 * On the last line, don't print in the last column (scrolls the
10742 * screen up on some terminals).
10743 */
10744 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010745 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 o = i + vim_strsize(buffer + i + 1);
10747#ifdef FEAT_WINDOWS
10748 if (wp->w_status_height == 0) /* can't use last char of screen */
10749#endif
10750 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010751#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 this_ru_col = ru_col - (Columns - width);
10753 if (this_ru_col < 0)
10754 this_ru_col = 0;
10755#endif
10756 /* Never use more than half the window/screen width, leave the other
10757 * half for the filename. */
10758 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10759 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10760 if (this_ru_col + o < WITH_WIDTH(width))
10761 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010762 /* need at least 3 chars left for get_rel_pos() + NUL */
10763 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 {
10765#ifdef FEAT_MBYTE
10766 if (has_mbyte)
10767 i += (*mb_char2bytes)(fillchar, buffer + i);
10768 else
10769#endif
10770 buffer[i++] = fillchar;
10771 ++o;
10772 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010773 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 }
10775 /* Truncate at window boundary. */
10776#ifdef FEAT_MBYTE
10777 if (has_mbyte)
10778 {
10779 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010780 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 {
10782 o += (*mb_ptr2cells)(buffer + i);
10783 if (this_ru_col + o > WITH_WIDTH(width))
10784 {
10785 buffer[i] = NUL;
10786 break;
10787 }
10788 }
10789 }
10790 else
10791#endif
10792 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10793 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10794
10795 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10796 i = redraw_cmdline;
10797 screen_fill(row, row + 1,
10798 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10799 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10800 fillchar, fillchar, attr);
10801 /* don't redraw the cmdline because of showing the ruler */
10802 redraw_cmdline = i;
10803 wp->w_ru_cursor = wp->w_cursor;
10804 wp->w_ru_virtcol = wp->w_virtcol;
10805 wp->w_ru_empty = empty_line;
10806 wp->w_ru_topline = wp->w_topline;
10807 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10808#ifdef FEAT_DIFF
10809 wp->w_ru_topfill = wp->w_topfill;
10810#endif
10811 }
10812}
10813#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010814
10815#if defined(FEAT_LINEBREAK) || defined(PROTO)
10816/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010817 * Return the width of the 'number' and 'relativenumber' column.
10818 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010819 * Otherwise it depends on 'numberwidth' and the line count.
10820 */
10821 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010822number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010823{
10824 int n;
10825 linenr_T lnum;
10826
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010827 if (wp->w_p_rnu && !wp->w_p_nu)
10828 /* cursor line shows "0" */
10829 lnum = wp->w_height;
10830 else
10831 /* cursor line shows absolute line number */
10832 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010833
Bram Moolenaar6b314672015-03-20 15:42:10 +010010834 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010835 return wp->w_nrwidth_width;
10836 wp->w_nrwidth_line_count = lnum;
10837
10838 n = 0;
10839 do
10840 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010841 lnum /= 10;
10842 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010843 } while (lnum > 0);
10844
10845 /* 'numberwidth' gives the minimal width plus one */
10846 if (n < wp->w_p_nuw - 1)
10847 n = wp->w_p_nuw - 1;
10848
10849 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010850 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010851 return n;
10852}
10853#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010854
10855/*
10856 * Return the current cursor column. This is the actual position on the
10857 * screen. First column is 0.
10858 */
10859 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010860screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010861{
10862 return screen_cur_col;
10863}
10864
10865/*
10866 * Return the current cursor row. This is the actual position on the screen.
10867 * First row is 0.
10868 */
10869 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010870screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010871{
10872 return screen_cur_row;
10873}