blob: c7b46ee4ec60fcd2ffda66d0ab262acb0d25e3fe [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 Moolenaar071d4272004-06-13 20:20:40 +0000414 * Changed something in the current window, at buffer line "lnum", that
415 * requires that line and possibly other lines to be redrawn.
416 * Used when entering/leaving Insert mode with the cursor on a folded line.
417 * Used to remove the "$" from a change command.
418 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
419 * may become invalid and the whole window will have to be redrawn.
420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100422redrawWinline(
423 linenr_T lnum,
424 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
426#ifdef FEAT_FOLDING
427 int i;
428#endif
429
430 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
431 curwin->w_redraw_top = lnum;
432 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
433 curwin->w_redraw_bot = lnum;
434 redraw_later(VALID);
435
436#ifdef FEAT_FOLDING
437 if (invalid)
438 {
439 /* A w_lines[] entry for this lnum has become invalid. */
440 i = find_wl_entry(curwin, lnum);
441 if (i >= 0)
442 curwin->w_lines[i].wl_valid = FALSE;
443 }
444#endif
445}
446
447/*
448 * update all windows that are editing the current buffer
449 */
450 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100451update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 redraw_curbuf_later(type);
454 update_screen(type);
455}
456
457/*
458 * update_screen()
459 *
460 * Based on the current value of curwin->w_topline, transfer a screenfull
461 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
462 */
463 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100464update_screen(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 win_T *wp;
467 static int did_intro = FALSE;
468#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
469 int did_one;
470#endif
471
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000472 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 if (!screen_valid(TRUE))
474 return;
475
476 if (must_redraw)
477 {
478 if (type < must_redraw) /* use maximal type */
479 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000480
481 /* must_redraw is reset here, so that when we run into some weird
482 * reason to redraw while busy redrawing (e.g., asynchronous
483 * scrolling), or update_topline() in win_update() will cause a
484 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 must_redraw = 0;
486 }
487
488 /* Need to update w_lines[]. */
489 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
490 type = NOT_VALID;
491
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000492 /* Postpone the redrawing when it's not needed and when being called
493 * recursively. */
494 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 {
496 redraw_later(type); /* remember type for next time */
497 must_redraw = type;
498 if (type > INVERTED_ALL)
499 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
500 return;
501 }
502
503 updating_screen = TRUE;
504#ifdef FEAT_SYN_HL
505 ++display_tick; /* let syntax code know we're in a next round of
506 * display updating */
507#endif
508
509 /*
510 * if the screen was scrolled up when displaying a message, scroll it down
511 */
512 if (msg_scrolled)
513 {
514 clear_cmdline = TRUE;
515 if (msg_scrolled > Rows - 5) /* clearing is faster */
516 type = CLEAR;
517 else if (type != CLEAR)
518 {
519 check_for_delay(FALSE);
520 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
521 type = CLEAR;
522 FOR_ALL_WINDOWS(wp)
523 {
524 if (W_WINROW(wp) < msg_scrolled)
525 {
526 if (W_WINROW(wp) + wp->w_height > msg_scrolled
527 && wp->w_redr_type < REDRAW_TOP
528 && wp->w_lines_valid > 0
529 && wp->w_topline == wp->w_lines[0].wl_lnum)
530 {
531 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
532 wp->w_redr_type = REDRAW_TOP;
533 }
534 else
535 {
536 wp->w_redr_type = NOT_VALID;
537#ifdef FEAT_WINDOWS
538 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
539 <= msg_scrolled)
540 wp->w_redr_status = TRUE;
541#endif
542 }
543 }
544 }
545 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000546#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000547 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550 msg_scrolled = 0;
551 need_wait_return = FALSE;
552 }
553
554 /* reset cmdline_row now (may have been changed temporarily) */
555 compute_cmdrow();
556
557 /* Check for changed highlighting */
558 if (need_highlight_changed)
559 highlight_changed();
560
561 if (type == CLEAR) /* first clear screen */
562 {
563 screenclear(); /* will reset clear_cmdline */
564 type = NOT_VALID;
565 }
566
567 if (clear_cmdline) /* going to clear cmdline (done below) */
568 check_for_delay(FALSE);
569
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000570#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200571 /* Force redraw when width of 'number' or 'relativenumber' column
572 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000573 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200574 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
575 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000576 curwin->w_redr_type = NOT_VALID;
577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 /*
580 * Only start redrawing if there is really something to do.
581 */
582 if (type == INVERTED)
583 update_curswant();
584 if (curwin->w_redr_type < type
585 && !((type == VALID
586 && curwin->w_lines[0].wl_valid
587#ifdef FEAT_DIFF
588 && curwin->w_topfill == curwin->w_old_topfill
589 && curwin->w_botfill == curwin->w_old_botfill
590#endif
591 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000593 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
595 && curwin->w_old_visual_mode == VIsual_mode
596 && (curwin->w_valid & VALID_VIRTCOL)
597 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 ))
599 curwin->w_redr_type = type;
600
Bram Moolenaar5a305422006-04-28 22:38:25 +0000601#ifdef FEAT_WINDOWS
602 /* Redraw the tab pages line if needed. */
603 if (redraw_tabline || type >= NOT_VALID)
604 draw_tabline();
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607#ifdef FEAT_SYN_HL
608 /*
609 * Correct stored syntax highlighting info for changes in each displayed
610 * buffer. Each buffer must only be done once.
611 */
612 FOR_ALL_WINDOWS(wp)
613 {
614 if (wp->w_buffer->b_mod_set)
615 {
616# ifdef FEAT_WINDOWS
617 win_T *wwp;
618
619 /* Check if we already did this buffer. */
620 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
621 if (wwp->w_buffer == wp->w_buffer)
622 break;
623# endif
624 if (
625# ifdef FEAT_WINDOWS
626 wwp == wp &&
627# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200628 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 syn_stack_apply_changes(wp->w_buffer);
630 }
631 }
632#endif
633
634 /*
635 * Go from top to bottom through the windows, redrawing the ones that need
636 * it.
637 */
638#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
639 did_one = FALSE;
640#endif
641#ifdef FEAT_SEARCH_EXTRA
642 search_hl.rm.regprog = NULL;
643#endif
644 FOR_ALL_WINDOWS(wp)
645 {
646 if (wp->w_redr_type != 0)
647 {
648 cursor_off();
649#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
650 if (!did_one)
651 {
652 did_one = TRUE;
653# ifdef FEAT_SEARCH_EXTRA
654 start_search_hl();
655# endif
656# ifdef FEAT_CLIPBOARD
657 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200658 if (clip_star.available && clip_isautosel_star())
659 clip_update_selection(&clip_star);
660 if (clip_plus.available && clip_isautosel_plus())
661 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# endif
663#ifdef FEAT_GUI
664 /* Remove the cursor before starting to do anything, because
665 * scrolling may make it difficult to redraw the text under
666 * it. */
667 if (gui.in_use)
668 gui_undraw_cursor();
669#endif
670 }
671#endif
672 win_update(wp);
673 }
674
675#ifdef FEAT_WINDOWS
676 /* redraw status line after the window to minimize cursor movement */
677 if (wp->w_redr_status)
678 {
679 cursor_off();
680 win_redr_status(wp);
681 }
682#endif
683 }
684#if defined(FEAT_SEARCH_EXTRA)
685 end_search_hl();
686#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100687#ifdef FEAT_INS_EXPAND
688 /* May need to redraw the popup menu. */
689 if (pum_visible())
690 pum_redraw();
691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693#ifdef FEAT_WINDOWS
694 /* Reset b_mod_set flags. Going through all windows is probably faster
695 * than going through all buffers (there could be many buffers). */
696 for (wp = firstwin; wp != NULL; wp = wp->w_next)
697 wp->w_buffer->b_mod_set = FALSE;
698#else
699 curbuf->b_mod_set = FALSE;
700#endif
701
702 updating_screen = FALSE;
703#ifdef FEAT_GUI
704 gui_may_resize_shell();
705#endif
706
707 /* Clear or redraw the command line. Done last, because scrolling may
708 * mess up the command line. */
709 if (clear_cmdline || redraw_cmdline)
710 showmode();
711
712 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200713 if (!did_intro)
714 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 did_intro = TRUE;
716
717#ifdef FEAT_GUI
718 /* Redraw the cursor and update the scrollbars when all screen updating is
719 * done. */
720 if (gui.in_use)
721 {
722 out_flush(); /* required before updating the cursor */
723 if (did_one)
724 gui_update_cursor(FALSE, FALSE);
725 gui_update_scrollbars(FALSE);
726 }
727#endif
728}
729
Bram Moolenaar860cae12010-06-05 23:22:07 +0200730#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200731/*
732 * Return TRUE if the cursor line in window "wp" may be concealed, according
733 * to the 'concealcursor' option.
734 */
735 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100736conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200737{
738 int c;
739
740 if (*wp->w_p_cocu == NUL)
741 return FALSE;
742 if (get_real_state() & VISUAL)
743 c = 'v';
744 else if (State & INSERT)
745 c = 'i';
746 else if (State & NORMAL)
747 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200748 else if (State & CMDLINE)
749 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200750 else
751 return FALSE;
752 return vim_strchr(wp->w_p_cocu, c) != NULL;
753}
754
755/*
756 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
757 */
758 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100759conceal_check_cursur_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200760{
761 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
762 {
763 need_cursor_line_redraw = TRUE;
764 /* Need to recompute cursor column, e.g., when starting Visual mode
765 * without concealing. */
766 curs_columns(TRUE);
767 }
768}
769
Bram Moolenaar860cae12010-06-05 23:22:07 +0200770 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100771update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200772{
773 int row;
774 int j;
775
776 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200777 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200778 {
779# ifdef FEAT_GUI
780 /* Remove the cursor before starting to do anything, because scrolling
781 * may make it difficult to redraw the text under it. */
782 if (gui.in_use)
783 gui_undraw_cursor();
784# endif
785 row = 0;
786 for (j = 0; j < wp->w_lines_valid; ++j)
787 {
788 if (lnum == wp->w_lines[j].wl_lnum)
789 {
790 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200791# ifdef FEAT_SEARCH_EXTRA
792 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200793 start_search_hl();
794 prepare_search_hl(wp, lnum);
795# endif
796 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
797# if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799# endif
800 break;
801 }
802 row += wp->w_lines[j].wl_size;
803 }
804# ifdef FEAT_GUI
805 /* Redraw the cursor */
806 if (gui.in_use)
807 {
808 out_flush(); /* required before updating the cursor */
809 gui_update_cursor(FALSE, FALSE);
810 }
811# endif
812 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200813 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200814}
815#endif
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100818static void update_prepare(void);
819static void update_finish(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
821/*
822 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000823 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 */
825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100826update_prepare(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 cursor_off();
829 updating_screen = TRUE;
830#ifdef FEAT_GUI
831 /* Remove the cursor before starting to do anything, because scrolling may
832 * make it difficult to redraw the text under it. */
833 if (gui.in_use)
834 gui_undraw_cursor();
835#endif
836#ifdef FEAT_SEARCH_EXTRA
837 start_search_hl();
838#endif
839}
840
841/*
842 * Finish updating one or more windows.
843 */
844 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100845update_finish(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846{
847 if (redraw_cmdline)
848 showmode();
849
850# ifdef FEAT_SEARCH_EXTRA
851 end_search_hl();
852# endif
853
854 updating_screen = FALSE;
855
856# ifdef FEAT_GUI
857 gui_may_resize_shell();
858
859 /* Redraw the cursor and update the scrollbars when all screen updating is
860 * done. */
861 if (gui.in_use)
862 {
863 out_flush(); /* required before updating the cursor */
864 gui_update_cursor(FALSE, FALSE);
865 gui_update_scrollbars(FALSE);
866 }
867# endif
868}
869#endif
870
871#if defined(FEAT_SIGNS) || defined(PROTO)
872 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100873update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
875 win_T *wp;
876 int doit = FALSE;
877
878# ifdef FEAT_FOLDING
879 win_foldinfo.fi_level = 0;
880# endif
881
882 /* update/delete a specific mark */
883 FOR_ALL_WINDOWS(wp)
884 {
885 if (buf != NULL && lnum > 0)
886 {
887 if (wp->w_buffer == buf && lnum >= wp->w_topline
888 && lnum < wp->w_botline)
889 {
890 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
891 wp->w_redraw_top = lnum;
892 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
893 wp->w_redraw_bot = lnum;
894 redraw_win_later(wp, VALID);
895 }
896 }
897 else
898 redraw_win_later(wp, VALID);
899 if (wp->w_redr_type != 0)
900 doit = TRUE;
901 }
902
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100903 /* Return when there is nothing to do, screen updating is already
904 * happening (recursive call) or still starting up. */
905 if (!doit || updating_screen
906#ifdef FEAT_GUI
907 || gui.starting
908#endif
909 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 return;
911
912 /* update all windows that need updating */
913 update_prepare();
914
915# ifdef FEAT_WINDOWS
916 for (wp = firstwin; wp; wp = wp->w_next)
917 {
918 if (wp->w_redr_type != 0)
919 win_update(wp);
920 if (wp->w_redr_status)
921 win_redr_status(wp);
922 }
923# else
924 if (curwin->w_redr_type != 0)
925 win_update(curwin);
926# endif
927
928 update_finish();
929}
930#endif
931
932
933#if defined(FEAT_GUI) || defined(PROTO)
934/*
935 * Update a single window, its status line and maybe the command line msg.
936 * Used for the GUI scrollbar.
937 */
938 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100939updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000941 /* return if already busy updating */
942 if (updating_screen)
943 return;
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 update_prepare();
946
947#ifdef FEAT_CLIPBOARD
948 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200949 if (clip_star.available && clip_isautosel_star())
950 clip_update_selection(&clip_star);
951 if (clip_plus.available && clip_isautosel_plus())
952 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000956
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000958 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000959 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000960 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (wp->w_redr_status
963# ifdef FEAT_CMDL_INFO
964 || p_ru
965# endif
966# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000967 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968# endif
969 )
970 win_redr_status(wp);
971#endif
972
973 update_finish();
974}
975#endif
976
977/*
978 * Update a single window.
979 *
980 * This may cause the windows below it also to be redrawn (when clearing the
981 * screen or scrolling lines).
982 *
983 * How the window is redrawn depends on wp->w_redr_type. Each type also
984 * implies the one below it.
985 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000986 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
988 * INVERTED redraw the changed part of the Visual area
989 * INVERTED_ALL redraw the whole Visual area
990 * VALID 1. scroll up/down to adjust for a changed w_topline
991 * 2. update lines at the top when scrolled down
992 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000993 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * b_mod_top and b_mod_bot.
995 * - if wp->w_redraw_top non-zero, redraw lines between
996 * wp->w_redraw_top and wp->w_redr_bot.
997 * - continue redrawing when syntax status is invalid.
998 * 4. if scrolled up, update lines at the bottom.
999 * This results in three areas that may need updating:
1000 * top: from first row to top_end (when scrolled down)
1001 * mid: from mid_start to mid_end (update inversion or changed text)
1002 * bot: from bot_start to last row (when scrolled up)
1003 */
1004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001005win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 buf_T *buf = wp->w_buffer;
1008 int type;
1009 int top_end = 0; /* Below last row of the top area that needs
1010 updating. 0 when no top area updating. */
1011 int mid_start = 999;/* first row of the mid area that needs
1012 updating. 999 when no mid area updating. */
1013 int mid_end = 0; /* Below last row of the mid area that needs
1014 updating. 0 when no mid area updating. */
1015 int bot_start = 999;/* first row of the bot area that needs
1016 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 int scrolled_down = FALSE; /* TRUE when scrolled down when
1018 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001020 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 int top_to_mod = FALSE; /* redraw above mod_top */
1022#endif
1023
1024 int row; /* current window row to display */
1025 linenr_T lnum; /* current buffer lnum to display */
1026 int idx; /* current index in w_lines[] */
1027 int srow; /* starting row of the current line */
1028
1029 int eof = FALSE; /* if TRUE, we hit the end of the file */
1030 int didline = FALSE; /* if TRUE, we finished the last line */
1031 int i;
1032 long j;
1033 static int recursive = FALSE; /* being called recursively */
1034 int old_botline = wp->w_botline;
1035#ifdef FEAT_FOLDING
1036 long fold_count;
1037#endif
1038#ifdef FEAT_SYN_HL
1039 /* remember what happened to the previous line, to know if
1040 * check_visual_highlight() can be used */
1041#define DID_NONE 1 /* didn't update a line */
1042#define DID_LINE 2 /* updated a normal line */
1043#define DID_FOLD 3 /* updated a folded line */
1044 int did_update = DID_NONE;
1045 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1046#endif
1047 linenr_T mod_top = 0;
1048 linenr_T mod_bot = 0;
1049#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1050 int save_got_int;
1051#endif
1052
1053 type = wp->w_redr_type;
1054
1055 if (type == NOT_VALID)
1056 {
1057#ifdef FEAT_WINDOWS
1058 wp->w_redr_status = TRUE;
1059#endif
1060 wp->w_lines_valid = 0;
1061 }
1062
1063 /* Window is zero-height: nothing to draw. */
1064 if (wp->w_height == 0)
1065 {
1066 wp->w_redr_type = 0;
1067 return;
1068 }
1069
1070#ifdef FEAT_VERTSPLIT
1071 /* Window is zero-width: Only need to draw the separator. */
1072 if (wp->w_width == 0)
1073 {
1074 /* draw the vertical separator right of this window */
1075 draw_vsep_win(wp, 0);
1076 wp->w_redr_type = 0;
1077 return;
1078 }
1079#endif
1080
1081#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001082 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
1084
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001085#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001086 /* Force redraw when width of 'number' or 'relativenumber' column
1087 * changes. */
1088 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001089 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001090 {
1091 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001092 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001093 }
1094 else
1095#endif
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1098 {
1099 /*
1100 * When there are both inserted/deleted lines and specific lines to be
1101 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1102 * everything (only happens when redrawing is off for while).
1103 */
1104 type = NOT_VALID;
1105 }
1106 else
1107 {
1108 /*
1109 * Set mod_top to the first line that needs displaying because of
1110 * changes. Set mod_bot to the first line after the changes.
1111 */
1112 mod_top = wp->w_redraw_top;
1113 if (wp->w_redraw_bot != 0)
1114 mod_bot = wp->w_redraw_bot + 1;
1115 else
1116 mod_bot = 0;
1117 wp->w_redraw_top = 0; /* reset for next time */
1118 wp->w_redraw_bot = 0;
1119 if (buf->b_mod_set)
1120 {
1121 if (mod_top == 0 || mod_top > buf->b_mod_top)
1122 {
1123 mod_top = buf->b_mod_top;
1124#ifdef FEAT_SYN_HL
1125 /* Need to redraw lines above the change that may be included
1126 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001127 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001129 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (mod_top < 1)
1131 mod_top = 1;
1132 }
1133#endif
1134 }
1135 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1136 mod_bot = buf->b_mod_bot;
1137
1138#ifdef FEAT_SEARCH_EXTRA
1139 /* When 'hlsearch' is on and using a multi-line search pattern, a
1140 * change in one line may make the Search highlighting in a
1141 * previous line invalid. Simple solution: redraw all visible
1142 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001143 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001145 if (search_hl.rm.regprog != NULL
1146 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001148 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001149 {
1150 cur = wp->w_match_head;
1151 while (cur != NULL)
1152 {
1153 if (cur->match.regprog != NULL
1154 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001155 {
1156 top_to_mod = TRUE;
1157 break;
1158 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001159 cur = cur->next;
1160 }
1161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162#endif
1163 }
1164#ifdef FEAT_FOLDING
1165 if (mod_top != 0 && hasAnyFolding(wp))
1166 {
1167 linenr_T lnumt, lnumb;
1168
1169 /*
1170 * A change in a line can cause lines above it to become folded or
1171 * unfolded. Find the top most buffer line that may be affected.
1172 * If the line was previously folded and displayed, get the first
1173 * line of that fold. If the line is folded now, get the first
1174 * folded line. Use the minimum of these two.
1175 */
1176
1177 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1178 * the line below it. If there is no valid entry, use w_topline.
1179 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1180 * to this line. If there is no valid entry, use MAXLNUM. */
1181 lnumt = wp->w_topline;
1182 lnumb = MAXLNUM;
1183 for (i = 0; i < wp->w_lines_valid; ++i)
1184 if (wp->w_lines[i].wl_valid)
1185 {
1186 if (wp->w_lines[i].wl_lastlnum < mod_top)
1187 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1188 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1189 {
1190 lnumb = wp->w_lines[i].wl_lnum;
1191 /* When there is a fold column it might need updating
1192 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001193 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 ++lnumb;
1195 }
1196 }
1197
1198 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1199 if (mod_top > lnumt)
1200 mod_top = lnumt;
1201
1202 /* Now do the same for the bottom line (one above mod_bot). */
1203 --mod_bot;
1204 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1205 ++mod_bot;
1206 if (mod_bot < lnumb)
1207 mod_bot = lnumb;
1208 }
1209#endif
1210
1211 /* When a change starts above w_topline and the end is below
1212 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001213 * If the end of the change is above w_topline: do like no change was
1214 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (mod_top != 0 && mod_top < wp->w_topline)
1216 {
1217 if (mod_bot > wp->w_topline)
1218 mod_top = wp->w_topline;
1219#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001220 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 top_end = 1;
1222#endif
1223 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224
1225 /* When line numbers are displayed need to redraw all lines below
1226 * inserted/deleted lines. */
1227 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1228 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * When only displaying the lines at the top, set top_end. Used when
1233 * window has scrolled down for msg_scrolled.
1234 */
1235 if (type == REDRAW_TOP)
1236 {
1237 j = 0;
1238 for (i = 0; i < wp->w_lines_valid; ++i)
1239 {
1240 j += wp->w_lines[i].wl_size;
1241 if (j >= wp->w_upd_rows)
1242 {
1243 top_end = j;
1244 break;
1245 }
1246 }
1247 if (top_end == 0)
1248 /* not found (cannot happen?): redraw everything */
1249 type = NOT_VALID;
1250 else
1251 /* top area defined, the rest is VALID */
1252 type = VALID;
1253 }
1254
Bram Moolenaar367329b2007-08-30 11:53:22 +00001255 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001256 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1257 * non-zero and thus not FALSE) will indicate that screenclear() was not
1258 * called. */
1259 if (screen_cleared)
1260 screen_cleared = MAYBE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /*
1263 * If there are no changes on the screen that require a complete redraw,
1264 * handle three cases:
1265 * 1: we are off the top of the screen by a few lines: scroll down
1266 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1267 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1268 * w_lines[] that needs updating.
1269 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001270 if ((type == VALID || type == SOME_VALID
1271 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272#ifdef FEAT_DIFF
1273 && !wp->w_botfill && !wp->w_old_botfill
1274#endif
1275 )
1276 {
1277 if (mod_top != 0 && wp->w_topline == mod_top)
1278 {
1279 /*
1280 * w_topline is the first changed line, the scrolling will be done
1281 * further down.
1282 */
1283 }
1284 else if (wp->w_lines[0].wl_valid
1285 && (wp->w_topline < wp->w_lines[0].wl_lnum
1286#ifdef FEAT_DIFF
1287 || (wp->w_topline == wp->w_lines[0].wl_lnum
1288 && wp->w_topfill > wp->w_old_topfill)
1289#endif
1290 ))
1291 {
1292 /*
1293 * New topline is above old topline: May scroll down.
1294 */
1295#ifdef FEAT_FOLDING
1296 if (hasAnyFolding(wp))
1297 {
1298 linenr_T ln;
1299
1300 /* count the number of lines we are off, counting a sequence
1301 * of folded lines as one */
1302 j = 0;
1303 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1304 {
1305 ++j;
1306 if (j >= wp->w_height - 2)
1307 break;
1308 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1309 }
1310 }
1311 else
1312#endif
1313 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1314 if (j < wp->w_height - 2) /* not too far off */
1315 {
1316 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1317#ifdef FEAT_DIFF
1318 /* insert extra lines for previously invisible filler lines */
1319 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1320 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1321 - wp->w_old_topfill;
1322#endif
1323 if (i < wp->w_height - 2) /* less than a screen off */
1324 {
1325 /*
1326 * Try to insert the correct number of lines.
1327 * If not the last window, delete the lines at the bottom.
1328 * win_ins_lines may fail when the terminal can't do it.
1329 */
1330 if (i > 0)
1331 check_for_delay(FALSE);
1332 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1333 {
1334 if (wp->w_lines_valid != 0)
1335 {
1336 /* Need to update rows that are new, stop at the
1337 * first one that scrolled down. */
1338 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 /* Move the entries that were scrolled, disable
1342 * the entries for the lines to be redrawn. */
1343 if ((wp->w_lines_valid += j) > wp->w_height)
1344 wp->w_lines_valid = wp->w_height;
1345 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1346 wp->w_lines[idx] = wp->w_lines[idx - j];
1347 while (idx >= 0)
1348 wp->w_lines[idx--].wl_valid = FALSE;
1349 }
1350 }
1351 else
1352 mid_start = 0; /* redraw all lines */
1353 }
1354 else
1355 mid_start = 0; /* redraw all lines */
1356 }
1357 else
1358 mid_start = 0; /* redraw all lines */
1359 }
1360 else
1361 {
1362 /*
1363 * New topline is at or below old topline: May scroll up.
1364 * When topline didn't change, find first entry in w_lines[] that
1365 * needs updating.
1366 */
1367
1368 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1369 j = -1;
1370 row = 0;
1371 for (i = 0; i < wp->w_lines_valid; i++)
1372 {
1373 if (wp->w_lines[i].wl_valid
1374 && wp->w_lines[i].wl_lnum == wp->w_topline)
1375 {
1376 j = i;
1377 break;
1378 }
1379 row += wp->w_lines[i].wl_size;
1380 }
1381 if (j == -1)
1382 {
1383 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1384 * lines */
1385 mid_start = 0;
1386 }
1387 else
1388 {
1389 /*
1390 * Try to delete the correct number of lines.
1391 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1392 */
1393#ifdef FEAT_DIFF
1394 /* If the topline didn't change, delete old filler lines,
1395 * otherwise delete filler lines of the new topline... */
1396 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1397 row += wp->w_old_topfill;
1398 else
1399 row += diff_check_fill(wp, wp->w_topline);
1400 /* ... but don't delete new filler lines. */
1401 row -= wp->w_topfill;
1402#endif
1403 if (row > 0)
1404 {
1405 check_for_delay(FALSE);
1406 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1407 bot_start = wp->w_height - row;
1408 else
1409 mid_start = 0; /* redraw all lines */
1410 }
1411 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1412 {
1413 /*
1414 * Skip the lines (below the deleted lines) that are still
1415 * valid and don't need redrawing. Copy their info
1416 * upwards, to compensate for the deleted lines. Set
1417 * bot_start to the first row that needs redrawing.
1418 */
1419 bot_start = 0;
1420 idx = 0;
1421 for (;;)
1422 {
1423 wp->w_lines[idx] = wp->w_lines[j];
1424 /* stop at line that didn't fit, unless it is still
1425 * valid (no lines deleted) */
1426 if (row > 0 && bot_start + row
1427 + (int)wp->w_lines[j].wl_size > wp->w_height)
1428 {
1429 wp->w_lines_valid = idx + 1;
1430 break;
1431 }
1432 bot_start += wp->w_lines[idx++].wl_size;
1433
1434 /* stop at the last valid entry in w_lines[].wl_size */
1435 if (++j >= wp->w_lines_valid)
1436 {
1437 wp->w_lines_valid = idx;
1438 break;
1439 }
1440 }
1441#ifdef FEAT_DIFF
1442 /* Correct the first entry for filler lines at the top
1443 * when it won't get updated below. */
1444 if (wp->w_p_diff && bot_start > 0)
1445 wp->w_lines[0].wl_size =
1446 plines_win_nofill(wp, wp->w_topline, TRUE)
1447 + wp->w_topfill;
1448#endif
1449 }
1450 }
1451 }
1452
1453 /* When starting redraw in the first line, redraw all lines. When
1454 * there is only one window it's probably faster to clear the screen
1455 * first. */
1456 if (mid_start == 0)
1457 {
1458 mid_end = wp->w_height;
1459 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001460 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001461 /* Clear the screen when it was not done by win_del_lines() or
1462 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1463 * then. */
1464 if (screen_cleared != TRUE)
1465 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001466#ifdef FEAT_WINDOWS
1467 /* The screen was cleared, redraw the tab pages line. */
1468 if (redraw_tabline)
1469 draw_tabline();
1470#endif
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473
1474 /* When win_del_lines() or win_ins_lines() caused the screen to be
1475 * cleared (only happens for the first window) or when screenclear()
1476 * was called directly above, "must_redraw" will have been set to
1477 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1478 if (screen_cleared == TRUE)
1479 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 else
1482 {
1483 /* Not VALID or INVERTED: redraw all lines. */
1484 mid_start = 0;
1485 mid_end = wp->w_height;
1486 }
1487
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001488 if (type == SOME_VALID)
1489 {
1490 /* SOME_VALID: redraw all lines. */
1491 mid_start = 0;
1492 mid_end = wp->w_height;
1493 type = NOT_VALID;
1494 }
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /* check if we are updating or removing the inverted part */
1497 if ((VIsual_active && buf == curwin->w_buffer)
1498 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1499 {
1500 linenr_T from, to;
1501
1502 if (VIsual_active)
1503 {
1504 if (VIsual_active
1505 && (VIsual_mode != wp->w_old_visual_mode
1506 || type == INVERTED_ALL))
1507 {
1508 /*
1509 * If the type of Visual selection changed, redraw the whole
1510 * selection. Also when the ownership of the X selection is
1511 * gained or lost.
1512 */
1513 if (curwin->w_cursor.lnum < VIsual.lnum)
1514 {
1515 from = curwin->w_cursor.lnum;
1516 to = VIsual.lnum;
1517 }
1518 else
1519 {
1520 from = VIsual.lnum;
1521 to = curwin->w_cursor.lnum;
1522 }
1523 /* redraw more when the cursor moved as well */
1524 if (wp->w_old_cursor_lnum < from)
1525 from = wp->w_old_cursor_lnum;
1526 if (wp->w_old_cursor_lnum > to)
1527 to = wp->w_old_cursor_lnum;
1528 if (wp->w_old_visual_lnum < from)
1529 from = wp->w_old_visual_lnum;
1530 if (wp->w_old_visual_lnum > to)
1531 to = wp->w_old_visual_lnum;
1532 }
1533 else
1534 {
1535 /*
1536 * Find the line numbers that need to be updated: The lines
1537 * between the old cursor position and the current cursor
1538 * position. Also check if the Visual position changed.
1539 */
1540 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1541 {
1542 from = curwin->w_cursor.lnum;
1543 to = wp->w_old_cursor_lnum;
1544 }
1545 else
1546 {
1547 from = wp->w_old_cursor_lnum;
1548 to = curwin->w_cursor.lnum;
1549 if (from == 0) /* Visual mode just started */
1550 from = to;
1551 }
1552
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001553 if (VIsual.lnum != wp->w_old_visual_lnum
1554 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 if (wp->w_old_visual_lnum < from
1557 && wp->w_old_visual_lnum != 0)
1558 from = wp->w_old_visual_lnum;
1559 if (wp->w_old_visual_lnum > to)
1560 to = wp->w_old_visual_lnum;
1561 if (VIsual.lnum < from)
1562 from = VIsual.lnum;
1563 if (VIsual.lnum > to)
1564 to = VIsual.lnum;
1565 }
1566 }
1567
1568 /*
1569 * If in block mode and changed column or curwin->w_curswant:
1570 * update all lines.
1571 * First compute the actual start and end column.
1572 */
1573 if (VIsual_mode == Ctrl_V)
1574 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001575 colnr_T fromc, toc;
1576#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1577 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaar404406a2014-10-09 13:24:43 +02001579 if (curwin->w_p_lbr)
1580 ve_flags = VE_ALL;
1581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001583#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1584 ve_flags = save_ve_flags;
1585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 ++toc;
1587 if (curwin->w_curswant == MAXCOL)
1588 toc = MAXCOL;
1589
1590 if (fromc != wp->w_old_cursor_fcol
1591 || toc != wp->w_old_cursor_lcol)
1592 {
1593 if (from > VIsual.lnum)
1594 from = VIsual.lnum;
1595 if (to < VIsual.lnum)
1596 to = VIsual.lnum;
1597 }
1598 wp->w_old_cursor_fcol = fromc;
1599 wp->w_old_cursor_lcol = toc;
1600 }
1601 }
1602 else
1603 {
1604 /* Use the line numbers of the old Visual area. */
1605 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1606 {
1607 from = wp->w_old_cursor_lnum;
1608 to = wp->w_old_visual_lnum;
1609 }
1610 else
1611 {
1612 from = wp->w_old_visual_lnum;
1613 to = wp->w_old_cursor_lnum;
1614 }
1615 }
1616
1617 /*
1618 * There is no need to update lines above the top of the window.
1619 */
1620 if (from < wp->w_topline)
1621 from = wp->w_topline;
1622
1623 /*
1624 * If we know the value of w_botline, use it to restrict the update to
1625 * the lines that are visible in the window.
1626 */
1627 if (wp->w_valid & VALID_BOTLINE)
1628 {
1629 if (from >= wp->w_botline)
1630 from = wp->w_botline - 1;
1631 if (to >= wp->w_botline)
1632 to = wp->w_botline - 1;
1633 }
1634
1635 /*
1636 * Find the minimal part to be updated.
1637 * Watch out for scrolling that made entries in w_lines[] invalid.
1638 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1639 * top_end; need to redraw from top_end to the "to" line.
1640 * A middle mouse click with a Visual selection may change the text
1641 * above the Visual area and reset wl_valid, do count these for
1642 * mid_end (in srow).
1643 */
1644 if (mid_start > 0)
1645 {
1646 lnum = wp->w_topline;
1647 idx = 0;
1648 srow = 0;
1649 if (scrolled_down)
1650 mid_start = top_end;
1651 else
1652 mid_start = 0;
1653 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1654 {
1655 if (wp->w_lines[idx].wl_valid)
1656 mid_start += wp->w_lines[idx].wl_size;
1657 else if (!scrolled_down)
1658 srow += wp->w_lines[idx].wl_size;
1659 ++idx;
1660# ifdef FEAT_FOLDING
1661 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1662 lnum = wp->w_lines[idx].wl_lnum;
1663 else
1664# endif
1665 ++lnum;
1666 }
1667 srow += mid_start;
1668 mid_end = wp->w_height;
1669 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1670 {
1671 if (wp->w_lines[idx].wl_valid
1672 && wp->w_lines[idx].wl_lnum >= to + 1)
1673 {
1674 /* Only update until first row of this line */
1675 mid_end = srow;
1676 break;
1677 }
1678 srow += wp->w_lines[idx].wl_size;
1679 }
1680 }
1681 }
1682
1683 if (VIsual_active && buf == curwin->w_buffer)
1684 {
1685 wp->w_old_visual_mode = VIsual_mode;
1686 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1687 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001688 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 wp->w_old_curswant = curwin->w_curswant;
1690 }
1691 else
1692 {
1693 wp->w_old_visual_mode = 0;
1694 wp->w_old_cursor_lnum = 0;
1695 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001696 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1700 /* reset got_int, otherwise regexp won't work */
1701 save_got_int = got_int;
1702 got_int = 0;
1703#endif
1704#ifdef FEAT_FOLDING
1705 win_foldinfo.fi_level = 0;
1706#endif
1707
1708 /*
1709 * Update all the window rows.
1710 */
1711 idx = 0; /* first entry in w_lines[].wl_size */
1712 row = 0;
1713 srow = 0;
1714 lnum = wp->w_topline; /* first line shown in window */
1715 for (;;)
1716 {
1717 /* stop updating when reached the end of the window (check for _past_
1718 * the end of the window is at the end of the loop) */
1719 if (row == wp->w_height)
1720 {
1721 didline = TRUE;
1722 break;
1723 }
1724
1725 /* stop updating when hit the end of the file */
1726 if (lnum > buf->b_ml.ml_line_count)
1727 {
1728 eof = TRUE;
1729 break;
1730 }
1731
1732 /* Remember the starting row of the line that is going to be dealt
1733 * with. It is used further down when the line doesn't fit. */
1734 srow = row;
1735
1736 /*
1737 * Update a line when it is in an area that needs updating, when it
1738 * has changes or w_lines[idx] is invalid.
1739 * bot_start may be halfway a wrapped line after using
1740 * win_del_lines(), check if the current line includes it.
1741 * When syntax folding is being used, the saved syntax states will
1742 * already have been updated, we can't see where the syntax state is
1743 * the same again, just update until the end of the window.
1744 */
1745 if (row < top_end
1746 || (row >= mid_start && row < mid_end)
1747#ifdef FEAT_SEARCH_EXTRA
1748 || top_to_mod
1749#endif
1750 || idx >= wp->w_lines_valid
1751 || (row + wp->w_lines[idx].wl_size > bot_start)
1752 || (mod_top != 0
1753 && (lnum == mod_top
1754 || (lnum >= mod_top
1755 && (lnum < mod_bot
1756#ifdef FEAT_SYN_HL
1757 || did_update == DID_FOLD
1758 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001759 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 && (
1761# ifdef FEAT_FOLDING
1762 (foldmethodIsSyntax(wp)
1763 && hasAnyFolding(wp)) ||
1764# endif
1765 syntax_check_changed(lnum)))
1766#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001767#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001768 /* match in fixed position might need redraw
1769 * if lines were inserted or deleted */
1770 || (wp->w_match_head != NULL
1771 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 )))))
1774 {
1775#ifdef FEAT_SEARCH_EXTRA
1776 if (lnum == mod_top)
1777 top_to_mod = FALSE;
1778#endif
1779
1780 /*
1781 * When at start of changed lines: May scroll following lines
1782 * up or down to minimize redrawing.
1783 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001784 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 */
1786 if (lnum == mod_top
1787 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001788 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 int old_rows = 0;
1791 int new_rows = 0;
1792 int xtra_rows;
1793 linenr_T l;
1794
1795 /* Count the old number of window rows, using w_lines[], which
1796 * should still contain the sizes for the lines as they are
1797 * currently displayed. */
1798 for (i = idx; i < wp->w_lines_valid; ++i)
1799 {
1800 /* Only valid lines have a meaningful wl_lnum. Invalid
1801 * lines are part of the changed area. */
1802 if (wp->w_lines[i].wl_valid
1803 && wp->w_lines[i].wl_lnum == mod_bot)
1804 break;
1805 old_rows += wp->w_lines[i].wl_size;
1806#ifdef FEAT_FOLDING
1807 if (wp->w_lines[i].wl_valid
1808 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1809 {
1810 /* Must have found the last valid entry above mod_bot.
1811 * Add following invalid entries. */
1812 ++i;
1813 while (i < wp->w_lines_valid
1814 && !wp->w_lines[i].wl_valid)
1815 old_rows += wp->w_lines[i++].wl_size;
1816 break;
1817 }
1818#endif
1819 }
1820
1821 if (i >= wp->w_lines_valid)
1822 {
1823 /* We can't find a valid line below the changed lines,
1824 * need to redraw until the end of the window.
1825 * Inserting/deleting lines has no use. */
1826 bot_start = 0;
1827 }
1828 else
1829 {
1830 /* Able to count old number of rows: Count new window
1831 * rows, and may insert/delete lines */
1832 j = idx;
1833 for (l = lnum; l < mod_bot; ++l)
1834 {
1835#ifdef FEAT_FOLDING
1836 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1837 ++new_rows;
1838 else
1839#endif
1840#ifdef FEAT_DIFF
1841 if (l == wp->w_topline)
1842 new_rows += plines_win_nofill(wp, l, TRUE)
1843 + wp->w_topfill;
1844 else
1845#endif
1846 new_rows += plines_win(wp, l, TRUE);
1847 ++j;
1848 if (new_rows > wp->w_height - row - 2)
1849 {
1850 /* it's getting too much, must redraw the rest */
1851 new_rows = 9999;
1852 break;
1853 }
1854 }
1855 xtra_rows = new_rows - old_rows;
1856 if (xtra_rows < 0)
1857 {
1858 /* May scroll text up. If there is not enough
1859 * remaining text or scrolling fails, must redraw the
1860 * rest. If scrolling works, must redraw the text
1861 * below the scrolled text. */
1862 if (row - xtra_rows >= wp->w_height - 2)
1863 mod_bot = MAXLNUM;
1864 else
1865 {
1866 check_for_delay(FALSE);
1867 if (win_del_lines(wp, row,
1868 -xtra_rows, FALSE, FALSE) == FAIL)
1869 mod_bot = MAXLNUM;
1870 else
1871 bot_start = wp->w_height + xtra_rows;
1872 }
1873 }
1874 else if (xtra_rows > 0)
1875 {
1876 /* May scroll text down. If there is not enough
1877 * remaining text of scrolling fails, must redraw the
1878 * rest. */
1879 if (row + xtra_rows >= wp->w_height - 2)
1880 mod_bot = MAXLNUM;
1881 else
1882 {
1883 check_for_delay(FALSE);
1884 if (win_ins_lines(wp, row + old_rows,
1885 xtra_rows, FALSE, FALSE) == FAIL)
1886 mod_bot = MAXLNUM;
1887 else if (top_end > row + old_rows)
1888 /* Scrolled the part at the top that requires
1889 * updating down. */
1890 top_end += xtra_rows;
1891 }
1892 }
1893
1894 /* When not updating the rest, may need to move w_lines[]
1895 * entries. */
1896 if (mod_bot != MAXLNUM && i != j)
1897 {
1898 if (j < i)
1899 {
1900 int x = row + new_rows;
1901
1902 /* move entries in w_lines[] upwards */
1903 for (;;)
1904 {
1905 /* stop at last valid entry in w_lines[] */
1906 if (i >= wp->w_lines_valid)
1907 {
1908 wp->w_lines_valid = j;
1909 break;
1910 }
1911 wp->w_lines[j] = wp->w_lines[i];
1912 /* stop at a line that won't fit */
1913 if (x + (int)wp->w_lines[j].wl_size
1914 > wp->w_height)
1915 {
1916 wp->w_lines_valid = j + 1;
1917 break;
1918 }
1919 x += wp->w_lines[j++].wl_size;
1920 ++i;
1921 }
1922 if (bot_start > x)
1923 bot_start = x;
1924 }
1925 else /* j > i */
1926 {
1927 /* move entries in w_lines[] downwards */
1928 j -= i;
1929 wp->w_lines_valid += j;
1930 if (wp->w_lines_valid > wp->w_height)
1931 wp->w_lines_valid = wp->w_height;
1932 for (i = wp->w_lines_valid; i - j >= idx; --i)
1933 wp->w_lines[i] = wp->w_lines[i - j];
1934
1935 /* The w_lines[] entries for inserted lines are
1936 * now invalid, but wl_size may be used above.
1937 * Reset to zero. */
1938 while (i >= idx)
1939 {
1940 wp->w_lines[i].wl_size = 0;
1941 wp->w_lines[i--].wl_valid = FALSE;
1942 }
1943 }
1944 }
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947
1948#ifdef FEAT_FOLDING
1949 /*
1950 * When lines are folded, display one line for all of them.
1951 * Otherwise, display normally (can be several display lines when
1952 * 'wrap' is on).
1953 */
1954 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1955 if (fold_count != 0)
1956 {
1957 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1958 ++row;
1959 --fold_count;
1960 wp->w_lines[idx].wl_folded = TRUE;
1961 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1962# ifdef FEAT_SYN_HL
1963 did_update = DID_FOLD;
1964# endif
1965 }
1966 else
1967#endif
1968 if (idx < wp->w_lines_valid
1969 && wp->w_lines[idx].wl_valid
1970 && wp->w_lines[idx].wl_lnum == lnum
1971 && lnum > wp->w_topline
1972 && !(dy_flags & DY_LASTLINE)
1973 && srow + wp->w_lines[idx].wl_size > wp->w_height
1974#ifdef FEAT_DIFF
1975 && diff_check_fill(wp, lnum) == 0
1976#endif
1977 )
1978 {
1979 /* This line is not going to fit. Don't draw anything here,
1980 * will draw "@ " lines below. */
1981 row = wp->w_height + 1;
1982 }
1983 else
1984 {
1985#ifdef FEAT_SEARCH_EXTRA
1986 prepare_search_hl(wp, lnum);
1987#endif
1988#ifdef FEAT_SYN_HL
1989 /* Let the syntax stuff know we skipped a few lines. */
1990 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001991 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 syntax_end_parsing(syntax_last_parsed + 1);
1993#endif
1994
1995 /*
1996 * Display one line.
1997 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001998 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000#ifdef FEAT_FOLDING
2001 wp->w_lines[idx].wl_folded = FALSE;
2002 wp->w_lines[idx].wl_lastlnum = lnum;
2003#endif
2004#ifdef FEAT_SYN_HL
2005 did_update = DID_LINE;
2006 syntax_last_parsed = lnum;
2007#endif
2008 }
2009
2010 wp->w_lines[idx].wl_lnum = lnum;
2011 wp->w_lines[idx].wl_valid = TRUE;
2012 if (row > wp->w_height) /* past end of screen */
2013 {
2014 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002015 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2017 ++idx;
2018 break;
2019 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002020 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 wp->w_lines[idx].wl_size = row - srow;
2022 ++idx;
2023#ifdef FEAT_FOLDING
2024 lnum += fold_count + 1;
2025#else
2026 ++lnum;
2027#endif
2028 }
2029 else
2030 {
2031 /* This line does not need updating, advance to the next one */
2032 row += wp->w_lines[idx++].wl_size;
2033 if (row > wp->w_height) /* past end of screen */
2034 break;
2035#ifdef FEAT_FOLDING
2036 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2037#else
2038 ++lnum;
2039#endif
2040#ifdef FEAT_SYN_HL
2041 did_update = DID_NONE;
2042#endif
2043 }
2044
2045 if (lnum > buf->b_ml.ml_line_count)
2046 {
2047 eof = TRUE;
2048 break;
2049 }
2050 }
2051 /*
2052 * End of loop over all window lines.
2053 */
2054
2055
2056 if (idx > wp->w_lines_valid)
2057 wp->w_lines_valid = idx;
2058
2059#ifdef FEAT_SYN_HL
2060 /*
2061 * Let the syntax stuff know we stop parsing here.
2062 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002063 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 syntax_end_parsing(syntax_last_parsed + 1);
2065#endif
2066
2067 /*
2068 * If we didn't hit the end of the file, and we didn't finish the last
2069 * line we were working on, then the line didn't fit.
2070 */
2071 wp->w_empty_rows = 0;
2072#ifdef FEAT_DIFF
2073 wp->w_filler_rows = 0;
2074#endif
2075 if (!eof && !didline)
2076 {
2077 if (lnum == wp->w_topline)
2078 {
2079 /*
2080 * Single line that does not fit!
2081 * Don't overwrite it, it can be edited.
2082 */
2083 wp->w_botline = lnum + 1;
2084 }
2085#ifdef FEAT_DIFF
2086 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2087 {
2088 /* Window ends in filler lines. */
2089 wp->w_botline = lnum;
2090 wp->w_filler_rows = wp->w_height - srow;
2091 }
2092#endif
2093 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2094 {
2095 /*
2096 * Last line isn't finished: Display "@@@" at the end.
2097 */
2098 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2099 W_WINROW(wp) + wp->w_height,
2100 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2101 '@', '@', hl_attr(HLF_AT));
2102 set_empty_rows(wp, srow);
2103 wp->w_botline = lnum;
2104 }
2105 else
2106 {
2107 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2108 wp->w_botline = lnum;
2109 }
2110 }
2111 else
2112 {
2113#ifdef FEAT_VERTSPLIT
2114 draw_vsep_win(wp, row);
2115#endif
2116 if (eof) /* we hit the end of the file */
2117 {
2118 wp->w_botline = buf->b_ml.ml_line_count + 1;
2119#ifdef FEAT_DIFF
2120 j = diff_check_fill(wp, wp->w_botline);
2121 if (j > 0 && !wp->w_botfill)
2122 {
2123 /*
2124 * Display filler lines at the end of the file
2125 */
2126 if (char2cells(fill_diff) > 1)
2127 i = '-';
2128 else
2129 i = fill_diff;
2130 if (row + j > wp->w_height)
2131 j = wp->w_height - row;
2132 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2133 row += j;
2134 }
2135#endif
2136 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002137 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 wp->w_botline = lnum;
2139
2140 /* make sure the rest of the screen is blank */
2141 /* put '~'s on rows that aren't part of the file. */
2142 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2143 }
2144
2145 /* Reset the type of redrawing required, the window has been updated. */
2146 wp->w_redr_type = 0;
2147#ifdef FEAT_DIFF
2148 wp->w_old_topfill = wp->w_topfill;
2149 wp->w_old_botfill = wp->w_botfill;
2150#endif
2151
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 /*
2155 * There is a trick with w_botline. If we invalidate it on each
2156 * change that might modify it, this will cause a lot of expensive
2157 * calls to plines() in update_topline() each time. Therefore the
2158 * value of w_botline is often approximated, and this value is used to
2159 * compute the value of w_topline. If the value of w_botline was
2160 * wrong, check that the value of w_topline is correct (cursor is on
2161 * the visible part of the text). If it's not, we need to redraw
2162 * again. Mostly this just means scrolling up a few lines, so it
2163 * doesn't look too bad. Only do this for the current window (where
2164 * changes are relevant).
2165 */
2166 wp->w_valid |= VALID_BOTLINE;
2167 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2168 {
2169 recursive = TRUE;
2170 curwin->w_valid &= ~VALID_TOPLINE;
2171 update_topline(); /* may invalidate w_botline again */
2172 if (must_redraw != 0)
2173 {
2174 /* Don't update for changes in buffer again. */
2175 i = curbuf->b_mod_set;
2176 curbuf->b_mod_set = FALSE;
2177 win_update(curwin);
2178 must_redraw = 0;
2179 curbuf->b_mod_set = i;
2180 }
2181 recursive = FALSE;
2182 }
2183 }
2184
2185#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2186 /* restore got_int, unless CTRL-C was hit while redrawing */
2187 if (!got_int)
2188 got_int = save_got_int;
2189#endif
2190}
2191
2192#ifdef FEAT_SIGNS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002193static int draw_signcolumn(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195/*
2196 * Return TRUE when window "wp" has a column to draw signs in.
2197 */
2198 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002199draw_signcolumn(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 return (wp->w_buffer->b_signlist != NULL
2202# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002203 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204# endif
2205 );
2206}
2207#endif
2208
2209/*
2210 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2211 * as the filler character.
2212 */
2213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002214win_draw_end(
2215 win_T *wp,
2216 int c1,
2217 int c2,
2218 int row,
2219 int endrow,
2220 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2223 int n = 0;
2224# define FDC_OFF n
2225#else
2226# define FDC_OFF 0
2227#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002228#ifdef FEAT_FOLDING
2229 int fdc = compute_foldcolumn(wp, 0);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232#ifdef FEAT_RIGHTLEFT
2233 if (wp->w_p_rl)
2234 {
2235 /* No check for cmdline window: should never be right-left. */
2236# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002237 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
2239 if (n > 0)
2240 {
2241 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002242 if (n > W_WIDTH(wp))
2243 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2245 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2246 ' ', ' ', hl_attr(HLF_FC));
2247 }
2248# endif
2249# ifdef FEAT_SIGNS
2250 if (draw_signcolumn(wp))
2251 {
2252 int nn = n + 2;
2253
2254 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002255 if (nn > W_WIDTH(wp))
2256 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2258 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2259 ' ', ' ', hl_attr(HLF_SC));
2260 n = nn;
2261 }
2262# endif
2263 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2264 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2265 c2, c2, hl_attr(hl));
2266 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2267 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2268 c1, c2, hl_attr(hl));
2269 }
2270 else
2271#endif
2272 {
2273#ifdef FEAT_CMDWIN
2274 if (cmdwin_type != 0 && wp == curwin)
2275 {
2276 /* draw the cmdline character in the leftmost column */
2277 n = 1;
2278 if (n > wp->w_width)
2279 n = wp->w_width;
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2282 cmdwin_type, ' ', hl_attr(HLF_AT));
2283 }
2284#endif
2285#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002286 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002288 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 /* draw the fold column at the left */
2291 if (nn > W_WIDTH(wp))
2292 nn = W_WIDTH(wp);
2293 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2294 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2295 ' ', ' ', hl_attr(HLF_FC));
2296 n = nn;
2297 }
2298#endif
2299#ifdef FEAT_SIGNS
2300 if (draw_signcolumn(wp))
2301 {
2302 int nn = n + 2;
2303
2304 /* draw the sign column after the fold column */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_SC));
2310 n = nn;
2311 }
2312#endif
2313 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2314 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2315 c1, c2, hl_attr(hl));
2316 }
2317 set_empty_rows(wp, row);
2318}
2319
Bram Moolenaar1a384422010-07-14 19:53:30 +02002320#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002321static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002322
2323/*
2324 * Advance **color_cols and return TRUE when there are columns to draw.
2325 */
2326 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002327advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002328{
2329 while (**color_cols >= 0 && vcol > **color_cols)
2330 ++*color_cols;
2331 return (**color_cols >= 0);
2332}
2333#endif
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#ifdef FEAT_FOLDING
2336/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002337 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2338 * space is available for window "wp", minus "col".
2339 */
2340 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002341compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002342{
2343 int fdc = wp->w_p_fdc;
2344 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2345 int wwidth = W_WIDTH(wp);
2346
2347 if (fdc > wwidth - (col + wmw))
2348 fdc = wwidth - (col + wmw);
2349 return fdc;
2350}
2351
2352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 * Display one folded line.
2354 */
2355 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002356fold_line(
2357 win_T *wp,
2358 long fold_count,
2359 foldinfo_T *foldinfo,
2360 linenr_T lnum,
2361 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
2363 char_u buf[51];
2364 pos_T *top, *bot;
2365 linenr_T lnume = lnum + fold_count - 1;
2366 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002367 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 int col;
2370 int txtcol;
2371 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002372 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 /* Build the fold line:
2375 * 1. Add the cmdwin_type for the command-line window
2376 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002377 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * 4. Compose the text
2379 * 5. Add the text
2380 * 6. set highlighting for the Visual area an other text
2381 */
2382 col = 0;
2383
2384 /*
2385 * 1. Add the cmdwin_type for the command-line window
2386 * Ignores 'rightleft', this window is never right-left.
2387 */
2388#ifdef FEAT_CMDWIN
2389 if (cmdwin_type != 0 && wp == curwin)
2390 {
2391 ScreenLines[off] = cmdwin_type;
2392 ScreenAttrs[off] = hl_attr(HLF_AT);
2393#ifdef FEAT_MBYTE
2394 if (enc_utf8)
2395 ScreenLinesUC[off] = 0;
2396#endif
2397 ++col;
2398 }
2399#endif
2400
2401 /*
2402 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002403 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002405 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 if (fdc > 0)
2407 {
2408 fill_foldcolumn(buf, wp, TRUE, lnum);
2409#ifdef FEAT_RIGHTLEFT
2410 if (wp->w_p_rl)
2411 {
2412 int i;
2413
2414 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2415 hl_attr(HLF_FC));
2416 /* reverse the fold column */
2417 for (i = 0; i < fdc; ++i)
2418 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2419 }
2420 else
2421#endif
2422 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2423 col += fdc;
2424 }
2425
2426#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2428 for (ri = 0; ri < l; ++ri) \
2429 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2430 else \
2431 for (ri = 0; ri < l; ++ri) \
2432 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002434# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2435 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437
Bram Moolenaar64486672010-05-16 15:46:46 +02002438 /* Set all attributes of the 'number' or 'relativenumber' column and the
2439 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002440 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442#ifdef FEAT_SIGNS
2443 /* If signs are being displayed, add two spaces. */
2444 if (draw_signcolumn(wp))
2445 {
2446 len = W_WIDTH(wp) - col;
2447 if (len > 0)
2448 {
2449 if (len > 2)
2450 len = 2;
2451# ifdef FEAT_RIGHTLEFT
2452 if (wp->w_p_rl)
2453 /* the line number isn't reversed */
2454 copy_text_attr(off + W_WIDTH(wp) - len - col,
2455 (char_u *)" ", len, hl_attr(HLF_FL));
2456 else
2457# endif
2458 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2459 col += len;
2460 }
2461 }
2462#endif
2463
2464 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002465 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002467 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 len = W_WIDTH(wp) - col;
2470 if (len > 0)
2471 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002472 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002473 long num;
2474 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002475
2476 if (len > w + 1)
2477 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002478
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002479 if (wp->w_p_nu && !wp->w_p_rnu)
2480 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002481 num = (long)lnum;
2482 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002483 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002484 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002485 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002486 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002487 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002488 /* 'number' + 'relativenumber': cursor line shows absolute
2489 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002490 num = lnum;
2491 fmt = "%-*ld ";
2492 }
2493 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002494
Bram Moolenaar700e7342013-01-30 12:31:36 +01002495 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496#ifdef FEAT_RIGHTLEFT
2497 if (wp->w_p_rl)
2498 /* the line number isn't reversed */
2499 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2500 hl_attr(HLF_FL));
2501 else
2502#endif
2503 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2504 col += len;
2505 }
2506 }
2507
2508 /*
2509 * 4. Compose the folded-line string with 'foldtext', if set.
2510 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002511 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 txtcol = col; /* remember where text starts */
2514
2515 /*
2516 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2517 * Right-left text is put in columns 0 - number-col, normal text is put
2518 * in columns number-col - window-width.
2519 */
2520#ifdef FEAT_MBYTE
2521 if (has_mbyte)
2522 {
2523 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002524 int u8c, u8cc[MAX_MCO];
2525 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 int idx;
2527 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002528 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529# ifdef FEAT_ARABIC
2530 int prev_c = 0; /* previous Arabic character */
2531 int prev_c1 = 0; /* first composing char for prev_c */
2532# endif
2533
2534# ifdef FEAT_RIGHTLEFT
2535 if (wp->w_p_rl)
2536 idx = off;
2537 else
2538# endif
2539 idx = off + col;
2540
2541 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2542 for (p = text; *p != NUL; )
2543 {
2544 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002545 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (col + cells > W_WIDTH(wp)
2547# ifdef FEAT_RIGHTLEFT
2548 - (wp->w_p_rl ? col : 0)
2549# endif
2550 )
2551 break;
2552 ScreenLines[idx] = *p;
2553 if (enc_utf8)
2554 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002555 u8c = utfc_ptr2char(p, u8cc);
2556 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 ScreenLinesUC[idx] = 0;
2559#ifdef FEAT_ARABIC
2560 prev_c = u8c;
2561#endif
2562 }
2563 else
2564 {
2565#ifdef FEAT_ARABIC
2566 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2567 {
2568 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002569 int pc, pc1, nc;
2570 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 int firstbyte = *p;
2572
2573 /* The idea of what is the previous and next
2574 * character depends on 'rightleft'. */
2575 if (wp->w_p_rl)
2576 {
2577 pc = prev_c;
2578 pc1 = prev_c1;
2579 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002580 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 else
2583 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002586 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 prev_c = u8c;
2589
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 pc, pc1, nc);
2592 ScreenLines[idx] = firstbyte;
2593 }
2594 else
2595 prev_c = u8c;
2596#endif
2597 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002598#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (u8c >= 0x10000)
2600 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2601 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 for (i = 0; i < Screen_mco; ++i)
2605 {
2606 ScreenLinesC[i][idx] = u8cc[i];
2607 if (u8cc[i] == 0)
2608 break;
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611 if (cells > 1)
2612 ScreenLines[idx + 1] = 0;
2613 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002614 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2615 /* double-byte single width character */
2616 ScreenLines2[idx] = p[1];
2617 else if (cells > 1)
2618 /* double-width character */
2619 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 col += cells;
2621 idx += cells;
2622 p += c_len;
2623 }
2624 }
2625 else
2626#endif
2627 {
2628 len = (int)STRLEN(text);
2629 if (len > W_WIDTH(wp) - col)
2630 len = W_WIDTH(wp) - col;
2631 if (len > 0)
2632 {
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 STRNCPY(current_ScreenLine, text, len);
2636 else
2637#endif
2638 STRNCPY(current_ScreenLine + col, text, len);
2639 col += len;
2640 }
2641 }
2642
2643 /* Fill the rest of the line with the fold filler */
2644#ifdef FEAT_RIGHTLEFT
2645 if (wp->w_p_rl)
2646 col -= txtcol;
2647#endif
2648 while (col < W_WIDTH(wp)
2649#ifdef FEAT_RIGHTLEFT
2650 - (wp->w_p_rl ? txtcol : 0)
2651#endif
2652 )
2653 {
2654#ifdef FEAT_MBYTE
2655 if (enc_utf8)
2656 {
2657 if (fill_fold >= 0x80)
2658 {
2659 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662 else
2663 ScreenLinesUC[off + col] = 0;
2664 }
2665#endif
2666 ScreenLines[off + col++] = fill_fold;
2667 }
2668
2669 if (text != buf)
2670 vim_free(text);
2671
2672 /*
2673 * 6. set highlighting for the Visual area an other text.
2674 * If all folded lines are in the Visual area, highlight the line.
2675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2677 {
2678 if (ltoreq(curwin->w_cursor, VIsual))
2679 {
2680 /* Visual is after curwin->w_cursor */
2681 top = &curwin->w_cursor;
2682 bot = &VIsual;
2683 }
2684 else
2685 {
2686 /* Visual is before curwin->w_cursor */
2687 top = &VIsual;
2688 bot = &curwin->w_cursor;
2689 }
2690 if (lnum >= top->lnum
2691 && lnume <= bot->lnum
2692 && (VIsual_mode != 'v'
2693 || ((lnum > top->lnum
2694 || (lnum == top->lnum
2695 && top->col == 0))
2696 && (lnume < bot->lnum
2697 || (lnume == bot->lnum
2698 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002699 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
2701 if (VIsual_mode == Ctrl_V)
2702 {
2703 /* Visual block mode: highlight the chars part of the block */
2704 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2705 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002706 if (wp->w_old_cursor_lcol != MAXCOL
2707 && wp->w_old_cursor_lcol + txtcol
2708 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 len = wp->w_old_cursor_lcol;
2710 else
2711 len = W_WIDTH(wp) - txtcol;
2712 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002713 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715 }
2716 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002717 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002719 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002725 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2726 if (wp->w_p_cc_cols)
2727 {
2728 int i = 0;
2729 int j = wp->w_p_cc_cols[i];
2730 int old_txtcol = txtcol;
2731
2732 while (j > -1)
2733 {
2734 txtcol += j;
2735 if (wp->w_p_wrap)
2736 txtcol -= wp->w_skipcol;
2737 else
2738 txtcol -= wp->w_leftcol;
2739 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2740 ScreenAttrs[off + txtcol] = hl_combine_attr(
2741 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2742 txtcol = old_txtcol;
2743 j = wp->w_p_cc_cols[++i];
2744 }
2745 }
2746
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002747 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002748 if (wp->w_p_cuc)
2749 {
2750 txtcol += wp->w_virtcol;
2751 if (wp->w_p_wrap)
2752 txtcol -= wp->w_skipcol;
2753 else
2754 txtcol -= wp->w_leftcol;
2755 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2756 ScreenAttrs[off + txtcol] = hl_combine_attr(
2757 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2758 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2762 (int)W_WIDTH(wp), FALSE);
2763
2764 /*
2765 * Update w_cline_height and w_cline_folded if the cursor line was
2766 * updated (saves a call to plines() later).
2767 */
2768 if (wp == curwin
2769 && lnum <= curwin->w_cursor.lnum
2770 && lnume >= curwin->w_cursor.lnum)
2771 {
2772 curwin->w_cline_row = row;
2773 curwin->w_cline_height = 1;
2774 curwin->w_cline_folded = TRUE;
2775 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2776 }
2777}
2778
2779/*
2780 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2781 */
2782 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002783copy_text_attr(
2784 int off,
2785 char_u *buf,
2786 int len,
2787 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002789 int i;
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 mch_memmove(ScreenLines + off, buf, (size_t)len);
2792# ifdef FEAT_MBYTE
2793 if (enc_utf8)
2794 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2795# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002796 for (i = 0; i < len; ++i)
2797 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798}
2799
2800/*
2801 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002802 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002805fill_foldcolumn(
2806 char_u *p,
2807 win_T *wp,
2808 int closed, /* TRUE of FALSE */
2809 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810{
2811 int i = 0;
2812 int level;
2813 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002814 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002815 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002818 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 level = win_foldinfo.fi_level;
2821 if (level > 0)
2822 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002823 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002824 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 /* If the column is too narrow, we start at the lowest level that
2827 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002828 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (first_level < 1)
2830 first_level = 1;
2831
Bram Moolenaar1c934292015-01-27 16:39:29 +01002832 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 if (win_foldinfo.fi_lnum == lnum
2835 && first_level + i >= win_foldinfo.fi_low_level)
2836 p[i] = '-';
2837 else if (first_level == 1)
2838 p[i] = '|';
2839 else if (first_level + i <= 9)
2840 p[i] = '0' + first_level + i;
2841 else
2842 p[i] = '>';
2843 if (first_level + i == level)
2844 break;
2845 }
2846 }
2847 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002848 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849}
2850#endif /* FEAT_FOLDING */
2851
2852/*
2853 * Display line "lnum" of window 'wp' on the screen.
2854 * Start at row "startrow", stop when "endrow" is reached.
2855 * wp->w_virtcol needs to be valid.
2856 *
2857 * Return the number of last row the line occupies.
2858 */
2859 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002860win_line(
2861 win_T *wp,
2862 linenr_T lnum,
2863 int startrow,
2864 int endrow,
2865 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 int col; /* visual column on screen */
2868 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2869 int c = 0; /* init for GCC */
2870 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002871#ifdef FEAT_LINEBREAK
2872 long vcol_sbr = -1; /* virtual column after showbreak */
2873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 long vcol_prev = -1; /* "vcol" of previous character */
2875 char_u *line; /* current line */
2876 char_u *ptr; /* current position in "line" */
2877 int row; /* row in the window, excl w_winrow */
2878 int screen_row; /* row on the screen, incl w_winrow */
2879
2880 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2881 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002882 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002883 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 int c_extra = NUL; /* extra chars, all the same */
2885 int extra_attr = 0; /* attributes when n_extra != 0 */
2886 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2887 displaying lcs_eol at end-of-line */
2888 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2889 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2890
2891 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2892 int saved_n_extra = 0;
2893 char_u *saved_p_extra = NULL;
2894 int saved_c_extra = 0;
2895 int saved_char_attr = 0;
2896
2897 int n_attr = 0; /* chars with special attr */
2898 int saved_attr2 = 0; /* char_attr saved for n_attr */
2899 int n_attr3 = 0; /* chars with overruling special attr */
2900 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2901
2902 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2903
2904 int fromcol, tocol; /* start/end of inverting */
2905 int fromcol_prev = -2; /* start of inverting after cursor */
2906 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002908 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 pos_T pos;
2910 long v;
2911
2912 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002913 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2915 in this line */
2916 int attr = 0; /* attributes for area highlighting */
2917 int area_attr = 0; /* attributes desired by highlighting */
2918 int search_attr = 0; /* attributes desired by 'hlsearch' */
2919#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 int syntax_attr = 0; /* attributes desired by syntax */
2922 int has_syntax = FALSE; /* this buffer has syntax highl. */
2923 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002924 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002925 int draw_color_col = FALSE; /* highlight colorcolumn */
2926 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002927#endif
2928#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002929 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002930# define SPWORDLEN 150
2931 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002932 int nextlinecol = 0; /* column where nextline[] starts */
2933 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002934 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002935 int spell_attr = 0; /* attributes desired by spelling */
2936 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002937 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2938 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002939 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002940 static int cap_col = -1; /* column to check for Cap word */
2941 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002942 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943#endif
2944 int extra_check; /* has syntax or linebreak */
2945#ifdef FEAT_MBYTE
2946 int multi_attr = 0; /* attributes desired by multibyte */
2947 int mb_l = 1; /* multi-byte byte length */
2948 int mb_c = 0; /* decoded multi-byte character */
2949 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002950 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#endif
2952#ifdef FEAT_DIFF
2953 int filler_lines; /* nr of filler lines to be drawn */
2954 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002955 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int change_start = MAXCOL; /* first col of changed area */
2957 int change_end = -1; /* last col of changed area */
2958#endif
2959 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2960#ifdef FEAT_LINEBREAK
2961 int need_showbreak = FALSE;
2962#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002963#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2964 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002966 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#endif
2968#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002969 matchitem_T *cur; /* points to the match list */
2970 match_T *shl; /* points to search_hl or a match */
2971 int shl_flag; /* flag to indicate whether search_hl
2972 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002973 int pos_inprogress; /* marks that position match search is
2974 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002975 int prevcol_hl_flag; /* flag to indicate whether prevcol
2976 equals startcol of search_hl or one
2977 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978#endif
2979#ifdef FEAT_ARABIC
2980 int prev_c = 0; /* previous Arabic character */
2981 int prev_c1 = 0; /* first composing char for prev_c */
2982#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002983#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002984 int did_line_attr = 0;
2985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /* draw_state: items that are drawn in sequence: */
2988#define WL_START 0 /* nothing done yet */
2989#ifdef FEAT_CMDWIN
2990# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2991#else
2992# define WL_CMDLINE WL_START
2993#endif
2994#ifdef FEAT_FOLDING
2995# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2996#else
2997# define WL_FOLD WL_CMDLINE
2998#endif
2999#ifdef FEAT_SIGNS
3000# define WL_SIGN WL_FOLD + 1 /* column for signs */
3001#else
3002# define WL_SIGN WL_FOLD /* column for signs */
3003#endif
3004#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003005#ifdef FEAT_LINEBREAK
3006# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003008# define WL_BRI WL_NR
3009#endif
3010#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3011# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3012#else
3013# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#endif
3015#define WL_LINE WL_SBR + 1 /* text in the line */
3016 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003017#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 int feedback_col = 0;
3019 int feedback_old_attr = -1;
3020#endif
3021
Bram Moolenaar860cae12010-06-05 23:22:07 +02003022#ifdef FEAT_CONCEAL
3023 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003024 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003025 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003026 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003027 int is_concealing = FALSE;
3028 int boguscols = 0; /* nonexistent columns added to force
3029 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003030 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003031 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003032 int match_conc = FALSE; /* cchar for match functions */
3033 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003034 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003035# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003036# define FIX_FOR_BOGUSCOLS \
3037 { \
3038 n_extra += vcol_off; \
3039 vcol -= vcol_off; \
3040 vcol_off = 0; \
3041 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003042 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003043 boguscols = 0; \
3044 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003045#else
3046# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
3049 if (startrow > endrow) /* past the end already! */
3050 return startrow;
3051
3052 row = startrow;
3053 screen_row = row + W_WINROW(wp);
3054
3055 /*
3056 * To speed up the loop below, set extra_check when there is linebreak,
3057 * trailing white space and/or syntax processing to be done.
3058 */
3059#ifdef FEAT_LINEBREAK
3060 extra_check = wp->w_p_lbr;
3061#else
3062 extra_check = 0;
3063#endif
3064#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003065 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
3067 /* Prepare for syntax highlighting in this line. When there is an
3068 * error, stop syntax highlighting. */
3069 save_did_emsg = did_emsg;
3070 did_emsg = FALSE;
3071 syntax_start(wp, lnum);
3072 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003073 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 {
3076 did_emsg = save_did_emsg;
3077 has_syntax = TRUE;
3078 extra_check = TRUE;
3079 }
3080 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003081
3082 /* Check for columns to display for 'colorcolumn'. */
3083 color_cols = wp->w_p_cc_cols;
3084 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003085 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003086#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003087
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003088#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003089 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003090 && *wp->w_s->b_p_spl != NUL
3091 && wp->w_s->b_langp.ga_len > 0
3092 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003093 {
3094 /* Prepare for spell checking. */
3095 has_spell = TRUE;
3096 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003097
3098 /* Get the start of the next line, so that words that wrap to the next
3099 * line are found too: "et<line-break>al.".
3100 * Trick: skip a few chars for C/shell/Vim comments */
3101 nextline[SPWORDLEN] = NUL;
3102 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3103 {
3104 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3105 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3106 }
3107
3108 /* When a word wrapped from the previous line the start of the current
3109 * line is valid. */
3110 if (lnum == checked_lnum)
3111 cur_checked_col = checked_col;
3112 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003113
3114 /* When there was a sentence end in the previous line may require a
3115 * word starting with capital in this line. In line 1 always check
3116 * the first word. */
3117 if (lnum != capcol_lnum)
3118 cap_col = -1;
3119 if (lnum == 1)
3120 cap_col = 0;
3121 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124
3125 /*
3126 * handle visual active in this window
3127 */
3128 fromcol = -10;
3129 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3131 {
3132 /* Visual is after curwin->w_cursor */
3133 if (ltoreq(curwin->w_cursor, VIsual))
3134 {
3135 top = &curwin->w_cursor;
3136 bot = &VIsual;
3137 }
3138 else /* Visual is before curwin->w_cursor */
3139 {
3140 top = &VIsual;
3141 bot = &curwin->w_cursor;
3142 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003143 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 if (VIsual_mode == Ctrl_V) /* block mode */
3145 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003146 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 fromcol = wp->w_old_cursor_fcol;
3149 tocol = wp->w_old_cursor_lcol;
3150 }
3151 }
3152 else /* non-block mode */
3153 {
3154 if (lnum > top->lnum && lnum <= bot->lnum)
3155 fromcol = 0;
3156 else if (lnum == top->lnum)
3157 {
3158 if (VIsual_mode == 'V') /* linewise */
3159 fromcol = 0;
3160 else
3161 {
3162 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3163 if (gchar_pos(top) == NUL)
3164 tocol = fromcol + 1;
3165 }
3166 }
3167 if (VIsual_mode != 'V' && lnum == bot->lnum)
3168 {
3169 if (*p_sel == 'e' && bot->col == 0
3170#ifdef FEAT_VIRTUALEDIT
3171 && bot->coladd == 0
3172#endif
3173 )
3174 {
3175 fromcol = -10;
3176 tocol = MAXCOL;
3177 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003178 else if (bot->col == MAXCOL)
3179 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181 {
3182 pos = *bot;
3183 if (*p_sel == 'e')
3184 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3185 else
3186 {
3187 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3188 ++tocol;
3189 }
3190 }
3191 }
3192 }
3193
3194#ifndef MSDOS
3195 /* Check if the character under the cursor should not be inverted */
3196 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3197# ifdef FEAT_GUI
3198 && !gui.in_use
3199# endif
3200 )
3201 noinvcur = TRUE;
3202#endif
3203
3204 /* if inverting in this line set area_highlighting */
3205 if (fromcol >= 0)
3206 {
3207 area_highlighting = TRUE;
3208 attr = hl_attr(HLF_V);
3209#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003210 if ((clip_star.available && !clip_star.owned
3211 && clip_isautosel_star())
3212 || (clip_plus.available && !clip_plus.owned
3213 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 attr = hl_attr(HLF_VNC);
3215#endif
3216 }
3217 }
3218
3219 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003220 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003222 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 && wp == curwin
3224 && lnum >= curwin->w_cursor.lnum
3225 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3226 {
3227 if (lnum == curwin->w_cursor.lnum)
3228 getvcol(curwin, &(curwin->w_cursor),
3229 (colnr_T *)&fromcol, NULL, NULL);
3230 else
3231 fromcol = 0;
3232 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3233 {
3234 pos.lnum = lnum;
3235 pos.col = search_match_endcol;
3236 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3237 }
3238 else
3239 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003240 /* do at least one character; happens when past end of line */
3241 if (fromcol == tocol)
3242 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 area_highlighting = TRUE;
3244 attr = hl_attr(HLF_I);
3245 }
3246
3247#ifdef FEAT_DIFF
3248 filler_lines = diff_check(wp, lnum);
3249 if (filler_lines < 0)
3250 {
3251 if (filler_lines == -1)
3252 {
3253 if (diff_find_change(wp, lnum, &change_start, &change_end))
3254 diff_hlf = HLF_ADD; /* added line */
3255 else if (change_start == 0)
3256 diff_hlf = HLF_TXD; /* changed text */
3257 else
3258 diff_hlf = HLF_CHD; /* changed line */
3259 }
3260 else
3261 diff_hlf = HLF_ADD; /* added line */
3262 filler_lines = 0;
3263 area_highlighting = TRUE;
3264 }
3265 if (lnum == wp->w_topline)
3266 filler_lines = wp->w_topfill;
3267 filler_todo = filler_lines;
3268#endif
3269
3270#ifdef LINE_ATTR
3271# ifdef FEAT_SIGNS
3272 /* If this line has a sign with line highlighting set line_attr. */
3273 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3274 if (v != 0)
3275 line_attr = sign_get_attr((int)v, TRUE);
3276# endif
3277# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3278 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003279 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 line_attr = hl_attr(HLF_L);
3281# endif
3282 if (line_attr != 0)
3283 area_highlighting = TRUE;
3284#endif
3285
3286 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3287 ptr = line;
3288
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003289#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003290 if (has_spell)
3291 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003292 /* For checking first word with a capital skip white space. */
3293 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003294 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003295
Bram Moolenaar30abd282005-06-22 22:35:10 +00003296 /* To be able to spell-check over line boundaries copy the end of the
3297 * current line into nextline[]. Above the start of the next line was
3298 * copied to nextline[SPWORDLEN]. */
3299 if (nextline[SPWORDLEN] == NUL)
3300 {
3301 /* No next line or it is empty. */
3302 nextlinecol = MAXCOL;
3303 nextline_idx = 0;
3304 }
3305 else
3306 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003307 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003308 if (v < SPWORDLEN)
3309 {
3310 /* Short line, use it completely and append the start of the
3311 * next line. */
3312 nextlinecol = 0;
3313 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003314 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003315 nextline_idx = v + 1;
3316 }
3317 else
3318 {
3319 /* Long line, use only the last SPWORDLEN bytes. */
3320 nextlinecol = v - SPWORDLEN;
3321 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3322 nextline_idx = SPWORDLEN + 1;
3323 }
3324 }
3325 }
3326#endif
3327
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003328 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003330 if (lcs_space || lcs_trail)
3331 extra_check = TRUE;
3332 /* find start of trailing whitespace */
3333 if (lcs_trail)
3334 {
3335 trailcol = (colnr_T)STRLEN(ptr);
3336 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3337 --trailcol;
3338 trailcol += (colnr_T) (ptr - line);
3339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341
3342 /*
3343 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3344 * first character to be displayed.
3345 */
3346 if (wp->w_p_wrap)
3347 v = wp->w_skipcol;
3348 else
3349 v = wp->w_leftcol;
3350 if (v > 0)
3351 {
3352#ifdef FEAT_MBYTE
3353 char_u *prev_ptr = ptr;
3354#endif
3355 while (vcol < v && *ptr != NUL)
3356 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003357 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 vcol += c;
3359#ifdef FEAT_MBYTE
3360 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003362 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003365 /* When:
3366 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003367 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003368 * - 'virtualedit' is set, or
3369 * - the visual mode is active,
3370 * the end of the line may be before the start of the displayed part.
3371 */
3372 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003373#ifdef FEAT_SYN_HL
3374 wp->w_p_cuc || draw_color_col ||
3375#endif
3376#ifdef FEAT_VIRTUALEDIT
3377 virtual_active() ||
3378#endif
3379 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 /* Handle a character that's not completely on the screen: Put ptr at
3385 * that character but skip the first few screen characters. */
3386 if (vcol > v)
3387 {
3388 vcol -= c;
3389#ifdef FEAT_MBYTE
3390 ptr = prev_ptr;
3391#else
3392 --ptr;
3393#endif
3394 n_skip = v - vcol;
3395 }
3396
3397 /*
3398 * Adjust for when the inverted text is before the screen,
3399 * and when the start of the inverted text is before the screen.
3400 */
3401 if (tocol <= vcol)
3402 fromcol = 0;
3403 else if (fromcol >= 0 && fromcol < vcol)
3404 fromcol = vcol;
3405
3406#ifdef FEAT_LINEBREAK
3407 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3408 if (wp->w_p_wrap)
3409 need_showbreak = TRUE;
3410#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003411#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003412 /* When spell checking a word we need to figure out the start of the
3413 * word and if it's badly spelled or not. */
3414 if (has_spell)
3415 {
3416 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003417 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003418 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003419
3420 pos = wp->w_cursor;
3421 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003422 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003423 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003424
3425 /* spell_move_to() may call ml_get() and make "line" invalid */
3426 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3427 ptr = line + linecol;
3428
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003429 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003430 {
3431 /* no bad word found at line start, don't check until end of a
3432 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003433 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003434 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003435 }
3436 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003437 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003438 /* bad word found, use attributes until end of word */
3439 word_end = wp->w_cursor.col + len + 1;
3440
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003441 /* Turn index into actual attributes. */
3442 if (spell_hlf != HLF_COUNT)
3443 spell_attr = highlight_attr[spell_hlf];
3444 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003445 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003446
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003447# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003448 /* Need to restart syntax highlighting for this line. */
3449 if (has_syntax)
3450 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003451# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003452 }
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455
3456 /*
3457 * Correct highlighting for cursor that can't be disabled.
3458 * Avoids having to check this for each character.
3459 */
3460 if (fromcol >= 0)
3461 {
3462 if (noinvcur)
3463 {
3464 if ((colnr_T)fromcol == wp->w_virtcol)
3465 {
3466 /* highlighting starts at cursor, let it start just after the
3467 * cursor */
3468 fromcol_prev = fromcol;
3469 fromcol = -1;
3470 }
3471 else if ((colnr_T)fromcol < wp->w_virtcol)
3472 /* restart highlighting after the cursor */
3473 fromcol_prev = wp->w_virtcol;
3474 }
3475 if (fromcol >= tocol)
3476 fromcol = -1;
3477 }
3478
3479#ifdef FEAT_SEARCH_EXTRA
3480 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003481 * Handle highlighting the last used search pattern and matches.
3482 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003484 cur = wp->w_match_head;
3485 shl_flag = FALSE;
3486 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003488 if (shl_flag == FALSE)
3489 {
3490 shl = &search_hl;
3491 shl_flag = TRUE;
3492 }
3493 else
3494 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003495 shl->startcol = MAXCOL;
3496 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003498 v = (long)(ptr - line);
3499 if (cur != NULL)
3500 cur->pos.cur = 0;
3501 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3502
3503 /* Need to get the line again, a multi-line regexp may have made it
3504 * invalid. */
3505 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3506 ptr = line + v;
3507
3508 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003510 if (shl->lnum == lnum)
3511 shl->startcol = shl->rm.startpos[0].col;
3512 else
3513 shl->startcol = 0;
3514 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3515 - shl->rm.startpos[0].lnum)
3516 shl->endcol = shl->rm.endpos[0].col;
3517 else
3518 shl->endcol = MAXCOL;
3519 /* Highlight one character for an empty match. */
3520 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003523 if (has_mbyte && line[shl->endcol] != NUL)
3524 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003527 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003529 if ((long)shl->startcol < v) /* match at leftcol */
3530 {
3531 shl->attr_cur = shl->attr;
3532 search_attr = shl->attr;
3533 }
3534 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003536 if (shl != &search_hl && cur != NULL)
3537 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539#endif
3540
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003541#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003542 /* Cursor line highlighting for 'cursorline' in the current window. Not
3543 * when Visual mode is active, because it's not clear what is selected
3544 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003545 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003546 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003547 {
3548 line_attr = hl_attr(HLF_CUL);
3549 area_highlighting = TRUE;
3550 }
3551#endif
3552
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003553 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 col = 0;
3555#ifdef FEAT_RIGHTLEFT
3556 if (wp->w_p_rl)
3557 {
3558 /* Rightleft window: process the text in the normal direction, but put
3559 * it in current_ScreenLine[] from right to left. Start at the
3560 * rightmost column of the window. */
3561 col = W_WIDTH(wp) - 1;
3562 off += col;
3563 }
3564#endif
3565
3566 /*
3567 * Repeat for the whole displayed line.
3568 */
3569 for (;;)
3570 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003571#ifdef FEAT_CONCEAL
3572 has_match_conc = FALSE;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 /* Skip this quickly when working on the text. */
3575 if (draw_state != WL_LINE)
3576 {
3577#ifdef FEAT_CMDWIN
3578 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3579 {
3580 draw_state = WL_CMDLINE;
3581 if (cmdwin_type != 0 && wp == curwin)
3582 {
3583 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003585 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_attr = hl_attr(HLF_AT);
3587 }
3588 }
3589#endif
3590
3591#ifdef FEAT_FOLDING
3592 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3593 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003594 int fdc = compute_foldcolumn(wp, 0);
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003597 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 /* Draw the 'foldcolumn'. */
3600 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003601 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003603 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 c_extra = NUL;
3605 char_attr = hl_attr(HLF_FC);
3606 }
3607 }
3608#endif
3609
3610#ifdef FEAT_SIGNS
3611 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3612 {
3613 draw_state = WL_SIGN;
3614 /* Show the sign column when there are any signs in this
3615 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003616 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003618 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003620 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621# endif
3622
3623 /* Draw two cells with the sign value or blank. */
3624 c_extra = ' ';
3625 char_attr = hl_attr(HLF_SC);
3626 n_extra = 2;
3627
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003628 if (row == startrow
3629#ifdef FEAT_DIFF
3630 + filler_lines && filler_todo <= 0
3631#endif
3632 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3635 SIGN_TEXT);
3636# ifdef FEAT_SIGN_ICONS
3637 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3638 SIGN_ICON);
3639 if (gui.in_use && icon_sign != 0)
3640 {
3641 /* Use the image in this position. */
3642 c_extra = SIGN_BYTE;
3643# ifdef FEAT_NETBEANS_INTG
3644 if (buf_signcount(wp->w_buffer, lnum) > 1)
3645 c_extra = MULTISIGN_BYTE;
3646# endif
3647 char_attr = icon_sign;
3648 }
3649 else
3650# endif
3651 if (text_sign != 0)
3652 {
3653 p_extra = sign_get_text(text_sign);
3654 if (p_extra != NULL)
3655 {
3656 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003657 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 char_attr = sign_get_attr(text_sign, FALSE);
3660 }
3661 }
3662 }
3663 }
3664#endif
3665
3666 if (draw_state == WL_NR - 1 && n_extra == 0)
3667 {
3668 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003669 /* Display the absolute or relative line number. After the
3670 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3671 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 && (row == startrow
3673#ifdef FEAT_DIFF
3674 + filler_lines
3675#endif
3676 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3677 {
3678 /* Draw the line number (empty space after wrapping). */
3679 if (row == startrow
3680#ifdef FEAT_DIFF
3681 + filler_lines
3682#endif
3683 )
3684 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003685 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003686 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003687
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003688 if (wp->w_p_nu && !wp->w_p_rnu)
3689 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003690 num = (long)lnum;
3691 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003692 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003693 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003694 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003695 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003696 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003697 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003698 num = lnum;
3699 fmt = "%-*ld ";
3700 }
3701 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003702
Bram Moolenaar700e7342013-01-30 12:31:36 +01003703 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003704 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (wp->w_skipcol > 0)
3706 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3707 *p_extra = '-';
3708#ifdef FEAT_RIGHTLEFT
3709 if (wp->w_p_rl) /* reverse line numbers */
3710 rl_mirror(extra);
3711#endif
3712 p_extra = extra;
3713 c_extra = NUL;
3714 }
3715 else
3716 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003717 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003719#ifdef FEAT_SYN_HL
3720 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003721 * the current line differently.
3722 * TODO: Can we use CursorLine instead of CursorLineNr
3723 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003724 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003725 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003726 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 }
3730
Bram Moolenaar597a4222014-06-25 14:39:50 +02003731#ifdef FEAT_LINEBREAK
3732 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3733 && n_extra == 0 && *p_sbr != NUL)
3734 /* draw indent after showbreak value */
3735 draw_state = WL_BRI;
3736 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3737 /* After the showbreak, draw the breakindent */
3738 draw_state = WL_BRI - 1;
3739
3740 /* draw 'breakindent': indent wrapped text accordingly */
3741 if (draw_state == WL_BRI - 1 && n_extra == 0)
3742 {
3743 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003744 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003745# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003746 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003747# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003748 )
3749 {
3750 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003751# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003752 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003753 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003754 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003755# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003756 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3757 char_attr = hl_combine_attr(char_attr,
3758 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003759# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003760 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003761# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003762 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003763 c_extra = ' ';
3764 n_extra = get_breakindent_win(wp,
3765 ml_get_buf(wp->w_buffer, lnum, FALSE));
3766 /* Correct end of highlighted area for 'breakindent',
3767 * required when 'linebreak' is also set. */
3768 if (tocol == vcol)
3769 tocol += n_extra;
3770 }
3771 }
3772#endif
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3775 if (draw_state == WL_SBR - 1 && n_extra == 0)
3776 {
3777 draw_state = WL_SBR;
3778# ifdef FEAT_DIFF
3779 if (filler_todo > 0)
3780 {
3781 /* Draw "deleted" diff line(s). */
3782 if (char2cells(fill_diff) > 1)
3783 c_extra = '-';
3784 else
3785 c_extra = fill_diff;
3786# ifdef FEAT_RIGHTLEFT
3787 if (wp->w_p_rl)
3788 n_extra = col + 1;
3789 else
3790# endif
3791 n_extra = W_WIDTH(wp) - col;
3792 char_attr = hl_attr(HLF_DED);
3793 }
3794# endif
3795# ifdef FEAT_LINEBREAK
3796 if (*p_sbr != NUL && need_showbreak)
3797 {
3798 /* Draw 'showbreak' at the start of each broken line. */
3799 p_extra = p_sbr;
3800 c_extra = NUL;
3801 n_extra = (int)STRLEN(p_sbr);
3802 char_attr = hl_attr(HLF_AT);
3803 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003804 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 /* Correct end of highlighted area for 'showbreak',
3806 * required when 'linebreak' is also set. */
3807 if (tocol == vcol)
3808 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003809#ifdef FEAT_SYN_HL
3810 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003811 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003812 char_attr = hl_combine_attr(char_attr,
3813 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816# endif
3817 }
3818#endif
3819
3820 if (draw_state == WL_LINE - 1 && n_extra == 0)
3821 {
3822 draw_state = WL_LINE;
3823 if (saved_n_extra)
3824 {
3825 /* Continue item from end of wrapped line. */
3826 n_extra = saved_n_extra;
3827 c_extra = saved_c_extra;
3828 p_extra = saved_p_extra;
3829 char_attr = saved_char_attr;
3830 }
3831 else
3832 char_attr = 0;
3833 }
3834 }
3835
3836 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003837 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003838 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839#ifdef FEAT_DIFF
3840 && filler_todo <= 0
3841#endif
3842 )
3843 {
3844 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3845 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003846 /* Pretend we have finished updating the window. Except when
3847 * 'cursorcolumn' is set. */
3848#ifdef FEAT_SYN_HL
3849 if (wp->w_p_cuc)
3850 row = wp->w_cline_row + wp->w_cline_height;
3851 else
3852#endif
3853 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 break;
3855 }
3856
3857 if (draw_state == WL_LINE && area_highlighting)
3858 {
3859 /* handle Visual or match highlighting in this line */
3860 if (vcol == fromcol
3861#ifdef FEAT_MBYTE
3862 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3863 && (*mb_ptr2cells)(ptr) > 1)
3864#endif
3865 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003866 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 && vcol < tocol))
3868 area_attr = attr; /* start highlighting */
3869 else if (area_attr != 0
3870 && (vcol == tocol
3871 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
3874#ifdef FEAT_SEARCH_EXTRA
3875 if (!n_extra)
3876 {
3877 /*
3878 * Check for start/end of search pattern match.
3879 * After end, check for start/end of next match.
3880 * When another match, have to check for start again.
3881 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003882 * Do this for 'search_hl' and the match list (ordered by
3883 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003885 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003886 cur = wp->w_match_head;
3887 shl_flag = FALSE;
3888 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003890 if (shl_flag == FALSE
3891 && ((cur != NULL
3892 && cur->priority > SEARCH_HL_PRIORITY)
3893 || cur == NULL))
3894 {
3895 shl = &search_hl;
3896 shl_flag = TRUE;
3897 }
3898 else
3899 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003900 if (cur != NULL)
3901 cur->pos.cur = 0;
3902 pos_inprogress = TRUE;
3903 while (shl->rm.regprog != NULL
3904 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003906 if (shl->startcol != MAXCOL
3907 && v >= (long)shl->startcol
3908 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003910#ifdef FEAT_MBYTE
3911 int tmp_col = v + MB_PTR2LEN(ptr);
3912
3913 if (shl->endcol < tmp_col)
3914 shl->endcol = tmp_col;
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003917#ifdef FEAT_CONCEAL
3918 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3919 == cur->hlg_id)
3920 {
3921 has_match_conc = TRUE;
3922 match_conc = cur->conceal_char;
3923 }
3924 else
3925 has_match_conc = match_conc = FALSE;
3926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003928 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
3930 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003931#ifdef FEAT_CONCEAL
3932 prev_syntax_id = 0;
3933#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003934 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3935 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003936 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 /* Need to get the line again, a multi-line regexp
3939 * may have made it invalid. */
3940 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3941 ptr = line + v;
3942
3943 if (shl->lnum == lnum)
3944 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003945 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003947 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003949 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003951 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 /* highlight empty match, try again after
3954 * it */
3955#ifdef FEAT_MBYTE
3956 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003957 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003958 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 else
3960#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003961 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963
3964 /* Loop to check if the match starts at the
3965 * current position */
3966 continue;
3967 }
3968 }
3969 break;
3970 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003971 if (shl != &search_hl && cur != NULL)
3972 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003974
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003975 /* Use attributes from match with highest priority among
3976 * 'search_hl' and the match list. */
3977 search_attr = search_hl.attr_cur;
3978 cur = wp->w_match_head;
3979 shl_flag = FALSE;
3980 while (cur != NULL || shl_flag == FALSE)
3981 {
3982 if (shl_flag == FALSE
3983 && ((cur != NULL
3984 && cur->priority > SEARCH_HL_PRIORITY)
3985 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003986 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003987 shl = &search_hl;
3988 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003989 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003990 else
3991 shl = &cur->hl;
3992 if (shl->attr_cur != 0)
3993 search_attr = shl->attr_cur;
3994 if (shl != &search_hl && cur != NULL)
3995 cur = cur->next;
3996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 }
3998#endif
3999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004001 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004003 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4004 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004006 if (diff_hlf == HLF_TXD && ptr - line > change_end
4007 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004009 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004010 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4011 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004014
4015 /* Decide which of the highlight attributes to use. */
4016 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004017#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004018 if (area_attr != 0)
4019 char_attr = hl_combine_attr(line_attr, area_attr);
4020 else if (search_attr != 0)
4021 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004022 /* Use line_attr when not in the Visual or 'incsearch' area
4023 * (area_attr may be 0 when "noinvcur" is set). */
4024 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004025 || vcol < fromcol || vcol_prev < fromcol_prev
4026 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004027 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004028#else
4029 if (area_attr != 0)
4030 char_attr = area_attr;
4031 else if (search_attr != 0)
4032 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033#endif
4034 else
4035 {
4036 attr_pri = FALSE;
4037#ifdef FEAT_SYN_HL
4038 if (has_syntax)
4039 char_attr = syntax_attr;
4040 else
4041#endif
4042 char_attr = 0;
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045
4046 /*
4047 * Get the next character to put on the screen.
4048 */
4049 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004050 * The "p_extra" points to the extra stuff that is inserted to
4051 * represent special characters (non-printable stuff) and other
4052 * things. When all characters are the same, c_extra is used.
4053 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4054 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4056 */
4057 if (n_extra > 0)
4058 {
4059 if (c_extra != NUL)
4060 {
4061 c = c_extra;
4062#ifdef FEAT_MBYTE
4063 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4064 if (enc_utf8 && (*mb_char2len)(c) > 1)
4065 {
4066 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004067 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
4071 mb_utf8 = FALSE;
4072#endif
4073 }
4074 else
4075 {
4076 c = *p_extra;
4077#ifdef FEAT_MBYTE
4078 if (has_mbyte)
4079 {
4080 mb_c = c;
4081 if (enc_utf8)
4082 {
4083 /* If the UTF-8 character is more than one byte:
4084 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004085 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 mb_utf8 = FALSE;
4087 if (mb_l > n_extra)
4088 mb_l = 1;
4089 else if (mb_l > 1)
4090 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004091 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004093 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 }
4096 else
4097 {
4098 /* if this is a DBCS character, put it in "mb_c" */
4099 mb_l = MB_BYTE2LEN(c);
4100 if (mb_l >= n_extra)
4101 mb_l = 1;
4102 else if (mb_l > 1)
4103 mb_c = (c << 8) + p_extra[1];
4104 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004105 if (mb_l == 0) /* at the NUL at end-of-line */
4106 mb_l = 1;
4107
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 /* If a double-width char doesn't fit display a '>' in the
4109 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004110 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111# ifdef FEAT_RIGHTLEFT
4112 wp->w_p_rl ? (col <= 0) :
4113# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004114 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 && (*mb_char2cells)(mb_c) == 2)
4116 {
4117 c = '>';
4118 mb_c = c;
4119 mb_l = 1;
4120 mb_utf8 = FALSE;
4121 multi_attr = hl_attr(HLF_AT);
4122 /* put the pointer back to output the double-width
4123 * character at the start of the next line. */
4124 ++n_extra;
4125 --p_extra;
4126 }
4127 else
4128 {
4129 n_extra -= mb_l - 1;
4130 p_extra += mb_l - 1;
4131 }
4132 }
4133#endif
4134 ++p_extra;
4135 }
4136 --n_extra;
4137 }
4138 else
4139 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004140 if (p_extra_free != NULL)
4141 {
4142 vim_free(p_extra_free);
4143 p_extra_free = NULL;
4144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 /*
4146 * Get a character from the line itself.
4147 */
4148 c = *ptr;
4149#ifdef FEAT_MBYTE
4150 if (has_mbyte)
4151 {
4152 mb_c = c;
4153 if (enc_utf8)
4154 {
4155 /* If the UTF-8 character is more than one byte: Decode it
4156 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004157 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 mb_utf8 = FALSE;
4159 if (mb_l > 1)
4160 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004161 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 /* Overlong encoded ASCII or ASCII with composing char
4163 * is displayed normally, except a NUL. */
4164 if (mb_c < 0x80)
4165 c = mb_c;
4166 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004167
4168 /* At start of the line we can have a composing char.
4169 * Draw it as a space with a composing char. */
4170 if (utf_iscomposing(mb_c))
4171 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004172 int i;
4173
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004174 for (i = Screen_mco - 1; i > 0; --i)
4175 u8cc[i] = u8cc[i - 1];
4176 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004177 mb_c = ' ';
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180
4181 if ((mb_l == 1 && c >= 0x80)
4182 || (mb_l >= 1 && mb_c == 0)
4183 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004184# ifdef UNICODE16
4185 || mb_c >= 0x10000
4186# endif
4187 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 /*
4190 * Illegal UTF-8 byte: display as <xx>.
4191 * Non-BMP character : display as ? or fullwidth ?.
4192 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004193# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004198# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 if (wp->w_p_rl) /* reverse */
4200 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004203# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 else if (utf_char2cells(mb_c) != 2)
4205 STRCPY(extra, "?");
4206 else
4207 /* 0xff1f in UTF-8: full-width '?' */
4208 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 p_extra = extra;
4212 c = *p_extra;
4213 mb_c = mb_ptr2char_adv(&p_extra);
4214 mb_utf8 = (c >= 0x80);
4215 n_extra = (int)STRLEN(p_extra);
4216 c_extra = NUL;
4217 if (area_attr == 0 && search_attr == 0)
4218 {
4219 n_attr = n_extra + 1;
4220 extra_attr = hl_attr(HLF_8);
4221 saved_attr2 = char_attr; /* save current attr */
4222 }
4223 }
4224 else if (mb_l == 0) /* at the NUL at end-of-line */
4225 mb_l = 1;
4226#ifdef FEAT_ARABIC
4227 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4228 {
4229 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 int pc, pc1, nc;
4231 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 /* The idea of what is the previous and next
4234 * character depends on 'rightleft'. */
4235 if (wp->w_p_rl)
4236 {
4237 pc = prev_c;
4238 pc1 = prev_c1;
4239 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004240 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242 else
4243 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004246 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 prev_c = mb_c;
4249
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004250 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252 else
4253 prev_c = mb_c;
4254#endif
4255 }
4256 else /* enc_dbcs */
4257 {
4258 mb_l = MB_BYTE2LEN(c);
4259 if (mb_l == 0) /* at the NUL at end-of-line */
4260 mb_l = 1;
4261 else if (mb_l > 1)
4262 {
4263 /* We assume a second byte below 32 is illegal.
4264 * Hopefully this is OK for all double-byte encodings!
4265 */
4266 if (ptr[1] >= 32)
4267 mb_c = (c << 8) + ptr[1];
4268 else
4269 {
4270 if (ptr[1] == NUL)
4271 {
4272 /* head byte at end of line */
4273 mb_l = 1;
4274 transchar_nonprint(extra, c);
4275 }
4276 else
4277 {
4278 /* illegal tail byte */
4279 mb_l = 2;
4280 STRCPY(extra, "XX");
4281 }
4282 p_extra = extra;
4283 n_extra = (int)STRLEN(extra) - 1;
4284 c_extra = NUL;
4285 c = *p_extra++;
4286 if (area_attr == 0 && search_attr == 0)
4287 {
4288 n_attr = n_extra + 1;
4289 extra_attr = hl_attr(HLF_8);
4290 saved_attr2 = char_attr; /* save current attr */
4291 }
4292 mb_c = c;
4293 }
4294 }
4295 }
4296 /* If a double-width char doesn't fit display a '>' in the
4297 * last column; the character is displayed at the start of the
4298 * next line. */
4299 if ((
4300# ifdef FEAT_RIGHTLEFT
4301 wp->w_p_rl ? (col <= 0) :
4302# endif
4303 (col >= W_WIDTH(wp) - 1))
4304 && (*mb_char2cells)(mb_c) == 2)
4305 {
4306 c = '>';
4307 mb_c = c;
4308 mb_utf8 = FALSE;
4309 mb_l = 1;
4310 multi_attr = hl_attr(HLF_AT);
4311 /* Put pointer back so that the character will be
4312 * displayed at the start of the next line. */
4313 --ptr;
4314 }
4315 else if (*ptr != NUL)
4316 ptr += mb_l - 1;
4317
4318 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004319 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004320 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004321 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004324 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 c = ' ';
4326 if (area_attr == 0 && search_attr == 0)
4327 {
4328 n_attr = n_extra + 1;
4329 extra_attr = hl_attr(HLF_AT);
4330 saved_attr2 = char_attr; /* save current attr */
4331 }
4332 mb_c = c;
4333 mb_utf8 = FALSE;
4334 mb_l = 1;
4335 }
4336
4337 }
4338#endif
4339 ++ptr;
4340
4341 if (extra_check)
4342 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004343#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004344 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004345#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004346
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004347#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /* Get syntax attribute, unless still at the start of the line
4349 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004350 v = (long)(ptr - line);
4351 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 /* Get the syntax attribute for the character. If there
4354 * is an error, disable syntax highlighting. */
4355 save_did_emsg = did_emsg;
4356 did_emsg = FALSE;
4357
Bram Moolenaar217ad922005-03-20 22:37:15 +00004358 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004359# ifdef FEAT_SPELL
4360 has_spell ? &can_spell :
4361# endif
4362 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004365 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004366 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004367 has_syntax = FALSE;
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 else
4370 did_emsg = save_did_emsg;
4371
4372 /* Need to get the line again, a multi-line regexp may
4373 * have made it invalid. */
4374 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4375 ptr = line + v;
4376
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004377 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004379 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004380 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004381# ifdef FEAT_CONCEAL
4382 /* no concealing past the end of the line, it interferes
4383 * with line highlighting */
4384 if (c == NUL)
4385 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004386 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004387 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004390#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004391
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004392#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004393 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004394 * Only do this when there is no syntax highlighting, the
4395 * @Spell cluster is not used or the current syntax item
4396 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004397 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004398 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004399 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004400# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004401 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004402 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004403# endif
4404 if (c != 0 && (
4405# ifdef FEAT_SYN_HL
4406 !has_syntax ||
4407# endif
4408 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004409 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004410 char_u *prev_ptr, *p;
4411 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004412 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004413# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004414 if (has_mbyte)
4415 {
4416 prev_ptr = ptr - mb_l;
4417 v -= mb_l - 1;
4418 }
4419 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004420# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004421 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004422
4423 /* Use nextline[] if possible, it has the start of the
4424 * next line concatenated. */
4425 if ((prev_ptr - line) - nextlinecol >= 0)
4426 p = nextline + (prev_ptr - line) - nextlinecol;
4427 else
4428 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004429 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004430 len = spell_check(wp, p, &spell_hlf, &cap_col,
4431 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004432 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004433
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004434 /* In Insert mode only highlight a word that
4435 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004436 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004437 && (State & INSERT) != 0
4438 && wp->w_cursor.lnum == lnum
4439 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004440 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004441 && wp->w_cursor.col < (colnr_T)word_end)
4442 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004443 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004444 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004445 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004446
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004447 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004448 && (p - nextline) + len > nextline_idx)
4449 {
4450 /* Remember that the good word continues at the
4451 * start of the next line. */
4452 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004453 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004454 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004455
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004456 /* Turn index into actual attributes. */
4457 if (spell_hlf != HLF_COUNT)
4458 spell_attr = highlight_attr[spell_hlf];
4459
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004460 if (cap_col > 0)
4461 {
4462 if (p != prev_ptr
4463 && (p - nextline) + cap_col >= nextline_idx)
4464 {
4465 /* Remember that the word in the next line
4466 * must start with a capital. */
4467 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004468 cap_col = (int)((p - nextline) + cap_col
4469 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004470 }
4471 else
4472 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004473 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004474 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004475 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004476 }
4477 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004478 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004479 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004480 char_attr = hl_combine_attr(char_attr, spell_attr);
4481 else
4482 char_attr = hl_combine_attr(spell_attr, char_attr);
4483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#endif
4485#ifdef FEAT_LINEBREAK
4486 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004487 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004489 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004491# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004492 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004493# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004494 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004496 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004498 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004499
Bram Moolenaar597a4222014-06-25 14:39:50 +02004500 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004501 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004502 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004503 if (c == TAB && n_extra + col > W_WIDTH(wp))
4504 n_extra = (int)wp->w_buffer->b_p_ts
4505 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4506
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004507# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004508 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004509# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004513 {
4514#ifdef FEAT_CONCEAL
4515 if (c == TAB)
4516 /* See "Tab alignment" below. */
4517 FIX_FOR_BOGUSCOLS;
4518#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004519 if (!wp->w_p_list)
4520 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523#endif
4524
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004525 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4526 */
4527 if (wp->w_p_list
4528 && (((c == 160
4529#ifdef FEAT_MBYTE
4530 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4531#endif
4532 ) && lcs_nbsp)
4533 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4534 {
4535 c = (c == ' ') ? lcs_space : lcs_nbsp;
4536 if (area_attr == 0 && search_attr == 0)
4537 {
4538 n_attr = 1;
4539 extra_attr = hl_attr(HLF_8);
4540 saved_attr2 = char_attr; /* save current attr */
4541 }
4542#ifdef FEAT_MBYTE
4543 mb_c = c;
4544 if (enc_utf8 && (*mb_char2len)(c) > 1)
4545 {
4546 mb_utf8 = TRUE;
4547 u8cc[0] = 0;
4548 c = 0xc0;
4549 }
4550 else
4551 mb_utf8 = FALSE;
4552#endif
4553 }
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4556 {
4557 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004558 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
4560 n_attr = 1;
4561 extra_attr = hl_attr(HLF_8);
4562 saved_attr2 = char_attr; /* save current attr */
4563 }
4564#ifdef FEAT_MBYTE
4565 mb_c = c;
4566 if (enc_utf8 && (*mb_char2len)(c) > 1)
4567 {
4568 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004569 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004570 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572 else
4573 mb_utf8 = FALSE;
4574#endif
4575 }
4576 }
4577
4578 /*
4579 * Handling of non-printable characters.
4580 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004581 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
4583 /*
4584 * when getting a character from the file, we may have to
4585 * turn it into something else on the way to putting it
4586 * into "ScreenLines".
4587 */
4588 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4589 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004590 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004591 long vcol_adjusted = vcol; /* removed showbreak length */
4592#ifdef FEAT_LINEBREAK
4593 /* only adjust the tab_len, when at the first column
4594 * after the showbreak value was drawn */
4595 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4596 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004599 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004600 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4601
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004602#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004603 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004604#endif
4605 /* tab amount depends on current column */
4606 n_extra = tab_len;
4607#ifdef FEAT_LINEBREAK
4608 else
4609 {
4610 char_u *p;
4611 int len = n_extra;
4612 int i;
4613 int saved_nextra = n_extra;
4614
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004615#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004616 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004617 /* there are characters to conceal */
4618 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004619 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4620 */
4621 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4622 && n_extra > tab_len)
4623 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004624#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004625
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004626 /* if n_extra > 0, it gives the number of chars, to
4627 * use for a tab, else we need to calculate the width
4628 * for a tab */
4629#ifdef FEAT_MBYTE
4630 len = (tab_len * mb_char2len(lcs_tab2));
4631 if (n_extra > 0)
4632 len += n_extra - tab_len;
4633#endif
4634 c = lcs_tab1;
4635 p = alloc((unsigned)(len + 1));
4636 vim_memset(p, ' ', len);
4637 p[len] = NUL;
4638 p_extra_free = p;
4639 for (i = 0; i < tab_len; i++)
4640 {
4641#ifdef FEAT_MBYTE
4642 mb_char2bytes(lcs_tab2, p);
4643 p += mb_char2len(lcs_tab2);
4644 n_extra += mb_char2len(lcs_tab2)
4645 - (saved_nextra > 0 ? 1 : 0);
4646#else
4647 p[i] = lcs_tab2;
4648#endif
4649 }
4650 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004651#ifdef FEAT_CONCEAL
4652 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4653 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004654 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004655 n_extra -= vcol_off;
4656#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004657 }
4658#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004659#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004660 {
4661 int vc_saved = vcol_off;
4662
4663 /* Tab alignment should be identical regardless of
4664 * 'conceallevel' value. So tab compensates of all
4665 * previous concealed characters, and thus resets
4666 * vcol_off and boguscols accumulated so far in the
4667 * line. Note that the tab can be longer than
4668 * 'tabstop' when there are concealed characters. */
4669 FIX_FOR_BOGUSCOLS;
4670
4671 /* Make sure, the highlighting for the tab char will be
4672 * correctly set further below (effectively reverts the
4673 * FIX_FOR_BOGSUCOLS macro */
4674 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004675 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004676 tab_len += vc_saved;
4677 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#ifdef FEAT_MBYTE
4680 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4681#endif
4682 if (wp->w_p_list)
4683 {
4684 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004685#ifdef FEAT_LINEBREAK
4686 if (wp->w_p_lbr)
4687 c_extra = NUL; /* using p_extra from above */
4688 else
4689#endif
4690 c_extra = lcs_tab2;
4691 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 extra_attr = hl_attr(HLF_8);
4693 saved_attr2 = char_attr; /* save current attr */
4694#ifdef FEAT_MBYTE
4695 mb_c = c;
4696 if (enc_utf8 && (*mb_char2len)(c) > 1)
4697 {
4698 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004699 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004700 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702#endif
4703 }
4704 else
4705 {
4706 c_extra = ' ';
4707 c = ' ';
4708 }
4709 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004710 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004711 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004712 || ((fromcol >= 0 || fromcol_prev >= 0)
4713 && tocol > vcol
4714 && VIsual_mode != Ctrl_V
4715 && (
4716# ifdef FEAT_RIGHTLEFT
4717 wp->w_p_rl ? (col >= 0) :
4718# endif
4719 (col < W_WIDTH(wp)))
4720 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004721 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004722 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004723 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004725 /* Display a '$' after the line or highlight an extra
4726 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4728 /* For a diff line the highlighting continues after the
4729 * "$". */
4730 if (
4731# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004732 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733# ifdef LINE_ATTR
4734 &&
4735# endif
4736# endif
4737# ifdef LINE_ATTR
4738 line_attr == 0
4739# endif
4740 )
4741#endif
4742 {
4743#ifdef FEAT_VIRTUALEDIT
4744 /* In virtualedit, visual selections may extend
4745 * beyond end of line. */
4746 if (area_highlighting && virtual_active()
4747 && tocol != MAXCOL && vcol < tocol)
4748 n_extra = 0;
4749 else
4750#endif
4751 {
4752 p_extra = at_end_str;
4753 n_extra = 1;
4754 c_extra = NUL;
4755 }
4756 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004757 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004758 c = lcs_eol;
4759 else
4760 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 lcs_eol_one = -1;
4762 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004763 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 extra_attr = hl_attr(HLF_AT);
4766 n_attr = 1;
4767 }
4768#ifdef FEAT_MBYTE
4769 mb_c = c;
4770 if (enc_utf8 && (*mb_char2len)(c) > 1)
4771 {
4772 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004773 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004774 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 else
4777 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4778#endif
4779 }
4780 else if (c != NUL)
4781 {
4782 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004783 if (n_extra == 0)
4784 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#ifdef FEAT_RIGHTLEFT
4786 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4787 rl_mirror(p_extra); /* reverse "<12>" */
4788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004790#ifdef FEAT_LINEBREAK
4791 if (wp->w_p_lbr)
4792 {
4793 char_u *p;
4794
4795 c = *p_extra;
4796 p = alloc((unsigned)n_extra + 1);
4797 vim_memset(p, ' ', n_extra);
4798 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4799 p[n_extra] = NUL;
4800 p_extra_free = p_extra = p;
4801 }
4802 else
4803#endif
4804 {
4805 n_extra = byte2cells(c) - 1;
4806 c = *p_extra++;
4807 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004808 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 n_attr = n_extra + 1;
4811 extra_attr = hl_attr(HLF_8);
4812 saved_attr2 = char_attr; /* save current attr */
4813 }
4814#ifdef FEAT_MBYTE
4815 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4816#endif
4817 }
4818#ifdef FEAT_VIRTUALEDIT
4819 else if (VIsual_active
4820 && (VIsual_mode == Ctrl_V
4821 || VIsual_mode == 'v')
4822 && virtual_active()
4823 && tocol != MAXCOL
4824 && vcol < tocol
4825 && (
4826# ifdef FEAT_RIGHTLEFT
4827 wp->w_p_rl ? (col >= 0) :
4828# endif
4829 (col < W_WIDTH(wp))))
4830 {
4831 c = ' ';
4832 --ptr; /* put it back at the NUL */
4833 }
4834#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004835#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 else if ((
4837# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004838 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 ) && (
4842# ifdef FEAT_RIGHTLEFT
4843 wp->w_p_rl ? (col >= 0) :
4844# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004845 (col
4846# ifdef FEAT_CONCEAL
4847 - boguscols
4848# endif
4849 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 /* Highlight until the right side of the window */
4852 c = ' ';
4853 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004854
4855 /* Remember we do the char for line highlighting. */
4856 ++did_line_attr;
4857
4858 /* don't do search HL for the rest of the line */
4859 if (line_attr != 0 && char_attr == search_attr && col > 0)
4860 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861# ifdef FEAT_DIFF
4862 if (diff_hlf == HLF_TXD)
4863 {
4864 diff_hlf = HLF_CHD;
4865 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004868 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4869 char_attr = hl_combine_attr(char_attr,
4870 hl_attr(HLF_CUL));
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873# endif
4874 }
4875#endif
4876 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004877
4878#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004879 if ( wp->w_p_cole > 0
4880 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004881 conceal_cursor_line(wp) )
4882 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004883 && !(lnum_in_visual_area
4884 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004885 {
4886 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004887 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004888 && (syn_get_sub_char() != NUL || match_conc
4889 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004890 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004891 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004892 /* First time at this concealed item: display one
4893 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004894 if (match_conc)
4895 c = match_conc;
4896 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004897 c = syn_get_sub_char();
4898 else if (lcs_conceal != NUL)
4899 c = lcs_conceal;
4900 else
4901 c = ' ';
4902
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004903 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004904
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004905 if (n_extra > 0)
4906 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004907 vcol += n_extra;
4908 if (wp->w_p_wrap && n_extra > 0)
4909 {
4910# ifdef FEAT_RIGHTLEFT
4911 if (wp->w_p_rl)
4912 {
4913 col -= n_extra;
4914 boguscols -= n_extra;
4915 }
4916 else
4917# endif
4918 {
4919 boguscols += n_extra;
4920 col += n_extra;
4921 }
4922 }
4923 n_extra = 0;
4924 n_attr = 0;
4925 }
4926 else if (n_skip == 0)
4927 {
4928 is_concealing = TRUE;
4929 n_skip = 1;
4930 }
4931# ifdef FEAT_MBYTE
4932 mb_c = c;
4933 if (enc_utf8 && (*mb_char2len)(c) > 1)
4934 {
4935 mb_utf8 = TRUE;
4936 u8cc[0] = 0;
4937 c = 0xc0;
4938 }
4939 else
4940 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4941# endif
4942 }
4943 else
4944 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004945 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004946 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004947 }
4948#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004951#ifdef FEAT_CONCEAL
4952 /* In the cursor line and we may be concealing characters: correct
4953 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004954 if (!did_wcol && draw_state == WL_LINE
4955 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004956 && conceal_cursor_line(wp)
4957 && (int)wp->w_virtcol <= vcol + n_skip)
4958 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01004959# ifdef FEAT_RIGHTLEFT
4960 if (wp->w_p_rl)
4961 wp->w_wcol = W_WIDTH(wp) - col + boguscols - 1;
4962 else
4963# endif
4964 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004965 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004966 did_wcol = TRUE;
4967 }
4968#endif
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 /* Don't override visual selection highlighting. */
4971 if (n_attr > 0
4972 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004973 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 char_attr = extra_attr;
4975
Bram Moolenaar81695252004-12-29 20:58:21 +00004976#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /* XIM don't send preedit_start and preedit_end, but they send
4978 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4979 * im_is_preediting() here. */
4980 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004981 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 && (State & INSERT)
4983 && !p_imdisable
4984 && im_is_preediting()
4985 && draw_state == WL_LINE)
4986 {
4987 colnr_T tcol;
4988
4989 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004990 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 else
4992 tcol = preedit_end_col;
4993 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4994 {
4995 if (feedback_old_attr < 0)
4996 {
4997 feedback_col = 0;
4998 feedback_old_attr = char_attr;
4999 }
5000 char_attr = im_get_feedback_attr(feedback_col);
5001 if (char_attr < 0)
5002 char_attr = feedback_old_attr;
5003 feedback_col++;
5004 }
5005 else if (feedback_old_attr >= 0)
5006 {
5007 char_attr = feedback_old_attr;
5008 feedback_old_attr = -1;
5009 feedback_col = 0;
5010 }
5011 }
5012#endif
5013 /*
5014 * Handle the case where we are in column 0 but not on the first
5015 * character of the line and the user wants us to show us a
5016 * special character (via 'listchars' option "precedes:<char>".
5017 */
5018 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005019 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5021#ifdef FEAT_DIFF
5022 && filler_todo <= 0
5023#endif
5024 && draw_state > WL_NR
5025 && c != NUL)
5026 {
5027 c = lcs_prec;
5028 lcs_prec_todo = NUL;
5029#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005030 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5031 {
5032 /* Double-width character being overwritten by the "precedes"
5033 * character, need to fill up half the character. */
5034 c_extra = MB_FILLER_CHAR;
5035 n_extra = 1;
5036 n_attr = 2;
5037 extra_attr = hl_attr(HLF_AT);
5038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 mb_c = c;
5040 if (enc_utf8 && (*mb_char2len)(c) > 1)
5041 {
5042 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005043 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005044 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 else
5047 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5048#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005049 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
5051 saved_attr3 = char_attr; /* save current attr */
5052 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5053 n_attr3 = 1;
5054 }
5055 }
5056
5057 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005058 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005060 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005061#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005062 || did_line_attr == 1
5063#endif
5064 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005066#ifdef FEAT_SEARCH_EXTRA
5067 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005068
5069 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005070 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005071 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005072#endif
5073
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005074 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 * highlight match at end of line. If it's beyond the last
5076 * char on the screen, just overwrite that one (tricky!) Not
5077 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005078#ifdef FEAT_SEARCH_EXTRA
5079 prevcol_hl_flag = FALSE;
5080 if (prevcol == (long)search_hl.startcol)
5081 prevcol_hl_flag = TRUE;
5082 else
5083 {
5084 cur = wp->w_match_head;
5085 while (cur != NULL)
5086 {
5087 if (prevcol == (long)cur->hl.startcol)
5088 {
5089 prevcol_hl_flag = TRUE;
5090 break;
5091 }
5092 cur = cur->next;
5093 }
5094 }
5095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005097 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005098 && (VIsual_mode != Ctrl_V
5099 || lnum == VIsual.lnum
5100 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005101 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#ifdef FEAT_SEARCH_EXTRA
5103 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005104 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005105# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005106 && did_line_attr <= 1
5107# endif
5108 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#endif
5110 ))
5111 {
5112 int n = 0;
5113
5114#ifdef FEAT_RIGHTLEFT
5115 if (wp->w_p_rl)
5116 {
5117 if (col < 0)
5118 n = 1;
5119 }
5120 else
5121#endif
5122 {
5123 if (col >= W_WIDTH(wp))
5124 n = -1;
5125 }
5126 if (n != 0)
5127 {
5128 /* At the window boundary, highlight the last character
5129 * instead (better than nothing). */
5130 off += n;
5131 col += n;
5132 }
5133 else
5134 {
5135 /* Add a blank character to highlight. */
5136 ScreenLines[off] = ' ';
5137#ifdef FEAT_MBYTE
5138 if (enc_utf8)
5139 ScreenLinesUC[off] = 0;
5140#endif
5141 }
5142#ifdef FEAT_SEARCH_EXTRA
5143 if (area_attr == 0)
5144 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005145 /* Use attributes from match with highest priority among
5146 * 'search_hl' and the match list. */
5147 char_attr = search_hl.attr;
5148 cur = wp->w_match_head;
5149 shl_flag = FALSE;
5150 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005151 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005152 if (shl_flag == FALSE
5153 && ((cur != NULL
5154 && cur->priority > SEARCH_HL_PRIORITY)
5155 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005156 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005157 shl = &search_hl;
5158 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005159 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005160 else
5161 shl = &cur->hl;
5162 if ((ptr - line) - 1 == (long)shl->startcol)
5163 char_attr = shl->attr;
5164 if (shl != &search_hl && cur != NULL)
5165 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168#endif
5169 ScreenAttrs[off] = char_attr;
5170#ifdef FEAT_RIGHTLEFT
5171 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005174 --off;
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 else
5177#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005180 ++off;
5181 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005182 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005183#ifdef FEAT_SYN_HL
5184 eol_hl_off = 1;
5185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
Bram Moolenaar91170f82006-05-05 21:15:17 +00005189 /*
5190 * At end of the text line.
5191 */
5192 if (c == NUL)
5193 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005194#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005195 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5196 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005197 {
5198 /* highlight last char after line */
5199 --col;
5200 --off;
5201 --vcol;
5202 }
5203
Bram Moolenaar1a384422010-07-14 19:53:30 +02005204 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005205 if (wp->w_p_wrap)
5206 v = wp->w_skipcol;
5207 else
5208 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005209
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005210 /* check if line ends before left margin */
5211 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005212 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005213#ifdef FEAT_CONCEAL
5214 /* Get rid of the boguscols now, we want to draw until the right
5215 * edge for 'cursorcolumn'. */
5216 col -= boguscols;
5217 boguscols = 0;
5218#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005219
5220 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005221 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005222
5223 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005224 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5225 && (int)wp->w_virtcol <
5226 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005227 && lnum != wp->w_cursor.lnum)
5228 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005229# ifdef FEAT_RIGHTLEFT
5230 && !wp->w_p_rl
5231# endif
5232 )
5233 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005234 int rightmost_vcol = 0;
5235 int i;
5236
5237 if (wp->w_p_cuc)
5238 rightmost_vcol = wp->w_virtcol;
5239 if (draw_color_col)
5240 /* determine rightmost colorcolumn to possibly draw */
5241 for (i = 0; color_cols[i] >= 0; ++i)
5242 if (rightmost_vcol < color_cols[i])
5243 rightmost_vcol = color_cols[i];
5244
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005245 while (col < W_WIDTH(wp))
5246 {
5247 ScreenLines[off] = ' ';
5248#ifdef FEAT_MBYTE
5249 if (enc_utf8)
5250 ScreenLinesUC[off] = 0;
5251#endif
5252 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005253 if (draw_color_col)
5254 draw_color_col = advance_color_col(VCOL_HLC,
5255 &color_cols);
5256
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005257 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005258 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005259 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005260 ScreenAttrs[off++] = hl_attr(HLF_MC);
5261 else
5262 ScreenAttrs[off++] = 0;
5263
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005264 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005265 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005266
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005267 ++vcol;
5268 }
5269 }
5270#endif
5271
Bram Moolenaar860cae12010-06-05 23:22:07 +02005272 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5273 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 row++;
5275
5276 /*
5277 * Update w_cline_height and w_cline_folded if the cursor line was
5278 * updated (saves a call to plines() later).
5279 */
5280 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5281 {
5282 curwin->w_cline_row = startrow;
5283 curwin->w_cline_height = row - startrow;
5284#ifdef FEAT_FOLDING
5285 curwin->w_cline_folded = FALSE;
5286#endif
5287 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5288 }
5289
5290 break;
5291 }
5292
5293 /* line continues beyond line end */
5294 if (lcs_ext
5295 && !wp->w_p_wrap
5296#ifdef FEAT_DIFF
5297 && filler_todo <= 0
5298#endif
5299 && (
5300#ifdef FEAT_RIGHTLEFT
5301 wp->w_p_rl ? col == 0 :
5302#endif
5303 col == W_WIDTH(wp) - 1)
5304 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005305 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5307 {
5308 c = lcs_ext;
5309 char_attr = hl_attr(HLF_AT);
5310#ifdef FEAT_MBYTE
5311 mb_c = c;
5312 if (enc_utf8 && (*mb_char2len)(c) > 1)
5313 {
5314 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005315 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005316 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318 else
5319 mb_utf8 = FALSE;
5320#endif
5321 }
5322
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005323#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005324 /* advance to the next 'colorcolumn' */
5325 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005326 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005327
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005328 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005329 * highlight the cursor position itself.
5330 * Also highlight the 'colorcolumn' if it is different than
5331 * 'cursorcolumn' */
5332 vcol_save_attr = -1;
5333 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005334 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005335 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005336 && lnum != wp->w_cursor.lnum)
5337 {
5338 vcol_save_attr = char_attr;
5339 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5340 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005341 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005342 {
5343 vcol_save_attr = char_attr;
5344 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5345 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005346 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005347#endif
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 /*
5350 * Store character to be displayed.
5351 * Skip characters that are left of the screen for 'nowrap'.
5352 */
5353 vcol_prev = vcol;
5354 if (draw_state < WL_LINE || n_skip <= 0)
5355 {
5356 /*
5357 * Store the character.
5358 */
5359#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5360 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5361 {
5362 /* A double-wide character is: put first halve in left cell. */
5363 --off;
5364 --col;
5365 }
5366#endif
5367 ScreenLines[off] = c;
5368#ifdef FEAT_MBYTE
5369 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005370 {
5371 if ((mb_c & 0xff00) == 0x8e00)
5372 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 else if (enc_utf8)
5376 {
5377 if (mb_utf8)
5378 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005379 int i;
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005382 if ((c & 0xff) == 0)
5383 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005384 for (i = 0; i < Screen_mco; ++i)
5385 {
5386 ScreenLinesC[i][off] = u8cc[i];
5387 if (u8cc[i] == 0)
5388 break;
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 else
5392 ScreenLinesUC[off] = 0;
5393 }
5394 if (multi_attr)
5395 {
5396 ScreenAttrs[off] = multi_attr;
5397 multi_attr = 0;
5398 }
5399 else
5400#endif
5401 ScreenAttrs[off] = char_attr;
5402
5403#ifdef FEAT_MBYTE
5404 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5405 {
5406 /* Need to fill two screen columns. */
5407 ++off;
5408 ++col;
5409 if (enc_utf8)
5410 /* UTF-8: Put a 0 in the second screen char. */
5411 ScreenLines[off] = 0;
5412 else
5413 /* DBCS: Put second byte in the second screen char. */
5414 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005415 if (draw_state > WL_NR
5416#ifdef FEAT_DIFF
5417 && filler_todo <= 0
5418#endif
5419 )
5420 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 /* When "tocol" is halfway a character, set it to the end of
5422 * the character, otherwise highlighting won't stop. */
5423 if (tocol == vcol)
5424 ++tocol;
5425#ifdef FEAT_RIGHTLEFT
5426 if (wp->w_p_rl)
5427 {
5428 /* now it's time to backup one cell */
5429 --off;
5430 --col;
5431 }
5432#endif
5433 }
5434#endif
5435#ifdef FEAT_RIGHTLEFT
5436 if (wp->w_p_rl)
5437 {
5438 --off;
5439 --col;
5440 }
5441 else
5442#endif
5443 {
5444 ++off;
5445 ++col;
5446 }
5447 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005448#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005449 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005450 {
5451 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005452 ++vcol_off;
5453 if (n_extra > 0)
5454 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005455 if (wp->w_p_wrap)
5456 {
5457 /*
5458 * Special voodoo required if 'wrap' is on.
5459 *
5460 * Advance the column indicator to force the line
5461 * drawing to wrap early. This will make the line
5462 * take up the same screen space when parts are concealed,
5463 * so that cursor line computations aren't messed up.
5464 *
5465 * To avoid the fictitious advance of 'col' causing
5466 * trailing junk to be written out of the screen line
5467 * we are building, 'boguscols' keeps track of the number
5468 * of bad columns we have advanced.
5469 */
5470 if (n_extra > 0)
5471 {
5472 vcol += n_extra;
5473# ifdef FEAT_RIGHTLEFT
5474 if (wp->w_p_rl)
5475 {
5476 col -= n_extra;
5477 boguscols -= n_extra;
5478 }
5479 else
5480# endif
5481 {
5482 col += n_extra;
5483 boguscols += n_extra;
5484 }
5485 n_extra = 0;
5486 n_attr = 0;
5487 }
5488
5489
5490# ifdef FEAT_MBYTE
5491 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5492 {
5493 /* Need to fill two screen columns. */
5494# ifdef FEAT_RIGHTLEFT
5495 if (wp->w_p_rl)
5496 {
5497 --boguscols;
5498 --col;
5499 }
5500 else
5501# endif
5502 {
5503 ++boguscols;
5504 ++col;
5505 }
5506 }
5507# endif
5508
5509# ifdef FEAT_RIGHTLEFT
5510 if (wp->w_p_rl)
5511 {
5512 --boguscols;
5513 --col;
5514 }
5515 else
5516# endif
5517 {
5518 ++boguscols;
5519 ++col;
5520 }
5521 }
5522 else
5523 {
5524 if (n_extra > 0)
5525 {
5526 vcol += n_extra;
5527 n_extra = 0;
5528 n_attr = 0;
5529 }
5530 }
5531
5532 }
5533#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 else
5535 --n_skip;
5536
Bram Moolenaar64486672010-05-16 15:46:46 +02005537 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5538 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005539 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#ifdef FEAT_DIFF
5541 && filler_todo <= 0
5542#endif
5543 )
5544 ++vcol;
5545
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005546#ifdef FEAT_SYN_HL
5547 if (vcol_save_attr >= 0)
5548 char_attr = vcol_save_attr;
5549#endif
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 /* restore attributes after "predeces" in 'listchars' */
5552 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5553 char_attr = saved_attr3;
5554
5555 /* restore attributes after last 'listchars' or 'number' char */
5556 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5557 char_attr = saved_attr2;
5558
5559 /*
5560 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005561 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 */
5563 if ((
5564#ifdef FEAT_RIGHTLEFT
5565 wp->w_p_rl ? (col < 0) :
5566#endif
5567 (col >= W_WIDTH(wp)))
5568 && (*ptr != NUL
5569#ifdef FEAT_DIFF
5570 || filler_todo > 0
5571#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005572 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5574 )
5575 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005576#ifdef FEAT_CONCEAL
5577 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5578 (int)W_WIDTH(wp), wp->w_p_rl);
5579 boguscols = 0;
5580#else
5581 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5582 (int)W_WIDTH(wp), wp->w_p_rl);
5583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 ++row;
5585 ++screen_row;
5586
5587 /* When not wrapping and finished diff lines, or when displayed
5588 * '$' and highlighting until last column, break here. */
5589 if ((!wp->w_p_wrap
5590#ifdef FEAT_DIFF
5591 && filler_todo <= 0
5592#endif
5593 ) || lcs_eol_one == -1)
5594 break;
5595
5596 /* When the window is too narrow draw all "@" lines. */
5597 if (draw_state != WL_LINE
5598#ifdef FEAT_DIFF
5599 && filler_todo <= 0
5600#endif
5601 )
5602 {
5603 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5604#ifdef FEAT_VERTSPLIT
5605 draw_vsep_win(wp, row);
5606#endif
5607 row = endrow;
5608 }
5609
5610 /* When line got too long for screen break here. */
5611 if (row == endrow)
5612 {
5613 ++row;
5614 break;
5615 }
5616
5617 if (screen_cur_row == screen_row - 1
5618#ifdef FEAT_DIFF
5619 && filler_todo <= 0
5620#endif
5621 && W_WIDTH(wp) == Columns)
5622 {
5623 /* Remember that the line wraps, used for modeless copy. */
5624 LineWraps[screen_row - 1] = TRUE;
5625
5626 /*
5627 * Special trick to make copy/paste of wrapped lines work with
5628 * xterm/screen: write an extra character beyond the end of
5629 * the line. This will work with all terminal types
5630 * (regardless of the xn,am settings).
5631 * Only do this on a fast tty.
5632 * Only do this if the cursor is on the current line
5633 * (something has been written in it).
5634 * Don't do this for the GUI.
5635 * Don't do this for double-width characters.
5636 * Don't do this for a window not at the right screen border.
5637 */
5638 if (p_tf
5639#ifdef FEAT_GUI
5640 && !gui.in_use
5641#endif
5642#ifdef FEAT_MBYTE
5643 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005644 && ((*mb_off2cells)(LineOffset[screen_row],
5645 LineOffset[screen_row] + screen_Columns)
5646 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005648 + (int)Columns - 2,
5649 LineOffset[screen_row] + screen_Columns)
5650 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#endif
5652 )
5653 {
5654 /* First make sure we are at the end of the screen line,
5655 * then output the same character again to let the
5656 * terminal know about the wrap. If the terminal doesn't
5657 * auto-wrap, we overwrite the character. */
5658 if (screen_cur_col != W_WIDTH(wp))
5659 screen_char(LineOffset[screen_row - 1]
5660 + (unsigned)Columns - 1,
5661 screen_row - 1, (int)(Columns - 1));
5662
5663#ifdef FEAT_MBYTE
5664 /* When there is a multi-byte character, just output a
5665 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005666 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5667 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 out_char(' ');
5669 else
5670#endif
5671 out_char(ScreenLines[LineOffset[screen_row - 1]
5672 + (Columns - 1)]);
5673 /* force a redraw of the first char on the next line */
5674 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5675 screen_start(); /* don't know where cursor is now */
5676 }
5677 }
5678
5679 col = 0;
5680 off = (unsigned)(current_ScreenLine - ScreenLines);
5681#ifdef FEAT_RIGHTLEFT
5682 if (wp->w_p_rl)
5683 {
5684 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5685 off += col;
5686 }
5687#endif
5688
5689 /* reset the drawing state for the start of a wrapped line */
5690 draw_state = WL_START;
5691 saved_n_extra = n_extra;
5692 saved_p_extra = p_extra;
5693 saved_c_extra = c_extra;
5694 saved_char_attr = char_attr;
5695 n_extra = 0;
5696 lcs_prec_todo = lcs_prec;
5697#ifdef FEAT_LINEBREAK
5698# ifdef FEAT_DIFF
5699 if (filler_todo <= 0)
5700# endif
5701 need_showbreak = TRUE;
5702#endif
5703#ifdef FEAT_DIFF
5704 --filler_todo;
5705 /* When the filler lines are actually below the last line of the
5706 * file, don't draw the line itself, break here. */
5707 if (filler_todo == 0 && wp->w_botfill)
5708 break;
5709#endif
5710 }
5711
5712 } /* for every character in the line */
5713
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005714#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005715 /* After an empty line check first word for capital. */
5716 if (*skipwhite(line) == NUL)
5717 {
5718 capcol_lnum = lnum + 1;
5719 cap_col = 0;
5720 }
5721#endif
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 return row;
5724}
5725
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005726#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01005727static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005728
5729/*
5730 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005731 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005732 */
5733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005734comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005735{
5736 int i;
5737
5738 for (i = 0; i < Screen_mco; ++i)
5739 {
5740 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5741 return TRUE;
5742 if (ScreenLinesC[i][off_from] == 0)
5743 break;
5744 }
5745 return FALSE;
5746}
5747#endif
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749/*
5750 * Check whether the given character needs redrawing:
5751 * - the (first byte of the) character is different
5752 * - the attributes are different
5753 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005754 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 */
5756 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005757char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
5759 if (cols > 0
5760 && ((ScreenLines[off_from] != ScreenLines[off_to]
5761 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5762
5763#ifdef FEAT_MBYTE
5764 || (enc_dbcs != 0
5765 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5766 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5767 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5768 : (cols > 1 && ScreenLines[off_from + 1]
5769 != ScreenLines[off_to + 1])))
5770 || (enc_utf8
5771 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5772 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005773 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005774 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5775 && ScreenLines[off_from + 1]
5776 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#endif
5778 ))
5779 return TRUE;
5780 return FALSE;
5781}
5782
5783/*
5784 * Move one "cooked" screen line to the screen, but only the characters that
5785 * have actually changed. Handle insert/delete character.
5786 * "coloff" gives the first column on the screen for this line.
5787 * "endcol" gives the columns where valid characters are.
5788 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5789 * needs to be cleared, negative otherwise.
5790 * "rlflag" is TRUE in a rightleft window:
5791 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5792 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5793 */
5794 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005795screen_line(
5796 int row,
5797 int coloff,
5798 int endcol,
5799 int clear_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#ifdef FEAT_RIGHTLEFT
Bram Moolenaar05540972016-01-30 20:31:25 +01005801 , int rlflag
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802#endif
Bram Moolenaar05540972016-01-30 20:31:25 +01005803 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 unsigned off_from;
5806 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005807#ifdef FEAT_MBYTE
5808 unsigned max_off_from;
5809 unsigned max_off_to;
5810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 int col = 0;
5812#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5813 int hl;
5814#endif
5815 int force = FALSE; /* force update rest of the line */
5816 int redraw_this /* bool: does character need redraw? */
5817#ifdef FEAT_GUI
5818 = TRUE /* For GUI when while-loop empty */
5819#endif
5820 ;
5821 int redraw_next; /* redraw_this for next character */
5822#ifdef FEAT_MBYTE
5823 int clear_next = FALSE;
5824 int char_cells; /* 1: normal char */
5825 /* 2: occupies two display cells */
5826# define CHAR_CELLS char_cells
5827#else
5828# define CHAR_CELLS 1
5829#endif
5830
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005831 /* Check for illegal row and col, just in case. */
5832 if (row >= Rows)
5833 row = Rows - 1;
5834 if (endcol > Columns)
5835 endcol = Columns;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837# ifdef FEAT_CLIPBOARD
5838 clip_may_clear_selection(row, row);
5839# endif
5840
5841 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5842 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005843#ifdef FEAT_MBYTE
5844 max_off_from = off_from + screen_Columns;
5845 max_off_to = LineOffset[row] + screen_Columns;
5846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848#ifdef FEAT_RIGHTLEFT
5849 if (rlflag)
5850 {
5851 /* Clear rest first, because it's left of the text. */
5852 if (clear_width > 0)
5853 {
5854 while (col <= endcol && ScreenLines[off_to] == ' '
5855 && ScreenAttrs[off_to] == 0
5856# ifdef FEAT_MBYTE
5857 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5858# endif
5859 )
5860 {
5861 ++off_to;
5862 ++col;
5863 }
5864 if (col <= endcol)
5865 screen_fill(row, row + 1, col + coloff,
5866 endcol + coloff + 1, ' ', ' ', 0);
5867 }
5868 col = endcol + 1;
5869 off_to = LineOffset[row] + col + coloff;
5870 off_from += col;
5871 endcol = (clear_width > 0 ? clear_width : -clear_width);
5872 }
5873#endif /* FEAT_RIGHTLEFT */
5874
5875 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5876
5877 while (col < endcol)
5878 {
5879#ifdef FEAT_MBYTE
5880 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005881 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 else
5883 char_cells = 1;
5884#endif
5885
5886 redraw_this = redraw_next;
5887 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5888 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5889
5890#ifdef FEAT_GUI
5891 /* If the next character was bold, then redraw the current character to
5892 * remove any pixels that might have spilt over into us. This only
5893 * happens in the GUI.
5894 */
5895 if (redraw_next && gui.in_use)
5896 {
5897 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005898 if (hl > HL_ALL)
5899 hl = syn_attr2attr(hl);
5900 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 redraw_this = TRUE;
5902 }
5903#endif
5904
5905 if (redraw_this)
5906 {
5907 /*
5908 * Special handling when 'xs' termcap flag set (hpterm):
5909 * Attributes for characters are stored at the position where the
5910 * cursor is when writing the highlighting code. The
5911 * start-highlighting code must be written with the cursor on the
5912 * first highlighted character. The stop-highlighting code must
5913 * be written with the cursor just after the last highlighted
5914 * character.
5915 * Overwriting a character doesn't remove it's highlighting. Need
5916 * to clear the rest of the line, and force redrawing it
5917 * completely.
5918 */
5919 if ( p_wiv
5920 && !force
5921#ifdef FEAT_GUI
5922 && !gui.in_use
5923#endif
5924 && ScreenAttrs[off_to] != 0
5925 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5926 {
5927 /*
5928 * Need to remove highlighting attributes here.
5929 */
5930 windgoto(row, col + coloff);
5931 out_str(T_CE); /* clear rest of this screen line */
5932 screen_start(); /* don't know where cursor is now */
5933 force = TRUE; /* force redraw of rest of the line */
5934 redraw_next = TRUE; /* or else next char would miss out */
5935
5936 /*
5937 * If the previous character was highlighted, need to stop
5938 * highlighting at this character.
5939 */
5940 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5941 {
5942 screen_attr = ScreenAttrs[off_to - 1];
5943 term_windgoto(row, col + coloff);
5944 screen_stop_highlight();
5945 }
5946 else
5947 screen_attr = 0; /* highlighting has stopped */
5948 }
5949#ifdef FEAT_MBYTE
5950 if (enc_dbcs != 0)
5951 {
5952 /* Check if overwriting a double-byte with a single-byte or
5953 * the other way around requires another character to be
5954 * redrawn. For UTF-8 this isn't needed, because comparing
5955 * ScreenLinesUC[] is sufficient. */
5956 if (char_cells == 1
5957 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005958 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
5960 /* Writing a single-cell character over a double-cell
5961 * character: need to redraw the next cell. */
5962 ScreenLines[off_to + 1] = 0;
5963 redraw_next = TRUE;
5964 }
5965 else if (char_cells == 2
5966 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005967 && (*mb_off2cells)(off_to, max_off_to) == 1
5968 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 {
5970 /* Writing the second half of a double-cell character over
5971 * a double-cell character: need to redraw the second
5972 * cell. */
5973 ScreenLines[off_to + 2] = 0;
5974 redraw_next = TRUE;
5975 }
5976
5977 if (enc_dbcs == DBCS_JPNU)
5978 ScreenLines2[off_to] = ScreenLines2[off_from];
5979 }
5980 /* When writing a single-width character over a double-width
5981 * character and at the end of the redrawn text, need to clear out
5982 * the right halve of the old character.
5983 * Also required when writing the right halve of a double-width
5984 * char over the left halve of an existing one. */
5985 if (has_mbyte && col + char_cells == endcol
5986 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005987 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005989 && (*mb_off2cells)(off_to, max_off_to) == 1
5990 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 clear_next = TRUE;
5992#endif
5993
5994 ScreenLines[off_to] = ScreenLines[off_from];
5995#ifdef FEAT_MBYTE
5996 if (enc_utf8)
5997 {
5998 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5999 if (ScreenLinesUC[off_from] != 0)
6000 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006001 int i;
6002
6003 for (i = 0; i < Screen_mco; ++i)
6004 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
6006 }
6007 if (char_cells == 2)
6008 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6009#endif
6010
6011#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006012 /* The bold trick makes a single column of pixels appear in the
6013 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 * character should be redrawn too. This happens for our own GUI
6015 * and for some xterms. */
6016 if (
6017# ifdef FEAT_GUI
6018 gui.in_use
6019# endif
6020# if defined(FEAT_GUI) && defined(UNIX)
6021 ||
6022# endif
6023# ifdef UNIX
6024 term_is_xterm
6025# endif
6026 )
6027 {
6028 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006029 if (hl > HL_ALL)
6030 hl = syn_attr2attr(hl);
6031 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 redraw_next = TRUE;
6033 }
6034#endif
6035 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6036#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006037 /* For simplicity set the attributes of second half of a
6038 * double-wide character equal to the first half. */
6039 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006041
6042 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 else
6045#endif
6046 screen_char(off_to, row, col + coloff);
6047 }
6048 else if ( p_wiv
6049#ifdef FEAT_GUI
6050 && !gui.in_use
6051#endif
6052 && col + coloff > 0)
6053 {
6054 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6055 {
6056 /*
6057 * Don't output stop-highlight when moving the cursor, it will
6058 * stop the highlighting when it should continue.
6059 */
6060 screen_attr = 0;
6061 }
6062 else if (screen_attr != 0)
6063 screen_stop_highlight();
6064 }
6065
6066 off_to += CHAR_CELLS;
6067 off_from += CHAR_CELLS;
6068 col += CHAR_CELLS;
6069 }
6070
6071#ifdef FEAT_MBYTE
6072 if (clear_next)
6073 {
6074 /* Clear the second half of a double-wide character of which the left
6075 * half was overwritten with a single-wide character. */
6076 ScreenLines[off_to] = ' ';
6077 if (enc_utf8)
6078 ScreenLinesUC[off_to] = 0;
6079 screen_char(off_to, row, col + coloff);
6080 }
6081#endif
6082
6083 if (clear_width > 0
6084#ifdef FEAT_RIGHTLEFT
6085 && !rlflag
6086#endif
6087 )
6088 {
6089#ifdef FEAT_GUI
6090 int startCol = col;
6091#endif
6092
6093 /* blank out the rest of the line */
6094 while (col < clear_width && ScreenLines[off_to] == ' '
6095 && ScreenAttrs[off_to] == 0
6096#ifdef FEAT_MBYTE
6097 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6098#endif
6099 )
6100 {
6101 ++off_to;
6102 ++col;
6103 }
6104 if (col < clear_width)
6105 {
6106#ifdef FEAT_GUI
6107 /*
6108 * In the GUI, clearing the rest of the line may leave pixels
6109 * behind if the first character cleared was bold. Some bold
6110 * fonts spill over the left. In this case we redraw the previous
6111 * character too. If we didn't skip any blanks above, then we
6112 * only redraw if the character wasn't already redrawn anyway.
6113 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006114 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 hl = ScreenAttrs[off_to];
6117 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006118 {
6119 int prev_cells = 1;
6120# ifdef FEAT_MBYTE
6121 if (enc_utf8)
6122 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6123 * that its width is 2. */
6124 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6125 else if (enc_dbcs != 0)
6126 {
6127 /* find previous character by counting from first
6128 * column and get its width. */
6129 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006130 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006131
6132 while (off < off_to)
6133 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006134 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006135 off += prev_cells;
6136 }
6137 }
6138
6139 if (enc_dbcs != 0 && prev_cells > 1)
6140 screen_char_2(off_to - prev_cells, row,
6141 col + coloff - prev_cells);
6142 else
6143# endif
6144 screen_char(off_to - prev_cells, row,
6145 col + coloff - prev_cells);
6146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
6148#endif
6149 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6150 ' ', ' ', 0);
6151#ifdef FEAT_VERTSPLIT
6152 off_to += clear_width - col;
6153 col = clear_width;
6154#endif
6155 }
6156 }
6157
6158 if (clear_width > 0)
6159 {
6160#ifdef FEAT_VERTSPLIT
6161 /* For a window that's left of another, draw the separator char. */
6162 if (col + coloff < Columns)
6163 {
6164 int c;
6165
6166 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006167 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006169 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6170 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171# endif
6172 || ScreenAttrs[off_to] != hl)
6173 {
6174 ScreenLines[off_to] = c;
6175 ScreenAttrs[off_to] = hl;
6176# ifdef FEAT_MBYTE
6177 if (enc_utf8)
6178 {
6179 if (c >= 0x80)
6180 {
6181 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006182 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 else
6185 ScreenLinesUC[off_to] = 0;
6186 }
6187# endif
6188 screen_char(off_to, row, col + coloff);
6189 }
6190 }
6191 else
6192#endif
6193 LineWraps[row] = FALSE;
6194 }
6195}
6196
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006197#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006199 * Mirror text "str" for right-left displaying.
6200 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006202 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006203rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 char_u *p1, *p2;
6206 int t;
6207
6208 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6209 {
6210 t = *p1;
6211 *p1 = *p2;
6212 *p2 = t;
6213 }
6214}
6215#endif
6216
6217#if defined(FEAT_WINDOWS) || defined(PROTO)
6218/*
6219 * mark all status lines for redraw; used after first :cd
6220 */
6221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006222status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 win_T *wp;
6225
6226 for (wp = firstwin; wp; wp = wp->w_next)
6227 if (wp->w_status_height)
6228 {
6229 wp->w_redr_status = TRUE;
6230 redraw_later(VALID);
6231 }
6232}
6233
6234/*
6235 * mark all status lines of the current buffer for redraw
6236 */
6237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006238status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 win_T *wp;
6241
6242 for (wp = firstwin; wp; wp = wp->w_next)
6243 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6244 {
6245 wp->w_redr_status = TRUE;
6246 redraw_later(VALID);
6247 }
6248}
6249
6250/*
6251 * Redraw all status lines that need to be redrawn.
6252 */
6253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006254redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 win_T *wp;
6257
6258 for (wp = firstwin; wp; wp = wp->w_next)
6259 if (wp->w_redr_status)
6260 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006261 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006262 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263}
6264#endif
6265
6266#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6267/*
6268 * Redraw all status lines at the bottom of frame "frp".
6269 */
6270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006271win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 if (frp->fr_layout == FR_LEAF)
6274 frp->fr_win->w_redr_status = TRUE;
6275 else if (frp->fr_layout == FR_ROW)
6276 {
6277 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6278 win_redraw_last_status(frp);
6279 }
6280 else /* frp->fr_layout == FR_COL */
6281 {
6282 frp = frp->fr_child;
6283 while (frp->fr_next != NULL)
6284 frp = frp->fr_next;
6285 win_redraw_last_status(frp);
6286 }
6287}
6288#endif
6289
6290#ifdef FEAT_VERTSPLIT
6291/*
6292 * Draw the verticap separator right of window "wp" starting with line "row".
6293 */
6294 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006295draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 int hl;
6298 int c;
6299
6300 if (wp->w_vsep_width)
6301 {
6302 /* draw the vertical separator right of this window */
6303 c = fillchar_vsep(&hl);
6304 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6305 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6306 c, ' ', hl);
6307 }
6308}
6309#endif
6310
6311#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006312static int status_match_len(expand_T *xp, char_u *s);
6313static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006316 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 */
6318 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006319status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 int len = 0;
6322
6323#ifdef FEAT_MENU
6324 int emenu = (xp->xp_context == EXPAND_MENUS
6325 || xp->xp_context == EXPAND_MENUNAMES);
6326
6327 /* Check for menu separators - replace with '|'. */
6328 if (emenu && menu_is_separator(s))
6329 return 1;
6330#endif
6331
6332 while (*s != NUL)
6333 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006334 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006335 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006336 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338
6339 return len;
6340}
6341
6342/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006343 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006344 * These are backslashes used for escaping. Do show backslashes in help tags.
6345 */
6346 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006347skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006348{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006349 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006350#ifdef FEAT_MENU
6351 || ((xp->xp_context == EXPAND_MENUS
6352 || xp->xp_context == EXPAND_MENUNAMES)
6353 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6354#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006355 )
6356 {
6357#ifndef BACKSLASH_IN_FILENAME
6358 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6359 return 2;
6360#endif
6361 return 1;
6362 }
6363 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006364}
6365
6366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 * Show wildchar matches in the status line.
6368 * Show at least the "match" item.
6369 * We start at item 'first_match' in the list and show all matches that fit.
6370 *
6371 * If inversion is possible we use it. Else '=' characters are used.
6372 */
6373 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006374win_redr_status_matches(
6375 expand_T *xp,
6376 int num_matches,
6377 char_u **matches, /* list of matches */
6378 int match,
6379 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
6381#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6382 int row;
6383 char_u *buf;
6384 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006385 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 int fillchar;
6387 int attr;
6388 int i;
6389 int highlight = TRUE;
6390 char_u *selstart = NULL;
6391 int selstart_col = 0;
6392 char_u *selend = NULL;
6393 static int first_match = 0;
6394 int add_left = FALSE;
6395 char_u *s;
6396#ifdef FEAT_MENU
6397 int emenu;
6398#endif
6399#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6400 int l;
6401#endif
6402
6403 if (matches == NULL) /* interrupted completion? */
6404 return;
6405
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006406#ifdef FEAT_MBYTE
6407 if (has_mbyte)
6408 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6409 else
6410#endif
6411 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (buf == NULL)
6413 return;
6414
6415 if (match == -1) /* don't show match but original text */
6416 {
6417 match = 0;
6418 highlight = FALSE;
6419 }
6420 /* count 1 for the ending ">" */
6421 clen = status_match_len(xp, L_MATCH(match)) + 3;
6422 if (match == 0)
6423 first_match = 0;
6424 else if (match < first_match)
6425 {
6426 /* jumping left, as far as we can go */
6427 first_match = match;
6428 add_left = TRUE;
6429 }
6430 else
6431 {
6432 /* check if match fits on the screen */
6433 for (i = first_match; i < match; ++i)
6434 clen += status_match_len(xp, L_MATCH(i)) + 2;
6435 if (first_match > 0)
6436 clen += 2;
6437 /* jumping right, put match at the left */
6438 if ((long)clen > Columns)
6439 {
6440 first_match = match;
6441 /* if showing the last match, we can add some on the left */
6442 clen = 2;
6443 for (i = match; i < num_matches; ++i)
6444 {
6445 clen += status_match_len(xp, L_MATCH(i)) + 2;
6446 if ((long)clen >= Columns)
6447 break;
6448 }
6449 if (i == num_matches)
6450 add_left = TRUE;
6451 }
6452 }
6453 if (add_left)
6454 while (first_match > 0)
6455 {
6456 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6457 if ((long)clen >= Columns)
6458 break;
6459 --first_match;
6460 }
6461
6462 fillchar = fillchar_status(&attr, TRUE);
6463
6464 if (first_match == 0)
6465 {
6466 *buf = NUL;
6467 len = 0;
6468 }
6469 else
6470 {
6471 STRCPY(buf, "< ");
6472 len = 2;
6473 }
6474 clen = len;
6475
6476 i = first_match;
6477 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6478 {
6479 if (i == match)
6480 {
6481 selstart = buf + len;
6482 selstart_col = clen;
6483 }
6484
6485 s = L_MATCH(i);
6486 /* Check for menu separators - replace with '|' */
6487#ifdef FEAT_MENU
6488 emenu = (xp->xp_context == EXPAND_MENUS
6489 || xp->xp_context == EXPAND_MENUNAMES);
6490 if (emenu && menu_is_separator(s))
6491 {
6492 STRCPY(buf + len, transchar('|'));
6493 l = (int)STRLEN(buf + len);
6494 len += l;
6495 clen += l;
6496 }
6497 else
6498#endif
6499 for ( ; *s != NUL; ++s)
6500 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006501 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 clen += ptr2cells(s);
6503#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006504 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 STRNCPY(buf + len, s, l);
6507 s += l - 1;
6508 len += l;
6509 }
6510 else
6511#endif
6512 {
6513 STRCPY(buf + len, transchar_byte(*s));
6514 len += (int)STRLEN(buf + len);
6515 }
6516 }
6517 if (i == match)
6518 selend = buf + len;
6519
6520 *(buf + len++) = ' ';
6521 *(buf + len++) = ' ';
6522 clen += 2;
6523 if (++i == num_matches)
6524 break;
6525 }
6526
6527 if (i != num_matches)
6528 {
6529 *(buf + len++) = '>';
6530 ++clen;
6531 }
6532
6533 buf[len] = NUL;
6534
6535 row = cmdline_row - 1;
6536 if (row >= 0)
6537 {
6538 if (wild_menu_showing == 0)
6539 {
6540 if (msg_scrolled > 0)
6541 {
6542 /* Put the wildmenu just above the command line. If there is
6543 * no room, scroll the screen one line up. */
6544 if (cmdline_row == Rows - 1)
6545 {
6546 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6547 ++msg_scrolled;
6548 }
6549 else
6550 {
6551 ++cmdline_row;
6552 ++row;
6553 }
6554 wild_menu_showing = WM_SCROLLED;
6555 }
6556 else
6557 {
6558 /* Create status line if needed by setting 'laststatus' to 2.
6559 * Set 'winminheight' to zero to avoid that the window is
6560 * resized. */
6561 if (lastwin->w_status_height == 0)
6562 {
6563 save_p_ls = p_ls;
6564 save_p_wmh = p_wmh;
6565 p_ls = 2;
6566 p_wmh = 0;
6567 last_status(FALSE);
6568 }
6569 wild_menu_showing = WM_SHOWN;
6570 }
6571 }
6572
6573 screen_puts(buf, row, 0, attr);
6574 if (selstart != NULL && highlight)
6575 {
6576 *selend = NUL;
6577 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6578 }
6579
6580 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6581 }
6582
6583#ifdef FEAT_VERTSPLIT
6584 win_redraw_last_status(topframe);
6585#else
6586 lastwin->w_redr_status = TRUE;
6587#endif
6588 vim_free(buf);
6589}
6590#endif
6591
6592#if defined(FEAT_WINDOWS) || defined(PROTO)
6593/*
6594 * Redraw the status line of window wp.
6595 *
6596 * If inversion is possible we use it. Else '=' characters are used.
6597 */
6598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006599win_redr_status(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 int row;
6602 char_u *p;
6603 int len;
6604 int fillchar;
6605 int attr;
6606 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006607 static int busy = FALSE;
6608
6609 /* It's possible to get here recursively when 'statusline' (indirectly)
6610 * invokes ":redrawstatus". Simply ignore the call then. */
6611 if (busy)
6612 return;
6613 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
6615 wp->w_redr_status = FALSE;
6616 if (wp->w_status_height == 0)
6617 {
6618 /* no status line, can only be last window */
6619 redraw_cmdline = TRUE;
6620 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006621 else if (!redrawing()
6622#ifdef FEAT_INS_EXPAND
6623 /* don't update status line when popup menu is visible and may be
6624 * drawn over it */
6625 || pum_visible()
6626#endif
6627 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
6629 /* Don't redraw right now, do it later. */
6630 wp->w_redr_status = TRUE;
6631 }
6632#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006633 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
6635 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006636 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
6638#endif
6639 else
6640 {
6641 fillchar = fillchar_status(&attr, wp == curwin);
6642
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006643 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 p = NameBuff;
6645 len = (int)STRLEN(p);
6646
6647 if (wp->w_buffer->b_help
6648#ifdef FEAT_QUICKFIX
6649 || wp->w_p_pvw
6650#endif
6651 || bufIsChanged(wp->w_buffer)
6652 || wp->w_buffer->b_p_ro)
6653 *(p + len++) = ' ';
6654 if (wp->w_buffer->b_help)
6655 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006656 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 len += (int)STRLEN(p + len);
6658 }
6659#ifdef FEAT_QUICKFIX
6660 if (wp->w_p_pvw)
6661 {
6662 STRCPY(p + len, _("[Preview]"));
6663 len += (int)STRLEN(p + len);
6664 }
6665#endif
6666 if (bufIsChanged(wp->w_buffer))
6667 {
6668 STRCPY(p + len, "[+]");
6669 len += 3;
6670 }
6671 if (wp->w_buffer->b_p_ro)
6672 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006673 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 len += 4;
6675 }
6676
6677#ifndef FEAT_VERTSPLIT
6678 this_ru_col = ru_col;
6679 if (this_ru_col < (Columns + 1) / 2)
6680 this_ru_col = (Columns + 1) / 2;
6681#else
6682 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6683 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6684 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6685 if (this_ru_col <= 1)
6686 {
6687 p = (char_u *)"<"; /* No room for file name! */
6688 len = 1;
6689 }
6690 else
6691#endif
6692#ifdef FEAT_MBYTE
6693 if (has_mbyte)
6694 {
6695 int clen = 0, i;
6696
6697 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006698 clen = mb_string2cells(p, -1);
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 /* Find first character that will fit.
6701 * Going from start to end is much faster for DBCS. */
6702 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006703 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 clen -= (*mb_ptr2cells)(p + i);
6705 len = clen;
6706 if (i > 0)
6707 {
6708 p = p + i - 1;
6709 *p = '<';
6710 ++len;
6711 }
6712
6713 }
6714 else
6715#endif
6716 if (len > this_ru_col - 1)
6717 {
6718 p += len - (this_ru_col - 1);
6719 *p = '<';
6720 len = this_ru_col - 1;
6721 }
6722
6723 row = W_WINROW(wp) + wp->w_height;
6724 screen_puts(p, row, W_WINCOL(wp), attr);
6725 screen_fill(row, row + 1, len + W_WINCOL(wp),
6726 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6727
6728 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6729 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6730 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6731 - 1 + W_WINCOL(wp)), attr);
6732
6733#ifdef FEAT_CMDL_INFO
6734 win_redr_ruler(wp, TRUE);
6735#endif
6736 }
6737
6738#ifdef FEAT_VERTSPLIT
6739 /*
6740 * May need to draw the character below the vertical separator.
6741 */
6742 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6743 {
6744 if (stl_connected(wp))
6745 fillchar = fillchar_status(&attr, wp == curwin);
6746 else
6747 fillchar = fillchar_vsep(&attr);
6748 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6749 attr);
6750 }
6751#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006752 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754
Bram Moolenaar238a5642006-02-21 22:12:05 +00006755#ifdef FEAT_STL_OPT
6756/*
6757 * Redraw the status line according to 'statusline' and take care of any
6758 * errors encountered.
6759 */
6760 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006761redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006762{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006763 static int entered = FALSE;
6764 int save_called_emsg = called_emsg;
6765
6766 /* When called recursively return. This can happen when the statusline
6767 * contains an expression that triggers a redraw. */
6768 if (entered)
6769 return;
6770 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006771
6772 called_emsg = FALSE;
6773 win_redr_custom(wp, FALSE);
6774 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006775 {
6776 /* When there is an error disable the statusline, otherwise the
6777 * display is messed up with errors and a redraw triggers the problem
6778 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006779 set_string_option_direct((char_u *)"statusline", -1,
6780 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006781 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006782 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006783 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006784 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006785}
6786#endif
6787
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788# ifdef FEAT_VERTSPLIT
6789/*
6790 * Return TRUE if the status line of window "wp" is connected to the status
6791 * line of the window right of it. If not, then it's a vertical separator.
6792 * Only call if (wp->w_vsep_width != 0).
6793 */
6794 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006795stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 frame_T *fr;
6798
6799 fr = wp->w_frame;
6800 while (fr->fr_parent != NULL)
6801 {
6802 if (fr->fr_parent->fr_layout == FR_COL)
6803 {
6804 if (fr->fr_next != NULL)
6805 break;
6806 }
6807 else
6808 {
6809 if (fr->fr_next != NULL)
6810 return TRUE;
6811 }
6812 fr = fr->fr_parent;
6813 }
6814 return FALSE;
6815}
6816# endif
6817
6818#endif /* FEAT_WINDOWS */
6819
6820#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6821/*
6822 * Get the value to show for the language mappings, active 'keymap'.
6823 */
6824 int
Bram Moolenaar05540972016-01-30 20:31:25 +01006825get_keymap_str(
6826 win_T *wp,
6827 char_u *buf, /* buffer for the result */
6828 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 char_u *p;
6831
6832 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6833 return FALSE;
6834
6835 {
6836#ifdef FEAT_EVAL
6837 buf_T *old_curbuf = curbuf;
6838 win_T *old_curwin = curwin;
6839 char_u *s;
6840
6841 curbuf = wp->w_buffer;
6842 curwin = wp;
6843 STRCPY(buf, "b:keymap_name"); /* must be writable */
6844 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006845 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 --emsg_skip;
6847 curbuf = old_curbuf;
6848 curwin = old_curwin;
6849 if (p == NULL || *p == NUL)
6850#endif
6851 {
6852#ifdef FEAT_KEYMAP
6853 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6854 p = wp->w_buffer->b_p_keymap;
6855 else
6856#endif
6857 p = (char_u *)"lang";
6858 }
6859 if ((int)(STRLEN(p) + 3) < len)
6860 sprintf((char *)buf, "<%s>", p);
6861 else
6862 buf[0] = NUL;
6863#ifdef FEAT_EVAL
6864 vim_free(s);
6865#endif
6866 }
6867 return buf[0] != NUL;
6868}
6869#endif
6870
6871#if defined(FEAT_STL_OPT) || defined(PROTO)
6872/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006873 * Redraw the status line or ruler of window "wp".
6874 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 */
6876 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006877win_redr_custom(
6878 win_T *wp,
6879 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006881 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 int attr;
6883 int curattr;
6884 int row;
6885 int col = 0;
6886 int maxwidth;
6887 int width;
6888 int n;
6889 int len;
6890 int fillchar;
6891 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006892 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006894 struct stl_hlrec hltab[STL_MAX_ITEM];
6895 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006896 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006897 win_T *ewp;
6898 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
Bram Moolenaar1d633412013-12-11 15:52:01 +01006900 /* There is a tiny chance that this gets called recursively: When
6901 * redrawing a status line triggers redrawing the ruler or tabline.
6902 * Avoid trouble by not allowing recursion. */
6903 if (entered)
6904 return;
6905 entered = TRUE;
6906
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006908 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006910 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006911 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006912 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006913 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006914 attr = hl_attr(HLF_TPF);
6915 maxwidth = Columns;
6916# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006917 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006920 else
6921 {
6922 row = W_WINROW(wp) + wp->w_height;
6923 fillchar = fillchar_status(&attr, wp == curwin);
6924 maxwidth = W_WIDTH(wp);
6925
6926 if (draw_ruler)
6927 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006928 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006929 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006930 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006931 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006932 if (*++stl == '-')
6933 stl++;
6934 if (atoi((char *)stl))
6935 while (VIM_ISDIGIT(*stl))
6936 stl++;
6937 if (*stl++ != '(')
6938 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006939 }
6940#ifdef FEAT_VERTSPLIT
6941 col = ru_col - (Columns - W_WIDTH(wp));
6942 if (col < (W_WIDTH(wp) + 1) / 2)
6943 col = (W_WIDTH(wp) + 1) / 2;
6944#else
6945 col = ru_col;
6946 if (col > (Columns + 1) / 2)
6947 col = (Columns + 1) / 2;
6948#endif
6949 maxwidth = W_WIDTH(wp) - col;
6950#ifdef FEAT_WINDOWS
6951 if (!wp->w_status_height)
6952#endif
6953 {
6954 row = Rows - 1;
6955 --maxwidth; /* writing in last column may cause scrolling */
6956 fillchar = ' ';
6957 attr = 0;
6958 }
6959
6960# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006961 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962# endif
6963 }
6964 else
6965 {
6966 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006967 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006969 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006970# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006971 use_sandbox = was_set_insecurely((char_u *)"statusline",
6972 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006973# endif
6974 }
6975
6976#ifdef FEAT_VERTSPLIT
6977 col += W_WINCOL(wp);
6978#endif
6979 }
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006982 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
Bram Moolenaar61452852011-02-01 18:01:11 +01006984 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6985 * the cursor away and back. */
6986 ewp = wp == NULL ? curwin : wp;
6987 p_crb_save = ewp->w_p_crb;
6988 ewp->w_p_crb = FALSE;
6989
Bram Moolenaar362f3562009-11-03 16:20:34 +00006990 /* Make a copy, because the statusline may include a function call that
6991 * might change the option value and free the memory. */
6992 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006993 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006994 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006995 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006996 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006997 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006999 /* Make all characters printable. */
7000 p = transstr(buf);
7001 if (p != NULL)
7002 {
7003 vim_strncpy(buf, p, sizeof(buf) - 1);
7004 vim_free(p);
7005 }
7006
7007 /* fill up with "fillchar" */
7008 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007009 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011#ifdef FEAT_MBYTE
7012 len += (*mb_char2bytes)(fillchar, buf + len);
7013#else
7014 buf[len++] = fillchar;
7015#endif
7016 ++width;
7017 }
7018 buf[len] = NUL;
7019
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007020 /*
7021 * Draw each snippet with the specified highlighting.
7022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 curattr = attr;
7024 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007025 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007027 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 screen_puts_len(p, len, row, col, curattr);
7029 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007030 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007032 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007034 else if (hltab[n].userhl < 0)
7035 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007037 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007038 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039#endif
7040 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007041 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007044
7045 if (wp == NULL)
7046 {
7047 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7048 col = 0;
7049 len = 0;
7050 p = buf;
7051 fillchar = 0;
7052 for (n = 0; tabtab[n].start != NULL; n++)
7053 {
7054 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7055 while (col < len)
7056 TabPageIdxs[col++] = fillchar;
7057 p = tabtab[n].start;
7058 fillchar = tabtab[n].userhl;
7059 }
7060 while (col < Columns)
7061 TabPageIdxs[col++] = fillchar;
7062 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007063
7064theend:
7065 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066}
7067
7068#endif /* FEAT_STL_OPT */
7069
7070/*
7071 * Output a single character directly to the screen and update ScreenLines.
7072 */
7073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007074screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 char_u buf[MB_MAXBYTES + 1];
7077
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007078#ifdef FEAT_MBYTE
7079 if (has_mbyte)
7080 buf[(*mb_char2bytes)(c, buf)] = NUL;
7081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007083 {
7084 buf[0] = c;
7085 buf[1] = NUL;
7086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 screen_puts(buf, row, col, attr);
7088}
7089
7090/*
7091 * Get a single character directly from ScreenLines into "bytes[]".
7092 * Also return its attribute in *attrp;
7093 */
7094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007095screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 unsigned off;
7098
7099 /* safety check */
7100 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7101 {
7102 off = LineOffset[row] + col;
7103 *attrp = ScreenAttrs[off];
7104 bytes[0] = ScreenLines[off];
7105 bytes[1] = NUL;
7106
7107#ifdef FEAT_MBYTE
7108 if (enc_utf8 && ScreenLinesUC[off] != 0)
7109 bytes[utfc_char2bytes(off, bytes)] = NUL;
7110 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7111 {
7112 bytes[0] = ScreenLines[off];
7113 bytes[1] = ScreenLines2[off];
7114 bytes[2] = NUL;
7115 }
7116 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7117 {
7118 bytes[1] = ScreenLines[off + 1];
7119 bytes[2] = NUL;
7120 }
7121#endif
7122 }
7123}
7124
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007125#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007126static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007127
7128/*
7129 * Return TRUE if composing characters for screen posn "off" differs from
7130 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007131 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007132 */
7133 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007134screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007135{
7136 int i;
7137
7138 for (i = 0; i < Screen_mco; ++i)
7139 {
7140 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7141 return TRUE;
7142 if (u8cc[i] == 0)
7143 break;
7144 }
7145 return FALSE;
7146}
7147#endif
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149/*
7150 * Put string '*text' on the screen at position 'row' and 'col', with
7151 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7152 * Note: only outputs within one row, message is truncated at screen boundary!
7153 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7154 */
7155 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007156screen_puts(
7157 char_u *text,
7158 int row,
7159 int col,
7160 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 screen_puts_len(text, -1, row, col, attr);
7163}
7164
7165/*
7166 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7167 * a NUL.
7168 */
7169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007170screen_puts_len(
7171 char_u *text,
7172 int textlen,
7173 int row,
7174 int col,
7175 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 unsigned off;
7178 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007179 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 int c;
7181#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007182 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 int mbyte_blen = 1;
7184 int mbyte_cells = 1;
7185 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007186 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 int clear_next_cell = FALSE;
7188# ifdef FEAT_ARABIC
7189 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007190 int pc, nc, nc1;
7191 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192# endif
7193#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007194#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7195 int force_redraw_this;
7196 int force_redraw_next = FALSE;
7197#endif
7198 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7201 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007202 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
Bram Moolenaarc236c162008-07-13 17:41:49 +00007204#ifdef FEAT_MBYTE
7205 /* When drawing over the right halve of a double-wide char clear out the
7206 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007207 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007208# ifdef FEAT_GUI
7209 && !gui.in_use
7210# endif
7211 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007212 {
7213 ScreenLines[off - 1] = ' ';
7214 ScreenAttrs[off - 1] = 0;
7215 if (enc_utf8)
7216 {
7217 ScreenLinesUC[off - 1] = 0;
7218 ScreenLinesC[0][off - 1] = 0;
7219 }
7220 /* redraw the previous cell, make it empty */
7221 screen_char(off - 1, row, col - 1);
7222 /* force the cell at "col" to be redrawn */
7223 force_redraw_next = TRUE;
7224 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007225#endif
7226
Bram Moolenaar367329b2007-08-30 11:53:22 +00007227#ifdef FEAT_MBYTE
7228 max_off = LineOffset[row] + screen_Columns;
7229#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007230 while (col < screen_Columns
7231 && (len < 0 || (int)(ptr - text) < len)
7232 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 c = *ptr;
7235#ifdef FEAT_MBYTE
7236 /* check if this is the first byte of a multibyte */
7237 if (has_mbyte)
7238 {
7239 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007240 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007242 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7244 mbyte_cells = 1;
7245 else if (enc_dbcs != 0)
7246 mbyte_cells = mbyte_blen;
7247 else /* enc_utf8 */
7248 {
7249 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007250 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 (int)((text + len) - ptr));
7252 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007253 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007255# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 /* Non-BMP character: display as ? or fullwidth ?. */
7257 if (u8c >= 0x10000)
7258 {
7259 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7260 if (attr == 0)
7261 attr = hl_attr(HLF_8);
7262 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264# ifdef FEAT_ARABIC
7265 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7266 {
7267 /* Do Arabic shaping. */
7268 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7269 {
7270 /* Past end of string to be displayed. */
7271 nc = NUL;
7272 nc1 = NUL;
7273 }
7274 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007275 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007276 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7277 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007278 nc1 = pcc[0];
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 pc = prev_c;
7281 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007282 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 else
7285 prev_c = u8c;
7286# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007287 if (col + mbyte_cells > screen_Columns)
7288 {
7289 /* Only 1 cell left, but character requires 2 cells:
7290 * display a '>' in the last column to avoid wrapping. */
7291 c = '>';
7292 mbyte_cells = 1;
7293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295 }
7296#endif
7297
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007298#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7299 force_redraw_this = force_redraw_next;
7300 force_redraw_next = FALSE;
7301#endif
7302
7303 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#ifdef FEAT_MBYTE
7305 || (mbyte_cells == 2
7306 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7307 || (enc_dbcs == DBCS_JPNU
7308 && c == 0x8e
7309 && ScreenLines2[off] != ptr[1])
7310 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007311 && (ScreenLinesUC[off] !=
7312 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7313 || (ScreenLinesUC[off] != 0
7314 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315#endif
7316 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007317 || exmode_active;
7318
7319 if (need_redraw
7320#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7321 || force_redraw_this
7322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 )
7324 {
7325#if defined(FEAT_GUI) || defined(UNIX)
7326 /* The bold trick makes a single row of pixels appear in the next
7327 * character. When a bold character is removed, the next
7328 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007329 * and for some xterms. */
7330 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331# ifdef FEAT_GUI
7332 gui.in_use
7333# endif
7334# if defined(FEAT_GUI) && defined(UNIX)
7335 ||
7336# endif
7337# ifdef UNIX
7338 term_is_xterm
7339# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007340 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007342 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007344 if (n > HL_ALL)
7345 n = syn_attr2attr(n);
7346 if (n & HL_BOLD)
7347 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349#endif
7350#ifdef FEAT_MBYTE
7351 /* When at the end of the text and overwriting a two-cell
7352 * character with a one-cell character, need to clear the next
7353 * cell. Also when overwriting the left halve of a two-cell char
7354 * with the right halve of a two-cell char. Do this only once
7355 * (mb_off2cells() may return 2 on the right halve). */
7356 if (clear_next_cell)
7357 clear_next_cell = FALSE;
7358 else if (has_mbyte
7359 && (len < 0 ? ptr[mbyte_blen] == NUL
7360 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007361 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007363 && (*mb_off2cells)(off, max_off) == 1
7364 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 clear_next_cell = TRUE;
7366
7367 /* Make sure we never leave a second byte of a double-byte behind,
7368 * it confuses mb_off2cells(). */
7369 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007370 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007372 && (*mb_off2cells)(off, max_off) == 1
7373 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 ScreenLines[off + mbyte_blen] = 0;
7375#endif
7376 ScreenLines[off] = c;
7377 ScreenAttrs[off] = attr;
7378#ifdef FEAT_MBYTE
7379 if (enc_utf8)
7380 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007381 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 ScreenLinesUC[off] = 0;
7383 else
7384 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007385 int i;
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007388 for (i = 0; i < Screen_mco; ++i)
7389 {
7390 ScreenLinesC[i][off] = u8cc[i];
7391 if (u8cc[i] == 0)
7392 break;
7393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395 if (mbyte_cells == 2)
7396 {
7397 ScreenLines[off + 1] = 0;
7398 ScreenAttrs[off + 1] = attr;
7399 }
7400 screen_char(off, row, col);
7401 }
7402 else if (mbyte_cells == 2)
7403 {
7404 ScreenLines[off + 1] = ptr[1];
7405 ScreenAttrs[off + 1] = attr;
7406 screen_char_2(off, row, col);
7407 }
7408 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7409 {
7410 ScreenLines2[off] = ptr[1];
7411 screen_char(off, row, col);
7412 }
7413 else
7414#endif
7415 screen_char(off, row, col);
7416 }
7417#ifdef FEAT_MBYTE
7418 if (has_mbyte)
7419 {
7420 off += mbyte_cells;
7421 col += mbyte_cells;
7422 ptr += mbyte_blen;
7423 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007424 {
7425 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007427 len = -1;
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 else
7431#endif
7432 {
7433 ++off;
7434 ++col;
7435 ++ptr;
7436 }
7437 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007438
7439#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7440 /* If we detected the next character needs to be redrawn, but the text
7441 * doesn't extend up to there, update the character here. */
7442 if (force_redraw_next && col < screen_Columns)
7443 {
7444# ifdef FEAT_MBYTE
7445 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7446 screen_char_2(off, row, col);
7447 else
7448# endif
7449 screen_char(off, row, col);
7450 }
7451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
7453
7454#ifdef FEAT_SEARCH_EXTRA
7455/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007456 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 */
7458 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007459start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 if (p_hls && !no_hlsearch)
7462 {
7463 last_pat_prog(&search_hl.rm);
7464 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007465# ifdef FEAT_RELTIME
7466 /* Set the time limit to 'redrawtime'. */
7467 profile_setlimit(p_rdt, &search_hl.tm);
7468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470}
7471
7472/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007473 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 */
7475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007476end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
7478 if (search_hl.rm.regprog != NULL)
7479 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007480 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 search_hl.rm.regprog = NULL;
7482 }
7483}
7484
7485/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007486 * Init for calling prepare_search_hl().
7487 */
7488 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007489init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007490{
7491 matchitem_T *cur;
7492
7493 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7494 * match */
7495 cur = wp->w_match_head;
7496 while (cur != NULL)
7497 {
7498 cur->hl.rm = cur->match;
7499 if (cur->hlg_id == 0)
7500 cur->hl.attr = 0;
7501 else
7502 cur->hl.attr = syn_id2attr(cur->hlg_id);
7503 cur->hl.buf = wp->w_buffer;
7504 cur->hl.lnum = 0;
7505 cur->hl.first_lnum = 0;
7506# ifdef FEAT_RELTIME
7507 /* Set the time limit to 'redrawtime'. */
7508 profile_setlimit(p_rdt, &(cur->hl.tm));
7509# endif
7510 cur = cur->next;
7511 }
7512 search_hl.buf = wp->w_buffer;
7513 search_hl.lnum = 0;
7514 search_hl.first_lnum = 0;
7515 /* time limit is set at the toplevel, for all windows */
7516}
7517
7518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 * Advance to the match in window "wp" line "lnum" or past it.
7520 */
7521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007522prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007524 matchitem_T *cur; /* points to the match list */
7525 match_T *shl; /* points to search_hl or a match */
7526 int shl_flag; /* flag to indicate whether search_hl
7527 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007528 int pos_inprogress; /* marks that position match search is
7529 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 int n;
7531
7532 /*
7533 * When using a multi-line pattern, start searching at the top
7534 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007535 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007537 cur = wp->w_match_head;
7538 shl_flag = FALSE;
7539 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007541 if (shl_flag == FALSE)
7542 {
7543 shl = &search_hl;
7544 shl_flag = TRUE;
7545 }
7546 else
7547 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 if (shl->rm.regprog != NULL
7549 && shl->lnum == 0
7550 && re_multiline(shl->rm.regprog))
7551 {
7552 if (shl->first_lnum == 0)
7553 {
7554# ifdef FEAT_FOLDING
7555 for (shl->first_lnum = lnum;
7556 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7557 if (hasFoldingWin(wp, shl->first_lnum - 1,
7558 NULL, NULL, TRUE, NULL))
7559 break;
7560# else
7561 shl->first_lnum = wp->w_topline;
7562# endif
7563 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007564 if (cur != NULL)
7565 cur->pos.cur = 0;
7566 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007568 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7569 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007571 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7572 pos_inprogress = cur == NULL || cur->pos.cur == 0
7573 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (shl->lnum != 0)
7575 {
7576 shl->first_lnum = shl->lnum
7577 + shl->rm.endpos[0].lnum
7578 - shl->rm.startpos[0].lnum;
7579 n = shl->rm.endpos[0].col;
7580 }
7581 else
7582 {
7583 ++shl->first_lnum;
7584 n = 0;
7585 }
7586 }
7587 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007588 if (shl != &search_hl && cur != NULL)
7589 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 }
7591}
7592
7593/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007594 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 * Uses shl->buf.
7596 * Sets shl->lnum and shl->rm contents.
7597 * Note: Assumes a previous match is always before "lnum", unless
7598 * shl->lnum is zero.
7599 * Careful: Any pointers for buffer lines will become invalid.
7600 */
7601 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007602next_search_hl(
7603 win_T *win,
7604 match_T *shl, /* points to search_hl or a match */
7605 linenr_T lnum,
7606 colnr_T mincol, /* minimal column for a match */
7607 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 linenr_T l;
7610 colnr_T matchcol;
7611 long nmatched;
7612
7613 if (shl->lnum != 0)
7614 {
7615 /* Check for three situations:
7616 * 1. If the "lnum" is below a previous match, start a new search.
7617 * 2. If the previous match includes "mincol", use it.
7618 * 3. Continue after the previous match.
7619 */
7620 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7621 if (lnum > l)
7622 shl->lnum = 0;
7623 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7624 return;
7625 }
7626
7627 /*
7628 * Repeat searching for a match until one is found that includes "mincol"
7629 * or none is found in this line.
7630 */
7631 called_emsg = FALSE;
7632 for (;;)
7633 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007634#ifdef FEAT_RELTIME
7635 /* Stop searching after passing the time limit. */
7636 if (profile_passed_limit(&(shl->tm)))
7637 {
7638 shl->lnum = 0; /* no match found in time */
7639 break;
7640 }
7641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 /* Three situations:
7643 * 1. No useful previous match: search from start of line.
7644 * 2. Not Vi compatible or empty match: continue at next character.
7645 * Break the loop if this is beyond the end of the line.
7646 * 3. Vi compatible searching: continue at end of previous match.
7647 */
7648 if (shl->lnum == 0)
7649 matchcol = 0;
7650 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7651 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007652 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007654 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007655
7656 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007657 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007658 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007660 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 shl->lnum = 0;
7662 break;
7663 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007664#ifdef FEAT_MBYTE
7665 if (has_mbyte)
7666 matchcol += mb_ptr2len(ml);
7667 else
7668#endif
7669 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 else
7672 matchcol = shl->rm.endpos[0].col;
7673
7674 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007675 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007677 /* Remember whether shl->rm is using a copy of the regprog in
7678 * cur->match. */
7679 int regprog_is_copy = (shl != &search_hl && cur != NULL
7680 && shl == &cur->hl
7681 && cur->match.regprog == cur->hl.rm.regprog);
7682
Bram Moolenaarb3414592014-06-17 17:48:32 +02007683 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7684 matchcol,
7685#ifdef FEAT_RELTIME
7686 &(shl->tm)
7687#else
7688 NULL
7689#endif
7690 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007691 /* Copy the regprog, in case it got freed and recompiled. */
7692 if (regprog_is_copy)
7693 cur->match.regprog = cur->hl.rm.regprog;
7694
Bram Moolenaarb3414592014-06-17 17:48:32 +02007695 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007696 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007697 /* Error while handling regexp: stop using this regexp. */
7698 if (shl == &search_hl)
7699 {
7700 /* don't free regprog in the match list, it's a copy */
7701 vim_regfree(shl->rm.regprog);
7702 SET_NO_HLSEARCH(TRUE);
7703 }
7704 shl->rm.regprog = NULL;
7705 shl->lnum = 0;
7706 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7707 message */
7708 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007709 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007710 }
7711 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007712 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007713 else
7714 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (nmatched == 0)
7716 {
7717 shl->lnum = 0; /* no match found */
7718 break;
7719 }
7720 if (shl->rm.startpos[0].lnum > 0
7721 || shl->rm.startpos[0].col >= mincol
7722 || nmatched > 1
7723 || shl->rm.endpos[0].col > mincol)
7724 {
7725 shl->lnum += shl->rm.startpos[0].lnum;
7726 break; /* useful match found */
7727 }
7728 }
7729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
Bram Moolenaarb3414592014-06-17 17:48:32 +02007731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007732next_search_hl_pos(
7733 match_T *shl, /* points to a match */
7734 linenr_T lnum,
7735 posmatch_T *posmatch, /* match positions */
7736 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007737{
7738 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007739 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007740
7741 shl->lnum = 0;
7742 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7743 {
7744 if (posmatch->pos[i].lnum == 0)
7745 break;
7746 if (posmatch->pos[i].col < mincol)
7747 continue;
7748 if (posmatch->pos[i].lnum == lnum)
7749 {
7750 if (shl->lnum == lnum)
7751 {
7752 /* partially sort positions by column numbers
7753 * on the same line */
7754 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7755 {
7756 llpos_T tmp = posmatch->pos[i];
7757
7758 posmatch->pos[i] = posmatch->pos[bot];
7759 posmatch->pos[bot] = tmp;
7760 }
7761 }
7762 else
7763 {
7764 bot = i;
7765 shl->lnum = lnum;
7766 }
7767 }
7768 }
7769 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007770 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007771 {
7772 colnr_T start = posmatch->pos[bot].col == 0
7773 ? 0 : posmatch->pos[bot].col - 1;
7774 colnr_T end = posmatch->pos[bot].col == 0
7775 ? MAXCOL : start + posmatch->pos[bot].len;
7776
7777 shl->rm.startpos[0].lnum = 0;
7778 shl->rm.startpos[0].col = start;
7779 shl->rm.endpos[0].lnum = 0;
7780 shl->rm.endpos[0].col = end;
7781 posmatch->cur = bot + 1;
7782 return TRUE;
7783 }
7784 return FALSE;
7785}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007786#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007789screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 attrentry_T *aep = NULL;
7792
7793 screen_attr = attr;
7794 if (full_screen
7795#ifdef WIN3264
7796 && termcap_active
7797#endif
7798 )
7799 {
7800#ifdef FEAT_GUI
7801 if (gui.in_use)
7802 {
7803 char buf[20];
7804
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007805 /* The GUI handles this internally. */
7806 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 OUT_STR(buf);
7808 }
7809 else
7810#endif
7811 {
7812 if (attr > HL_ALL) /* special HL attr. */
7813 {
7814 if (t_colors > 1)
7815 aep = syn_cterm_attr2entry(attr);
7816 else
7817 aep = syn_term_attr2entry(attr);
7818 if (aep == NULL) /* did ":syntax clear" */
7819 attr = 0;
7820 else
7821 attr = aep->ae_attr;
7822 }
7823 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7824 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007825 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7826 && cterm_normal_fg_bold)
7827 /* If the Normal FG color has BOLD attribute and the new HL
7828 * has a FG color defined, clear BOLD. */
7829 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7831 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007832 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7833 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 out_str(T_US);
7835 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7836 out_str(T_CZH);
7837 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7838 out_str(T_MR);
7839
7840 /*
7841 * Output the color or start string after bold etc., in case the
7842 * bold etc. override the color setting.
7843 */
7844 if (aep != NULL)
7845 {
7846 if (t_colors > 1)
7847 {
7848 if (aep->ae_u.cterm.fg_color)
7849 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7850 if (aep->ae_u.cterm.bg_color)
7851 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7852 }
7853 else
7854 {
7855 if (aep->ae_u.term.start != NULL)
7856 out_str(aep->ae_u.term.start);
7857 }
7858 }
7859 }
7860 }
7861}
7862
7863 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007864screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 int do_ME = FALSE; /* output T_ME code */
7867
7868 if (screen_attr != 0
7869#ifdef WIN3264
7870 && termcap_active
7871#endif
7872 )
7873 {
7874#ifdef FEAT_GUI
7875 if (gui.in_use)
7876 {
7877 char buf[20];
7878
7879 /* use internal GUI code */
7880 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7881 OUT_STR(buf);
7882 }
7883 else
7884#endif
7885 {
7886 if (screen_attr > HL_ALL) /* special HL attr. */
7887 {
7888 attrentry_T *aep;
7889
7890 if (t_colors > 1)
7891 {
7892 /*
7893 * Assume that t_me restores the original colors!
7894 */
7895 aep = syn_cterm_attr2entry(screen_attr);
7896 if (aep != NULL && (aep->ae_u.cterm.fg_color
7897 || aep->ae_u.cterm.bg_color))
7898 do_ME = TRUE;
7899 }
7900 else
7901 {
7902 aep = syn_term_attr2entry(screen_attr);
7903 if (aep != NULL && aep->ae_u.term.stop != NULL)
7904 {
7905 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7906 do_ME = TRUE;
7907 else
7908 out_str(aep->ae_u.term.stop);
7909 }
7910 }
7911 if (aep == NULL) /* did ":syntax clear" */
7912 screen_attr = 0;
7913 else
7914 screen_attr = aep->ae_attr;
7915 }
7916
7917 /*
7918 * Often all ending-codes are equal to T_ME. Avoid outputting the
7919 * same sequence several times.
7920 */
7921 if (screen_attr & HL_STANDOUT)
7922 {
7923 if (STRCMP(T_SE, T_ME) == 0)
7924 do_ME = TRUE;
7925 else
7926 out_str(T_SE);
7927 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007928 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {
7930 if (STRCMP(T_UE, T_ME) == 0)
7931 do_ME = TRUE;
7932 else
7933 out_str(T_UE);
7934 }
7935 if (screen_attr & HL_ITALIC)
7936 {
7937 if (STRCMP(T_CZR, T_ME) == 0)
7938 do_ME = TRUE;
7939 else
7940 out_str(T_CZR);
7941 }
7942 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7943 out_str(T_ME);
7944
7945 if (t_colors > 1)
7946 {
7947 /* set Normal cterm colors */
7948 if (cterm_normal_fg_color != 0)
7949 term_fg_color(cterm_normal_fg_color - 1);
7950 if (cterm_normal_bg_color != 0)
7951 term_bg_color(cterm_normal_bg_color - 1);
7952 if (cterm_normal_fg_bold)
7953 out_str(T_MD);
7954 }
7955 }
7956 }
7957 screen_attr = 0;
7958}
7959
7960/*
7961 * Reset the colors for a cterm. Used when leaving Vim.
7962 * The machine specific code may override this again.
7963 */
7964 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007965reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 if (t_colors > 1)
7968 {
7969 /* set Normal cterm colors */
7970 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7971 {
7972 out_str(T_OP);
7973 screen_attr = -1;
7974 }
7975 if (cterm_normal_fg_bold)
7976 {
7977 out_str(T_ME);
7978 screen_attr = -1;
7979 }
7980 }
7981}
7982
7983/*
7984 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7985 * using the attributes from ScreenAttrs["off"].
7986 */
7987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007988screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 int attr;
7991
7992 /* Check for illegal values, just in case (could happen just after
7993 * resizing). */
7994 if (row >= screen_Rows || col >= screen_Columns)
7995 return;
7996
Bram Moolenaar494838a2015-02-10 19:20:37 +01007997 /* Outputting a character in the last cell on the screen may scroll the
7998 * screen up. Only do it when the "xn" termcap property is set, otherwise
7999 * mark the character invalid (update it when scrolled up). */
8000 if (*T_XN == NUL
8001 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002#ifdef FEAT_RIGHTLEFT
8003 /* account for first command-line character in rightleft mode */
8004 && !cmdmsg_rl
8005#endif
8006 )
8007 {
8008 ScreenAttrs[off] = (sattr_T)-1;
8009 return;
8010 }
8011
8012 /*
8013 * Stop highlighting first, so it's easier to move the cursor.
8014 */
8015#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8016 if (screen_char_attr != 0)
8017 attr = screen_char_attr;
8018 else
8019#endif
8020 attr = ScreenAttrs[off];
8021 if (screen_attr != attr)
8022 screen_stop_highlight();
8023
8024 windgoto(row, col);
8025
8026 if (screen_attr != attr)
8027 screen_start_highlight(attr);
8028
8029#ifdef FEAT_MBYTE
8030 if (enc_utf8 && ScreenLinesUC[off] != 0)
8031 {
8032 char_u buf[MB_MAXBYTES + 1];
8033
8034 /* Convert UTF-8 character to bytes and write it. */
8035
8036 buf[utfc_char2bytes(off, buf)] = NUL;
8037
8038 out_str(buf);
8039 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8040 ++screen_cur_col;
8041 }
8042 else
8043#endif
8044 {
8045#ifdef FEAT_MBYTE
8046 out_flush_check();
8047#endif
8048 out_char(ScreenLines[off]);
8049#ifdef FEAT_MBYTE
8050 /* double-byte character in single-width cell */
8051 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8052 out_char(ScreenLines2[off]);
8053#endif
8054 }
8055
8056 screen_cur_col++;
8057}
8058
8059#ifdef FEAT_MBYTE
8060
8061/*
8062 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8063 * on the screen at position 'row' and 'col'.
8064 * The attributes of the first byte is used for all. This is required to
8065 * output the two bytes of a double-byte character with nothing in between.
8066 */
8067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008068screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 /* Check for illegal values (could be wrong when screen was resized). */
8071 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8072 return;
8073
8074 /* Outputting the last character on the screen may scrollup the screen.
8075 * Don't to it! Mark the character invalid (update it when scrolled up) */
8076 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8077 {
8078 ScreenAttrs[off] = (sattr_T)-1;
8079 return;
8080 }
8081
8082 /* Output the first byte normally (positions the cursor), then write the
8083 * second byte directly. */
8084 screen_char(off, row, col);
8085 out_char(ScreenLines[off + 1]);
8086 ++screen_cur_col;
8087}
8088#endif
8089
8090#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8091/*
8092 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8093 * This uses the contents of ScreenLines[] and doesn't change it.
8094 */
8095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008096screen_draw_rectangle(
8097 int row,
8098 int col,
8099 int height,
8100 int width,
8101 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 int r, c;
8104 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008105#ifdef FEAT_MBYTE
8106 int max_off;
8107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008109 /* Can't use ScreenLines unless initialized */
8110 if (ScreenLines == NULL)
8111 return;
8112
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (invert)
8114 screen_char_attr = HL_INVERSE;
8115 for (r = row; r < row + height; ++r)
8116 {
8117 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008118#ifdef FEAT_MBYTE
8119 max_off = off + screen_Columns;
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 for (c = col; c < col + width; ++c)
8122 {
8123#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008124 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
8126 screen_char_2(off + c, r, c);
8127 ++c;
8128 }
8129 else
8130#endif
8131 {
8132 screen_char(off + c, r, c);
8133#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008134 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 ++c;
8136#endif
8137 }
8138 }
8139 }
8140 screen_char_attr = 0;
8141}
8142#endif
8143
8144#ifdef FEAT_VERTSPLIT
8145/*
8146 * Redraw the characters for a vertically split window.
8147 */
8148 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008149redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
8151 int col;
8152 int width;
8153
8154# ifdef FEAT_CLIPBOARD
8155 clip_may_clear_selection(row, end - 1);
8156# endif
8157
8158 if (wp == NULL)
8159 {
8160 col = 0;
8161 width = Columns;
8162 }
8163 else
8164 {
8165 col = wp->w_wincol;
8166 width = wp->w_width;
8167 }
8168 screen_draw_rectangle(row, col, end - row, width, FALSE);
8169}
8170#endif
8171
8172/*
8173 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8174 * with character 'c1' in first column followed by 'c2' in the other columns.
8175 * Use attributes 'attr'.
8176 */
8177 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008178screen_fill(
8179 int start_row,
8180 int end_row,
8181 int start_col,
8182 int end_col,
8183 int c1,
8184 int c2,
8185 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186{
8187 int row;
8188 int col;
8189 int off;
8190 int end_off;
8191 int did_delete;
8192 int c;
8193 int norm_term;
8194#if defined(FEAT_GUI) || defined(UNIX)
8195 int force_next = FALSE;
8196#endif
8197
8198 if (end_row > screen_Rows) /* safety check */
8199 end_row = screen_Rows;
8200 if (end_col > screen_Columns) /* safety check */
8201 end_col = screen_Columns;
8202 if (ScreenLines == NULL
8203 || start_row >= end_row
8204 || start_col >= end_col) /* nothing to do */
8205 return;
8206
8207 /* it's a "normal" terminal when not in a GUI or cterm */
8208 norm_term = (
8209#ifdef FEAT_GUI
8210 !gui.in_use &&
8211#endif
8212 t_colors <= 1);
8213 for (row = start_row; row < end_row; ++row)
8214 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008215#ifdef FEAT_MBYTE
8216 if (has_mbyte
8217# ifdef FEAT_GUI
8218 && !gui.in_use
8219# endif
8220 )
8221 {
8222 /* When drawing over the right halve of a double-wide char clear
8223 * out the left halve. When drawing over the left halve of a
8224 * double wide-char clear out the right halve. Only needed in a
8225 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008226 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008227 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008228 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008229 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008230 }
8231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 /*
8233 * Try to use delete-line termcap code, when no attributes or in a
8234 * "normal" terminal, where a bold/italic space is just a
8235 * space.
8236 */
8237 did_delete = FALSE;
8238 if (c2 == ' '
8239 && end_col == Columns
8240 && can_clear(T_CE)
8241 && (attr == 0
8242 || (norm_term
8243 && attr <= HL_ALL
8244 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8245 {
8246 /*
8247 * check if we really need to clear something
8248 */
8249 col = start_col;
8250 if (c1 != ' ') /* don't clear first char */
8251 ++col;
8252
8253 off = LineOffset[row] + col;
8254 end_off = LineOffset[row] + end_col;
8255
8256 /* skip blanks (used often, keep it fast!) */
8257#ifdef FEAT_MBYTE
8258 if (enc_utf8)
8259 while (off < end_off && ScreenLines[off] == ' '
8260 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8261 ++off;
8262 else
8263#endif
8264 while (off < end_off && ScreenLines[off] == ' '
8265 && ScreenAttrs[off] == 0)
8266 ++off;
8267 if (off < end_off) /* something to be cleared */
8268 {
8269 col = off - LineOffset[row];
8270 screen_stop_highlight();
8271 term_windgoto(row, col);/* clear rest of this screen line */
8272 out_str(T_CE);
8273 screen_start(); /* don't know where cursor is now */
8274 col = end_col - col;
8275 while (col--) /* clear chars in ScreenLines */
8276 {
8277 ScreenLines[off] = ' ';
8278#ifdef FEAT_MBYTE
8279 if (enc_utf8)
8280 ScreenLinesUC[off] = 0;
8281#endif
8282 ScreenAttrs[off] = 0;
8283 ++off;
8284 }
8285 }
8286 did_delete = TRUE; /* the chars are cleared now */
8287 }
8288
8289 off = LineOffset[row] + start_col;
8290 c = c1;
8291 for (col = start_col; col < end_col; ++col)
8292 {
8293 if (ScreenLines[off] != c
8294#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008295 || (enc_utf8 && (int)ScreenLinesUC[off]
8296 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297#endif
8298 || ScreenAttrs[off] != attr
8299#if defined(FEAT_GUI) || defined(UNIX)
8300 || force_next
8301#endif
8302 )
8303 {
8304#if defined(FEAT_GUI) || defined(UNIX)
8305 /* The bold trick may make a single row of pixels appear in
8306 * the next character. When a bold character is removed, the
8307 * next character should be redrawn too. This happens for our
8308 * own GUI and for some xterms. */
8309 if (
8310# ifdef FEAT_GUI
8311 gui.in_use
8312# endif
8313# if defined(FEAT_GUI) && defined(UNIX)
8314 ||
8315# endif
8316# ifdef UNIX
8317 term_is_xterm
8318# endif
8319 )
8320 {
8321 if (ScreenLines[off] != ' '
8322 && (ScreenAttrs[off] > HL_ALL
8323 || ScreenAttrs[off] & HL_BOLD))
8324 force_next = TRUE;
8325 else
8326 force_next = FALSE;
8327 }
8328#endif
8329 ScreenLines[off] = c;
8330#ifdef FEAT_MBYTE
8331 if (enc_utf8)
8332 {
8333 if (c >= 0x80)
8334 {
8335 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008336 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338 else
8339 ScreenLinesUC[off] = 0;
8340 }
8341#endif
8342 ScreenAttrs[off] = attr;
8343 if (!did_delete || c != ' ')
8344 screen_char(off, row, col);
8345 }
8346 ++off;
8347 if (col == start_col)
8348 {
8349 if (did_delete)
8350 break;
8351 c = c2;
8352 }
8353 }
8354 if (end_col == Columns)
8355 LineWraps[row] = FALSE;
8356 if (row == Rows - 1) /* overwritten the command line */
8357 {
8358 redraw_cmdline = TRUE;
8359 if (c1 == ' ' && c2 == ' ')
8360 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008361 if (start_col == 0)
8362 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 }
8364 }
8365}
8366
8367/*
8368 * Check if there should be a delay. Used before clearing or redrawing the
8369 * screen or the command line.
8370 */
8371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8375 && !did_wait_return
8376 && emsg_silent == 0)
8377 {
8378 out_flush();
8379 ui_delay(1000L, TRUE);
8380 emsg_on_display = FALSE;
8381 if (check_msg_scroll)
8382 msg_scroll = FALSE;
8383 }
8384}
8385
8386/*
8387 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008388 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 * Returns TRUE if there is a valid screen to write to.
8390 * Returns FALSE when starting up and screen not initialized yet.
8391 */
8392 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008393screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008395 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 return (ScreenLines != NULL);
8397}
8398
8399/*
8400 * Resize the shell to Rows and Columns.
8401 * Allocate ScreenLines[] and associated items.
8402 *
8403 * There may be some time between setting Rows and Columns and (re)allocating
8404 * ScreenLines[]. This happens when starting up and when (manually) changing
8405 * the shell size. Always use screen_Rows and screen_Columns to access items
8406 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8407 * final size of the shell is needed.
8408 */
8409 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008410screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 int new_row, old_row;
8413#ifdef FEAT_GUI
8414 int old_Rows;
8415#endif
8416 win_T *wp;
8417 int outofmem = FALSE;
8418 int len;
8419 schar_T *new_ScreenLines;
8420#ifdef FEAT_MBYTE
8421 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008422 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008424 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425#endif
8426 sattr_T *new_ScreenAttrs;
8427 unsigned *new_LineOffset;
8428 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008429#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008430 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008431 tabpage_T *tp;
8432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008434 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008435#ifdef FEAT_AUTOCMD
8436 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008438retry:
8439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 /*
8441 * Allocation of the screen buffers is done only when the size changes and
8442 * when Rows and Columns have been set and we have started doing full
8443 * screen stuff.
8444 */
8445 if ((ScreenLines != NULL
8446 && Rows == screen_Rows
8447 && Columns == screen_Columns
8448#ifdef FEAT_MBYTE
8449 && enc_utf8 == (ScreenLinesUC != NULL)
8450 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008451 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452#endif
8453 )
8454 || Rows == 0
8455 || Columns == 0
8456 || (!full_screen && ScreenLines == NULL))
8457 return;
8458
8459 /*
8460 * It's possible that we produce an out-of-memory message below, which
8461 * will cause this function to be called again. To break the loop, just
8462 * return here.
8463 */
8464 if (entered)
8465 return;
8466 entered = TRUE;
8467
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008468 /*
8469 * Note that the window sizes are updated before reallocating the arrays,
8470 * thus we must not redraw here!
8471 */
8472 ++RedrawingDisabled;
8473
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 win_new_shellsize(); /* fit the windows in the new sized shell */
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 comp_col(); /* recompute columns for shown command and ruler */
8477
8478 /*
8479 * We're changing the size of the screen.
8480 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8481 * - Move lines from the old arrays into the new arrays, clear extra
8482 * lines (unless the screen is going to be cleared).
8483 * - Free the old arrays.
8484 *
8485 * If anything fails, make ScreenLines NULL, so we don't do anything!
8486 * Continuing with the old ScreenLines may result in a crash, because the
8487 * size is wrong.
8488 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008489 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008491#ifdef FEAT_AUTOCMD
8492 if (aucmd_win != NULL)
8493 win_free_lsize(aucmd_win);
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
8496 new_ScreenLines = (schar_T *)lalloc((long_u)(
8497 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8498#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008499 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (enc_utf8)
8501 {
8502 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8503 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008504 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008505 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8507 }
8508 if (enc_dbcs == DBCS_JPNU)
8509 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8510 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8511#endif
8512 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8513 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8514 new_LineOffset = (unsigned *)lalloc((long_u)(
8515 Rows * sizeof(unsigned)), FALSE);
8516 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008517#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008518 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008521 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
8523 if (win_alloc_lines(wp) == FAIL)
8524 {
8525 outofmem = TRUE;
8526#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008527 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528#endif
8529 }
8530 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008531#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008532 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8533 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008534 outofmem = TRUE;
8535#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008536#ifdef FEAT_WINDOWS
8537give_up:
8538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008540#ifdef FEAT_MBYTE
8541 for (i = 0; i < p_mco; ++i)
8542 if (new_ScreenLinesC[i] == NULL)
8543 break;
8544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (new_ScreenLines == NULL
8546#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008547 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8549#endif
8550 || new_ScreenAttrs == NULL
8551 || new_LineOffset == NULL
8552 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008553#ifdef FEAT_WINDOWS
8554 || new_TabPageIdxs == NULL
8555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 || outofmem)
8557 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008558 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008559 {
8560 /* guess the size */
8561 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8562
8563 /* Remember we did this to avoid getting outofmem messages over
8564 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008565 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 vim_free(new_ScreenLines);
8568 new_ScreenLines = NULL;
8569#ifdef FEAT_MBYTE
8570 vim_free(new_ScreenLinesUC);
8571 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008572 for (i = 0; i < p_mco; ++i)
8573 {
8574 vim_free(new_ScreenLinesC[i]);
8575 new_ScreenLinesC[i] = NULL;
8576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 vim_free(new_ScreenLines2);
8578 new_ScreenLines2 = NULL;
8579#endif
8580 vim_free(new_ScreenAttrs);
8581 new_ScreenAttrs = NULL;
8582 vim_free(new_LineOffset);
8583 new_LineOffset = NULL;
8584 vim_free(new_LineWraps);
8585 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008586#ifdef FEAT_WINDOWS
8587 vim_free(new_TabPageIdxs);
8588 new_TabPageIdxs = NULL;
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591 else
8592 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008593 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 for (new_row = 0; new_row < Rows; ++new_row)
8596 {
8597 new_LineOffset[new_row] = new_row * Columns;
8598 new_LineWraps[new_row] = FALSE;
8599
8600 /*
8601 * If the screen is not going to be cleared, copy as much as
8602 * possible from the old screen to the new one and clear the rest
8603 * (used when resizing the window at the "--more--" prompt or when
8604 * executing an external command, for the GUI).
8605 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008606 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
8608 (void)vim_memset(new_ScreenLines + new_row * Columns,
8609 ' ', (size_t)Columns * sizeof(schar_T));
8610#ifdef FEAT_MBYTE
8611 if (enc_utf8)
8612 {
8613 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8614 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 for (i = 0; i < p_mco; ++i)
8616 (void)vim_memset(new_ScreenLinesC[i]
8617 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 0, (size_t)Columns * sizeof(u8char_T));
8619 }
8620 if (enc_dbcs == DBCS_JPNU)
8621 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8622 0, (size_t)Columns * sizeof(schar_T));
8623#endif
8624 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8625 0, (size_t)Columns * sizeof(sattr_T));
8626 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008627 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
8629 if (screen_Columns < Columns)
8630 len = screen_Columns;
8631 else
8632 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008633#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008634 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008635 * may be invalid now. Also when p_mco changes. */
8636 if (!(enc_utf8 && ScreenLinesUC == NULL)
8637 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008638#endif
8639 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8640 ScreenLines + LineOffset[old_row],
8641 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008643 if (enc_utf8 && ScreenLinesUC != NULL
8644 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8647 ScreenLinesUC + LineOffset[old_row],
8648 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008649 for (i = 0; i < p_mco; ++i)
8650 mch_memmove(new_ScreenLinesC[i]
8651 + new_LineOffset[new_row],
8652 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 (size_t)len * sizeof(u8char_T));
8654 }
8655 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8656 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8657 ScreenLines2 + LineOffset[old_row],
8658 (size_t)len * sizeof(schar_T));
8659#endif
8660 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8661 ScreenAttrs + LineOffset[old_row],
8662 (size_t)len * sizeof(sattr_T));
8663 }
8664 }
8665 }
8666 /* Use the last line of the screen for the current line. */
8667 current_ScreenLine = new_ScreenLines + Rows * Columns;
8668 }
8669
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008670 free_screenlines();
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 ScreenLines = new_ScreenLines;
8673#ifdef FEAT_MBYTE
8674 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008675 for (i = 0; i < p_mco; ++i)
8676 ScreenLinesC[i] = new_ScreenLinesC[i];
8677 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 ScreenLines2 = new_ScreenLines2;
8679#endif
8680 ScreenAttrs = new_ScreenAttrs;
8681 LineOffset = new_LineOffset;
8682 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008683#ifdef FEAT_WINDOWS
8684 TabPageIdxs = new_TabPageIdxs;
8685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
8687 /* It's important that screen_Rows and screen_Columns reflect the actual
8688 * size of ScreenLines[]. Set them before calling anything. */
8689#ifdef FEAT_GUI
8690 old_Rows = screen_Rows;
8691#endif
8692 screen_Rows = Rows;
8693 screen_Columns = Columns;
8694
8695 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008696 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 screenclear2();
8698
8699#ifdef FEAT_GUI
8700 else if (gui.in_use
8701 && !gui.starting
8702 && ScreenLines != NULL
8703 && old_Rows != Rows)
8704 {
8705 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8706 /*
8707 * Adjust the position of the cursor, for when executing an external
8708 * command.
8709 */
8710 if (msg_row >= Rows) /* Rows got smaller */
8711 msg_row = Rows - 1; /* put cursor at last row */
8712 else if (Rows > old_Rows) /* Rows got bigger */
8713 msg_row += Rows - old_Rows; /* put cursor in same place */
8714 if (msg_col >= Columns) /* Columns got smaller */
8715 msg_col = Columns - 1; /* put cursor at last column */
8716 }
8717#endif
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008720 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008721
8722#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008723 /*
8724 * Do not apply autocommands more than 3 times to avoid an endless loop
8725 * in case applying autocommands always changes Rows or Columns.
8726 */
8727 if (starting == 0 && ++retry_count <= 3)
8728 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008729 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008730 /* In rare cases, autocommands may have altered Rows or Columns,
8731 * jump back to check if we need to allocate the screen again. */
8732 goto retry;
8733 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
8736
8737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008738free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008739{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008740#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 int i;
8742
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008743 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008744 for (i = 0; i < Screen_mco; ++i)
8745 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008746 vim_free(ScreenLines2);
8747#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008748 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008749 vim_free(ScreenAttrs);
8750 vim_free(LineOffset);
8751 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008752#ifdef FEAT_WINDOWS
8753 vim_free(TabPageIdxs);
8754#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008755}
8756
8757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008758screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759{
8760 check_for_delay(FALSE);
8761 screenalloc(FALSE); /* allocate screen buffers if size changed */
8762 screenclear2(); /* clear the screen */
8763}
8764
8765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008766screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 int i;
8769
8770 if (starting == NO_SCREEN || ScreenLines == NULL
8771#ifdef FEAT_GUI
8772 || (gui.in_use && gui.starting)
8773#endif
8774 )
8775 return;
8776
8777#ifdef FEAT_GUI
8778 if (!gui.in_use)
8779#endif
8780 screen_attr = -1; /* force setting the Normal colors */
8781 screen_stop_highlight(); /* don't want highlighting here */
8782
8783#ifdef FEAT_CLIPBOARD
8784 /* disable selection without redrawing it */
8785 clip_scroll_selection(9999);
8786#endif
8787
8788 /* blank out ScreenLines */
8789 for (i = 0; i < Rows; ++i)
8790 {
8791 lineclear(LineOffset[i], (int)Columns);
8792 LineWraps[i] = FALSE;
8793 }
8794
8795 if (can_clear(T_CL))
8796 {
8797 out_str(T_CL); /* clear the display */
8798 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008799 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 }
8801 else
8802 {
8803 /* can't clear the screen, mark all chars with invalid attributes */
8804 for (i = 0; i < Rows; ++i)
8805 lineinvalid(LineOffset[i], (int)Columns);
8806 clear_cmdline = TRUE;
8807 }
8808
8809 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8810
8811 win_rest_invalid(firstwin);
8812 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008813#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008814 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 if (must_redraw == CLEAR) /* no need to clear again */
8817 must_redraw = NOT_VALID;
8818 compute_cmdrow();
8819 msg_row = cmdline_row; /* put cursor on last line for messages */
8820 msg_col = 0;
8821 screen_start(); /* don't know where cursor is now */
8822 msg_scrolled = 0; /* can't scroll back */
8823 msg_didany = FALSE;
8824 msg_didout = FALSE;
8825}
8826
8827/*
8828 * Clear one line in ScreenLines.
8829 */
8830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008831lineclear(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8834#ifdef FEAT_MBYTE
8835 if (enc_utf8)
8836 (void)vim_memset(ScreenLinesUC + off, 0,
8837 (size_t)width * sizeof(u8char_T));
8838#endif
8839 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8840}
8841
8842/*
8843 * Mark one line in ScreenLines invalid by setting the attributes to an
8844 * invalid value.
8845 */
8846 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008847lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8850}
8851
8852#ifdef FEAT_VERTSPLIT
8853/*
8854 * Copy part of a Screenline for vertically split window "wp".
8855 */
8856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008857linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 unsigned off_to = LineOffset[to] + wp->w_wincol;
8860 unsigned off_from = LineOffset[from] + wp->w_wincol;
8861
8862 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8863 wp->w_width * sizeof(schar_T));
8864# ifdef FEAT_MBYTE
8865 if (enc_utf8)
8866 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008867 int i;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8870 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008871 for (i = 0; i < p_mco; ++i)
8872 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8873 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 if (enc_dbcs == DBCS_JPNU)
8876 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8877 wp->w_width * sizeof(schar_T));
8878# endif
8879 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8880 wp->w_width * sizeof(sattr_T));
8881}
8882#endif
8883
8884/*
8885 * Return TRUE if clearing with term string "p" would work.
8886 * It can't work when the string is empty or it won't set the right background.
8887 */
8888 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008889can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 return (*p != NUL && (t_colors <= 1
8892#ifdef FEAT_GUI
8893 || gui.in_use
8894#endif
8895 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8896}
8897
8898/*
8899 * Reset cursor position. Use whenever cursor was moved because of outputting
8900 * something directly to the screen (shell commands) or a terminal control
8901 * code.
8902 */
8903 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008904screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 screen_cur_row = screen_cur_col = 9999;
8907}
8908
8909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 * Move the cursor to position "row","col" in the screen.
8911 * This tries to find the most efficient way to move, minimizing the number of
8912 * characters sent to the terminal.
8913 */
8914 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008915windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008917 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 int i;
8919 int plan;
8920 int cost;
8921 int wouldbe_col;
8922 int noinvcurs;
8923 char_u *bs;
8924 int goto_cost;
8925 int attr;
8926
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008927#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8929
8930#define PLAN_LE 1
8931#define PLAN_CR 2
8932#define PLAN_NL 3
8933#define PLAN_WRITE 4
8934 /* Can't use ScreenLines unless initialized */
8935 if (ScreenLines == NULL)
8936 return;
8937
8938 if (col != screen_cur_col || row != screen_cur_row)
8939 {
8940 /* Check for valid position. */
8941 if (row < 0) /* window without text lines? */
8942 row = 0;
8943 if (row >= screen_Rows)
8944 row = screen_Rows - 1;
8945 if (col >= screen_Columns)
8946 col = screen_Columns - 1;
8947
8948 /* check if no cursor movement is allowed in highlight mode */
8949 if (screen_attr && *T_MS == NUL)
8950 noinvcurs = HIGHL_COST;
8951 else
8952 noinvcurs = 0;
8953 goto_cost = GOTO_COST + noinvcurs;
8954
8955 /*
8956 * Plan how to do the positioning:
8957 * 1. Use CR to move it to column 0, same row.
8958 * 2. Use T_LE to move it a few columns to the left.
8959 * 3. Use NL to move a few lines down, column 0.
8960 * 4. Move a few columns to the right with T_ND or by writing chars.
8961 *
8962 * Don't do this if the cursor went beyond the last column, the cursor
8963 * position is unknown then (some terminals wrap, some don't )
8964 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008965 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 * characters to move the cursor to the right.
8967 */
8968 if (row >= screen_cur_row && screen_cur_col < Columns)
8969 {
8970 /*
8971 * If the cursor is in the same row, bigger col, we can use CR
8972 * or T_LE.
8973 */
8974 bs = NULL; /* init for GCC */
8975 attr = screen_attr;
8976 if (row == screen_cur_row && col < screen_cur_col)
8977 {
8978 /* "le" is preferred over "bc", because "bc" is obsolete */
8979 if (*T_LE)
8980 bs = T_LE; /* "cursor left" */
8981 else
8982 bs = T_BC; /* "backspace character (old) */
8983 if (*bs)
8984 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8985 else
8986 cost = 999;
8987 if (col + 1 < cost) /* using CR is less characters */
8988 {
8989 plan = PLAN_CR;
8990 wouldbe_col = 0;
8991 cost = 1; /* CR is just one character */
8992 }
8993 else
8994 {
8995 plan = PLAN_LE;
8996 wouldbe_col = col;
8997 }
8998 if (noinvcurs) /* will stop highlighting */
8999 {
9000 cost += noinvcurs;
9001 attr = 0;
9002 }
9003 }
9004
9005 /*
9006 * If the cursor is above where we want to be, we can use CR LF.
9007 */
9008 else if (row > screen_cur_row)
9009 {
9010 plan = PLAN_NL;
9011 wouldbe_col = 0;
9012 cost = (row - screen_cur_row) * 2; /* CR LF */
9013 if (noinvcurs) /* will stop highlighting */
9014 {
9015 cost += noinvcurs;
9016 attr = 0;
9017 }
9018 }
9019
9020 /*
9021 * If the cursor is in the same row, smaller col, just use write.
9022 */
9023 else
9024 {
9025 plan = PLAN_WRITE;
9026 wouldbe_col = screen_cur_col;
9027 cost = 0;
9028 }
9029
9030 /*
9031 * Check if any characters that need to be written have the
9032 * correct attributes. Also avoid UTF-8 characters.
9033 */
9034 i = col - wouldbe_col;
9035 if (i > 0)
9036 cost += i;
9037 if (cost < goto_cost && i > 0)
9038 {
9039 /*
9040 * Check if the attributes are correct without additionally
9041 * stopping highlighting.
9042 */
9043 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9044 while (i && *p++ == attr)
9045 --i;
9046 if (i != 0)
9047 {
9048 /*
9049 * Try if it works when highlighting is stopped here.
9050 */
9051 if (*--p == 0)
9052 {
9053 cost += noinvcurs;
9054 while (i && *p++ == 0)
9055 --i;
9056 }
9057 if (i != 0)
9058 cost = 999; /* different attributes, don't do it */
9059 }
9060#ifdef FEAT_MBYTE
9061 if (enc_utf8)
9062 {
9063 /* Don't use an UTF-8 char for positioning, it's slow. */
9064 for (i = wouldbe_col; i < col; ++i)
9065 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9066 {
9067 cost = 999;
9068 break;
9069 }
9070 }
9071#endif
9072 }
9073
9074 /*
9075 * We can do it without term_windgoto()!
9076 */
9077 if (cost < goto_cost)
9078 {
9079 if (plan == PLAN_LE)
9080 {
9081 if (noinvcurs)
9082 screen_stop_highlight();
9083 while (screen_cur_col > col)
9084 {
9085 out_str(bs);
9086 --screen_cur_col;
9087 }
9088 }
9089 else if (plan == PLAN_CR)
9090 {
9091 if (noinvcurs)
9092 screen_stop_highlight();
9093 out_char('\r');
9094 screen_cur_col = 0;
9095 }
9096 else if (plan == PLAN_NL)
9097 {
9098 if (noinvcurs)
9099 screen_stop_highlight();
9100 while (screen_cur_row < row)
9101 {
9102 out_char('\n');
9103 ++screen_cur_row;
9104 }
9105 screen_cur_col = 0;
9106 }
9107
9108 i = col - screen_cur_col;
9109 if (i > 0)
9110 {
9111 /*
9112 * Use cursor-right if it's one character only. Avoids
9113 * removing a line of pixels from the last bold char, when
9114 * using the bold trick in the GUI.
9115 */
9116 if (T_ND[0] != NUL && T_ND[1] == NUL)
9117 {
9118 while (i-- > 0)
9119 out_char(*T_ND);
9120 }
9121 else
9122 {
9123 int off;
9124
9125 off = LineOffset[row] + screen_cur_col;
9126 while (i-- > 0)
9127 {
9128 if (ScreenAttrs[off] != screen_attr)
9129 screen_stop_highlight();
9130#ifdef FEAT_MBYTE
9131 out_flush_check();
9132#endif
9133 out_char(ScreenLines[off]);
9134#ifdef FEAT_MBYTE
9135 if (enc_dbcs == DBCS_JPNU
9136 && ScreenLines[off] == 0x8e)
9137 out_char(ScreenLines2[off]);
9138#endif
9139 ++off;
9140 }
9141 }
9142 }
9143 }
9144 }
9145 else
9146 cost = 999;
9147
9148 if (cost >= goto_cost)
9149 {
9150 if (noinvcurs)
9151 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009152 if (row == screen_cur_row && (col > screen_cur_col)
9153 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 term_cursor_right(col - screen_cur_col);
9155 else
9156 term_windgoto(row, col);
9157 }
9158 screen_cur_row = row;
9159 screen_cur_col = col;
9160 }
9161}
9162
9163/*
9164 * Set cursor to its position in the current window.
9165 */
9166 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009167setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 if (redrawing())
9170 {
9171 validate_cursor();
9172 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9173 W_WINCOL(curwin) + (
9174#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009175 /* With 'rightleft' set and the cursor on a double-wide
9176 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9178# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009179 (has_mbyte
9180 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9181 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182# endif
9183 1)) :
9184#endif
9185 curwin->w_wcol));
9186 }
9187}
9188
9189
9190/*
9191 * insert 'line_count' lines at 'row' in window 'wp'
9192 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9193 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9194 * scrolling.
9195 * Returns FAIL if the lines are not inserted, OK for success.
9196 */
9197 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009198win_ins_lines(
9199 win_T *wp,
9200 int row,
9201 int line_count,
9202 int invalid,
9203 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
9205 int did_delete;
9206 int nextrow;
9207 int lastrow;
9208 int retval;
9209
9210 if (invalid)
9211 wp->w_lines_valid = 0;
9212
9213 if (wp->w_height < 5)
9214 return FAIL;
9215
9216 if (line_count > wp->w_height - row)
9217 line_count = wp->w_height - row;
9218
9219 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9220 if (retval != MAYBE)
9221 return retval;
9222
9223 /*
9224 * If there is a next window or a status line, we first try to delete the
9225 * lines at the bottom to avoid messing what is after the window.
9226 * If this fails and there are following windows, don't do anything to avoid
9227 * messing up those windows, better just redraw.
9228 */
9229 did_delete = FALSE;
9230#ifdef FEAT_WINDOWS
9231 if (wp->w_next != NULL || wp->w_status_height)
9232 {
9233 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9234 line_count, (int)Rows, FALSE, NULL) == OK)
9235 did_delete = TRUE;
9236 else if (wp->w_next)
9237 return FAIL;
9238 }
9239#endif
9240 /*
9241 * if no lines deleted, blank the lines that will end up below the window
9242 */
9243 if (!did_delete)
9244 {
9245#ifdef FEAT_WINDOWS
9246 wp->w_redr_status = TRUE;
9247#endif
9248 redraw_cmdline = TRUE;
9249 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9250 lastrow = nextrow + line_count;
9251 if (lastrow > Rows)
9252 lastrow = Rows;
9253 screen_fill(nextrow - line_count, lastrow - line_count,
9254 W_WINCOL(wp), (int)W_ENDCOL(wp),
9255 ' ', ' ', 0);
9256 }
9257
9258 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9259 == FAIL)
9260 {
9261 /* deletion will have messed up other windows */
9262 if (did_delete)
9263 {
9264#ifdef FEAT_WINDOWS
9265 wp->w_redr_status = TRUE;
9266#endif
9267 win_rest_invalid(W_NEXT(wp));
9268 }
9269 return FAIL;
9270 }
9271
9272 return OK;
9273}
9274
9275/*
9276 * delete "line_count" window lines at "row" in window "wp"
9277 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9278 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9279 * scrolling
9280 * Return OK for success, FAIL if the lines are not deleted.
9281 */
9282 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009283win_del_lines(
9284 win_T *wp,
9285 int row,
9286 int line_count,
9287 int invalid,
9288 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
9290 int retval;
9291
9292 if (invalid)
9293 wp->w_lines_valid = 0;
9294
9295 if (line_count > wp->w_height - row)
9296 line_count = wp->w_height - row;
9297
9298 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9299 if (retval != MAYBE)
9300 return retval;
9301
9302 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9303 (int)Rows, FALSE, NULL) == FAIL)
9304 return FAIL;
9305
9306#ifdef FEAT_WINDOWS
9307 /*
9308 * If there are windows or status lines below, try to put them at the
9309 * correct place. If we can't do that, they have to be redrawn.
9310 */
9311 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9312 {
9313 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9314 line_count, (int)Rows, NULL) == FAIL)
9315 {
9316 wp->w_redr_status = TRUE;
9317 win_rest_invalid(wp->w_next);
9318 }
9319 }
9320 /*
9321 * If this is the last window and there is no status line, redraw the
9322 * command line later.
9323 */
9324 else
9325#endif
9326 redraw_cmdline = TRUE;
9327 return OK;
9328}
9329
9330/*
9331 * Common code for win_ins_lines() and win_del_lines().
9332 * Returns OK or FAIL when the work has been done.
9333 * Returns MAYBE when not finished yet.
9334 */
9335 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009336win_do_lines(
9337 win_T *wp,
9338 int row,
9339 int line_count,
9340 int mayclear,
9341 int del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 int retval;
9344
9345 if (!redrawing() || line_count <= 0)
9346 return FAIL;
9347
9348 /* only a few lines left: redraw is faster */
9349 if (mayclear && Rows - line_count < 5
9350#ifdef FEAT_VERTSPLIT
9351 && wp->w_width == Columns
9352#endif
9353 )
9354 {
9355 screenclear(); /* will set wp->w_lines_valid to 0 */
9356 return FAIL;
9357 }
9358
9359 /*
9360 * Delete all remaining lines
9361 */
9362 if (row + line_count >= wp->w_height)
9363 {
9364 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9365 W_WINCOL(wp), (int)W_ENDCOL(wp),
9366 ' ', ' ', 0);
9367 return OK;
9368 }
9369
9370 /*
9371 * when scrolling, the message on the command line should be cleared,
9372 * otherwise it will stay there forever.
9373 */
9374 clear_cmdline = TRUE;
9375
9376 /*
9377 * If the terminal can set a scroll region, use that.
9378 * Always do this in a vertically split window. This will redraw from
9379 * ScreenLines[] when t_CV isn't defined. That's faster than using
9380 * win_line().
9381 * Don't use a scroll region when we are going to redraw the text, writing
9382 * a character in the lower right corner of the scroll region causes a
9383 * scroll-up in the DJGPP version.
9384 */
9385 if (scroll_region
9386#ifdef FEAT_VERTSPLIT
9387 || W_WIDTH(wp) != Columns
9388#endif
9389 )
9390 {
9391#ifdef FEAT_VERTSPLIT
9392 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9393#endif
9394 scroll_region_set(wp, row);
9395 if (del)
9396 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9397 wp->w_height - row, FALSE, wp);
9398 else
9399 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9400 wp->w_height - row, wp);
9401#ifdef FEAT_VERTSPLIT
9402 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9403#endif
9404 scroll_region_reset();
9405 return retval;
9406 }
9407
9408#ifdef FEAT_WINDOWS
9409 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9410 return FAIL;
9411#endif
9412
9413 return MAYBE;
9414}
9415
9416/*
9417 * window 'wp' and everything after it is messed up, mark it for redraw
9418 */
9419 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009420win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422#ifdef FEAT_WINDOWS
9423 while (wp != NULL)
9424#else
9425 if (wp != NULL)
9426#endif
9427 {
9428 redraw_win_later(wp, NOT_VALID);
9429#ifdef FEAT_WINDOWS
9430 wp->w_redr_status = TRUE;
9431 wp = wp->w_next;
9432#endif
9433 }
9434 redraw_cmdline = TRUE;
9435}
9436
9437/*
9438 * The rest of the routines in this file perform screen manipulations. The
9439 * given operation is performed physically on the screen. The corresponding
9440 * change is also made to the internal screen image. In this way, the editor
9441 * anticipates the effect of editing changes on the appearance of the screen.
9442 * That way, when we call screenupdate a complete redraw isn't usually
9443 * necessary. Another advantage is that we can keep adding code to anticipate
9444 * screen changes, and in the meantime, everything still works.
9445 */
9446
9447/*
9448 * types for inserting or deleting lines
9449 */
9450#define USE_T_CAL 1
9451#define USE_T_CDL 2
9452#define USE_T_AL 3
9453#define USE_T_CE 4
9454#define USE_T_DL 5
9455#define USE_T_SR 6
9456#define USE_NL 7
9457#define USE_T_CD 8
9458#define USE_REDRAW 9
9459
9460/*
9461 * insert lines on the screen and update ScreenLines[]
9462 * 'end' is the line after the scrolled part. Normally it is Rows.
9463 * When scrolling region used 'off' is the offset from the top for the region.
9464 * 'row' and 'end' are relative to the start of the region.
9465 *
9466 * return FAIL for failure, OK for success.
9467 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009468 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009469screen_ins_lines(
9470 int off,
9471 int row,
9472 int line_count,
9473 int end,
9474 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475{
9476 int i;
9477 int j;
9478 unsigned temp;
9479 int cursor_row;
9480 int type;
9481 int result_empty;
9482 int can_ce = can_clear(T_CE);
9483
9484 /*
9485 * FAIL if
9486 * - there is no valid screen
9487 * - the screen has to be redrawn completely
9488 * - the line count is less than one
9489 * - the line count is more than 'ttyscroll'
9490 */
9491 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9492 return FAIL;
9493
9494 /*
9495 * There are seven ways to insert lines:
9496 * 0. When in a vertically split window and t_CV isn't set, redraw the
9497 * characters from ScreenLines[].
9498 * 1. Use T_CD (clear to end of display) if it exists and the result of
9499 * the insert is just empty lines
9500 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9501 * present or line_count > 1. It looks better if we do all the inserts
9502 * at once.
9503 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9504 * insert is just empty lines and T_CE is not present or line_count >
9505 * 1.
9506 * 4. Use T_AL (insert line) if it exists.
9507 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9508 * just empty lines.
9509 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9510 * just empty lines.
9511 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9512 * the 'da' flag is not set or we have clear line capability.
9513 * 8. redraw the characters from ScreenLines[].
9514 *
9515 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9516 * the scrollbar for the window. It does have insert line, use that if it
9517 * exists.
9518 */
9519 result_empty = (row + line_count >= end);
9520#ifdef FEAT_VERTSPLIT
9521 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9522 type = USE_REDRAW;
9523 else
9524#endif
9525 if (can_clear(T_CD) && result_empty)
9526 type = USE_T_CD;
9527 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9528 type = USE_T_CAL;
9529 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9530 type = USE_T_CDL;
9531 else if (*T_AL != NUL)
9532 type = USE_T_AL;
9533 else if (can_ce && result_empty)
9534 type = USE_T_CE;
9535 else if (*T_DL != NUL && result_empty)
9536 type = USE_T_DL;
9537 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9538 type = USE_T_SR;
9539 else
9540 return FAIL;
9541
9542 /*
9543 * For clearing the lines screen_del_lines() is used. This will also take
9544 * care of t_db if necessary.
9545 */
9546 if (type == USE_T_CD || type == USE_T_CDL ||
9547 type == USE_T_CE || type == USE_T_DL)
9548 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9549
9550 /*
9551 * If text is retained below the screen, first clear or delete as many
9552 * lines at the bottom of the window as are about to be inserted so that
9553 * the deleted lines won't later surface during a screen_del_lines.
9554 */
9555 if (*T_DB)
9556 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9557
9558#ifdef FEAT_CLIPBOARD
9559 /* Remove a modeless selection when inserting lines halfway the screen
9560 * or not the full width of the screen. */
9561 if (off + row > 0
9562# ifdef FEAT_VERTSPLIT
9563 || (wp != NULL && wp->w_width != Columns)
9564# endif
9565 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009566 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 else
9568 clip_scroll_selection(-line_count);
9569#endif
9570
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#ifdef FEAT_GUI
9572 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9573 * scrolling is actually carried out. */
9574 gui_dont_update_cursor();
9575#endif
9576
9577 if (*T_CCS != NUL) /* cursor relative to region */
9578 cursor_row = row;
9579 else
9580 cursor_row = row + off;
9581
9582 /*
9583 * Shift LineOffset[] line_count down to reflect the inserted lines.
9584 * Clear the inserted lines in ScreenLines[].
9585 */
9586 row += off;
9587 end += off;
9588 for (i = 0; i < line_count; ++i)
9589 {
9590#ifdef FEAT_VERTSPLIT
9591 if (wp != NULL && wp->w_width != Columns)
9592 {
9593 /* need to copy part of a line */
9594 j = end - 1 - i;
9595 while ((j -= line_count) >= row)
9596 linecopy(j + line_count, j, wp);
9597 j += line_count;
9598 if (can_clear((char_u *)" "))
9599 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9600 else
9601 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9602 LineWraps[j] = FALSE;
9603 }
9604 else
9605#endif
9606 {
9607 j = end - 1 - i;
9608 temp = LineOffset[j];
9609 while ((j -= line_count) >= row)
9610 {
9611 LineOffset[j + line_count] = LineOffset[j];
9612 LineWraps[j + line_count] = LineWraps[j];
9613 }
9614 LineOffset[j + line_count] = temp;
9615 LineWraps[j + line_count] = FALSE;
9616 if (can_clear((char_u *)" "))
9617 lineclear(temp, (int)Columns);
9618 else
9619 lineinvalid(temp, (int)Columns);
9620 }
9621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
9623 screen_stop_highlight();
9624 windgoto(cursor_row, 0);
9625
9626#ifdef FEAT_VERTSPLIT
9627 /* redraw the characters */
9628 if (type == USE_REDRAW)
9629 redraw_block(row, end, wp);
9630 else
9631#endif
9632 if (type == USE_T_CAL)
9633 {
9634 term_append_lines(line_count);
9635 screen_start(); /* don't know where cursor is now */
9636 }
9637 else
9638 {
9639 for (i = 0; i < line_count; i++)
9640 {
9641 if (type == USE_T_AL)
9642 {
9643 if (i && cursor_row != 0)
9644 windgoto(cursor_row, 0);
9645 out_str(T_AL);
9646 }
9647 else /* type == USE_T_SR */
9648 out_str(T_SR);
9649 screen_start(); /* don't know where cursor is now */
9650 }
9651 }
9652
9653 /*
9654 * With scroll-reverse and 'da' flag set we need to clear the lines that
9655 * have been scrolled down into the region.
9656 */
9657 if (type == USE_T_SR && *T_DA)
9658 {
9659 for (i = 0; i < line_count; ++i)
9660 {
9661 windgoto(off + i, 0);
9662 out_str(T_CE);
9663 screen_start(); /* don't know where cursor is now */
9664 }
9665 }
9666
9667#ifdef FEAT_GUI
9668 gui_can_update_cursor();
9669 if (gui.in_use)
9670 out_flush(); /* always flush after a scroll */
9671#endif
9672 return OK;
9673}
9674
9675/*
9676 * delete lines on the screen and update ScreenLines[]
9677 * 'end' is the line after the scrolled part. Normally it is Rows.
9678 * When scrolling region used 'off' is the offset from the top for the region.
9679 * 'row' and 'end' are relative to the start of the region.
9680 *
9681 * Return OK for success, FAIL if the lines are not deleted.
9682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009684screen_del_lines(
9685 int off,
9686 int row,
9687 int line_count,
9688 int end,
9689 int force, /* even when line_count > p_ttyscroll */
9690 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692 int j;
9693 int i;
9694 unsigned temp;
9695 int cursor_row;
9696 int cursor_end;
9697 int result_empty; /* result is empty until end of region */
9698 int can_delete; /* deleting line codes can be used */
9699 int type;
9700
9701 /*
9702 * FAIL if
9703 * - there is no valid screen
9704 * - the screen has to be redrawn completely
9705 * - the line count is less than one
9706 * - the line count is more than 'ttyscroll'
9707 */
9708 if (!screen_valid(TRUE) || line_count <= 0 ||
9709 (!force && line_count > p_ttyscroll))
9710 return FAIL;
9711
9712 /*
9713 * Check if the rest of the current region will become empty.
9714 */
9715 result_empty = row + line_count >= end;
9716
9717 /*
9718 * We can delete lines only when 'db' flag not set or when 'ce' option
9719 * available.
9720 */
9721 can_delete = (*T_DB == NUL || can_clear(T_CE));
9722
9723 /*
9724 * There are six ways to delete lines:
9725 * 0. When in a vertically split window and t_CV isn't set, redraw the
9726 * characters from ScreenLines[].
9727 * 1. Use T_CD if it exists and the result is empty.
9728 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9729 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9730 * none of the other ways work.
9731 * 4. Use T_CE (erase line) if the result is empty.
9732 * 5. Use T_DL (delete line) if it exists.
9733 * 6. redraw the characters from ScreenLines[].
9734 */
9735#ifdef FEAT_VERTSPLIT
9736 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9737 type = USE_REDRAW;
9738 else
9739#endif
9740 if (can_clear(T_CD) && result_empty)
9741 type = USE_T_CD;
9742#if defined(__BEOS__) && defined(BEOS_DR8)
9743 /*
9744 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9745 * its internal termcap... this works okay for tests which test *T_DB !=
9746 * NUL. It has the disadvantage that the user cannot use any :set t_*
9747 * command to get T_DB (back) to empty_option, only :set term=... will do
9748 * the trick...
9749 * Anyway, this hack will hopefully go away with the next OS release.
9750 * (Olaf Seibert)
9751 */
9752 else if (row == 0 && T_DB == empty_option
9753 && (line_count == 1 || *T_CDL == NUL))
9754#else
9755 else if (row == 0 && (
9756#ifndef AMIGA
9757 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9758 * up, so use delete-line command */
9759 line_count == 1 ||
9760#endif
9761 *T_CDL == NUL))
9762#endif
9763 type = USE_NL;
9764 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9765 type = USE_T_CDL;
9766 else if (can_clear(T_CE) && result_empty
9767#ifdef FEAT_VERTSPLIT
9768 && (wp == NULL || wp->w_width == Columns)
9769#endif
9770 )
9771 type = USE_T_CE;
9772 else if (*T_DL != NUL && can_delete)
9773 type = USE_T_DL;
9774 else if (*T_CDL != NUL && can_delete)
9775 type = USE_T_CDL;
9776 else
9777 return FAIL;
9778
9779#ifdef FEAT_CLIPBOARD
9780 /* Remove a modeless selection when deleting lines halfway the screen or
9781 * not the full width of the screen. */
9782 if (off + row > 0
9783# ifdef FEAT_VERTSPLIT
9784 || (wp != NULL && wp->w_width != Columns)
9785# endif
9786 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009787 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 else
9789 clip_scroll_selection(line_count);
9790#endif
9791
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792#ifdef FEAT_GUI
9793 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9794 * scrolling is actually carried out. */
9795 gui_dont_update_cursor();
9796#endif
9797
9798 if (*T_CCS != NUL) /* cursor relative to region */
9799 {
9800 cursor_row = row;
9801 cursor_end = end;
9802 }
9803 else
9804 {
9805 cursor_row = row + off;
9806 cursor_end = end + off;
9807 }
9808
9809 /*
9810 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9811 * Clear the inserted lines in ScreenLines[].
9812 */
9813 row += off;
9814 end += off;
9815 for (i = 0; i < line_count; ++i)
9816 {
9817#ifdef FEAT_VERTSPLIT
9818 if (wp != NULL && wp->w_width != Columns)
9819 {
9820 /* need to copy part of a line */
9821 j = row + i;
9822 while ((j += line_count) <= end - 1)
9823 linecopy(j - line_count, j, wp);
9824 j -= line_count;
9825 if (can_clear((char_u *)" "))
9826 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9827 else
9828 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9829 LineWraps[j] = FALSE;
9830 }
9831 else
9832#endif
9833 {
9834 /* whole width, moving the line pointers is faster */
9835 j = row + i;
9836 temp = LineOffset[j];
9837 while ((j += line_count) <= end - 1)
9838 {
9839 LineOffset[j - line_count] = LineOffset[j];
9840 LineWraps[j - line_count] = LineWraps[j];
9841 }
9842 LineOffset[j - line_count] = temp;
9843 LineWraps[j - line_count] = FALSE;
9844 if (can_clear((char_u *)" "))
9845 lineclear(temp, (int)Columns);
9846 else
9847 lineinvalid(temp, (int)Columns);
9848 }
9849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850
9851 screen_stop_highlight();
9852
9853#ifdef FEAT_VERTSPLIT
9854 /* redraw the characters */
9855 if (type == USE_REDRAW)
9856 redraw_block(row, end, wp);
9857 else
9858#endif
9859 if (type == USE_T_CD) /* delete the lines */
9860 {
9861 windgoto(cursor_row, 0);
9862 out_str(T_CD);
9863 screen_start(); /* don't know where cursor is now */
9864 }
9865 else if (type == USE_T_CDL)
9866 {
9867 windgoto(cursor_row, 0);
9868 term_delete_lines(line_count);
9869 screen_start(); /* don't know where cursor is now */
9870 }
9871 /*
9872 * Deleting lines at top of the screen or scroll region: Just scroll
9873 * the whole screen (scroll region) up by outputting newlines on the
9874 * last line.
9875 */
9876 else if (type == USE_NL)
9877 {
9878 windgoto(cursor_end - 1, 0);
9879 for (i = line_count; --i >= 0; )
9880 out_char('\n'); /* cursor will remain on same line */
9881 }
9882 else
9883 {
9884 for (i = line_count; --i >= 0; )
9885 {
9886 if (type == USE_T_DL)
9887 {
9888 windgoto(cursor_row, 0);
9889 out_str(T_DL); /* delete a line */
9890 }
9891 else /* type == USE_T_CE */
9892 {
9893 windgoto(cursor_row + i, 0);
9894 out_str(T_CE); /* erase a line */
9895 }
9896 screen_start(); /* don't know where cursor is now */
9897 }
9898 }
9899
9900 /*
9901 * If the 'db' flag is set, we need to clear the lines that have been
9902 * scrolled up at the bottom of the region.
9903 */
9904 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9905 {
9906 for (i = line_count; i > 0; --i)
9907 {
9908 windgoto(cursor_end - i, 0);
9909 out_str(T_CE); /* erase a line */
9910 screen_start(); /* don't know where cursor is now */
9911 }
9912 }
9913
9914#ifdef FEAT_GUI
9915 gui_can_update_cursor();
9916 if (gui.in_use)
9917 out_flush(); /* always flush after a scroll */
9918#endif
9919
9920 return OK;
9921}
9922
9923/*
9924 * show the current mode and ruler
9925 *
9926 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9927 * If clear_cmdline is FALSE there may be a message there that needs to be
9928 * cleared only if a mode is shown.
9929 * Return the length of the message (0 if no message).
9930 */
9931 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009932showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933{
9934 int need_clear;
9935 int length = 0;
9936 int do_mode;
9937 int attr;
9938 int nwr_save;
9939#ifdef FEAT_INS_EXPAND
9940 int sub_attr;
9941#endif
9942
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009943 do_mode = ((p_smd && msg_silent == 0)
9944 && ((State & INSERT)
9945 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009946 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 if (do_mode || Recording)
9948 {
9949 /*
9950 * Don't show mode right now, when not redrawing or inside a mapping.
9951 * Call char_avail() only when we are going to show something, because
9952 * it takes a bit of time.
9953 */
9954 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9955 {
9956 redraw_cmdline = TRUE; /* show mode later */
9957 return 0;
9958 }
9959
9960 nwr_save = need_wait_return;
9961
9962 /* wait a bit before overwriting an important message */
9963 check_for_delay(FALSE);
9964
9965 /* if the cmdline is more than one line high, erase top lines */
9966 need_clear = clear_cmdline;
9967 if (clear_cmdline && cmdline_row < Rows - 1)
9968 msg_clr_cmdline(); /* will reset clear_cmdline */
9969
9970 /* Position on the last line in the window, column 0 */
9971 msg_pos_mode();
9972 cursor_off();
9973 attr = hl_attr(HLF_CM); /* Highlight mode */
9974 if (do_mode)
9975 {
9976 MSG_PUTS_ATTR("--", attr);
9977#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009978 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009979# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009980 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009981# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009982 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009983# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009984 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009985# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 MSG_PUTS_ATTR(" IM", attr);
9987# else
9988 MSG_PUTS_ATTR(" XIM", attr);
9989# endif
9990#endif
9991#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9992 if (gui.in_use)
9993 {
9994 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009995 {
9996 /* HANGUL */
9997 if (enc_utf8)
9998 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
9999 else
10000 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 }
10003#endif
10004#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010005 /* CTRL-X in Insert mode */
10006 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
10008 /* These messages can get long, avoid a wrap in a narrow
10009 * window. Prefer showing edit_submode_extra. */
10010 length = (Rows - msg_row) * Columns - 3;
10011 if (edit_submode_extra != NULL)
10012 length -= vim_strsize(edit_submode_extra);
10013 if (length > 0)
10014 {
10015 if (edit_submode_pre != NULL)
10016 length -= vim_strsize(edit_submode_pre);
10017 if (length - vim_strsize(edit_submode) > 0)
10018 {
10019 if (edit_submode_pre != NULL)
10020 msg_puts_attr(edit_submode_pre, attr);
10021 msg_puts_attr(edit_submode, attr);
10022 }
10023 if (edit_submode_extra != NULL)
10024 {
10025 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10026 if ((int)edit_submode_highl < (int)HLF_COUNT)
10027 sub_attr = hl_attr(edit_submode_highl);
10028 else
10029 sub_attr = attr;
10030 msg_puts_attr(edit_submode_extra, sub_attr);
10031 }
10032 }
10033 length = 0;
10034 }
10035 else
10036#endif
10037 {
10038#ifdef FEAT_VREPLACE
10039 if (State & VREPLACE_FLAG)
10040 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10041 else
10042#endif
10043 if (State & REPLACE_FLAG)
10044 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10045 else if (State & INSERT)
10046 {
10047#ifdef FEAT_RIGHTLEFT
10048 if (p_ri)
10049 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10050#endif
10051 MSG_PUTS_ATTR(_(" INSERT"), attr);
10052 }
10053 else if (restart_edit == 'I')
10054 MSG_PUTS_ATTR(_(" (insert)"), attr);
10055 else if (restart_edit == 'R')
10056 MSG_PUTS_ATTR(_(" (replace)"), attr);
10057 else if (restart_edit == 'V')
10058 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10059#ifdef FEAT_RIGHTLEFT
10060 if (p_hkmap)
10061 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10062# ifdef FEAT_FKMAP
10063 if (p_fkmap)
10064 MSG_PUTS_ATTR(farsi_text_5, attr);
10065# endif
10066#endif
10067#ifdef FEAT_KEYMAP
10068 if (State & LANGMAP)
10069 {
10070# ifdef FEAT_ARABIC
10071 if (curwin->w_p_arab)
10072 MSG_PUTS_ATTR(_(" Arabic"), attr);
10073 else
10074# endif
10075 MSG_PUTS_ATTR(_(" (lang)"), attr);
10076 }
10077#endif
10078 if ((State & INSERT) && p_paste)
10079 MSG_PUTS_ATTR(_(" (paste)"), attr);
10080
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 if (VIsual_active)
10082 {
10083 char *p;
10084
10085 /* Don't concatenate separate words to avoid translation
10086 * problems. */
10087 switch ((VIsual_select ? 4 : 0)
10088 + (VIsual_mode == Ctrl_V) * 2
10089 + (VIsual_mode == 'V'))
10090 {
10091 case 0: p = N_(" VISUAL"); break;
10092 case 1: p = N_(" VISUAL LINE"); break;
10093 case 2: p = N_(" VISUAL BLOCK"); break;
10094 case 4: p = N_(" SELECT"); break;
10095 case 5: p = N_(" SELECT LINE"); break;
10096 default: p = N_(" SELECT BLOCK"); break;
10097 }
10098 MSG_PUTS_ATTR(_(p), attr);
10099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 MSG_PUTS_ATTR(" --", attr);
10101 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 need_clear = TRUE;
10104 }
10105 if (Recording
10106#ifdef FEAT_INS_EXPAND
10107 && edit_submode == NULL /* otherwise it gets too long */
10108#endif
10109 )
10110 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010111 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 need_clear = TRUE;
10113 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010114
10115 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (need_clear || clear_cmdline)
10117 msg_clr_eos();
10118 msg_didout = FALSE; /* overwrite this message */
10119 length = msg_col;
10120 msg_col = 0;
10121 need_wait_return = nwr_save; /* never ask for hit-return for this */
10122 }
10123 else if (clear_cmdline && msg_silent == 0)
10124 /* Clear the whole command line. Will reset "clear_cmdline". */
10125 msg_clr_cmdline();
10126
10127#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 /* In Visual mode the size of the selected area must be redrawn. */
10129 if (VIsual_active)
10130 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131
10132 /* If the last window has no status line, the ruler is after the mode
10133 * message and must be redrawn */
10134 if (redrawing()
10135# ifdef FEAT_WINDOWS
10136 && lastwin->w_status_height == 0
10137# endif
10138 )
10139 win_redr_ruler(lastwin, TRUE);
10140#endif
10141 redraw_cmdline = FALSE;
10142 clear_cmdline = FALSE;
10143
10144 return length;
10145}
10146
10147/*
10148 * Position for a mode message.
10149 */
10150 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010151msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 msg_col = 0;
10154 msg_row = Rows - 1;
10155}
10156
10157/*
10158 * Delete mode message. Used when ESC is typed which is expected to end
10159 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010160 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 */
10162 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010163unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164{
10165 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010166 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 */
10168 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10169 redraw_cmdline = TRUE; /* delete mode later */
10170 else
10171 {
10172 msg_pos_mode();
10173 if (Recording)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010174 recording_mode(hl_attr(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 msg_clr_eos();
10176 }
10177}
10178
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010180recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010181{
10182 MSG_PUTS_ATTR(_("recording"), attr);
10183 if (!shortmess(SHM_RECORDING))
10184 {
10185 char_u s[4];
10186 sprintf((char *)s, " @%c", Recording);
10187 MSG_PUTS_ATTR(s, attr);
10188 }
10189}
10190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010191#if defined(FEAT_WINDOWS)
10192/*
10193 * Draw the tab pages line at the top of the Vim window.
10194 */
10195 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010196draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010197{
10198 int tabcount = 0;
10199 tabpage_T *tp;
10200 int tabwidth;
10201 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010202 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010203 int attr;
10204 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010205 win_T *cwp;
10206 int wincount;
10207 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010208 int c;
10209 int len;
10210 int attr_sel = hl_attr(HLF_TPS);
10211 int attr_nosel = hl_attr(HLF_TP);
10212 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010213 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010214 int room;
10215 int use_sep_chars = (t_colors < 8
10216#ifdef FEAT_GUI
10217 && !gui.in_use
10218#endif
10219 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010220
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010221 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010222
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010223#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010224 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010225 if (gui_use_tabline())
10226 {
10227 gui_update_tabline();
10228 return;
10229 }
10230#endif
10231
10232 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010233 return;
10234
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010235#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010236
10237 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10238 for (scol = 0; scol < Columns; ++scol)
10239 TabPageIdxs[scol] = 0;
10240
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010241 /* Use the 'tabline' option if it's set. */
10242 if (*p_tal != NUL)
10243 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010244 int save_called_emsg = called_emsg;
10245
10246 /* Check for an error. If there is one we would loop in redrawing the
10247 * screen. Avoid that by making 'tabline' empty. */
10248 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010249 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010250 if (called_emsg)
10251 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010252 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010253 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010254 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010255 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010256#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010257 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010258 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10259 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010260
Bram Moolenaar238a5642006-02-21 22:12:05 +000010261 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10262 if (tabwidth < 6)
10263 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010264
Bram Moolenaar238a5642006-02-21 22:12:05 +000010265 attr = attr_nosel;
10266 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010267 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010268 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10269 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010270 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010271 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010272
Bram Moolenaar238a5642006-02-21 22:12:05 +000010273 if (tp->tp_topframe == topframe)
10274 attr = attr_sel;
10275 if (use_sep_chars && col > 0)
10276 screen_putchar('|', 0, col++, attr);
10277
10278 if (tp->tp_topframe != topframe)
10279 attr = attr_nosel;
10280
10281 screen_putchar(' ', 0, col++, attr);
10282
10283 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010284 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010285 cwp = curwin;
10286 wp = firstwin;
10287 }
10288 else
10289 {
10290 cwp = tp->tp_curwin;
10291 wp = tp->tp_firstwin;
10292 }
10293
10294 modified = FALSE;
10295 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10296 if (bufIsChanged(wp->w_buffer))
10297 modified = TRUE;
10298 if (modified || wincount > 1)
10299 {
10300 if (wincount > 1)
10301 {
10302 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010303 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010304 if (col + len >= Columns - 3)
10305 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010306 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010307#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010308 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010309#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010310 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010311#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010312 );
10313 col += len;
10314 }
10315 if (modified)
10316 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10317 screen_putchar(' ', 0, col++, attr);
10318 }
10319
10320 room = scol - col + tabwidth - 1;
10321 if (room > 0)
10322 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010323 /* Get buffer name in NameBuff[] */
10324 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010325 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010326 len = vim_strsize(NameBuff);
10327 p = NameBuff;
10328#ifdef FEAT_MBYTE
10329 if (has_mbyte)
10330 while (len > room)
10331 {
10332 len -= ptr2cells(p);
10333 mb_ptr_adv(p);
10334 }
10335 else
10336#endif
10337 if (len > room)
10338 {
10339 p += len - room;
10340 len = room;
10341 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010342 if (len > Columns - col - 1)
10343 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010345 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010346 col += len;
10347 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010348 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010349
10350 /* Store the tab page number in TabPageIdxs[], so that
10351 * jump_to_mouse() knows where each one is. */
10352 ++tabcount;
10353 while (scol < col)
10354 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010355 }
10356
Bram Moolenaar238a5642006-02-21 22:12:05 +000010357 if (use_sep_chars)
10358 c = '_';
10359 else
10360 c = ' ';
10361 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010362
10363 /* Put an "X" for closing the current tab if there are several. */
10364 if (first_tabpage->tp_next != NULL)
10365 {
10366 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10367 TabPageIdxs[Columns - 1] = -999;
10368 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010369 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010370
10371 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10372 * set. */
10373 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010374}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010375
10376/*
10377 * Get buffer name for "buf" into NameBuff[].
10378 * Takes care of special buffer names and translates special characters.
10379 */
10380 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010381get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010382{
10383 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010384 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010385 else
10386 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10387 trans_characters(NameBuff, MAXPATHL);
10388}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010389#endif
10390
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10392/*
10393 * Get the character to use in a status line. Get its attributes in "*attr".
10394 */
10395 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010396fillchar_status(int *attr, int is_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397{
10398 int fill;
10399 if (is_curwin)
10400 {
10401 *attr = hl_attr(HLF_S);
10402 fill = fill_stl;
10403 }
10404 else
10405 {
10406 *attr = hl_attr(HLF_SNC);
10407 fill = fill_stlnc;
10408 }
10409 /* Use fill when there is highlighting, and highlighting of current
10410 * window differs, or the fillchars differ, or this is not the
10411 * current window */
10412 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10413 || !is_curwin || firstwin == lastwin)
10414 || (fill_stl != fill_stlnc)))
10415 return fill;
10416 if (is_curwin)
10417 return '^';
10418 return '=';
10419}
10420#endif
10421
10422#ifdef FEAT_VERTSPLIT
10423/*
10424 * Get the character to use in a separator between vertically split windows.
10425 * Get its attributes in "*attr".
10426 */
10427 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010428fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429{
10430 *attr = hl_attr(HLF_C);
10431 if (*attr == 0 && fill_vert == ' ')
10432 return '|';
10433 else
10434 return fill_vert;
10435}
10436#endif
10437
10438/*
10439 * Return TRUE if redrawing should currently be done.
10440 */
10441 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010442redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443{
10444 return (!RedrawingDisabled
10445 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10446}
10447
10448/*
10449 * Return TRUE if printing messages should currently be done.
10450 */
10451 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010452messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
10454 return (!(p_lz && char_avail() && !KeyTyped));
10455}
10456
10457/*
10458 * Show current status info in ruler and various other places
10459 * If always is FALSE, only show ruler if position has changed.
10460 */
10461 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010462showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
10464 if (!always && !redrawing())
10465 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010466#ifdef FEAT_INS_EXPAND
10467 if (pum_visible())
10468 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010469# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010470 /* Don't redraw right now, do it later. */
10471 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010472# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010473 return;
10474 }
10475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010477 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010478 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010479 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 else
10482#endif
10483#ifdef FEAT_CMDL_INFO
10484 win_redr_ruler(curwin, always);
10485#endif
10486
10487#ifdef FEAT_TITLE
10488 if (need_maketitle
10489# ifdef FEAT_STL_OPT
10490 || (p_icon && (stl_syntax & STL_IN_ICON))
10491 || (p_title && (stl_syntax & STL_IN_TITLE))
10492# endif
10493 )
10494 maketitle();
10495#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010496#ifdef FEAT_WINDOWS
10497 /* Redraw the tab pages line if needed. */
10498 if (redraw_tabline)
10499 draw_tabline();
10500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501}
10502
10503#ifdef FEAT_CMDL_INFO
10504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010505win_redr_ruler(win_T *wp, int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010507#define RULER_BUF_LEN 70
10508 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 int row;
10510 int fillchar;
10511 int attr;
10512 int empty_line = FALSE;
10513 colnr_T virtcol;
10514 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010515 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 int o;
10517#ifdef FEAT_VERTSPLIT
10518 int this_ru_col;
10519 int off = 0;
10520 int width = Columns;
10521# define WITH_OFF(x) x
10522# define WITH_WIDTH(x) x
10523#else
10524# define WITH_OFF(x) 0
10525# define WITH_WIDTH(x) Columns
10526# define this_ru_col ru_col
10527#endif
10528
10529 /* If 'ruler' off or redrawing disabled, don't do anything */
10530 if (!p_ru)
10531 return;
10532
10533 /*
10534 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10535 * after deleting lines, before cursor.lnum is corrected.
10536 */
10537 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10538 return;
10539
10540#ifdef FEAT_INS_EXPAND
10541 /* Don't draw the ruler while doing insert-completion, it might overwrite
10542 * the (long) mode message. */
10543# ifdef FEAT_WINDOWS
10544 if (wp == lastwin && lastwin->w_status_height == 0)
10545# endif
10546 if (edit_submode != NULL)
10547 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010548 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10549 if (pum_visible())
10550 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551#endif
10552
10553#ifdef FEAT_STL_OPT
10554 if (*p_ruf)
10555 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010556 int save_called_emsg = called_emsg;
10557
10558 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010560 if (called_emsg)
10561 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010562 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010563 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 return;
10565 }
10566#endif
10567
10568 /*
10569 * Check if not in Insert mode and the line is empty (will show "0-1").
10570 */
10571 if (!(State & INSERT)
10572 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10573 empty_line = TRUE;
10574
10575 /*
10576 * Only draw the ruler when something changed.
10577 */
10578 validate_virtcol_win(wp);
10579 if ( redraw_cmdline
10580 || always
10581 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10582 || wp->w_cursor.col != wp->w_ru_cursor.col
10583 || wp->w_virtcol != wp->w_ru_virtcol
10584#ifdef FEAT_VIRTUALEDIT
10585 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10586#endif
10587 || wp->w_topline != wp->w_ru_topline
10588 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10589#ifdef FEAT_DIFF
10590 || wp->w_topfill != wp->w_ru_topfill
10591#endif
10592 || empty_line != wp->w_ru_empty)
10593 {
10594 cursor_off();
10595#ifdef FEAT_WINDOWS
10596 if (wp->w_status_height)
10597 {
10598 row = W_WINROW(wp) + wp->w_height;
10599 fillchar = fillchar_status(&attr, wp == curwin);
10600# ifdef FEAT_VERTSPLIT
10601 off = W_WINCOL(wp);
10602 width = W_WIDTH(wp);
10603# endif
10604 }
10605 else
10606#endif
10607 {
10608 row = Rows - 1;
10609 fillchar = ' ';
10610 attr = 0;
10611#ifdef FEAT_VERTSPLIT
10612 width = Columns;
10613 off = 0;
10614#endif
10615 }
10616
10617 /* In list mode virtcol needs to be recomputed */
10618 virtcol = wp->w_virtcol;
10619 if (wp->w_p_list && lcs_tab1 == NUL)
10620 {
10621 wp->w_p_list = FALSE;
10622 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10623 wp->w_p_list = TRUE;
10624 }
10625
10626 /*
10627 * Some sprintfs return the length, some return a pointer.
10628 * To avoid portability problems we use strlen() here.
10629 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010630 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10632 ? 0L
10633 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010634 len = STRLEN(buffer);
10635 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10637 (int)virtcol + 1);
10638
10639 /*
10640 * Add a "50%" if there is room for it.
10641 * On the last line, don't print in the last column (scrolls the
10642 * screen up on some terminals).
10643 */
10644 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010645 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 o = i + vim_strsize(buffer + i + 1);
10647#ifdef FEAT_WINDOWS
10648 if (wp->w_status_height == 0) /* can't use last char of screen */
10649#endif
10650 ++o;
10651#ifdef FEAT_VERTSPLIT
10652 this_ru_col = ru_col - (Columns - width);
10653 if (this_ru_col < 0)
10654 this_ru_col = 0;
10655#endif
10656 /* Never use more than half the window/screen width, leave the other
10657 * half for the filename. */
10658 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10659 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10660 if (this_ru_col + o < WITH_WIDTH(width))
10661 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010662 /* need at least 3 chars left for get_rel_pos() + NUL */
10663 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 {
10665#ifdef FEAT_MBYTE
10666 if (has_mbyte)
10667 i += (*mb_char2bytes)(fillchar, buffer + i);
10668 else
10669#endif
10670 buffer[i++] = fillchar;
10671 ++o;
10672 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010673 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 }
10675 /* Truncate at window boundary. */
10676#ifdef FEAT_MBYTE
10677 if (has_mbyte)
10678 {
10679 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010680 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 {
10682 o += (*mb_ptr2cells)(buffer + i);
10683 if (this_ru_col + o > WITH_WIDTH(width))
10684 {
10685 buffer[i] = NUL;
10686 break;
10687 }
10688 }
10689 }
10690 else
10691#endif
10692 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10693 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10694
10695 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10696 i = redraw_cmdline;
10697 screen_fill(row, row + 1,
10698 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10699 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10700 fillchar, fillchar, attr);
10701 /* don't redraw the cmdline because of showing the ruler */
10702 redraw_cmdline = i;
10703 wp->w_ru_cursor = wp->w_cursor;
10704 wp->w_ru_virtcol = wp->w_virtcol;
10705 wp->w_ru_empty = empty_line;
10706 wp->w_ru_topline = wp->w_topline;
10707 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10708#ifdef FEAT_DIFF
10709 wp->w_ru_topfill = wp->w_topfill;
10710#endif
10711 }
10712}
10713#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010714
10715#if defined(FEAT_LINEBREAK) || defined(PROTO)
10716/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010717 * Return the width of the 'number' and 'relativenumber' column.
10718 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010719 * Otherwise it depends on 'numberwidth' and the line count.
10720 */
10721 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010722number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010723{
10724 int n;
10725 linenr_T lnum;
10726
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010727 if (wp->w_p_rnu && !wp->w_p_nu)
10728 /* cursor line shows "0" */
10729 lnum = wp->w_height;
10730 else
10731 /* cursor line shows absolute line number */
10732 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010733
Bram Moolenaar6b314672015-03-20 15:42:10 +010010734 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010735 return wp->w_nrwidth_width;
10736 wp->w_nrwidth_line_count = lnum;
10737
10738 n = 0;
10739 do
10740 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010741 lnum /= 10;
10742 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010743 } while (lnum > 0);
10744
10745 /* 'numberwidth' gives the minimal width plus one */
10746 if (n < wp->w_p_nuw - 1)
10747 n = wp->w_p_nuw - 1;
10748
10749 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010750 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010751 return n;
10752}
10753#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010754
10755/*
10756 * Return the current cursor column. This is the actual position on the
10757 * screen. First column is 0.
10758 */
10759 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010760screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010761{
10762 return screen_cur_col;
10763}
10764
10765/*
10766 * Return the current cursor row. This is the actual position on the screen.
10767 * First row is 0.
10768 */
10769 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010770screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010771{
10772 return screen_cur_row;
10773}