blob: 2e425cba4c5d14030992725e221a2d12b32aca5f [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
419redraw_after_callback()
420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
425 else if ((State & NORMAL) || (State & INSERT))
426 {
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 {
435 gui_update_cursor(TRUE, FALSE);
436 gui_mch_flush();
437 }
438#endif
439}
440
441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 * Changed something in the current window, at buffer line "lnum", that
443 * requires that line and possibly other lines to be redrawn.
444 * Used when entering/leaving Insert mode with the cursor on a folded line.
445 * Used to remove the "$" from a change command.
446 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
447 * may become invalid and the whole window will have to be redrawn.
448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100450redrawWinline(
451 linenr_T lnum,
452 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454#ifdef FEAT_FOLDING
455 int i;
456#endif
457
458 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
459 curwin->w_redraw_top = lnum;
460 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
461 curwin->w_redraw_bot = lnum;
462 redraw_later(VALID);
463
464#ifdef FEAT_FOLDING
465 if (invalid)
466 {
467 /* A w_lines[] entry for this lnum has become invalid. */
468 i = find_wl_entry(curwin, lnum);
469 if (i >= 0)
470 curwin->w_lines[i].wl_valid = FALSE;
471 }
472#endif
473}
474
475/*
476 * update all windows that are editing the current buffer
477 */
478 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100479update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
481 redraw_curbuf_later(type);
482 update_screen(type);
483}
484
485/*
486 * update_screen()
487 *
488 * Based on the current value of curwin->w_topline, transfer a screenfull
489 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
490 */
491 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100492update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 win_T *wp;
495 static int did_intro = FALSE;
496#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
497 int did_one;
498#endif
499
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000500 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (!screen_valid(TRUE))
502 return;
503
504 if (must_redraw)
505 {
506 if (type < must_redraw) /* use maximal type */
507 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000508
509 /* must_redraw is reset here, so that when we run into some weird
510 * reason to redraw while busy redrawing (e.g., asynchronous
511 * scrolling), or update_topline() in win_update() will cause a
512 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 must_redraw = 0;
514 }
515
516 /* Need to update w_lines[]. */
517 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
518 type = NOT_VALID;
519
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000520 /* Postpone the redrawing when it's not needed and when being called
521 * recursively. */
522 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 redraw_later(type); /* remember type for next time */
525 must_redraw = type;
526 if (type > INVERTED_ALL)
527 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
528 return;
529 }
530
531 updating_screen = TRUE;
532#ifdef FEAT_SYN_HL
533 ++display_tick; /* let syntax code know we're in a next round of
534 * display updating */
535#endif
536
537 /*
538 * if the screen was scrolled up when displaying a message, scroll it down
539 */
540 if (msg_scrolled)
541 {
542 clear_cmdline = TRUE;
543 if (msg_scrolled > Rows - 5) /* clearing is faster */
544 type = CLEAR;
545 else if (type != CLEAR)
546 {
547 check_for_delay(FALSE);
548 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
549 type = CLEAR;
550 FOR_ALL_WINDOWS(wp)
551 {
552 if (W_WINROW(wp) < msg_scrolled)
553 {
554 if (W_WINROW(wp) + wp->w_height > msg_scrolled
555 && wp->w_redr_type < REDRAW_TOP
556 && wp->w_lines_valid > 0
557 && wp->w_topline == wp->w_lines[0].wl_lnum)
558 {
559 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
560 wp->w_redr_type = REDRAW_TOP;
561 }
562 else
563 {
564 wp->w_redr_type = NOT_VALID;
565#ifdef FEAT_WINDOWS
566 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
567 <= msg_scrolled)
568 wp->w_redr_status = TRUE;
569#endif
570 }
571 }
572 }
573 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000574#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000575 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578 msg_scrolled = 0;
579 need_wait_return = FALSE;
580 }
581
582 /* reset cmdline_row now (may have been changed temporarily) */
583 compute_cmdrow();
584
585 /* Check for changed highlighting */
586 if (need_highlight_changed)
587 highlight_changed();
588
589 if (type == CLEAR) /* first clear screen */
590 {
591 screenclear(); /* will reset clear_cmdline */
592 type = NOT_VALID;
593 }
594
595 if (clear_cmdline) /* going to clear cmdline (done below) */
596 check_for_delay(FALSE);
597
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000598#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200599 /* Force redraw when width of 'number' or 'relativenumber' column
600 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000601 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200602 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
603 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604 curwin->w_redr_type = NOT_VALID;
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 /*
608 * Only start redrawing if there is really something to do.
609 */
610 if (type == INVERTED)
611 update_curswant();
612 if (curwin->w_redr_type < type
613 && !((type == VALID
614 && curwin->w_lines[0].wl_valid
615#ifdef FEAT_DIFF
616 && curwin->w_topfill == curwin->w_old_topfill
617 && curwin->w_botfill == curwin->w_old_botfill
618#endif
619 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000621 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
623 && curwin->w_old_visual_mode == VIsual_mode
624 && (curwin->w_valid & VALID_VIRTCOL)
625 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ))
627 curwin->w_redr_type = type;
628
Bram Moolenaar5a305422006-04-28 22:38:25 +0000629#ifdef FEAT_WINDOWS
630 /* Redraw the tab pages line if needed. */
631 if (redraw_tabline || type >= NOT_VALID)
632 draw_tabline();
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_SYN_HL
636 /*
637 * Correct stored syntax highlighting info for changes in each displayed
638 * buffer. Each buffer must only be done once.
639 */
640 FOR_ALL_WINDOWS(wp)
641 {
642 if (wp->w_buffer->b_mod_set)
643 {
644# ifdef FEAT_WINDOWS
645 win_T *wwp;
646
647 /* Check if we already did this buffer. */
648 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
649 if (wwp->w_buffer == wp->w_buffer)
650 break;
651# endif
652 if (
653# ifdef FEAT_WINDOWS
654 wwp == wp &&
655# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200656 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 syn_stack_apply_changes(wp->w_buffer);
658 }
659 }
660#endif
661
662 /*
663 * Go from top to bottom through the windows, redrawing the ones that need
664 * it.
665 */
666#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
667 did_one = FALSE;
668#endif
669#ifdef FEAT_SEARCH_EXTRA
670 search_hl.rm.regprog = NULL;
671#endif
672 FOR_ALL_WINDOWS(wp)
673 {
674 if (wp->w_redr_type != 0)
675 {
676 cursor_off();
677#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
678 if (!did_one)
679 {
680 did_one = TRUE;
681# ifdef FEAT_SEARCH_EXTRA
682 start_search_hl();
683# endif
684# ifdef FEAT_CLIPBOARD
685 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200686 if (clip_star.available && clip_isautosel_star())
687 clip_update_selection(&clip_star);
688 if (clip_plus.available && clip_isautosel_plus())
689 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690# endif
691#ifdef FEAT_GUI
692 /* Remove the cursor before starting to do anything, because
693 * scrolling may make it difficult to redraw the text under
694 * it. */
695 if (gui.in_use)
696 gui_undraw_cursor();
697#endif
698 }
699#endif
700 win_update(wp);
701 }
702
703#ifdef FEAT_WINDOWS
704 /* redraw status line after the window to minimize cursor movement */
705 if (wp->w_redr_status)
706 {
707 cursor_off();
708 win_redr_status(wp);
709 }
710#endif
711 }
712#if defined(FEAT_SEARCH_EXTRA)
713 end_search_hl();
714#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100715#ifdef FEAT_INS_EXPAND
716 /* May need to redraw the popup menu. */
717 if (pum_visible())
718 pum_redraw();
719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721#ifdef FEAT_WINDOWS
722 /* Reset b_mod_set flags. Going through all windows is probably faster
723 * than going through all buffers (there could be many buffers). */
724 for (wp = firstwin; wp != NULL; wp = wp->w_next)
725 wp->w_buffer->b_mod_set = FALSE;
726#else
727 curbuf->b_mod_set = FALSE;
728#endif
729
730 updating_screen = FALSE;
731#ifdef FEAT_GUI
732 gui_may_resize_shell();
733#endif
734
735 /* Clear or redraw the command line. Done last, because scrolling may
736 * mess up the command line. */
737 if (clear_cmdline || redraw_cmdline)
738 showmode();
739
740 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200741 if (!did_intro)
742 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 did_intro = TRUE;
744
745#ifdef FEAT_GUI
746 /* Redraw the cursor and update the scrollbars when all screen updating is
747 * done. */
748 if (gui.in_use)
749 {
750 out_flush(); /* required before updating the cursor */
751 if (did_one)
752 gui_update_cursor(FALSE, FALSE);
753 gui_update_scrollbars(FALSE);
754 }
755#endif
756}
757
Bram Moolenaar860cae12010-06-05 23:22:07 +0200758#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200759/*
760 * Return TRUE if the cursor line in window "wp" may be concealed, according
761 * to the 'concealcursor' option.
762 */
763 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100764conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200765{
766 int c;
767
768 if (*wp->w_p_cocu == NUL)
769 return FALSE;
770 if (get_real_state() & VISUAL)
771 c = 'v';
772 else if (State & INSERT)
773 c = 'i';
774 else if (State & NORMAL)
775 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200776 else if (State & CMDLINE)
777 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200778 else
779 return FALSE;
780 return vim_strchr(wp->w_p_cocu, c) != NULL;
781}
782
783/*
784 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
785 */
786 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100787conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200788{
789 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
790 {
791 need_cursor_line_redraw = TRUE;
792 /* Need to recompute cursor column, e.g., when starting Visual mode
793 * without concealing. */
794 curs_columns(TRUE);
795 }
796}
797
Bram Moolenaar860cae12010-06-05 23:22:07 +0200798 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100799update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200800{
801 int row;
802 int j;
803
Bram Moolenaar908be432016-05-24 10:51:30 +0200804 /* Don't do anything if the screen structures are (not yet) valid. */
805 if (!screen_valid(TRUE))
806 return;
807
Bram Moolenaar860cae12010-06-05 23:22:07 +0200808 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200809 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200810 {
811# ifdef FEAT_GUI
812 /* Remove the cursor before starting to do anything, because scrolling
813 * may make it difficult to redraw the text under it. */
814 if (gui.in_use)
815 gui_undraw_cursor();
816# endif
817 row = 0;
818 for (j = 0; j < wp->w_lines_valid; ++j)
819 {
820 if (lnum == wp->w_lines[j].wl_lnum)
821 {
822 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200823# ifdef FEAT_SEARCH_EXTRA
824 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200825 start_search_hl();
826 prepare_search_hl(wp, lnum);
827# endif
828 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
829# if defined(FEAT_SEARCH_EXTRA)
830 end_search_hl();
831# endif
832 break;
833 }
834 row += wp->w_lines[j].wl_size;
835 }
836# ifdef FEAT_GUI
837 /* Redraw the cursor */
838 if (gui.in_use)
839 {
840 out_flush(); /* required before updating the cursor */
841 gui_update_cursor(FALSE, FALSE);
842 }
843# endif
844 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200845 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200846}
847#endif
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100850static void update_prepare(void);
851static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
853/*
854 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000855 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 */
857 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100858update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
860 cursor_off();
861 updating_screen = TRUE;
862#ifdef FEAT_GUI
863 /* Remove the cursor before starting to do anything, because scrolling may
864 * make it difficult to redraw the text under it. */
865 if (gui.in_use)
866 gui_undraw_cursor();
867#endif
868#ifdef FEAT_SEARCH_EXTRA
869 start_search_hl();
870#endif
871}
872
873/*
874 * Finish updating one or more windows.
875 */
876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100877update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 if (redraw_cmdline)
880 showmode();
881
882# ifdef FEAT_SEARCH_EXTRA
883 end_search_hl();
884# endif
885
886 updating_screen = FALSE;
887
888# ifdef FEAT_GUI
889 gui_may_resize_shell();
890
891 /* Redraw the cursor and update the scrollbars when all screen updating is
892 * done. */
893 if (gui.in_use)
894 {
895 out_flush(); /* required before updating the cursor */
896 gui_update_cursor(FALSE, FALSE);
897 gui_update_scrollbars(FALSE);
898 }
899# endif
900}
901#endif
902
903#if defined(FEAT_SIGNS) || defined(PROTO)
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 win_T *wp;
908 int doit = FALSE;
909
910# ifdef FEAT_FOLDING
911 win_foldinfo.fi_level = 0;
912# endif
913
914 /* update/delete a specific mark */
915 FOR_ALL_WINDOWS(wp)
916 {
917 if (buf != NULL && lnum > 0)
918 {
919 if (wp->w_buffer == buf && lnum >= wp->w_topline
920 && lnum < wp->w_botline)
921 {
922 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
923 wp->w_redraw_top = lnum;
924 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
925 wp->w_redraw_bot = lnum;
926 redraw_win_later(wp, VALID);
927 }
928 }
929 else
930 redraw_win_later(wp, VALID);
931 if (wp->w_redr_type != 0)
932 doit = TRUE;
933 }
934
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100935 /* Return when there is nothing to do, screen updating is already
936 * happening (recursive call) or still starting up. */
937 if (!doit || updating_screen
938#ifdef FEAT_GUI
939 || gui.starting
940#endif
941 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 return;
943
944 /* update all windows that need updating */
945 update_prepare();
946
947# ifdef FEAT_WINDOWS
948 for (wp = firstwin; wp; wp = wp->w_next)
949 {
950 if (wp->w_redr_type != 0)
951 win_update(wp);
952 if (wp->w_redr_status)
953 win_redr_status(wp);
954 }
955# else
956 if (curwin->w_redr_type != 0)
957 win_update(curwin);
958# endif
959
960 update_finish();
961}
962#endif
963
964
965#if defined(FEAT_GUI) || defined(PROTO)
966/*
967 * Update a single window, its status line and maybe the command line msg.
968 * Used for the GUI scrollbar.
969 */
970 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100971updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000973 /* return if already busy updating */
974 if (updating_screen)
975 return;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 update_prepare();
978
979#ifdef FEAT_CLIPBOARD
980 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200981 if (clip_star.available && clip_isautosel_star())
982 clip_update_selection(&clip_star);
983 if (clip_plus.available && clip_isautosel_plus())
984 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000990 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000991 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000992 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (wp->w_redr_status
995# ifdef FEAT_CMDL_INFO
996 || p_ru
997# endif
998# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000999 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000# endif
1001 )
1002 win_redr_status(wp);
1003#endif
1004
1005 update_finish();
1006}
1007#endif
1008
1009/*
1010 * Update a single window.
1011 *
1012 * This may cause the windows below it also to be redrawn (when clearing the
1013 * screen or scrolling lines).
1014 *
1015 * How the window is redrawn depends on wp->w_redr_type. Each type also
1016 * implies the one below it.
1017 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001018 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1020 * INVERTED redraw the changed part of the Visual area
1021 * INVERTED_ALL redraw the whole Visual area
1022 * VALID 1. scroll up/down to adjust for a changed w_topline
1023 * 2. update lines at the top when scrolled down
1024 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001025 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * b_mod_top and b_mod_bot.
1027 * - if wp->w_redraw_top non-zero, redraw lines between
1028 * wp->w_redraw_top and wp->w_redr_bot.
1029 * - continue redrawing when syntax status is invalid.
1030 * 4. if scrolled up, update lines at the bottom.
1031 * This results in three areas that may need updating:
1032 * top: from first row to top_end (when scrolled down)
1033 * mid: from mid_start to mid_end (update inversion or changed text)
1034 * bot: from bot_start to last row (when scrolled up)
1035 */
1036 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001037win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
1039 buf_T *buf = wp->w_buffer;
1040 int type;
1041 int top_end = 0; /* Below last row of the top area that needs
1042 updating. 0 when no top area updating. */
1043 int mid_start = 999;/* first row of the mid area that needs
1044 updating. 999 when no mid area updating. */
1045 int mid_end = 0; /* Below last row of the mid area that needs
1046 updating. 0 when no mid area updating. */
1047 int bot_start = 999;/* first row of the bot area that needs
1048 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 int scrolled_down = FALSE; /* TRUE when scrolled down when
1050 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001052 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 int top_to_mod = FALSE; /* redraw above mod_top */
1054#endif
1055
1056 int row; /* current window row to display */
1057 linenr_T lnum; /* current buffer lnum to display */
1058 int idx; /* current index in w_lines[] */
1059 int srow; /* starting row of the current line */
1060
1061 int eof = FALSE; /* if TRUE, we hit the end of the file */
1062 int didline = FALSE; /* if TRUE, we finished the last line */
1063 int i;
1064 long j;
1065 static int recursive = FALSE; /* being called recursively */
1066 int old_botline = wp->w_botline;
1067#ifdef FEAT_FOLDING
1068 long fold_count;
1069#endif
1070#ifdef FEAT_SYN_HL
1071 /* remember what happened to the previous line, to know if
1072 * check_visual_highlight() can be used */
1073#define DID_NONE 1 /* didn't update a line */
1074#define DID_LINE 2 /* updated a normal line */
1075#define DID_FOLD 3 /* updated a folded line */
1076 int did_update = DID_NONE;
1077 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1078#endif
1079 linenr_T mod_top = 0;
1080 linenr_T mod_bot = 0;
1081#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1082 int save_got_int;
1083#endif
1084
1085 type = wp->w_redr_type;
1086
1087 if (type == NOT_VALID)
1088 {
1089#ifdef FEAT_WINDOWS
1090 wp->w_redr_status = TRUE;
1091#endif
1092 wp->w_lines_valid = 0;
1093 }
1094
1095 /* Window is zero-height: nothing to draw. */
1096 if (wp->w_height == 0)
1097 {
1098 wp->w_redr_type = 0;
1099 return;
1100 }
1101
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001102#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 /* Window is zero-width: Only need to draw the separator. */
1104 if (wp->w_width == 0)
1105 {
1106 /* draw the vertical separator right of this window */
1107 draw_vsep_win(wp, 0);
1108 wp->w_redr_type = 0;
1109 return;
1110 }
1111#endif
1112
1113#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001114 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
1116
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001117#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001118 /* Force redraw when width of 'number' or 'relativenumber' column
1119 * changes. */
1120 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001121 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001122 {
1123 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001124 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001125 }
1126 else
1127#endif
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1130 {
1131 /*
1132 * When there are both inserted/deleted lines and specific lines to be
1133 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1134 * everything (only happens when redrawing is off for while).
1135 */
1136 type = NOT_VALID;
1137 }
1138 else
1139 {
1140 /*
1141 * Set mod_top to the first line that needs displaying because of
1142 * changes. Set mod_bot to the first line after the changes.
1143 */
1144 mod_top = wp->w_redraw_top;
1145 if (wp->w_redraw_bot != 0)
1146 mod_bot = wp->w_redraw_bot + 1;
1147 else
1148 mod_bot = 0;
1149 wp->w_redraw_top = 0; /* reset for next time */
1150 wp->w_redraw_bot = 0;
1151 if (buf->b_mod_set)
1152 {
1153 if (mod_top == 0 || mod_top > buf->b_mod_top)
1154 {
1155 mod_top = buf->b_mod_top;
1156#ifdef FEAT_SYN_HL
1157 /* Need to redraw lines above the change that may be included
1158 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001159 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001161 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (mod_top < 1)
1163 mod_top = 1;
1164 }
1165#endif
1166 }
1167 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1168 mod_bot = buf->b_mod_bot;
1169
1170#ifdef FEAT_SEARCH_EXTRA
1171 /* When 'hlsearch' is on and using a multi-line search pattern, a
1172 * change in one line may make the Search highlighting in a
1173 * previous line invalid. Simple solution: redraw all visible
1174 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001175 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001177 if (search_hl.rm.regprog != NULL
1178 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001180 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001181 {
1182 cur = wp->w_match_head;
1183 while (cur != NULL)
1184 {
1185 if (cur->match.regprog != NULL
1186 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001187 {
1188 top_to_mod = TRUE;
1189 break;
1190 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001191 cur = cur->next;
1192 }
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#endif
1195 }
1196#ifdef FEAT_FOLDING
1197 if (mod_top != 0 && hasAnyFolding(wp))
1198 {
1199 linenr_T lnumt, lnumb;
1200
1201 /*
1202 * A change in a line can cause lines above it to become folded or
1203 * unfolded. Find the top most buffer line that may be affected.
1204 * If the line was previously folded and displayed, get the first
1205 * line of that fold. If the line is folded now, get the first
1206 * folded line. Use the minimum of these two.
1207 */
1208
1209 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1210 * the line below it. If there is no valid entry, use w_topline.
1211 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1212 * to this line. If there is no valid entry, use MAXLNUM. */
1213 lnumt = wp->w_topline;
1214 lnumb = MAXLNUM;
1215 for (i = 0; i < wp->w_lines_valid; ++i)
1216 if (wp->w_lines[i].wl_valid)
1217 {
1218 if (wp->w_lines[i].wl_lastlnum < mod_top)
1219 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1220 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1221 {
1222 lnumb = wp->w_lines[i].wl_lnum;
1223 /* When there is a fold column it might need updating
1224 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001225 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 ++lnumb;
1227 }
1228 }
1229
1230 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1231 if (mod_top > lnumt)
1232 mod_top = lnumt;
1233
1234 /* Now do the same for the bottom line (one above mod_bot). */
1235 --mod_bot;
1236 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1237 ++mod_bot;
1238 if (mod_bot < lnumb)
1239 mod_bot = lnumb;
1240 }
1241#endif
1242
1243 /* When a change starts above w_topline and the end is below
1244 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001245 * If the end of the change is above w_topline: do like no change was
1246 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (mod_top != 0 && mod_top < wp->w_topline)
1248 {
1249 if (mod_bot > wp->w_topline)
1250 mod_top = wp->w_topline;
1251#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 top_end = 1;
1254#endif
1255 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001256
1257 /* When line numbers are displayed need to redraw all lines below
1258 * inserted/deleted lines. */
1259 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1260 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262
1263 /*
1264 * When only displaying the lines at the top, set top_end. Used when
1265 * window has scrolled down for msg_scrolled.
1266 */
1267 if (type == REDRAW_TOP)
1268 {
1269 j = 0;
1270 for (i = 0; i < wp->w_lines_valid; ++i)
1271 {
1272 j += wp->w_lines[i].wl_size;
1273 if (j >= wp->w_upd_rows)
1274 {
1275 top_end = j;
1276 break;
1277 }
1278 }
1279 if (top_end == 0)
1280 /* not found (cannot happen?): redraw everything */
1281 type = NOT_VALID;
1282 else
1283 /* top area defined, the rest is VALID */
1284 type = VALID;
1285 }
1286
Bram Moolenaar367329b2007-08-30 11:53:22 +00001287 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001288 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1289 * non-zero and thus not FALSE) will indicate that screenclear() was not
1290 * called. */
1291 if (screen_cleared)
1292 screen_cleared = MAYBE;
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 /*
1295 * If there are no changes on the screen that require a complete redraw,
1296 * handle three cases:
1297 * 1: we are off the top of the screen by a few lines: scroll down
1298 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1299 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1300 * w_lines[] that needs updating.
1301 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001302 if ((type == VALID || type == SOME_VALID
1303 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#ifdef FEAT_DIFF
1305 && !wp->w_botfill && !wp->w_old_botfill
1306#endif
1307 )
1308 {
1309 if (mod_top != 0 && wp->w_topline == mod_top)
1310 {
1311 /*
1312 * w_topline is the first changed line, the scrolling will be done
1313 * further down.
1314 */
1315 }
1316 else if (wp->w_lines[0].wl_valid
1317 && (wp->w_topline < wp->w_lines[0].wl_lnum
1318#ifdef FEAT_DIFF
1319 || (wp->w_topline == wp->w_lines[0].wl_lnum
1320 && wp->w_topfill > wp->w_old_topfill)
1321#endif
1322 ))
1323 {
1324 /*
1325 * New topline is above old topline: May scroll down.
1326 */
1327#ifdef FEAT_FOLDING
1328 if (hasAnyFolding(wp))
1329 {
1330 linenr_T ln;
1331
1332 /* count the number of lines we are off, counting a sequence
1333 * of folded lines as one */
1334 j = 0;
1335 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1336 {
1337 ++j;
1338 if (j >= wp->w_height - 2)
1339 break;
1340 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1341 }
1342 }
1343 else
1344#endif
1345 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1346 if (j < wp->w_height - 2) /* not too far off */
1347 {
1348 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1349#ifdef FEAT_DIFF
1350 /* insert extra lines for previously invisible filler lines */
1351 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1352 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1353 - wp->w_old_topfill;
1354#endif
1355 if (i < wp->w_height - 2) /* less than a screen off */
1356 {
1357 /*
1358 * Try to insert the correct number of lines.
1359 * If not the last window, delete the lines at the bottom.
1360 * win_ins_lines may fail when the terminal can't do it.
1361 */
1362 if (i > 0)
1363 check_for_delay(FALSE);
1364 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1365 {
1366 if (wp->w_lines_valid != 0)
1367 {
1368 /* Need to update rows that are new, stop at the
1369 * first one that scrolled down. */
1370 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 /* Move the entries that were scrolled, disable
1374 * the entries for the lines to be redrawn. */
1375 if ((wp->w_lines_valid += j) > wp->w_height)
1376 wp->w_lines_valid = wp->w_height;
1377 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1378 wp->w_lines[idx] = wp->w_lines[idx - j];
1379 while (idx >= 0)
1380 wp->w_lines[idx--].wl_valid = FALSE;
1381 }
1382 }
1383 else
1384 mid_start = 0; /* redraw all lines */
1385 }
1386 else
1387 mid_start = 0; /* redraw all lines */
1388 }
1389 else
1390 mid_start = 0; /* redraw all lines */
1391 }
1392 else
1393 {
1394 /*
1395 * New topline is at or below old topline: May scroll up.
1396 * When topline didn't change, find first entry in w_lines[] that
1397 * needs updating.
1398 */
1399
1400 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1401 j = -1;
1402 row = 0;
1403 for (i = 0; i < wp->w_lines_valid; i++)
1404 {
1405 if (wp->w_lines[i].wl_valid
1406 && wp->w_lines[i].wl_lnum == wp->w_topline)
1407 {
1408 j = i;
1409 break;
1410 }
1411 row += wp->w_lines[i].wl_size;
1412 }
1413 if (j == -1)
1414 {
1415 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1416 * lines */
1417 mid_start = 0;
1418 }
1419 else
1420 {
1421 /*
1422 * Try to delete the correct number of lines.
1423 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1424 */
1425#ifdef FEAT_DIFF
1426 /* If the topline didn't change, delete old filler lines,
1427 * otherwise delete filler lines of the new topline... */
1428 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1429 row += wp->w_old_topfill;
1430 else
1431 row += diff_check_fill(wp, wp->w_topline);
1432 /* ... but don't delete new filler lines. */
1433 row -= wp->w_topfill;
1434#endif
1435 if (row > 0)
1436 {
1437 check_for_delay(FALSE);
1438 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1439 bot_start = wp->w_height - row;
1440 else
1441 mid_start = 0; /* redraw all lines */
1442 }
1443 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1444 {
1445 /*
1446 * Skip the lines (below the deleted lines) that are still
1447 * valid and don't need redrawing. Copy their info
1448 * upwards, to compensate for the deleted lines. Set
1449 * bot_start to the first row that needs redrawing.
1450 */
1451 bot_start = 0;
1452 idx = 0;
1453 for (;;)
1454 {
1455 wp->w_lines[idx] = wp->w_lines[j];
1456 /* stop at line that didn't fit, unless it is still
1457 * valid (no lines deleted) */
1458 if (row > 0 && bot_start + row
1459 + (int)wp->w_lines[j].wl_size > wp->w_height)
1460 {
1461 wp->w_lines_valid = idx + 1;
1462 break;
1463 }
1464 bot_start += wp->w_lines[idx++].wl_size;
1465
1466 /* stop at the last valid entry in w_lines[].wl_size */
1467 if (++j >= wp->w_lines_valid)
1468 {
1469 wp->w_lines_valid = idx;
1470 break;
1471 }
1472 }
1473#ifdef FEAT_DIFF
1474 /* Correct the first entry for filler lines at the top
1475 * when it won't get updated below. */
1476 if (wp->w_p_diff && bot_start > 0)
1477 wp->w_lines[0].wl_size =
1478 plines_win_nofill(wp, wp->w_topline, TRUE)
1479 + wp->w_topfill;
1480#endif
1481 }
1482 }
1483 }
1484
1485 /* When starting redraw in the first line, redraw all lines. When
1486 * there is only one window it's probably faster to clear the screen
1487 * first. */
1488 if (mid_start == 0)
1489 {
1490 mid_end = wp->w_height;
1491 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001492 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001493 /* Clear the screen when it was not done by win_del_lines() or
1494 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1495 * then. */
1496 if (screen_cleared != TRUE)
1497 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001498#ifdef FEAT_WINDOWS
1499 /* The screen was cleared, redraw the tab pages line. */
1500 if (redraw_tabline)
1501 draw_tabline();
1502#endif
1503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001505
1506 /* When win_del_lines() or win_ins_lines() caused the screen to be
1507 * cleared (only happens for the first window) or when screenclear()
1508 * was called directly above, "must_redraw" will have been set to
1509 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1510 if (screen_cleared == TRUE)
1511 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 else
1514 {
1515 /* Not VALID or INVERTED: redraw all lines. */
1516 mid_start = 0;
1517 mid_end = wp->w_height;
1518 }
1519
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001520 if (type == SOME_VALID)
1521 {
1522 /* SOME_VALID: redraw all lines. */
1523 mid_start = 0;
1524 mid_end = wp->w_height;
1525 type = NOT_VALID;
1526 }
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 /* check if we are updating or removing the inverted part */
1529 if ((VIsual_active && buf == curwin->w_buffer)
1530 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1531 {
1532 linenr_T from, to;
1533
1534 if (VIsual_active)
1535 {
1536 if (VIsual_active
1537 && (VIsual_mode != wp->w_old_visual_mode
1538 || type == INVERTED_ALL))
1539 {
1540 /*
1541 * If the type of Visual selection changed, redraw the whole
1542 * selection. Also when the ownership of the X selection is
1543 * gained or lost.
1544 */
1545 if (curwin->w_cursor.lnum < VIsual.lnum)
1546 {
1547 from = curwin->w_cursor.lnum;
1548 to = VIsual.lnum;
1549 }
1550 else
1551 {
1552 from = VIsual.lnum;
1553 to = curwin->w_cursor.lnum;
1554 }
1555 /* redraw more when the cursor moved as well */
1556 if (wp->w_old_cursor_lnum < from)
1557 from = wp->w_old_cursor_lnum;
1558 if (wp->w_old_cursor_lnum > to)
1559 to = wp->w_old_cursor_lnum;
1560 if (wp->w_old_visual_lnum < from)
1561 from = wp->w_old_visual_lnum;
1562 if (wp->w_old_visual_lnum > to)
1563 to = wp->w_old_visual_lnum;
1564 }
1565 else
1566 {
1567 /*
1568 * Find the line numbers that need to be updated: The lines
1569 * between the old cursor position and the current cursor
1570 * position. Also check if the Visual position changed.
1571 */
1572 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1573 {
1574 from = curwin->w_cursor.lnum;
1575 to = wp->w_old_cursor_lnum;
1576 }
1577 else
1578 {
1579 from = wp->w_old_cursor_lnum;
1580 to = curwin->w_cursor.lnum;
1581 if (from == 0) /* Visual mode just started */
1582 from = to;
1583 }
1584
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001585 if (VIsual.lnum != wp->w_old_visual_lnum
1586 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 if (wp->w_old_visual_lnum < from
1589 && wp->w_old_visual_lnum != 0)
1590 from = wp->w_old_visual_lnum;
1591 if (wp->w_old_visual_lnum > to)
1592 to = wp->w_old_visual_lnum;
1593 if (VIsual.lnum < from)
1594 from = VIsual.lnum;
1595 if (VIsual.lnum > to)
1596 to = VIsual.lnum;
1597 }
1598 }
1599
1600 /*
1601 * If in block mode and changed column or curwin->w_curswant:
1602 * update all lines.
1603 * First compute the actual start and end column.
1604 */
1605 if (VIsual_mode == Ctrl_V)
1606 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001607 colnr_T fromc, toc;
1608#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1609 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar404406a2014-10-09 13:24:43 +02001611 if (curwin->w_p_lbr)
1612 ve_flags = VE_ALL;
1613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001615#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1616 ve_flags = save_ve_flags;
1617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 ++toc;
1619 if (curwin->w_curswant == MAXCOL)
1620 toc = MAXCOL;
1621
1622 if (fromc != wp->w_old_cursor_fcol
1623 || toc != wp->w_old_cursor_lcol)
1624 {
1625 if (from > VIsual.lnum)
1626 from = VIsual.lnum;
1627 if (to < VIsual.lnum)
1628 to = VIsual.lnum;
1629 }
1630 wp->w_old_cursor_fcol = fromc;
1631 wp->w_old_cursor_lcol = toc;
1632 }
1633 }
1634 else
1635 {
1636 /* Use the line numbers of the old Visual area. */
1637 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1638 {
1639 from = wp->w_old_cursor_lnum;
1640 to = wp->w_old_visual_lnum;
1641 }
1642 else
1643 {
1644 from = wp->w_old_visual_lnum;
1645 to = wp->w_old_cursor_lnum;
1646 }
1647 }
1648
1649 /*
1650 * There is no need to update lines above the top of the window.
1651 */
1652 if (from < wp->w_topline)
1653 from = wp->w_topline;
1654
1655 /*
1656 * If we know the value of w_botline, use it to restrict the update to
1657 * the lines that are visible in the window.
1658 */
1659 if (wp->w_valid & VALID_BOTLINE)
1660 {
1661 if (from >= wp->w_botline)
1662 from = wp->w_botline - 1;
1663 if (to >= wp->w_botline)
1664 to = wp->w_botline - 1;
1665 }
1666
1667 /*
1668 * Find the minimal part to be updated.
1669 * Watch out for scrolling that made entries in w_lines[] invalid.
1670 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1671 * top_end; need to redraw from top_end to the "to" line.
1672 * A middle mouse click with a Visual selection may change the text
1673 * above the Visual area and reset wl_valid, do count these for
1674 * mid_end (in srow).
1675 */
1676 if (mid_start > 0)
1677 {
1678 lnum = wp->w_topline;
1679 idx = 0;
1680 srow = 0;
1681 if (scrolled_down)
1682 mid_start = top_end;
1683 else
1684 mid_start = 0;
1685 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1686 {
1687 if (wp->w_lines[idx].wl_valid)
1688 mid_start += wp->w_lines[idx].wl_size;
1689 else if (!scrolled_down)
1690 srow += wp->w_lines[idx].wl_size;
1691 ++idx;
1692# ifdef FEAT_FOLDING
1693 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1694 lnum = wp->w_lines[idx].wl_lnum;
1695 else
1696# endif
1697 ++lnum;
1698 }
1699 srow += mid_start;
1700 mid_end = wp->w_height;
1701 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1702 {
1703 if (wp->w_lines[idx].wl_valid
1704 && wp->w_lines[idx].wl_lnum >= to + 1)
1705 {
1706 /* Only update until first row of this line */
1707 mid_end = srow;
1708 break;
1709 }
1710 srow += wp->w_lines[idx].wl_size;
1711 }
1712 }
1713 }
1714
1715 if (VIsual_active && buf == curwin->w_buffer)
1716 {
1717 wp->w_old_visual_mode = VIsual_mode;
1718 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1719 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001720 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 wp->w_old_curswant = curwin->w_curswant;
1722 }
1723 else
1724 {
1725 wp->w_old_visual_mode = 0;
1726 wp->w_old_cursor_lnum = 0;
1727 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001728 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1732 /* reset got_int, otherwise regexp won't work */
1733 save_got_int = got_int;
1734 got_int = 0;
1735#endif
1736#ifdef FEAT_FOLDING
1737 win_foldinfo.fi_level = 0;
1738#endif
1739
1740 /*
1741 * Update all the window rows.
1742 */
1743 idx = 0; /* first entry in w_lines[].wl_size */
1744 row = 0;
1745 srow = 0;
1746 lnum = wp->w_topline; /* first line shown in window */
1747 for (;;)
1748 {
1749 /* stop updating when reached the end of the window (check for _past_
1750 * the end of the window is at the end of the loop) */
1751 if (row == wp->w_height)
1752 {
1753 didline = TRUE;
1754 break;
1755 }
1756
1757 /* stop updating when hit the end of the file */
1758 if (lnum > buf->b_ml.ml_line_count)
1759 {
1760 eof = TRUE;
1761 break;
1762 }
1763
1764 /* Remember the starting row of the line that is going to be dealt
1765 * with. It is used further down when the line doesn't fit. */
1766 srow = row;
1767
1768 /*
1769 * Update a line when it is in an area that needs updating, when it
1770 * has changes or w_lines[idx] is invalid.
1771 * bot_start may be halfway a wrapped line after using
1772 * win_del_lines(), check if the current line includes it.
1773 * When syntax folding is being used, the saved syntax states will
1774 * already have been updated, we can't see where the syntax state is
1775 * the same again, just update until the end of the window.
1776 */
1777 if (row < top_end
1778 || (row >= mid_start && row < mid_end)
1779#ifdef FEAT_SEARCH_EXTRA
1780 || top_to_mod
1781#endif
1782 || idx >= wp->w_lines_valid
1783 || (row + wp->w_lines[idx].wl_size > bot_start)
1784 || (mod_top != 0
1785 && (lnum == mod_top
1786 || (lnum >= mod_top
1787 && (lnum < mod_bot
1788#ifdef FEAT_SYN_HL
1789 || did_update == DID_FOLD
1790 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001791 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 && (
1793# ifdef FEAT_FOLDING
1794 (foldmethodIsSyntax(wp)
1795 && hasAnyFolding(wp)) ||
1796# endif
1797 syntax_check_changed(lnum)))
1798#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001799#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001800 /* match in fixed position might need redraw
1801 * if lines were inserted or deleted */
1802 || (wp->w_match_head != NULL
1803 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 )))))
1806 {
1807#ifdef FEAT_SEARCH_EXTRA
1808 if (lnum == mod_top)
1809 top_to_mod = FALSE;
1810#endif
1811
1812 /*
1813 * When at start of changed lines: May scroll following lines
1814 * up or down to minimize redrawing.
1815 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001816 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 */
1818 if (lnum == mod_top
1819 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001820 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
1822 int old_rows = 0;
1823 int new_rows = 0;
1824 int xtra_rows;
1825 linenr_T l;
1826
1827 /* Count the old number of window rows, using w_lines[], which
1828 * should still contain the sizes for the lines as they are
1829 * currently displayed. */
1830 for (i = idx; i < wp->w_lines_valid; ++i)
1831 {
1832 /* Only valid lines have a meaningful wl_lnum. Invalid
1833 * lines are part of the changed area. */
1834 if (wp->w_lines[i].wl_valid
1835 && wp->w_lines[i].wl_lnum == mod_bot)
1836 break;
1837 old_rows += wp->w_lines[i].wl_size;
1838#ifdef FEAT_FOLDING
1839 if (wp->w_lines[i].wl_valid
1840 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1841 {
1842 /* Must have found the last valid entry above mod_bot.
1843 * Add following invalid entries. */
1844 ++i;
1845 while (i < wp->w_lines_valid
1846 && !wp->w_lines[i].wl_valid)
1847 old_rows += wp->w_lines[i++].wl_size;
1848 break;
1849 }
1850#endif
1851 }
1852
1853 if (i >= wp->w_lines_valid)
1854 {
1855 /* We can't find a valid line below the changed lines,
1856 * need to redraw until the end of the window.
1857 * Inserting/deleting lines has no use. */
1858 bot_start = 0;
1859 }
1860 else
1861 {
1862 /* Able to count old number of rows: Count new window
1863 * rows, and may insert/delete lines */
1864 j = idx;
1865 for (l = lnum; l < mod_bot; ++l)
1866 {
1867#ifdef FEAT_FOLDING
1868 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1869 ++new_rows;
1870 else
1871#endif
1872#ifdef FEAT_DIFF
1873 if (l == wp->w_topline)
1874 new_rows += plines_win_nofill(wp, l, TRUE)
1875 + wp->w_topfill;
1876 else
1877#endif
1878 new_rows += plines_win(wp, l, TRUE);
1879 ++j;
1880 if (new_rows > wp->w_height - row - 2)
1881 {
1882 /* it's getting too much, must redraw the rest */
1883 new_rows = 9999;
1884 break;
1885 }
1886 }
1887 xtra_rows = new_rows - old_rows;
1888 if (xtra_rows < 0)
1889 {
1890 /* May scroll text up. If there is not enough
1891 * remaining text or scrolling fails, must redraw the
1892 * rest. If scrolling works, must redraw the text
1893 * below the scrolled text. */
1894 if (row - xtra_rows >= wp->w_height - 2)
1895 mod_bot = MAXLNUM;
1896 else
1897 {
1898 check_for_delay(FALSE);
1899 if (win_del_lines(wp, row,
1900 -xtra_rows, FALSE, FALSE) == FAIL)
1901 mod_bot = MAXLNUM;
1902 else
1903 bot_start = wp->w_height + xtra_rows;
1904 }
1905 }
1906 else if (xtra_rows > 0)
1907 {
1908 /* May scroll text down. If there is not enough
1909 * remaining text of scrolling fails, must redraw the
1910 * rest. */
1911 if (row + xtra_rows >= wp->w_height - 2)
1912 mod_bot = MAXLNUM;
1913 else
1914 {
1915 check_for_delay(FALSE);
1916 if (win_ins_lines(wp, row + old_rows,
1917 xtra_rows, FALSE, FALSE) == FAIL)
1918 mod_bot = MAXLNUM;
1919 else if (top_end > row + old_rows)
1920 /* Scrolled the part at the top that requires
1921 * updating down. */
1922 top_end += xtra_rows;
1923 }
1924 }
1925
1926 /* When not updating the rest, may need to move w_lines[]
1927 * entries. */
1928 if (mod_bot != MAXLNUM && i != j)
1929 {
1930 if (j < i)
1931 {
1932 int x = row + new_rows;
1933
1934 /* move entries in w_lines[] upwards */
1935 for (;;)
1936 {
1937 /* stop at last valid entry in w_lines[] */
1938 if (i >= wp->w_lines_valid)
1939 {
1940 wp->w_lines_valid = j;
1941 break;
1942 }
1943 wp->w_lines[j] = wp->w_lines[i];
1944 /* stop at a line that won't fit */
1945 if (x + (int)wp->w_lines[j].wl_size
1946 > wp->w_height)
1947 {
1948 wp->w_lines_valid = j + 1;
1949 break;
1950 }
1951 x += wp->w_lines[j++].wl_size;
1952 ++i;
1953 }
1954 if (bot_start > x)
1955 bot_start = x;
1956 }
1957 else /* j > i */
1958 {
1959 /* move entries in w_lines[] downwards */
1960 j -= i;
1961 wp->w_lines_valid += j;
1962 if (wp->w_lines_valid > wp->w_height)
1963 wp->w_lines_valid = wp->w_height;
1964 for (i = wp->w_lines_valid; i - j >= idx; --i)
1965 wp->w_lines[i] = wp->w_lines[i - j];
1966
1967 /* The w_lines[] entries for inserted lines are
1968 * now invalid, but wl_size may be used above.
1969 * Reset to zero. */
1970 while (i >= idx)
1971 {
1972 wp->w_lines[i].wl_size = 0;
1973 wp->w_lines[i--].wl_valid = FALSE;
1974 }
1975 }
1976 }
1977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979
1980#ifdef FEAT_FOLDING
1981 /*
1982 * When lines are folded, display one line for all of them.
1983 * Otherwise, display normally (can be several display lines when
1984 * 'wrap' is on).
1985 */
1986 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1987 if (fold_count != 0)
1988 {
1989 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1990 ++row;
1991 --fold_count;
1992 wp->w_lines[idx].wl_folded = TRUE;
1993 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1994# ifdef FEAT_SYN_HL
1995 did_update = DID_FOLD;
1996# endif
1997 }
1998 else
1999#endif
2000 if (idx < wp->w_lines_valid
2001 && wp->w_lines[idx].wl_valid
2002 && wp->w_lines[idx].wl_lnum == lnum
2003 && lnum > wp->w_topline
2004 && !(dy_flags & DY_LASTLINE)
2005 && srow + wp->w_lines[idx].wl_size > wp->w_height
2006#ifdef FEAT_DIFF
2007 && diff_check_fill(wp, lnum) == 0
2008#endif
2009 )
2010 {
2011 /* This line is not going to fit. Don't draw anything here,
2012 * will draw "@ " lines below. */
2013 row = wp->w_height + 1;
2014 }
2015 else
2016 {
2017#ifdef FEAT_SEARCH_EXTRA
2018 prepare_search_hl(wp, lnum);
2019#endif
2020#ifdef FEAT_SYN_HL
2021 /* Let the syntax stuff know we skipped a few lines. */
2022 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002023 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 syntax_end_parsing(syntax_last_parsed + 1);
2025#endif
2026
2027 /*
2028 * Display one line.
2029 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002030 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032#ifdef FEAT_FOLDING
2033 wp->w_lines[idx].wl_folded = FALSE;
2034 wp->w_lines[idx].wl_lastlnum = lnum;
2035#endif
2036#ifdef FEAT_SYN_HL
2037 did_update = DID_LINE;
2038 syntax_last_parsed = lnum;
2039#endif
2040 }
2041
2042 wp->w_lines[idx].wl_lnum = lnum;
2043 wp->w_lines[idx].wl_valid = TRUE;
2044 if (row > wp->w_height) /* past end of screen */
2045 {
2046 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002047 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2049 ++idx;
2050 break;
2051 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002052 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 wp->w_lines[idx].wl_size = row - srow;
2054 ++idx;
2055#ifdef FEAT_FOLDING
2056 lnum += fold_count + 1;
2057#else
2058 ++lnum;
2059#endif
2060 }
2061 else
2062 {
2063 /* This line does not need updating, advance to the next one */
2064 row += wp->w_lines[idx++].wl_size;
2065 if (row > wp->w_height) /* past end of screen */
2066 break;
2067#ifdef FEAT_FOLDING
2068 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2069#else
2070 ++lnum;
2071#endif
2072#ifdef FEAT_SYN_HL
2073 did_update = DID_NONE;
2074#endif
2075 }
2076
2077 if (lnum > buf->b_ml.ml_line_count)
2078 {
2079 eof = TRUE;
2080 break;
2081 }
2082 }
2083 /*
2084 * End of loop over all window lines.
2085 */
2086
2087
2088 if (idx > wp->w_lines_valid)
2089 wp->w_lines_valid = idx;
2090
2091#ifdef FEAT_SYN_HL
2092 /*
2093 * Let the syntax stuff know we stop parsing here.
2094 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002095 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 syntax_end_parsing(syntax_last_parsed + 1);
2097#endif
2098
2099 /*
2100 * If we didn't hit the end of the file, and we didn't finish the last
2101 * line we were working on, then the line didn't fit.
2102 */
2103 wp->w_empty_rows = 0;
2104#ifdef FEAT_DIFF
2105 wp->w_filler_rows = 0;
2106#endif
2107 if (!eof && !didline)
2108 {
2109 if (lnum == wp->w_topline)
2110 {
2111 /*
2112 * Single line that does not fit!
2113 * Don't overwrite it, it can be edited.
2114 */
2115 wp->w_botline = lnum + 1;
2116 }
2117#ifdef FEAT_DIFF
2118 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2119 {
2120 /* Window ends in filler lines. */
2121 wp->w_botline = lnum;
2122 wp->w_filler_rows = wp->w_height - srow;
2123 }
2124#endif
2125 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2126 {
2127 /*
2128 * Last line isn't finished: Display "@@@" at the end.
2129 */
2130 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2131 W_WINROW(wp) + wp->w_height,
2132 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2133 '@', '@', hl_attr(HLF_AT));
2134 set_empty_rows(wp, srow);
2135 wp->w_botline = lnum;
2136 }
2137 else
2138 {
2139 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2140 wp->w_botline = lnum;
2141 }
2142 }
2143 else
2144 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002145#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 draw_vsep_win(wp, row);
2147#endif
2148 if (eof) /* we hit the end of the file */
2149 {
2150 wp->w_botline = buf->b_ml.ml_line_count + 1;
2151#ifdef FEAT_DIFF
2152 j = diff_check_fill(wp, wp->w_botline);
2153 if (j > 0 && !wp->w_botfill)
2154 {
2155 /*
2156 * Display filler lines at the end of the file
2157 */
2158 if (char2cells(fill_diff) > 1)
2159 i = '-';
2160 else
2161 i = fill_diff;
2162 if (row + j > wp->w_height)
2163 j = wp->w_height - row;
2164 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2165 row += j;
2166 }
2167#endif
2168 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002169 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 wp->w_botline = lnum;
2171
2172 /* make sure the rest of the screen is blank */
2173 /* put '~'s on rows that aren't part of the file. */
2174 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2175 }
2176
2177 /* Reset the type of redrawing required, the window has been updated. */
2178 wp->w_redr_type = 0;
2179#ifdef FEAT_DIFF
2180 wp->w_old_topfill = wp->w_topfill;
2181 wp->w_old_botfill = wp->w_botfill;
2182#endif
2183
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002184 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /*
2187 * There is a trick with w_botline. If we invalidate it on each
2188 * change that might modify it, this will cause a lot of expensive
2189 * calls to plines() in update_topline() each time. Therefore the
2190 * value of w_botline is often approximated, and this value is used to
2191 * compute the value of w_topline. If the value of w_botline was
2192 * wrong, check that the value of w_topline is correct (cursor is on
2193 * the visible part of the text). If it's not, we need to redraw
2194 * again. Mostly this just means scrolling up a few lines, so it
2195 * doesn't look too bad. Only do this for the current window (where
2196 * changes are relevant).
2197 */
2198 wp->w_valid |= VALID_BOTLINE;
2199 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2200 {
2201 recursive = TRUE;
2202 curwin->w_valid &= ~VALID_TOPLINE;
2203 update_topline(); /* may invalidate w_botline again */
2204 if (must_redraw != 0)
2205 {
2206 /* Don't update for changes in buffer again. */
2207 i = curbuf->b_mod_set;
2208 curbuf->b_mod_set = FALSE;
2209 win_update(curwin);
2210 must_redraw = 0;
2211 curbuf->b_mod_set = i;
2212 }
2213 recursive = FALSE;
2214 }
2215 }
2216
2217#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2218 /* restore got_int, unless CTRL-C was hit while redrawing */
2219 if (!got_int)
2220 got_int = save_got_int;
2221#endif
2222}
2223
2224#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002225static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227/*
2228 * Return TRUE when window "wp" has a column to draw signs in.
2229 */
2230 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002231draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 return (wp->w_buffer->b_signlist != NULL
2234# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002235 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236# endif
2237 );
2238}
2239#endif
2240
2241/*
2242 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2243 * as the filler character.
2244 */
2245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002246win_draw_end(
2247 win_T *wp,
2248 int c1,
2249 int c2,
2250 int row,
2251 int endrow,
2252 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2255 int n = 0;
2256# define FDC_OFF n
2257#else
2258# define FDC_OFF 0
2259#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002260#ifdef FEAT_FOLDING
2261 int fdc = compute_foldcolumn(wp, 0);
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264#ifdef FEAT_RIGHTLEFT
2265 if (wp->w_p_rl)
2266 {
2267 /* No check for cmdline window: should never be right-left. */
2268# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002269 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270
2271 if (n > 0)
2272 {
2273 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002274 if (n > W_WIDTH(wp))
2275 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2277 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2278 ' ', ' ', hl_attr(HLF_FC));
2279 }
2280# endif
2281# ifdef FEAT_SIGNS
2282 if (draw_signcolumn(wp))
2283 {
2284 int nn = n + 2;
2285
2286 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002287 if (nn > W_WIDTH(wp))
2288 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2290 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2291 ' ', ' ', hl_attr(HLF_SC));
2292 n = nn;
2293 }
2294# endif
2295 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2296 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2297 c2, c2, hl_attr(hl));
2298 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2299 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2300 c1, c2, hl_attr(hl));
2301 }
2302 else
2303#endif
2304 {
2305#ifdef FEAT_CMDWIN
2306 if (cmdwin_type != 0 && wp == curwin)
2307 {
2308 /* draw the cmdline character in the leftmost column */
2309 n = 1;
2310 if (n > wp->w_width)
2311 n = wp->w_width;
2312 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2313 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2314 cmdwin_type, ' ', hl_attr(HLF_AT));
2315 }
2316#endif
2317#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002318 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002320 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 /* draw the fold column at the left */
2323 if (nn > W_WIDTH(wp))
2324 nn = W_WIDTH(wp);
2325 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2326 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2327 ' ', ' ', hl_attr(HLF_FC));
2328 n = nn;
2329 }
2330#endif
2331#ifdef FEAT_SIGNS
2332 if (draw_signcolumn(wp))
2333 {
2334 int nn = n + 2;
2335
2336 /* draw the sign column after the fold column */
2337 if (nn > W_WIDTH(wp))
2338 nn = W_WIDTH(wp);
2339 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2340 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2341 ' ', ' ', hl_attr(HLF_SC));
2342 n = nn;
2343 }
2344#endif
2345 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2346 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2347 c1, c2, hl_attr(hl));
2348 }
2349 set_empty_rows(wp, row);
2350}
2351
Bram Moolenaar1a384422010-07-14 19:53:30 +02002352#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002353static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002354
2355/*
2356 * Advance **color_cols and return TRUE when there are columns to draw.
2357 */
2358 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002359advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002360{
2361 while (**color_cols >= 0 && vcol > **color_cols)
2362 ++*color_cols;
2363 return (**color_cols >= 0);
2364}
2365#endif
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#ifdef FEAT_FOLDING
2368/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002369 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2370 * space is available for window "wp", minus "col".
2371 */
2372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002373compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002374{
2375 int fdc = wp->w_p_fdc;
2376 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2377 int wwidth = W_WIDTH(wp);
2378
2379 if (fdc > wwidth - (col + wmw))
2380 fdc = wwidth - (col + wmw);
2381 return fdc;
2382}
2383
2384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * Display one folded line.
2386 */
2387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002388fold_line(
2389 win_T *wp,
2390 long fold_count,
2391 foldinfo_T *foldinfo,
2392 linenr_T lnum,
2393 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
2395 char_u buf[51];
2396 pos_T *top, *bot;
2397 linenr_T lnume = lnum + fold_count - 1;
2398 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002399 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 int col;
2402 int txtcol;
2403 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002404 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /* Build the fold line:
2407 * 1. Add the cmdwin_type for the command-line window
2408 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002409 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 * 4. Compose the text
2411 * 5. Add the text
2412 * 6. set highlighting for the Visual area an other text
2413 */
2414 col = 0;
2415
2416 /*
2417 * 1. Add the cmdwin_type for the command-line window
2418 * Ignores 'rightleft', this window is never right-left.
2419 */
2420#ifdef FEAT_CMDWIN
2421 if (cmdwin_type != 0 && wp == curwin)
2422 {
2423 ScreenLines[off] = cmdwin_type;
2424 ScreenAttrs[off] = hl_attr(HLF_AT);
2425#ifdef FEAT_MBYTE
2426 if (enc_utf8)
2427 ScreenLinesUC[off] = 0;
2428#endif
2429 ++col;
2430 }
2431#endif
2432
2433 /*
2434 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002435 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002437 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (fdc > 0)
2439 {
2440 fill_foldcolumn(buf, wp, TRUE, lnum);
2441#ifdef FEAT_RIGHTLEFT
2442 if (wp->w_p_rl)
2443 {
2444 int i;
2445
2446 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2447 hl_attr(HLF_FC));
2448 /* reverse the fold column */
2449 for (i = 0; i < fdc; ++i)
2450 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2451 }
2452 else
2453#endif
2454 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2455 col += fdc;
2456 }
2457
2458#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002459# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2460 for (ri = 0; ri < l; ++ri) \
2461 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2462 else \
2463 for (ri = 0; ri < l; ++ri) \
2464 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002466# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2467 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#endif
2469
Bram Moolenaar64486672010-05-16 15:46:46 +02002470 /* Set all attributes of the 'number' or 'relativenumber' column and the
2471 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002472 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474#ifdef FEAT_SIGNS
2475 /* If signs are being displayed, add two spaces. */
2476 if (draw_signcolumn(wp))
2477 {
2478 len = W_WIDTH(wp) - col;
2479 if (len > 0)
2480 {
2481 if (len > 2)
2482 len = 2;
2483# ifdef FEAT_RIGHTLEFT
2484 if (wp->w_p_rl)
2485 /* the line number isn't reversed */
2486 copy_text_attr(off + W_WIDTH(wp) - len - col,
2487 (char_u *)" ", len, hl_attr(HLF_FL));
2488 else
2489# endif
2490 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2491 col += len;
2492 }
2493 }
2494#endif
2495
2496 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002497 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002499 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 len = W_WIDTH(wp) - col;
2502 if (len > 0)
2503 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002504 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002505 long num;
2506 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002507
2508 if (len > w + 1)
2509 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002510
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002511 if (wp->w_p_nu && !wp->w_p_rnu)
2512 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002513 num = (long)lnum;
2514 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002515 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002516 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002517 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002518 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002519 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002520 /* 'number' + 'relativenumber': cursor line shows absolute
2521 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002522 num = lnum;
2523 fmt = "%-*ld ";
2524 }
2525 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002526
Bram Moolenaar700e7342013-01-30 12:31:36 +01002527 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#ifdef FEAT_RIGHTLEFT
2529 if (wp->w_p_rl)
2530 /* the line number isn't reversed */
2531 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2532 hl_attr(HLF_FL));
2533 else
2534#endif
2535 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2536 col += len;
2537 }
2538 }
2539
2540 /*
2541 * 4. Compose the folded-line string with 'foldtext', if set.
2542 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002543 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 txtcol = col; /* remember where text starts */
2546
2547 /*
2548 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2549 * Right-left text is put in columns 0 - number-col, normal text is put
2550 * in columns number-col - window-width.
2551 */
2552#ifdef FEAT_MBYTE
2553 if (has_mbyte)
2554 {
2555 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002556 int u8c, u8cc[MAX_MCO];
2557 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int idx;
2559 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002560 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561# ifdef FEAT_ARABIC
2562 int prev_c = 0; /* previous Arabic character */
2563 int prev_c1 = 0; /* first composing char for prev_c */
2564# endif
2565
2566# ifdef FEAT_RIGHTLEFT
2567 if (wp->w_p_rl)
2568 idx = off;
2569 else
2570# endif
2571 idx = off + col;
2572
2573 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2574 for (p = text; *p != NUL; )
2575 {
2576 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002577 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (col + cells > W_WIDTH(wp)
2579# ifdef FEAT_RIGHTLEFT
2580 - (wp->w_p_rl ? col : 0)
2581# endif
2582 )
2583 break;
2584 ScreenLines[idx] = *p;
2585 if (enc_utf8)
2586 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 u8c = utfc_ptr2char(p, u8cc);
2588 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
2590 ScreenLinesUC[idx] = 0;
2591#ifdef FEAT_ARABIC
2592 prev_c = u8c;
2593#endif
2594 }
2595 else
2596 {
2597#ifdef FEAT_ARABIC
2598 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2599 {
2600 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002601 int pc, pc1, nc;
2602 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 int firstbyte = *p;
2604
2605 /* The idea of what is the previous and next
2606 * character depends on 'rightleft'. */
2607 if (wp->w_p_rl)
2608 {
2609 pc = prev_c;
2610 pc1 = prev_c1;
2611 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002612 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614 else
2615 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002616 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620 prev_c = u8c;
2621
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002622 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 pc, pc1, nc);
2624 ScreenLines[idx] = firstbyte;
2625 }
2626 else
2627 prev_c = u8c;
2628#endif
2629 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002630#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (u8c >= 0x10000)
2632 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2633 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002636 for (i = 0; i < Screen_mco; ++i)
2637 {
2638 ScreenLinesC[i][idx] = u8cc[i];
2639 if (u8cc[i] == 0)
2640 break;
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 if (cells > 1)
2644 ScreenLines[idx + 1] = 0;
2645 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002646 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2647 /* double-byte single width character */
2648 ScreenLines2[idx] = p[1];
2649 else if (cells > 1)
2650 /* double-width character */
2651 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 col += cells;
2653 idx += cells;
2654 p += c_len;
2655 }
2656 }
2657 else
2658#endif
2659 {
2660 len = (int)STRLEN(text);
2661 if (len > W_WIDTH(wp) - col)
2662 len = W_WIDTH(wp) - col;
2663 if (len > 0)
2664 {
2665#ifdef FEAT_RIGHTLEFT
2666 if (wp->w_p_rl)
2667 STRNCPY(current_ScreenLine, text, len);
2668 else
2669#endif
2670 STRNCPY(current_ScreenLine + col, text, len);
2671 col += len;
2672 }
2673 }
2674
2675 /* Fill the rest of the line with the fold filler */
2676#ifdef FEAT_RIGHTLEFT
2677 if (wp->w_p_rl)
2678 col -= txtcol;
2679#endif
2680 while (col < W_WIDTH(wp)
2681#ifdef FEAT_RIGHTLEFT
2682 - (wp->w_p_rl ? txtcol : 0)
2683#endif
2684 )
2685 {
2686#ifdef FEAT_MBYTE
2687 if (enc_utf8)
2688 {
2689 if (fill_fold >= 0x80)
2690 {
2691 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002692 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694 else
2695 ScreenLinesUC[off + col] = 0;
2696 }
2697#endif
2698 ScreenLines[off + col++] = fill_fold;
2699 }
2700
2701 if (text != buf)
2702 vim_free(text);
2703
2704 /*
2705 * 6. set highlighting for the Visual area an other text.
2706 * If all folded lines are in the Visual area, highlight the line.
2707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2709 {
2710 if (ltoreq(curwin->w_cursor, VIsual))
2711 {
2712 /* Visual is after curwin->w_cursor */
2713 top = &curwin->w_cursor;
2714 bot = &VIsual;
2715 }
2716 else
2717 {
2718 /* Visual is before curwin->w_cursor */
2719 top = &VIsual;
2720 bot = &curwin->w_cursor;
2721 }
2722 if (lnum >= top->lnum
2723 && lnume <= bot->lnum
2724 && (VIsual_mode != 'v'
2725 || ((lnum > top->lnum
2726 || (lnum == top->lnum
2727 && top->col == 0))
2728 && (lnume < bot->lnum
2729 || (lnume == bot->lnum
2730 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002731 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 if (VIsual_mode == Ctrl_V)
2734 {
2735 /* Visual block mode: highlight the chars part of the block */
2736 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2737 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002738 if (wp->w_old_cursor_lcol != MAXCOL
2739 && wp->w_old_cursor_lcol + txtcol
2740 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 len = wp->w_old_cursor_lcol;
2742 else
2743 len = W_WIDTH(wp) - txtcol;
2744 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002745 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 }
2747 }
2748 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002751 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002756#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002757 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2758 if (wp->w_p_cc_cols)
2759 {
2760 int i = 0;
2761 int j = wp->w_p_cc_cols[i];
2762 int old_txtcol = txtcol;
2763
2764 while (j > -1)
2765 {
2766 txtcol += j;
2767 if (wp->w_p_wrap)
2768 txtcol -= wp->w_skipcol;
2769 else
2770 txtcol -= wp->w_leftcol;
2771 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2772 ScreenAttrs[off + txtcol] = hl_combine_attr(
2773 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2774 txtcol = old_txtcol;
2775 j = wp->w_p_cc_cols[++i];
2776 }
2777 }
2778
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002779 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002780 if (wp->w_p_cuc)
2781 {
2782 txtcol += wp->w_virtcol;
2783 if (wp->w_p_wrap)
2784 txtcol -= wp->w_skipcol;
2785 else
2786 txtcol -= wp->w_leftcol;
2787 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2788 ScreenAttrs[off + txtcol] = hl_combine_attr(
2789 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2790 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2794 (int)W_WIDTH(wp), FALSE);
2795
2796 /*
2797 * Update w_cline_height and w_cline_folded if the cursor line was
2798 * updated (saves a call to plines() later).
2799 */
2800 if (wp == curwin
2801 && lnum <= curwin->w_cursor.lnum
2802 && lnume >= curwin->w_cursor.lnum)
2803 {
2804 curwin->w_cline_row = row;
2805 curwin->w_cline_height = 1;
2806 curwin->w_cline_folded = TRUE;
2807 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2808 }
2809}
2810
2811/*
2812 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2813 */
2814 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002815copy_text_attr(
2816 int off,
2817 char_u *buf,
2818 int len,
2819 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002821 int i;
2822
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 mch_memmove(ScreenLines + off, buf, (size_t)len);
2824# ifdef FEAT_MBYTE
2825 if (enc_utf8)
2826 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2827# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002828 for (i = 0; i < len; ++i)
2829 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830}
2831
2832/*
2833 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002834 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
2836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002837fill_foldcolumn(
2838 char_u *p,
2839 win_T *wp,
2840 int closed, /* TRUE of FALSE */
2841 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 int i = 0;
2844 int level;
2845 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002846 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002847 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002850 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 level = win_foldinfo.fi_level;
2853 if (level > 0)
2854 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002855 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002856 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* If the column is too narrow, we start at the lowest level that
2859 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002860 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (first_level < 1)
2862 first_level = 1;
2863
Bram Moolenaar1c934292015-01-27 16:39:29 +01002864 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
2866 if (win_foldinfo.fi_lnum == lnum
2867 && first_level + i >= win_foldinfo.fi_low_level)
2868 p[i] = '-';
2869 else if (first_level == 1)
2870 p[i] = '|';
2871 else if (first_level + i <= 9)
2872 p[i] = '0' + first_level + i;
2873 else
2874 p[i] = '>';
2875 if (first_level + i == level)
2876 break;
2877 }
2878 }
2879 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002880 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881}
2882#endif /* FEAT_FOLDING */
2883
2884/*
2885 * Display line "lnum" of window 'wp' on the screen.
2886 * Start at row "startrow", stop when "endrow" is reached.
2887 * wp->w_virtcol needs to be valid.
2888 *
2889 * Return the number of last row the line occupies.
2890 */
2891 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002892win_line(
2893 win_T *wp,
2894 linenr_T lnum,
2895 int startrow,
2896 int endrow,
2897 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 int col; /* visual column on screen */
2900 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2901 int c = 0; /* init for GCC */
2902 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002903#ifdef FEAT_LINEBREAK
2904 long vcol_sbr = -1; /* virtual column after showbreak */
2905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 long vcol_prev = -1; /* "vcol" of previous character */
2907 char_u *line; /* current line */
2908 char_u *ptr; /* current position in "line" */
2909 int row; /* row in the window, excl w_winrow */
2910 int screen_row; /* row on the screen, incl w_winrow */
2911
2912 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2913 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002914 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002915 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int c_extra = NUL; /* extra chars, all the same */
2917 int extra_attr = 0; /* attributes when n_extra != 0 */
2918 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2919 displaying lcs_eol at end-of-line */
2920 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2921 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2922
2923 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2924 int saved_n_extra = 0;
2925 char_u *saved_p_extra = NULL;
2926 int saved_c_extra = 0;
2927 int saved_char_attr = 0;
2928
2929 int n_attr = 0; /* chars with special attr */
2930 int saved_attr2 = 0; /* char_attr saved for n_attr */
2931 int n_attr3 = 0; /* chars with overruling special attr */
2932 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2933
2934 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2935
2936 int fromcol, tocol; /* start/end of inverting */
2937 int fromcol_prev = -2; /* start of inverting after cursor */
2938 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002940 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 pos_T pos;
2942 long v;
2943
2944 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002945 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2947 in this line */
2948 int attr = 0; /* attributes for area highlighting */
2949 int area_attr = 0; /* attributes desired by highlighting */
2950 int search_attr = 0; /* attributes desired by 'hlsearch' */
2951#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002952 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 int syntax_attr = 0; /* attributes desired by syntax */
2954 int has_syntax = FALSE; /* this buffer has syntax highl. */
2955 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002956 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002957 int draw_color_col = FALSE; /* highlight colorcolumn */
2958 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002959#endif
2960#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002961 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002962# define SPWORDLEN 150
2963 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002964 int nextlinecol = 0; /* column where nextline[] starts */
2965 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002966 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002967 int spell_attr = 0; /* attributes desired by spelling */
2968 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002969 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2970 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002971 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002972 static int cap_col = -1; /* column to check for Cap word */
2973 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002974 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975#endif
2976 int extra_check; /* has syntax or linebreak */
2977#ifdef FEAT_MBYTE
2978 int multi_attr = 0; /* attributes desired by multibyte */
2979 int mb_l = 1; /* multi-byte byte length */
2980 int mb_c = 0; /* decoded multi-byte character */
2981 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002982 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#endif
2984#ifdef FEAT_DIFF
2985 int filler_lines; /* nr of filler lines to be drawn */
2986 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002987 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 int change_start = MAXCOL; /* first col of changed area */
2989 int change_end = -1; /* last col of changed area */
2990#endif
2991 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2992#ifdef FEAT_LINEBREAK
2993 int need_showbreak = FALSE;
2994#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002995#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2996 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002998 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#endif
3000#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003001 matchitem_T *cur; /* points to the match list */
3002 match_T *shl; /* points to search_hl or a match */
3003 int shl_flag; /* flag to indicate whether search_hl
3004 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003005 int pos_inprogress; /* marks that position match search is
3006 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003007 int prevcol_hl_flag; /* flag to indicate whether prevcol
3008 equals startcol of search_hl or one
3009 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
3011#ifdef FEAT_ARABIC
3012 int prev_c = 0; /* previous Arabic character */
3013 int prev_c1 = 0; /* first composing char for prev_c */
3014#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003015#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003016 int did_line_attr = 0;
3017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 /* draw_state: items that are drawn in sequence: */
3020#define WL_START 0 /* nothing done yet */
3021#ifdef FEAT_CMDWIN
3022# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3023#else
3024# define WL_CMDLINE WL_START
3025#endif
3026#ifdef FEAT_FOLDING
3027# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3028#else
3029# define WL_FOLD WL_CMDLINE
3030#endif
3031#ifdef FEAT_SIGNS
3032# define WL_SIGN WL_FOLD + 1 /* column for signs */
3033#else
3034# define WL_SIGN WL_FOLD /* column for signs */
3035#endif
3036#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003037#ifdef FEAT_LINEBREAK
3038# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003040# define WL_BRI WL_NR
3041#endif
3042#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3043# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3044#else
3045# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
3047#define WL_LINE WL_SBR + 1 /* text in the line */
3048 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003049#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 int feedback_col = 0;
3051 int feedback_old_attr = -1;
3052#endif
3053
Bram Moolenaar860cae12010-06-05 23:22:07 +02003054#ifdef FEAT_CONCEAL
3055 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003056 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003057 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003058 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003059 int is_concealing = FALSE;
3060 int boguscols = 0; /* nonexistent columns added to force
3061 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003062 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003063 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003064 int match_conc = 0; /* cchar for match functions */
3065 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003066 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003067# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003068# define FIX_FOR_BOGUSCOLS \
3069 { \
3070 n_extra += vcol_off; \
3071 vcol -= vcol_off; \
3072 vcol_off = 0; \
3073 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003074 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003075 boguscols = 0; \
3076 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003077#else
3078# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 if (startrow > endrow) /* past the end already! */
3082 return startrow;
3083
3084 row = startrow;
3085 screen_row = row + W_WINROW(wp);
3086
3087 /*
3088 * To speed up the loop below, set extra_check when there is linebreak,
3089 * trailing white space and/or syntax processing to be done.
3090 */
3091#ifdef FEAT_LINEBREAK
3092 extra_check = wp->w_p_lbr;
3093#else
3094 extra_check = 0;
3095#endif
3096#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003097 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
3099 /* Prepare for syntax highlighting in this line. When there is an
3100 * error, stop syntax highlighting. */
3101 save_did_emsg = did_emsg;
3102 did_emsg = FALSE;
3103 syntax_start(wp, lnum);
3104 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003105 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 else
3107 {
3108 did_emsg = save_did_emsg;
3109 has_syntax = TRUE;
3110 extra_check = TRUE;
3111 }
3112 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003113
3114 /* Check for columns to display for 'colorcolumn'. */
3115 color_cols = wp->w_p_cc_cols;
3116 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003117 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003118#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003120#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003121 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003122 && *wp->w_s->b_p_spl != NUL
3123 && wp->w_s->b_langp.ga_len > 0
3124 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003125 {
3126 /* Prepare for spell checking. */
3127 has_spell = TRUE;
3128 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003129
3130 /* Get the start of the next line, so that words that wrap to the next
3131 * line are found too: "et<line-break>al.".
3132 * Trick: skip a few chars for C/shell/Vim comments */
3133 nextline[SPWORDLEN] = NUL;
3134 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3135 {
3136 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3137 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3138 }
3139
3140 /* When a word wrapped from the previous line the start of the current
3141 * line is valid. */
3142 if (lnum == checked_lnum)
3143 cur_checked_col = checked_col;
3144 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003145
3146 /* When there was a sentence end in the previous line may require a
3147 * word starting with capital in this line. In line 1 always check
3148 * the first word. */
3149 if (lnum != capcol_lnum)
3150 cap_col = -1;
3151 if (lnum == 1)
3152 cap_col = 0;
3153 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156
3157 /*
3158 * handle visual active in this window
3159 */
3160 fromcol = -10;
3161 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3163 {
3164 /* Visual is after curwin->w_cursor */
3165 if (ltoreq(curwin->w_cursor, VIsual))
3166 {
3167 top = &curwin->w_cursor;
3168 bot = &VIsual;
3169 }
3170 else /* Visual is before curwin->w_cursor */
3171 {
3172 top = &VIsual;
3173 bot = &curwin->w_cursor;
3174 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003175 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (VIsual_mode == Ctrl_V) /* block mode */
3177 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003178 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
3180 fromcol = wp->w_old_cursor_fcol;
3181 tocol = wp->w_old_cursor_lcol;
3182 }
3183 }
3184 else /* non-block mode */
3185 {
3186 if (lnum > top->lnum && lnum <= bot->lnum)
3187 fromcol = 0;
3188 else if (lnum == top->lnum)
3189 {
3190 if (VIsual_mode == 'V') /* linewise */
3191 fromcol = 0;
3192 else
3193 {
3194 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3195 if (gchar_pos(top) == NUL)
3196 tocol = fromcol + 1;
3197 }
3198 }
3199 if (VIsual_mode != 'V' && lnum == bot->lnum)
3200 {
3201 if (*p_sel == 'e' && bot->col == 0
3202#ifdef FEAT_VIRTUALEDIT
3203 && bot->coladd == 0
3204#endif
3205 )
3206 {
3207 fromcol = -10;
3208 tocol = MAXCOL;
3209 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003210 else if (bot->col == MAXCOL)
3211 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
3213 {
3214 pos = *bot;
3215 if (*p_sel == 'e')
3216 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3217 else
3218 {
3219 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3220 ++tocol;
3221 }
3222 }
3223 }
3224 }
3225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /* Check if the character under the cursor should not be inverted */
3227 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003228#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 )
3232 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 /* if inverting in this line set area_highlighting */
3235 if (fromcol >= 0)
3236 {
3237 area_highlighting = TRUE;
3238 attr = hl_attr(HLF_V);
3239#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003240 if ((clip_star.available && !clip_star.owned
3241 && clip_isautosel_star())
3242 || (clip_plus.available && !clip_plus.owned
3243 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 attr = hl_attr(HLF_VNC);
3245#endif
3246 }
3247 }
3248
3249 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003250 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003252 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 && wp == curwin
3254 && lnum >= curwin->w_cursor.lnum
3255 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3256 {
3257 if (lnum == curwin->w_cursor.lnum)
3258 getvcol(curwin, &(curwin->w_cursor),
3259 (colnr_T *)&fromcol, NULL, NULL);
3260 else
3261 fromcol = 0;
3262 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3263 {
3264 pos.lnum = lnum;
3265 pos.col = search_match_endcol;
3266 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3267 }
3268 else
3269 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003270 /* do at least one character; happens when past end of line */
3271 if (fromcol == tocol)
3272 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 area_highlighting = TRUE;
3274 attr = hl_attr(HLF_I);
3275 }
3276
3277#ifdef FEAT_DIFF
3278 filler_lines = diff_check(wp, lnum);
3279 if (filler_lines < 0)
3280 {
3281 if (filler_lines == -1)
3282 {
3283 if (diff_find_change(wp, lnum, &change_start, &change_end))
3284 diff_hlf = HLF_ADD; /* added line */
3285 else if (change_start == 0)
3286 diff_hlf = HLF_TXD; /* changed text */
3287 else
3288 diff_hlf = HLF_CHD; /* changed line */
3289 }
3290 else
3291 diff_hlf = HLF_ADD; /* added line */
3292 filler_lines = 0;
3293 area_highlighting = TRUE;
3294 }
3295 if (lnum == wp->w_topline)
3296 filler_lines = wp->w_topfill;
3297 filler_todo = filler_lines;
3298#endif
3299
3300#ifdef LINE_ATTR
3301# ifdef FEAT_SIGNS
3302 /* If this line has a sign with line highlighting set line_attr. */
3303 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3304 if (v != 0)
3305 line_attr = sign_get_attr((int)v, TRUE);
3306# endif
3307# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3308 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003309 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 line_attr = hl_attr(HLF_L);
3311# endif
3312 if (line_attr != 0)
3313 area_highlighting = TRUE;
3314#endif
3315
3316 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3317 ptr = line;
3318
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003319#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003320 if (has_spell)
3321 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003322 /* For checking first word with a capital skip white space. */
3323 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003324 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003325
Bram Moolenaar30abd282005-06-22 22:35:10 +00003326 /* To be able to spell-check over line boundaries copy the end of the
3327 * current line into nextline[]. Above the start of the next line was
3328 * copied to nextline[SPWORDLEN]. */
3329 if (nextline[SPWORDLEN] == NUL)
3330 {
3331 /* No next line or it is empty. */
3332 nextlinecol = MAXCOL;
3333 nextline_idx = 0;
3334 }
3335 else
3336 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003337 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003338 if (v < SPWORDLEN)
3339 {
3340 /* Short line, use it completely and append the start of the
3341 * next line. */
3342 nextlinecol = 0;
3343 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003344 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003345 nextline_idx = v + 1;
3346 }
3347 else
3348 {
3349 /* Long line, use only the last SPWORDLEN bytes. */
3350 nextlinecol = v - SPWORDLEN;
3351 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3352 nextline_idx = SPWORDLEN + 1;
3353 }
3354 }
3355 }
3356#endif
3357
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003358 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003360 if (lcs_space || lcs_trail)
3361 extra_check = TRUE;
3362 /* find start of trailing whitespace */
3363 if (lcs_trail)
3364 {
3365 trailcol = (colnr_T)STRLEN(ptr);
3366 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3367 --trailcol;
3368 trailcol += (colnr_T) (ptr - line);
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371
3372 /*
3373 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3374 * first character to be displayed.
3375 */
3376 if (wp->w_p_wrap)
3377 v = wp->w_skipcol;
3378 else
3379 v = wp->w_leftcol;
3380 if (v > 0)
3381 {
3382#ifdef FEAT_MBYTE
3383 char_u *prev_ptr = ptr;
3384#endif
3385 while (vcol < v && *ptr != NUL)
3386 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003387 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 vcol += c;
3389#ifdef FEAT_MBYTE
3390 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003392 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003395 /* When:
3396 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003397 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003398 * - 'virtualedit' is set, or
3399 * - the visual mode is active,
3400 * the end of the line may be before the start of the displayed part.
3401 */
3402 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003403#ifdef FEAT_SYN_HL
3404 wp->w_p_cuc || draw_color_col ||
3405#endif
3406#ifdef FEAT_VIRTUALEDIT
3407 virtual_active() ||
3408#endif
3409 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414 /* Handle a character that's not completely on the screen: Put ptr at
3415 * that character but skip the first few screen characters. */
3416 if (vcol > v)
3417 {
3418 vcol -= c;
3419#ifdef FEAT_MBYTE
3420 ptr = prev_ptr;
3421#else
3422 --ptr;
3423#endif
3424 n_skip = v - vcol;
3425 }
3426
3427 /*
3428 * Adjust for when the inverted text is before the screen,
3429 * and when the start of the inverted text is before the screen.
3430 */
3431 if (tocol <= vcol)
3432 fromcol = 0;
3433 else if (fromcol >= 0 && fromcol < vcol)
3434 fromcol = vcol;
3435
3436#ifdef FEAT_LINEBREAK
3437 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3438 if (wp->w_p_wrap)
3439 need_showbreak = TRUE;
3440#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003441#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003442 /* When spell checking a word we need to figure out the start of the
3443 * word and if it's badly spelled or not. */
3444 if (has_spell)
3445 {
3446 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003447 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003448 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003449
3450 pos = wp->w_cursor;
3451 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003452 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003453 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003454
3455 /* spell_move_to() may call ml_get() and make "line" invalid */
3456 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3457 ptr = line + linecol;
3458
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003459 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003460 {
3461 /* no bad word found at line start, don't check until end of a
3462 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003463 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003464 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003465 }
3466 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003467 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003468 /* bad word found, use attributes until end of word */
3469 word_end = wp->w_cursor.col + len + 1;
3470
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003471 /* Turn index into actual attributes. */
3472 if (spell_hlf != HLF_COUNT)
3473 spell_attr = highlight_attr[spell_hlf];
3474 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003475 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003476
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003477# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003478 /* Need to restart syntax highlighting for this line. */
3479 if (has_syntax)
3480 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003481# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003482 }
3483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485
3486 /*
3487 * Correct highlighting for cursor that can't be disabled.
3488 * Avoids having to check this for each character.
3489 */
3490 if (fromcol >= 0)
3491 {
3492 if (noinvcur)
3493 {
3494 if ((colnr_T)fromcol == wp->w_virtcol)
3495 {
3496 /* highlighting starts at cursor, let it start just after the
3497 * cursor */
3498 fromcol_prev = fromcol;
3499 fromcol = -1;
3500 }
3501 else if ((colnr_T)fromcol < wp->w_virtcol)
3502 /* restart highlighting after the cursor */
3503 fromcol_prev = wp->w_virtcol;
3504 }
3505 if (fromcol >= tocol)
3506 fromcol = -1;
3507 }
3508
3509#ifdef FEAT_SEARCH_EXTRA
3510 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003511 * Handle highlighting the last used search pattern and matches.
3512 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003514 cur = wp->w_match_head;
3515 shl_flag = FALSE;
3516 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003518 if (shl_flag == FALSE)
3519 {
3520 shl = &search_hl;
3521 shl_flag = TRUE;
3522 }
3523 else
3524 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003525 shl->startcol = MAXCOL;
3526 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003528 v = (long)(ptr - line);
3529 if (cur != NULL)
3530 cur->pos.cur = 0;
3531 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3532
3533 /* Need to get the line again, a multi-line regexp may have made it
3534 * invalid. */
3535 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3536 ptr = line + v;
3537
3538 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003540 if (shl->lnum == lnum)
3541 shl->startcol = shl->rm.startpos[0].col;
3542 else
3543 shl->startcol = 0;
3544 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3545 - shl->rm.startpos[0].lnum)
3546 shl->endcol = shl->rm.endpos[0].col;
3547 else
3548 shl->endcol = MAXCOL;
3549 /* Highlight one character for an empty match. */
3550 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003553 if (has_mbyte && line[shl->endcol] != NUL)
3554 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003557 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003559 if ((long)shl->startcol < v) /* match at leftcol */
3560 {
3561 shl->attr_cur = shl->attr;
3562 search_attr = shl->attr;
3563 }
3564 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003566 if (shl != &search_hl && cur != NULL)
3567 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569#endif
3570
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003571#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003572 /* Cursor line highlighting for 'cursorline' in the current window. Not
3573 * when Visual mode is active, because it's not clear what is selected
3574 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003575 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003576 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003577 {
3578 line_attr = hl_attr(HLF_CUL);
3579 area_highlighting = TRUE;
3580 }
3581#endif
3582
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003583 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 col = 0;
3585#ifdef FEAT_RIGHTLEFT
3586 if (wp->w_p_rl)
3587 {
3588 /* Rightleft window: process the text in the normal direction, but put
3589 * it in current_ScreenLine[] from right to left. Start at the
3590 * rightmost column of the window. */
3591 col = W_WIDTH(wp) - 1;
3592 off += col;
3593 }
3594#endif
3595
3596 /*
3597 * Repeat for the whole displayed line.
3598 */
3599 for (;;)
3600 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003601#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003602 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 /* Skip this quickly when working on the text. */
3605 if (draw_state != WL_LINE)
3606 {
3607#ifdef FEAT_CMDWIN
3608 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3609 {
3610 draw_state = WL_CMDLINE;
3611 if (cmdwin_type != 0 && wp == curwin)
3612 {
3613 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003615 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 char_attr = hl_attr(HLF_AT);
3617 }
3618 }
3619#endif
3620
3621#ifdef FEAT_FOLDING
3622 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3623 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003624 int fdc = compute_foldcolumn(wp, 0);
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003627 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 /* Draw the 'foldcolumn'. */
3630 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003631 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003633 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 c_extra = NUL;
3635 char_attr = hl_attr(HLF_FC);
3636 }
3637 }
3638#endif
3639
3640#ifdef FEAT_SIGNS
3641 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3642 {
3643 draw_state = WL_SIGN;
3644 /* Show the sign column when there are any signs in this
3645 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003646 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003648 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003650 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651# endif
3652
3653 /* Draw two cells with the sign value or blank. */
3654 c_extra = ' ';
3655 char_attr = hl_attr(HLF_SC);
3656 n_extra = 2;
3657
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003658 if (row == startrow
3659#ifdef FEAT_DIFF
3660 + filler_lines && filler_todo <= 0
3661#endif
3662 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3665 SIGN_TEXT);
3666# ifdef FEAT_SIGN_ICONS
3667 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3668 SIGN_ICON);
3669 if (gui.in_use && icon_sign != 0)
3670 {
3671 /* Use the image in this position. */
3672 c_extra = SIGN_BYTE;
3673# ifdef FEAT_NETBEANS_INTG
3674 if (buf_signcount(wp->w_buffer, lnum) > 1)
3675 c_extra = MULTISIGN_BYTE;
3676# endif
3677 char_attr = icon_sign;
3678 }
3679 else
3680# endif
3681 if (text_sign != 0)
3682 {
3683 p_extra = sign_get_text(text_sign);
3684 if (p_extra != NULL)
3685 {
3686 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003687 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 char_attr = sign_get_attr(text_sign, FALSE);
3690 }
3691 }
3692 }
3693 }
3694#endif
3695
3696 if (draw_state == WL_NR - 1 && n_extra == 0)
3697 {
3698 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003699 /* Display the absolute or relative line number. After the
3700 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3701 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 && (row == startrow
3703#ifdef FEAT_DIFF
3704 + filler_lines
3705#endif
3706 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3707 {
3708 /* Draw the line number (empty space after wrapping). */
3709 if (row == startrow
3710#ifdef FEAT_DIFF
3711 + filler_lines
3712#endif
3713 )
3714 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003715 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003716 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003717
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003718 if (wp->w_p_nu && !wp->w_p_rnu)
3719 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003720 num = (long)lnum;
3721 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003722 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003723 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003724 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003725 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003726 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003727 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003728 num = lnum;
3729 fmt = "%-*ld ";
3730 }
3731 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003732
Bram Moolenaar700e7342013-01-30 12:31:36 +01003733 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003734 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (wp->w_skipcol > 0)
3736 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3737 *p_extra = '-';
3738#ifdef FEAT_RIGHTLEFT
3739 if (wp->w_p_rl) /* reverse line numbers */
3740 rl_mirror(extra);
3741#endif
3742 p_extra = extra;
3743 c_extra = NUL;
3744 }
3745 else
3746 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003747 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003749#ifdef FEAT_SYN_HL
3750 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003751 * the current line differently.
3752 * TODO: Can we use CursorLine instead of CursorLineNr
3753 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003754 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003755 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003756 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759 }
3760
Bram Moolenaar597a4222014-06-25 14:39:50 +02003761#ifdef FEAT_LINEBREAK
3762 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3763 && n_extra == 0 && *p_sbr != NUL)
3764 /* draw indent after showbreak value */
3765 draw_state = WL_BRI;
3766 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3767 /* After the showbreak, draw the breakindent */
3768 draw_state = WL_BRI - 1;
3769
3770 /* draw 'breakindent': indent wrapped text accordingly */
3771 if (draw_state == WL_BRI - 1 && n_extra == 0)
3772 {
3773 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003774 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003775# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003776 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003777# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003778 )
3779 {
3780 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003781# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003782 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003783 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003784 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003785# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003786 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3787 char_attr = hl_combine_attr(char_attr,
3788 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003789# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003790 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003791# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003792 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003793 c_extra = ' ';
3794 n_extra = get_breakindent_win(wp,
3795 ml_get_buf(wp->w_buffer, lnum, FALSE));
3796 /* Correct end of highlighted area for 'breakindent',
3797 * required when 'linebreak' is also set. */
3798 if (tocol == vcol)
3799 tocol += n_extra;
3800 }
3801 }
3802#endif
3803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3805 if (draw_state == WL_SBR - 1 && n_extra == 0)
3806 {
3807 draw_state = WL_SBR;
3808# ifdef FEAT_DIFF
3809 if (filler_todo > 0)
3810 {
3811 /* Draw "deleted" diff line(s). */
3812 if (char2cells(fill_diff) > 1)
3813 c_extra = '-';
3814 else
3815 c_extra = fill_diff;
3816# ifdef FEAT_RIGHTLEFT
3817 if (wp->w_p_rl)
3818 n_extra = col + 1;
3819 else
3820# endif
3821 n_extra = W_WIDTH(wp) - col;
3822 char_attr = hl_attr(HLF_DED);
3823 }
3824# endif
3825# ifdef FEAT_LINEBREAK
3826 if (*p_sbr != NUL && need_showbreak)
3827 {
3828 /* Draw 'showbreak' at the start of each broken line. */
3829 p_extra = p_sbr;
3830 c_extra = NUL;
3831 n_extra = (int)STRLEN(p_sbr);
3832 char_attr = hl_attr(HLF_AT);
3833 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003834 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 /* Correct end of highlighted area for 'showbreak',
3836 * required when 'linebreak' is also set. */
3837 if (tocol == vcol)
3838 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003839#ifdef FEAT_SYN_HL
3840 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003841 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003842 char_attr = hl_combine_attr(char_attr,
3843 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846# endif
3847 }
3848#endif
3849
3850 if (draw_state == WL_LINE - 1 && n_extra == 0)
3851 {
3852 draw_state = WL_LINE;
3853 if (saved_n_extra)
3854 {
3855 /* Continue item from end of wrapped line. */
3856 n_extra = saved_n_extra;
3857 c_extra = saved_c_extra;
3858 p_extra = saved_p_extra;
3859 char_attr = saved_char_attr;
3860 }
3861 else
3862 char_attr = 0;
3863 }
3864 }
3865
3866 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003867 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003868 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#ifdef FEAT_DIFF
3870 && filler_todo <= 0
3871#endif
3872 )
3873 {
3874 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3875 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003876 /* Pretend we have finished updating the window. Except when
3877 * 'cursorcolumn' is set. */
3878#ifdef FEAT_SYN_HL
3879 if (wp->w_p_cuc)
3880 row = wp->w_cline_row + wp->w_cline_height;
3881 else
3882#endif
3883 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 break;
3885 }
3886
3887 if (draw_state == WL_LINE && area_highlighting)
3888 {
3889 /* handle Visual or match highlighting in this line */
3890 if (vcol == fromcol
3891#ifdef FEAT_MBYTE
3892 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3893 && (*mb_ptr2cells)(ptr) > 1)
3894#endif
3895 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003896 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 && vcol < tocol))
3898 area_attr = attr; /* start highlighting */
3899 else if (area_attr != 0
3900 && (vcol == tocol
3901 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
3904#ifdef FEAT_SEARCH_EXTRA
3905 if (!n_extra)
3906 {
3907 /*
3908 * Check for start/end of search pattern match.
3909 * After end, check for start/end of next match.
3910 * When another match, have to check for start again.
3911 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003912 * Do this for 'search_hl' and the match list (ordered by
3913 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003915 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003916 cur = wp->w_match_head;
3917 shl_flag = FALSE;
3918 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003920 if (shl_flag == FALSE
3921 && ((cur != NULL
3922 && cur->priority > SEARCH_HL_PRIORITY)
3923 || cur == NULL))
3924 {
3925 shl = &search_hl;
3926 shl_flag = TRUE;
3927 }
3928 else
3929 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003930 if (cur != NULL)
3931 cur->pos.cur = 0;
3932 pos_inprogress = TRUE;
3933 while (shl->rm.regprog != NULL
3934 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003936 if (shl->startcol != MAXCOL
3937 && v >= (long)shl->startcol
3938 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003940#ifdef FEAT_MBYTE
3941 int tmp_col = v + MB_PTR2LEN(ptr);
3942
3943 if (shl->endcol < tmp_col)
3944 shl->endcol = tmp_col;
3945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003947#ifdef FEAT_CONCEAL
3948 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3949 == cur->hlg_id)
3950 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003951 has_match_conc =
3952 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003953 match_conc = cur->conceal_char;
3954 }
3955 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003956 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003959 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
3961 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003962#ifdef FEAT_CONCEAL
3963 prev_syntax_id = 0;
3964#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003965 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3966 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003967 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969 /* Need to get the line again, a multi-line regexp
3970 * may have made it invalid. */
3971 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3972 ptr = line + v;
3973
3974 if (shl->lnum == lnum)
3975 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003976 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003978 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003980 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003982 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
3984 /* highlight empty match, try again after
3985 * it */
3986#ifdef FEAT_MBYTE
3987 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003988 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003989 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 else
3991#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003992 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994
3995 /* Loop to check if the match starts at the
3996 * current position */
3997 continue;
3998 }
3999 }
4000 break;
4001 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004002 if (shl != &search_hl && cur != NULL)
4003 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004005
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004006 /* Use attributes from match with highest priority among
4007 * 'search_hl' and the match list. */
4008 search_attr = search_hl.attr_cur;
4009 cur = wp->w_match_head;
4010 shl_flag = FALSE;
4011 while (cur != NULL || shl_flag == FALSE)
4012 {
4013 if (shl_flag == FALSE
4014 && ((cur != NULL
4015 && cur->priority > SEARCH_HL_PRIORITY)
4016 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004017 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004018 shl = &search_hl;
4019 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004020 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004021 else
4022 shl = &cur->hl;
4023 if (shl->attr_cur != 0)
4024 search_attr = shl->attr_cur;
4025 if (shl != &search_hl && cur != NULL)
4026 cur = cur->next;
4027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029#endif
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004032 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004034 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4035 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004037 if (diff_hlf == HLF_TXD && ptr - line > change_end
4038 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004040 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004041 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4042 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004045
4046 /* Decide which of the highlight attributes to use. */
4047 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004048#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004049 if (area_attr != 0)
4050 char_attr = hl_combine_attr(line_attr, area_attr);
4051 else if (search_attr != 0)
4052 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004053 /* Use line_attr when not in the Visual or 'incsearch' area
4054 * (area_attr may be 0 when "noinvcur" is set). */
4055 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004056 || vcol < fromcol || vcol_prev < fromcol_prev
4057 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004058 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004059#else
4060 if (area_attr != 0)
4061 char_attr = area_attr;
4062 else if (search_attr != 0)
4063 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004064#endif
4065 else
4066 {
4067 attr_pri = FALSE;
4068#ifdef FEAT_SYN_HL
4069 if (has_syntax)
4070 char_attr = syntax_attr;
4071 else
4072#endif
4073 char_attr = 0;
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076
4077 /*
4078 * Get the next character to put on the screen.
4079 */
4080 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004081 * The "p_extra" points to the extra stuff that is inserted to
4082 * represent special characters (non-printable stuff) and other
4083 * things. When all characters are the same, c_extra is used.
4084 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4085 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4087 */
4088 if (n_extra > 0)
4089 {
4090 if (c_extra != NUL)
4091 {
4092 c = c_extra;
4093#ifdef FEAT_MBYTE
4094 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4095 if (enc_utf8 && (*mb_char2len)(c) > 1)
4096 {
4097 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004098 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004099 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 else
4102 mb_utf8 = FALSE;
4103#endif
4104 }
4105 else
4106 {
4107 c = *p_extra;
4108#ifdef FEAT_MBYTE
4109 if (has_mbyte)
4110 {
4111 mb_c = c;
4112 if (enc_utf8)
4113 {
4114 /* If the UTF-8 character is more than one byte:
4115 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004116 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 mb_utf8 = FALSE;
4118 if (mb_l > n_extra)
4119 mb_l = 1;
4120 else if (mb_l > 1)
4121 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004122 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 }
4127 else
4128 {
4129 /* if this is a DBCS character, put it in "mb_c" */
4130 mb_l = MB_BYTE2LEN(c);
4131 if (mb_l >= n_extra)
4132 mb_l = 1;
4133 else if (mb_l > 1)
4134 mb_c = (c << 8) + p_extra[1];
4135 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004136 if (mb_l == 0) /* at the NUL at end-of-line */
4137 mb_l = 1;
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /* If a double-width char doesn't fit display a '>' in the
4140 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004141 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142# ifdef FEAT_RIGHTLEFT
4143 wp->w_p_rl ? (col <= 0) :
4144# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004145 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 && (*mb_char2cells)(mb_c) == 2)
4147 {
4148 c = '>';
4149 mb_c = c;
4150 mb_l = 1;
4151 mb_utf8 = FALSE;
4152 multi_attr = hl_attr(HLF_AT);
4153 /* put the pointer back to output the double-width
4154 * character at the start of the next line. */
4155 ++n_extra;
4156 --p_extra;
4157 }
4158 else
4159 {
4160 n_extra -= mb_l - 1;
4161 p_extra += mb_l - 1;
4162 }
4163 }
4164#endif
4165 ++p_extra;
4166 }
4167 --n_extra;
4168 }
4169 else
4170 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004171 if (p_extra_free != NULL)
4172 {
4173 vim_free(p_extra_free);
4174 p_extra_free = NULL;
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /*
4177 * Get a character from the line itself.
4178 */
4179 c = *ptr;
4180#ifdef FEAT_MBYTE
4181 if (has_mbyte)
4182 {
4183 mb_c = c;
4184 if (enc_utf8)
4185 {
4186 /* If the UTF-8 character is more than one byte: Decode it
4187 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004188 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 mb_utf8 = FALSE;
4190 if (mb_l > 1)
4191 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004192 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 /* Overlong encoded ASCII or ASCII with composing char
4194 * is displayed normally, except a NUL. */
4195 if (mb_c < 0x80)
4196 c = mb_c;
4197 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004198
4199 /* At start of the line we can have a composing char.
4200 * Draw it as a space with a composing char. */
4201 if (utf_iscomposing(mb_c))
4202 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004203 int i;
4204
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004205 for (i = Screen_mco - 1; i > 0; --i)
4206 u8cc[i] = u8cc[i - 1];
4207 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004208 mb_c = ' ';
4209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211
4212 if ((mb_l == 1 && c >= 0x80)
4213 || (mb_l >= 1 && mb_c == 0)
4214 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004215# ifdef UNICODE16
4216 || mb_c >= 0x10000
4217# endif
4218 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 /*
4221 * Illegal UTF-8 byte: display as <xx>.
4222 * Non-BMP character : display as ? or fullwidth ?.
4223 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004224# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
4228 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004229# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (wp->w_p_rl) /* reverse */
4231 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004234# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 else if (utf_char2cells(mb_c) != 2)
4236 STRCPY(extra, "?");
4237 else
4238 /* 0xff1f in UTF-8: full-width '?' */
4239 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 p_extra = extra;
4243 c = *p_extra;
4244 mb_c = mb_ptr2char_adv(&p_extra);
4245 mb_utf8 = (c >= 0x80);
4246 n_extra = (int)STRLEN(p_extra);
4247 c_extra = NUL;
4248 if (area_attr == 0 && search_attr == 0)
4249 {
4250 n_attr = n_extra + 1;
4251 extra_attr = hl_attr(HLF_8);
4252 saved_attr2 = char_attr; /* save current attr */
4253 }
4254 }
4255 else if (mb_l == 0) /* at the NUL at end-of-line */
4256 mb_l = 1;
4257#ifdef FEAT_ARABIC
4258 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4259 {
4260 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004261 int pc, pc1, nc;
4262 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 /* The idea of what is the previous and next
4265 * character depends on 'rightleft'. */
4266 if (wp->w_p_rl)
4267 {
4268 pc = prev_c;
4269 pc1 = prev_c1;
4270 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004271 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 else
4274 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004275 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004277 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 prev_c = mb_c;
4280
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004281 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 else
4284 prev_c = mb_c;
4285#endif
4286 }
4287 else /* enc_dbcs */
4288 {
4289 mb_l = MB_BYTE2LEN(c);
4290 if (mb_l == 0) /* at the NUL at end-of-line */
4291 mb_l = 1;
4292 else if (mb_l > 1)
4293 {
4294 /* We assume a second byte below 32 is illegal.
4295 * Hopefully this is OK for all double-byte encodings!
4296 */
4297 if (ptr[1] >= 32)
4298 mb_c = (c << 8) + ptr[1];
4299 else
4300 {
4301 if (ptr[1] == NUL)
4302 {
4303 /* head byte at end of line */
4304 mb_l = 1;
4305 transchar_nonprint(extra, c);
4306 }
4307 else
4308 {
4309 /* illegal tail byte */
4310 mb_l = 2;
4311 STRCPY(extra, "XX");
4312 }
4313 p_extra = extra;
4314 n_extra = (int)STRLEN(extra) - 1;
4315 c_extra = NUL;
4316 c = *p_extra++;
4317 if (area_attr == 0 && search_attr == 0)
4318 {
4319 n_attr = n_extra + 1;
4320 extra_attr = hl_attr(HLF_8);
4321 saved_attr2 = char_attr; /* save current attr */
4322 }
4323 mb_c = c;
4324 }
4325 }
4326 }
4327 /* If a double-width char doesn't fit display a '>' in the
4328 * last column; the character is displayed at the start of the
4329 * next line. */
4330 if ((
4331# ifdef FEAT_RIGHTLEFT
4332 wp->w_p_rl ? (col <= 0) :
4333# endif
4334 (col >= W_WIDTH(wp) - 1))
4335 && (*mb_char2cells)(mb_c) == 2)
4336 {
4337 c = '>';
4338 mb_c = c;
4339 mb_utf8 = FALSE;
4340 mb_l = 1;
4341 multi_attr = hl_attr(HLF_AT);
4342 /* Put pointer back so that the character will be
4343 * displayed at the start of the next line. */
4344 --ptr;
4345 }
4346 else if (*ptr != NUL)
4347 ptr += mb_l - 1;
4348
4349 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004350 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004351 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004352 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004355 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 c = ' ';
4357 if (area_attr == 0 && search_attr == 0)
4358 {
4359 n_attr = n_extra + 1;
4360 extra_attr = hl_attr(HLF_AT);
4361 saved_attr2 = char_attr; /* save current attr */
4362 }
4363 mb_c = c;
4364 mb_utf8 = FALSE;
4365 mb_l = 1;
4366 }
4367
4368 }
4369#endif
4370 ++ptr;
4371
4372 if (extra_check)
4373 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004374#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004375 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004376#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004377
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004378#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* Get syntax attribute, unless still at the start of the line
4380 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004381 v = (long)(ptr - line);
4382 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 /* Get the syntax attribute for the character. If there
4385 * is an error, disable syntax highlighting. */
4386 save_did_emsg = did_emsg;
4387 did_emsg = FALSE;
4388
Bram Moolenaar217ad922005-03-20 22:37:15 +00004389 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004390# ifdef FEAT_SPELL
4391 has_spell ? &can_spell :
4392# endif
4393 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004396 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004397 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004398 has_syntax = FALSE;
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 else
4401 did_emsg = save_did_emsg;
4402
4403 /* Need to get the line again, a multi-line regexp may
4404 * have made it invalid. */
4405 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4406 ptr = line + v;
4407
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004408 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004410 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004411 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004412# ifdef FEAT_CONCEAL
4413 /* no concealing past the end of the line, it interferes
4414 * with line highlighting */
4415 if (c == NUL)
4416 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004417 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004418 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004421#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004422
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004423#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004424 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004425 * Only do this when there is no syntax highlighting, the
4426 * @Spell cluster is not used or the current syntax item
4427 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004428 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004429 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004430 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004431# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004432 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004433 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004434# endif
4435 if (c != 0 && (
4436# ifdef FEAT_SYN_HL
4437 !has_syntax ||
4438# endif
4439 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004440 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004441 char_u *prev_ptr, *p;
4442 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004443 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004444# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004445 if (has_mbyte)
4446 {
4447 prev_ptr = ptr - mb_l;
4448 v -= mb_l - 1;
4449 }
4450 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004451# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004452 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004453
4454 /* Use nextline[] if possible, it has the start of the
4455 * next line concatenated. */
4456 if ((prev_ptr - line) - nextlinecol >= 0)
4457 p = nextline + (prev_ptr - line) - nextlinecol;
4458 else
4459 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004460 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004461 len = spell_check(wp, p, &spell_hlf, &cap_col,
4462 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004463 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004464
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004465 /* In Insert mode only highlight a word that
4466 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004467 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004468 && (State & INSERT) != 0
4469 && wp->w_cursor.lnum == lnum
4470 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004471 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004472 && wp->w_cursor.col < (colnr_T)word_end)
4473 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004474 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004475 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004476 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004477
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004478 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004479 && (p - nextline) + len > nextline_idx)
4480 {
4481 /* Remember that the good word continues at the
4482 * start of the next line. */
4483 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004484 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004485 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004486
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004487 /* Turn index into actual attributes. */
4488 if (spell_hlf != HLF_COUNT)
4489 spell_attr = highlight_attr[spell_hlf];
4490
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004491 if (cap_col > 0)
4492 {
4493 if (p != prev_ptr
4494 && (p - nextline) + cap_col >= nextline_idx)
4495 {
4496 /* Remember that the word in the next line
4497 * must start with a capital. */
4498 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004499 cap_col = (int)((p - nextline) + cap_col
4500 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004501 }
4502 else
4503 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004504 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004505 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004506 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004507 }
4508 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004509 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004510 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004511 char_attr = hl_combine_attr(char_attr, spell_attr);
4512 else
4513 char_attr = hl_combine_attr(spell_attr, char_attr);
4514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515#endif
4516#ifdef FEAT_LINEBREAK
4517 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004518 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004520 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004522# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004523 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004524# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004525 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004527 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004529 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004530
Bram Moolenaar597a4222014-06-25 14:39:50 +02004531 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004532 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004533 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004534 if (c == TAB && n_extra + col > W_WIDTH(wp))
4535 n_extra = (int)wp->w_buffer->b_p_ts
4536 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4537
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004538# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004539 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004540# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004544 {
4545#ifdef FEAT_CONCEAL
4546 if (c == TAB)
4547 /* See "Tab alignment" below. */
4548 FIX_FOR_BOGUSCOLS;
4549#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004550 if (!wp->w_p_list)
4551 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554#endif
4555
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004556 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4557 */
4558 if (wp->w_p_list
4559 && (((c == 160
4560#ifdef FEAT_MBYTE
4561 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4562#endif
4563 ) && lcs_nbsp)
4564 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4565 {
4566 c = (c == ' ') ? lcs_space : lcs_nbsp;
4567 if (area_attr == 0 && search_attr == 0)
4568 {
4569 n_attr = 1;
4570 extra_attr = hl_attr(HLF_8);
4571 saved_attr2 = char_attr; /* save current attr */
4572 }
4573#ifdef FEAT_MBYTE
4574 mb_c = c;
4575 if (enc_utf8 && (*mb_char2len)(c) > 1)
4576 {
4577 mb_utf8 = TRUE;
4578 u8cc[0] = 0;
4579 c = 0xc0;
4580 }
4581 else
4582 mb_utf8 = FALSE;
4583#endif
4584 }
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4587 {
4588 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004589 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 n_attr = 1;
4592 extra_attr = hl_attr(HLF_8);
4593 saved_attr2 = char_attr; /* save current attr */
4594 }
4595#ifdef FEAT_MBYTE
4596 mb_c = c;
4597 if (enc_utf8 && (*mb_char2len)(c) > 1)
4598 {
4599 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004600 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004601 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 else
4604 mb_utf8 = FALSE;
4605#endif
4606 }
4607 }
4608
4609 /*
4610 * Handling of non-printable characters.
4611 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004612 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 /*
4615 * when getting a character from the file, we may have to
4616 * turn it into something else on the way to putting it
4617 * into "ScreenLines".
4618 */
4619 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4620 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004621 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004622 long vcol_adjusted = vcol; /* removed showbreak length */
4623#ifdef FEAT_LINEBREAK
4624 /* only adjust the tab_len, when at the first column
4625 * after the showbreak value was drawn */
4626 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4627 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004630 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004631 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4632
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004633#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004634 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004635#endif
4636 /* tab amount depends on current column */
4637 n_extra = tab_len;
4638#ifdef FEAT_LINEBREAK
4639 else
4640 {
4641 char_u *p;
4642 int len = n_extra;
4643 int i;
4644 int saved_nextra = n_extra;
4645
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004646#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004647 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004648 /* there are characters to conceal */
4649 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004650 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4651 */
4652 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4653 && n_extra > tab_len)
4654 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004655#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004656
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004657 /* if n_extra > 0, it gives the number of chars, to
4658 * use for a tab, else we need to calculate the width
4659 * for a tab */
4660#ifdef FEAT_MBYTE
4661 len = (tab_len * mb_char2len(lcs_tab2));
4662 if (n_extra > 0)
4663 len += n_extra - tab_len;
4664#endif
4665 c = lcs_tab1;
4666 p = alloc((unsigned)(len + 1));
4667 vim_memset(p, ' ', len);
4668 p[len] = NUL;
4669 p_extra_free = p;
4670 for (i = 0; i < tab_len; i++)
4671 {
4672#ifdef FEAT_MBYTE
4673 mb_char2bytes(lcs_tab2, p);
4674 p += mb_char2len(lcs_tab2);
4675 n_extra += mb_char2len(lcs_tab2)
4676 - (saved_nextra > 0 ? 1 : 0);
4677#else
4678 p[i] = lcs_tab2;
4679#endif
4680 }
4681 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004682#ifdef FEAT_CONCEAL
4683 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4684 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004685 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004686 n_extra -= vcol_off;
4687#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004688 }
4689#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004690#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004691 {
4692 int vc_saved = vcol_off;
4693
4694 /* Tab alignment should be identical regardless of
4695 * 'conceallevel' value. So tab compensates of all
4696 * previous concealed characters, and thus resets
4697 * vcol_off and boguscols accumulated so far in the
4698 * line. Note that the tab can be longer than
4699 * 'tabstop' when there are concealed characters. */
4700 FIX_FOR_BOGUSCOLS;
4701
4702 /* Make sure, the highlighting for the tab char will be
4703 * correctly set further below (effectively reverts the
4704 * FIX_FOR_BOGSUCOLS macro */
4705 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004706 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004707 tab_len += vc_saved;
4708 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#ifdef FEAT_MBYTE
4711 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4712#endif
4713 if (wp->w_p_list)
4714 {
4715 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004716#ifdef FEAT_LINEBREAK
4717 if (wp->w_p_lbr)
4718 c_extra = NUL; /* using p_extra from above */
4719 else
4720#endif
4721 c_extra = lcs_tab2;
4722 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 extra_attr = hl_attr(HLF_8);
4724 saved_attr2 = char_attr; /* save current attr */
4725#ifdef FEAT_MBYTE
4726 mb_c = c;
4727 if (enc_utf8 && (*mb_char2len)(c) > 1)
4728 {
4729 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004730 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004731 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733#endif
4734 }
4735 else
4736 {
4737 c_extra = ' ';
4738 c = ' ';
4739 }
4740 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004741 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004742 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004743 || ((fromcol >= 0 || fromcol_prev >= 0)
4744 && tocol > vcol
4745 && VIsual_mode != Ctrl_V
4746 && (
4747# ifdef FEAT_RIGHTLEFT
4748 wp->w_p_rl ? (col >= 0) :
4749# endif
4750 (col < W_WIDTH(wp)))
4751 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004752 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004753 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004754 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004756 /* Display a '$' after the line or highlight an extra
4757 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4759 /* For a diff line the highlighting continues after the
4760 * "$". */
4761 if (
4762# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004763 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764# ifdef LINE_ATTR
4765 &&
4766# endif
4767# endif
4768# ifdef LINE_ATTR
4769 line_attr == 0
4770# endif
4771 )
4772#endif
4773 {
4774#ifdef FEAT_VIRTUALEDIT
4775 /* In virtualedit, visual selections may extend
4776 * beyond end of line. */
4777 if (area_highlighting && virtual_active()
4778 && tocol != MAXCOL && vcol < tocol)
4779 n_extra = 0;
4780 else
4781#endif
4782 {
4783 p_extra = at_end_str;
4784 n_extra = 1;
4785 c_extra = NUL;
4786 }
4787 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004788 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004789 c = lcs_eol;
4790 else
4791 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 lcs_eol_one = -1;
4793 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004794 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 extra_attr = hl_attr(HLF_AT);
4797 n_attr = 1;
4798 }
4799#ifdef FEAT_MBYTE
4800 mb_c = c;
4801 if (enc_utf8 && (*mb_char2len)(c) > 1)
4802 {
4803 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004804 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004805 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 else
4808 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4809#endif
4810 }
4811 else if (c != NUL)
4812 {
4813 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004814 if (n_extra == 0)
4815 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#ifdef FEAT_RIGHTLEFT
4817 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4818 rl_mirror(p_extra); /* reverse "<12>" */
4819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004821#ifdef FEAT_LINEBREAK
4822 if (wp->w_p_lbr)
4823 {
4824 char_u *p;
4825
4826 c = *p_extra;
4827 p = alloc((unsigned)n_extra + 1);
4828 vim_memset(p, ' ', n_extra);
4829 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4830 p[n_extra] = NUL;
4831 p_extra_free = p_extra = p;
4832 }
4833 else
4834#endif
4835 {
4836 n_extra = byte2cells(c) - 1;
4837 c = *p_extra++;
4838 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004839 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
4841 n_attr = n_extra + 1;
4842 extra_attr = hl_attr(HLF_8);
4843 saved_attr2 = char_attr; /* save current attr */
4844 }
4845#ifdef FEAT_MBYTE
4846 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4847#endif
4848 }
4849#ifdef FEAT_VIRTUALEDIT
4850 else if (VIsual_active
4851 && (VIsual_mode == Ctrl_V
4852 || VIsual_mode == 'v')
4853 && virtual_active()
4854 && tocol != MAXCOL
4855 && vcol < tocol
4856 && (
4857# ifdef FEAT_RIGHTLEFT
4858 wp->w_p_rl ? (col >= 0) :
4859# endif
4860 (col < W_WIDTH(wp))))
4861 {
4862 c = ' ';
4863 --ptr; /* put it back at the NUL */
4864 }
4865#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004866#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 else if ((
4868# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004869 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 ) && (
4873# ifdef FEAT_RIGHTLEFT
4874 wp->w_p_rl ? (col >= 0) :
4875# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004876 (col
4877# ifdef FEAT_CONCEAL
4878 - boguscols
4879# endif
4880 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 /* Highlight until the right side of the window */
4883 c = ' ';
4884 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004885
4886 /* Remember we do the char for line highlighting. */
4887 ++did_line_attr;
4888
4889 /* don't do search HL for the rest of the line */
4890 if (line_attr != 0 && char_attr == search_attr && col > 0)
4891 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892# ifdef FEAT_DIFF
4893 if (diff_hlf == HLF_TXD)
4894 {
4895 diff_hlf = HLF_CHD;
4896 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004899 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4900 char_attr = hl_combine_attr(char_attr,
4901 hl_attr(HLF_CUL));
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904# endif
4905 }
4906#endif
4907 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004908
4909#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004910 if ( wp->w_p_cole > 0
4911 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004912 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004913 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004914 && !(lnum_in_visual_area
4915 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004916 {
4917 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004918 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004919 && (syn_get_sub_char() != NUL || match_conc
4920 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004921 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004922 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004923 /* First time at this concealed item: display one
4924 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004925 if (match_conc)
4926 c = match_conc;
4927 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004928 c = syn_get_sub_char();
4929 else if (lcs_conceal != NUL)
4930 c = lcs_conceal;
4931 else
4932 c = ' ';
4933
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004934 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004935
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004936 if (n_extra > 0)
4937 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004938 vcol += n_extra;
4939 if (wp->w_p_wrap && n_extra > 0)
4940 {
4941# ifdef FEAT_RIGHTLEFT
4942 if (wp->w_p_rl)
4943 {
4944 col -= n_extra;
4945 boguscols -= n_extra;
4946 }
4947 else
4948# endif
4949 {
4950 boguscols += n_extra;
4951 col += n_extra;
4952 }
4953 }
4954 n_extra = 0;
4955 n_attr = 0;
4956 }
4957 else if (n_skip == 0)
4958 {
4959 is_concealing = TRUE;
4960 n_skip = 1;
4961 }
4962# ifdef FEAT_MBYTE
4963 mb_c = c;
4964 if (enc_utf8 && (*mb_char2len)(c) > 1)
4965 {
4966 mb_utf8 = TRUE;
4967 u8cc[0] = 0;
4968 c = 0xc0;
4969 }
4970 else
4971 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4972# endif
4973 }
4974 else
4975 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004976 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004977 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004978 }
4979#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004982#ifdef FEAT_CONCEAL
4983 /* In the cursor line and we may be concealing characters: correct
4984 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004985 if (!did_wcol && draw_state == WL_LINE
4986 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004987 && conceal_cursor_line(wp)
4988 && (int)wp->w_virtcol <= vcol + n_skip)
4989 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004990# ifdef FEAT_RIGHTLEFT
4991 if (wp->w_p_rl)
4992 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4993 else
4994# endif
4995 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004996 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004997 did_wcol = TRUE;
4998 }
4999#endif
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 /* Don't override visual selection highlighting. */
5002 if (n_attr > 0
5003 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005004 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 char_attr = extra_attr;
5006
Bram Moolenaar81695252004-12-29 20:58:21 +00005007#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /* XIM don't send preedit_start and preedit_end, but they send
5009 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5010 * im_is_preediting() here. */
5011 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005012 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 && (State & INSERT)
5014 && !p_imdisable
5015 && im_is_preediting()
5016 && draw_state == WL_LINE)
5017 {
5018 colnr_T tcol;
5019
5020 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005021 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else
5023 tcol = preedit_end_col;
5024 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5025 {
5026 if (feedback_old_attr < 0)
5027 {
5028 feedback_col = 0;
5029 feedback_old_attr = char_attr;
5030 }
5031 char_attr = im_get_feedback_attr(feedback_col);
5032 if (char_attr < 0)
5033 char_attr = feedback_old_attr;
5034 feedback_col++;
5035 }
5036 else if (feedback_old_attr >= 0)
5037 {
5038 char_attr = feedback_old_attr;
5039 feedback_old_attr = -1;
5040 feedback_col = 0;
5041 }
5042 }
5043#endif
5044 /*
5045 * Handle the case where we are in column 0 but not on the first
5046 * character of the line and the user wants us to show us a
5047 * special character (via 'listchars' option "precedes:<char>".
5048 */
5049 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005050 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5052#ifdef FEAT_DIFF
5053 && filler_todo <= 0
5054#endif
5055 && draw_state > WL_NR
5056 && c != NUL)
5057 {
5058 c = lcs_prec;
5059 lcs_prec_todo = NUL;
5060#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005061 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5062 {
5063 /* Double-width character being overwritten by the "precedes"
5064 * character, need to fill up half the character. */
5065 c_extra = MB_FILLER_CHAR;
5066 n_extra = 1;
5067 n_attr = 2;
5068 extra_attr = hl_attr(HLF_AT);
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 mb_c = c;
5071 if (enc_utf8 && (*mb_char2len)(c) > 1)
5072 {
5073 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005074 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005075 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else
5078 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5079#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005080 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 saved_attr3 = char_attr; /* save current attr */
5083 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5084 n_attr3 = 1;
5085 }
5086 }
5087
5088 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005089 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005091 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005092#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005093 || did_line_attr == 1
5094#endif
5095 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005097#ifdef FEAT_SEARCH_EXTRA
5098 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005099
5100 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005101 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005102 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005103#endif
5104
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005105 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 * highlight match at end of line. If it's beyond the last
5107 * char on the screen, just overwrite that one (tricky!) Not
5108 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005109#ifdef FEAT_SEARCH_EXTRA
5110 prevcol_hl_flag = FALSE;
5111 if (prevcol == (long)search_hl.startcol)
5112 prevcol_hl_flag = TRUE;
5113 else
5114 {
5115 cur = wp->w_match_head;
5116 while (cur != NULL)
5117 {
5118 if (prevcol == (long)cur->hl.startcol)
5119 {
5120 prevcol_hl_flag = TRUE;
5121 break;
5122 }
5123 cur = cur->next;
5124 }
5125 }
5126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005128 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005129 && (VIsual_mode != Ctrl_V
5130 || lnum == VIsual.lnum
5131 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005132 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133#ifdef FEAT_SEARCH_EXTRA
5134 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005135 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005136# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005137 && did_line_attr <= 1
5138# endif
5139 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#endif
5141 ))
5142 {
5143 int n = 0;
5144
5145#ifdef FEAT_RIGHTLEFT
5146 if (wp->w_p_rl)
5147 {
5148 if (col < 0)
5149 n = 1;
5150 }
5151 else
5152#endif
5153 {
5154 if (col >= W_WIDTH(wp))
5155 n = -1;
5156 }
5157 if (n != 0)
5158 {
5159 /* At the window boundary, highlight the last character
5160 * instead (better than nothing). */
5161 off += n;
5162 col += n;
5163 }
5164 else
5165 {
5166 /* Add a blank character to highlight. */
5167 ScreenLines[off] = ' ';
5168#ifdef FEAT_MBYTE
5169 if (enc_utf8)
5170 ScreenLinesUC[off] = 0;
5171#endif
5172 }
5173#ifdef FEAT_SEARCH_EXTRA
5174 if (area_attr == 0)
5175 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005176 /* Use attributes from match with highest priority among
5177 * 'search_hl' and the match list. */
5178 char_attr = search_hl.attr;
5179 cur = wp->w_match_head;
5180 shl_flag = FALSE;
5181 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005182 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005183 if (shl_flag == FALSE
5184 && ((cur != NULL
5185 && cur->priority > SEARCH_HL_PRIORITY)
5186 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005187 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005188 shl = &search_hl;
5189 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005190 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005191 else
5192 shl = &cur->hl;
5193 if ((ptr - line) - 1 == (long)shl->startcol)
5194 char_attr = shl->attr;
5195 if (shl != &search_hl && cur != NULL)
5196 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199#endif
5200 ScreenAttrs[off] = char_attr;
5201#ifdef FEAT_RIGHTLEFT
5202 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005205 --off;
5206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 else
5208#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005211 ++off;
5212 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005213 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005214#ifdef FEAT_SYN_HL
5215 eol_hl_off = 1;
5216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
Bram Moolenaar91170f82006-05-05 21:15:17 +00005220 /*
5221 * At end of the text line.
5222 */
5223 if (c == NUL)
5224 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005225#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005226 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5227 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005228 {
5229 /* highlight last char after line */
5230 --col;
5231 --off;
5232 --vcol;
5233 }
5234
Bram Moolenaar1a384422010-07-14 19:53:30 +02005235 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005236 if (wp->w_p_wrap)
5237 v = wp->w_skipcol;
5238 else
5239 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005240
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005241 /* check if line ends before left margin */
5242 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005243 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005244#ifdef FEAT_CONCEAL
5245 /* Get rid of the boguscols now, we want to draw until the right
5246 * edge for 'cursorcolumn'. */
5247 col -= boguscols;
5248 boguscols = 0;
5249#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005250
5251 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005252 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005253
5254 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005255 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5256 && (int)wp->w_virtcol <
5257 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005258 && lnum != wp->w_cursor.lnum)
5259 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005260# ifdef FEAT_RIGHTLEFT
5261 && !wp->w_p_rl
5262# endif
5263 )
5264 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005265 int rightmost_vcol = 0;
5266 int i;
5267
5268 if (wp->w_p_cuc)
5269 rightmost_vcol = wp->w_virtcol;
5270 if (draw_color_col)
5271 /* determine rightmost colorcolumn to possibly draw */
5272 for (i = 0; color_cols[i] >= 0; ++i)
5273 if (rightmost_vcol < color_cols[i])
5274 rightmost_vcol = color_cols[i];
5275
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005276 while (col < W_WIDTH(wp))
5277 {
5278 ScreenLines[off] = ' ';
5279#ifdef FEAT_MBYTE
5280 if (enc_utf8)
5281 ScreenLinesUC[off] = 0;
5282#endif
5283 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005284 if (draw_color_col)
5285 draw_color_col = advance_color_col(VCOL_HLC,
5286 &color_cols);
5287
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005288 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005289 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005290 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005291 ScreenAttrs[off++] = hl_attr(HLF_MC);
5292 else
5293 ScreenAttrs[off++] = 0;
5294
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005295 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005296 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005297
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005298 ++vcol;
5299 }
5300 }
5301#endif
5302
Bram Moolenaar860cae12010-06-05 23:22:07 +02005303 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5304 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 row++;
5306
5307 /*
5308 * Update w_cline_height and w_cline_folded if the cursor line was
5309 * updated (saves a call to plines() later).
5310 */
5311 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5312 {
5313 curwin->w_cline_row = startrow;
5314 curwin->w_cline_height = row - startrow;
5315#ifdef FEAT_FOLDING
5316 curwin->w_cline_folded = FALSE;
5317#endif
5318 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5319 }
5320
5321 break;
5322 }
5323
5324 /* line continues beyond line end */
5325 if (lcs_ext
5326 && !wp->w_p_wrap
5327#ifdef FEAT_DIFF
5328 && filler_todo <= 0
5329#endif
5330 && (
5331#ifdef FEAT_RIGHTLEFT
5332 wp->w_p_rl ? col == 0 :
5333#endif
5334 col == W_WIDTH(wp) - 1)
5335 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005336 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5338 {
5339 c = lcs_ext;
5340 char_attr = hl_attr(HLF_AT);
5341#ifdef FEAT_MBYTE
5342 mb_c = c;
5343 if (enc_utf8 && (*mb_char2len)(c) > 1)
5344 {
5345 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005346 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005347 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 else
5350 mb_utf8 = FALSE;
5351#endif
5352 }
5353
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005354#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005355 /* advance to the next 'colorcolumn' */
5356 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005357 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005358
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005359 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005360 * highlight the cursor position itself.
5361 * Also highlight the 'colorcolumn' if it is different than
5362 * 'cursorcolumn' */
5363 vcol_save_attr = -1;
5364 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005365 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005366 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005367 && lnum != wp->w_cursor.lnum)
5368 {
5369 vcol_save_attr = char_attr;
5370 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5371 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005372 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005373 {
5374 vcol_save_attr = char_attr;
5375 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5376 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005377 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005378#endif
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /*
5381 * Store character to be displayed.
5382 * Skip characters that are left of the screen for 'nowrap'.
5383 */
5384 vcol_prev = vcol;
5385 if (draw_state < WL_LINE || n_skip <= 0)
5386 {
5387 /*
5388 * Store the character.
5389 */
5390#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5391 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5392 {
5393 /* A double-wide character is: put first halve in left cell. */
5394 --off;
5395 --col;
5396 }
5397#endif
5398 ScreenLines[off] = c;
5399#ifdef FEAT_MBYTE
5400 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005401 {
5402 if ((mb_c & 0xff00) == 0x8e00)
5403 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 else if (enc_utf8)
5407 {
5408 if (mb_utf8)
5409 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005410 int i;
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005413 if ((c & 0xff) == 0)
5414 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005415 for (i = 0; i < Screen_mco; ++i)
5416 {
5417 ScreenLinesC[i][off] = u8cc[i];
5418 if (u8cc[i] == 0)
5419 break;
5420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422 else
5423 ScreenLinesUC[off] = 0;
5424 }
5425 if (multi_attr)
5426 {
5427 ScreenAttrs[off] = multi_attr;
5428 multi_attr = 0;
5429 }
5430 else
5431#endif
5432 ScreenAttrs[off] = char_attr;
5433
5434#ifdef FEAT_MBYTE
5435 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5436 {
5437 /* Need to fill two screen columns. */
5438 ++off;
5439 ++col;
5440 if (enc_utf8)
5441 /* UTF-8: Put a 0 in the second screen char. */
5442 ScreenLines[off] = 0;
5443 else
5444 /* DBCS: Put second byte in the second screen char. */
5445 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005446 if (draw_state > WL_NR
5447#ifdef FEAT_DIFF
5448 && filler_todo <= 0
5449#endif
5450 )
5451 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 /* When "tocol" is halfway a character, set it to the end of
5453 * the character, otherwise highlighting won't stop. */
5454 if (tocol == vcol)
5455 ++tocol;
5456#ifdef FEAT_RIGHTLEFT
5457 if (wp->w_p_rl)
5458 {
5459 /* now it's time to backup one cell */
5460 --off;
5461 --col;
5462 }
5463#endif
5464 }
5465#endif
5466#ifdef FEAT_RIGHTLEFT
5467 if (wp->w_p_rl)
5468 {
5469 --off;
5470 --col;
5471 }
5472 else
5473#endif
5474 {
5475 ++off;
5476 ++col;
5477 }
5478 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005479#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005480 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005481 {
5482 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005483 ++vcol_off;
5484 if (n_extra > 0)
5485 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005486 if (wp->w_p_wrap)
5487 {
5488 /*
5489 * Special voodoo required if 'wrap' is on.
5490 *
5491 * Advance the column indicator to force the line
5492 * drawing to wrap early. This will make the line
5493 * take up the same screen space when parts are concealed,
5494 * so that cursor line computations aren't messed up.
5495 *
5496 * To avoid the fictitious advance of 'col' causing
5497 * trailing junk to be written out of the screen line
5498 * we are building, 'boguscols' keeps track of the number
5499 * of bad columns we have advanced.
5500 */
5501 if (n_extra > 0)
5502 {
5503 vcol += n_extra;
5504# ifdef FEAT_RIGHTLEFT
5505 if (wp->w_p_rl)
5506 {
5507 col -= n_extra;
5508 boguscols -= n_extra;
5509 }
5510 else
5511# endif
5512 {
5513 col += n_extra;
5514 boguscols += n_extra;
5515 }
5516 n_extra = 0;
5517 n_attr = 0;
5518 }
5519
5520
5521# ifdef FEAT_MBYTE
5522 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5523 {
5524 /* Need to fill two screen columns. */
5525# ifdef FEAT_RIGHTLEFT
5526 if (wp->w_p_rl)
5527 {
5528 --boguscols;
5529 --col;
5530 }
5531 else
5532# endif
5533 {
5534 ++boguscols;
5535 ++col;
5536 }
5537 }
5538# endif
5539
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 else
5554 {
5555 if (n_extra > 0)
5556 {
5557 vcol += n_extra;
5558 n_extra = 0;
5559 n_attr = 0;
5560 }
5561 }
5562
5563 }
5564#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 else
5566 --n_skip;
5567
Bram Moolenaar64486672010-05-16 15:46:46 +02005568 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5569 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005570 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571#ifdef FEAT_DIFF
5572 && filler_todo <= 0
5573#endif
5574 )
5575 ++vcol;
5576
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005577#ifdef FEAT_SYN_HL
5578 if (vcol_save_attr >= 0)
5579 char_attr = vcol_save_attr;
5580#endif
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 /* restore attributes after "predeces" in 'listchars' */
5583 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5584 char_attr = saved_attr3;
5585
5586 /* restore attributes after last 'listchars' or 'number' char */
5587 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5588 char_attr = saved_attr2;
5589
5590 /*
5591 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005592 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 */
5594 if ((
5595#ifdef FEAT_RIGHTLEFT
5596 wp->w_p_rl ? (col < 0) :
5597#endif
5598 (col >= W_WIDTH(wp)))
5599 && (*ptr != NUL
5600#ifdef FEAT_DIFF
5601 || filler_todo > 0
5602#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005603 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5605 )
5606 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005607#ifdef FEAT_CONCEAL
5608 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5609 (int)W_WIDTH(wp), wp->w_p_rl);
5610 boguscols = 0;
5611#else
5612 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5613 (int)W_WIDTH(wp), wp->w_p_rl);
5614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 ++row;
5616 ++screen_row;
5617
5618 /* When not wrapping and finished diff lines, or when displayed
5619 * '$' and highlighting until last column, break here. */
5620 if ((!wp->w_p_wrap
5621#ifdef FEAT_DIFF
5622 && filler_todo <= 0
5623#endif
5624 ) || lcs_eol_one == -1)
5625 break;
5626
5627 /* When the window is too narrow draw all "@" lines. */
5628 if (draw_state != WL_LINE
5629#ifdef FEAT_DIFF
5630 && filler_todo <= 0
5631#endif
5632 )
5633 {
5634 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005635#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 draw_vsep_win(wp, row);
5637#endif
5638 row = endrow;
5639 }
5640
5641 /* When line got too long for screen break here. */
5642 if (row == endrow)
5643 {
5644 ++row;
5645 break;
5646 }
5647
5648 if (screen_cur_row == screen_row - 1
5649#ifdef FEAT_DIFF
5650 && filler_todo <= 0
5651#endif
5652 && W_WIDTH(wp) == Columns)
5653 {
5654 /* Remember that the line wraps, used for modeless copy. */
5655 LineWraps[screen_row - 1] = TRUE;
5656
5657 /*
5658 * Special trick to make copy/paste of wrapped lines work with
5659 * xterm/screen: write an extra character beyond the end of
5660 * the line. This will work with all terminal types
5661 * (regardless of the xn,am settings).
5662 * Only do this on a fast tty.
5663 * Only do this if the cursor is on the current line
5664 * (something has been written in it).
5665 * Don't do this for the GUI.
5666 * Don't do this for double-width characters.
5667 * Don't do this for a window not at the right screen border.
5668 */
5669 if (p_tf
5670#ifdef FEAT_GUI
5671 && !gui.in_use
5672#endif
5673#ifdef FEAT_MBYTE
5674 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005675 && ((*mb_off2cells)(LineOffset[screen_row],
5676 LineOffset[screen_row] + screen_Columns)
5677 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005679 + (int)Columns - 2,
5680 LineOffset[screen_row] + screen_Columns)
5681 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682#endif
5683 )
5684 {
5685 /* First make sure we are at the end of the screen line,
5686 * then output the same character again to let the
5687 * terminal know about the wrap. If the terminal doesn't
5688 * auto-wrap, we overwrite the character. */
5689 if (screen_cur_col != W_WIDTH(wp))
5690 screen_char(LineOffset[screen_row - 1]
5691 + (unsigned)Columns - 1,
5692 screen_row - 1, (int)(Columns - 1));
5693
5694#ifdef FEAT_MBYTE
5695 /* When there is a multi-byte character, just output a
5696 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005697 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5698 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 out_char(' ');
5700 else
5701#endif
5702 out_char(ScreenLines[LineOffset[screen_row - 1]
5703 + (Columns - 1)]);
5704 /* force a redraw of the first char on the next line */
5705 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5706 screen_start(); /* don't know where cursor is now */
5707 }
5708 }
5709
5710 col = 0;
5711 off = (unsigned)(current_ScreenLine - ScreenLines);
5712#ifdef FEAT_RIGHTLEFT
5713 if (wp->w_p_rl)
5714 {
5715 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5716 off += col;
5717 }
5718#endif
5719
5720 /* reset the drawing state for the start of a wrapped line */
5721 draw_state = WL_START;
5722 saved_n_extra = n_extra;
5723 saved_p_extra = p_extra;
5724 saved_c_extra = c_extra;
5725 saved_char_attr = char_attr;
5726 n_extra = 0;
5727 lcs_prec_todo = lcs_prec;
5728#ifdef FEAT_LINEBREAK
5729# ifdef FEAT_DIFF
5730 if (filler_todo <= 0)
5731# endif
5732 need_showbreak = TRUE;
5733#endif
5734#ifdef FEAT_DIFF
5735 --filler_todo;
5736 /* When the filler lines are actually below the last line of the
5737 * file, don't draw the line itself, break here. */
5738 if (filler_todo == 0 && wp->w_botfill)
5739 break;
5740#endif
5741 }
5742
5743 } /* for every character in the line */
5744
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005745#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005746 /* After an empty line check first word for capital. */
5747 if (*skipwhite(line) == NUL)
5748 {
5749 capcol_lnum = lnum + 1;
5750 cap_col = 0;
5751 }
5752#endif
5753
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 return row;
5755}
5756
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005757#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005758static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005759
5760/*
5761 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005762 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005763 */
5764 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005765comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005766{
5767 int i;
5768
5769 for (i = 0; i < Screen_mco; ++i)
5770 {
5771 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5772 return TRUE;
5773 if (ScreenLinesC[i][off_from] == 0)
5774 break;
5775 }
5776 return FALSE;
5777}
5778#endif
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780/*
5781 * Check whether the given character needs redrawing:
5782 * - the (first byte of the) character is different
5783 * - the attributes are different
5784 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005785 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 */
5787 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005788char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 if (cols > 0
5791 && ((ScreenLines[off_from] != ScreenLines[off_to]
5792 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5793
5794#ifdef FEAT_MBYTE
5795 || (enc_dbcs != 0
5796 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5797 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5798 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5799 : (cols > 1 && ScreenLines[off_from + 1]
5800 != ScreenLines[off_to + 1])))
5801 || (enc_utf8
5802 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5803 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005804 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005805 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5806 && ScreenLines[off_from + 1]
5807 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#endif
5809 ))
5810 return TRUE;
5811 return FALSE;
5812}
5813
5814/*
5815 * Move one "cooked" screen line to the screen, but only the characters that
5816 * have actually changed. Handle insert/delete character.
5817 * "coloff" gives the first column on the screen for this line.
5818 * "endcol" gives the columns where valid characters are.
5819 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5820 * needs to be cleared, negative otherwise.
5821 * "rlflag" is TRUE in a rightleft window:
5822 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5823 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5824 */
5825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005826screen_line(
5827 int row,
5828 int coloff,
5829 int endcol,
5830 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005832 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005834 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 unsigned off_from;
5837 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005838#ifdef FEAT_MBYTE
5839 unsigned max_off_from;
5840 unsigned max_off_to;
5841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005843#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 int hl;
5845#endif
5846 int force = FALSE; /* force update rest of the line */
5847 int redraw_this /* bool: does character need redraw? */
5848#ifdef FEAT_GUI
5849 = TRUE /* For GUI when while-loop empty */
5850#endif
5851 ;
5852 int redraw_next; /* redraw_this for next character */
5853#ifdef FEAT_MBYTE
5854 int clear_next = FALSE;
5855 int char_cells; /* 1: normal char */
5856 /* 2: occupies two display cells */
5857# define CHAR_CELLS char_cells
5858#else
5859# define CHAR_CELLS 1
5860#endif
5861
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005862 /* Check for illegal row and col, just in case. */
5863 if (row >= Rows)
5864 row = Rows - 1;
5865 if (endcol > Columns)
5866 endcol = Columns;
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868# ifdef FEAT_CLIPBOARD
5869 clip_may_clear_selection(row, row);
5870# endif
5871
5872 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5873 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005874#ifdef FEAT_MBYTE
5875 max_off_from = off_from + screen_Columns;
5876 max_off_to = LineOffset[row] + screen_Columns;
5877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
5879#ifdef FEAT_RIGHTLEFT
5880 if (rlflag)
5881 {
5882 /* Clear rest first, because it's left of the text. */
5883 if (clear_width > 0)
5884 {
5885 while (col <= endcol && ScreenLines[off_to] == ' '
5886 && ScreenAttrs[off_to] == 0
5887# ifdef FEAT_MBYTE
5888 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5889# endif
5890 )
5891 {
5892 ++off_to;
5893 ++col;
5894 }
5895 if (col <= endcol)
5896 screen_fill(row, row + 1, col + coloff,
5897 endcol + coloff + 1, ' ', ' ', 0);
5898 }
5899 col = endcol + 1;
5900 off_to = LineOffset[row] + col + coloff;
5901 off_from += col;
5902 endcol = (clear_width > 0 ? clear_width : -clear_width);
5903 }
5904#endif /* FEAT_RIGHTLEFT */
5905
5906 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5907
5908 while (col < endcol)
5909 {
5910#ifdef FEAT_MBYTE
5911 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005912 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 else
5914 char_cells = 1;
5915#endif
5916
5917 redraw_this = redraw_next;
5918 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5919 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5920
5921#ifdef FEAT_GUI
5922 /* If the next character was bold, then redraw the current character to
5923 * remove any pixels that might have spilt over into us. This only
5924 * happens in the GUI.
5925 */
5926 if (redraw_next && gui.in_use)
5927 {
5928 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005929 if (hl > HL_ALL)
5930 hl = syn_attr2attr(hl);
5931 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 redraw_this = TRUE;
5933 }
5934#endif
5935
5936 if (redraw_this)
5937 {
5938 /*
5939 * Special handling when 'xs' termcap flag set (hpterm):
5940 * Attributes for characters are stored at the position where the
5941 * cursor is when writing the highlighting code. The
5942 * start-highlighting code must be written with the cursor on the
5943 * first highlighted character. The stop-highlighting code must
5944 * be written with the cursor just after the last highlighted
5945 * character.
5946 * Overwriting a character doesn't remove it's highlighting. Need
5947 * to clear the rest of the line, and force redrawing it
5948 * completely.
5949 */
5950 if ( p_wiv
5951 && !force
5952#ifdef FEAT_GUI
5953 && !gui.in_use
5954#endif
5955 && ScreenAttrs[off_to] != 0
5956 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5957 {
5958 /*
5959 * Need to remove highlighting attributes here.
5960 */
5961 windgoto(row, col + coloff);
5962 out_str(T_CE); /* clear rest of this screen line */
5963 screen_start(); /* don't know where cursor is now */
5964 force = TRUE; /* force redraw of rest of the line */
5965 redraw_next = TRUE; /* or else next char would miss out */
5966
5967 /*
5968 * If the previous character was highlighted, need to stop
5969 * highlighting at this character.
5970 */
5971 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5972 {
5973 screen_attr = ScreenAttrs[off_to - 1];
5974 term_windgoto(row, col + coloff);
5975 screen_stop_highlight();
5976 }
5977 else
5978 screen_attr = 0; /* highlighting has stopped */
5979 }
5980#ifdef FEAT_MBYTE
5981 if (enc_dbcs != 0)
5982 {
5983 /* Check if overwriting a double-byte with a single-byte or
5984 * the other way around requires another character to be
5985 * redrawn. For UTF-8 this isn't needed, because comparing
5986 * ScreenLinesUC[] is sufficient. */
5987 if (char_cells == 1
5988 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005989 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {
5991 /* Writing a single-cell character over a double-cell
5992 * character: need to redraw the next cell. */
5993 ScreenLines[off_to + 1] = 0;
5994 redraw_next = TRUE;
5995 }
5996 else if (char_cells == 2
5997 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005998 && (*mb_off2cells)(off_to, max_off_to) == 1
5999 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
6001 /* Writing the second half of a double-cell character over
6002 * a double-cell character: need to redraw the second
6003 * cell. */
6004 ScreenLines[off_to + 2] = 0;
6005 redraw_next = TRUE;
6006 }
6007
6008 if (enc_dbcs == DBCS_JPNU)
6009 ScreenLines2[off_to] = ScreenLines2[off_from];
6010 }
6011 /* When writing a single-width character over a double-width
6012 * character and at the end of the redrawn text, need to clear out
6013 * the right halve of the old character.
6014 * Also required when writing the right halve of a double-width
6015 * char over the left halve of an existing one. */
6016 if (has_mbyte && col + char_cells == endcol
6017 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006018 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006020 && (*mb_off2cells)(off_to, max_off_to) == 1
6021 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 clear_next = TRUE;
6023#endif
6024
6025 ScreenLines[off_to] = ScreenLines[off_from];
6026#ifdef FEAT_MBYTE
6027 if (enc_utf8)
6028 {
6029 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6030 if (ScreenLinesUC[off_from] != 0)
6031 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006032 int i;
6033
6034 for (i = 0; i < Screen_mco; ++i)
6035 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 }
6037 }
6038 if (char_cells == 2)
6039 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6040#endif
6041
6042#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006043 /* The bold trick makes a single column of pixels appear in the
6044 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * character should be redrawn too. This happens for our own GUI
6046 * and for some xterms. */
6047 if (
6048# ifdef FEAT_GUI
6049 gui.in_use
6050# endif
6051# if defined(FEAT_GUI) && defined(UNIX)
6052 ||
6053# endif
6054# ifdef UNIX
6055 term_is_xterm
6056# endif
6057 )
6058 {
6059 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006060 if (hl > HL_ALL)
6061 hl = syn_attr2attr(hl);
6062 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 redraw_next = TRUE;
6064 }
6065#endif
6066 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6067#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006068 /* For simplicity set the attributes of second half of a
6069 * double-wide character equal to the first half. */
6070 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006072
6073 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 else
6076#endif
6077 screen_char(off_to, row, col + coloff);
6078 }
6079 else if ( p_wiv
6080#ifdef FEAT_GUI
6081 && !gui.in_use
6082#endif
6083 && col + coloff > 0)
6084 {
6085 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6086 {
6087 /*
6088 * Don't output stop-highlight when moving the cursor, it will
6089 * stop the highlighting when it should continue.
6090 */
6091 screen_attr = 0;
6092 }
6093 else if (screen_attr != 0)
6094 screen_stop_highlight();
6095 }
6096
6097 off_to += CHAR_CELLS;
6098 off_from += CHAR_CELLS;
6099 col += CHAR_CELLS;
6100 }
6101
6102#ifdef FEAT_MBYTE
6103 if (clear_next)
6104 {
6105 /* Clear the second half of a double-wide character of which the left
6106 * half was overwritten with a single-wide character. */
6107 ScreenLines[off_to] = ' ';
6108 if (enc_utf8)
6109 ScreenLinesUC[off_to] = 0;
6110 screen_char(off_to, row, col + coloff);
6111 }
6112#endif
6113
6114 if (clear_width > 0
6115#ifdef FEAT_RIGHTLEFT
6116 && !rlflag
6117#endif
6118 )
6119 {
6120#ifdef FEAT_GUI
6121 int startCol = col;
6122#endif
6123
6124 /* blank out the rest of the line */
6125 while (col < clear_width && ScreenLines[off_to] == ' '
6126 && ScreenAttrs[off_to] == 0
6127#ifdef FEAT_MBYTE
6128 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6129#endif
6130 )
6131 {
6132 ++off_to;
6133 ++col;
6134 }
6135 if (col < clear_width)
6136 {
6137#ifdef FEAT_GUI
6138 /*
6139 * In the GUI, clearing the rest of the line may leave pixels
6140 * behind if the first character cleared was bold. Some bold
6141 * fonts spill over the left. In this case we redraw the previous
6142 * character too. If we didn't skip any blanks above, then we
6143 * only redraw if the character wasn't already redrawn anyway.
6144 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006145 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 hl = ScreenAttrs[off_to];
6148 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006149 {
6150 int prev_cells = 1;
6151# ifdef FEAT_MBYTE
6152 if (enc_utf8)
6153 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6154 * that its width is 2. */
6155 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6156 else if (enc_dbcs != 0)
6157 {
6158 /* find previous character by counting from first
6159 * column and get its width. */
6160 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006161 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006162
6163 while (off < off_to)
6164 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006165 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006166 off += prev_cells;
6167 }
6168 }
6169
6170 if (enc_dbcs != 0 && prev_cells > 1)
6171 screen_char_2(off_to - prev_cells, row,
6172 col + coloff - prev_cells);
6173 else
6174# endif
6175 screen_char(off_to - prev_cells, row,
6176 col + coloff - prev_cells);
6177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
6179#endif
6180 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6181 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006182#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 off_to += clear_width - col;
6184 col = clear_width;
6185#endif
6186 }
6187 }
6188
6189 if (clear_width > 0)
6190 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006191#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 /* For a window that's left of another, draw the separator char. */
6193 if (col + coloff < Columns)
6194 {
6195 int c;
6196
6197 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006198 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006200 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6201 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202# endif
6203 || ScreenAttrs[off_to] != hl)
6204 {
6205 ScreenLines[off_to] = c;
6206 ScreenAttrs[off_to] = hl;
6207# ifdef FEAT_MBYTE
6208 if (enc_utf8)
6209 {
6210 if (c >= 0x80)
6211 {
6212 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006213 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215 else
6216 ScreenLinesUC[off_to] = 0;
6217 }
6218# endif
6219 screen_char(off_to, row, col + coloff);
6220 }
6221 }
6222 else
6223#endif
6224 LineWraps[row] = FALSE;
6225 }
6226}
6227
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006228#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006230 * Mirror text "str" for right-left displaying.
6231 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006234rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 char_u *p1, *p2;
6237 int t;
6238
6239 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6240 {
6241 t = *p1;
6242 *p1 = *p2;
6243 *p2 = t;
6244 }
6245}
6246#endif
6247
6248#if defined(FEAT_WINDOWS) || defined(PROTO)
6249/*
6250 * mark all status lines for redraw; used after first :cd
6251 */
6252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006253status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 win_T *wp;
6256
6257 for (wp = firstwin; wp; wp = wp->w_next)
6258 if (wp->w_status_height)
6259 {
6260 wp->w_redr_status = TRUE;
6261 redraw_later(VALID);
6262 }
6263}
6264
6265/*
6266 * mark all status lines of the current buffer for redraw
6267 */
6268 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006269status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 win_T *wp;
6272
6273 for (wp = firstwin; wp; wp = wp->w_next)
6274 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6275 {
6276 wp->w_redr_status = TRUE;
6277 redraw_later(VALID);
6278 }
6279}
6280
6281/*
6282 * Redraw all status lines that need to be redrawn.
6283 */
6284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006285redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 win_T *wp;
6288
6289 for (wp = firstwin; wp; wp = wp->w_next)
6290 if (wp->w_redr_status)
6291 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006292 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006293 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294}
6295#endif
6296
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006297#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298/*
6299 * Redraw all status lines at the bottom of frame "frp".
6300 */
6301 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006302win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 if (frp->fr_layout == FR_LEAF)
6305 frp->fr_win->w_redr_status = TRUE;
6306 else if (frp->fr_layout == FR_ROW)
6307 {
6308 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6309 win_redraw_last_status(frp);
6310 }
6311 else /* frp->fr_layout == FR_COL */
6312 {
6313 frp = frp->fr_child;
6314 while (frp->fr_next != NULL)
6315 frp = frp->fr_next;
6316 win_redraw_last_status(frp);
6317 }
6318}
6319#endif
6320
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006321#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322/*
6323 * Draw the verticap separator right of window "wp" starting with line "row".
6324 */
6325 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006326draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 int hl;
6329 int c;
6330
6331 if (wp->w_vsep_width)
6332 {
6333 /* draw the vertical separator right of this window */
6334 c = fillchar_vsep(&hl);
6335 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6336 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6337 c, ' ', hl);
6338 }
6339}
6340#endif
6341
6342#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006343static int status_match_len(expand_T *xp, char_u *s);
6344static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006347 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 */
6349 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006350status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 int len = 0;
6353
6354#ifdef FEAT_MENU
6355 int emenu = (xp->xp_context == EXPAND_MENUS
6356 || xp->xp_context == EXPAND_MENUNAMES);
6357
6358 /* Check for menu separators - replace with '|'. */
6359 if (emenu && menu_is_separator(s))
6360 return 1;
6361#endif
6362
6363 while (*s != NUL)
6364 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006365 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006366 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006367 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369
6370 return len;
6371}
6372
6373/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006374 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006375 * These are backslashes used for escaping. Do show backslashes in help tags.
6376 */
6377 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006378skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006379{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006380 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006381#ifdef FEAT_MENU
6382 || ((xp->xp_context == EXPAND_MENUS
6383 || xp->xp_context == EXPAND_MENUNAMES)
6384 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6385#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006386 )
6387 {
6388#ifndef BACKSLASH_IN_FILENAME
6389 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6390 return 2;
6391#endif
6392 return 1;
6393 }
6394 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006395}
6396
6397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * Show wildchar matches in the status line.
6399 * Show at least the "match" item.
6400 * We start at item 'first_match' in the list and show all matches that fit.
6401 *
6402 * If inversion is possible we use it. Else '=' characters are used.
6403 */
6404 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006405win_redr_status_matches(
6406 expand_T *xp,
6407 int num_matches,
6408 char_u **matches, /* list of matches */
6409 int match,
6410 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6413 int row;
6414 char_u *buf;
6415 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006416 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 int fillchar;
6418 int attr;
6419 int i;
6420 int highlight = TRUE;
6421 char_u *selstart = NULL;
6422 int selstart_col = 0;
6423 char_u *selend = NULL;
6424 static int first_match = 0;
6425 int add_left = FALSE;
6426 char_u *s;
6427#ifdef FEAT_MENU
6428 int emenu;
6429#endif
6430#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6431 int l;
6432#endif
6433
6434 if (matches == NULL) /* interrupted completion? */
6435 return;
6436
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006437#ifdef FEAT_MBYTE
6438 if (has_mbyte)
6439 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6440 else
6441#endif
6442 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (buf == NULL)
6444 return;
6445
6446 if (match == -1) /* don't show match but original text */
6447 {
6448 match = 0;
6449 highlight = FALSE;
6450 }
6451 /* count 1 for the ending ">" */
6452 clen = status_match_len(xp, L_MATCH(match)) + 3;
6453 if (match == 0)
6454 first_match = 0;
6455 else if (match < first_match)
6456 {
6457 /* jumping left, as far as we can go */
6458 first_match = match;
6459 add_left = TRUE;
6460 }
6461 else
6462 {
6463 /* check if match fits on the screen */
6464 for (i = first_match; i < match; ++i)
6465 clen += status_match_len(xp, L_MATCH(i)) + 2;
6466 if (first_match > 0)
6467 clen += 2;
6468 /* jumping right, put match at the left */
6469 if ((long)clen > Columns)
6470 {
6471 first_match = match;
6472 /* if showing the last match, we can add some on the left */
6473 clen = 2;
6474 for (i = match; i < num_matches; ++i)
6475 {
6476 clen += status_match_len(xp, L_MATCH(i)) + 2;
6477 if ((long)clen >= Columns)
6478 break;
6479 }
6480 if (i == num_matches)
6481 add_left = TRUE;
6482 }
6483 }
6484 if (add_left)
6485 while (first_match > 0)
6486 {
6487 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6488 if ((long)clen >= Columns)
6489 break;
6490 --first_match;
6491 }
6492
6493 fillchar = fillchar_status(&attr, TRUE);
6494
6495 if (first_match == 0)
6496 {
6497 *buf = NUL;
6498 len = 0;
6499 }
6500 else
6501 {
6502 STRCPY(buf, "< ");
6503 len = 2;
6504 }
6505 clen = len;
6506
6507 i = first_match;
6508 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6509 {
6510 if (i == match)
6511 {
6512 selstart = buf + len;
6513 selstart_col = clen;
6514 }
6515
6516 s = L_MATCH(i);
6517 /* Check for menu separators - replace with '|' */
6518#ifdef FEAT_MENU
6519 emenu = (xp->xp_context == EXPAND_MENUS
6520 || xp->xp_context == EXPAND_MENUNAMES);
6521 if (emenu && menu_is_separator(s))
6522 {
6523 STRCPY(buf + len, transchar('|'));
6524 l = (int)STRLEN(buf + len);
6525 len += l;
6526 clen += l;
6527 }
6528 else
6529#endif
6530 for ( ; *s != NUL; ++s)
6531 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006532 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 clen += ptr2cells(s);
6534#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006535 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 STRNCPY(buf + len, s, l);
6538 s += l - 1;
6539 len += l;
6540 }
6541 else
6542#endif
6543 {
6544 STRCPY(buf + len, transchar_byte(*s));
6545 len += (int)STRLEN(buf + len);
6546 }
6547 }
6548 if (i == match)
6549 selend = buf + len;
6550
6551 *(buf + len++) = ' ';
6552 *(buf + len++) = ' ';
6553 clen += 2;
6554 if (++i == num_matches)
6555 break;
6556 }
6557
6558 if (i != num_matches)
6559 {
6560 *(buf + len++) = '>';
6561 ++clen;
6562 }
6563
6564 buf[len] = NUL;
6565
6566 row = cmdline_row - 1;
6567 if (row >= 0)
6568 {
6569 if (wild_menu_showing == 0)
6570 {
6571 if (msg_scrolled > 0)
6572 {
6573 /* Put the wildmenu just above the command line. If there is
6574 * no room, scroll the screen one line up. */
6575 if (cmdline_row == Rows - 1)
6576 {
6577 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6578 ++msg_scrolled;
6579 }
6580 else
6581 {
6582 ++cmdline_row;
6583 ++row;
6584 }
6585 wild_menu_showing = WM_SCROLLED;
6586 }
6587 else
6588 {
6589 /* Create status line if needed by setting 'laststatus' to 2.
6590 * Set 'winminheight' to zero to avoid that the window is
6591 * resized. */
6592 if (lastwin->w_status_height == 0)
6593 {
6594 save_p_ls = p_ls;
6595 save_p_wmh = p_wmh;
6596 p_ls = 2;
6597 p_wmh = 0;
6598 last_status(FALSE);
6599 }
6600 wild_menu_showing = WM_SHOWN;
6601 }
6602 }
6603
6604 screen_puts(buf, row, 0, attr);
6605 if (selstart != NULL && highlight)
6606 {
6607 *selend = NUL;
6608 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6609 }
6610
6611 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6612 }
6613
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006614#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 win_redraw_last_status(topframe);
6616#else
6617 lastwin->w_redr_status = TRUE;
6618#endif
6619 vim_free(buf);
6620}
6621#endif
6622
6623#if defined(FEAT_WINDOWS) || defined(PROTO)
6624/*
6625 * Redraw the status line of window wp.
6626 *
6627 * If inversion is possible we use it. Else '=' characters are used.
6628 */
6629 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006630win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 int row;
6633 char_u *p;
6634 int len;
6635 int fillchar;
6636 int attr;
6637 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006638 static int busy = FALSE;
6639
6640 /* It's possible to get here recursively when 'statusline' (indirectly)
6641 * invokes ":redrawstatus". Simply ignore the call then. */
6642 if (busy)
6643 return;
6644 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 wp->w_redr_status = FALSE;
6647 if (wp->w_status_height == 0)
6648 {
6649 /* no status line, can only be last window */
6650 redraw_cmdline = TRUE;
6651 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006652 else if (!redrawing()
6653#ifdef FEAT_INS_EXPAND
6654 /* don't update status line when popup menu is visible and may be
6655 * drawn over it */
6656 || pum_visible()
6657#endif
6658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
6660 /* Don't redraw right now, do it later. */
6661 wp->w_redr_status = TRUE;
6662 }
6663#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006664 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
6666 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006667 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 }
6669#endif
6670 else
6671 {
6672 fillchar = fillchar_status(&attr, wp == curwin);
6673
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006674 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 p = NameBuff;
6676 len = (int)STRLEN(p);
6677
6678 if (wp->w_buffer->b_help
6679#ifdef FEAT_QUICKFIX
6680 || wp->w_p_pvw
6681#endif
6682 || bufIsChanged(wp->w_buffer)
6683 || wp->w_buffer->b_p_ro)
6684 *(p + len++) = ' ';
6685 if (wp->w_buffer->b_help)
6686 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006687 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 len += (int)STRLEN(p + len);
6689 }
6690#ifdef FEAT_QUICKFIX
6691 if (wp->w_p_pvw)
6692 {
6693 STRCPY(p + len, _("[Preview]"));
6694 len += (int)STRLEN(p + len);
6695 }
6696#endif
6697 if (bufIsChanged(wp->w_buffer))
6698 {
6699 STRCPY(p + len, "[+]");
6700 len += 3;
6701 }
6702 if (wp->w_buffer->b_p_ro)
6703 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006704 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 len += 4;
6706 }
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6709 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6710 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6711 if (this_ru_col <= 1)
6712 {
6713 p = (char_u *)"<"; /* No room for file name! */
6714 len = 1;
6715 }
6716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717#ifdef FEAT_MBYTE
6718 if (has_mbyte)
6719 {
6720 int clen = 0, i;
6721
6722 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006723 clen = mb_string2cells(p, -1);
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 /* Find first character that will fit.
6726 * Going from start to end is much faster for DBCS. */
6727 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006728 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 clen -= (*mb_ptr2cells)(p + i);
6730 len = clen;
6731 if (i > 0)
6732 {
6733 p = p + i - 1;
6734 *p = '<';
6735 ++len;
6736 }
6737
6738 }
6739 else
6740#endif
6741 if (len > this_ru_col - 1)
6742 {
6743 p += len - (this_ru_col - 1);
6744 *p = '<';
6745 len = this_ru_col - 1;
6746 }
6747
6748 row = W_WINROW(wp) + wp->w_height;
6749 screen_puts(p, row, W_WINCOL(wp), attr);
6750 screen_fill(row, row + 1, len + W_WINCOL(wp),
6751 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6752
6753 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6754 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6755 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6756 - 1 + W_WINCOL(wp)), attr);
6757
6758#ifdef FEAT_CMDL_INFO
6759 win_redr_ruler(wp, TRUE);
6760#endif
6761 }
6762
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 /*
6764 * May need to draw the character below the vertical separator.
6765 */
6766 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6767 {
6768 if (stl_connected(wp))
6769 fillchar = fillchar_status(&attr, wp == curwin);
6770 else
6771 fillchar = fillchar_vsep(&attr);
6772 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6773 attr);
6774 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006775 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776}
6777
Bram Moolenaar238a5642006-02-21 22:12:05 +00006778#ifdef FEAT_STL_OPT
6779/*
6780 * Redraw the status line according to 'statusline' and take care of any
6781 * errors encountered.
6782 */
6783 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006784redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006785{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006786 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006787 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006788
6789 /* When called recursively return. This can happen when the statusline
6790 * contains an expression that triggers a redraw. */
6791 if (entered)
6792 return;
6793 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006794
Bram Moolenaara742e082016-04-05 21:10:38 +02006795 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006796 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006797 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006798 {
6799 /* When there is an error disable the statusline, otherwise the
6800 * display is messed up with errors and a redraw triggers the problem
6801 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006802 set_string_option_direct((char_u *)"statusline", -1,
6803 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006804 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006805 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006806 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006807 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006808}
6809#endif
6810
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811/*
6812 * Return TRUE if the status line of window "wp" is connected to the status
6813 * line of the window right of it. If not, then it's a vertical separator.
6814 * Only call if (wp->w_vsep_width != 0).
6815 */
6816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006817stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 frame_T *fr;
6820
6821 fr = wp->w_frame;
6822 while (fr->fr_parent != NULL)
6823 {
6824 if (fr->fr_parent->fr_layout == FR_COL)
6825 {
6826 if (fr->fr_next != NULL)
6827 break;
6828 }
6829 else
6830 {
6831 if (fr->fr_next != NULL)
6832 return TRUE;
6833 }
6834 fr = fr->fr_parent;
6835 }
6836 return FALSE;
6837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839#endif /* FEAT_WINDOWS */
6840
6841#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6842/*
6843 * Get the value to show for the language mappings, active 'keymap'.
6844 */
6845 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006846get_keymap_str(
6847 win_T *wp,
6848 char_u *buf, /* buffer for the result */
6849 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 char_u *p;
6852
6853 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6854 return FALSE;
6855
6856 {
6857#ifdef FEAT_EVAL
6858 buf_T *old_curbuf = curbuf;
6859 win_T *old_curwin = curwin;
6860 char_u *s;
6861
6862 curbuf = wp->w_buffer;
6863 curwin = wp;
6864 STRCPY(buf, "b:keymap_name"); /* must be writable */
6865 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006866 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 --emsg_skip;
6868 curbuf = old_curbuf;
6869 curwin = old_curwin;
6870 if (p == NULL || *p == NUL)
6871#endif
6872 {
6873#ifdef FEAT_KEYMAP
6874 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6875 p = wp->w_buffer->b_p_keymap;
6876 else
6877#endif
6878 p = (char_u *)"lang";
6879 }
6880 if ((int)(STRLEN(p) + 3) < len)
6881 sprintf((char *)buf, "<%s>", p);
6882 else
6883 buf[0] = NUL;
6884#ifdef FEAT_EVAL
6885 vim_free(s);
6886#endif
6887 }
6888 return buf[0] != NUL;
6889}
6890#endif
6891
6892#if defined(FEAT_STL_OPT) || defined(PROTO)
6893/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006894 * Redraw the status line or ruler of window "wp".
6895 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 */
6897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006898win_redr_custom(
6899 win_T *wp,
6900 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006902 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 int attr;
6904 int curattr;
6905 int row;
6906 int col = 0;
6907 int maxwidth;
6908 int width;
6909 int n;
6910 int len;
6911 int fillchar;
6912 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006913 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006915 struct stl_hlrec hltab[STL_MAX_ITEM];
6916 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006917 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006918 win_T *ewp;
6919 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
Bram Moolenaar1d633412013-12-11 15:52:01 +01006921 /* There is a tiny chance that this gets called recursively: When
6922 * redrawing a status line triggers redrawing the ruler or tabline.
6923 * Avoid trouble by not allowing recursion. */
6924 if (entered)
6925 return;
6926 entered = TRUE;
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006929 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006931 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006932 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006933 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006934 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006935 attr = hl_attr(HLF_TPF);
6936 maxwidth = Columns;
6937# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006938 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006941 else
6942 {
6943 row = W_WINROW(wp) + wp->w_height;
6944 fillchar = fillchar_status(&attr, wp == curwin);
6945 maxwidth = W_WIDTH(wp);
6946
6947 if (draw_ruler)
6948 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006949 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006950 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006951 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006952 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006953 if (*++stl == '-')
6954 stl++;
6955 if (atoi((char *)stl))
6956 while (VIM_ISDIGIT(*stl))
6957 stl++;
6958 if (*stl++ != '(')
6959 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006960 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006961#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962 col = ru_col - (Columns - W_WIDTH(wp));
6963 if (col < (W_WIDTH(wp) + 1) / 2)
6964 col = (W_WIDTH(wp) + 1) / 2;
6965#else
6966 col = ru_col;
6967 if (col > (Columns + 1) / 2)
6968 col = (Columns + 1) / 2;
6969#endif
6970 maxwidth = W_WIDTH(wp) - col;
6971#ifdef FEAT_WINDOWS
6972 if (!wp->w_status_height)
6973#endif
6974 {
6975 row = Rows - 1;
6976 --maxwidth; /* writing in last column may cause scrolling */
6977 fillchar = ' ';
6978 attr = 0;
6979 }
6980
6981# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006982 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006983# endif
6984 }
6985 else
6986 {
6987 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006989 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006991# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006992 use_sandbox = was_set_insecurely((char_u *)"statusline",
6993 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006994# endif
6995 }
6996
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006997#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006998 col += W_WINCOL(wp);
6999#endif
7000 }
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007003 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaar61452852011-02-01 18:01:11 +01007005 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7006 * the cursor away and back. */
7007 ewp = wp == NULL ? curwin : wp;
7008 p_crb_save = ewp->w_p_crb;
7009 ewp->w_p_crb = FALSE;
7010
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 /* Make a copy, because the statusline may include a function call that
7012 * might change the option value and free the memory. */
7013 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007014 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007015 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007016 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007017 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007018 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007020 /* Make all characters printable. */
7021 p = transstr(buf);
7022 if (p != NULL)
7023 {
7024 vim_strncpy(buf, p, sizeof(buf) - 1);
7025 vim_free(p);
7026 }
7027
7028 /* fill up with "fillchar" */
7029 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007030 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032#ifdef FEAT_MBYTE
7033 len += (*mb_char2bytes)(fillchar, buf + len);
7034#else
7035 buf[len++] = fillchar;
7036#endif
7037 ++width;
7038 }
7039 buf[len] = NUL;
7040
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007041 /*
7042 * Draw each snippet with the specified highlighting.
7043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 curattr = attr;
7045 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007046 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007048 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 screen_puts_len(p, len, row, col, curattr);
7050 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007051 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007053 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007055 else if (hltab[n].userhl < 0)
7056 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007058 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007059 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#endif
7061 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007062 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 }
7064 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007065
7066 if (wp == NULL)
7067 {
7068 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7069 col = 0;
7070 len = 0;
7071 p = buf;
7072 fillchar = 0;
7073 for (n = 0; tabtab[n].start != NULL; n++)
7074 {
7075 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7076 while (col < len)
7077 TabPageIdxs[col++] = fillchar;
7078 p = tabtab[n].start;
7079 fillchar = tabtab[n].userhl;
7080 }
7081 while (col < Columns)
7082 TabPageIdxs[col++] = fillchar;
7083 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007084
7085theend:
7086 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
7089#endif /* FEAT_STL_OPT */
7090
7091/*
7092 * Output a single character directly to the screen and update ScreenLines.
7093 */
7094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007095screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 char_u buf[MB_MAXBYTES + 1];
7098
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007099#ifdef FEAT_MBYTE
7100 if (has_mbyte)
7101 buf[(*mb_char2bytes)(c, buf)] = NUL;
7102 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007104 {
7105 buf[0] = c;
7106 buf[1] = NUL;
7107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 screen_puts(buf, row, col, attr);
7109}
7110
7111/*
7112 * Get a single character directly from ScreenLines into "bytes[]".
7113 * Also return its attribute in *attrp;
7114 */
7115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007116screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 unsigned off;
7119
7120 /* safety check */
7121 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7122 {
7123 off = LineOffset[row] + col;
7124 *attrp = ScreenAttrs[off];
7125 bytes[0] = ScreenLines[off];
7126 bytes[1] = NUL;
7127
7128#ifdef FEAT_MBYTE
7129 if (enc_utf8 && ScreenLinesUC[off] != 0)
7130 bytes[utfc_char2bytes(off, bytes)] = NUL;
7131 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7132 {
7133 bytes[0] = ScreenLines[off];
7134 bytes[1] = ScreenLines2[off];
7135 bytes[2] = NUL;
7136 }
7137 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7138 {
7139 bytes[1] = ScreenLines[off + 1];
7140 bytes[2] = NUL;
7141 }
7142#endif
7143 }
7144}
7145
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007146#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007147static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007148
7149/*
7150 * Return TRUE if composing characters for screen posn "off" differs from
7151 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007152 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007153 */
7154 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007155screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007156{
7157 int i;
7158
7159 for (i = 0; i < Screen_mco; ++i)
7160 {
7161 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7162 return TRUE;
7163 if (u8cc[i] == 0)
7164 break;
7165 }
7166 return FALSE;
7167}
7168#endif
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170/*
7171 * Put string '*text' on the screen at position 'row' and 'col', with
7172 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7173 * Note: only outputs within one row, message is truncated at screen boundary!
7174 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7175 */
7176 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007177screen_puts(
7178 char_u *text,
7179 int row,
7180 int col,
7181 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182{
7183 screen_puts_len(text, -1, row, col, attr);
7184}
7185
7186/*
7187 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7188 * a NUL.
7189 */
7190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007191screen_puts_len(
7192 char_u *text,
7193 int textlen,
7194 int row,
7195 int col,
7196 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 unsigned off;
7199 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007200 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int c;
7202#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007203 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 int mbyte_blen = 1;
7205 int mbyte_cells = 1;
7206 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007207 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 int clear_next_cell = FALSE;
7209# ifdef FEAT_ARABIC
7210 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007211 int pc, nc, nc1;
7212 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213# endif
7214#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007215#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7216 int force_redraw_this;
7217 int force_redraw_next = FALSE;
7218#endif
7219 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7222 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007223 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaarc236c162008-07-13 17:41:49 +00007225#ifdef FEAT_MBYTE
7226 /* When drawing over the right halve of a double-wide char clear out the
7227 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007228 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007229# ifdef FEAT_GUI
7230 && !gui.in_use
7231# endif
7232 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007233 {
7234 ScreenLines[off - 1] = ' ';
7235 ScreenAttrs[off - 1] = 0;
7236 if (enc_utf8)
7237 {
7238 ScreenLinesUC[off - 1] = 0;
7239 ScreenLinesC[0][off - 1] = 0;
7240 }
7241 /* redraw the previous cell, make it empty */
7242 screen_char(off - 1, row, col - 1);
7243 /* force the cell at "col" to be redrawn */
7244 force_redraw_next = TRUE;
7245 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007246#endif
7247
Bram Moolenaar367329b2007-08-30 11:53:22 +00007248#ifdef FEAT_MBYTE
7249 max_off = LineOffset[row] + screen_Columns;
7250#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007251 while (col < screen_Columns
7252 && (len < 0 || (int)(ptr - text) < len)
7253 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
7255 c = *ptr;
7256#ifdef FEAT_MBYTE
7257 /* check if this is the first byte of a multibyte */
7258 if (has_mbyte)
7259 {
7260 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007263 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7265 mbyte_cells = 1;
7266 else if (enc_dbcs != 0)
7267 mbyte_cells = mbyte_blen;
7268 else /* enc_utf8 */
7269 {
7270 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007271 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 (int)((text + len) - ptr));
7273 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007274 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007276# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 /* Non-BMP character: display as ? or fullwidth ?. */
7278 if (u8c >= 0x10000)
7279 {
7280 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7281 if (attr == 0)
7282 attr = hl_attr(HLF_8);
7283 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285# ifdef FEAT_ARABIC
7286 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7287 {
7288 /* Do Arabic shaping. */
7289 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7290 {
7291 /* Past end of string to be displayed. */
7292 nc = NUL;
7293 nc1 = NUL;
7294 }
7295 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007296 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007297 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7298 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007299 nc1 = pcc[0];
7300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 pc = prev_c;
7302 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007303 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305 else
7306 prev_c = u8c;
7307# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007308 if (col + mbyte_cells > screen_Columns)
7309 {
7310 /* Only 1 cell left, but character requires 2 cells:
7311 * display a '>' in the last column to avoid wrapping. */
7312 c = '>';
7313 mbyte_cells = 1;
7314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
7316 }
7317#endif
7318
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007319#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7320 force_redraw_this = force_redraw_next;
7321 force_redraw_next = FALSE;
7322#endif
7323
7324 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325#ifdef FEAT_MBYTE
7326 || (mbyte_cells == 2
7327 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7328 || (enc_dbcs == DBCS_JPNU
7329 && c == 0x8e
7330 && ScreenLines2[off] != ptr[1])
7331 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007332 && (ScreenLinesUC[off] !=
7333 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7334 || (ScreenLinesUC[off] != 0
7335 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#endif
7337 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007338 || exmode_active;
7339
7340 if (need_redraw
7341#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7342 || force_redraw_this
7343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 )
7345 {
7346#if defined(FEAT_GUI) || defined(UNIX)
7347 /* The bold trick makes a single row of pixels appear in the next
7348 * character. When a bold character is removed, the next
7349 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007350 * and for some xterms. */
7351 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# ifdef FEAT_GUI
7353 gui.in_use
7354# endif
7355# if defined(FEAT_GUI) && defined(UNIX)
7356 ||
7357# endif
7358# ifdef UNIX
7359 term_is_xterm
7360# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007361 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007363 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007365 if (n > HL_ALL)
7366 n = syn_attr2attr(n);
7367 if (n & HL_BOLD)
7368 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370#endif
7371#ifdef FEAT_MBYTE
7372 /* When at the end of the text and overwriting a two-cell
7373 * character with a one-cell character, need to clear the next
7374 * cell. Also when overwriting the left halve of a two-cell char
7375 * with the right halve of a two-cell char. Do this only once
7376 * (mb_off2cells() may return 2 on the right halve). */
7377 if (clear_next_cell)
7378 clear_next_cell = FALSE;
7379 else if (has_mbyte
7380 && (len < 0 ? ptr[mbyte_blen] == NUL
7381 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007382 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007384 && (*mb_off2cells)(off, max_off) == 1
7385 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 clear_next_cell = TRUE;
7387
7388 /* Make sure we never leave a second byte of a double-byte behind,
7389 * it confuses mb_off2cells(). */
7390 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007391 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007393 && (*mb_off2cells)(off, max_off) == 1
7394 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 ScreenLines[off + mbyte_blen] = 0;
7396#endif
7397 ScreenLines[off] = c;
7398 ScreenAttrs[off] = attr;
7399#ifdef FEAT_MBYTE
7400 if (enc_utf8)
7401 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 ScreenLinesUC[off] = 0;
7404 else
7405 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007406 int i;
7407
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 for (i = 0; i < Screen_mco; ++i)
7410 {
7411 ScreenLinesC[i][off] = u8cc[i];
7412 if (u8cc[i] == 0)
7413 break;
7414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 }
7416 if (mbyte_cells == 2)
7417 {
7418 ScreenLines[off + 1] = 0;
7419 ScreenAttrs[off + 1] = attr;
7420 }
7421 screen_char(off, row, col);
7422 }
7423 else if (mbyte_cells == 2)
7424 {
7425 ScreenLines[off + 1] = ptr[1];
7426 ScreenAttrs[off + 1] = attr;
7427 screen_char_2(off, row, col);
7428 }
7429 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7430 {
7431 ScreenLines2[off] = ptr[1];
7432 screen_char(off, row, col);
7433 }
7434 else
7435#endif
7436 screen_char(off, row, col);
7437 }
7438#ifdef FEAT_MBYTE
7439 if (has_mbyte)
7440 {
7441 off += mbyte_cells;
7442 col += mbyte_cells;
7443 ptr += mbyte_blen;
7444 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007445 {
7446 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007448 len = -1;
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 else
7452#endif
7453 {
7454 ++off;
7455 ++col;
7456 ++ptr;
7457 }
7458 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007459
7460#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7461 /* If we detected the next character needs to be redrawn, but the text
7462 * doesn't extend up to there, update the character here. */
7463 if (force_redraw_next && col < screen_Columns)
7464 {
7465# ifdef FEAT_MBYTE
7466 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7467 screen_char_2(off, row, col);
7468 else
7469# endif
7470 screen_char(off, row, col);
7471 }
7472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473}
7474
7475#ifdef FEAT_SEARCH_EXTRA
7476/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007477 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 */
7479 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007480start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 if (p_hls && !no_hlsearch)
7483 {
7484 last_pat_prog(&search_hl.rm);
7485 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007486# ifdef FEAT_RELTIME
7487 /* Set the time limit to 'redrawtime'. */
7488 profile_setlimit(p_rdt, &search_hl.tm);
7489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491}
7492
7493/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007494 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 */
7496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007497end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 if (search_hl.rm.regprog != NULL)
7500 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007501 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 search_hl.rm.regprog = NULL;
7503 }
7504}
7505
7506/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007507 * Init for calling prepare_search_hl().
7508 */
7509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007511{
7512 matchitem_T *cur;
7513
7514 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7515 * match */
7516 cur = wp->w_match_head;
7517 while (cur != NULL)
7518 {
7519 cur->hl.rm = cur->match;
7520 if (cur->hlg_id == 0)
7521 cur->hl.attr = 0;
7522 else
7523 cur->hl.attr = syn_id2attr(cur->hlg_id);
7524 cur->hl.buf = wp->w_buffer;
7525 cur->hl.lnum = 0;
7526 cur->hl.first_lnum = 0;
7527# ifdef FEAT_RELTIME
7528 /* Set the time limit to 'redrawtime'. */
7529 profile_setlimit(p_rdt, &(cur->hl.tm));
7530# endif
7531 cur = cur->next;
7532 }
7533 search_hl.buf = wp->w_buffer;
7534 search_hl.lnum = 0;
7535 search_hl.first_lnum = 0;
7536 /* time limit is set at the toplevel, for all windows */
7537}
7538
7539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 * Advance to the match in window "wp" line "lnum" or past it.
7541 */
7542 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007543prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007545 matchitem_T *cur; /* points to the match list */
7546 match_T *shl; /* points to search_hl or a match */
7547 int shl_flag; /* flag to indicate whether search_hl
7548 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007549 int pos_inprogress; /* marks that position match search is
7550 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 int n;
7552
7553 /*
7554 * When using a multi-line pattern, start searching at the top
7555 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007556 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007558 cur = wp->w_match_head;
7559 shl_flag = FALSE;
7560 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007562 if (shl_flag == FALSE)
7563 {
7564 shl = &search_hl;
7565 shl_flag = TRUE;
7566 }
7567 else
7568 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (shl->rm.regprog != NULL
7570 && shl->lnum == 0
7571 && re_multiline(shl->rm.regprog))
7572 {
7573 if (shl->first_lnum == 0)
7574 {
7575# ifdef FEAT_FOLDING
7576 for (shl->first_lnum = lnum;
7577 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7578 if (hasFoldingWin(wp, shl->first_lnum - 1,
7579 NULL, NULL, TRUE, NULL))
7580 break;
7581# else
7582 shl->first_lnum = wp->w_topline;
7583# endif
7584 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007585 if (cur != NULL)
7586 cur->pos.cur = 0;
7587 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007589 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7590 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007592 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7593 pos_inprogress = cur == NULL || cur->pos.cur == 0
7594 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (shl->lnum != 0)
7596 {
7597 shl->first_lnum = shl->lnum
7598 + shl->rm.endpos[0].lnum
7599 - shl->rm.startpos[0].lnum;
7600 n = shl->rm.endpos[0].col;
7601 }
7602 else
7603 {
7604 ++shl->first_lnum;
7605 n = 0;
7606 }
7607 }
7608 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007609 if (shl != &search_hl && cur != NULL)
7610 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612}
7613
7614/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007615 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 * Uses shl->buf.
7617 * Sets shl->lnum and shl->rm contents.
7618 * Note: Assumes a previous match is always before "lnum", unless
7619 * shl->lnum is zero.
7620 * Careful: Any pointers for buffer lines will become invalid.
7621 */
7622 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007623next_search_hl(
7624 win_T *win,
7625 match_T *shl, /* points to search_hl or a match */
7626 linenr_T lnum,
7627 colnr_T mincol, /* minimal column for a match */
7628 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 linenr_T l;
7631 colnr_T matchcol;
7632 long nmatched;
7633
7634 if (shl->lnum != 0)
7635 {
7636 /* Check for three situations:
7637 * 1. If the "lnum" is below a previous match, start a new search.
7638 * 2. If the previous match includes "mincol", use it.
7639 * 3. Continue after the previous match.
7640 */
7641 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7642 if (lnum > l)
7643 shl->lnum = 0;
7644 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7645 return;
7646 }
7647
7648 /*
7649 * Repeat searching for a match until one is found that includes "mincol"
7650 * or none is found in this line.
7651 */
7652 called_emsg = FALSE;
7653 for (;;)
7654 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007655#ifdef FEAT_RELTIME
7656 /* Stop searching after passing the time limit. */
7657 if (profile_passed_limit(&(shl->tm)))
7658 {
7659 shl->lnum = 0; /* no match found in time */
7660 break;
7661 }
7662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 /* Three situations:
7664 * 1. No useful previous match: search from start of line.
7665 * 2. Not Vi compatible or empty match: continue at next character.
7666 * Break the loop if this is beyond the end of the line.
7667 * 3. Vi compatible searching: continue at end of previous match.
7668 */
7669 if (shl->lnum == 0)
7670 matchcol = 0;
7671 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7672 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007673 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007675 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007676
7677 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007678 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007679 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007681 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 shl->lnum = 0;
7683 break;
7684 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007685#ifdef FEAT_MBYTE
7686 if (has_mbyte)
7687 matchcol += mb_ptr2len(ml);
7688 else
7689#endif
7690 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
7692 else
7693 matchcol = shl->rm.endpos[0].col;
7694
7695 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007696 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007698 /* Remember whether shl->rm is using a copy of the regprog in
7699 * cur->match. */
7700 int regprog_is_copy = (shl != &search_hl && cur != NULL
7701 && shl == &cur->hl
7702 && cur->match.regprog == cur->hl.rm.regprog);
7703
Bram Moolenaarb3414592014-06-17 17:48:32 +02007704 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7705 matchcol,
7706#ifdef FEAT_RELTIME
7707 &(shl->tm)
7708#else
7709 NULL
7710#endif
7711 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007712 /* Copy the regprog, in case it got freed and recompiled. */
7713 if (regprog_is_copy)
7714 cur->match.regprog = cur->hl.rm.regprog;
7715
Bram Moolenaarb3414592014-06-17 17:48:32 +02007716 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007717 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007718 /* Error while handling regexp: stop using this regexp. */
7719 if (shl == &search_hl)
7720 {
7721 /* don't free regprog in the match list, it's a copy */
7722 vim_regfree(shl->rm.regprog);
7723 SET_NO_HLSEARCH(TRUE);
7724 }
7725 shl->rm.regprog = NULL;
7726 shl->lnum = 0;
7727 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7728 message */
7729 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007730 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007731 }
7732 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007733 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007734 else
7735 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (nmatched == 0)
7737 {
7738 shl->lnum = 0; /* no match found */
7739 break;
7740 }
7741 if (shl->rm.startpos[0].lnum > 0
7742 || shl->rm.startpos[0].col >= mincol
7743 || nmatched > 1
7744 || shl->rm.endpos[0].col > mincol)
7745 {
7746 shl->lnum += shl->rm.startpos[0].lnum;
7747 break; /* useful match found */
7748 }
7749 }
7750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
Bram Moolenaarb3414592014-06-17 17:48:32 +02007752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007753next_search_hl_pos(
7754 match_T *shl, /* points to a match */
7755 linenr_T lnum,
7756 posmatch_T *posmatch, /* match positions */
7757 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007758{
7759 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007760 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007761
7762 shl->lnum = 0;
7763 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7764 {
7765 if (posmatch->pos[i].lnum == 0)
7766 break;
7767 if (posmatch->pos[i].col < mincol)
7768 continue;
7769 if (posmatch->pos[i].lnum == lnum)
7770 {
7771 if (shl->lnum == lnum)
7772 {
7773 /* partially sort positions by column numbers
7774 * on the same line */
7775 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7776 {
7777 llpos_T tmp = posmatch->pos[i];
7778
7779 posmatch->pos[i] = posmatch->pos[bot];
7780 posmatch->pos[bot] = tmp;
7781 }
7782 }
7783 else
7784 {
7785 bot = i;
7786 shl->lnum = lnum;
7787 }
7788 }
7789 }
7790 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007791 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007792 {
7793 colnr_T start = posmatch->pos[bot].col == 0
7794 ? 0 : posmatch->pos[bot].col - 1;
7795 colnr_T end = posmatch->pos[bot].col == 0
7796 ? MAXCOL : start + posmatch->pos[bot].len;
7797
7798 shl->rm.startpos[0].lnum = 0;
7799 shl->rm.startpos[0].col = start;
7800 shl->rm.endpos[0].lnum = 0;
7801 shl->rm.endpos[0].col = end;
7802 posmatch->cur = bot + 1;
7803 return TRUE;
7804 }
7805 return FALSE;
7806}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007807#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007808
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811{
7812 attrentry_T *aep = NULL;
7813
7814 screen_attr = attr;
7815 if (full_screen
7816#ifdef WIN3264
7817 && termcap_active
7818#endif
7819 )
7820 {
7821#ifdef FEAT_GUI
7822 if (gui.in_use)
7823 {
7824 char buf[20];
7825
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007826 /* The GUI handles this internally. */
7827 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 OUT_STR(buf);
7829 }
7830 else
7831#endif
7832 {
7833 if (attr > HL_ALL) /* special HL attr. */
7834 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007835 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 aep = syn_cterm_attr2entry(attr);
7837 else
7838 aep = syn_term_attr2entry(attr);
7839 if (aep == NULL) /* did ":syntax clear" */
7840 attr = 0;
7841 else
7842 attr = aep->ae_attr;
7843 }
7844 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7845 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007846 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007847#ifdef FEAT_TERMGUICOLORS
7848 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007849 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007850#endif
7851 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007852#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007853 )
7854#endif
7855 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007856 /* If the Normal FG color has BOLD attribute and the new HL
7857 * has a FG color defined, clear BOLD. */
7858 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7860 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007861 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7862 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 out_str(T_US);
7864 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7865 out_str(T_CZH);
7866 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7867 out_str(T_MR);
7868
7869 /*
7870 * Output the color or start string after bold etc., in case the
7871 * bold etc. override the color setting.
7872 */
7873 if (aep != NULL)
7874 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007875#ifdef FEAT_TERMGUICOLORS
7876 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007878 if (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007879 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007880 if (aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007881 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007886 if (t_colors > 1)
7887 {
7888 if (aep->ae_u.cterm.fg_color)
7889 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7890 if (aep->ae_u.cterm.bg_color)
7891 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7892 }
7893 else
7894 {
7895 if (aep->ae_u.term.start != NULL)
7896 out_str(aep->ae_u.term.start);
7897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899 }
7900 }
7901 }
7902}
7903
7904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007905screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 int do_ME = FALSE; /* output T_ME code */
7908
7909 if (screen_attr != 0
7910#ifdef WIN3264
7911 && termcap_active
7912#endif
7913 )
7914 {
7915#ifdef FEAT_GUI
7916 if (gui.in_use)
7917 {
7918 char buf[20];
7919
7920 /* use internal GUI code */
7921 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7922 OUT_STR(buf);
7923 }
7924 else
7925#endif
7926 {
7927 if (screen_attr > HL_ALL) /* special HL attr. */
7928 {
7929 attrentry_T *aep;
7930
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007931 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {
7933 /*
7934 * Assume that t_me restores the original colors!
7935 */
7936 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007937 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007938#ifdef FEAT_TERMGUICOLORS
7939 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007940 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR ||
7941 aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007942#endif
7943 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007944#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007945 )
7946#endif
7947 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 do_ME = TRUE;
7949 }
7950 else
7951 {
7952 aep = syn_term_attr2entry(screen_attr);
7953 if (aep != NULL && aep->ae_u.term.stop != NULL)
7954 {
7955 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7956 do_ME = TRUE;
7957 else
7958 out_str(aep->ae_u.term.stop);
7959 }
7960 }
7961 if (aep == NULL) /* did ":syntax clear" */
7962 screen_attr = 0;
7963 else
7964 screen_attr = aep->ae_attr;
7965 }
7966
7967 /*
7968 * Often all ending-codes are equal to T_ME. Avoid outputting the
7969 * same sequence several times.
7970 */
7971 if (screen_attr & HL_STANDOUT)
7972 {
7973 if (STRCMP(T_SE, T_ME) == 0)
7974 do_ME = TRUE;
7975 else
7976 out_str(T_SE);
7977 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007978 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
7980 if (STRCMP(T_UE, T_ME) == 0)
7981 do_ME = TRUE;
7982 else
7983 out_str(T_UE);
7984 }
7985 if (screen_attr & HL_ITALIC)
7986 {
7987 if (STRCMP(T_CZR, T_ME) == 0)
7988 do_ME = TRUE;
7989 else
7990 out_str(T_CZR);
7991 }
7992 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7993 out_str(T_ME);
7994
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007995#ifdef FEAT_TERMGUICOLORS
7996 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007998 if (cterm_normal_fg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007999 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar902647d2016-04-22 11:49:06 +02008000 if (cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008001 term_bg_rgb_color(cterm_normal_bg_gui_color);
8002 }
8003 else
8004#endif
8005 {
8006 if (t_colors > 1)
8007 {
8008 /* set Normal cterm colors */
8009 if (cterm_normal_fg_color != 0)
8010 term_fg_color(cterm_normal_fg_color - 1);
8011 if (cterm_normal_bg_color != 0)
8012 term_bg_color(cterm_normal_bg_color - 1);
8013 if (cterm_normal_fg_bold)
8014 out_str(T_MD);
8015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 }
8017 }
8018 }
8019 screen_attr = 0;
8020}
8021
8022/*
8023 * Reset the colors for a cterm. Used when leaving Vim.
8024 * The machine specific code may override this again.
8025 */
8026 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008027reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008029 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
8031 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008032#ifdef FEAT_TERMGUICOLORS
8033 if (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02008034 (cterm_normal_fg_gui_color != (long_u)INVALCOLOR
8035 || cterm_normal_bg_gui_color != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008036 (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
8037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {
8041 out_str(T_OP);
8042 screen_attr = -1;
8043 }
8044 if (cterm_normal_fg_bold)
8045 {
8046 out_str(T_ME);
8047 screen_attr = -1;
8048 }
8049 }
8050}
8051
8052/*
8053 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8054 * using the attributes from ScreenAttrs["off"].
8055 */
8056 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008057screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
8059 int attr;
8060
8061 /* Check for illegal values, just in case (could happen just after
8062 * resizing). */
8063 if (row >= screen_Rows || col >= screen_Columns)
8064 return;
8065
Bram Moolenaar494838a2015-02-10 19:20:37 +01008066 /* Outputting a character in the last cell on the screen may scroll the
8067 * screen up. Only do it when the "xn" termcap property is set, otherwise
8068 * mark the character invalid (update it when scrolled up). */
8069 if (*T_XN == NUL
8070 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071#ifdef FEAT_RIGHTLEFT
8072 /* account for first command-line character in rightleft mode */
8073 && !cmdmsg_rl
8074#endif
8075 )
8076 {
8077 ScreenAttrs[off] = (sattr_T)-1;
8078 return;
8079 }
8080
8081 /*
8082 * Stop highlighting first, so it's easier to move the cursor.
8083 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008084#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 if (screen_char_attr != 0)
8086 attr = screen_char_attr;
8087 else
8088#endif
8089 attr = ScreenAttrs[off];
8090 if (screen_attr != attr)
8091 screen_stop_highlight();
8092
8093 windgoto(row, col);
8094
8095 if (screen_attr != attr)
8096 screen_start_highlight(attr);
8097
8098#ifdef FEAT_MBYTE
8099 if (enc_utf8 && ScreenLinesUC[off] != 0)
8100 {
8101 char_u buf[MB_MAXBYTES + 1];
8102
8103 /* Convert UTF-8 character to bytes and write it. */
8104
8105 buf[utfc_char2bytes(off, buf)] = NUL;
8106
8107 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008108 if (utf_ambiguous_width(ScreenLinesUC[off]))
8109 screen_cur_col = 9999;
8110 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 ++screen_cur_col;
8112 }
8113 else
8114#endif
8115 {
8116#ifdef FEAT_MBYTE
8117 out_flush_check();
8118#endif
8119 out_char(ScreenLines[off]);
8120#ifdef FEAT_MBYTE
8121 /* double-byte character in single-width cell */
8122 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8123 out_char(ScreenLines2[off]);
8124#endif
8125 }
8126
8127 screen_cur_col++;
8128}
8129
8130#ifdef FEAT_MBYTE
8131
8132/*
8133 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8134 * on the screen at position 'row' and 'col'.
8135 * The attributes of the first byte is used for all. This is required to
8136 * output the two bytes of a double-byte character with nothing in between.
8137 */
8138 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008139screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
8141 /* Check for illegal values (could be wrong when screen was resized). */
8142 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8143 return;
8144
8145 /* Outputting the last character on the screen may scrollup the screen.
8146 * Don't to it! Mark the character invalid (update it when scrolled up) */
8147 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8148 {
8149 ScreenAttrs[off] = (sattr_T)-1;
8150 return;
8151 }
8152
8153 /* Output the first byte normally (positions the cursor), then write the
8154 * second byte directly. */
8155 screen_char(off, row, col);
8156 out_char(ScreenLines[off + 1]);
8157 ++screen_cur_col;
8158}
8159#endif
8160
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008161#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162/*
8163 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8164 * This uses the contents of ScreenLines[] and doesn't change it.
8165 */
8166 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008167screen_draw_rectangle(
8168 int row,
8169 int col,
8170 int height,
8171 int width,
8172 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 int r, c;
8175 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008176#ifdef FEAT_MBYTE
8177 int max_off;
8178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008180 /* Can't use ScreenLines unless initialized */
8181 if (ScreenLines == NULL)
8182 return;
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (invert)
8185 screen_char_attr = HL_INVERSE;
8186 for (r = row; r < row + height; ++r)
8187 {
8188 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008189#ifdef FEAT_MBYTE
8190 max_off = off + screen_Columns;
8191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 for (c = col; c < col + width; ++c)
8193 {
8194#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008195 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {
8197 screen_char_2(off + c, r, c);
8198 ++c;
8199 }
8200 else
8201#endif
8202 {
8203 screen_char(off + c, r, c);
8204#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008205 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 ++c;
8207#endif
8208 }
8209 }
8210 }
8211 screen_char_attr = 0;
8212}
8213#endif
8214
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008215#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216/*
8217 * Redraw the characters for a vertically split window.
8218 */
8219 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008220redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 int col;
8223 int width;
8224
8225# ifdef FEAT_CLIPBOARD
8226 clip_may_clear_selection(row, end - 1);
8227# endif
8228
8229 if (wp == NULL)
8230 {
8231 col = 0;
8232 width = Columns;
8233 }
8234 else
8235 {
8236 col = wp->w_wincol;
8237 width = wp->w_width;
8238 }
8239 screen_draw_rectangle(row, col, end - row, width, FALSE);
8240}
8241#endif
8242
8243/*
8244 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8245 * with character 'c1' in first column followed by 'c2' in the other columns.
8246 * Use attributes 'attr'.
8247 */
8248 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008249screen_fill(
8250 int start_row,
8251 int end_row,
8252 int start_col,
8253 int end_col,
8254 int c1,
8255 int c2,
8256 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 int row;
8259 int col;
8260 int off;
8261 int end_off;
8262 int did_delete;
8263 int c;
8264 int norm_term;
8265#if defined(FEAT_GUI) || defined(UNIX)
8266 int force_next = FALSE;
8267#endif
8268
8269 if (end_row > screen_Rows) /* safety check */
8270 end_row = screen_Rows;
8271 if (end_col > screen_Columns) /* safety check */
8272 end_col = screen_Columns;
8273 if (ScreenLines == NULL
8274 || start_row >= end_row
8275 || start_col >= end_col) /* nothing to do */
8276 return;
8277
8278 /* it's a "normal" terminal when not in a GUI or cterm */
8279 norm_term = (
8280#ifdef FEAT_GUI
8281 !gui.in_use &&
8282#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008283 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 for (row = start_row; row < end_row; ++row)
8285 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008286#ifdef FEAT_MBYTE
8287 if (has_mbyte
8288# ifdef FEAT_GUI
8289 && !gui.in_use
8290# endif
8291 )
8292 {
8293 /* When drawing over the right halve of a double-wide char clear
8294 * out the left halve. When drawing over the left halve of a
8295 * double wide-char clear out the right halve. Only needed in a
8296 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008297 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008298 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008299 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008300 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008301 }
8302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 /*
8304 * Try to use delete-line termcap code, when no attributes or in a
8305 * "normal" terminal, where a bold/italic space is just a
8306 * space.
8307 */
8308 did_delete = FALSE;
8309 if (c2 == ' '
8310 && end_col == Columns
8311 && can_clear(T_CE)
8312 && (attr == 0
8313 || (norm_term
8314 && attr <= HL_ALL
8315 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8316 {
8317 /*
8318 * check if we really need to clear something
8319 */
8320 col = start_col;
8321 if (c1 != ' ') /* don't clear first char */
8322 ++col;
8323
8324 off = LineOffset[row] + col;
8325 end_off = LineOffset[row] + end_col;
8326
8327 /* skip blanks (used often, keep it fast!) */
8328#ifdef FEAT_MBYTE
8329 if (enc_utf8)
8330 while (off < end_off && ScreenLines[off] == ' '
8331 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8332 ++off;
8333 else
8334#endif
8335 while (off < end_off && ScreenLines[off] == ' '
8336 && ScreenAttrs[off] == 0)
8337 ++off;
8338 if (off < end_off) /* something to be cleared */
8339 {
8340 col = off - LineOffset[row];
8341 screen_stop_highlight();
8342 term_windgoto(row, col);/* clear rest of this screen line */
8343 out_str(T_CE);
8344 screen_start(); /* don't know where cursor is now */
8345 col = end_col - col;
8346 while (col--) /* clear chars in ScreenLines */
8347 {
8348 ScreenLines[off] = ' ';
8349#ifdef FEAT_MBYTE
8350 if (enc_utf8)
8351 ScreenLinesUC[off] = 0;
8352#endif
8353 ScreenAttrs[off] = 0;
8354 ++off;
8355 }
8356 }
8357 did_delete = TRUE; /* the chars are cleared now */
8358 }
8359
8360 off = LineOffset[row] + start_col;
8361 c = c1;
8362 for (col = start_col; col < end_col; ++col)
8363 {
8364 if (ScreenLines[off] != c
8365#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008366 || (enc_utf8 && (int)ScreenLinesUC[off]
8367 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368#endif
8369 || ScreenAttrs[off] != attr
8370#if defined(FEAT_GUI) || defined(UNIX)
8371 || force_next
8372#endif
8373 )
8374 {
8375#if defined(FEAT_GUI) || defined(UNIX)
8376 /* The bold trick may make a single row of pixels appear in
8377 * the next character. When a bold character is removed, the
8378 * next character should be redrawn too. This happens for our
8379 * own GUI and for some xterms. */
8380 if (
8381# ifdef FEAT_GUI
8382 gui.in_use
8383# endif
8384# if defined(FEAT_GUI) && defined(UNIX)
8385 ||
8386# endif
8387# ifdef UNIX
8388 term_is_xterm
8389# endif
8390 )
8391 {
8392 if (ScreenLines[off] != ' '
8393 && (ScreenAttrs[off] > HL_ALL
8394 || ScreenAttrs[off] & HL_BOLD))
8395 force_next = TRUE;
8396 else
8397 force_next = FALSE;
8398 }
8399#endif
8400 ScreenLines[off] = c;
8401#ifdef FEAT_MBYTE
8402 if (enc_utf8)
8403 {
8404 if (c >= 0x80)
8405 {
8406 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008407 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 }
8409 else
8410 ScreenLinesUC[off] = 0;
8411 }
8412#endif
8413 ScreenAttrs[off] = attr;
8414 if (!did_delete || c != ' ')
8415 screen_char(off, row, col);
8416 }
8417 ++off;
8418 if (col == start_col)
8419 {
8420 if (did_delete)
8421 break;
8422 c = c2;
8423 }
8424 }
8425 if (end_col == Columns)
8426 LineWraps[row] = FALSE;
8427 if (row == Rows - 1) /* overwritten the command line */
8428 {
8429 redraw_cmdline = TRUE;
8430 if (c1 == ' ' && c2 == ' ')
8431 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008432 if (start_col == 0)
8433 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 }
8436}
8437
8438/*
8439 * Check if there should be a delay. Used before clearing or redrawing the
8440 * screen or the command line.
8441 */
8442 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008443check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8446 && !did_wait_return
8447 && emsg_silent == 0)
8448 {
8449 out_flush();
8450 ui_delay(1000L, TRUE);
8451 emsg_on_display = FALSE;
8452 if (check_msg_scroll)
8453 msg_scroll = FALSE;
8454 }
8455}
8456
8457/*
8458 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008459 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 * Returns TRUE if there is a valid screen to write to.
8461 * Returns FALSE when starting up and screen not initialized yet.
8462 */
8463 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008464screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008466 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 return (ScreenLines != NULL);
8468}
8469
8470/*
8471 * Resize the shell to Rows and Columns.
8472 * Allocate ScreenLines[] and associated items.
8473 *
8474 * There may be some time between setting Rows and Columns and (re)allocating
8475 * ScreenLines[]. This happens when starting up and when (manually) changing
8476 * the shell size. Always use screen_Rows and screen_Columns to access items
8477 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8478 * final size of the shell is needed.
8479 */
8480 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008481screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 int new_row, old_row;
8484#ifdef FEAT_GUI
8485 int old_Rows;
8486#endif
8487 win_T *wp;
8488 int outofmem = FALSE;
8489 int len;
8490 schar_T *new_ScreenLines;
8491#ifdef FEAT_MBYTE
8492 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008493 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008495 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496#endif
8497 sattr_T *new_ScreenAttrs;
8498 unsigned *new_LineOffset;
8499 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008500#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008501 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008502 tabpage_T *tp;
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008505 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008506#ifdef FEAT_AUTOCMD
8507 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008509retry:
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 /*
8512 * Allocation of the screen buffers is done only when the size changes and
8513 * when Rows and Columns have been set and we have started doing full
8514 * screen stuff.
8515 */
8516 if ((ScreenLines != NULL
8517 && Rows == screen_Rows
8518 && Columns == screen_Columns
8519#ifdef FEAT_MBYTE
8520 && enc_utf8 == (ScreenLinesUC != NULL)
8521 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008522 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523#endif
8524 )
8525 || Rows == 0
8526 || Columns == 0
8527 || (!full_screen && ScreenLines == NULL))
8528 return;
8529
8530 /*
8531 * It's possible that we produce an out-of-memory message below, which
8532 * will cause this function to be called again. To break the loop, just
8533 * return here.
8534 */
8535 if (entered)
8536 return;
8537 entered = TRUE;
8538
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008539 /*
8540 * Note that the window sizes are updated before reallocating the arrays,
8541 * thus we must not redraw here!
8542 */
8543 ++RedrawingDisabled;
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 win_new_shellsize(); /* fit the windows in the new sized shell */
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 comp_col(); /* recompute columns for shown command and ruler */
8548
8549 /*
8550 * We're changing the size of the screen.
8551 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8552 * - Move lines from the old arrays into the new arrays, clear extra
8553 * lines (unless the screen is going to be cleared).
8554 * - Free the old arrays.
8555 *
8556 * If anything fails, make ScreenLines NULL, so we don't do anything!
8557 * Continuing with the old ScreenLines may result in a crash, because the
8558 * size is wrong.
8559 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008560 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008562#ifdef FEAT_AUTOCMD
8563 if (aucmd_win != NULL)
8564 win_free_lsize(aucmd_win);
8565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 new_ScreenLines = (schar_T *)lalloc((long_u)(
8568 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8569#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008570 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 if (enc_utf8)
8572 {
8573 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8574 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008575 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008576 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8578 }
8579 if (enc_dbcs == DBCS_JPNU)
8580 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8581 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8582#endif
8583 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8584 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8585 new_LineOffset = (unsigned *)lalloc((long_u)(
8586 Rows * sizeof(unsigned)), FALSE);
8587 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008588#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008589 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008592 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {
8594 if (win_alloc_lines(wp) == FAIL)
8595 {
8596 outofmem = TRUE;
8597#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008598 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599#endif
8600 }
8601 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008602#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008603 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8604 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008605 outofmem = TRUE;
8606#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008607#ifdef FEAT_WINDOWS
8608give_up:
8609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008611#ifdef FEAT_MBYTE
8612 for (i = 0; i < p_mco; ++i)
8613 if (new_ScreenLinesC[i] == NULL)
8614 break;
8615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 if (new_ScreenLines == NULL
8617#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008618 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8620#endif
8621 || new_ScreenAttrs == NULL
8622 || new_LineOffset == NULL
8623 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008624#ifdef FEAT_WINDOWS
8625 || new_TabPageIdxs == NULL
8626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 || outofmem)
8628 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008629 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008630 {
8631 /* guess the size */
8632 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8633
8634 /* Remember we did this to avoid getting outofmem messages over
8635 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008636 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 vim_free(new_ScreenLines);
8639 new_ScreenLines = NULL;
8640#ifdef FEAT_MBYTE
8641 vim_free(new_ScreenLinesUC);
8642 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008643 for (i = 0; i < p_mco; ++i)
8644 {
8645 vim_free(new_ScreenLinesC[i]);
8646 new_ScreenLinesC[i] = NULL;
8647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 vim_free(new_ScreenLines2);
8649 new_ScreenLines2 = NULL;
8650#endif
8651 vim_free(new_ScreenAttrs);
8652 new_ScreenAttrs = NULL;
8653 vim_free(new_LineOffset);
8654 new_LineOffset = NULL;
8655 vim_free(new_LineWraps);
8656 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008657#ifdef FEAT_WINDOWS
8658 vim_free(new_TabPageIdxs);
8659 new_TabPageIdxs = NULL;
8660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662 else
8663 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008664 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 for (new_row = 0; new_row < Rows; ++new_row)
8667 {
8668 new_LineOffset[new_row] = new_row * Columns;
8669 new_LineWraps[new_row] = FALSE;
8670
8671 /*
8672 * If the screen is not going to be cleared, copy as much as
8673 * possible from the old screen to the new one and clear the rest
8674 * (used when resizing the window at the "--more--" prompt or when
8675 * executing an external command, for the GUI).
8676 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008677 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
8679 (void)vim_memset(new_ScreenLines + new_row * Columns,
8680 ' ', (size_t)Columns * sizeof(schar_T));
8681#ifdef FEAT_MBYTE
8682 if (enc_utf8)
8683 {
8684 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8685 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008686 for (i = 0; i < p_mco; ++i)
8687 (void)vim_memset(new_ScreenLinesC[i]
8688 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 0, (size_t)Columns * sizeof(u8char_T));
8690 }
8691 if (enc_dbcs == DBCS_JPNU)
8692 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8693 0, (size_t)Columns * sizeof(schar_T));
8694#endif
8695 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8696 0, (size_t)Columns * sizeof(sattr_T));
8697 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008698 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {
8700 if (screen_Columns < Columns)
8701 len = screen_Columns;
8702 else
8703 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008704#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008705 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008706 * may be invalid now. Also when p_mco changes. */
8707 if (!(enc_utf8 && ScreenLinesUC == NULL)
8708 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008709#endif
8710 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8711 ScreenLines + LineOffset[old_row],
8712 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008714 if (enc_utf8 && ScreenLinesUC != NULL
8715 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {
8717 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8718 ScreenLinesUC + LineOffset[old_row],
8719 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008720 for (i = 0; i < p_mco; ++i)
8721 mch_memmove(new_ScreenLinesC[i]
8722 + new_LineOffset[new_row],
8723 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 (size_t)len * sizeof(u8char_T));
8725 }
8726 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8727 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8728 ScreenLines2 + LineOffset[old_row],
8729 (size_t)len * sizeof(schar_T));
8730#endif
8731 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8732 ScreenAttrs + LineOffset[old_row],
8733 (size_t)len * sizeof(sattr_T));
8734 }
8735 }
8736 }
8737 /* Use the last line of the screen for the current line. */
8738 current_ScreenLine = new_ScreenLines + Rows * Columns;
8739 }
8740
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008741 free_screenlines();
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 ScreenLines = new_ScreenLines;
8744#ifdef FEAT_MBYTE
8745 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008746 for (i = 0; i < p_mco; ++i)
8747 ScreenLinesC[i] = new_ScreenLinesC[i];
8748 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 ScreenLines2 = new_ScreenLines2;
8750#endif
8751 ScreenAttrs = new_ScreenAttrs;
8752 LineOffset = new_LineOffset;
8753 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008754#ifdef FEAT_WINDOWS
8755 TabPageIdxs = new_TabPageIdxs;
8756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
8758 /* It's important that screen_Rows and screen_Columns reflect the actual
8759 * size of ScreenLines[]. Set them before calling anything. */
8760#ifdef FEAT_GUI
8761 old_Rows = screen_Rows;
8762#endif
8763 screen_Rows = Rows;
8764 screen_Columns = Columns;
8765
8766 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008767 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 screenclear2();
8769
8770#ifdef FEAT_GUI
8771 else if (gui.in_use
8772 && !gui.starting
8773 && ScreenLines != NULL
8774 && old_Rows != Rows)
8775 {
8776 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8777 /*
8778 * Adjust the position of the cursor, for when executing an external
8779 * command.
8780 */
8781 if (msg_row >= Rows) /* Rows got smaller */
8782 msg_row = Rows - 1; /* put cursor at last row */
8783 else if (Rows > old_Rows) /* Rows got bigger */
8784 msg_row += Rows - old_Rows; /* put cursor in same place */
8785 if (msg_col >= Columns) /* Columns got smaller */
8786 msg_col = Columns - 1; /* put cursor at last column */
8787 }
8788#endif
8789
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008791 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008792
8793#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008794 /*
8795 * Do not apply autocommands more than 3 times to avoid an endless loop
8796 * in case applying autocommands always changes Rows or Columns.
8797 */
8798 if (starting == 0 && ++retry_count <= 3)
8799 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008800 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008801 /* In rare cases, autocommands may have altered Rows or Columns,
8802 * jump back to check if we need to allocate the screen again. */
8803 goto retry;
8804 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008809free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008810{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008811#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008812 int i;
8813
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008814 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 for (i = 0; i < Screen_mco; ++i)
8816 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008817 vim_free(ScreenLines2);
8818#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008819 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008820 vim_free(ScreenAttrs);
8821 vim_free(LineOffset);
8822 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008823#ifdef FEAT_WINDOWS
8824 vim_free(TabPageIdxs);
8825#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008826}
8827
8828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008829screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 check_for_delay(FALSE);
8832 screenalloc(FALSE); /* allocate screen buffers if size changed */
8833 screenclear2(); /* clear the screen */
8834}
8835
8836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008837screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 int i;
8840
8841 if (starting == NO_SCREEN || ScreenLines == NULL
8842#ifdef FEAT_GUI
8843 || (gui.in_use && gui.starting)
8844#endif
8845 )
8846 return;
8847
8848#ifdef FEAT_GUI
8849 if (!gui.in_use)
8850#endif
8851 screen_attr = -1; /* force setting the Normal colors */
8852 screen_stop_highlight(); /* don't want highlighting here */
8853
8854#ifdef FEAT_CLIPBOARD
8855 /* disable selection without redrawing it */
8856 clip_scroll_selection(9999);
8857#endif
8858
8859 /* blank out ScreenLines */
8860 for (i = 0; i < Rows; ++i)
8861 {
8862 lineclear(LineOffset[i], (int)Columns);
8863 LineWraps[i] = FALSE;
8864 }
8865
8866 if (can_clear(T_CL))
8867 {
8868 out_str(T_CL); /* clear the display */
8869 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008870 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 else
8873 {
8874 /* can't clear the screen, mark all chars with invalid attributes */
8875 for (i = 0; i < Rows; ++i)
8876 lineinvalid(LineOffset[i], (int)Columns);
8877 clear_cmdline = TRUE;
8878 }
8879
8880 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8881
8882 win_rest_invalid(firstwin);
8883 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008884#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008885 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 if (must_redraw == CLEAR) /* no need to clear again */
8888 must_redraw = NOT_VALID;
8889 compute_cmdrow();
8890 msg_row = cmdline_row; /* put cursor on last line for messages */
8891 msg_col = 0;
8892 screen_start(); /* don't know where cursor is now */
8893 msg_scrolled = 0; /* can't scroll back */
8894 msg_didany = FALSE;
8895 msg_didout = FALSE;
8896}
8897
8898/*
8899 * Clear one line in ScreenLines.
8900 */
8901 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008902lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8905#ifdef FEAT_MBYTE
8906 if (enc_utf8)
8907 (void)vim_memset(ScreenLinesUC + off, 0,
8908 (size_t)width * sizeof(u8char_T));
8909#endif
8910 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8911}
8912
8913/*
8914 * Mark one line in ScreenLines invalid by setting the attributes to an
8915 * invalid value.
8916 */
8917 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008918lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8921}
8922
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008923#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924/*
8925 * Copy part of a Screenline for vertically split window "wp".
8926 */
8927 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008928linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 unsigned off_to = LineOffset[to] + wp->w_wincol;
8931 unsigned off_from = LineOffset[from] + wp->w_wincol;
8932
8933 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8934 wp->w_width * sizeof(schar_T));
8935# ifdef FEAT_MBYTE
8936 if (enc_utf8)
8937 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 int i;
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8941 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008942 for (i = 0; i < p_mco; ++i)
8943 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8944 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
8946 if (enc_dbcs == DBCS_JPNU)
8947 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8948 wp->w_width * sizeof(schar_T));
8949# endif
8950 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8951 wp->w_width * sizeof(sattr_T));
8952}
8953#endif
8954
8955/*
8956 * Return TRUE if clearing with term string "p" would work.
8957 * It can't work when the string is empty or it won't set the right background.
8958 */
8959 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008960can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 return (*p != NUL && (t_colors <= 1
8963#ifdef FEAT_GUI
8964 || gui.in_use
8965#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008966#ifdef FEAT_TERMGUICOLORS
8967 || (p_tgc && cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8970}
8971
8972/*
8973 * Reset cursor position. Use whenever cursor was moved because of outputting
8974 * something directly to the screen (shell commands) or a terminal control
8975 * code.
8976 */
8977 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008978screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 screen_cur_row = screen_cur_col = 9999;
8981}
8982
8983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 * Move the cursor to position "row","col" in the screen.
8985 * This tries to find the most efficient way to move, minimizing the number of
8986 * characters sent to the terminal.
8987 */
8988 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008989windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008991 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 int i;
8993 int plan;
8994 int cost;
8995 int wouldbe_col;
8996 int noinvcurs;
8997 char_u *bs;
8998 int goto_cost;
8999 int attr;
9000
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009001#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9003
9004#define PLAN_LE 1
9005#define PLAN_CR 2
9006#define PLAN_NL 3
9007#define PLAN_WRITE 4
9008 /* Can't use ScreenLines unless initialized */
9009 if (ScreenLines == NULL)
9010 return;
9011
9012 if (col != screen_cur_col || row != screen_cur_row)
9013 {
9014 /* Check for valid position. */
9015 if (row < 0) /* window without text lines? */
9016 row = 0;
9017 if (row >= screen_Rows)
9018 row = screen_Rows - 1;
9019 if (col >= screen_Columns)
9020 col = screen_Columns - 1;
9021
9022 /* check if no cursor movement is allowed in highlight mode */
9023 if (screen_attr && *T_MS == NUL)
9024 noinvcurs = HIGHL_COST;
9025 else
9026 noinvcurs = 0;
9027 goto_cost = GOTO_COST + noinvcurs;
9028
9029 /*
9030 * Plan how to do the positioning:
9031 * 1. Use CR to move it to column 0, same row.
9032 * 2. Use T_LE to move it a few columns to the left.
9033 * 3. Use NL to move a few lines down, column 0.
9034 * 4. Move a few columns to the right with T_ND or by writing chars.
9035 *
9036 * Don't do this if the cursor went beyond the last column, the cursor
9037 * position is unknown then (some terminals wrap, some don't )
9038 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009039 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 * characters to move the cursor to the right.
9041 */
9042 if (row >= screen_cur_row && screen_cur_col < Columns)
9043 {
9044 /*
9045 * If the cursor is in the same row, bigger col, we can use CR
9046 * or T_LE.
9047 */
9048 bs = NULL; /* init for GCC */
9049 attr = screen_attr;
9050 if (row == screen_cur_row && col < screen_cur_col)
9051 {
9052 /* "le" is preferred over "bc", because "bc" is obsolete */
9053 if (*T_LE)
9054 bs = T_LE; /* "cursor left" */
9055 else
9056 bs = T_BC; /* "backspace character (old) */
9057 if (*bs)
9058 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9059 else
9060 cost = 999;
9061 if (col + 1 < cost) /* using CR is less characters */
9062 {
9063 plan = PLAN_CR;
9064 wouldbe_col = 0;
9065 cost = 1; /* CR is just one character */
9066 }
9067 else
9068 {
9069 plan = PLAN_LE;
9070 wouldbe_col = col;
9071 }
9072 if (noinvcurs) /* will stop highlighting */
9073 {
9074 cost += noinvcurs;
9075 attr = 0;
9076 }
9077 }
9078
9079 /*
9080 * If the cursor is above where we want to be, we can use CR LF.
9081 */
9082 else if (row > screen_cur_row)
9083 {
9084 plan = PLAN_NL;
9085 wouldbe_col = 0;
9086 cost = (row - screen_cur_row) * 2; /* CR LF */
9087 if (noinvcurs) /* will stop highlighting */
9088 {
9089 cost += noinvcurs;
9090 attr = 0;
9091 }
9092 }
9093
9094 /*
9095 * If the cursor is in the same row, smaller col, just use write.
9096 */
9097 else
9098 {
9099 plan = PLAN_WRITE;
9100 wouldbe_col = screen_cur_col;
9101 cost = 0;
9102 }
9103
9104 /*
9105 * Check if any characters that need to be written have the
9106 * correct attributes. Also avoid UTF-8 characters.
9107 */
9108 i = col - wouldbe_col;
9109 if (i > 0)
9110 cost += i;
9111 if (cost < goto_cost && i > 0)
9112 {
9113 /*
9114 * Check if the attributes are correct without additionally
9115 * stopping highlighting.
9116 */
9117 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9118 while (i && *p++ == attr)
9119 --i;
9120 if (i != 0)
9121 {
9122 /*
9123 * Try if it works when highlighting is stopped here.
9124 */
9125 if (*--p == 0)
9126 {
9127 cost += noinvcurs;
9128 while (i && *p++ == 0)
9129 --i;
9130 }
9131 if (i != 0)
9132 cost = 999; /* different attributes, don't do it */
9133 }
9134#ifdef FEAT_MBYTE
9135 if (enc_utf8)
9136 {
9137 /* Don't use an UTF-8 char for positioning, it's slow. */
9138 for (i = wouldbe_col; i < col; ++i)
9139 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9140 {
9141 cost = 999;
9142 break;
9143 }
9144 }
9145#endif
9146 }
9147
9148 /*
9149 * We can do it without term_windgoto()!
9150 */
9151 if (cost < goto_cost)
9152 {
9153 if (plan == PLAN_LE)
9154 {
9155 if (noinvcurs)
9156 screen_stop_highlight();
9157 while (screen_cur_col > col)
9158 {
9159 out_str(bs);
9160 --screen_cur_col;
9161 }
9162 }
9163 else if (plan == PLAN_CR)
9164 {
9165 if (noinvcurs)
9166 screen_stop_highlight();
9167 out_char('\r');
9168 screen_cur_col = 0;
9169 }
9170 else if (plan == PLAN_NL)
9171 {
9172 if (noinvcurs)
9173 screen_stop_highlight();
9174 while (screen_cur_row < row)
9175 {
9176 out_char('\n');
9177 ++screen_cur_row;
9178 }
9179 screen_cur_col = 0;
9180 }
9181
9182 i = col - screen_cur_col;
9183 if (i > 0)
9184 {
9185 /*
9186 * Use cursor-right if it's one character only. Avoids
9187 * removing a line of pixels from the last bold char, when
9188 * using the bold trick in the GUI.
9189 */
9190 if (T_ND[0] != NUL && T_ND[1] == NUL)
9191 {
9192 while (i-- > 0)
9193 out_char(*T_ND);
9194 }
9195 else
9196 {
9197 int off;
9198
9199 off = LineOffset[row] + screen_cur_col;
9200 while (i-- > 0)
9201 {
9202 if (ScreenAttrs[off] != screen_attr)
9203 screen_stop_highlight();
9204#ifdef FEAT_MBYTE
9205 out_flush_check();
9206#endif
9207 out_char(ScreenLines[off]);
9208#ifdef FEAT_MBYTE
9209 if (enc_dbcs == DBCS_JPNU
9210 && ScreenLines[off] == 0x8e)
9211 out_char(ScreenLines2[off]);
9212#endif
9213 ++off;
9214 }
9215 }
9216 }
9217 }
9218 }
9219 else
9220 cost = 999;
9221
9222 if (cost >= goto_cost)
9223 {
9224 if (noinvcurs)
9225 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009226 if (row == screen_cur_row && (col > screen_cur_col)
9227 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 term_cursor_right(col - screen_cur_col);
9229 else
9230 term_windgoto(row, col);
9231 }
9232 screen_cur_row = row;
9233 screen_cur_col = col;
9234 }
9235}
9236
9237/*
9238 * Set cursor to its position in the current window.
9239 */
9240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009241setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242{
9243 if (redrawing())
9244 {
9245 validate_cursor();
9246 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9247 W_WINCOL(curwin) + (
9248#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009249 /* With 'rightleft' set and the cursor on a double-wide
9250 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9252# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009253 (has_mbyte
9254 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9255 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256# endif
9257 1)) :
9258#endif
9259 curwin->w_wcol));
9260 }
9261}
9262
9263
9264/*
9265 * insert 'line_count' lines at 'row' in window 'wp'
9266 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9267 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9268 * scrolling.
9269 * Returns FAIL if the lines are not inserted, OK for success.
9270 */
9271 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009272win_ins_lines(
9273 win_T *wp,
9274 int row,
9275 int line_count,
9276 int invalid,
9277 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
9279 int did_delete;
9280 int nextrow;
9281 int lastrow;
9282 int retval;
9283
9284 if (invalid)
9285 wp->w_lines_valid = 0;
9286
9287 if (wp->w_height < 5)
9288 return FAIL;
9289
9290 if (line_count > wp->w_height - row)
9291 line_count = wp->w_height - row;
9292
9293 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9294 if (retval != MAYBE)
9295 return retval;
9296
9297 /*
9298 * If there is a next window or a status line, we first try to delete the
9299 * lines at the bottom to avoid messing what is after the window.
9300 * If this fails and there are following windows, don't do anything to avoid
9301 * messing up those windows, better just redraw.
9302 */
9303 did_delete = FALSE;
9304#ifdef FEAT_WINDOWS
9305 if (wp->w_next != NULL || wp->w_status_height)
9306 {
9307 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9308 line_count, (int)Rows, FALSE, NULL) == OK)
9309 did_delete = TRUE;
9310 else if (wp->w_next)
9311 return FAIL;
9312 }
9313#endif
9314 /*
9315 * if no lines deleted, blank the lines that will end up below the window
9316 */
9317 if (!did_delete)
9318 {
9319#ifdef FEAT_WINDOWS
9320 wp->w_redr_status = TRUE;
9321#endif
9322 redraw_cmdline = TRUE;
9323 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9324 lastrow = nextrow + line_count;
9325 if (lastrow > Rows)
9326 lastrow = Rows;
9327 screen_fill(nextrow - line_count, lastrow - line_count,
9328 W_WINCOL(wp), (int)W_ENDCOL(wp),
9329 ' ', ' ', 0);
9330 }
9331
9332 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9333 == FAIL)
9334 {
9335 /* deletion will have messed up other windows */
9336 if (did_delete)
9337 {
9338#ifdef FEAT_WINDOWS
9339 wp->w_redr_status = TRUE;
9340#endif
9341 win_rest_invalid(W_NEXT(wp));
9342 }
9343 return FAIL;
9344 }
9345
9346 return OK;
9347}
9348
9349/*
9350 * delete "line_count" window lines at "row" in window "wp"
9351 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9352 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9353 * scrolling
9354 * Return OK for success, FAIL if the lines are not deleted.
9355 */
9356 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009357win_del_lines(
9358 win_T *wp,
9359 int row,
9360 int line_count,
9361 int invalid,
9362 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 int retval;
9365
9366 if (invalid)
9367 wp->w_lines_valid = 0;
9368
9369 if (line_count > wp->w_height - row)
9370 line_count = wp->w_height - row;
9371
9372 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9373 if (retval != MAYBE)
9374 return retval;
9375
9376 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9377 (int)Rows, FALSE, NULL) == FAIL)
9378 return FAIL;
9379
9380#ifdef FEAT_WINDOWS
9381 /*
9382 * If there are windows or status lines below, try to put them at the
9383 * correct place. If we can't do that, they have to be redrawn.
9384 */
9385 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9386 {
9387 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9388 line_count, (int)Rows, NULL) == FAIL)
9389 {
9390 wp->w_redr_status = TRUE;
9391 win_rest_invalid(wp->w_next);
9392 }
9393 }
9394 /*
9395 * If this is the last window and there is no status line, redraw the
9396 * command line later.
9397 */
9398 else
9399#endif
9400 redraw_cmdline = TRUE;
9401 return OK;
9402}
9403
9404/*
9405 * Common code for win_ins_lines() and win_del_lines().
9406 * Returns OK or FAIL when the work has been done.
9407 * Returns MAYBE when not finished yet.
9408 */
9409 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009410win_do_lines(
9411 win_T *wp,
9412 int row,
9413 int line_count,
9414 int mayclear,
9415 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 int retval;
9418
9419 if (!redrawing() || line_count <= 0)
9420 return FAIL;
9421
9422 /* only a few lines left: redraw is faster */
9423 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009424#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 && wp->w_width == Columns
9426#endif
9427 )
9428 {
9429 screenclear(); /* will set wp->w_lines_valid to 0 */
9430 return FAIL;
9431 }
9432
9433 /*
9434 * Delete all remaining lines
9435 */
9436 if (row + line_count >= wp->w_height)
9437 {
9438 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9439 W_WINCOL(wp), (int)W_ENDCOL(wp),
9440 ' ', ' ', 0);
9441 return OK;
9442 }
9443
9444 /*
9445 * when scrolling, the message on the command line should be cleared,
9446 * otherwise it will stay there forever.
9447 */
9448 clear_cmdline = TRUE;
9449
9450 /*
9451 * If the terminal can set a scroll region, use that.
9452 * Always do this in a vertically split window. This will redraw from
9453 * ScreenLines[] when t_CV isn't defined. That's faster than using
9454 * win_line().
9455 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009456 * a character in the lower right corner of the scroll region may cause a
9457 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 */
9459 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009460#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 || W_WIDTH(wp) != Columns
9462#endif
9463 )
9464 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009465#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9467#endif
9468 scroll_region_set(wp, row);
9469 if (del)
9470 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9471 wp->w_height - row, FALSE, wp);
9472 else
9473 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9474 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009475#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9477#endif
9478 scroll_region_reset();
9479 return retval;
9480 }
9481
9482#ifdef FEAT_WINDOWS
9483 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9484 return FAIL;
9485#endif
9486
9487 return MAYBE;
9488}
9489
9490/*
9491 * window 'wp' and everything after it is messed up, mark it for redraw
9492 */
9493 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009494win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495{
9496#ifdef FEAT_WINDOWS
9497 while (wp != NULL)
9498#else
9499 if (wp != NULL)
9500#endif
9501 {
9502 redraw_win_later(wp, NOT_VALID);
9503#ifdef FEAT_WINDOWS
9504 wp->w_redr_status = TRUE;
9505 wp = wp->w_next;
9506#endif
9507 }
9508 redraw_cmdline = TRUE;
9509}
9510
9511/*
9512 * The rest of the routines in this file perform screen manipulations. The
9513 * given operation is performed physically on the screen. The corresponding
9514 * change is also made to the internal screen image. In this way, the editor
9515 * anticipates the effect of editing changes on the appearance of the screen.
9516 * That way, when we call screenupdate a complete redraw isn't usually
9517 * necessary. Another advantage is that we can keep adding code to anticipate
9518 * screen changes, and in the meantime, everything still works.
9519 */
9520
9521/*
9522 * types for inserting or deleting lines
9523 */
9524#define USE_T_CAL 1
9525#define USE_T_CDL 2
9526#define USE_T_AL 3
9527#define USE_T_CE 4
9528#define USE_T_DL 5
9529#define USE_T_SR 6
9530#define USE_NL 7
9531#define USE_T_CD 8
9532#define USE_REDRAW 9
9533
9534/*
9535 * insert lines on the screen and update ScreenLines[]
9536 * 'end' is the line after the scrolled part. Normally it is Rows.
9537 * When scrolling region used 'off' is the offset from the top for the region.
9538 * 'row' and 'end' are relative to the start of the region.
9539 *
9540 * return FAIL for failure, OK for success.
9541 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009542 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009543screen_ins_lines(
9544 int off,
9545 int row,
9546 int line_count,
9547 int end,
9548 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 int i;
9551 int j;
9552 unsigned temp;
9553 int cursor_row;
9554 int type;
9555 int result_empty;
9556 int can_ce = can_clear(T_CE);
9557
9558 /*
9559 * FAIL if
9560 * - there is no valid screen
9561 * - the screen has to be redrawn completely
9562 * - the line count is less than one
9563 * - the line count is more than 'ttyscroll'
9564 */
9565 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9566 return FAIL;
9567
9568 /*
9569 * There are seven ways to insert lines:
9570 * 0. When in a vertically split window and t_CV isn't set, redraw the
9571 * characters from ScreenLines[].
9572 * 1. Use T_CD (clear to end of display) if it exists and the result of
9573 * the insert is just empty lines
9574 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9575 * present or line_count > 1. It looks better if we do all the inserts
9576 * at once.
9577 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9578 * insert is just empty lines and T_CE is not present or line_count >
9579 * 1.
9580 * 4. Use T_AL (insert line) if it exists.
9581 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9582 * just empty lines.
9583 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9584 * just empty lines.
9585 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9586 * the 'da' flag is not set or we have clear line capability.
9587 * 8. redraw the characters from ScreenLines[].
9588 *
9589 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9590 * the scrollbar for the window. It does have insert line, use that if it
9591 * exists.
9592 */
9593 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009594#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9596 type = USE_REDRAW;
9597 else
9598#endif
9599 if (can_clear(T_CD) && result_empty)
9600 type = USE_T_CD;
9601 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9602 type = USE_T_CAL;
9603 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9604 type = USE_T_CDL;
9605 else if (*T_AL != NUL)
9606 type = USE_T_AL;
9607 else if (can_ce && result_empty)
9608 type = USE_T_CE;
9609 else if (*T_DL != NUL && result_empty)
9610 type = USE_T_DL;
9611 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9612 type = USE_T_SR;
9613 else
9614 return FAIL;
9615
9616 /*
9617 * For clearing the lines screen_del_lines() is used. This will also take
9618 * care of t_db if necessary.
9619 */
9620 if (type == USE_T_CD || type == USE_T_CDL ||
9621 type == USE_T_CE || type == USE_T_DL)
9622 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9623
9624 /*
9625 * If text is retained below the screen, first clear or delete as many
9626 * lines at the bottom of the window as are about to be inserted so that
9627 * the deleted lines won't later surface during a screen_del_lines.
9628 */
9629 if (*T_DB)
9630 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9631
9632#ifdef FEAT_CLIPBOARD
9633 /* Remove a modeless selection when inserting lines halfway the screen
9634 * or not the full width of the screen. */
9635 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009636# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 || (wp != NULL && wp->w_width != Columns)
9638# endif
9639 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009640 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 else
9642 clip_scroll_selection(-line_count);
9643#endif
9644
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#ifdef FEAT_GUI
9646 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9647 * scrolling is actually carried out. */
9648 gui_dont_update_cursor();
9649#endif
9650
9651 if (*T_CCS != NUL) /* cursor relative to region */
9652 cursor_row = row;
9653 else
9654 cursor_row = row + off;
9655
9656 /*
9657 * Shift LineOffset[] line_count down to reflect the inserted lines.
9658 * Clear the inserted lines in ScreenLines[].
9659 */
9660 row += off;
9661 end += off;
9662 for (i = 0; i < line_count; ++i)
9663 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009664#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 if (wp != NULL && wp->w_width != Columns)
9666 {
9667 /* need to copy part of a line */
9668 j = end - 1 - i;
9669 while ((j -= line_count) >= row)
9670 linecopy(j + line_count, j, wp);
9671 j += line_count;
9672 if (can_clear((char_u *)" "))
9673 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9674 else
9675 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9676 LineWraps[j] = FALSE;
9677 }
9678 else
9679#endif
9680 {
9681 j = end - 1 - i;
9682 temp = LineOffset[j];
9683 while ((j -= line_count) >= row)
9684 {
9685 LineOffset[j + line_count] = LineOffset[j];
9686 LineWraps[j + line_count] = LineWraps[j];
9687 }
9688 LineOffset[j + line_count] = temp;
9689 LineWraps[j + line_count] = FALSE;
9690 if (can_clear((char_u *)" "))
9691 lineclear(temp, (int)Columns);
9692 else
9693 lineinvalid(temp, (int)Columns);
9694 }
9695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
9697 screen_stop_highlight();
9698 windgoto(cursor_row, 0);
9699
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009700#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 /* redraw the characters */
9702 if (type == USE_REDRAW)
9703 redraw_block(row, end, wp);
9704 else
9705#endif
9706 if (type == USE_T_CAL)
9707 {
9708 term_append_lines(line_count);
9709 screen_start(); /* don't know where cursor is now */
9710 }
9711 else
9712 {
9713 for (i = 0; i < line_count; i++)
9714 {
9715 if (type == USE_T_AL)
9716 {
9717 if (i && cursor_row != 0)
9718 windgoto(cursor_row, 0);
9719 out_str(T_AL);
9720 }
9721 else /* type == USE_T_SR */
9722 out_str(T_SR);
9723 screen_start(); /* don't know where cursor is now */
9724 }
9725 }
9726
9727 /*
9728 * With scroll-reverse and 'da' flag set we need to clear the lines that
9729 * have been scrolled down into the region.
9730 */
9731 if (type == USE_T_SR && *T_DA)
9732 {
9733 for (i = 0; i < line_count; ++i)
9734 {
9735 windgoto(off + i, 0);
9736 out_str(T_CE);
9737 screen_start(); /* don't know where cursor is now */
9738 }
9739 }
9740
9741#ifdef FEAT_GUI
9742 gui_can_update_cursor();
9743 if (gui.in_use)
9744 out_flush(); /* always flush after a scroll */
9745#endif
9746 return OK;
9747}
9748
9749/*
9750 * delete lines on the screen and update ScreenLines[]
9751 * 'end' is the line after the scrolled part. Normally it is Rows.
9752 * When scrolling region used 'off' is the offset from the top for the region.
9753 * 'row' and 'end' are relative to the start of the region.
9754 *
9755 * Return OK for success, FAIL if the lines are not deleted.
9756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009758screen_del_lines(
9759 int off,
9760 int row,
9761 int line_count,
9762 int end,
9763 int force, /* even when line_count > p_ttyscroll */
9764 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765{
9766 int j;
9767 int i;
9768 unsigned temp;
9769 int cursor_row;
9770 int cursor_end;
9771 int result_empty; /* result is empty until end of region */
9772 int can_delete; /* deleting line codes can be used */
9773 int type;
9774
9775 /*
9776 * FAIL if
9777 * - there is no valid screen
9778 * - the screen has to be redrawn completely
9779 * - the line count is less than one
9780 * - the line count is more than 'ttyscroll'
9781 */
9782 if (!screen_valid(TRUE) || line_count <= 0 ||
9783 (!force && line_count > p_ttyscroll))
9784 return FAIL;
9785
9786 /*
9787 * Check if the rest of the current region will become empty.
9788 */
9789 result_empty = row + line_count >= end;
9790
9791 /*
9792 * We can delete lines only when 'db' flag not set or when 'ce' option
9793 * available.
9794 */
9795 can_delete = (*T_DB == NUL || can_clear(T_CE));
9796
9797 /*
9798 * There are six ways to delete lines:
9799 * 0. When in a vertically split window and t_CV isn't set, redraw the
9800 * characters from ScreenLines[].
9801 * 1. Use T_CD if it exists and the result is empty.
9802 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9803 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9804 * none of the other ways work.
9805 * 4. Use T_CE (erase line) if the result is empty.
9806 * 5. Use T_DL (delete line) if it exists.
9807 * 6. redraw the characters from ScreenLines[].
9808 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009809#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9811 type = USE_REDRAW;
9812 else
9813#endif
9814 if (can_clear(T_CD) && result_empty)
9815 type = USE_T_CD;
9816#if defined(__BEOS__) && defined(BEOS_DR8)
9817 /*
9818 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9819 * its internal termcap... this works okay for tests which test *T_DB !=
9820 * NUL. It has the disadvantage that the user cannot use any :set t_*
9821 * command to get T_DB (back) to empty_option, only :set term=... will do
9822 * the trick...
9823 * Anyway, this hack will hopefully go away with the next OS release.
9824 * (Olaf Seibert)
9825 */
9826 else if (row == 0 && T_DB == empty_option
9827 && (line_count == 1 || *T_CDL == NUL))
9828#else
9829 else if (row == 0 && (
9830#ifndef AMIGA
9831 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9832 * up, so use delete-line command */
9833 line_count == 1 ||
9834#endif
9835 *T_CDL == NUL))
9836#endif
9837 type = USE_NL;
9838 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9839 type = USE_T_CDL;
9840 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009841#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 && (wp == NULL || wp->w_width == Columns)
9843#endif
9844 )
9845 type = USE_T_CE;
9846 else if (*T_DL != NUL && can_delete)
9847 type = USE_T_DL;
9848 else if (*T_CDL != NUL && can_delete)
9849 type = USE_T_CDL;
9850 else
9851 return FAIL;
9852
9853#ifdef FEAT_CLIPBOARD
9854 /* Remove a modeless selection when deleting lines halfway the screen or
9855 * not the full width of the screen. */
9856 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009857# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 || (wp != NULL && wp->w_width != Columns)
9859# endif
9860 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009861 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 else
9863 clip_scroll_selection(line_count);
9864#endif
9865
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#ifdef FEAT_GUI
9867 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9868 * scrolling is actually carried out. */
9869 gui_dont_update_cursor();
9870#endif
9871
9872 if (*T_CCS != NUL) /* cursor relative to region */
9873 {
9874 cursor_row = row;
9875 cursor_end = end;
9876 }
9877 else
9878 {
9879 cursor_row = row + off;
9880 cursor_end = end + off;
9881 }
9882
9883 /*
9884 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9885 * Clear the inserted lines in ScreenLines[].
9886 */
9887 row += off;
9888 end += off;
9889 for (i = 0; i < line_count; ++i)
9890 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009891#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 if (wp != NULL && wp->w_width != Columns)
9893 {
9894 /* need to copy part of a line */
9895 j = row + i;
9896 while ((j += line_count) <= end - 1)
9897 linecopy(j - line_count, j, wp);
9898 j -= line_count;
9899 if (can_clear((char_u *)" "))
9900 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9901 else
9902 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9903 LineWraps[j] = FALSE;
9904 }
9905 else
9906#endif
9907 {
9908 /* whole width, moving the line pointers is faster */
9909 j = row + i;
9910 temp = LineOffset[j];
9911 while ((j += line_count) <= end - 1)
9912 {
9913 LineOffset[j - line_count] = LineOffset[j];
9914 LineWraps[j - line_count] = LineWraps[j];
9915 }
9916 LineOffset[j - line_count] = temp;
9917 LineWraps[j - line_count] = FALSE;
9918 if (can_clear((char_u *)" "))
9919 lineclear(temp, (int)Columns);
9920 else
9921 lineinvalid(temp, (int)Columns);
9922 }
9923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
9925 screen_stop_highlight();
9926
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009927#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 /* redraw the characters */
9929 if (type == USE_REDRAW)
9930 redraw_block(row, end, wp);
9931 else
9932#endif
9933 if (type == USE_T_CD) /* delete the lines */
9934 {
9935 windgoto(cursor_row, 0);
9936 out_str(T_CD);
9937 screen_start(); /* don't know where cursor is now */
9938 }
9939 else if (type == USE_T_CDL)
9940 {
9941 windgoto(cursor_row, 0);
9942 term_delete_lines(line_count);
9943 screen_start(); /* don't know where cursor is now */
9944 }
9945 /*
9946 * Deleting lines at top of the screen or scroll region: Just scroll
9947 * the whole screen (scroll region) up by outputting newlines on the
9948 * last line.
9949 */
9950 else if (type == USE_NL)
9951 {
9952 windgoto(cursor_end - 1, 0);
9953 for (i = line_count; --i >= 0; )
9954 out_char('\n'); /* cursor will remain on same line */
9955 }
9956 else
9957 {
9958 for (i = line_count; --i >= 0; )
9959 {
9960 if (type == USE_T_DL)
9961 {
9962 windgoto(cursor_row, 0);
9963 out_str(T_DL); /* delete a line */
9964 }
9965 else /* type == USE_T_CE */
9966 {
9967 windgoto(cursor_row + i, 0);
9968 out_str(T_CE); /* erase a line */
9969 }
9970 screen_start(); /* don't know where cursor is now */
9971 }
9972 }
9973
9974 /*
9975 * If the 'db' flag is set, we need to clear the lines that have been
9976 * scrolled up at the bottom of the region.
9977 */
9978 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9979 {
9980 for (i = line_count; i > 0; --i)
9981 {
9982 windgoto(cursor_end - i, 0);
9983 out_str(T_CE); /* erase a line */
9984 screen_start(); /* don't know where cursor is now */
9985 }
9986 }
9987
9988#ifdef FEAT_GUI
9989 gui_can_update_cursor();
9990 if (gui.in_use)
9991 out_flush(); /* always flush after a scroll */
9992#endif
9993
9994 return OK;
9995}
9996
9997/*
9998 * show the current mode and ruler
9999 *
10000 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10001 * If clear_cmdline is FALSE there may be a message there that needs to be
10002 * cleared only if a mode is shown.
10003 * Return the length of the message (0 if no message).
10004 */
10005 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010006showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007{
10008 int need_clear;
10009 int length = 0;
10010 int do_mode;
10011 int attr;
10012 int nwr_save;
10013#ifdef FEAT_INS_EXPAND
10014 int sub_attr;
10015#endif
10016
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010017 do_mode = ((p_smd && msg_silent == 0)
10018 && ((State & INSERT)
10019 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010020 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 if (do_mode || Recording)
10022 {
10023 /*
10024 * Don't show mode right now, when not redrawing or inside a mapping.
10025 * Call char_avail() only when we are going to show something, because
10026 * it takes a bit of time.
10027 */
10028 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10029 {
10030 redraw_cmdline = TRUE; /* show mode later */
10031 return 0;
10032 }
10033
10034 nwr_save = need_wait_return;
10035
10036 /* wait a bit before overwriting an important message */
10037 check_for_delay(FALSE);
10038
10039 /* if the cmdline is more than one line high, erase top lines */
10040 need_clear = clear_cmdline;
10041 if (clear_cmdline && cmdline_row < Rows - 1)
10042 msg_clr_cmdline(); /* will reset clear_cmdline */
10043
10044 /* Position on the last line in the window, column 0 */
10045 msg_pos_mode();
10046 cursor_off();
10047 attr = hl_attr(HLF_CM); /* Highlight mode */
10048 if (do_mode)
10049 {
10050 MSG_PUTS_ATTR("--", attr);
10051#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010052 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010053# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010054 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010055# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010056 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010057# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010058 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010059# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 MSG_PUTS_ATTR(" IM", attr);
10061# else
10062 MSG_PUTS_ATTR(" XIM", attr);
10063# endif
10064#endif
10065#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10066 if (gui.in_use)
10067 {
10068 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010069 {
10070 /* HANGUL */
10071 if (enc_utf8)
10072 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10073 else
10074 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077#endif
10078#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010079 /* CTRL-X in Insert mode */
10080 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 {
10082 /* These messages can get long, avoid a wrap in a narrow
10083 * window. Prefer showing edit_submode_extra. */
10084 length = (Rows - msg_row) * Columns - 3;
10085 if (edit_submode_extra != NULL)
10086 length -= vim_strsize(edit_submode_extra);
10087 if (length > 0)
10088 {
10089 if (edit_submode_pre != NULL)
10090 length -= vim_strsize(edit_submode_pre);
10091 if (length - vim_strsize(edit_submode) > 0)
10092 {
10093 if (edit_submode_pre != NULL)
10094 msg_puts_attr(edit_submode_pre, attr);
10095 msg_puts_attr(edit_submode, attr);
10096 }
10097 if (edit_submode_extra != NULL)
10098 {
10099 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10100 if ((int)edit_submode_highl < (int)HLF_COUNT)
10101 sub_attr = hl_attr(edit_submode_highl);
10102 else
10103 sub_attr = attr;
10104 msg_puts_attr(edit_submode_extra, sub_attr);
10105 }
10106 }
10107 length = 0;
10108 }
10109 else
10110#endif
10111 {
10112#ifdef FEAT_VREPLACE
10113 if (State & VREPLACE_FLAG)
10114 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10115 else
10116#endif
10117 if (State & REPLACE_FLAG)
10118 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10119 else if (State & INSERT)
10120 {
10121#ifdef FEAT_RIGHTLEFT
10122 if (p_ri)
10123 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10124#endif
10125 MSG_PUTS_ATTR(_(" INSERT"), attr);
10126 }
10127 else if (restart_edit == 'I')
10128 MSG_PUTS_ATTR(_(" (insert)"), attr);
10129 else if (restart_edit == 'R')
10130 MSG_PUTS_ATTR(_(" (replace)"), attr);
10131 else if (restart_edit == 'V')
10132 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10133#ifdef FEAT_RIGHTLEFT
10134 if (p_hkmap)
10135 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10136# ifdef FEAT_FKMAP
10137 if (p_fkmap)
10138 MSG_PUTS_ATTR(farsi_text_5, attr);
10139# endif
10140#endif
10141#ifdef FEAT_KEYMAP
10142 if (State & LANGMAP)
10143 {
10144# ifdef FEAT_ARABIC
10145 if (curwin->w_p_arab)
10146 MSG_PUTS_ATTR(_(" Arabic"), attr);
10147 else
10148# endif
10149 MSG_PUTS_ATTR(_(" (lang)"), attr);
10150 }
10151#endif
10152 if ((State & INSERT) && p_paste)
10153 MSG_PUTS_ATTR(_(" (paste)"), attr);
10154
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 if (VIsual_active)
10156 {
10157 char *p;
10158
10159 /* Don't concatenate separate words to avoid translation
10160 * problems. */
10161 switch ((VIsual_select ? 4 : 0)
10162 + (VIsual_mode == Ctrl_V) * 2
10163 + (VIsual_mode == 'V'))
10164 {
10165 case 0: p = N_(" VISUAL"); break;
10166 case 1: p = N_(" VISUAL LINE"); break;
10167 case 2: p = N_(" VISUAL BLOCK"); break;
10168 case 4: p = N_(" SELECT"); break;
10169 case 5: p = N_(" SELECT LINE"); break;
10170 default: p = N_(" SELECT BLOCK"); break;
10171 }
10172 MSG_PUTS_ATTR(_(p), attr);
10173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 MSG_PUTS_ATTR(" --", attr);
10175 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 need_clear = TRUE;
10178 }
10179 if (Recording
10180#ifdef FEAT_INS_EXPAND
10181 && edit_submode == NULL /* otherwise it gets too long */
10182#endif
10183 )
10184 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010185 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 need_clear = TRUE;
10187 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010188
10189 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (need_clear || clear_cmdline)
10191 msg_clr_eos();
10192 msg_didout = FALSE; /* overwrite this message */
10193 length = msg_col;
10194 msg_col = 0;
10195 need_wait_return = nwr_save; /* never ask for hit-return for this */
10196 }
10197 else if (clear_cmdline && msg_silent == 0)
10198 /* Clear the whole command line. Will reset "clear_cmdline". */
10199 msg_clr_cmdline();
10200
10201#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 /* In Visual mode the size of the selected area must be redrawn. */
10203 if (VIsual_active)
10204 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
10206 /* If the last window has no status line, the ruler is after the mode
10207 * message and must be redrawn */
10208 if (redrawing()
10209# ifdef FEAT_WINDOWS
10210 && lastwin->w_status_height == 0
10211# endif
10212 )
10213 win_redr_ruler(lastwin, TRUE);
10214#endif
10215 redraw_cmdline = FALSE;
10216 clear_cmdline = FALSE;
10217
10218 return length;
10219}
10220
10221/*
10222 * Position for a mode message.
10223 */
10224 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010225msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 msg_col = 0;
10228 msg_row = Rows - 1;
10229}
10230
10231/*
10232 * Delete mode message. Used when ESC is typed which is expected to end
10233 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010234 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 */
10236 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010237unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238{
10239 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010240 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 */
10242 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10243 redraw_cmdline = TRUE; /* delete mode later */
10244 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010245 clearmode();
10246}
10247
10248/*
10249 * Clear the mode message.
10250 */
10251 void
10252clearmode()
10253{
10254 msg_pos_mode();
10255 if (Recording)
10256 recording_mode(hl_attr(HLF_CM));
10257 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258}
10259
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010261recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010262{
10263 MSG_PUTS_ATTR(_("recording"), attr);
10264 if (!shortmess(SHM_RECORDING))
10265 {
10266 char_u s[4];
10267 sprintf((char *)s, " @%c", Recording);
10268 MSG_PUTS_ATTR(s, attr);
10269 }
10270}
10271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010272#if defined(FEAT_WINDOWS)
10273/*
10274 * Draw the tab pages line at the top of the Vim window.
10275 */
10276 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010277draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010278{
10279 int tabcount = 0;
10280 tabpage_T *tp;
10281 int tabwidth;
10282 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010283 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010284 int attr;
10285 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010286 win_T *cwp;
10287 int wincount;
10288 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010289 int c;
10290 int len;
10291 int attr_sel = hl_attr(HLF_TPS);
10292 int attr_nosel = hl_attr(HLF_TP);
10293 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010294 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010295 int room;
10296 int use_sep_chars = (t_colors < 8
10297#ifdef FEAT_GUI
10298 && !gui.in_use
10299#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010300#ifdef FEAT_TERMGUICOLORS
10301 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010302#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010303 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010304
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010305 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010306
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010307#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010308 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010309 if (gui_use_tabline())
10310 {
10311 gui_update_tabline();
10312 return;
10313 }
10314#endif
10315
10316 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010317 return;
10318
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010319#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010320
10321 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10322 for (scol = 0; scol < Columns; ++scol)
10323 TabPageIdxs[scol] = 0;
10324
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010325 /* Use the 'tabline' option if it's set. */
10326 if (*p_tal != NUL)
10327 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010328 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010329
10330 /* Check for an error. If there is one we would loop in redrawing the
10331 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010332 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010333 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010334 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010335 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010336 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010337 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010338 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010339 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010340#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010341 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010342 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10343 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010344
Bram Moolenaar238a5642006-02-21 22:12:05 +000010345 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10346 if (tabwidth < 6)
10347 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010348
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349 attr = attr_nosel;
10350 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010351 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010352 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10353 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010354 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010356
Bram Moolenaar238a5642006-02-21 22:12:05 +000010357 if (tp->tp_topframe == topframe)
10358 attr = attr_sel;
10359 if (use_sep_chars && col > 0)
10360 screen_putchar('|', 0, col++, attr);
10361
10362 if (tp->tp_topframe != topframe)
10363 attr = attr_nosel;
10364
10365 screen_putchar(' ', 0, col++, attr);
10366
10367 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010368 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 cwp = curwin;
10370 wp = firstwin;
10371 }
10372 else
10373 {
10374 cwp = tp->tp_curwin;
10375 wp = tp->tp_firstwin;
10376 }
10377
10378 modified = FALSE;
10379 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10380 if (bufIsChanged(wp->w_buffer))
10381 modified = TRUE;
10382 if (modified || wincount > 1)
10383 {
10384 if (wincount > 1)
10385 {
10386 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010387 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010388 if (col + len >= Columns - 3)
10389 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010390 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010391#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010392 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010393#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010394 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010395#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010396 );
10397 col += len;
10398 }
10399 if (modified)
10400 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10401 screen_putchar(' ', 0, col++, attr);
10402 }
10403
10404 room = scol - col + tabwidth - 1;
10405 if (room > 0)
10406 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010407 /* Get buffer name in NameBuff[] */
10408 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010409 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010410 len = vim_strsize(NameBuff);
10411 p = NameBuff;
10412#ifdef FEAT_MBYTE
10413 if (has_mbyte)
10414 while (len > room)
10415 {
10416 len -= ptr2cells(p);
10417 mb_ptr_adv(p);
10418 }
10419 else
10420#endif
10421 if (len > room)
10422 {
10423 p += len - room;
10424 len = room;
10425 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010426 if (len > Columns - col - 1)
10427 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010428
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010429 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010430 col += len;
10431 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010432 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010433
10434 /* Store the tab page number in TabPageIdxs[], so that
10435 * jump_to_mouse() knows where each one is. */
10436 ++tabcount;
10437 while (scol < col)
10438 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010439 }
10440
Bram Moolenaar238a5642006-02-21 22:12:05 +000010441 if (use_sep_chars)
10442 c = '_';
10443 else
10444 c = ' ';
10445 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010446
10447 /* Put an "X" for closing the current tab if there are several. */
10448 if (first_tabpage->tp_next != NULL)
10449 {
10450 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10451 TabPageIdxs[Columns - 1] = -999;
10452 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010453 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010454
10455 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10456 * set. */
10457 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010458}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010459
10460/*
10461 * Get buffer name for "buf" into NameBuff[].
10462 * Takes care of special buffer names and translates special characters.
10463 */
10464 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010465get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010466{
10467 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010468 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010469 else
10470 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10471 trans_characters(NameBuff, MAXPATHL);
10472}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010473#endif
10474
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10476/*
10477 * Get the character to use in a status line. Get its attributes in "*attr".
10478 */
10479 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010480fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
10482 int fill;
10483 if (is_curwin)
10484 {
10485 *attr = hl_attr(HLF_S);
10486 fill = fill_stl;
10487 }
10488 else
10489 {
10490 *attr = hl_attr(HLF_SNC);
10491 fill = fill_stlnc;
10492 }
10493 /* Use fill when there is highlighting, and highlighting of current
10494 * window differs, or the fillchars differ, or this is not the
10495 * current window */
10496 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10497 || !is_curwin || firstwin == lastwin)
10498 || (fill_stl != fill_stlnc)))
10499 return fill;
10500 if (is_curwin)
10501 return '^';
10502 return '=';
10503}
10504#endif
10505
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010506#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507/*
10508 * Get the character to use in a separator between vertically split windows.
10509 * Get its attributes in "*attr".
10510 */
10511 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010512fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
10514 *attr = hl_attr(HLF_C);
10515 if (*attr == 0 && fill_vert == ' ')
10516 return '|';
10517 else
10518 return fill_vert;
10519}
10520#endif
10521
10522/*
10523 * Return TRUE if redrawing should currently be done.
10524 */
10525 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010526redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
10528 return (!RedrawingDisabled
10529 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10530}
10531
10532/*
10533 * Return TRUE if printing messages should currently be done.
10534 */
10535 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010536messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537{
10538 return (!(p_lz && char_avail() && !KeyTyped));
10539}
10540
10541/*
10542 * Show current status info in ruler and various other places
10543 * If always is FALSE, only show ruler if position has changed.
10544 */
10545 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010546showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547{
10548 if (!always && !redrawing())
10549 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010550#ifdef FEAT_INS_EXPAND
10551 if (pum_visible())
10552 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010553# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010554 /* Don't redraw right now, do it later. */
10555 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010556# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010557 return;
10558 }
10559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010561 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010562 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010563 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 else
10566#endif
10567#ifdef FEAT_CMDL_INFO
10568 win_redr_ruler(curwin, always);
10569#endif
10570
10571#ifdef FEAT_TITLE
10572 if (need_maketitle
10573# ifdef FEAT_STL_OPT
10574 || (p_icon && (stl_syntax & STL_IN_ICON))
10575 || (p_title && (stl_syntax & STL_IN_TITLE))
10576# endif
10577 )
10578 maketitle();
10579#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010580#ifdef FEAT_WINDOWS
10581 /* Redraw the tab pages line if needed. */
10582 if (redraw_tabline)
10583 draw_tabline();
10584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587#ifdef FEAT_CMDL_INFO
10588 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010589win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010591#define RULER_BUF_LEN 70
10592 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 int row;
10594 int fillchar;
10595 int attr;
10596 int empty_line = FALSE;
10597 colnr_T virtcol;
10598 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010599 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010601#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 int this_ru_col;
10603 int off = 0;
10604 int width = Columns;
10605# define WITH_OFF(x) x
10606# define WITH_WIDTH(x) x
10607#else
10608# define WITH_OFF(x) 0
10609# define WITH_WIDTH(x) Columns
10610# define this_ru_col ru_col
10611#endif
10612
10613 /* If 'ruler' off or redrawing disabled, don't do anything */
10614 if (!p_ru)
10615 return;
10616
10617 /*
10618 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10619 * after deleting lines, before cursor.lnum is corrected.
10620 */
10621 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10622 return;
10623
10624#ifdef FEAT_INS_EXPAND
10625 /* Don't draw the ruler while doing insert-completion, it might overwrite
10626 * the (long) mode message. */
10627# ifdef FEAT_WINDOWS
10628 if (wp == lastwin && lastwin->w_status_height == 0)
10629# endif
10630 if (edit_submode != NULL)
10631 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010632 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10633 if (pum_visible())
10634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635#endif
10636
10637#ifdef FEAT_STL_OPT
10638 if (*p_ruf)
10639 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010640 int save_called_emsg = called_emsg;
10641
10642 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010644 if (called_emsg)
10645 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010646 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 return;
10649 }
10650#endif
10651
10652 /*
10653 * Check if not in Insert mode and the line is empty (will show "0-1").
10654 */
10655 if (!(State & INSERT)
10656 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10657 empty_line = TRUE;
10658
10659 /*
10660 * Only draw the ruler when something changed.
10661 */
10662 validate_virtcol_win(wp);
10663 if ( redraw_cmdline
10664 || always
10665 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10666 || wp->w_cursor.col != wp->w_ru_cursor.col
10667 || wp->w_virtcol != wp->w_ru_virtcol
10668#ifdef FEAT_VIRTUALEDIT
10669 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10670#endif
10671 || wp->w_topline != wp->w_ru_topline
10672 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10673#ifdef FEAT_DIFF
10674 || wp->w_topfill != wp->w_ru_topfill
10675#endif
10676 || empty_line != wp->w_ru_empty)
10677 {
10678 cursor_off();
10679#ifdef FEAT_WINDOWS
10680 if (wp->w_status_height)
10681 {
10682 row = W_WINROW(wp) + wp->w_height;
10683 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 off = W_WINCOL(wp);
10685 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 }
10687 else
10688#endif
10689 {
10690 row = Rows - 1;
10691 fillchar = ' ';
10692 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010693#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694 width = Columns;
10695 off = 0;
10696#endif
10697 }
10698
10699 /* In list mode virtcol needs to be recomputed */
10700 virtcol = wp->w_virtcol;
10701 if (wp->w_p_list && lcs_tab1 == NUL)
10702 {
10703 wp->w_p_list = FALSE;
10704 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10705 wp->w_p_list = TRUE;
10706 }
10707
10708 /*
10709 * Some sprintfs return the length, some return a pointer.
10710 * To avoid portability problems we use strlen() here.
10711 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010712 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10714 ? 0L
10715 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010716 len = STRLEN(buffer);
10717 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10719 (int)virtcol + 1);
10720
10721 /*
10722 * Add a "50%" if there is room for it.
10723 * On the last line, don't print in the last column (scrolls the
10724 * screen up on some terminals).
10725 */
10726 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010727 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 o = i + vim_strsize(buffer + i + 1);
10729#ifdef FEAT_WINDOWS
10730 if (wp->w_status_height == 0) /* can't use last char of screen */
10731#endif
10732 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010733#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 this_ru_col = ru_col - (Columns - width);
10735 if (this_ru_col < 0)
10736 this_ru_col = 0;
10737#endif
10738 /* Never use more than half the window/screen width, leave the other
10739 * half for the filename. */
10740 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10741 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10742 if (this_ru_col + o < WITH_WIDTH(width))
10743 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010744 /* need at least 3 chars left for get_rel_pos() + NUL */
10745 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 {
10747#ifdef FEAT_MBYTE
10748 if (has_mbyte)
10749 i += (*mb_char2bytes)(fillchar, buffer + i);
10750 else
10751#endif
10752 buffer[i++] = fillchar;
10753 ++o;
10754 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010755 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 }
10757 /* Truncate at window boundary. */
10758#ifdef FEAT_MBYTE
10759 if (has_mbyte)
10760 {
10761 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010762 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 {
10764 o += (*mb_ptr2cells)(buffer + i);
10765 if (this_ru_col + o > WITH_WIDTH(width))
10766 {
10767 buffer[i] = NUL;
10768 break;
10769 }
10770 }
10771 }
10772 else
10773#endif
10774 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10775 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10776
10777 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10778 i = redraw_cmdline;
10779 screen_fill(row, row + 1,
10780 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10781 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10782 fillchar, fillchar, attr);
10783 /* don't redraw the cmdline because of showing the ruler */
10784 redraw_cmdline = i;
10785 wp->w_ru_cursor = wp->w_cursor;
10786 wp->w_ru_virtcol = wp->w_virtcol;
10787 wp->w_ru_empty = empty_line;
10788 wp->w_ru_topline = wp->w_topline;
10789 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10790#ifdef FEAT_DIFF
10791 wp->w_ru_topfill = wp->w_topfill;
10792#endif
10793 }
10794}
10795#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010796
10797#if defined(FEAT_LINEBREAK) || defined(PROTO)
10798/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010799 * Return the width of the 'number' and 'relativenumber' column.
10800 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010801 * Otherwise it depends on 'numberwidth' and the line count.
10802 */
10803 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010804number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010805{
10806 int n;
10807 linenr_T lnum;
10808
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010809 if (wp->w_p_rnu && !wp->w_p_nu)
10810 /* cursor line shows "0" */
10811 lnum = wp->w_height;
10812 else
10813 /* cursor line shows absolute line number */
10814 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010815
Bram Moolenaar6b314672015-03-20 15:42:10 +010010816 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010817 return wp->w_nrwidth_width;
10818 wp->w_nrwidth_line_count = lnum;
10819
10820 n = 0;
10821 do
10822 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010823 lnum /= 10;
10824 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010825 } while (lnum > 0);
10826
10827 /* 'numberwidth' gives the minimal width plus one */
10828 if (n < wp->w_p_nuw - 1)
10829 n = wp->w_p_nuw - 1;
10830
10831 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010832 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010833 return n;
10834}
10835#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010836
10837/*
10838 * Return the current cursor column. This is the actual position on the
10839 * screen. First column is 0.
10840 */
10841 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010842screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010843{
10844 return screen_cur_col;
10845}
10846
10847/*
10848 * Return the current cursor row. This is the actual position on the screen.
10849 * First row is 0.
10850 */
10851 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010852screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010853{
10854 return screen_cur_row;
10855}