blob: 2f08afb7a828b0899d2472139faa14047c0a787f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
115/*
116 * Buffer for one screen line (characters and attributes).
117 */
118static schar_T *current_ScreenLine;
119
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100120static void win_update(win_T *wp);
121static void win_draw_end(win_T *wp, int c1, int c2, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100123static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
124static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
125static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static int win_line(win_T *, linenr_T, int, int, int nochange);
128static int char_needs_redraw(int off_from, int off_to, int cols);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifdef FEAT_RIGHTLEFT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100130static void screen_line(int row, int coloff, int endcol, int clear_width, int rlflag);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void screen_line(int row, int coloff, int endcol, int clear_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100136#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100140static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200143# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100144static void start_search_hl(void);
145static void end_search_hl(void);
146static void init_search_hl(win_T *wp);
147static void prepare_search_hl(win_T *wp, linenr_T lnum);
148static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
149static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_start_highlight(int attr);
152static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void screenclear2(void);
157static void lineclear(unsigned off, int width);
158static void lineinvalid(unsigned off, int width);
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100159#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static void linecopy(int to, int from, win_T *wp);
161static void redraw_block(int row, int end, win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del);
164static void win_rest_invalid(win_T *wp);
165static void msg_pos_mode(void);
166static void recording_mode(int attr);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100168static void draw_tabline(void);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100171static int fillchar_status(int *attr, int is_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#ifdef FEAT_WINDOWS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100174static int fillchar_vsep(int *attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100177static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100180static void win_redr_ruler(win_T *wp, int always);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181#endif
182
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100183#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
Bram Moolenaarcf089462016-06-12 21:18:43 +0200419redraw_after_callback(void)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100420{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100421 if (State == HITRETURN || State == ASKMORE)
422 ; /* do nothing */
423 else if (State & CMDLINE)
424 redrawcmdline();
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 {
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200435 /* Don't update the cursor when it is blinking and off to avoid
436 * flicker. */
437 if (!gui_mch_is_blink_off())
Bram Moolenaar703a8042016-06-04 16:24:32 +0200438 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 gui_mch_flush();
440 }
441#endif
442}
443
444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 * Changed something in the current window, at buffer line "lnum", that
446 * requires that line and possibly other lines to be redrawn.
447 * Used when entering/leaving Insert mode with the cursor on a folded line.
448 * Used to remove the "$" from a change command.
449 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
450 * may become invalid and the whole window will have to be redrawn.
451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100453redrawWinline(
454 linenr_T lnum,
455 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_FOLDING
458 int i;
459#endif
460
461 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
462 curwin->w_redraw_top = lnum;
463 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
464 curwin->w_redraw_bot = lnum;
465 redraw_later(VALID);
466
467#ifdef FEAT_FOLDING
468 if (invalid)
469 {
470 /* A w_lines[] entry for this lnum has become invalid. */
471 i = find_wl_entry(curwin, lnum);
472 if (i >= 0)
473 curwin->w_lines[i].wl_valid = FALSE;
474 }
475#endif
476}
477
478/*
479 * update all windows that are editing the current buffer
480 */
481 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100482update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 redraw_curbuf_later(type);
485 update_screen(type);
486}
487
488/*
489 * update_screen()
490 *
491 * Based on the current value of curwin->w_topline, transfer a screenfull
492 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
493 */
494 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100495update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497 win_T *wp;
498 static int did_intro = FALSE;
499#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
500 int did_one;
501#endif
502
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000503 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 if (!screen_valid(TRUE))
505 return;
506
507 if (must_redraw)
508 {
509 if (type < must_redraw) /* use maximal type */
510 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000511
512 /* must_redraw is reset here, so that when we run into some weird
513 * reason to redraw while busy redrawing (e.g., asynchronous
514 * scrolling), or update_topline() in win_update() will cause a
515 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 must_redraw = 0;
517 }
518
519 /* Need to update w_lines[]. */
520 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
521 type = NOT_VALID;
522
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000523 /* Postpone the redrawing when it's not needed and when being called
524 * recursively. */
525 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
527 redraw_later(type); /* remember type for next time */
528 must_redraw = type;
529 if (type > INVERTED_ALL)
530 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
531 return;
532 }
533
534 updating_screen = TRUE;
535#ifdef FEAT_SYN_HL
536 ++display_tick; /* let syntax code know we're in a next round of
537 * display updating */
538#endif
539
540 /*
541 * if the screen was scrolled up when displaying a message, scroll it down
542 */
543 if (msg_scrolled)
544 {
545 clear_cmdline = TRUE;
546 if (msg_scrolled > Rows - 5) /* clearing is faster */
547 type = CLEAR;
548 else if (type != CLEAR)
549 {
550 check_for_delay(FALSE);
551 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
552 type = CLEAR;
553 FOR_ALL_WINDOWS(wp)
554 {
555 if (W_WINROW(wp) < msg_scrolled)
556 {
557 if (W_WINROW(wp) + wp->w_height > msg_scrolled
558 && wp->w_redr_type < REDRAW_TOP
559 && wp->w_lines_valid > 0
560 && wp->w_topline == wp->w_lines[0].wl_lnum)
561 {
562 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
563 wp->w_redr_type = REDRAW_TOP;
564 }
565 else
566 {
567 wp->w_redr_type = NOT_VALID;
568#ifdef FEAT_WINDOWS
569 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
570 <= msg_scrolled)
571 wp->w_redr_status = TRUE;
572#endif
573 }
574 }
575 }
576 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000577#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000578 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581 msg_scrolled = 0;
582 need_wait_return = FALSE;
583 }
584
585 /* reset cmdline_row now (may have been changed temporarily) */
586 compute_cmdrow();
587
588 /* Check for changed highlighting */
589 if (need_highlight_changed)
590 highlight_changed();
591
592 if (type == CLEAR) /* first clear screen */
593 {
594 screenclear(); /* will reset clear_cmdline */
595 type = NOT_VALID;
596 }
597
598 if (clear_cmdline) /* going to clear cmdline (done below) */
599 check_for_delay(FALSE);
600
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000601#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200602 /* Force redraw when width of 'number' or 'relativenumber' column
603 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000604 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200605 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
606 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000607 curwin->w_redr_type = NOT_VALID;
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 /*
611 * Only start redrawing if there is really something to do.
612 */
613 if (type == INVERTED)
614 update_curswant();
615 if (curwin->w_redr_type < type
616 && !((type == VALID
617 && curwin->w_lines[0].wl_valid
618#ifdef FEAT_DIFF
619 && curwin->w_topfill == curwin->w_old_topfill
620 && curwin->w_botfill == curwin->w_old_botfill
621#endif
622 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000624 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
626 && curwin->w_old_visual_mode == VIsual_mode
627 && (curwin->w_valid & VALID_VIRTCOL)
628 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 ))
630 curwin->w_redr_type = type;
631
Bram Moolenaar5a305422006-04-28 22:38:25 +0000632#ifdef FEAT_WINDOWS
633 /* Redraw the tab pages line if needed. */
634 if (redraw_tabline || type >= NOT_VALID)
635 draw_tabline();
636#endif
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef FEAT_SYN_HL
639 /*
640 * Correct stored syntax highlighting info for changes in each displayed
641 * buffer. Each buffer must only be done once.
642 */
643 FOR_ALL_WINDOWS(wp)
644 {
645 if (wp->w_buffer->b_mod_set)
646 {
647# ifdef FEAT_WINDOWS
648 win_T *wwp;
649
650 /* Check if we already did this buffer. */
651 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
652 if (wwp->w_buffer == wp->w_buffer)
653 break;
654# endif
655 if (
656# ifdef FEAT_WINDOWS
657 wwp == wp &&
658# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200659 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 syn_stack_apply_changes(wp->w_buffer);
661 }
662 }
663#endif
664
665 /*
666 * Go from top to bottom through the windows, redrawing the ones that need
667 * it.
668 */
669#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
670 did_one = FALSE;
671#endif
672#ifdef FEAT_SEARCH_EXTRA
673 search_hl.rm.regprog = NULL;
674#endif
675 FOR_ALL_WINDOWS(wp)
676 {
677 if (wp->w_redr_type != 0)
678 {
679 cursor_off();
680#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
681 if (!did_one)
682 {
683 did_one = TRUE;
684# ifdef FEAT_SEARCH_EXTRA
685 start_search_hl();
686# endif
687# ifdef FEAT_CLIPBOARD
688 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200689 if (clip_star.available && clip_isautosel_star())
690 clip_update_selection(&clip_star);
691 if (clip_plus.available && clip_isautosel_plus())
692 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693# endif
694#ifdef FEAT_GUI
695 /* Remove the cursor before starting to do anything, because
696 * scrolling may make it difficult to redraw the text under
697 * it. */
698 if (gui.in_use)
699 gui_undraw_cursor();
700#endif
701 }
702#endif
703 win_update(wp);
704 }
705
706#ifdef FEAT_WINDOWS
707 /* redraw status line after the window to minimize cursor movement */
708 if (wp->w_redr_status)
709 {
710 cursor_off();
711 win_redr_status(wp);
712 }
713#endif
714 }
715#if defined(FEAT_SEARCH_EXTRA)
716 end_search_hl();
717#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100718#ifdef FEAT_INS_EXPAND
719 /* May need to redraw the popup menu. */
720 if (pum_visible())
721 pum_redraw();
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724#ifdef FEAT_WINDOWS
725 /* Reset b_mod_set flags. Going through all windows is probably faster
726 * than going through all buffers (there could be many buffers). */
727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
728 wp->w_buffer->b_mod_set = FALSE;
729#else
730 curbuf->b_mod_set = FALSE;
731#endif
732
733 updating_screen = FALSE;
734#ifdef FEAT_GUI
735 gui_may_resize_shell();
736#endif
737
738 /* Clear or redraw the command line. Done last, because scrolling may
739 * mess up the command line. */
740 if (clear_cmdline || redraw_cmdline)
741 showmode();
742
743 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200744 if (!did_intro)
745 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 did_intro = TRUE;
747
748#ifdef FEAT_GUI
749 /* Redraw the cursor and update the scrollbars when all screen updating is
750 * done. */
751 if (gui.in_use)
752 {
753 out_flush(); /* required before updating the cursor */
754 if (did_one)
755 gui_update_cursor(FALSE, FALSE);
756 gui_update_scrollbars(FALSE);
757 }
758#endif
759}
760
Bram Moolenaar860cae12010-06-05 23:22:07 +0200761#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200762/*
763 * Return TRUE if the cursor line in window "wp" may be concealed, according
764 * to the 'concealcursor' option.
765 */
766 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100767conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200768{
769 int c;
770
771 if (*wp->w_p_cocu == NUL)
772 return FALSE;
773 if (get_real_state() & VISUAL)
774 c = 'v';
775 else if (State & INSERT)
776 c = 'i';
777 else if (State & NORMAL)
778 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200779 else if (State & CMDLINE)
780 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200781 else
782 return FALSE;
783 return vim_strchr(wp->w_p_cocu, c) != NULL;
784}
785
786/*
787 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
788 */
789 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100790conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200791{
792 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
793 {
794 need_cursor_line_redraw = TRUE;
795 /* Need to recompute cursor column, e.g., when starting Visual mode
796 * without concealing. */
797 curs_columns(TRUE);
798 }
799}
800
Bram Moolenaar860cae12010-06-05 23:22:07 +0200801 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100802update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200803{
804 int row;
805 int j;
806
Bram Moolenaar908be432016-05-24 10:51:30 +0200807 /* Don't do anything if the screen structures are (not yet) valid. */
808 if (!screen_valid(TRUE))
809 return;
810
Bram Moolenaar860cae12010-06-05 23:22:07 +0200811 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200812 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200813 {
814# ifdef FEAT_GUI
815 /* Remove the cursor before starting to do anything, because scrolling
816 * may make it difficult to redraw the text under it. */
817 if (gui.in_use)
818 gui_undraw_cursor();
819# endif
820 row = 0;
821 for (j = 0; j < wp->w_lines_valid; ++j)
822 {
823 if (lnum == wp->w_lines[j].wl_lnum)
824 {
825 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200826# ifdef FEAT_SEARCH_EXTRA
827 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200828 start_search_hl();
829 prepare_search_hl(wp, lnum);
830# endif
831 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
832# if defined(FEAT_SEARCH_EXTRA)
833 end_search_hl();
834# endif
835 break;
836 }
837 row += wp->w_lines[j].wl_size;
838 }
839# ifdef FEAT_GUI
840 /* Redraw the cursor */
841 if (gui.in_use)
842 {
843 out_flush(); /* required before updating the cursor */
844 gui_update_cursor(FALSE, FALSE);
845 }
846# endif
847 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200848 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200849}
850#endif
851
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100853static void update_prepare(void);
854static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856/*
857 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000858 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100861update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
863 cursor_off();
864 updating_screen = TRUE;
865#ifdef FEAT_GUI
866 /* Remove the cursor before starting to do anything, because scrolling may
867 * make it difficult to redraw the text under it. */
868 if (gui.in_use)
869 gui_undraw_cursor();
870#endif
871#ifdef FEAT_SEARCH_EXTRA
872 start_search_hl();
873#endif
874}
875
876/*
877 * Finish updating one or more windows.
878 */
879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100880update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882 if (redraw_cmdline)
883 showmode();
884
885# ifdef FEAT_SEARCH_EXTRA
886 end_search_hl();
887# endif
888
889 updating_screen = FALSE;
890
891# ifdef FEAT_GUI
892 gui_may_resize_shell();
893
894 /* Redraw the cursor and update the scrollbars when all screen updating is
895 * done. */
896 if (gui.in_use)
897 {
898 out_flush(); /* required before updating the cursor */
899 gui_update_cursor(FALSE, FALSE);
900 gui_update_scrollbars(FALSE);
901 }
902# endif
903}
904#endif
905
906#if defined(FEAT_SIGNS) || defined(PROTO)
907 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100908update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 win_T *wp;
911 int doit = FALSE;
912
913# ifdef FEAT_FOLDING
914 win_foldinfo.fi_level = 0;
915# endif
916
917 /* update/delete a specific mark */
918 FOR_ALL_WINDOWS(wp)
919 {
920 if (buf != NULL && lnum > 0)
921 {
922 if (wp->w_buffer == buf && lnum >= wp->w_topline
923 && lnum < wp->w_botline)
924 {
925 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
926 wp->w_redraw_top = lnum;
927 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
928 wp->w_redraw_bot = lnum;
929 redraw_win_later(wp, VALID);
930 }
931 }
932 else
933 redraw_win_later(wp, VALID);
934 if (wp->w_redr_type != 0)
935 doit = TRUE;
936 }
937
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100938 /* Return when there is nothing to do, screen updating is already
939 * happening (recursive call) or still starting up. */
940 if (!doit || updating_screen
941#ifdef FEAT_GUI
942 || gui.starting
943#endif
944 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 return;
946
947 /* update all windows that need updating */
948 update_prepare();
949
950# ifdef FEAT_WINDOWS
951 for (wp = firstwin; wp; wp = wp->w_next)
952 {
953 if (wp->w_redr_type != 0)
954 win_update(wp);
955 if (wp->w_redr_status)
956 win_redr_status(wp);
957 }
958# else
959 if (curwin->w_redr_type != 0)
960 win_update(curwin);
961# endif
962
963 update_finish();
964}
965#endif
966
967
968#if defined(FEAT_GUI) || defined(PROTO)
969/*
970 * Update a single window, its status line and maybe the command line msg.
971 * Used for the GUI scrollbar.
972 */
973 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100974updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000976 /* return if already busy updating */
977 if (updating_screen)
978 return;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 update_prepare();
981
982#ifdef FEAT_CLIPBOARD
983 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200984 if (clip_star.available && clip_isautosel_star())
985 clip_update_selection(&clip_star);
986 if (clip_plus.available && clip_isautosel_plus())
987 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000993 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000994 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000995 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (wp->w_redr_status
998# ifdef FEAT_CMDL_INFO
999 || p_ru
1000# endif
1001# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001002 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003# endif
1004 )
1005 win_redr_status(wp);
1006#endif
1007
1008 update_finish();
1009}
1010#endif
1011
1012/*
1013 * Update a single window.
1014 *
1015 * This may cause the windows below it also to be redrawn (when clearing the
1016 * screen or scrolling lines).
1017 *
1018 * How the window is redrawn depends on wp->w_redr_type. Each type also
1019 * implies the one below it.
1020 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001021 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1023 * INVERTED redraw the changed part of the Visual area
1024 * INVERTED_ALL redraw the whole Visual area
1025 * VALID 1. scroll up/down to adjust for a changed w_topline
1026 * 2. update lines at the top when scrolled down
1027 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001028 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * b_mod_top and b_mod_bot.
1030 * - if wp->w_redraw_top non-zero, redraw lines between
1031 * wp->w_redraw_top and wp->w_redr_bot.
1032 * - continue redrawing when syntax status is invalid.
1033 * 4. if scrolled up, update lines at the bottom.
1034 * This results in three areas that may need updating:
1035 * top: from first row to top_end (when scrolled down)
1036 * mid: from mid_start to mid_end (update inversion or changed text)
1037 * bot: from bot_start to last row (when scrolled up)
1038 */
1039 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001040win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 buf_T *buf = wp->w_buffer;
1043 int type;
1044 int top_end = 0; /* Below last row of the top area that needs
1045 updating. 0 when no top area updating. */
1046 int mid_start = 999;/* first row of the mid area that needs
1047 updating. 999 when no mid area updating. */
1048 int mid_end = 0; /* Below last row of the mid area that needs
1049 updating. 0 when no mid area updating. */
1050 int bot_start = 999;/* first row of the bot area that needs
1051 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 int scrolled_down = FALSE; /* TRUE when scrolled down when
1053 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001055 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 int top_to_mod = FALSE; /* redraw above mod_top */
1057#endif
1058
1059 int row; /* current window row to display */
1060 linenr_T lnum; /* current buffer lnum to display */
1061 int idx; /* current index in w_lines[] */
1062 int srow; /* starting row of the current line */
1063
1064 int eof = FALSE; /* if TRUE, we hit the end of the file */
1065 int didline = FALSE; /* if TRUE, we finished the last line */
1066 int i;
1067 long j;
1068 static int recursive = FALSE; /* being called recursively */
1069 int old_botline = wp->w_botline;
1070#ifdef FEAT_FOLDING
1071 long fold_count;
1072#endif
1073#ifdef FEAT_SYN_HL
1074 /* remember what happened to the previous line, to know if
1075 * check_visual_highlight() can be used */
1076#define DID_NONE 1 /* didn't update a line */
1077#define DID_LINE 2 /* updated a normal line */
1078#define DID_FOLD 3 /* updated a folded line */
1079 int did_update = DID_NONE;
1080 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1081#endif
1082 linenr_T mod_top = 0;
1083 linenr_T mod_bot = 0;
1084#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1085 int save_got_int;
1086#endif
1087
1088 type = wp->w_redr_type;
1089
1090 if (type == NOT_VALID)
1091 {
1092#ifdef FEAT_WINDOWS
1093 wp->w_redr_status = TRUE;
1094#endif
1095 wp->w_lines_valid = 0;
1096 }
1097
1098 /* Window is zero-height: nothing to draw. */
1099 if (wp->w_height == 0)
1100 {
1101 wp->w_redr_type = 0;
1102 return;
1103 }
1104
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001105#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /* Window is zero-width: Only need to draw the separator. */
1107 if (wp->w_width == 0)
1108 {
1109 /* draw the vertical separator right of this window */
1110 draw_vsep_win(wp, 0);
1111 wp->w_redr_type = 0;
1112 return;
1113 }
1114#endif
1115
1116#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001117 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118#endif
1119
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001120#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001121 /* Force redraw when width of 'number' or 'relativenumber' column
1122 * changes. */
1123 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001124 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001125 {
1126 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001127 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001128 }
1129 else
1130#endif
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1133 {
1134 /*
1135 * When there are both inserted/deleted lines and specific lines to be
1136 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1137 * everything (only happens when redrawing is off for while).
1138 */
1139 type = NOT_VALID;
1140 }
1141 else
1142 {
1143 /*
1144 * Set mod_top to the first line that needs displaying because of
1145 * changes. Set mod_bot to the first line after the changes.
1146 */
1147 mod_top = wp->w_redraw_top;
1148 if (wp->w_redraw_bot != 0)
1149 mod_bot = wp->w_redraw_bot + 1;
1150 else
1151 mod_bot = 0;
1152 wp->w_redraw_top = 0; /* reset for next time */
1153 wp->w_redraw_bot = 0;
1154 if (buf->b_mod_set)
1155 {
1156 if (mod_top == 0 || mod_top > buf->b_mod_top)
1157 {
1158 mod_top = buf->b_mod_top;
1159#ifdef FEAT_SYN_HL
1160 /* Need to redraw lines above the change that may be included
1161 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001162 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001164 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (mod_top < 1)
1166 mod_top = 1;
1167 }
1168#endif
1169 }
1170 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1171 mod_bot = buf->b_mod_bot;
1172
1173#ifdef FEAT_SEARCH_EXTRA
1174 /* When 'hlsearch' is on and using a multi-line search pattern, a
1175 * change in one line may make the Search highlighting in a
1176 * previous line invalid. Simple solution: redraw all visible
1177 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001178 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001180 if (search_hl.rm.regprog != NULL
1181 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001183 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001184 {
1185 cur = wp->w_match_head;
1186 while (cur != NULL)
1187 {
1188 if (cur->match.regprog != NULL
1189 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001190 {
1191 top_to_mod = TRUE;
1192 break;
1193 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001194 cur = cur->next;
1195 }
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#endif
1198 }
1199#ifdef FEAT_FOLDING
1200 if (mod_top != 0 && hasAnyFolding(wp))
1201 {
1202 linenr_T lnumt, lnumb;
1203
1204 /*
1205 * A change in a line can cause lines above it to become folded or
1206 * unfolded. Find the top most buffer line that may be affected.
1207 * If the line was previously folded and displayed, get the first
1208 * line of that fold. If the line is folded now, get the first
1209 * folded line. Use the minimum of these two.
1210 */
1211
1212 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1213 * the line below it. If there is no valid entry, use w_topline.
1214 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1215 * to this line. If there is no valid entry, use MAXLNUM. */
1216 lnumt = wp->w_topline;
1217 lnumb = MAXLNUM;
1218 for (i = 0; i < wp->w_lines_valid; ++i)
1219 if (wp->w_lines[i].wl_valid)
1220 {
1221 if (wp->w_lines[i].wl_lastlnum < mod_top)
1222 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1223 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1224 {
1225 lnumb = wp->w_lines[i].wl_lnum;
1226 /* When there is a fold column it might need updating
1227 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001228 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 ++lnumb;
1230 }
1231 }
1232
1233 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1234 if (mod_top > lnumt)
1235 mod_top = lnumt;
1236
1237 /* Now do the same for the bottom line (one above mod_bot). */
1238 --mod_bot;
1239 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1240 ++mod_bot;
1241 if (mod_bot < lnumb)
1242 mod_bot = lnumb;
1243 }
1244#endif
1245
1246 /* When a change starts above w_topline and the end is below
1247 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001248 * If the end of the change is above w_topline: do like no change was
1249 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (mod_top != 0 && mod_top < wp->w_topline)
1251 {
1252 if (mod_bot > wp->w_topline)
1253 mod_top = wp->w_topline;
1254#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 top_end = 1;
1257#endif
1258 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001259
1260 /* When line numbers are displayed need to redraw all lines below
1261 * inserted/deleted lines. */
1262 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1263 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
1266 /*
1267 * When only displaying the lines at the top, set top_end. Used when
1268 * window has scrolled down for msg_scrolled.
1269 */
1270 if (type == REDRAW_TOP)
1271 {
1272 j = 0;
1273 for (i = 0; i < wp->w_lines_valid; ++i)
1274 {
1275 j += wp->w_lines[i].wl_size;
1276 if (j >= wp->w_upd_rows)
1277 {
1278 top_end = j;
1279 break;
1280 }
1281 }
1282 if (top_end == 0)
1283 /* not found (cannot happen?): redraw everything */
1284 type = NOT_VALID;
1285 else
1286 /* top area defined, the rest is VALID */
1287 type = VALID;
1288 }
1289
Bram Moolenaar367329b2007-08-30 11:53:22 +00001290 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001291 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1292 * non-zero and thus not FALSE) will indicate that screenclear() was not
1293 * called. */
1294 if (screen_cleared)
1295 screen_cleared = MAYBE;
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 /*
1298 * If there are no changes on the screen that require a complete redraw,
1299 * handle three cases:
1300 * 1: we are off the top of the screen by a few lines: scroll down
1301 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1302 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1303 * w_lines[] that needs updating.
1304 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001305 if ((type == VALID || type == SOME_VALID
1306 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#ifdef FEAT_DIFF
1308 && !wp->w_botfill && !wp->w_old_botfill
1309#endif
1310 )
1311 {
1312 if (mod_top != 0 && wp->w_topline == mod_top)
1313 {
1314 /*
1315 * w_topline is the first changed line, the scrolling will be done
1316 * further down.
1317 */
1318 }
1319 else if (wp->w_lines[0].wl_valid
1320 && (wp->w_topline < wp->w_lines[0].wl_lnum
1321#ifdef FEAT_DIFF
1322 || (wp->w_topline == wp->w_lines[0].wl_lnum
1323 && wp->w_topfill > wp->w_old_topfill)
1324#endif
1325 ))
1326 {
1327 /*
1328 * New topline is above old topline: May scroll down.
1329 */
1330#ifdef FEAT_FOLDING
1331 if (hasAnyFolding(wp))
1332 {
1333 linenr_T ln;
1334
1335 /* count the number of lines we are off, counting a sequence
1336 * of folded lines as one */
1337 j = 0;
1338 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1339 {
1340 ++j;
1341 if (j >= wp->w_height - 2)
1342 break;
1343 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1344 }
1345 }
1346 else
1347#endif
1348 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1349 if (j < wp->w_height - 2) /* not too far off */
1350 {
1351 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1352#ifdef FEAT_DIFF
1353 /* insert extra lines for previously invisible filler lines */
1354 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1355 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1356 - wp->w_old_topfill;
1357#endif
1358 if (i < wp->w_height - 2) /* less than a screen off */
1359 {
1360 /*
1361 * Try to insert the correct number of lines.
1362 * If not the last window, delete the lines at the bottom.
1363 * win_ins_lines may fail when the terminal can't do it.
1364 */
1365 if (i > 0)
1366 check_for_delay(FALSE);
1367 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1368 {
1369 if (wp->w_lines_valid != 0)
1370 {
1371 /* Need to update rows that are new, stop at the
1372 * first one that scrolled down. */
1373 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /* Move the entries that were scrolled, disable
1377 * the entries for the lines to be redrawn. */
1378 if ((wp->w_lines_valid += j) > wp->w_height)
1379 wp->w_lines_valid = wp->w_height;
1380 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1381 wp->w_lines[idx] = wp->w_lines[idx - j];
1382 while (idx >= 0)
1383 wp->w_lines[idx--].wl_valid = FALSE;
1384 }
1385 }
1386 else
1387 mid_start = 0; /* redraw all lines */
1388 }
1389 else
1390 mid_start = 0; /* redraw all lines */
1391 }
1392 else
1393 mid_start = 0; /* redraw all lines */
1394 }
1395 else
1396 {
1397 /*
1398 * New topline is at or below old topline: May scroll up.
1399 * When topline didn't change, find first entry in w_lines[] that
1400 * needs updating.
1401 */
1402
1403 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1404 j = -1;
1405 row = 0;
1406 for (i = 0; i < wp->w_lines_valid; i++)
1407 {
1408 if (wp->w_lines[i].wl_valid
1409 && wp->w_lines[i].wl_lnum == wp->w_topline)
1410 {
1411 j = i;
1412 break;
1413 }
1414 row += wp->w_lines[i].wl_size;
1415 }
1416 if (j == -1)
1417 {
1418 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1419 * lines */
1420 mid_start = 0;
1421 }
1422 else
1423 {
1424 /*
1425 * Try to delete the correct number of lines.
1426 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1427 */
1428#ifdef FEAT_DIFF
1429 /* If the topline didn't change, delete old filler lines,
1430 * otherwise delete filler lines of the new topline... */
1431 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1432 row += wp->w_old_topfill;
1433 else
1434 row += diff_check_fill(wp, wp->w_topline);
1435 /* ... but don't delete new filler lines. */
1436 row -= wp->w_topfill;
1437#endif
1438 if (row > 0)
1439 {
1440 check_for_delay(FALSE);
1441 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1442 bot_start = wp->w_height - row;
1443 else
1444 mid_start = 0; /* redraw all lines */
1445 }
1446 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1447 {
1448 /*
1449 * Skip the lines (below the deleted lines) that are still
1450 * valid and don't need redrawing. Copy their info
1451 * upwards, to compensate for the deleted lines. Set
1452 * bot_start to the first row that needs redrawing.
1453 */
1454 bot_start = 0;
1455 idx = 0;
1456 for (;;)
1457 {
1458 wp->w_lines[idx] = wp->w_lines[j];
1459 /* stop at line that didn't fit, unless it is still
1460 * valid (no lines deleted) */
1461 if (row > 0 && bot_start + row
1462 + (int)wp->w_lines[j].wl_size > wp->w_height)
1463 {
1464 wp->w_lines_valid = idx + 1;
1465 break;
1466 }
1467 bot_start += wp->w_lines[idx++].wl_size;
1468
1469 /* stop at the last valid entry in w_lines[].wl_size */
1470 if (++j >= wp->w_lines_valid)
1471 {
1472 wp->w_lines_valid = idx;
1473 break;
1474 }
1475 }
1476#ifdef FEAT_DIFF
1477 /* Correct the first entry for filler lines at the top
1478 * when it won't get updated below. */
1479 if (wp->w_p_diff && bot_start > 0)
1480 wp->w_lines[0].wl_size =
1481 plines_win_nofill(wp, wp->w_topline, TRUE)
1482 + wp->w_topfill;
1483#endif
1484 }
1485 }
1486 }
1487
1488 /* When starting redraw in the first line, redraw all lines. When
1489 * there is only one window it's probably faster to clear the screen
1490 * first. */
1491 if (mid_start == 0)
1492 {
1493 mid_end = wp->w_height;
1494 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001495 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001496 /* Clear the screen when it was not done by win_del_lines() or
1497 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1498 * then. */
1499 if (screen_cleared != TRUE)
1500 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001501#ifdef FEAT_WINDOWS
1502 /* The screen was cleared, redraw the tab pages line. */
1503 if (redraw_tabline)
1504 draw_tabline();
1505#endif
1506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001508
1509 /* When win_del_lines() or win_ins_lines() caused the screen to be
1510 * cleared (only happens for the first window) or when screenclear()
1511 * was called directly above, "must_redraw" will have been set to
1512 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1513 if (screen_cleared == TRUE)
1514 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 }
1516 else
1517 {
1518 /* Not VALID or INVERTED: redraw all lines. */
1519 mid_start = 0;
1520 mid_end = wp->w_height;
1521 }
1522
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001523 if (type == SOME_VALID)
1524 {
1525 /* SOME_VALID: redraw all lines. */
1526 mid_start = 0;
1527 mid_end = wp->w_height;
1528 type = NOT_VALID;
1529 }
1530
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 /* check if we are updating or removing the inverted part */
1532 if ((VIsual_active && buf == curwin->w_buffer)
1533 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1534 {
1535 linenr_T from, to;
1536
1537 if (VIsual_active)
1538 {
1539 if (VIsual_active
1540 && (VIsual_mode != wp->w_old_visual_mode
1541 || type == INVERTED_ALL))
1542 {
1543 /*
1544 * If the type of Visual selection changed, redraw the whole
1545 * selection. Also when the ownership of the X selection is
1546 * gained or lost.
1547 */
1548 if (curwin->w_cursor.lnum < VIsual.lnum)
1549 {
1550 from = curwin->w_cursor.lnum;
1551 to = VIsual.lnum;
1552 }
1553 else
1554 {
1555 from = VIsual.lnum;
1556 to = curwin->w_cursor.lnum;
1557 }
1558 /* redraw more when the cursor moved as well */
1559 if (wp->w_old_cursor_lnum < from)
1560 from = wp->w_old_cursor_lnum;
1561 if (wp->w_old_cursor_lnum > to)
1562 to = wp->w_old_cursor_lnum;
1563 if (wp->w_old_visual_lnum < from)
1564 from = wp->w_old_visual_lnum;
1565 if (wp->w_old_visual_lnum > to)
1566 to = wp->w_old_visual_lnum;
1567 }
1568 else
1569 {
1570 /*
1571 * Find the line numbers that need to be updated: The lines
1572 * between the old cursor position and the current cursor
1573 * position. Also check if the Visual position changed.
1574 */
1575 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1576 {
1577 from = curwin->w_cursor.lnum;
1578 to = wp->w_old_cursor_lnum;
1579 }
1580 else
1581 {
1582 from = wp->w_old_cursor_lnum;
1583 to = curwin->w_cursor.lnum;
1584 if (from == 0) /* Visual mode just started */
1585 from = to;
1586 }
1587
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001588 if (VIsual.lnum != wp->w_old_visual_lnum
1589 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 if (wp->w_old_visual_lnum < from
1592 && wp->w_old_visual_lnum != 0)
1593 from = wp->w_old_visual_lnum;
1594 if (wp->w_old_visual_lnum > to)
1595 to = wp->w_old_visual_lnum;
1596 if (VIsual.lnum < from)
1597 from = VIsual.lnum;
1598 if (VIsual.lnum > to)
1599 to = VIsual.lnum;
1600 }
1601 }
1602
1603 /*
1604 * If in block mode and changed column or curwin->w_curswant:
1605 * update all lines.
1606 * First compute the actual start and end column.
1607 */
1608 if (VIsual_mode == Ctrl_V)
1609 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001610 colnr_T fromc, toc;
1611#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1612 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar404406a2014-10-09 13:24:43 +02001614 if (curwin->w_p_lbr)
1615 ve_flags = VE_ALL;
1616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001618#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1619 ve_flags = save_ve_flags;
1620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 ++toc;
1622 if (curwin->w_curswant == MAXCOL)
1623 toc = MAXCOL;
1624
1625 if (fromc != wp->w_old_cursor_fcol
1626 || toc != wp->w_old_cursor_lcol)
1627 {
1628 if (from > VIsual.lnum)
1629 from = VIsual.lnum;
1630 if (to < VIsual.lnum)
1631 to = VIsual.lnum;
1632 }
1633 wp->w_old_cursor_fcol = fromc;
1634 wp->w_old_cursor_lcol = toc;
1635 }
1636 }
1637 else
1638 {
1639 /* Use the line numbers of the old Visual area. */
1640 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1641 {
1642 from = wp->w_old_cursor_lnum;
1643 to = wp->w_old_visual_lnum;
1644 }
1645 else
1646 {
1647 from = wp->w_old_visual_lnum;
1648 to = wp->w_old_cursor_lnum;
1649 }
1650 }
1651
1652 /*
1653 * There is no need to update lines above the top of the window.
1654 */
1655 if (from < wp->w_topline)
1656 from = wp->w_topline;
1657
1658 /*
1659 * If we know the value of w_botline, use it to restrict the update to
1660 * the lines that are visible in the window.
1661 */
1662 if (wp->w_valid & VALID_BOTLINE)
1663 {
1664 if (from >= wp->w_botline)
1665 from = wp->w_botline - 1;
1666 if (to >= wp->w_botline)
1667 to = wp->w_botline - 1;
1668 }
1669
1670 /*
1671 * Find the minimal part to be updated.
1672 * Watch out for scrolling that made entries in w_lines[] invalid.
1673 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1674 * top_end; need to redraw from top_end to the "to" line.
1675 * A middle mouse click with a Visual selection may change the text
1676 * above the Visual area and reset wl_valid, do count these for
1677 * mid_end (in srow).
1678 */
1679 if (mid_start > 0)
1680 {
1681 lnum = wp->w_topline;
1682 idx = 0;
1683 srow = 0;
1684 if (scrolled_down)
1685 mid_start = top_end;
1686 else
1687 mid_start = 0;
1688 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1689 {
1690 if (wp->w_lines[idx].wl_valid)
1691 mid_start += wp->w_lines[idx].wl_size;
1692 else if (!scrolled_down)
1693 srow += wp->w_lines[idx].wl_size;
1694 ++idx;
1695# ifdef FEAT_FOLDING
1696 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1697 lnum = wp->w_lines[idx].wl_lnum;
1698 else
1699# endif
1700 ++lnum;
1701 }
1702 srow += mid_start;
1703 mid_end = wp->w_height;
1704 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1705 {
1706 if (wp->w_lines[idx].wl_valid
1707 && wp->w_lines[idx].wl_lnum >= to + 1)
1708 {
1709 /* Only update until first row of this line */
1710 mid_end = srow;
1711 break;
1712 }
1713 srow += wp->w_lines[idx].wl_size;
1714 }
1715 }
1716 }
1717
1718 if (VIsual_active && buf == curwin->w_buffer)
1719 {
1720 wp->w_old_visual_mode = VIsual_mode;
1721 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1722 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001723 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 wp->w_old_curswant = curwin->w_curswant;
1725 }
1726 else
1727 {
1728 wp->w_old_visual_mode = 0;
1729 wp->w_old_cursor_lnum = 0;
1730 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001731 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1735 /* reset got_int, otherwise regexp won't work */
1736 save_got_int = got_int;
1737 got_int = 0;
1738#endif
1739#ifdef FEAT_FOLDING
1740 win_foldinfo.fi_level = 0;
1741#endif
1742
1743 /*
1744 * Update all the window rows.
1745 */
1746 idx = 0; /* first entry in w_lines[].wl_size */
1747 row = 0;
1748 srow = 0;
1749 lnum = wp->w_topline; /* first line shown in window */
1750 for (;;)
1751 {
1752 /* stop updating when reached the end of the window (check for _past_
1753 * the end of the window is at the end of the loop) */
1754 if (row == wp->w_height)
1755 {
1756 didline = TRUE;
1757 break;
1758 }
1759
1760 /* stop updating when hit the end of the file */
1761 if (lnum > buf->b_ml.ml_line_count)
1762 {
1763 eof = TRUE;
1764 break;
1765 }
1766
1767 /* Remember the starting row of the line that is going to be dealt
1768 * with. It is used further down when the line doesn't fit. */
1769 srow = row;
1770
1771 /*
1772 * Update a line when it is in an area that needs updating, when it
1773 * has changes or w_lines[idx] is invalid.
1774 * bot_start may be halfway a wrapped line after using
1775 * win_del_lines(), check if the current line includes it.
1776 * When syntax folding is being used, the saved syntax states will
1777 * already have been updated, we can't see where the syntax state is
1778 * the same again, just update until the end of the window.
1779 */
1780 if (row < top_end
1781 || (row >= mid_start && row < mid_end)
1782#ifdef FEAT_SEARCH_EXTRA
1783 || top_to_mod
1784#endif
1785 || idx >= wp->w_lines_valid
1786 || (row + wp->w_lines[idx].wl_size > bot_start)
1787 || (mod_top != 0
1788 && (lnum == mod_top
1789 || (lnum >= mod_top
1790 && (lnum < mod_bot
1791#ifdef FEAT_SYN_HL
1792 || did_update == DID_FOLD
1793 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001794 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 && (
1796# ifdef FEAT_FOLDING
1797 (foldmethodIsSyntax(wp)
1798 && hasAnyFolding(wp)) ||
1799# endif
1800 syntax_check_changed(lnum)))
1801#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001802#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001803 /* match in fixed position might need redraw
1804 * if lines were inserted or deleted */
1805 || (wp->w_match_head != NULL
1806 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 )))))
1809 {
1810#ifdef FEAT_SEARCH_EXTRA
1811 if (lnum == mod_top)
1812 top_to_mod = FALSE;
1813#endif
1814
1815 /*
1816 * When at start of changed lines: May scroll following lines
1817 * up or down to minimize redrawing.
1818 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001819 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821 if (lnum == mod_top
1822 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001823 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 int old_rows = 0;
1826 int new_rows = 0;
1827 int xtra_rows;
1828 linenr_T l;
1829
1830 /* Count the old number of window rows, using w_lines[], which
1831 * should still contain the sizes for the lines as they are
1832 * currently displayed. */
1833 for (i = idx; i < wp->w_lines_valid; ++i)
1834 {
1835 /* Only valid lines have a meaningful wl_lnum. Invalid
1836 * lines are part of the changed area. */
1837 if (wp->w_lines[i].wl_valid
1838 && wp->w_lines[i].wl_lnum == mod_bot)
1839 break;
1840 old_rows += wp->w_lines[i].wl_size;
1841#ifdef FEAT_FOLDING
1842 if (wp->w_lines[i].wl_valid
1843 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1844 {
1845 /* Must have found the last valid entry above mod_bot.
1846 * Add following invalid entries. */
1847 ++i;
1848 while (i < wp->w_lines_valid
1849 && !wp->w_lines[i].wl_valid)
1850 old_rows += wp->w_lines[i++].wl_size;
1851 break;
1852 }
1853#endif
1854 }
1855
1856 if (i >= wp->w_lines_valid)
1857 {
1858 /* We can't find a valid line below the changed lines,
1859 * need to redraw until the end of the window.
1860 * Inserting/deleting lines has no use. */
1861 bot_start = 0;
1862 }
1863 else
1864 {
1865 /* Able to count old number of rows: Count new window
1866 * rows, and may insert/delete lines */
1867 j = idx;
1868 for (l = lnum; l < mod_bot; ++l)
1869 {
1870#ifdef FEAT_FOLDING
1871 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1872 ++new_rows;
1873 else
1874#endif
1875#ifdef FEAT_DIFF
1876 if (l == wp->w_topline)
1877 new_rows += plines_win_nofill(wp, l, TRUE)
1878 + wp->w_topfill;
1879 else
1880#endif
1881 new_rows += plines_win(wp, l, TRUE);
1882 ++j;
1883 if (new_rows > wp->w_height - row - 2)
1884 {
1885 /* it's getting too much, must redraw the rest */
1886 new_rows = 9999;
1887 break;
1888 }
1889 }
1890 xtra_rows = new_rows - old_rows;
1891 if (xtra_rows < 0)
1892 {
1893 /* May scroll text up. If there is not enough
1894 * remaining text or scrolling fails, must redraw the
1895 * rest. If scrolling works, must redraw the text
1896 * below the scrolled text. */
1897 if (row - xtra_rows >= wp->w_height - 2)
1898 mod_bot = MAXLNUM;
1899 else
1900 {
1901 check_for_delay(FALSE);
1902 if (win_del_lines(wp, row,
1903 -xtra_rows, FALSE, FALSE) == FAIL)
1904 mod_bot = MAXLNUM;
1905 else
1906 bot_start = wp->w_height + xtra_rows;
1907 }
1908 }
1909 else if (xtra_rows > 0)
1910 {
1911 /* May scroll text down. If there is not enough
1912 * remaining text of scrolling fails, must redraw the
1913 * rest. */
1914 if (row + xtra_rows >= wp->w_height - 2)
1915 mod_bot = MAXLNUM;
1916 else
1917 {
1918 check_for_delay(FALSE);
1919 if (win_ins_lines(wp, row + old_rows,
1920 xtra_rows, FALSE, FALSE) == FAIL)
1921 mod_bot = MAXLNUM;
1922 else if (top_end > row + old_rows)
1923 /* Scrolled the part at the top that requires
1924 * updating down. */
1925 top_end += xtra_rows;
1926 }
1927 }
1928
1929 /* When not updating the rest, may need to move w_lines[]
1930 * entries. */
1931 if (mod_bot != MAXLNUM && i != j)
1932 {
1933 if (j < i)
1934 {
1935 int x = row + new_rows;
1936
1937 /* move entries in w_lines[] upwards */
1938 for (;;)
1939 {
1940 /* stop at last valid entry in w_lines[] */
1941 if (i >= wp->w_lines_valid)
1942 {
1943 wp->w_lines_valid = j;
1944 break;
1945 }
1946 wp->w_lines[j] = wp->w_lines[i];
1947 /* stop at a line that won't fit */
1948 if (x + (int)wp->w_lines[j].wl_size
1949 > wp->w_height)
1950 {
1951 wp->w_lines_valid = j + 1;
1952 break;
1953 }
1954 x += wp->w_lines[j++].wl_size;
1955 ++i;
1956 }
1957 if (bot_start > x)
1958 bot_start = x;
1959 }
1960 else /* j > i */
1961 {
1962 /* move entries in w_lines[] downwards */
1963 j -= i;
1964 wp->w_lines_valid += j;
1965 if (wp->w_lines_valid > wp->w_height)
1966 wp->w_lines_valid = wp->w_height;
1967 for (i = wp->w_lines_valid; i - j >= idx; --i)
1968 wp->w_lines[i] = wp->w_lines[i - j];
1969
1970 /* The w_lines[] entries for inserted lines are
1971 * now invalid, but wl_size may be used above.
1972 * Reset to zero. */
1973 while (i >= idx)
1974 {
1975 wp->w_lines[i].wl_size = 0;
1976 wp->w_lines[i--].wl_valid = FALSE;
1977 }
1978 }
1979 }
1980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983#ifdef FEAT_FOLDING
1984 /*
1985 * When lines are folded, display one line for all of them.
1986 * Otherwise, display normally (can be several display lines when
1987 * 'wrap' is on).
1988 */
1989 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1990 if (fold_count != 0)
1991 {
1992 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1993 ++row;
1994 --fold_count;
1995 wp->w_lines[idx].wl_folded = TRUE;
1996 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1997# ifdef FEAT_SYN_HL
1998 did_update = DID_FOLD;
1999# endif
2000 }
2001 else
2002#endif
2003 if (idx < wp->w_lines_valid
2004 && wp->w_lines[idx].wl_valid
2005 && wp->w_lines[idx].wl_lnum == lnum
2006 && lnum > wp->w_topline
2007 && !(dy_flags & DY_LASTLINE)
2008 && srow + wp->w_lines[idx].wl_size > wp->w_height
2009#ifdef FEAT_DIFF
2010 && diff_check_fill(wp, lnum) == 0
2011#endif
2012 )
2013 {
2014 /* This line is not going to fit. Don't draw anything here,
2015 * will draw "@ " lines below. */
2016 row = wp->w_height + 1;
2017 }
2018 else
2019 {
2020#ifdef FEAT_SEARCH_EXTRA
2021 prepare_search_hl(wp, lnum);
2022#endif
2023#ifdef FEAT_SYN_HL
2024 /* Let the syntax stuff know we skipped a few lines. */
2025 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002026 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 syntax_end_parsing(syntax_last_parsed + 1);
2028#endif
2029
2030 /*
2031 * Display one line.
2032 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002033 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035#ifdef FEAT_FOLDING
2036 wp->w_lines[idx].wl_folded = FALSE;
2037 wp->w_lines[idx].wl_lastlnum = lnum;
2038#endif
2039#ifdef FEAT_SYN_HL
2040 did_update = DID_LINE;
2041 syntax_last_parsed = lnum;
2042#endif
2043 }
2044
2045 wp->w_lines[idx].wl_lnum = lnum;
2046 wp->w_lines[idx].wl_valid = TRUE;
2047 if (row > wp->w_height) /* past end of screen */
2048 {
2049 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002050 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2052 ++idx;
2053 break;
2054 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002055 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 wp->w_lines[idx].wl_size = row - srow;
2057 ++idx;
2058#ifdef FEAT_FOLDING
2059 lnum += fold_count + 1;
2060#else
2061 ++lnum;
2062#endif
2063 }
2064 else
2065 {
2066 /* This line does not need updating, advance to the next one */
2067 row += wp->w_lines[idx++].wl_size;
2068 if (row > wp->w_height) /* past end of screen */
2069 break;
2070#ifdef FEAT_FOLDING
2071 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2072#else
2073 ++lnum;
2074#endif
2075#ifdef FEAT_SYN_HL
2076 did_update = DID_NONE;
2077#endif
2078 }
2079
2080 if (lnum > buf->b_ml.ml_line_count)
2081 {
2082 eof = TRUE;
2083 break;
2084 }
2085 }
2086 /*
2087 * End of loop over all window lines.
2088 */
2089
2090
2091 if (idx > wp->w_lines_valid)
2092 wp->w_lines_valid = idx;
2093
2094#ifdef FEAT_SYN_HL
2095 /*
2096 * Let the syntax stuff know we stop parsing here.
2097 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002098 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 syntax_end_parsing(syntax_last_parsed + 1);
2100#endif
2101
2102 /*
2103 * If we didn't hit the end of the file, and we didn't finish the last
2104 * line we were working on, then the line didn't fit.
2105 */
2106 wp->w_empty_rows = 0;
2107#ifdef FEAT_DIFF
2108 wp->w_filler_rows = 0;
2109#endif
2110 if (!eof && !didline)
2111 {
2112 if (lnum == wp->w_topline)
2113 {
2114 /*
2115 * Single line that does not fit!
2116 * Don't overwrite it, it can be edited.
2117 */
2118 wp->w_botline = lnum + 1;
2119 }
2120#ifdef FEAT_DIFF
2121 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2122 {
2123 /* Window ends in filler lines. */
2124 wp->w_botline = lnum;
2125 wp->w_filler_rows = wp->w_height - srow;
2126 }
2127#endif
2128 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2129 {
2130 /*
2131 * Last line isn't finished: Display "@@@" at the end.
2132 */
2133 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2134 W_WINROW(wp) + wp->w_height,
2135 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2136 '@', '@', hl_attr(HLF_AT));
2137 set_empty_rows(wp, srow);
2138 wp->w_botline = lnum;
2139 }
2140 else
2141 {
2142 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2143 wp->w_botline = lnum;
2144 }
2145 }
2146 else
2147 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002148#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 draw_vsep_win(wp, row);
2150#endif
2151 if (eof) /* we hit the end of the file */
2152 {
2153 wp->w_botline = buf->b_ml.ml_line_count + 1;
2154#ifdef FEAT_DIFF
2155 j = diff_check_fill(wp, wp->w_botline);
2156 if (j > 0 && !wp->w_botfill)
2157 {
2158 /*
2159 * Display filler lines at the end of the file
2160 */
2161 if (char2cells(fill_diff) > 1)
2162 i = '-';
2163 else
2164 i = fill_diff;
2165 if (row + j > wp->w_height)
2166 j = wp->w_height - row;
2167 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2168 row += j;
2169 }
2170#endif
2171 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002172 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 wp->w_botline = lnum;
2174
2175 /* make sure the rest of the screen is blank */
2176 /* put '~'s on rows that aren't part of the file. */
2177 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2178 }
2179
2180 /* Reset the type of redrawing required, the window has been updated. */
2181 wp->w_redr_type = 0;
2182#ifdef FEAT_DIFF
2183 wp->w_old_topfill = wp->w_topfill;
2184 wp->w_old_botfill = wp->w_botfill;
2185#endif
2186
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002187 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 /*
2190 * There is a trick with w_botline. If we invalidate it on each
2191 * change that might modify it, this will cause a lot of expensive
2192 * calls to plines() in update_topline() each time. Therefore the
2193 * value of w_botline is often approximated, and this value is used to
2194 * compute the value of w_topline. If the value of w_botline was
2195 * wrong, check that the value of w_topline is correct (cursor is on
2196 * the visible part of the text). If it's not, we need to redraw
2197 * again. Mostly this just means scrolling up a few lines, so it
2198 * doesn't look too bad. Only do this for the current window (where
2199 * changes are relevant).
2200 */
2201 wp->w_valid |= VALID_BOTLINE;
2202 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2203 {
2204 recursive = TRUE;
2205 curwin->w_valid &= ~VALID_TOPLINE;
2206 update_topline(); /* may invalidate w_botline again */
2207 if (must_redraw != 0)
2208 {
2209 /* Don't update for changes in buffer again. */
2210 i = curbuf->b_mod_set;
2211 curbuf->b_mod_set = FALSE;
2212 win_update(curwin);
2213 must_redraw = 0;
2214 curbuf->b_mod_set = i;
2215 }
2216 recursive = FALSE;
2217 }
2218 }
2219
2220#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2221 /* restore got_int, unless CTRL-C was hit while redrawing */
2222 if (!got_int)
2223 got_int = save_got_int;
2224#endif
2225}
2226
2227#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002228static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230/*
2231 * Return TRUE when window "wp" has a column to draw signs in.
2232 */
2233 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002234draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235{
2236 return (wp->w_buffer->b_signlist != NULL
2237# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002238 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239# endif
2240 );
2241}
2242#endif
2243
2244/*
2245 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2246 * as the filler character.
2247 */
2248 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002249win_draw_end(
2250 win_T *wp,
2251 int c1,
2252 int c2,
2253 int row,
2254 int endrow,
2255 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2258 int n = 0;
2259# define FDC_OFF n
2260#else
2261# define FDC_OFF 0
2262#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002263#ifdef FEAT_FOLDING
2264 int fdc = compute_foldcolumn(wp, 0);
2265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267#ifdef FEAT_RIGHTLEFT
2268 if (wp->w_p_rl)
2269 {
2270 /* No check for cmdline window: should never be right-left. */
2271# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002272 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 if (n > 0)
2275 {
2276 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002277 if (n > W_WIDTH(wp))
2278 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2280 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2281 ' ', ' ', hl_attr(HLF_FC));
2282 }
2283# endif
2284# ifdef FEAT_SIGNS
2285 if (draw_signcolumn(wp))
2286 {
2287 int nn = n + 2;
2288
2289 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002290 if (nn > W_WIDTH(wp))
2291 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2293 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2294 ' ', ' ', hl_attr(HLF_SC));
2295 n = nn;
2296 }
2297# endif
2298 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2299 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2300 c2, c2, hl_attr(hl));
2301 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2302 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2303 c1, c2, hl_attr(hl));
2304 }
2305 else
2306#endif
2307 {
2308#ifdef FEAT_CMDWIN
2309 if (cmdwin_type != 0 && wp == curwin)
2310 {
2311 /* draw the cmdline character in the leftmost column */
2312 n = 1;
2313 if (n > wp->w_width)
2314 n = wp->w_width;
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2317 cmdwin_type, ' ', hl_attr(HLF_AT));
2318 }
2319#endif
2320#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002321 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002323 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 /* draw the fold column at the left */
2326 if (nn > W_WIDTH(wp))
2327 nn = W_WIDTH(wp);
2328 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2329 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2330 ' ', ' ', hl_attr(HLF_FC));
2331 n = nn;
2332 }
2333#endif
2334#ifdef FEAT_SIGNS
2335 if (draw_signcolumn(wp))
2336 {
2337 int nn = n + 2;
2338
2339 /* draw the sign column after the fold column */
2340 if (nn > W_WIDTH(wp))
2341 nn = W_WIDTH(wp);
2342 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2343 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2344 ' ', ' ', hl_attr(HLF_SC));
2345 n = nn;
2346 }
2347#endif
2348 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2349 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2350 c1, c2, hl_attr(hl));
2351 }
2352 set_empty_rows(wp, row);
2353}
2354
Bram Moolenaar1a384422010-07-14 19:53:30 +02002355#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002356static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002357
2358/*
2359 * Advance **color_cols and return TRUE when there are columns to draw.
2360 */
2361 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002362advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002363{
2364 while (**color_cols >= 0 && vcol > **color_cols)
2365 ++*color_cols;
2366 return (**color_cols >= 0);
2367}
2368#endif
2369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#ifdef FEAT_FOLDING
2371/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002372 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2373 * space is available for window "wp", minus "col".
2374 */
2375 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002376compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002377{
2378 int fdc = wp->w_p_fdc;
2379 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2380 int wwidth = W_WIDTH(wp);
2381
2382 if (fdc > wwidth - (col + wmw))
2383 fdc = wwidth - (col + wmw);
2384 return fdc;
2385}
2386
2387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 * Display one folded line.
2389 */
2390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002391fold_line(
2392 win_T *wp,
2393 long fold_count,
2394 foldinfo_T *foldinfo,
2395 linenr_T lnum,
2396 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 char_u buf[51];
2399 pos_T *top, *bot;
2400 linenr_T lnume = lnum + fold_count - 1;
2401 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002402 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 int col;
2405 int txtcol;
2406 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002407 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Build the fold line:
2410 * 1. Add the cmdwin_type for the command-line window
2411 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002412 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 * 4. Compose the text
2414 * 5. Add the text
2415 * 6. set highlighting for the Visual area an other text
2416 */
2417 col = 0;
2418
2419 /*
2420 * 1. Add the cmdwin_type for the command-line window
2421 * Ignores 'rightleft', this window is never right-left.
2422 */
2423#ifdef FEAT_CMDWIN
2424 if (cmdwin_type != 0 && wp == curwin)
2425 {
2426 ScreenLines[off] = cmdwin_type;
2427 ScreenAttrs[off] = hl_attr(HLF_AT);
2428#ifdef FEAT_MBYTE
2429 if (enc_utf8)
2430 ScreenLinesUC[off] = 0;
2431#endif
2432 ++col;
2433 }
2434#endif
2435
2436 /*
2437 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002438 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002440 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (fdc > 0)
2442 {
2443 fill_foldcolumn(buf, wp, TRUE, lnum);
2444#ifdef FEAT_RIGHTLEFT
2445 if (wp->w_p_rl)
2446 {
2447 int i;
2448
2449 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2450 hl_attr(HLF_FC));
2451 /* reverse the fold column */
2452 for (i = 0; i < fdc; ++i)
2453 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2454 }
2455 else
2456#endif
2457 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2458 col += fdc;
2459 }
2460
2461#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002462# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2463 for (ri = 0; ri < l; ++ri) \
2464 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2465 else \
2466 for (ri = 0; ri < l; ++ri) \
2467 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002469# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2470 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#endif
2472
Bram Moolenaar64486672010-05-16 15:46:46 +02002473 /* Set all attributes of the 'number' or 'relativenumber' column and the
2474 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002475 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477#ifdef FEAT_SIGNS
2478 /* If signs are being displayed, add two spaces. */
2479 if (draw_signcolumn(wp))
2480 {
2481 len = W_WIDTH(wp) - col;
2482 if (len > 0)
2483 {
2484 if (len > 2)
2485 len = 2;
2486# ifdef FEAT_RIGHTLEFT
2487 if (wp->w_p_rl)
2488 /* the line number isn't reversed */
2489 copy_text_attr(off + W_WIDTH(wp) - len - col,
2490 (char_u *)" ", len, hl_attr(HLF_FL));
2491 else
2492# endif
2493 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2494 col += len;
2495 }
2496 }
2497#endif
2498
2499 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002500 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002502 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
2504 len = W_WIDTH(wp) - col;
2505 if (len > 0)
2506 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002507 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002508 long num;
2509 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002510
2511 if (len > w + 1)
2512 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002513
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002514 if (wp->w_p_nu && !wp->w_p_rnu)
2515 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002516 num = (long)lnum;
2517 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002518 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002519 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002520 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002521 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002522 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002523 /* 'number' + 'relativenumber': cursor line shows absolute
2524 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002525 num = lnum;
2526 fmt = "%-*ld ";
2527 }
2528 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002529
Bram Moolenaar700e7342013-01-30 12:31:36 +01002530 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531#ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 /* the line number isn't reversed */
2534 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2535 hl_attr(HLF_FL));
2536 else
2537#endif
2538 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2539 col += len;
2540 }
2541 }
2542
2543 /*
2544 * 4. Compose the folded-line string with 'foldtext', if set.
2545 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002546 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 txtcol = col; /* remember where text starts */
2549
2550 /*
2551 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2552 * Right-left text is put in columns 0 - number-col, normal text is put
2553 * in columns number-col - window-width.
2554 */
2555#ifdef FEAT_MBYTE
2556 if (has_mbyte)
2557 {
2558 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002559 int u8c, u8cc[MAX_MCO];
2560 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 int idx;
2562 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002563 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564# ifdef FEAT_ARABIC
2565 int prev_c = 0; /* previous Arabic character */
2566 int prev_c1 = 0; /* first composing char for prev_c */
2567# endif
2568
2569# ifdef FEAT_RIGHTLEFT
2570 if (wp->w_p_rl)
2571 idx = off;
2572 else
2573# endif
2574 idx = off + col;
2575
2576 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2577 for (p = text; *p != NUL; )
2578 {
2579 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002580 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (col + cells > W_WIDTH(wp)
2582# ifdef FEAT_RIGHTLEFT
2583 - (wp->w_p_rl ? col : 0)
2584# endif
2585 )
2586 break;
2587 ScreenLines[idx] = *p;
2588 if (enc_utf8)
2589 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 u8c = utfc_ptr2char(p, u8cc);
2591 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 ScreenLinesUC[idx] = 0;
2594#ifdef FEAT_ARABIC
2595 prev_c = u8c;
2596#endif
2597 }
2598 else
2599 {
2600#ifdef FEAT_ARABIC
2601 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2602 {
2603 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 int pc, pc1, nc;
2605 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int firstbyte = *p;
2607
2608 /* The idea of what is the previous and next
2609 * character depends on 'rightleft'. */
2610 if (wp->w_p_rl)
2611 {
2612 pc = prev_c;
2613 pc1 = prev_c1;
2614 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002615 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617 else
2618 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002619 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 prev_c = u8c;
2624
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 pc, pc1, nc);
2627 ScreenLines[idx] = firstbyte;
2628 }
2629 else
2630 prev_c = u8c;
2631#endif
2632 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002633#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (u8c >= 0x10000)
2635 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2636 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002639 for (i = 0; i < Screen_mco; ++i)
2640 {
2641 ScreenLinesC[i][idx] = u8cc[i];
2642 if (u8cc[i] == 0)
2643 break;
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
2646 if (cells > 1)
2647 ScreenLines[idx + 1] = 0;
2648 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002649 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2650 /* double-byte single width character */
2651 ScreenLines2[idx] = p[1];
2652 else if (cells > 1)
2653 /* double-width character */
2654 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 col += cells;
2656 idx += cells;
2657 p += c_len;
2658 }
2659 }
2660 else
2661#endif
2662 {
2663 len = (int)STRLEN(text);
2664 if (len > W_WIDTH(wp) - col)
2665 len = W_WIDTH(wp) - col;
2666 if (len > 0)
2667 {
2668#ifdef FEAT_RIGHTLEFT
2669 if (wp->w_p_rl)
2670 STRNCPY(current_ScreenLine, text, len);
2671 else
2672#endif
2673 STRNCPY(current_ScreenLine + col, text, len);
2674 col += len;
2675 }
2676 }
2677
2678 /* Fill the rest of the line with the fold filler */
2679#ifdef FEAT_RIGHTLEFT
2680 if (wp->w_p_rl)
2681 col -= txtcol;
2682#endif
2683 while (col < W_WIDTH(wp)
2684#ifdef FEAT_RIGHTLEFT
2685 - (wp->w_p_rl ? txtcol : 0)
2686#endif
2687 )
2688 {
2689#ifdef FEAT_MBYTE
2690 if (enc_utf8)
2691 {
2692 if (fill_fold >= 0x80)
2693 {
2694 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002695 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 else
2698 ScreenLinesUC[off + col] = 0;
2699 }
2700#endif
2701 ScreenLines[off + col++] = fill_fold;
2702 }
2703
2704 if (text != buf)
2705 vim_free(text);
2706
2707 /*
2708 * 6. set highlighting for the Visual area an other text.
2709 * If all folded lines are in the Visual area, highlight the line.
2710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2712 {
2713 if (ltoreq(curwin->w_cursor, VIsual))
2714 {
2715 /* Visual is after curwin->w_cursor */
2716 top = &curwin->w_cursor;
2717 bot = &VIsual;
2718 }
2719 else
2720 {
2721 /* Visual is before curwin->w_cursor */
2722 top = &VIsual;
2723 bot = &curwin->w_cursor;
2724 }
2725 if (lnum >= top->lnum
2726 && lnume <= bot->lnum
2727 && (VIsual_mode != 'v'
2728 || ((lnum > top->lnum
2729 || (lnum == top->lnum
2730 && top->col == 0))
2731 && (lnume < bot->lnum
2732 || (lnume == bot->lnum
2733 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002734 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
2736 if (VIsual_mode == Ctrl_V)
2737 {
2738 /* Visual block mode: highlight the chars part of the block */
2739 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2740 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002741 if (wp->w_old_cursor_lcol != MAXCOL
2742 && wp->w_old_cursor_lcol + txtcol
2743 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 len = wp->w_old_cursor_lcol;
2745 else
2746 len = W_WIDTH(wp) - txtcol;
2747 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002748 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 }
2751 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002754 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002759#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002760 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2761 if (wp->w_p_cc_cols)
2762 {
2763 int i = 0;
2764 int j = wp->w_p_cc_cols[i];
2765 int old_txtcol = txtcol;
2766
2767 while (j > -1)
2768 {
2769 txtcol += j;
2770 if (wp->w_p_wrap)
2771 txtcol -= wp->w_skipcol;
2772 else
2773 txtcol -= wp->w_leftcol;
2774 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2775 ScreenAttrs[off + txtcol] = hl_combine_attr(
2776 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2777 txtcol = old_txtcol;
2778 j = wp->w_p_cc_cols[++i];
2779 }
2780 }
2781
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002782 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002783 if (wp->w_p_cuc)
2784 {
2785 txtcol += wp->w_virtcol;
2786 if (wp->w_p_wrap)
2787 txtcol -= wp->w_skipcol;
2788 else
2789 txtcol -= wp->w_leftcol;
2790 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2791 ScreenAttrs[off + txtcol] = hl_combine_attr(
2792 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2793 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795
2796 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2797 (int)W_WIDTH(wp), FALSE);
2798
2799 /*
2800 * Update w_cline_height and w_cline_folded if the cursor line was
2801 * updated (saves a call to plines() later).
2802 */
2803 if (wp == curwin
2804 && lnum <= curwin->w_cursor.lnum
2805 && lnume >= curwin->w_cursor.lnum)
2806 {
2807 curwin->w_cline_row = row;
2808 curwin->w_cline_height = 1;
2809 curwin->w_cline_folded = TRUE;
2810 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2811 }
2812}
2813
2814/*
2815 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2816 */
2817 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002818copy_text_attr(
2819 int off,
2820 char_u *buf,
2821 int len,
2822 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002824 int i;
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 mch_memmove(ScreenLines + off, buf, (size_t)len);
2827# ifdef FEAT_MBYTE
2828 if (enc_utf8)
2829 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2830# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002831 for (i = 0; i < len; ++i)
2832 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833}
2834
2835/*
2836 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002837 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 */
2839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002840fill_foldcolumn(
2841 char_u *p,
2842 win_T *wp,
2843 int closed, /* TRUE of FALSE */
2844 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 int i = 0;
2847 int level;
2848 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002849 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002850 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002853 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 level = win_foldinfo.fi_level;
2856 if (level > 0)
2857 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002858 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002859 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002860
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 /* If the column is too narrow, we start at the lowest level that
2862 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002863 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (first_level < 1)
2865 first_level = 1;
2866
Bram Moolenaar1c934292015-01-27 16:39:29 +01002867 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 {
2869 if (win_foldinfo.fi_lnum == lnum
2870 && first_level + i >= win_foldinfo.fi_low_level)
2871 p[i] = '-';
2872 else if (first_level == 1)
2873 p[i] = '|';
2874 else if (first_level + i <= 9)
2875 p[i] = '0' + first_level + i;
2876 else
2877 p[i] = '>';
2878 if (first_level + i == level)
2879 break;
2880 }
2881 }
2882 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002883 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884}
2885#endif /* FEAT_FOLDING */
2886
2887/*
2888 * Display line "lnum" of window 'wp' on the screen.
2889 * Start at row "startrow", stop when "endrow" is reached.
2890 * wp->w_virtcol needs to be valid.
2891 *
2892 * Return the number of last row the line occupies.
2893 */
2894 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002895win_line(
2896 win_T *wp,
2897 linenr_T lnum,
2898 int startrow,
2899 int endrow,
2900 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 int col; /* visual column on screen */
2903 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2904 int c = 0; /* init for GCC */
2905 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002906#ifdef FEAT_LINEBREAK
2907 long vcol_sbr = -1; /* virtual column after showbreak */
2908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 long vcol_prev = -1; /* "vcol" of previous character */
2910 char_u *line; /* current line */
2911 char_u *ptr; /* current position in "line" */
2912 int row; /* row in the window, excl w_winrow */
2913 int screen_row; /* row on the screen, incl w_winrow */
2914
2915 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2916 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002917 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002918 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 int c_extra = NUL; /* extra chars, all the same */
2920 int extra_attr = 0; /* attributes when n_extra != 0 */
2921 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2922 displaying lcs_eol at end-of-line */
2923 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2924 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2925
2926 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2927 int saved_n_extra = 0;
2928 char_u *saved_p_extra = NULL;
2929 int saved_c_extra = 0;
2930 int saved_char_attr = 0;
2931
2932 int n_attr = 0; /* chars with special attr */
2933 int saved_attr2 = 0; /* char_attr saved for n_attr */
2934 int n_attr3 = 0; /* chars with overruling special attr */
2935 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2936
2937 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2938
2939 int fromcol, tocol; /* start/end of inverting */
2940 int fromcol_prev = -2; /* start of inverting after cursor */
2941 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002943 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 pos_T pos;
2945 long v;
2946
2947 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002948 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2950 in this line */
2951 int attr = 0; /* attributes for area highlighting */
2952 int area_attr = 0; /* attributes desired by highlighting */
2953 int search_attr = 0; /* attributes desired by 'hlsearch' */
2954#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002955 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int syntax_attr = 0; /* attributes desired by syntax */
2957 int has_syntax = FALSE; /* this buffer has syntax highl. */
2958 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002959 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002960 int draw_color_col = FALSE; /* highlight colorcolumn */
2961 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002962#endif
2963#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002964 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002965# define SPWORDLEN 150
2966 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002967 int nextlinecol = 0; /* column where nextline[] starts */
2968 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002969 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002970 int spell_attr = 0; /* attributes desired by spelling */
2971 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002972 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2973 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002974 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002975 static int cap_col = -1; /* column to check for Cap word */
2976 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002977 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#endif
2979 int extra_check; /* has syntax or linebreak */
2980#ifdef FEAT_MBYTE
2981 int multi_attr = 0; /* attributes desired by multibyte */
2982 int mb_l = 1; /* multi-byte byte length */
2983 int mb_c = 0; /* decoded multi-byte character */
2984 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002985 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986#endif
2987#ifdef FEAT_DIFF
2988 int filler_lines; /* nr of filler lines to be drawn */
2989 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002990 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 int change_start = MAXCOL; /* first col of changed area */
2992 int change_end = -1; /* last col of changed area */
2993#endif
2994 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2995#ifdef FEAT_LINEBREAK
2996 int need_showbreak = FALSE;
2997#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002998#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2999 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003001 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
3003#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003004 matchitem_T *cur; /* points to the match list */
3005 match_T *shl; /* points to search_hl or a match */
3006 int shl_flag; /* flag to indicate whether search_hl
3007 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003008 int pos_inprogress; /* marks that position match search is
3009 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003010 int prevcol_hl_flag; /* flag to indicate whether prevcol
3011 equals startcol of search_hl or one
3012 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013#endif
3014#ifdef FEAT_ARABIC
3015 int prev_c = 0; /* previous Arabic character */
3016 int prev_c1 = 0; /* first composing char for prev_c */
3017#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003018#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003019 int did_line_attr = 0;
3020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 /* draw_state: items that are drawn in sequence: */
3023#define WL_START 0 /* nothing done yet */
3024#ifdef FEAT_CMDWIN
3025# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3026#else
3027# define WL_CMDLINE WL_START
3028#endif
3029#ifdef FEAT_FOLDING
3030# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3031#else
3032# define WL_FOLD WL_CMDLINE
3033#endif
3034#ifdef FEAT_SIGNS
3035# define WL_SIGN WL_FOLD + 1 /* column for signs */
3036#else
3037# define WL_SIGN WL_FOLD /* column for signs */
3038#endif
3039#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003040#ifdef FEAT_LINEBREAK
3041# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003043# define WL_BRI WL_NR
3044#endif
3045#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3046# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3047#else
3048# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#endif
3050#define WL_LINE WL_SBR + 1 /* text in the line */
3051 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003052#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 int feedback_col = 0;
3054 int feedback_old_attr = -1;
3055#endif
3056
Bram Moolenaar860cae12010-06-05 23:22:07 +02003057#ifdef FEAT_CONCEAL
3058 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003059 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003060 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003061 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003062 int is_concealing = FALSE;
3063 int boguscols = 0; /* nonexistent columns added to force
3064 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003065 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003066 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003067 int match_conc = 0; /* cchar for match functions */
3068 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003069 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003070# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003071# define FIX_FOR_BOGUSCOLS \
3072 { \
3073 n_extra += vcol_off; \
3074 vcol -= vcol_off; \
3075 vcol_off = 0; \
3076 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003077 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003078 boguscols = 0; \
3079 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003080#else
3081# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 if (startrow > endrow) /* past the end already! */
3085 return startrow;
3086
3087 row = startrow;
3088 screen_row = row + W_WINROW(wp);
3089
3090 /*
3091 * To speed up the loop below, set extra_check when there is linebreak,
3092 * trailing white space and/or syntax processing to be done.
3093 */
3094#ifdef FEAT_LINEBREAK
3095 extra_check = wp->w_p_lbr;
3096#else
3097 extra_check = 0;
3098#endif
3099#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003100 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
3102 /* Prepare for syntax highlighting in this line. When there is an
3103 * error, stop syntax highlighting. */
3104 save_did_emsg = did_emsg;
3105 did_emsg = FALSE;
3106 syntax_start(wp, lnum);
3107 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003108 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 else
3110 {
3111 did_emsg = save_did_emsg;
3112 has_syntax = TRUE;
3113 extra_check = TRUE;
3114 }
3115 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003116
3117 /* Check for columns to display for 'colorcolumn'. */
3118 color_cols = wp->w_p_cc_cols;
3119 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003120 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003121#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003122
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003123#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003124 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003125 && *wp->w_s->b_p_spl != NUL
3126 && wp->w_s->b_langp.ga_len > 0
3127 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003128 {
3129 /* Prepare for spell checking. */
3130 has_spell = TRUE;
3131 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003132
3133 /* Get the start of the next line, so that words that wrap to the next
3134 * line are found too: "et<line-break>al.".
3135 * Trick: skip a few chars for C/shell/Vim comments */
3136 nextline[SPWORDLEN] = NUL;
3137 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3138 {
3139 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3140 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3141 }
3142
3143 /* When a word wrapped from the previous line the start of the current
3144 * line is valid. */
3145 if (lnum == checked_lnum)
3146 cur_checked_col = checked_col;
3147 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003148
3149 /* When there was a sentence end in the previous line may require a
3150 * word starting with capital in this line. In line 1 always check
3151 * the first word. */
3152 if (lnum != capcol_lnum)
3153 cap_col = -1;
3154 if (lnum == 1)
3155 cap_col = 0;
3156 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#endif
3159
3160 /*
3161 * handle visual active in this window
3162 */
3163 fromcol = -10;
3164 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3166 {
3167 /* Visual is after curwin->w_cursor */
3168 if (ltoreq(curwin->w_cursor, VIsual))
3169 {
3170 top = &curwin->w_cursor;
3171 bot = &VIsual;
3172 }
3173 else /* Visual is before curwin->w_cursor */
3174 {
3175 top = &VIsual;
3176 bot = &curwin->w_cursor;
3177 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003178 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (VIsual_mode == Ctrl_V) /* block mode */
3180 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003181 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 fromcol = wp->w_old_cursor_fcol;
3184 tocol = wp->w_old_cursor_lcol;
3185 }
3186 }
3187 else /* non-block mode */
3188 {
3189 if (lnum > top->lnum && lnum <= bot->lnum)
3190 fromcol = 0;
3191 else if (lnum == top->lnum)
3192 {
3193 if (VIsual_mode == 'V') /* linewise */
3194 fromcol = 0;
3195 else
3196 {
3197 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3198 if (gchar_pos(top) == NUL)
3199 tocol = fromcol + 1;
3200 }
3201 }
3202 if (VIsual_mode != 'V' && lnum == bot->lnum)
3203 {
3204 if (*p_sel == 'e' && bot->col == 0
3205#ifdef FEAT_VIRTUALEDIT
3206 && bot->coladd == 0
3207#endif
3208 )
3209 {
3210 fromcol = -10;
3211 tocol = MAXCOL;
3212 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003213 else if (bot->col == MAXCOL)
3214 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
3216 {
3217 pos = *bot;
3218 if (*p_sel == 'e')
3219 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3220 else
3221 {
3222 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3223 ++tocol;
3224 }
3225 }
3226 }
3227 }
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /* Check if the character under the cursor should not be inverted */
3230 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003231#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 )
3235 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 /* if inverting in this line set area_highlighting */
3238 if (fromcol >= 0)
3239 {
3240 area_highlighting = TRUE;
3241 attr = hl_attr(HLF_V);
3242#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003243 if ((clip_star.available && !clip_star.owned
3244 && clip_isautosel_star())
3245 || (clip_plus.available && !clip_plus.owned
3246 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 attr = hl_attr(HLF_VNC);
3248#endif
3249 }
3250 }
3251
3252 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003253 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003255 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 && wp == curwin
3257 && lnum >= curwin->w_cursor.lnum
3258 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3259 {
3260 if (lnum == curwin->w_cursor.lnum)
3261 getvcol(curwin, &(curwin->w_cursor),
3262 (colnr_T *)&fromcol, NULL, NULL);
3263 else
3264 fromcol = 0;
3265 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3266 {
3267 pos.lnum = lnum;
3268 pos.col = search_match_endcol;
3269 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3270 }
3271 else
3272 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003273 /* do at least one character; happens when past end of line */
3274 if (fromcol == tocol)
3275 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 area_highlighting = TRUE;
3277 attr = hl_attr(HLF_I);
3278 }
3279
3280#ifdef FEAT_DIFF
3281 filler_lines = diff_check(wp, lnum);
3282 if (filler_lines < 0)
3283 {
3284 if (filler_lines == -1)
3285 {
3286 if (diff_find_change(wp, lnum, &change_start, &change_end))
3287 diff_hlf = HLF_ADD; /* added line */
3288 else if (change_start == 0)
3289 diff_hlf = HLF_TXD; /* changed text */
3290 else
3291 diff_hlf = HLF_CHD; /* changed line */
3292 }
3293 else
3294 diff_hlf = HLF_ADD; /* added line */
3295 filler_lines = 0;
3296 area_highlighting = TRUE;
3297 }
3298 if (lnum == wp->w_topline)
3299 filler_lines = wp->w_topfill;
3300 filler_todo = filler_lines;
3301#endif
3302
3303#ifdef LINE_ATTR
3304# ifdef FEAT_SIGNS
3305 /* If this line has a sign with line highlighting set line_attr. */
3306 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3307 if (v != 0)
3308 line_attr = sign_get_attr((int)v, TRUE);
3309# endif
3310# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3311 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003312 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 line_attr = hl_attr(HLF_L);
3314# endif
3315 if (line_attr != 0)
3316 area_highlighting = TRUE;
3317#endif
3318
3319 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3320 ptr = line;
3321
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003322#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003323 if (has_spell)
3324 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003325 /* For checking first word with a capital skip white space. */
3326 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003327 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003328
Bram Moolenaar30abd282005-06-22 22:35:10 +00003329 /* To be able to spell-check over line boundaries copy the end of the
3330 * current line into nextline[]. Above the start of the next line was
3331 * copied to nextline[SPWORDLEN]. */
3332 if (nextline[SPWORDLEN] == NUL)
3333 {
3334 /* No next line or it is empty. */
3335 nextlinecol = MAXCOL;
3336 nextline_idx = 0;
3337 }
3338 else
3339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003340 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003341 if (v < SPWORDLEN)
3342 {
3343 /* Short line, use it completely and append the start of the
3344 * next line. */
3345 nextlinecol = 0;
3346 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003347 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003348 nextline_idx = v + 1;
3349 }
3350 else
3351 {
3352 /* Long line, use only the last SPWORDLEN bytes. */
3353 nextlinecol = v - SPWORDLEN;
3354 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3355 nextline_idx = SPWORDLEN + 1;
3356 }
3357 }
3358 }
3359#endif
3360
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003361 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003363 if (lcs_space || lcs_trail)
3364 extra_check = TRUE;
3365 /* find start of trailing whitespace */
3366 if (lcs_trail)
3367 {
3368 trailcol = (colnr_T)STRLEN(ptr);
3369 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3370 --trailcol;
3371 trailcol += (colnr_T) (ptr - line);
3372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374
3375 /*
3376 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3377 * first character to be displayed.
3378 */
3379 if (wp->w_p_wrap)
3380 v = wp->w_skipcol;
3381 else
3382 v = wp->w_leftcol;
3383 if (v > 0)
3384 {
3385#ifdef FEAT_MBYTE
3386 char_u *prev_ptr = ptr;
3387#endif
3388 while (vcol < v && *ptr != NUL)
3389 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003390 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 vcol += c;
3392#ifdef FEAT_MBYTE
3393 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003395 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003398 /* When:
3399 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003400 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003401 * - 'virtualedit' is set, or
3402 * - the visual mode is active,
3403 * the end of the line may be before the start of the displayed part.
3404 */
3405 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003406#ifdef FEAT_SYN_HL
3407 wp->w_p_cuc || draw_color_col ||
3408#endif
3409#ifdef FEAT_VIRTUALEDIT
3410 virtual_active() ||
3411#endif
3412 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
3417 /* Handle a character that's not completely on the screen: Put ptr at
3418 * that character but skip the first few screen characters. */
3419 if (vcol > v)
3420 {
3421 vcol -= c;
3422#ifdef FEAT_MBYTE
3423 ptr = prev_ptr;
3424#else
3425 --ptr;
3426#endif
3427 n_skip = v - vcol;
3428 }
3429
3430 /*
3431 * Adjust for when the inverted text is before the screen,
3432 * and when the start of the inverted text is before the screen.
3433 */
3434 if (tocol <= vcol)
3435 fromcol = 0;
3436 else if (fromcol >= 0 && fromcol < vcol)
3437 fromcol = vcol;
3438
3439#ifdef FEAT_LINEBREAK
3440 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3441 if (wp->w_p_wrap)
3442 need_showbreak = TRUE;
3443#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003444#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003445 /* When spell checking a word we need to figure out the start of the
3446 * word and if it's badly spelled or not. */
3447 if (has_spell)
3448 {
3449 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003450 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003451 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003452
3453 pos = wp->w_cursor;
3454 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003455 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003456 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003457
3458 /* spell_move_to() may call ml_get() and make "line" invalid */
3459 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3460 ptr = line + linecol;
3461
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003462 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003463 {
3464 /* no bad word found at line start, don't check until end of a
3465 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003466 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003467 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003468 }
3469 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003470 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003471 /* bad word found, use attributes until end of word */
3472 word_end = wp->w_cursor.col + len + 1;
3473
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003474 /* Turn index into actual attributes. */
3475 if (spell_hlf != HLF_COUNT)
3476 spell_attr = highlight_attr[spell_hlf];
3477 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003478 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003479
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003480# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003481 /* Need to restart syntax highlighting for this line. */
3482 if (has_syntax)
3483 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003484# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003485 }
3486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489 /*
3490 * Correct highlighting for cursor that can't be disabled.
3491 * Avoids having to check this for each character.
3492 */
3493 if (fromcol >= 0)
3494 {
3495 if (noinvcur)
3496 {
3497 if ((colnr_T)fromcol == wp->w_virtcol)
3498 {
3499 /* highlighting starts at cursor, let it start just after the
3500 * cursor */
3501 fromcol_prev = fromcol;
3502 fromcol = -1;
3503 }
3504 else if ((colnr_T)fromcol < wp->w_virtcol)
3505 /* restart highlighting after the cursor */
3506 fromcol_prev = wp->w_virtcol;
3507 }
3508 if (fromcol >= tocol)
3509 fromcol = -1;
3510 }
3511
3512#ifdef FEAT_SEARCH_EXTRA
3513 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003514 * Handle highlighting the last used search pattern and matches.
3515 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003517 cur = wp->w_match_head;
3518 shl_flag = FALSE;
3519 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003521 if (shl_flag == FALSE)
3522 {
3523 shl = &search_hl;
3524 shl_flag = TRUE;
3525 }
3526 else
3527 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003528 shl->startcol = MAXCOL;
3529 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003531 v = (long)(ptr - line);
3532 if (cur != NULL)
3533 cur->pos.cur = 0;
3534 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3535
3536 /* Need to get the line again, a multi-line regexp may have made it
3537 * invalid. */
3538 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3539 ptr = line + v;
3540
3541 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003543 if (shl->lnum == lnum)
3544 shl->startcol = shl->rm.startpos[0].col;
3545 else
3546 shl->startcol = 0;
3547 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3548 - shl->rm.startpos[0].lnum)
3549 shl->endcol = shl->rm.endpos[0].col;
3550 else
3551 shl->endcol = MAXCOL;
3552 /* Highlight one character for an empty match. */
3553 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003556 if (has_mbyte && line[shl->endcol] != NUL)
3557 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003560 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003562 if ((long)shl->startcol < v) /* match at leftcol */
3563 {
3564 shl->attr_cur = shl->attr;
3565 search_attr = shl->attr;
3566 }
3567 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003569 if (shl != &search_hl && cur != NULL)
3570 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572#endif
3573
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003574#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003575 /* Cursor line highlighting for 'cursorline' in the current window. Not
3576 * when Visual mode is active, because it's not clear what is selected
3577 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003578 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003579 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003580 {
3581 line_attr = hl_attr(HLF_CUL);
3582 area_highlighting = TRUE;
3583 }
3584#endif
3585
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003586 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 col = 0;
3588#ifdef FEAT_RIGHTLEFT
3589 if (wp->w_p_rl)
3590 {
3591 /* Rightleft window: process the text in the normal direction, but put
3592 * it in current_ScreenLine[] from right to left. Start at the
3593 * rightmost column of the window. */
3594 col = W_WIDTH(wp) - 1;
3595 off += col;
3596 }
3597#endif
3598
3599 /*
3600 * Repeat for the whole displayed line.
3601 */
3602 for (;;)
3603 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003604#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003605 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /* Skip this quickly when working on the text. */
3608 if (draw_state != WL_LINE)
3609 {
3610#ifdef FEAT_CMDWIN
3611 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3612 {
3613 draw_state = WL_CMDLINE;
3614 if (cmdwin_type != 0 && wp == curwin)
3615 {
3616 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003618 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 char_attr = hl_attr(HLF_AT);
3620 }
3621 }
3622#endif
3623
3624#ifdef FEAT_FOLDING
3625 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3626 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003627 int fdc = compute_foldcolumn(wp, 0);
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003630 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 /* Draw the 'foldcolumn'. */
3633 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003634 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003636 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 c_extra = NUL;
3638 char_attr = hl_attr(HLF_FC);
3639 }
3640 }
3641#endif
3642
3643#ifdef FEAT_SIGNS
3644 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3645 {
3646 draw_state = WL_SIGN;
3647 /* Show the sign column when there are any signs in this
3648 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003649 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003651 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003653 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654# endif
3655
3656 /* Draw two cells with the sign value or blank. */
3657 c_extra = ' ';
3658 char_attr = hl_attr(HLF_SC);
3659 n_extra = 2;
3660
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003661 if (row == startrow
3662#ifdef FEAT_DIFF
3663 + filler_lines && filler_todo <= 0
3664#endif
3665 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3668 SIGN_TEXT);
3669# ifdef FEAT_SIGN_ICONS
3670 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3671 SIGN_ICON);
3672 if (gui.in_use && icon_sign != 0)
3673 {
3674 /* Use the image in this position. */
3675 c_extra = SIGN_BYTE;
3676# ifdef FEAT_NETBEANS_INTG
3677 if (buf_signcount(wp->w_buffer, lnum) > 1)
3678 c_extra = MULTISIGN_BYTE;
3679# endif
3680 char_attr = icon_sign;
3681 }
3682 else
3683# endif
3684 if (text_sign != 0)
3685 {
3686 p_extra = sign_get_text(text_sign);
3687 if (p_extra != NULL)
3688 {
3689 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003690 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 char_attr = sign_get_attr(text_sign, FALSE);
3693 }
3694 }
3695 }
3696 }
3697#endif
3698
3699 if (draw_state == WL_NR - 1 && n_extra == 0)
3700 {
3701 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003702 /* Display the absolute or relative line number. After the
3703 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3704 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 && (row == startrow
3706#ifdef FEAT_DIFF
3707 + filler_lines
3708#endif
3709 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3710 {
3711 /* Draw the line number (empty space after wrapping). */
3712 if (row == startrow
3713#ifdef FEAT_DIFF
3714 + filler_lines
3715#endif
3716 )
3717 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003718 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003719 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003720
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003721 if (wp->w_p_nu && !wp->w_p_rnu)
3722 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003723 num = (long)lnum;
3724 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003725 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003726 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003727 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003728 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003729 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003730 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003731 num = lnum;
3732 fmt = "%-*ld ";
3733 }
3734 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003735
Bram Moolenaar700e7342013-01-30 12:31:36 +01003736 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003737 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (wp->w_skipcol > 0)
3739 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3740 *p_extra = '-';
3741#ifdef FEAT_RIGHTLEFT
3742 if (wp->w_p_rl) /* reverse line numbers */
3743 rl_mirror(extra);
3744#endif
3745 p_extra = extra;
3746 c_extra = NUL;
3747 }
3748 else
3749 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003750 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003752#ifdef FEAT_SYN_HL
3753 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003754 * the current line differently.
3755 * TODO: Can we use CursorLine instead of CursorLineNr
3756 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003757 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003758 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003759 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763
Bram Moolenaar597a4222014-06-25 14:39:50 +02003764#ifdef FEAT_LINEBREAK
3765 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3766 && n_extra == 0 && *p_sbr != NUL)
3767 /* draw indent after showbreak value */
3768 draw_state = WL_BRI;
3769 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3770 /* After the showbreak, draw the breakindent */
3771 draw_state = WL_BRI - 1;
3772
3773 /* draw 'breakindent': indent wrapped text accordingly */
3774 if (draw_state == WL_BRI - 1 && n_extra == 0)
3775 {
3776 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003777 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003778# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003779 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003780# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003781 )
3782 {
3783 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003784# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003785 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003786 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003787 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003788# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003789 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3790 char_attr = hl_combine_attr(char_attr,
3791 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003792# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003793 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003794# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003795 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003796 c_extra = ' ';
3797 n_extra = get_breakindent_win(wp,
3798 ml_get_buf(wp->w_buffer, lnum, FALSE));
3799 /* Correct end of highlighted area for 'breakindent',
3800 * required when 'linebreak' is also set. */
3801 if (tocol == vcol)
3802 tocol += n_extra;
3803 }
3804 }
3805#endif
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3808 if (draw_state == WL_SBR - 1 && n_extra == 0)
3809 {
3810 draw_state = WL_SBR;
3811# ifdef FEAT_DIFF
3812 if (filler_todo > 0)
3813 {
3814 /* Draw "deleted" diff line(s). */
3815 if (char2cells(fill_diff) > 1)
3816 c_extra = '-';
3817 else
3818 c_extra = fill_diff;
3819# ifdef FEAT_RIGHTLEFT
3820 if (wp->w_p_rl)
3821 n_extra = col + 1;
3822 else
3823# endif
3824 n_extra = W_WIDTH(wp) - col;
3825 char_attr = hl_attr(HLF_DED);
3826 }
3827# endif
3828# ifdef FEAT_LINEBREAK
3829 if (*p_sbr != NUL && need_showbreak)
3830 {
3831 /* Draw 'showbreak' at the start of each broken line. */
3832 p_extra = p_sbr;
3833 c_extra = NUL;
3834 n_extra = (int)STRLEN(p_sbr);
3835 char_attr = hl_attr(HLF_AT);
3836 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003837 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 /* Correct end of highlighted area for 'showbreak',
3839 * required when 'linebreak' is also set. */
3840 if (tocol == vcol)
3841 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003842#ifdef FEAT_SYN_HL
3843 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003844 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003845 char_attr = hl_combine_attr(char_attr,
3846 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849# endif
3850 }
3851#endif
3852
3853 if (draw_state == WL_LINE - 1 && n_extra == 0)
3854 {
3855 draw_state = WL_LINE;
3856 if (saved_n_extra)
3857 {
3858 /* Continue item from end of wrapped line. */
3859 n_extra = saved_n_extra;
3860 c_extra = saved_c_extra;
3861 p_extra = saved_p_extra;
3862 char_attr = saved_char_attr;
3863 }
3864 else
3865 char_attr = 0;
3866 }
3867 }
3868
3869 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003870 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003871 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#ifdef FEAT_DIFF
3873 && filler_todo <= 0
3874#endif
3875 )
3876 {
3877 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3878 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003879 /* Pretend we have finished updating the window. Except when
3880 * 'cursorcolumn' is set. */
3881#ifdef FEAT_SYN_HL
3882 if (wp->w_p_cuc)
3883 row = wp->w_cline_row + wp->w_cline_height;
3884 else
3885#endif
3886 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 break;
3888 }
3889
3890 if (draw_state == WL_LINE && area_highlighting)
3891 {
3892 /* handle Visual or match highlighting in this line */
3893 if (vcol == fromcol
3894#ifdef FEAT_MBYTE
3895 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3896 && (*mb_ptr2cells)(ptr) > 1)
3897#endif
3898 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003899 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 && vcol < tocol))
3901 area_attr = attr; /* start highlighting */
3902 else if (area_attr != 0
3903 && (vcol == tocol
3904 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907#ifdef FEAT_SEARCH_EXTRA
3908 if (!n_extra)
3909 {
3910 /*
3911 * Check for start/end of search pattern match.
3912 * After end, check for start/end of next match.
3913 * When another match, have to check for start again.
3914 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003915 * Do this for 'search_hl' and the match list (ordered by
3916 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003918 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003919 cur = wp->w_match_head;
3920 shl_flag = FALSE;
3921 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003923 if (shl_flag == FALSE
3924 && ((cur != NULL
3925 && cur->priority > SEARCH_HL_PRIORITY)
3926 || cur == NULL))
3927 {
3928 shl = &search_hl;
3929 shl_flag = TRUE;
3930 }
3931 else
3932 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003933 if (cur != NULL)
3934 cur->pos.cur = 0;
3935 pos_inprogress = TRUE;
3936 while (shl->rm.regprog != NULL
3937 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003939 if (shl->startcol != MAXCOL
3940 && v >= (long)shl->startcol
3941 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003943#ifdef FEAT_MBYTE
3944 int tmp_col = v + MB_PTR2LEN(ptr);
3945
3946 if (shl->endcol < tmp_col)
3947 shl->endcol = tmp_col;
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003950#ifdef FEAT_CONCEAL
3951 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3952 == cur->hlg_id)
3953 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02003954 has_match_conc =
3955 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003956 match_conc = cur->conceal_char;
3957 }
3958 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02003959 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003962 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003965#ifdef FEAT_CONCEAL
3966 prev_syntax_id = 0;
3967#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003968 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3969 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003970 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972 /* Need to get the line again, a multi-line regexp
3973 * may have made it invalid. */
3974 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3975 ptr = line + v;
3976
3977 if (shl->lnum == lnum)
3978 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003979 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003981 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003983 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003985 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
3987 /* highlight empty match, try again after
3988 * it */
3989#ifdef FEAT_MBYTE
3990 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003991 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003992 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 else
3994#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003995 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997
3998 /* Loop to check if the match starts at the
3999 * current position */
4000 continue;
4001 }
4002 }
4003 break;
4004 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004005 if (shl != &search_hl && cur != NULL)
4006 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004008
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004009 /* Use attributes from match with highest priority among
4010 * 'search_hl' and the match list. */
4011 search_attr = search_hl.attr_cur;
4012 cur = wp->w_match_head;
4013 shl_flag = FALSE;
4014 while (cur != NULL || shl_flag == FALSE)
4015 {
4016 if (shl_flag == FALSE
4017 && ((cur != NULL
4018 && cur->priority > SEARCH_HL_PRIORITY)
4019 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004020 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004021 shl = &search_hl;
4022 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004023 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004024 else
4025 shl = &cur->hl;
4026 if (shl->attr_cur != 0)
4027 search_attr = shl->attr_cur;
4028 if (shl != &search_hl && cur != NULL)
4029 cur = cur->next;
4030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
4032#endif
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004037 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4038 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004040 if (diff_hlf == HLF_TXD && ptr - line > change_end
4041 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004043 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004044 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4045 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004048
4049 /* Decide which of the highlight attributes to use. */
4050 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004051#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004052 if (area_attr != 0)
4053 char_attr = hl_combine_attr(line_attr, area_attr);
4054 else if (search_attr != 0)
4055 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004056 /* Use line_attr when not in the Visual or 'incsearch' area
4057 * (area_attr may be 0 when "noinvcur" is set). */
4058 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004059 || vcol < fromcol || vcol_prev < fromcol_prev
4060 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004061 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004062#else
4063 if (area_attr != 0)
4064 char_attr = area_attr;
4065 else if (search_attr != 0)
4066 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004067#endif
4068 else
4069 {
4070 attr_pri = FALSE;
4071#ifdef FEAT_SYN_HL
4072 if (has_syntax)
4073 char_attr = syntax_attr;
4074 else
4075#endif
4076 char_attr = 0;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 }
4079
4080 /*
4081 * Get the next character to put on the screen.
4082 */
4083 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004084 * The "p_extra" points to the extra stuff that is inserted to
4085 * represent special characters (non-printable stuff) and other
4086 * things. When all characters are the same, c_extra is used.
4087 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4088 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4090 */
4091 if (n_extra > 0)
4092 {
4093 if (c_extra != NUL)
4094 {
4095 c = c_extra;
4096#ifdef FEAT_MBYTE
4097 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4098 if (enc_utf8 && (*mb_char2len)(c) > 1)
4099 {
4100 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004101 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 else
4105 mb_utf8 = FALSE;
4106#endif
4107 }
4108 else
4109 {
4110 c = *p_extra;
4111#ifdef FEAT_MBYTE
4112 if (has_mbyte)
4113 {
4114 mb_c = c;
4115 if (enc_utf8)
4116 {
4117 /* If the UTF-8 character is more than one byte:
4118 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004119 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 mb_utf8 = FALSE;
4121 if (mb_l > n_extra)
4122 mb_l = 1;
4123 else if (mb_l > 1)
4124 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004125 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004127 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130 else
4131 {
4132 /* if this is a DBCS character, put it in "mb_c" */
4133 mb_l = MB_BYTE2LEN(c);
4134 if (mb_l >= n_extra)
4135 mb_l = 1;
4136 else if (mb_l > 1)
4137 mb_c = (c << 8) + p_extra[1];
4138 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004139 if (mb_l == 0) /* at the NUL at end-of-line */
4140 mb_l = 1;
4141
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /* If a double-width char doesn't fit display a '>' in the
4143 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004144 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145# ifdef FEAT_RIGHTLEFT
4146 wp->w_p_rl ? (col <= 0) :
4147# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004148 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 && (*mb_char2cells)(mb_c) == 2)
4150 {
4151 c = '>';
4152 mb_c = c;
4153 mb_l = 1;
4154 mb_utf8 = FALSE;
4155 multi_attr = hl_attr(HLF_AT);
4156 /* put the pointer back to output the double-width
4157 * character at the start of the next line. */
4158 ++n_extra;
4159 --p_extra;
4160 }
4161 else
4162 {
4163 n_extra -= mb_l - 1;
4164 p_extra += mb_l - 1;
4165 }
4166 }
4167#endif
4168 ++p_extra;
4169 }
4170 --n_extra;
4171 }
4172 else
4173 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004174 if (p_extra_free != NULL)
4175 {
4176 vim_free(p_extra_free);
4177 p_extra_free = NULL;
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * Get a character from the line itself.
4181 */
4182 c = *ptr;
4183#ifdef FEAT_MBYTE
4184 if (has_mbyte)
4185 {
4186 mb_c = c;
4187 if (enc_utf8)
4188 {
4189 /* If the UTF-8 character is more than one byte: Decode it
4190 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004191 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 mb_utf8 = FALSE;
4193 if (mb_l > 1)
4194 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004195 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /* Overlong encoded ASCII or ASCII with composing char
4197 * is displayed normally, except a NUL. */
4198 if (mb_c < 0x80)
4199 c = mb_c;
4200 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004201
4202 /* At start of the line we can have a composing char.
4203 * Draw it as a space with a composing char. */
4204 if (utf_iscomposing(mb_c))
4205 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004206 int i;
4207
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004208 for (i = Screen_mco - 1; i > 0; --i)
4209 u8cc[i] = u8cc[i - 1];
4210 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004211 mb_c = ' ';
4212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214
4215 if ((mb_l == 1 && c >= 0x80)
4216 || (mb_l >= 1 && mb_c == 0)
4217 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004218# ifdef UNICODE16
4219 || mb_c >= 0x10000
4220# endif
4221 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 /*
4224 * Illegal UTF-8 byte: display as <xx>.
4225 * Non-BMP character : display as ? or fullwidth ?.
4226 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004227# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004232# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (wp->w_p_rl) /* reverse */
4234 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004237# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 else if (utf_char2cells(mb_c) != 2)
4239 STRCPY(extra, "?");
4240 else
4241 /* 0xff1f in UTF-8: full-width '?' */
4242 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 p_extra = extra;
4246 c = *p_extra;
4247 mb_c = mb_ptr2char_adv(&p_extra);
4248 mb_utf8 = (c >= 0x80);
4249 n_extra = (int)STRLEN(p_extra);
4250 c_extra = NUL;
4251 if (area_attr == 0 && search_attr == 0)
4252 {
4253 n_attr = n_extra + 1;
4254 extra_attr = hl_attr(HLF_8);
4255 saved_attr2 = char_attr; /* save current attr */
4256 }
4257 }
4258 else if (mb_l == 0) /* at the NUL at end-of-line */
4259 mb_l = 1;
4260#ifdef FEAT_ARABIC
4261 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4262 {
4263 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004264 int pc, pc1, nc;
4265 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /* The idea of what is the previous and next
4268 * character depends on 'rightleft'. */
4269 if (wp->w_p_rl)
4270 {
4271 pc = prev_c;
4272 pc1 = prev_c1;
4273 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004274 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 else
4277 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004278 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 prev_c = mb_c;
4283
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004284 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 else
4287 prev_c = mb_c;
4288#endif
4289 }
4290 else /* enc_dbcs */
4291 {
4292 mb_l = MB_BYTE2LEN(c);
4293 if (mb_l == 0) /* at the NUL at end-of-line */
4294 mb_l = 1;
4295 else if (mb_l > 1)
4296 {
4297 /* We assume a second byte below 32 is illegal.
4298 * Hopefully this is OK for all double-byte encodings!
4299 */
4300 if (ptr[1] >= 32)
4301 mb_c = (c << 8) + ptr[1];
4302 else
4303 {
4304 if (ptr[1] == NUL)
4305 {
4306 /* head byte at end of line */
4307 mb_l = 1;
4308 transchar_nonprint(extra, c);
4309 }
4310 else
4311 {
4312 /* illegal tail byte */
4313 mb_l = 2;
4314 STRCPY(extra, "XX");
4315 }
4316 p_extra = extra;
4317 n_extra = (int)STRLEN(extra) - 1;
4318 c_extra = NUL;
4319 c = *p_extra++;
4320 if (area_attr == 0 && search_attr == 0)
4321 {
4322 n_attr = n_extra + 1;
4323 extra_attr = hl_attr(HLF_8);
4324 saved_attr2 = char_attr; /* save current attr */
4325 }
4326 mb_c = c;
4327 }
4328 }
4329 }
4330 /* If a double-width char doesn't fit display a '>' in the
4331 * last column; the character is displayed at the start of the
4332 * next line. */
4333 if ((
4334# ifdef FEAT_RIGHTLEFT
4335 wp->w_p_rl ? (col <= 0) :
4336# endif
4337 (col >= W_WIDTH(wp) - 1))
4338 && (*mb_char2cells)(mb_c) == 2)
4339 {
4340 c = '>';
4341 mb_c = c;
4342 mb_utf8 = FALSE;
4343 mb_l = 1;
4344 multi_attr = hl_attr(HLF_AT);
4345 /* Put pointer back so that the character will be
4346 * displayed at the start of the next line. */
4347 --ptr;
4348 }
4349 else if (*ptr != NUL)
4350 ptr += mb_l - 1;
4351
4352 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004353 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004354 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004355 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004358 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 c = ' ';
4360 if (area_attr == 0 && search_attr == 0)
4361 {
4362 n_attr = n_extra + 1;
4363 extra_attr = hl_attr(HLF_AT);
4364 saved_attr2 = char_attr; /* save current attr */
4365 }
4366 mb_c = c;
4367 mb_utf8 = FALSE;
4368 mb_l = 1;
4369 }
4370
4371 }
4372#endif
4373 ++ptr;
4374
4375 if (extra_check)
4376 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004377#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004378 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004379#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004380
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004381#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 /* Get syntax attribute, unless still at the start of the line
4383 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004384 v = (long)(ptr - line);
4385 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 /* Get the syntax attribute for the character. If there
4388 * is an error, disable syntax highlighting. */
4389 save_did_emsg = did_emsg;
4390 did_emsg = FALSE;
4391
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004393# ifdef FEAT_SPELL
4394 has_spell ? &can_spell :
4395# endif
4396 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004399 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004400 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004401 has_syntax = FALSE;
4402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 else
4404 did_emsg = save_did_emsg;
4405
4406 /* Need to get the line again, a multi-line regexp may
4407 * have made it invalid. */
4408 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4409 ptr = line + v;
4410
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004411 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004413 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004414 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004415# ifdef FEAT_CONCEAL
4416 /* no concealing past the end of the line, it interferes
4417 * with line highlighting */
4418 if (c == NUL)
4419 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004420 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004421 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004424#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004425
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004426#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004427 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004428 * Only do this when there is no syntax highlighting, the
4429 * @Spell cluster is not used or the current syntax item
4430 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004431 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004432 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004433 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004434# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004435 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004436 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004437# endif
4438 if (c != 0 && (
4439# ifdef FEAT_SYN_HL
4440 !has_syntax ||
4441# endif
4442 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004443 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004444 char_u *prev_ptr, *p;
4445 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004446 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004447# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004448 if (has_mbyte)
4449 {
4450 prev_ptr = ptr - mb_l;
4451 v -= mb_l - 1;
4452 }
4453 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004454# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004455 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004456
4457 /* Use nextline[] if possible, it has the start of the
4458 * next line concatenated. */
4459 if ((prev_ptr - line) - nextlinecol >= 0)
4460 p = nextline + (prev_ptr - line) - nextlinecol;
4461 else
4462 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004463 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004464 len = spell_check(wp, p, &spell_hlf, &cap_col,
4465 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004466 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004467
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004468 /* In Insert mode only highlight a word that
4469 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004470 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004471 && (State & INSERT) != 0
4472 && wp->w_cursor.lnum == lnum
4473 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004474 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004475 && wp->w_cursor.col < (colnr_T)word_end)
4476 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004477 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004478 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004479 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004480
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004481 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004482 && (p - nextline) + len > nextline_idx)
4483 {
4484 /* Remember that the good word continues at the
4485 * start of the next line. */
4486 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004487 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004488 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004489
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004490 /* Turn index into actual attributes. */
4491 if (spell_hlf != HLF_COUNT)
4492 spell_attr = highlight_attr[spell_hlf];
4493
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004494 if (cap_col > 0)
4495 {
4496 if (p != prev_ptr
4497 && (p - nextline) + cap_col >= nextline_idx)
4498 {
4499 /* Remember that the word in the next line
4500 * must start with a capital. */
4501 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004502 cap_col = (int)((p - nextline) + cap_col
4503 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004504 }
4505 else
4506 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004507 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004508 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004509 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004510 }
4511 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004512 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004513 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004514 char_attr = hl_combine_attr(char_attr, spell_attr);
4515 else
4516 char_attr = hl_combine_attr(spell_attr, char_attr);
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#endif
4519#ifdef FEAT_LINEBREAK
4520 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004521 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004523 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004525# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004526 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004527# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004528 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004530 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004532 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004533
Bram Moolenaar597a4222014-06-25 14:39:50 +02004534 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004535 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004536 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004537 if (c == TAB && n_extra + col > W_WIDTH(wp))
4538 n_extra = (int)wp->w_buffer->b_p_ts
4539 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4540
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004541# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004542 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004543# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004547 {
4548#ifdef FEAT_CONCEAL
4549 if (c == TAB)
4550 /* See "Tab alignment" below. */
4551 FIX_FOR_BOGUSCOLS;
4552#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004553 if (!wp->w_p_list)
4554 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557#endif
4558
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004559 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4560 */
4561 if (wp->w_p_list
4562 && (((c == 160
4563#ifdef FEAT_MBYTE
4564 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4565#endif
4566 ) && lcs_nbsp)
4567 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4568 {
4569 c = (c == ' ') ? lcs_space : lcs_nbsp;
4570 if (area_attr == 0 && search_attr == 0)
4571 {
4572 n_attr = 1;
4573 extra_attr = hl_attr(HLF_8);
4574 saved_attr2 = char_attr; /* save current attr */
4575 }
4576#ifdef FEAT_MBYTE
4577 mb_c = c;
4578 if (enc_utf8 && (*mb_char2len)(c) > 1)
4579 {
4580 mb_utf8 = TRUE;
4581 u8cc[0] = 0;
4582 c = 0xc0;
4583 }
4584 else
4585 mb_utf8 = FALSE;
4586#endif
4587 }
4588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4590 {
4591 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004592 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 n_attr = 1;
4595 extra_attr = hl_attr(HLF_8);
4596 saved_attr2 = char_attr; /* save current attr */
4597 }
4598#ifdef FEAT_MBYTE
4599 mb_c = c;
4600 if (enc_utf8 && (*mb_char2len)(c) > 1)
4601 {
4602 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004604 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 else
4607 mb_utf8 = FALSE;
4608#endif
4609 }
4610 }
4611
4612 /*
4613 * Handling of non-printable characters.
4614 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004615 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 /*
4618 * when getting a character from the file, we may have to
4619 * turn it into something else on the way to putting it
4620 * into "ScreenLines".
4621 */
4622 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4623 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004624 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004625 long vcol_adjusted = vcol; /* removed showbreak length */
4626#ifdef FEAT_LINEBREAK
4627 /* only adjust the tab_len, when at the first column
4628 * after the showbreak value was drawn */
4629 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4630 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004633 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004634 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4635
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004636#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004637 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004638#endif
4639 /* tab amount depends on current column */
4640 n_extra = tab_len;
4641#ifdef FEAT_LINEBREAK
4642 else
4643 {
4644 char_u *p;
4645 int len = n_extra;
4646 int i;
4647 int saved_nextra = n_extra;
4648
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004649#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004650 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004651 /* there are characters to conceal */
4652 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004653 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4654 */
4655 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4656 && n_extra > tab_len)
4657 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004658#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004659
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004660 /* if n_extra > 0, it gives the number of chars, to
4661 * use for a tab, else we need to calculate the width
4662 * for a tab */
4663#ifdef FEAT_MBYTE
4664 len = (tab_len * mb_char2len(lcs_tab2));
4665 if (n_extra > 0)
4666 len += n_extra - tab_len;
4667#endif
4668 c = lcs_tab1;
4669 p = alloc((unsigned)(len + 1));
4670 vim_memset(p, ' ', len);
4671 p[len] = NUL;
4672 p_extra_free = p;
4673 for (i = 0; i < tab_len; i++)
4674 {
4675#ifdef FEAT_MBYTE
4676 mb_char2bytes(lcs_tab2, p);
4677 p += mb_char2len(lcs_tab2);
4678 n_extra += mb_char2len(lcs_tab2)
4679 - (saved_nextra > 0 ? 1 : 0);
4680#else
4681 p[i] = lcs_tab2;
4682#endif
4683 }
4684 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004685#ifdef FEAT_CONCEAL
4686 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4687 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004688 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004689 n_extra -= vcol_off;
4690#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004691 }
4692#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004693#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004694 {
4695 int vc_saved = vcol_off;
4696
4697 /* Tab alignment should be identical regardless of
4698 * 'conceallevel' value. So tab compensates of all
4699 * previous concealed characters, and thus resets
4700 * vcol_off and boguscols accumulated so far in the
4701 * line. Note that the tab can be longer than
4702 * 'tabstop' when there are concealed characters. */
4703 FIX_FOR_BOGUSCOLS;
4704
4705 /* Make sure, the highlighting for the tab char will be
4706 * correctly set further below (effectively reverts the
4707 * FIX_FOR_BOGSUCOLS macro */
4708 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004709 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004710 tab_len += vc_saved;
4711 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#ifdef FEAT_MBYTE
4714 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4715#endif
4716 if (wp->w_p_list)
4717 {
4718 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004719#ifdef FEAT_LINEBREAK
4720 if (wp->w_p_lbr)
4721 c_extra = NUL; /* using p_extra from above */
4722 else
4723#endif
4724 c_extra = lcs_tab2;
4725 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 extra_attr = hl_attr(HLF_8);
4727 saved_attr2 = char_attr; /* save current attr */
4728#ifdef FEAT_MBYTE
4729 mb_c = c;
4730 if (enc_utf8 && (*mb_char2len)(c) > 1)
4731 {
4732 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004733 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004734 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736#endif
4737 }
4738 else
4739 {
4740 c_extra = ' ';
4741 c = ' ';
4742 }
4743 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004744 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004745 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004746 || ((fromcol >= 0 || fromcol_prev >= 0)
4747 && tocol > vcol
4748 && VIsual_mode != Ctrl_V
4749 && (
4750# ifdef FEAT_RIGHTLEFT
4751 wp->w_p_rl ? (col >= 0) :
4752# endif
4753 (col < W_WIDTH(wp)))
4754 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004755 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004756 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004757 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004759 /* Display a '$' after the line or highlight an extra
4760 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4762 /* For a diff line the highlighting continues after the
4763 * "$". */
4764 if (
4765# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004766 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767# ifdef LINE_ATTR
4768 &&
4769# endif
4770# endif
4771# ifdef LINE_ATTR
4772 line_attr == 0
4773# endif
4774 )
4775#endif
4776 {
4777#ifdef FEAT_VIRTUALEDIT
4778 /* In virtualedit, visual selections may extend
4779 * beyond end of line. */
4780 if (area_highlighting && virtual_active()
4781 && tocol != MAXCOL && vcol < tocol)
4782 n_extra = 0;
4783 else
4784#endif
4785 {
4786 p_extra = at_end_str;
4787 n_extra = 1;
4788 c_extra = NUL;
4789 }
4790 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004791 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004792 c = lcs_eol;
4793 else
4794 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 lcs_eol_one = -1;
4796 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004797 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 extra_attr = hl_attr(HLF_AT);
4800 n_attr = 1;
4801 }
4802#ifdef FEAT_MBYTE
4803 mb_c = c;
4804 if (enc_utf8 && (*mb_char2len)(c) > 1)
4805 {
4806 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004807 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004808 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 else
4811 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4812#endif
4813 }
4814 else if (c != NUL)
4815 {
4816 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004817 if (n_extra == 0)
4818 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#ifdef FEAT_RIGHTLEFT
4820 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4821 rl_mirror(p_extra); /* reverse "<12>" */
4822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004824#ifdef FEAT_LINEBREAK
4825 if (wp->w_p_lbr)
4826 {
4827 char_u *p;
4828
4829 c = *p_extra;
4830 p = alloc((unsigned)n_extra + 1);
4831 vim_memset(p, ' ', n_extra);
4832 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4833 p[n_extra] = NUL;
4834 p_extra_free = p_extra = p;
4835 }
4836 else
4837#endif
4838 {
4839 n_extra = byte2cells(c) - 1;
4840 c = *p_extra++;
4841 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004842 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 n_attr = n_extra + 1;
4845 extra_attr = hl_attr(HLF_8);
4846 saved_attr2 = char_attr; /* save current attr */
4847 }
4848#ifdef FEAT_MBYTE
4849 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4850#endif
4851 }
4852#ifdef FEAT_VIRTUALEDIT
4853 else if (VIsual_active
4854 && (VIsual_mode == Ctrl_V
4855 || VIsual_mode == 'v')
4856 && virtual_active()
4857 && tocol != MAXCOL
4858 && vcol < tocol
4859 && (
4860# ifdef FEAT_RIGHTLEFT
4861 wp->w_p_rl ? (col >= 0) :
4862# endif
4863 (col < W_WIDTH(wp))))
4864 {
4865 c = ' ';
4866 --ptr; /* put it back at the NUL */
4867 }
4868#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004869#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else if ((
4871# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004872 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 ) && (
4876# ifdef FEAT_RIGHTLEFT
4877 wp->w_p_rl ? (col >= 0) :
4878# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004879 (col
4880# ifdef FEAT_CONCEAL
4881 - boguscols
4882# endif
4883 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885 /* Highlight until the right side of the window */
4886 c = ' ';
4887 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004888
4889 /* Remember we do the char for line highlighting. */
4890 ++did_line_attr;
4891
4892 /* don't do search HL for the rest of the line */
4893 if (line_attr != 0 && char_attr == search_attr && col > 0)
4894 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895# ifdef FEAT_DIFF
4896 if (diff_hlf == HLF_TXD)
4897 {
4898 diff_hlf = HLF_CHD;
4899 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004902 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4903 char_attr = hl_combine_attr(char_attr,
4904 hl_attr(HLF_CUL));
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907# endif
4908 }
4909#endif
4910 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004911
4912#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004913 if ( wp->w_p_cole > 0
4914 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004915 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02004916 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004917 && !(lnum_in_visual_area
4918 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004919 {
4920 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02004921 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004922 && (syn_get_sub_char() != NUL || match_conc
4923 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004924 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004925 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004926 /* First time at this concealed item: display one
4927 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004928 if (match_conc)
4929 c = match_conc;
4930 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931 c = syn_get_sub_char();
4932 else if (lcs_conceal != NUL)
4933 c = lcs_conceal;
4934 else
4935 c = ' ';
4936
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004937 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004938
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004939 if (n_extra > 0)
4940 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004941 vcol += n_extra;
4942 if (wp->w_p_wrap && n_extra > 0)
4943 {
4944# ifdef FEAT_RIGHTLEFT
4945 if (wp->w_p_rl)
4946 {
4947 col -= n_extra;
4948 boguscols -= n_extra;
4949 }
4950 else
4951# endif
4952 {
4953 boguscols += n_extra;
4954 col += n_extra;
4955 }
4956 }
4957 n_extra = 0;
4958 n_attr = 0;
4959 }
4960 else if (n_skip == 0)
4961 {
4962 is_concealing = TRUE;
4963 n_skip = 1;
4964 }
4965# ifdef FEAT_MBYTE
4966 mb_c = c;
4967 if (enc_utf8 && (*mb_char2len)(c) > 1)
4968 {
4969 mb_utf8 = TRUE;
4970 u8cc[0] = 0;
4971 c = 0xc0;
4972 }
4973 else
4974 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4975# endif
4976 }
4977 else
4978 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004979 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004980 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004981 }
4982#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004985#ifdef FEAT_CONCEAL
4986 /* In the cursor line and we may be concealing characters: correct
4987 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004988 if (!did_wcol && draw_state == WL_LINE
4989 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004990 && conceal_cursor_line(wp)
4991 && (int)wp->w_virtcol <= vcol + n_skip)
4992 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004993# ifdef FEAT_RIGHTLEFT
4994 if (wp->w_p_rl)
4995 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4996 else
4997# endif
4998 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004999 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005000 did_wcol = TRUE;
5001 }
5002#endif
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /* Don't override visual selection highlighting. */
5005 if (n_attr > 0
5006 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005007 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 char_attr = extra_attr;
5009
Bram Moolenaar81695252004-12-29 20:58:21 +00005010#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /* XIM don't send preedit_start and preedit_end, but they send
5012 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5013 * im_is_preediting() here. */
5014 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005015 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 && (State & INSERT)
5017 && !p_imdisable
5018 && im_is_preediting()
5019 && draw_state == WL_LINE)
5020 {
5021 colnr_T tcol;
5022
5023 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005024 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else
5026 tcol = preedit_end_col;
5027 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5028 {
5029 if (feedback_old_attr < 0)
5030 {
5031 feedback_col = 0;
5032 feedback_old_attr = char_attr;
5033 }
5034 char_attr = im_get_feedback_attr(feedback_col);
5035 if (char_attr < 0)
5036 char_attr = feedback_old_attr;
5037 feedback_col++;
5038 }
5039 else if (feedback_old_attr >= 0)
5040 {
5041 char_attr = feedback_old_attr;
5042 feedback_old_attr = -1;
5043 feedback_col = 0;
5044 }
5045 }
5046#endif
5047 /*
5048 * Handle the case where we are in column 0 but not on the first
5049 * character of the line and the user wants us to show us a
5050 * special character (via 'listchars' option "precedes:<char>".
5051 */
5052 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005053 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5055#ifdef FEAT_DIFF
5056 && filler_todo <= 0
5057#endif
5058 && draw_state > WL_NR
5059 && c != NUL)
5060 {
5061 c = lcs_prec;
5062 lcs_prec_todo = NUL;
5063#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005064 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5065 {
5066 /* Double-width character being overwritten by the "precedes"
5067 * character, need to fill up half the character. */
5068 c_extra = MB_FILLER_CHAR;
5069 n_extra = 1;
5070 n_attr = 2;
5071 extra_attr = hl_attr(HLF_AT);
5072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 mb_c = c;
5074 if (enc_utf8 && (*mb_char2len)(c) > 1)
5075 {
5076 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005077 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else
5081 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5082#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005083 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 saved_attr3 = char_attr; /* save current attr */
5086 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5087 n_attr3 = 1;
5088 }
5089 }
5090
5091 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005092 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005094 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005095#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005096 || did_line_attr == 1
5097#endif
5098 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005100#ifdef FEAT_SEARCH_EXTRA
5101 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005102
5103 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005104 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005105 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005106#endif
5107
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005108 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 * highlight match at end of line. If it's beyond the last
5110 * char on the screen, just overwrite that one (tricky!) Not
5111 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005112#ifdef FEAT_SEARCH_EXTRA
5113 prevcol_hl_flag = FALSE;
5114 if (prevcol == (long)search_hl.startcol)
5115 prevcol_hl_flag = TRUE;
5116 else
5117 {
5118 cur = wp->w_match_head;
5119 while (cur != NULL)
5120 {
5121 if (prevcol == (long)cur->hl.startcol)
5122 {
5123 prevcol_hl_flag = TRUE;
5124 break;
5125 }
5126 cur = cur->next;
5127 }
5128 }
5129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005131 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005132 && (VIsual_mode != Ctrl_V
5133 || lnum == VIsual.lnum
5134 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005135 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#ifdef FEAT_SEARCH_EXTRA
5137 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005138 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005139# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005140 && did_line_attr <= 1
5141# endif
5142 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143#endif
5144 ))
5145 {
5146 int n = 0;
5147
5148#ifdef FEAT_RIGHTLEFT
5149 if (wp->w_p_rl)
5150 {
5151 if (col < 0)
5152 n = 1;
5153 }
5154 else
5155#endif
5156 {
5157 if (col >= W_WIDTH(wp))
5158 n = -1;
5159 }
5160 if (n != 0)
5161 {
5162 /* At the window boundary, highlight the last character
5163 * instead (better than nothing). */
5164 off += n;
5165 col += n;
5166 }
5167 else
5168 {
5169 /* Add a blank character to highlight. */
5170 ScreenLines[off] = ' ';
5171#ifdef FEAT_MBYTE
5172 if (enc_utf8)
5173 ScreenLinesUC[off] = 0;
5174#endif
5175 }
5176#ifdef FEAT_SEARCH_EXTRA
5177 if (area_attr == 0)
5178 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005179 /* Use attributes from match with highest priority among
5180 * 'search_hl' and the match list. */
5181 char_attr = search_hl.attr;
5182 cur = wp->w_match_head;
5183 shl_flag = FALSE;
5184 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005185 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005186 if (shl_flag == FALSE
5187 && ((cur != NULL
5188 && cur->priority > SEARCH_HL_PRIORITY)
5189 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005191 shl = &search_hl;
5192 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005193 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005194 else
5195 shl = &cur->hl;
5196 if ((ptr - line) - 1 == (long)shl->startcol)
5197 char_attr = shl->attr;
5198 if (shl != &search_hl && cur != NULL)
5199 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202#endif
5203 ScreenAttrs[off] = char_attr;
5204#ifdef FEAT_RIGHTLEFT
5205 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005208 --off;
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else
5211#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005214 ++off;
5215 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005216 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005217#ifdef FEAT_SYN_HL
5218 eol_hl_off = 1;
5219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaar91170f82006-05-05 21:15:17 +00005223 /*
5224 * At end of the text line.
5225 */
5226 if (c == NUL)
5227 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005228#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005229 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5230 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005231 {
5232 /* highlight last char after line */
5233 --col;
5234 --off;
5235 --vcol;
5236 }
5237
Bram Moolenaar1a384422010-07-14 19:53:30 +02005238 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005239 if (wp->w_p_wrap)
5240 v = wp->w_skipcol;
5241 else
5242 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005243
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005244 /* check if line ends before left margin */
5245 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005246 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005247#ifdef FEAT_CONCEAL
5248 /* Get rid of the boguscols now, we want to draw until the right
5249 * edge for 'cursorcolumn'. */
5250 col -= boguscols;
5251 boguscols = 0;
5252#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005253
5254 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005255 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005256
5257 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005258 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5259 && (int)wp->w_virtcol <
5260 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005261 && lnum != wp->w_cursor.lnum)
5262 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005263# ifdef FEAT_RIGHTLEFT
5264 && !wp->w_p_rl
5265# endif
5266 )
5267 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005268 int rightmost_vcol = 0;
5269 int i;
5270
5271 if (wp->w_p_cuc)
5272 rightmost_vcol = wp->w_virtcol;
5273 if (draw_color_col)
5274 /* determine rightmost colorcolumn to possibly draw */
5275 for (i = 0; color_cols[i] >= 0; ++i)
5276 if (rightmost_vcol < color_cols[i])
5277 rightmost_vcol = color_cols[i];
5278
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005279 while (col < W_WIDTH(wp))
5280 {
5281 ScreenLines[off] = ' ';
5282#ifdef FEAT_MBYTE
5283 if (enc_utf8)
5284 ScreenLinesUC[off] = 0;
5285#endif
5286 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005287 if (draw_color_col)
5288 draw_color_col = advance_color_col(VCOL_HLC,
5289 &color_cols);
5290
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005291 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005292 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005293 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005294 ScreenAttrs[off++] = hl_attr(HLF_MC);
5295 else
5296 ScreenAttrs[off++] = 0;
5297
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005298 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005299 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005300
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005301 ++vcol;
5302 }
5303 }
5304#endif
5305
Bram Moolenaar860cae12010-06-05 23:22:07 +02005306 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5307 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 row++;
5309
5310 /*
5311 * Update w_cline_height and w_cline_folded if the cursor line was
5312 * updated (saves a call to plines() later).
5313 */
5314 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5315 {
5316 curwin->w_cline_row = startrow;
5317 curwin->w_cline_height = row - startrow;
5318#ifdef FEAT_FOLDING
5319 curwin->w_cline_folded = FALSE;
5320#endif
5321 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5322 }
5323
5324 break;
5325 }
5326
5327 /* line continues beyond line end */
5328 if (lcs_ext
5329 && !wp->w_p_wrap
5330#ifdef FEAT_DIFF
5331 && filler_todo <= 0
5332#endif
5333 && (
5334#ifdef FEAT_RIGHTLEFT
5335 wp->w_p_rl ? col == 0 :
5336#endif
5337 col == W_WIDTH(wp) - 1)
5338 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005339 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5341 {
5342 c = lcs_ext;
5343 char_attr = hl_attr(HLF_AT);
5344#ifdef FEAT_MBYTE
5345 mb_c = c;
5346 if (enc_utf8 && (*mb_char2len)(c) > 1)
5347 {
5348 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005349 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005350 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 else
5353 mb_utf8 = FALSE;
5354#endif
5355 }
5356
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005357#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005358 /* advance to the next 'colorcolumn' */
5359 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005360 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005361
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005362 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005363 * highlight the cursor position itself.
5364 * Also highlight the 'colorcolumn' if it is different than
5365 * 'cursorcolumn' */
5366 vcol_save_attr = -1;
5367 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005368 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005369 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005370 && lnum != wp->w_cursor.lnum)
5371 {
5372 vcol_save_attr = char_attr;
5373 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5374 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005375 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005376 {
5377 vcol_save_attr = char_attr;
5378 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5379 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005380 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005381#endif
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 /*
5384 * Store character to be displayed.
5385 * Skip characters that are left of the screen for 'nowrap'.
5386 */
5387 vcol_prev = vcol;
5388 if (draw_state < WL_LINE || n_skip <= 0)
5389 {
5390 /*
5391 * Store the character.
5392 */
5393#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5394 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5395 {
5396 /* A double-wide character is: put first halve in left cell. */
5397 --off;
5398 --col;
5399 }
5400#endif
5401 ScreenLines[off] = c;
5402#ifdef FEAT_MBYTE
5403 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005404 {
5405 if ((mb_c & 0xff00) == 0x8e00)
5406 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 else if (enc_utf8)
5410 {
5411 if (mb_utf8)
5412 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005413 int i;
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005416 if ((c & 0xff) == 0)
5417 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005418 for (i = 0; i < Screen_mco; ++i)
5419 {
5420 ScreenLinesC[i][off] = u8cc[i];
5421 if (u8cc[i] == 0)
5422 break;
5423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425 else
5426 ScreenLinesUC[off] = 0;
5427 }
5428 if (multi_attr)
5429 {
5430 ScreenAttrs[off] = multi_attr;
5431 multi_attr = 0;
5432 }
5433 else
5434#endif
5435 ScreenAttrs[off] = char_attr;
5436
5437#ifdef FEAT_MBYTE
5438 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5439 {
5440 /* Need to fill two screen columns. */
5441 ++off;
5442 ++col;
5443 if (enc_utf8)
5444 /* UTF-8: Put a 0 in the second screen char. */
5445 ScreenLines[off] = 0;
5446 else
5447 /* DBCS: Put second byte in the second screen char. */
5448 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005449 if (draw_state > WL_NR
5450#ifdef FEAT_DIFF
5451 && filler_todo <= 0
5452#endif
5453 )
5454 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 /* When "tocol" is halfway a character, set it to the end of
5456 * the character, otherwise highlighting won't stop. */
5457 if (tocol == vcol)
5458 ++tocol;
5459#ifdef FEAT_RIGHTLEFT
5460 if (wp->w_p_rl)
5461 {
5462 /* now it's time to backup one cell */
5463 --off;
5464 --col;
5465 }
5466#endif
5467 }
5468#endif
5469#ifdef FEAT_RIGHTLEFT
5470 if (wp->w_p_rl)
5471 {
5472 --off;
5473 --col;
5474 }
5475 else
5476#endif
5477 {
5478 ++off;
5479 ++col;
5480 }
5481 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005482#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005483 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005484 {
5485 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005486 ++vcol_off;
5487 if (n_extra > 0)
5488 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005489 if (wp->w_p_wrap)
5490 {
5491 /*
5492 * Special voodoo required if 'wrap' is on.
5493 *
5494 * Advance the column indicator to force the line
5495 * drawing to wrap early. This will make the line
5496 * take up the same screen space when parts are concealed,
5497 * so that cursor line computations aren't messed up.
5498 *
5499 * To avoid the fictitious advance of 'col' causing
5500 * trailing junk to be written out of the screen line
5501 * we are building, 'boguscols' keeps track of the number
5502 * of bad columns we have advanced.
5503 */
5504 if (n_extra > 0)
5505 {
5506 vcol += n_extra;
5507# ifdef FEAT_RIGHTLEFT
5508 if (wp->w_p_rl)
5509 {
5510 col -= n_extra;
5511 boguscols -= n_extra;
5512 }
5513 else
5514# endif
5515 {
5516 col += n_extra;
5517 boguscols += n_extra;
5518 }
5519 n_extra = 0;
5520 n_attr = 0;
5521 }
5522
5523
5524# ifdef FEAT_MBYTE
5525 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5526 {
5527 /* Need to fill two screen columns. */
5528# ifdef FEAT_RIGHTLEFT
5529 if (wp->w_p_rl)
5530 {
5531 --boguscols;
5532 --col;
5533 }
5534 else
5535# endif
5536 {
5537 ++boguscols;
5538 ++col;
5539 }
5540 }
5541# endif
5542
5543# ifdef FEAT_RIGHTLEFT
5544 if (wp->w_p_rl)
5545 {
5546 --boguscols;
5547 --col;
5548 }
5549 else
5550# endif
5551 {
5552 ++boguscols;
5553 ++col;
5554 }
5555 }
5556 else
5557 {
5558 if (n_extra > 0)
5559 {
5560 vcol += n_extra;
5561 n_extra = 0;
5562 n_attr = 0;
5563 }
5564 }
5565
5566 }
5567#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else
5569 --n_skip;
5570
Bram Moolenaar64486672010-05-16 15:46:46 +02005571 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5572 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005573 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574#ifdef FEAT_DIFF
5575 && filler_todo <= 0
5576#endif
5577 )
5578 ++vcol;
5579
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005580#ifdef FEAT_SYN_HL
5581 if (vcol_save_attr >= 0)
5582 char_attr = vcol_save_attr;
5583#endif
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 /* restore attributes after "predeces" in 'listchars' */
5586 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5587 char_attr = saved_attr3;
5588
5589 /* restore attributes after last 'listchars' or 'number' char */
5590 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5591 char_attr = saved_attr2;
5592
5593 /*
5594 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005595 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 */
5597 if ((
5598#ifdef FEAT_RIGHTLEFT
5599 wp->w_p_rl ? (col < 0) :
5600#endif
5601 (col >= W_WIDTH(wp)))
5602 && (*ptr != NUL
5603#ifdef FEAT_DIFF
5604 || filler_todo > 0
5605#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005606 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5608 )
5609 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005610#ifdef FEAT_CONCEAL
5611 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5612 (int)W_WIDTH(wp), wp->w_p_rl);
5613 boguscols = 0;
5614#else
5615 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5616 (int)W_WIDTH(wp), wp->w_p_rl);
5617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ++row;
5619 ++screen_row;
5620
5621 /* When not wrapping and finished diff lines, or when displayed
5622 * '$' and highlighting until last column, break here. */
5623 if ((!wp->w_p_wrap
5624#ifdef FEAT_DIFF
5625 && filler_todo <= 0
5626#endif
5627 ) || lcs_eol_one == -1)
5628 break;
5629
5630 /* When the window is too narrow draw all "@" lines. */
5631 if (draw_state != WL_LINE
5632#ifdef FEAT_DIFF
5633 && filler_todo <= 0
5634#endif
5635 )
5636 {
5637 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005638#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 draw_vsep_win(wp, row);
5640#endif
5641 row = endrow;
5642 }
5643
5644 /* When line got too long for screen break here. */
5645 if (row == endrow)
5646 {
5647 ++row;
5648 break;
5649 }
5650
5651 if (screen_cur_row == screen_row - 1
5652#ifdef FEAT_DIFF
5653 && filler_todo <= 0
5654#endif
5655 && W_WIDTH(wp) == Columns)
5656 {
5657 /* Remember that the line wraps, used for modeless copy. */
5658 LineWraps[screen_row - 1] = TRUE;
5659
5660 /*
5661 * Special trick to make copy/paste of wrapped lines work with
5662 * xterm/screen: write an extra character beyond the end of
5663 * the line. This will work with all terminal types
5664 * (regardless of the xn,am settings).
5665 * Only do this on a fast tty.
5666 * Only do this if the cursor is on the current line
5667 * (something has been written in it).
5668 * Don't do this for the GUI.
5669 * Don't do this for double-width characters.
5670 * Don't do this for a window not at the right screen border.
5671 */
5672 if (p_tf
5673#ifdef FEAT_GUI
5674 && !gui.in_use
5675#endif
5676#ifdef FEAT_MBYTE
5677 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005678 && ((*mb_off2cells)(LineOffset[screen_row],
5679 LineOffset[screen_row] + screen_Columns)
5680 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005682 + (int)Columns - 2,
5683 LineOffset[screen_row] + screen_Columns)
5684 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#endif
5686 )
5687 {
5688 /* First make sure we are at the end of the screen line,
5689 * then output the same character again to let the
5690 * terminal know about the wrap. If the terminal doesn't
5691 * auto-wrap, we overwrite the character. */
5692 if (screen_cur_col != W_WIDTH(wp))
5693 screen_char(LineOffset[screen_row - 1]
5694 + (unsigned)Columns - 1,
5695 screen_row - 1, (int)(Columns - 1));
5696
5697#ifdef FEAT_MBYTE
5698 /* When there is a multi-byte character, just output a
5699 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005700 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5701 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 out_char(' ');
5703 else
5704#endif
5705 out_char(ScreenLines[LineOffset[screen_row - 1]
5706 + (Columns - 1)]);
5707 /* force a redraw of the first char on the next line */
5708 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5709 screen_start(); /* don't know where cursor is now */
5710 }
5711 }
5712
5713 col = 0;
5714 off = (unsigned)(current_ScreenLine - ScreenLines);
5715#ifdef FEAT_RIGHTLEFT
5716 if (wp->w_p_rl)
5717 {
5718 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5719 off += col;
5720 }
5721#endif
5722
5723 /* reset the drawing state for the start of a wrapped line */
5724 draw_state = WL_START;
5725 saved_n_extra = n_extra;
5726 saved_p_extra = p_extra;
5727 saved_c_extra = c_extra;
5728 saved_char_attr = char_attr;
5729 n_extra = 0;
5730 lcs_prec_todo = lcs_prec;
5731#ifdef FEAT_LINEBREAK
5732# ifdef FEAT_DIFF
5733 if (filler_todo <= 0)
5734# endif
5735 need_showbreak = TRUE;
5736#endif
5737#ifdef FEAT_DIFF
5738 --filler_todo;
5739 /* When the filler lines are actually below the last line of the
5740 * file, don't draw the line itself, break here. */
5741 if (filler_todo == 0 && wp->w_botfill)
5742 break;
5743#endif
5744 }
5745
5746 } /* for every character in the line */
5747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005748#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005749 /* After an empty line check first word for capital. */
5750 if (*skipwhite(line) == NUL)
5751 {
5752 capcol_lnum = lnum + 1;
5753 cap_col = 0;
5754 }
5755#endif
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return row;
5758}
5759
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005760#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005761static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005762
5763/*
5764 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005765 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005766 */
5767 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005768comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005769{
5770 int i;
5771
5772 for (i = 0; i < Screen_mco; ++i)
5773 {
5774 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5775 return TRUE;
5776 if (ScreenLinesC[i][off_from] == 0)
5777 break;
5778 }
5779 return FALSE;
5780}
5781#endif
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
5784 * Check whether the given character needs redrawing:
5785 * - the (first byte of the) character is different
5786 * - the attributes are different
5787 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005788 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 */
5790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005791char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 if (cols > 0
5794 && ((ScreenLines[off_from] != ScreenLines[off_to]
5795 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5796
5797#ifdef FEAT_MBYTE
5798 || (enc_dbcs != 0
5799 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5800 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5801 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5802 : (cols > 1 && ScreenLines[off_from + 1]
5803 != ScreenLines[off_to + 1])))
5804 || (enc_utf8
5805 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5806 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005807 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005808 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5809 && ScreenLines[off_from + 1]
5810 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811#endif
5812 ))
5813 return TRUE;
5814 return FALSE;
5815}
5816
5817/*
5818 * Move one "cooked" screen line to the screen, but only the characters that
5819 * have actually changed. Handle insert/delete character.
5820 * "coloff" gives the first column on the screen for this line.
5821 * "endcol" gives the columns where valid characters are.
5822 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5823 * needs to be cleared, negative otherwise.
5824 * "rlflag" is TRUE in a rightleft window:
5825 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5826 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5827 */
5828 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005829screen_line(
5830 int row,
5831 int coloff,
5832 int endcol,
5833 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005835 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005837 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 unsigned off_from;
5840 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005841#ifdef FEAT_MBYTE
5842 unsigned max_off_from;
5843 unsigned max_off_to;
5844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int col = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005846#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 int hl;
5848#endif
5849 int force = FALSE; /* force update rest of the line */
5850 int redraw_this /* bool: does character need redraw? */
5851#ifdef FEAT_GUI
5852 = TRUE /* For GUI when while-loop empty */
5853#endif
5854 ;
5855 int redraw_next; /* redraw_this for next character */
5856#ifdef FEAT_MBYTE
5857 int clear_next = FALSE;
5858 int char_cells; /* 1: normal char */
5859 /* 2: occupies two display cells */
5860# define CHAR_CELLS char_cells
5861#else
5862# define CHAR_CELLS 1
5863#endif
5864
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005865 /* Check for illegal row and col, just in case. */
5866 if (row >= Rows)
5867 row = Rows - 1;
5868 if (endcol > Columns)
5869 endcol = Columns;
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871# ifdef FEAT_CLIPBOARD
5872 clip_may_clear_selection(row, row);
5873# endif
5874
5875 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5876 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005877#ifdef FEAT_MBYTE
5878 max_off_from = off_from + screen_Columns;
5879 max_off_to = LineOffset[row] + screen_Columns;
5880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
5882#ifdef FEAT_RIGHTLEFT
5883 if (rlflag)
5884 {
5885 /* Clear rest first, because it's left of the text. */
5886 if (clear_width > 0)
5887 {
5888 while (col <= endcol && ScreenLines[off_to] == ' '
5889 && ScreenAttrs[off_to] == 0
5890# ifdef FEAT_MBYTE
5891 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5892# endif
5893 )
5894 {
5895 ++off_to;
5896 ++col;
5897 }
5898 if (col <= endcol)
5899 screen_fill(row, row + 1, col + coloff,
5900 endcol + coloff + 1, ' ', ' ', 0);
5901 }
5902 col = endcol + 1;
5903 off_to = LineOffset[row] + col + coloff;
5904 off_from += col;
5905 endcol = (clear_width > 0 ? clear_width : -clear_width);
5906 }
5907#endif /* FEAT_RIGHTLEFT */
5908
5909 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5910
5911 while (col < endcol)
5912 {
5913#ifdef FEAT_MBYTE
5914 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005915 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 else
5917 char_cells = 1;
5918#endif
5919
5920 redraw_this = redraw_next;
5921 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5922 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5923
5924#ifdef FEAT_GUI
5925 /* If the next character was bold, then redraw the current character to
5926 * remove any pixels that might have spilt over into us. This only
5927 * happens in the GUI.
5928 */
5929 if (redraw_next && gui.in_use)
5930 {
5931 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005932 if (hl > HL_ALL)
5933 hl = syn_attr2attr(hl);
5934 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 redraw_this = TRUE;
5936 }
5937#endif
5938
5939 if (redraw_this)
5940 {
5941 /*
5942 * Special handling when 'xs' termcap flag set (hpterm):
5943 * Attributes for characters are stored at the position where the
5944 * cursor is when writing the highlighting code. The
5945 * start-highlighting code must be written with the cursor on the
5946 * first highlighted character. The stop-highlighting code must
5947 * be written with the cursor just after the last highlighted
5948 * character.
5949 * Overwriting a character doesn't remove it's highlighting. Need
5950 * to clear the rest of the line, and force redrawing it
5951 * completely.
5952 */
5953 if ( p_wiv
5954 && !force
5955#ifdef FEAT_GUI
5956 && !gui.in_use
5957#endif
5958 && ScreenAttrs[off_to] != 0
5959 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5960 {
5961 /*
5962 * Need to remove highlighting attributes here.
5963 */
5964 windgoto(row, col + coloff);
5965 out_str(T_CE); /* clear rest of this screen line */
5966 screen_start(); /* don't know where cursor is now */
5967 force = TRUE; /* force redraw of rest of the line */
5968 redraw_next = TRUE; /* or else next char would miss out */
5969
5970 /*
5971 * If the previous character was highlighted, need to stop
5972 * highlighting at this character.
5973 */
5974 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5975 {
5976 screen_attr = ScreenAttrs[off_to - 1];
5977 term_windgoto(row, col + coloff);
5978 screen_stop_highlight();
5979 }
5980 else
5981 screen_attr = 0; /* highlighting has stopped */
5982 }
5983#ifdef FEAT_MBYTE
5984 if (enc_dbcs != 0)
5985 {
5986 /* Check if overwriting a double-byte with a single-byte or
5987 * the other way around requires another character to be
5988 * redrawn. For UTF-8 this isn't needed, because comparing
5989 * ScreenLinesUC[] is sufficient. */
5990 if (char_cells == 1
5991 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005992 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 /* Writing a single-cell character over a double-cell
5995 * character: need to redraw the next cell. */
5996 ScreenLines[off_to + 1] = 0;
5997 redraw_next = TRUE;
5998 }
5999 else if (char_cells == 2
6000 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006001 && (*mb_off2cells)(off_to, max_off_to) == 1
6002 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {
6004 /* Writing the second half of a double-cell character over
6005 * a double-cell character: need to redraw the second
6006 * cell. */
6007 ScreenLines[off_to + 2] = 0;
6008 redraw_next = TRUE;
6009 }
6010
6011 if (enc_dbcs == DBCS_JPNU)
6012 ScreenLines2[off_to] = ScreenLines2[off_from];
6013 }
6014 /* When writing a single-width character over a double-width
6015 * character and at the end of the redrawn text, need to clear out
6016 * the right halve of the old character.
6017 * Also required when writing the right halve of a double-width
6018 * char over the left halve of an existing one. */
6019 if (has_mbyte && col + char_cells == endcol
6020 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006021 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006023 && (*mb_off2cells)(off_to, max_off_to) == 1
6024 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 clear_next = TRUE;
6026#endif
6027
6028 ScreenLines[off_to] = ScreenLines[off_from];
6029#ifdef FEAT_MBYTE
6030 if (enc_utf8)
6031 {
6032 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6033 if (ScreenLinesUC[off_from] != 0)
6034 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006035 int i;
6036
6037 for (i = 0; i < Screen_mco; ++i)
6038 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 }
6040 }
6041 if (char_cells == 2)
6042 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6043#endif
6044
6045#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006046 /* The bold trick makes a single column of pixels appear in the
6047 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 * character should be redrawn too. This happens for our own GUI
6049 * and for some xterms. */
6050 if (
6051# ifdef FEAT_GUI
6052 gui.in_use
6053# endif
6054# if defined(FEAT_GUI) && defined(UNIX)
6055 ||
6056# endif
6057# ifdef UNIX
6058 term_is_xterm
6059# endif
6060 )
6061 {
6062 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006063 if (hl > HL_ALL)
6064 hl = syn_attr2attr(hl);
6065 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 redraw_next = TRUE;
6067 }
6068#endif
6069 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6070#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006071 /* For simplicity set the attributes of second half of a
6072 * double-wide character equal to the first half. */
6073 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006075
6076 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 else
6079#endif
6080 screen_char(off_to, row, col + coloff);
6081 }
6082 else if ( p_wiv
6083#ifdef FEAT_GUI
6084 && !gui.in_use
6085#endif
6086 && col + coloff > 0)
6087 {
6088 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6089 {
6090 /*
6091 * Don't output stop-highlight when moving the cursor, it will
6092 * stop the highlighting when it should continue.
6093 */
6094 screen_attr = 0;
6095 }
6096 else if (screen_attr != 0)
6097 screen_stop_highlight();
6098 }
6099
6100 off_to += CHAR_CELLS;
6101 off_from += CHAR_CELLS;
6102 col += CHAR_CELLS;
6103 }
6104
6105#ifdef FEAT_MBYTE
6106 if (clear_next)
6107 {
6108 /* Clear the second half of a double-wide character of which the left
6109 * half was overwritten with a single-wide character. */
6110 ScreenLines[off_to] = ' ';
6111 if (enc_utf8)
6112 ScreenLinesUC[off_to] = 0;
6113 screen_char(off_to, row, col + coloff);
6114 }
6115#endif
6116
6117 if (clear_width > 0
6118#ifdef FEAT_RIGHTLEFT
6119 && !rlflag
6120#endif
6121 )
6122 {
6123#ifdef FEAT_GUI
6124 int startCol = col;
6125#endif
6126
6127 /* blank out the rest of the line */
6128 while (col < clear_width && ScreenLines[off_to] == ' '
6129 && ScreenAttrs[off_to] == 0
6130#ifdef FEAT_MBYTE
6131 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6132#endif
6133 )
6134 {
6135 ++off_to;
6136 ++col;
6137 }
6138 if (col < clear_width)
6139 {
6140#ifdef FEAT_GUI
6141 /*
6142 * In the GUI, clearing the rest of the line may leave pixels
6143 * behind if the first character cleared was bold. Some bold
6144 * fonts spill over the left. In this case we redraw the previous
6145 * character too. If we didn't skip any blanks above, then we
6146 * only redraw if the character wasn't already redrawn anyway.
6147 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006148 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {
6150 hl = ScreenAttrs[off_to];
6151 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006152 {
6153 int prev_cells = 1;
6154# ifdef FEAT_MBYTE
6155 if (enc_utf8)
6156 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6157 * that its width is 2. */
6158 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6159 else if (enc_dbcs != 0)
6160 {
6161 /* find previous character by counting from first
6162 * column and get its width. */
6163 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006164 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006165
6166 while (off < off_to)
6167 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006168 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006169 off += prev_cells;
6170 }
6171 }
6172
6173 if (enc_dbcs != 0 && prev_cells > 1)
6174 screen_char_2(off_to - prev_cells, row,
6175 col + coloff - prev_cells);
6176 else
6177# endif
6178 screen_char(off_to - prev_cells, row,
6179 col + coloff - prev_cells);
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182#endif
6183 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6184 ' ', ' ', 0);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006185#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 off_to += clear_width - col;
6187 col = clear_width;
6188#endif
6189 }
6190 }
6191
6192 if (clear_width > 0)
6193 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006194#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 /* For a window that's left of another, draw the separator char. */
6196 if (col + coloff < Columns)
6197 {
6198 int c;
6199
6200 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006201 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006203 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6204 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205# endif
6206 || ScreenAttrs[off_to] != hl)
6207 {
6208 ScreenLines[off_to] = c;
6209 ScreenAttrs[off_to] = hl;
6210# ifdef FEAT_MBYTE
6211 if (enc_utf8)
6212 {
6213 if (c >= 0x80)
6214 {
6215 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006216 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218 else
6219 ScreenLinesUC[off_to] = 0;
6220 }
6221# endif
6222 screen_char(off_to, row, col + coloff);
6223 }
6224 }
6225 else
6226#endif
6227 LineWraps[row] = FALSE;
6228 }
6229}
6230
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006231#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006233 * Mirror text "str" for right-left displaying.
6234 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006237rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 char_u *p1, *p2;
6240 int t;
6241
6242 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6243 {
6244 t = *p1;
6245 *p1 = *p2;
6246 *p2 = t;
6247 }
6248}
6249#endif
6250
6251#if defined(FEAT_WINDOWS) || defined(PROTO)
6252/*
6253 * mark all status lines for redraw; used after first :cd
6254 */
6255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006256status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 win_T *wp;
6259
6260 for (wp = firstwin; wp; wp = wp->w_next)
6261 if (wp->w_status_height)
6262 {
6263 wp->w_redr_status = TRUE;
6264 redraw_later(VALID);
6265 }
6266}
6267
6268/*
6269 * mark all status lines of the current buffer for redraw
6270 */
6271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006272status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 win_T *wp;
6275
6276 for (wp = firstwin; wp; wp = wp->w_next)
6277 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6278 {
6279 wp->w_redr_status = TRUE;
6280 redraw_later(VALID);
6281 }
6282}
6283
6284/*
6285 * Redraw all status lines that need to be redrawn.
6286 */
6287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006288redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 win_T *wp;
6291
6292 for (wp = firstwin; wp; wp = wp->w_next)
6293 if (wp->w_redr_status)
6294 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006295 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006296 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297}
6298#endif
6299
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006300#if (defined(FEAT_WILDMENU) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301/*
6302 * Redraw all status lines at the bottom of frame "frp".
6303 */
6304 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006305win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
6307 if (frp->fr_layout == FR_LEAF)
6308 frp->fr_win->w_redr_status = TRUE;
6309 else if (frp->fr_layout == FR_ROW)
6310 {
6311 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6312 win_redraw_last_status(frp);
6313 }
6314 else /* frp->fr_layout == FR_COL */
6315 {
6316 frp = frp->fr_child;
6317 while (frp->fr_next != NULL)
6318 frp = frp->fr_next;
6319 win_redraw_last_status(frp);
6320 }
6321}
6322#endif
6323
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006324#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325/*
6326 * Draw the verticap separator right of window "wp" starting with line "row".
6327 */
6328 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006329draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 int hl;
6332 int c;
6333
6334 if (wp->w_vsep_width)
6335 {
6336 /* draw the vertical separator right of this window */
6337 c = fillchar_vsep(&hl);
6338 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6339 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6340 c, ' ', hl);
6341 }
6342}
6343#endif
6344
6345#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006346static int status_match_len(expand_T *xp, char_u *s);
6347static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006350 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 */
6352 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006353status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 int len = 0;
6356
6357#ifdef FEAT_MENU
6358 int emenu = (xp->xp_context == EXPAND_MENUS
6359 || xp->xp_context == EXPAND_MENUNAMES);
6360
6361 /* Check for menu separators - replace with '|'. */
6362 if (emenu && menu_is_separator(s))
6363 return 1;
6364#endif
6365
6366 while (*s != NUL)
6367 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006368 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006369 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006370 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
6372
6373 return len;
6374}
6375
6376/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006377 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006378 * These are backslashes used for escaping. Do show backslashes in help tags.
6379 */
6380 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006381skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006382{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006383 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006384#ifdef FEAT_MENU
6385 || ((xp->xp_context == EXPAND_MENUS
6386 || xp->xp_context == EXPAND_MENUNAMES)
6387 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6388#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006389 )
6390 {
6391#ifndef BACKSLASH_IN_FILENAME
6392 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6393 return 2;
6394#endif
6395 return 1;
6396 }
6397 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006398}
6399
6400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * Show wildchar matches in the status line.
6402 * Show at least the "match" item.
6403 * We start at item 'first_match' in the list and show all matches that fit.
6404 *
6405 * If inversion is possible we use it. Else '=' characters are used.
6406 */
6407 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006408win_redr_status_matches(
6409 expand_T *xp,
6410 int num_matches,
6411 char_u **matches, /* list of matches */
6412 int match,
6413 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6416 int row;
6417 char_u *buf;
6418 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006419 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 int fillchar;
6421 int attr;
6422 int i;
6423 int highlight = TRUE;
6424 char_u *selstart = NULL;
6425 int selstart_col = 0;
6426 char_u *selend = NULL;
6427 static int first_match = 0;
6428 int add_left = FALSE;
6429 char_u *s;
6430#ifdef FEAT_MENU
6431 int emenu;
6432#endif
6433#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6434 int l;
6435#endif
6436
6437 if (matches == NULL) /* interrupted completion? */
6438 return;
6439
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006440#ifdef FEAT_MBYTE
6441 if (has_mbyte)
6442 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6443 else
6444#endif
6445 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (buf == NULL)
6447 return;
6448
6449 if (match == -1) /* don't show match but original text */
6450 {
6451 match = 0;
6452 highlight = FALSE;
6453 }
6454 /* count 1 for the ending ">" */
6455 clen = status_match_len(xp, L_MATCH(match)) + 3;
6456 if (match == 0)
6457 first_match = 0;
6458 else if (match < first_match)
6459 {
6460 /* jumping left, as far as we can go */
6461 first_match = match;
6462 add_left = TRUE;
6463 }
6464 else
6465 {
6466 /* check if match fits on the screen */
6467 for (i = first_match; i < match; ++i)
6468 clen += status_match_len(xp, L_MATCH(i)) + 2;
6469 if (first_match > 0)
6470 clen += 2;
6471 /* jumping right, put match at the left */
6472 if ((long)clen > Columns)
6473 {
6474 first_match = match;
6475 /* if showing the last match, we can add some on the left */
6476 clen = 2;
6477 for (i = match; i < num_matches; ++i)
6478 {
6479 clen += status_match_len(xp, L_MATCH(i)) + 2;
6480 if ((long)clen >= Columns)
6481 break;
6482 }
6483 if (i == num_matches)
6484 add_left = TRUE;
6485 }
6486 }
6487 if (add_left)
6488 while (first_match > 0)
6489 {
6490 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6491 if ((long)clen >= Columns)
6492 break;
6493 --first_match;
6494 }
6495
6496 fillchar = fillchar_status(&attr, TRUE);
6497
6498 if (first_match == 0)
6499 {
6500 *buf = NUL;
6501 len = 0;
6502 }
6503 else
6504 {
6505 STRCPY(buf, "< ");
6506 len = 2;
6507 }
6508 clen = len;
6509
6510 i = first_match;
6511 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6512 {
6513 if (i == match)
6514 {
6515 selstart = buf + len;
6516 selstart_col = clen;
6517 }
6518
6519 s = L_MATCH(i);
6520 /* Check for menu separators - replace with '|' */
6521#ifdef FEAT_MENU
6522 emenu = (xp->xp_context == EXPAND_MENUS
6523 || xp->xp_context == EXPAND_MENUNAMES);
6524 if (emenu && menu_is_separator(s))
6525 {
6526 STRCPY(buf + len, transchar('|'));
6527 l = (int)STRLEN(buf + len);
6528 len += l;
6529 clen += l;
6530 }
6531 else
6532#endif
6533 for ( ; *s != NUL; ++s)
6534 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006535 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 clen += ptr2cells(s);
6537#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006538 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
6540 STRNCPY(buf + len, s, l);
6541 s += l - 1;
6542 len += l;
6543 }
6544 else
6545#endif
6546 {
6547 STRCPY(buf + len, transchar_byte(*s));
6548 len += (int)STRLEN(buf + len);
6549 }
6550 }
6551 if (i == match)
6552 selend = buf + len;
6553
6554 *(buf + len++) = ' ';
6555 *(buf + len++) = ' ';
6556 clen += 2;
6557 if (++i == num_matches)
6558 break;
6559 }
6560
6561 if (i != num_matches)
6562 {
6563 *(buf + len++) = '>';
6564 ++clen;
6565 }
6566
6567 buf[len] = NUL;
6568
6569 row = cmdline_row - 1;
6570 if (row >= 0)
6571 {
6572 if (wild_menu_showing == 0)
6573 {
6574 if (msg_scrolled > 0)
6575 {
6576 /* Put the wildmenu just above the command line. If there is
6577 * no room, scroll the screen one line up. */
6578 if (cmdline_row == Rows - 1)
6579 {
6580 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6581 ++msg_scrolled;
6582 }
6583 else
6584 {
6585 ++cmdline_row;
6586 ++row;
6587 }
6588 wild_menu_showing = WM_SCROLLED;
6589 }
6590 else
6591 {
6592 /* Create status line if needed by setting 'laststatus' to 2.
6593 * Set 'winminheight' to zero to avoid that the window is
6594 * resized. */
6595 if (lastwin->w_status_height == 0)
6596 {
6597 save_p_ls = p_ls;
6598 save_p_wmh = p_wmh;
6599 p_ls = 2;
6600 p_wmh = 0;
6601 last_status(FALSE);
6602 }
6603 wild_menu_showing = WM_SHOWN;
6604 }
6605 }
6606
6607 screen_puts(buf, row, 0, attr);
6608 if (selstart != NULL && highlight)
6609 {
6610 *selend = NUL;
6611 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6612 }
6613
6614 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6615 }
6616
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006617#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 win_redraw_last_status(topframe);
6619#else
6620 lastwin->w_redr_status = TRUE;
6621#endif
6622 vim_free(buf);
6623}
6624#endif
6625
6626#if defined(FEAT_WINDOWS) || defined(PROTO)
6627/*
6628 * Redraw the status line of window wp.
6629 *
6630 * If inversion is possible we use it. Else '=' characters are used.
6631 */
6632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006633win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 int row;
6636 char_u *p;
6637 int len;
6638 int fillchar;
6639 int attr;
6640 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006641 static int busy = FALSE;
6642
6643 /* It's possible to get here recursively when 'statusline' (indirectly)
6644 * invokes ":redrawstatus". Simply ignore the call then. */
6645 if (busy)
6646 return;
6647 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
6649 wp->w_redr_status = FALSE;
6650 if (wp->w_status_height == 0)
6651 {
6652 /* no status line, can only be last window */
6653 redraw_cmdline = TRUE;
6654 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006655 else if (!redrawing()
6656#ifdef FEAT_INS_EXPAND
6657 /* don't update status line when popup menu is visible and may be
6658 * drawn over it */
6659 || pum_visible()
6660#endif
6661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
6663 /* Don't redraw right now, do it later. */
6664 wp->w_redr_status = TRUE;
6665 }
6666#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006667 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
6669 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006670 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
6672#endif
6673 else
6674 {
6675 fillchar = fillchar_status(&attr, wp == curwin);
6676
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006677 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 p = NameBuff;
6679 len = (int)STRLEN(p);
6680
6681 if (wp->w_buffer->b_help
6682#ifdef FEAT_QUICKFIX
6683 || wp->w_p_pvw
6684#endif
6685 || bufIsChanged(wp->w_buffer)
6686 || wp->w_buffer->b_p_ro)
6687 *(p + len++) = ' ';
6688 if (wp->w_buffer->b_help)
6689 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006690 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 len += (int)STRLEN(p + len);
6692 }
6693#ifdef FEAT_QUICKFIX
6694 if (wp->w_p_pvw)
6695 {
6696 STRCPY(p + len, _("[Preview]"));
6697 len += (int)STRLEN(p + len);
6698 }
6699#endif
6700 if (bufIsChanged(wp->w_buffer))
6701 {
6702 STRCPY(p + len, "[+]");
6703 len += 3;
6704 }
6705 if (wp->w_buffer->b_p_ro)
6706 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006707 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 len += 4;
6709 }
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6712 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6713 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6714 if (this_ru_col <= 1)
6715 {
6716 p = (char_u *)"<"; /* No room for file name! */
6717 len = 1;
6718 }
6719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720#ifdef FEAT_MBYTE
6721 if (has_mbyte)
6722 {
6723 int clen = 0, i;
6724
6725 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006726 clen = mb_string2cells(p, -1);
6727
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 /* Find first character that will fit.
6729 * Going from start to end is much faster for DBCS. */
6730 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006731 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 clen -= (*mb_ptr2cells)(p + i);
6733 len = clen;
6734 if (i > 0)
6735 {
6736 p = p + i - 1;
6737 *p = '<';
6738 ++len;
6739 }
6740
6741 }
6742 else
6743#endif
6744 if (len > this_ru_col - 1)
6745 {
6746 p += len - (this_ru_col - 1);
6747 *p = '<';
6748 len = this_ru_col - 1;
6749 }
6750
6751 row = W_WINROW(wp) + wp->w_height;
6752 screen_puts(p, row, W_WINCOL(wp), attr);
6753 screen_fill(row, row + 1, len + W_WINCOL(wp),
6754 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6755
6756 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6757 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6758 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6759 - 1 + W_WINCOL(wp)), attr);
6760
6761#ifdef FEAT_CMDL_INFO
6762 win_redr_ruler(wp, TRUE);
6763#endif
6764 }
6765
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 /*
6767 * May need to draw the character below the vertical separator.
6768 */
6769 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6770 {
6771 if (stl_connected(wp))
6772 fillchar = fillchar_status(&attr, wp == curwin);
6773 else
6774 fillchar = fillchar_vsep(&attr);
6775 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6776 attr);
6777 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006778 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780
Bram Moolenaar238a5642006-02-21 22:12:05 +00006781#ifdef FEAT_STL_OPT
6782/*
6783 * Redraw the status line according to 'statusline' and take care of any
6784 * errors encountered.
6785 */
6786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006787redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006788{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006789 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02006790 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006791
6792 /* When called recursively return. This can happen when the statusline
6793 * contains an expression that triggers a redraw. */
6794 if (entered)
6795 return;
6796 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006797
Bram Moolenaara742e082016-04-05 21:10:38 +02006798 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006799 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02006800 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 {
6802 /* When there is an error disable the statusline, otherwise the
6803 * display is messed up with errors and a redraw triggers the problem
6804 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006805 set_string_option_direct((char_u *)"statusline", -1,
6806 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006807 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006808 }
Bram Moolenaara742e082016-04-05 21:10:38 +02006809 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006810 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006811}
6812#endif
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814/*
6815 * Return TRUE if the status line of window "wp" is connected to the status
6816 * line of the window right of it. If not, then it's a vertical separator.
6817 * Only call if (wp->w_vsep_width != 0).
6818 */
6819 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006820stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 frame_T *fr;
6823
6824 fr = wp->w_frame;
6825 while (fr->fr_parent != NULL)
6826 {
6827 if (fr->fr_parent->fr_layout == FR_COL)
6828 {
6829 if (fr->fr_next != NULL)
6830 break;
6831 }
6832 else
6833 {
6834 if (fr->fr_next != NULL)
6835 return TRUE;
6836 }
6837 fr = fr->fr_parent;
6838 }
6839 return FALSE;
6840}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
6842#endif /* FEAT_WINDOWS */
6843
6844#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6845/*
6846 * Get the value to show for the language mappings, active 'keymap'.
6847 */
6848 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006849get_keymap_str(
6850 win_T *wp,
6851 char_u *buf, /* buffer for the result */
6852 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 char_u *p;
6855
6856 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6857 return FALSE;
6858
6859 {
6860#ifdef FEAT_EVAL
6861 buf_T *old_curbuf = curbuf;
6862 win_T *old_curwin = curwin;
6863 char_u *s;
6864
6865 curbuf = wp->w_buffer;
6866 curwin = wp;
6867 STRCPY(buf, "b:keymap_name"); /* must be writable */
6868 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006869 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 --emsg_skip;
6871 curbuf = old_curbuf;
6872 curwin = old_curwin;
6873 if (p == NULL || *p == NUL)
6874#endif
6875 {
6876#ifdef FEAT_KEYMAP
6877 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6878 p = wp->w_buffer->b_p_keymap;
6879 else
6880#endif
6881 p = (char_u *)"lang";
6882 }
6883 if ((int)(STRLEN(p) + 3) < len)
6884 sprintf((char *)buf, "<%s>", p);
6885 else
6886 buf[0] = NUL;
6887#ifdef FEAT_EVAL
6888 vim_free(s);
6889#endif
6890 }
6891 return buf[0] != NUL;
6892}
6893#endif
6894
6895#if defined(FEAT_STL_OPT) || defined(PROTO)
6896/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006897 * Redraw the status line or ruler of window "wp".
6898 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
6900 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006901win_redr_custom(
6902 win_T *wp,
6903 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006905 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 int attr;
6907 int curattr;
6908 int row;
6909 int col = 0;
6910 int maxwidth;
6911 int width;
6912 int n;
6913 int len;
6914 int fillchar;
6915 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006916 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006918 struct stl_hlrec hltab[STL_MAX_ITEM];
6919 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006920 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006921 win_T *ewp;
6922 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar1d633412013-12-11 15:52:01 +01006924 /* There is a tiny chance that this gets called recursively: When
6925 * redrawing a status line triggers redrawing the ruler or tabline.
6926 * Avoid trouble by not allowing recursion. */
6927 if (entered)
6928 return;
6929 entered = TRUE;
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006932 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006934 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006935 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006936 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006937 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006938 attr = hl_attr(HLF_TPF);
6939 maxwidth = Columns;
6940# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006941 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006944 else
6945 {
6946 row = W_WINROW(wp) + wp->w_height;
6947 fillchar = fillchar_status(&attr, wp == curwin);
6948 maxwidth = W_WIDTH(wp);
6949
6950 if (draw_ruler)
6951 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006953 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006954 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006955 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006956 if (*++stl == '-')
6957 stl++;
6958 if (atoi((char *)stl))
6959 while (VIM_ISDIGIT(*stl))
6960 stl++;
6961 if (*stl++ != '(')
6962 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006963 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006964#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006965 col = ru_col - (Columns - W_WIDTH(wp));
6966 if (col < (W_WIDTH(wp) + 1) / 2)
6967 col = (W_WIDTH(wp) + 1) / 2;
6968#else
6969 col = ru_col;
6970 if (col > (Columns + 1) / 2)
6971 col = (Columns + 1) / 2;
6972#endif
6973 maxwidth = W_WIDTH(wp) - col;
6974#ifdef FEAT_WINDOWS
6975 if (!wp->w_status_height)
6976#endif
6977 {
6978 row = Rows - 1;
6979 --maxwidth; /* writing in last column may cause scrolling */
6980 fillchar = ' ';
6981 attr = 0;
6982 }
6983
6984# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006985 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006986# endif
6987 }
6988 else
6989 {
6990 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006992 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006994# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006995 use_sandbox = was_set_insecurely((char_u *)"statusline",
6996 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006997# endif
6998 }
6999
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007000#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007001 col += W_WINCOL(wp);
7002#endif
7003 }
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007006 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaar61452852011-02-01 18:01:11 +01007008 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7009 * the cursor away and back. */
7010 ewp = wp == NULL ? curwin : wp;
7011 p_crb_save = ewp->w_p_crb;
7012 ewp->w_p_crb = FALSE;
7013
Bram Moolenaar362f3562009-11-03 16:20:34 +00007014 /* Make a copy, because the statusline may include a function call that
7015 * might change the option value and free the memory. */
7016 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007017 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007018 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007019 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007021 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007023 /* Make all characters printable. */
7024 p = transstr(buf);
7025 if (p != NULL)
7026 {
7027 vim_strncpy(buf, p, sizeof(buf) - 1);
7028 vim_free(p);
7029 }
7030
7031 /* fill up with "fillchar" */
7032 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007033 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
7035#ifdef FEAT_MBYTE
7036 len += (*mb_char2bytes)(fillchar, buf + len);
7037#else
7038 buf[len++] = fillchar;
7039#endif
7040 ++width;
7041 }
7042 buf[len] = NUL;
7043
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007044 /*
7045 * Draw each snippet with the specified highlighting.
7046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 curattr = attr;
7048 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007051 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 screen_puts_len(p, len, row, col, curattr);
7053 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007054 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007058 else if (hltab[n].userhl < 0)
7059 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007061 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007062 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
7064 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007065 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007068
7069 if (wp == NULL)
7070 {
7071 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7072 col = 0;
7073 len = 0;
7074 p = buf;
7075 fillchar = 0;
7076 for (n = 0; tabtab[n].start != NULL; n++)
7077 {
7078 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7079 while (col < len)
7080 TabPageIdxs[col++] = fillchar;
7081 p = tabtab[n].start;
7082 fillchar = tabtab[n].userhl;
7083 }
7084 while (col < Columns)
7085 TabPageIdxs[col++] = fillchar;
7086 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007087
7088theend:
7089 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091
7092#endif /* FEAT_STL_OPT */
7093
7094/*
7095 * Output a single character directly to the screen and update ScreenLines.
7096 */
7097 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007098screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 char_u buf[MB_MAXBYTES + 1];
7101
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007102#ifdef FEAT_MBYTE
7103 if (has_mbyte)
7104 buf[(*mb_char2bytes)(c, buf)] = NUL;
7105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007107 {
7108 buf[0] = c;
7109 buf[1] = NUL;
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 screen_puts(buf, row, col, attr);
7112}
7113
7114/*
7115 * Get a single character directly from ScreenLines into "bytes[]".
7116 * Also return its attribute in *attrp;
7117 */
7118 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007119screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120{
7121 unsigned off;
7122
7123 /* safety check */
7124 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7125 {
7126 off = LineOffset[row] + col;
7127 *attrp = ScreenAttrs[off];
7128 bytes[0] = ScreenLines[off];
7129 bytes[1] = NUL;
7130
7131#ifdef FEAT_MBYTE
7132 if (enc_utf8 && ScreenLinesUC[off] != 0)
7133 bytes[utfc_char2bytes(off, bytes)] = NUL;
7134 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7135 {
7136 bytes[0] = ScreenLines[off];
7137 bytes[1] = ScreenLines2[off];
7138 bytes[2] = NUL;
7139 }
7140 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7141 {
7142 bytes[1] = ScreenLines[off + 1];
7143 bytes[2] = NUL;
7144 }
7145#endif
7146 }
7147}
7148
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007149#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007150static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007151
7152/*
7153 * Return TRUE if composing characters for screen posn "off" differs from
7154 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007155 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007156 */
7157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007158screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007159{
7160 int i;
7161
7162 for (i = 0; i < Screen_mco; ++i)
7163 {
7164 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7165 return TRUE;
7166 if (u8cc[i] == 0)
7167 break;
7168 }
7169 return FALSE;
7170}
7171#endif
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * Put string '*text' on the screen at position 'row' and 'col', with
7175 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7176 * Note: only outputs within one row, message is truncated at screen boundary!
7177 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7178 */
7179 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007180screen_puts(
7181 char_u *text,
7182 int row,
7183 int col,
7184 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186 screen_puts_len(text, -1, row, col, attr);
7187}
7188
7189/*
7190 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7191 * a NUL.
7192 */
7193 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007194screen_puts_len(
7195 char_u *text,
7196 int textlen,
7197 int row,
7198 int col,
7199 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 unsigned off;
7202 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007203 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 int c;
7205#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007206 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 int mbyte_blen = 1;
7208 int mbyte_cells = 1;
7209 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007210 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 int clear_next_cell = FALSE;
7212# ifdef FEAT_ARABIC
7213 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007214 int pc, nc, nc1;
7215 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216# endif
7217#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007218#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7219 int force_redraw_this;
7220 int force_redraw_next = FALSE;
7221#endif
7222 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
7224 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7225 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007226 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaarc236c162008-07-13 17:41:49 +00007228#ifdef FEAT_MBYTE
7229 /* When drawing over the right halve of a double-wide char clear out the
7230 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007231 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007232# ifdef FEAT_GUI
7233 && !gui.in_use
7234# endif
7235 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007236 {
7237 ScreenLines[off - 1] = ' ';
7238 ScreenAttrs[off - 1] = 0;
7239 if (enc_utf8)
7240 {
7241 ScreenLinesUC[off - 1] = 0;
7242 ScreenLinesC[0][off - 1] = 0;
7243 }
7244 /* redraw the previous cell, make it empty */
7245 screen_char(off - 1, row, col - 1);
7246 /* force the cell at "col" to be redrawn */
7247 force_redraw_next = TRUE;
7248 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007249#endif
7250
Bram Moolenaar367329b2007-08-30 11:53:22 +00007251#ifdef FEAT_MBYTE
7252 max_off = LineOffset[row] + screen_Columns;
7253#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007254 while (col < screen_Columns
7255 && (len < 0 || (int)(ptr - text) < len)
7256 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 c = *ptr;
7259#ifdef FEAT_MBYTE
7260 /* check if this is the first byte of a multibyte */
7261 if (has_mbyte)
7262 {
7263 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007264 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007266 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7268 mbyte_cells = 1;
7269 else if (enc_dbcs != 0)
7270 mbyte_cells = mbyte_blen;
7271 else /* enc_utf8 */
7272 {
7273 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007274 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 (int)((text + len) - ptr));
7276 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007277 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007279# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 /* Non-BMP character: display as ? or fullwidth ?. */
7281 if (u8c >= 0x10000)
7282 {
7283 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7284 if (attr == 0)
7285 attr = hl_attr(HLF_8);
7286 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288# ifdef FEAT_ARABIC
7289 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7290 {
7291 /* Do Arabic shaping. */
7292 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7293 {
7294 /* Past end of string to be displayed. */
7295 nc = NUL;
7296 nc1 = NUL;
7297 }
7298 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007299 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007300 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7301 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007302 nc1 = pcc[0];
7303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 pc = prev_c;
7305 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007306 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
7308 else
7309 prev_c = u8c;
7310# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007311 if (col + mbyte_cells > screen_Columns)
7312 {
7313 /* Only 1 cell left, but character requires 2 cells:
7314 * display a '>' in the last column to avoid wrapping. */
7315 c = '>';
7316 mbyte_cells = 1;
7317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
7319 }
7320#endif
7321
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007322#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7323 force_redraw_this = force_redraw_next;
7324 force_redraw_next = FALSE;
7325#endif
7326
7327 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328#ifdef FEAT_MBYTE
7329 || (mbyte_cells == 2
7330 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7331 || (enc_dbcs == DBCS_JPNU
7332 && c == 0x8e
7333 && ScreenLines2[off] != ptr[1])
7334 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007335 && (ScreenLinesUC[off] !=
7336 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7337 || (ScreenLinesUC[off] != 0
7338 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339#endif
7340 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007341 || exmode_active;
7342
7343 if (need_redraw
7344#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7345 || force_redraw_this
7346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 )
7348 {
7349#if defined(FEAT_GUI) || defined(UNIX)
7350 /* The bold trick makes a single row of pixels appear in the next
7351 * character. When a bold character is removed, the next
7352 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007353 * and for some xterms. */
7354 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355# ifdef FEAT_GUI
7356 gui.in_use
7357# endif
7358# if defined(FEAT_GUI) && defined(UNIX)
7359 ||
7360# endif
7361# ifdef UNIX
7362 term_is_xterm
7363# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007364 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007366 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007368 if (n > HL_ALL)
7369 n = syn_attr2attr(n);
7370 if (n & HL_BOLD)
7371 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373#endif
7374#ifdef FEAT_MBYTE
7375 /* When at the end of the text and overwriting a two-cell
7376 * character with a one-cell character, need to clear the next
7377 * cell. Also when overwriting the left halve of a two-cell char
7378 * with the right halve of a two-cell char. Do this only once
7379 * (mb_off2cells() may return 2 on the right halve). */
7380 if (clear_next_cell)
7381 clear_next_cell = FALSE;
7382 else if (has_mbyte
7383 && (len < 0 ? ptr[mbyte_blen] == NUL
7384 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007385 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007387 && (*mb_off2cells)(off, max_off) == 1
7388 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 clear_next_cell = TRUE;
7390
7391 /* Make sure we never leave a second byte of a double-byte behind,
7392 * it confuses mb_off2cells(). */
7393 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007394 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007396 && (*mb_off2cells)(off, max_off) == 1
7397 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 ScreenLines[off + mbyte_blen] = 0;
7399#endif
7400 ScreenLines[off] = c;
7401 ScreenAttrs[off] = attr;
7402#ifdef FEAT_MBYTE
7403 if (enc_utf8)
7404 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007405 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 ScreenLinesUC[off] = 0;
7407 else
7408 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 int i;
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412 for (i = 0; i < Screen_mco; ++i)
7413 {
7414 ScreenLinesC[i][off] = u8cc[i];
7415 if (u8cc[i] == 0)
7416 break;
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
7419 if (mbyte_cells == 2)
7420 {
7421 ScreenLines[off + 1] = 0;
7422 ScreenAttrs[off + 1] = attr;
7423 }
7424 screen_char(off, row, col);
7425 }
7426 else if (mbyte_cells == 2)
7427 {
7428 ScreenLines[off + 1] = ptr[1];
7429 ScreenAttrs[off + 1] = attr;
7430 screen_char_2(off, row, col);
7431 }
7432 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7433 {
7434 ScreenLines2[off] = ptr[1];
7435 screen_char(off, row, col);
7436 }
7437 else
7438#endif
7439 screen_char(off, row, col);
7440 }
7441#ifdef FEAT_MBYTE
7442 if (has_mbyte)
7443 {
7444 off += mbyte_cells;
7445 col += mbyte_cells;
7446 ptr += mbyte_blen;
7447 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007448 {
7449 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007451 len = -1;
7452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454 else
7455#endif
7456 {
7457 ++off;
7458 ++col;
7459 ++ptr;
7460 }
7461 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007462
7463#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7464 /* If we detected the next character needs to be redrawn, but the text
7465 * doesn't extend up to there, update the character here. */
7466 if (force_redraw_next && col < screen_Columns)
7467 {
7468# ifdef FEAT_MBYTE
7469 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7470 screen_char_2(off, row, col);
7471 else
7472# endif
7473 screen_char(off, row, col);
7474 }
7475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476}
7477
7478#ifdef FEAT_SEARCH_EXTRA
7479/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007480 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 */
7482 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007483start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 if (p_hls && !no_hlsearch)
7486 {
7487 last_pat_prog(&search_hl.rm);
7488 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007489# ifdef FEAT_RELTIME
7490 /* Set the time limit to 'redrawtime'. */
7491 profile_setlimit(p_rdt, &search_hl.tm);
7492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494}
7495
7496/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007497 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 */
7499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007500end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
7502 if (search_hl.rm.regprog != NULL)
7503 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007504 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 search_hl.rm.regprog = NULL;
7506 }
7507}
7508
7509/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007510 * Init for calling prepare_search_hl().
7511 */
7512 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007513init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007514{
7515 matchitem_T *cur;
7516
7517 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7518 * match */
7519 cur = wp->w_match_head;
7520 while (cur != NULL)
7521 {
7522 cur->hl.rm = cur->match;
7523 if (cur->hlg_id == 0)
7524 cur->hl.attr = 0;
7525 else
7526 cur->hl.attr = syn_id2attr(cur->hlg_id);
7527 cur->hl.buf = wp->w_buffer;
7528 cur->hl.lnum = 0;
7529 cur->hl.first_lnum = 0;
7530# ifdef FEAT_RELTIME
7531 /* Set the time limit to 'redrawtime'. */
7532 profile_setlimit(p_rdt, &(cur->hl.tm));
7533# endif
7534 cur = cur->next;
7535 }
7536 search_hl.buf = wp->w_buffer;
7537 search_hl.lnum = 0;
7538 search_hl.first_lnum = 0;
7539 /* time limit is set at the toplevel, for all windows */
7540}
7541
7542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 * Advance to the match in window "wp" line "lnum" or past it.
7544 */
7545 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007546prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007548 matchitem_T *cur; /* points to the match list */
7549 match_T *shl; /* points to search_hl or a match */
7550 int shl_flag; /* flag to indicate whether search_hl
7551 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007552 int pos_inprogress; /* marks that position match search is
7553 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 int n;
7555
7556 /*
7557 * When using a multi-line pattern, start searching at the top
7558 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007559 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007561 cur = wp->w_match_head;
7562 shl_flag = FALSE;
7563 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007565 if (shl_flag == FALSE)
7566 {
7567 shl = &search_hl;
7568 shl_flag = TRUE;
7569 }
7570 else
7571 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (shl->rm.regprog != NULL
7573 && shl->lnum == 0
7574 && re_multiline(shl->rm.regprog))
7575 {
7576 if (shl->first_lnum == 0)
7577 {
7578# ifdef FEAT_FOLDING
7579 for (shl->first_lnum = lnum;
7580 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7581 if (hasFoldingWin(wp, shl->first_lnum - 1,
7582 NULL, NULL, TRUE, NULL))
7583 break;
7584# else
7585 shl->first_lnum = wp->w_topline;
7586# endif
7587 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007588 if (cur != NULL)
7589 cur->pos.cur = 0;
7590 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007592 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7593 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007595 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7596 pos_inprogress = cur == NULL || cur->pos.cur == 0
7597 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (shl->lnum != 0)
7599 {
7600 shl->first_lnum = shl->lnum
7601 + shl->rm.endpos[0].lnum
7602 - shl->rm.startpos[0].lnum;
7603 n = shl->rm.endpos[0].col;
7604 }
7605 else
7606 {
7607 ++shl->first_lnum;
7608 n = 0;
7609 }
7610 }
7611 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007612 if (shl != &search_hl && cur != NULL)
7613 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 }
7615}
7616
7617/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007618 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 * Uses shl->buf.
7620 * Sets shl->lnum and shl->rm contents.
7621 * Note: Assumes a previous match is always before "lnum", unless
7622 * shl->lnum is zero.
7623 * Careful: Any pointers for buffer lines will become invalid.
7624 */
7625 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007626next_search_hl(
7627 win_T *win,
7628 match_T *shl, /* points to search_hl or a match */
7629 linenr_T lnum,
7630 colnr_T mincol, /* minimal column for a match */
7631 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 linenr_T l;
7634 colnr_T matchcol;
7635 long nmatched;
7636
7637 if (shl->lnum != 0)
7638 {
7639 /* Check for three situations:
7640 * 1. If the "lnum" is below a previous match, start a new search.
7641 * 2. If the previous match includes "mincol", use it.
7642 * 3. Continue after the previous match.
7643 */
7644 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7645 if (lnum > l)
7646 shl->lnum = 0;
7647 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7648 return;
7649 }
7650
7651 /*
7652 * Repeat searching for a match until one is found that includes "mincol"
7653 * or none is found in this line.
7654 */
7655 called_emsg = FALSE;
7656 for (;;)
7657 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007658#ifdef FEAT_RELTIME
7659 /* Stop searching after passing the time limit. */
7660 if (profile_passed_limit(&(shl->tm)))
7661 {
7662 shl->lnum = 0; /* no match found in time */
7663 break;
7664 }
7665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 /* Three situations:
7667 * 1. No useful previous match: search from start of line.
7668 * 2. Not Vi compatible or empty match: continue at next character.
7669 * Break the loop if this is beyond the end of the line.
7670 * 3. Vi compatible searching: continue at end of previous match.
7671 */
7672 if (shl->lnum == 0)
7673 matchcol = 0;
7674 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7675 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007676 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007678 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007679
7680 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007681 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007682 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007684 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 shl->lnum = 0;
7686 break;
7687 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007688#ifdef FEAT_MBYTE
7689 if (has_mbyte)
7690 matchcol += mb_ptr2len(ml);
7691 else
7692#endif
7693 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
7695 else
7696 matchcol = shl->rm.endpos[0].col;
7697
7698 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007699 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007701 /* Remember whether shl->rm is using a copy of the regprog in
7702 * cur->match. */
7703 int regprog_is_copy = (shl != &search_hl && cur != NULL
7704 && shl == &cur->hl
7705 && cur->match.regprog == cur->hl.rm.regprog);
7706
Bram Moolenaarb3414592014-06-17 17:48:32 +02007707 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7708 matchcol,
7709#ifdef FEAT_RELTIME
7710 &(shl->tm)
7711#else
7712 NULL
7713#endif
7714 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007715 /* Copy the regprog, in case it got freed and recompiled. */
7716 if (regprog_is_copy)
7717 cur->match.regprog = cur->hl.rm.regprog;
7718
Bram Moolenaarb3414592014-06-17 17:48:32 +02007719 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007720 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007721 /* Error while handling regexp: stop using this regexp. */
7722 if (shl == &search_hl)
7723 {
7724 /* don't free regprog in the match list, it's a copy */
7725 vim_regfree(shl->rm.regprog);
7726 SET_NO_HLSEARCH(TRUE);
7727 }
7728 shl->rm.regprog = NULL;
7729 shl->lnum = 0;
7730 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7731 message */
7732 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007733 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007734 }
7735 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007736 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007737 else
7738 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (nmatched == 0)
7740 {
7741 shl->lnum = 0; /* no match found */
7742 break;
7743 }
7744 if (shl->rm.startpos[0].lnum > 0
7745 || shl->rm.startpos[0].col >= mincol
7746 || nmatched > 1
7747 || shl->rm.endpos[0].col > mincol)
7748 {
7749 shl->lnum += shl->rm.startpos[0].lnum;
7750 break; /* useful match found */
7751 }
7752 }
7753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
Bram Moolenaarb3414592014-06-17 17:48:32 +02007755 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007756next_search_hl_pos(
7757 match_T *shl, /* points to a match */
7758 linenr_T lnum,
7759 posmatch_T *posmatch, /* match positions */
7760 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007761{
7762 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007763 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007764
7765 shl->lnum = 0;
7766 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7767 {
7768 if (posmatch->pos[i].lnum == 0)
7769 break;
7770 if (posmatch->pos[i].col < mincol)
7771 continue;
7772 if (posmatch->pos[i].lnum == lnum)
7773 {
7774 if (shl->lnum == lnum)
7775 {
7776 /* partially sort positions by column numbers
7777 * on the same line */
7778 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7779 {
7780 llpos_T tmp = posmatch->pos[i];
7781
7782 posmatch->pos[i] = posmatch->pos[bot];
7783 posmatch->pos[bot] = tmp;
7784 }
7785 }
7786 else
7787 {
7788 bot = i;
7789 shl->lnum = lnum;
7790 }
7791 }
7792 }
7793 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007794 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007795 {
7796 colnr_T start = posmatch->pos[bot].col == 0
7797 ? 0 : posmatch->pos[bot].col - 1;
7798 colnr_T end = posmatch->pos[bot].col == 0
7799 ? MAXCOL : start + posmatch->pos[bot].len;
7800
7801 shl->rm.startpos[0].lnum = 0;
7802 shl->rm.startpos[0].col = start;
7803 shl->rm.endpos[0].lnum = 0;
7804 shl->rm.endpos[0].col = end;
7805 posmatch->cur = bot + 1;
7806 return TRUE;
7807 }
7808 return FALSE;
7809}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007810#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007813screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 attrentry_T *aep = NULL;
7816
7817 screen_attr = attr;
7818 if (full_screen
7819#ifdef WIN3264
7820 && termcap_active
7821#endif
7822 )
7823 {
7824#ifdef FEAT_GUI
7825 if (gui.in_use)
7826 {
7827 char buf[20];
7828
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007829 /* The GUI handles this internally. */
7830 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 OUT_STR(buf);
7832 }
7833 else
7834#endif
7835 {
7836 if (attr > HL_ALL) /* special HL attr. */
7837 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007838 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 aep = syn_cterm_attr2entry(attr);
7840 else
7841 aep = syn_term_attr2entry(attr);
7842 if (aep == NULL) /* did ":syntax clear" */
7843 attr = 0;
7844 else
7845 attr = aep->ae_attr;
7846 }
7847 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7848 out_str(T_MD);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007849 else if (aep != NULL && cterm_normal_fg_bold &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007850#ifdef FEAT_TERMGUICOLORS
7851 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007852 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007853#endif
7854 (t_colors > 1 && aep->ae_u.cterm.fg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007855#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007856 )
7857#endif
7858 )
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007859 /* If the Normal FG color has BOLD attribute and the new HL
7860 * has a FG color defined, clear BOLD. */
7861 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7863 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007864 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7865 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 out_str(T_US);
7867 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7868 out_str(T_CZH);
7869 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7870 out_str(T_MR);
7871
7872 /*
7873 * Output the color or start string after bold etc., in case the
7874 * bold etc. override the color setting.
7875 */
7876 if (aep != NULL)
7877 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007878#ifdef FEAT_TERMGUICOLORS
7879 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02007881 if (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007882 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaar902647d2016-04-22 11:49:06 +02007883 if (aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007884 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007889 if (t_colors > 1)
7890 {
7891 if (aep->ae_u.cterm.fg_color)
7892 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7893 if (aep->ae_u.cterm.bg_color)
7894 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7895 }
7896 else
7897 {
7898 if (aep->ae_u.term.start != NULL)
7899 out_str(aep->ae_u.term.start);
7900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 }
7902 }
7903 }
7904 }
7905}
7906
7907 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007908screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909{
7910 int do_ME = FALSE; /* output T_ME code */
7911
7912 if (screen_attr != 0
7913#ifdef WIN3264
7914 && termcap_active
7915#endif
7916 )
7917 {
7918#ifdef FEAT_GUI
7919 if (gui.in_use)
7920 {
7921 char buf[20];
7922
7923 /* use internal GUI code */
7924 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7925 OUT_STR(buf);
7926 }
7927 else
7928#endif
7929 {
7930 if (screen_attr > HL_ALL) /* special HL attr. */
7931 {
7932 attrentry_T *aep;
7933
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007934 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {
7936 /*
7937 * Assume that t_me restores the original colors!
7938 */
7939 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007940 if (aep != NULL &&
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007941#ifdef FEAT_TERMGUICOLORS
7942 (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02007943 (aep->ae_u.cterm.fg_rgb != (long_u)INVALCOLOR ||
7944 aep->ae_u.cterm.bg_rgb != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007945#endif
7946 (aep->ae_u.cterm.fg_color || aep->ae_u.cterm.bg_color)
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007947#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007948 )
7949#endif
7950 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 do_ME = TRUE;
7952 }
7953 else
7954 {
7955 aep = syn_term_attr2entry(screen_attr);
7956 if (aep != NULL && aep->ae_u.term.stop != NULL)
7957 {
7958 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7959 do_ME = TRUE;
7960 else
7961 out_str(aep->ae_u.term.stop);
7962 }
7963 }
7964 if (aep == NULL) /* did ":syntax clear" */
7965 screen_attr = 0;
7966 else
7967 screen_attr = aep->ae_attr;
7968 }
7969
7970 /*
7971 * Often all ending-codes are equal to T_ME. Avoid outputting the
7972 * same sequence several times.
7973 */
7974 if (screen_attr & HL_STANDOUT)
7975 {
7976 if (STRCMP(T_SE, T_ME) == 0)
7977 do_ME = TRUE;
7978 else
7979 out_str(T_SE);
7980 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007981 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {
7983 if (STRCMP(T_UE, T_ME) == 0)
7984 do_ME = TRUE;
7985 else
7986 out_str(T_UE);
7987 }
7988 if (screen_attr & HL_ITALIC)
7989 {
7990 if (STRCMP(T_CZR, T_ME) == 0)
7991 do_ME = TRUE;
7992 else
7993 out_str(T_CZR);
7994 }
7995 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7996 out_str(T_ME);
7997
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007998#ifdef FEAT_TERMGUICOLORS
7999 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
Bram Moolenaar902647d2016-04-22 11:49:06 +02008001 if (cterm_normal_fg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008002 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar902647d2016-04-22 11:49:06 +02008003 if (cterm_normal_bg_gui_color != (long_u)INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008004 term_bg_rgb_color(cterm_normal_bg_gui_color);
8005 }
8006 else
8007#endif
8008 {
8009 if (t_colors > 1)
8010 {
8011 /* set Normal cterm colors */
8012 if (cterm_normal_fg_color != 0)
8013 term_fg_color(cterm_normal_fg_color - 1);
8014 if (cterm_normal_bg_color != 0)
8015 term_bg_color(cterm_normal_bg_color - 1);
8016 if (cterm_normal_fg_bold)
8017 out_str(T_MD);
8018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 }
8021 }
8022 screen_attr = 0;
8023}
8024
8025/*
8026 * Reset the colors for a cterm. Used when leaving Vim.
8027 * The machine specific code may override this again.
8028 */
8029 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008030reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008032 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {
8034 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008035#ifdef FEAT_TERMGUICOLORS
8036 if (p_tgc ?
Bram Moolenaar902647d2016-04-22 11:49:06 +02008037 (cterm_normal_fg_gui_color != (long_u)INVALCOLOR
8038 || cterm_normal_bg_gui_color != (long_u)INVALCOLOR):
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008039 (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
8040#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {
8044 out_str(T_OP);
8045 screen_attr = -1;
8046 }
8047 if (cterm_normal_fg_bold)
8048 {
8049 out_str(T_ME);
8050 screen_attr = -1;
8051 }
8052 }
8053}
8054
8055/*
8056 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8057 * using the attributes from ScreenAttrs["off"].
8058 */
8059 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008060screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
8062 int attr;
8063
8064 /* Check for illegal values, just in case (could happen just after
8065 * resizing). */
8066 if (row >= screen_Rows || col >= screen_Columns)
8067 return;
8068
Bram Moolenaar494838a2015-02-10 19:20:37 +01008069 /* Outputting a character in the last cell on the screen may scroll the
8070 * screen up. Only do it when the "xn" termcap property is set, otherwise
8071 * mark the character invalid (update it when scrolled up). */
8072 if (*T_XN == NUL
8073 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074#ifdef FEAT_RIGHTLEFT
8075 /* account for first command-line character in rightleft mode */
8076 && !cmdmsg_rl
8077#endif
8078 )
8079 {
8080 ScreenAttrs[off] = (sattr_T)-1;
8081 return;
8082 }
8083
8084 /*
8085 * Stop highlighting first, so it's easier to move the cursor.
8086 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008087#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 if (screen_char_attr != 0)
8089 attr = screen_char_attr;
8090 else
8091#endif
8092 attr = ScreenAttrs[off];
8093 if (screen_attr != attr)
8094 screen_stop_highlight();
8095
8096 windgoto(row, col);
8097
8098 if (screen_attr != attr)
8099 screen_start_highlight(attr);
8100
8101#ifdef FEAT_MBYTE
8102 if (enc_utf8 && ScreenLinesUC[off] != 0)
8103 {
8104 char_u buf[MB_MAXBYTES + 1];
8105
8106 /* Convert UTF-8 character to bytes and write it. */
8107
8108 buf[utfc_char2bytes(off, buf)] = NUL;
8109
8110 out_str(buf);
Bram Moolenaarcb070082016-04-02 22:14:51 +02008111 if (utf_ambiguous_width(ScreenLinesUC[off]))
8112 screen_cur_col = 9999;
8113 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 ++screen_cur_col;
8115 }
8116 else
8117#endif
8118 {
8119#ifdef FEAT_MBYTE
8120 out_flush_check();
8121#endif
8122 out_char(ScreenLines[off]);
8123#ifdef FEAT_MBYTE
8124 /* double-byte character in single-width cell */
8125 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8126 out_char(ScreenLines2[off]);
8127#endif
8128 }
8129
8130 screen_cur_col++;
8131}
8132
8133#ifdef FEAT_MBYTE
8134
8135/*
8136 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8137 * on the screen at position 'row' and 'col'.
8138 * The attributes of the first byte is used for all. This is required to
8139 * output the two bytes of a double-byte character with nothing in between.
8140 */
8141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008142screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
8144 /* Check for illegal values (could be wrong when screen was resized). */
8145 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8146 return;
8147
8148 /* Outputting the last character on the screen may scrollup the screen.
8149 * Don't to it! Mark the character invalid (update it when scrolled up) */
8150 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8151 {
8152 ScreenAttrs[off] = (sattr_T)-1;
8153 return;
8154 }
8155
8156 /* Output the first byte normally (positions the cursor), then write the
8157 * second byte directly. */
8158 screen_char(off, row, col);
8159 out_char(ScreenLines[off + 1]);
8160 ++screen_cur_col;
8161}
8162#endif
8163
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008164#if defined(FEAT_CLIPBOARD) || defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165/*
8166 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8167 * This uses the contents of ScreenLines[] and doesn't change it.
8168 */
8169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008170screen_draw_rectangle(
8171 int row,
8172 int col,
8173 int height,
8174 int width,
8175 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 int r, c;
8178 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008179#ifdef FEAT_MBYTE
8180 int max_off;
8181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008183 /* Can't use ScreenLines unless initialized */
8184 if (ScreenLines == NULL)
8185 return;
8186
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (invert)
8188 screen_char_attr = HL_INVERSE;
8189 for (r = row; r < row + height; ++r)
8190 {
8191 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008192#ifdef FEAT_MBYTE
8193 max_off = off + screen_Columns;
8194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 for (c = col; c < col + width; ++c)
8196 {
8197#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008198 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
8200 screen_char_2(off + c, r, c);
8201 ++c;
8202 }
8203 else
8204#endif
8205 {
8206 screen_char(off + c, r, c);
8207#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008208 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 ++c;
8210#endif
8211 }
8212 }
8213 }
8214 screen_char_attr = 0;
8215}
8216#endif
8217
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008218#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219/*
8220 * Redraw the characters for a vertically split window.
8221 */
8222 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008223redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 int col;
8226 int width;
8227
8228# ifdef FEAT_CLIPBOARD
8229 clip_may_clear_selection(row, end - 1);
8230# endif
8231
8232 if (wp == NULL)
8233 {
8234 col = 0;
8235 width = Columns;
8236 }
8237 else
8238 {
8239 col = wp->w_wincol;
8240 width = wp->w_width;
8241 }
8242 screen_draw_rectangle(row, col, end - row, width, FALSE);
8243}
8244#endif
8245
8246/*
8247 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8248 * with character 'c1' in first column followed by 'c2' in the other columns.
8249 * Use attributes 'attr'.
8250 */
8251 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008252screen_fill(
8253 int start_row,
8254 int end_row,
8255 int start_col,
8256 int end_col,
8257 int c1,
8258 int c2,
8259 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 int row;
8262 int col;
8263 int off;
8264 int end_off;
8265 int did_delete;
8266 int c;
8267 int norm_term;
8268#if defined(FEAT_GUI) || defined(UNIX)
8269 int force_next = FALSE;
8270#endif
8271
8272 if (end_row > screen_Rows) /* safety check */
8273 end_row = screen_Rows;
8274 if (end_col > screen_Columns) /* safety check */
8275 end_col = screen_Columns;
8276 if (ScreenLines == NULL
8277 || start_row >= end_row
8278 || start_col >= end_col) /* nothing to do */
8279 return;
8280
8281 /* it's a "normal" terminal when not in a GUI or cterm */
8282 norm_term = (
8283#ifdef FEAT_GUI
8284 !gui.in_use &&
8285#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008286 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 for (row = start_row; row < end_row; ++row)
8288 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008289#ifdef FEAT_MBYTE
8290 if (has_mbyte
8291# ifdef FEAT_GUI
8292 && !gui.in_use
8293# endif
8294 )
8295 {
8296 /* When drawing over the right halve of a double-wide char clear
8297 * out the left halve. When drawing over the left halve of a
8298 * double wide-char clear out the right halve. Only needed in a
8299 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008300 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008301 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008302 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008303 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008304 }
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 /*
8307 * Try to use delete-line termcap code, when no attributes or in a
8308 * "normal" terminal, where a bold/italic space is just a
8309 * space.
8310 */
8311 did_delete = FALSE;
8312 if (c2 == ' '
8313 && end_col == Columns
8314 && can_clear(T_CE)
8315 && (attr == 0
8316 || (norm_term
8317 && attr <= HL_ALL
8318 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8319 {
8320 /*
8321 * check if we really need to clear something
8322 */
8323 col = start_col;
8324 if (c1 != ' ') /* don't clear first char */
8325 ++col;
8326
8327 off = LineOffset[row] + col;
8328 end_off = LineOffset[row] + end_col;
8329
8330 /* skip blanks (used often, keep it fast!) */
8331#ifdef FEAT_MBYTE
8332 if (enc_utf8)
8333 while (off < end_off && ScreenLines[off] == ' '
8334 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8335 ++off;
8336 else
8337#endif
8338 while (off < end_off && ScreenLines[off] == ' '
8339 && ScreenAttrs[off] == 0)
8340 ++off;
8341 if (off < end_off) /* something to be cleared */
8342 {
8343 col = off - LineOffset[row];
8344 screen_stop_highlight();
8345 term_windgoto(row, col);/* clear rest of this screen line */
8346 out_str(T_CE);
8347 screen_start(); /* don't know where cursor is now */
8348 col = end_col - col;
8349 while (col--) /* clear chars in ScreenLines */
8350 {
8351 ScreenLines[off] = ' ';
8352#ifdef FEAT_MBYTE
8353 if (enc_utf8)
8354 ScreenLinesUC[off] = 0;
8355#endif
8356 ScreenAttrs[off] = 0;
8357 ++off;
8358 }
8359 }
8360 did_delete = TRUE; /* the chars are cleared now */
8361 }
8362
8363 off = LineOffset[row] + start_col;
8364 c = c1;
8365 for (col = start_col; col < end_col; ++col)
8366 {
8367 if (ScreenLines[off] != c
8368#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008369 || (enc_utf8 && (int)ScreenLinesUC[off]
8370 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#endif
8372 || ScreenAttrs[off] != attr
8373#if defined(FEAT_GUI) || defined(UNIX)
8374 || force_next
8375#endif
8376 )
8377 {
8378#if defined(FEAT_GUI) || defined(UNIX)
8379 /* The bold trick may make a single row of pixels appear in
8380 * the next character. When a bold character is removed, the
8381 * next character should be redrawn too. This happens for our
8382 * own GUI and for some xterms. */
8383 if (
8384# ifdef FEAT_GUI
8385 gui.in_use
8386# endif
8387# if defined(FEAT_GUI) && defined(UNIX)
8388 ||
8389# endif
8390# ifdef UNIX
8391 term_is_xterm
8392# endif
8393 )
8394 {
8395 if (ScreenLines[off] != ' '
8396 && (ScreenAttrs[off] > HL_ALL
8397 || ScreenAttrs[off] & HL_BOLD))
8398 force_next = TRUE;
8399 else
8400 force_next = FALSE;
8401 }
8402#endif
8403 ScreenLines[off] = c;
8404#ifdef FEAT_MBYTE
8405 if (enc_utf8)
8406 {
8407 if (c >= 0x80)
8408 {
8409 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008410 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412 else
8413 ScreenLinesUC[off] = 0;
8414 }
8415#endif
8416 ScreenAttrs[off] = attr;
8417 if (!did_delete || c != ' ')
8418 screen_char(off, row, col);
8419 }
8420 ++off;
8421 if (col == start_col)
8422 {
8423 if (did_delete)
8424 break;
8425 c = c2;
8426 }
8427 }
8428 if (end_col == Columns)
8429 LineWraps[row] = FALSE;
8430 if (row == Rows - 1) /* overwritten the command line */
8431 {
8432 redraw_cmdline = TRUE;
8433 if (c1 == ' ' && c2 == ' ')
8434 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008435 if (start_col == 0)
8436 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438 }
8439}
8440
8441/*
8442 * Check if there should be a delay. Used before clearing or redrawing the
8443 * screen or the command line.
8444 */
8445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008446check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8449 && !did_wait_return
8450 && emsg_silent == 0)
8451 {
8452 out_flush();
8453 ui_delay(1000L, TRUE);
8454 emsg_on_display = FALSE;
8455 if (check_msg_scroll)
8456 msg_scroll = FALSE;
8457 }
8458}
8459
8460/*
8461 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008462 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * Returns TRUE if there is a valid screen to write to.
8464 * Returns FALSE when starting up and screen not initialized yet.
8465 */
8466 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008467screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008469 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 return (ScreenLines != NULL);
8471}
8472
8473/*
8474 * Resize the shell to Rows and Columns.
8475 * Allocate ScreenLines[] and associated items.
8476 *
8477 * There may be some time between setting Rows and Columns and (re)allocating
8478 * ScreenLines[]. This happens when starting up and when (manually) changing
8479 * the shell size. Always use screen_Rows and screen_Columns to access items
8480 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8481 * final size of the shell is needed.
8482 */
8483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008484screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 int new_row, old_row;
8487#ifdef FEAT_GUI
8488 int old_Rows;
8489#endif
8490 win_T *wp;
8491 int outofmem = FALSE;
8492 int len;
8493 schar_T *new_ScreenLines;
8494#ifdef FEAT_MBYTE
8495 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008496 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008498 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#endif
8500 sattr_T *new_ScreenAttrs;
8501 unsigned *new_LineOffset;
8502 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008503#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008504 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008505 tabpage_T *tp;
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008508 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008509#ifdef FEAT_AUTOCMD
8510 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008512retry:
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 /*
8515 * Allocation of the screen buffers is done only when the size changes and
8516 * when Rows and Columns have been set and we have started doing full
8517 * screen stuff.
8518 */
8519 if ((ScreenLines != NULL
8520 && Rows == screen_Rows
8521 && Columns == screen_Columns
8522#ifdef FEAT_MBYTE
8523 && enc_utf8 == (ScreenLinesUC != NULL)
8524 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008525 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526#endif
8527 )
8528 || Rows == 0
8529 || Columns == 0
8530 || (!full_screen && ScreenLines == NULL))
8531 return;
8532
8533 /*
8534 * It's possible that we produce an out-of-memory message below, which
8535 * will cause this function to be called again. To break the loop, just
8536 * return here.
8537 */
8538 if (entered)
8539 return;
8540 entered = TRUE;
8541
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008542 /*
8543 * Note that the window sizes are updated before reallocating the arrays,
8544 * thus we must not redraw here!
8545 */
8546 ++RedrawingDisabled;
8547
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 win_new_shellsize(); /* fit the windows in the new sized shell */
8549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 comp_col(); /* recompute columns for shown command and ruler */
8551
8552 /*
8553 * We're changing the size of the screen.
8554 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8555 * - Move lines from the old arrays into the new arrays, clear extra
8556 * lines (unless the screen is going to be cleared).
8557 * - Free the old arrays.
8558 *
8559 * If anything fails, make ScreenLines NULL, so we don't do anything!
8560 * Continuing with the old ScreenLines may result in a crash, because the
8561 * size is wrong.
8562 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008563 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008565#ifdef FEAT_AUTOCMD
8566 if (aucmd_win != NULL)
8567 win_free_lsize(aucmd_win);
8568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 new_ScreenLines = (schar_T *)lalloc((long_u)(
8571 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8572#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008573 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (enc_utf8)
8575 {
8576 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8577 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008578 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008579 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8581 }
8582 if (enc_dbcs == DBCS_JPNU)
8583 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8584 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8585#endif
8586 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8587 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8588 new_LineOffset = (unsigned *)lalloc((long_u)(
8589 Rows * sizeof(unsigned)), FALSE);
8590 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008591#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008592 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008595 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {
8597 if (win_alloc_lines(wp) == FAIL)
8598 {
8599 outofmem = TRUE;
8600#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008601 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602#endif
8603 }
8604 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008605#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008606 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8607 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008608 outofmem = TRUE;
8609#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008610#ifdef FEAT_WINDOWS
8611give_up:
8612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008614#ifdef FEAT_MBYTE
8615 for (i = 0; i < p_mco; ++i)
8616 if (new_ScreenLinesC[i] == NULL)
8617 break;
8618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 if (new_ScreenLines == NULL
8620#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008621 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8623#endif
8624 || new_ScreenAttrs == NULL
8625 || new_LineOffset == NULL
8626 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008627#ifdef FEAT_WINDOWS
8628 || new_TabPageIdxs == NULL
8629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 || outofmem)
8631 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008632 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008633 {
8634 /* guess the size */
8635 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8636
8637 /* Remember we did this to avoid getting outofmem messages over
8638 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008639 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 vim_free(new_ScreenLines);
8642 new_ScreenLines = NULL;
8643#ifdef FEAT_MBYTE
8644 vim_free(new_ScreenLinesUC);
8645 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008646 for (i = 0; i < p_mco; ++i)
8647 {
8648 vim_free(new_ScreenLinesC[i]);
8649 new_ScreenLinesC[i] = NULL;
8650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 vim_free(new_ScreenLines2);
8652 new_ScreenLines2 = NULL;
8653#endif
8654 vim_free(new_ScreenAttrs);
8655 new_ScreenAttrs = NULL;
8656 vim_free(new_LineOffset);
8657 new_LineOffset = NULL;
8658 vim_free(new_LineWraps);
8659 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008660#ifdef FEAT_WINDOWS
8661 vim_free(new_TabPageIdxs);
8662 new_TabPageIdxs = NULL;
8663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 }
8665 else
8666 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008667 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008668
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 for (new_row = 0; new_row < Rows; ++new_row)
8670 {
8671 new_LineOffset[new_row] = new_row * Columns;
8672 new_LineWraps[new_row] = FALSE;
8673
8674 /*
8675 * If the screen is not going to be cleared, copy as much as
8676 * possible from the old screen to the new one and clear the rest
8677 * (used when resizing the window at the "--more--" prompt or when
8678 * executing an external command, for the GUI).
8679 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008680 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 {
8682 (void)vim_memset(new_ScreenLines + new_row * Columns,
8683 ' ', (size_t)Columns * sizeof(schar_T));
8684#ifdef FEAT_MBYTE
8685 if (enc_utf8)
8686 {
8687 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8688 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008689 for (i = 0; i < p_mco; ++i)
8690 (void)vim_memset(new_ScreenLinesC[i]
8691 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 0, (size_t)Columns * sizeof(u8char_T));
8693 }
8694 if (enc_dbcs == DBCS_JPNU)
8695 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8696 0, (size_t)Columns * sizeof(schar_T));
8697#endif
8698 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8699 0, (size_t)Columns * sizeof(sattr_T));
8700 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008701 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
8703 if (screen_Columns < Columns)
8704 len = screen_Columns;
8705 else
8706 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008707#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008708 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008709 * may be invalid now. Also when p_mco changes. */
8710 if (!(enc_utf8 && ScreenLinesUC == NULL)
8711 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008712#endif
8713 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8714 ScreenLines + LineOffset[old_row],
8715 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008717 if (enc_utf8 && ScreenLinesUC != NULL
8718 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {
8720 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8721 ScreenLinesUC + LineOffset[old_row],
8722 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 for (i = 0; i < p_mco; ++i)
8724 mch_memmove(new_ScreenLinesC[i]
8725 + new_LineOffset[new_row],
8726 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 (size_t)len * sizeof(u8char_T));
8728 }
8729 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8730 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8731 ScreenLines2 + LineOffset[old_row],
8732 (size_t)len * sizeof(schar_T));
8733#endif
8734 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8735 ScreenAttrs + LineOffset[old_row],
8736 (size_t)len * sizeof(sattr_T));
8737 }
8738 }
8739 }
8740 /* Use the last line of the screen for the current line. */
8741 current_ScreenLine = new_ScreenLines + Rows * Columns;
8742 }
8743
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008744 free_screenlines();
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 ScreenLines = new_ScreenLines;
8747#ifdef FEAT_MBYTE
8748 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008749 for (i = 0; i < p_mco; ++i)
8750 ScreenLinesC[i] = new_ScreenLinesC[i];
8751 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 ScreenLines2 = new_ScreenLines2;
8753#endif
8754 ScreenAttrs = new_ScreenAttrs;
8755 LineOffset = new_LineOffset;
8756 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008757#ifdef FEAT_WINDOWS
8758 TabPageIdxs = new_TabPageIdxs;
8759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
8761 /* It's important that screen_Rows and screen_Columns reflect the actual
8762 * size of ScreenLines[]. Set them before calling anything. */
8763#ifdef FEAT_GUI
8764 old_Rows = screen_Rows;
8765#endif
8766 screen_Rows = Rows;
8767 screen_Columns = Columns;
8768
8769 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008770 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 screenclear2();
8772
8773#ifdef FEAT_GUI
8774 else if (gui.in_use
8775 && !gui.starting
8776 && ScreenLines != NULL
8777 && old_Rows != Rows)
8778 {
8779 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8780 /*
8781 * Adjust the position of the cursor, for when executing an external
8782 * command.
8783 */
8784 if (msg_row >= Rows) /* Rows got smaller */
8785 msg_row = Rows - 1; /* put cursor at last row */
8786 else if (Rows > old_Rows) /* Rows got bigger */
8787 msg_row += Rows - old_Rows; /* put cursor in same place */
8788 if (msg_col >= Columns) /* Columns got smaller */
8789 msg_col = Columns - 1; /* put cursor at last column */
8790 }
8791#endif
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008794 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008795
8796#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008797 /*
8798 * Do not apply autocommands more than 3 times to avoid an endless loop
8799 * in case applying autocommands always changes Rows or Columns.
8800 */
8801 if (starting == 0 && ++retry_count <= 3)
8802 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008803 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008804 /* In rare cases, autocommands may have altered Rows or Columns,
8805 * jump back to check if we need to allocate the screen again. */
8806 goto retry;
8807 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809}
8810
8811 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008812free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008813{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008814#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 int i;
8816
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008817 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008818 for (i = 0; i < Screen_mco; ++i)
8819 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008820 vim_free(ScreenLines2);
8821#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008822 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008823 vim_free(ScreenAttrs);
8824 vim_free(LineOffset);
8825 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008826#ifdef FEAT_WINDOWS
8827 vim_free(TabPageIdxs);
8828#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008829}
8830
8831 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008832screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 check_for_delay(FALSE);
8835 screenalloc(FALSE); /* allocate screen buffers if size changed */
8836 screenclear2(); /* clear the screen */
8837}
8838
8839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008840screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841{
8842 int i;
8843
8844 if (starting == NO_SCREEN || ScreenLines == NULL
8845#ifdef FEAT_GUI
8846 || (gui.in_use && gui.starting)
8847#endif
8848 )
8849 return;
8850
8851#ifdef FEAT_GUI
8852 if (!gui.in_use)
8853#endif
8854 screen_attr = -1; /* force setting the Normal colors */
8855 screen_stop_highlight(); /* don't want highlighting here */
8856
8857#ifdef FEAT_CLIPBOARD
8858 /* disable selection without redrawing it */
8859 clip_scroll_selection(9999);
8860#endif
8861
8862 /* blank out ScreenLines */
8863 for (i = 0; i < Rows; ++i)
8864 {
8865 lineclear(LineOffset[i], (int)Columns);
8866 LineWraps[i] = FALSE;
8867 }
8868
8869 if (can_clear(T_CL))
8870 {
8871 out_str(T_CL); /* clear the display */
8872 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008873 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 else
8876 {
8877 /* can't clear the screen, mark all chars with invalid attributes */
8878 for (i = 0; i < Rows; ++i)
8879 lineinvalid(LineOffset[i], (int)Columns);
8880 clear_cmdline = TRUE;
8881 }
8882
8883 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8884
8885 win_rest_invalid(firstwin);
8886 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008887#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008888 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (must_redraw == CLEAR) /* no need to clear again */
8891 must_redraw = NOT_VALID;
8892 compute_cmdrow();
8893 msg_row = cmdline_row; /* put cursor on last line for messages */
8894 msg_col = 0;
8895 screen_start(); /* don't know where cursor is now */
8896 msg_scrolled = 0; /* can't scroll back */
8897 msg_didany = FALSE;
8898 msg_didout = FALSE;
8899}
8900
8901/*
8902 * Clear one line in ScreenLines.
8903 */
8904 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008905lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8908#ifdef FEAT_MBYTE
8909 if (enc_utf8)
8910 (void)vim_memset(ScreenLinesUC + off, 0,
8911 (size_t)width * sizeof(u8char_T));
8912#endif
8913 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8914}
8915
8916/*
8917 * Mark one line in ScreenLines invalid by setting the attributes to an
8918 * invalid value.
8919 */
8920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008921lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8924}
8925
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008926#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927/*
8928 * Copy part of a Screenline for vertically split window "wp".
8929 */
8930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008931linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 unsigned off_to = LineOffset[to] + wp->w_wincol;
8934 unsigned off_from = LineOffset[from] + wp->w_wincol;
8935
8936 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8937 wp->w_width * sizeof(schar_T));
8938# ifdef FEAT_MBYTE
8939 if (enc_utf8)
8940 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008941 int i;
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8944 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008945 for (i = 0; i < p_mco; ++i)
8946 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8947 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 }
8949 if (enc_dbcs == DBCS_JPNU)
8950 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8951 wp->w_width * sizeof(schar_T));
8952# endif
8953 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8954 wp->w_width * sizeof(sattr_T));
8955}
8956#endif
8957
8958/*
8959 * Return TRUE if clearing with term string "p" would work.
8960 * It can't work when the string is empty or it won't set the right background.
8961 */
8962 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008963can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 return (*p != NUL && (t_colors <= 1
8966#ifdef FEAT_GUI
8967 || gui.in_use
8968#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008969#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard18f6722016-06-17 13:18:49 +02008970 || (p_tgc && cterm_normal_bg_gui_color == (long_u)INVALCOLOR)
8971 || (!p_tgc && cterm_normal_bg_color == 0)
8972#else
8973 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008974#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02008975 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978/*
8979 * Reset cursor position. Use whenever cursor was moved because of outputting
8980 * something directly to the screen (shell commands) or a terminal control
8981 * code.
8982 */
8983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008984screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986 screen_cur_row = screen_cur_col = 9999;
8987}
8988
8989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 * Move the cursor to position "row","col" in the screen.
8991 * This tries to find the most efficient way to move, minimizing the number of
8992 * characters sent to the terminal.
8993 */
8994 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008995windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008997 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 int i;
8999 int plan;
9000 int cost;
9001 int wouldbe_col;
9002 int noinvcurs;
9003 char_u *bs;
9004 int goto_cost;
9005 int attr;
9006
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009007#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9009
9010#define PLAN_LE 1
9011#define PLAN_CR 2
9012#define PLAN_NL 3
9013#define PLAN_WRITE 4
9014 /* Can't use ScreenLines unless initialized */
9015 if (ScreenLines == NULL)
9016 return;
9017
9018 if (col != screen_cur_col || row != screen_cur_row)
9019 {
9020 /* Check for valid position. */
9021 if (row < 0) /* window without text lines? */
9022 row = 0;
9023 if (row >= screen_Rows)
9024 row = screen_Rows - 1;
9025 if (col >= screen_Columns)
9026 col = screen_Columns - 1;
9027
9028 /* check if no cursor movement is allowed in highlight mode */
9029 if (screen_attr && *T_MS == NUL)
9030 noinvcurs = HIGHL_COST;
9031 else
9032 noinvcurs = 0;
9033 goto_cost = GOTO_COST + noinvcurs;
9034
9035 /*
9036 * Plan how to do the positioning:
9037 * 1. Use CR to move it to column 0, same row.
9038 * 2. Use T_LE to move it a few columns to the left.
9039 * 3. Use NL to move a few lines down, column 0.
9040 * 4. Move a few columns to the right with T_ND or by writing chars.
9041 *
9042 * Don't do this if the cursor went beyond the last column, the cursor
9043 * position is unknown then (some terminals wrap, some don't )
9044 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009045 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 * characters to move the cursor to the right.
9047 */
9048 if (row >= screen_cur_row && screen_cur_col < Columns)
9049 {
9050 /*
9051 * If the cursor is in the same row, bigger col, we can use CR
9052 * or T_LE.
9053 */
9054 bs = NULL; /* init for GCC */
9055 attr = screen_attr;
9056 if (row == screen_cur_row && col < screen_cur_col)
9057 {
9058 /* "le" is preferred over "bc", because "bc" is obsolete */
9059 if (*T_LE)
9060 bs = T_LE; /* "cursor left" */
9061 else
9062 bs = T_BC; /* "backspace character (old) */
9063 if (*bs)
9064 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9065 else
9066 cost = 999;
9067 if (col + 1 < cost) /* using CR is less characters */
9068 {
9069 plan = PLAN_CR;
9070 wouldbe_col = 0;
9071 cost = 1; /* CR is just one character */
9072 }
9073 else
9074 {
9075 plan = PLAN_LE;
9076 wouldbe_col = col;
9077 }
9078 if (noinvcurs) /* will stop highlighting */
9079 {
9080 cost += noinvcurs;
9081 attr = 0;
9082 }
9083 }
9084
9085 /*
9086 * If the cursor is above where we want to be, we can use CR LF.
9087 */
9088 else if (row > screen_cur_row)
9089 {
9090 plan = PLAN_NL;
9091 wouldbe_col = 0;
9092 cost = (row - screen_cur_row) * 2; /* CR LF */
9093 if (noinvcurs) /* will stop highlighting */
9094 {
9095 cost += noinvcurs;
9096 attr = 0;
9097 }
9098 }
9099
9100 /*
9101 * If the cursor is in the same row, smaller col, just use write.
9102 */
9103 else
9104 {
9105 plan = PLAN_WRITE;
9106 wouldbe_col = screen_cur_col;
9107 cost = 0;
9108 }
9109
9110 /*
9111 * Check if any characters that need to be written have the
9112 * correct attributes. Also avoid UTF-8 characters.
9113 */
9114 i = col - wouldbe_col;
9115 if (i > 0)
9116 cost += i;
9117 if (cost < goto_cost && i > 0)
9118 {
9119 /*
9120 * Check if the attributes are correct without additionally
9121 * stopping highlighting.
9122 */
9123 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9124 while (i && *p++ == attr)
9125 --i;
9126 if (i != 0)
9127 {
9128 /*
9129 * Try if it works when highlighting is stopped here.
9130 */
9131 if (*--p == 0)
9132 {
9133 cost += noinvcurs;
9134 while (i && *p++ == 0)
9135 --i;
9136 }
9137 if (i != 0)
9138 cost = 999; /* different attributes, don't do it */
9139 }
9140#ifdef FEAT_MBYTE
9141 if (enc_utf8)
9142 {
9143 /* Don't use an UTF-8 char for positioning, it's slow. */
9144 for (i = wouldbe_col; i < col; ++i)
9145 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9146 {
9147 cost = 999;
9148 break;
9149 }
9150 }
9151#endif
9152 }
9153
9154 /*
9155 * We can do it without term_windgoto()!
9156 */
9157 if (cost < goto_cost)
9158 {
9159 if (plan == PLAN_LE)
9160 {
9161 if (noinvcurs)
9162 screen_stop_highlight();
9163 while (screen_cur_col > col)
9164 {
9165 out_str(bs);
9166 --screen_cur_col;
9167 }
9168 }
9169 else if (plan == PLAN_CR)
9170 {
9171 if (noinvcurs)
9172 screen_stop_highlight();
9173 out_char('\r');
9174 screen_cur_col = 0;
9175 }
9176 else if (plan == PLAN_NL)
9177 {
9178 if (noinvcurs)
9179 screen_stop_highlight();
9180 while (screen_cur_row < row)
9181 {
9182 out_char('\n');
9183 ++screen_cur_row;
9184 }
9185 screen_cur_col = 0;
9186 }
9187
9188 i = col - screen_cur_col;
9189 if (i > 0)
9190 {
9191 /*
9192 * Use cursor-right if it's one character only. Avoids
9193 * removing a line of pixels from the last bold char, when
9194 * using the bold trick in the GUI.
9195 */
9196 if (T_ND[0] != NUL && T_ND[1] == NUL)
9197 {
9198 while (i-- > 0)
9199 out_char(*T_ND);
9200 }
9201 else
9202 {
9203 int off;
9204
9205 off = LineOffset[row] + screen_cur_col;
9206 while (i-- > 0)
9207 {
9208 if (ScreenAttrs[off] != screen_attr)
9209 screen_stop_highlight();
9210#ifdef FEAT_MBYTE
9211 out_flush_check();
9212#endif
9213 out_char(ScreenLines[off]);
9214#ifdef FEAT_MBYTE
9215 if (enc_dbcs == DBCS_JPNU
9216 && ScreenLines[off] == 0x8e)
9217 out_char(ScreenLines2[off]);
9218#endif
9219 ++off;
9220 }
9221 }
9222 }
9223 }
9224 }
9225 else
9226 cost = 999;
9227
9228 if (cost >= goto_cost)
9229 {
9230 if (noinvcurs)
9231 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009232 if (row == screen_cur_row && (col > screen_cur_col)
9233 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 term_cursor_right(col - screen_cur_col);
9235 else
9236 term_windgoto(row, col);
9237 }
9238 screen_cur_row = row;
9239 screen_cur_col = col;
9240 }
9241}
9242
9243/*
9244 * Set cursor to its position in the current window.
9245 */
9246 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009247setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248{
9249 if (redrawing())
9250 {
9251 validate_cursor();
9252 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9253 W_WINCOL(curwin) + (
9254#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009255 /* With 'rightleft' set and the cursor on a double-wide
9256 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9258# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009259 (has_mbyte
9260 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9261 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262# endif
9263 1)) :
9264#endif
9265 curwin->w_wcol));
9266 }
9267}
9268
9269
9270/*
9271 * insert 'line_count' lines at 'row' in window 'wp'
9272 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9273 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9274 * scrolling.
9275 * Returns FAIL if the lines are not inserted, OK for success.
9276 */
9277 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009278win_ins_lines(
9279 win_T *wp,
9280 int row,
9281 int line_count,
9282 int invalid,
9283 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284{
9285 int did_delete;
9286 int nextrow;
9287 int lastrow;
9288 int retval;
9289
9290 if (invalid)
9291 wp->w_lines_valid = 0;
9292
9293 if (wp->w_height < 5)
9294 return FAIL;
9295
9296 if (line_count > wp->w_height - row)
9297 line_count = wp->w_height - row;
9298
9299 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9300 if (retval != MAYBE)
9301 return retval;
9302
9303 /*
9304 * If there is a next window or a status line, we first try to delete the
9305 * lines at the bottom to avoid messing what is after the window.
9306 * If this fails and there are following windows, don't do anything to avoid
9307 * messing up those windows, better just redraw.
9308 */
9309 did_delete = FALSE;
9310#ifdef FEAT_WINDOWS
9311 if (wp->w_next != NULL || wp->w_status_height)
9312 {
9313 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9314 line_count, (int)Rows, FALSE, NULL) == OK)
9315 did_delete = TRUE;
9316 else if (wp->w_next)
9317 return FAIL;
9318 }
9319#endif
9320 /*
9321 * if no lines deleted, blank the lines that will end up below the window
9322 */
9323 if (!did_delete)
9324 {
9325#ifdef FEAT_WINDOWS
9326 wp->w_redr_status = TRUE;
9327#endif
9328 redraw_cmdline = TRUE;
9329 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9330 lastrow = nextrow + line_count;
9331 if (lastrow > Rows)
9332 lastrow = Rows;
9333 screen_fill(nextrow - line_count, lastrow - line_count,
9334 W_WINCOL(wp), (int)W_ENDCOL(wp),
9335 ' ', ' ', 0);
9336 }
9337
9338 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9339 == FAIL)
9340 {
9341 /* deletion will have messed up other windows */
9342 if (did_delete)
9343 {
9344#ifdef FEAT_WINDOWS
9345 wp->w_redr_status = TRUE;
9346#endif
9347 win_rest_invalid(W_NEXT(wp));
9348 }
9349 return FAIL;
9350 }
9351
9352 return OK;
9353}
9354
9355/*
9356 * delete "line_count" window lines at "row" in window "wp"
9357 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9358 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9359 * scrolling
9360 * Return OK for success, FAIL if the lines are not deleted.
9361 */
9362 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009363win_del_lines(
9364 win_T *wp,
9365 int row,
9366 int line_count,
9367 int invalid,
9368 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 int retval;
9371
9372 if (invalid)
9373 wp->w_lines_valid = 0;
9374
9375 if (line_count > wp->w_height - row)
9376 line_count = wp->w_height - row;
9377
9378 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9379 if (retval != MAYBE)
9380 return retval;
9381
9382 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9383 (int)Rows, FALSE, NULL) == FAIL)
9384 return FAIL;
9385
9386#ifdef FEAT_WINDOWS
9387 /*
9388 * If there are windows or status lines below, try to put them at the
9389 * correct place. If we can't do that, they have to be redrawn.
9390 */
9391 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9392 {
9393 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9394 line_count, (int)Rows, NULL) == FAIL)
9395 {
9396 wp->w_redr_status = TRUE;
9397 win_rest_invalid(wp->w_next);
9398 }
9399 }
9400 /*
9401 * If this is the last window and there is no status line, redraw the
9402 * command line later.
9403 */
9404 else
9405#endif
9406 redraw_cmdline = TRUE;
9407 return OK;
9408}
9409
9410/*
9411 * Common code for win_ins_lines() and win_del_lines().
9412 * Returns OK or FAIL when the work has been done.
9413 * Returns MAYBE when not finished yet.
9414 */
9415 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009416win_do_lines(
9417 win_T *wp,
9418 int row,
9419 int line_count,
9420 int mayclear,
9421 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 int retval;
9424
9425 if (!redrawing() || line_count <= 0)
9426 return FAIL;
9427
9428 /* only a few lines left: redraw is faster */
9429 if (mayclear && Rows - line_count < 5
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009430#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 && wp->w_width == Columns
9432#endif
9433 )
9434 {
9435 screenclear(); /* will set wp->w_lines_valid to 0 */
9436 return FAIL;
9437 }
9438
9439 /*
9440 * Delete all remaining lines
9441 */
9442 if (row + line_count >= wp->w_height)
9443 {
9444 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9445 W_WINCOL(wp), (int)W_ENDCOL(wp),
9446 ' ', ' ', 0);
9447 return OK;
9448 }
9449
9450 /*
9451 * when scrolling, the message on the command line should be cleared,
9452 * otherwise it will stay there forever.
9453 */
9454 clear_cmdline = TRUE;
9455
9456 /*
9457 * If the terminal can set a scroll region, use that.
9458 * Always do this in a vertically split window. This will redraw from
9459 * ScreenLines[] when t_CV isn't defined. That's faster than using
9460 * win_line().
9461 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009462 * a character in the lower right corner of the scroll region may cause a
9463 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 */
9465 if (scroll_region
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009466#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 || W_WIDTH(wp) != Columns
9468#endif
9469 )
9470 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009471#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9473#endif
9474 scroll_region_set(wp, row);
9475 if (del)
9476 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9477 wp->w_height - row, FALSE, wp);
9478 else
9479 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9480 wp->w_height - row, wp);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009481#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9483#endif
9484 scroll_region_reset();
9485 return retval;
9486 }
9487
9488#ifdef FEAT_WINDOWS
9489 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9490 return FAIL;
9491#endif
9492
9493 return MAYBE;
9494}
9495
9496/*
9497 * window 'wp' and everything after it is messed up, mark it for redraw
9498 */
9499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009500win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#ifdef FEAT_WINDOWS
9503 while (wp != NULL)
9504#else
9505 if (wp != NULL)
9506#endif
9507 {
9508 redraw_win_later(wp, NOT_VALID);
9509#ifdef FEAT_WINDOWS
9510 wp->w_redr_status = TRUE;
9511 wp = wp->w_next;
9512#endif
9513 }
9514 redraw_cmdline = TRUE;
9515}
9516
9517/*
9518 * The rest of the routines in this file perform screen manipulations. The
9519 * given operation is performed physically on the screen. The corresponding
9520 * change is also made to the internal screen image. In this way, the editor
9521 * anticipates the effect of editing changes on the appearance of the screen.
9522 * That way, when we call screenupdate a complete redraw isn't usually
9523 * necessary. Another advantage is that we can keep adding code to anticipate
9524 * screen changes, and in the meantime, everything still works.
9525 */
9526
9527/*
9528 * types for inserting or deleting lines
9529 */
9530#define USE_T_CAL 1
9531#define USE_T_CDL 2
9532#define USE_T_AL 3
9533#define USE_T_CE 4
9534#define USE_T_DL 5
9535#define USE_T_SR 6
9536#define USE_NL 7
9537#define USE_T_CD 8
9538#define USE_REDRAW 9
9539
9540/*
9541 * insert lines on the screen and update ScreenLines[]
9542 * 'end' is the line after the scrolled part. Normally it is Rows.
9543 * When scrolling region used 'off' is the offset from the top for the region.
9544 * 'row' and 'end' are relative to the start of the region.
9545 *
9546 * return FAIL for failure, OK for success.
9547 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009548 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009549screen_ins_lines(
9550 int off,
9551 int row,
9552 int line_count,
9553 int end,
9554 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 int i;
9557 int j;
9558 unsigned temp;
9559 int cursor_row;
9560 int type;
9561 int result_empty;
9562 int can_ce = can_clear(T_CE);
9563
9564 /*
9565 * FAIL if
9566 * - there is no valid screen
9567 * - the screen has to be redrawn completely
9568 * - the line count is less than one
9569 * - the line count is more than 'ttyscroll'
9570 */
9571 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9572 return FAIL;
9573
9574 /*
9575 * There are seven ways to insert lines:
9576 * 0. When in a vertically split window and t_CV isn't set, redraw the
9577 * characters from ScreenLines[].
9578 * 1. Use T_CD (clear to end of display) if it exists and the result of
9579 * the insert is just empty lines
9580 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9581 * present or line_count > 1. It looks better if we do all the inserts
9582 * at once.
9583 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9584 * insert is just empty lines and T_CE is not present or line_count >
9585 * 1.
9586 * 4. Use T_AL (insert line) if it exists.
9587 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9588 * just empty lines.
9589 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9590 * just empty lines.
9591 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9592 * the 'da' flag is not set or we have clear line capability.
9593 * 8. redraw the characters from ScreenLines[].
9594 *
9595 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9596 * the scrollbar for the window. It does have insert line, use that if it
9597 * exists.
9598 */
9599 result_empty = (row + line_count >= end);
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009600#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9602 type = USE_REDRAW;
9603 else
9604#endif
9605 if (can_clear(T_CD) && result_empty)
9606 type = USE_T_CD;
9607 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9608 type = USE_T_CAL;
9609 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9610 type = USE_T_CDL;
9611 else if (*T_AL != NUL)
9612 type = USE_T_AL;
9613 else if (can_ce && result_empty)
9614 type = USE_T_CE;
9615 else if (*T_DL != NUL && result_empty)
9616 type = USE_T_DL;
9617 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9618 type = USE_T_SR;
9619 else
9620 return FAIL;
9621
9622 /*
9623 * For clearing the lines screen_del_lines() is used. This will also take
9624 * care of t_db if necessary.
9625 */
9626 if (type == USE_T_CD || type == USE_T_CDL ||
9627 type == USE_T_CE || type == USE_T_DL)
9628 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9629
9630 /*
9631 * If text is retained below the screen, first clear or delete as many
9632 * lines at the bottom of the window as are about to be inserted so that
9633 * the deleted lines won't later surface during a screen_del_lines.
9634 */
9635 if (*T_DB)
9636 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9637
9638#ifdef FEAT_CLIPBOARD
9639 /* Remove a modeless selection when inserting lines halfway the screen
9640 * or not the full width of the screen. */
9641 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009642# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 || (wp != NULL && wp->w_width != Columns)
9644# endif
9645 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009646 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 else
9648 clip_scroll_selection(-line_count);
9649#endif
9650
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651#ifdef FEAT_GUI
9652 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9653 * scrolling is actually carried out. */
9654 gui_dont_update_cursor();
9655#endif
9656
9657 if (*T_CCS != NUL) /* cursor relative to region */
9658 cursor_row = row;
9659 else
9660 cursor_row = row + off;
9661
9662 /*
9663 * Shift LineOffset[] line_count down to reflect the inserted lines.
9664 * Clear the inserted lines in ScreenLines[].
9665 */
9666 row += off;
9667 end += off;
9668 for (i = 0; i < line_count; ++i)
9669 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009670#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 if (wp != NULL && wp->w_width != Columns)
9672 {
9673 /* need to copy part of a line */
9674 j = end - 1 - i;
9675 while ((j -= line_count) >= row)
9676 linecopy(j + line_count, j, wp);
9677 j += line_count;
9678 if (can_clear((char_u *)" "))
9679 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9680 else
9681 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9682 LineWraps[j] = FALSE;
9683 }
9684 else
9685#endif
9686 {
9687 j = end - 1 - i;
9688 temp = LineOffset[j];
9689 while ((j -= line_count) >= row)
9690 {
9691 LineOffset[j + line_count] = LineOffset[j];
9692 LineWraps[j + line_count] = LineWraps[j];
9693 }
9694 LineOffset[j + line_count] = temp;
9695 LineWraps[j + line_count] = FALSE;
9696 if (can_clear((char_u *)" "))
9697 lineclear(temp, (int)Columns);
9698 else
9699 lineinvalid(temp, (int)Columns);
9700 }
9701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702
9703 screen_stop_highlight();
9704 windgoto(cursor_row, 0);
9705
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009706#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 /* redraw the characters */
9708 if (type == USE_REDRAW)
9709 redraw_block(row, end, wp);
9710 else
9711#endif
9712 if (type == USE_T_CAL)
9713 {
9714 term_append_lines(line_count);
9715 screen_start(); /* don't know where cursor is now */
9716 }
9717 else
9718 {
9719 for (i = 0; i < line_count; i++)
9720 {
9721 if (type == USE_T_AL)
9722 {
9723 if (i && cursor_row != 0)
9724 windgoto(cursor_row, 0);
9725 out_str(T_AL);
9726 }
9727 else /* type == USE_T_SR */
9728 out_str(T_SR);
9729 screen_start(); /* don't know where cursor is now */
9730 }
9731 }
9732
9733 /*
9734 * With scroll-reverse and 'da' flag set we need to clear the lines that
9735 * have been scrolled down into the region.
9736 */
9737 if (type == USE_T_SR && *T_DA)
9738 {
9739 for (i = 0; i < line_count; ++i)
9740 {
9741 windgoto(off + i, 0);
9742 out_str(T_CE);
9743 screen_start(); /* don't know where cursor is now */
9744 }
9745 }
9746
9747#ifdef FEAT_GUI
9748 gui_can_update_cursor();
9749 if (gui.in_use)
9750 out_flush(); /* always flush after a scroll */
9751#endif
9752 return OK;
9753}
9754
9755/*
9756 * delete lines on the screen and update ScreenLines[]
9757 * 'end' is the line after the scrolled part. Normally it is Rows.
9758 * When scrolling region used 'off' is the offset from the top for the region.
9759 * 'row' and 'end' are relative to the start of the region.
9760 *
9761 * Return OK for success, FAIL if the lines are not deleted.
9762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009764screen_del_lines(
9765 int off,
9766 int row,
9767 int line_count,
9768 int end,
9769 int force, /* even when line_count > p_ttyscroll */
9770 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 int j;
9773 int i;
9774 unsigned temp;
9775 int cursor_row;
9776 int cursor_end;
9777 int result_empty; /* result is empty until end of region */
9778 int can_delete; /* deleting line codes can be used */
9779 int type;
9780
9781 /*
9782 * FAIL if
9783 * - there is no valid screen
9784 * - the screen has to be redrawn completely
9785 * - the line count is less than one
9786 * - the line count is more than 'ttyscroll'
9787 */
9788 if (!screen_valid(TRUE) || line_count <= 0 ||
9789 (!force && line_count > p_ttyscroll))
9790 return FAIL;
9791
9792 /*
9793 * Check if the rest of the current region will become empty.
9794 */
9795 result_empty = row + line_count >= end;
9796
9797 /*
9798 * We can delete lines only when 'db' flag not set or when 'ce' option
9799 * available.
9800 */
9801 can_delete = (*T_DB == NUL || can_clear(T_CE));
9802
9803 /*
9804 * There are six ways to delete lines:
9805 * 0. When in a vertically split window and t_CV isn't set, redraw the
9806 * characters from ScreenLines[].
9807 * 1. Use T_CD if it exists and the result is empty.
9808 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9809 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9810 * none of the other ways work.
9811 * 4. Use T_CE (erase line) if the result is empty.
9812 * 5. Use T_DL (delete line) if it exists.
9813 * 6. redraw the characters from ScreenLines[].
9814 */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009815#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9817 type = USE_REDRAW;
9818 else
9819#endif
9820 if (can_clear(T_CD) && result_empty)
9821 type = USE_T_CD;
9822#if defined(__BEOS__) && defined(BEOS_DR8)
9823 /*
9824 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9825 * its internal termcap... this works okay for tests which test *T_DB !=
9826 * NUL. It has the disadvantage that the user cannot use any :set t_*
9827 * command to get T_DB (back) to empty_option, only :set term=... will do
9828 * the trick...
9829 * Anyway, this hack will hopefully go away with the next OS release.
9830 * (Olaf Seibert)
9831 */
9832 else if (row == 0 && T_DB == empty_option
9833 && (line_count == 1 || *T_CDL == NUL))
9834#else
9835 else if (row == 0 && (
9836#ifndef AMIGA
9837 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9838 * up, so use delete-line command */
9839 line_count == 1 ||
9840#endif
9841 *T_CDL == NUL))
9842#endif
9843 type = USE_NL;
9844 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9845 type = USE_T_CDL;
9846 else if (can_clear(T_CE) && result_empty
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009847#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 && (wp == NULL || wp->w_width == Columns)
9849#endif
9850 )
9851 type = USE_T_CE;
9852 else if (*T_DL != NUL && can_delete)
9853 type = USE_T_DL;
9854 else if (*T_CDL != NUL && can_delete)
9855 type = USE_T_CDL;
9856 else
9857 return FAIL;
9858
9859#ifdef FEAT_CLIPBOARD
9860 /* Remove a modeless selection when deleting lines halfway the screen or
9861 * not the full width of the screen. */
9862 if (off + row > 0
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009863# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 || (wp != NULL && wp->w_width != Columns)
9865# endif
9866 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009867 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 else
9869 clip_scroll_selection(line_count);
9870#endif
9871
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#ifdef FEAT_GUI
9873 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9874 * scrolling is actually carried out. */
9875 gui_dont_update_cursor();
9876#endif
9877
9878 if (*T_CCS != NUL) /* cursor relative to region */
9879 {
9880 cursor_row = row;
9881 cursor_end = end;
9882 }
9883 else
9884 {
9885 cursor_row = row + off;
9886 cursor_end = end + off;
9887 }
9888
9889 /*
9890 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9891 * Clear the inserted lines in ScreenLines[].
9892 */
9893 row += off;
9894 end += off;
9895 for (i = 0; i < line_count; ++i)
9896 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009897#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 if (wp != NULL && wp->w_width != Columns)
9899 {
9900 /* need to copy part of a line */
9901 j = row + i;
9902 while ((j += line_count) <= end - 1)
9903 linecopy(j - line_count, j, wp);
9904 j -= line_count;
9905 if (can_clear((char_u *)" "))
9906 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9907 else
9908 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9909 LineWraps[j] = FALSE;
9910 }
9911 else
9912#endif
9913 {
9914 /* whole width, moving the line pointers is faster */
9915 j = row + i;
9916 temp = LineOffset[j];
9917 while ((j += line_count) <= end - 1)
9918 {
9919 LineOffset[j - line_count] = LineOffset[j];
9920 LineWraps[j - line_count] = LineWraps[j];
9921 }
9922 LineOffset[j - line_count] = temp;
9923 LineWraps[j - line_count] = FALSE;
9924 if (can_clear((char_u *)" "))
9925 lineclear(temp, (int)Columns);
9926 else
9927 lineinvalid(temp, (int)Columns);
9928 }
9929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930
9931 screen_stop_highlight();
9932
Bram Moolenaar44a2f922016-03-19 22:11:51 +01009933#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 /* redraw the characters */
9935 if (type == USE_REDRAW)
9936 redraw_block(row, end, wp);
9937 else
9938#endif
9939 if (type == USE_T_CD) /* delete the lines */
9940 {
9941 windgoto(cursor_row, 0);
9942 out_str(T_CD);
9943 screen_start(); /* don't know where cursor is now */
9944 }
9945 else if (type == USE_T_CDL)
9946 {
9947 windgoto(cursor_row, 0);
9948 term_delete_lines(line_count);
9949 screen_start(); /* don't know where cursor is now */
9950 }
9951 /*
9952 * Deleting lines at top of the screen or scroll region: Just scroll
9953 * the whole screen (scroll region) up by outputting newlines on the
9954 * last line.
9955 */
9956 else if (type == USE_NL)
9957 {
9958 windgoto(cursor_end - 1, 0);
9959 for (i = line_count; --i >= 0; )
9960 out_char('\n'); /* cursor will remain on same line */
9961 }
9962 else
9963 {
9964 for (i = line_count; --i >= 0; )
9965 {
9966 if (type == USE_T_DL)
9967 {
9968 windgoto(cursor_row, 0);
9969 out_str(T_DL); /* delete a line */
9970 }
9971 else /* type == USE_T_CE */
9972 {
9973 windgoto(cursor_row + i, 0);
9974 out_str(T_CE); /* erase a line */
9975 }
9976 screen_start(); /* don't know where cursor is now */
9977 }
9978 }
9979
9980 /*
9981 * If the 'db' flag is set, we need to clear the lines that have been
9982 * scrolled up at the bottom of the region.
9983 */
9984 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9985 {
9986 for (i = line_count; i > 0; --i)
9987 {
9988 windgoto(cursor_end - i, 0);
9989 out_str(T_CE); /* erase a line */
9990 screen_start(); /* don't know where cursor is now */
9991 }
9992 }
9993
9994#ifdef FEAT_GUI
9995 gui_can_update_cursor();
9996 if (gui.in_use)
9997 out_flush(); /* always flush after a scroll */
9998#endif
9999
10000 return OK;
10001}
10002
10003/*
10004 * show the current mode and ruler
10005 *
10006 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10007 * If clear_cmdline is FALSE there may be a message there that needs to be
10008 * cleared only if a mode is shown.
10009 * Return the length of the message (0 if no message).
10010 */
10011 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010012showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 int need_clear;
10015 int length = 0;
10016 int do_mode;
10017 int attr;
10018 int nwr_save;
10019#ifdef FEAT_INS_EXPAND
10020 int sub_attr;
10021#endif
10022
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010023 do_mode = ((p_smd && msg_silent == 0)
10024 && ((State & INSERT)
10025 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010026 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if (do_mode || Recording)
10028 {
10029 /*
10030 * Don't show mode right now, when not redrawing or inside a mapping.
10031 * Call char_avail() only when we are going to show something, because
10032 * it takes a bit of time.
10033 */
10034 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10035 {
10036 redraw_cmdline = TRUE; /* show mode later */
10037 return 0;
10038 }
10039
10040 nwr_save = need_wait_return;
10041
10042 /* wait a bit before overwriting an important message */
10043 check_for_delay(FALSE);
10044
10045 /* if the cmdline is more than one line high, erase top lines */
10046 need_clear = clear_cmdline;
10047 if (clear_cmdline && cmdline_row < Rows - 1)
10048 msg_clr_cmdline(); /* will reset clear_cmdline */
10049
10050 /* Position on the last line in the window, column 0 */
10051 msg_pos_mode();
10052 cursor_off();
10053 attr = hl_attr(HLF_CM); /* Highlight mode */
10054 if (do_mode)
10055 {
10056 MSG_PUTS_ATTR("--", attr);
10057#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010058 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010059# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010060 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010061# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010062 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010063# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010064 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010065# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 MSG_PUTS_ATTR(" IM", attr);
10067# else
10068 MSG_PUTS_ATTR(" XIM", attr);
10069# endif
10070#endif
10071#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10072 if (gui.in_use)
10073 {
10074 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010075 {
10076 /* HANGUL */
10077 if (enc_utf8)
10078 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10079 else
10080 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 }
10083#endif
10084#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010085 /* CTRL-X in Insert mode */
10086 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
10088 /* These messages can get long, avoid a wrap in a narrow
10089 * window. Prefer showing edit_submode_extra. */
10090 length = (Rows - msg_row) * Columns - 3;
10091 if (edit_submode_extra != NULL)
10092 length -= vim_strsize(edit_submode_extra);
10093 if (length > 0)
10094 {
10095 if (edit_submode_pre != NULL)
10096 length -= vim_strsize(edit_submode_pre);
10097 if (length - vim_strsize(edit_submode) > 0)
10098 {
10099 if (edit_submode_pre != NULL)
10100 msg_puts_attr(edit_submode_pre, attr);
10101 msg_puts_attr(edit_submode, attr);
10102 }
10103 if (edit_submode_extra != NULL)
10104 {
10105 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10106 if ((int)edit_submode_highl < (int)HLF_COUNT)
10107 sub_attr = hl_attr(edit_submode_highl);
10108 else
10109 sub_attr = attr;
10110 msg_puts_attr(edit_submode_extra, sub_attr);
10111 }
10112 }
10113 length = 0;
10114 }
10115 else
10116#endif
10117 {
10118#ifdef FEAT_VREPLACE
10119 if (State & VREPLACE_FLAG)
10120 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10121 else
10122#endif
10123 if (State & REPLACE_FLAG)
10124 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10125 else if (State & INSERT)
10126 {
10127#ifdef FEAT_RIGHTLEFT
10128 if (p_ri)
10129 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10130#endif
10131 MSG_PUTS_ATTR(_(" INSERT"), attr);
10132 }
10133 else if (restart_edit == 'I')
10134 MSG_PUTS_ATTR(_(" (insert)"), attr);
10135 else if (restart_edit == 'R')
10136 MSG_PUTS_ATTR(_(" (replace)"), attr);
10137 else if (restart_edit == 'V')
10138 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10139#ifdef FEAT_RIGHTLEFT
10140 if (p_hkmap)
10141 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10142# ifdef FEAT_FKMAP
10143 if (p_fkmap)
10144 MSG_PUTS_ATTR(farsi_text_5, attr);
10145# endif
10146#endif
10147#ifdef FEAT_KEYMAP
10148 if (State & LANGMAP)
10149 {
10150# ifdef FEAT_ARABIC
10151 if (curwin->w_p_arab)
10152 MSG_PUTS_ATTR(_(" Arabic"), attr);
10153 else
10154# endif
10155 MSG_PUTS_ATTR(_(" (lang)"), attr);
10156 }
10157#endif
10158 if ((State & INSERT) && p_paste)
10159 MSG_PUTS_ATTR(_(" (paste)"), attr);
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 if (VIsual_active)
10162 {
10163 char *p;
10164
10165 /* Don't concatenate separate words to avoid translation
10166 * problems. */
10167 switch ((VIsual_select ? 4 : 0)
10168 + (VIsual_mode == Ctrl_V) * 2
10169 + (VIsual_mode == 'V'))
10170 {
10171 case 0: p = N_(" VISUAL"); break;
10172 case 1: p = N_(" VISUAL LINE"); break;
10173 case 2: p = N_(" VISUAL BLOCK"); break;
10174 case 4: p = N_(" SELECT"); break;
10175 case 5: p = N_(" SELECT LINE"); break;
10176 default: p = N_(" SELECT BLOCK"); break;
10177 }
10178 MSG_PUTS_ATTR(_(p), attr);
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 MSG_PUTS_ATTR(" --", attr);
10181 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 need_clear = TRUE;
10184 }
10185 if (Recording
10186#ifdef FEAT_INS_EXPAND
10187 && edit_submode == NULL /* otherwise it gets too long */
10188#endif
10189 )
10190 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010191 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 need_clear = TRUE;
10193 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010194
10195 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 if (need_clear || clear_cmdline)
10197 msg_clr_eos();
10198 msg_didout = FALSE; /* overwrite this message */
10199 length = msg_col;
10200 msg_col = 0;
10201 need_wait_return = nwr_save; /* never ask for hit-return for this */
10202 }
10203 else if (clear_cmdline && msg_silent == 0)
10204 /* Clear the whole command line. Will reset "clear_cmdline". */
10205 msg_clr_cmdline();
10206
10207#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 /* In Visual mode the size of the selected area must be redrawn. */
10209 if (VIsual_active)
10210 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211
10212 /* If the last window has no status line, the ruler is after the mode
10213 * message and must be redrawn */
10214 if (redrawing()
10215# ifdef FEAT_WINDOWS
10216 && lastwin->w_status_height == 0
10217# endif
10218 )
10219 win_redr_ruler(lastwin, TRUE);
10220#endif
10221 redraw_cmdline = FALSE;
10222 clear_cmdline = FALSE;
10223
10224 return length;
10225}
10226
10227/*
10228 * Position for a mode message.
10229 */
10230 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010231msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
10233 msg_col = 0;
10234 msg_row = Rows - 1;
10235}
10236
10237/*
10238 * Delete mode message. Used when ESC is typed which is expected to end
10239 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010240 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 */
10242 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010243unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244{
10245 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010246 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 */
10248 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10249 redraw_cmdline = TRUE; /* delete mode later */
10250 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010251 clearmode();
10252}
10253
10254/*
10255 * Clear the mode message.
10256 */
10257 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010258clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010259{
10260 msg_pos_mode();
10261 if (Recording)
10262 recording_mode(hl_attr(HLF_CM));
10263 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264}
10265
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010266 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010267recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010268{
10269 MSG_PUTS_ATTR(_("recording"), attr);
10270 if (!shortmess(SHM_RECORDING))
10271 {
10272 char_u s[4];
10273 sprintf((char *)s, " @%c", Recording);
10274 MSG_PUTS_ATTR(s, attr);
10275 }
10276}
10277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010278#if defined(FEAT_WINDOWS)
10279/*
10280 * Draw the tab pages line at the top of the Vim window.
10281 */
10282 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010283draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010284{
10285 int tabcount = 0;
10286 tabpage_T *tp;
10287 int tabwidth;
10288 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010289 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010290 int attr;
10291 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010292 win_T *cwp;
10293 int wincount;
10294 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010295 int c;
10296 int len;
10297 int attr_sel = hl_attr(HLF_TPS);
10298 int attr_nosel = hl_attr(HLF_TP);
10299 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010300 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010301 int room;
10302 int use_sep_chars = (t_colors < 8
10303#ifdef FEAT_GUI
10304 && !gui.in_use
10305#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010306#ifdef FEAT_TERMGUICOLORS
10307 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010308#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010309 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010310
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010311 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010312
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010313#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010314 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010315 if (gui_use_tabline())
10316 {
10317 gui_update_tabline();
10318 return;
10319 }
10320#endif
10321
10322 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010323 return;
10324
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010325#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010326
10327 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10328 for (scol = 0; scol < Columns; ++scol)
10329 TabPageIdxs[scol] = 0;
10330
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010331 /* Use the 'tabline' option if it's set. */
10332 if (*p_tal != NUL)
10333 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010334 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010335
10336 /* Check for an error. If there is one we would loop in redrawing the
10337 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010338 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010339 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010340 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010341 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010342 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010343 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010344 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010345 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010346#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010347 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010348 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10349 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010350
Bram Moolenaar238a5642006-02-21 22:12:05 +000010351 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10352 if (tabwidth < 6)
10353 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010354
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355 attr = attr_nosel;
10356 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010357 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010358 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10359 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010360 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010361 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010362
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363 if (tp->tp_topframe == topframe)
10364 attr = attr_sel;
10365 if (use_sep_chars && col > 0)
10366 screen_putchar('|', 0, col++, attr);
10367
10368 if (tp->tp_topframe != topframe)
10369 attr = attr_nosel;
10370
10371 screen_putchar(' ', 0, col++, attr);
10372
10373 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010374 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010375 cwp = curwin;
10376 wp = firstwin;
10377 }
10378 else
10379 {
10380 cwp = tp->tp_curwin;
10381 wp = tp->tp_firstwin;
10382 }
10383
10384 modified = FALSE;
10385 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10386 if (bufIsChanged(wp->w_buffer))
10387 modified = TRUE;
10388 if (modified || wincount > 1)
10389 {
10390 if (wincount > 1)
10391 {
10392 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010393 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010394 if (col + len >= Columns - 3)
10395 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010396 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010397#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010398 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010399#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010400 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010401#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010402 );
10403 col += len;
10404 }
10405 if (modified)
10406 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10407 screen_putchar(' ', 0, col++, attr);
10408 }
10409
10410 room = scol - col + tabwidth - 1;
10411 if (room > 0)
10412 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010413 /* Get buffer name in NameBuff[] */
10414 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010415 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010416 len = vim_strsize(NameBuff);
10417 p = NameBuff;
10418#ifdef FEAT_MBYTE
10419 if (has_mbyte)
10420 while (len > room)
10421 {
10422 len -= ptr2cells(p);
10423 mb_ptr_adv(p);
10424 }
10425 else
10426#endif
10427 if (len > room)
10428 {
10429 p += len - room;
10430 len = room;
10431 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010432 if (len > Columns - col - 1)
10433 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010435 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010436 col += len;
10437 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010438 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010439
10440 /* Store the tab page number in TabPageIdxs[], so that
10441 * jump_to_mouse() knows where each one is. */
10442 ++tabcount;
10443 while (scol < col)
10444 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010445 }
10446
Bram Moolenaar238a5642006-02-21 22:12:05 +000010447 if (use_sep_chars)
10448 c = '_';
10449 else
10450 c = ' ';
10451 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010452
10453 /* Put an "X" for closing the current tab if there are several. */
10454 if (first_tabpage->tp_next != NULL)
10455 {
10456 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10457 TabPageIdxs[Columns - 1] = -999;
10458 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010459 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010460
10461 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10462 * set. */
10463 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010464}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010465
10466/*
10467 * Get buffer name for "buf" into NameBuff[].
10468 * Takes care of special buffer names and translates special characters.
10469 */
10470 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010471get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010472{
10473 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010474 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010475 else
10476 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10477 trans_characters(NameBuff, MAXPATHL);
10478}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010479#endif
10480
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10482/*
10483 * Get the character to use in a status line. Get its attributes in "*attr".
10484 */
10485 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010486fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
10488 int fill;
10489 if (is_curwin)
10490 {
10491 *attr = hl_attr(HLF_S);
10492 fill = fill_stl;
10493 }
10494 else
10495 {
10496 *attr = hl_attr(HLF_SNC);
10497 fill = fill_stlnc;
10498 }
10499 /* Use fill when there is highlighting, and highlighting of current
10500 * window differs, or the fillchars differ, or this is not the
10501 * current window */
10502 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10503 || !is_curwin || firstwin == lastwin)
10504 || (fill_stl != fill_stlnc)))
10505 return fill;
10506 if (is_curwin)
10507 return '^';
10508 return '=';
10509}
10510#endif
10511
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010512#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513/*
10514 * Get the character to use in a separator between vertically split windows.
10515 * Get its attributes in "*attr".
10516 */
10517 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010518fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520 *attr = hl_attr(HLF_C);
10521 if (*attr == 0 && fill_vert == ' ')
10522 return '|';
10523 else
10524 return fill_vert;
10525}
10526#endif
10527
10528/*
10529 * Return TRUE if redrawing should currently be done.
10530 */
10531 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010532redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533{
10534 return (!RedrawingDisabled
10535 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10536}
10537
10538/*
10539 * Return TRUE if printing messages should currently be done.
10540 */
10541 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010542messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543{
10544 return (!(p_lz && char_avail() && !KeyTyped));
10545}
10546
10547/*
10548 * Show current status info in ruler and various other places
10549 * If always is FALSE, only show ruler if position has changed.
10550 */
10551 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010552showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554 if (!always && !redrawing())
10555 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010556#ifdef FEAT_INS_EXPAND
10557 if (pum_visible())
10558 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010559# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010560 /* Don't redraw right now, do it later. */
10561 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010562# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010563 return;
10564 }
10565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010567 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010568 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010569 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 else
10572#endif
10573#ifdef FEAT_CMDL_INFO
10574 win_redr_ruler(curwin, always);
10575#endif
10576
10577#ifdef FEAT_TITLE
10578 if (need_maketitle
10579# ifdef FEAT_STL_OPT
10580 || (p_icon && (stl_syntax & STL_IN_ICON))
10581 || (p_title && (stl_syntax & STL_IN_TITLE))
10582# endif
10583 )
10584 maketitle();
10585#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010586#ifdef FEAT_WINDOWS
10587 /* Redraw the tab pages line if needed. */
10588 if (redraw_tabline)
10589 draw_tabline();
10590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591}
10592
10593#ifdef FEAT_CMDL_INFO
10594 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010595win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010597#define RULER_BUF_LEN 70
10598 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 int row;
10600 int fillchar;
10601 int attr;
10602 int empty_line = FALSE;
10603 colnr_T virtcol;
10604 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010605 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 int o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010607#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 int this_ru_col;
10609 int off = 0;
10610 int width = Columns;
10611# define WITH_OFF(x) x
10612# define WITH_WIDTH(x) x
10613#else
10614# define WITH_OFF(x) 0
10615# define WITH_WIDTH(x) Columns
10616# define this_ru_col ru_col
10617#endif
10618
10619 /* If 'ruler' off or redrawing disabled, don't do anything */
10620 if (!p_ru)
10621 return;
10622
10623 /*
10624 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10625 * after deleting lines, before cursor.lnum is corrected.
10626 */
10627 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10628 return;
10629
10630#ifdef FEAT_INS_EXPAND
10631 /* Don't draw the ruler while doing insert-completion, it might overwrite
10632 * the (long) mode message. */
10633# ifdef FEAT_WINDOWS
10634 if (wp == lastwin && lastwin->w_status_height == 0)
10635# endif
10636 if (edit_submode != NULL)
10637 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010638 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10639 if (pum_visible())
10640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641#endif
10642
10643#ifdef FEAT_STL_OPT
10644 if (*p_ruf)
10645 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 int save_called_emsg = called_emsg;
10647
10648 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010650 if (called_emsg)
10651 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010652 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010653 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 return;
10655 }
10656#endif
10657
10658 /*
10659 * Check if not in Insert mode and the line is empty (will show "0-1").
10660 */
10661 if (!(State & INSERT)
10662 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10663 empty_line = TRUE;
10664
10665 /*
10666 * Only draw the ruler when something changed.
10667 */
10668 validate_virtcol_win(wp);
10669 if ( redraw_cmdline
10670 || always
10671 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10672 || wp->w_cursor.col != wp->w_ru_cursor.col
10673 || wp->w_virtcol != wp->w_ru_virtcol
10674#ifdef FEAT_VIRTUALEDIT
10675 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10676#endif
10677 || wp->w_topline != wp->w_ru_topline
10678 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10679#ifdef FEAT_DIFF
10680 || wp->w_topfill != wp->w_ru_topfill
10681#endif
10682 || empty_line != wp->w_ru_empty)
10683 {
10684 cursor_off();
10685#ifdef FEAT_WINDOWS
10686 if (wp->w_status_height)
10687 {
10688 row = W_WINROW(wp) + wp->w_height;
10689 fillchar = fillchar_status(&attr, wp == curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 off = W_WINCOL(wp);
10691 width = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692 }
10693 else
10694#endif
10695 {
10696 row = Rows - 1;
10697 fillchar = ' ';
10698 attr = 0;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010699#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 width = Columns;
10701 off = 0;
10702#endif
10703 }
10704
10705 /* In list mode virtcol needs to be recomputed */
10706 virtcol = wp->w_virtcol;
10707 if (wp->w_p_list && lcs_tab1 == NUL)
10708 {
10709 wp->w_p_list = FALSE;
10710 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10711 wp->w_p_list = TRUE;
10712 }
10713
10714 /*
10715 * Some sprintfs return the length, some return a pointer.
10716 * To avoid portability problems we use strlen() here.
10717 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010718 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10720 ? 0L
10721 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010722 len = STRLEN(buffer);
10723 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10725 (int)virtcol + 1);
10726
10727 /*
10728 * Add a "50%" if there is room for it.
10729 * On the last line, don't print in the last column (scrolls the
10730 * screen up on some terminals).
10731 */
10732 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010733 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 o = i + vim_strsize(buffer + i + 1);
10735#ifdef FEAT_WINDOWS
10736 if (wp->w_status_height == 0) /* can't use last char of screen */
10737#endif
10738 ++o;
Bram Moolenaar44a2f922016-03-19 22:11:51 +010010739#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 this_ru_col = ru_col - (Columns - width);
10741 if (this_ru_col < 0)
10742 this_ru_col = 0;
10743#endif
10744 /* Never use more than half the window/screen width, leave the other
10745 * half for the filename. */
10746 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10747 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10748 if (this_ru_col + o < WITH_WIDTH(width))
10749 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010750 /* need at least 3 chars left for get_rel_pos() + NUL */
10751 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 {
10753#ifdef FEAT_MBYTE
10754 if (has_mbyte)
10755 i += (*mb_char2bytes)(fillchar, buffer + i);
10756 else
10757#endif
10758 buffer[i++] = fillchar;
10759 ++o;
10760 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010761 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 }
10763 /* Truncate at window boundary. */
10764#ifdef FEAT_MBYTE
10765 if (has_mbyte)
10766 {
10767 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010768 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
10770 o += (*mb_ptr2cells)(buffer + i);
10771 if (this_ru_col + o > WITH_WIDTH(width))
10772 {
10773 buffer[i] = NUL;
10774 break;
10775 }
10776 }
10777 }
10778 else
10779#endif
10780 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10781 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10782
10783 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10784 i = redraw_cmdline;
10785 screen_fill(row, row + 1,
10786 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10787 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10788 fillchar, fillchar, attr);
10789 /* don't redraw the cmdline because of showing the ruler */
10790 redraw_cmdline = i;
10791 wp->w_ru_cursor = wp->w_cursor;
10792 wp->w_ru_virtcol = wp->w_virtcol;
10793 wp->w_ru_empty = empty_line;
10794 wp->w_ru_topline = wp->w_topline;
10795 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10796#ifdef FEAT_DIFF
10797 wp->w_ru_topfill = wp->w_topfill;
10798#endif
10799 }
10800}
10801#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010802
10803#if defined(FEAT_LINEBREAK) || defined(PROTO)
10804/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010805 * Return the width of the 'number' and 'relativenumber' column.
10806 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010807 * Otherwise it depends on 'numberwidth' and the line count.
10808 */
10809 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010810number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010811{
10812 int n;
10813 linenr_T lnum;
10814
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010815 if (wp->w_p_rnu && !wp->w_p_nu)
10816 /* cursor line shows "0" */
10817 lnum = wp->w_height;
10818 else
10819 /* cursor line shows absolute line number */
10820 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010821
Bram Moolenaar6b314672015-03-20 15:42:10 +010010822 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010823 return wp->w_nrwidth_width;
10824 wp->w_nrwidth_line_count = lnum;
10825
10826 n = 0;
10827 do
10828 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010829 lnum /= 10;
10830 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010831 } while (lnum > 0);
10832
10833 /* 'numberwidth' gives the minimal width plus one */
10834 if (n < wp->w_p_nuw - 1)
10835 n = wp->w_p_nuw - 1;
10836
10837 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010838 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010839 return n;
10840}
10841#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010842
10843/*
10844 * Return the current cursor column. This is the actual position on the
10845 * screen. First column is 0.
10846 */
10847 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010848screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010849{
10850 return screen_cur_col;
10851}
10852
10853/*
10854 * Return the current cursor row. This is the actual position on the screen.
10855 * First row is 0.
10856 */
10857 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010858screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010859{
10860 return screen_cur_row;
10861}