blob: c1eb1c459c8d76292e9f400cc91f238737a1d7b9 [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 Moolenaar071d4272004-06-13 20:20:40 +0000136#ifdef FEAT_VERTSPLIT
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 Moolenaar071d4272004-06-13 20:20:40 +0000159#ifdef FEAT_VERTSPLIT
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
173#ifdef FEAT_VERTSPLIT
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
183#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
184/* Ugly global: overrule attribute used by screen_char() */
185static int screen_char_attr = 0;
186#endif
187
188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
191 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
204 if (wp->w_redr_type < type)
205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100251redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 win_T *wp;
260
261 FOR_ALL_WINDOWS(wp)
262 {
263 if (wp->w_buffer == buf)
264 redraw_win_later(wp, type);
265 }
266}
267
268/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200269 * Redraw as soon as possible. When the command line is not scrolled redraw
270 * right away and restore what was on the command line.
271 * Return a code indicating what happened.
272 */
273 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100274redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200275{
276 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200277 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200278 int r;
279 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200280 schar_T *screenline; /* copy from ScreenLines[] */
281 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200282#ifdef FEAT_MBYTE
283 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200284 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287#endif
288
289 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200290 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 return ret;
292
293 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200294 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200295 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200298 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 if (screenline == NULL || screenattr == NULL)
300 ret = 2;
301#ifdef FEAT_MBYTE
302 if (enc_utf8)
303 {
304 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200305 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306 if (screenlineUC == NULL)
307 ret = 2;
308 for (i = 0; i < p_mco; ++i)
309 {
310 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200311 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312 if (screenlineC[i] == NULL)
313 ret = 2;
314 }
315 }
316 if (enc_dbcs == DBCS_JPNU)
317 {
318 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200319 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline2 == NULL)
321 ret = 2;
322 }
323#endif
324
325 if (ret != 2)
326 {
327 /* Save the text displayed in the command line area. */
328 for (r = 0; r < rows; ++r)
329 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (size_t)cols * sizeof(schar_T));
333 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200339 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200343 mch_memmove(screenlineC[i] + r * cols,
344 ScreenLinesC[i] + LineOffset[cmdline_row + r],
345 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 }
347 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200349 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200350 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351#endif
352 }
353
354 update_screen(0);
355 ret = 3;
356
357 if (must_redraw == 0)
358 {
359 int off = (int)(current_ScreenLine - ScreenLines);
360
361 /* Restore the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
364 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 screenline + r * cols,
366 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 screenattr + r * cols,
369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#ifdef FEAT_MBYTE
371 if (enc_utf8)
372 {
373 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenlineUC + r * cols,
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 for (i = 0; i < p_mco; ++i)
377 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 screenlineC[i] + r * cols,
379 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381 if (enc_dbcs == DBCS_JPNU)
382 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 screenline2 + r * cols,
384 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200385#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200386 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200387 }
388 ret = 4;
389 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200390 }
391
392 vim_free(screenline);
393 vim_free(screenattr);
394#ifdef FEAT_MBYTE
395 if (enc_utf8)
396 {
397 vim_free(screenlineUC);
398 for (i = 0; i < p_mco; ++i)
399 vim_free(screenlineC[i]);
400 }
401 if (enc_dbcs == DBCS_JPNU)
402 vim_free(screenline2);
403#endif
404
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200405 /* Show the intro message when appropriate. */
406 maybe_intro_message();
407
408 setcursor();
409
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 return ret;
411}
412
413/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100414 * Invoked after an asynchronous callback is called.
415 * If an echo command was used the cursor needs to be put back where
416 * it belongs. If highlighting was changed a redraw is needed.
417 */
418 void
419redraw_after_callback()
420{
421 update_screen(0);
422 setcursor();
423 cursor_on();
424 out_flush();
425#ifdef FEAT_GUI
426 if (gui.in_use)
427 {
428 gui_update_cursor(TRUE, FALSE);
429 gui_mch_flush();
430 }
431#endif
432}
433
434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 * Changed something in the current window, at buffer line "lnum", that
436 * requires that line and possibly other lines to be redrawn.
437 * Used when entering/leaving Insert mode with the cursor on a folded line.
438 * Used to remove the "$" from a change command.
439 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
440 * may become invalid and the whole window will have to be redrawn.
441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100443redrawWinline(
444 linenr_T lnum,
445 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446{
447#ifdef FEAT_FOLDING
448 int i;
449#endif
450
451 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
452 curwin->w_redraw_top = lnum;
453 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
454 curwin->w_redraw_bot = lnum;
455 redraw_later(VALID);
456
457#ifdef FEAT_FOLDING
458 if (invalid)
459 {
460 /* A w_lines[] entry for this lnum has become invalid. */
461 i = find_wl_entry(curwin, lnum);
462 if (i >= 0)
463 curwin->w_lines[i].wl_valid = FALSE;
464 }
465#endif
466}
467
468/*
469 * update all windows that are editing the current buffer
470 */
471 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100472update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 redraw_curbuf_later(type);
475 update_screen(type);
476}
477
478/*
479 * update_screen()
480 *
481 * Based on the current value of curwin->w_topline, transfer a screenfull
482 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
483 */
484 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100485update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 win_T *wp;
488 static int did_intro = FALSE;
489#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
490 int did_one;
491#endif
492
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000493 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 if (!screen_valid(TRUE))
495 return;
496
497 if (must_redraw)
498 {
499 if (type < must_redraw) /* use maximal type */
500 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000501
502 /* must_redraw is reset here, so that when we run into some weird
503 * reason to redraw while busy redrawing (e.g., asynchronous
504 * scrolling), or update_topline() in win_update() will cause a
505 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 must_redraw = 0;
507 }
508
509 /* Need to update w_lines[]. */
510 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
511 type = NOT_VALID;
512
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000513 /* Postpone the redrawing when it's not needed and when being called
514 * recursively. */
515 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 {
517 redraw_later(type); /* remember type for next time */
518 must_redraw = type;
519 if (type > INVERTED_ALL)
520 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
521 return;
522 }
523
524 updating_screen = TRUE;
525#ifdef FEAT_SYN_HL
526 ++display_tick; /* let syntax code know we're in a next round of
527 * display updating */
528#endif
529
530 /*
531 * if the screen was scrolled up when displaying a message, scroll it down
532 */
533 if (msg_scrolled)
534 {
535 clear_cmdline = TRUE;
536 if (msg_scrolled > Rows - 5) /* clearing is faster */
537 type = CLEAR;
538 else if (type != CLEAR)
539 {
540 check_for_delay(FALSE);
541 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
542 type = CLEAR;
543 FOR_ALL_WINDOWS(wp)
544 {
545 if (W_WINROW(wp) < msg_scrolled)
546 {
547 if (W_WINROW(wp) + wp->w_height > msg_scrolled
548 && wp->w_redr_type < REDRAW_TOP
549 && wp->w_lines_valid > 0
550 && wp->w_topline == wp->w_lines[0].wl_lnum)
551 {
552 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
553 wp->w_redr_type = REDRAW_TOP;
554 }
555 else
556 {
557 wp->w_redr_type = NOT_VALID;
558#ifdef FEAT_WINDOWS
559 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
560 <= msg_scrolled)
561 wp->w_redr_status = TRUE;
562#endif
563 }
564 }
565 }
566 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000567#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000568 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571 msg_scrolled = 0;
572 need_wait_return = FALSE;
573 }
574
575 /* reset cmdline_row now (may have been changed temporarily) */
576 compute_cmdrow();
577
578 /* Check for changed highlighting */
579 if (need_highlight_changed)
580 highlight_changed();
581
582 if (type == CLEAR) /* first clear screen */
583 {
584 screenclear(); /* will reset clear_cmdline */
585 type = NOT_VALID;
586 }
587
588 if (clear_cmdline) /* going to clear cmdline (done below) */
589 check_for_delay(FALSE);
590
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000591#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200592 /* Force redraw when width of 'number' or 'relativenumber' column
593 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000594 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200595 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
596 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000597 curwin->w_redr_type = NOT_VALID;
598#endif
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 /*
601 * Only start redrawing if there is really something to do.
602 */
603 if (type == INVERTED)
604 update_curswant();
605 if (curwin->w_redr_type < type
606 && !((type == VALID
607 && curwin->w_lines[0].wl_valid
608#ifdef FEAT_DIFF
609 && curwin->w_topfill == curwin->w_old_topfill
610 && curwin->w_botfill == curwin->w_old_botfill
611#endif
612 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000614 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
616 && curwin->w_old_visual_mode == VIsual_mode
617 && (curwin->w_valid & VALID_VIRTCOL)
618 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 ))
620 curwin->w_redr_type = type;
621
Bram Moolenaar5a305422006-04-28 22:38:25 +0000622#ifdef FEAT_WINDOWS
623 /* Redraw the tab pages line if needed. */
624 if (redraw_tabline || type >= NOT_VALID)
625 draw_tabline();
626#endif
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#ifdef FEAT_SYN_HL
629 /*
630 * Correct stored syntax highlighting info for changes in each displayed
631 * buffer. Each buffer must only be done once.
632 */
633 FOR_ALL_WINDOWS(wp)
634 {
635 if (wp->w_buffer->b_mod_set)
636 {
637# ifdef FEAT_WINDOWS
638 win_T *wwp;
639
640 /* Check if we already did this buffer. */
641 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
642 if (wwp->w_buffer == wp->w_buffer)
643 break;
644# endif
645 if (
646# ifdef FEAT_WINDOWS
647 wwp == wp &&
648# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200649 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 syn_stack_apply_changes(wp->w_buffer);
651 }
652 }
653#endif
654
655 /*
656 * Go from top to bottom through the windows, redrawing the ones that need
657 * it.
658 */
659#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
660 did_one = FALSE;
661#endif
662#ifdef FEAT_SEARCH_EXTRA
663 search_hl.rm.regprog = NULL;
664#endif
665 FOR_ALL_WINDOWS(wp)
666 {
667 if (wp->w_redr_type != 0)
668 {
669 cursor_off();
670#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
671 if (!did_one)
672 {
673 did_one = TRUE;
674# ifdef FEAT_SEARCH_EXTRA
675 start_search_hl();
676# endif
677# ifdef FEAT_CLIPBOARD
678 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200679 if (clip_star.available && clip_isautosel_star())
680 clip_update_selection(&clip_star);
681 if (clip_plus.available && clip_isautosel_plus())
682 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683# endif
684#ifdef FEAT_GUI
685 /* Remove the cursor before starting to do anything, because
686 * scrolling may make it difficult to redraw the text under
687 * it. */
688 if (gui.in_use)
689 gui_undraw_cursor();
690#endif
691 }
692#endif
693 win_update(wp);
694 }
695
696#ifdef FEAT_WINDOWS
697 /* redraw status line after the window to minimize cursor movement */
698 if (wp->w_redr_status)
699 {
700 cursor_off();
701 win_redr_status(wp);
702 }
703#endif
704 }
705#if defined(FEAT_SEARCH_EXTRA)
706 end_search_hl();
707#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100708#ifdef FEAT_INS_EXPAND
709 /* May need to redraw the popup menu. */
710 if (pum_visible())
711 pum_redraw();
712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
714#ifdef FEAT_WINDOWS
715 /* Reset b_mod_set flags. Going through all windows is probably faster
716 * than going through all buffers (there could be many buffers). */
717 for (wp = firstwin; wp != NULL; wp = wp->w_next)
718 wp->w_buffer->b_mod_set = FALSE;
719#else
720 curbuf->b_mod_set = FALSE;
721#endif
722
723 updating_screen = FALSE;
724#ifdef FEAT_GUI
725 gui_may_resize_shell();
726#endif
727
728 /* Clear or redraw the command line. Done last, because scrolling may
729 * mess up the command line. */
730 if (clear_cmdline || redraw_cmdline)
731 showmode();
732
733 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200734 if (!did_intro)
735 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 did_intro = TRUE;
737
738#ifdef FEAT_GUI
739 /* Redraw the cursor and update the scrollbars when all screen updating is
740 * done. */
741 if (gui.in_use)
742 {
743 out_flush(); /* required before updating the cursor */
744 if (did_one)
745 gui_update_cursor(FALSE, FALSE);
746 gui_update_scrollbars(FALSE);
747 }
748#endif
749}
750
Bram Moolenaar860cae12010-06-05 23:22:07 +0200751#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200752/*
753 * Return TRUE if the cursor line in window "wp" may be concealed, according
754 * to the 'concealcursor' option.
755 */
756 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100757conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200758{
759 int c;
760
761 if (*wp->w_p_cocu == NUL)
762 return FALSE;
763 if (get_real_state() & VISUAL)
764 c = 'v';
765 else if (State & INSERT)
766 c = 'i';
767 else if (State & NORMAL)
768 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200769 else if (State & CMDLINE)
770 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200771 else
772 return FALSE;
773 return vim_strchr(wp->w_p_cocu, c) != NULL;
774}
775
776/*
777 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
778 */
779 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100780conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200781{
782 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
783 {
784 need_cursor_line_redraw = TRUE;
785 /* Need to recompute cursor column, e.g., when starting Visual mode
786 * without concealing. */
787 curs_columns(TRUE);
788 }
789}
790
Bram Moolenaar860cae12010-06-05 23:22:07 +0200791 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100792update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200793{
794 int row;
795 int j;
796
797 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200798 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200799 {
800# ifdef FEAT_GUI
801 /* Remove the cursor before starting to do anything, because scrolling
802 * may make it difficult to redraw the text under it. */
803 if (gui.in_use)
804 gui_undraw_cursor();
805# endif
806 row = 0;
807 for (j = 0; j < wp->w_lines_valid; ++j)
808 {
809 if (lnum == wp->w_lines[j].wl_lnum)
810 {
811 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200812# ifdef FEAT_SEARCH_EXTRA
813 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200814 start_search_hl();
815 prepare_search_hl(wp, lnum);
816# endif
817 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
818# if defined(FEAT_SEARCH_EXTRA)
819 end_search_hl();
820# endif
821 break;
822 }
823 row += wp->w_lines[j].wl_size;
824 }
825# ifdef FEAT_GUI
826 /* Redraw the cursor */
827 if (gui.in_use)
828 {
829 out_flush(); /* required before updating the cursor */
830 gui_update_cursor(FALSE, FALSE);
831 }
832# endif
833 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200834 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200835}
836#endif
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100839static void update_prepare(void);
840static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842/*
843 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000844 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 */
846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100847update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 cursor_off();
850 updating_screen = TRUE;
851#ifdef FEAT_GUI
852 /* Remove the cursor before starting to do anything, because scrolling may
853 * make it difficult to redraw the text under it. */
854 if (gui.in_use)
855 gui_undraw_cursor();
856#endif
857#ifdef FEAT_SEARCH_EXTRA
858 start_search_hl();
859#endif
860}
861
862/*
863 * Finish updating one or more windows.
864 */
865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100866update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 if (redraw_cmdline)
869 showmode();
870
871# ifdef FEAT_SEARCH_EXTRA
872 end_search_hl();
873# endif
874
875 updating_screen = FALSE;
876
877# ifdef FEAT_GUI
878 gui_may_resize_shell();
879
880 /* Redraw the cursor and update the scrollbars when all screen updating is
881 * done. */
882 if (gui.in_use)
883 {
884 out_flush(); /* required before updating the cursor */
885 gui_update_cursor(FALSE, FALSE);
886 gui_update_scrollbars(FALSE);
887 }
888# endif
889}
890#endif
891
892#if defined(FEAT_SIGNS) || defined(PROTO)
893 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100894update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 win_T *wp;
897 int doit = FALSE;
898
899# ifdef FEAT_FOLDING
900 win_foldinfo.fi_level = 0;
901# endif
902
903 /* update/delete a specific mark */
904 FOR_ALL_WINDOWS(wp)
905 {
906 if (buf != NULL && lnum > 0)
907 {
908 if (wp->w_buffer == buf && lnum >= wp->w_topline
909 && lnum < wp->w_botline)
910 {
911 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
912 wp->w_redraw_top = lnum;
913 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
914 wp->w_redraw_bot = lnum;
915 redraw_win_later(wp, VALID);
916 }
917 }
918 else
919 redraw_win_later(wp, VALID);
920 if (wp->w_redr_type != 0)
921 doit = TRUE;
922 }
923
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100924 /* Return when there is nothing to do, screen updating is already
925 * happening (recursive call) or still starting up. */
926 if (!doit || updating_screen
927#ifdef FEAT_GUI
928 || gui.starting
929#endif
930 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 return;
932
933 /* update all windows that need updating */
934 update_prepare();
935
936# ifdef FEAT_WINDOWS
937 for (wp = firstwin; wp; wp = wp->w_next)
938 {
939 if (wp->w_redr_type != 0)
940 win_update(wp);
941 if (wp->w_redr_status)
942 win_redr_status(wp);
943 }
944# else
945 if (curwin->w_redr_type != 0)
946 win_update(curwin);
947# endif
948
949 update_finish();
950}
951#endif
952
953
954#if defined(FEAT_GUI) || defined(PROTO)
955/*
956 * Update a single window, its status line and maybe the command line msg.
957 * Used for the GUI scrollbar.
958 */
959 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100960updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000962 /* return if already busy updating */
963 if (updating_screen)
964 return;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 update_prepare();
967
968#ifdef FEAT_CLIPBOARD
969 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200970 if (clip_star.available && clip_isautosel_star())
971 clip_update_selection(&clip_star);
972 if (clip_plus.available && clip_isautosel_plus())
973 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000979 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000980 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000981 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (wp->w_redr_status
984# ifdef FEAT_CMDL_INFO
985 || p_ru
986# endif
987# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000988 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989# endif
990 )
991 win_redr_status(wp);
992#endif
993
994 update_finish();
995}
996#endif
997
998/*
999 * Update a single window.
1000 *
1001 * This may cause the windows below it also to be redrawn (when clearing the
1002 * screen or scrolling lines).
1003 *
1004 * How the window is redrawn depends on wp->w_redr_type. Each type also
1005 * implies the one below it.
1006 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001007 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1009 * INVERTED redraw the changed part of the Visual area
1010 * INVERTED_ALL redraw the whole Visual area
1011 * VALID 1. scroll up/down to adjust for a changed w_topline
1012 * 2. update lines at the top when scrolled down
1013 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001014 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * b_mod_top and b_mod_bot.
1016 * - if wp->w_redraw_top non-zero, redraw lines between
1017 * wp->w_redraw_top and wp->w_redr_bot.
1018 * - continue redrawing when syntax status is invalid.
1019 * 4. if scrolled up, update lines at the bottom.
1020 * This results in three areas that may need updating:
1021 * top: from first row to top_end (when scrolled down)
1022 * mid: from mid_start to mid_end (update inversion or changed text)
1023 * bot: from bot_start to last row (when scrolled up)
1024 */
1025 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001026win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027{
1028 buf_T *buf = wp->w_buffer;
1029 int type;
1030 int top_end = 0; /* Below last row of the top area that needs
1031 updating. 0 when no top area updating. */
1032 int mid_start = 999;/* first row of the mid area that needs
1033 updating. 999 when no mid area updating. */
1034 int mid_end = 0; /* Below last row of the mid area that needs
1035 updating. 0 when no mid area updating. */
1036 int bot_start = 999;/* first row of the bot area that needs
1037 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 int scrolled_down = FALSE; /* TRUE when scrolled down when
1039 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001041 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 int top_to_mod = FALSE; /* redraw above mod_top */
1043#endif
1044
1045 int row; /* current window row to display */
1046 linenr_T lnum; /* current buffer lnum to display */
1047 int idx; /* current index in w_lines[] */
1048 int srow; /* starting row of the current line */
1049
1050 int eof = FALSE; /* if TRUE, we hit the end of the file */
1051 int didline = FALSE; /* if TRUE, we finished the last line */
1052 int i;
1053 long j;
1054 static int recursive = FALSE; /* being called recursively */
1055 int old_botline = wp->w_botline;
1056#ifdef FEAT_FOLDING
1057 long fold_count;
1058#endif
1059#ifdef FEAT_SYN_HL
1060 /* remember what happened to the previous line, to know if
1061 * check_visual_highlight() can be used */
1062#define DID_NONE 1 /* didn't update a line */
1063#define DID_LINE 2 /* updated a normal line */
1064#define DID_FOLD 3 /* updated a folded line */
1065 int did_update = DID_NONE;
1066 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1067#endif
1068 linenr_T mod_top = 0;
1069 linenr_T mod_bot = 0;
1070#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1071 int save_got_int;
1072#endif
1073
1074 type = wp->w_redr_type;
1075
1076 if (type == NOT_VALID)
1077 {
1078#ifdef FEAT_WINDOWS
1079 wp->w_redr_status = TRUE;
1080#endif
1081 wp->w_lines_valid = 0;
1082 }
1083
1084 /* Window is zero-height: nothing to draw. */
1085 if (wp->w_height == 0)
1086 {
1087 wp->w_redr_type = 0;
1088 return;
1089 }
1090
1091#ifdef FEAT_VERTSPLIT
1092 /* Window is zero-width: Only need to draw the separator. */
1093 if (wp->w_width == 0)
1094 {
1095 /* draw the vertical separator right of this window */
1096 draw_vsep_win(wp, 0);
1097 wp->w_redr_type = 0;
1098 return;
1099 }
1100#endif
1101
1102#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001103 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001106#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001107 /* Force redraw when width of 'number' or 'relativenumber' column
1108 * changes. */
1109 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001110 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001111 {
1112 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001113 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001114 }
1115 else
1116#endif
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1119 {
1120 /*
1121 * When there are both inserted/deleted lines and specific lines to be
1122 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1123 * everything (only happens when redrawing is off for while).
1124 */
1125 type = NOT_VALID;
1126 }
1127 else
1128 {
1129 /*
1130 * Set mod_top to the first line that needs displaying because of
1131 * changes. Set mod_bot to the first line after the changes.
1132 */
1133 mod_top = wp->w_redraw_top;
1134 if (wp->w_redraw_bot != 0)
1135 mod_bot = wp->w_redraw_bot + 1;
1136 else
1137 mod_bot = 0;
1138 wp->w_redraw_top = 0; /* reset for next time */
1139 wp->w_redraw_bot = 0;
1140 if (buf->b_mod_set)
1141 {
1142 if (mod_top == 0 || mod_top > buf->b_mod_top)
1143 {
1144 mod_top = buf->b_mod_top;
1145#ifdef FEAT_SYN_HL
1146 /* Need to redraw lines above the change that may be included
1147 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001148 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001150 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (mod_top < 1)
1152 mod_top = 1;
1153 }
1154#endif
1155 }
1156 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1157 mod_bot = buf->b_mod_bot;
1158
1159#ifdef FEAT_SEARCH_EXTRA
1160 /* When 'hlsearch' is on and using a multi-line search pattern, a
1161 * change in one line may make the Search highlighting in a
1162 * previous line invalid. Simple solution: redraw all visible
1163 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001164 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001166 if (search_hl.rm.regprog != NULL
1167 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001169 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001170 {
1171 cur = wp->w_match_head;
1172 while (cur != NULL)
1173 {
1174 if (cur->match.regprog != NULL
1175 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001176 {
1177 top_to_mod = TRUE;
1178 break;
1179 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001180 cur = cur->next;
1181 }
1182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#endif
1184 }
1185#ifdef FEAT_FOLDING
1186 if (mod_top != 0 && hasAnyFolding(wp))
1187 {
1188 linenr_T lnumt, lnumb;
1189
1190 /*
1191 * A change in a line can cause lines above it to become folded or
1192 * unfolded. Find the top most buffer line that may be affected.
1193 * If the line was previously folded and displayed, get the first
1194 * line of that fold. If the line is folded now, get the first
1195 * folded line. Use the minimum of these two.
1196 */
1197
1198 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1199 * the line below it. If there is no valid entry, use w_topline.
1200 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1201 * to this line. If there is no valid entry, use MAXLNUM. */
1202 lnumt = wp->w_topline;
1203 lnumb = MAXLNUM;
1204 for (i = 0; i < wp->w_lines_valid; ++i)
1205 if (wp->w_lines[i].wl_valid)
1206 {
1207 if (wp->w_lines[i].wl_lastlnum < mod_top)
1208 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1209 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1210 {
1211 lnumb = wp->w_lines[i].wl_lnum;
1212 /* When there is a fold column it might need updating
1213 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001214 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 ++lnumb;
1216 }
1217 }
1218
1219 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1220 if (mod_top > lnumt)
1221 mod_top = lnumt;
1222
1223 /* Now do the same for the bottom line (one above mod_bot). */
1224 --mod_bot;
1225 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1226 ++mod_bot;
1227 if (mod_bot < lnumb)
1228 mod_bot = lnumb;
1229 }
1230#endif
1231
1232 /* When a change starts above w_topline and the end is below
1233 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001234 * If the end of the change is above w_topline: do like no change was
1235 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (mod_top != 0 && mod_top < wp->w_topline)
1237 {
1238 if (mod_bot > wp->w_topline)
1239 mod_top = wp->w_topline;
1240#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 top_end = 1;
1243#endif
1244 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001245
1246 /* When line numbers are displayed need to redraw all lines below
1247 * inserted/deleted lines. */
1248 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1249 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251
1252 /*
1253 * When only displaying the lines at the top, set top_end. Used when
1254 * window has scrolled down for msg_scrolled.
1255 */
1256 if (type == REDRAW_TOP)
1257 {
1258 j = 0;
1259 for (i = 0; i < wp->w_lines_valid; ++i)
1260 {
1261 j += wp->w_lines[i].wl_size;
1262 if (j >= wp->w_upd_rows)
1263 {
1264 top_end = j;
1265 break;
1266 }
1267 }
1268 if (top_end == 0)
1269 /* not found (cannot happen?): redraw everything */
1270 type = NOT_VALID;
1271 else
1272 /* top area defined, the rest is VALID */
1273 type = VALID;
1274 }
1275
Bram Moolenaar367329b2007-08-30 11:53:22 +00001276 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001277 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1278 * non-zero and thus not FALSE) will indicate that screenclear() was not
1279 * called. */
1280 if (screen_cleared)
1281 screen_cleared = MAYBE;
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 /*
1284 * If there are no changes on the screen that require a complete redraw,
1285 * handle three cases:
1286 * 1: we are off the top of the screen by a few lines: scroll down
1287 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1288 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1289 * w_lines[] that needs updating.
1290 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001291 if ((type == VALID || type == SOME_VALID
1292 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#ifdef FEAT_DIFF
1294 && !wp->w_botfill && !wp->w_old_botfill
1295#endif
1296 )
1297 {
1298 if (mod_top != 0 && wp->w_topline == mod_top)
1299 {
1300 /*
1301 * w_topline is the first changed line, the scrolling will be done
1302 * further down.
1303 */
1304 }
1305 else if (wp->w_lines[0].wl_valid
1306 && (wp->w_topline < wp->w_lines[0].wl_lnum
1307#ifdef FEAT_DIFF
1308 || (wp->w_topline == wp->w_lines[0].wl_lnum
1309 && wp->w_topfill > wp->w_old_topfill)
1310#endif
1311 ))
1312 {
1313 /*
1314 * New topline is above old topline: May scroll down.
1315 */
1316#ifdef FEAT_FOLDING
1317 if (hasAnyFolding(wp))
1318 {
1319 linenr_T ln;
1320
1321 /* count the number of lines we are off, counting a sequence
1322 * of folded lines as one */
1323 j = 0;
1324 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1325 {
1326 ++j;
1327 if (j >= wp->w_height - 2)
1328 break;
1329 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1330 }
1331 }
1332 else
1333#endif
1334 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1335 if (j < wp->w_height - 2) /* not too far off */
1336 {
1337 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1338#ifdef FEAT_DIFF
1339 /* insert extra lines for previously invisible filler lines */
1340 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1341 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1342 - wp->w_old_topfill;
1343#endif
1344 if (i < wp->w_height - 2) /* less than a screen off */
1345 {
1346 /*
1347 * Try to insert the correct number of lines.
1348 * If not the last window, delete the lines at the bottom.
1349 * win_ins_lines may fail when the terminal can't do it.
1350 */
1351 if (i > 0)
1352 check_for_delay(FALSE);
1353 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1354 {
1355 if (wp->w_lines_valid != 0)
1356 {
1357 /* Need to update rows that are new, stop at the
1358 * first one that scrolled down. */
1359 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 /* Move the entries that were scrolled, disable
1363 * the entries for the lines to be redrawn. */
1364 if ((wp->w_lines_valid += j) > wp->w_height)
1365 wp->w_lines_valid = wp->w_height;
1366 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1367 wp->w_lines[idx] = wp->w_lines[idx - j];
1368 while (idx >= 0)
1369 wp->w_lines[idx--].wl_valid = FALSE;
1370 }
1371 }
1372 else
1373 mid_start = 0; /* redraw all lines */
1374 }
1375 else
1376 mid_start = 0; /* redraw all lines */
1377 }
1378 else
1379 mid_start = 0; /* redraw all lines */
1380 }
1381 else
1382 {
1383 /*
1384 * New topline is at or below old topline: May scroll up.
1385 * When topline didn't change, find first entry in w_lines[] that
1386 * needs updating.
1387 */
1388
1389 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1390 j = -1;
1391 row = 0;
1392 for (i = 0; i < wp->w_lines_valid; i++)
1393 {
1394 if (wp->w_lines[i].wl_valid
1395 && wp->w_lines[i].wl_lnum == wp->w_topline)
1396 {
1397 j = i;
1398 break;
1399 }
1400 row += wp->w_lines[i].wl_size;
1401 }
1402 if (j == -1)
1403 {
1404 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1405 * lines */
1406 mid_start = 0;
1407 }
1408 else
1409 {
1410 /*
1411 * Try to delete the correct number of lines.
1412 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1413 */
1414#ifdef FEAT_DIFF
1415 /* If the topline didn't change, delete old filler lines,
1416 * otherwise delete filler lines of the new topline... */
1417 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1418 row += wp->w_old_topfill;
1419 else
1420 row += diff_check_fill(wp, wp->w_topline);
1421 /* ... but don't delete new filler lines. */
1422 row -= wp->w_topfill;
1423#endif
1424 if (row > 0)
1425 {
1426 check_for_delay(FALSE);
1427 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1428 bot_start = wp->w_height - row;
1429 else
1430 mid_start = 0; /* redraw all lines */
1431 }
1432 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1433 {
1434 /*
1435 * Skip the lines (below the deleted lines) that are still
1436 * valid and don't need redrawing. Copy their info
1437 * upwards, to compensate for the deleted lines. Set
1438 * bot_start to the first row that needs redrawing.
1439 */
1440 bot_start = 0;
1441 idx = 0;
1442 for (;;)
1443 {
1444 wp->w_lines[idx] = wp->w_lines[j];
1445 /* stop at line that didn't fit, unless it is still
1446 * valid (no lines deleted) */
1447 if (row > 0 && bot_start + row
1448 + (int)wp->w_lines[j].wl_size > wp->w_height)
1449 {
1450 wp->w_lines_valid = idx + 1;
1451 break;
1452 }
1453 bot_start += wp->w_lines[idx++].wl_size;
1454
1455 /* stop at the last valid entry in w_lines[].wl_size */
1456 if (++j >= wp->w_lines_valid)
1457 {
1458 wp->w_lines_valid = idx;
1459 break;
1460 }
1461 }
1462#ifdef FEAT_DIFF
1463 /* Correct the first entry for filler lines at the top
1464 * when it won't get updated below. */
1465 if (wp->w_p_diff && bot_start > 0)
1466 wp->w_lines[0].wl_size =
1467 plines_win_nofill(wp, wp->w_topline, TRUE)
1468 + wp->w_topfill;
1469#endif
1470 }
1471 }
1472 }
1473
1474 /* When starting redraw in the first line, redraw all lines. When
1475 * there is only one window it's probably faster to clear the screen
1476 * first. */
1477 if (mid_start == 0)
1478 {
1479 mid_end = wp->w_height;
1480 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001481 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001482 /* Clear the screen when it was not done by win_del_lines() or
1483 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1484 * then. */
1485 if (screen_cleared != TRUE)
1486 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001487#ifdef FEAT_WINDOWS
1488 /* The screen was cleared, redraw the tab pages line. */
1489 if (redraw_tabline)
1490 draw_tabline();
1491#endif
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001494
1495 /* When win_del_lines() or win_ins_lines() caused the screen to be
1496 * cleared (only happens for the first window) or when screenclear()
1497 * was called directly above, "must_redraw" will have been set to
1498 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1499 if (screen_cleared == TRUE)
1500 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 else
1503 {
1504 /* Not VALID or INVERTED: redraw all lines. */
1505 mid_start = 0;
1506 mid_end = wp->w_height;
1507 }
1508
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001509 if (type == SOME_VALID)
1510 {
1511 /* SOME_VALID: redraw all lines. */
1512 mid_start = 0;
1513 mid_end = wp->w_height;
1514 type = NOT_VALID;
1515 }
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 /* check if we are updating or removing the inverted part */
1518 if ((VIsual_active && buf == curwin->w_buffer)
1519 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1520 {
1521 linenr_T from, to;
1522
1523 if (VIsual_active)
1524 {
1525 if (VIsual_active
1526 && (VIsual_mode != wp->w_old_visual_mode
1527 || type == INVERTED_ALL))
1528 {
1529 /*
1530 * If the type of Visual selection changed, redraw the whole
1531 * selection. Also when the ownership of the X selection is
1532 * gained or lost.
1533 */
1534 if (curwin->w_cursor.lnum < VIsual.lnum)
1535 {
1536 from = curwin->w_cursor.lnum;
1537 to = VIsual.lnum;
1538 }
1539 else
1540 {
1541 from = VIsual.lnum;
1542 to = curwin->w_cursor.lnum;
1543 }
1544 /* redraw more when the cursor moved as well */
1545 if (wp->w_old_cursor_lnum < from)
1546 from = wp->w_old_cursor_lnum;
1547 if (wp->w_old_cursor_lnum > to)
1548 to = wp->w_old_cursor_lnum;
1549 if (wp->w_old_visual_lnum < from)
1550 from = wp->w_old_visual_lnum;
1551 if (wp->w_old_visual_lnum > to)
1552 to = wp->w_old_visual_lnum;
1553 }
1554 else
1555 {
1556 /*
1557 * Find the line numbers that need to be updated: The lines
1558 * between the old cursor position and the current cursor
1559 * position. Also check if the Visual position changed.
1560 */
1561 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1562 {
1563 from = curwin->w_cursor.lnum;
1564 to = wp->w_old_cursor_lnum;
1565 }
1566 else
1567 {
1568 from = wp->w_old_cursor_lnum;
1569 to = curwin->w_cursor.lnum;
1570 if (from == 0) /* Visual mode just started */
1571 from = to;
1572 }
1573
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001574 if (VIsual.lnum != wp->w_old_visual_lnum
1575 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 if (wp->w_old_visual_lnum < from
1578 && wp->w_old_visual_lnum != 0)
1579 from = wp->w_old_visual_lnum;
1580 if (wp->w_old_visual_lnum > to)
1581 to = wp->w_old_visual_lnum;
1582 if (VIsual.lnum < from)
1583 from = VIsual.lnum;
1584 if (VIsual.lnum > to)
1585 to = VIsual.lnum;
1586 }
1587 }
1588
1589 /*
1590 * If in block mode and changed column or curwin->w_curswant:
1591 * update all lines.
1592 * First compute the actual start and end column.
1593 */
1594 if (VIsual_mode == Ctrl_V)
1595 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001596 colnr_T fromc, toc;
1597#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1598 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar404406a2014-10-09 13:24:43 +02001600 if (curwin->w_p_lbr)
1601 ve_flags = VE_ALL;
1602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001604#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1605 ve_flags = save_ve_flags;
1606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 ++toc;
1608 if (curwin->w_curswant == MAXCOL)
1609 toc = MAXCOL;
1610
1611 if (fromc != wp->w_old_cursor_fcol
1612 || toc != wp->w_old_cursor_lcol)
1613 {
1614 if (from > VIsual.lnum)
1615 from = VIsual.lnum;
1616 if (to < VIsual.lnum)
1617 to = VIsual.lnum;
1618 }
1619 wp->w_old_cursor_fcol = fromc;
1620 wp->w_old_cursor_lcol = toc;
1621 }
1622 }
1623 else
1624 {
1625 /* Use the line numbers of the old Visual area. */
1626 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1627 {
1628 from = wp->w_old_cursor_lnum;
1629 to = wp->w_old_visual_lnum;
1630 }
1631 else
1632 {
1633 from = wp->w_old_visual_lnum;
1634 to = wp->w_old_cursor_lnum;
1635 }
1636 }
1637
1638 /*
1639 * There is no need to update lines above the top of the window.
1640 */
1641 if (from < wp->w_topline)
1642 from = wp->w_topline;
1643
1644 /*
1645 * If we know the value of w_botline, use it to restrict the update to
1646 * the lines that are visible in the window.
1647 */
1648 if (wp->w_valid & VALID_BOTLINE)
1649 {
1650 if (from >= wp->w_botline)
1651 from = wp->w_botline - 1;
1652 if (to >= wp->w_botline)
1653 to = wp->w_botline - 1;
1654 }
1655
1656 /*
1657 * Find the minimal part to be updated.
1658 * Watch out for scrolling that made entries in w_lines[] invalid.
1659 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1660 * top_end; need to redraw from top_end to the "to" line.
1661 * A middle mouse click with a Visual selection may change the text
1662 * above the Visual area and reset wl_valid, do count these for
1663 * mid_end (in srow).
1664 */
1665 if (mid_start > 0)
1666 {
1667 lnum = wp->w_topline;
1668 idx = 0;
1669 srow = 0;
1670 if (scrolled_down)
1671 mid_start = top_end;
1672 else
1673 mid_start = 0;
1674 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1675 {
1676 if (wp->w_lines[idx].wl_valid)
1677 mid_start += wp->w_lines[idx].wl_size;
1678 else if (!scrolled_down)
1679 srow += wp->w_lines[idx].wl_size;
1680 ++idx;
1681# ifdef FEAT_FOLDING
1682 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1683 lnum = wp->w_lines[idx].wl_lnum;
1684 else
1685# endif
1686 ++lnum;
1687 }
1688 srow += mid_start;
1689 mid_end = wp->w_height;
1690 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1691 {
1692 if (wp->w_lines[idx].wl_valid
1693 && wp->w_lines[idx].wl_lnum >= to + 1)
1694 {
1695 /* Only update until first row of this line */
1696 mid_end = srow;
1697 break;
1698 }
1699 srow += wp->w_lines[idx].wl_size;
1700 }
1701 }
1702 }
1703
1704 if (VIsual_active && buf == curwin->w_buffer)
1705 {
1706 wp->w_old_visual_mode = VIsual_mode;
1707 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1708 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001709 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 wp->w_old_curswant = curwin->w_curswant;
1711 }
1712 else
1713 {
1714 wp->w_old_visual_mode = 0;
1715 wp->w_old_cursor_lnum = 0;
1716 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001717 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1721 /* reset got_int, otherwise regexp won't work */
1722 save_got_int = got_int;
1723 got_int = 0;
1724#endif
1725#ifdef FEAT_FOLDING
1726 win_foldinfo.fi_level = 0;
1727#endif
1728
1729 /*
1730 * Update all the window rows.
1731 */
1732 idx = 0; /* first entry in w_lines[].wl_size */
1733 row = 0;
1734 srow = 0;
1735 lnum = wp->w_topline; /* first line shown in window */
1736 for (;;)
1737 {
1738 /* stop updating when reached the end of the window (check for _past_
1739 * the end of the window is at the end of the loop) */
1740 if (row == wp->w_height)
1741 {
1742 didline = TRUE;
1743 break;
1744 }
1745
1746 /* stop updating when hit the end of the file */
1747 if (lnum > buf->b_ml.ml_line_count)
1748 {
1749 eof = TRUE;
1750 break;
1751 }
1752
1753 /* Remember the starting row of the line that is going to be dealt
1754 * with. It is used further down when the line doesn't fit. */
1755 srow = row;
1756
1757 /*
1758 * Update a line when it is in an area that needs updating, when it
1759 * has changes or w_lines[idx] is invalid.
1760 * bot_start may be halfway a wrapped line after using
1761 * win_del_lines(), check if the current line includes it.
1762 * When syntax folding is being used, the saved syntax states will
1763 * already have been updated, we can't see where the syntax state is
1764 * the same again, just update until the end of the window.
1765 */
1766 if (row < top_end
1767 || (row >= mid_start && row < mid_end)
1768#ifdef FEAT_SEARCH_EXTRA
1769 || top_to_mod
1770#endif
1771 || idx >= wp->w_lines_valid
1772 || (row + wp->w_lines[idx].wl_size > bot_start)
1773 || (mod_top != 0
1774 && (lnum == mod_top
1775 || (lnum >= mod_top
1776 && (lnum < mod_bot
1777#ifdef FEAT_SYN_HL
1778 || did_update == DID_FOLD
1779 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001780 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 && (
1782# ifdef FEAT_FOLDING
1783 (foldmethodIsSyntax(wp)
1784 && hasAnyFolding(wp)) ||
1785# endif
1786 syntax_check_changed(lnum)))
1787#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001788#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001789 /* match in fixed position might need redraw
1790 * if lines were inserted or deleted */
1791 || (wp->w_match_head != NULL
1792 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 )))))
1795 {
1796#ifdef FEAT_SEARCH_EXTRA
1797 if (lnum == mod_top)
1798 top_to_mod = FALSE;
1799#endif
1800
1801 /*
1802 * When at start of changed lines: May scroll following lines
1803 * up or down to minimize redrawing.
1804 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001805 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 */
1807 if (lnum == mod_top
1808 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001809 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811 int old_rows = 0;
1812 int new_rows = 0;
1813 int xtra_rows;
1814 linenr_T l;
1815
1816 /* Count the old number of window rows, using w_lines[], which
1817 * should still contain the sizes for the lines as they are
1818 * currently displayed. */
1819 for (i = idx; i < wp->w_lines_valid; ++i)
1820 {
1821 /* Only valid lines have a meaningful wl_lnum. Invalid
1822 * lines are part of the changed area. */
1823 if (wp->w_lines[i].wl_valid
1824 && wp->w_lines[i].wl_lnum == mod_bot)
1825 break;
1826 old_rows += wp->w_lines[i].wl_size;
1827#ifdef FEAT_FOLDING
1828 if (wp->w_lines[i].wl_valid
1829 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1830 {
1831 /* Must have found the last valid entry above mod_bot.
1832 * Add following invalid entries. */
1833 ++i;
1834 while (i < wp->w_lines_valid
1835 && !wp->w_lines[i].wl_valid)
1836 old_rows += wp->w_lines[i++].wl_size;
1837 break;
1838 }
1839#endif
1840 }
1841
1842 if (i >= wp->w_lines_valid)
1843 {
1844 /* We can't find a valid line below the changed lines,
1845 * need to redraw until the end of the window.
1846 * Inserting/deleting lines has no use. */
1847 bot_start = 0;
1848 }
1849 else
1850 {
1851 /* Able to count old number of rows: Count new window
1852 * rows, and may insert/delete lines */
1853 j = idx;
1854 for (l = lnum; l < mod_bot; ++l)
1855 {
1856#ifdef FEAT_FOLDING
1857 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1858 ++new_rows;
1859 else
1860#endif
1861#ifdef FEAT_DIFF
1862 if (l == wp->w_topline)
1863 new_rows += plines_win_nofill(wp, l, TRUE)
1864 + wp->w_topfill;
1865 else
1866#endif
1867 new_rows += plines_win(wp, l, TRUE);
1868 ++j;
1869 if (new_rows > wp->w_height - row - 2)
1870 {
1871 /* it's getting too much, must redraw the rest */
1872 new_rows = 9999;
1873 break;
1874 }
1875 }
1876 xtra_rows = new_rows - old_rows;
1877 if (xtra_rows < 0)
1878 {
1879 /* May scroll text up. If there is not enough
1880 * remaining text or scrolling fails, must redraw the
1881 * rest. If scrolling works, must redraw the text
1882 * below the scrolled text. */
1883 if (row - xtra_rows >= wp->w_height - 2)
1884 mod_bot = MAXLNUM;
1885 else
1886 {
1887 check_for_delay(FALSE);
1888 if (win_del_lines(wp, row,
1889 -xtra_rows, FALSE, FALSE) == FAIL)
1890 mod_bot = MAXLNUM;
1891 else
1892 bot_start = wp->w_height + xtra_rows;
1893 }
1894 }
1895 else if (xtra_rows > 0)
1896 {
1897 /* May scroll text down. If there is not enough
1898 * remaining text of scrolling fails, must redraw the
1899 * rest. */
1900 if (row + xtra_rows >= wp->w_height - 2)
1901 mod_bot = MAXLNUM;
1902 else
1903 {
1904 check_for_delay(FALSE);
1905 if (win_ins_lines(wp, row + old_rows,
1906 xtra_rows, FALSE, FALSE) == FAIL)
1907 mod_bot = MAXLNUM;
1908 else if (top_end > row + old_rows)
1909 /* Scrolled the part at the top that requires
1910 * updating down. */
1911 top_end += xtra_rows;
1912 }
1913 }
1914
1915 /* When not updating the rest, may need to move w_lines[]
1916 * entries. */
1917 if (mod_bot != MAXLNUM && i != j)
1918 {
1919 if (j < i)
1920 {
1921 int x = row + new_rows;
1922
1923 /* move entries in w_lines[] upwards */
1924 for (;;)
1925 {
1926 /* stop at last valid entry in w_lines[] */
1927 if (i >= wp->w_lines_valid)
1928 {
1929 wp->w_lines_valid = j;
1930 break;
1931 }
1932 wp->w_lines[j] = wp->w_lines[i];
1933 /* stop at a line that won't fit */
1934 if (x + (int)wp->w_lines[j].wl_size
1935 > wp->w_height)
1936 {
1937 wp->w_lines_valid = j + 1;
1938 break;
1939 }
1940 x += wp->w_lines[j++].wl_size;
1941 ++i;
1942 }
1943 if (bot_start > x)
1944 bot_start = x;
1945 }
1946 else /* j > i */
1947 {
1948 /* move entries in w_lines[] downwards */
1949 j -= i;
1950 wp->w_lines_valid += j;
1951 if (wp->w_lines_valid > wp->w_height)
1952 wp->w_lines_valid = wp->w_height;
1953 for (i = wp->w_lines_valid; i - j >= idx; --i)
1954 wp->w_lines[i] = wp->w_lines[i - j];
1955
1956 /* The w_lines[] entries for inserted lines are
1957 * now invalid, but wl_size may be used above.
1958 * Reset to zero. */
1959 while (i >= idx)
1960 {
1961 wp->w_lines[i].wl_size = 0;
1962 wp->w_lines[i--].wl_valid = FALSE;
1963 }
1964 }
1965 }
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968
1969#ifdef FEAT_FOLDING
1970 /*
1971 * When lines are folded, display one line for all of them.
1972 * Otherwise, display normally (can be several display lines when
1973 * 'wrap' is on).
1974 */
1975 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1976 if (fold_count != 0)
1977 {
1978 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1979 ++row;
1980 --fold_count;
1981 wp->w_lines[idx].wl_folded = TRUE;
1982 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1983# ifdef FEAT_SYN_HL
1984 did_update = DID_FOLD;
1985# endif
1986 }
1987 else
1988#endif
1989 if (idx < wp->w_lines_valid
1990 && wp->w_lines[idx].wl_valid
1991 && wp->w_lines[idx].wl_lnum == lnum
1992 && lnum > wp->w_topline
1993 && !(dy_flags & DY_LASTLINE)
1994 && srow + wp->w_lines[idx].wl_size > wp->w_height
1995#ifdef FEAT_DIFF
1996 && diff_check_fill(wp, lnum) == 0
1997#endif
1998 )
1999 {
2000 /* This line is not going to fit. Don't draw anything here,
2001 * will draw "@ " lines below. */
2002 row = wp->w_height + 1;
2003 }
2004 else
2005 {
2006#ifdef FEAT_SEARCH_EXTRA
2007 prepare_search_hl(wp, lnum);
2008#endif
2009#ifdef FEAT_SYN_HL
2010 /* Let the syntax stuff know we skipped a few lines. */
2011 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002012 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 syntax_end_parsing(syntax_last_parsed + 1);
2014#endif
2015
2016 /*
2017 * Display one line.
2018 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002019 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021#ifdef FEAT_FOLDING
2022 wp->w_lines[idx].wl_folded = FALSE;
2023 wp->w_lines[idx].wl_lastlnum = lnum;
2024#endif
2025#ifdef FEAT_SYN_HL
2026 did_update = DID_LINE;
2027 syntax_last_parsed = lnum;
2028#endif
2029 }
2030
2031 wp->w_lines[idx].wl_lnum = lnum;
2032 wp->w_lines[idx].wl_valid = TRUE;
2033 if (row > wp->w_height) /* past end of screen */
2034 {
2035 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002036 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2038 ++idx;
2039 break;
2040 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002041 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 wp->w_lines[idx].wl_size = row - srow;
2043 ++idx;
2044#ifdef FEAT_FOLDING
2045 lnum += fold_count + 1;
2046#else
2047 ++lnum;
2048#endif
2049 }
2050 else
2051 {
2052 /* This line does not need updating, advance to the next one */
2053 row += wp->w_lines[idx++].wl_size;
2054 if (row > wp->w_height) /* past end of screen */
2055 break;
2056#ifdef FEAT_FOLDING
2057 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2058#else
2059 ++lnum;
2060#endif
2061#ifdef FEAT_SYN_HL
2062 did_update = DID_NONE;
2063#endif
2064 }
2065
2066 if (lnum > buf->b_ml.ml_line_count)
2067 {
2068 eof = TRUE;
2069 break;
2070 }
2071 }
2072 /*
2073 * End of loop over all window lines.
2074 */
2075
2076
2077 if (idx > wp->w_lines_valid)
2078 wp->w_lines_valid = idx;
2079
2080#ifdef FEAT_SYN_HL
2081 /*
2082 * Let the syntax stuff know we stop parsing here.
2083 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002084 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 syntax_end_parsing(syntax_last_parsed + 1);
2086#endif
2087
2088 /*
2089 * If we didn't hit the end of the file, and we didn't finish the last
2090 * line we were working on, then the line didn't fit.
2091 */
2092 wp->w_empty_rows = 0;
2093#ifdef FEAT_DIFF
2094 wp->w_filler_rows = 0;
2095#endif
2096 if (!eof && !didline)
2097 {
2098 if (lnum == wp->w_topline)
2099 {
2100 /*
2101 * Single line that does not fit!
2102 * Don't overwrite it, it can be edited.
2103 */
2104 wp->w_botline = lnum + 1;
2105 }
2106#ifdef FEAT_DIFF
2107 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2108 {
2109 /* Window ends in filler lines. */
2110 wp->w_botline = lnum;
2111 wp->w_filler_rows = wp->w_height - srow;
2112 }
2113#endif
2114 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2115 {
2116 /*
2117 * Last line isn't finished: Display "@@@" at the end.
2118 */
2119 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2120 W_WINROW(wp) + wp->w_height,
2121 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2122 '@', '@', hl_attr(HLF_AT));
2123 set_empty_rows(wp, srow);
2124 wp->w_botline = lnum;
2125 }
2126 else
2127 {
2128 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2129 wp->w_botline = lnum;
2130 }
2131 }
2132 else
2133 {
2134#ifdef FEAT_VERTSPLIT
2135 draw_vsep_win(wp, row);
2136#endif
2137 if (eof) /* we hit the end of the file */
2138 {
2139 wp->w_botline = buf->b_ml.ml_line_count + 1;
2140#ifdef FEAT_DIFF
2141 j = diff_check_fill(wp, wp->w_botline);
2142 if (j > 0 && !wp->w_botfill)
2143 {
2144 /*
2145 * Display filler lines at the end of the file
2146 */
2147 if (char2cells(fill_diff) > 1)
2148 i = '-';
2149 else
2150 i = fill_diff;
2151 if (row + j > wp->w_height)
2152 j = wp->w_height - row;
2153 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2154 row += j;
2155 }
2156#endif
2157 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002158 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 wp->w_botline = lnum;
2160
2161 /* make sure the rest of the screen is blank */
2162 /* put '~'s on rows that aren't part of the file. */
2163 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2164 }
2165
2166 /* Reset the type of redrawing required, the window has been updated. */
2167 wp->w_redr_type = 0;
2168#ifdef FEAT_DIFF
2169 wp->w_old_topfill = wp->w_topfill;
2170 wp->w_old_botfill = wp->w_botfill;
2171#endif
2172
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002173 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
2175 /*
2176 * There is a trick with w_botline. If we invalidate it on each
2177 * change that might modify it, this will cause a lot of expensive
2178 * calls to plines() in update_topline() each time. Therefore the
2179 * value of w_botline is often approximated, and this value is used to
2180 * compute the value of w_topline. If the value of w_botline was
2181 * wrong, check that the value of w_topline is correct (cursor is on
2182 * the visible part of the text). If it's not, we need to redraw
2183 * again. Mostly this just means scrolling up a few lines, so it
2184 * doesn't look too bad. Only do this for the current window (where
2185 * changes are relevant).
2186 */
2187 wp->w_valid |= VALID_BOTLINE;
2188 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2189 {
2190 recursive = TRUE;
2191 curwin->w_valid &= ~VALID_TOPLINE;
2192 update_topline(); /* may invalidate w_botline again */
2193 if (must_redraw != 0)
2194 {
2195 /* Don't update for changes in buffer again. */
2196 i = curbuf->b_mod_set;
2197 curbuf->b_mod_set = FALSE;
2198 win_update(curwin);
2199 must_redraw = 0;
2200 curbuf->b_mod_set = i;
2201 }
2202 recursive = FALSE;
2203 }
2204 }
2205
2206#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2207 /* restore got_int, unless CTRL-C was hit while redrawing */
2208 if (!got_int)
2209 got_int = save_got_int;
2210#endif
2211}
2212
2213#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002214static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216/*
2217 * Return TRUE when window "wp" has a column to draw signs in.
2218 */
2219 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002220draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 return (wp->w_buffer->b_signlist != NULL
2223# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002224 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225# endif
2226 );
2227}
2228#endif
2229
2230/*
2231 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2232 * as the filler character.
2233 */
2234 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002235win_draw_end(
2236 win_T *wp,
2237 int c1,
2238 int c2,
2239 int row,
2240 int endrow,
2241 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2244 int n = 0;
2245# define FDC_OFF n
2246#else
2247# define FDC_OFF 0
2248#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002249#ifdef FEAT_FOLDING
2250 int fdc = compute_foldcolumn(wp, 0);
2251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253#ifdef FEAT_RIGHTLEFT
2254 if (wp->w_p_rl)
2255 {
2256 /* No check for cmdline window: should never be right-left. */
2257# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002258 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260 if (n > 0)
2261 {
2262 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002263 if (n > W_WIDTH(wp))
2264 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2266 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2267 ' ', ' ', hl_attr(HLF_FC));
2268 }
2269# endif
2270# ifdef FEAT_SIGNS
2271 if (draw_signcolumn(wp))
2272 {
2273 int nn = n + 2;
2274
2275 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002276 if (nn > W_WIDTH(wp))
2277 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2279 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2280 ' ', ' ', hl_attr(HLF_SC));
2281 n = nn;
2282 }
2283# endif
2284 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2285 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2286 c2, c2, hl_attr(hl));
2287 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2288 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2289 c1, c2, hl_attr(hl));
2290 }
2291 else
2292#endif
2293 {
2294#ifdef FEAT_CMDWIN
2295 if (cmdwin_type != 0 && wp == curwin)
2296 {
2297 /* draw the cmdline character in the leftmost column */
2298 n = 1;
2299 if (n > wp->w_width)
2300 n = wp->w_width;
2301 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2302 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2303 cmdwin_type, ' ', hl_attr(HLF_AT));
2304 }
2305#endif
2306#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002307 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002309 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 /* draw the fold column at the left */
2312 if (nn > W_WIDTH(wp))
2313 nn = W_WIDTH(wp);
2314 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2315 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2316 ' ', ' ', hl_attr(HLF_FC));
2317 n = nn;
2318 }
2319#endif
2320#ifdef FEAT_SIGNS
2321 if (draw_signcolumn(wp))
2322 {
2323 int nn = n + 2;
2324
2325 /* draw the sign column after the fold column */
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_SC));
2331 n = nn;
2332 }
2333#endif
2334 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2335 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2336 c1, c2, hl_attr(hl));
2337 }
2338 set_empty_rows(wp, row);
2339}
2340
Bram Moolenaar1a384422010-07-14 19:53:30 +02002341#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002342static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002343
2344/*
2345 * Advance **color_cols and return TRUE when there are columns to draw.
2346 */
2347 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002348advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002349{
2350 while (**color_cols >= 0 && vcol > **color_cols)
2351 ++*color_cols;
2352 return (**color_cols >= 0);
2353}
2354#endif
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#ifdef FEAT_FOLDING
2357/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002358 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2359 * space is available for window "wp", minus "col".
2360 */
2361 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002362compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002363{
2364 int fdc = wp->w_p_fdc;
2365 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2366 int wwidth = W_WIDTH(wp);
2367
2368 if (fdc > wwidth - (col + wmw))
2369 fdc = wwidth - (col + wmw);
2370 return fdc;
2371}
2372
2373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * Display one folded line.
2375 */
2376 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002377fold_line(
2378 win_T *wp,
2379 long fold_count,
2380 foldinfo_T *foldinfo,
2381 linenr_T lnum,
2382 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 char_u buf[51];
2385 pos_T *top, *bot;
2386 linenr_T lnume = lnum + fold_count - 1;
2387 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002388 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 int col;
2391 int txtcol;
2392 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002393 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 /* Build the fold line:
2396 * 1. Add the cmdwin_type for the command-line window
2397 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002398 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * 4. Compose the text
2400 * 5. Add the text
2401 * 6. set highlighting for the Visual area an other text
2402 */
2403 col = 0;
2404
2405 /*
2406 * 1. Add the cmdwin_type for the command-line window
2407 * Ignores 'rightleft', this window is never right-left.
2408 */
2409#ifdef FEAT_CMDWIN
2410 if (cmdwin_type != 0 && wp == curwin)
2411 {
2412 ScreenLines[off] = cmdwin_type;
2413 ScreenAttrs[off] = hl_attr(HLF_AT);
2414#ifdef FEAT_MBYTE
2415 if (enc_utf8)
2416 ScreenLinesUC[off] = 0;
2417#endif
2418 ++col;
2419 }
2420#endif
2421
2422 /*
2423 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002426 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (fdc > 0)
2428 {
2429 fill_foldcolumn(buf, wp, TRUE, lnum);
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 {
2433 int i;
2434
2435 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2436 hl_attr(HLF_FC));
2437 /* reverse the fold column */
2438 for (i = 0; i < fdc; ++i)
2439 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2440 }
2441 else
2442#endif
2443 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2444 col += fdc;
2445 }
2446
2447#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002448# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2449 for (ri = 0; ri < l; ++ri) \
2450 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2451 else \
2452 for (ri = 0; ri < l; ++ri) \
2453 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002455# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2456 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#endif
2458
Bram Moolenaar64486672010-05-16 15:46:46 +02002459 /* Set all attributes of the 'number' or 'relativenumber' column and the
2460 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002461 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
2463#ifdef FEAT_SIGNS
2464 /* If signs are being displayed, add two spaces. */
2465 if (draw_signcolumn(wp))
2466 {
2467 len = W_WIDTH(wp) - col;
2468 if (len > 0)
2469 {
2470 if (len > 2)
2471 len = 2;
2472# ifdef FEAT_RIGHTLEFT
2473 if (wp->w_p_rl)
2474 /* the line number isn't reversed */
2475 copy_text_attr(off + W_WIDTH(wp) - len - col,
2476 (char_u *)" ", len, hl_attr(HLF_FL));
2477 else
2478# endif
2479 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2480 col += len;
2481 }
2482 }
2483#endif
2484
2485 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002486 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002488 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 len = W_WIDTH(wp) - col;
2491 if (len > 0)
2492 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002493 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002494 long num;
2495 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002496
2497 if (len > w + 1)
2498 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002499
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002500 if (wp->w_p_nu && !wp->w_p_rnu)
2501 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002502 num = (long)lnum;
2503 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002504 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002505 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002506 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002507 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002508 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002509 /* 'number' + 'relativenumber': cursor line shows absolute
2510 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002511 num = lnum;
2512 fmt = "%-*ld ";
2513 }
2514 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002515
Bram Moolenaar700e7342013-01-30 12:31:36 +01002516 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517#ifdef FEAT_RIGHTLEFT
2518 if (wp->w_p_rl)
2519 /* the line number isn't reversed */
2520 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2521 hl_attr(HLF_FL));
2522 else
2523#endif
2524 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2525 col += len;
2526 }
2527 }
2528
2529 /*
2530 * 4. Compose the folded-line string with 'foldtext', if set.
2531 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002532 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 txtcol = col; /* remember where text starts */
2535
2536 /*
2537 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2538 * Right-left text is put in columns 0 - number-col, normal text is put
2539 * in columns number-col - window-width.
2540 */
2541#ifdef FEAT_MBYTE
2542 if (has_mbyte)
2543 {
2544 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002545 int u8c, u8cc[MAX_MCO];
2546 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 int idx;
2548 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002549 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550# ifdef FEAT_ARABIC
2551 int prev_c = 0; /* previous Arabic character */
2552 int prev_c1 = 0; /* first composing char for prev_c */
2553# endif
2554
2555# ifdef FEAT_RIGHTLEFT
2556 if (wp->w_p_rl)
2557 idx = off;
2558 else
2559# endif
2560 idx = off + col;
2561
2562 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2563 for (p = text; *p != NUL; )
2564 {
2565 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002566 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (col + cells > W_WIDTH(wp)
2568# ifdef FEAT_RIGHTLEFT
2569 - (wp->w_p_rl ? col : 0)
2570# endif
2571 )
2572 break;
2573 ScreenLines[idx] = *p;
2574 if (enc_utf8)
2575 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002576 u8c = utfc_ptr2char(p, u8cc);
2577 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 ScreenLinesUC[idx] = 0;
2580#ifdef FEAT_ARABIC
2581 prev_c = u8c;
2582#endif
2583 }
2584 else
2585 {
2586#ifdef FEAT_ARABIC
2587 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2588 {
2589 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 int pc, pc1, nc;
2591 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int firstbyte = *p;
2593
2594 /* The idea of what is the previous and next
2595 * character depends on 'rightleft'. */
2596 if (wp->w_p_rl)
2597 {
2598 pc = prev_c;
2599 pc1 = prev_c1;
2600 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002601 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603 else
2604 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002605 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002607 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609 prev_c = u8c;
2610
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002611 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 pc, pc1, nc);
2613 ScreenLines[idx] = firstbyte;
2614 }
2615 else
2616 prev_c = u8c;
2617#endif
2618 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002619#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 if (u8c >= 0x10000)
2621 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2622 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002625 for (i = 0; i < Screen_mco; ++i)
2626 {
2627 ScreenLinesC[i][idx] = u8cc[i];
2628 if (u8cc[i] == 0)
2629 break;
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 if (cells > 1)
2633 ScreenLines[idx + 1] = 0;
2634 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002635 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2636 /* double-byte single width character */
2637 ScreenLines2[idx] = p[1];
2638 else if (cells > 1)
2639 /* double-width character */
2640 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 col += cells;
2642 idx += cells;
2643 p += c_len;
2644 }
2645 }
2646 else
2647#endif
2648 {
2649 len = (int)STRLEN(text);
2650 if (len > W_WIDTH(wp) - col)
2651 len = W_WIDTH(wp) - col;
2652 if (len > 0)
2653 {
2654#ifdef FEAT_RIGHTLEFT
2655 if (wp->w_p_rl)
2656 STRNCPY(current_ScreenLine, text, len);
2657 else
2658#endif
2659 STRNCPY(current_ScreenLine + col, text, len);
2660 col += len;
2661 }
2662 }
2663
2664 /* Fill the rest of the line with the fold filler */
2665#ifdef FEAT_RIGHTLEFT
2666 if (wp->w_p_rl)
2667 col -= txtcol;
2668#endif
2669 while (col < W_WIDTH(wp)
2670#ifdef FEAT_RIGHTLEFT
2671 - (wp->w_p_rl ? txtcol : 0)
2672#endif
2673 )
2674 {
2675#ifdef FEAT_MBYTE
2676 if (enc_utf8)
2677 {
2678 if (fill_fold >= 0x80)
2679 {
2680 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683 else
2684 ScreenLinesUC[off + col] = 0;
2685 }
2686#endif
2687 ScreenLines[off + col++] = fill_fold;
2688 }
2689
2690 if (text != buf)
2691 vim_free(text);
2692
2693 /*
2694 * 6. set highlighting for the Visual area an other text.
2695 * If all folded lines are in the Visual area, highlight the line.
2696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2698 {
2699 if (ltoreq(curwin->w_cursor, VIsual))
2700 {
2701 /* Visual is after curwin->w_cursor */
2702 top = &curwin->w_cursor;
2703 bot = &VIsual;
2704 }
2705 else
2706 {
2707 /* Visual is before curwin->w_cursor */
2708 top = &VIsual;
2709 bot = &curwin->w_cursor;
2710 }
2711 if (lnum >= top->lnum
2712 && lnume <= bot->lnum
2713 && (VIsual_mode != 'v'
2714 || ((lnum > top->lnum
2715 || (lnum == top->lnum
2716 && top->col == 0))
2717 && (lnume < bot->lnum
2718 || (lnume == bot->lnum
2719 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002720 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 if (VIsual_mode == Ctrl_V)
2723 {
2724 /* Visual block mode: highlight the chars part of the block */
2725 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2726 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002727 if (wp->w_old_cursor_lcol != MAXCOL
2728 && wp->w_old_cursor_lcol + txtcol
2729 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 len = wp->w_old_cursor_lcol;
2731 else
2732 len = W_WIDTH(wp) - txtcol;
2733 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002734 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 }
2737 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002740 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002745#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002746 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2747 if (wp->w_p_cc_cols)
2748 {
2749 int i = 0;
2750 int j = wp->w_p_cc_cols[i];
2751 int old_txtcol = txtcol;
2752
2753 while (j > -1)
2754 {
2755 txtcol += j;
2756 if (wp->w_p_wrap)
2757 txtcol -= wp->w_skipcol;
2758 else
2759 txtcol -= wp->w_leftcol;
2760 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2761 ScreenAttrs[off + txtcol] = hl_combine_attr(
2762 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2763 txtcol = old_txtcol;
2764 j = wp->w_p_cc_cols[++i];
2765 }
2766 }
2767
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002768 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002769 if (wp->w_p_cuc)
2770 {
2771 txtcol += wp->w_virtcol;
2772 if (wp->w_p_wrap)
2773 txtcol -= wp->w_skipcol;
2774 else
2775 txtcol -= wp->w_leftcol;
2776 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2777 ScreenAttrs[off + txtcol] = hl_combine_attr(
2778 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2783 (int)W_WIDTH(wp), FALSE);
2784
2785 /*
2786 * Update w_cline_height and w_cline_folded if the cursor line was
2787 * updated (saves a call to plines() later).
2788 */
2789 if (wp == curwin
2790 && lnum <= curwin->w_cursor.lnum
2791 && lnume >= curwin->w_cursor.lnum)
2792 {
2793 curwin->w_cline_row = row;
2794 curwin->w_cline_height = 1;
2795 curwin->w_cline_folded = TRUE;
2796 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2797 }
2798}
2799
2800/*
2801 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2802 */
2803 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002804copy_text_attr(
2805 int off,
2806 char_u *buf,
2807 int len,
2808 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002810 int i;
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 mch_memmove(ScreenLines + off, buf, (size_t)len);
2813# ifdef FEAT_MBYTE
2814 if (enc_utf8)
2815 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2816# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002817 for (i = 0; i < len; ++i)
2818 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
2821/*
2822 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002823 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 */
2825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002826fill_foldcolumn(
2827 char_u *p,
2828 win_T *wp,
2829 int closed, /* TRUE of FALSE */
2830 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
2832 int i = 0;
2833 int level;
2834 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002835 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002836 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002839 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840
2841 level = win_foldinfo.fi_level;
2842 if (level > 0)
2843 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002844 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002845 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 /* If the column is too narrow, we start at the lowest level that
2848 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002849 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (first_level < 1)
2851 first_level = 1;
2852
Bram Moolenaar1c934292015-01-27 16:39:29 +01002853 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
2855 if (win_foldinfo.fi_lnum == lnum
2856 && first_level + i >= win_foldinfo.fi_low_level)
2857 p[i] = '-';
2858 else if (first_level == 1)
2859 p[i] = '|';
2860 else if (first_level + i <= 9)
2861 p[i] = '0' + first_level + i;
2862 else
2863 p[i] = '>';
2864 if (first_level + i == level)
2865 break;
2866 }
2867 }
2868 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002869 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870}
2871#endif /* FEAT_FOLDING */
2872
2873/*
2874 * Display line "lnum" of window 'wp' on the screen.
2875 * Start at row "startrow", stop when "endrow" is reached.
2876 * wp->w_virtcol needs to be valid.
2877 *
2878 * Return the number of last row the line occupies.
2879 */
2880 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002881win_line(
2882 win_T *wp,
2883 linenr_T lnum,
2884 int startrow,
2885 int endrow,
2886 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 int col; /* visual column on screen */
2889 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2890 int c = 0; /* init for GCC */
2891 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002892#ifdef FEAT_LINEBREAK
2893 long vcol_sbr = -1; /* virtual column after showbreak */
2894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 long vcol_prev = -1; /* "vcol" of previous character */
2896 char_u *line; /* current line */
2897 char_u *ptr; /* current position in "line" */
2898 int row; /* row in the window, excl w_winrow */
2899 int screen_row; /* row on the screen, incl w_winrow */
2900
2901 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2902 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002903 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002904 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 int c_extra = NUL; /* extra chars, all the same */
2906 int extra_attr = 0; /* attributes when n_extra != 0 */
2907 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2908 displaying lcs_eol at end-of-line */
2909 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2910 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2911
2912 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2913 int saved_n_extra = 0;
2914 char_u *saved_p_extra = NULL;
2915 int saved_c_extra = 0;
2916 int saved_char_attr = 0;
2917
2918 int n_attr = 0; /* chars with special attr */
2919 int saved_attr2 = 0; /* char_attr saved for n_attr */
2920 int n_attr3 = 0; /* chars with overruling special attr */
2921 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2922
2923 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2924
2925 int fromcol, tocol; /* start/end of inverting */
2926 int fromcol_prev = -2; /* start of inverting after cursor */
2927 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002929 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 pos_T pos;
2931 long v;
2932
2933 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002934 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2936 in this line */
2937 int attr = 0; /* attributes for area highlighting */
2938 int area_attr = 0; /* attributes desired by highlighting */
2939 int search_attr = 0; /* attributes desired by 'hlsearch' */
2940#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002941 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 int syntax_attr = 0; /* attributes desired by syntax */
2943 int has_syntax = FALSE; /* this buffer has syntax highl. */
2944 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002945 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002946 int draw_color_col = FALSE; /* highlight colorcolumn */
2947 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002948#endif
2949#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002950 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002951# define SPWORDLEN 150
2952 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002953 int nextlinecol = 0; /* column where nextline[] starts */
2954 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002955 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002956 int spell_attr = 0; /* attributes desired by spelling */
2957 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002958 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2959 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002960 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002961 static int cap_col = -1; /* column to check for Cap word */
2962 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002963 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964#endif
2965 int extra_check; /* has syntax or linebreak */
2966#ifdef FEAT_MBYTE
2967 int multi_attr = 0; /* attributes desired by multibyte */
2968 int mb_l = 1; /* multi-byte byte length */
2969 int mb_c = 0; /* decoded multi-byte character */
2970 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002971 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#endif
2973#ifdef FEAT_DIFF
2974 int filler_lines; /* nr of filler lines to be drawn */
2975 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002976 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 int change_start = MAXCOL; /* first col of changed area */
2978 int change_end = -1; /* last col of changed area */
2979#endif
2980 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2981#ifdef FEAT_LINEBREAK
2982 int need_showbreak = FALSE;
2983#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002984#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2985 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002987 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#endif
2989#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002990 matchitem_T *cur; /* points to the match list */
2991 match_T *shl; /* points to search_hl or a match */
2992 int shl_flag; /* flag to indicate whether search_hl
2993 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002994 int pos_inprogress; /* marks that position match search is
2995 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002996 int prevcol_hl_flag; /* flag to indicate whether prevcol
2997 equals startcol of search_hl or one
2998 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#endif
3000#ifdef FEAT_ARABIC
3001 int prev_c = 0; /* previous Arabic character */
3002 int prev_c1 = 0; /* first composing char for prev_c */
3003#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003004#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003005 int did_line_attr = 0;
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 /* draw_state: items that are drawn in sequence: */
3009#define WL_START 0 /* nothing done yet */
3010#ifdef FEAT_CMDWIN
3011# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3012#else
3013# define WL_CMDLINE WL_START
3014#endif
3015#ifdef FEAT_FOLDING
3016# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3017#else
3018# define WL_FOLD WL_CMDLINE
3019#endif
3020#ifdef FEAT_SIGNS
3021# define WL_SIGN WL_FOLD + 1 /* column for signs */
3022#else
3023# define WL_SIGN WL_FOLD /* column for signs */
3024#endif
3025#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003026#ifdef FEAT_LINEBREAK
3027# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003029# define WL_BRI WL_NR
3030#endif
3031#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3032# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3033#else
3034# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035#endif
3036#define WL_LINE WL_SBR + 1 /* text in the line */
3037 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003038#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int feedback_col = 0;
3040 int feedback_old_attr = -1;
3041#endif
3042
Bram Moolenaar860cae12010-06-05 23:22:07 +02003043#ifdef FEAT_CONCEAL
3044 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003045 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003046 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003047 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003048 int is_concealing = FALSE;
3049 int boguscols = 0; /* nonexistent columns added to force
3050 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003051 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003052 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003053 int match_conc = FALSE; /* cchar for match functions */
3054 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003055 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003056# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003057# define FIX_FOR_BOGUSCOLS \
3058 { \
3059 n_extra += vcol_off; \
3060 vcol -= vcol_off; \
3061 vcol_off = 0; \
3062 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003063 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003064 boguscols = 0; \
3065 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003066#else
3067# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 if (startrow > endrow) /* past the end already! */
3071 return startrow;
3072
3073 row = startrow;
3074 screen_row = row + W_WINROW(wp);
3075
3076 /*
3077 * To speed up the loop below, set extra_check when there is linebreak,
3078 * trailing white space and/or syntax processing to be done.
3079 */
3080#ifdef FEAT_LINEBREAK
3081 extra_check = wp->w_p_lbr;
3082#else
3083 extra_check = 0;
3084#endif
3085#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003086 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 /* Prepare for syntax highlighting in this line. When there is an
3089 * error, stop syntax highlighting. */
3090 save_did_emsg = did_emsg;
3091 did_emsg = FALSE;
3092 syntax_start(wp, lnum);
3093 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003094 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
3096 {
3097 did_emsg = save_did_emsg;
3098 has_syntax = TRUE;
3099 extra_check = TRUE;
3100 }
3101 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003102
3103 /* Check for columns to display for 'colorcolumn'. */
3104 color_cols = wp->w_p_cc_cols;
3105 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003106 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003107#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003108
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003109#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003110 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003111 && *wp->w_s->b_p_spl != NUL
3112 && wp->w_s->b_langp.ga_len > 0
3113 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003114 {
3115 /* Prepare for spell checking. */
3116 has_spell = TRUE;
3117 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118
3119 /* Get the start of the next line, so that words that wrap to the next
3120 * line are found too: "et<line-break>al.".
3121 * Trick: skip a few chars for C/shell/Vim comments */
3122 nextline[SPWORDLEN] = NUL;
3123 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3124 {
3125 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3126 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3127 }
3128
3129 /* When a word wrapped from the previous line the start of the current
3130 * line is valid. */
3131 if (lnum == checked_lnum)
3132 cur_checked_col = checked_col;
3133 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003134
3135 /* When there was a sentence end in the previous line may require a
3136 * word starting with capital in this line. In line 1 always check
3137 * the first word. */
3138 if (lnum != capcol_lnum)
3139 cap_col = -1;
3140 if (lnum == 1)
3141 cap_col = 0;
3142 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#endif
3145
3146 /*
3147 * handle visual active in this window
3148 */
3149 fromcol = -10;
3150 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3152 {
3153 /* Visual is after curwin->w_cursor */
3154 if (ltoreq(curwin->w_cursor, VIsual))
3155 {
3156 top = &curwin->w_cursor;
3157 bot = &VIsual;
3158 }
3159 else /* Visual is before curwin->w_cursor */
3160 {
3161 top = &VIsual;
3162 bot = &curwin->w_cursor;
3163 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003164 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (VIsual_mode == Ctrl_V) /* block mode */
3166 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003167 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 fromcol = wp->w_old_cursor_fcol;
3170 tocol = wp->w_old_cursor_lcol;
3171 }
3172 }
3173 else /* non-block mode */
3174 {
3175 if (lnum > top->lnum && lnum <= bot->lnum)
3176 fromcol = 0;
3177 else if (lnum == top->lnum)
3178 {
3179 if (VIsual_mode == 'V') /* linewise */
3180 fromcol = 0;
3181 else
3182 {
3183 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3184 if (gchar_pos(top) == NUL)
3185 tocol = fromcol + 1;
3186 }
3187 }
3188 if (VIsual_mode != 'V' && lnum == bot->lnum)
3189 {
3190 if (*p_sel == 'e' && bot->col == 0
3191#ifdef FEAT_VIRTUALEDIT
3192 && bot->coladd == 0
3193#endif
3194 )
3195 {
3196 fromcol = -10;
3197 tocol = MAXCOL;
3198 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003199 else if (bot->col == MAXCOL)
3200 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
3202 {
3203 pos = *bot;
3204 if (*p_sel == 'e')
3205 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3206 else
3207 {
3208 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3209 ++tocol;
3210 }
3211 }
3212 }
3213 }
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 /* Check if the character under the cursor should not be inverted */
3216 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003217#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 )
3221 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 /* if inverting in this line set area_highlighting */
3224 if (fromcol >= 0)
3225 {
3226 area_highlighting = TRUE;
3227 attr = hl_attr(HLF_V);
3228#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003229 if ((clip_star.available && !clip_star.owned
3230 && clip_isautosel_star())
3231 || (clip_plus.available && !clip_plus.owned
3232 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 attr = hl_attr(HLF_VNC);
3234#endif
3235 }
3236 }
3237
3238 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003239 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003241 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 && wp == curwin
3243 && lnum >= curwin->w_cursor.lnum
3244 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3245 {
3246 if (lnum == curwin->w_cursor.lnum)
3247 getvcol(curwin, &(curwin->w_cursor),
3248 (colnr_T *)&fromcol, NULL, NULL);
3249 else
3250 fromcol = 0;
3251 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3252 {
3253 pos.lnum = lnum;
3254 pos.col = search_match_endcol;
3255 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3256 }
3257 else
3258 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003259 /* do at least one character; happens when past end of line */
3260 if (fromcol == tocol)
3261 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 area_highlighting = TRUE;
3263 attr = hl_attr(HLF_I);
3264 }
3265
3266#ifdef FEAT_DIFF
3267 filler_lines = diff_check(wp, lnum);
3268 if (filler_lines < 0)
3269 {
3270 if (filler_lines == -1)
3271 {
3272 if (diff_find_change(wp, lnum, &change_start, &change_end))
3273 diff_hlf = HLF_ADD; /* added line */
3274 else if (change_start == 0)
3275 diff_hlf = HLF_TXD; /* changed text */
3276 else
3277 diff_hlf = HLF_CHD; /* changed line */
3278 }
3279 else
3280 diff_hlf = HLF_ADD; /* added line */
3281 filler_lines = 0;
3282 area_highlighting = TRUE;
3283 }
3284 if (lnum == wp->w_topline)
3285 filler_lines = wp->w_topfill;
3286 filler_todo = filler_lines;
3287#endif
3288
3289#ifdef LINE_ATTR
3290# ifdef FEAT_SIGNS
3291 /* If this line has a sign with line highlighting set line_attr. */
3292 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3293 if (v != 0)
3294 line_attr = sign_get_attr((int)v, TRUE);
3295# endif
3296# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3297 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003298 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 line_attr = hl_attr(HLF_L);
3300# endif
3301 if (line_attr != 0)
3302 area_highlighting = TRUE;
3303#endif
3304
3305 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3306 ptr = line;
3307
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003308#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003309 if (has_spell)
3310 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003311 /* For checking first word with a capital skip white space. */
3312 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003313 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003314
Bram Moolenaar30abd282005-06-22 22:35:10 +00003315 /* To be able to spell-check over line boundaries copy the end of the
3316 * current line into nextline[]. Above the start of the next line was
3317 * copied to nextline[SPWORDLEN]. */
3318 if (nextline[SPWORDLEN] == NUL)
3319 {
3320 /* No next line or it is empty. */
3321 nextlinecol = MAXCOL;
3322 nextline_idx = 0;
3323 }
3324 else
3325 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003326 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003327 if (v < SPWORDLEN)
3328 {
3329 /* Short line, use it completely and append the start of the
3330 * next line. */
3331 nextlinecol = 0;
3332 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003333 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003334 nextline_idx = v + 1;
3335 }
3336 else
3337 {
3338 /* Long line, use only the last SPWORDLEN bytes. */
3339 nextlinecol = v - SPWORDLEN;
3340 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3341 nextline_idx = SPWORDLEN + 1;
3342 }
3343 }
3344 }
3345#endif
3346
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003347 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003349 if (lcs_space || lcs_trail)
3350 extra_check = TRUE;
3351 /* find start of trailing whitespace */
3352 if (lcs_trail)
3353 {
3354 trailcol = (colnr_T)STRLEN(ptr);
3355 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3356 --trailcol;
3357 trailcol += (colnr_T) (ptr - line);
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 }
3360
3361 /*
3362 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3363 * first character to be displayed.
3364 */
3365 if (wp->w_p_wrap)
3366 v = wp->w_skipcol;
3367 else
3368 v = wp->w_leftcol;
3369 if (v > 0)
3370 {
3371#ifdef FEAT_MBYTE
3372 char_u *prev_ptr = ptr;
3373#endif
3374 while (vcol < v && *ptr != NUL)
3375 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003376 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 vcol += c;
3378#ifdef FEAT_MBYTE
3379 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003381 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003384 /* When:
3385 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003386 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003387 * - 'virtualedit' is set, or
3388 * - the visual mode is active,
3389 * the end of the line may be before the start of the displayed part.
3390 */
3391 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003392#ifdef FEAT_SYN_HL
3393 wp->w_p_cuc || draw_color_col ||
3394#endif
3395#ifdef FEAT_VIRTUALEDIT
3396 virtual_active() ||
3397#endif
3398 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 /* Handle a character that's not completely on the screen: Put ptr at
3404 * that character but skip the first few screen characters. */
3405 if (vcol > v)
3406 {
3407 vcol -= c;
3408#ifdef FEAT_MBYTE
3409 ptr = prev_ptr;
3410#else
3411 --ptr;
3412#endif
3413 n_skip = v - vcol;
3414 }
3415
3416 /*
3417 * Adjust for when the inverted text is before the screen,
3418 * and when the start of the inverted text is before the screen.
3419 */
3420 if (tocol <= vcol)
3421 fromcol = 0;
3422 else if (fromcol >= 0 && fromcol < vcol)
3423 fromcol = vcol;
3424
3425#ifdef FEAT_LINEBREAK
3426 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3427 if (wp->w_p_wrap)
3428 need_showbreak = TRUE;
3429#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003430#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003431 /* When spell checking a word we need to figure out the start of the
3432 * word and if it's badly spelled or not. */
3433 if (has_spell)
3434 {
3435 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003436 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003437 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003438
3439 pos = wp->w_cursor;
3440 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003441 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003442 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003443
3444 /* spell_move_to() may call ml_get() and make "line" invalid */
3445 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3446 ptr = line + linecol;
3447
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003448 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003449 {
3450 /* no bad word found at line start, don't check until end of a
3451 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003452 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003453 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003454 }
3455 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003456 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003457 /* bad word found, use attributes until end of word */
3458 word_end = wp->w_cursor.col + len + 1;
3459
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003460 /* Turn index into actual attributes. */
3461 if (spell_hlf != HLF_COUNT)
3462 spell_attr = highlight_attr[spell_hlf];
3463 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003464 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003465
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003466# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003467 /* Need to restart syntax highlighting for this line. */
3468 if (has_syntax)
3469 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003470# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003471 }
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474
3475 /*
3476 * Correct highlighting for cursor that can't be disabled.
3477 * Avoids having to check this for each character.
3478 */
3479 if (fromcol >= 0)
3480 {
3481 if (noinvcur)
3482 {
3483 if ((colnr_T)fromcol == wp->w_virtcol)
3484 {
3485 /* highlighting starts at cursor, let it start just after the
3486 * cursor */
3487 fromcol_prev = fromcol;
3488 fromcol = -1;
3489 }
3490 else if ((colnr_T)fromcol < wp->w_virtcol)
3491 /* restart highlighting after the cursor */
3492 fromcol_prev = wp->w_virtcol;
3493 }
3494 if (fromcol >= tocol)
3495 fromcol = -1;
3496 }
3497
3498#ifdef FEAT_SEARCH_EXTRA
3499 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 * Handle highlighting the last used search pattern and matches.
3501 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003503 cur = wp->w_match_head;
3504 shl_flag = FALSE;
3505 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003507 if (shl_flag == FALSE)
3508 {
3509 shl = &search_hl;
3510 shl_flag = TRUE;
3511 }
3512 else
3513 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003514 shl->startcol = MAXCOL;
3515 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003517 v = (long)(ptr - line);
3518 if (cur != NULL)
3519 cur->pos.cur = 0;
3520 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3521
3522 /* Need to get the line again, a multi-line regexp may have made it
3523 * invalid. */
3524 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3525 ptr = line + v;
3526
3527 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003529 if (shl->lnum == lnum)
3530 shl->startcol = shl->rm.startpos[0].col;
3531 else
3532 shl->startcol = 0;
3533 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3534 - shl->rm.startpos[0].lnum)
3535 shl->endcol = shl->rm.endpos[0].col;
3536 else
3537 shl->endcol = MAXCOL;
3538 /* Highlight one character for an empty match. */
3539 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003542 if (has_mbyte && line[shl->endcol] != NUL)
3543 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3544 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003546 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003548 if ((long)shl->startcol < v) /* match at leftcol */
3549 {
3550 shl->attr_cur = shl->attr;
3551 search_attr = shl->attr;
3552 }
3553 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003555 if (shl != &search_hl && cur != NULL)
3556 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558#endif
3559
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003560#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003561 /* Cursor line highlighting for 'cursorline' in the current window. Not
3562 * when Visual mode is active, because it's not clear what is selected
3563 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003564 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003565 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003566 {
3567 line_attr = hl_attr(HLF_CUL);
3568 area_highlighting = TRUE;
3569 }
3570#endif
3571
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003572 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 col = 0;
3574#ifdef FEAT_RIGHTLEFT
3575 if (wp->w_p_rl)
3576 {
3577 /* Rightleft window: process the text in the normal direction, but put
3578 * it in current_ScreenLine[] from right to left. Start at the
3579 * rightmost column of the window. */
3580 col = W_WIDTH(wp) - 1;
3581 off += col;
3582 }
3583#endif
3584
3585 /*
3586 * Repeat for the whole displayed line.
3587 */
3588 for (;;)
3589 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003590#ifdef FEAT_CONCEAL
3591 has_match_conc = FALSE;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 /* Skip this quickly when working on the text. */
3594 if (draw_state != WL_LINE)
3595 {
3596#ifdef FEAT_CMDWIN
3597 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3598 {
3599 draw_state = WL_CMDLINE;
3600 if (cmdwin_type != 0 && wp == curwin)
3601 {
3602 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003604 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_attr = hl_attr(HLF_AT);
3606 }
3607 }
3608#endif
3609
3610#ifdef FEAT_FOLDING
3611 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3612 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003613 int fdc = compute_foldcolumn(wp, 0);
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003616 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 /* Draw the 'foldcolumn'. */
3619 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003620 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003622 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 c_extra = NUL;
3624 char_attr = hl_attr(HLF_FC);
3625 }
3626 }
3627#endif
3628
3629#ifdef FEAT_SIGNS
3630 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3631 {
3632 draw_state = WL_SIGN;
3633 /* Show the sign column when there are any signs in this
3634 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003635 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003637 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003639 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640# endif
3641
3642 /* Draw two cells with the sign value or blank. */
3643 c_extra = ' ';
3644 char_attr = hl_attr(HLF_SC);
3645 n_extra = 2;
3646
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003647 if (row == startrow
3648#ifdef FEAT_DIFF
3649 + filler_lines && filler_todo <= 0
3650#endif
3651 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3654 SIGN_TEXT);
3655# ifdef FEAT_SIGN_ICONS
3656 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3657 SIGN_ICON);
3658 if (gui.in_use && icon_sign != 0)
3659 {
3660 /* Use the image in this position. */
3661 c_extra = SIGN_BYTE;
3662# ifdef FEAT_NETBEANS_INTG
3663 if (buf_signcount(wp->w_buffer, lnum) > 1)
3664 c_extra = MULTISIGN_BYTE;
3665# endif
3666 char_attr = icon_sign;
3667 }
3668 else
3669# endif
3670 if (text_sign != 0)
3671 {
3672 p_extra = sign_get_text(text_sign);
3673 if (p_extra != NULL)
3674 {
3675 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003676 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 char_attr = sign_get_attr(text_sign, FALSE);
3679 }
3680 }
3681 }
3682 }
3683#endif
3684
3685 if (draw_state == WL_NR - 1 && n_extra == 0)
3686 {
3687 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003688 /* Display the absolute or relative line number. After the
3689 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3690 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 && (row == startrow
3692#ifdef FEAT_DIFF
3693 + filler_lines
3694#endif
3695 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3696 {
3697 /* Draw the line number (empty space after wrapping). */
3698 if (row == startrow
3699#ifdef FEAT_DIFF
3700 + filler_lines
3701#endif
3702 )
3703 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003704 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003705 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003706
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003707 if (wp->w_p_nu && !wp->w_p_rnu)
3708 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003709 num = (long)lnum;
3710 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003711 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003712 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003713 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003714 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003715 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003716 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003717 num = lnum;
3718 fmt = "%-*ld ";
3719 }
3720 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003721
Bram Moolenaar700e7342013-01-30 12:31:36 +01003722 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003723 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (wp->w_skipcol > 0)
3725 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3726 *p_extra = '-';
3727#ifdef FEAT_RIGHTLEFT
3728 if (wp->w_p_rl) /* reverse line numbers */
3729 rl_mirror(extra);
3730#endif
3731 p_extra = extra;
3732 c_extra = NUL;
3733 }
3734 else
3735 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003736 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003738#ifdef FEAT_SYN_HL
3739 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003740 * the current line differently.
3741 * TODO: Can we use CursorLine instead of CursorLineNr
3742 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003743 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003744 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003745 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 }
3749
Bram Moolenaar597a4222014-06-25 14:39:50 +02003750#ifdef FEAT_LINEBREAK
3751 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3752 && n_extra == 0 && *p_sbr != NUL)
3753 /* draw indent after showbreak value */
3754 draw_state = WL_BRI;
3755 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3756 /* After the showbreak, draw the breakindent */
3757 draw_state = WL_BRI - 1;
3758
3759 /* draw 'breakindent': indent wrapped text accordingly */
3760 if (draw_state == WL_BRI - 1 && n_extra == 0)
3761 {
3762 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003763 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003764# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003765 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003766# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003767 )
3768 {
3769 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003770# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003771 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003772 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003773 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003774# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003775 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3776 char_attr = hl_combine_attr(char_attr,
3777 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003778# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003779 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003780# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003781 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003782 c_extra = ' ';
3783 n_extra = get_breakindent_win(wp,
3784 ml_get_buf(wp->w_buffer, lnum, FALSE));
3785 /* Correct end of highlighted area for 'breakindent',
3786 * required when 'linebreak' is also set. */
3787 if (tocol == vcol)
3788 tocol += n_extra;
3789 }
3790 }
3791#endif
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3794 if (draw_state == WL_SBR - 1 && n_extra == 0)
3795 {
3796 draw_state = WL_SBR;
3797# ifdef FEAT_DIFF
3798 if (filler_todo > 0)
3799 {
3800 /* Draw "deleted" diff line(s). */
3801 if (char2cells(fill_diff) > 1)
3802 c_extra = '-';
3803 else
3804 c_extra = fill_diff;
3805# ifdef FEAT_RIGHTLEFT
3806 if (wp->w_p_rl)
3807 n_extra = col + 1;
3808 else
3809# endif
3810 n_extra = W_WIDTH(wp) - col;
3811 char_attr = hl_attr(HLF_DED);
3812 }
3813# endif
3814# ifdef FEAT_LINEBREAK
3815 if (*p_sbr != NUL && need_showbreak)
3816 {
3817 /* Draw 'showbreak' at the start of each broken line. */
3818 p_extra = p_sbr;
3819 c_extra = NUL;
3820 n_extra = (int)STRLEN(p_sbr);
3821 char_attr = hl_attr(HLF_AT);
3822 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003823 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 /* Correct end of highlighted area for 'showbreak',
3825 * required when 'linebreak' is also set. */
3826 if (tocol == vcol)
3827 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003828#ifdef FEAT_SYN_HL
3829 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003830 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003831 char_attr = hl_combine_attr(char_attr,
3832 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835# endif
3836 }
3837#endif
3838
3839 if (draw_state == WL_LINE - 1 && n_extra == 0)
3840 {
3841 draw_state = WL_LINE;
3842 if (saved_n_extra)
3843 {
3844 /* Continue item from end of wrapped line. */
3845 n_extra = saved_n_extra;
3846 c_extra = saved_c_extra;
3847 p_extra = saved_p_extra;
3848 char_attr = saved_char_attr;
3849 }
3850 else
3851 char_attr = 0;
3852 }
3853 }
3854
3855 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003856 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003857 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#ifdef FEAT_DIFF
3859 && filler_todo <= 0
3860#endif
3861 )
3862 {
3863 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3864 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003865 /* Pretend we have finished updating the window. Except when
3866 * 'cursorcolumn' is set. */
3867#ifdef FEAT_SYN_HL
3868 if (wp->w_p_cuc)
3869 row = wp->w_cline_row + wp->w_cline_height;
3870 else
3871#endif
3872 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 break;
3874 }
3875
3876 if (draw_state == WL_LINE && area_highlighting)
3877 {
3878 /* handle Visual or match highlighting in this line */
3879 if (vcol == fromcol
3880#ifdef FEAT_MBYTE
3881 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3882 && (*mb_ptr2cells)(ptr) > 1)
3883#endif
3884 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003885 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 && vcol < tocol))
3887 area_attr = attr; /* start highlighting */
3888 else if (area_attr != 0
3889 && (vcol == tocol
3890 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893#ifdef FEAT_SEARCH_EXTRA
3894 if (!n_extra)
3895 {
3896 /*
3897 * Check for start/end of search pattern match.
3898 * After end, check for start/end of next match.
3899 * When another match, have to check for start again.
3900 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003901 * Do this for 'search_hl' and the match list (ordered by
3902 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003904 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003905 cur = wp->w_match_head;
3906 shl_flag = FALSE;
3907 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003909 if (shl_flag == FALSE
3910 && ((cur != NULL
3911 && cur->priority > SEARCH_HL_PRIORITY)
3912 || cur == NULL))
3913 {
3914 shl = &search_hl;
3915 shl_flag = TRUE;
3916 }
3917 else
3918 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003919 if (cur != NULL)
3920 cur->pos.cur = 0;
3921 pos_inprogress = TRUE;
3922 while (shl->rm.regprog != NULL
3923 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003925 if (shl->startcol != MAXCOL
3926 && v >= (long)shl->startcol
3927 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003929#ifdef FEAT_MBYTE
3930 int tmp_col = v + MB_PTR2LEN(ptr);
3931
3932 if (shl->endcol < tmp_col)
3933 shl->endcol = tmp_col;
3934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003936#ifdef FEAT_CONCEAL
3937 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3938 == cur->hlg_id)
3939 {
3940 has_match_conc = TRUE;
3941 match_conc = cur->conceal_char;
3942 }
3943 else
3944 has_match_conc = match_conc = FALSE;
3945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003947 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003950#ifdef FEAT_CONCEAL
3951 prev_syntax_id = 0;
3952#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003953 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3954 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003955 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
3957 /* Need to get the line again, a multi-line regexp
3958 * may have made it invalid. */
3959 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3960 ptr = line + v;
3961
3962 if (shl->lnum == lnum)
3963 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003964 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003966 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003968 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003970 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
3972 /* highlight empty match, try again after
3973 * it */
3974#ifdef FEAT_MBYTE
3975 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003976 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003977 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
3979#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003980 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
3983 /* Loop to check if the match starts at the
3984 * current position */
3985 continue;
3986 }
3987 }
3988 break;
3989 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003990 if (shl != &search_hl && cur != NULL)
3991 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003993
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003994 /* Use attributes from match with highest priority among
3995 * 'search_hl' and the match list. */
3996 search_attr = search_hl.attr_cur;
3997 cur = wp->w_match_head;
3998 shl_flag = FALSE;
3999 while (cur != NULL || shl_flag == FALSE)
4000 {
4001 if (shl_flag == FALSE
4002 && ((cur != NULL
4003 && cur->priority > SEARCH_HL_PRIORITY)
4004 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004005 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004006 shl = &search_hl;
4007 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004008 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004009 else
4010 shl = &cur->hl;
4011 if (shl->attr_cur != 0)
4012 search_attr = shl->attr_cur;
4013 if (shl != &search_hl && cur != NULL)
4014 cur = cur->next;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004020 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004022 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4023 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004025 if (diff_hlf == HLF_TXD && ptr - line > change_end
4026 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004028 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004029 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4030 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
4032#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033
4034 /* Decide which of the highlight attributes to use. */
4035 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004036#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004037 if (area_attr != 0)
4038 char_attr = hl_combine_attr(line_attr, area_attr);
4039 else if (search_attr != 0)
4040 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004041 /* Use line_attr when not in the Visual or 'incsearch' area
4042 * (area_attr may be 0 when "noinvcur" is set). */
4043 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004044 || vcol < fromcol || vcol_prev < fromcol_prev
4045 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004046 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004047#else
4048 if (area_attr != 0)
4049 char_attr = area_attr;
4050 else if (search_attr != 0)
4051 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004052#endif
4053 else
4054 {
4055 attr_pri = FALSE;
4056#ifdef FEAT_SYN_HL
4057 if (has_syntax)
4058 char_attr = syntax_attr;
4059 else
4060#endif
4061 char_attr = 0;
4062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064
4065 /*
4066 * Get the next character to put on the screen.
4067 */
4068 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004069 * The "p_extra" points to the extra stuff that is inserted to
4070 * represent special characters (non-printable stuff) and other
4071 * things. When all characters are the same, c_extra is used.
4072 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4073 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4075 */
4076 if (n_extra > 0)
4077 {
4078 if (c_extra != NUL)
4079 {
4080 c = c_extra;
4081#ifdef FEAT_MBYTE
4082 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4083 if (enc_utf8 && (*mb_char2len)(c) > 1)
4084 {
4085 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004086 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else
4090 mb_utf8 = FALSE;
4091#endif
4092 }
4093 else
4094 {
4095 c = *p_extra;
4096#ifdef FEAT_MBYTE
4097 if (has_mbyte)
4098 {
4099 mb_c = c;
4100 if (enc_utf8)
4101 {
4102 /* If the UTF-8 character is more than one byte:
4103 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004104 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 mb_utf8 = FALSE;
4106 if (mb_l > n_extra)
4107 mb_l = 1;
4108 else if (mb_l > 1)
4109 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004110 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004112 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 }
4115 else
4116 {
4117 /* if this is a DBCS character, put it in "mb_c" */
4118 mb_l = MB_BYTE2LEN(c);
4119 if (mb_l >= n_extra)
4120 mb_l = 1;
4121 else if (mb_l > 1)
4122 mb_c = (c << 8) + p_extra[1];
4123 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004124 if (mb_l == 0) /* at the NUL at end-of-line */
4125 mb_l = 1;
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 /* If a double-width char doesn't fit display a '>' in the
4128 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004129 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130# ifdef FEAT_RIGHTLEFT
4131 wp->w_p_rl ? (col <= 0) :
4132# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004133 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 && (*mb_char2cells)(mb_c) == 2)
4135 {
4136 c = '>';
4137 mb_c = c;
4138 mb_l = 1;
4139 mb_utf8 = FALSE;
4140 multi_attr = hl_attr(HLF_AT);
4141 /* put the pointer back to output the double-width
4142 * character at the start of the next line. */
4143 ++n_extra;
4144 --p_extra;
4145 }
4146 else
4147 {
4148 n_extra -= mb_l - 1;
4149 p_extra += mb_l - 1;
4150 }
4151 }
4152#endif
4153 ++p_extra;
4154 }
4155 --n_extra;
4156 }
4157 else
4158 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004159 if (p_extra_free != NULL)
4160 {
4161 vim_free(p_extra_free);
4162 p_extra_free = NULL;
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 /*
4165 * Get a character from the line itself.
4166 */
4167 c = *ptr;
4168#ifdef FEAT_MBYTE
4169 if (has_mbyte)
4170 {
4171 mb_c = c;
4172 if (enc_utf8)
4173 {
4174 /* If the UTF-8 character is more than one byte: Decode it
4175 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004176 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 mb_utf8 = FALSE;
4178 if (mb_l > 1)
4179 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004180 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /* Overlong encoded ASCII or ASCII with composing char
4182 * is displayed normally, except a NUL. */
4183 if (mb_c < 0x80)
4184 c = mb_c;
4185 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004186
4187 /* At start of the line we can have a composing char.
4188 * Draw it as a space with a composing char. */
4189 if (utf_iscomposing(mb_c))
4190 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004191 int i;
4192
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004193 for (i = Screen_mco - 1; i > 0; --i)
4194 u8cc[i] = u8cc[i - 1];
4195 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004196 mb_c = ' ';
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199
4200 if ((mb_l == 1 && c >= 0x80)
4201 || (mb_l >= 1 && mb_c == 0)
4202 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004203# ifdef UNICODE16
4204 || mb_c >= 0x10000
4205# endif
4206 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
4208 /*
4209 * Illegal UTF-8 byte: display as <xx>.
4210 * Non-BMP character : display as ? or fullwidth ?.
4211 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004212# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 {
4216 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004217# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (wp->w_p_rl) /* reverse */
4219 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004222# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 else if (utf_char2cells(mb_c) != 2)
4224 STRCPY(extra, "?");
4225 else
4226 /* 0xff1f in UTF-8: full-width '?' */
4227 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 p_extra = extra;
4231 c = *p_extra;
4232 mb_c = mb_ptr2char_adv(&p_extra);
4233 mb_utf8 = (c >= 0x80);
4234 n_extra = (int)STRLEN(p_extra);
4235 c_extra = NUL;
4236 if (area_attr == 0 && search_attr == 0)
4237 {
4238 n_attr = n_extra + 1;
4239 extra_attr = hl_attr(HLF_8);
4240 saved_attr2 = char_attr; /* save current attr */
4241 }
4242 }
4243 else if (mb_l == 0) /* at the NUL at end-of-line */
4244 mb_l = 1;
4245#ifdef FEAT_ARABIC
4246 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4247 {
4248 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 int pc, pc1, nc;
4250 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /* The idea of what is the previous and next
4253 * character depends on 'rightleft'. */
4254 if (wp->w_p_rl)
4255 {
4256 pc = prev_c;
4257 pc1 = prev_c1;
4258 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 else
4262 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004263 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004265 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 prev_c = mb_c;
4268
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004269 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 else
4272 prev_c = mb_c;
4273#endif
4274 }
4275 else /* enc_dbcs */
4276 {
4277 mb_l = MB_BYTE2LEN(c);
4278 if (mb_l == 0) /* at the NUL at end-of-line */
4279 mb_l = 1;
4280 else if (mb_l > 1)
4281 {
4282 /* We assume a second byte below 32 is illegal.
4283 * Hopefully this is OK for all double-byte encodings!
4284 */
4285 if (ptr[1] >= 32)
4286 mb_c = (c << 8) + ptr[1];
4287 else
4288 {
4289 if (ptr[1] == NUL)
4290 {
4291 /* head byte at end of line */
4292 mb_l = 1;
4293 transchar_nonprint(extra, c);
4294 }
4295 else
4296 {
4297 /* illegal tail byte */
4298 mb_l = 2;
4299 STRCPY(extra, "XX");
4300 }
4301 p_extra = extra;
4302 n_extra = (int)STRLEN(extra) - 1;
4303 c_extra = NUL;
4304 c = *p_extra++;
4305 if (area_attr == 0 && search_attr == 0)
4306 {
4307 n_attr = n_extra + 1;
4308 extra_attr = hl_attr(HLF_8);
4309 saved_attr2 = char_attr; /* save current attr */
4310 }
4311 mb_c = c;
4312 }
4313 }
4314 }
4315 /* If a double-width char doesn't fit display a '>' in the
4316 * last column; the character is displayed at the start of the
4317 * next line. */
4318 if ((
4319# ifdef FEAT_RIGHTLEFT
4320 wp->w_p_rl ? (col <= 0) :
4321# endif
4322 (col >= W_WIDTH(wp) - 1))
4323 && (*mb_char2cells)(mb_c) == 2)
4324 {
4325 c = '>';
4326 mb_c = c;
4327 mb_utf8 = FALSE;
4328 mb_l = 1;
4329 multi_attr = hl_attr(HLF_AT);
4330 /* Put pointer back so that the character will be
4331 * displayed at the start of the next line. */
4332 --ptr;
4333 }
4334 else if (*ptr != NUL)
4335 ptr += mb_l - 1;
4336
4337 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004338 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004339 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004340 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004343 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 c = ' ';
4345 if (area_attr == 0 && search_attr == 0)
4346 {
4347 n_attr = n_extra + 1;
4348 extra_attr = hl_attr(HLF_AT);
4349 saved_attr2 = char_attr; /* save current attr */
4350 }
4351 mb_c = c;
4352 mb_utf8 = FALSE;
4353 mb_l = 1;
4354 }
4355
4356 }
4357#endif
4358 ++ptr;
4359
4360 if (extra_check)
4361 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004362#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004363 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004364#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004365
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004366#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /* Get syntax attribute, unless still at the start of the line
4368 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004369 v = (long)(ptr - line);
4370 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
4372 /* Get the syntax attribute for the character. If there
4373 * is an error, disable syntax highlighting. */
4374 save_did_emsg = did_emsg;
4375 did_emsg = FALSE;
4376
Bram Moolenaar217ad922005-03-20 22:37:15 +00004377 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004378# ifdef FEAT_SPELL
4379 has_spell ? &can_spell :
4380# endif
4381 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004384 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004385 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004386 has_syntax = FALSE;
4387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 did_emsg = save_did_emsg;
4390
4391 /* Need to get the line again, a multi-line regexp may
4392 * have made it invalid. */
4393 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4394 ptr = line + v;
4395
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004396 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004398 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004399 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004400# ifdef FEAT_CONCEAL
4401 /* no concealing past the end of the line, it interferes
4402 * with line highlighting */
4403 if (c == NUL)
4404 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004405 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004406 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004407# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004409#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004410
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004411#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004412 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004413 * Only do this when there is no syntax highlighting, the
4414 * @Spell cluster is not used or the current syntax item
4415 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004416 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004417 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004418 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004419# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004420 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004421 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004422# endif
4423 if (c != 0 && (
4424# ifdef FEAT_SYN_HL
4425 !has_syntax ||
4426# endif
4427 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004428 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004429 char_u *prev_ptr, *p;
4430 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004431 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004432# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004433 if (has_mbyte)
4434 {
4435 prev_ptr = ptr - mb_l;
4436 v -= mb_l - 1;
4437 }
4438 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004439# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004440 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004441
4442 /* Use nextline[] if possible, it has the start of the
4443 * next line concatenated. */
4444 if ((prev_ptr - line) - nextlinecol >= 0)
4445 p = nextline + (prev_ptr - line) - nextlinecol;
4446 else
4447 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004448 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004449 len = spell_check(wp, p, &spell_hlf, &cap_col,
4450 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004451 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004452
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004453 /* In Insert mode only highlight a word that
4454 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004455 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004456 && (State & INSERT) != 0
4457 && wp->w_cursor.lnum == lnum
4458 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004459 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004460 && wp->w_cursor.col < (colnr_T)word_end)
4461 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004462 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004463 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004464 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004465
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004466 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004467 && (p - nextline) + len > nextline_idx)
4468 {
4469 /* Remember that the good word continues at the
4470 * start of the next line. */
4471 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004472 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004473 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004474
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004475 /* Turn index into actual attributes. */
4476 if (spell_hlf != HLF_COUNT)
4477 spell_attr = highlight_attr[spell_hlf];
4478
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004479 if (cap_col > 0)
4480 {
4481 if (p != prev_ptr
4482 && (p - nextline) + cap_col >= nextline_idx)
4483 {
4484 /* Remember that the word in the next line
4485 * must start with a capital. */
4486 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004487 cap_col = (int)((p - nextline) + cap_col
4488 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004489 }
4490 else
4491 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004492 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004493 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004494 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004495 }
4496 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004497 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004498 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004499 char_attr = hl_combine_attr(char_attr, spell_attr);
4500 else
4501 char_attr = hl_combine_attr(spell_attr, char_attr);
4502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#endif
4504#ifdef FEAT_LINEBREAK
4505 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004506 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004508 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004510# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004511 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004512# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004513 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004515 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004517 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004518
Bram Moolenaar597a4222014-06-25 14:39:50 +02004519 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004520 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004521 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004522 if (c == TAB && n_extra + col > W_WIDTH(wp))
4523 n_extra = (int)wp->w_buffer->b_p_ts
4524 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4525
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004526# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004527 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004528# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004532 {
4533#ifdef FEAT_CONCEAL
4534 if (c == TAB)
4535 /* See "Tab alignment" below. */
4536 FIX_FOR_BOGUSCOLS;
4537#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004538 if (!wp->w_p_list)
4539 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542#endif
4543
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004544 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4545 */
4546 if (wp->w_p_list
4547 && (((c == 160
4548#ifdef FEAT_MBYTE
4549 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4550#endif
4551 ) && lcs_nbsp)
4552 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4553 {
4554 c = (c == ' ') ? lcs_space : lcs_nbsp;
4555 if (area_attr == 0 && search_attr == 0)
4556 {
4557 n_attr = 1;
4558 extra_attr = hl_attr(HLF_8);
4559 saved_attr2 = char_attr; /* save current attr */
4560 }
4561#ifdef FEAT_MBYTE
4562 mb_c = c;
4563 if (enc_utf8 && (*mb_char2len)(c) > 1)
4564 {
4565 mb_utf8 = TRUE;
4566 u8cc[0] = 0;
4567 c = 0xc0;
4568 }
4569 else
4570 mb_utf8 = FALSE;
4571#endif
4572 }
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4575 {
4576 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004577 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
4579 n_attr = 1;
4580 extra_attr = hl_attr(HLF_8);
4581 saved_attr2 = char_attr; /* save current attr */
4582 }
4583#ifdef FEAT_MBYTE
4584 mb_c = c;
4585 if (enc_utf8 && (*mb_char2len)(c) > 1)
4586 {
4587 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004588 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004589 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 else
4592 mb_utf8 = FALSE;
4593#endif
4594 }
4595 }
4596
4597 /*
4598 * Handling of non-printable characters.
4599 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004600 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
4602 /*
4603 * when getting a character from the file, we may have to
4604 * turn it into something else on the way to putting it
4605 * into "ScreenLines".
4606 */
4607 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4608 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004609 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004610 long vcol_adjusted = vcol; /* removed showbreak length */
4611#ifdef FEAT_LINEBREAK
4612 /* only adjust the tab_len, when at the first column
4613 * after the showbreak value was drawn */
4614 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4615 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004618 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004619 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4620
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004621#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004622 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004623#endif
4624 /* tab amount depends on current column */
4625 n_extra = tab_len;
4626#ifdef FEAT_LINEBREAK
4627 else
4628 {
4629 char_u *p;
4630 int len = n_extra;
4631 int i;
4632 int saved_nextra = n_extra;
4633
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004634#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004635 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004636 /* there are characters to conceal */
4637 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004638 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4639 */
4640 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4641 && n_extra > tab_len)
4642 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004643#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004644
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004645 /* if n_extra > 0, it gives the number of chars, to
4646 * use for a tab, else we need to calculate the width
4647 * for a tab */
4648#ifdef FEAT_MBYTE
4649 len = (tab_len * mb_char2len(lcs_tab2));
4650 if (n_extra > 0)
4651 len += n_extra - tab_len;
4652#endif
4653 c = lcs_tab1;
4654 p = alloc((unsigned)(len + 1));
4655 vim_memset(p, ' ', len);
4656 p[len] = NUL;
4657 p_extra_free = p;
4658 for (i = 0; i < tab_len; i++)
4659 {
4660#ifdef FEAT_MBYTE
4661 mb_char2bytes(lcs_tab2, p);
4662 p += mb_char2len(lcs_tab2);
4663 n_extra += mb_char2len(lcs_tab2)
4664 - (saved_nextra > 0 ? 1 : 0);
4665#else
4666 p[i] = lcs_tab2;
4667#endif
4668 }
4669 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004670#ifdef FEAT_CONCEAL
4671 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4672 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004673 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004674 n_extra -= vcol_off;
4675#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004676 }
4677#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004678#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004679 {
4680 int vc_saved = vcol_off;
4681
4682 /* Tab alignment should be identical regardless of
4683 * 'conceallevel' value. So tab compensates of all
4684 * previous concealed characters, and thus resets
4685 * vcol_off and boguscols accumulated so far in the
4686 * line. Note that the tab can be longer than
4687 * 'tabstop' when there are concealed characters. */
4688 FIX_FOR_BOGUSCOLS;
4689
4690 /* Make sure, the highlighting for the tab char will be
4691 * correctly set further below (effectively reverts the
4692 * FIX_FOR_BOGSUCOLS macro */
4693 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004694 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004695 tab_len += vc_saved;
4696 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#ifdef FEAT_MBYTE
4699 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4700#endif
4701 if (wp->w_p_list)
4702 {
4703 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004704#ifdef FEAT_LINEBREAK
4705 if (wp->w_p_lbr)
4706 c_extra = NUL; /* using p_extra from above */
4707 else
4708#endif
4709 c_extra = lcs_tab2;
4710 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 extra_attr = hl_attr(HLF_8);
4712 saved_attr2 = char_attr; /* save current attr */
4713#ifdef FEAT_MBYTE
4714 mb_c = c;
4715 if (enc_utf8 && (*mb_char2len)(c) > 1)
4716 {
4717 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004718 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004719 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721#endif
4722 }
4723 else
4724 {
4725 c_extra = ' ';
4726 c = ' ';
4727 }
4728 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004729 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004730 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004731 || ((fromcol >= 0 || fromcol_prev >= 0)
4732 && tocol > vcol
4733 && VIsual_mode != Ctrl_V
4734 && (
4735# ifdef FEAT_RIGHTLEFT
4736 wp->w_p_rl ? (col >= 0) :
4737# endif
4738 (col < W_WIDTH(wp)))
4739 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004740 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004741 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004742 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004744 /* Display a '$' after the line or highlight an extra
4745 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4747 /* For a diff line the highlighting continues after the
4748 * "$". */
4749 if (
4750# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004751 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752# ifdef LINE_ATTR
4753 &&
4754# endif
4755# endif
4756# ifdef LINE_ATTR
4757 line_attr == 0
4758# endif
4759 )
4760#endif
4761 {
4762#ifdef FEAT_VIRTUALEDIT
4763 /* In virtualedit, visual selections may extend
4764 * beyond end of line. */
4765 if (area_highlighting && virtual_active()
4766 && tocol != MAXCOL && vcol < tocol)
4767 n_extra = 0;
4768 else
4769#endif
4770 {
4771 p_extra = at_end_str;
4772 n_extra = 1;
4773 c_extra = NUL;
4774 }
4775 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004776 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004777 c = lcs_eol;
4778 else
4779 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 lcs_eol_one = -1;
4781 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004782 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 extra_attr = hl_attr(HLF_AT);
4785 n_attr = 1;
4786 }
4787#ifdef FEAT_MBYTE
4788 mb_c = c;
4789 if (enc_utf8 && (*mb_char2len)(c) > 1)
4790 {
4791 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004792 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004793 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else
4796 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4797#endif
4798 }
4799 else if (c != NUL)
4800 {
4801 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004802 if (n_extra == 0)
4803 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#ifdef FEAT_RIGHTLEFT
4805 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4806 rl_mirror(p_extra); /* reverse "<12>" */
4807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004809#ifdef FEAT_LINEBREAK
4810 if (wp->w_p_lbr)
4811 {
4812 char_u *p;
4813
4814 c = *p_extra;
4815 p = alloc((unsigned)n_extra + 1);
4816 vim_memset(p, ' ', n_extra);
4817 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4818 p[n_extra] = NUL;
4819 p_extra_free = p_extra = p;
4820 }
4821 else
4822#endif
4823 {
4824 n_extra = byte2cells(c) - 1;
4825 c = *p_extra++;
4826 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004827 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 n_attr = n_extra + 1;
4830 extra_attr = hl_attr(HLF_8);
4831 saved_attr2 = char_attr; /* save current attr */
4832 }
4833#ifdef FEAT_MBYTE
4834 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4835#endif
4836 }
4837#ifdef FEAT_VIRTUALEDIT
4838 else if (VIsual_active
4839 && (VIsual_mode == Ctrl_V
4840 || VIsual_mode == 'v')
4841 && virtual_active()
4842 && tocol != MAXCOL
4843 && vcol < tocol
4844 && (
4845# ifdef FEAT_RIGHTLEFT
4846 wp->w_p_rl ? (col >= 0) :
4847# endif
4848 (col < W_WIDTH(wp))))
4849 {
4850 c = ' ';
4851 --ptr; /* put it back at the NUL */
4852 }
4853#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004854#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else if ((
4856# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004857 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 ) && (
4861# ifdef FEAT_RIGHTLEFT
4862 wp->w_p_rl ? (col >= 0) :
4863# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004864 (col
4865# ifdef FEAT_CONCEAL
4866 - boguscols
4867# endif
4868 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 /* Highlight until the right side of the window */
4871 c = ' ';
4872 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004873
4874 /* Remember we do the char for line highlighting. */
4875 ++did_line_attr;
4876
4877 /* don't do search HL for the rest of the line */
4878 if (line_attr != 0 && char_attr == search_attr && col > 0)
4879 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880# ifdef FEAT_DIFF
4881 if (diff_hlf == HLF_TXD)
4882 {
4883 diff_hlf = HLF_CHD;
4884 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004887 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4888 char_attr = hl_combine_attr(char_attr,
4889 hl_attr(HLF_CUL));
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892# endif
4893 }
4894#endif
4895 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004896
4897#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004898 if ( wp->w_p_cole > 0
4899 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004900 conceal_cursor_line(wp) )
4901 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004902 && !(lnum_in_visual_area
4903 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004904 {
4905 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004906 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004907 && (syn_get_sub_char() != NUL || match_conc
4908 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004909 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004910 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004911 /* First time at this concealed item: display one
4912 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004913 if (match_conc)
4914 c = match_conc;
4915 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004916 c = syn_get_sub_char();
4917 else if (lcs_conceal != NUL)
4918 c = lcs_conceal;
4919 else
4920 c = ' ';
4921
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004922 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004923
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004924 if (n_extra > 0)
4925 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004926 vcol += n_extra;
4927 if (wp->w_p_wrap && n_extra > 0)
4928 {
4929# ifdef FEAT_RIGHTLEFT
4930 if (wp->w_p_rl)
4931 {
4932 col -= n_extra;
4933 boguscols -= n_extra;
4934 }
4935 else
4936# endif
4937 {
4938 boguscols += n_extra;
4939 col += n_extra;
4940 }
4941 }
4942 n_extra = 0;
4943 n_attr = 0;
4944 }
4945 else if (n_skip == 0)
4946 {
4947 is_concealing = TRUE;
4948 n_skip = 1;
4949 }
4950# ifdef FEAT_MBYTE
4951 mb_c = c;
4952 if (enc_utf8 && (*mb_char2len)(c) > 1)
4953 {
4954 mb_utf8 = TRUE;
4955 u8cc[0] = 0;
4956 c = 0xc0;
4957 }
4958 else
4959 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4960# endif
4961 }
4962 else
4963 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004964 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004965 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004966 }
4967#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004970#ifdef FEAT_CONCEAL
4971 /* In the cursor line and we may be concealing characters: correct
4972 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004973 if (!did_wcol && draw_state == WL_LINE
4974 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004975 && conceal_cursor_line(wp)
4976 && (int)wp->w_virtcol <= vcol + n_skip)
4977 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004978# ifdef FEAT_RIGHTLEFT
4979 if (wp->w_p_rl)
4980 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4981 else
4982# endif
4983 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004984 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004985 did_wcol = TRUE;
4986 }
4987#endif
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /* Don't override visual selection highlighting. */
4990 if (n_attr > 0
4991 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004992 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 char_attr = extra_attr;
4994
Bram Moolenaar81695252004-12-29 20:58:21 +00004995#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /* XIM don't send preedit_start and preedit_end, but they send
4997 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4998 * im_is_preediting() here. */
4999 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005000 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 && (State & INSERT)
5002 && !p_imdisable
5003 && im_is_preediting()
5004 && draw_state == WL_LINE)
5005 {
5006 colnr_T tcol;
5007
5008 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005009 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 else
5011 tcol = preedit_end_col;
5012 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5013 {
5014 if (feedback_old_attr < 0)
5015 {
5016 feedback_col = 0;
5017 feedback_old_attr = char_attr;
5018 }
5019 char_attr = im_get_feedback_attr(feedback_col);
5020 if (char_attr < 0)
5021 char_attr = feedback_old_attr;
5022 feedback_col++;
5023 }
5024 else if (feedback_old_attr >= 0)
5025 {
5026 char_attr = feedback_old_attr;
5027 feedback_old_attr = -1;
5028 feedback_col = 0;
5029 }
5030 }
5031#endif
5032 /*
5033 * Handle the case where we are in column 0 but not on the first
5034 * character of the line and the user wants us to show us a
5035 * special character (via 'listchars' option "precedes:<char>".
5036 */
5037 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005038 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5040#ifdef FEAT_DIFF
5041 && filler_todo <= 0
5042#endif
5043 && draw_state > WL_NR
5044 && c != NUL)
5045 {
5046 c = lcs_prec;
5047 lcs_prec_todo = NUL;
5048#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005049 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5050 {
5051 /* Double-width character being overwritten by the "precedes"
5052 * character, need to fill up half the character. */
5053 c_extra = MB_FILLER_CHAR;
5054 n_extra = 1;
5055 n_attr = 2;
5056 extra_attr = hl_attr(HLF_AT);
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 mb_c = c;
5059 if (enc_utf8 && (*mb_char2len)(c) > 1)
5060 {
5061 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005062 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005063 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 else
5066 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5067#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005068 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 saved_attr3 = char_attr; /* save current attr */
5071 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5072 n_attr3 = 1;
5073 }
5074 }
5075
5076 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005077 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005079 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005080#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005081 || did_line_attr == 1
5082#endif
5083 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005085#ifdef FEAT_SEARCH_EXTRA
5086 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005087
5088 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005089 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005090 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005091#endif
5092
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005093 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 * highlight match at end of line. If it's beyond the last
5095 * char on the screen, just overwrite that one (tricky!) Not
5096 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005097#ifdef FEAT_SEARCH_EXTRA
5098 prevcol_hl_flag = FALSE;
5099 if (prevcol == (long)search_hl.startcol)
5100 prevcol_hl_flag = TRUE;
5101 else
5102 {
5103 cur = wp->w_match_head;
5104 while (cur != NULL)
5105 {
5106 if (prevcol == (long)cur->hl.startcol)
5107 {
5108 prevcol_hl_flag = TRUE;
5109 break;
5110 }
5111 cur = cur->next;
5112 }
5113 }
5114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005116 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005117 && (VIsual_mode != Ctrl_V
5118 || lnum == VIsual.lnum
5119 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005120 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#ifdef FEAT_SEARCH_EXTRA
5122 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005123 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005124# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005125 && did_line_attr <= 1
5126# endif
5127 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128#endif
5129 ))
5130 {
5131 int n = 0;
5132
5133#ifdef FEAT_RIGHTLEFT
5134 if (wp->w_p_rl)
5135 {
5136 if (col < 0)
5137 n = 1;
5138 }
5139 else
5140#endif
5141 {
5142 if (col >= W_WIDTH(wp))
5143 n = -1;
5144 }
5145 if (n != 0)
5146 {
5147 /* At the window boundary, highlight the last character
5148 * instead (better than nothing). */
5149 off += n;
5150 col += n;
5151 }
5152 else
5153 {
5154 /* Add a blank character to highlight. */
5155 ScreenLines[off] = ' ';
5156#ifdef FEAT_MBYTE
5157 if (enc_utf8)
5158 ScreenLinesUC[off] = 0;
5159#endif
5160 }
5161#ifdef FEAT_SEARCH_EXTRA
5162 if (area_attr == 0)
5163 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005164 /* Use attributes from match with highest priority among
5165 * 'search_hl' and the match list. */
5166 char_attr = search_hl.attr;
5167 cur = wp->w_match_head;
5168 shl_flag = FALSE;
5169 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005170 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005171 if (shl_flag == FALSE
5172 && ((cur != NULL
5173 && cur->priority > SEARCH_HL_PRIORITY)
5174 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005175 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005176 shl = &search_hl;
5177 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005178 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005179 else
5180 shl = &cur->hl;
5181 if ((ptr - line) - 1 == (long)shl->startcol)
5182 char_attr = shl->attr;
5183 if (shl != &search_hl && cur != NULL)
5184 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187#endif
5188 ScreenAttrs[off] = char_attr;
5189#ifdef FEAT_RIGHTLEFT
5190 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005193 --off;
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else
5196#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005197 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005199 ++off;
5200 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005201 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005202#ifdef FEAT_SYN_HL
5203 eol_hl_off = 1;
5204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaar91170f82006-05-05 21:15:17 +00005208 /*
5209 * At end of the text line.
5210 */
5211 if (c == NUL)
5212 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005213#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005214 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5215 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005216 {
5217 /* highlight last char after line */
5218 --col;
5219 --off;
5220 --vcol;
5221 }
5222
Bram Moolenaar1a384422010-07-14 19:53:30 +02005223 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005224 if (wp->w_p_wrap)
5225 v = wp->w_skipcol;
5226 else
5227 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005228
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005229 /* check if line ends before left margin */
5230 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005231 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005232#ifdef FEAT_CONCEAL
5233 /* Get rid of the boguscols now, we want to draw until the right
5234 * edge for 'cursorcolumn'. */
5235 col -= boguscols;
5236 boguscols = 0;
5237#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005238
5239 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005240 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005241
5242 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005243 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5244 && (int)wp->w_virtcol <
5245 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005246 && lnum != wp->w_cursor.lnum)
5247 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005248# ifdef FEAT_RIGHTLEFT
5249 && !wp->w_p_rl
5250# endif
5251 )
5252 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005253 int rightmost_vcol = 0;
5254 int i;
5255
5256 if (wp->w_p_cuc)
5257 rightmost_vcol = wp->w_virtcol;
5258 if (draw_color_col)
5259 /* determine rightmost colorcolumn to possibly draw */
5260 for (i = 0; color_cols[i] >= 0; ++i)
5261 if (rightmost_vcol < color_cols[i])
5262 rightmost_vcol = color_cols[i];
5263
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005264 while (col < W_WIDTH(wp))
5265 {
5266 ScreenLines[off] = ' ';
5267#ifdef FEAT_MBYTE
5268 if (enc_utf8)
5269 ScreenLinesUC[off] = 0;
5270#endif
5271 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005272 if (draw_color_col)
5273 draw_color_col = advance_color_col(VCOL_HLC,
5274 &color_cols);
5275
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005276 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005277 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005278 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005279 ScreenAttrs[off++] = hl_attr(HLF_MC);
5280 else
5281 ScreenAttrs[off++] = 0;
5282
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005283 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005284 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005285
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005286 ++vcol;
5287 }
5288 }
5289#endif
5290
Bram Moolenaar860cae12010-06-05 23:22:07 +02005291 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5292 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 row++;
5294
5295 /*
5296 * Update w_cline_height and w_cline_folded if the cursor line was
5297 * updated (saves a call to plines() later).
5298 */
5299 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5300 {
5301 curwin->w_cline_row = startrow;
5302 curwin->w_cline_height = row - startrow;
5303#ifdef FEAT_FOLDING
5304 curwin->w_cline_folded = FALSE;
5305#endif
5306 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5307 }
5308
5309 break;
5310 }
5311
5312 /* line continues beyond line end */
5313 if (lcs_ext
5314 && !wp->w_p_wrap
5315#ifdef FEAT_DIFF
5316 && filler_todo <= 0
5317#endif
5318 && (
5319#ifdef FEAT_RIGHTLEFT
5320 wp->w_p_rl ? col == 0 :
5321#endif
5322 col == W_WIDTH(wp) - 1)
5323 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005324 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5326 {
5327 c = lcs_ext;
5328 char_attr = hl_attr(HLF_AT);
5329#ifdef FEAT_MBYTE
5330 mb_c = c;
5331 if (enc_utf8 && (*mb_char2len)(c) > 1)
5332 {
5333 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005334 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005335 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 else
5338 mb_utf8 = FALSE;
5339#endif
5340 }
5341
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005342#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005343 /* advance to the next 'colorcolumn' */
5344 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005345 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005346
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005347 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005348 * highlight the cursor position itself.
5349 * Also highlight the 'colorcolumn' if it is different than
5350 * 'cursorcolumn' */
5351 vcol_save_attr = -1;
5352 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005353 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005354 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005355 && lnum != wp->w_cursor.lnum)
5356 {
5357 vcol_save_attr = char_attr;
5358 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5359 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005360 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005361 {
5362 vcol_save_attr = char_attr;
5363 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5364 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005365 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005366#endif
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 /*
5369 * Store character to be displayed.
5370 * Skip characters that are left of the screen for 'nowrap'.
5371 */
5372 vcol_prev = vcol;
5373 if (draw_state < WL_LINE || n_skip <= 0)
5374 {
5375 /*
5376 * Store the character.
5377 */
5378#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5379 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5380 {
5381 /* A double-wide character is: put first halve in left cell. */
5382 --off;
5383 --col;
5384 }
5385#endif
5386 ScreenLines[off] = c;
5387#ifdef FEAT_MBYTE
5388 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005389 {
5390 if ((mb_c & 0xff00) == 0x8e00)
5391 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 else if (enc_utf8)
5395 {
5396 if (mb_utf8)
5397 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005398 int i;
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005401 if ((c & 0xff) == 0)
5402 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005403 for (i = 0; i < Screen_mco; ++i)
5404 {
5405 ScreenLinesC[i][off] = u8cc[i];
5406 if (u8cc[i] == 0)
5407 break;
5408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 else
5411 ScreenLinesUC[off] = 0;
5412 }
5413 if (multi_attr)
5414 {
5415 ScreenAttrs[off] = multi_attr;
5416 multi_attr = 0;
5417 }
5418 else
5419#endif
5420 ScreenAttrs[off] = char_attr;
5421
5422#ifdef FEAT_MBYTE
5423 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5424 {
5425 /* Need to fill two screen columns. */
5426 ++off;
5427 ++col;
5428 if (enc_utf8)
5429 /* UTF-8: Put a 0 in the second screen char. */
5430 ScreenLines[off] = 0;
5431 else
5432 /* DBCS: Put second byte in the second screen char. */
5433 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005434 if (draw_state > WL_NR
5435#ifdef FEAT_DIFF
5436 && filler_todo <= 0
5437#endif
5438 )
5439 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 /* When "tocol" is halfway a character, set it to the end of
5441 * the character, otherwise highlighting won't stop. */
5442 if (tocol == vcol)
5443 ++tocol;
5444#ifdef FEAT_RIGHTLEFT
5445 if (wp->w_p_rl)
5446 {
5447 /* now it's time to backup one cell */
5448 --off;
5449 --col;
5450 }
5451#endif
5452 }
5453#endif
5454#ifdef FEAT_RIGHTLEFT
5455 if (wp->w_p_rl)
5456 {
5457 --off;
5458 --col;
5459 }
5460 else
5461#endif
5462 {
5463 ++off;
5464 ++col;
5465 }
5466 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005467#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005468 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005469 {
5470 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005471 ++vcol_off;
5472 if (n_extra > 0)
5473 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474 if (wp->w_p_wrap)
5475 {
5476 /*
5477 * Special voodoo required if 'wrap' is on.
5478 *
5479 * Advance the column indicator to force the line
5480 * drawing to wrap early. This will make the line
5481 * take up the same screen space when parts are concealed,
5482 * so that cursor line computations aren't messed up.
5483 *
5484 * To avoid the fictitious advance of 'col' causing
5485 * trailing junk to be written out of the screen line
5486 * we are building, 'boguscols' keeps track of the number
5487 * of bad columns we have advanced.
5488 */
5489 if (n_extra > 0)
5490 {
5491 vcol += n_extra;
5492# ifdef FEAT_RIGHTLEFT
5493 if (wp->w_p_rl)
5494 {
5495 col -= n_extra;
5496 boguscols -= n_extra;
5497 }
5498 else
5499# endif
5500 {
5501 col += n_extra;
5502 boguscols += n_extra;
5503 }
5504 n_extra = 0;
5505 n_attr = 0;
5506 }
5507
5508
5509# ifdef FEAT_MBYTE
5510 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5511 {
5512 /* Need to fill two screen columns. */
5513# ifdef FEAT_RIGHTLEFT
5514 if (wp->w_p_rl)
5515 {
5516 --boguscols;
5517 --col;
5518 }
5519 else
5520# endif
5521 {
5522 ++boguscols;
5523 ++col;
5524 }
5525 }
5526# endif
5527
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 else
5542 {
5543 if (n_extra > 0)
5544 {
5545 vcol += n_extra;
5546 n_extra = 0;
5547 n_attr = 0;
5548 }
5549 }
5550
5551 }
5552#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else
5554 --n_skip;
5555
Bram Moolenaar64486672010-05-16 15:46:46 +02005556 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5557 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005558 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559#ifdef FEAT_DIFF
5560 && filler_todo <= 0
5561#endif
5562 )
5563 ++vcol;
5564
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005565#ifdef FEAT_SYN_HL
5566 if (vcol_save_attr >= 0)
5567 char_attr = vcol_save_attr;
5568#endif
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 /* restore attributes after "predeces" in 'listchars' */
5571 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5572 char_attr = saved_attr3;
5573
5574 /* restore attributes after last 'listchars' or 'number' char */
5575 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5576 char_attr = saved_attr2;
5577
5578 /*
5579 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005580 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
5582 if ((
5583#ifdef FEAT_RIGHTLEFT
5584 wp->w_p_rl ? (col < 0) :
5585#endif
5586 (col >= W_WIDTH(wp)))
5587 && (*ptr != NUL
5588#ifdef FEAT_DIFF
5589 || filler_todo > 0
5590#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005591 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5593 )
5594 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005595#ifdef FEAT_CONCEAL
5596 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5597 (int)W_WIDTH(wp), wp->w_p_rl);
5598 boguscols = 0;
5599#else
5600 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5601 (int)W_WIDTH(wp), wp->w_p_rl);
5602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 ++row;
5604 ++screen_row;
5605
5606 /* When not wrapping and finished diff lines, or when displayed
5607 * '$' and highlighting until last column, break here. */
5608 if ((!wp->w_p_wrap
5609#ifdef FEAT_DIFF
5610 && filler_todo <= 0
5611#endif
5612 ) || lcs_eol_one == -1)
5613 break;
5614
5615 /* When the window is too narrow draw all "@" lines. */
5616 if (draw_state != WL_LINE
5617#ifdef FEAT_DIFF
5618 && filler_todo <= 0
5619#endif
5620 )
5621 {
5622 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5623#ifdef FEAT_VERTSPLIT
5624 draw_vsep_win(wp, row);
5625#endif
5626 row = endrow;
5627 }
5628
5629 /* When line got too long for screen break here. */
5630 if (row == endrow)
5631 {
5632 ++row;
5633 break;
5634 }
5635
5636 if (screen_cur_row == screen_row - 1
5637#ifdef FEAT_DIFF
5638 && filler_todo <= 0
5639#endif
5640 && W_WIDTH(wp) == Columns)
5641 {
5642 /* Remember that the line wraps, used for modeless copy. */
5643 LineWraps[screen_row - 1] = TRUE;
5644
5645 /*
5646 * Special trick to make copy/paste of wrapped lines work with
5647 * xterm/screen: write an extra character beyond the end of
5648 * the line. This will work with all terminal types
5649 * (regardless of the xn,am settings).
5650 * Only do this on a fast tty.
5651 * Only do this if the cursor is on the current line
5652 * (something has been written in it).
5653 * Don't do this for the GUI.
5654 * Don't do this for double-width characters.
5655 * Don't do this for a window not at the right screen border.
5656 */
5657 if (p_tf
5658#ifdef FEAT_GUI
5659 && !gui.in_use
5660#endif
5661#ifdef FEAT_MBYTE
5662 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005663 && ((*mb_off2cells)(LineOffset[screen_row],
5664 LineOffset[screen_row] + screen_Columns)
5665 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005667 + (int)Columns - 2,
5668 LineOffset[screen_row] + screen_Columns)
5669 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670#endif
5671 )
5672 {
5673 /* First make sure we are at the end of the screen line,
5674 * then output the same character again to let the
5675 * terminal know about the wrap. If the terminal doesn't
5676 * auto-wrap, we overwrite the character. */
5677 if (screen_cur_col != W_WIDTH(wp))
5678 screen_char(LineOffset[screen_row - 1]
5679 + (unsigned)Columns - 1,
5680 screen_row - 1, (int)(Columns - 1));
5681
5682#ifdef FEAT_MBYTE
5683 /* When there is a multi-byte character, just output a
5684 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005685 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5686 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 out_char(' ');
5688 else
5689#endif
5690 out_char(ScreenLines[LineOffset[screen_row - 1]
5691 + (Columns - 1)]);
5692 /* force a redraw of the first char on the next line */
5693 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5694 screen_start(); /* don't know where cursor is now */
5695 }
5696 }
5697
5698 col = 0;
5699 off = (unsigned)(current_ScreenLine - ScreenLines);
5700#ifdef FEAT_RIGHTLEFT
5701 if (wp->w_p_rl)
5702 {
5703 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5704 off += col;
5705 }
5706#endif
5707
5708 /* reset the drawing state for the start of a wrapped line */
5709 draw_state = WL_START;
5710 saved_n_extra = n_extra;
5711 saved_p_extra = p_extra;
5712 saved_c_extra = c_extra;
5713 saved_char_attr = char_attr;
5714 n_extra = 0;
5715 lcs_prec_todo = lcs_prec;
5716#ifdef FEAT_LINEBREAK
5717# ifdef FEAT_DIFF
5718 if (filler_todo <= 0)
5719# endif
5720 need_showbreak = TRUE;
5721#endif
5722#ifdef FEAT_DIFF
5723 --filler_todo;
5724 /* When the filler lines are actually below the last line of the
5725 * file, don't draw the line itself, break here. */
5726 if (filler_todo == 0 && wp->w_botfill)
5727 break;
5728#endif
5729 }
5730
5731 } /* for every character in the line */
5732
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005733#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005734 /* After an empty line check first word for capital. */
5735 if (*skipwhite(line) == NUL)
5736 {
5737 capcol_lnum = lnum + 1;
5738 cap_col = 0;
5739 }
5740#endif
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 return row;
5743}
5744
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005745#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005746static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005747
5748/*
5749 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005750 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005751 */
5752 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005753comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005754{
5755 int i;
5756
5757 for (i = 0; i < Screen_mco; ++i)
5758 {
5759 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5760 return TRUE;
5761 if (ScreenLinesC[i][off_from] == 0)
5762 break;
5763 }
5764 return FALSE;
5765}
5766#endif
5767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768/*
5769 * Check whether the given character needs redrawing:
5770 * - the (first byte of the) character is different
5771 * - the attributes are different
5772 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005773 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 */
5775 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005776char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 if (cols > 0
5779 && ((ScreenLines[off_from] != ScreenLines[off_to]
5780 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5781
5782#ifdef FEAT_MBYTE
5783 || (enc_dbcs != 0
5784 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5785 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5786 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5787 : (cols > 1 && ScreenLines[off_from + 1]
5788 != ScreenLines[off_to + 1])))
5789 || (enc_utf8
5790 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5791 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005792 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005793 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5794 && ScreenLines[off_from + 1]
5795 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#endif
5797 ))
5798 return TRUE;
5799 return FALSE;
5800}
5801
5802/*
5803 * Move one "cooked" screen line to the screen, but only the characters that
5804 * have actually changed. Handle insert/delete character.
5805 * "coloff" gives the first column on the screen for this line.
5806 * "endcol" gives the columns where valid characters are.
5807 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5808 * needs to be cleared, negative otherwise.
5809 * "rlflag" is TRUE in a rightleft window:
5810 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5811 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5812 */
5813 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005814screen_line(
5815 int row,
5816 int coloff,
5817 int endcol,
5818 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005820 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005822 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 unsigned off_from;
5825 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005826#ifdef FEAT_MBYTE
5827 unsigned max_off_from;
5828 unsigned max_off_to;
5829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 int col = 0;
5831#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5832 int hl;
5833#endif
5834 int force = FALSE; /* force update rest of the line */
5835 int redraw_this /* bool: does character need redraw? */
5836#ifdef FEAT_GUI
5837 = TRUE /* For GUI when while-loop empty */
5838#endif
5839 ;
5840 int redraw_next; /* redraw_this for next character */
5841#ifdef FEAT_MBYTE
5842 int clear_next = FALSE;
5843 int char_cells; /* 1: normal char */
5844 /* 2: occupies two display cells */
5845# define CHAR_CELLS char_cells
5846#else
5847# define CHAR_CELLS 1
5848#endif
5849
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005850 /* Check for illegal row and col, just in case. */
5851 if (row >= Rows)
5852 row = Rows - 1;
5853 if (endcol > Columns)
5854 endcol = Columns;
5855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856# ifdef FEAT_CLIPBOARD
5857 clip_may_clear_selection(row, row);
5858# endif
5859
5860 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5861 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005862#ifdef FEAT_MBYTE
5863 max_off_from = off_from + screen_Columns;
5864 max_off_to = LineOffset[row] + screen_Columns;
5865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867#ifdef FEAT_RIGHTLEFT
5868 if (rlflag)
5869 {
5870 /* Clear rest first, because it's left of the text. */
5871 if (clear_width > 0)
5872 {
5873 while (col <= endcol && ScreenLines[off_to] == ' '
5874 && ScreenAttrs[off_to] == 0
5875# ifdef FEAT_MBYTE
5876 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5877# endif
5878 )
5879 {
5880 ++off_to;
5881 ++col;
5882 }
5883 if (col <= endcol)
5884 screen_fill(row, row + 1, col + coloff,
5885 endcol + coloff + 1, ' ', ' ', 0);
5886 }
5887 col = endcol + 1;
5888 off_to = LineOffset[row] + col + coloff;
5889 off_from += col;
5890 endcol = (clear_width > 0 ? clear_width : -clear_width);
5891 }
5892#endif /* FEAT_RIGHTLEFT */
5893
5894 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5895
5896 while (col < endcol)
5897 {
5898#ifdef FEAT_MBYTE
5899 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005900 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 else
5902 char_cells = 1;
5903#endif
5904
5905 redraw_this = redraw_next;
5906 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5907 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5908
5909#ifdef FEAT_GUI
5910 /* If the next character was bold, then redraw the current character to
5911 * remove any pixels that might have spilt over into us. This only
5912 * happens in the GUI.
5913 */
5914 if (redraw_next && gui.in_use)
5915 {
5916 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005917 if (hl > HL_ALL)
5918 hl = syn_attr2attr(hl);
5919 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 redraw_this = TRUE;
5921 }
5922#endif
5923
5924 if (redraw_this)
5925 {
5926 /*
5927 * Special handling when 'xs' termcap flag set (hpterm):
5928 * Attributes for characters are stored at the position where the
5929 * cursor is when writing the highlighting code. The
5930 * start-highlighting code must be written with the cursor on the
5931 * first highlighted character. The stop-highlighting code must
5932 * be written with the cursor just after the last highlighted
5933 * character.
5934 * Overwriting a character doesn't remove it's highlighting. Need
5935 * to clear the rest of the line, and force redrawing it
5936 * completely.
5937 */
5938 if ( p_wiv
5939 && !force
5940#ifdef FEAT_GUI
5941 && !gui.in_use
5942#endif
5943 && ScreenAttrs[off_to] != 0
5944 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5945 {
5946 /*
5947 * Need to remove highlighting attributes here.
5948 */
5949 windgoto(row, col + coloff);
5950 out_str(T_CE); /* clear rest of this screen line */
5951 screen_start(); /* don't know where cursor is now */
5952 force = TRUE; /* force redraw of rest of the line */
5953 redraw_next = TRUE; /* or else next char would miss out */
5954
5955 /*
5956 * If the previous character was highlighted, need to stop
5957 * highlighting at this character.
5958 */
5959 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5960 {
5961 screen_attr = ScreenAttrs[off_to - 1];
5962 term_windgoto(row, col + coloff);
5963 screen_stop_highlight();
5964 }
5965 else
5966 screen_attr = 0; /* highlighting has stopped */
5967 }
5968#ifdef FEAT_MBYTE
5969 if (enc_dbcs != 0)
5970 {
5971 /* Check if overwriting a double-byte with a single-byte or
5972 * the other way around requires another character to be
5973 * redrawn. For UTF-8 this isn't needed, because comparing
5974 * ScreenLinesUC[] is sufficient. */
5975 if (char_cells == 1
5976 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005977 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 {
5979 /* Writing a single-cell character over a double-cell
5980 * character: need to redraw the next cell. */
5981 ScreenLines[off_to + 1] = 0;
5982 redraw_next = TRUE;
5983 }
5984 else if (char_cells == 2
5985 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005986 && (*mb_off2cells)(off_to, max_off_to) == 1
5987 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 {
5989 /* Writing the second half of a double-cell character over
5990 * a double-cell character: need to redraw the second
5991 * cell. */
5992 ScreenLines[off_to + 2] = 0;
5993 redraw_next = TRUE;
5994 }
5995
5996 if (enc_dbcs == DBCS_JPNU)
5997 ScreenLines2[off_to] = ScreenLines2[off_from];
5998 }
5999 /* When writing a single-width character over a double-width
6000 * character and at the end of the redrawn text, need to clear out
6001 * the right halve of the old character.
6002 * Also required when writing the right halve of a double-width
6003 * char over the left halve of an existing one. */
6004 if (has_mbyte && col + char_cells == endcol
6005 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006006 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006008 && (*mb_off2cells)(off_to, max_off_to) == 1
6009 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 clear_next = TRUE;
6011#endif
6012
6013 ScreenLines[off_to] = ScreenLines[off_from];
6014#ifdef FEAT_MBYTE
6015 if (enc_utf8)
6016 {
6017 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6018 if (ScreenLinesUC[off_from] != 0)
6019 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006020 int i;
6021
6022 for (i = 0; i < Screen_mco; ++i)
6023 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 }
6025 }
6026 if (char_cells == 2)
6027 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6028#endif
6029
6030#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006031 /* The bold trick makes a single column of pixels appear in the
6032 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 * character should be redrawn too. This happens for our own GUI
6034 * and for some xterms. */
6035 if (
6036# ifdef FEAT_GUI
6037 gui.in_use
6038# endif
6039# if defined(FEAT_GUI) && defined(UNIX)
6040 ||
6041# endif
6042# ifdef UNIX
6043 term_is_xterm
6044# endif
6045 )
6046 {
6047 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006048 if (hl > HL_ALL)
6049 hl = syn_attr2attr(hl);
6050 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 redraw_next = TRUE;
6052 }
6053#endif
6054 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6055#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006056 /* For simplicity set the attributes of second half of a
6057 * double-wide character equal to the first half. */
6058 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006060
6061 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 else
6064#endif
6065 screen_char(off_to, row, col + coloff);
6066 }
6067 else if ( p_wiv
6068#ifdef FEAT_GUI
6069 && !gui.in_use
6070#endif
6071 && col + coloff > 0)
6072 {
6073 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6074 {
6075 /*
6076 * Don't output stop-highlight when moving the cursor, it will
6077 * stop the highlighting when it should continue.
6078 */
6079 screen_attr = 0;
6080 }
6081 else if (screen_attr != 0)
6082 screen_stop_highlight();
6083 }
6084
6085 off_to += CHAR_CELLS;
6086 off_from += CHAR_CELLS;
6087 col += CHAR_CELLS;
6088 }
6089
6090#ifdef FEAT_MBYTE
6091 if (clear_next)
6092 {
6093 /* Clear the second half of a double-wide character of which the left
6094 * half was overwritten with a single-wide character. */
6095 ScreenLines[off_to] = ' ';
6096 if (enc_utf8)
6097 ScreenLinesUC[off_to] = 0;
6098 screen_char(off_to, row, col + coloff);
6099 }
6100#endif
6101
6102 if (clear_width > 0
6103#ifdef FEAT_RIGHTLEFT
6104 && !rlflag
6105#endif
6106 )
6107 {
6108#ifdef FEAT_GUI
6109 int startCol = col;
6110#endif
6111
6112 /* blank out the rest of the line */
6113 while (col < clear_width && ScreenLines[off_to] == ' '
6114 && ScreenAttrs[off_to] == 0
6115#ifdef FEAT_MBYTE
6116 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6117#endif
6118 )
6119 {
6120 ++off_to;
6121 ++col;
6122 }
6123 if (col < clear_width)
6124 {
6125#ifdef FEAT_GUI
6126 /*
6127 * In the GUI, clearing the rest of the line may leave pixels
6128 * behind if the first character cleared was bold. Some bold
6129 * fonts spill over the left. In this case we redraw the previous
6130 * character too. If we didn't skip any blanks above, then we
6131 * only redraw if the character wasn't already redrawn anyway.
6132 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006133 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 hl = ScreenAttrs[off_to];
6136 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006137 {
6138 int prev_cells = 1;
6139# ifdef FEAT_MBYTE
6140 if (enc_utf8)
6141 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6142 * that its width is 2. */
6143 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6144 else if (enc_dbcs != 0)
6145 {
6146 /* find previous character by counting from first
6147 * column and get its width. */
6148 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006149 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006150
6151 while (off < off_to)
6152 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006153 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006154 off += prev_cells;
6155 }
6156 }
6157
6158 if (enc_dbcs != 0 && prev_cells > 1)
6159 screen_char_2(off_to - prev_cells, row,
6160 col + coloff - prev_cells);
6161 else
6162# endif
6163 screen_char(off_to - prev_cells, row,
6164 col + coloff - prev_cells);
6165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 }
6167#endif
6168 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6169 ' ', ' ', 0);
6170#ifdef FEAT_VERTSPLIT
6171 off_to += clear_width - col;
6172 col = clear_width;
6173#endif
6174 }
6175 }
6176
6177 if (clear_width > 0)
6178 {
6179#ifdef FEAT_VERTSPLIT
6180 /* For a window that's left of another, draw the separator char. */
6181 if (col + coloff < Columns)
6182 {
6183 int c;
6184
6185 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006186 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006188 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6189 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190# endif
6191 || ScreenAttrs[off_to] != hl)
6192 {
6193 ScreenLines[off_to] = c;
6194 ScreenAttrs[off_to] = hl;
6195# ifdef FEAT_MBYTE
6196 if (enc_utf8)
6197 {
6198 if (c >= 0x80)
6199 {
6200 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006201 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203 else
6204 ScreenLinesUC[off_to] = 0;
6205 }
6206# endif
6207 screen_char(off_to, row, col + coloff);
6208 }
6209 }
6210 else
6211#endif
6212 LineWraps[row] = FALSE;
6213 }
6214}
6215
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006216#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006218 * Mirror text "str" for right-left displaying.
6219 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006222rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 char_u *p1, *p2;
6225 int t;
6226
6227 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6228 {
6229 t = *p1;
6230 *p1 = *p2;
6231 *p2 = t;
6232 }
6233}
6234#endif
6235
6236#if defined(FEAT_WINDOWS) || defined(PROTO)
6237/*
6238 * mark all status lines for redraw; used after first :cd
6239 */
6240 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006241status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 win_T *wp;
6244
6245 for (wp = firstwin; wp; wp = wp->w_next)
6246 if (wp->w_status_height)
6247 {
6248 wp->w_redr_status = TRUE;
6249 redraw_later(VALID);
6250 }
6251}
6252
6253/*
6254 * mark all status lines of the current buffer for redraw
6255 */
6256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006257status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258{
6259 win_T *wp;
6260
6261 for (wp = firstwin; wp; wp = wp->w_next)
6262 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6263 {
6264 wp->w_redr_status = TRUE;
6265 redraw_later(VALID);
6266 }
6267}
6268
6269/*
6270 * Redraw all status lines that need to be redrawn.
6271 */
6272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006273redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 win_T *wp;
6276
6277 for (wp = firstwin; wp; wp = wp->w_next)
6278 if (wp->w_redr_status)
6279 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006280 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006281 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282}
6283#endif
6284
6285#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6286/*
6287 * Redraw all status lines at the bottom of frame "frp".
6288 */
6289 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006290win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 if (frp->fr_layout == FR_LEAF)
6293 frp->fr_win->w_redr_status = TRUE;
6294 else if (frp->fr_layout == FR_ROW)
6295 {
6296 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6297 win_redraw_last_status(frp);
6298 }
6299 else /* frp->fr_layout == FR_COL */
6300 {
6301 frp = frp->fr_child;
6302 while (frp->fr_next != NULL)
6303 frp = frp->fr_next;
6304 win_redraw_last_status(frp);
6305 }
6306}
6307#endif
6308
6309#ifdef FEAT_VERTSPLIT
6310/*
6311 * Draw the verticap separator right of window "wp" starting with line "row".
6312 */
6313 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006314draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 int hl;
6317 int c;
6318
6319 if (wp->w_vsep_width)
6320 {
6321 /* draw the vertical separator right of this window */
6322 c = fillchar_vsep(&hl);
6323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6324 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6325 c, ' ', hl);
6326 }
6327}
6328#endif
6329
6330#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006331static int status_match_len(expand_T *xp, char_u *s);
6332static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006335 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 */
6337 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006338status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 int len = 0;
6341
6342#ifdef FEAT_MENU
6343 int emenu = (xp->xp_context == EXPAND_MENUS
6344 || xp->xp_context == EXPAND_MENUNAMES);
6345
6346 /* Check for menu separators - replace with '|'. */
6347 if (emenu && menu_is_separator(s))
6348 return 1;
6349#endif
6350
6351 while (*s != NUL)
6352 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006353 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006354 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006355 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357
6358 return len;
6359}
6360
6361/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006362 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006363 * These are backslashes used for escaping. Do show backslashes in help tags.
6364 */
6365 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006366skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006367{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006368 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006369#ifdef FEAT_MENU
6370 || ((xp->xp_context == EXPAND_MENUS
6371 || xp->xp_context == EXPAND_MENUNAMES)
6372 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6373#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006374 )
6375 {
6376#ifndef BACKSLASH_IN_FILENAME
6377 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6378 return 2;
6379#endif
6380 return 1;
6381 }
6382 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006383}
6384
6385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Show wildchar matches in the status line.
6387 * Show at least the "match" item.
6388 * We start at item 'first_match' in the list and show all matches that fit.
6389 *
6390 * If inversion is possible we use it. Else '=' characters are used.
6391 */
6392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006393win_redr_status_matches(
6394 expand_T *xp,
6395 int num_matches,
6396 char_u **matches, /* list of matches */
6397 int match,
6398 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6401 int row;
6402 char_u *buf;
6403 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006404 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 int fillchar;
6406 int attr;
6407 int i;
6408 int highlight = TRUE;
6409 char_u *selstart = NULL;
6410 int selstart_col = 0;
6411 char_u *selend = NULL;
6412 static int first_match = 0;
6413 int add_left = FALSE;
6414 char_u *s;
6415#ifdef FEAT_MENU
6416 int emenu;
6417#endif
6418#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6419 int l;
6420#endif
6421
6422 if (matches == NULL) /* interrupted completion? */
6423 return;
6424
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006425#ifdef FEAT_MBYTE
6426 if (has_mbyte)
6427 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6428 else
6429#endif
6430 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (buf == NULL)
6432 return;
6433
6434 if (match == -1) /* don't show match but original text */
6435 {
6436 match = 0;
6437 highlight = FALSE;
6438 }
6439 /* count 1 for the ending ">" */
6440 clen = status_match_len(xp, L_MATCH(match)) + 3;
6441 if (match == 0)
6442 first_match = 0;
6443 else if (match < first_match)
6444 {
6445 /* jumping left, as far as we can go */
6446 first_match = match;
6447 add_left = TRUE;
6448 }
6449 else
6450 {
6451 /* check if match fits on the screen */
6452 for (i = first_match; i < match; ++i)
6453 clen += status_match_len(xp, L_MATCH(i)) + 2;
6454 if (first_match > 0)
6455 clen += 2;
6456 /* jumping right, put match at the left */
6457 if ((long)clen > Columns)
6458 {
6459 first_match = match;
6460 /* if showing the last match, we can add some on the left */
6461 clen = 2;
6462 for (i = match; i < num_matches; ++i)
6463 {
6464 clen += status_match_len(xp, L_MATCH(i)) + 2;
6465 if ((long)clen >= Columns)
6466 break;
6467 }
6468 if (i == num_matches)
6469 add_left = TRUE;
6470 }
6471 }
6472 if (add_left)
6473 while (first_match > 0)
6474 {
6475 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6476 if ((long)clen >= Columns)
6477 break;
6478 --first_match;
6479 }
6480
6481 fillchar = fillchar_status(&attr, TRUE);
6482
6483 if (first_match == 0)
6484 {
6485 *buf = NUL;
6486 len = 0;
6487 }
6488 else
6489 {
6490 STRCPY(buf, "< ");
6491 len = 2;
6492 }
6493 clen = len;
6494
6495 i = first_match;
6496 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6497 {
6498 if (i == match)
6499 {
6500 selstart = buf + len;
6501 selstart_col = clen;
6502 }
6503
6504 s = L_MATCH(i);
6505 /* Check for menu separators - replace with '|' */
6506#ifdef FEAT_MENU
6507 emenu = (xp->xp_context == EXPAND_MENUS
6508 || xp->xp_context == EXPAND_MENUNAMES);
6509 if (emenu && menu_is_separator(s))
6510 {
6511 STRCPY(buf + len, transchar('|'));
6512 l = (int)STRLEN(buf + len);
6513 len += l;
6514 clen += l;
6515 }
6516 else
6517#endif
6518 for ( ; *s != NUL; ++s)
6519 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006520 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 clen += ptr2cells(s);
6522#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006523 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
6525 STRNCPY(buf + len, s, l);
6526 s += l - 1;
6527 len += l;
6528 }
6529 else
6530#endif
6531 {
6532 STRCPY(buf + len, transchar_byte(*s));
6533 len += (int)STRLEN(buf + len);
6534 }
6535 }
6536 if (i == match)
6537 selend = buf + len;
6538
6539 *(buf + len++) = ' ';
6540 *(buf + len++) = ' ';
6541 clen += 2;
6542 if (++i == num_matches)
6543 break;
6544 }
6545
6546 if (i != num_matches)
6547 {
6548 *(buf + len++) = '>';
6549 ++clen;
6550 }
6551
6552 buf[len] = NUL;
6553
6554 row = cmdline_row - 1;
6555 if (row >= 0)
6556 {
6557 if (wild_menu_showing == 0)
6558 {
6559 if (msg_scrolled > 0)
6560 {
6561 /* Put the wildmenu just above the command line. If there is
6562 * no room, scroll the screen one line up. */
6563 if (cmdline_row == Rows - 1)
6564 {
6565 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6566 ++msg_scrolled;
6567 }
6568 else
6569 {
6570 ++cmdline_row;
6571 ++row;
6572 }
6573 wild_menu_showing = WM_SCROLLED;
6574 }
6575 else
6576 {
6577 /* Create status line if needed by setting 'laststatus' to 2.
6578 * Set 'winminheight' to zero to avoid that the window is
6579 * resized. */
6580 if (lastwin->w_status_height == 0)
6581 {
6582 save_p_ls = p_ls;
6583 save_p_wmh = p_wmh;
6584 p_ls = 2;
6585 p_wmh = 0;
6586 last_status(FALSE);
6587 }
6588 wild_menu_showing = WM_SHOWN;
6589 }
6590 }
6591
6592 screen_puts(buf, row, 0, attr);
6593 if (selstart != NULL && highlight)
6594 {
6595 *selend = NUL;
6596 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6597 }
6598
6599 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6600 }
6601
6602#ifdef FEAT_VERTSPLIT
6603 win_redraw_last_status(topframe);
6604#else
6605 lastwin->w_redr_status = TRUE;
6606#endif
6607 vim_free(buf);
6608}
6609#endif
6610
6611#if defined(FEAT_WINDOWS) || defined(PROTO)
6612/*
6613 * Redraw the status line of window wp.
6614 *
6615 * If inversion is possible we use it. Else '=' characters are used.
6616 */
6617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006618win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 int row;
6621 char_u *p;
6622 int len;
6623 int fillchar;
6624 int attr;
6625 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006626 static int busy = FALSE;
6627
6628 /* It's possible to get here recursively when 'statusline' (indirectly)
6629 * invokes ":redrawstatus". Simply ignore the call then. */
6630 if (busy)
6631 return;
6632 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
6634 wp->w_redr_status = FALSE;
6635 if (wp->w_status_height == 0)
6636 {
6637 /* no status line, can only be last window */
6638 redraw_cmdline = TRUE;
6639 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006640 else if (!redrawing()
6641#ifdef FEAT_INS_EXPAND
6642 /* don't update status line when popup menu is visible and may be
6643 * drawn over it */
6644 || pum_visible()
6645#endif
6646 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
6648 /* Don't redraw right now, do it later. */
6649 wp->w_redr_status = TRUE;
6650 }
6651#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006652 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 {
6654 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006655 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657#endif
6658 else
6659 {
6660 fillchar = fillchar_status(&attr, wp == curwin);
6661
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006662 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 p = NameBuff;
6664 len = (int)STRLEN(p);
6665
6666 if (wp->w_buffer->b_help
6667#ifdef FEAT_QUICKFIX
6668 || wp->w_p_pvw
6669#endif
6670 || bufIsChanged(wp->w_buffer)
6671 || wp->w_buffer->b_p_ro)
6672 *(p + len++) = ' ';
6673 if (wp->w_buffer->b_help)
6674 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006675 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 len += (int)STRLEN(p + len);
6677 }
6678#ifdef FEAT_QUICKFIX
6679 if (wp->w_p_pvw)
6680 {
6681 STRCPY(p + len, _("[Preview]"));
6682 len += (int)STRLEN(p + len);
6683 }
6684#endif
6685 if (bufIsChanged(wp->w_buffer))
6686 {
6687 STRCPY(p + len, "[+]");
6688 len += 3;
6689 }
6690 if (wp->w_buffer->b_p_ro)
6691 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006692 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 len += 4;
6694 }
6695
6696#ifndef FEAT_VERTSPLIT
6697 this_ru_col = ru_col;
6698 if (this_ru_col < (Columns + 1) / 2)
6699 this_ru_col = (Columns + 1) / 2;
6700#else
6701 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6702 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6703 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6704 if (this_ru_col <= 1)
6705 {
6706 p = (char_u *)"<"; /* No room for file name! */
6707 len = 1;
6708 }
6709 else
6710#endif
6711#ifdef FEAT_MBYTE
6712 if (has_mbyte)
6713 {
6714 int clen = 0, i;
6715
6716 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006717 clen = mb_string2cells(p, -1);
6718
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 /* Find first character that will fit.
6720 * Going from start to end is much faster for DBCS. */
6721 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006722 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 clen -= (*mb_ptr2cells)(p + i);
6724 len = clen;
6725 if (i > 0)
6726 {
6727 p = p + i - 1;
6728 *p = '<';
6729 ++len;
6730 }
6731
6732 }
6733 else
6734#endif
6735 if (len > this_ru_col - 1)
6736 {
6737 p += len - (this_ru_col - 1);
6738 *p = '<';
6739 len = this_ru_col - 1;
6740 }
6741
6742 row = W_WINROW(wp) + wp->w_height;
6743 screen_puts(p, row, W_WINCOL(wp), attr);
6744 screen_fill(row, row + 1, len + W_WINCOL(wp),
6745 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6746
6747 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6748 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6749 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6750 - 1 + W_WINCOL(wp)), attr);
6751
6752#ifdef FEAT_CMDL_INFO
6753 win_redr_ruler(wp, TRUE);
6754#endif
6755 }
6756
6757#ifdef FEAT_VERTSPLIT
6758 /*
6759 * May need to draw the character below the vertical separator.
6760 */
6761 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6762 {
6763 if (stl_connected(wp))
6764 fillchar = fillchar_status(&attr, wp == curwin);
6765 else
6766 fillchar = fillchar_vsep(&attr);
6767 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6768 attr);
6769 }
6770#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006771 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772}
6773
Bram Moolenaar238a5642006-02-21 22:12:05 +00006774#ifdef FEAT_STL_OPT
6775/*
6776 * Redraw the status line according to 'statusline' and take care of any
6777 * errors encountered.
6778 */
6779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006780redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006781{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006782 static int entered = FALSE;
6783 int save_called_emsg = called_emsg;
6784
6785 /* When called recursively return. This can happen when the statusline
6786 * contains an expression that triggers a redraw. */
6787 if (entered)
6788 return;
6789 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006790
6791 called_emsg = FALSE;
6792 win_redr_custom(wp, FALSE);
6793 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006794 {
6795 /* When there is an error disable the statusline, otherwise the
6796 * display is messed up with errors and a redraw triggers the problem
6797 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006798 set_string_option_direct((char_u *)"statusline", -1,
6799 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006800 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006802 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006803 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006804}
6805#endif
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807# ifdef FEAT_VERTSPLIT
6808/*
6809 * Return TRUE if the status line of window "wp" is connected to the status
6810 * line of the window right of it. If not, then it's a vertical separator.
6811 * Only call if (wp->w_vsep_width != 0).
6812 */
6813 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006814stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 frame_T *fr;
6817
6818 fr = wp->w_frame;
6819 while (fr->fr_parent != NULL)
6820 {
6821 if (fr->fr_parent->fr_layout == FR_COL)
6822 {
6823 if (fr->fr_next != NULL)
6824 break;
6825 }
6826 else
6827 {
6828 if (fr->fr_next != NULL)
6829 return TRUE;
6830 }
6831 fr = fr->fr_parent;
6832 }
6833 return FALSE;
6834}
6835# endif
6836
6837#endif /* FEAT_WINDOWS */
6838
6839#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6840/*
6841 * Get the value to show for the language mappings, active 'keymap'.
6842 */
6843 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006844get_keymap_str(
6845 win_T *wp,
6846 char_u *buf, /* buffer for the result */
6847 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 char_u *p;
6850
6851 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6852 return FALSE;
6853
6854 {
6855#ifdef FEAT_EVAL
6856 buf_T *old_curbuf = curbuf;
6857 win_T *old_curwin = curwin;
6858 char_u *s;
6859
6860 curbuf = wp->w_buffer;
6861 curwin = wp;
6862 STRCPY(buf, "b:keymap_name"); /* must be writable */
6863 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006864 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 --emsg_skip;
6866 curbuf = old_curbuf;
6867 curwin = old_curwin;
6868 if (p == NULL || *p == NUL)
6869#endif
6870 {
6871#ifdef FEAT_KEYMAP
6872 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6873 p = wp->w_buffer->b_p_keymap;
6874 else
6875#endif
6876 p = (char_u *)"lang";
6877 }
6878 if ((int)(STRLEN(p) + 3) < len)
6879 sprintf((char *)buf, "<%s>", p);
6880 else
6881 buf[0] = NUL;
6882#ifdef FEAT_EVAL
6883 vim_free(s);
6884#endif
6885 }
6886 return buf[0] != NUL;
6887}
6888#endif
6889
6890#if defined(FEAT_STL_OPT) || defined(PROTO)
6891/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006892 * Redraw the status line or ruler of window "wp".
6893 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 */
6895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006896win_redr_custom(
6897 win_T *wp,
6898 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006900 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 int attr;
6902 int curattr;
6903 int row;
6904 int col = 0;
6905 int maxwidth;
6906 int width;
6907 int n;
6908 int len;
6909 int fillchar;
6910 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006911 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006913 struct stl_hlrec hltab[STL_MAX_ITEM];
6914 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006915 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006916 win_T *ewp;
6917 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
Bram Moolenaar1d633412013-12-11 15:52:01 +01006919 /* There is a tiny chance that this gets called recursively: When
6920 * redrawing a status line triggers redrawing the ruler or tabline.
6921 * Avoid trouble by not allowing recursion. */
6922 if (entered)
6923 return;
6924 entered = TRUE;
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006927 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006929 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006930 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006931 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006932 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006933 attr = hl_attr(HLF_TPF);
6934 maxwidth = Columns;
6935# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006936 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006939 else
6940 {
6941 row = W_WINROW(wp) + wp->w_height;
6942 fillchar = fillchar_status(&attr, wp == curwin);
6943 maxwidth = W_WIDTH(wp);
6944
6945 if (draw_ruler)
6946 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006947 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006948 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006949 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006950 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006951 if (*++stl == '-')
6952 stl++;
6953 if (atoi((char *)stl))
6954 while (VIM_ISDIGIT(*stl))
6955 stl++;
6956 if (*stl++ != '(')
6957 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006958 }
6959#ifdef FEAT_VERTSPLIT
6960 col = ru_col - (Columns - W_WIDTH(wp));
6961 if (col < (W_WIDTH(wp) + 1) / 2)
6962 col = (W_WIDTH(wp) + 1) / 2;
6963#else
6964 col = ru_col;
6965 if (col > (Columns + 1) / 2)
6966 col = (Columns + 1) / 2;
6967#endif
6968 maxwidth = W_WIDTH(wp) - col;
6969#ifdef FEAT_WINDOWS
6970 if (!wp->w_status_height)
6971#endif
6972 {
6973 row = Rows - 1;
6974 --maxwidth; /* writing in last column may cause scrolling */
6975 fillchar = ' ';
6976 attr = 0;
6977 }
6978
6979# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006980 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006981# endif
6982 }
6983 else
6984 {
6985 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006986 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006987 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006988 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006989# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006990 use_sandbox = was_set_insecurely((char_u *)"statusline",
6991 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006992# endif
6993 }
6994
6995#ifdef FEAT_VERTSPLIT
6996 col += W_WINCOL(wp);
6997#endif
6998 }
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007001 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
Bram Moolenaar61452852011-02-01 18:01:11 +01007003 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7004 * the cursor away and back. */
7005 ewp = wp == NULL ? curwin : wp;
7006 p_crb_save = ewp->w_p_crb;
7007 ewp->w_p_crb = FALSE;
7008
Bram Moolenaar362f3562009-11-03 16:20:34 +00007009 /* Make a copy, because the statusline may include a function call that
7010 * might change the option value and free the memory. */
7011 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007012 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007013 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007014 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007015 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007016 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007018 /* Make all characters printable. */
7019 p = transstr(buf);
7020 if (p != NULL)
7021 {
7022 vim_strncpy(buf, p, sizeof(buf) - 1);
7023 vim_free(p);
7024 }
7025
7026 /* fill up with "fillchar" */
7027 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007028 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 {
7030#ifdef FEAT_MBYTE
7031 len += (*mb_char2bytes)(fillchar, buf + len);
7032#else
7033 buf[len++] = fillchar;
7034#endif
7035 ++width;
7036 }
7037 buf[len] = NUL;
7038
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007039 /*
7040 * Draw each snippet with the specified highlighting.
7041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 curattr = attr;
7043 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007044 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007046 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 screen_puts_len(p, len, row, col, curattr);
7048 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007051 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007053 else if (hltab[n].userhl < 0)
7054 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007056 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007057 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058#endif
7059 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007060 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
7062 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007063
7064 if (wp == NULL)
7065 {
7066 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7067 col = 0;
7068 len = 0;
7069 p = buf;
7070 fillchar = 0;
7071 for (n = 0; tabtab[n].start != NULL; n++)
7072 {
7073 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7074 while (col < len)
7075 TabPageIdxs[col++] = fillchar;
7076 p = tabtab[n].start;
7077 fillchar = tabtab[n].userhl;
7078 }
7079 while (col < Columns)
7080 TabPageIdxs[col++] = fillchar;
7081 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007082
7083theend:
7084 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
7087#endif /* FEAT_STL_OPT */
7088
7089/*
7090 * Output a single character directly to the screen and update ScreenLines.
7091 */
7092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007093screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 char_u buf[MB_MAXBYTES + 1];
7096
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007097#ifdef FEAT_MBYTE
7098 if (has_mbyte)
7099 buf[(*mb_char2bytes)(c, buf)] = NUL;
7100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007102 {
7103 buf[0] = c;
7104 buf[1] = NUL;
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 screen_puts(buf, row, col, attr);
7107}
7108
7109/*
7110 * Get a single character directly from ScreenLines into "bytes[]".
7111 * Also return its attribute in *attrp;
7112 */
7113 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007114screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 unsigned off;
7117
7118 /* safety check */
7119 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7120 {
7121 off = LineOffset[row] + col;
7122 *attrp = ScreenAttrs[off];
7123 bytes[0] = ScreenLines[off];
7124 bytes[1] = NUL;
7125
7126#ifdef FEAT_MBYTE
7127 if (enc_utf8 && ScreenLinesUC[off] != 0)
7128 bytes[utfc_char2bytes(off, bytes)] = NUL;
7129 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7130 {
7131 bytes[0] = ScreenLines[off];
7132 bytes[1] = ScreenLines2[off];
7133 bytes[2] = NUL;
7134 }
7135 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7136 {
7137 bytes[1] = ScreenLines[off + 1];
7138 bytes[2] = NUL;
7139 }
7140#endif
7141 }
7142}
7143
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007144#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007145static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007146
7147/*
7148 * Return TRUE if composing characters for screen posn "off" differs from
7149 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007150 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007151 */
7152 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007153screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007154{
7155 int i;
7156
7157 for (i = 0; i < Screen_mco; ++i)
7158 {
7159 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7160 return TRUE;
7161 if (u8cc[i] == 0)
7162 break;
7163 }
7164 return FALSE;
7165}
7166#endif
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168/*
7169 * Put string '*text' on the screen at position 'row' and 'col', with
7170 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7171 * Note: only outputs within one row, message is truncated at screen boundary!
7172 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7173 */
7174 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007175screen_puts(
7176 char_u *text,
7177 int row,
7178 int col,
7179 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 screen_puts_len(text, -1, row, col, attr);
7182}
7183
7184/*
7185 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7186 * a NUL.
7187 */
7188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007189screen_puts_len(
7190 char_u *text,
7191 int textlen,
7192 int row,
7193 int col,
7194 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 unsigned off;
7197 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007198 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 int c;
7200#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007201 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 int mbyte_blen = 1;
7203 int mbyte_cells = 1;
7204 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007205 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 int clear_next_cell = FALSE;
7207# ifdef FEAT_ARABIC
7208 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007209 int pc, nc, nc1;
7210 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211# endif
7212#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007213#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7214 int force_redraw_this;
7215 int force_redraw_next = FALSE;
7216#endif
7217 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7220 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007221 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
Bram Moolenaarc236c162008-07-13 17:41:49 +00007223#ifdef FEAT_MBYTE
7224 /* When drawing over the right halve of a double-wide char clear out the
7225 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007226 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007227# ifdef FEAT_GUI
7228 && !gui.in_use
7229# endif
7230 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007231 {
7232 ScreenLines[off - 1] = ' ';
7233 ScreenAttrs[off - 1] = 0;
7234 if (enc_utf8)
7235 {
7236 ScreenLinesUC[off - 1] = 0;
7237 ScreenLinesC[0][off - 1] = 0;
7238 }
7239 /* redraw the previous cell, make it empty */
7240 screen_char(off - 1, row, col - 1);
7241 /* force the cell at "col" to be redrawn */
7242 force_redraw_next = TRUE;
7243 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007244#endif
7245
Bram Moolenaar367329b2007-08-30 11:53:22 +00007246#ifdef FEAT_MBYTE
7247 max_off = LineOffset[row] + screen_Columns;
7248#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007249 while (col < screen_Columns
7250 && (len < 0 || (int)(ptr - text) < len)
7251 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253 c = *ptr;
7254#ifdef FEAT_MBYTE
7255 /* check if this is the first byte of a multibyte */
7256 if (has_mbyte)
7257 {
7258 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007259 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7263 mbyte_cells = 1;
7264 else if (enc_dbcs != 0)
7265 mbyte_cells = mbyte_blen;
7266 else /* enc_utf8 */
7267 {
7268 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007269 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 (int)((text + len) - ptr));
7271 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007272 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007274# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* Non-BMP character: display as ? or fullwidth ?. */
7276 if (u8c >= 0x10000)
7277 {
7278 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7279 if (attr == 0)
7280 attr = hl_attr(HLF_8);
7281 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007282# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283# ifdef FEAT_ARABIC
7284 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7285 {
7286 /* Do Arabic shaping. */
7287 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7288 {
7289 /* Past end of string to be displayed. */
7290 nc = NUL;
7291 nc1 = NUL;
7292 }
7293 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007294 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007295 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7296 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007297 nc1 = pcc[0];
7298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 pc = prev_c;
7300 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007301 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303 else
7304 prev_c = u8c;
7305# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007306 if (col + mbyte_cells > screen_Columns)
7307 {
7308 /* Only 1 cell left, but character requires 2 cells:
7309 * display a '>' in the last column to avoid wrapping. */
7310 c = '>';
7311 mbyte_cells = 1;
7312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 }
7314 }
7315#endif
7316
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007317#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7318 force_redraw_this = force_redraw_next;
7319 force_redraw_next = FALSE;
7320#endif
7321
7322 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323#ifdef FEAT_MBYTE
7324 || (mbyte_cells == 2
7325 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7326 || (enc_dbcs == DBCS_JPNU
7327 && c == 0x8e
7328 && ScreenLines2[off] != ptr[1])
7329 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007330 && (ScreenLinesUC[off] !=
7331 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7332 || (ScreenLinesUC[off] != 0
7333 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#endif
7335 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007336 || exmode_active;
7337
7338 if (need_redraw
7339#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7340 || force_redraw_this
7341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 )
7343 {
7344#if defined(FEAT_GUI) || defined(UNIX)
7345 /* The bold trick makes a single row of pixels appear in the next
7346 * character. When a bold character is removed, the next
7347 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007348 * and for some xterms. */
7349 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350# ifdef FEAT_GUI
7351 gui.in_use
7352# endif
7353# if defined(FEAT_GUI) && defined(UNIX)
7354 ||
7355# endif
7356# ifdef UNIX
7357 term_is_xterm
7358# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007359 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007361 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007363 if (n > HL_ALL)
7364 n = syn_attr2attr(n);
7365 if (n & HL_BOLD)
7366 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368#endif
7369#ifdef FEAT_MBYTE
7370 /* When at the end of the text and overwriting a two-cell
7371 * character with a one-cell character, need to clear the next
7372 * cell. Also when overwriting the left halve of a two-cell char
7373 * with the right halve of a two-cell char. Do this only once
7374 * (mb_off2cells() may return 2 on the right halve). */
7375 if (clear_next_cell)
7376 clear_next_cell = FALSE;
7377 else if (has_mbyte
7378 && (len < 0 ? ptr[mbyte_blen] == NUL
7379 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007380 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007382 && (*mb_off2cells)(off, max_off) == 1
7383 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 clear_next_cell = TRUE;
7385
7386 /* Make sure we never leave a second byte of a double-byte behind,
7387 * it confuses mb_off2cells(). */
7388 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007389 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007391 && (*mb_off2cells)(off, max_off) == 1
7392 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 ScreenLines[off + mbyte_blen] = 0;
7394#endif
7395 ScreenLines[off] = c;
7396 ScreenAttrs[off] = attr;
7397#ifdef FEAT_MBYTE
7398 if (enc_utf8)
7399 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007400 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 ScreenLinesUC[off] = 0;
7402 else
7403 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404 int i;
7405
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007407 for (i = 0; i < Screen_mco; ++i)
7408 {
7409 ScreenLinesC[i][off] = u8cc[i];
7410 if (u8cc[i] == 0)
7411 break;
7412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
7414 if (mbyte_cells == 2)
7415 {
7416 ScreenLines[off + 1] = 0;
7417 ScreenAttrs[off + 1] = attr;
7418 }
7419 screen_char(off, row, col);
7420 }
7421 else if (mbyte_cells == 2)
7422 {
7423 ScreenLines[off + 1] = ptr[1];
7424 ScreenAttrs[off + 1] = attr;
7425 screen_char_2(off, row, col);
7426 }
7427 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7428 {
7429 ScreenLines2[off] = ptr[1];
7430 screen_char(off, row, col);
7431 }
7432 else
7433#endif
7434 screen_char(off, row, col);
7435 }
7436#ifdef FEAT_MBYTE
7437 if (has_mbyte)
7438 {
7439 off += mbyte_cells;
7440 col += mbyte_cells;
7441 ptr += mbyte_blen;
7442 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007443 {
7444 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007446 len = -1;
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 }
7449 else
7450#endif
7451 {
7452 ++off;
7453 ++col;
7454 ++ptr;
7455 }
7456 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007457
7458#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7459 /* If we detected the next character needs to be redrawn, but the text
7460 * doesn't extend up to there, update the character here. */
7461 if (force_redraw_next && col < screen_Columns)
7462 {
7463# ifdef FEAT_MBYTE
7464 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7465 screen_char_2(off, row, col);
7466 else
7467# endif
7468 screen_char(off, row, col);
7469 }
7470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471}
7472
7473#ifdef FEAT_SEARCH_EXTRA
7474/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007475 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 */
7477 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007478start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479{
7480 if (p_hls && !no_hlsearch)
7481 {
7482 last_pat_prog(&search_hl.rm);
7483 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007484# ifdef FEAT_RELTIME
7485 /* Set the time limit to 'redrawtime'. */
7486 profile_setlimit(p_rdt, &search_hl.tm);
7487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 }
7489}
7490
7491/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007492 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 */
7494 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007495end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 if (search_hl.rm.regprog != NULL)
7498 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007499 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 search_hl.rm.regprog = NULL;
7501 }
7502}
7503
7504/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007505 * Init for calling prepare_search_hl().
7506 */
7507 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007508init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007509{
7510 matchitem_T *cur;
7511
7512 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7513 * match */
7514 cur = wp->w_match_head;
7515 while (cur != NULL)
7516 {
7517 cur->hl.rm = cur->match;
7518 if (cur->hlg_id == 0)
7519 cur->hl.attr = 0;
7520 else
7521 cur->hl.attr = syn_id2attr(cur->hlg_id);
7522 cur->hl.buf = wp->w_buffer;
7523 cur->hl.lnum = 0;
7524 cur->hl.first_lnum = 0;
7525# ifdef FEAT_RELTIME
7526 /* Set the time limit to 'redrawtime'. */
7527 profile_setlimit(p_rdt, &(cur->hl.tm));
7528# endif
7529 cur = cur->next;
7530 }
7531 search_hl.buf = wp->w_buffer;
7532 search_hl.lnum = 0;
7533 search_hl.first_lnum = 0;
7534 /* time limit is set at the toplevel, for all windows */
7535}
7536
7537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 * Advance to the match in window "wp" line "lnum" or past it.
7539 */
7540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007541prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007543 matchitem_T *cur; /* points to the match list */
7544 match_T *shl; /* points to search_hl or a match */
7545 int shl_flag; /* flag to indicate whether search_hl
7546 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007547 int pos_inprogress; /* marks that position match search is
7548 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 int n;
7550
7551 /*
7552 * When using a multi-line pattern, start searching at the top
7553 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007554 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007556 cur = wp->w_match_head;
7557 shl_flag = FALSE;
7558 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007560 if (shl_flag == FALSE)
7561 {
7562 shl = &search_hl;
7563 shl_flag = TRUE;
7564 }
7565 else
7566 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 if (shl->rm.regprog != NULL
7568 && shl->lnum == 0
7569 && re_multiline(shl->rm.regprog))
7570 {
7571 if (shl->first_lnum == 0)
7572 {
7573# ifdef FEAT_FOLDING
7574 for (shl->first_lnum = lnum;
7575 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7576 if (hasFoldingWin(wp, shl->first_lnum - 1,
7577 NULL, NULL, TRUE, NULL))
7578 break;
7579# else
7580 shl->first_lnum = wp->w_topline;
7581# endif
7582 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007583 if (cur != NULL)
7584 cur->pos.cur = 0;
7585 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007587 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7588 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007590 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7591 pos_inprogress = cur == NULL || cur->pos.cur == 0
7592 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (shl->lnum != 0)
7594 {
7595 shl->first_lnum = shl->lnum
7596 + shl->rm.endpos[0].lnum
7597 - shl->rm.startpos[0].lnum;
7598 n = shl->rm.endpos[0].col;
7599 }
7600 else
7601 {
7602 ++shl->first_lnum;
7603 n = 0;
7604 }
7605 }
7606 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007607 if (shl != &search_hl && cur != NULL)
7608 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
7610}
7611
7612/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007613 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 * Uses shl->buf.
7615 * Sets shl->lnum and shl->rm contents.
7616 * Note: Assumes a previous match is always before "lnum", unless
7617 * shl->lnum is zero.
7618 * Careful: Any pointers for buffer lines will become invalid.
7619 */
7620 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007621next_search_hl(
7622 win_T *win,
7623 match_T *shl, /* points to search_hl or a match */
7624 linenr_T lnum,
7625 colnr_T mincol, /* minimal column for a match */
7626 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
7628 linenr_T l;
7629 colnr_T matchcol;
7630 long nmatched;
7631
7632 if (shl->lnum != 0)
7633 {
7634 /* Check for three situations:
7635 * 1. If the "lnum" is below a previous match, start a new search.
7636 * 2. If the previous match includes "mincol", use it.
7637 * 3. Continue after the previous match.
7638 */
7639 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7640 if (lnum > l)
7641 shl->lnum = 0;
7642 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7643 return;
7644 }
7645
7646 /*
7647 * Repeat searching for a match until one is found that includes "mincol"
7648 * or none is found in this line.
7649 */
7650 called_emsg = FALSE;
7651 for (;;)
7652 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007653#ifdef FEAT_RELTIME
7654 /* Stop searching after passing the time limit. */
7655 if (profile_passed_limit(&(shl->tm)))
7656 {
7657 shl->lnum = 0; /* no match found in time */
7658 break;
7659 }
7660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 /* Three situations:
7662 * 1. No useful previous match: search from start of line.
7663 * 2. Not Vi compatible or empty match: continue at next character.
7664 * Break the loop if this is beyond the end of the line.
7665 * 3. Vi compatible searching: continue at end of previous match.
7666 */
7667 if (shl->lnum == 0)
7668 matchcol = 0;
7669 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7670 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007671 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007673 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007674
7675 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007676 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007677 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007679 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 shl->lnum = 0;
7681 break;
7682 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007683#ifdef FEAT_MBYTE
7684 if (has_mbyte)
7685 matchcol += mb_ptr2len(ml);
7686 else
7687#endif
7688 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690 else
7691 matchcol = shl->rm.endpos[0].col;
7692
7693 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007694 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007696 /* Remember whether shl->rm is using a copy of the regprog in
7697 * cur->match. */
7698 int regprog_is_copy = (shl != &search_hl && cur != NULL
7699 && shl == &cur->hl
7700 && cur->match.regprog == cur->hl.rm.regprog);
7701
Bram Moolenaarb3414592014-06-17 17:48:32 +02007702 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7703 matchcol,
7704#ifdef FEAT_RELTIME
7705 &(shl->tm)
7706#else
7707 NULL
7708#endif
7709 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007710 /* Copy the regprog, in case it got freed and recompiled. */
7711 if (regprog_is_copy)
7712 cur->match.regprog = cur->hl.rm.regprog;
7713
Bram Moolenaarb3414592014-06-17 17:48:32 +02007714 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007715 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007716 /* Error while handling regexp: stop using this regexp. */
7717 if (shl == &search_hl)
7718 {
7719 /* don't free regprog in the match list, it's a copy */
7720 vim_regfree(shl->rm.regprog);
7721 SET_NO_HLSEARCH(TRUE);
7722 }
7723 shl->rm.regprog = NULL;
7724 shl->lnum = 0;
7725 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7726 message */
7727 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007728 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007729 }
7730 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007731 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007732 else
7733 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 if (nmatched == 0)
7735 {
7736 shl->lnum = 0; /* no match found */
7737 break;
7738 }
7739 if (shl->rm.startpos[0].lnum > 0
7740 || shl->rm.startpos[0].col >= mincol
7741 || nmatched > 1
7742 || shl->rm.endpos[0].col > mincol)
7743 {
7744 shl->lnum += shl->rm.startpos[0].lnum;
7745 break; /* useful match found */
7746 }
7747 }
7748}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
Bram Moolenaarb3414592014-06-17 17:48:32 +02007750 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007751next_search_hl_pos(
7752 match_T *shl, /* points to a match */
7753 linenr_T lnum,
7754 posmatch_T *posmatch, /* match positions */
7755 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007756{
7757 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007758 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007759
7760 shl->lnum = 0;
7761 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7762 {
7763 if (posmatch->pos[i].lnum == 0)
7764 break;
7765 if (posmatch->pos[i].col < mincol)
7766 continue;
7767 if (posmatch->pos[i].lnum == lnum)
7768 {
7769 if (shl->lnum == lnum)
7770 {
7771 /* partially sort positions by column numbers
7772 * on the same line */
7773 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7774 {
7775 llpos_T tmp = posmatch->pos[i];
7776
7777 posmatch->pos[i] = posmatch->pos[bot];
7778 posmatch->pos[bot] = tmp;
7779 }
7780 }
7781 else
7782 {
7783 bot = i;
7784 shl->lnum = lnum;
7785 }
7786 }
7787 }
7788 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007789 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007790 {
7791 colnr_T start = posmatch->pos[bot].col == 0
7792 ? 0 : posmatch->pos[bot].col - 1;
7793 colnr_T end = posmatch->pos[bot].col == 0
7794 ? MAXCOL : start + posmatch->pos[bot].len;
7795
7796 shl->rm.startpos[0].lnum = 0;
7797 shl->rm.startpos[0].col = start;
7798 shl->rm.endpos[0].lnum = 0;
7799 shl->rm.endpos[0].col = end;
7800 posmatch->cur = bot + 1;
7801 return TRUE;
7802 }
7803 return FALSE;
7804}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007805#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007808screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 attrentry_T *aep = NULL;
7811
7812 screen_attr = attr;
7813 if (full_screen
7814#ifdef WIN3264
7815 && termcap_active
7816#endif
7817 )
7818 {
7819#ifdef FEAT_GUI
7820 if (gui.in_use)
7821 {
7822 char buf[20];
7823
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007824 /* The GUI handles this internally. */
7825 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 OUT_STR(buf);
7827 }
7828 else
7829#endif
7830 {
7831 if (attr > HL_ALL) /* special HL attr. */
7832 {
7833 if (t_colors > 1)
7834 aep = syn_cterm_attr2entry(attr);
7835 else
7836 aep = syn_term_attr2entry(attr);
7837 if (aep == NULL) /* did ":syntax clear" */
7838 attr = 0;
7839 else
7840 attr = aep->ae_attr;
7841 }
7842 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7843 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007844 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7845 && cterm_normal_fg_bold)
7846 /* If the Normal FG color has BOLD attribute and the new HL
7847 * has a FG color defined, clear BOLD. */
7848 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7850 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007851 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7852 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 out_str(T_US);
7854 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7855 out_str(T_CZH);
7856 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7857 out_str(T_MR);
7858
7859 /*
7860 * Output the color or start string after bold etc., in case the
7861 * bold etc. override the color setting.
7862 */
7863 if (aep != NULL)
7864 {
7865 if (t_colors > 1)
7866 {
7867 if (aep->ae_u.cterm.fg_color)
7868 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7869 if (aep->ae_u.cterm.bg_color)
7870 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7871 }
7872 else
7873 {
7874 if (aep->ae_u.term.start != NULL)
7875 out_str(aep->ae_u.term.start);
7876 }
7877 }
7878 }
7879 }
7880}
7881
7882 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007883screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 int do_ME = FALSE; /* output T_ME code */
7886
7887 if (screen_attr != 0
7888#ifdef WIN3264
7889 && termcap_active
7890#endif
7891 )
7892 {
7893#ifdef FEAT_GUI
7894 if (gui.in_use)
7895 {
7896 char buf[20];
7897
7898 /* use internal GUI code */
7899 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7900 OUT_STR(buf);
7901 }
7902 else
7903#endif
7904 {
7905 if (screen_attr > HL_ALL) /* special HL attr. */
7906 {
7907 attrentry_T *aep;
7908
7909 if (t_colors > 1)
7910 {
7911 /*
7912 * Assume that t_me restores the original colors!
7913 */
7914 aep = syn_cterm_attr2entry(screen_attr);
7915 if (aep != NULL && (aep->ae_u.cterm.fg_color
7916 || aep->ae_u.cterm.bg_color))
7917 do_ME = TRUE;
7918 }
7919 else
7920 {
7921 aep = syn_term_attr2entry(screen_attr);
7922 if (aep != NULL && aep->ae_u.term.stop != NULL)
7923 {
7924 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7925 do_ME = TRUE;
7926 else
7927 out_str(aep->ae_u.term.stop);
7928 }
7929 }
7930 if (aep == NULL) /* did ":syntax clear" */
7931 screen_attr = 0;
7932 else
7933 screen_attr = aep->ae_attr;
7934 }
7935
7936 /*
7937 * Often all ending-codes are equal to T_ME. Avoid outputting the
7938 * same sequence several times.
7939 */
7940 if (screen_attr & HL_STANDOUT)
7941 {
7942 if (STRCMP(T_SE, T_ME) == 0)
7943 do_ME = TRUE;
7944 else
7945 out_str(T_SE);
7946 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007947 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
7949 if (STRCMP(T_UE, T_ME) == 0)
7950 do_ME = TRUE;
7951 else
7952 out_str(T_UE);
7953 }
7954 if (screen_attr & HL_ITALIC)
7955 {
7956 if (STRCMP(T_CZR, T_ME) == 0)
7957 do_ME = TRUE;
7958 else
7959 out_str(T_CZR);
7960 }
7961 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7962 out_str(T_ME);
7963
7964 if (t_colors > 1)
7965 {
7966 /* set Normal cterm colors */
7967 if (cterm_normal_fg_color != 0)
7968 term_fg_color(cterm_normal_fg_color - 1);
7969 if (cterm_normal_bg_color != 0)
7970 term_bg_color(cterm_normal_bg_color - 1);
7971 if (cterm_normal_fg_bold)
7972 out_str(T_MD);
7973 }
7974 }
7975 }
7976 screen_attr = 0;
7977}
7978
7979/*
7980 * Reset the colors for a cterm. Used when leaving Vim.
7981 * The machine specific code may override this again.
7982 */
7983 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007984reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 if (t_colors > 1)
7987 {
7988 /* set Normal cterm colors */
7989 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7990 {
7991 out_str(T_OP);
7992 screen_attr = -1;
7993 }
7994 if (cterm_normal_fg_bold)
7995 {
7996 out_str(T_ME);
7997 screen_attr = -1;
7998 }
7999 }
8000}
8001
8002/*
8003 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8004 * using the attributes from ScreenAttrs["off"].
8005 */
8006 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008007screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 int attr;
8010
8011 /* Check for illegal values, just in case (could happen just after
8012 * resizing). */
8013 if (row >= screen_Rows || col >= screen_Columns)
8014 return;
8015
Bram Moolenaar494838a2015-02-10 19:20:37 +01008016 /* Outputting a character in the last cell on the screen may scroll the
8017 * screen up. Only do it when the "xn" termcap property is set, otherwise
8018 * mark the character invalid (update it when scrolled up). */
8019 if (*T_XN == NUL
8020 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021#ifdef FEAT_RIGHTLEFT
8022 /* account for first command-line character in rightleft mode */
8023 && !cmdmsg_rl
8024#endif
8025 )
8026 {
8027 ScreenAttrs[off] = (sattr_T)-1;
8028 return;
8029 }
8030
8031 /*
8032 * Stop highlighting first, so it's easier to move the cursor.
8033 */
8034#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8035 if (screen_char_attr != 0)
8036 attr = screen_char_attr;
8037 else
8038#endif
8039 attr = ScreenAttrs[off];
8040 if (screen_attr != attr)
8041 screen_stop_highlight();
8042
8043 windgoto(row, col);
8044
8045 if (screen_attr != attr)
8046 screen_start_highlight(attr);
8047
8048#ifdef FEAT_MBYTE
8049 if (enc_utf8 && ScreenLinesUC[off] != 0)
8050 {
8051 char_u buf[MB_MAXBYTES + 1];
8052
8053 /* Convert UTF-8 character to bytes and write it. */
8054
8055 buf[utfc_char2bytes(off, buf)] = NUL;
8056
8057 out_str(buf);
8058 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8059 ++screen_cur_col;
8060 }
8061 else
8062#endif
8063 {
8064#ifdef FEAT_MBYTE
8065 out_flush_check();
8066#endif
8067 out_char(ScreenLines[off]);
8068#ifdef FEAT_MBYTE
8069 /* double-byte character in single-width cell */
8070 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8071 out_char(ScreenLines2[off]);
8072#endif
8073 }
8074
8075 screen_cur_col++;
8076}
8077
8078#ifdef FEAT_MBYTE
8079
8080/*
8081 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8082 * on the screen at position 'row' and 'col'.
8083 * The attributes of the first byte is used for all. This is required to
8084 * output the two bytes of a double-byte character with nothing in between.
8085 */
8086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008087screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 /* Check for illegal values (could be wrong when screen was resized). */
8090 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8091 return;
8092
8093 /* Outputting the last character on the screen may scrollup the screen.
8094 * Don't to it! Mark the character invalid (update it when scrolled up) */
8095 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8096 {
8097 ScreenAttrs[off] = (sattr_T)-1;
8098 return;
8099 }
8100
8101 /* Output the first byte normally (positions the cursor), then write the
8102 * second byte directly. */
8103 screen_char(off, row, col);
8104 out_char(ScreenLines[off + 1]);
8105 ++screen_cur_col;
8106}
8107#endif
8108
8109#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8110/*
8111 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8112 * This uses the contents of ScreenLines[] and doesn't change it.
8113 */
8114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008115screen_draw_rectangle(
8116 int row,
8117 int col,
8118 int height,
8119 int width,
8120 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 int r, c;
8123 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008124#ifdef FEAT_MBYTE
8125 int max_off;
8126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008128 /* Can't use ScreenLines unless initialized */
8129 if (ScreenLines == NULL)
8130 return;
8131
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 if (invert)
8133 screen_char_attr = HL_INVERSE;
8134 for (r = row; r < row + height; ++r)
8135 {
8136 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008137#ifdef FEAT_MBYTE
8138 max_off = off + screen_Columns;
8139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 for (c = col; c < col + width; ++c)
8141 {
8142#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008143 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 screen_char_2(off + c, r, c);
8146 ++c;
8147 }
8148 else
8149#endif
8150 {
8151 screen_char(off + c, r, c);
8152#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008153 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 ++c;
8155#endif
8156 }
8157 }
8158 }
8159 screen_char_attr = 0;
8160}
8161#endif
8162
8163#ifdef FEAT_VERTSPLIT
8164/*
8165 * Redraw the characters for a vertically split window.
8166 */
8167 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008168redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 int col;
8171 int width;
8172
8173# ifdef FEAT_CLIPBOARD
8174 clip_may_clear_selection(row, end - 1);
8175# endif
8176
8177 if (wp == NULL)
8178 {
8179 col = 0;
8180 width = Columns;
8181 }
8182 else
8183 {
8184 col = wp->w_wincol;
8185 width = wp->w_width;
8186 }
8187 screen_draw_rectangle(row, col, end - row, width, FALSE);
8188}
8189#endif
8190
8191/*
8192 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8193 * with character 'c1' in first column followed by 'c2' in the other columns.
8194 * Use attributes 'attr'.
8195 */
8196 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008197screen_fill(
8198 int start_row,
8199 int end_row,
8200 int start_col,
8201 int end_col,
8202 int c1,
8203 int c2,
8204 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 int row;
8207 int col;
8208 int off;
8209 int end_off;
8210 int did_delete;
8211 int c;
8212 int norm_term;
8213#if defined(FEAT_GUI) || defined(UNIX)
8214 int force_next = FALSE;
8215#endif
8216
8217 if (end_row > screen_Rows) /* safety check */
8218 end_row = screen_Rows;
8219 if (end_col > screen_Columns) /* safety check */
8220 end_col = screen_Columns;
8221 if (ScreenLines == NULL
8222 || start_row >= end_row
8223 || start_col >= end_col) /* nothing to do */
8224 return;
8225
8226 /* it's a "normal" terminal when not in a GUI or cterm */
8227 norm_term = (
8228#ifdef FEAT_GUI
8229 !gui.in_use &&
8230#endif
8231 t_colors <= 1);
8232 for (row = start_row; row < end_row; ++row)
8233 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008234#ifdef FEAT_MBYTE
8235 if (has_mbyte
8236# ifdef FEAT_GUI
8237 && !gui.in_use
8238# endif
8239 )
8240 {
8241 /* When drawing over the right halve of a double-wide char clear
8242 * out the left halve. When drawing over the left halve of a
8243 * double wide-char clear out the right halve. Only needed in a
8244 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008245 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008246 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008247 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008248 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008249 }
8250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 /*
8252 * Try to use delete-line termcap code, when no attributes or in a
8253 * "normal" terminal, where a bold/italic space is just a
8254 * space.
8255 */
8256 did_delete = FALSE;
8257 if (c2 == ' '
8258 && end_col == Columns
8259 && can_clear(T_CE)
8260 && (attr == 0
8261 || (norm_term
8262 && attr <= HL_ALL
8263 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8264 {
8265 /*
8266 * check if we really need to clear something
8267 */
8268 col = start_col;
8269 if (c1 != ' ') /* don't clear first char */
8270 ++col;
8271
8272 off = LineOffset[row] + col;
8273 end_off = LineOffset[row] + end_col;
8274
8275 /* skip blanks (used often, keep it fast!) */
8276#ifdef FEAT_MBYTE
8277 if (enc_utf8)
8278 while (off < end_off && ScreenLines[off] == ' '
8279 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8280 ++off;
8281 else
8282#endif
8283 while (off < end_off && ScreenLines[off] == ' '
8284 && ScreenAttrs[off] == 0)
8285 ++off;
8286 if (off < end_off) /* something to be cleared */
8287 {
8288 col = off - LineOffset[row];
8289 screen_stop_highlight();
8290 term_windgoto(row, col);/* clear rest of this screen line */
8291 out_str(T_CE);
8292 screen_start(); /* don't know where cursor is now */
8293 col = end_col - col;
8294 while (col--) /* clear chars in ScreenLines */
8295 {
8296 ScreenLines[off] = ' ';
8297#ifdef FEAT_MBYTE
8298 if (enc_utf8)
8299 ScreenLinesUC[off] = 0;
8300#endif
8301 ScreenAttrs[off] = 0;
8302 ++off;
8303 }
8304 }
8305 did_delete = TRUE; /* the chars are cleared now */
8306 }
8307
8308 off = LineOffset[row] + start_col;
8309 c = c1;
8310 for (col = start_col; col < end_col; ++col)
8311 {
8312 if (ScreenLines[off] != c
8313#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008314 || (enc_utf8 && (int)ScreenLinesUC[off]
8315 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316#endif
8317 || ScreenAttrs[off] != attr
8318#if defined(FEAT_GUI) || defined(UNIX)
8319 || force_next
8320#endif
8321 )
8322 {
8323#if defined(FEAT_GUI) || defined(UNIX)
8324 /* The bold trick may make a single row of pixels appear in
8325 * the next character. When a bold character is removed, the
8326 * next character should be redrawn too. This happens for our
8327 * own GUI and for some xterms. */
8328 if (
8329# ifdef FEAT_GUI
8330 gui.in_use
8331# endif
8332# if defined(FEAT_GUI) && defined(UNIX)
8333 ||
8334# endif
8335# ifdef UNIX
8336 term_is_xterm
8337# endif
8338 )
8339 {
8340 if (ScreenLines[off] != ' '
8341 && (ScreenAttrs[off] > HL_ALL
8342 || ScreenAttrs[off] & HL_BOLD))
8343 force_next = TRUE;
8344 else
8345 force_next = FALSE;
8346 }
8347#endif
8348 ScreenLines[off] = c;
8349#ifdef FEAT_MBYTE
8350 if (enc_utf8)
8351 {
8352 if (c >= 0x80)
8353 {
8354 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008355 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357 else
8358 ScreenLinesUC[off] = 0;
8359 }
8360#endif
8361 ScreenAttrs[off] = attr;
8362 if (!did_delete || c != ' ')
8363 screen_char(off, row, col);
8364 }
8365 ++off;
8366 if (col == start_col)
8367 {
8368 if (did_delete)
8369 break;
8370 c = c2;
8371 }
8372 }
8373 if (end_col == Columns)
8374 LineWraps[row] = FALSE;
8375 if (row == Rows - 1) /* overwritten the command line */
8376 {
8377 redraw_cmdline = TRUE;
8378 if (c1 == ' ' && c2 == ' ')
8379 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008380 if (start_col == 0)
8381 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 }
8384}
8385
8386/*
8387 * Check if there should be a delay. Used before clearing or redrawing the
8388 * screen or the command line.
8389 */
8390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008391check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
8393 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8394 && !did_wait_return
8395 && emsg_silent == 0)
8396 {
8397 out_flush();
8398 ui_delay(1000L, TRUE);
8399 emsg_on_display = FALSE;
8400 if (check_msg_scroll)
8401 msg_scroll = FALSE;
8402 }
8403}
8404
8405/*
8406 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008407 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 * Returns TRUE if there is a valid screen to write to.
8409 * Returns FALSE when starting up and screen not initialized yet.
8410 */
8411 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008412screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008414 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 return (ScreenLines != NULL);
8416}
8417
8418/*
8419 * Resize the shell to Rows and Columns.
8420 * Allocate ScreenLines[] and associated items.
8421 *
8422 * There may be some time between setting Rows and Columns and (re)allocating
8423 * ScreenLines[]. This happens when starting up and when (manually) changing
8424 * the shell size. Always use screen_Rows and screen_Columns to access items
8425 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8426 * final size of the shell is needed.
8427 */
8428 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008429screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 int new_row, old_row;
8432#ifdef FEAT_GUI
8433 int old_Rows;
8434#endif
8435 win_T *wp;
8436 int outofmem = FALSE;
8437 int len;
8438 schar_T *new_ScreenLines;
8439#ifdef FEAT_MBYTE
8440 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008441 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008443 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444#endif
8445 sattr_T *new_ScreenAttrs;
8446 unsigned *new_LineOffset;
8447 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008448#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008449 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008450 tabpage_T *tp;
8451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008453 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008454#ifdef FEAT_AUTOCMD
8455 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008457retry:
8458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 /*
8460 * Allocation of the screen buffers is done only when the size changes and
8461 * when Rows and Columns have been set and we have started doing full
8462 * screen stuff.
8463 */
8464 if ((ScreenLines != NULL
8465 && Rows == screen_Rows
8466 && Columns == screen_Columns
8467#ifdef FEAT_MBYTE
8468 && enc_utf8 == (ScreenLinesUC != NULL)
8469 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008470 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#endif
8472 )
8473 || Rows == 0
8474 || Columns == 0
8475 || (!full_screen && ScreenLines == NULL))
8476 return;
8477
8478 /*
8479 * It's possible that we produce an out-of-memory message below, which
8480 * will cause this function to be called again. To break the loop, just
8481 * return here.
8482 */
8483 if (entered)
8484 return;
8485 entered = TRUE;
8486
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008487 /*
8488 * Note that the window sizes are updated before reallocating the arrays,
8489 * thus we must not redraw here!
8490 */
8491 ++RedrawingDisabled;
8492
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 win_new_shellsize(); /* fit the windows in the new sized shell */
8494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 comp_col(); /* recompute columns for shown command and ruler */
8496
8497 /*
8498 * We're changing the size of the screen.
8499 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8500 * - Move lines from the old arrays into the new arrays, clear extra
8501 * lines (unless the screen is going to be cleared).
8502 * - Free the old arrays.
8503 *
8504 * If anything fails, make ScreenLines NULL, so we don't do anything!
8505 * Continuing with the old ScreenLines may result in a crash, because the
8506 * size is wrong.
8507 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008508 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008510#ifdef FEAT_AUTOCMD
8511 if (aucmd_win != NULL)
8512 win_free_lsize(aucmd_win);
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
8515 new_ScreenLines = (schar_T *)lalloc((long_u)(
8516 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8517#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008518 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (enc_utf8)
8520 {
8521 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8522 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008523 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008524 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8526 }
8527 if (enc_dbcs == DBCS_JPNU)
8528 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8529 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8530#endif
8531 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8532 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8533 new_LineOffset = (unsigned *)lalloc((long_u)(
8534 Rows * sizeof(unsigned)), FALSE);
8535 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008536#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008537 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008540 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
8542 if (win_alloc_lines(wp) == FAIL)
8543 {
8544 outofmem = TRUE;
8545#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008546 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547#endif
8548 }
8549 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008550#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008551 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8552 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008553 outofmem = TRUE;
8554#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008555#ifdef FEAT_WINDOWS
8556give_up:
8557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008559#ifdef FEAT_MBYTE
8560 for (i = 0; i < p_mco; ++i)
8561 if (new_ScreenLinesC[i] == NULL)
8562 break;
8563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (new_ScreenLines == NULL
8565#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008566 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8568#endif
8569 || new_ScreenAttrs == NULL
8570 || new_LineOffset == NULL
8571 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008572#ifdef FEAT_WINDOWS
8573 || new_TabPageIdxs == NULL
8574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 || outofmem)
8576 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008577 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008578 {
8579 /* guess the size */
8580 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8581
8582 /* Remember we did this to avoid getting outofmem messages over
8583 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008584 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 vim_free(new_ScreenLines);
8587 new_ScreenLines = NULL;
8588#ifdef FEAT_MBYTE
8589 vim_free(new_ScreenLinesUC);
8590 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008591 for (i = 0; i < p_mco; ++i)
8592 {
8593 vim_free(new_ScreenLinesC[i]);
8594 new_ScreenLinesC[i] = NULL;
8595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 vim_free(new_ScreenLines2);
8597 new_ScreenLines2 = NULL;
8598#endif
8599 vim_free(new_ScreenAttrs);
8600 new_ScreenAttrs = NULL;
8601 vim_free(new_LineOffset);
8602 new_LineOffset = NULL;
8603 vim_free(new_LineWraps);
8604 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008605#ifdef FEAT_WINDOWS
8606 vim_free(new_TabPageIdxs);
8607 new_TabPageIdxs = NULL;
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
8610 else
8611 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008612 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 for (new_row = 0; new_row < Rows; ++new_row)
8615 {
8616 new_LineOffset[new_row] = new_row * Columns;
8617 new_LineWraps[new_row] = FALSE;
8618
8619 /*
8620 * If the screen is not going to be cleared, copy as much as
8621 * possible from the old screen to the new one and clear the rest
8622 * (used when resizing the window at the "--more--" prompt or when
8623 * executing an external command, for the GUI).
8624 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008625 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {
8627 (void)vim_memset(new_ScreenLines + new_row * Columns,
8628 ' ', (size_t)Columns * sizeof(schar_T));
8629#ifdef FEAT_MBYTE
8630 if (enc_utf8)
8631 {
8632 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8633 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008634 for (i = 0; i < p_mco; ++i)
8635 (void)vim_memset(new_ScreenLinesC[i]
8636 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 0, (size_t)Columns * sizeof(u8char_T));
8638 }
8639 if (enc_dbcs == DBCS_JPNU)
8640 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8641 0, (size_t)Columns * sizeof(schar_T));
8642#endif
8643 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8644 0, (size_t)Columns * sizeof(sattr_T));
8645 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008646 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 {
8648 if (screen_Columns < Columns)
8649 len = screen_Columns;
8650 else
8651 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008652#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008653 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008654 * may be invalid now. Also when p_mco changes. */
8655 if (!(enc_utf8 && ScreenLinesUC == NULL)
8656 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008657#endif
8658 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8659 ScreenLines + LineOffset[old_row],
8660 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008662 if (enc_utf8 && ScreenLinesUC != NULL
8663 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {
8665 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8666 ScreenLinesUC + LineOffset[old_row],
8667 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008668 for (i = 0; i < p_mco; ++i)
8669 mch_memmove(new_ScreenLinesC[i]
8670 + new_LineOffset[new_row],
8671 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 (size_t)len * sizeof(u8char_T));
8673 }
8674 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8675 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8676 ScreenLines2 + LineOffset[old_row],
8677 (size_t)len * sizeof(schar_T));
8678#endif
8679 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8680 ScreenAttrs + LineOffset[old_row],
8681 (size_t)len * sizeof(sattr_T));
8682 }
8683 }
8684 }
8685 /* Use the last line of the screen for the current line. */
8686 current_ScreenLine = new_ScreenLines + Rows * Columns;
8687 }
8688
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008689 free_screenlines();
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 ScreenLines = new_ScreenLines;
8692#ifdef FEAT_MBYTE
8693 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008694 for (i = 0; i < p_mco; ++i)
8695 ScreenLinesC[i] = new_ScreenLinesC[i];
8696 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 ScreenLines2 = new_ScreenLines2;
8698#endif
8699 ScreenAttrs = new_ScreenAttrs;
8700 LineOffset = new_LineOffset;
8701 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008702#ifdef FEAT_WINDOWS
8703 TabPageIdxs = new_TabPageIdxs;
8704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
8706 /* It's important that screen_Rows and screen_Columns reflect the actual
8707 * size of ScreenLines[]. Set them before calling anything. */
8708#ifdef FEAT_GUI
8709 old_Rows = screen_Rows;
8710#endif
8711 screen_Rows = Rows;
8712 screen_Columns = Columns;
8713
8714 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008715 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 screenclear2();
8717
8718#ifdef FEAT_GUI
8719 else if (gui.in_use
8720 && !gui.starting
8721 && ScreenLines != NULL
8722 && old_Rows != Rows)
8723 {
8724 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8725 /*
8726 * Adjust the position of the cursor, for when executing an external
8727 * command.
8728 */
8729 if (msg_row >= Rows) /* Rows got smaller */
8730 msg_row = Rows - 1; /* put cursor at last row */
8731 else if (Rows > old_Rows) /* Rows got bigger */
8732 msg_row += Rows - old_Rows; /* put cursor in same place */
8733 if (msg_col >= Columns) /* Columns got smaller */
8734 msg_col = Columns - 1; /* put cursor at last column */
8735 }
8736#endif
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008739 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008740
8741#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008742 /*
8743 * Do not apply autocommands more than 3 times to avoid an endless loop
8744 * in case applying autocommands always changes Rows or Columns.
8745 */
8746 if (starting == 0 && ++retry_count <= 3)
8747 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008748 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008749 /* In rare cases, autocommands may have altered Rows or Columns,
8750 * jump back to check if we need to allocate the screen again. */
8751 goto retry;
8752 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754}
8755
8756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008757free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008758{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008759#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008760 int i;
8761
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008762 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008763 for (i = 0; i < Screen_mco; ++i)
8764 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008765 vim_free(ScreenLines2);
8766#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008767 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008768 vim_free(ScreenAttrs);
8769 vim_free(LineOffset);
8770 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008771#ifdef FEAT_WINDOWS
8772 vim_free(TabPageIdxs);
8773#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008774}
8775
8776 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008777screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 check_for_delay(FALSE);
8780 screenalloc(FALSE); /* allocate screen buffers if size changed */
8781 screenclear2(); /* clear the screen */
8782}
8783
8784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008785screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
8787 int i;
8788
8789 if (starting == NO_SCREEN || ScreenLines == NULL
8790#ifdef FEAT_GUI
8791 || (gui.in_use && gui.starting)
8792#endif
8793 )
8794 return;
8795
8796#ifdef FEAT_GUI
8797 if (!gui.in_use)
8798#endif
8799 screen_attr = -1; /* force setting the Normal colors */
8800 screen_stop_highlight(); /* don't want highlighting here */
8801
8802#ifdef FEAT_CLIPBOARD
8803 /* disable selection without redrawing it */
8804 clip_scroll_selection(9999);
8805#endif
8806
8807 /* blank out ScreenLines */
8808 for (i = 0; i < Rows; ++i)
8809 {
8810 lineclear(LineOffset[i], (int)Columns);
8811 LineWraps[i] = FALSE;
8812 }
8813
8814 if (can_clear(T_CL))
8815 {
8816 out_str(T_CL); /* clear the display */
8817 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008818 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 }
8820 else
8821 {
8822 /* can't clear the screen, mark all chars with invalid attributes */
8823 for (i = 0; i < Rows; ++i)
8824 lineinvalid(LineOffset[i], (int)Columns);
8825 clear_cmdline = TRUE;
8826 }
8827
8828 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8829
8830 win_rest_invalid(firstwin);
8831 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008832#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008833 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if (must_redraw == CLEAR) /* no need to clear again */
8836 must_redraw = NOT_VALID;
8837 compute_cmdrow();
8838 msg_row = cmdline_row; /* put cursor on last line for messages */
8839 msg_col = 0;
8840 screen_start(); /* don't know where cursor is now */
8841 msg_scrolled = 0; /* can't scroll back */
8842 msg_didany = FALSE;
8843 msg_didout = FALSE;
8844}
8845
8846/*
8847 * Clear one line in ScreenLines.
8848 */
8849 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008850lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8853#ifdef FEAT_MBYTE
8854 if (enc_utf8)
8855 (void)vim_memset(ScreenLinesUC + off, 0,
8856 (size_t)width * sizeof(u8char_T));
8857#endif
8858 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8859}
8860
8861/*
8862 * Mark one line in ScreenLines invalid by setting the attributes to an
8863 * invalid value.
8864 */
8865 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008866lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8869}
8870
8871#ifdef FEAT_VERTSPLIT
8872/*
8873 * Copy part of a Screenline for vertically split window "wp".
8874 */
8875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008876linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878 unsigned off_to = LineOffset[to] + wp->w_wincol;
8879 unsigned off_from = LineOffset[from] + wp->w_wincol;
8880
8881 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8882 wp->w_width * sizeof(schar_T));
8883# ifdef FEAT_MBYTE
8884 if (enc_utf8)
8885 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 int i;
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8889 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008890 for (i = 0; i < p_mco; ++i)
8891 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8892 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894 if (enc_dbcs == DBCS_JPNU)
8895 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8896 wp->w_width * sizeof(schar_T));
8897# endif
8898 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8899 wp->w_width * sizeof(sattr_T));
8900}
8901#endif
8902
8903/*
8904 * Return TRUE if clearing with term string "p" would work.
8905 * It can't work when the string is empty or it won't set the right background.
8906 */
8907 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008908can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 return (*p != NUL && (t_colors <= 1
8911#ifdef FEAT_GUI
8912 || gui.in_use
8913#endif
8914 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8915}
8916
8917/*
8918 * Reset cursor position. Use whenever cursor was moved because of outputting
8919 * something directly to the screen (shell commands) or a terminal control
8920 * code.
8921 */
8922 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008923screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 screen_cur_row = screen_cur_col = 9999;
8926}
8927
8928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 * Move the cursor to position "row","col" in the screen.
8930 * This tries to find the most efficient way to move, minimizing the number of
8931 * characters sent to the terminal.
8932 */
8933 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008934windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008936 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 int i;
8938 int plan;
8939 int cost;
8940 int wouldbe_col;
8941 int noinvcurs;
8942 char_u *bs;
8943 int goto_cost;
8944 int attr;
8945
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008946#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8948
8949#define PLAN_LE 1
8950#define PLAN_CR 2
8951#define PLAN_NL 3
8952#define PLAN_WRITE 4
8953 /* Can't use ScreenLines unless initialized */
8954 if (ScreenLines == NULL)
8955 return;
8956
8957 if (col != screen_cur_col || row != screen_cur_row)
8958 {
8959 /* Check for valid position. */
8960 if (row < 0) /* window without text lines? */
8961 row = 0;
8962 if (row >= screen_Rows)
8963 row = screen_Rows - 1;
8964 if (col >= screen_Columns)
8965 col = screen_Columns - 1;
8966
8967 /* check if no cursor movement is allowed in highlight mode */
8968 if (screen_attr && *T_MS == NUL)
8969 noinvcurs = HIGHL_COST;
8970 else
8971 noinvcurs = 0;
8972 goto_cost = GOTO_COST + noinvcurs;
8973
8974 /*
8975 * Plan how to do the positioning:
8976 * 1. Use CR to move it to column 0, same row.
8977 * 2. Use T_LE to move it a few columns to the left.
8978 * 3. Use NL to move a few lines down, column 0.
8979 * 4. Move a few columns to the right with T_ND or by writing chars.
8980 *
8981 * Don't do this if the cursor went beyond the last column, the cursor
8982 * position is unknown then (some terminals wrap, some don't )
8983 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008984 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 * characters to move the cursor to the right.
8986 */
8987 if (row >= screen_cur_row && screen_cur_col < Columns)
8988 {
8989 /*
8990 * If the cursor is in the same row, bigger col, we can use CR
8991 * or T_LE.
8992 */
8993 bs = NULL; /* init for GCC */
8994 attr = screen_attr;
8995 if (row == screen_cur_row && col < screen_cur_col)
8996 {
8997 /* "le" is preferred over "bc", because "bc" is obsolete */
8998 if (*T_LE)
8999 bs = T_LE; /* "cursor left" */
9000 else
9001 bs = T_BC; /* "backspace character (old) */
9002 if (*bs)
9003 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9004 else
9005 cost = 999;
9006 if (col + 1 < cost) /* using CR is less characters */
9007 {
9008 plan = PLAN_CR;
9009 wouldbe_col = 0;
9010 cost = 1; /* CR is just one character */
9011 }
9012 else
9013 {
9014 plan = PLAN_LE;
9015 wouldbe_col = col;
9016 }
9017 if (noinvcurs) /* will stop highlighting */
9018 {
9019 cost += noinvcurs;
9020 attr = 0;
9021 }
9022 }
9023
9024 /*
9025 * If the cursor is above where we want to be, we can use CR LF.
9026 */
9027 else if (row > screen_cur_row)
9028 {
9029 plan = PLAN_NL;
9030 wouldbe_col = 0;
9031 cost = (row - screen_cur_row) * 2; /* CR LF */
9032 if (noinvcurs) /* will stop highlighting */
9033 {
9034 cost += noinvcurs;
9035 attr = 0;
9036 }
9037 }
9038
9039 /*
9040 * If the cursor is in the same row, smaller col, just use write.
9041 */
9042 else
9043 {
9044 plan = PLAN_WRITE;
9045 wouldbe_col = screen_cur_col;
9046 cost = 0;
9047 }
9048
9049 /*
9050 * Check if any characters that need to be written have the
9051 * correct attributes. Also avoid UTF-8 characters.
9052 */
9053 i = col - wouldbe_col;
9054 if (i > 0)
9055 cost += i;
9056 if (cost < goto_cost && i > 0)
9057 {
9058 /*
9059 * Check if the attributes are correct without additionally
9060 * stopping highlighting.
9061 */
9062 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9063 while (i && *p++ == attr)
9064 --i;
9065 if (i != 0)
9066 {
9067 /*
9068 * Try if it works when highlighting is stopped here.
9069 */
9070 if (*--p == 0)
9071 {
9072 cost += noinvcurs;
9073 while (i && *p++ == 0)
9074 --i;
9075 }
9076 if (i != 0)
9077 cost = 999; /* different attributes, don't do it */
9078 }
9079#ifdef FEAT_MBYTE
9080 if (enc_utf8)
9081 {
9082 /* Don't use an UTF-8 char for positioning, it's slow. */
9083 for (i = wouldbe_col; i < col; ++i)
9084 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9085 {
9086 cost = 999;
9087 break;
9088 }
9089 }
9090#endif
9091 }
9092
9093 /*
9094 * We can do it without term_windgoto()!
9095 */
9096 if (cost < goto_cost)
9097 {
9098 if (plan == PLAN_LE)
9099 {
9100 if (noinvcurs)
9101 screen_stop_highlight();
9102 while (screen_cur_col > col)
9103 {
9104 out_str(bs);
9105 --screen_cur_col;
9106 }
9107 }
9108 else if (plan == PLAN_CR)
9109 {
9110 if (noinvcurs)
9111 screen_stop_highlight();
9112 out_char('\r');
9113 screen_cur_col = 0;
9114 }
9115 else if (plan == PLAN_NL)
9116 {
9117 if (noinvcurs)
9118 screen_stop_highlight();
9119 while (screen_cur_row < row)
9120 {
9121 out_char('\n');
9122 ++screen_cur_row;
9123 }
9124 screen_cur_col = 0;
9125 }
9126
9127 i = col - screen_cur_col;
9128 if (i > 0)
9129 {
9130 /*
9131 * Use cursor-right if it's one character only. Avoids
9132 * removing a line of pixels from the last bold char, when
9133 * using the bold trick in the GUI.
9134 */
9135 if (T_ND[0] != NUL && T_ND[1] == NUL)
9136 {
9137 while (i-- > 0)
9138 out_char(*T_ND);
9139 }
9140 else
9141 {
9142 int off;
9143
9144 off = LineOffset[row] + screen_cur_col;
9145 while (i-- > 0)
9146 {
9147 if (ScreenAttrs[off] != screen_attr)
9148 screen_stop_highlight();
9149#ifdef FEAT_MBYTE
9150 out_flush_check();
9151#endif
9152 out_char(ScreenLines[off]);
9153#ifdef FEAT_MBYTE
9154 if (enc_dbcs == DBCS_JPNU
9155 && ScreenLines[off] == 0x8e)
9156 out_char(ScreenLines2[off]);
9157#endif
9158 ++off;
9159 }
9160 }
9161 }
9162 }
9163 }
9164 else
9165 cost = 999;
9166
9167 if (cost >= goto_cost)
9168 {
9169 if (noinvcurs)
9170 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009171 if (row == screen_cur_row && (col > screen_cur_col)
9172 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 term_cursor_right(col - screen_cur_col);
9174 else
9175 term_windgoto(row, col);
9176 }
9177 screen_cur_row = row;
9178 screen_cur_col = col;
9179 }
9180}
9181
9182/*
9183 * Set cursor to its position in the current window.
9184 */
9185 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009186setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 if (redrawing())
9189 {
9190 validate_cursor();
9191 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9192 W_WINCOL(curwin) + (
9193#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009194 /* With 'rightleft' set and the cursor on a double-wide
9195 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9197# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009198 (has_mbyte
9199 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9200 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201# endif
9202 1)) :
9203#endif
9204 curwin->w_wcol));
9205 }
9206}
9207
9208
9209/*
9210 * insert 'line_count' lines at 'row' in window 'wp'
9211 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9212 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9213 * scrolling.
9214 * Returns FAIL if the lines are not inserted, OK for success.
9215 */
9216 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009217win_ins_lines(
9218 win_T *wp,
9219 int row,
9220 int line_count,
9221 int invalid,
9222 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int did_delete;
9225 int nextrow;
9226 int lastrow;
9227 int retval;
9228
9229 if (invalid)
9230 wp->w_lines_valid = 0;
9231
9232 if (wp->w_height < 5)
9233 return FAIL;
9234
9235 if (line_count > wp->w_height - row)
9236 line_count = wp->w_height - row;
9237
9238 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9239 if (retval != MAYBE)
9240 return retval;
9241
9242 /*
9243 * If there is a next window or a status line, we first try to delete the
9244 * lines at the bottom to avoid messing what is after the window.
9245 * If this fails and there are following windows, don't do anything to avoid
9246 * messing up those windows, better just redraw.
9247 */
9248 did_delete = FALSE;
9249#ifdef FEAT_WINDOWS
9250 if (wp->w_next != NULL || wp->w_status_height)
9251 {
9252 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9253 line_count, (int)Rows, FALSE, NULL) == OK)
9254 did_delete = TRUE;
9255 else if (wp->w_next)
9256 return FAIL;
9257 }
9258#endif
9259 /*
9260 * if no lines deleted, blank the lines that will end up below the window
9261 */
9262 if (!did_delete)
9263 {
9264#ifdef FEAT_WINDOWS
9265 wp->w_redr_status = TRUE;
9266#endif
9267 redraw_cmdline = TRUE;
9268 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9269 lastrow = nextrow + line_count;
9270 if (lastrow > Rows)
9271 lastrow = Rows;
9272 screen_fill(nextrow - line_count, lastrow - line_count,
9273 W_WINCOL(wp), (int)W_ENDCOL(wp),
9274 ' ', ' ', 0);
9275 }
9276
9277 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9278 == FAIL)
9279 {
9280 /* deletion will have messed up other windows */
9281 if (did_delete)
9282 {
9283#ifdef FEAT_WINDOWS
9284 wp->w_redr_status = TRUE;
9285#endif
9286 win_rest_invalid(W_NEXT(wp));
9287 }
9288 return FAIL;
9289 }
9290
9291 return OK;
9292}
9293
9294/*
9295 * delete "line_count" window lines at "row" in window "wp"
9296 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9297 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9298 * scrolling
9299 * Return OK for success, FAIL if the lines are not deleted.
9300 */
9301 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009302win_del_lines(
9303 win_T *wp,
9304 int row,
9305 int line_count,
9306 int invalid,
9307 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 int retval;
9310
9311 if (invalid)
9312 wp->w_lines_valid = 0;
9313
9314 if (line_count > wp->w_height - row)
9315 line_count = wp->w_height - row;
9316
9317 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9318 if (retval != MAYBE)
9319 return retval;
9320
9321 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9322 (int)Rows, FALSE, NULL) == FAIL)
9323 return FAIL;
9324
9325#ifdef FEAT_WINDOWS
9326 /*
9327 * If there are windows or status lines below, try to put them at the
9328 * correct place. If we can't do that, they have to be redrawn.
9329 */
9330 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9331 {
9332 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9333 line_count, (int)Rows, NULL) == FAIL)
9334 {
9335 wp->w_redr_status = TRUE;
9336 win_rest_invalid(wp->w_next);
9337 }
9338 }
9339 /*
9340 * If this is the last window and there is no status line, redraw the
9341 * command line later.
9342 */
9343 else
9344#endif
9345 redraw_cmdline = TRUE;
9346 return OK;
9347}
9348
9349/*
9350 * Common code for win_ins_lines() and win_del_lines().
9351 * Returns OK or FAIL when the work has been done.
9352 * Returns MAYBE when not finished yet.
9353 */
9354 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009355win_do_lines(
9356 win_T *wp,
9357 int row,
9358 int line_count,
9359 int mayclear,
9360 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
9362 int retval;
9363
9364 if (!redrawing() || line_count <= 0)
9365 return FAIL;
9366
9367 /* only a few lines left: redraw is faster */
9368 if (mayclear && Rows - line_count < 5
9369#ifdef FEAT_VERTSPLIT
9370 && wp->w_width == Columns
9371#endif
9372 )
9373 {
9374 screenclear(); /* will set wp->w_lines_valid to 0 */
9375 return FAIL;
9376 }
9377
9378 /*
9379 * Delete all remaining lines
9380 */
9381 if (row + line_count >= wp->w_height)
9382 {
9383 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9384 W_WINCOL(wp), (int)W_ENDCOL(wp),
9385 ' ', ' ', 0);
9386 return OK;
9387 }
9388
9389 /*
9390 * when scrolling, the message on the command line should be cleared,
9391 * otherwise it will stay there forever.
9392 */
9393 clear_cmdline = TRUE;
9394
9395 /*
9396 * If the terminal can set a scroll region, use that.
9397 * Always do this in a vertically split window. This will redraw from
9398 * ScreenLines[] when t_CV isn't defined. That's faster than using
9399 * win_line().
9400 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009401 * a character in the lower right corner of the scroll region may cause a
9402 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 */
9404 if (scroll_region
9405#ifdef FEAT_VERTSPLIT
9406 || W_WIDTH(wp) != Columns
9407#endif
9408 )
9409 {
9410#ifdef FEAT_VERTSPLIT
9411 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9412#endif
9413 scroll_region_set(wp, row);
9414 if (del)
9415 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9416 wp->w_height - row, FALSE, wp);
9417 else
9418 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9419 wp->w_height - row, wp);
9420#ifdef FEAT_VERTSPLIT
9421 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9422#endif
9423 scroll_region_reset();
9424 return retval;
9425 }
9426
9427#ifdef FEAT_WINDOWS
9428 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9429 return FAIL;
9430#endif
9431
9432 return MAYBE;
9433}
9434
9435/*
9436 * window 'wp' and everything after it is messed up, mark it for redraw
9437 */
9438 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009439win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441#ifdef FEAT_WINDOWS
9442 while (wp != NULL)
9443#else
9444 if (wp != NULL)
9445#endif
9446 {
9447 redraw_win_later(wp, NOT_VALID);
9448#ifdef FEAT_WINDOWS
9449 wp->w_redr_status = TRUE;
9450 wp = wp->w_next;
9451#endif
9452 }
9453 redraw_cmdline = TRUE;
9454}
9455
9456/*
9457 * The rest of the routines in this file perform screen manipulations. The
9458 * given operation is performed physically on the screen. The corresponding
9459 * change is also made to the internal screen image. In this way, the editor
9460 * anticipates the effect of editing changes on the appearance of the screen.
9461 * That way, when we call screenupdate a complete redraw isn't usually
9462 * necessary. Another advantage is that we can keep adding code to anticipate
9463 * screen changes, and in the meantime, everything still works.
9464 */
9465
9466/*
9467 * types for inserting or deleting lines
9468 */
9469#define USE_T_CAL 1
9470#define USE_T_CDL 2
9471#define USE_T_AL 3
9472#define USE_T_CE 4
9473#define USE_T_DL 5
9474#define USE_T_SR 6
9475#define USE_NL 7
9476#define USE_T_CD 8
9477#define USE_REDRAW 9
9478
9479/*
9480 * insert lines on the screen and update ScreenLines[]
9481 * 'end' is the line after the scrolled part. Normally it is Rows.
9482 * When scrolling region used 'off' is the offset from the top for the region.
9483 * 'row' and 'end' are relative to the start of the region.
9484 *
9485 * return FAIL for failure, OK for success.
9486 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009488screen_ins_lines(
9489 int off,
9490 int row,
9491 int line_count,
9492 int end,
9493 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 int i;
9496 int j;
9497 unsigned temp;
9498 int cursor_row;
9499 int type;
9500 int result_empty;
9501 int can_ce = can_clear(T_CE);
9502
9503 /*
9504 * FAIL if
9505 * - there is no valid screen
9506 * - the screen has to be redrawn completely
9507 * - the line count is less than one
9508 * - the line count is more than 'ttyscroll'
9509 */
9510 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9511 return FAIL;
9512
9513 /*
9514 * There are seven ways to insert lines:
9515 * 0. When in a vertically split window and t_CV isn't set, redraw the
9516 * characters from ScreenLines[].
9517 * 1. Use T_CD (clear to end of display) if it exists and the result of
9518 * the insert is just empty lines
9519 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9520 * present or line_count > 1. It looks better if we do all the inserts
9521 * at once.
9522 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9523 * insert is just empty lines and T_CE is not present or line_count >
9524 * 1.
9525 * 4. Use T_AL (insert line) if it exists.
9526 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9527 * just empty lines.
9528 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9529 * just empty lines.
9530 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9531 * the 'da' flag is not set or we have clear line capability.
9532 * 8. redraw the characters from ScreenLines[].
9533 *
9534 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9535 * the scrollbar for the window. It does have insert line, use that if it
9536 * exists.
9537 */
9538 result_empty = (row + line_count >= end);
9539#ifdef FEAT_VERTSPLIT
9540 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9541 type = USE_REDRAW;
9542 else
9543#endif
9544 if (can_clear(T_CD) && result_empty)
9545 type = USE_T_CD;
9546 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9547 type = USE_T_CAL;
9548 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9549 type = USE_T_CDL;
9550 else if (*T_AL != NUL)
9551 type = USE_T_AL;
9552 else if (can_ce && result_empty)
9553 type = USE_T_CE;
9554 else if (*T_DL != NUL && result_empty)
9555 type = USE_T_DL;
9556 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9557 type = USE_T_SR;
9558 else
9559 return FAIL;
9560
9561 /*
9562 * For clearing the lines screen_del_lines() is used. This will also take
9563 * care of t_db if necessary.
9564 */
9565 if (type == USE_T_CD || type == USE_T_CDL ||
9566 type == USE_T_CE || type == USE_T_DL)
9567 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9568
9569 /*
9570 * If text is retained below the screen, first clear or delete as many
9571 * lines at the bottom of the window as are about to be inserted so that
9572 * the deleted lines won't later surface during a screen_del_lines.
9573 */
9574 if (*T_DB)
9575 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9576
9577#ifdef FEAT_CLIPBOARD
9578 /* Remove a modeless selection when inserting lines halfway the screen
9579 * or not the full width of the screen. */
9580 if (off + row > 0
9581# ifdef FEAT_VERTSPLIT
9582 || (wp != NULL && wp->w_width != Columns)
9583# endif
9584 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009585 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 else
9587 clip_scroll_selection(-line_count);
9588#endif
9589
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590#ifdef FEAT_GUI
9591 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9592 * scrolling is actually carried out. */
9593 gui_dont_update_cursor();
9594#endif
9595
9596 if (*T_CCS != NUL) /* cursor relative to region */
9597 cursor_row = row;
9598 else
9599 cursor_row = row + off;
9600
9601 /*
9602 * Shift LineOffset[] line_count down to reflect the inserted lines.
9603 * Clear the inserted lines in ScreenLines[].
9604 */
9605 row += off;
9606 end += off;
9607 for (i = 0; i < line_count; ++i)
9608 {
9609#ifdef FEAT_VERTSPLIT
9610 if (wp != NULL && wp->w_width != Columns)
9611 {
9612 /* need to copy part of a line */
9613 j = end - 1 - i;
9614 while ((j -= line_count) >= row)
9615 linecopy(j + line_count, j, wp);
9616 j += line_count;
9617 if (can_clear((char_u *)" "))
9618 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9619 else
9620 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9621 LineWraps[j] = FALSE;
9622 }
9623 else
9624#endif
9625 {
9626 j = end - 1 - i;
9627 temp = LineOffset[j];
9628 while ((j -= line_count) >= row)
9629 {
9630 LineOffset[j + line_count] = LineOffset[j];
9631 LineWraps[j + line_count] = LineWraps[j];
9632 }
9633 LineOffset[j + line_count] = temp;
9634 LineWraps[j + line_count] = FALSE;
9635 if (can_clear((char_u *)" "))
9636 lineclear(temp, (int)Columns);
9637 else
9638 lineinvalid(temp, (int)Columns);
9639 }
9640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641
9642 screen_stop_highlight();
9643 windgoto(cursor_row, 0);
9644
9645#ifdef FEAT_VERTSPLIT
9646 /* redraw the characters */
9647 if (type == USE_REDRAW)
9648 redraw_block(row, end, wp);
9649 else
9650#endif
9651 if (type == USE_T_CAL)
9652 {
9653 term_append_lines(line_count);
9654 screen_start(); /* don't know where cursor is now */
9655 }
9656 else
9657 {
9658 for (i = 0; i < line_count; i++)
9659 {
9660 if (type == USE_T_AL)
9661 {
9662 if (i && cursor_row != 0)
9663 windgoto(cursor_row, 0);
9664 out_str(T_AL);
9665 }
9666 else /* type == USE_T_SR */
9667 out_str(T_SR);
9668 screen_start(); /* don't know where cursor is now */
9669 }
9670 }
9671
9672 /*
9673 * With scroll-reverse and 'da' flag set we need to clear the lines that
9674 * have been scrolled down into the region.
9675 */
9676 if (type == USE_T_SR && *T_DA)
9677 {
9678 for (i = 0; i < line_count; ++i)
9679 {
9680 windgoto(off + i, 0);
9681 out_str(T_CE);
9682 screen_start(); /* don't know where cursor is now */
9683 }
9684 }
9685
9686#ifdef FEAT_GUI
9687 gui_can_update_cursor();
9688 if (gui.in_use)
9689 out_flush(); /* always flush after a scroll */
9690#endif
9691 return OK;
9692}
9693
9694/*
9695 * delete lines on the screen and update ScreenLines[]
9696 * 'end' is the line after the scrolled part. Normally it is Rows.
9697 * When scrolling region used 'off' is the offset from the top for the region.
9698 * 'row' and 'end' are relative to the start of the region.
9699 *
9700 * Return OK for success, FAIL if the lines are not deleted.
9701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009703screen_del_lines(
9704 int off,
9705 int row,
9706 int line_count,
9707 int end,
9708 int force, /* even when line_count > p_ttyscroll */
9709 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 int j;
9712 int i;
9713 unsigned temp;
9714 int cursor_row;
9715 int cursor_end;
9716 int result_empty; /* result is empty until end of region */
9717 int can_delete; /* deleting line codes can be used */
9718 int type;
9719
9720 /*
9721 * FAIL if
9722 * - there is no valid screen
9723 * - the screen has to be redrawn completely
9724 * - the line count is less than one
9725 * - the line count is more than 'ttyscroll'
9726 */
9727 if (!screen_valid(TRUE) || line_count <= 0 ||
9728 (!force && line_count > p_ttyscroll))
9729 return FAIL;
9730
9731 /*
9732 * Check if the rest of the current region will become empty.
9733 */
9734 result_empty = row + line_count >= end;
9735
9736 /*
9737 * We can delete lines only when 'db' flag not set or when 'ce' option
9738 * available.
9739 */
9740 can_delete = (*T_DB == NUL || can_clear(T_CE));
9741
9742 /*
9743 * There are six ways to delete lines:
9744 * 0. When in a vertically split window and t_CV isn't set, redraw the
9745 * characters from ScreenLines[].
9746 * 1. Use T_CD if it exists and the result is empty.
9747 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9748 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9749 * none of the other ways work.
9750 * 4. Use T_CE (erase line) if the result is empty.
9751 * 5. Use T_DL (delete line) if it exists.
9752 * 6. redraw the characters from ScreenLines[].
9753 */
9754#ifdef FEAT_VERTSPLIT
9755 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9756 type = USE_REDRAW;
9757 else
9758#endif
9759 if (can_clear(T_CD) && result_empty)
9760 type = USE_T_CD;
9761#if defined(__BEOS__) && defined(BEOS_DR8)
9762 /*
9763 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9764 * its internal termcap... this works okay for tests which test *T_DB !=
9765 * NUL. It has the disadvantage that the user cannot use any :set t_*
9766 * command to get T_DB (back) to empty_option, only :set term=... will do
9767 * the trick...
9768 * Anyway, this hack will hopefully go away with the next OS release.
9769 * (Olaf Seibert)
9770 */
9771 else if (row == 0 && T_DB == empty_option
9772 && (line_count == 1 || *T_CDL == NUL))
9773#else
9774 else if (row == 0 && (
9775#ifndef AMIGA
9776 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9777 * up, so use delete-line command */
9778 line_count == 1 ||
9779#endif
9780 *T_CDL == NUL))
9781#endif
9782 type = USE_NL;
9783 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9784 type = USE_T_CDL;
9785 else if (can_clear(T_CE) && result_empty
9786#ifdef FEAT_VERTSPLIT
9787 && (wp == NULL || wp->w_width == Columns)
9788#endif
9789 )
9790 type = USE_T_CE;
9791 else if (*T_DL != NUL && can_delete)
9792 type = USE_T_DL;
9793 else if (*T_CDL != NUL && can_delete)
9794 type = USE_T_CDL;
9795 else
9796 return FAIL;
9797
9798#ifdef FEAT_CLIPBOARD
9799 /* Remove a modeless selection when deleting lines halfway the screen or
9800 * not the full width of the screen. */
9801 if (off + row > 0
9802# ifdef FEAT_VERTSPLIT
9803 || (wp != NULL && wp->w_width != Columns)
9804# endif
9805 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009806 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 else
9808 clip_scroll_selection(line_count);
9809#endif
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811#ifdef FEAT_GUI
9812 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9813 * scrolling is actually carried out. */
9814 gui_dont_update_cursor();
9815#endif
9816
9817 if (*T_CCS != NUL) /* cursor relative to region */
9818 {
9819 cursor_row = row;
9820 cursor_end = end;
9821 }
9822 else
9823 {
9824 cursor_row = row + off;
9825 cursor_end = end + off;
9826 }
9827
9828 /*
9829 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9830 * Clear the inserted lines in ScreenLines[].
9831 */
9832 row += off;
9833 end += off;
9834 for (i = 0; i < line_count; ++i)
9835 {
9836#ifdef FEAT_VERTSPLIT
9837 if (wp != NULL && wp->w_width != Columns)
9838 {
9839 /* need to copy part of a line */
9840 j = row + i;
9841 while ((j += line_count) <= end - 1)
9842 linecopy(j - line_count, j, wp);
9843 j -= line_count;
9844 if (can_clear((char_u *)" "))
9845 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9846 else
9847 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9848 LineWraps[j] = FALSE;
9849 }
9850 else
9851#endif
9852 {
9853 /* whole width, moving the line pointers is faster */
9854 j = row + i;
9855 temp = LineOffset[j];
9856 while ((j += line_count) <= end - 1)
9857 {
9858 LineOffset[j - line_count] = LineOffset[j];
9859 LineWraps[j - line_count] = LineWraps[j];
9860 }
9861 LineOffset[j - line_count] = temp;
9862 LineWraps[j - line_count] = FALSE;
9863 if (can_clear((char_u *)" "))
9864 lineclear(temp, (int)Columns);
9865 else
9866 lineinvalid(temp, (int)Columns);
9867 }
9868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 screen_stop_highlight();
9871
9872#ifdef FEAT_VERTSPLIT
9873 /* redraw the characters */
9874 if (type == USE_REDRAW)
9875 redraw_block(row, end, wp);
9876 else
9877#endif
9878 if (type == USE_T_CD) /* delete the lines */
9879 {
9880 windgoto(cursor_row, 0);
9881 out_str(T_CD);
9882 screen_start(); /* don't know where cursor is now */
9883 }
9884 else if (type == USE_T_CDL)
9885 {
9886 windgoto(cursor_row, 0);
9887 term_delete_lines(line_count);
9888 screen_start(); /* don't know where cursor is now */
9889 }
9890 /*
9891 * Deleting lines at top of the screen or scroll region: Just scroll
9892 * the whole screen (scroll region) up by outputting newlines on the
9893 * last line.
9894 */
9895 else if (type == USE_NL)
9896 {
9897 windgoto(cursor_end - 1, 0);
9898 for (i = line_count; --i >= 0; )
9899 out_char('\n'); /* cursor will remain on same line */
9900 }
9901 else
9902 {
9903 for (i = line_count; --i >= 0; )
9904 {
9905 if (type == USE_T_DL)
9906 {
9907 windgoto(cursor_row, 0);
9908 out_str(T_DL); /* delete a line */
9909 }
9910 else /* type == USE_T_CE */
9911 {
9912 windgoto(cursor_row + i, 0);
9913 out_str(T_CE); /* erase a line */
9914 }
9915 screen_start(); /* don't know where cursor is now */
9916 }
9917 }
9918
9919 /*
9920 * If the 'db' flag is set, we need to clear the lines that have been
9921 * scrolled up at the bottom of the region.
9922 */
9923 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9924 {
9925 for (i = line_count; i > 0; --i)
9926 {
9927 windgoto(cursor_end - i, 0);
9928 out_str(T_CE); /* erase a line */
9929 screen_start(); /* don't know where cursor is now */
9930 }
9931 }
9932
9933#ifdef FEAT_GUI
9934 gui_can_update_cursor();
9935 if (gui.in_use)
9936 out_flush(); /* always flush after a scroll */
9937#endif
9938
9939 return OK;
9940}
9941
9942/*
9943 * show the current mode and ruler
9944 *
9945 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9946 * If clear_cmdline is FALSE there may be a message there that needs to be
9947 * cleared only if a mode is shown.
9948 * Return the length of the message (0 if no message).
9949 */
9950 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009951showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952{
9953 int need_clear;
9954 int length = 0;
9955 int do_mode;
9956 int attr;
9957 int nwr_save;
9958#ifdef FEAT_INS_EXPAND
9959 int sub_attr;
9960#endif
9961
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009962 do_mode = ((p_smd && msg_silent == 0)
9963 && ((State & INSERT)
9964 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009965 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 if (do_mode || Recording)
9967 {
9968 /*
9969 * Don't show mode right now, when not redrawing or inside a mapping.
9970 * Call char_avail() only when we are going to show something, because
9971 * it takes a bit of time.
9972 */
9973 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9974 {
9975 redraw_cmdline = TRUE; /* show mode later */
9976 return 0;
9977 }
9978
9979 nwr_save = need_wait_return;
9980
9981 /* wait a bit before overwriting an important message */
9982 check_for_delay(FALSE);
9983
9984 /* if the cmdline is more than one line high, erase top lines */
9985 need_clear = clear_cmdline;
9986 if (clear_cmdline && cmdline_row < Rows - 1)
9987 msg_clr_cmdline(); /* will reset clear_cmdline */
9988
9989 /* Position on the last line in the window, column 0 */
9990 msg_pos_mode();
9991 cursor_off();
9992 attr = hl_attr(HLF_CM); /* Highlight mode */
9993 if (do_mode)
9994 {
9995 MSG_PUTS_ATTR("--", attr);
9996#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009997 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009998# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009999 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010000# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010001 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010002# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010003 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010004# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 MSG_PUTS_ATTR(" IM", attr);
10006# else
10007 MSG_PUTS_ATTR(" XIM", attr);
10008# endif
10009#endif
10010#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10011 if (gui.in_use)
10012 {
10013 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010014 {
10015 /* HANGUL */
10016 if (enc_utf8)
10017 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10018 else
10019 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 }
10022#endif
10023#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010024 /* CTRL-X in Insert mode */
10025 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
10027 /* These messages can get long, avoid a wrap in a narrow
10028 * window. Prefer showing edit_submode_extra. */
10029 length = (Rows - msg_row) * Columns - 3;
10030 if (edit_submode_extra != NULL)
10031 length -= vim_strsize(edit_submode_extra);
10032 if (length > 0)
10033 {
10034 if (edit_submode_pre != NULL)
10035 length -= vim_strsize(edit_submode_pre);
10036 if (length - vim_strsize(edit_submode) > 0)
10037 {
10038 if (edit_submode_pre != NULL)
10039 msg_puts_attr(edit_submode_pre, attr);
10040 msg_puts_attr(edit_submode, attr);
10041 }
10042 if (edit_submode_extra != NULL)
10043 {
10044 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10045 if ((int)edit_submode_highl < (int)HLF_COUNT)
10046 sub_attr = hl_attr(edit_submode_highl);
10047 else
10048 sub_attr = attr;
10049 msg_puts_attr(edit_submode_extra, sub_attr);
10050 }
10051 }
10052 length = 0;
10053 }
10054 else
10055#endif
10056 {
10057#ifdef FEAT_VREPLACE
10058 if (State & VREPLACE_FLAG)
10059 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10060 else
10061#endif
10062 if (State & REPLACE_FLAG)
10063 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10064 else if (State & INSERT)
10065 {
10066#ifdef FEAT_RIGHTLEFT
10067 if (p_ri)
10068 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10069#endif
10070 MSG_PUTS_ATTR(_(" INSERT"), attr);
10071 }
10072 else if (restart_edit == 'I')
10073 MSG_PUTS_ATTR(_(" (insert)"), attr);
10074 else if (restart_edit == 'R')
10075 MSG_PUTS_ATTR(_(" (replace)"), attr);
10076 else if (restart_edit == 'V')
10077 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10078#ifdef FEAT_RIGHTLEFT
10079 if (p_hkmap)
10080 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10081# ifdef FEAT_FKMAP
10082 if (p_fkmap)
10083 MSG_PUTS_ATTR(farsi_text_5, attr);
10084# endif
10085#endif
10086#ifdef FEAT_KEYMAP
10087 if (State & LANGMAP)
10088 {
10089# ifdef FEAT_ARABIC
10090 if (curwin->w_p_arab)
10091 MSG_PUTS_ATTR(_(" Arabic"), attr);
10092 else
10093# endif
10094 MSG_PUTS_ATTR(_(" (lang)"), attr);
10095 }
10096#endif
10097 if ((State & INSERT) && p_paste)
10098 MSG_PUTS_ATTR(_(" (paste)"), attr);
10099
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (VIsual_active)
10101 {
10102 char *p;
10103
10104 /* Don't concatenate separate words to avoid translation
10105 * problems. */
10106 switch ((VIsual_select ? 4 : 0)
10107 + (VIsual_mode == Ctrl_V) * 2
10108 + (VIsual_mode == 'V'))
10109 {
10110 case 0: p = N_(" VISUAL"); break;
10111 case 1: p = N_(" VISUAL LINE"); break;
10112 case 2: p = N_(" VISUAL BLOCK"); break;
10113 case 4: p = N_(" SELECT"); break;
10114 case 5: p = N_(" SELECT LINE"); break;
10115 default: p = N_(" SELECT BLOCK"); break;
10116 }
10117 MSG_PUTS_ATTR(_(p), attr);
10118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 MSG_PUTS_ATTR(" --", attr);
10120 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010121
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 need_clear = TRUE;
10123 }
10124 if (Recording
10125#ifdef FEAT_INS_EXPAND
10126 && edit_submode == NULL /* otherwise it gets too long */
10127#endif
10128 )
10129 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010130 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 need_clear = TRUE;
10132 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010133
10134 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 if (need_clear || clear_cmdline)
10136 msg_clr_eos();
10137 msg_didout = FALSE; /* overwrite this message */
10138 length = msg_col;
10139 msg_col = 0;
10140 need_wait_return = nwr_save; /* never ask for hit-return for this */
10141 }
10142 else if (clear_cmdline && msg_silent == 0)
10143 /* Clear the whole command line. Will reset "clear_cmdline". */
10144 msg_clr_cmdline();
10145
10146#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 /* In Visual mode the size of the selected area must be redrawn. */
10148 if (VIsual_active)
10149 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150
10151 /* If the last window has no status line, the ruler is after the mode
10152 * message and must be redrawn */
10153 if (redrawing()
10154# ifdef FEAT_WINDOWS
10155 && lastwin->w_status_height == 0
10156# endif
10157 )
10158 win_redr_ruler(lastwin, TRUE);
10159#endif
10160 redraw_cmdline = FALSE;
10161 clear_cmdline = FALSE;
10162
10163 return length;
10164}
10165
10166/*
10167 * Position for a mode message.
10168 */
10169 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010170msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 msg_col = 0;
10173 msg_row = Rows - 1;
10174}
10175
10176/*
10177 * Delete mode message. Used when ESC is typed which is expected to end
10178 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010179 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 */
10181 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010182unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010185 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 */
10187 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10188 redraw_cmdline = TRUE; /* delete mode later */
10189 else
10190 {
10191 msg_pos_mode();
10192 if (Recording)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010193 recording_mode(hl_attr(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 msg_clr_eos();
10195 }
10196}
10197
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010198 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010199recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010200{
10201 MSG_PUTS_ATTR(_("recording"), attr);
10202 if (!shortmess(SHM_RECORDING))
10203 {
10204 char_u s[4];
10205 sprintf((char *)s, " @%c", Recording);
10206 MSG_PUTS_ATTR(s, attr);
10207 }
10208}
10209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010210#if defined(FEAT_WINDOWS)
10211/*
10212 * Draw the tab pages line at the top of the Vim window.
10213 */
10214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010215draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010216{
10217 int tabcount = 0;
10218 tabpage_T *tp;
10219 int tabwidth;
10220 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010221 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010222 int attr;
10223 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010224 win_T *cwp;
10225 int wincount;
10226 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010227 int c;
10228 int len;
10229 int attr_sel = hl_attr(HLF_TPS);
10230 int attr_nosel = hl_attr(HLF_TP);
10231 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010232 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010233 int room;
10234 int use_sep_chars = (t_colors < 8
10235#ifdef FEAT_GUI
10236 && !gui.in_use
10237#endif
10238 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010239
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010240 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010241
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010242#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010243 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010244 if (gui_use_tabline())
10245 {
10246 gui_update_tabline();
10247 return;
10248 }
10249#endif
10250
10251 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010252 return;
10253
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010254#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010255
10256 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10257 for (scol = 0; scol < Columns; ++scol)
10258 TabPageIdxs[scol] = 0;
10259
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010260 /* Use the 'tabline' option if it's set. */
10261 if (*p_tal != NUL)
10262 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010263 int save_called_emsg = called_emsg;
10264
10265 /* Check for an error. If there is one we would loop in redrawing the
10266 * screen. Avoid that by making 'tabline' empty. */
10267 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010268 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010269 if (called_emsg)
10270 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010271 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010272 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010273 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010275#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010276 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010277 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10278 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010279
Bram Moolenaar238a5642006-02-21 22:12:05 +000010280 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10281 if (tabwidth < 6)
10282 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010283
Bram Moolenaar238a5642006-02-21 22:12:05 +000010284 attr = attr_nosel;
10285 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010286 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010287 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10288 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010289 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010290 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010291
Bram Moolenaar238a5642006-02-21 22:12:05 +000010292 if (tp->tp_topframe == topframe)
10293 attr = attr_sel;
10294 if (use_sep_chars && col > 0)
10295 screen_putchar('|', 0, col++, attr);
10296
10297 if (tp->tp_topframe != topframe)
10298 attr = attr_nosel;
10299
10300 screen_putchar(' ', 0, col++, attr);
10301
10302 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010303 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010304 cwp = curwin;
10305 wp = firstwin;
10306 }
10307 else
10308 {
10309 cwp = tp->tp_curwin;
10310 wp = tp->tp_firstwin;
10311 }
10312
10313 modified = FALSE;
10314 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10315 if (bufIsChanged(wp->w_buffer))
10316 modified = TRUE;
10317 if (modified || wincount > 1)
10318 {
10319 if (wincount > 1)
10320 {
10321 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010322 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010323 if (col + len >= Columns - 3)
10324 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010325 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010326#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010327 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010328#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010329 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010330#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331 );
10332 col += len;
10333 }
10334 if (modified)
10335 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10336 screen_putchar(' ', 0, col++, attr);
10337 }
10338
10339 room = scol - col + tabwidth - 1;
10340 if (room > 0)
10341 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010342 /* Get buffer name in NameBuff[] */
10343 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010344 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010345 len = vim_strsize(NameBuff);
10346 p = NameBuff;
10347#ifdef FEAT_MBYTE
10348 if (has_mbyte)
10349 while (len > room)
10350 {
10351 len -= ptr2cells(p);
10352 mb_ptr_adv(p);
10353 }
10354 else
10355#endif
10356 if (len > room)
10357 {
10358 p += len - room;
10359 len = room;
10360 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010361 if (len > Columns - col - 1)
10362 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010363
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010364 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010365 col += len;
10366 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010367 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010368
10369 /* Store the tab page number in TabPageIdxs[], so that
10370 * jump_to_mouse() knows where each one is. */
10371 ++tabcount;
10372 while (scol < col)
10373 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010374 }
10375
Bram Moolenaar238a5642006-02-21 22:12:05 +000010376 if (use_sep_chars)
10377 c = '_';
10378 else
10379 c = ' ';
10380 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010381
10382 /* Put an "X" for closing the current tab if there are several. */
10383 if (first_tabpage->tp_next != NULL)
10384 {
10385 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10386 TabPageIdxs[Columns - 1] = -999;
10387 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010388 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010389
10390 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10391 * set. */
10392 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010393}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010394
10395/*
10396 * Get buffer name for "buf" into NameBuff[].
10397 * Takes care of special buffer names and translates special characters.
10398 */
10399 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010400get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010401{
10402 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010403 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010404 else
10405 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10406 trans_characters(NameBuff, MAXPATHL);
10407}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010408#endif
10409
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10411/*
10412 * Get the character to use in a status line. Get its attributes in "*attr".
10413 */
10414 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010415fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 int fill;
10418 if (is_curwin)
10419 {
10420 *attr = hl_attr(HLF_S);
10421 fill = fill_stl;
10422 }
10423 else
10424 {
10425 *attr = hl_attr(HLF_SNC);
10426 fill = fill_stlnc;
10427 }
10428 /* Use fill when there is highlighting, and highlighting of current
10429 * window differs, or the fillchars differ, or this is not the
10430 * current window */
10431 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10432 || !is_curwin || firstwin == lastwin)
10433 || (fill_stl != fill_stlnc)))
10434 return fill;
10435 if (is_curwin)
10436 return '^';
10437 return '=';
10438}
10439#endif
10440
10441#ifdef FEAT_VERTSPLIT
10442/*
10443 * Get the character to use in a separator between vertically split windows.
10444 * Get its attributes in "*attr".
10445 */
10446 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010447fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448{
10449 *attr = hl_attr(HLF_C);
10450 if (*attr == 0 && fill_vert == ' ')
10451 return '|';
10452 else
10453 return fill_vert;
10454}
10455#endif
10456
10457/*
10458 * Return TRUE if redrawing should currently be done.
10459 */
10460 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010461redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462{
10463 return (!RedrawingDisabled
10464 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10465}
10466
10467/*
10468 * Return TRUE if printing messages should currently be done.
10469 */
10470 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010471messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 return (!(p_lz && char_avail() && !KeyTyped));
10474}
10475
10476/*
10477 * Show current status info in ruler and various other places
10478 * If always is FALSE, only show ruler if position has changed.
10479 */
10480 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010481showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482{
10483 if (!always && !redrawing())
10484 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010485#ifdef FEAT_INS_EXPAND
10486 if (pum_visible())
10487 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010488# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010489 /* Don't redraw right now, do it later. */
10490 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010491# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010492 return;
10493 }
10494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010496 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010497 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010498 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 else
10501#endif
10502#ifdef FEAT_CMDL_INFO
10503 win_redr_ruler(curwin, always);
10504#endif
10505
10506#ifdef FEAT_TITLE
10507 if (need_maketitle
10508# ifdef FEAT_STL_OPT
10509 || (p_icon && (stl_syntax & STL_IN_ICON))
10510 || (p_title && (stl_syntax & STL_IN_TITLE))
10511# endif
10512 )
10513 maketitle();
10514#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010515#ifdef FEAT_WINDOWS
10516 /* Redraw the tab pages line if needed. */
10517 if (redraw_tabline)
10518 draw_tabline();
10519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520}
10521
10522#ifdef FEAT_CMDL_INFO
10523 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010524win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010526#define RULER_BUF_LEN 70
10527 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 int row;
10529 int fillchar;
10530 int attr;
10531 int empty_line = FALSE;
10532 colnr_T virtcol;
10533 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010534 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 int o;
10536#ifdef FEAT_VERTSPLIT
10537 int this_ru_col;
10538 int off = 0;
10539 int width = Columns;
10540# define WITH_OFF(x) x
10541# define WITH_WIDTH(x) x
10542#else
10543# define WITH_OFF(x) 0
10544# define WITH_WIDTH(x) Columns
10545# define this_ru_col ru_col
10546#endif
10547
10548 /* If 'ruler' off or redrawing disabled, don't do anything */
10549 if (!p_ru)
10550 return;
10551
10552 /*
10553 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10554 * after deleting lines, before cursor.lnum is corrected.
10555 */
10556 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10557 return;
10558
10559#ifdef FEAT_INS_EXPAND
10560 /* Don't draw the ruler while doing insert-completion, it might overwrite
10561 * the (long) mode message. */
10562# ifdef FEAT_WINDOWS
10563 if (wp == lastwin && lastwin->w_status_height == 0)
10564# endif
10565 if (edit_submode != NULL)
10566 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010567 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10568 if (pum_visible())
10569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570#endif
10571
10572#ifdef FEAT_STL_OPT
10573 if (*p_ruf)
10574 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010575 int save_called_emsg = called_emsg;
10576
10577 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010579 if (called_emsg)
10580 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010581 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010582 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 return;
10584 }
10585#endif
10586
10587 /*
10588 * Check if not in Insert mode and the line is empty (will show "0-1").
10589 */
10590 if (!(State & INSERT)
10591 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10592 empty_line = TRUE;
10593
10594 /*
10595 * Only draw the ruler when something changed.
10596 */
10597 validate_virtcol_win(wp);
10598 if ( redraw_cmdline
10599 || always
10600 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10601 || wp->w_cursor.col != wp->w_ru_cursor.col
10602 || wp->w_virtcol != wp->w_ru_virtcol
10603#ifdef FEAT_VIRTUALEDIT
10604 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10605#endif
10606 || wp->w_topline != wp->w_ru_topline
10607 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10608#ifdef FEAT_DIFF
10609 || wp->w_topfill != wp->w_ru_topfill
10610#endif
10611 || empty_line != wp->w_ru_empty)
10612 {
10613 cursor_off();
10614#ifdef FEAT_WINDOWS
10615 if (wp->w_status_height)
10616 {
10617 row = W_WINROW(wp) + wp->w_height;
10618 fillchar = fillchar_status(&attr, wp == curwin);
10619# ifdef FEAT_VERTSPLIT
10620 off = W_WINCOL(wp);
10621 width = W_WIDTH(wp);
10622# endif
10623 }
10624 else
10625#endif
10626 {
10627 row = Rows - 1;
10628 fillchar = ' ';
10629 attr = 0;
10630#ifdef FEAT_VERTSPLIT
10631 width = Columns;
10632 off = 0;
10633#endif
10634 }
10635
10636 /* In list mode virtcol needs to be recomputed */
10637 virtcol = wp->w_virtcol;
10638 if (wp->w_p_list && lcs_tab1 == NUL)
10639 {
10640 wp->w_p_list = FALSE;
10641 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10642 wp->w_p_list = TRUE;
10643 }
10644
10645 /*
10646 * Some sprintfs return the length, some return a pointer.
10647 * To avoid portability problems we use strlen() here.
10648 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010649 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10651 ? 0L
10652 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010653 len = STRLEN(buffer);
10654 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10656 (int)virtcol + 1);
10657
10658 /*
10659 * Add a "50%" if there is room for it.
10660 * On the last line, don't print in the last column (scrolls the
10661 * screen up on some terminals).
10662 */
10663 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010664 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 o = i + vim_strsize(buffer + i + 1);
10666#ifdef FEAT_WINDOWS
10667 if (wp->w_status_height == 0) /* can't use last char of screen */
10668#endif
10669 ++o;
10670#ifdef FEAT_VERTSPLIT
10671 this_ru_col = ru_col - (Columns - width);
10672 if (this_ru_col < 0)
10673 this_ru_col = 0;
10674#endif
10675 /* Never use more than half the window/screen width, leave the other
10676 * half for the filename. */
10677 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10678 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10679 if (this_ru_col + o < WITH_WIDTH(width))
10680 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010681 /* need at least 3 chars left for get_rel_pos() + NUL */
10682 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 {
10684#ifdef FEAT_MBYTE
10685 if (has_mbyte)
10686 i += (*mb_char2bytes)(fillchar, buffer + i);
10687 else
10688#endif
10689 buffer[i++] = fillchar;
10690 ++o;
10691 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010692 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 }
10694 /* Truncate at window boundary. */
10695#ifdef FEAT_MBYTE
10696 if (has_mbyte)
10697 {
10698 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010699 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 {
10701 o += (*mb_ptr2cells)(buffer + i);
10702 if (this_ru_col + o > WITH_WIDTH(width))
10703 {
10704 buffer[i] = NUL;
10705 break;
10706 }
10707 }
10708 }
10709 else
10710#endif
10711 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10712 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10713
10714 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10715 i = redraw_cmdline;
10716 screen_fill(row, row + 1,
10717 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10718 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10719 fillchar, fillchar, attr);
10720 /* don't redraw the cmdline because of showing the ruler */
10721 redraw_cmdline = i;
10722 wp->w_ru_cursor = wp->w_cursor;
10723 wp->w_ru_virtcol = wp->w_virtcol;
10724 wp->w_ru_empty = empty_line;
10725 wp->w_ru_topline = wp->w_topline;
10726 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10727#ifdef FEAT_DIFF
10728 wp->w_ru_topfill = wp->w_topfill;
10729#endif
10730 }
10731}
10732#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010733
10734#if defined(FEAT_LINEBREAK) || defined(PROTO)
10735/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010736 * Return the width of the 'number' and 'relativenumber' column.
10737 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010738 * Otherwise it depends on 'numberwidth' and the line count.
10739 */
10740 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010741number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010742{
10743 int n;
10744 linenr_T lnum;
10745
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010746 if (wp->w_p_rnu && !wp->w_p_nu)
10747 /* cursor line shows "0" */
10748 lnum = wp->w_height;
10749 else
10750 /* cursor line shows absolute line number */
10751 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010752
Bram Moolenaar6b314672015-03-20 15:42:10 +010010753 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010754 return wp->w_nrwidth_width;
10755 wp->w_nrwidth_line_count = lnum;
10756
10757 n = 0;
10758 do
10759 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010760 lnum /= 10;
10761 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010762 } while (lnum > 0);
10763
10764 /* 'numberwidth' gives the minimal width plus one */
10765 if (n < wp->w_p_nuw - 1)
10766 n = wp->w_p_nuw - 1;
10767
10768 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010769 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010770 return n;
10771}
10772#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010773
10774/*
10775 * Return the current cursor column. This is the actual position on the
10776 * screen. First column is 0.
10777 */
10778 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010779screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010780{
10781 return screen_cur_col;
10782}
10783
10784/*
10785 * Return the current cursor row. This is the actual position on the screen.
10786 * First row is 0.
10787 */
10788 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010789screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010790{
10791 return screen_cur_row;
10792}