blob: d80ad6f96e69b3b6a89da5f4c879d27531ec545e [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 Moolenaar1c934292015-01-27 16:39:29 +0100112static int compute_foldcolumn __ARGS((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
120static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000121static void win_draw_end __ARGS((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
123static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
124static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
125static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
126#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000127static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
129#ifdef FEAT_RIGHTLEFT
130static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
131# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
132#else
133static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
134# 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
137static void draw_vsep_win __ARGS((win_T *wp, int row));
138#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000140static void redraw_custom_statusline __ARGS((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 Moolenaar071d4272004-06-13 20:20:40 +0000144static void start_search_hl __ARGS((void));
145static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200146static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200148static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
149static int next_search_hl_pos __ARGS((match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
151static void screen_start_highlight __ARGS((int attr));
152static void screen_char __ARGS((unsigned off, int row, int col));
153#ifdef FEAT_MBYTE
154static void screen_char_2 __ARGS((unsigned off, int row, int col));
155#endif
156static void screenclear2 __ARGS((void));
157static void lineclear __ARGS((unsigned off, int width));
158static void lineinvalid __ARGS((unsigned off, int width));
159#ifdef FEAT_VERTSPLIT
160static void linecopy __ARGS((int to, int from, win_T *wp));
161static void redraw_block __ARGS((int row, int end, win_T *wp));
162#endif
163static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
164static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165static void msg_pos_mode __ARGS((void));
Bram Moolenaara0ed84a2015-11-19 17:56:13 +0100166static void recording_mode __ARGS((int attr));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000168static void draw_tabline __ARGS((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)
171static int fillchar_status __ARGS((int *attr, int is_curwin));
172#endif
173#ifdef FEAT_VERTSPLIT
174static int fillchar_vsep __ARGS((int *attr));
175#endif
176#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000177static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#endif
179#ifdef FEAT_CMDL_INFO
180static void win_redr_ruler __ARGS((win_T *wp, int always));
181#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
194redraw_later(type)
195 int type;
196{
197 redraw_win_later(curwin, type);
198}
199
200 void
201redraw_win_later(wp, type)
202 win_T *wp;
203 int type;
204{
205 if (wp->w_redr_type < type)
206 {
207 wp->w_redr_type = type;
208 if (type >= NOT_VALID)
209 wp->w_lines_valid = 0;
210 if (must_redraw < type) /* must_redraw is the maximum of all windows */
211 must_redraw = type;
212 }
213}
214
215/*
216 * Force a complete redraw later. Also resets the highlighting. To be used
217 * after executing a shell command that messes up the screen.
218 */
219 void
220redraw_later_clear()
221{
222 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000223#ifdef FEAT_GUI
224 if (gui.in_use)
225 /* Use a code that will reset gui.highlight_mask in
226 * gui_stop_highlight(). */
227 screen_attr = HL_ALL + 1;
228 else
229#endif
230 /* Use attributes that is very unlikely to appear in text. */
231 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232}
233
234/*
235 * Mark all windows to be redrawn later.
236 */
237 void
238redraw_all_later(type)
239 int type;
240{
241 win_T *wp;
242
243 FOR_ALL_WINDOWS(wp)
244 {
245 redraw_win_later(wp, type);
246 }
247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
253redraw_curbuf_later(type)
254 int type;
255{
256 redraw_buf_later(curbuf, type);
257}
258
259 void
260redraw_buf_later(buf, type)
261 buf_T *buf;
262 int type;
263{
264 win_T *wp;
265
266 FOR_ALL_WINDOWS(wp)
267 {
268 if (wp->w_buffer == buf)
269 redraw_win_later(wp, type);
270 }
271}
272
273/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200274 * Redraw as soon as possible. When the command line is not scrolled redraw
275 * right away and restore what was on the command line.
276 * Return a code indicating what happened.
277 */
278 int
279redraw_asap(type)
280 int type;
281{
282 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200283 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200284 int r;
285 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 schar_T *screenline; /* copy from ScreenLines[] */
287 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288#ifdef FEAT_MBYTE
289 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200290 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200291 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200292 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200293#endif
294
295 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 return ret;
298
299 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200300 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200302 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200304 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305 if (screenline == NULL || screenattr == NULL)
306 ret = 2;
307#ifdef FEAT_MBYTE
308 if (enc_utf8)
309 {
310 screenlineUC = (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 (screenlineUC == NULL)
313 ret = 2;
314 for (i = 0; i < p_mco; ++i)
315 {
316 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 if (screenlineC[i] == NULL)
319 ret = 2;
320 }
321 }
322 if (enc_dbcs == DBCS_JPNU)
323 {
324 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 if (screenline2 == NULL)
327 ret = 2;
328 }
329#endif
330
331 if (ret != 2)
332 {
333 /* Save the text displayed in the command line area. */
334 for (r = 0; r < rows; ++r)
335 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200336 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200338 (size_t)cols * sizeof(schar_T));
339 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342#ifdef FEAT_MBYTE
343 if (enc_utf8)
344 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200345 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200346 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200347 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200348 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 mch_memmove(screenlineC[i] + r * cols,
350 ScreenLinesC[i] + LineOffset[cmdline_row + r],
351 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 }
353 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357#endif
358 }
359
360 update_screen(0);
361 ret = 3;
362
363 if (must_redraw == 0)
364 {
365 int off = (int)(current_ScreenLine - ScreenLines);
366
367 /* Restore the text displayed in the command line area. */
368 for (r = 0; r < rows; ++r)
369 {
370 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 screenline + r * cols,
372 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 screenattr + r * cols,
375 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376#ifdef FEAT_MBYTE
377 if (enc_utf8)
378 {
379 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 screenlineUC + r * cols,
381 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382 for (i = 0; i < p_mco; ++i)
383 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200384 screenlineC[i] + r * cols,
385 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200386 }
387 if (enc_dbcs == DBCS_JPNU)
388 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200389 screenline2 + r * cols,
390 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200391#endif
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 SCREEN_LINE(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394 ret = 4;
395 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 }
397
398 vim_free(screenline);
399 vim_free(screenattr);
400#ifdef FEAT_MBYTE
401 if (enc_utf8)
402 {
403 vim_free(screenlineUC);
404 for (i = 0; i < p_mco; ++i)
405 vim_free(screenlineC[i]);
406 }
407 if (enc_dbcs == DBCS_JPNU)
408 vim_free(screenline2);
409#endif
410
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200411 /* Show the intro message when appropriate. */
412 maybe_intro_message();
413
414 setcursor();
415
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416 return ret;
417}
418
419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 * Changed something in the current window, at buffer line "lnum", that
421 * requires that line and possibly other lines to be redrawn.
422 * Used when entering/leaving Insert mode with the cursor on a folded line.
423 * Used to remove the "$" from a change command.
424 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
425 * may become invalid and the whole window will have to be redrawn.
426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 void
428redrawWinline(lnum, invalid)
429 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000430 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
432#ifdef FEAT_FOLDING
433 int i;
434#endif
435
436 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
437 curwin->w_redraw_top = lnum;
438 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
439 curwin->w_redraw_bot = lnum;
440 redraw_later(VALID);
441
442#ifdef FEAT_FOLDING
443 if (invalid)
444 {
445 /* A w_lines[] entry for this lnum has become invalid. */
446 i = find_wl_entry(curwin, lnum);
447 if (i >= 0)
448 curwin->w_lines[i].wl_valid = FALSE;
449 }
450#endif
451}
452
453/*
454 * update all windows that are editing the current buffer
455 */
456 void
457update_curbuf(type)
458 int type;
459{
460 redraw_curbuf_later(type);
461 update_screen(type);
462}
463
464/*
465 * update_screen()
466 *
467 * Based on the current value of curwin->w_topline, transfer a screenfull
468 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
469 */
470 void
471update_screen(type)
472 int type;
473{
474 win_T *wp;
475 static int did_intro = FALSE;
476#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
477 int did_one;
478#endif
479
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000480 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (!screen_valid(TRUE))
482 return;
483
484 if (must_redraw)
485 {
486 if (type < must_redraw) /* use maximal type */
487 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000488
489 /* must_redraw is reset here, so that when we run into some weird
490 * reason to redraw while busy redrawing (e.g., asynchronous
491 * scrolling), or update_topline() in win_update() will cause a
492 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 must_redraw = 0;
494 }
495
496 /* Need to update w_lines[]. */
497 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
498 type = NOT_VALID;
499
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000500 /* Postpone the redrawing when it's not needed and when being called
501 * recursively. */
502 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 {
504 redraw_later(type); /* remember type for next time */
505 must_redraw = type;
506 if (type > INVERTED_ALL)
507 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
508 return;
509 }
510
511 updating_screen = TRUE;
512#ifdef FEAT_SYN_HL
513 ++display_tick; /* let syntax code know we're in a next round of
514 * display updating */
515#endif
516
517 /*
518 * if the screen was scrolled up when displaying a message, scroll it down
519 */
520 if (msg_scrolled)
521 {
522 clear_cmdline = TRUE;
523 if (msg_scrolled > Rows - 5) /* clearing is faster */
524 type = CLEAR;
525 else if (type != CLEAR)
526 {
527 check_for_delay(FALSE);
528 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
529 type = CLEAR;
530 FOR_ALL_WINDOWS(wp)
531 {
532 if (W_WINROW(wp) < msg_scrolled)
533 {
534 if (W_WINROW(wp) + wp->w_height > msg_scrolled
535 && wp->w_redr_type < REDRAW_TOP
536 && wp->w_lines_valid > 0
537 && wp->w_topline == wp->w_lines[0].wl_lnum)
538 {
539 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
540 wp->w_redr_type = REDRAW_TOP;
541 }
542 else
543 {
544 wp->w_redr_type = NOT_VALID;
545#ifdef FEAT_WINDOWS
546 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
547 <= msg_scrolled)
548 wp->w_redr_status = TRUE;
549#endif
550 }
551 }
552 }
553 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000554#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000555 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558 msg_scrolled = 0;
559 need_wait_return = FALSE;
560 }
561
562 /* reset cmdline_row now (may have been changed temporarily) */
563 compute_cmdrow();
564
565 /* Check for changed highlighting */
566 if (need_highlight_changed)
567 highlight_changed();
568
569 if (type == CLEAR) /* first clear screen */
570 {
571 screenclear(); /* will reset clear_cmdline */
572 type = NOT_VALID;
573 }
574
575 if (clear_cmdline) /* going to clear cmdline (done below) */
576 check_for_delay(FALSE);
577
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000578#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200579 /* Force redraw when width of 'number' or 'relativenumber' column
580 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000581 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200582 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
583 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000584 curwin->w_redr_type = NOT_VALID;
585#endif
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * Only start redrawing if there is really something to do.
589 */
590 if (type == INVERTED)
591 update_curswant();
592 if (curwin->w_redr_type < type
593 && !((type == VALID
594 && curwin->w_lines[0].wl_valid
595#ifdef FEAT_DIFF
596 && curwin->w_topfill == curwin->w_old_topfill
597 && curwin->w_botfill == curwin->w_old_botfill
598#endif
599 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000601 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
603 && curwin->w_old_visual_mode == VIsual_mode
604 && (curwin->w_valid & VALID_VIRTCOL)
605 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 ))
607 curwin->w_redr_type = type;
608
Bram Moolenaar5a305422006-04-28 22:38:25 +0000609#ifdef FEAT_WINDOWS
610 /* Redraw the tab pages line if needed. */
611 if (redraw_tabline || type >= NOT_VALID)
612 draw_tabline();
613#endif
614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#ifdef FEAT_SYN_HL
616 /*
617 * Correct stored syntax highlighting info for changes in each displayed
618 * buffer. Each buffer must only be done once.
619 */
620 FOR_ALL_WINDOWS(wp)
621 {
622 if (wp->w_buffer->b_mod_set)
623 {
624# ifdef FEAT_WINDOWS
625 win_T *wwp;
626
627 /* Check if we already did this buffer. */
628 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
629 if (wwp->w_buffer == wp->w_buffer)
630 break;
631# endif
632 if (
633# ifdef FEAT_WINDOWS
634 wwp == wp &&
635# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200636 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 syn_stack_apply_changes(wp->w_buffer);
638 }
639 }
640#endif
641
642 /*
643 * Go from top to bottom through the windows, redrawing the ones that need
644 * it.
645 */
646#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
647 did_one = FALSE;
648#endif
649#ifdef FEAT_SEARCH_EXTRA
650 search_hl.rm.regprog = NULL;
651#endif
652 FOR_ALL_WINDOWS(wp)
653 {
654 if (wp->w_redr_type != 0)
655 {
656 cursor_off();
657#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
658 if (!did_one)
659 {
660 did_one = TRUE;
661# ifdef FEAT_SEARCH_EXTRA
662 start_search_hl();
663# endif
664# ifdef FEAT_CLIPBOARD
665 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200666 if (clip_star.available && clip_isautosel_star())
667 clip_update_selection(&clip_star);
668 if (clip_plus.available && clip_isautosel_plus())
669 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670# endif
671#ifdef FEAT_GUI
672 /* Remove the cursor before starting to do anything, because
673 * scrolling may make it difficult to redraw the text under
674 * it. */
675 if (gui.in_use)
676 gui_undraw_cursor();
677#endif
678 }
679#endif
680 win_update(wp);
681 }
682
683#ifdef FEAT_WINDOWS
684 /* redraw status line after the window to minimize cursor movement */
685 if (wp->w_redr_status)
686 {
687 cursor_off();
688 win_redr_status(wp);
689 }
690#endif
691 }
692#if defined(FEAT_SEARCH_EXTRA)
693 end_search_hl();
694#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100695#ifdef FEAT_INS_EXPAND
696 /* May need to redraw the popup menu. */
697 if (pum_visible())
698 pum_redraw();
699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701#ifdef FEAT_WINDOWS
702 /* Reset b_mod_set flags. Going through all windows is probably faster
703 * than going through all buffers (there could be many buffers). */
704 for (wp = firstwin; wp != NULL; wp = wp->w_next)
705 wp->w_buffer->b_mod_set = FALSE;
706#else
707 curbuf->b_mod_set = FALSE;
708#endif
709
710 updating_screen = FALSE;
711#ifdef FEAT_GUI
712 gui_may_resize_shell();
713#endif
714
715 /* Clear or redraw the command line. Done last, because scrolling may
716 * mess up the command line. */
717 if (clear_cmdline || redraw_cmdline)
718 showmode();
719
720 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200721 if (!did_intro)
722 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 did_intro = TRUE;
724
725#ifdef FEAT_GUI
726 /* Redraw the cursor and update the scrollbars when all screen updating is
727 * done. */
728 if (gui.in_use)
729 {
730 out_flush(); /* required before updating the cursor */
731 if (did_one)
732 gui_update_cursor(FALSE, FALSE);
733 gui_update_scrollbars(FALSE);
734 }
735#endif
736}
737
Bram Moolenaar860cae12010-06-05 23:22:07 +0200738#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200739/*
740 * Return TRUE if the cursor line in window "wp" may be concealed, according
741 * to the 'concealcursor' option.
742 */
743 int
744conceal_cursor_line(wp)
745 win_T *wp;
746{
747 int c;
748
749 if (*wp->w_p_cocu == NUL)
750 return FALSE;
751 if (get_real_state() & VISUAL)
752 c = 'v';
753 else if (State & INSERT)
754 c = 'i';
755 else if (State & NORMAL)
756 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200757 else if (State & CMDLINE)
758 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200759 else
760 return FALSE;
761 return vim_strchr(wp->w_p_cocu, c) != NULL;
762}
763
764/*
765 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
766 */
767 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200768conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200769{
770 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
771 {
772 need_cursor_line_redraw = TRUE;
773 /* Need to recompute cursor column, e.g., when starting Visual mode
774 * without concealing. */
775 curs_columns(TRUE);
776 }
777}
778
Bram Moolenaar860cae12010-06-05 23:22:07 +0200779 void
780update_single_line(wp, lnum)
781 win_T *wp;
782 linenr_T lnum;
783{
784 int row;
785 int j;
786
787 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200788 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200789 {
790# ifdef FEAT_GUI
791 /* Remove the cursor before starting to do anything, because scrolling
792 * may make it difficult to redraw the text under it. */
793 if (gui.in_use)
794 gui_undraw_cursor();
795# endif
796 row = 0;
797 for (j = 0; j < wp->w_lines_valid; ++j)
798 {
799 if (lnum == wp->w_lines[j].wl_lnum)
800 {
801 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200802# ifdef FEAT_SEARCH_EXTRA
803 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200804 start_search_hl();
805 prepare_search_hl(wp, lnum);
806# endif
807 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
808# if defined(FEAT_SEARCH_EXTRA)
809 end_search_hl();
810# endif
811 break;
812 }
813 row += wp->w_lines[j].wl_size;
814 }
815# ifdef FEAT_GUI
816 /* Redraw the cursor */
817 if (gui.in_use)
818 {
819 out_flush(); /* required before updating the cursor */
820 gui_update_cursor(FALSE, FALSE);
821 }
822# endif
823 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200824 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200825}
826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
829static void update_prepare __ARGS((void));
830static void update_finish __ARGS((void));
831
832/*
833 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000834 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 */
836 static void
837update_prepare()
838{
839 cursor_off();
840 updating_screen = TRUE;
841#ifdef FEAT_GUI
842 /* Remove the cursor before starting to do anything, because scrolling may
843 * make it difficult to redraw the text under it. */
844 if (gui.in_use)
845 gui_undraw_cursor();
846#endif
847#ifdef FEAT_SEARCH_EXTRA
848 start_search_hl();
849#endif
850}
851
852/*
853 * Finish updating one or more windows.
854 */
855 static void
856update_finish()
857{
858 if (redraw_cmdline)
859 showmode();
860
861# ifdef FEAT_SEARCH_EXTRA
862 end_search_hl();
863# endif
864
865 updating_screen = FALSE;
866
867# ifdef FEAT_GUI
868 gui_may_resize_shell();
869
870 /* Redraw the cursor and update the scrollbars when all screen updating is
871 * done. */
872 if (gui.in_use)
873 {
874 out_flush(); /* required before updating the cursor */
875 gui_update_cursor(FALSE, FALSE);
876 gui_update_scrollbars(FALSE);
877 }
878# endif
879}
880#endif
881
882#if defined(FEAT_SIGNS) || defined(PROTO)
883 void
884update_debug_sign(buf, lnum)
885 buf_T *buf;
886 linenr_T lnum;
887{
888 win_T *wp;
889 int doit = FALSE;
890
891# ifdef FEAT_FOLDING
892 win_foldinfo.fi_level = 0;
893# endif
894
895 /* update/delete a specific mark */
896 FOR_ALL_WINDOWS(wp)
897 {
898 if (buf != NULL && lnum > 0)
899 {
900 if (wp->w_buffer == buf && lnum >= wp->w_topline
901 && lnum < wp->w_botline)
902 {
903 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
904 wp->w_redraw_top = lnum;
905 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
906 wp->w_redraw_bot = lnum;
907 redraw_win_later(wp, VALID);
908 }
909 }
910 else
911 redraw_win_later(wp, VALID);
912 if (wp->w_redr_type != 0)
913 doit = TRUE;
914 }
915
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100916 /* Return when there is nothing to do, screen updating is already
917 * happening (recursive call) or still starting up. */
918 if (!doit || updating_screen
919#ifdef FEAT_GUI
920 || gui.starting
921#endif
922 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 return;
924
925 /* update all windows that need updating */
926 update_prepare();
927
928# ifdef FEAT_WINDOWS
929 for (wp = firstwin; wp; wp = wp->w_next)
930 {
931 if (wp->w_redr_type != 0)
932 win_update(wp);
933 if (wp->w_redr_status)
934 win_redr_status(wp);
935 }
936# else
937 if (curwin->w_redr_type != 0)
938 win_update(curwin);
939# endif
940
941 update_finish();
942}
943#endif
944
945
946#if defined(FEAT_GUI) || defined(PROTO)
947/*
948 * Update a single window, its status line and maybe the command line msg.
949 * Used for the GUI scrollbar.
950 */
951 void
952updateWindow(wp)
953 win_T *wp;
954{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000955 /* return if already busy updating */
956 if (updating_screen)
957 return;
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 update_prepare();
960
961#ifdef FEAT_CLIPBOARD
962 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200963 if (clip_star.available && clip_isautosel_star())
964 clip_update_selection(&clip_star);
965 if (clip_plus.available && clip_isautosel_plus())
966 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000972 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000973 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000974 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (wp->w_redr_status
977# ifdef FEAT_CMDL_INFO
978 || p_ru
979# endif
980# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000981 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# endif
983 )
984 win_redr_status(wp);
985#endif
986
987 update_finish();
988}
989#endif
990
991/*
992 * Update a single window.
993 *
994 * This may cause the windows below it also to be redrawn (when clearing the
995 * screen or scrolling lines).
996 *
997 * How the window is redrawn depends on wp->w_redr_type. Each type also
998 * implies the one below it.
999 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001000 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1002 * INVERTED redraw the changed part of the Visual area
1003 * INVERTED_ALL redraw the whole Visual area
1004 * VALID 1. scroll up/down to adjust for a changed w_topline
1005 * 2. update lines at the top when scrolled down
1006 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001007 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 * b_mod_top and b_mod_bot.
1009 * - if wp->w_redraw_top non-zero, redraw lines between
1010 * wp->w_redraw_top and wp->w_redr_bot.
1011 * - continue redrawing when syntax status is invalid.
1012 * 4. if scrolled up, update lines at the bottom.
1013 * This results in three areas that may need updating:
1014 * top: from first row to top_end (when scrolled down)
1015 * mid: from mid_start to mid_end (update inversion or changed text)
1016 * bot: from bot_start to last row (when scrolled up)
1017 */
1018 static void
1019win_update(wp)
1020 win_T *wp;
1021{
1022 buf_T *buf = wp->w_buffer;
1023 int type;
1024 int top_end = 0; /* Below last row of the top area that needs
1025 updating. 0 when no top area updating. */
1026 int mid_start = 999;/* first row of the mid area that needs
1027 updating. 999 when no mid area updating. */
1028 int mid_end = 0; /* Below last row of the mid area that needs
1029 updating. 0 when no mid area updating. */
1030 int bot_start = 999;/* first row of the bot area that needs
1031 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 int scrolled_down = FALSE; /* TRUE when scrolled down when
1033 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001035 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 int top_to_mod = FALSE; /* redraw above mod_top */
1037#endif
1038
1039 int row; /* current window row to display */
1040 linenr_T lnum; /* current buffer lnum to display */
1041 int idx; /* current index in w_lines[] */
1042 int srow; /* starting row of the current line */
1043
1044 int eof = FALSE; /* if TRUE, we hit the end of the file */
1045 int didline = FALSE; /* if TRUE, we finished the last line */
1046 int i;
1047 long j;
1048 static int recursive = FALSE; /* being called recursively */
1049 int old_botline = wp->w_botline;
1050#ifdef FEAT_FOLDING
1051 long fold_count;
1052#endif
1053#ifdef FEAT_SYN_HL
1054 /* remember what happened to the previous line, to know if
1055 * check_visual_highlight() can be used */
1056#define DID_NONE 1 /* didn't update a line */
1057#define DID_LINE 2 /* updated a normal line */
1058#define DID_FOLD 3 /* updated a folded line */
1059 int did_update = DID_NONE;
1060 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1061#endif
1062 linenr_T mod_top = 0;
1063 linenr_T mod_bot = 0;
1064#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1065 int save_got_int;
1066#endif
1067
1068 type = wp->w_redr_type;
1069
1070 if (type == NOT_VALID)
1071 {
1072#ifdef FEAT_WINDOWS
1073 wp->w_redr_status = TRUE;
1074#endif
1075 wp->w_lines_valid = 0;
1076 }
1077
1078 /* Window is zero-height: nothing to draw. */
1079 if (wp->w_height == 0)
1080 {
1081 wp->w_redr_type = 0;
1082 return;
1083 }
1084
1085#ifdef FEAT_VERTSPLIT
1086 /* Window is zero-width: Only need to draw the separator. */
1087 if (wp->w_width == 0)
1088 {
1089 /* draw the vertical separator right of this window */
1090 draw_vsep_win(wp, 0);
1091 wp->w_redr_type = 0;
1092 return;
1093 }
1094#endif
1095
1096#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001097 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001100#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001101 /* Force redraw when width of 'number' or 'relativenumber' column
1102 * changes. */
1103 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001104 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001105 {
1106 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001107 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001108 }
1109 else
1110#endif
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1113 {
1114 /*
1115 * When there are both inserted/deleted lines and specific lines to be
1116 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1117 * everything (only happens when redrawing is off for while).
1118 */
1119 type = NOT_VALID;
1120 }
1121 else
1122 {
1123 /*
1124 * Set mod_top to the first line that needs displaying because of
1125 * changes. Set mod_bot to the first line after the changes.
1126 */
1127 mod_top = wp->w_redraw_top;
1128 if (wp->w_redraw_bot != 0)
1129 mod_bot = wp->w_redraw_bot + 1;
1130 else
1131 mod_bot = 0;
1132 wp->w_redraw_top = 0; /* reset for next time */
1133 wp->w_redraw_bot = 0;
1134 if (buf->b_mod_set)
1135 {
1136 if (mod_top == 0 || mod_top > buf->b_mod_top)
1137 {
1138 mod_top = buf->b_mod_top;
1139#ifdef FEAT_SYN_HL
1140 /* Need to redraw lines above the change that may be included
1141 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001142 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001144 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (mod_top < 1)
1146 mod_top = 1;
1147 }
1148#endif
1149 }
1150 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1151 mod_bot = buf->b_mod_bot;
1152
1153#ifdef FEAT_SEARCH_EXTRA
1154 /* When 'hlsearch' is on and using a multi-line search pattern, a
1155 * change in one line may make the Search highlighting in a
1156 * previous line invalid. Simple solution: redraw all visible
1157 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001158 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001160 if (search_hl.rm.regprog != NULL
1161 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001163 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001164 {
1165 cur = wp->w_match_head;
1166 while (cur != NULL)
1167 {
1168 if (cur->match.regprog != NULL
1169 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001170 {
1171 top_to_mod = TRUE;
1172 break;
1173 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001174 cur = cur->next;
1175 }
1176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#endif
1178 }
1179#ifdef FEAT_FOLDING
1180 if (mod_top != 0 && hasAnyFolding(wp))
1181 {
1182 linenr_T lnumt, lnumb;
1183
1184 /*
1185 * A change in a line can cause lines above it to become folded or
1186 * unfolded. Find the top most buffer line that may be affected.
1187 * If the line was previously folded and displayed, get the first
1188 * line of that fold. If the line is folded now, get the first
1189 * folded line. Use the minimum of these two.
1190 */
1191
1192 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1193 * the line below it. If there is no valid entry, use w_topline.
1194 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1195 * to this line. If there is no valid entry, use MAXLNUM. */
1196 lnumt = wp->w_topline;
1197 lnumb = MAXLNUM;
1198 for (i = 0; i < wp->w_lines_valid; ++i)
1199 if (wp->w_lines[i].wl_valid)
1200 {
1201 if (wp->w_lines[i].wl_lastlnum < mod_top)
1202 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1203 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1204 {
1205 lnumb = wp->w_lines[i].wl_lnum;
1206 /* When there is a fold column it might need updating
1207 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001208 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 ++lnumb;
1210 }
1211 }
1212
1213 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1214 if (mod_top > lnumt)
1215 mod_top = lnumt;
1216
1217 /* Now do the same for the bottom line (one above mod_bot). */
1218 --mod_bot;
1219 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1220 ++mod_bot;
1221 if (mod_bot < lnumb)
1222 mod_bot = lnumb;
1223 }
1224#endif
1225
1226 /* When a change starts above w_topline and the end is below
1227 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001228 * If the end of the change is above w_topline: do like no change was
1229 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (mod_top != 0 && mod_top < wp->w_topline)
1231 {
1232 if (mod_bot > wp->w_topline)
1233 mod_top = wp->w_topline;
1234#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001235 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 top_end = 1;
1237#endif
1238 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001239
1240 /* When line numbers are displayed need to redraw all lines below
1241 * inserted/deleted lines. */
1242 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1243 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245
1246 /*
1247 * When only displaying the lines at the top, set top_end. Used when
1248 * window has scrolled down for msg_scrolled.
1249 */
1250 if (type == REDRAW_TOP)
1251 {
1252 j = 0;
1253 for (i = 0; i < wp->w_lines_valid; ++i)
1254 {
1255 j += wp->w_lines[i].wl_size;
1256 if (j >= wp->w_upd_rows)
1257 {
1258 top_end = j;
1259 break;
1260 }
1261 }
1262 if (top_end == 0)
1263 /* not found (cannot happen?): redraw everything */
1264 type = NOT_VALID;
1265 else
1266 /* top area defined, the rest is VALID */
1267 type = VALID;
1268 }
1269
Bram Moolenaar367329b2007-08-30 11:53:22 +00001270 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001271 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1272 * non-zero and thus not FALSE) will indicate that screenclear() was not
1273 * called. */
1274 if (screen_cleared)
1275 screen_cleared = MAYBE;
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 /*
1278 * If there are no changes on the screen that require a complete redraw,
1279 * handle three cases:
1280 * 1: we are off the top of the screen by a few lines: scroll down
1281 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1282 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1283 * w_lines[] that needs updating.
1284 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001285 if ((type == VALID || type == SOME_VALID
1286 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287#ifdef FEAT_DIFF
1288 && !wp->w_botfill && !wp->w_old_botfill
1289#endif
1290 )
1291 {
1292 if (mod_top != 0 && wp->w_topline == mod_top)
1293 {
1294 /*
1295 * w_topline is the first changed line, the scrolling will be done
1296 * further down.
1297 */
1298 }
1299 else if (wp->w_lines[0].wl_valid
1300 && (wp->w_topline < wp->w_lines[0].wl_lnum
1301#ifdef FEAT_DIFF
1302 || (wp->w_topline == wp->w_lines[0].wl_lnum
1303 && wp->w_topfill > wp->w_old_topfill)
1304#endif
1305 ))
1306 {
1307 /*
1308 * New topline is above old topline: May scroll down.
1309 */
1310#ifdef FEAT_FOLDING
1311 if (hasAnyFolding(wp))
1312 {
1313 linenr_T ln;
1314
1315 /* count the number of lines we are off, counting a sequence
1316 * of folded lines as one */
1317 j = 0;
1318 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1319 {
1320 ++j;
1321 if (j >= wp->w_height - 2)
1322 break;
1323 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1324 }
1325 }
1326 else
1327#endif
1328 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1329 if (j < wp->w_height - 2) /* not too far off */
1330 {
1331 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1332#ifdef FEAT_DIFF
1333 /* insert extra lines for previously invisible filler lines */
1334 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1335 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1336 - wp->w_old_topfill;
1337#endif
1338 if (i < wp->w_height - 2) /* less than a screen off */
1339 {
1340 /*
1341 * Try to insert the correct number of lines.
1342 * If not the last window, delete the lines at the bottom.
1343 * win_ins_lines may fail when the terminal can't do it.
1344 */
1345 if (i > 0)
1346 check_for_delay(FALSE);
1347 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1348 {
1349 if (wp->w_lines_valid != 0)
1350 {
1351 /* Need to update rows that are new, stop at the
1352 * first one that scrolled down. */
1353 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356 /* Move the entries that were scrolled, disable
1357 * the entries for the lines to be redrawn. */
1358 if ((wp->w_lines_valid += j) > wp->w_height)
1359 wp->w_lines_valid = wp->w_height;
1360 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1361 wp->w_lines[idx] = wp->w_lines[idx - j];
1362 while (idx >= 0)
1363 wp->w_lines[idx--].wl_valid = FALSE;
1364 }
1365 }
1366 else
1367 mid_start = 0; /* redraw all lines */
1368 }
1369 else
1370 mid_start = 0; /* redraw all lines */
1371 }
1372 else
1373 mid_start = 0; /* redraw all lines */
1374 }
1375 else
1376 {
1377 /*
1378 * New topline is at or below old topline: May scroll up.
1379 * When topline didn't change, find first entry in w_lines[] that
1380 * needs updating.
1381 */
1382
1383 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1384 j = -1;
1385 row = 0;
1386 for (i = 0; i < wp->w_lines_valid; i++)
1387 {
1388 if (wp->w_lines[i].wl_valid
1389 && wp->w_lines[i].wl_lnum == wp->w_topline)
1390 {
1391 j = i;
1392 break;
1393 }
1394 row += wp->w_lines[i].wl_size;
1395 }
1396 if (j == -1)
1397 {
1398 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1399 * lines */
1400 mid_start = 0;
1401 }
1402 else
1403 {
1404 /*
1405 * Try to delete the correct number of lines.
1406 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1407 */
1408#ifdef FEAT_DIFF
1409 /* If the topline didn't change, delete old filler lines,
1410 * otherwise delete filler lines of the new topline... */
1411 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1412 row += wp->w_old_topfill;
1413 else
1414 row += diff_check_fill(wp, wp->w_topline);
1415 /* ... but don't delete new filler lines. */
1416 row -= wp->w_topfill;
1417#endif
1418 if (row > 0)
1419 {
1420 check_for_delay(FALSE);
1421 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1422 bot_start = wp->w_height - row;
1423 else
1424 mid_start = 0; /* redraw all lines */
1425 }
1426 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1427 {
1428 /*
1429 * Skip the lines (below the deleted lines) that are still
1430 * valid and don't need redrawing. Copy their info
1431 * upwards, to compensate for the deleted lines. Set
1432 * bot_start to the first row that needs redrawing.
1433 */
1434 bot_start = 0;
1435 idx = 0;
1436 for (;;)
1437 {
1438 wp->w_lines[idx] = wp->w_lines[j];
1439 /* stop at line that didn't fit, unless it is still
1440 * valid (no lines deleted) */
1441 if (row > 0 && bot_start + row
1442 + (int)wp->w_lines[j].wl_size > wp->w_height)
1443 {
1444 wp->w_lines_valid = idx + 1;
1445 break;
1446 }
1447 bot_start += wp->w_lines[idx++].wl_size;
1448
1449 /* stop at the last valid entry in w_lines[].wl_size */
1450 if (++j >= wp->w_lines_valid)
1451 {
1452 wp->w_lines_valid = idx;
1453 break;
1454 }
1455 }
1456#ifdef FEAT_DIFF
1457 /* Correct the first entry for filler lines at the top
1458 * when it won't get updated below. */
1459 if (wp->w_p_diff && bot_start > 0)
1460 wp->w_lines[0].wl_size =
1461 plines_win_nofill(wp, wp->w_topline, TRUE)
1462 + wp->w_topfill;
1463#endif
1464 }
1465 }
1466 }
1467
1468 /* When starting redraw in the first line, redraw all lines. When
1469 * there is only one window it's probably faster to clear the screen
1470 * first. */
1471 if (mid_start == 0)
1472 {
1473 mid_end = wp->w_height;
1474 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001475 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001476 /* Clear the screen when it was not done by win_del_lines() or
1477 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1478 * then. */
1479 if (screen_cleared != TRUE)
1480 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001481#ifdef FEAT_WINDOWS
1482 /* The screen was cleared, redraw the tab pages line. */
1483 if (redraw_tabline)
1484 draw_tabline();
1485#endif
1486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001488
1489 /* When win_del_lines() or win_ins_lines() caused the screen to be
1490 * cleared (only happens for the first window) or when screenclear()
1491 * was called directly above, "must_redraw" will have been set to
1492 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1493 if (screen_cleared == TRUE)
1494 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 else
1497 {
1498 /* Not VALID or INVERTED: redraw all lines. */
1499 mid_start = 0;
1500 mid_end = wp->w_height;
1501 }
1502
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001503 if (type == SOME_VALID)
1504 {
1505 /* SOME_VALID: redraw all lines. */
1506 mid_start = 0;
1507 mid_end = wp->w_height;
1508 type = NOT_VALID;
1509 }
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 /* check if we are updating or removing the inverted part */
1512 if ((VIsual_active && buf == curwin->w_buffer)
1513 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1514 {
1515 linenr_T from, to;
1516
1517 if (VIsual_active)
1518 {
1519 if (VIsual_active
1520 && (VIsual_mode != wp->w_old_visual_mode
1521 || type == INVERTED_ALL))
1522 {
1523 /*
1524 * If the type of Visual selection changed, redraw the whole
1525 * selection. Also when the ownership of the X selection is
1526 * gained or lost.
1527 */
1528 if (curwin->w_cursor.lnum < VIsual.lnum)
1529 {
1530 from = curwin->w_cursor.lnum;
1531 to = VIsual.lnum;
1532 }
1533 else
1534 {
1535 from = VIsual.lnum;
1536 to = curwin->w_cursor.lnum;
1537 }
1538 /* redraw more when the cursor moved as well */
1539 if (wp->w_old_cursor_lnum < from)
1540 from = wp->w_old_cursor_lnum;
1541 if (wp->w_old_cursor_lnum > to)
1542 to = wp->w_old_cursor_lnum;
1543 if (wp->w_old_visual_lnum < from)
1544 from = wp->w_old_visual_lnum;
1545 if (wp->w_old_visual_lnum > to)
1546 to = wp->w_old_visual_lnum;
1547 }
1548 else
1549 {
1550 /*
1551 * Find the line numbers that need to be updated: The lines
1552 * between the old cursor position and the current cursor
1553 * position. Also check if the Visual position changed.
1554 */
1555 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1556 {
1557 from = curwin->w_cursor.lnum;
1558 to = wp->w_old_cursor_lnum;
1559 }
1560 else
1561 {
1562 from = wp->w_old_cursor_lnum;
1563 to = curwin->w_cursor.lnum;
1564 if (from == 0) /* Visual mode just started */
1565 from = to;
1566 }
1567
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001568 if (VIsual.lnum != wp->w_old_visual_lnum
1569 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571 if (wp->w_old_visual_lnum < from
1572 && wp->w_old_visual_lnum != 0)
1573 from = wp->w_old_visual_lnum;
1574 if (wp->w_old_visual_lnum > to)
1575 to = wp->w_old_visual_lnum;
1576 if (VIsual.lnum < from)
1577 from = VIsual.lnum;
1578 if (VIsual.lnum > to)
1579 to = VIsual.lnum;
1580 }
1581 }
1582
1583 /*
1584 * If in block mode and changed column or curwin->w_curswant:
1585 * update all lines.
1586 * First compute the actual start and end column.
1587 */
1588 if (VIsual_mode == Ctrl_V)
1589 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001590 colnr_T fromc, toc;
1591#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1592 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaar404406a2014-10-09 13:24:43 +02001594 if (curwin->w_p_lbr)
1595 ve_flags = VE_ALL;
1596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001598#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1599 ve_flags = save_ve_flags;
1600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 ++toc;
1602 if (curwin->w_curswant == MAXCOL)
1603 toc = MAXCOL;
1604
1605 if (fromc != wp->w_old_cursor_fcol
1606 || toc != wp->w_old_cursor_lcol)
1607 {
1608 if (from > VIsual.lnum)
1609 from = VIsual.lnum;
1610 if (to < VIsual.lnum)
1611 to = VIsual.lnum;
1612 }
1613 wp->w_old_cursor_fcol = fromc;
1614 wp->w_old_cursor_lcol = toc;
1615 }
1616 }
1617 else
1618 {
1619 /* Use the line numbers of the old Visual area. */
1620 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1621 {
1622 from = wp->w_old_cursor_lnum;
1623 to = wp->w_old_visual_lnum;
1624 }
1625 else
1626 {
1627 from = wp->w_old_visual_lnum;
1628 to = wp->w_old_cursor_lnum;
1629 }
1630 }
1631
1632 /*
1633 * There is no need to update lines above the top of the window.
1634 */
1635 if (from < wp->w_topline)
1636 from = wp->w_topline;
1637
1638 /*
1639 * If we know the value of w_botline, use it to restrict the update to
1640 * the lines that are visible in the window.
1641 */
1642 if (wp->w_valid & VALID_BOTLINE)
1643 {
1644 if (from >= wp->w_botline)
1645 from = wp->w_botline - 1;
1646 if (to >= wp->w_botline)
1647 to = wp->w_botline - 1;
1648 }
1649
1650 /*
1651 * Find the minimal part to be updated.
1652 * Watch out for scrolling that made entries in w_lines[] invalid.
1653 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1654 * top_end; need to redraw from top_end to the "to" line.
1655 * A middle mouse click with a Visual selection may change the text
1656 * above the Visual area and reset wl_valid, do count these for
1657 * mid_end (in srow).
1658 */
1659 if (mid_start > 0)
1660 {
1661 lnum = wp->w_topline;
1662 idx = 0;
1663 srow = 0;
1664 if (scrolled_down)
1665 mid_start = top_end;
1666 else
1667 mid_start = 0;
1668 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1669 {
1670 if (wp->w_lines[idx].wl_valid)
1671 mid_start += wp->w_lines[idx].wl_size;
1672 else if (!scrolled_down)
1673 srow += wp->w_lines[idx].wl_size;
1674 ++idx;
1675# ifdef FEAT_FOLDING
1676 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1677 lnum = wp->w_lines[idx].wl_lnum;
1678 else
1679# endif
1680 ++lnum;
1681 }
1682 srow += mid_start;
1683 mid_end = wp->w_height;
1684 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1685 {
1686 if (wp->w_lines[idx].wl_valid
1687 && wp->w_lines[idx].wl_lnum >= to + 1)
1688 {
1689 /* Only update until first row of this line */
1690 mid_end = srow;
1691 break;
1692 }
1693 srow += wp->w_lines[idx].wl_size;
1694 }
1695 }
1696 }
1697
1698 if (VIsual_active && buf == curwin->w_buffer)
1699 {
1700 wp->w_old_visual_mode = VIsual_mode;
1701 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1702 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001703 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 wp->w_old_curswant = curwin->w_curswant;
1705 }
1706 else
1707 {
1708 wp->w_old_visual_mode = 0;
1709 wp->w_old_cursor_lnum = 0;
1710 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001711 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1715 /* reset got_int, otherwise regexp won't work */
1716 save_got_int = got_int;
1717 got_int = 0;
1718#endif
1719#ifdef FEAT_FOLDING
1720 win_foldinfo.fi_level = 0;
1721#endif
1722
1723 /*
1724 * Update all the window rows.
1725 */
1726 idx = 0; /* first entry in w_lines[].wl_size */
1727 row = 0;
1728 srow = 0;
1729 lnum = wp->w_topline; /* first line shown in window */
1730 for (;;)
1731 {
1732 /* stop updating when reached the end of the window (check for _past_
1733 * the end of the window is at the end of the loop) */
1734 if (row == wp->w_height)
1735 {
1736 didline = TRUE;
1737 break;
1738 }
1739
1740 /* stop updating when hit the end of the file */
1741 if (lnum > buf->b_ml.ml_line_count)
1742 {
1743 eof = TRUE;
1744 break;
1745 }
1746
1747 /* Remember the starting row of the line that is going to be dealt
1748 * with. It is used further down when the line doesn't fit. */
1749 srow = row;
1750
1751 /*
1752 * Update a line when it is in an area that needs updating, when it
1753 * has changes or w_lines[idx] is invalid.
1754 * bot_start may be halfway a wrapped line after using
1755 * win_del_lines(), check if the current line includes it.
1756 * When syntax folding is being used, the saved syntax states will
1757 * already have been updated, we can't see where the syntax state is
1758 * the same again, just update until the end of the window.
1759 */
1760 if (row < top_end
1761 || (row >= mid_start && row < mid_end)
1762#ifdef FEAT_SEARCH_EXTRA
1763 || top_to_mod
1764#endif
1765 || idx >= wp->w_lines_valid
1766 || (row + wp->w_lines[idx].wl_size > bot_start)
1767 || (mod_top != 0
1768 && (lnum == mod_top
1769 || (lnum >= mod_top
1770 && (lnum < mod_bot
1771#ifdef FEAT_SYN_HL
1772 || did_update == DID_FOLD
1773 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001774 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 && (
1776# ifdef FEAT_FOLDING
1777 (foldmethodIsSyntax(wp)
1778 && hasAnyFolding(wp)) ||
1779# endif
1780 syntax_check_changed(lnum)))
1781#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001782#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001783 /* match in fixed position might need redraw
1784 * if lines were inserted or deleted */
1785 || (wp->w_match_head != NULL
1786 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 )))))
1789 {
1790#ifdef FEAT_SEARCH_EXTRA
1791 if (lnum == mod_top)
1792 top_to_mod = FALSE;
1793#endif
1794
1795 /*
1796 * When at start of changed lines: May scroll following lines
1797 * up or down to minimize redrawing.
1798 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001799 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
1801 if (lnum == mod_top
1802 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001803 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
1805 int old_rows = 0;
1806 int new_rows = 0;
1807 int xtra_rows;
1808 linenr_T l;
1809
1810 /* Count the old number of window rows, using w_lines[], which
1811 * should still contain the sizes for the lines as they are
1812 * currently displayed. */
1813 for (i = idx; i < wp->w_lines_valid; ++i)
1814 {
1815 /* Only valid lines have a meaningful wl_lnum. Invalid
1816 * lines are part of the changed area. */
1817 if (wp->w_lines[i].wl_valid
1818 && wp->w_lines[i].wl_lnum == mod_bot)
1819 break;
1820 old_rows += wp->w_lines[i].wl_size;
1821#ifdef FEAT_FOLDING
1822 if (wp->w_lines[i].wl_valid
1823 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1824 {
1825 /* Must have found the last valid entry above mod_bot.
1826 * Add following invalid entries. */
1827 ++i;
1828 while (i < wp->w_lines_valid
1829 && !wp->w_lines[i].wl_valid)
1830 old_rows += wp->w_lines[i++].wl_size;
1831 break;
1832 }
1833#endif
1834 }
1835
1836 if (i >= wp->w_lines_valid)
1837 {
1838 /* We can't find a valid line below the changed lines,
1839 * need to redraw until the end of the window.
1840 * Inserting/deleting lines has no use. */
1841 bot_start = 0;
1842 }
1843 else
1844 {
1845 /* Able to count old number of rows: Count new window
1846 * rows, and may insert/delete lines */
1847 j = idx;
1848 for (l = lnum; l < mod_bot; ++l)
1849 {
1850#ifdef FEAT_FOLDING
1851 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1852 ++new_rows;
1853 else
1854#endif
1855#ifdef FEAT_DIFF
1856 if (l == wp->w_topline)
1857 new_rows += plines_win_nofill(wp, l, TRUE)
1858 + wp->w_topfill;
1859 else
1860#endif
1861 new_rows += plines_win(wp, l, TRUE);
1862 ++j;
1863 if (new_rows > wp->w_height - row - 2)
1864 {
1865 /* it's getting too much, must redraw the rest */
1866 new_rows = 9999;
1867 break;
1868 }
1869 }
1870 xtra_rows = new_rows - old_rows;
1871 if (xtra_rows < 0)
1872 {
1873 /* May scroll text up. If there is not enough
1874 * remaining text or scrolling fails, must redraw the
1875 * rest. If scrolling works, must redraw the text
1876 * below the scrolled text. */
1877 if (row - xtra_rows >= wp->w_height - 2)
1878 mod_bot = MAXLNUM;
1879 else
1880 {
1881 check_for_delay(FALSE);
1882 if (win_del_lines(wp, row,
1883 -xtra_rows, FALSE, FALSE) == FAIL)
1884 mod_bot = MAXLNUM;
1885 else
1886 bot_start = wp->w_height + xtra_rows;
1887 }
1888 }
1889 else if (xtra_rows > 0)
1890 {
1891 /* May scroll text down. If there is not enough
1892 * remaining text of scrolling fails, must redraw the
1893 * rest. */
1894 if (row + xtra_rows >= wp->w_height - 2)
1895 mod_bot = MAXLNUM;
1896 else
1897 {
1898 check_for_delay(FALSE);
1899 if (win_ins_lines(wp, row + old_rows,
1900 xtra_rows, FALSE, FALSE) == FAIL)
1901 mod_bot = MAXLNUM;
1902 else if (top_end > row + old_rows)
1903 /* Scrolled the part at the top that requires
1904 * updating down. */
1905 top_end += xtra_rows;
1906 }
1907 }
1908
1909 /* When not updating the rest, may need to move w_lines[]
1910 * entries. */
1911 if (mod_bot != MAXLNUM && i != j)
1912 {
1913 if (j < i)
1914 {
1915 int x = row + new_rows;
1916
1917 /* move entries in w_lines[] upwards */
1918 for (;;)
1919 {
1920 /* stop at last valid entry in w_lines[] */
1921 if (i >= wp->w_lines_valid)
1922 {
1923 wp->w_lines_valid = j;
1924 break;
1925 }
1926 wp->w_lines[j] = wp->w_lines[i];
1927 /* stop at a line that won't fit */
1928 if (x + (int)wp->w_lines[j].wl_size
1929 > wp->w_height)
1930 {
1931 wp->w_lines_valid = j + 1;
1932 break;
1933 }
1934 x += wp->w_lines[j++].wl_size;
1935 ++i;
1936 }
1937 if (bot_start > x)
1938 bot_start = x;
1939 }
1940 else /* j > i */
1941 {
1942 /* move entries in w_lines[] downwards */
1943 j -= i;
1944 wp->w_lines_valid += j;
1945 if (wp->w_lines_valid > wp->w_height)
1946 wp->w_lines_valid = wp->w_height;
1947 for (i = wp->w_lines_valid; i - j >= idx; --i)
1948 wp->w_lines[i] = wp->w_lines[i - j];
1949
1950 /* The w_lines[] entries for inserted lines are
1951 * now invalid, but wl_size may be used above.
1952 * Reset to zero. */
1953 while (i >= idx)
1954 {
1955 wp->w_lines[i].wl_size = 0;
1956 wp->w_lines[i--].wl_valid = FALSE;
1957 }
1958 }
1959 }
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962
1963#ifdef FEAT_FOLDING
1964 /*
1965 * When lines are folded, display one line for all of them.
1966 * Otherwise, display normally (can be several display lines when
1967 * 'wrap' is on).
1968 */
1969 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1970 if (fold_count != 0)
1971 {
1972 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1973 ++row;
1974 --fold_count;
1975 wp->w_lines[idx].wl_folded = TRUE;
1976 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1977# ifdef FEAT_SYN_HL
1978 did_update = DID_FOLD;
1979# endif
1980 }
1981 else
1982#endif
1983 if (idx < wp->w_lines_valid
1984 && wp->w_lines[idx].wl_valid
1985 && wp->w_lines[idx].wl_lnum == lnum
1986 && lnum > wp->w_topline
1987 && !(dy_flags & DY_LASTLINE)
1988 && srow + wp->w_lines[idx].wl_size > wp->w_height
1989#ifdef FEAT_DIFF
1990 && diff_check_fill(wp, lnum) == 0
1991#endif
1992 )
1993 {
1994 /* This line is not going to fit. Don't draw anything here,
1995 * will draw "@ " lines below. */
1996 row = wp->w_height + 1;
1997 }
1998 else
1999 {
2000#ifdef FEAT_SEARCH_EXTRA
2001 prepare_search_hl(wp, lnum);
2002#endif
2003#ifdef FEAT_SYN_HL
2004 /* Let the syntax stuff know we skipped a few lines. */
2005 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002006 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 syntax_end_parsing(syntax_last_parsed + 1);
2008#endif
2009
2010 /*
2011 * Display one line.
2012 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002013 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
2015#ifdef FEAT_FOLDING
2016 wp->w_lines[idx].wl_folded = FALSE;
2017 wp->w_lines[idx].wl_lastlnum = lnum;
2018#endif
2019#ifdef FEAT_SYN_HL
2020 did_update = DID_LINE;
2021 syntax_last_parsed = lnum;
2022#endif
2023 }
2024
2025 wp->w_lines[idx].wl_lnum = lnum;
2026 wp->w_lines[idx].wl_valid = TRUE;
2027 if (row > wp->w_height) /* past end of screen */
2028 {
2029 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002030 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2032 ++idx;
2033 break;
2034 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002035 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 wp->w_lines[idx].wl_size = row - srow;
2037 ++idx;
2038#ifdef FEAT_FOLDING
2039 lnum += fold_count + 1;
2040#else
2041 ++lnum;
2042#endif
2043 }
2044 else
2045 {
2046 /* This line does not need updating, advance to the next one */
2047 row += wp->w_lines[idx++].wl_size;
2048 if (row > wp->w_height) /* past end of screen */
2049 break;
2050#ifdef FEAT_FOLDING
2051 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2052#else
2053 ++lnum;
2054#endif
2055#ifdef FEAT_SYN_HL
2056 did_update = DID_NONE;
2057#endif
2058 }
2059
2060 if (lnum > buf->b_ml.ml_line_count)
2061 {
2062 eof = TRUE;
2063 break;
2064 }
2065 }
2066 /*
2067 * End of loop over all window lines.
2068 */
2069
2070
2071 if (idx > wp->w_lines_valid)
2072 wp->w_lines_valid = idx;
2073
2074#ifdef FEAT_SYN_HL
2075 /*
2076 * Let the syntax stuff know we stop parsing here.
2077 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002078 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 syntax_end_parsing(syntax_last_parsed + 1);
2080#endif
2081
2082 /*
2083 * If we didn't hit the end of the file, and we didn't finish the last
2084 * line we were working on, then the line didn't fit.
2085 */
2086 wp->w_empty_rows = 0;
2087#ifdef FEAT_DIFF
2088 wp->w_filler_rows = 0;
2089#endif
2090 if (!eof && !didline)
2091 {
2092 if (lnum == wp->w_topline)
2093 {
2094 /*
2095 * Single line that does not fit!
2096 * Don't overwrite it, it can be edited.
2097 */
2098 wp->w_botline = lnum + 1;
2099 }
2100#ifdef FEAT_DIFF
2101 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2102 {
2103 /* Window ends in filler lines. */
2104 wp->w_botline = lnum;
2105 wp->w_filler_rows = wp->w_height - srow;
2106 }
2107#endif
2108 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2109 {
2110 /*
2111 * Last line isn't finished: Display "@@@" at the end.
2112 */
2113 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2114 W_WINROW(wp) + wp->w_height,
2115 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2116 '@', '@', hl_attr(HLF_AT));
2117 set_empty_rows(wp, srow);
2118 wp->w_botline = lnum;
2119 }
2120 else
2121 {
2122 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2123 wp->w_botline = lnum;
2124 }
2125 }
2126 else
2127 {
2128#ifdef FEAT_VERTSPLIT
2129 draw_vsep_win(wp, row);
2130#endif
2131 if (eof) /* we hit the end of the file */
2132 {
2133 wp->w_botline = buf->b_ml.ml_line_count + 1;
2134#ifdef FEAT_DIFF
2135 j = diff_check_fill(wp, wp->w_botline);
2136 if (j > 0 && !wp->w_botfill)
2137 {
2138 /*
2139 * Display filler lines at the end of the file
2140 */
2141 if (char2cells(fill_diff) > 1)
2142 i = '-';
2143 else
2144 i = fill_diff;
2145 if (row + j > wp->w_height)
2146 j = wp->w_height - row;
2147 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2148 row += j;
2149 }
2150#endif
2151 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 wp->w_botline = lnum;
2154
2155 /* make sure the rest of the screen is blank */
2156 /* put '~'s on rows that aren't part of the file. */
2157 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2158 }
2159
2160 /* Reset the type of redrawing required, the window has been updated. */
2161 wp->w_redr_type = 0;
2162#ifdef FEAT_DIFF
2163 wp->w_old_topfill = wp->w_topfill;
2164 wp->w_old_botfill = wp->w_botfill;
2165#endif
2166
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002167 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 /*
2170 * There is a trick with w_botline. If we invalidate it on each
2171 * change that might modify it, this will cause a lot of expensive
2172 * calls to plines() in update_topline() each time. Therefore the
2173 * value of w_botline is often approximated, and this value is used to
2174 * compute the value of w_topline. If the value of w_botline was
2175 * wrong, check that the value of w_topline is correct (cursor is on
2176 * the visible part of the text). If it's not, we need to redraw
2177 * again. Mostly this just means scrolling up a few lines, so it
2178 * doesn't look too bad. Only do this for the current window (where
2179 * changes are relevant).
2180 */
2181 wp->w_valid |= VALID_BOTLINE;
2182 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2183 {
2184 recursive = TRUE;
2185 curwin->w_valid &= ~VALID_TOPLINE;
2186 update_topline(); /* may invalidate w_botline again */
2187 if (must_redraw != 0)
2188 {
2189 /* Don't update for changes in buffer again. */
2190 i = curbuf->b_mod_set;
2191 curbuf->b_mod_set = FALSE;
2192 win_update(curwin);
2193 must_redraw = 0;
2194 curbuf->b_mod_set = i;
2195 }
2196 recursive = FALSE;
2197 }
2198 }
2199
2200#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2201 /* restore got_int, unless CTRL-C was hit while redrawing */
2202 if (!got_int)
2203 got_int = save_got_int;
2204#endif
2205}
2206
2207#ifdef FEAT_SIGNS
2208static int draw_signcolumn __ARGS((win_T *wp));
2209
2210/*
2211 * Return TRUE when window "wp" has a column to draw signs in.
2212 */
2213 static int
2214draw_signcolumn(wp)
2215 win_T *wp;
2216{
2217 return (wp->w_buffer->b_signlist != NULL
2218# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01002219 || wp->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220# endif
2221 );
2222}
2223#endif
2224
2225/*
2226 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2227 * as the filler character.
2228 */
2229 static void
2230win_draw_end(wp, c1, c2, row, endrow, hl)
2231 win_T *wp;
2232 int c1;
2233 int c2;
2234 int row;
2235 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002236 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
2238#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2239 int n = 0;
2240# define FDC_OFF n
2241#else
2242# define FDC_OFF 0
2243#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002244#ifdef FEAT_FOLDING
2245 int fdc = compute_foldcolumn(wp, 0);
2246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248#ifdef FEAT_RIGHTLEFT
2249 if (wp->w_p_rl)
2250 {
2251 /* No check for cmdline window: should never be right-left. */
2252# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002253 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 if (n > 0)
2256 {
2257 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002258 if (n > W_WIDTH(wp))
2259 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2261 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2262 ' ', ' ', hl_attr(HLF_FC));
2263 }
2264# endif
2265# ifdef FEAT_SIGNS
2266 if (draw_signcolumn(wp))
2267 {
2268 int nn = n + 2;
2269
2270 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002271 if (nn > W_WIDTH(wp))
2272 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2274 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2275 ' ', ' ', hl_attr(HLF_SC));
2276 n = nn;
2277 }
2278# endif
2279 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2280 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2281 c2, c2, hl_attr(hl));
2282 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2283 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2284 c1, c2, hl_attr(hl));
2285 }
2286 else
2287#endif
2288 {
2289#ifdef FEAT_CMDWIN
2290 if (cmdwin_type != 0 && wp == curwin)
2291 {
2292 /* draw the cmdline character in the leftmost column */
2293 n = 1;
2294 if (n > wp->w_width)
2295 n = wp->w_width;
2296 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2297 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2298 cmdwin_type, ' ', hl_attr(HLF_AT));
2299 }
2300#endif
2301#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002302 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002304 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 /* draw the fold column at the left */
2307 if (nn > W_WIDTH(wp))
2308 nn = W_WIDTH(wp);
2309 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2310 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2311 ' ', ' ', hl_attr(HLF_FC));
2312 n = nn;
2313 }
2314#endif
2315#ifdef FEAT_SIGNS
2316 if (draw_signcolumn(wp))
2317 {
2318 int nn = n + 2;
2319
2320 /* draw the sign column after the fold column */
2321 if (nn > W_WIDTH(wp))
2322 nn = W_WIDTH(wp);
2323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2324 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2325 ' ', ' ', hl_attr(HLF_SC));
2326 n = nn;
2327 }
2328#endif
2329 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2330 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2331 c1, c2, hl_attr(hl));
2332 }
2333 set_empty_rows(wp, row);
2334}
2335
Bram Moolenaar1a384422010-07-14 19:53:30 +02002336#ifdef FEAT_SYN_HL
2337static int advance_color_col __ARGS((int vcol, int **color_cols));
2338
2339/*
2340 * Advance **color_cols and return TRUE when there are columns to draw.
2341 */
2342 static int
2343advance_color_col(vcol, color_cols)
2344 int vcol;
2345 int **color_cols;
2346{
2347 while (**color_cols >= 0 && vcol > **color_cols)
2348 ++*color_cols;
2349 return (**color_cols >= 0);
2350}
2351#endif
2352
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_FOLDING
2354/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002355 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2356 * space is available for window "wp", minus "col".
2357 */
2358 static int
2359compute_foldcolumn(wp, col)
2360 win_T *wp;
2361 int col;
2362{
2363 int fdc = wp->w_p_fdc;
2364 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
2365 int wwidth = W_WIDTH(wp);
2366
2367 if (fdc > wwidth - (col + wmw))
2368 fdc = wwidth - (col + wmw);
2369 return fdc;
2370}
2371
2372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * Display one folded line.
2374 */
2375 static void
2376fold_line(wp, fold_count, foldinfo, lnum, row)
2377 win_T *wp;
2378 long fold_count;
2379 foldinfo_T *foldinfo;
2380 linenr_T lnum;
2381 int row;
2382{
2383 char_u buf[51];
2384 pos_T *top, *bot;
2385 linenr_T lnume = lnum + fold_count - 1;
2386 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002387 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 int col;
2390 int txtcol;
2391 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002392 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 /* Build the fold line:
2395 * 1. Add the cmdwin_type for the command-line window
2396 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002397 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 * 4. Compose the text
2399 * 5. Add the text
2400 * 6. set highlighting for the Visual area an other text
2401 */
2402 col = 0;
2403
2404 /*
2405 * 1. Add the cmdwin_type for the command-line window
2406 * Ignores 'rightleft', this window is never right-left.
2407 */
2408#ifdef FEAT_CMDWIN
2409 if (cmdwin_type != 0 && wp == curwin)
2410 {
2411 ScreenLines[off] = cmdwin_type;
2412 ScreenAttrs[off] = hl_attr(HLF_AT);
2413#ifdef FEAT_MBYTE
2414 if (enc_utf8)
2415 ScreenLinesUC[off] = 0;
2416#endif
2417 ++col;
2418 }
2419#endif
2420
2421 /*
2422 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002423 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002425 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (fdc > 0)
2427 {
2428 fill_foldcolumn(buf, wp, TRUE, lnum);
2429#ifdef FEAT_RIGHTLEFT
2430 if (wp->w_p_rl)
2431 {
2432 int i;
2433
2434 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2435 hl_attr(HLF_FC));
2436 /* reverse the fold column */
2437 for (i = 0; i < fdc; ++i)
2438 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2439 }
2440 else
2441#endif
2442 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2443 col += fdc;
2444 }
2445
2446#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002447# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2448 for (ri = 0; ri < l; ++ri) \
2449 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2450 else \
2451 for (ri = 0; ri < l; ++ri) \
2452 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002454# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2455 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456#endif
2457
Bram Moolenaar64486672010-05-16 15:46:46 +02002458 /* Set all attributes of the 'number' or 'relativenumber' column and the
2459 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002460 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462#ifdef FEAT_SIGNS
2463 /* If signs are being displayed, add two spaces. */
2464 if (draw_signcolumn(wp))
2465 {
2466 len = W_WIDTH(wp) - col;
2467 if (len > 0)
2468 {
2469 if (len > 2)
2470 len = 2;
2471# ifdef FEAT_RIGHTLEFT
2472 if (wp->w_p_rl)
2473 /* the line number isn't reversed */
2474 copy_text_attr(off + W_WIDTH(wp) - len - col,
2475 (char_u *)" ", len, hl_attr(HLF_FL));
2476 else
2477# endif
2478 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2479 col += len;
2480 }
2481 }
2482#endif
2483
2484 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002485 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002487 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 len = W_WIDTH(wp) - col;
2490 if (len > 0)
2491 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002492 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002493 long num;
2494 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002495
2496 if (len > w + 1)
2497 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002498
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002499 if (wp->w_p_nu && !wp->w_p_rnu)
2500 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002501 num = (long)lnum;
2502 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002503 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002504 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002505 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002506 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002507 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002508 /* 'number' + 'relativenumber': cursor line shows absolute
2509 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002510 num = lnum;
2511 fmt = "%-*ld ";
2512 }
2513 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002514
Bram Moolenaar700e7342013-01-30 12:31:36 +01002515 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516#ifdef FEAT_RIGHTLEFT
2517 if (wp->w_p_rl)
2518 /* the line number isn't reversed */
2519 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2520 hl_attr(HLF_FL));
2521 else
2522#endif
2523 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2524 col += len;
2525 }
2526 }
2527
2528 /*
2529 * 4. Compose the folded-line string with 'foldtext', if set.
2530 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002531 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 txtcol = col; /* remember where text starts */
2534
2535 /*
2536 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2537 * Right-left text is put in columns 0 - number-col, normal text is put
2538 * in columns number-col - window-width.
2539 */
2540#ifdef FEAT_MBYTE
2541 if (has_mbyte)
2542 {
2543 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002544 int u8c, u8cc[MAX_MCO];
2545 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 int idx;
2547 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002548 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549# ifdef FEAT_ARABIC
2550 int prev_c = 0; /* previous Arabic character */
2551 int prev_c1 = 0; /* first composing char for prev_c */
2552# endif
2553
2554# ifdef FEAT_RIGHTLEFT
2555 if (wp->w_p_rl)
2556 idx = off;
2557 else
2558# endif
2559 idx = off + col;
2560
2561 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2562 for (p = text; *p != NUL; )
2563 {
2564 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002565 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (col + cells > W_WIDTH(wp)
2567# ifdef FEAT_RIGHTLEFT
2568 - (wp->w_p_rl ? col : 0)
2569# endif
2570 )
2571 break;
2572 ScreenLines[idx] = *p;
2573 if (enc_utf8)
2574 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 u8c = utfc_ptr2char(p, u8cc);
2576 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
2578 ScreenLinesUC[idx] = 0;
2579#ifdef FEAT_ARABIC
2580 prev_c = u8c;
2581#endif
2582 }
2583 else
2584 {
2585#ifdef FEAT_ARABIC
2586 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2587 {
2588 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002589 int pc, pc1, nc;
2590 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 int firstbyte = *p;
2592
2593 /* The idea of what is the previous and next
2594 * character depends on 'rightleft'. */
2595 if (wp->w_p_rl)
2596 {
2597 pc = prev_c;
2598 pc1 = prev_c1;
2599 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002600 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602 else
2603 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002604 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002606 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 prev_c = u8c;
2609
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002610 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 pc, pc1, nc);
2612 ScreenLines[idx] = firstbyte;
2613 }
2614 else
2615 prev_c = u8c;
2616#endif
2617 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002618#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 if (u8c >= 0x10000)
2620 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2621 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002624 for (i = 0; i < Screen_mco; ++i)
2625 {
2626 ScreenLinesC[i][idx] = u8cc[i];
2627 if (u8cc[i] == 0)
2628 break;
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 if (cells > 1)
2632 ScreenLines[idx + 1] = 0;
2633 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002634 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2635 /* double-byte single width character */
2636 ScreenLines2[idx] = p[1];
2637 else if (cells > 1)
2638 /* double-width character */
2639 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 col += cells;
2641 idx += cells;
2642 p += c_len;
2643 }
2644 }
2645 else
2646#endif
2647 {
2648 len = (int)STRLEN(text);
2649 if (len > W_WIDTH(wp) - col)
2650 len = W_WIDTH(wp) - col;
2651 if (len > 0)
2652 {
2653#ifdef FEAT_RIGHTLEFT
2654 if (wp->w_p_rl)
2655 STRNCPY(current_ScreenLine, text, len);
2656 else
2657#endif
2658 STRNCPY(current_ScreenLine + col, text, len);
2659 col += len;
2660 }
2661 }
2662
2663 /* Fill the rest of the line with the fold filler */
2664#ifdef FEAT_RIGHTLEFT
2665 if (wp->w_p_rl)
2666 col -= txtcol;
2667#endif
2668 while (col < W_WIDTH(wp)
2669#ifdef FEAT_RIGHTLEFT
2670 - (wp->w_p_rl ? txtcol : 0)
2671#endif
2672 )
2673 {
2674#ifdef FEAT_MBYTE
2675 if (enc_utf8)
2676 {
2677 if (fill_fold >= 0x80)
2678 {
2679 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002680 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 else
2683 ScreenLinesUC[off + col] = 0;
2684 }
2685#endif
2686 ScreenLines[off + col++] = fill_fold;
2687 }
2688
2689 if (text != buf)
2690 vim_free(text);
2691
2692 /*
2693 * 6. set highlighting for the Visual area an other text.
2694 * If all folded lines are in the Visual area, highlight the line.
2695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2697 {
2698 if (ltoreq(curwin->w_cursor, VIsual))
2699 {
2700 /* Visual is after curwin->w_cursor */
2701 top = &curwin->w_cursor;
2702 bot = &VIsual;
2703 }
2704 else
2705 {
2706 /* Visual is before curwin->w_cursor */
2707 top = &VIsual;
2708 bot = &curwin->w_cursor;
2709 }
2710 if (lnum >= top->lnum
2711 && lnume <= bot->lnum
2712 && (VIsual_mode != 'v'
2713 || ((lnum > top->lnum
2714 || (lnum == top->lnum
2715 && top->col == 0))
2716 && (lnume < bot->lnum
2717 || (lnume == bot->lnum
2718 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002719 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 if (VIsual_mode == Ctrl_V)
2722 {
2723 /* Visual block mode: highlight the chars part of the block */
2724 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2725 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002726 if (wp->w_old_cursor_lcol != MAXCOL
2727 && wp->w_old_cursor_lcol + txtcol
2728 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 len = wp->w_old_cursor_lcol;
2730 else
2731 len = W_WIDTH(wp) - txtcol;
2732 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002733 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 }
2736 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002739 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002744#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002745 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2746 if (wp->w_p_cc_cols)
2747 {
2748 int i = 0;
2749 int j = wp->w_p_cc_cols[i];
2750 int old_txtcol = txtcol;
2751
2752 while (j > -1)
2753 {
2754 txtcol += j;
2755 if (wp->w_p_wrap)
2756 txtcol -= wp->w_skipcol;
2757 else
2758 txtcol -= wp->w_leftcol;
2759 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2760 ScreenAttrs[off + txtcol] = hl_combine_attr(
2761 ScreenAttrs[off + txtcol], hl_attr(HLF_MC));
2762 txtcol = old_txtcol;
2763 j = wp->w_p_cc_cols[++i];
2764 }
2765 }
2766
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002767 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002768 if (wp->w_p_cuc)
2769 {
2770 txtcol += wp->w_virtcol;
2771 if (wp->w_p_wrap)
2772 txtcol -= wp->w_skipcol;
2773 else
2774 txtcol -= wp->w_leftcol;
2775 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2776 ScreenAttrs[off + txtcol] = hl_combine_attr(
2777 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2778 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2782 (int)W_WIDTH(wp), FALSE);
2783
2784 /*
2785 * Update w_cline_height and w_cline_folded if the cursor line was
2786 * updated (saves a call to plines() later).
2787 */
2788 if (wp == curwin
2789 && lnum <= curwin->w_cursor.lnum
2790 && lnume >= curwin->w_cursor.lnum)
2791 {
2792 curwin->w_cline_row = row;
2793 curwin->w_cline_height = 1;
2794 curwin->w_cline_folded = TRUE;
2795 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2796 }
2797}
2798
2799/*
2800 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2801 */
2802 static void
2803copy_text_attr(off, buf, len, attr)
2804 int off;
2805 char_u *buf;
2806 int len;
2807 int attr;
2808{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002809 int i;
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 mch_memmove(ScreenLines + off, buf, (size_t)len);
2812# ifdef FEAT_MBYTE
2813 if (enc_utf8)
2814 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2815# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002816 for (i = 0; i < len; ++i)
2817 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818}
2819
2820/*
2821 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002822 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
2824 static void
2825fill_foldcolumn(p, wp, closed, lnum)
2826 char_u *p;
2827 win_T *wp;
2828 int closed; /* TRUE of FALSE */
2829 linenr_T lnum; /* current line number */
2830{
2831 int i = 0;
2832 int level;
2833 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002834 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002835 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002838 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 level = win_foldinfo.fi_level;
2841 if (level > 0)
2842 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002843 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002844 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 /* If the column is too narrow, we start at the lowest level that
2847 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002848 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (first_level < 1)
2850 first_level = 1;
2851
Bram Moolenaar1c934292015-01-27 16:39:29 +01002852 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 if (win_foldinfo.fi_lnum == lnum
2855 && first_level + i >= win_foldinfo.fi_low_level)
2856 p[i] = '-';
2857 else if (first_level == 1)
2858 p[i] = '|';
2859 else if (first_level + i <= 9)
2860 p[i] = '0' + first_level + i;
2861 else
2862 p[i] = '>';
2863 if (first_level + i == level)
2864 break;
2865 }
2866 }
2867 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002868 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869}
2870#endif /* FEAT_FOLDING */
2871
2872/*
2873 * Display line "lnum" of window 'wp' on the screen.
2874 * Start at row "startrow", stop when "endrow" is reached.
2875 * wp->w_virtcol needs to be valid.
2876 *
2877 * Return the number of last row the line occupies.
2878 */
2879 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002880win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 win_T *wp;
2882 linenr_T lnum;
2883 int startrow;
2884 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002885 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 int col; /* visual column on screen */
2888 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2889 int c = 0; /* init for GCC */
2890 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01002891#ifdef FEAT_LINEBREAK
2892 long vcol_sbr = -1; /* virtual column after showbreak */
2893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 long vcol_prev = -1; /* "vcol" of previous character */
2895 char_u *line; /* current line */
2896 char_u *ptr; /* current position in "line" */
2897 int row; /* row in the window, excl w_winrow */
2898 int screen_row; /* row on the screen, incl w_winrow */
2899
2900 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2901 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002902 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002903 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 int c_extra = NUL; /* extra chars, all the same */
2905 int extra_attr = 0; /* attributes when n_extra != 0 */
2906 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2907 displaying lcs_eol at end-of-line */
2908 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2909 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2910
2911 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2912 int saved_n_extra = 0;
2913 char_u *saved_p_extra = NULL;
2914 int saved_c_extra = 0;
2915 int saved_char_attr = 0;
2916
2917 int n_attr = 0; /* chars with special attr */
2918 int saved_attr2 = 0; /* char_attr saved for n_attr */
2919 int n_attr3 = 0; /* chars with overruling special attr */
2920 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2921
2922 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2923
2924 int fromcol, tocol; /* start/end of inverting */
2925 int fromcol_prev = -2; /* start of inverting after cursor */
2926 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002928 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 pos_T pos;
2930 long v;
2931
2932 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002933 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2935 in this line */
2936 int attr = 0; /* attributes for area highlighting */
2937 int area_attr = 0; /* attributes desired by highlighting */
2938 int search_attr = 0; /* attributes desired by 'hlsearch' */
2939#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002940 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 int syntax_attr = 0; /* attributes desired by syntax */
2942 int has_syntax = FALSE; /* this buffer has syntax highl. */
2943 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002944 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002945 int draw_color_col = FALSE; /* highlight colorcolumn */
2946 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002947#endif
2948#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002949 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002950# define SPWORDLEN 150
2951 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002952 int nextlinecol = 0; /* column where nextline[] starts */
2953 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002954 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002955 int spell_attr = 0; /* attributes desired by spelling */
2956 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002957 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2958 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002959 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002960 static int cap_col = -1; /* column to check for Cap word */
2961 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002962 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#endif
2964 int extra_check; /* has syntax or linebreak */
2965#ifdef FEAT_MBYTE
2966 int multi_attr = 0; /* attributes desired by multibyte */
2967 int mb_l = 1; /* multi-byte byte length */
2968 int mb_c = 0; /* decoded multi-byte character */
2969 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002970 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971#endif
2972#ifdef FEAT_DIFF
2973 int filler_lines; /* nr of filler lines to be drawn */
2974 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002975 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 int change_start = MAXCOL; /* first col of changed area */
2977 int change_end = -1; /* last col of changed area */
2978#endif
2979 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2980#ifdef FEAT_LINEBREAK
2981 int need_showbreak = FALSE;
2982#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002983#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2984 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002986 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#endif
2988#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002989 matchitem_T *cur; /* points to the match list */
2990 match_T *shl; /* points to search_hl or a match */
2991 int shl_flag; /* flag to indicate whether search_hl
2992 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002993 int pos_inprogress; /* marks that position match search is
2994 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002995 int prevcol_hl_flag; /* flag to indicate whether prevcol
2996 equals startcol of search_hl or one
2997 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998#endif
2999#ifdef FEAT_ARABIC
3000 int prev_c = 0; /* previous Arabic character */
3001 int prev_c1 = 0; /* first composing char for prev_c */
3002#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003003#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003004 int did_line_attr = 0;
3005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 /* draw_state: items that are drawn in sequence: */
3008#define WL_START 0 /* nothing done yet */
3009#ifdef FEAT_CMDWIN
3010# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3011#else
3012# define WL_CMDLINE WL_START
3013#endif
3014#ifdef FEAT_FOLDING
3015# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3016#else
3017# define WL_FOLD WL_CMDLINE
3018#endif
3019#ifdef FEAT_SIGNS
3020# define WL_SIGN WL_FOLD + 1 /* column for signs */
3021#else
3022# define WL_SIGN WL_FOLD /* column for signs */
3023#endif
3024#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003025#ifdef FEAT_LINEBREAK
3026# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003028# define WL_BRI WL_NR
3029#endif
3030#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3031# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3032#else
3033# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#endif
3035#define WL_LINE WL_SBR + 1 /* text in the line */
3036 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003037#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 int feedback_col = 0;
3039 int feedback_old_attr = -1;
3040#endif
3041
Bram Moolenaar860cae12010-06-05 23:22:07 +02003042#ifdef FEAT_CONCEAL
3043 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003044 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003045 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003046 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003047 int is_concealing = FALSE;
3048 int boguscols = 0; /* nonexistent columns added to force
3049 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003050 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003051 int did_wcol = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003052 int match_conc = FALSE; /* cchar for match functions */
3053 int has_match_conc = FALSE; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003054 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003055# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003056# define FIX_FOR_BOGUSCOLS \
3057 { \
3058 n_extra += vcol_off; \
3059 vcol -= vcol_off; \
3060 vcol_off = 0; \
3061 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003062 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003063 boguscols = 0; \
3064 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003065#else
3066# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 if (startrow > endrow) /* past the end already! */
3070 return startrow;
3071
3072 row = startrow;
3073 screen_row = row + W_WINROW(wp);
3074
3075 /*
3076 * To speed up the loop below, set extra_check when there is linebreak,
3077 * trailing white space and/or syntax processing to be done.
3078 */
3079#ifdef FEAT_LINEBREAK
3080 extra_check = wp->w_p_lbr;
3081#else
3082 extra_check = 0;
3083#endif
3084#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003085 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
3087 /* Prepare for syntax highlighting in this line. When there is an
3088 * error, stop syntax highlighting. */
3089 save_did_emsg = did_emsg;
3090 did_emsg = FALSE;
3091 syntax_start(wp, lnum);
3092 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003093 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 else
3095 {
3096 did_emsg = save_did_emsg;
3097 has_syntax = TRUE;
3098 extra_check = TRUE;
3099 }
3100 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003101
3102 /* Check for columns to display for 'colorcolumn'. */
3103 color_cols = wp->w_p_cc_cols;
3104 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003105 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003106#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003107
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003108#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003109 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003110 && *wp->w_s->b_p_spl != NUL
3111 && wp->w_s->b_langp.ga_len > 0
3112 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003113 {
3114 /* Prepare for spell checking. */
3115 has_spell = TRUE;
3116 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003117
3118 /* Get the start of the next line, so that words that wrap to the next
3119 * line are found too: "et<line-break>al.".
3120 * Trick: skip a few chars for C/shell/Vim comments */
3121 nextline[SPWORDLEN] = NUL;
3122 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3123 {
3124 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3125 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3126 }
3127
3128 /* When a word wrapped from the previous line the start of the current
3129 * line is valid. */
3130 if (lnum == checked_lnum)
3131 cur_checked_col = checked_col;
3132 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003133
3134 /* When there was a sentence end in the previous line may require a
3135 * word starting with capital in this line. In line 1 always check
3136 * the first word. */
3137 if (lnum != capcol_lnum)
3138 cap_col = -1;
3139 if (lnum == 1)
3140 cap_col = 0;
3141 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#endif
3144
3145 /*
3146 * handle visual active in this window
3147 */
3148 fromcol = -10;
3149 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3151 {
3152 /* Visual is after curwin->w_cursor */
3153 if (ltoreq(curwin->w_cursor, VIsual))
3154 {
3155 top = &curwin->w_cursor;
3156 bot = &VIsual;
3157 }
3158 else /* Visual is before curwin->w_cursor */
3159 {
3160 top = &VIsual;
3161 bot = &curwin->w_cursor;
3162 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003163 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (VIsual_mode == Ctrl_V) /* block mode */
3165 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003166 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 fromcol = wp->w_old_cursor_fcol;
3169 tocol = wp->w_old_cursor_lcol;
3170 }
3171 }
3172 else /* non-block mode */
3173 {
3174 if (lnum > top->lnum && lnum <= bot->lnum)
3175 fromcol = 0;
3176 else if (lnum == top->lnum)
3177 {
3178 if (VIsual_mode == 'V') /* linewise */
3179 fromcol = 0;
3180 else
3181 {
3182 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3183 if (gchar_pos(top) == NUL)
3184 tocol = fromcol + 1;
3185 }
3186 }
3187 if (VIsual_mode != 'V' && lnum == bot->lnum)
3188 {
3189 if (*p_sel == 'e' && bot->col == 0
3190#ifdef FEAT_VIRTUALEDIT
3191 && bot->coladd == 0
3192#endif
3193 )
3194 {
3195 fromcol = -10;
3196 tocol = MAXCOL;
3197 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003198 else if (bot->col == MAXCOL)
3199 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 else
3201 {
3202 pos = *bot;
3203 if (*p_sel == 'e')
3204 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3205 else
3206 {
3207 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3208 ++tocol;
3209 }
3210 }
3211 }
3212 }
3213
3214#ifndef MSDOS
3215 /* Check if the character under the cursor should not be inverted */
3216 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3217# ifdef FEAT_GUI
3218 && !gui.in_use
3219# endif
3220 )
3221 noinvcur = TRUE;
3222#endif
3223
3224 /* if inverting in this line set area_highlighting */
3225 if (fromcol >= 0)
3226 {
3227 area_highlighting = TRUE;
3228 attr = hl_attr(HLF_V);
3229#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003230 if ((clip_star.available && !clip_star.owned
3231 && clip_isautosel_star())
3232 || (clip_plus.available && !clip_plus.owned
3233 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 attr = hl_attr(HLF_VNC);
3235#endif
3236 }
3237 }
3238
3239 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003240 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003242 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 && wp == curwin
3244 && lnum >= curwin->w_cursor.lnum
3245 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3246 {
3247 if (lnum == curwin->w_cursor.lnum)
3248 getvcol(curwin, &(curwin->w_cursor),
3249 (colnr_T *)&fromcol, NULL, NULL);
3250 else
3251 fromcol = 0;
3252 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3253 {
3254 pos.lnum = lnum;
3255 pos.col = search_match_endcol;
3256 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3257 }
3258 else
3259 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003260 /* do at least one character; happens when past end of line */
3261 if (fromcol == tocol)
3262 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 area_highlighting = TRUE;
3264 attr = hl_attr(HLF_I);
3265 }
3266
3267#ifdef FEAT_DIFF
3268 filler_lines = diff_check(wp, lnum);
3269 if (filler_lines < 0)
3270 {
3271 if (filler_lines == -1)
3272 {
3273 if (diff_find_change(wp, lnum, &change_start, &change_end))
3274 diff_hlf = HLF_ADD; /* added line */
3275 else if (change_start == 0)
3276 diff_hlf = HLF_TXD; /* changed text */
3277 else
3278 diff_hlf = HLF_CHD; /* changed line */
3279 }
3280 else
3281 diff_hlf = HLF_ADD; /* added line */
3282 filler_lines = 0;
3283 area_highlighting = TRUE;
3284 }
3285 if (lnum == wp->w_topline)
3286 filler_lines = wp->w_topfill;
3287 filler_todo = filler_lines;
3288#endif
3289
3290#ifdef LINE_ATTR
3291# ifdef FEAT_SIGNS
3292 /* If this line has a sign with line highlighting set line_attr. */
3293 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3294 if (v != 0)
3295 line_attr = sign_get_attr((int)v, TRUE);
3296# endif
3297# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3298 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003299 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 line_attr = hl_attr(HLF_L);
3301# endif
3302 if (line_attr != 0)
3303 area_highlighting = TRUE;
3304#endif
3305
3306 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3307 ptr = line;
3308
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003309#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003310 if (has_spell)
3311 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003312 /* For checking first word with a capital skip white space. */
3313 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003314 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003315
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316 /* To be able to spell-check over line boundaries copy the end of the
3317 * current line into nextline[]. Above the start of the next line was
3318 * copied to nextline[SPWORDLEN]. */
3319 if (nextline[SPWORDLEN] == NUL)
3320 {
3321 /* No next line or it is empty. */
3322 nextlinecol = MAXCOL;
3323 nextline_idx = 0;
3324 }
3325 else
3326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003327 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003328 if (v < SPWORDLEN)
3329 {
3330 /* Short line, use it completely and append the start of the
3331 * next line. */
3332 nextlinecol = 0;
3333 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003334 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003335 nextline_idx = v + 1;
3336 }
3337 else
3338 {
3339 /* Long line, use only the last SPWORDLEN bytes. */
3340 nextlinecol = v - SPWORDLEN;
3341 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3342 nextline_idx = SPWORDLEN + 1;
3343 }
3344 }
3345 }
3346#endif
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 /* find start of trailing whitespace */
3349 if (wp->w_p_list && lcs_trail)
3350 {
3351 trailcol = (colnr_T)STRLEN(ptr);
3352 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3353 --trailcol;
3354 trailcol += (colnr_T) (ptr - line);
3355 extra_check = TRUE;
3356 }
3357
3358 /*
3359 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3360 * first character to be displayed.
3361 */
3362 if (wp->w_p_wrap)
3363 v = wp->w_skipcol;
3364 else
3365 v = wp->w_leftcol;
3366 if (v > 0)
3367 {
3368#ifdef FEAT_MBYTE
3369 char_u *prev_ptr = ptr;
3370#endif
3371 while (vcol < v && *ptr != NUL)
3372 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003373 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 vcol += c;
3375#ifdef FEAT_MBYTE
3376 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003378 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003381 /* When:
3382 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003383 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003384 * - 'virtualedit' is set, or
3385 * - the visual mode is active,
3386 * the end of the line may be before the start of the displayed part.
3387 */
3388 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003389#ifdef FEAT_SYN_HL
3390 wp->w_p_cuc || draw_color_col ||
3391#endif
3392#ifdef FEAT_VIRTUALEDIT
3393 virtual_active() ||
3394#endif
3395 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400 /* Handle a character that's not completely on the screen: Put ptr at
3401 * that character but skip the first few screen characters. */
3402 if (vcol > v)
3403 {
3404 vcol -= c;
3405#ifdef FEAT_MBYTE
3406 ptr = prev_ptr;
3407#else
3408 --ptr;
3409#endif
3410 n_skip = v - vcol;
3411 }
3412
3413 /*
3414 * Adjust for when the inverted text is before the screen,
3415 * and when the start of the inverted text is before the screen.
3416 */
3417 if (tocol <= vcol)
3418 fromcol = 0;
3419 else if (fromcol >= 0 && fromcol < vcol)
3420 fromcol = vcol;
3421
3422#ifdef FEAT_LINEBREAK
3423 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3424 if (wp->w_p_wrap)
3425 need_showbreak = TRUE;
3426#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003427#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003428 /* When spell checking a word we need to figure out the start of the
3429 * word and if it's badly spelled or not. */
3430 if (has_spell)
3431 {
3432 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003433 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003434 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003435
3436 pos = wp->w_cursor;
3437 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003438 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003439 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003440
3441 /* spell_move_to() may call ml_get() and make "line" invalid */
3442 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3443 ptr = line + linecol;
3444
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003445 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003446 {
3447 /* no bad word found at line start, don't check until end of a
3448 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003449 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003450 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003451 }
3452 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003453 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003454 /* bad word found, use attributes until end of word */
3455 word_end = wp->w_cursor.col + len + 1;
3456
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003457 /* Turn index into actual attributes. */
3458 if (spell_hlf != HLF_COUNT)
3459 spell_attr = highlight_attr[spell_hlf];
3460 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003461 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003462
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003463# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003464 /* Need to restart syntax highlighting for this line. */
3465 if (has_syntax)
3466 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003467# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003468 }
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471
3472 /*
3473 * Correct highlighting for cursor that can't be disabled.
3474 * Avoids having to check this for each character.
3475 */
3476 if (fromcol >= 0)
3477 {
3478 if (noinvcur)
3479 {
3480 if ((colnr_T)fromcol == wp->w_virtcol)
3481 {
3482 /* highlighting starts at cursor, let it start just after the
3483 * cursor */
3484 fromcol_prev = fromcol;
3485 fromcol = -1;
3486 }
3487 else if ((colnr_T)fromcol < wp->w_virtcol)
3488 /* restart highlighting after the cursor */
3489 fromcol_prev = wp->w_virtcol;
3490 }
3491 if (fromcol >= tocol)
3492 fromcol = -1;
3493 }
3494
3495#ifdef FEAT_SEARCH_EXTRA
3496 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003497 * Handle highlighting the last used search pattern and matches.
3498 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003500 cur = wp->w_match_head;
3501 shl_flag = FALSE;
3502 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003504 if (shl_flag == FALSE)
3505 {
3506 shl = &search_hl;
3507 shl_flag = TRUE;
3508 }
3509 else
3510 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003511 shl->startcol = MAXCOL;
3512 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003514 v = (long)(ptr - line);
3515 if (cur != NULL)
3516 cur->pos.cur = 0;
3517 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3518
3519 /* Need to get the line again, a multi-line regexp may have made it
3520 * invalid. */
3521 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3522 ptr = line + v;
3523
3524 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003526 if (shl->lnum == lnum)
3527 shl->startcol = shl->rm.startpos[0].col;
3528 else
3529 shl->startcol = 0;
3530 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3531 - shl->rm.startpos[0].lnum)
3532 shl->endcol = shl->rm.endpos[0].col;
3533 else
3534 shl->endcol = MAXCOL;
3535 /* Highlight one character for an empty match. */
3536 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003539 if (has_mbyte && line[shl->endcol] != NUL)
3540 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3541 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003543 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003545 if ((long)shl->startcol < v) /* match at leftcol */
3546 {
3547 shl->attr_cur = shl->attr;
3548 search_attr = shl->attr;
3549 }
3550 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003552 if (shl != &search_hl && cur != NULL)
3553 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555#endif
3556
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003557#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003558 /* Cursor line highlighting for 'cursorline' in the current window. Not
3559 * when Visual mode is active, because it's not clear what is selected
3560 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003561 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003562 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003563 {
3564 line_attr = hl_attr(HLF_CUL);
3565 area_highlighting = TRUE;
3566 }
3567#endif
3568
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003569 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 col = 0;
3571#ifdef FEAT_RIGHTLEFT
3572 if (wp->w_p_rl)
3573 {
3574 /* Rightleft window: process the text in the normal direction, but put
3575 * it in current_ScreenLine[] from right to left. Start at the
3576 * rightmost column of the window. */
3577 col = W_WIDTH(wp) - 1;
3578 off += col;
3579 }
3580#endif
3581
3582 /*
3583 * Repeat for the whole displayed line.
3584 */
3585 for (;;)
3586 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003587#ifdef FEAT_CONCEAL
3588 has_match_conc = FALSE;
3589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /* Skip this quickly when working on the text. */
3591 if (draw_state != WL_LINE)
3592 {
3593#ifdef FEAT_CMDWIN
3594 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3595 {
3596 draw_state = WL_CMDLINE;
3597 if (cmdwin_type != 0 && wp == curwin)
3598 {
3599 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003601 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 char_attr = hl_attr(HLF_AT);
3603 }
3604 }
3605#endif
3606
3607#ifdef FEAT_FOLDING
3608 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3609 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003610 int fdc = compute_foldcolumn(wp, 0);
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003613 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 /* Draw the 'foldcolumn'. */
3616 fill_foldcolumn(extra, wp, FALSE, lnum);
Bram Moolenaar1c934292015-01-27 16:39:29 +01003617 n_extra = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003619 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 c_extra = NUL;
3621 char_attr = hl_attr(HLF_FC);
3622 }
3623 }
3624#endif
3625
3626#ifdef FEAT_SIGNS
3627 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3628 {
3629 draw_state = WL_SIGN;
3630 /* Show the sign column when there are any signs in this
3631 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003632 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003634 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003636 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637# endif
3638
3639 /* Draw two cells with the sign value or blank. */
3640 c_extra = ' ';
3641 char_attr = hl_attr(HLF_SC);
3642 n_extra = 2;
3643
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003644 if (row == startrow
3645#ifdef FEAT_DIFF
3646 + filler_lines && filler_todo <= 0
3647#endif
3648 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3651 SIGN_TEXT);
3652# ifdef FEAT_SIGN_ICONS
3653 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3654 SIGN_ICON);
3655 if (gui.in_use && icon_sign != 0)
3656 {
3657 /* Use the image in this position. */
3658 c_extra = SIGN_BYTE;
3659# ifdef FEAT_NETBEANS_INTG
3660 if (buf_signcount(wp->w_buffer, lnum) > 1)
3661 c_extra = MULTISIGN_BYTE;
3662# endif
3663 char_attr = icon_sign;
3664 }
3665 else
3666# endif
3667 if (text_sign != 0)
3668 {
3669 p_extra = sign_get_text(text_sign);
3670 if (p_extra != NULL)
3671 {
3672 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003673 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 char_attr = sign_get_attr(text_sign, FALSE);
3676 }
3677 }
3678 }
3679 }
3680#endif
3681
3682 if (draw_state == WL_NR - 1 && n_extra == 0)
3683 {
3684 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003685 /* Display the absolute or relative line number. After the
3686 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3687 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 && (row == startrow
3689#ifdef FEAT_DIFF
3690 + filler_lines
3691#endif
3692 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3693 {
3694 /* Draw the line number (empty space after wrapping). */
3695 if (row == startrow
3696#ifdef FEAT_DIFF
3697 + filler_lines
3698#endif
3699 )
3700 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003701 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003702 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003703
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003704 if (wp->w_p_nu && !wp->w_p_rnu)
3705 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003706 num = (long)lnum;
3707 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003708 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003709 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003710 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003711 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003712 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003713 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003714 num = lnum;
3715 fmt = "%-*ld ";
3716 }
3717 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003718
Bram Moolenaar700e7342013-01-30 12:31:36 +01003719 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003720 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (wp->w_skipcol > 0)
3722 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3723 *p_extra = '-';
3724#ifdef FEAT_RIGHTLEFT
3725 if (wp->w_p_rl) /* reverse line numbers */
3726 rl_mirror(extra);
3727#endif
3728 p_extra = extra;
3729 c_extra = NUL;
3730 }
3731 else
3732 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003733 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003735#ifdef FEAT_SYN_HL
3736 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003737 * the current line differently.
3738 * TODO: Can we use CursorLine instead of CursorLineNr
3739 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003740 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003741 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003742 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 }
3746
Bram Moolenaar597a4222014-06-25 14:39:50 +02003747#ifdef FEAT_LINEBREAK
3748 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3749 && n_extra == 0 && *p_sbr != NUL)
3750 /* draw indent after showbreak value */
3751 draw_state = WL_BRI;
3752 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3753 /* After the showbreak, draw the breakindent */
3754 draw_state = WL_BRI - 1;
3755
3756 /* draw 'breakindent': indent wrapped text accordingly */
3757 if (draw_state == WL_BRI - 1 && n_extra == 0)
3758 {
3759 draw_state = WL_BRI;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003760 if (wp->w_p_bri && n_extra == 0 && row != startrow
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003761# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003762 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003763# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003764 )
3765 {
3766 char_attr = 0; /* was: hl_attr(HLF_AT); */
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003767# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003768 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003769 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770 char_attr = hl_attr(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003771# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003772 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3773 char_attr = hl_combine_attr(char_attr,
3774 hl_attr(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003775# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003776 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003777# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003778 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003779 c_extra = ' ';
3780 n_extra = get_breakindent_win(wp,
3781 ml_get_buf(wp->w_buffer, lnum, FALSE));
3782 /* Correct end of highlighted area for 'breakindent',
3783 * required when 'linebreak' is also set. */
3784 if (tocol == vcol)
3785 tocol += n_extra;
3786 }
3787 }
3788#endif
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3791 if (draw_state == WL_SBR - 1 && n_extra == 0)
3792 {
3793 draw_state = WL_SBR;
3794# ifdef FEAT_DIFF
3795 if (filler_todo > 0)
3796 {
3797 /* Draw "deleted" diff line(s). */
3798 if (char2cells(fill_diff) > 1)
3799 c_extra = '-';
3800 else
3801 c_extra = fill_diff;
3802# ifdef FEAT_RIGHTLEFT
3803 if (wp->w_p_rl)
3804 n_extra = col + 1;
3805 else
3806# endif
3807 n_extra = W_WIDTH(wp) - col;
3808 char_attr = hl_attr(HLF_DED);
3809 }
3810# endif
3811# ifdef FEAT_LINEBREAK
3812 if (*p_sbr != NUL && need_showbreak)
3813 {
3814 /* Draw 'showbreak' at the start of each broken line. */
3815 p_extra = p_sbr;
3816 c_extra = NUL;
3817 n_extra = (int)STRLEN(p_sbr);
3818 char_attr = hl_attr(HLF_AT);
3819 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01003820 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 /* Correct end of highlighted area for 'showbreak',
3822 * required when 'linebreak' is also set. */
3823 if (tocol == vcol)
3824 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003825#ifdef FEAT_SYN_HL
3826 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003827 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003828 char_attr = hl_combine_attr(char_attr,
3829 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832# endif
3833 }
3834#endif
3835
3836 if (draw_state == WL_LINE - 1 && n_extra == 0)
3837 {
3838 draw_state = WL_LINE;
3839 if (saved_n_extra)
3840 {
3841 /* Continue item from end of wrapped line. */
3842 n_extra = saved_n_extra;
3843 c_extra = saved_c_extra;
3844 p_extra = saved_p_extra;
3845 char_attr = saved_char_attr;
3846 }
3847 else
3848 char_attr = 0;
3849 }
3850 }
3851
3852 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003853 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003854 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#ifdef FEAT_DIFF
3856 && filler_todo <= 0
3857#endif
3858 )
3859 {
3860 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3861 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003862 /* Pretend we have finished updating the window. Except when
3863 * 'cursorcolumn' is set. */
3864#ifdef FEAT_SYN_HL
3865 if (wp->w_p_cuc)
3866 row = wp->w_cline_row + wp->w_cline_height;
3867 else
3868#endif
3869 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871 }
3872
3873 if (draw_state == WL_LINE && area_highlighting)
3874 {
3875 /* handle Visual or match highlighting in this line */
3876 if (vcol == fromcol
3877#ifdef FEAT_MBYTE
3878 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3879 && (*mb_ptr2cells)(ptr) > 1)
3880#endif
3881 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003882 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 && vcol < tocol))
3884 area_attr = attr; /* start highlighting */
3885 else if (area_attr != 0
3886 && (vcol == tocol
3887 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890#ifdef FEAT_SEARCH_EXTRA
3891 if (!n_extra)
3892 {
3893 /*
3894 * Check for start/end of search pattern match.
3895 * After end, check for start/end of next match.
3896 * When another match, have to check for start again.
3897 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003898 * Do this for 'search_hl' and the match list (ordered by
3899 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003901 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003902 cur = wp->w_match_head;
3903 shl_flag = FALSE;
3904 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003906 if (shl_flag == FALSE
3907 && ((cur != NULL
3908 && cur->priority > SEARCH_HL_PRIORITY)
3909 || cur == NULL))
3910 {
3911 shl = &search_hl;
3912 shl_flag = TRUE;
3913 }
3914 else
3915 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003916 if (cur != NULL)
3917 cur->pos.cur = 0;
3918 pos_inprogress = TRUE;
3919 while (shl->rm.regprog != NULL
3920 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003922 if (shl->startcol != MAXCOL
3923 && v >= (long)shl->startcol
3924 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003926#ifdef FEAT_MBYTE
3927 int tmp_col = v + MB_PTR2LEN(ptr);
3928
3929 if (shl->endcol < tmp_col)
3930 shl->endcol = tmp_col;
3931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003933#ifdef FEAT_CONCEAL
3934 if (cur != NULL && syn_name2id((char_u *)"Conceal")
3935 == cur->hlg_id)
3936 {
3937 has_match_conc = TRUE;
3938 match_conc = cur->conceal_char;
3939 }
3940 else
3941 has_match_conc = match_conc = FALSE;
3942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01003944 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
3946 shl->attr_cur = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003947#ifdef FEAT_CONCEAL
3948 prev_syntax_id = 0;
3949#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003950 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3951 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02003952 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 /* Need to get the line again, a multi-line regexp
3955 * may have made it invalid. */
3956 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3957 ptr = line + v;
3958
3959 if (shl->lnum == lnum)
3960 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003961 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003963 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003965 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003967 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
3969 /* highlight empty match, try again after
3970 * it */
3971#ifdef FEAT_MBYTE
3972 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003973 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003974 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 else
3976#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003977 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 }
3979
3980 /* Loop to check if the match starts at the
3981 * current position */
3982 continue;
3983 }
3984 }
3985 break;
3986 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003987 if (shl != &search_hl && cur != NULL)
3988 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003990
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003991 /* Use attributes from match with highest priority among
3992 * 'search_hl' and the match list. */
3993 search_attr = search_hl.attr_cur;
3994 cur = wp->w_match_head;
3995 shl_flag = FALSE;
3996 while (cur != NULL || shl_flag == FALSE)
3997 {
3998 if (shl_flag == FALSE
3999 && ((cur != NULL
4000 && cur->priority > SEARCH_HL_PRIORITY)
4001 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004002 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004003 shl = &search_hl;
4004 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004005 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004006 else
4007 shl = &cur->hl;
4008 if (shl->attr_cur != 0)
4009 search_attr = shl->attr_cur;
4010 if (shl != &search_hl && cur != NULL)
4011 cur = cur->next;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014#endif
4015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004017 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004019 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4020 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004022 if (diff_hlf == HLF_TXD && ptr - line > change_end
4023 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004025 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004026 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4027 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004030
4031 /* Decide which of the highlight attributes to use. */
4032 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004034 if (area_attr != 0)
4035 char_attr = hl_combine_attr(line_attr, area_attr);
4036 else if (search_attr != 0)
4037 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004038 /* Use line_attr when not in the Visual or 'incsearch' area
4039 * (area_attr may be 0 when "noinvcur" is set). */
4040 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004041 || vcol < fromcol || vcol_prev < fromcol_prev
4042 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004043 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004044#else
4045 if (area_attr != 0)
4046 char_attr = area_attr;
4047 else if (search_attr != 0)
4048 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004049#endif
4050 else
4051 {
4052 attr_pri = FALSE;
4053#ifdef FEAT_SYN_HL
4054 if (has_syntax)
4055 char_attr = syntax_attr;
4056 else
4057#endif
4058 char_attr = 0;
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061
4062 /*
4063 * Get the next character to put on the screen.
4064 */
4065 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004066 * The "p_extra" points to the extra stuff that is inserted to
4067 * represent special characters (non-printable stuff) and other
4068 * things. When all characters are the same, c_extra is used.
4069 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4070 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4072 */
4073 if (n_extra > 0)
4074 {
4075 if (c_extra != NUL)
4076 {
4077 c = c_extra;
4078#ifdef FEAT_MBYTE
4079 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
4080 if (enc_utf8 && (*mb_char2len)(c) > 1)
4081 {
4082 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086 else
4087 mb_utf8 = FALSE;
4088#endif
4089 }
4090 else
4091 {
4092 c = *p_extra;
4093#ifdef FEAT_MBYTE
4094 if (has_mbyte)
4095 {
4096 mb_c = c;
4097 if (enc_utf8)
4098 {
4099 /* If the UTF-8 character is more than one byte:
4100 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004101 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 mb_utf8 = FALSE;
4103 if (mb_l > n_extra)
4104 mb_l = 1;
4105 else if (mb_l > 1)
4106 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004107 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004109 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 }
4112 else
4113 {
4114 /* if this is a DBCS character, put it in "mb_c" */
4115 mb_l = MB_BYTE2LEN(c);
4116 if (mb_l >= n_extra)
4117 mb_l = 1;
4118 else if (mb_l > 1)
4119 mb_c = (c << 8) + p_extra[1];
4120 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004121 if (mb_l == 0) /* at the NUL at end-of-line */
4122 mb_l = 1;
4123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 /* If a double-width char doesn't fit display a '>' in the
4125 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004126 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127# ifdef FEAT_RIGHTLEFT
4128 wp->w_p_rl ? (col <= 0) :
4129# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004130 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 && (*mb_char2cells)(mb_c) == 2)
4132 {
4133 c = '>';
4134 mb_c = c;
4135 mb_l = 1;
4136 mb_utf8 = FALSE;
4137 multi_attr = hl_attr(HLF_AT);
4138 /* put the pointer back to output the double-width
4139 * character at the start of the next line. */
4140 ++n_extra;
4141 --p_extra;
4142 }
4143 else
4144 {
4145 n_extra -= mb_l - 1;
4146 p_extra += mb_l - 1;
4147 }
4148 }
4149#endif
4150 ++p_extra;
4151 }
4152 --n_extra;
4153 }
4154 else
4155 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004156 if (p_extra_free != NULL)
4157 {
4158 vim_free(p_extra_free);
4159 p_extra_free = NULL;
4160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 /*
4162 * Get a character from the line itself.
4163 */
4164 c = *ptr;
4165#ifdef FEAT_MBYTE
4166 if (has_mbyte)
4167 {
4168 mb_c = c;
4169 if (enc_utf8)
4170 {
4171 /* If the UTF-8 character is more than one byte: Decode it
4172 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004173 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 mb_utf8 = FALSE;
4175 if (mb_l > 1)
4176 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /* Overlong encoded ASCII or ASCII with composing char
4179 * is displayed normally, except a NUL. */
4180 if (mb_c < 0x80)
4181 c = mb_c;
4182 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004183
4184 /* At start of the line we can have a composing char.
4185 * Draw it as a space with a composing char. */
4186 if (utf_iscomposing(mb_c))
4187 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004188 int i;
4189
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004190 for (i = Screen_mco - 1; i > 0; --i)
4191 u8cc[i] = u8cc[i - 1];
4192 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004193 mb_c = ' ';
4194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196
4197 if ((mb_l == 1 && c >= 0x80)
4198 || (mb_l >= 1 && mb_c == 0)
4199 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004200# ifdef UNICODE16
4201 || mb_c >= 0x10000
4202# endif
4203 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 /*
4206 * Illegal UTF-8 byte: display as <xx>.
4207 * Non-BMP character : display as ? or fullwidth ?.
4208 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004209# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004214# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (wp->w_p_rl) /* reverse */
4216 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004219# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 else if (utf_char2cells(mb_c) != 2)
4221 STRCPY(extra, "?");
4222 else
4223 /* 0xff1f in UTF-8: full-width '?' */
4224 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004225# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 p_extra = extra;
4228 c = *p_extra;
4229 mb_c = mb_ptr2char_adv(&p_extra);
4230 mb_utf8 = (c >= 0x80);
4231 n_extra = (int)STRLEN(p_extra);
4232 c_extra = NUL;
4233 if (area_attr == 0 && search_attr == 0)
4234 {
4235 n_attr = n_extra + 1;
4236 extra_attr = hl_attr(HLF_8);
4237 saved_attr2 = char_attr; /* save current attr */
4238 }
4239 }
4240 else if (mb_l == 0) /* at the NUL at end-of-line */
4241 mb_l = 1;
4242#ifdef FEAT_ARABIC
4243 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4244 {
4245 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004246 int pc, pc1, nc;
4247 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /* The idea of what is the previous and next
4250 * character depends on 'rightleft'. */
4251 if (wp->w_p_rl)
4252 {
4253 pc = prev_c;
4254 pc1 = prev_c1;
4255 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 else
4259 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004262 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 prev_c = mb_c;
4265
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 else
4269 prev_c = mb_c;
4270#endif
4271 }
4272 else /* enc_dbcs */
4273 {
4274 mb_l = MB_BYTE2LEN(c);
4275 if (mb_l == 0) /* at the NUL at end-of-line */
4276 mb_l = 1;
4277 else if (mb_l > 1)
4278 {
4279 /* We assume a second byte below 32 is illegal.
4280 * Hopefully this is OK for all double-byte encodings!
4281 */
4282 if (ptr[1] >= 32)
4283 mb_c = (c << 8) + ptr[1];
4284 else
4285 {
4286 if (ptr[1] == NUL)
4287 {
4288 /* head byte at end of line */
4289 mb_l = 1;
4290 transchar_nonprint(extra, c);
4291 }
4292 else
4293 {
4294 /* illegal tail byte */
4295 mb_l = 2;
4296 STRCPY(extra, "XX");
4297 }
4298 p_extra = extra;
4299 n_extra = (int)STRLEN(extra) - 1;
4300 c_extra = NUL;
4301 c = *p_extra++;
4302 if (area_attr == 0 && search_attr == 0)
4303 {
4304 n_attr = n_extra + 1;
4305 extra_attr = hl_attr(HLF_8);
4306 saved_attr2 = char_attr; /* save current attr */
4307 }
4308 mb_c = c;
4309 }
4310 }
4311 }
4312 /* If a double-width char doesn't fit display a '>' in the
4313 * last column; the character is displayed at the start of the
4314 * next line. */
4315 if ((
4316# ifdef FEAT_RIGHTLEFT
4317 wp->w_p_rl ? (col <= 0) :
4318# endif
4319 (col >= W_WIDTH(wp) - 1))
4320 && (*mb_char2cells)(mb_c) == 2)
4321 {
4322 c = '>';
4323 mb_c = c;
4324 mb_utf8 = FALSE;
4325 mb_l = 1;
4326 multi_attr = hl_attr(HLF_AT);
4327 /* Put pointer back so that the character will be
4328 * displayed at the start of the next line. */
4329 --ptr;
4330 }
4331 else if (*ptr != NUL)
4332 ptr += mb_l - 1;
4333
4334 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004335 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004336 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004337 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004340 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 c = ' ';
4342 if (area_attr == 0 && search_attr == 0)
4343 {
4344 n_attr = n_extra + 1;
4345 extra_attr = hl_attr(HLF_AT);
4346 saved_attr2 = char_attr; /* save current attr */
4347 }
4348 mb_c = c;
4349 mb_utf8 = FALSE;
4350 mb_l = 1;
4351 }
4352
4353 }
4354#endif
4355 ++ptr;
4356
Bram Moolenaar79278362015-04-21 18:33:48 +02004357 /* 'list': change char 160 to lcs_nbsp and space to lcs_space. */
4358 if (wp->w_p_list
4359 && (((c == 160
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004360#ifdef FEAT_MBYTE
Bram Moolenaar73284b92015-05-04 17:28:22 +02004361 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004362#endif
Bram Moolenaar79278362015-04-21 18:33:48 +02004363 ) && lcs_nbsp)
Bram Moolenaarc4dc2862015-05-04 16:10:26 +02004364 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004365 {
Bram Moolenaar79278362015-04-21 18:33:48 +02004366 c = (c == ' ') ? lcs_space : lcs_nbsp;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367 if (area_attr == 0 && search_attr == 0)
4368 {
4369 n_attr = 1;
4370 extra_attr = hl_attr(HLF_8);
4371 saved_attr2 = char_attr; /* save current attr */
4372 }
4373#ifdef FEAT_MBYTE
4374 mb_c = c;
4375 if (enc_utf8 && (*mb_char2len)(c) > 1)
4376 {
4377 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004378 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004379 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004380 }
4381 else
4382 mb_utf8 = FALSE;
4383#endif
4384 }
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (extra_check)
4387 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004388#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004389 int can_spell = TRUE;
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_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 /* Get syntax attribute, unless still at the start of the line
4394 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004395 v = (long)(ptr - line);
4396 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
4398 /* Get the syntax attribute for the character. If there
4399 * is an error, disable syntax highlighting. */
4400 save_did_emsg = did_emsg;
4401 did_emsg = FALSE;
4402
Bram Moolenaar217ad922005-03-20 22:37:15 +00004403 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004404# ifdef FEAT_SPELL
4405 has_spell ? &can_spell :
4406# endif
4407 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004410 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004411 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004412 has_syntax = FALSE;
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 else
4415 did_emsg = save_did_emsg;
4416
4417 /* Need to get the line again, a multi-line regexp may
4418 * have made it invalid. */
4419 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4420 ptr = line + v;
4421
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004422 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004424 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004425 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004426# ifdef FEAT_CONCEAL
4427 /* no concealing past the end of the line, it interferes
4428 * with line highlighting */
4429 if (c == NUL)
4430 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004431 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004432 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004435#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004436
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004437#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004438 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004439 * Only do this when there is no syntax highlighting, the
4440 * @Spell cluster is not used or the current syntax item
4441 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004442 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004443 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004444 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004445# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004446 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004447 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004448# endif
4449 if (c != 0 && (
4450# ifdef FEAT_SYN_HL
4451 !has_syntax ||
4452# endif
4453 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004454 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004455 char_u *prev_ptr, *p;
4456 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004457 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004458# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004459 if (has_mbyte)
4460 {
4461 prev_ptr = ptr - mb_l;
4462 v -= mb_l - 1;
4463 }
4464 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004465# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004466 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004467
4468 /* Use nextline[] if possible, it has the start of the
4469 * next line concatenated. */
4470 if ((prev_ptr - line) - nextlinecol >= 0)
4471 p = nextline + (prev_ptr - line) - nextlinecol;
4472 else
4473 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004474 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004475 len = spell_check(wp, p, &spell_hlf, &cap_col,
4476 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004477 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004478
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004479 /* In Insert mode only highlight a word that
4480 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004481 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004482 && (State & INSERT) != 0
4483 && wp->w_cursor.lnum == lnum
4484 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004485 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004486 && wp->w_cursor.col < (colnr_T)word_end)
4487 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004488 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004489 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004490 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004491
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004492 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004493 && (p - nextline) + len > nextline_idx)
4494 {
4495 /* Remember that the good word continues at the
4496 * start of the next line. */
4497 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004498 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004499 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004500
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004501 /* Turn index into actual attributes. */
4502 if (spell_hlf != HLF_COUNT)
4503 spell_attr = highlight_attr[spell_hlf];
4504
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004505 if (cap_col > 0)
4506 {
4507 if (p != prev_ptr
4508 && (p - nextline) + cap_col >= nextline_idx)
4509 {
4510 /* Remember that the word in the next line
4511 * must start with a capital. */
4512 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004513 cap_col = (int)((p - nextline) + cap_col
4514 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004515 }
4516 else
4517 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004518 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004519 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004520 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004521 }
4522 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004523 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004524 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004525 char_attr = hl_combine_attr(char_attr, spell_attr);
4526 else
4527 char_attr = hl_combine_attr(spell_attr, char_attr);
4528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529#endif
4530#ifdef FEAT_LINEBREAK
4531 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004532 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004534 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004536# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004537 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004538# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004539 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004541 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004543 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004544
Bram Moolenaar597a4222014-06-25 14:39:50 +02004545 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004546 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004547 NULL) - 1;
Bram Moolenaara3650912014-11-19 13:21:57 +01004548 if (c == TAB && n_extra + col > W_WIDTH(wp))
4549 n_extra = (int)wp->w_buffer->b_p_ts
4550 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4551
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004552# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004553 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004554# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004558 {
4559#ifdef FEAT_CONCEAL
4560 if (c == TAB)
4561 /* See "Tab alignment" below. */
4562 FIX_FOR_BOGUSCOLS;
4563#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004564 if (!wp->w_p_list)
4565 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568#endif
4569
4570 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4571 {
4572 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004573 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 n_attr = 1;
4576 extra_attr = hl_attr(HLF_8);
4577 saved_attr2 = char_attr; /* save current attr */
4578 }
4579#ifdef FEAT_MBYTE
4580 mb_c = c;
4581 if (enc_utf8 && (*mb_char2len)(c) > 1)
4582 {
4583 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004584 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004585 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 else
4588 mb_utf8 = FALSE;
4589#endif
4590 }
4591 }
4592
4593 /*
4594 * Handling of non-printable characters.
4595 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004596 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 /*
4599 * when getting a character from the file, we may have to
4600 * turn it into something else on the way to putting it
4601 * into "ScreenLines".
4602 */
4603 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4604 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004605 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004606 long vcol_adjusted = vcol; /* removed showbreak length */
4607#ifdef FEAT_LINEBREAK
4608 /* only adjust the tab_len, when at the first column
4609 * after the showbreak value was drawn */
4610 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4611 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004614 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaard574ea22015-01-14 19:35:14 +01004615 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4616
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004617#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004618 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004619#endif
4620 /* tab amount depends on current column */
4621 n_extra = tab_len;
4622#ifdef FEAT_LINEBREAK
4623 else
4624 {
4625 char_u *p;
4626 int len = n_extra;
4627 int i;
4628 int saved_nextra = n_extra;
4629
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004630#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004631 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004632 /* there are characters to conceal */
4633 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004634 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4635 */
4636 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4637 && n_extra > tab_len)
4638 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004639#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004640
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004641 /* if n_extra > 0, it gives the number of chars, to
4642 * use for a tab, else we need to calculate the width
4643 * for a tab */
4644#ifdef FEAT_MBYTE
4645 len = (tab_len * mb_char2len(lcs_tab2));
4646 if (n_extra > 0)
4647 len += n_extra - tab_len;
4648#endif
4649 c = lcs_tab1;
4650 p = alloc((unsigned)(len + 1));
4651 vim_memset(p, ' ', len);
4652 p[len] = NUL;
4653 p_extra_free = p;
4654 for (i = 0; i < tab_len; i++)
4655 {
4656#ifdef FEAT_MBYTE
4657 mb_char2bytes(lcs_tab2, p);
4658 p += mb_char2len(lcs_tab2);
4659 n_extra += mb_char2len(lcs_tab2)
4660 - (saved_nextra > 0 ? 1 : 0);
4661#else
4662 p[i] = lcs_tab2;
4663#endif
4664 }
4665 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004666#ifdef FEAT_CONCEAL
4667 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4668 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004669 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004670 n_extra -= vcol_off;
4671#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004672 }
4673#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004674#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004675 {
4676 int vc_saved = vcol_off;
4677
4678 /* Tab alignment should be identical regardless of
4679 * 'conceallevel' value. So tab compensates of all
4680 * previous concealed characters, and thus resets
4681 * vcol_off and boguscols accumulated so far in the
4682 * line. Note that the tab can be longer than
4683 * 'tabstop' when there are concealed characters. */
4684 FIX_FOR_BOGUSCOLS;
4685
4686 /* Make sure, the highlighting for the tab char will be
4687 * correctly set further below (effectively reverts the
4688 * FIX_FOR_BOGSUCOLS macro */
4689 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004690 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004691 tab_len += vc_saved;
4692 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#ifdef FEAT_MBYTE
4695 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4696#endif
4697 if (wp->w_p_list)
4698 {
4699 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004700#ifdef FEAT_LINEBREAK
4701 if (wp->w_p_lbr)
4702 c_extra = NUL; /* using p_extra from above */
4703 else
4704#endif
4705 c_extra = lcs_tab2;
4706 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 extra_attr = hl_attr(HLF_8);
4708 saved_attr2 = char_attr; /* save current attr */
4709#ifdef FEAT_MBYTE
4710 mb_c = c;
4711 if (enc_utf8 && (*mb_char2len)(c) > 1)
4712 {
4713 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004714 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004715 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717#endif
4718 }
4719 else
4720 {
4721 c_extra = ' ';
4722 c = ' ';
4723 }
4724 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004725 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004726 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004727 || ((fromcol >= 0 || fromcol_prev >= 0)
4728 && tocol > vcol
4729 && VIsual_mode != Ctrl_V
4730 && (
4731# ifdef FEAT_RIGHTLEFT
4732 wp->w_p_rl ? (col >= 0) :
4733# endif
4734 (col < W_WIDTH(wp)))
4735 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004736 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004737 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004738 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004740 /* Display a '$' after the line or highlight an extra
4741 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4743 /* For a diff line the highlighting continues after the
4744 * "$". */
4745 if (
4746# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004747 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748# ifdef LINE_ATTR
4749 &&
4750# endif
4751# endif
4752# ifdef LINE_ATTR
4753 line_attr == 0
4754# endif
4755 )
4756#endif
4757 {
4758#ifdef FEAT_VIRTUALEDIT
4759 /* In virtualedit, visual selections may extend
4760 * beyond end of line. */
4761 if (area_highlighting && virtual_active()
4762 && tocol != MAXCOL && vcol < tocol)
4763 n_extra = 0;
4764 else
4765#endif
4766 {
4767 p_extra = at_end_str;
4768 n_extra = 1;
4769 c_extra = NUL;
4770 }
4771 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02004772 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004773 c = lcs_eol;
4774 else
4775 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 lcs_eol_one = -1;
4777 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004778 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 extra_attr = hl_attr(HLF_AT);
4781 n_attr = 1;
4782 }
4783#ifdef FEAT_MBYTE
4784 mb_c = c;
4785 if (enc_utf8 && (*mb_char2len)(c) > 1)
4786 {
4787 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004788 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004789 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else
4792 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4793#endif
4794 }
4795 else if (c != NUL)
4796 {
4797 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004798 if (n_extra == 0)
4799 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800#ifdef FEAT_RIGHTLEFT
4801 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4802 rl_mirror(p_extra); /* reverse "<12>" */
4803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004805#ifdef FEAT_LINEBREAK
4806 if (wp->w_p_lbr)
4807 {
4808 char_u *p;
4809
4810 c = *p_extra;
4811 p = alloc((unsigned)n_extra + 1);
4812 vim_memset(p, ' ', n_extra);
4813 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4814 p[n_extra] = NUL;
4815 p_extra_free = p_extra = p;
4816 }
4817 else
4818#endif
4819 {
4820 n_extra = byte2cells(c) - 1;
4821 c = *p_extra++;
4822 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004823 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 n_attr = n_extra + 1;
4826 extra_attr = hl_attr(HLF_8);
4827 saved_attr2 = char_attr; /* save current attr */
4828 }
4829#ifdef FEAT_MBYTE
4830 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4831#endif
4832 }
4833#ifdef FEAT_VIRTUALEDIT
4834 else if (VIsual_active
4835 && (VIsual_mode == Ctrl_V
4836 || VIsual_mode == 'v')
4837 && virtual_active()
4838 && tocol != MAXCOL
4839 && vcol < tocol
4840 && (
4841# ifdef FEAT_RIGHTLEFT
4842 wp->w_p_rl ? (col >= 0) :
4843# endif
4844 (col < W_WIDTH(wp))))
4845 {
4846 c = ' ';
4847 --ptr; /* put it back at the NUL */
4848 }
4849#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004850#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 else if ((
4852# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004853 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 ) && (
4857# ifdef FEAT_RIGHTLEFT
4858 wp->w_p_rl ? (col >= 0) :
4859# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004860 (col
4861# ifdef FEAT_CONCEAL
4862 - boguscols
4863# endif
4864 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
4866 /* Highlight until the right side of the window */
4867 c = ' ';
4868 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004869
4870 /* Remember we do the char for line highlighting. */
4871 ++did_line_attr;
4872
4873 /* don't do search HL for the rest of the line */
4874 if (line_attr != 0 && char_attr == search_attr && col > 0)
4875 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876# ifdef FEAT_DIFF
4877 if (diff_hlf == HLF_TXD)
4878 {
4879 diff_hlf = HLF_CHD;
4880 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004883 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4884 char_attr = hl_combine_attr(char_attr,
4885 hl_attr(HLF_CUL));
4886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888# endif
4889 }
4890#endif
4891 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004892
4893#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004894 if ( wp->w_p_cole > 0
4895 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02004896 conceal_cursor_line(wp) )
4897 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004898 && !(lnum_in_visual_area
4899 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004900 {
4901 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004902 if (prev_syntax_id != syntax_seqnr
Bram Moolenaar6561d522015-07-21 15:48:27 +02004903 && (syn_get_sub_char() != NUL || match_conc
4904 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004905 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004906 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004907 /* First time at this concealed item: display one
4908 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02004909 if (match_conc)
4910 c = match_conc;
4911 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004912 c = syn_get_sub_char();
4913 else if (lcs_conceal != NUL)
4914 c = lcs_conceal;
4915 else
4916 c = ' ';
4917
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004918 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004919
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004920 if (n_extra > 0)
4921 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004922 vcol += n_extra;
4923 if (wp->w_p_wrap && n_extra > 0)
4924 {
4925# ifdef FEAT_RIGHTLEFT
4926 if (wp->w_p_rl)
4927 {
4928 col -= n_extra;
4929 boguscols -= n_extra;
4930 }
4931 else
4932# endif
4933 {
4934 boguscols += n_extra;
4935 col += n_extra;
4936 }
4937 }
4938 n_extra = 0;
4939 n_attr = 0;
4940 }
4941 else if (n_skip == 0)
4942 {
4943 is_concealing = TRUE;
4944 n_skip = 1;
4945 }
4946# ifdef FEAT_MBYTE
4947 mb_c = c;
4948 if (enc_utf8 && (*mb_char2len)(c) > 1)
4949 {
4950 mb_utf8 = TRUE;
4951 u8cc[0] = 0;
4952 c = 0xc0;
4953 }
4954 else
4955 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4956# endif
4957 }
4958 else
4959 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004960 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004961 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004962 }
4963#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004966#ifdef FEAT_CONCEAL
4967 /* In the cursor line and we may be concealing characters: correct
4968 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004969 if (!did_wcol && draw_state == WL_LINE
4970 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004971 && conceal_cursor_line(wp)
4972 && (int)wp->w_virtcol <= vcol + n_skip)
4973 {
4974 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004975 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004976 did_wcol = TRUE;
4977 }
4978#endif
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /* Don't override visual selection highlighting. */
4981 if (n_attr > 0
4982 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004983 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 char_attr = extra_attr;
4985
Bram Moolenaar81695252004-12-29 20:58:21 +00004986#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 /* XIM don't send preedit_start and preedit_end, but they send
4988 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4989 * im_is_preediting() here. */
4990 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004991 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 && (State & INSERT)
4993 && !p_imdisable
4994 && im_is_preediting()
4995 && draw_state == WL_LINE)
4996 {
4997 colnr_T tcol;
4998
4999 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005000 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 else
5002 tcol = preedit_end_col;
5003 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5004 {
5005 if (feedback_old_attr < 0)
5006 {
5007 feedback_col = 0;
5008 feedback_old_attr = char_attr;
5009 }
5010 char_attr = im_get_feedback_attr(feedback_col);
5011 if (char_attr < 0)
5012 char_attr = feedback_old_attr;
5013 feedback_col++;
5014 }
5015 else if (feedback_old_attr >= 0)
5016 {
5017 char_attr = feedback_old_attr;
5018 feedback_old_attr = -1;
5019 feedback_col = 0;
5020 }
5021 }
5022#endif
5023 /*
5024 * Handle the case where we are in column 0 but not on the first
5025 * character of the line and the user wants us to show us a
5026 * special character (via 'listchars' option "precedes:<char>".
5027 */
5028 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005029 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5031#ifdef FEAT_DIFF
5032 && filler_todo <= 0
5033#endif
5034 && draw_state > WL_NR
5035 && c != NUL)
5036 {
5037 c = lcs_prec;
5038 lcs_prec_todo = NUL;
5039#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005040 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5041 {
5042 /* Double-width character being overwritten by the "precedes"
5043 * character, need to fill up half the character. */
5044 c_extra = MB_FILLER_CHAR;
5045 n_extra = 1;
5046 n_attr = 2;
5047 extra_attr = hl_attr(HLF_AT);
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 mb_c = c;
5050 if (enc_utf8 && (*mb_char2len)(c) > 1)
5051 {
5052 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005053 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005054 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
5056 else
5057 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5058#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005059 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
5061 saved_attr3 = char_attr; /* save current attr */
5062 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
5063 n_attr3 = 1;
5064 }
5065 }
5066
5067 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005068 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005070 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005071#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005072 || did_line_attr == 1
5073#endif
5074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005076#ifdef FEAT_SEARCH_EXTRA
5077 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005078
5079 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005080 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005081 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005082#endif
5083
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005084 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 * highlight match at end of line. If it's beyond the last
5086 * char on the screen, just overwrite that one (tricky!) Not
5087 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005088#ifdef FEAT_SEARCH_EXTRA
5089 prevcol_hl_flag = FALSE;
5090 if (prevcol == (long)search_hl.startcol)
5091 prevcol_hl_flag = TRUE;
5092 else
5093 {
5094 cur = wp->w_match_head;
5095 while (cur != NULL)
5096 {
5097 if (prevcol == (long)cur->hl.startcol)
5098 {
5099 prevcol_hl_flag = TRUE;
5100 break;
5101 }
5102 cur = cur->next;
5103 }
5104 }
5105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005107 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005108 && (VIsual_mode != Ctrl_V
5109 || lnum == VIsual.lnum
5110 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005111 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#ifdef FEAT_SEARCH_EXTRA
5113 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005114 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005115# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005116 && did_line_attr <= 1
5117# endif
5118 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#endif
5120 ))
5121 {
5122 int n = 0;
5123
5124#ifdef FEAT_RIGHTLEFT
5125 if (wp->w_p_rl)
5126 {
5127 if (col < 0)
5128 n = 1;
5129 }
5130 else
5131#endif
5132 {
5133 if (col >= W_WIDTH(wp))
5134 n = -1;
5135 }
5136 if (n != 0)
5137 {
5138 /* At the window boundary, highlight the last character
5139 * instead (better than nothing). */
5140 off += n;
5141 col += n;
5142 }
5143 else
5144 {
5145 /* Add a blank character to highlight. */
5146 ScreenLines[off] = ' ';
5147#ifdef FEAT_MBYTE
5148 if (enc_utf8)
5149 ScreenLinesUC[off] = 0;
5150#endif
5151 }
5152#ifdef FEAT_SEARCH_EXTRA
5153 if (area_attr == 0)
5154 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005155 /* Use attributes from match with highest priority among
5156 * 'search_hl' and the match list. */
5157 char_attr = search_hl.attr;
5158 cur = wp->w_match_head;
5159 shl_flag = FALSE;
5160 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005161 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005162 if (shl_flag == FALSE
5163 && ((cur != NULL
5164 && cur->priority > SEARCH_HL_PRIORITY)
5165 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005166 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005167 shl = &search_hl;
5168 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005169 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005170 else
5171 shl = &cur->hl;
5172 if ((ptr - line) - 1 == (long)shl->startcol)
5173 char_attr = shl->attr;
5174 if (shl != &search_hl && cur != NULL)
5175 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178#endif
5179 ScreenAttrs[off] = char_attr;
5180#ifdef FEAT_RIGHTLEFT
5181 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005184 --off;
5185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 else
5187#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005190 ++off;
5191 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005192 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005193#ifdef FEAT_SYN_HL
5194 eol_hl_off = 1;
5195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar91170f82006-05-05 21:15:17 +00005199 /*
5200 * At end of the text line.
5201 */
5202 if (c == NUL)
5203 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005204#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005205 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5206 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005207 {
5208 /* highlight last char after line */
5209 --col;
5210 --off;
5211 --vcol;
5212 }
5213
Bram Moolenaar1a384422010-07-14 19:53:30 +02005214 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005215 if (wp->w_p_wrap)
5216 v = wp->w_skipcol;
5217 else
5218 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005219
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005220 /* check if line ends before left margin */
5221 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005222 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005223#ifdef FEAT_CONCEAL
5224 /* Get rid of the boguscols now, we want to draw until the right
5225 * edge for 'cursorcolumn'. */
5226 col -= boguscols;
5227 boguscols = 0;
5228#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005229
5230 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005231 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005232
5233 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005234 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5235 && (int)wp->w_virtcol <
5236 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005237 && lnum != wp->w_cursor.lnum)
5238 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005239# ifdef FEAT_RIGHTLEFT
5240 && !wp->w_p_rl
5241# endif
5242 )
5243 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005244 int rightmost_vcol = 0;
5245 int i;
5246
5247 if (wp->w_p_cuc)
5248 rightmost_vcol = wp->w_virtcol;
5249 if (draw_color_col)
5250 /* determine rightmost colorcolumn to possibly draw */
5251 for (i = 0; color_cols[i] >= 0; ++i)
5252 if (rightmost_vcol < color_cols[i])
5253 rightmost_vcol = color_cols[i];
5254
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005255 while (col < W_WIDTH(wp))
5256 {
5257 ScreenLines[off] = ' ';
5258#ifdef FEAT_MBYTE
5259 if (enc_utf8)
5260 ScreenLinesUC[off] = 0;
5261#endif
5262 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005263 if (draw_color_col)
5264 draw_color_col = advance_color_col(VCOL_HLC,
5265 &color_cols);
5266
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005267 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005268 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005269 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005270 ScreenAttrs[off++] = hl_attr(HLF_MC);
5271 else
5272 ScreenAttrs[off++] = 0;
5273
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005274 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005275 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005276
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005277 ++vcol;
5278 }
5279 }
5280#endif
5281
Bram Moolenaar860cae12010-06-05 23:22:07 +02005282 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5283 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 row++;
5285
5286 /*
5287 * Update w_cline_height and w_cline_folded if the cursor line was
5288 * updated (saves a call to plines() later).
5289 */
5290 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5291 {
5292 curwin->w_cline_row = startrow;
5293 curwin->w_cline_height = row - startrow;
5294#ifdef FEAT_FOLDING
5295 curwin->w_cline_folded = FALSE;
5296#endif
5297 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5298 }
5299
5300 break;
5301 }
5302
5303 /* line continues beyond line end */
5304 if (lcs_ext
5305 && !wp->w_p_wrap
5306#ifdef FEAT_DIFF
5307 && filler_todo <= 0
5308#endif
5309 && (
5310#ifdef FEAT_RIGHTLEFT
5311 wp->w_p_rl ? col == 0 :
5312#endif
5313 col == W_WIDTH(wp) - 1)
5314 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005315 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5317 {
5318 c = lcs_ext;
5319 char_attr = hl_attr(HLF_AT);
5320#ifdef FEAT_MBYTE
5321 mb_c = c;
5322 if (enc_utf8 && (*mb_char2len)(c) > 1)
5323 {
5324 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005325 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005326 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
5328 else
5329 mb_utf8 = FALSE;
5330#endif
5331 }
5332
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005333#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005334 /* advance to the next 'colorcolumn' */
5335 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005336 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005337
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005338 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005339 * highlight the cursor position itself.
5340 * Also highlight the 'colorcolumn' if it is different than
5341 * 'cursorcolumn' */
5342 vcol_save_attr = -1;
5343 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005344 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005345 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005346 && lnum != wp->w_cursor.lnum)
5347 {
5348 vcol_save_attr = char_attr;
5349 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5350 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005351 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005352 {
5353 vcol_save_attr = char_attr;
5354 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5355 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005356 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005357#endif
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 /*
5360 * Store character to be displayed.
5361 * Skip characters that are left of the screen for 'nowrap'.
5362 */
5363 vcol_prev = vcol;
5364 if (draw_state < WL_LINE || n_skip <= 0)
5365 {
5366 /*
5367 * Store the character.
5368 */
5369#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5370 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5371 {
5372 /* A double-wide character is: put first halve in left cell. */
5373 --off;
5374 --col;
5375 }
5376#endif
5377 ScreenLines[off] = c;
5378#ifdef FEAT_MBYTE
5379 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005380 {
5381 if ((mb_c & 0xff00) == 0x8e00)
5382 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 else if (enc_utf8)
5386 {
5387 if (mb_utf8)
5388 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389 int i;
5390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005392 if ((c & 0xff) == 0)
5393 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005394 for (i = 0; i < Screen_mco; ++i)
5395 {
5396 ScreenLinesC[i][off] = u8cc[i];
5397 if (u8cc[i] == 0)
5398 break;
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401 else
5402 ScreenLinesUC[off] = 0;
5403 }
5404 if (multi_attr)
5405 {
5406 ScreenAttrs[off] = multi_attr;
5407 multi_attr = 0;
5408 }
5409 else
5410#endif
5411 ScreenAttrs[off] = char_attr;
5412
5413#ifdef FEAT_MBYTE
5414 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5415 {
5416 /* Need to fill two screen columns. */
5417 ++off;
5418 ++col;
5419 if (enc_utf8)
5420 /* UTF-8: Put a 0 in the second screen char. */
5421 ScreenLines[off] = 0;
5422 else
5423 /* DBCS: Put second byte in the second screen char. */
5424 ScreenLines[off] = mb_c & 0xff;
5425 ++vcol;
5426 /* When "tocol" is halfway a character, set it to the end of
5427 * the character, otherwise highlighting won't stop. */
5428 if (tocol == vcol)
5429 ++tocol;
5430#ifdef FEAT_RIGHTLEFT
5431 if (wp->w_p_rl)
5432 {
5433 /* now it's time to backup one cell */
5434 --off;
5435 --col;
5436 }
5437#endif
5438 }
5439#endif
5440#ifdef FEAT_RIGHTLEFT
5441 if (wp->w_p_rl)
5442 {
5443 --off;
5444 --col;
5445 }
5446 else
5447#endif
5448 {
5449 ++off;
5450 ++col;
5451 }
5452 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005453#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005454 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005455 {
5456 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005457 ++vcol_off;
5458 if (n_extra > 0)
5459 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005460 if (wp->w_p_wrap)
5461 {
5462 /*
5463 * Special voodoo required if 'wrap' is on.
5464 *
5465 * Advance the column indicator to force the line
5466 * drawing to wrap early. This will make the line
5467 * take up the same screen space when parts are concealed,
5468 * so that cursor line computations aren't messed up.
5469 *
5470 * To avoid the fictitious advance of 'col' causing
5471 * trailing junk to be written out of the screen line
5472 * we are building, 'boguscols' keeps track of the number
5473 * of bad columns we have advanced.
5474 */
5475 if (n_extra > 0)
5476 {
5477 vcol += n_extra;
5478# ifdef FEAT_RIGHTLEFT
5479 if (wp->w_p_rl)
5480 {
5481 col -= n_extra;
5482 boguscols -= n_extra;
5483 }
5484 else
5485# endif
5486 {
5487 col += n_extra;
5488 boguscols += n_extra;
5489 }
5490 n_extra = 0;
5491 n_attr = 0;
5492 }
5493
5494
5495# ifdef FEAT_MBYTE
5496 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5497 {
5498 /* Need to fill two screen columns. */
5499# ifdef FEAT_RIGHTLEFT
5500 if (wp->w_p_rl)
5501 {
5502 --boguscols;
5503 --col;
5504 }
5505 else
5506# endif
5507 {
5508 ++boguscols;
5509 ++col;
5510 }
5511 }
5512# endif
5513
5514# ifdef FEAT_RIGHTLEFT
5515 if (wp->w_p_rl)
5516 {
5517 --boguscols;
5518 --col;
5519 }
5520 else
5521# endif
5522 {
5523 ++boguscols;
5524 ++col;
5525 }
5526 }
5527 else
5528 {
5529 if (n_extra > 0)
5530 {
5531 vcol += n_extra;
5532 n_extra = 0;
5533 n_attr = 0;
5534 }
5535 }
5536
5537 }
5538#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else
5540 --n_skip;
5541
Bram Moolenaar64486672010-05-16 15:46:46 +02005542 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5543 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005544 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#ifdef FEAT_DIFF
5546 && filler_todo <= 0
5547#endif
5548 )
5549 ++vcol;
5550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005551#ifdef FEAT_SYN_HL
5552 if (vcol_save_attr >= 0)
5553 char_attr = vcol_save_attr;
5554#endif
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 /* restore attributes after "predeces" in 'listchars' */
5557 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5558 char_attr = saved_attr3;
5559
5560 /* restore attributes after last 'listchars' or 'number' char */
5561 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5562 char_attr = saved_attr2;
5563
5564 /*
5565 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005566 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 */
5568 if ((
5569#ifdef FEAT_RIGHTLEFT
5570 wp->w_p_rl ? (col < 0) :
5571#endif
5572 (col >= W_WIDTH(wp)))
5573 && (*ptr != NUL
5574#ifdef FEAT_DIFF
5575 || filler_todo > 0
5576#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005577 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5579 )
5580 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005581#ifdef FEAT_CONCEAL
5582 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5583 (int)W_WIDTH(wp), wp->w_p_rl);
5584 boguscols = 0;
5585#else
5586 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5587 (int)W_WIDTH(wp), wp->w_p_rl);
5588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 ++row;
5590 ++screen_row;
5591
5592 /* When not wrapping and finished diff lines, or when displayed
5593 * '$' and highlighting until last column, break here. */
5594 if ((!wp->w_p_wrap
5595#ifdef FEAT_DIFF
5596 && filler_todo <= 0
5597#endif
5598 ) || lcs_eol_one == -1)
5599 break;
5600
5601 /* When the window is too narrow draw all "@" lines. */
5602 if (draw_state != WL_LINE
5603#ifdef FEAT_DIFF
5604 && filler_todo <= 0
5605#endif
5606 )
5607 {
5608 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5609#ifdef FEAT_VERTSPLIT
5610 draw_vsep_win(wp, row);
5611#endif
5612 row = endrow;
5613 }
5614
5615 /* When line got too long for screen break here. */
5616 if (row == endrow)
5617 {
5618 ++row;
5619 break;
5620 }
5621
5622 if (screen_cur_row == screen_row - 1
5623#ifdef FEAT_DIFF
5624 && filler_todo <= 0
5625#endif
5626 && W_WIDTH(wp) == Columns)
5627 {
5628 /* Remember that the line wraps, used for modeless copy. */
5629 LineWraps[screen_row - 1] = TRUE;
5630
5631 /*
5632 * Special trick to make copy/paste of wrapped lines work with
5633 * xterm/screen: write an extra character beyond the end of
5634 * the line. This will work with all terminal types
5635 * (regardless of the xn,am settings).
5636 * Only do this on a fast tty.
5637 * Only do this if the cursor is on the current line
5638 * (something has been written in it).
5639 * Don't do this for the GUI.
5640 * Don't do this for double-width characters.
5641 * Don't do this for a window not at the right screen border.
5642 */
5643 if (p_tf
5644#ifdef FEAT_GUI
5645 && !gui.in_use
5646#endif
5647#ifdef FEAT_MBYTE
5648 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005649 && ((*mb_off2cells)(LineOffset[screen_row],
5650 LineOffset[screen_row] + screen_Columns)
5651 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005653 + (int)Columns - 2,
5654 LineOffset[screen_row] + screen_Columns)
5655 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#endif
5657 )
5658 {
5659 /* First make sure we are at the end of the screen line,
5660 * then output the same character again to let the
5661 * terminal know about the wrap. If the terminal doesn't
5662 * auto-wrap, we overwrite the character. */
5663 if (screen_cur_col != W_WIDTH(wp))
5664 screen_char(LineOffset[screen_row - 1]
5665 + (unsigned)Columns - 1,
5666 screen_row - 1, (int)(Columns - 1));
5667
5668#ifdef FEAT_MBYTE
5669 /* When there is a multi-byte character, just output a
5670 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005671 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5672 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 out_char(' ');
5674 else
5675#endif
5676 out_char(ScreenLines[LineOffset[screen_row - 1]
5677 + (Columns - 1)]);
5678 /* force a redraw of the first char on the next line */
5679 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5680 screen_start(); /* don't know where cursor is now */
5681 }
5682 }
5683
5684 col = 0;
5685 off = (unsigned)(current_ScreenLine - ScreenLines);
5686#ifdef FEAT_RIGHTLEFT
5687 if (wp->w_p_rl)
5688 {
5689 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5690 off += col;
5691 }
5692#endif
5693
5694 /* reset the drawing state for the start of a wrapped line */
5695 draw_state = WL_START;
5696 saved_n_extra = n_extra;
5697 saved_p_extra = p_extra;
5698 saved_c_extra = c_extra;
5699 saved_char_attr = char_attr;
5700 n_extra = 0;
5701 lcs_prec_todo = lcs_prec;
5702#ifdef FEAT_LINEBREAK
5703# ifdef FEAT_DIFF
5704 if (filler_todo <= 0)
5705# endif
5706 need_showbreak = TRUE;
5707#endif
5708#ifdef FEAT_DIFF
5709 --filler_todo;
5710 /* When the filler lines are actually below the last line of the
5711 * file, don't draw the line itself, break here. */
5712 if (filler_todo == 0 && wp->w_botfill)
5713 break;
5714#endif
5715 }
5716
5717 } /* for every character in the line */
5718
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005719#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005720 /* After an empty line check first word for capital. */
5721 if (*skipwhite(line) == NUL)
5722 {
5723 capcol_lnum = lnum + 1;
5724 cap_col = 0;
5725 }
5726#endif
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 return row;
5729}
5730
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005731#ifdef FEAT_MBYTE
5732static int comp_char_differs __ARGS((int, int));
5733
5734/*
5735 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005736 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005737 */
5738 static int
5739comp_char_differs(off_from, off_to)
5740 int off_from;
5741 int off_to;
5742{
5743 int i;
5744
5745 for (i = 0; i < Screen_mco; ++i)
5746 {
5747 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5748 return TRUE;
5749 if (ScreenLinesC[i][off_from] == 0)
5750 break;
5751 }
5752 return FALSE;
5753}
5754#endif
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756/*
5757 * Check whether the given character needs redrawing:
5758 * - the (first byte of the) character is different
5759 * - the attributes are different
5760 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005761 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 */
5763 static int
5764char_needs_redraw(off_from, off_to, cols)
5765 int off_from;
5766 int off_to;
5767 int cols;
5768{
5769 if (cols > 0
5770 && ((ScreenLines[off_from] != ScreenLines[off_to]
5771 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5772
5773#ifdef FEAT_MBYTE
5774 || (enc_dbcs != 0
5775 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5776 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5777 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5778 : (cols > 1 && ScreenLines[off_from + 1]
5779 != ScreenLines[off_to + 1])))
5780 || (enc_utf8
5781 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5782 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005783 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005784 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5785 && ScreenLines[off_from + 1]
5786 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787#endif
5788 ))
5789 return TRUE;
5790 return FALSE;
5791}
5792
5793/*
5794 * Move one "cooked" screen line to the screen, but only the characters that
5795 * have actually changed. Handle insert/delete character.
5796 * "coloff" gives the first column on the screen for this line.
5797 * "endcol" gives the columns where valid characters are.
5798 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5799 * needs to be cleared, negative otherwise.
5800 * "rlflag" is TRUE in a rightleft window:
5801 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5802 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5803 */
5804 static void
5805screen_line(row, coloff, endcol, clear_width
5806#ifdef FEAT_RIGHTLEFT
5807 , rlflag
5808#endif
5809 )
5810 int row;
5811 int coloff;
5812 int endcol;
5813 int clear_width;
5814#ifdef FEAT_RIGHTLEFT
5815 int rlflag;
5816#endif
5817{
5818 unsigned off_from;
5819 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005820#ifdef FEAT_MBYTE
5821 unsigned max_off_from;
5822 unsigned max_off_to;
5823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 int col = 0;
5825#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5826 int hl;
5827#endif
5828 int force = FALSE; /* force update rest of the line */
5829 int redraw_this /* bool: does character need redraw? */
5830#ifdef FEAT_GUI
5831 = TRUE /* For GUI when while-loop empty */
5832#endif
5833 ;
5834 int redraw_next; /* redraw_this for next character */
5835#ifdef FEAT_MBYTE
5836 int clear_next = FALSE;
5837 int char_cells; /* 1: normal char */
5838 /* 2: occupies two display cells */
5839# define CHAR_CELLS char_cells
5840#else
5841# define CHAR_CELLS 1
5842#endif
5843
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005844 /* Check for illegal row and col, just in case. */
5845 if (row >= Rows)
5846 row = Rows - 1;
5847 if (endcol > Columns)
5848 endcol = Columns;
5849
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850# ifdef FEAT_CLIPBOARD
5851 clip_may_clear_selection(row, row);
5852# endif
5853
5854 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5855 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005856#ifdef FEAT_MBYTE
5857 max_off_from = off_from + screen_Columns;
5858 max_off_to = LineOffset[row] + screen_Columns;
5859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861#ifdef FEAT_RIGHTLEFT
5862 if (rlflag)
5863 {
5864 /* Clear rest first, because it's left of the text. */
5865 if (clear_width > 0)
5866 {
5867 while (col <= endcol && ScreenLines[off_to] == ' '
5868 && ScreenAttrs[off_to] == 0
5869# ifdef FEAT_MBYTE
5870 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5871# endif
5872 )
5873 {
5874 ++off_to;
5875 ++col;
5876 }
5877 if (col <= endcol)
5878 screen_fill(row, row + 1, col + coloff,
5879 endcol + coloff + 1, ' ', ' ', 0);
5880 }
5881 col = endcol + 1;
5882 off_to = LineOffset[row] + col + coloff;
5883 off_from += col;
5884 endcol = (clear_width > 0 ? clear_width : -clear_width);
5885 }
5886#endif /* FEAT_RIGHTLEFT */
5887
5888 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5889
5890 while (col < endcol)
5891 {
5892#ifdef FEAT_MBYTE
5893 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005894 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 else
5896 char_cells = 1;
5897#endif
5898
5899 redraw_this = redraw_next;
5900 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5901 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5902
5903#ifdef FEAT_GUI
5904 /* If the next character was bold, then redraw the current character to
5905 * remove any pixels that might have spilt over into us. This only
5906 * happens in the GUI.
5907 */
5908 if (redraw_next && gui.in_use)
5909 {
5910 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005911 if (hl > HL_ALL)
5912 hl = syn_attr2attr(hl);
5913 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 redraw_this = TRUE;
5915 }
5916#endif
5917
5918 if (redraw_this)
5919 {
5920 /*
5921 * Special handling when 'xs' termcap flag set (hpterm):
5922 * Attributes for characters are stored at the position where the
5923 * cursor is when writing the highlighting code. The
5924 * start-highlighting code must be written with the cursor on the
5925 * first highlighted character. The stop-highlighting code must
5926 * be written with the cursor just after the last highlighted
5927 * character.
5928 * Overwriting a character doesn't remove it's highlighting. Need
5929 * to clear the rest of the line, and force redrawing it
5930 * completely.
5931 */
5932 if ( p_wiv
5933 && !force
5934#ifdef FEAT_GUI
5935 && !gui.in_use
5936#endif
5937 && ScreenAttrs[off_to] != 0
5938 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5939 {
5940 /*
5941 * Need to remove highlighting attributes here.
5942 */
5943 windgoto(row, col + coloff);
5944 out_str(T_CE); /* clear rest of this screen line */
5945 screen_start(); /* don't know where cursor is now */
5946 force = TRUE; /* force redraw of rest of the line */
5947 redraw_next = TRUE; /* or else next char would miss out */
5948
5949 /*
5950 * If the previous character was highlighted, need to stop
5951 * highlighting at this character.
5952 */
5953 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5954 {
5955 screen_attr = ScreenAttrs[off_to - 1];
5956 term_windgoto(row, col + coloff);
5957 screen_stop_highlight();
5958 }
5959 else
5960 screen_attr = 0; /* highlighting has stopped */
5961 }
5962#ifdef FEAT_MBYTE
5963 if (enc_dbcs != 0)
5964 {
5965 /* Check if overwriting a double-byte with a single-byte or
5966 * the other way around requires another character to be
5967 * redrawn. For UTF-8 this isn't needed, because comparing
5968 * ScreenLinesUC[] is sufficient. */
5969 if (char_cells == 1
5970 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005971 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 /* Writing a single-cell character over a double-cell
5974 * character: need to redraw the next cell. */
5975 ScreenLines[off_to + 1] = 0;
5976 redraw_next = TRUE;
5977 }
5978 else if (char_cells == 2
5979 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005980 && (*mb_off2cells)(off_to, max_off_to) == 1
5981 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 /* Writing the second half of a double-cell character over
5984 * a double-cell character: need to redraw the second
5985 * cell. */
5986 ScreenLines[off_to + 2] = 0;
5987 redraw_next = TRUE;
5988 }
5989
5990 if (enc_dbcs == DBCS_JPNU)
5991 ScreenLines2[off_to] = ScreenLines2[off_from];
5992 }
5993 /* When writing a single-width character over a double-width
5994 * character and at the end of the redrawn text, need to clear out
5995 * the right halve of the old character.
5996 * Also required when writing the right halve of a double-width
5997 * char over the left halve of an existing one. */
5998 if (has_mbyte && col + char_cells == endcol
5999 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006000 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006002 && (*mb_off2cells)(off_to, max_off_to) == 1
6003 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 clear_next = TRUE;
6005#endif
6006
6007 ScreenLines[off_to] = ScreenLines[off_from];
6008#ifdef FEAT_MBYTE
6009 if (enc_utf8)
6010 {
6011 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6012 if (ScreenLinesUC[off_from] != 0)
6013 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006014 int i;
6015
6016 for (i = 0; i < Screen_mco; ++i)
6017 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
6019 }
6020 if (char_cells == 2)
6021 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6022#endif
6023
6024#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006025 /* The bold trick makes a single column of pixels appear in the
6026 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 * character should be redrawn too. This happens for our own GUI
6028 * and for some xterms. */
6029 if (
6030# ifdef FEAT_GUI
6031 gui.in_use
6032# endif
6033# if defined(FEAT_GUI) && defined(UNIX)
6034 ||
6035# endif
6036# ifdef UNIX
6037 term_is_xterm
6038# endif
6039 )
6040 {
6041 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006042 if (hl > HL_ALL)
6043 hl = syn_attr2attr(hl);
6044 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 redraw_next = TRUE;
6046 }
6047#endif
6048 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6049#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006050 /* For simplicity set the attributes of second half of a
6051 * double-wide character equal to the first half. */
6052 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006054
6055 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 else
6058#endif
6059 screen_char(off_to, row, col + coloff);
6060 }
6061 else if ( p_wiv
6062#ifdef FEAT_GUI
6063 && !gui.in_use
6064#endif
6065 && col + coloff > 0)
6066 {
6067 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6068 {
6069 /*
6070 * Don't output stop-highlight when moving the cursor, it will
6071 * stop the highlighting when it should continue.
6072 */
6073 screen_attr = 0;
6074 }
6075 else if (screen_attr != 0)
6076 screen_stop_highlight();
6077 }
6078
6079 off_to += CHAR_CELLS;
6080 off_from += CHAR_CELLS;
6081 col += CHAR_CELLS;
6082 }
6083
6084#ifdef FEAT_MBYTE
6085 if (clear_next)
6086 {
6087 /* Clear the second half of a double-wide character of which the left
6088 * half was overwritten with a single-wide character. */
6089 ScreenLines[off_to] = ' ';
6090 if (enc_utf8)
6091 ScreenLinesUC[off_to] = 0;
6092 screen_char(off_to, row, col + coloff);
6093 }
6094#endif
6095
6096 if (clear_width > 0
6097#ifdef FEAT_RIGHTLEFT
6098 && !rlflag
6099#endif
6100 )
6101 {
6102#ifdef FEAT_GUI
6103 int startCol = col;
6104#endif
6105
6106 /* blank out the rest of the line */
6107 while (col < clear_width && ScreenLines[off_to] == ' '
6108 && ScreenAttrs[off_to] == 0
6109#ifdef FEAT_MBYTE
6110 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6111#endif
6112 )
6113 {
6114 ++off_to;
6115 ++col;
6116 }
6117 if (col < clear_width)
6118 {
6119#ifdef FEAT_GUI
6120 /*
6121 * In the GUI, clearing the rest of the line may leave pixels
6122 * behind if the first character cleared was bold. Some bold
6123 * fonts spill over the left. In this case we redraw the previous
6124 * character too. If we didn't skip any blanks above, then we
6125 * only redraw if the character wasn't already redrawn anyway.
6126 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006127 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 hl = ScreenAttrs[off_to];
6130 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006131 {
6132 int prev_cells = 1;
6133# ifdef FEAT_MBYTE
6134 if (enc_utf8)
6135 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6136 * that its width is 2. */
6137 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6138 else if (enc_dbcs != 0)
6139 {
6140 /* find previous character by counting from first
6141 * column and get its width. */
6142 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006143 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006144
6145 while (off < off_to)
6146 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006147 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006148 off += prev_cells;
6149 }
6150 }
6151
6152 if (enc_dbcs != 0 && prev_cells > 1)
6153 screen_char_2(off_to - prev_cells, row,
6154 col + coloff - prev_cells);
6155 else
6156# endif
6157 screen_char(off_to - prev_cells, row,
6158 col + coloff - prev_cells);
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161#endif
6162 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6163 ' ', ' ', 0);
6164#ifdef FEAT_VERTSPLIT
6165 off_to += clear_width - col;
6166 col = clear_width;
6167#endif
6168 }
6169 }
6170
6171 if (clear_width > 0)
6172 {
6173#ifdef FEAT_VERTSPLIT
6174 /* For a window that's left of another, draw the separator char. */
6175 if (col + coloff < Columns)
6176 {
6177 int c;
6178
6179 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006180 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006182 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6183 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184# endif
6185 || ScreenAttrs[off_to] != hl)
6186 {
6187 ScreenLines[off_to] = c;
6188 ScreenAttrs[off_to] = hl;
6189# ifdef FEAT_MBYTE
6190 if (enc_utf8)
6191 {
6192 if (c >= 0x80)
6193 {
6194 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006195 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 else
6198 ScreenLinesUC[off_to] = 0;
6199 }
6200# endif
6201 screen_char(off_to, row, col + coloff);
6202 }
6203 }
6204 else
6205#endif
6206 LineWraps[row] = FALSE;
6207 }
6208}
6209
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006212 * Mirror text "str" for right-left displaying.
6213 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006215 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216rl_mirror(str)
6217 char_u *str;
6218{
6219 char_u *p1, *p2;
6220 int t;
6221
6222 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6223 {
6224 t = *p1;
6225 *p1 = *p2;
6226 *p2 = t;
6227 }
6228}
6229#endif
6230
6231#if defined(FEAT_WINDOWS) || defined(PROTO)
6232/*
6233 * mark all status lines for redraw; used after first :cd
6234 */
6235 void
6236status_redraw_all()
6237{
6238 win_T *wp;
6239
6240 for (wp = firstwin; wp; wp = wp->w_next)
6241 if (wp->w_status_height)
6242 {
6243 wp->w_redr_status = TRUE;
6244 redraw_later(VALID);
6245 }
6246}
6247
6248/*
6249 * mark all status lines of the current buffer for redraw
6250 */
6251 void
6252status_redraw_curbuf()
6253{
6254 win_T *wp;
6255
6256 for (wp = firstwin; wp; wp = wp->w_next)
6257 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6258 {
6259 wp->w_redr_status = TRUE;
6260 redraw_later(VALID);
6261 }
6262}
6263
6264/*
6265 * Redraw all status lines that need to be redrawn.
6266 */
6267 void
6268redraw_statuslines()
6269{
6270 win_T *wp;
6271
6272 for (wp = firstwin; wp; wp = wp->w_next)
6273 if (wp->w_redr_status)
6274 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006275 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006276 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277}
6278#endif
6279
6280#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6281/*
6282 * Redraw all status lines at the bottom of frame "frp".
6283 */
6284 void
6285win_redraw_last_status(frp)
6286 frame_T *frp;
6287{
6288 if (frp->fr_layout == FR_LEAF)
6289 frp->fr_win->w_redr_status = TRUE;
6290 else if (frp->fr_layout == FR_ROW)
6291 {
6292 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6293 win_redraw_last_status(frp);
6294 }
6295 else /* frp->fr_layout == FR_COL */
6296 {
6297 frp = frp->fr_child;
6298 while (frp->fr_next != NULL)
6299 frp = frp->fr_next;
6300 win_redraw_last_status(frp);
6301 }
6302}
6303#endif
6304
6305#ifdef FEAT_VERTSPLIT
6306/*
6307 * Draw the verticap separator right of window "wp" starting with line "row".
6308 */
6309 static void
6310draw_vsep_win(wp, row)
6311 win_T *wp;
6312 int row;
6313{
6314 int hl;
6315 int c;
6316
6317 if (wp->w_vsep_width)
6318 {
6319 /* draw the vertical separator right of this window */
6320 c = fillchar_vsep(&hl);
6321 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6322 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6323 c, ' ', hl);
6324 }
6325}
6326#endif
6327
6328#ifdef FEAT_WILDMENU
6329static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006330static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006333 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 */
6335 static int
6336status_match_len(xp, s)
6337 expand_T *xp;
6338 char_u *s;
6339{
6340 int len = 0;
6341
6342#ifdef FEAT_MENU
6343 int emenu = (xp->xp_context == EXPAND_MENUS
6344 || xp->xp_context == EXPAND_MENUNAMES);
6345
6346 /* Check for menu separators - replace with '|'. */
6347 if (emenu && menu_is_separator(s))
6348 return 1;
6349#endif
6350
6351 while (*s != NUL)
6352 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006353 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006354 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006355 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357
6358 return len;
6359}
6360
6361/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006362 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006363 * These are backslashes used for escaping. Do show backslashes in help tags.
6364 */
6365 static int
6366skip_status_match_char(xp, s)
6367 expand_T *xp;
6368 char_u *s;
6369{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006370 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006371#ifdef FEAT_MENU
6372 || ((xp->xp_context == EXPAND_MENUS
6373 || xp->xp_context == EXPAND_MENUNAMES)
6374 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6375#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006376 )
6377 {
6378#ifndef BACKSLASH_IN_FILENAME
6379 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6380 return 2;
6381#endif
6382 return 1;
6383 }
6384 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006385}
6386
6387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 * Show wildchar matches in the status line.
6389 * Show at least the "match" item.
6390 * We start at item 'first_match' in the list and show all matches that fit.
6391 *
6392 * If inversion is possible we use it. Else '=' characters are used.
6393 */
6394 void
6395win_redr_status_matches(xp, num_matches, matches, match, showtail)
6396 expand_T *xp;
6397 int num_matches;
6398 char_u **matches; /* list of matches */
6399 int match;
6400 int showtail;
6401{
6402#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6403 int row;
6404 char_u *buf;
6405 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006406 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 int fillchar;
6408 int attr;
6409 int i;
6410 int highlight = TRUE;
6411 char_u *selstart = NULL;
6412 int selstart_col = 0;
6413 char_u *selend = NULL;
6414 static int first_match = 0;
6415 int add_left = FALSE;
6416 char_u *s;
6417#ifdef FEAT_MENU
6418 int emenu;
6419#endif
6420#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6421 int l;
6422#endif
6423
6424 if (matches == NULL) /* interrupted completion? */
6425 return;
6426
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006427#ifdef FEAT_MBYTE
6428 if (has_mbyte)
6429 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6430 else
6431#endif
6432 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (buf == NULL)
6434 return;
6435
6436 if (match == -1) /* don't show match but original text */
6437 {
6438 match = 0;
6439 highlight = FALSE;
6440 }
6441 /* count 1 for the ending ">" */
6442 clen = status_match_len(xp, L_MATCH(match)) + 3;
6443 if (match == 0)
6444 first_match = 0;
6445 else if (match < first_match)
6446 {
6447 /* jumping left, as far as we can go */
6448 first_match = match;
6449 add_left = TRUE;
6450 }
6451 else
6452 {
6453 /* check if match fits on the screen */
6454 for (i = first_match; i < match; ++i)
6455 clen += status_match_len(xp, L_MATCH(i)) + 2;
6456 if (first_match > 0)
6457 clen += 2;
6458 /* jumping right, put match at the left */
6459 if ((long)clen > Columns)
6460 {
6461 first_match = match;
6462 /* if showing the last match, we can add some on the left */
6463 clen = 2;
6464 for (i = match; i < num_matches; ++i)
6465 {
6466 clen += status_match_len(xp, L_MATCH(i)) + 2;
6467 if ((long)clen >= Columns)
6468 break;
6469 }
6470 if (i == num_matches)
6471 add_left = TRUE;
6472 }
6473 }
6474 if (add_left)
6475 while (first_match > 0)
6476 {
6477 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6478 if ((long)clen >= Columns)
6479 break;
6480 --first_match;
6481 }
6482
6483 fillchar = fillchar_status(&attr, TRUE);
6484
6485 if (first_match == 0)
6486 {
6487 *buf = NUL;
6488 len = 0;
6489 }
6490 else
6491 {
6492 STRCPY(buf, "< ");
6493 len = 2;
6494 }
6495 clen = len;
6496
6497 i = first_match;
6498 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6499 {
6500 if (i == match)
6501 {
6502 selstart = buf + len;
6503 selstart_col = clen;
6504 }
6505
6506 s = L_MATCH(i);
6507 /* Check for menu separators - replace with '|' */
6508#ifdef FEAT_MENU
6509 emenu = (xp->xp_context == EXPAND_MENUS
6510 || xp->xp_context == EXPAND_MENUNAMES);
6511 if (emenu && menu_is_separator(s))
6512 {
6513 STRCPY(buf + len, transchar('|'));
6514 l = (int)STRLEN(buf + len);
6515 len += l;
6516 clen += l;
6517 }
6518 else
6519#endif
6520 for ( ; *s != NUL; ++s)
6521 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006522 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 clen += ptr2cells(s);
6524#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006525 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 STRNCPY(buf + len, s, l);
6528 s += l - 1;
6529 len += l;
6530 }
6531 else
6532#endif
6533 {
6534 STRCPY(buf + len, transchar_byte(*s));
6535 len += (int)STRLEN(buf + len);
6536 }
6537 }
6538 if (i == match)
6539 selend = buf + len;
6540
6541 *(buf + len++) = ' ';
6542 *(buf + len++) = ' ';
6543 clen += 2;
6544 if (++i == num_matches)
6545 break;
6546 }
6547
6548 if (i != num_matches)
6549 {
6550 *(buf + len++) = '>';
6551 ++clen;
6552 }
6553
6554 buf[len] = NUL;
6555
6556 row = cmdline_row - 1;
6557 if (row >= 0)
6558 {
6559 if (wild_menu_showing == 0)
6560 {
6561 if (msg_scrolled > 0)
6562 {
6563 /* Put the wildmenu just above the command line. If there is
6564 * no room, scroll the screen one line up. */
6565 if (cmdline_row == Rows - 1)
6566 {
6567 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6568 ++msg_scrolled;
6569 }
6570 else
6571 {
6572 ++cmdline_row;
6573 ++row;
6574 }
6575 wild_menu_showing = WM_SCROLLED;
6576 }
6577 else
6578 {
6579 /* Create status line if needed by setting 'laststatus' to 2.
6580 * Set 'winminheight' to zero to avoid that the window is
6581 * resized. */
6582 if (lastwin->w_status_height == 0)
6583 {
6584 save_p_ls = p_ls;
6585 save_p_wmh = p_wmh;
6586 p_ls = 2;
6587 p_wmh = 0;
6588 last_status(FALSE);
6589 }
6590 wild_menu_showing = WM_SHOWN;
6591 }
6592 }
6593
6594 screen_puts(buf, row, 0, attr);
6595 if (selstart != NULL && highlight)
6596 {
6597 *selend = NUL;
6598 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6599 }
6600
6601 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6602 }
6603
6604#ifdef FEAT_VERTSPLIT
6605 win_redraw_last_status(topframe);
6606#else
6607 lastwin->w_redr_status = TRUE;
6608#endif
6609 vim_free(buf);
6610}
6611#endif
6612
6613#if defined(FEAT_WINDOWS) || defined(PROTO)
6614/*
6615 * Redraw the status line of window wp.
6616 *
6617 * If inversion is possible we use it. Else '=' characters are used.
6618 */
6619 void
6620win_redr_status(wp)
6621 win_T *wp;
6622{
6623 int row;
6624 char_u *p;
6625 int len;
6626 int fillchar;
6627 int attr;
6628 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006629 static int busy = FALSE;
6630
6631 /* It's possible to get here recursively when 'statusline' (indirectly)
6632 * invokes ":redrawstatus". Simply ignore the call then. */
6633 if (busy)
6634 return;
6635 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637 wp->w_redr_status = FALSE;
6638 if (wp->w_status_height == 0)
6639 {
6640 /* no status line, can only be last window */
6641 redraw_cmdline = TRUE;
6642 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006643 else if (!redrawing()
6644#ifdef FEAT_INS_EXPAND
6645 /* don't update status line when popup menu is visible and may be
6646 * drawn over it */
6647 || pum_visible()
6648#endif
6649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
6651 /* Don't redraw right now, do it later. */
6652 wp->w_redr_status = TRUE;
6653 }
6654#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006655 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {
6657 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006658 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 }
6660#endif
6661 else
6662 {
6663 fillchar = fillchar_status(&attr, wp == curwin);
6664
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006665 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 p = NameBuff;
6667 len = (int)STRLEN(p);
6668
6669 if (wp->w_buffer->b_help
6670#ifdef FEAT_QUICKFIX
6671 || wp->w_p_pvw
6672#endif
6673 || bufIsChanged(wp->w_buffer)
6674 || wp->w_buffer->b_p_ro)
6675 *(p + len++) = ' ';
6676 if (wp->w_buffer->b_help)
6677 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006678 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 len += (int)STRLEN(p + len);
6680 }
6681#ifdef FEAT_QUICKFIX
6682 if (wp->w_p_pvw)
6683 {
6684 STRCPY(p + len, _("[Preview]"));
6685 len += (int)STRLEN(p + len);
6686 }
6687#endif
6688 if (bufIsChanged(wp->w_buffer))
6689 {
6690 STRCPY(p + len, "[+]");
6691 len += 3;
6692 }
6693 if (wp->w_buffer->b_p_ro)
6694 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006695 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 len += 4;
6697 }
6698
6699#ifndef FEAT_VERTSPLIT
6700 this_ru_col = ru_col;
6701 if (this_ru_col < (Columns + 1) / 2)
6702 this_ru_col = (Columns + 1) / 2;
6703#else
6704 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6705 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6706 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6707 if (this_ru_col <= 1)
6708 {
6709 p = (char_u *)"<"; /* No room for file name! */
6710 len = 1;
6711 }
6712 else
6713#endif
6714#ifdef FEAT_MBYTE
6715 if (has_mbyte)
6716 {
6717 int clen = 0, i;
6718
6719 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006720 clen = mb_string2cells(p, -1);
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 /* Find first character that will fit.
6723 * Going from start to end is much faster for DBCS. */
6724 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006725 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 clen -= (*mb_ptr2cells)(p + i);
6727 len = clen;
6728 if (i > 0)
6729 {
6730 p = p + i - 1;
6731 *p = '<';
6732 ++len;
6733 }
6734
6735 }
6736 else
6737#endif
6738 if (len > this_ru_col - 1)
6739 {
6740 p += len - (this_ru_col - 1);
6741 *p = '<';
6742 len = this_ru_col - 1;
6743 }
6744
6745 row = W_WINROW(wp) + wp->w_height;
6746 screen_puts(p, row, W_WINCOL(wp), attr);
6747 screen_fill(row, row + 1, len + W_WINCOL(wp),
6748 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6749
6750 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6751 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6752 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6753 - 1 + W_WINCOL(wp)), attr);
6754
6755#ifdef FEAT_CMDL_INFO
6756 win_redr_ruler(wp, TRUE);
6757#endif
6758 }
6759
6760#ifdef FEAT_VERTSPLIT
6761 /*
6762 * May need to draw the character below the vertical separator.
6763 */
6764 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6765 {
6766 if (stl_connected(wp))
6767 fillchar = fillchar_status(&attr, wp == curwin);
6768 else
6769 fillchar = fillchar_vsep(&attr);
6770 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6771 attr);
6772 }
6773#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006774 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776
Bram Moolenaar238a5642006-02-21 22:12:05 +00006777#ifdef FEAT_STL_OPT
6778/*
6779 * Redraw the status line according to 'statusline' and take care of any
6780 * errors encountered.
6781 */
6782 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006783redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006784 win_T *wp;
6785{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006786 static int entered = FALSE;
6787 int save_called_emsg = called_emsg;
6788
6789 /* When called recursively return. This can happen when the statusline
6790 * contains an expression that triggers a redraw. */
6791 if (entered)
6792 return;
6793 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006794
6795 called_emsg = FALSE;
6796 win_redr_custom(wp, FALSE);
6797 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006798 {
6799 /* When there is an error disable the statusline, otherwise the
6800 * display is messed up with errors and a redraw triggers the problem
6801 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006802 set_string_option_direct((char_u *)"statusline", -1,
6803 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006804 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006805 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006806 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006807 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006808}
6809#endif
6810
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811# ifdef FEAT_VERTSPLIT
6812/*
6813 * Return TRUE if the status line of window "wp" is connected to the status
6814 * line of the window right of it. If not, then it's a vertical separator.
6815 * Only call if (wp->w_vsep_width != 0).
6816 */
6817 int
6818stl_connected(wp)
6819 win_T *wp;
6820{
6821 frame_T *fr;
6822
6823 fr = wp->w_frame;
6824 while (fr->fr_parent != NULL)
6825 {
6826 if (fr->fr_parent->fr_layout == FR_COL)
6827 {
6828 if (fr->fr_next != NULL)
6829 break;
6830 }
6831 else
6832 {
6833 if (fr->fr_next != NULL)
6834 return TRUE;
6835 }
6836 fr = fr->fr_parent;
6837 }
6838 return FALSE;
6839}
6840# endif
6841
6842#endif /* FEAT_WINDOWS */
6843
6844#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6845/*
6846 * Get the value to show for the language mappings, active 'keymap'.
6847 */
6848 int
6849get_keymap_str(wp, buf, len)
6850 win_T *wp;
6851 char_u *buf; /* buffer for the result */
6852 int len; /* length of buffer */
6853{
6854 char_u *p;
6855
6856 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6857 return FALSE;
6858
6859 {
6860#ifdef FEAT_EVAL
6861 buf_T *old_curbuf = curbuf;
6862 win_T *old_curwin = curwin;
6863 char_u *s;
6864
6865 curbuf = wp->w_buffer;
6866 curwin = wp;
6867 STRCPY(buf, "b:keymap_name"); /* must be writable */
6868 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006869 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 --emsg_skip;
6871 curbuf = old_curbuf;
6872 curwin = old_curwin;
6873 if (p == NULL || *p == NUL)
6874#endif
6875 {
6876#ifdef FEAT_KEYMAP
6877 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6878 p = wp->w_buffer->b_p_keymap;
6879 else
6880#endif
6881 p = (char_u *)"lang";
6882 }
6883 if ((int)(STRLEN(p) + 3) < len)
6884 sprintf((char *)buf, "<%s>", p);
6885 else
6886 buf[0] = NUL;
6887#ifdef FEAT_EVAL
6888 vim_free(s);
6889#endif
6890 }
6891 return buf[0] != NUL;
6892}
6893#endif
6894
6895#if defined(FEAT_STL_OPT) || defined(PROTO)
6896/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006897 * Redraw the status line or ruler of window "wp".
6898 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 */
6900 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006901win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006903 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006905 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 int attr;
6907 int curattr;
6908 int row;
6909 int col = 0;
6910 int maxwidth;
6911 int width;
6912 int n;
6913 int len;
6914 int fillchar;
6915 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006916 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006918 struct stl_hlrec hltab[STL_MAX_ITEM];
6919 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006920 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006921 win_T *ewp;
6922 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
Bram Moolenaar1d633412013-12-11 15:52:01 +01006924 /* There is a tiny chance that this gets called recursively: When
6925 * redrawing a status line triggers redrawing the ruler or tabline.
6926 * Avoid trouble by not allowing recursion. */
6927 if (entered)
6928 return;
6929 entered = TRUE;
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006932 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006934 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006935 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006936 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006937 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006938 attr = hl_attr(HLF_TPF);
6939 maxwidth = Columns;
6940# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006941 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006944 else
6945 {
6946 row = W_WINROW(wp) + wp->w_height;
6947 fillchar = fillchar_status(&attr, wp == curwin);
6948 maxwidth = W_WIDTH(wp);
6949
6950 if (draw_ruler)
6951 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006952 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006953 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006954 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006955 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006956 if (*++stl == '-')
6957 stl++;
6958 if (atoi((char *)stl))
6959 while (VIM_ISDIGIT(*stl))
6960 stl++;
6961 if (*stl++ != '(')
6962 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006963 }
6964#ifdef FEAT_VERTSPLIT
6965 col = ru_col - (Columns - W_WIDTH(wp));
6966 if (col < (W_WIDTH(wp) + 1) / 2)
6967 col = (W_WIDTH(wp) + 1) / 2;
6968#else
6969 col = ru_col;
6970 if (col > (Columns + 1) / 2)
6971 col = (Columns + 1) / 2;
6972#endif
6973 maxwidth = W_WIDTH(wp) - col;
6974#ifdef FEAT_WINDOWS
6975 if (!wp->w_status_height)
6976#endif
6977 {
6978 row = Rows - 1;
6979 --maxwidth; /* writing in last column may cause scrolling */
6980 fillchar = ' ';
6981 attr = 0;
6982 }
6983
6984# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006985 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006986# endif
6987 }
6988 else
6989 {
6990 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006991 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006992 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006993 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006994# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006995 use_sandbox = was_set_insecurely((char_u *)"statusline",
6996 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006997# endif
6998 }
6999
7000#ifdef FEAT_VERTSPLIT
7001 col += W_WINCOL(wp);
7002#endif
7003 }
7004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007006 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaar61452852011-02-01 18:01:11 +01007008 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7009 * the cursor away and back. */
7010 ewp = wp == NULL ? curwin : wp;
7011 p_crb_save = ewp->w_p_crb;
7012 ewp->w_p_crb = FALSE;
7013
Bram Moolenaar362f3562009-11-03 16:20:34 +00007014 /* Make a copy, because the statusline may include a function call that
7015 * might change the option value and free the memory. */
7016 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007017 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007018 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007019 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007021 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007023 /* Make all characters printable. */
7024 p = transstr(buf);
7025 if (p != NULL)
7026 {
7027 vim_strncpy(buf, p, sizeof(buf) - 1);
7028 vim_free(p);
7029 }
7030
7031 /* fill up with "fillchar" */
7032 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007033 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
7035#ifdef FEAT_MBYTE
7036 len += (*mb_char2bytes)(fillchar, buf + len);
7037#else
7038 buf[len++] = fillchar;
7039#endif
7040 ++width;
7041 }
7042 buf[len] = NUL;
7043
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007044 /*
7045 * Draw each snippet with the specified highlighting.
7046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 curattr = attr;
7048 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007051 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 screen_puts_len(p, len, row, col, curattr);
7053 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007054 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007058 else if (hltab[n].userhl < 0)
7059 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007061 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007062 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
7064 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007065 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007068
7069 if (wp == NULL)
7070 {
7071 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7072 col = 0;
7073 len = 0;
7074 p = buf;
7075 fillchar = 0;
7076 for (n = 0; tabtab[n].start != NULL; n++)
7077 {
7078 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7079 while (col < len)
7080 TabPageIdxs[col++] = fillchar;
7081 p = tabtab[n].start;
7082 fillchar = tabtab[n].userhl;
7083 }
7084 while (col < Columns)
7085 TabPageIdxs[col++] = fillchar;
7086 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007087
7088theend:
7089 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091
7092#endif /* FEAT_STL_OPT */
7093
7094/*
7095 * Output a single character directly to the screen and update ScreenLines.
7096 */
7097 void
7098screen_putchar(c, row, col, attr)
7099 int c;
7100 int row, col;
7101 int attr;
7102{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 char_u buf[MB_MAXBYTES + 1];
7104
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007105#ifdef FEAT_MBYTE
7106 if (has_mbyte)
7107 buf[(*mb_char2bytes)(c, buf)] = NUL;
7108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007110 {
7111 buf[0] = c;
7112 buf[1] = NUL;
7113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 screen_puts(buf, row, col, attr);
7115}
7116
7117/*
7118 * Get a single character directly from ScreenLines into "bytes[]".
7119 * Also return its attribute in *attrp;
7120 */
7121 void
7122screen_getbytes(row, col, bytes, attrp)
7123 int row, col;
7124 char_u *bytes;
7125 int *attrp;
7126{
7127 unsigned off;
7128
7129 /* safety check */
7130 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7131 {
7132 off = LineOffset[row] + col;
7133 *attrp = ScreenAttrs[off];
7134 bytes[0] = ScreenLines[off];
7135 bytes[1] = NUL;
7136
7137#ifdef FEAT_MBYTE
7138 if (enc_utf8 && ScreenLinesUC[off] != 0)
7139 bytes[utfc_char2bytes(off, bytes)] = NUL;
7140 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7141 {
7142 bytes[0] = ScreenLines[off];
7143 bytes[1] = ScreenLines2[off];
7144 bytes[2] = NUL;
7145 }
7146 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7147 {
7148 bytes[1] = ScreenLines[off + 1];
7149 bytes[2] = NUL;
7150 }
7151#endif
7152 }
7153}
7154
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007155#ifdef FEAT_MBYTE
7156static int screen_comp_differs __ARGS((int, int*));
7157
7158/*
7159 * Return TRUE if composing characters for screen posn "off" differs from
7160 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007161 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007162 */
7163 static int
7164screen_comp_differs(off, u8cc)
7165 int off;
7166 int *u8cc;
7167{
7168 int i;
7169
7170 for (i = 0; i < Screen_mco; ++i)
7171 {
7172 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7173 return TRUE;
7174 if (u8cc[i] == 0)
7175 break;
7176 }
7177 return FALSE;
7178}
7179#endif
7180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181/*
7182 * Put string '*text' on the screen at position 'row' and 'col', with
7183 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7184 * Note: only outputs within one row, message is truncated at screen boundary!
7185 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7186 */
7187 void
7188screen_puts(text, row, col, attr)
7189 char_u *text;
7190 int row;
7191 int col;
7192 int attr;
7193{
7194 screen_puts_len(text, -1, row, col, attr);
7195}
7196
7197/*
7198 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7199 * a NUL.
7200 */
7201 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007202screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007204 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 int row;
7206 int col;
7207 int attr;
7208{
7209 unsigned off;
7210 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007211 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 int c;
7213#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007214 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 int mbyte_blen = 1;
7216 int mbyte_cells = 1;
7217 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007218 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 int clear_next_cell = FALSE;
7220# ifdef FEAT_ARABIC
7221 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007222 int pc, nc, nc1;
7223 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224# endif
7225#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007226#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7227 int force_redraw_this;
7228 int force_redraw_next = FALSE;
7229#endif
7230 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7233 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007234 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
Bram Moolenaarc236c162008-07-13 17:41:49 +00007236#ifdef FEAT_MBYTE
7237 /* When drawing over the right halve of a double-wide char clear out the
7238 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007239 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007240# ifdef FEAT_GUI
7241 && !gui.in_use
7242# endif
7243 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007244 {
7245 ScreenLines[off - 1] = ' ';
7246 ScreenAttrs[off - 1] = 0;
7247 if (enc_utf8)
7248 {
7249 ScreenLinesUC[off - 1] = 0;
7250 ScreenLinesC[0][off - 1] = 0;
7251 }
7252 /* redraw the previous cell, make it empty */
7253 screen_char(off - 1, row, col - 1);
7254 /* force the cell at "col" to be redrawn */
7255 force_redraw_next = TRUE;
7256 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007257#endif
7258
Bram Moolenaar367329b2007-08-30 11:53:22 +00007259#ifdef FEAT_MBYTE
7260 max_off = LineOffset[row] + screen_Columns;
7261#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007262 while (col < screen_Columns
7263 && (len < 0 || (int)(ptr - text) < len)
7264 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 c = *ptr;
7267#ifdef FEAT_MBYTE
7268 /* check if this is the first byte of a multibyte */
7269 if (has_mbyte)
7270 {
7271 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007272 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7276 mbyte_cells = 1;
7277 else if (enc_dbcs != 0)
7278 mbyte_cells = mbyte_blen;
7279 else /* enc_utf8 */
7280 {
7281 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007282 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 (int)((text + len) - ptr));
7284 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007285 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007287# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 /* Non-BMP character: display as ? or fullwidth ?. */
7289 if (u8c >= 0x10000)
7290 {
7291 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7292 if (attr == 0)
7293 attr = hl_attr(HLF_8);
7294 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296# ifdef FEAT_ARABIC
7297 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7298 {
7299 /* Do Arabic shaping. */
7300 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7301 {
7302 /* Past end of string to be displayed. */
7303 nc = NUL;
7304 nc1 = NUL;
7305 }
7306 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007307 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007308 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7309 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007310 nc1 = pcc[0];
7311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 pc = prev_c;
7313 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007314 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
7316 else
7317 prev_c = u8c;
7318# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007319 if (col + mbyte_cells > screen_Columns)
7320 {
7321 /* Only 1 cell left, but character requires 2 cells:
7322 * display a '>' in the last column to avoid wrapping. */
7323 c = '>';
7324 mbyte_cells = 1;
7325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
7327 }
7328#endif
7329
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007330#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7331 force_redraw_this = force_redraw_next;
7332 force_redraw_next = FALSE;
7333#endif
7334
7335 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#ifdef FEAT_MBYTE
7337 || (mbyte_cells == 2
7338 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7339 || (enc_dbcs == DBCS_JPNU
7340 && c == 0x8e
7341 && ScreenLines2[off] != ptr[1])
7342 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007343 && (ScreenLinesUC[off] !=
7344 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7345 || (ScreenLinesUC[off] != 0
7346 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347#endif
7348 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007349 || exmode_active;
7350
7351 if (need_redraw
7352#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7353 || force_redraw_this
7354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 )
7356 {
7357#if defined(FEAT_GUI) || defined(UNIX)
7358 /* The bold trick makes a single row of pixels appear in the next
7359 * character. When a bold character is removed, the next
7360 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007361 * and for some xterms. */
7362 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363# ifdef FEAT_GUI
7364 gui.in_use
7365# endif
7366# if defined(FEAT_GUI) && defined(UNIX)
7367 ||
7368# endif
7369# ifdef UNIX
7370 term_is_xterm
7371# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007372 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007374 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007376 if (n > HL_ALL)
7377 n = syn_attr2attr(n);
7378 if (n & HL_BOLD)
7379 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381#endif
7382#ifdef FEAT_MBYTE
7383 /* When at the end of the text and overwriting a two-cell
7384 * character with a one-cell character, need to clear the next
7385 * cell. Also when overwriting the left halve of a two-cell char
7386 * with the right halve of a two-cell char. Do this only once
7387 * (mb_off2cells() may return 2 on the right halve). */
7388 if (clear_next_cell)
7389 clear_next_cell = FALSE;
7390 else if (has_mbyte
7391 && (len < 0 ? ptr[mbyte_blen] == NUL
7392 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007393 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007395 && (*mb_off2cells)(off, max_off) == 1
7396 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 clear_next_cell = TRUE;
7398
7399 /* Make sure we never leave a second byte of a double-byte behind,
7400 * it confuses mb_off2cells(). */
7401 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007402 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007404 && (*mb_off2cells)(off, max_off) == 1
7405 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 ScreenLines[off + mbyte_blen] = 0;
7407#endif
7408 ScreenLines[off] = c;
7409 ScreenAttrs[off] = attr;
7410#ifdef FEAT_MBYTE
7411 if (enc_utf8)
7412 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007413 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 ScreenLinesUC[off] = 0;
7415 else
7416 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007417 int i;
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007420 for (i = 0; i < Screen_mco; ++i)
7421 {
7422 ScreenLinesC[i][off] = u8cc[i];
7423 if (u8cc[i] == 0)
7424 break;
7425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 if (mbyte_cells == 2)
7428 {
7429 ScreenLines[off + 1] = 0;
7430 ScreenAttrs[off + 1] = attr;
7431 }
7432 screen_char(off, row, col);
7433 }
7434 else if (mbyte_cells == 2)
7435 {
7436 ScreenLines[off + 1] = ptr[1];
7437 ScreenAttrs[off + 1] = attr;
7438 screen_char_2(off, row, col);
7439 }
7440 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7441 {
7442 ScreenLines2[off] = ptr[1];
7443 screen_char(off, row, col);
7444 }
7445 else
7446#endif
7447 screen_char(off, row, col);
7448 }
7449#ifdef FEAT_MBYTE
7450 if (has_mbyte)
7451 {
7452 off += mbyte_cells;
7453 col += mbyte_cells;
7454 ptr += mbyte_blen;
7455 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007456 {
7457 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007459 len = -1;
7460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
7462 else
7463#endif
7464 {
7465 ++off;
7466 ++col;
7467 ++ptr;
7468 }
7469 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007470
7471#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7472 /* If we detected the next character needs to be redrawn, but the text
7473 * doesn't extend up to there, update the character here. */
7474 if (force_redraw_next && col < screen_Columns)
7475 {
7476# ifdef FEAT_MBYTE
7477 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7478 screen_char_2(off, row, col);
7479 else
7480# endif
7481 screen_char(off, row, col);
7482 }
7483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
7486#ifdef FEAT_SEARCH_EXTRA
7487/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007488 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 */
7490 static void
7491start_search_hl()
7492{
7493 if (p_hls && !no_hlsearch)
7494 {
7495 last_pat_prog(&search_hl.rm);
7496 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007497# ifdef FEAT_RELTIME
7498 /* Set the time limit to 'redrawtime'. */
7499 profile_setlimit(p_rdt, &search_hl.tm);
7500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 }
7502}
7503
7504/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007505 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 */
7507 static void
7508end_search_hl()
7509{
7510 if (search_hl.rm.regprog != NULL)
7511 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007512 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 search_hl.rm.regprog = NULL;
7514 }
7515}
7516
7517/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007518 * Init for calling prepare_search_hl().
7519 */
7520 static void
7521init_search_hl(wp)
7522 win_T *wp;
7523{
7524 matchitem_T *cur;
7525
7526 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7527 * match */
7528 cur = wp->w_match_head;
7529 while (cur != NULL)
7530 {
7531 cur->hl.rm = cur->match;
7532 if (cur->hlg_id == 0)
7533 cur->hl.attr = 0;
7534 else
7535 cur->hl.attr = syn_id2attr(cur->hlg_id);
7536 cur->hl.buf = wp->w_buffer;
7537 cur->hl.lnum = 0;
7538 cur->hl.first_lnum = 0;
7539# ifdef FEAT_RELTIME
7540 /* Set the time limit to 'redrawtime'. */
7541 profile_setlimit(p_rdt, &(cur->hl.tm));
7542# endif
7543 cur = cur->next;
7544 }
7545 search_hl.buf = wp->w_buffer;
7546 search_hl.lnum = 0;
7547 search_hl.first_lnum = 0;
7548 /* time limit is set at the toplevel, for all windows */
7549}
7550
7551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 * Advance to the match in window "wp" line "lnum" or past it.
7553 */
7554 static void
7555prepare_search_hl(wp, lnum)
7556 win_T *wp;
7557 linenr_T lnum;
7558{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007559 matchitem_T *cur; /* points to the match list */
7560 match_T *shl; /* points to search_hl or a match */
7561 int shl_flag; /* flag to indicate whether search_hl
7562 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007563 int pos_inprogress; /* marks that position match search is
7564 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 int n;
7566
7567 /*
7568 * When using a multi-line pattern, start searching at the top
7569 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007570 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007572 cur = wp->w_match_head;
7573 shl_flag = FALSE;
7574 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007576 if (shl_flag == FALSE)
7577 {
7578 shl = &search_hl;
7579 shl_flag = TRUE;
7580 }
7581 else
7582 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 if (shl->rm.regprog != NULL
7584 && shl->lnum == 0
7585 && re_multiline(shl->rm.regprog))
7586 {
7587 if (shl->first_lnum == 0)
7588 {
7589# ifdef FEAT_FOLDING
7590 for (shl->first_lnum = lnum;
7591 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7592 if (hasFoldingWin(wp, shl->first_lnum - 1,
7593 NULL, NULL, TRUE, NULL))
7594 break;
7595# else
7596 shl->first_lnum = wp->w_topline;
7597# endif
7598 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007599 if (cur != NULL)
7600 cur->pos.cur = 0;
7601 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007603 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7604 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007606 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7607 pos_inprogress = cur == NULL || cur->pos.cur == 0
7608 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 if (shl->lnum != 0)
7610 {
7611 shl->first_lnum = shl->lnum
7612 + shl->rm.endpos[0].lnum
7613 - shl->rm.startpos[0].lnum;
7614 n = shl->rm.endpos[0].col;
7615 }
7616 else
7617 {
7618 ++shl->first_lnum;
7619 n = 0;
7620 }
7621 }
7622 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007623 if (shl != &search_hl && cur != NULL)
7624 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626}
7627
7628/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007629 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 * Uses shl->buf.
7631 * Sets shl->lnum and shl->rm contents.
7632 * Note: Assumes a previous match is always before "lnum", unless
7633 * shl->lnum is zero.
7634 * Careful: Any pointers for buffer lines will become invalid.
7635 */
7636 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007637next_search_hl(win, shl, lnum, mincol, cur)
7638 win_T *win;
7639 match_T *shl; /* points to search_hl or a match */
7640 linenr_T lnum;
7641 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007642 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 linenr_T l;
7645 colnr_T matchcol;
7646 long nmatched;
7647
7648 if (shl->lnum != 0)
7649 {
7650 /* Check for three situations:
7651 * 1. If the "lnum" is below a previous match, start a new search.
7652 * 2. If the previous match includes "mincol", use it.
7653 * 3. Continue after the previous match.
7654 */
7655 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7656 if (lnum > l)
7657 shl->lnum = 0;
7658 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7659 return;
7660 }
7661
7662 /*
7663 * Repeat searching for a match until one is found that includes "mincol"
7664 * or none is found in this line.
7665 */
7666 called_emsg = FALSE;
7667 for (;;)
7668 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007669#ifdef FEAT_RELTIME
7670 /* Stop searching after passing the time limit. */
7671 if (profile_passed_limit(&(shl->tm)))
7672 {
7673 shl->lnum = 0; /* no match found in time */
7674 break;
7675 }
7676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 /* Three situations:
7678 * 1. No useful previous match: search from start of line.
7679 * 2. Not Vi compatible or empty match: continue at next character.
7680 * Break the loop if this is beyond the end of the line.
7681 * 3. Vi compatible searching: continue at end of previous match.
7682 */
7683 if (shl->lnum == 0)
7684 matchcol = 0;
7685 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7686 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007687 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007689 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007690
7691 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007692 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007693 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007695 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 shl->lnum = 0;
7697 break;
7698 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007699#ifdef FEAT_MBYTE
7700 if (has_mbyte)
7701 matchcol += mb_ptr2len(ml);
7702 else
7703#endif
7704 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706 else
7707 matchcol = shl->rm.endpos[0].col;
7708
7709 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007710 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007712 /* Remember whether shl->rm is using a copy of the regprog in
7713 * cur->match. */
7714 int regprog_is_copy = (shl != &search_hl && cur != NULL
7715 && shl == &cur->hl
7716 && cur->match.regprog == cur->hl.rm.regprog);
7717
Bram Moolenaarb3414592014-06-17 17:48:32 +02007718 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7719 matchcol,
7720#ifdef FEAT_RELTIME
7721 &(shl->tm)
7722#else
7723 NULL
7724#endif
7725 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007726 /* Copy the regprog, in case it got freed and recompiled. */
7727 if (regprog_is_copy)
7728 cur->match.regprog = cur->hl.rm.regprog;
7729
Bram Moolenaarb3414592014-06-17 17:48:32 +02007730 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007731 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007732 /* Error while handling regexp: stop using this regexp. */
7733 if (shl == &search_hl)
7734 {
7735 /* don't free regprog in the match list, it's a copy */
7736 vim_regfree(shl->rm.regprog);
7737 SET_NO_HLSEARCH(TRUE);
7738 }
7739 shl->rm.regprog = NULL;
7740 shl->lnum = 0;
7741 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7742 message */
7743 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007744 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007745 }
7746 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007747 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007748 else
7749 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 if (nmatched == 0)
7751 {
7752 shl->lnum = 0; /* no match found */
7753 break;
7754 }
7755 if (shl->rm.startpos[0].lnum > 0
7756 || shl->rm.startpos[0].col >= mincol
7757 || nmatched > 1
7758 || shl->rm.endpos[0].col > mincol)
7759 {
7760 shl->lnum += shl->rm.startpos[0].lnum;
7761 break; /* useful match found */
7762 }
7763 }
7764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
Bram Moolenaarb3414592014-06-17 17:48:32 +02007766 static int
7767next_search_hl_pos(shl, lnum, posmatch, mincol)
7768 match_T *shl; /* points to a match */
7769 linenr_T lnum;
7770 posmatch_T *posmatch; /* match positions */
7771 colnr_T mincol; /* minimal column for a match */
7772{
7773 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007774 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007775
7776 shl->lnum = 0;
7777 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7778 {
7779 if (posmatch->pos[i].lnum == 0)
7780 break;
7781 if (posmatch->pos[i].col < mincol)
7782 continue;
7783 if (posmatch->pos[i].lnum == lnum)
7784 {
7785 if (shl->lnum == lnum)
7786 {
7787 /* partially sort positions by column numbers
7788 * on the same line */
7789 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7790 {
7791 llpos_T tmp = posmatch->pos[i];
7792
7793 posmatch->pos[i] = posmatch->pos[bot];
7794 posmatch->pos[bot] = tmp;
7795 }
7796 }
7797 else
7798 {
7799 bot = i;
7800 shl->lnum = lnum;
7801 }
7802 }
7803 }
7804 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007805 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007806 {
7807 colnr_T start = posmatch->pos[bot].col == 0
7808 ? 0 : posmatch->pos[bot].col - 1;
7809 colnr_T end = posmatch->pos[bot].col == 0
7810 ? MAXCOL : start + posmatch->pos[bot].len;
7811
7812 shl->rm.startpos[0].lnum = 0;
7813 shl->rm.startpos[0].col = start;
7814 shl->rm.endpos[0].lnum = 0;
7815 shl->rm.endpos[0].col = end;
7816 posmatch->cur = bot + 1;
7817 return TRUE;
7818 }
7819 return FALSE;
7820}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007821#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007822
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 static void
7824screen_start_highlight(attr)
7825 int attr;
7826{
7827 attrentry_T *aep = NULL;
7828
7829 screen_attr = attr;
7830 if (full_screen
7831#ifdef WIN3264
7832 && termcap_active
7833#endif
7834 )
7835 {
7836#ifdef FEAT_GUI
7837 if (gui.in_use)
7838 {
7839 char buf[20];
7840
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007841 /* The GUI handles this internally. */
7842 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 OUT_STR(buf);
7844 }
7845 else
7846#endif
7847 {
7848 if (attr > HL_ALL) /* special HL attr. */
7849 {
7850 if (t_colors > 1)
7851 aep = syn_cterm_attr2entry(attr);
7852 else
7853 aep = syn_term_attr2entry(attr);
7854 if (aep == NULL) /* did ":syntax clear" */
7855 attr = 0;
7856 else
7857 attr = aep->ae_attr;
7858 }
7859 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7860 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007861 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7862 && cterm_normal_fg_bold)
7863 /* If the Normal FG color has BOLD attribute and the new HL
7864 * has a FG color defined, clear BOLD. */
7865 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7867 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007868 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7869 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 out_str(T_US);
7871 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7872 out_str(T_CZH);
7873 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7874 out_str(T_MR);
7875
7876 /*
7877 * Output the color or start string after bold etc., in case the
7878 * bold etc. override the color setting.
7879 */
7880 if (aep != NULL)
7881 {
7882 if (t_colors > 1)
7883 {
7884 if (aep->ae_u.cterm.fg_color)
7885 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7886 if (aep->ae_u.cterm.bg_color)
7887 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7888 }
7889 else
7890 {
7891 if (aep->ae_u.term.start != NULL)
7892 out_str(aep->ae_u.term.start);
7893 }
7894 }
7895 }
7896 }
7897}
7898
7899 void
7900screen_stop_highlight()
7901{
7902 int do_ME = FALSE; /* output T_ME code */
7903
7904 if (screen_attr != 0
7905#ifdef WIN3264
7906 && termcap_active
7907#endif
7908 )
7909 {
7910#ifdef FEAT_GUI
7911 if (gui.in_use)
7912 {
7913 char buf[20];
7914
7915 /* use internal GUI code */
7916 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7917 OUT_STR(buf);
7918 }
7919 else
7920#endif
7921 {
7922 if (screen_attr > HL_ALL) /* special HL attr. */
7923 {
7924 attrentry_T *aep;
7925
7926 if (t_colors > 1)
7927 {
7928 /*
7929 * Assume that t_me restores the original colors!
7930 */
7931 aep = syn_cterm_attr2entry(screen_attr);
7932 if (aep != NULL && (aep->ae_u.cterm.fg_color
7933 || aep->ae_u.cterm.bg_color))
7934 do_ME = TRUE;
7935 }
7936 else
7937 {
7938 aep = syn_term_attr2entry(screen_attr);
7939 if (aep != NULL && aep->ae_u.term.stop != NULL)
7940 {
7941 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7942 do_ME = TRUE;
7943 else
7944 out_str(aep->ae_u.term.stop);
7945 }
7946 }
7947 if (aep == NULL) /* did ":syntax clear" */
7948 screen_attr = 0;
7949 else
7950 screen_attr = aep->ae_attr;
7951 }
7952
7953 /*
7954 * Often all ending-codes are equal to T_ME. Avoid outputting the
7955 * same sequence several times.
7956 */
7957 if (screen_attr & HL_STANDOUT)
7958 {
7959 if (STRCMP(T_SE, T_ME) == 0)
7960 do_ME = TRUE;
7961 else
7962 out_str(T_SE);
7963 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007964 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {
7966 if (STRCMP(T_UE, T_ME) == 0)
7967 do_ME = TRUE;
7968 else
7969 out_str(T_UE);
7970 }
7971 if (screen_attr & HL_ITALIC)
7972 {
7973 if (STRCMP(T_CZR, T_ME) == 0)
7974 do_ME = TRUE;
7975 else
7976 out_str(T_CZR);
7977 }
7978 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7979 out_str(T_ME);
7980
7981 if (t_colors > 1)
7982 {
7983 /* set Normal cterm colors */
7984 if (cterm_normal_fg_color != 0)
7985 term_fg_color(cterm_normal_fg_color - 1);
7986 if (cterm_normal_bg_color != 0)
7987 term_bg_color(cterm_normal_bg_color - 1);
7988 if (cterm_normal_fg_bold)
7989 out_str(T_MD);
7990 }
7991 }
7992 }
7993 screen_attr = 0;
7994}
7995
7996/*
7997 * Reset the colors for a cterm. Used when leaving Vim.
7998 * The machine specific code may override this again.
7999 */
8000 void
8001reset_cterm_colors()
8002{
8003 if (t_colors > 1)
8004 {
8005 /* set Normal cterm colors */
8006 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
8007 {
8008 out_str(T_OP);
8009 screen_attr = -1;
8010 }
8011 if (cterm_normal_fg_bold)
8012 {
8013 out_str(T_ME);
8014 screen_attr = -1;
8015 }
8016 }
8017}
8018
8019/*
8020 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8021 * using the attributes from ScreenAttrs["off"].
8022 */
8023 static void
8024screen_char(off, row, col)
8025 unsigned off;
8026 int row;
8027 int col;
8028{
8029 int attr;
8030
8031 /* Check for illegal values, just in case (could happen just after
8032 * resizing). */
8033 if (row >= screen_Rows || col >= screen_Columns)
8034 return;
8035
Bram Moolenaar494838a2015-02-10 19:20:37 +01008036 /* Outputting a character in the last cell on the screen may scroll the
8037 * screen up. Only do it when the "xn" termcap property is set, otherwise
8038 * mark the character invalid (update it when scrolled up). */
8039 if (*T_XN == NUL
8040 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041#ifdef FEAT_RIGHTLEFT
8042 /* account for first command-line character in rightleft mode */
8043 && !cmdmsg_rl
8044#endif
8045 )
8046 {
8047 ScreenAttrs[off] = (sattr_T)-1;
8048 return;
8049 }
8050
8051 /*
8052 * Stop highlighting first, so it's easier to move the cursor.
8053 */
8054#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8055 if (screen_char_attr != 0)
8056 attr = screen_char_attr;
8057 else
8058#endif
8059 attr = ScreenAttrs[off];
8060 if (screen_attr != attr)
8061 screen_stop_highlight();
8062
8063 windgoto(row, col);
8064
8065 if (screen_attr != attr)
8066 screen_start_highlight(attr);
8067
8068#ifdef FEAT_MBYTE
8069 if (enc_utf8 && ScreenLinesUC[off] != 0)
8070 {
8071 char_u buf[MB_MAXBYTES + 1];
8072
8073 /* Convert UTF-8 character to bytes and write it. */
8074
8075 buf[utfc_char2bytes(off, buf)] = NUL;
8076
8077 out_str(buf);
8078 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8079 ++screen_cur_col;
8080 }
8081 else
8082#endif
8083 {
8084#ifdef FEAT_MBYTE
8085 out_flush_check();
8086#endif
8087 out_char(ScreenLines[off]);
8088#ifdef FEAT_MBYTE
8089 /* double-byte character in single-width cell */
8090 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8091 out_char(ScreenLines2[off]);
8092#endif
8093 }
8094
8095 screen_cur_col++;
8096}
8097
8098#ifdef FEAT_MBYTE
8099
8100/*
8101 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8102 * on the screen at position 'row' and 'col'.
8103 * The attributes of the first byte is used for all. This is required to
8104 * output the two bytes of a double-byte character with nothing in between.
8105 */
8106 static void
8107screen_char_2(off, row, col)
8108 unsigned off;
8109 int row;
8110 int col;
8111{
8112 /* Check for illegal values (could be wrong when screen was resized). */
8113 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8114 return;
8115
8116 /* Outputting the last character on the screen may scrollup the screen.
8117 * Don't to it! Mark the character invalid (update it when scrolled up) */
8118 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8119 {
8120 ScreenAttrs[off] = (sattr_T)-1;
8121 return;
8122 }
8123
8124 /* Output the first byte normally (positions the cursor), then write the
8125 * second byte directly. */
8126 screen_char(off, row, col);
8127 out_char(ScreenLines[off + 1]);
8128 ++screen_cur_col;
8129}
8130#endif
8131
8132#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8133/*
8134 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8135 * This uses the contents of ScreenLines[] and doesn't change it.
8136 */
8137 void
8138screen_draw_rectangle(row, col, height, width, invert)
8139 int row;
8140 int col;
8141 int height;
8142 int width;
8143 int invert;
8144{
8145 int r, c;
8146 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008147#ifdef FEAT_MBYTE
8148 int max_off;
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008151 /* Can't use ScreenLines unless initialized */
8152 if (ScreenLines == NULL)
8153 return;
8154
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (invert)
8156 screen_char_attr = HL_INVERSE;
8157 for (r = row; r < row + height; ++r)
8158 {
8159 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008160#ifdef FEAT_MBYTE
8161 max_off = off + screen_Columns;
8162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 for (c = col; c < col + width; ++c)
8164 {
8165#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008166 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {
8168 screen_char_2(off + c, r, c);
8169 ++c;
8170 }
8171 else
8172#endif
8173 {
8174 screen_char(off + c, r, c);
8175#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008176 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 ++c;
8178#endif
8179 }
8180 }
8181 }
8182 screen_char_attr = 0;
8183}
8184#endif
8185
8186#ifdef FEAT_VERTSPLIT
8187/*
8188 * Redraw the characters for a vertically split window.
8189 */
8190 static void
8191redraw_block(row, end, wp)
8192 int row;
8193 int end;
8194 win_T *wp;
8195{
8196 int col;
8197 int width;
8198
8199# ifdef FEAT_CLIPBOARD
8200 clip_may_clear_selection(row, end - 1);
8201# endif
8202
8203 if (wp == NULL)
8204 {
8205 col = 0;
8206 width = Columns;
8207 }
8208 else
8209 {
8210 col = wp->w_wincol;
8211 width = wp->w_width;
8212 }
8213 screen_draw_rectangle(row, col, end - row, width, FALSE);
8214}
8215#endif
8216
8217/*
8218 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8219 * with character 'c1' in first column followed by 'c2' in the other columns.
8220 * Use attributes 'attr'.
8221 */
8222 void
8223screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8224 int start_row, end_row;
8225 int start_col, end_col;
8226 int c1, c2;
8227 int attr;
8228{
8229 int row;
8230 int col;
8231 int off;
8232 int end_off;
8233 int did_delete;
8234 int c;
8235 int norm_term;
8236#if defined(FEAT_GUI) || defined(UNIX)
8237 int force_next = FALSE;
8238#endif
8239
8240 if (end_row > screen_Rows) /* safety check */
8241 end_row = screen_Rows;
8242 if (end_col > screen_Columns) /* safety check */
8243 end_col = screen_Columns;
8244 if (ScreenLines == NULL
8245 || start_row >= end_row
8246 || start_col >= end_col) /* nothing to do */
8247 return;
8248
8249 /* it's a "normal" terminal when not in a GUI or cterm */
8250 norm_term = (
8251#ifdef FEAT_GUI
8252 !gui.in_use &&
8253#endif
8254 t_colors <= 1);
8255 for (row = start_row; row < end_row; ++row)
8256 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008257#ifdef FEAT_MBYTE
8258 if (has_mbyte
8259# ifdef FEAT_GUI
8260 && !gui.in_use
8261# endif
8262 )
8263 {
8264 /* When drawing over the right halve of a double-wide char clear
8265 * out the left halve. When drawing over the left halve of a
8266 * double wide-char clear out the right halve. Only needed in a
8267 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008268 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008269 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008270 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008271 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008272 }
8273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 /*
8275 * Try to use delete-line termcap code, when no attributes or in a
8276 * "normal" terminal, where a bold/italic space is just a
8277 * space.
8278 */
8279 did_delete = FALSE;
8280 if (c2 == ' '
8281 && end_col == Columns
8282 && can_clear(T_CE)
8283 && (attr == 0
8284 || (norm_term
8285 && attr <= HL_ALL
8286 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8287 {
8288 /*
8289 * check if we really need to clear something
8290 */
8291 col = start_col;
8292 if (c1 != ' ') /* don't clear first char */
8293 ++col;
8294
8295 off = LineOffset[row] + col;
8296 end_off = LineOffset[row] + end_col;
8297
8298 /* skip blanks (used often, keep it fast!) */
8299#ifdef FEAT_MBYTE
8300 if (enc_utf8)
8301 while (off < end_off && ScreenLines[off] == ' '
8302 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8303 ++off;
8304 else
8305#endif
8306 while (off < end_off && ScreenLines[off] == ' '
8307 && ScreenAttrs[off] == 0)
8308 ++off;
8309 if (off < end_off) /* something to be cleared */
8310 {
8311 col = off - LineOffset[row];
8312 screen_stop_highlight();
8313 term_windgoto(row, col);/* clear rest of this screen line */
8314 out_str(T_CE);
8315 screen_start(); /* don't know where cursor is now */
8316 col = end_col - col;
8317 while (col--) /* clear chars in ScreenLines */
8318 {
8319 ScreenLines[off] = ' ';
8320#ifdef FEAT_MBYTE
8321 if (enc_utf8)
8322 ScreenLinesUC[off] = 0;
8323#endif
8324 ScreenAttrs[off] = 0;
8325 ++off;
8326 }
8327 }
8328 did_delete = TRUE; /* the chars are cleared now */
8329 }
8330
8331 off = LineOffset[row] + start_col;
8332 c = c1;
8333 for (col = start_col; col < end_col; ++col)
8334 {
8335 if (ScreenLines[off] != c
8336#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008337 || (enc_utf8 && (int)ScreenLinesUC[off]
8338 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339#endif
8340 || ScreenAttrs[off] != attr
8341#if defined(FEAT_GUI) || defined(UNIX)
8342 || force_next
8343#endif
8344 )
8345 {
8346#if defined(FEAT_GUI) || defined(UNIX)
8347 /* The bold trick may make a single row of pixels appear in
8348 * the next character. When a bold character is removed, the
8349 * next character should be redrawn too. This happens for our
8350 * own GUI and for some xterms. */
8351 if (
8352# ifdef FEAT_GUI
8353 gui.in_use
8354# endif
8355# if defined(FEAT_GUI) && defined(UNIX)
8356 ||
8357# endif
8358# ifdef UNIX
8359 term_is_xterm
8360# endif
8361 )
8362 {
8363 if (ScreenLines[off] != ' '
8364 && (ScreenAttrs[off] > HL_ALL
8365 || ScreenAttrs[off] & HL_BOLD))
8366 force_next = TRUE;
8367 else
8368 force_next = FALSE;
8369 }
8370#endif
8371 ScreenLines[off] = c;
8372#ifdef FEAT_MBYTE
8373 if (enc_utf8)
8374 {
8375 if (c >= 0x80)
8376 {
8377 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008378 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 }
8380 else
8381 ScreenLinesUC[off] = 0;
8382 }
8383#endif
8384 ScreenAttrs[off] = attr;
8385 if (!did_delete || c != ' ')
8386 screen_char(off, row, col);
8387 }
8388 ++off;
8389 if (col == start_col)
8390 {
8391 if (did_delete)
8392 break;
8393 c = c2;
8394 }
8395 }
8396 if (end_col == Columns)
8397 LineWraps[row] = FALSE;
8398 if (row == Rows - 1) /* overwritten the command line */
8399 {
8400 redraw_cmdline = TRUE;
8401 if (c1 == ' ' && c2 == ' ')
8402 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008403 if (start_col == 0)
8404 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 }
8406 }
8407}
8408
8409/*
8410 * Check if there should be a delay. Used before clearing or redrawing the
8411 * screen or the command line.
8412 */
8413 void
8414check_for_delay(check_msg_scroll)
8415 int check_msg_scroll;
8416{
8417 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8418 && !did_wait_return
8419 && emsg_silent == 0)
8420 {
8421 out_flush();
8422 ui_delay(1000L, TRUE);
8423 emsg_on_display = FALSE;
8424 if (check_msg_scroll)
8425 msg_scroll = FALSE;
8426 }
8427}
8428
8429/*
8430 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008431 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 * Returns TRUE if there is a valid screen to write to.
8433 * Returns FALSE when starting up and screen not initialized yet.
8434 */
8435 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008436screen_valid(doclear)
8437 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008439 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 return (ScreenLines != NULL);
8441}
8442
8443/*
8444 * Resize the shell to Rows and Columns.
8445 * Allocate ScreenLines[] and associated items.
8446 *
8447 * There may be some time between setting Rows and Columns and (re)allocating
8448 * ScreenLines[]. This happens when starting up and when (manually) changing
8449 * the shell size. Always use screen_Rows and screen_Columns to access items
8450 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8451 * final size of the shell is needed.
8452 */
8453 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008454screenalloc(doclear)
8455 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 int new_row, old_row;
8458#ifdef FEAT_GUI
8459 int old_Rows;
8460#endif
8461 win_T *wp;
8462 int outofmem = FALSE;
8463 int len;
8464 schar_T *new_ScreenLines;
8465#ifdef FEAT_MBYTE
8466 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008467 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008469 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470#endif
8471 sattr_T *new_ScreenAttrs;
8472 unsigned *new_LineOffset;
8473 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008474#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008475 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008476 tabpage_T *tp;
8477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008479 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008480#ifdef FEAT_AUTOCMD
8481 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008483retry:
8484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 /*
8486 * Allocation of the screen buffers is done only when the size changes and
8487 * when Rows and Columns have been set and we have started doing full
8488 * screen stuff.
8489 */
8490 if ((ScreenLines != NULL
8491 && Rows == screen_Rows
8492 && Columns == screen_Columns
8493#ifdef FEAT_MBYTE
8494 && enc_utf8 == (ScreenLinesUC != NULL)
8495 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008496 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497#endif
8498 )
8499 || Rows == 0
8500 || Columns == 0
8501 || (!full_screen && ScreenLines == NULL))
8502 return;
8503
8504 /*
8505 * It's possible that we produce an out-of-memory message below, which
8506 * will cause this function to be called again. To break the loop, just
8507 * return here.
8508 */
8509 if (entered)
8510 return;
8511 entered = TRUE;
8512
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008513 /*
8514 * Note that the window sizes are updated before reallocating the arrays,
8515 * thus we must not redraw here!
8516 */
8517 ++RedrawingDisabled;
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 win_new_shellsize(); /* fit the windows in the new sized shell */
8520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 comp_col(); /* recompute columns for shown command and ruler */
8522
8523 /*
8524 * We're changing the size of the screen.
8525 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8526 * - Move lines from the old arrays into the new arrays, clear extra
8527 * lines (unless the screen is going to be cleared).
8528 * - Free the old arrays.
8529 *
8530 * If anything fails, make ScreenLines NULL, so we don't do anything!
8531 * Continuing with the old ScreenLines may result in a crash, because the
8532 * size is wrong.
8533 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008534 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008536#ifdef FEAT_AUTOCMD
8537 if (aucmd_win != NULL)
8538 win_free_lsize(aucmd_win);
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
8541 new_ScreenLines = (schar_T *)lalloc((long_u)(
8542 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8543#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008544 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 if (enc_utf8)
8546 {
8547 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8548 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008549 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008550 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8552 }
8553 if (enc_dbcs == DBCS_JPNU)
8554 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8555 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8556#endif
8557 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8558 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8559 new_LineOffset = (unsigned *)lalloc((long_u)(
8560 Rows * sizeof(unsigned)), FALSE);
8561 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008562#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008563 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008566 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 if (win_alloc_lines(wp) == FAIL)
8569 {
8570 outofmem = TRUE;
8571#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008572 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573#endif
8574 }
8575 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008576#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008577 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8578 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008579 outofmem = TRUE;
8580#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008581#ifdef FEAT_WINDOWS
8582give_up:
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008585#ifdef FEAT_MBYTE
8586 for (i = 0; i < p_mco; ++i)
8587 if (new_ScreenLinesC[i] == NULL)
8588 break;
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 if (new_ScreenLines == NULL
8591#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008592 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8594#endif
8595 || new_ScreenAttrs == NULL
8596 || new_LineOffset == NULL
8597 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008598#ifdef FEAT_WINDOWS
8599 || new_TabPageIdxs == NULL
8600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 || outofmem)
8602 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008603 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008604 {
8605 /* guess the size */
8606 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8607
8608 /* Remember we did this to avoid getting outofmem messages over
8609 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008610 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 vim_free(new_ScreenLines);
8613 new_ScreenLines = NULL;
8614#ifdef FEAT_MBYTE
8615 vim_free(new_ScreenLinesUC);
8616 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008617 for (i = 0; i < p_mco; ++i)
8618 {
8619 vim_free(new_ScreenLinesC[i]);
8620 new_ScreenLinesC[i] = NULL;
8621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 vim_free(new_ScreenLines2);
8623 new_ScreenLines2 = NULL;
8624#endif
8625 vim_free(new_ScreenAttrs);
8626 new_ScreenAttrs = NULL;
8627 vim_free(new_LineOffset);
8628 new_LineOffset = NULL;
8629 vim_free(new_LineWraps);
8630 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008631#ifdef FEAT_WINDOWS
8632 vim_free(new_TabPageIdxs);
8633 new_TabPageIdxs = NULL;
8634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636 else
8637 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008638 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 for (new_row = 0; new_row < Rows; ++new_row)
8641 {
8642 new_LineOffset[new_row] = new_row * Columns;
8643 new_LineWraps[new_row] = FALSE;
8644
8645 /*
8646 * If the screen is not going to be cleared, copy as much as
8647 * possible from the old screen to the new one and clear the rest
8648 * (used when resizing the window at the "--more--" prompt or when
8649 * executing an external command, for the GUI).
8650 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008651 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {
8653 (void)vim_memset(new_ScreenLines + new_row * Columns,
8654 ' ', (size_t)Columns * sizeof(schar_T));
8655#ifdef FEAT_MBYTE
8656 if (enc_utf8)
8657 {
8658 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8659 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008660 for (i = 0; i < p_mco; ++i)
8661 (void)vim_memset(new_ScreenLinesC[i]
8662 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 0, (size_t)Columns * sizeof(u8char_T));
8664 }
8665 if (enc_dbcs == DBCS_JPNU)
8666 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8667 0, (size_t)Columns * sizeof(schar_T));
8668#endif
8669 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8670 0, (size_t)Columns * sizeof(sattr_T));
8671 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008672 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
8674 if (screen_Columns < Columns)
8675 len = screen_Columns;
8676 else
8677 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008678#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008679 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008680 * may be invalid now. Also when p_mco changes. */
8681 if (!(enc_utf8 && ScreenLinesUC == NULL)
8682 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008683#endif
8684 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8685 ScreenLines + LineOffset[old_row],
8686 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008688 if (enc_utf8 && ScreenLinesUC != NULL
8689 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
8691 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8692 ScreenLinesUC + LineOffset[old_row],
8693 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008694 for (i = 0; i < p_mco; ++i)
8695 mch_memmove(new_ScreenLinesC[i]
8696 + new_LineOffset[new_row],
8697 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 (size_t)len * sizeof(u8char_T));
8699 }
8700 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8701 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8702 ScreenLines2 + LineOffset[old_row],
8703 (size_t)len * sizeof(schar_T));
8704#endif
8705 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8706 ScreenAttrs + LineOffset[old_row],
8707 (size_t)len * sizeof(sattr_T));
8708 }
8709 }
8710 }
8711 /* Use the last line of the screen for the current line. */
8712 current_ScreenLine = new_ScreenLines + Rows * Columns;
8713 }
8714
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008715 free_screenlines();
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 ScreenLines = new_ScreenLines;
8718#ifdef FEAT_MBYTE
8719 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008720 for (i = 0; i < p_mco; ++i)
8721 ScreenLinesC[i] = new_ScreenLinesC[i];
8722 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 ScreenLines2 = new_ScreenLines2;
8724#endif
8725 ScreenAttrs = new_ScreenAttrs;
8726 LineOffset = new_LineOffset;
8727 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008728#ifdef FEAT_WINDOWS
8729 TabPageIdxs = new_TabPageIdxs;
8730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731
8732 /* It's important that screen_Rows and screen_Columns reflect the actual
8733 * size of ScreenLines[]. Set them before calling anything. */
8734#ifdef FEAT_GUI
8735 old_Rows = screen_Rows;
8736#endif
8737 screen_Rows = Rows;
8738 screen_Columns = Columns;
8739
8740 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008741 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 screenclear2();
8743
8744#ifdef FEAT_GUI
8745 else if (gui.in_use
8746 && !gui.starting
8747 && ScreenLines != NULL
8748 && old_Rows != Rows)
8749 {
8750 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8751 /*
8752 * Adjust the position of the cursor, for when executing an external
8753 * command.
8754 */
8755 if (msg_row >= Rows) /* Rows got smaller */
8756 msg_row = Rows - 1; /* put cursor at last row */
8757 else if (Rows > old_Rows) /* Rows got bigger */
8758 msg_row += Rows - old_Rows; /* put cursor in same place */
8759 if (msg_col >= Columns) /* Columns got smaller */
8760 msg_col = Columns - 1; /* put cursor at last column */
8761 }
8762#endif
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008765 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008766
8767#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008768 /*
8769 * Do not apply autocommands more than 3 times to avoid an endless loop
8770 * in case applying autocommands always changes Rows or Columns.
8771 */
8772 if (starting == 0 && ++retry_count <= 3)
8773 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008774 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008775 /* In rare cases, autocommands may have altered Rows or Columns,
8776 * jump back to check if we need to allocate the screen again. */
8777 goto retry;
8778 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780}
8781
8782 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008783free_screenlines()
8784{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008785#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008786 int i;
8787
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008788 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008789 for (i = 0; i < Screen_mco; ++i)
8790 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008791 vim_free(ScreenLines2);
8792#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008793 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008794 vim_free(ScreenAttrs);
8795 vim_free(LineOffset);
8796 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008797#ifdef FEAT_WINDOWS
8798 vim_free(TabPageIdxs);
8799#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008800}
8801
8802 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803screenclear()
8804{
8805 check_for_delay(FALSE);
8806 screenalloc(FALSE); /* allocate screen buffers if size changed */
8807 screenclear2(); /* clear the screen */
8808}
8809
8810 static void
8811screenclear2()
8812{
8813 int i;
8814
8815 if (starting == NO_SCREEN || ScreenLines == NULL
8816#ifdef FEAT_GUI
8817 || (gui.in_use && gui.starting)
8818#endif
8819 )
8820 return;
8821
8822#ifdef FEAT_GUI
8823 if (!gui.in_use)
8824#endif
8825 screen_attr = -1; /* force setting the Normal colors */
8826 screen_stop_highlight(); /* don't want highlighting here */
8827
8828#ifdef FEAT_CLIPBOARD
8829 /* disable selection without redrawing it */
8830 clip_scroll_selection(9999);
8831#endif
8832
8833 /* blank out ScreenLines */
8834 for (i = 0; i < Rows; ++i)
8835 {
8836 lineclear(LineOffset[i], (int)Columns);
8837 LineWraps[i] = FALSE;
8838 }
8839
8840 if (can_clear(T_CL))
8841 {
8842 out_str(T_CL); /* clear the display */
8843 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008844 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
8846 else
8847 {
8848 /* can't clear the screen, mark all chars with invalid attributes */
8849 for (i = 0; i < Rows; ++i)
8850 lineinvalid(LineOffset[i], (int)Columns);
8851 clear_cmdline = TRUE;
8852 }
8853
8854 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8855
8856 win_rest_invalid(firstwin);
8857 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008858#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008859 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (must_redraw == CLEAR) /* no need to clear again */
8862 must_redraw = NOT_VALID;
8863 compute_cmdrow();
8864 msg_row = cmdline_row; /* put cursor on last line for messages */
8865 msg_col = 0;
8866 screen_start(); /* don't know where cursor is now */
8867 msg_scrolled = 0; /* can't scroll back */
8868 msg_didany = FALSE;
8869 msg_didout = FALSE;
8870}
8871
8872/*
8873 * Clear one line in ScreenLines.
8874 */
8875 static void
8876lineclear(off, width)
8877 unsigned off;
8878 int width;
8879{
8880 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8881#ifdef FEAT_MBYTE
8882 if (enc_utf8)
8883 (void)vim_memset(ScreenLinesUC + off, 0,
8884 (size_t)width * sizeof(u8char_T));
8885#endif
8886 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8887}
8888
8889/*
8890 * Mark one line in ScreenLines invalid by setting the attributes to an
8891 * invalid value.
8892 */
8893 static void
8894lineinvalid(off, width)
8895 unsigned off;
8896 int width;
8897{
8898 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8899}
8900
8901#ifdef FEAT_VERTSPLIT
8902/*
8903 * Copy part of a Screenline for vertically split window "wp".
8904 */
8905 static void
8906linecopy(to, from, wp)
8907 int to;
8908 int from;
8909 win_T *wp;
8910{
8911 unsigned off_to = LineOffset[to] + wp->w_wincol;
8912 unsigned off_from = LineOffset[from] + wp->w_wincol;
8913
8914 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8915 wp->w_width * sizeof(schar_T));
8916# ifdef FEAT_MBYTE
8917 if (enc_utf8)
8918 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008919 int i;
8920
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8922 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008923 for (i = 0; i < p_mco; ++i)
8924 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8925 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 }
8927 if (enc_dbcs == DBCS_JPNU)
8928 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8929 wp->w_width * sizeof(schar_T));
8930# endif
8931 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8932 wp->w_width * sizeof(sattr_T));
8933}
8934#endif
8935
8936/*
8937 * Return TRUE if clearing with term string "p" would work.
8938 * It can't work when the string is empty or it won't set the right background.
8939 */
8940 int
8941can_clear(p)
8942 char_u *p;
8943{
8944 return (*p != NUL && (t_colors <= 1
8945#ifdef FEAT_GUI
8946 || gui.in_use
8947#endif
8948 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8949}
8950
8951/*
8952 * Reset cursor position. Use whenever cursor was moved because of outputting
8953 * something directly to the screen (shell commands) or a terminal control
8954 * code.
8955 */
8956 void
8957screen_start()
8958{
8959 screen_cur_row = screen_cur_col = 9999;
8960}
8961
8962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 * Move the cursor to position "row","col" in the screen.
8964 * This tries to find the most efficient way to move, minimizing the number of
8965 * characters sent to the terminal.
8966 */
8967 void
8968windgoto(row, col)
8969 int row;
8970 int col;
8971{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008972 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 int i;
8974 int plan;
8975 int cost;
8976 int wouldbe_col;
8977 int noinvcurs;
8978 char_u *bs;
8979 int goto_cost;
8980 int attr;
8981
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008982#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8984
8985#define PLAN_LE 1
8986#define PLAN_CR 2
8987#define PLAN_NL 3
8988#define PLAN_WRITE 4
8989 /* Can't use ScreenLines unless initialized */
8990 if (ScreenLines == NULL)
8991 return;
8992
8993 if (col != screen_cur_col || row != screen_cur_row)
8994 {
8995 /* Check for valid position. */
8996 if (row < 0) /* window without text lines? */
8997 row = 0;
8998 if (row >= screen_Rows)
8999 row = screen_Rows - 1;
9000 if (col >= screen_Columns)
9001 col = screen_Columns - 1;
9002
9003 /* check if no cursor movement is allowed in highlight mode */
9004 if (screen_attr && *T_MS == NUL)
9005 noinvcurs = HIGHL_COST;
9006 else
9007 noinvcurs = 0;
9008 goto_cost = GOTO_COST + noinvcurs;
9009
9010 /*
9011 * Plan how to do the positioning:
9012 * 1. Use CR to move it to column 0, same row.
9013 * 2. Use T_LE to move it a few columns to the left.
9014 * 3. Use NL to move a few lines down, column 0.
9015 * 4. Move a few columns to the right with T_ND or by writing chars.
9016 *
9017 * Don't do this if the cursor went beyond the last column, the cursor
9018 * position is unknown then (some terminals wrap, some don't )
9019 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009020 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 * characters to move the cursor to the right.
9022 */
9023 if (row >= screen_cur_row && screen_cur_col < Columns)
9024 {
9025 /*
9026 * If the cursor is in the same row, bigger col, we can use CR
9027 * or T_LE.
9028 */
9029 bs = NULL; /* init for GCC */
9030 attr = screen_attr;
9031 if (row == screen_cur_row && col < screen_cur_col)
9032 {
9033 /* "le" is preferred over "bc", because "bc" is obsolete */
9034 if (*T_LE)
9035 bs = T_LE; /* "cursor left" */
9036 else
9037 bs = T_BC; /* "backspace character (old) */
9038 if (*bs)
9039 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9040 else
9041 cost = 999;
9042 if (col + 1 < cost) /* using CR is less characters */
9043 {
9044 plan = PLAN_CR;
9045 wouldbe_col = 0;
9046 cost = 1; /* CR is just one character */
9047 }
9048 else
9049 {
9050 plan = PLAN_LE;
9051 wouldbe_col = col;
9052 }
9053 if (noinvcurs) /* will stop highlighting */
9054 {
9055 cost += noinvcurs;
9056 attr = 0;
9057 }
9058 }
9059
9060 /*
9061 * If the cursor is above where we want to be, we can use CR LF.
9062 */
9063 else if (row > screen_cur_row)
9064 {
9065 plan = PLAN_NL;
9066 wouldbe_col = 0;
9067 cost = (row - screen_cur_row) * 2; /* CR LF */
9068 if (noinvcurs) /* will stop highlighting */
9069 {
9070 cost += noinvcurs;
9071 attr = 0;
9072 }
9073 }
9074
9075 /*
9076 * If the cursor is in the same row, smaller col, just use write.
9077 */
9078 else
9079 {
9080 plan = PLAN_WRITE;
9081 wouldbe_col = screen_cur_col;
9082 cost = 0;
9083 }
9084
9085 /*
9086 * Check if any characters that need to be written have the
9087 * correct attributes. Also avoid UTF-8 characters.
9088 */
9089 i = col - wouldbe_col;
9090 if (i > 0)
9091 cost += i;
9092 if (cost < goto_cost && i > 0)
9093 {
9094 /*
9095 * Check if the attributes are correct without additionally
9096 * stopping highlighting.
9097 */
9098 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9099 while (i && *p++ == attr)
9100 --i;
9101 if (i != 0)
9102 {
9103 /*
9104 * Try if it works when highlighting is stopped here.
9105 */
9106 if (*--p == 0)
9107 {
9108 cost += noinvcurs;
9109 while (i && *p++ == 0)
9110 --i;
9111 }
9112 if (i != 0)
9113 cost = 999; /* different attributes, don't do it */
9114 }
9115#ifdef FEAT_MBYTE
9116 if (enc_utf8)
9117 {
9118 /* Don't use an UTF-8 char for positioning, it's slow. */
9119 for (i = wouldbe_col; i < col; ++i)
9120 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9121 {
9122 cost = 999;
9123 break;
9124 }
9125 }
9126#endif
9127 }
9128
9129 /*
9130 * We can do it without term_windgoto()!
9131 */
9132 if (cost < goto_cost)
9133 {
9134 if (plan == PLAN_LE)
9135 {
9136 if (noinvcurs)
9137 screen_stop_highlight();
9138 while (screen_cur_col > col)
9139 {
9140 out_str(bs);
9141 --screen_cur_col;
9142 }
9143 }
9144 else if (plan == PLAN_CR)
9145 {
9146 if (noinvcurs)
9147 screen_stop_highlight();
9148 out_char('\r');
9149 screen_cur_col = 0;
9150 }
9151 else if (plan == PLAN_NL)
9152 {
9153 if (noinvcurs)
9154 screen_stop_highlight();
9155 while (screen_cur_row < row)
9156 {
9157 out_char('\n');
9158 ++screen_cur_row;
9159 }
9160 screen_cur_col = 0;
9161 }
9162
9163 i = col - screen_cur_col;
9164 if (i > 0)
9165 {
9166 /*
9167 * Use cursor-right if it's one character only. Avoids
9168 * removing a line of pixels from the last bold char, when
9169 * using the bold trick in the GUI.
9170 */
9171 if (T_ND[0] != NUL && T_ND[1] == NUL)
9172 {
9173 while (i-- > 0)
9174 out_char(*T_ND);
9175 }
9176 else
9177 {
9178 int off;
9179
9180 off = LineOffset[row] + screen_cur_col;
9181 while (i-- > 0)
9182 {
9183 if (ScreenAttrs[off] != screen_attr)
9184 screen_stop_highlight();
9185#ifdef FEAT_MBYTE
9186 out_flush_check();
9187#endif
9188 out_char(ScreenLines[off]);
9189#ifdef FEAT_MBYTE
9190 if (enc_dbcs == DBCS_JPNU
9191 && ScreenLines[off] == 0x8e)
9192 out_char(ScreenLines2[off]);
9193#endif
9194 ++off;
9195 }
9196 }
9197 }
9198 }
9199 }
9200 else
9201 cost = 999;
9202
9203 if (cost >= goto_cost)
9204 {
9205 if (noinvcurs)
9206 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009207 if (row == screen_cur_row && (col > screen_cur_col)
9208 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 term_cursor_right(col - screen_cur_col);
9210 else
9211 term_windgoto(row, col);
9212 }
9213 screen_cur_row = row;
9214 screen_cur_col = col;
9215 }
9216}
9217
9218/*
9219 * Set cursor to its position in the current window.
9220 */
9221 void
9222setcursor()
9223{
9224 if (redrawing())
9225 {
9226 validate_cursor();
9227 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9228 W_WINCOL(curwin) + (
9229#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009230 /* With 'rightleft' set and the cursor on a double-wide
9231 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9233# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009234 (has_mbyte
9235 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9236 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237# endif
9238 1)) :
9239#endif
9240 curwin->w_wcol));
9241 }
9242}
9243
9244
9245/*
9246 * insert 'line_count' lines at 'row' in window 'wp'
9247 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9248 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9249 * scrolling.
9250 * Returns FAIL if the lines are not inserted, OK for success.
9251 */
9252 int
9253win_ins_lines(wp, row, line_count, invalid, mayclear)
9254 win_T *wp;
9255 int row;
9256 int line_count;
9257 int invalid;
9258 int mayclear;
9259{
9260 int did_delete;
9261 int nextrow;
9262 int lastrow;
9263 int retval;
9264
9265 if (invalid)
9266 wp->w_lines_valid = 0;
9267
9268 if (wp->w_height < 5)
9269 return FAIL;
9270
9271 if (line_count > wp->w_height - row)
9272 line_count = wp->w_height - row;
9273
9274 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9275 if (retval != MAYBE)
9276 return retval;
9277
9278 /*
9279 * If there is a next window or a status line, we first try to delete the
9280 * lines at the bottom to avoid messing what is after the window.
9281 * If this fails and there are following windows, don't do anything to avoid
9282 * messing up those windows, better just redraw.
9283 */
9284 did_delete = FALSE;
9285#ifdef FEAT_WINDOWS
9286 if (wp->w_next != NULL || wp->w_status_height)
9287 {
9288 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9289 line_count, (int)Rows, FALSE, NULL) == OK)
9290 did_delete = TRUE;
9291 else if (wp->w_next)
9292 return FAIL;
9293 }
9294#endif
9295 /*
9296 * if no lines deleted, blank the lines that will end up below the window
9297 */
9298 if (!did_delete)
9299 {
9300#ifdef FEAT_WINDOWS
9301 wp->w_redr_status = TRUE;
9302#endif
9303 redraw_cmdline = TRUE;
9304 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9305 lastrow = nextrow + line_count;
9306 if (lastrow > Rows)
9307 lastrow = Rows;
9308 screen_fill(nextrow - line_count, lastrow - line_count,
9309 W_WINCOL(wp), (int)W_ENDCOL(wp),
9310 ' ', ' ', 0);
9311 }
9312
9313 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9314 == FAIL)
9315 {
9316 /* deletion will have messed up other windows */
9317 if (did_delete)
9318 {
9319#ifdef FEAT_WINDOWS
9320 wp->w_redr_status = TRUE;
9321#endif
9322 win_rest_invalid(W_NEXT(wp));
9323 }
9324 return FAIL;
9325 }
9326
9327 return OK;
9328}
9329
9330/*
9331 * delete "line_count" window lines at "row" in window "wp"
9332 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9333 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9334 * scrolling
9335 * Return OK for success, FAIL if the lines are not deleted.
9336 */
9337 int
9338win_del_lines(wp, row, line_count, invalid, mayclear)
9339 win_T *wp;
9340 int row;
9341 int line_count;
9342 int invalid;
9343 int mayclear;
9344{
9345 int retval;
9346
9347 if (invalid)
9348 wp->w_lines_valid = 0;
9349
9350 if (line_count > wp->w_height - row)
9351 line_count = wp->w_height - row;
9352
9353 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9354 if (retval != MAYBE)
9355 return retval;
9356
9357 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9358 (int)Rows, FALSE, NULL) == FAIL)
9359 return FAIL;
9360
9361#ifdef FEAT_WINDOWS
9362 /*
9363 * If there are windows or status lines below, try to put them at the
9364 * correct place. If we can't do that, they have to be redrawn.
9365 */
9366 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9367 {
9368 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9369 line_count, (int)Rows, NULL) == FAIL)
9370 {
9371 wp->w_redr_status = TRUE;
9372 win_rest_invalid(wp->w_next);
9373 }
9374 }
9375 /*
9376 * If this is the last window and there is no status line, redraw the
9377 * command line later.
9378 */
9379 else
9380#endif
9381 redraw_cmdline = TRUE;
9382 return OK;
9383}
9384
9385/*
9386 * Common code for win_ins_lines() and win_del_lines().
9387 * Returns OK or FAIL when the work has been done.
9388 * Returns MAYBE when not finished yet.
9389 */
9390 static int
9391win_do_lines(wp, row, line_count, mayclear, del)
9392 win_T *wp;
9393 int row;
9394 int line_count;
9395 int mayclear;
9396 int del;
9397{
9398 int retval;
9399
9400 if (!redrawing() || line_count <= 0)
9401 return FAIL;
9402
9403 /* only a few lines left: redraw is faster */
9404 if (mayclear && Rows - line_count < 5
9405#ifdef FEAT_VERTSPLIT
9406 && wp->w_width == Columns
9407#endif
9408 )
9409 {
9410 screenclear(); /* will set wp->w_lines_valid to 0 */
9411 return FAIL;
9412 }
9413
9414 /*
9415 * Delete all remaining lines
9416 */
9417 if (row + line_count >= wp->w_height)
9418 {
9419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9420 W_WINCOL(wp), (int)W_ENDCOL(wp),
9421 ' ', ' ', 0);
9422 return OK;
9423 }
9424
9425 /*
9426 * when scrolling, the message on the command line should be cleared,
9427 * otherwise it will stay there forever.
9428 */
9429 clear_cmdline = TRUE;
9430
9431 /*
9432 * If the terminal can set a scroll region, use that.
9433 * Always do this in a vertically split window. This will redraw from
9434 * ScreenLines[] when t_CV isn't defined. That's faster than using
9435 * win_line().
9436 * Don't use a scroll region when we are going to redraw the text, writing
9437 * a character in the lower right corner of the scroll region causes a
9438 * scroll-up in the DJGPP version.
9439 */
9440 if (scroll_region
9441#ifdef FEAT_VERTSPLIT
9442 || W_WIDTH(wp) != Columns
9443#endif
9444 )
9445 {
9446#ifdef FEAT_VERTSPLIT
9447 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9448#endif
9449 scroll_region_set(wp, row);
9450 if (del)
9451 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9452 wp->w_height - row, FALSE, wp);
9453 else
9454 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9455 wp->w_height - row, wp);
9456#ifdef FEAT_VERTSPLIT
9457 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9458#endif
9459 scroll_region_reset();
9460 return retval;
9461 }
9462
9463#ifdef FEAT_WINDOWS
9464 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9465 return FAIL;
9466#endif
9467
9468 return MAYBE;
9469}
9470
9471/*
9472 * window 'wp' and everything after it is messed up, mark it for redraw
9473 */
9474 static void
9475win_rest_invalid(wp)
9476 win_T *wp;
9477{
9478#ifdef FEAT_WINDOWS
9479 while (wp != NULL)
9480#else
9481 if (wp != NULL)
9482#endif
9483 {
9484 redraw_win_later(wp, NOT_VALID);
9485#ifdef FEAT_WINDOWS
9486 wp->w_redr_status = TRUE;
9487 wp = wp->w_next;
9488#endif
9489 }
9490 redraw_cmdline = TRUE;
9491}
9492
9493/*
9494 * The rest of the routines in this file perform screen manipulations. The
9495 * given operation is performed physically on the screen. The corresponding
9496 * change is also made to the internal screen image. In this way, the editor
9497 * anticipates the effect of editing changes on the appearance of the screen.
9498 * That way, when we call screenupdate a complete redraw isn't usually
9499 * necessary. Another advantage is that we can keep adding code to anticipate
9500 * screen changes, and in the meantime, everything still works.
9501 */
9502
9503/*
9504 * types for inserting or deleting lines
9505 */
9506#define USE_T_CAL 1
9507#define USE_T_CDL 2
9508#define USE_T_AL 3
9509#define USE_T_CE 4
9510#define USE_T_DL 5
9511#define USE_T_SR 6
9512#define USE_NL 7
9513#define USE_T_CD 8
9514#define USE_REDRAW 9
9515
9516/*
9517 * insert lines on the screen and update ScreenLines[]
9518 * 'end' is the line after the scrolled part. Normally it is Rows.
9519 * When scrolling region used 'off' is the offset from the top for the region.
9520 * 'row' and 'end' are relative to the start of the region.
9521 *
9522 * return FAIL for failure, OK for success.
9523 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009524 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525screen_ins_lines(off, row, line_count, end, wp)
9526 int off;
9527 int row;
9528 int line_count;
9529 int end;
9530 win_T *wp; /* NULL or window to use width from */
9531{
9532 int i;
9533 int j;
9534 unsigned temp;
9535 int cursor_row;
9536 int type;
9537 int result_empty;
9538 int can_ce = can_clear(T_CE);
9539
9540 /*
9541 * FAIL if
9542 * - there is no valid screen
9543 * - the screen has to be redrawn completely
9544 * - the line count is less than one
9545 * - the line count is more than 'ttyscroll'
9546 */
9547 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9548 return FAIL;
9549
9550 /*
9551 * There are seven ways to insert lines:
9552 * 0. When in a vertically split window and t_CV isn't set, redraw the
9553 * characters from ScreenLines[].
9554 * 1. Use T_CD (clear to end of display) if it exists and the result of
9555 * the insert is just empty lines
9556 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9557 * present or line_count > 1. It looks better if we do all the inserts
9558 * at once.
9559 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9560 * insert is just empty lines and T_CE is not present or line_count >
9561 * 1.
9562 * 4. Use T_AL (insert line) if it exists.
9563 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9564 * just empty lines.
9565 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9566 * just empty lines.
9567 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9568 * the 'da' flag is not set or we have clear line capability.
9569 * 8. redraw the characters from ScreenLines[].
9570 *
9571 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9572 * the scrollbar for the window. It does have insert line, use that if it
9573 * exists.
9574 */
9575 result_empty = (row + line_count >= end);
9576#ifdef FEAT_VERTSPLIT
9577 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9578 type = USE_REDRAW;
9579 else
9580#endif
9581 if (can_clear(T_CD) && result_empty)
9582 type = USE_T_CD;
9583 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9584 type = USE_T_CAL;
9585 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9586 type = USE_T_CDL;
9587 else if (*T_AL != NUL)
9588 type = USE_T_AL;
9589 else if (can_ce && result_empty)
9590 type = USE_T_CE;
9591 else if (*T_DL != NUL && result_empty)
9592 type = USE_T_DL;
9593 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9594 type = USE_T_SR;
9595 else
9596 return FAIL;
9597
9598 /*
9599 * For clearing the lines screen_del_lines() is used. This will also take
9600 * care of t_db if necessary.
9601 */
9602 if (type == USE_T_CD || type == USE_T_CDL ||
9603 type == USE_T_CE || type == USE_T_DL)
9604 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9605
9606 /*
9607 * If text is retained below the screen, first clear or delete as many
9608 * lines at the bottom of the window as are about to be inserted so that
9609 * the deleted lines won't later surface during a screen_del_lines.
9610 */
9611 if (*T_DB)
9612 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9613
9614#ifdef FEAT_CLIPBOARD
9615 /* Remove a modeless selection when inserting lines halfway the screen
9616 * or not the full width of the screen. */
9617 if (off + row > 0
9618# ifdef FEAT_VERTSPLIT
9619 || (wp != NULL && wp->w_width != Columns)
9620# endif
9621 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009622 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 else
9624 clip_scroll_selection(-line_count);
9625#endif
9626
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#ifdef FEAT_GUI
9628 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9629 * scrolling is actually carried out. */
9630 gui_dont_update_cursor();
9631#endif
9632
9633 if (*T_CCS != NUL) /* cursor relative to region */
9634 cursor_row = row;
9635 else
9636 cursor_row = row + off;
9637
9638 /*
9639 * Shift LineOffset[] line_count down to reflect the inserted lines.
9640 * Clear the inserted lines in ScreenLines[].
9641 */
9642 row += off;
9643 end += off;
9644 for (i = 0; i < line_count; ++i)
9645 {
9646#ifdef FEAT_VERTSPLIT
9647 if (wp != NULL && wp->w_width != Columns)
9648 {
9649 /* need to copy part of a line */
9650 j = end - 1 - i;
9651 while ((j -= line_count) >= row)
9652 linecopy(j + line_count, j, wp);
9653 j += line_count;
9654 if (can_clear((char_u *)" "))
9655 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9656 else
9657 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9658 LineWraps[j] = FALSE;
9659 }
9660 else
9661#endif
9662 {
9663 j = end - 1 - i;
9664 temp = LineOffset[j];
9665 while ((j -= line_count) >= row)
9666 {
9667 LineOffset[j + line_count] = LineOffset[j];
9668 LineWraps[j + line_count] = LineWraps[j];
9669 }
9670 LineOffset[j + line_count] = temp;
9671 LineWraps[j + line_count] = FALSE;
9672 if (can_clear((char_u *)" "))
9673 lineclear(temp, (int)Columns);
9674 else
9675 lineinvalid(temp, (int)Columns);
9676 }
9677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678
9679 screen_stop_highlight();
9680 windgoto(cursor_row, 0);
9681
9682#ifdef FEAT_VERTSPLIT
9683 /* redraw the characters */
9684 if (type == USE_REDRAW)
9685 redraw_block(row, end, wp);
9686 else
9687#endif
9688 if (type == USE_T_CAL)
9689 {
9690 term_append_lines(line_count);
9691 screen_start(); /* don't know where cursor is now */
9692 }
9693 else
9694 {
9695 for (i = 0; i < line_count; i++)
9696 {
9697 if (type == USE_T_AL)
9698 {
9699 if (i && cursor_row != 0)
9700 windgoto(cursor_row, 0);
9701 out_str(T_AL);
9702 }
9703 else /* type == USE_T_SR */
9704 out_str(T_SR);
9705 screen_start(); /* don't know where cursor is now */
9706 }
9707 }
9708
9709 /*
9710 * With scroll-reverse and 'da' flag set we need to clear the lines that
9711 * have been scrolled down into the region.
9712 */
9713 if (type == USE_T_SR && *T_DA)
9714 {
9715 for (i = 0; i < line_count; ++i)
9716 {
9717 windgoto(off + i, 0);
9718 out_str(T_CE);
9719 screen_start(); /* don't know where cursor is now */
9720 }
9721 }
9722
9723#ifdef FEAT_GUI
9724 gui_can_update_cursor();
9725 if (gui.in_use)
9726 out_flush(); /* always flush after a scroll */
9727#endif
9728 return OK;
9729}
9730
9731/*
9732 * delete lines on the screen and update ScreenLines[]
9733 * 'end' is the line after the scrolled part. Normally it is Rows.
9734 * When scrolling region used 'off' is the offset from the top for the region.
9735 * 'row' and 'end' are relative to the start of the region.
9736 *
9737 * Return OK for success, FAIL if the lines are not deleted.
9738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 int
9740screen_del_lines(off, row, line_count, end, force, wp)
9741 int off;
9742 int row;
9743 int line_count;
9744 int end;
9745 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009746 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747{
9748 int j;
9749 int i;
9750 unsigned temp;
9751 int cursor_row;
9752 int cursor_end;
9753 int result_empty; /* result is empty until end of region */
9754 int can_delete; /* deleting line codes can be used */
9755 int type;
9756
9757 /*
9758 * FAIL if
9759 * - there is no valid screen
9760 * - the screen has to be redrawn completely
9761 * - the line count is less than one
9762 * - the line count is more than 'ttyscroll'
9763 */
9764 if (!screen_valid(TRUE) || line_count <= 0 ||
9765 (!force && line_count > p_ttyscroll))
9766 return FAIL;
9767
9768 /*
9769 * Check if the rest of the current region will become empty.
9770 */
9771 result_empty = row + line_count >= end;
9772
9773 /*
9774 * We can delete lines only when 'db' flag not set or when 'ce' option
9775 * available.
9776 */
9777 can_delete = (*T_DB == NUL || can_clear(T_CE));
9778
9779 /*
9780 * There are six ways to delete lines:
9781 * 0. When in a vertically split window and t_CV isn't set, redraw the
9782 * characters from ScreenLines[].
9783 * 1. Use T_CD if it exists and the result is empty.
9784 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9785 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9786 * none of the other ways work.
9787 * 4. Use T_CE (erase line) if the result is empty.
9788 * 5. Use T_DL (delete line) if it exists.
9789 * 6. redraw the characters from ScreenLines[].
9790 */
9791#ifdef FEAT_VERTSPLIT
9792 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9793 type = USE_REDRAW;
9794 else
9795#endif
9796 if (can_clear(T_CD) && result_empty)
9797 type = USE_T_CD;
9798#if defined(__BEOS__) && defined(BEOS_DR8)
9799 /*
9800 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9801 * its internal termcap... this works okay for tests which test *T_DB !=
9802 * NUL. It has the disadvantage that the user cannot use any :set t_*
9803 * command to get T_DB (back) to empty_option, only :set term=... will do
9804 * the trick...
9805 * Anyway, this hack will hopefully go away with the next OS release.
9806 * (Olaf Seibert)
9807 */
9808 else if (row == 0 && T_DB == empty_option
9809 && (line_count == 1 || *T_CDL == NUL))
9810#else
9811 else if (row == 0 && (
9812#ifndef AMIGA
9813 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9814 * up, so use delete-line command */
9815 line_count == 1 ||
9816#endif
9817 *T_CDL == NUL))
9818#endif
9819 type = USE_NL;
9820 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9821 type = USE_T_CDL;
9822 else if (can_clear(T_CE) && result_empty
9823#ifdef FEAT_VERTSPLIT
9824 && (wp == NULL || wp->w_width == Columns)
9825#endif
9826 )
9827 type = USE_T_CE;
9828 else if (*T_DL != NUL && can_delete)
9829 type = USE_T_DL;
9830 else if (*T_CDL != NUL && can_delete)
9831 type = USE_T_CDL;
9832 else
9833 return FAIL;
9834
9835#ifdef FEAT_CLIPBOARD
9836 /* Remove a modeless selection when deleting lines halfway the screen or
9837 * not the full width of the screen. */
9838 if (off + row > 0
9839# ifdef FEAT_VERTSPLIT
9840 || (wp != NULL && wp->w_width != Columns)
9841# endif
9842 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009843 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 else
9845 clip_scroll_selection(line_count);
9846#endif
9847
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848#ifdef FEAT_GUI
9849 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9850 * scrolling is actually carried out. */
9851 gui_dont_update_cursor();
9852#endif
9853
9854 if (*T_CCS != NUL) /* cursor relative to region */
9855 {
9856 cursor_row = row;
9857 cursor_end = end;
9858 }
9859 else
9860 {
9861 cursor_row = row + off;
9862 cursor_end = end + off;
9863 }
9864
9865 /*
9866 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9867 * Clear the inserted lines in ScreenLines[].
9868 */
9869 row += off;
9870 end += off;
9871 for (i = 0; i < line_count; ++i)
9872 {
9873#ifdef FEAT_VERTSPLIT
9874 if (wp != NULL && wp->w_width != Columns)
9875 {
9876 /* need to copy part of a line */
9877 j = row + i;
9878 while ((j += line_count) <= end - 1)
9879 linecopy(j - line_count, j, wp);
9880 j -= line_count;
9881 if (can_clear((char_u *)" "))
9882 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9883 else
9884 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9885 LineWraps[j] = FALSE;
9886 }
9887 else
9888#endif
9889 {
9890 /* whole width, moving the line pointers is faster */
9891 j = row + i;
9892 temp = LineOffset[j];
9893 while ((j += line_count) <= end - 1)
9894 {
9895 LineOffset[j - line_count] = LineOffset[j];
9896 LineWraps[j - line_count] = LineWraps[j];
9897 }
9898 LineOffset[j - line_count] = temp;
9899 LineWraps[j - line_count] = FALSE;
9900 if (can_clear((char_u *)" "))
9901 lineclear(temp, (int)Columns);
9902 else
9903 lineinvalid(temp, (int)Columns);
9904 }
9905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
9907 screen_stop_highlight();
9908
9909#ifdef FEAT_VERTSPLIT
9910 /* redraw the characters */
9911 if (type == USE_REDRAW)
9912 redraw_block(row, end, wp);
9913 else
9914#endif
9915 if (type == USE_T_CD) /* delete the lines */
9916 {
9917 windgoto(cursor_row, 0);
9918 out_str(T_CD);
9919 screen_start(); /* don't know where cursor is now */
9920 }
9921 else if (type == USE_T_CDL)
9922 {
9923 windgoto(cursor_row, 0);
9924 term_delete_lines(line_count);
9925 screen_start(); /* don't know where cursor is now */
9926 }
9927 /*
9928 * Deleting lines at top of the screen or scroll region: Just scroll
9929 * the whole screen (scroll region) up by outputting newlines on the
9930 * last line.
9931 */
9932 else if (type == USE_NL)
9933 {
9934 windgoto(cursor_end - 1, 0);
9935 for (i = line_count; --i >= 0; )
9936 out_char('\n'); /* cursor will remain on same line */
9937 }
9938 else
9939 {
9940 for (i = line_count; --i >= 0; )
9941 {
9942 if (type == USE_T_DL)
9943 {
9944 windgoto(cursor_row, 0);
9945 out_str(T_DL); /* delete a line */
9946 }
9947 else /* type == USE_T_CE */
9948 {
9949 windgoto(cursor_row + i, 0);
9950 out_str(T_CE); /* erase a line */
9951 }
9952 screen_start(); /* don't know where cursor is now */
9953 }
9954 }
9955
9956 /*
9957 * If the 'db' flag is set, we need to clear the lines that have been
9958 * scrolled up at the bottom of the region.
9959 */
9960 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9961 {
9962 for (i = line_count; i > 0; --i)
9963 {
9964 windgoto(cursor_end - i, 0);
9965 out_str(T_CE); /* erase a line */
9966 screen_start(); /* don't know where cursor is now */
9967 }
9968 }
9969
9970#ifdef FEAT_GUI
9971 gui_can_update_cursor();
9972 if (gui.in_use)
9973 out_flush(); /* always flush after a scroll */
9974#endif
9975
9976 return OK;
9977}
9978
9979/*
9980 * show the current mode and ruler
9981 *
9982 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9983 * If clear_cmdline is FALSE there may be a message there that needs to be
9984 * cleared only if a mode is shown.
9985 * Return the length of the message (0 if no message).
9986 */
9987 int
9988showmode()
9989{
9990 int need_clear;
9991 int length = 0;
9992 int do_mode;
9993 int attr;
9994 int nwr_save;
9995#ifdef FEAT_INS_EXPAND
9996 int sub_attr;
9997#endif
9998
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009999 do_mode = ((p_smd && msg_silent == 0)
10000 && ((State & INSERT)
10001 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010002 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 if (do_mode || Recording)
10004 {
10005 /*
10006 * Don't show mode right now, when not redrawing or inside a mapping.
10007 * Call char_avail() only when we are going to show something, because
10008 * it takes a bit of time.
10009 */
10010 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10011 {
10012 redraw_cmdline = TRUE; /* show mode later */
10013 return 0;
10014 }
10015
10016 nwr_save = need_wait_return;
10017
10018 /* wait a bit before overwriting an important message */
10019 check_for_delay(FALSE);
10020
10021 /* if the cmdline is more than one line high, erase top lines */
10022 need_clear = clear_cmdline;
10023 if (clear_cmdline && cmdline_row < Rows - 1)
10024 msg_clr_cmdline(); /* will reset clear_cmdline */
10025
10026 /* Position on the last line in the window, column 0 */
10027 msg_pos_mode();
10028 cursor_off();
10029 attr = hl_attr(HLF_CM); /* Highlight mode */
10030 if (do_mode)
10031 {
10032 MSG_PUTS_ATTR("--", attr);
10033#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010034 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010035# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010036 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010037# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010038 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010039# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010040 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010041# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 MSG_PUTS_ATTR(" IM", attr);
10043# else
10044 MSG_PUTS_ATTR(" XIM", attr);
10045# endif
10046#endif
10047#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10048 if (gui.in_use)
10049 {
10050 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010051 {
10052 /* HANGUL */
10053 if (enc_utf8)
10054 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10055 else
10056 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 }
10059#endif
10060#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010061 /* CTRL-X in Insert mode */
10062 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
10064 /* These messages can get long, avoid a wrap in a narrow
10065 * window. Prefer showing edit_submode_extra. */
10066 length = (Rows - msg_row) * Columns - 3;
10067 if (edit_submode_extra != NULL)
10068 length -= vim_strsize(edit_submode_extra);
10069 if (length > 0)
10070 {
10071 if (edit_submode_pre != NULL)
10072 length -= vim_strsize(edit_submode_pre);
10073 if (length - vim_strsize(edit_submode) > 0)
10074 {
10075 if (edit_submode_pre != NULL)
10076 msg_puts_attr(edit_submode_pre, attr);
10077 msg_puts_attr(edit_submode, attr);
10078 }
10079 if (edit_submode_extra != NULL)
10080 {
10081 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10082 if ((int)edit_submode_highl < (int)HLF_COUNT)
10083 sub_attr = hl_attr(edit_submode_highl);
10084 else
10085 sub_attr = attr;
10086 msg_puts_attr(edit_submode_extra, sub_attr);
10087 }
10088 }
10089 length = 0;
10090 }
10091 else
10092#endif
10093 {
10094#ifdef FEAT_VREPLACE
10095 if (State & VREPLACE_FLAG)
10096 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10097 else
10098#endif
10099 if (State & REPLACE_FLAG)
10100 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10101 else if (State & INSERT)
10102 {
10103#ifdef FEAT_RIGHTLEFT
10104 if (p_ri)
10105 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10106#endif
10107 MSG_PUTS_ATTR(_(" INSERT"), attr);
10108 }
10109 else if (restart_edit == 'I')
10110 MSG_PUTS_ATTR(_(" (insert)"), attr);
10111 else if (restart_edit == 'R')
10112 MSG_PUTS_ATTR(_(" (replace)"), attr);
10113 else if (restart_edit == 'V')
10114 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10115#ifdef FEAT_RIGHTLEFT
10116 if (p_hkmap)
10117 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10118# ifdef FEAT_FKMAP
10119 if (p_fkmap)
10120 MSG_PUTS_ATTR(farsi_text_5, attr);
10121# endif
10122#endif
10123#ifdef FEAT_KEYMAP
10124 if (State & LANGMAP)
10125 {
10126# ifdef FEAT_ARABIC
10127 if (curwin->w_p_arab)
10128 MSG_PUTS_ATTR(_(" Arabic"), attr);
10129 else
10130# endif
10131 MSG_PUTS_ATTR(_(" (lang)"), attr);
10132 }
10133#endif
10134 if ((State & INSERT) && p_paste)
10135 MSG_PUTS_ATTR(_(" (paste)"), attr);
10136
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (VIsual_active)
10138 {
10139 char *p;
10140
10141 /* Don't concatenate separate words to avoid translation
10142 * problems. */
10143 switch ((VIsual_select ? 4 : 0)
10144 + (VIsual_mode == Ctrl_V) * 2
10145 + (VIsual_mode == 'V'))
10146 {
10147 case 0: p = N_(" VISUAL"); break;
10148 case 1: p = N_(" VISUAL LINE"); break;
10149 case 2: p = N_(" VISUAL BLOCK"); break;
10150 case 4: p = N_(" SELECT"); break;
10151 case 5: p = N_(" SELECT LINE"); break;
10152 default: p = N_(" SELECT BLOCK"); break;
10153 }
10154 MSG_PUTS_ATTR(_(p), attr);
10155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 MSG_PUTS_ATTR(" --", attr);
10157 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010158
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 need_clear = TRUE;
10160 }
10161 if (Recording
10162#ifdef FEAT_INS_EXPAND
10163 && edit_submode == NULL /* otherwise it gets too long */
10164#endif
10165 )
10166 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010167 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 need_clear = TRUE;
10169 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010170
10171 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 if (need_clear || clear_cmdline)
10173 msg_clr_eos();
10174 msg_didout = FALSE; /* overwrite this message */
10175 length = msg_col;
10176 msg_col = 0;
10177 need_wait_return = nwr_save; /* never ask for hit-return for this */
10178 }
10179 else if (clear_cmdline && msg_silent == 0)
10180 /* Clear the whole command line. Will reset "clear_cmdline". */
10181 msg_clr_cmdline();
10182
10183#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 /* In Visual mode the size of the selected area must be redrawn. */
10185 if (VIsual_active)
10186 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187
10188 /* If the last window has no status line, the ruler is after the mode
10189 * message and must be redrawn */
10190 if (redrawing()
10191# ifdef FEAT_WINDOWS
10192 && lastwin->w_status_height == 0
10193# endif
10194 )
10195 win_redr_ruler(lastwin, TRUE);
10196#endif
10197 redraw_cmdline = FALSE;
10198 clear_cmdline = FALSE;
10199
10200 return length;
10201}
10202
10203/*
10204 * Position for a mode message.
10205 */
10206 static void
10207msg_pos_mode()
10208{
10209 msg_col = 0;
10210 msg_row = Rows - 1;
10211}
10212
10213/*
10214 * Delete mode message. Used when ESC is typed which is expected to end
10215 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010216 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 */
10218 void
10219unshowmode(force)
10220 int force;
10221{
10222 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010223 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 */
10225 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10226 redraw_cmdline = TRUE; /* delete mode later */
10227 else
10228 {
10229 msg_pos_mode();
10230 if (Recording)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010231 recording_mode(hl_attr(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 msg_clr_eos();
10233 }
10234}
10235
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010236 static void
10237recording_mode(attr)
10238 int attr;
10239{
10240 MSG_PUTS_ATTR(_("recording"), attr);
10241 if (!shortmess(SHM_RECORDING))
10242 {
10243 char_u s[4];
10244 sprintf((char *)s, " @%c", Recording);
10245 MSG_PUTS_ATTR(s, attr);
10246 }
10247}
10248
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010249#if defined(FEAT_WINDOWS)
10250/*
10251 * Draw the tab pages line at the top of the Vim window.
10252 */
10253 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010254draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010255{
10256 int tabcount = 0;
10257 tabpage_T *tp;
10258 int tabwidth;
10259 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010260 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010261 int attr;
10262 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010263 win_T *cwp;
10264 int wincount;
10265 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010266 int c;
10267 int len;
10268 int attr_sel = hl_attr(HLF_TPS);
10269 int attr_nosel = hl_attr(HLF_TP);
10270 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010271 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010272 int room;
10273 int use_sep_chars = (t_colors < 8
10274#ifdef FEAT_GUI
10275 && !gui.in_use
10276#endif
10277 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010278
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010279 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010280
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010281#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010282 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010283 if (gui_use_tabline())
10284 {
10285 gui_update_tabline();
10286 return;
10287 }
10288#endif
10289
10290 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010291 return;
10292
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010293#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010294
10295 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10296 for (scol = 0; scol < Columns; ++scol)
10297 TabPageIdxs[scol] = 0;
10298
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010299 /* Use the 'tabline' option if it's set. */
10300 if (*p_tal != NUL)
10301 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010302 int save_called_emsg = called_emsg;
10303
10304 /* Check for an error. If there is one we would loop in redrawing the
10305 * screen. Avoid that by making 'tabline' empty. */
10306 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010307 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010308 if (called_emsg)
10309 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010310 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010311 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010312 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010313 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010314#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010315 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010316 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10317 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010318
Bram Moolenaar238a5642006-02-21 22:12:05 +000010319 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10320 if (tabwidth < 6)
10321 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010322
Bram Moolenaar238a5642006-02-21 22:12:05 +000010323 attr = attr_nosel;
10324 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010325 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010326 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10327 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010328 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010329 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010330
Bram Moolenaar238a5642006-02-21 22:12:05 +000010331 if (tp->tp_topframe == topframe)
10332 attr = attr_sel;
10333 if (use_sep_chars && col > 0)
10334 screen_putchar('|', 0, col++, attr);
10335
10336 if (tp->tp_topframe != topframe)
10337 attr = attr_nosel;
10338
10339 screen_putchar(' ', 0, col++, attr);
10340
10341 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010342 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010343 cwp = curwin;
10344 wp = firstwin;
10345 }
10346 else
10347 {
10348 cwp = tp->tp_curwin;
10349 wp = tp->tp_firstwin;
10350 }
10351
10352 modified = FALSE;
10353 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10354 if (bufIsChanged(wp->w_buffer))
10355 modified = TRUE;
10356 if (modified || wincount > 1)
10357 {
10358 if (wincount > 1)
10359 {
10360 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010361 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010362 if (col + len >= Columns - 3)
10363 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010364 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010365#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010366 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010367#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010368 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010369#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010370 );
10371 col += len;
10372 }
10373 if (modified)
10374 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10375 screen_putchar(' ', 0, col++, attr);
10376 }
10377
10378 room = scol - col + tabwidth - 1;
10379 if (room > 0)
10380 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010381 /* Get buffer name in NameBuff[] */
10382 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010383 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010384 len = vim_strsize(NameBuff);
10385 p = NameBuff;
10386#ifdef FEAT_MBYTE
10387 if (has_mbyte)
10388 while (len > room)
10389 {
10390 len -= ptr2cells(p);
10391 mb_ptr_adv(p);
10392 }
10393 else
10394#endif
10395 if (len > room)
10396 {
10397 p += len - room;
10398 len = room;
10399 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010400 if (len > Columns - col - 1)
10401 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010402
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010403 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010404 col += len;
10405 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010406 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010407
10408 /* Store the tab page number in TabPageIdxs[], so that
10409 * jump_to_mouse() knows where each one is. */
10410 ++tabcount;
10411 while (scol < col)
10412 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010413 }
10414
Bram Moolenaar238a5642006-02-21 22:12:05 +000010415 if (use_sep_chars)
10416 c = '_';
10417 else
10418 c = ' ';
10419 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010420
10421 /* Put an "X" for closing the current tab if there are several. */
10422 if (first_tabpage->tp_next != NULL)
10423 {
10424 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10425 TabPageIdxs[Columns - 1] = -999;
10426 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010427 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010428
10429 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10430 * set. */
10431 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010432}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010433
10434/*
10435 * Get buffer name for "buf" into NameBuff[].
10436 * Takes care of special buffer names and translates special characters.
10437 */
10438 void
10439get_trans_bufname(buf)
10440 buf_T *buf;
10441{
10442 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010443 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010444 else
10445 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10446 trans_characters(NameBuff, MAXPATHL);
10447}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010448#endif
10449
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10451/*
10452 * Get the character to use in a status line. Get its attributes in "*attr".
10453 */
10454 static int
10455fillchar_status(attr, is_curwin)
10456 int *attr;
10457 int is_curwin;
10458{
10459 int fill;
10460 if (is_curwin)
10461 {
10462 *attr = hl_attr(HLF_S);
10463 fill = fill_stl;
10464 }
10465 else
10466 {
10467 *attr = hl_attr(HLF_SNC);
10468 fill = fill_stlnc;
10469 }
10470 /* Use fill when there is highlighting, and highlighting of current
10471 * window differs, or the fillchars differ, or this is not the
10472 * current window */
10473 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10474 || !is_curwin || firstwin == lastwin)
10475 || (fill_stl != fill_stlnc)))
10476 return fill;
10477 if (is_curwin)
10478 return '^';
10479 return '=';
10480}
10481#endif
10482
10483#ifdef FEAT_VERTSPLIT
10484/*
10485 * Get the character to use in a separator between vertically split windows.
10486 * Get its attributes in "*attr".
10487 */
10488 static int
10489fillchar_vsep(attr)
10490 int *attr;
10491{
10492 *attr = hl_attr(HLF_C);
10493 if (*attr == 0 && fill_vert == ' ')
10494 return '|';
10495 else
10496 return fill_vert;
10497}
10498#endif
10499
10500/*
10501 * Return TRUE if redrawing should currently be done.
10502 */
10503 int
10504redrawing()
10505{
10506 return (!RedrawingDisabled
10507 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10508}
10509
10510/*
10511 * Return TRUE if printing messages should currently be done.
10512 */
10513 int
10514messaging()
10515{
10516 return (!(p_lz && char_avail() && !KeyTyped));
10517}
10518
10519/*
10520 * Show current status info in ruler and various other places
10521 * If always is FALSE, only show ruler if position has changed.
10522 */
10523 void
10524showruler(always)
10525 int always;
10526{
10527 if (!always && !redrawing())
10528 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010529#ifdef FEAT_INS_EXPAND
10530 if (pum_visible())
10531 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010532# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010533 /* Don't redraw right now, do it later. */
10534 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010535# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010536 return;
10537 }
10538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010540 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010541 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010542 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 else
10545#endif
10546#ifdef FEAT_CMDL_INFO
10547 win_redr_ruler(curwin, always);
10548#endif
10549
10550#ifdef FEAT_TITLE
10551 if (need_maketitle
10552# ifdef FEAT_STL_OPT
10553 || (p_icon && (stl_syntax & STL_IN_ICON))
10554 || (p_title && (stl_syntax & STL_IN_TITLE))
10555# endif
10556 )
10557 maketitle();
10558#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010559#ifdef FEAT_WINDOWS
10560 /* Redraw the tab pages line if needed. */
10561 if (redraw_tabline)
10562 draw_tabline();
10563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564}
10565
10566#ifdef FEAT_CMDL_INFO
10567 static void
10568win_redr_ruler(wp, always)
10569 win_T *wp;
10570 int always;
10571{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010572#define RULER_BUF_LEN 70
10573 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 int row;
10575 int fillchar;
10576 int attr;
10577 int empty_line = FALSE;
10578 colnr_T virtcol;
10579 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010580 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 int o;
10582#ifdef FEAT_VERTSPLIT
10583 int this_ru_col;
10584 int off = 0;
10585 int width = Columns;
10586# define WITH_OFF(x) x
10587# define WITH_WIDTH(x) x
10588#else
10589# define WITH_OFF(x) 0
10590# define WITH_WIDTH(x) Columns
10591# define this_ru_col ru_col
10592#endif
10593
10594 /* If 'ruler' off or redrawing disabled, don't do anything */
10595 if (!p_ru)
10596 return;
10597
10598 /*
10599 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10600 * after deleting lines, before cursor.lnum is corrected.
10601 */
10602 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10603 return;
10604
10605#ifdef FEAT_INS_EXPAND
10606 /* Don't draw the ruler while doing insert-completion, it might overwrite
10607 * the (long) mode message. */
10608# ifdef FEAT_WINDOWS
10609 if (wp == lastwin && lastwin->w_status_height == 0)
10610# endif
10611 if (edit_submode != NULL)
10612 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010613 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10614 if (pum_visible())
10615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616#endif
10617
10618#ifdef FEAT_STL_OPT
10619 if (*p_ruf)
10620 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 int save_called_emsg = called_emsg;
10622
10623 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 if (called_emsg)
10626 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010627 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 return;
10630 }
10631#endif
10632
10633 /*
10634 * Check if not in Insert mode and the line is empty (will show "0-1").
10635 */
10636 if (!(State & INSERT)
10637 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10638 empty_line = TRUE;
10639
10640 /*
10641 * Only draw the ruler when something changed.
10642 */
10643 validate_virtcol_win(wp);
10644 if ( redraw_cmdline
10645 || always
10646 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10647 || wp->w_cursor.col != wp->w_ru_cursor.col
10648 || wp->w_virtcol != wp->w_ru_virtcol
10649#ifdef FEAT_VIRTUALEDIT
10650 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10651#endif
10652 || wp->w_topline != wp->w_ru_topline
10653 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10654#ifdef FEAT_DIFF
10655 || wp->w_topfill != wp->w_ru_topfill
10656#endif
10657 || empty_line != wp->w_ru_empty)
10658 {
10659 cursor_off();
10660#ifdef FEAT_WINDOWS
10661 if (wp->w_status_height)
10662 {
10663 row = W_WINROW(wp) + wp->w_height;
10664 fillchar = fillchar_status(&attr, wp == curwin);
10665# ifdef FEAT_VERTSPLIT
10666 off = W_WINCOL(wp);
10667 width = W_WIDTH(wp);
10668# endif
10669 }
10670 else
10671#endif
10672 {
10673 row = Rows - 1;
10674 fillchar = ' ';
10675 attr = 0;
10676#ifdef FEAT_VERTSPLIT
10677 width = Columns;
10678 off = 0;
10679#endif
10680 }
10681
10682 /* In list mode virtcol needs to be recomputed */
10683 virtcol = wp->w_virtcol;
10684 if (wp->w_p_list && lcs_tab1 == NUL)
10685 {
10686 wp->w_p_list = FALSE;
10687 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10688 wp->w_p_list = TRUE;
10689 }
10690
10691 /*
10692 * Some sprintfs return the length, some return a pointer.
10693 * To avoid portability problems we use strlen() here.
10694 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010695 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10697 ? 0L
10698 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010699 len = STRLEN(buffer);
10700 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10702 (int)virtcol + 1);
10703
10704 /*
10705 * Add a "50%" if there is room for it.
10706 * On the last line, don't print in the last column (scrolls the
10707 * screen up on some terminals).
10708 */
10709 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010710 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 o = i + vim_strsize(buffer + i + 1);
10712#ifdef FEAT_WINDOWS
10713 if (wp->w_status_height == 0) /* can't use last char of screen */
10714#endif
10715 ++o;
10716#ifdef FEAT_VERTSPLIT
10717 this_ru_col = ru_col - (Columns - width);
10718 if (this_ru_col < 0)
10719 this_ru_col = 0;
10720#endif
10721 /* Never use more than half the window/screen width, leave the other
10722 * half for the filename. */
10723 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10724 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10725 if (this_ru_col + o < WITH_WIDTH(width))
10726 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010727 /* need at least 3 chars left for get_rel_pos() + NUL */
10728 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 {
10730#ifdef FEAT_MBYTE
10731 if (has_mbyte)
10732 i += (*mb_char2bytes)(fillchar, buffer + i);
10733 else
10734#endif
10735 buffer[i++] = fillchar;
10736 ++o;
10737 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010738 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 }
10740 /* Truncate at window boundary. */
10741#ifdef FEAT_MBYTE
10742 if (has_mbyte)
10743 {
10744 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010745 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 {
10747 o += (*mb_ptr2cells)(buffer + i);
10748 if (this_ru_col + o > WITH_WIDTH(width))
10749 {
10750 buffer[i] = NUL;
10751 break;
10752 }
10753 }
10754 }
10755 else
10756#endif
10757 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10758 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10759
10760 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10761 i = redraw_cmdline;
10762 screen_fill(row, row + 1,
10763 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10764 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10765 fillchar, fillchar, attr);
10766 /* don't redraw the cmdline because of showing the ruler */
10767 redraw_cmdline = i;
10768 wp->w_ru_cursor = wp->w_cursor;
10769 wp->w_ru_virtcol = wp->w_virtcol;
10770 wp->w_ru_empty = empty_line;
10771 wp->w_ru_topline = wp->w_topline;
10772 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10773#ifdef FEAT_DIFF
10774 wp->w_ru_topfill = wp->w_topfill;
10775#endif
10776 }
10777}
10778#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010779
10780#if defined(FEAT_LINEBREAK) || defined(PROTO)
10781/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010782 * Return the width of the 'number' and 'relativenumber' column.
10783 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010784 * Otherwise it depends on 'numberwidth' and the line count.
10785 */
10786 int
10787number_width(wp)
10788 win_T *wp;
10789{
10790 int n;
10791 linenr_T lnum;
10792
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010793 if (wp->w_p_rnu && !wp->w_p_nu)
10794 /* cursor line shows "0" */
10795 lnum = wp->w_height;
10796 else
10797 /* cursor line shows absolute line number */
10798 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010799
Bram Moolenaar6b314672015-03-20 15:42:10 +010010800 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010801 return wp->w_nrwidth_width;
10802 wp->w_nrwidth_line_count = lnum;
10803
10804 n = 0;
10805 do
10806 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010807 lnum /= 10;
10808 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010809 } while (lnum > 0);
10810
10811 /* 'numberwidth' gives the minimal width plus one */
10812 if (n < wp->w_p_nuw - 1)
10813 n = wp->w_p_nuw - 1;
10814
10815 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010816 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010817 return n;
10818}
10819#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010820
10821/*
10822 * Return the current cursor column. This is the actual position on the
10823 * screen. First column is 0.
10824 */
10825 int
10826screen_screencol()
10827{
10828 return screen_cur_col;
10829}
10830
10831/*
10832 * Return the current cursor row. This is the actual position on the screen.
10833 * First row is 0.
10834 */
10835 int
10836screen_screenrow()
10837{
10838 return screen_cur_row;
10839}