blob: 581638157c6a01f6f4317b35f2979752e33e8c3f [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;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005425 if (draw_state > WL_NR
5426#ifdef FEAT_DIFF
5427 && filler_todo <= 0
5428#endif
5429 )
5430 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /* When "tocol" is halfway a character, set it to the end of
5432 * the character, otherwise highlighting won't stop. */
5433 if (tocol == vcol)
5434 ++tocol;
5435#ifdef FEAT_RIGHTLEFT
5436 if (wp->w_p_rl)
5437 {
5438 /* now it's time to backup one cell */
5439 --off;
5440 --col;
5441 }
5442#endif
5443 }
5444#endif
5445#ifdef FEAT_RIGHTLEFT
5446 if (wp->w_p_rl)
5447 {
5448 --off;
5449 --col;
5450 }
5451 else
5452#endif
5453 {
5454 ++off;
5455 ++col;
5456 }
5457 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005458#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005459 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005460 {
5461 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005462 ++vcol_off;
5463 if (n_extra > 0)
5464 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005465 if (wp->w_p_wrap)
5466 {
5467 /*
5468 * Special voodoo required if 'wrap' is on.
5469 *
5470 * Advance the column indicator to force the line
5471 * drawing to wrap early. This will make the line
5472 * take up the same screen space when parts are concealed,
5473 * so that cursor line computations aren't messed up.
5474 *
5475 * To avoid the fictitious advance of 'col' causing
5476 * trailing junk to be written out of the screen line
5477 * we are building, 'boguscols' keeps track of the number
5478 * of bad columns we have advanced.
5479 */
5480 if (n_extra > 0)
5481 {
5482 vcol += n_extra;
5483# ifdef FEAT_RIGHTLEFT
5484 if (wp->w_p_rl)
5485 {
5486 col -= n_extra;
5487 boguscols -= n_extra;
5488 }
5489 else
5490# endif
5491 {
5492 col += n_extra;
5493 boguscols += n_extra;
5494 }
5495 n_extra = 0;
5496 n_attr = 0;
5497 }
5498
5499
5500# ifdef FEAT_MBYTE
5501 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5502 {
5503 /* Need to fill two screen columns. */
5504# ifdef FEAT_RIGHTLEFT
5505 if (wp->w_p_rl)
5506 {
5507 --boguscols;
5508 --col;
5509 }
5510 else
5511# endif
5512 {
5513 ++boguscols;
5514 ++col;
5515 }
5516 }
5517# endif
5518
5519# ifdef FEAT_RIGHTLEFT
5520 if (wp->w_p_rl)
5521 {
5522 --boguscols;
5523 --col;
5524 }
5525 else
5526# endif
5527 {
5528 ++boguscols;
5529 ++col;
5530 }
5531 }
5532 else
5533 {
5534 if (n_extra > 0)
5535 {
5536 vcol += n_extra;
5537 n_extra = 0;
5538 n_attr = 0;
5539 }
5540 }
5541
5542 }
5543#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 else
5545 --n_skip;
5546
Bram Moolenaar64486672010-05-16 15:46:46 +02005547 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5548 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005549 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#ifdef FEAT_DIFF
5551 && filler_todo <= 0
5552#endif
5553 )
5554 ++vcol;
5555
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556#ifdef FEAT_SYN_HL
5557 if (vcol_save_attr >= 0)
5558 char_attr = vcol_save_attr;
5559#endif
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /* restore attributes after "predeces" in 'listchars' */
5562 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5563 char_attr = saved_attr3;
5564
5565 /* restore attributes after last 'listchars' or 'number' char */
5566 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5567 char_attr = saved_attr2;
5568
5569 /*
5570 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005571 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 */
5573 if ((
5574#ifdef FEAT_RIGHTLEFT
5575 wp->w_p_rl ? (col < 0) :
5576#endif
5577 (col >= W_WIDTH(wp)))
5578 && (*ptr != NUL
5579#ifdef FEAT_DIFF
5580 || filler_todo > 0
5581#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005582 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5584 )
5585 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005586#ifdef FEAT_CONCEAL
5587 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5588 (int)W_WIDTH(wp), wp->w_p_rl);
5589 boguscols = 0;
5590#else
5591 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5592 (int)W_WIDTH(wp), wp->w_p_rl);
5593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 ++row;
5595 ++screen_row;
5596
5597 /* When not wrapping and finished diff lines, or when displayed
5598 * '$' and highlighting until last column, break here. */
5599 if ((!wp->w_p_wrap
5600#ifdef FEAT_DIFF
5601 && filler_todo <= 0
5602#endif
5603 ) || lcs_eol_one == -1)
5604 break;
5605
5606 /* When the window is too narrow draw all "@" lines. */
5607 if (draw_state != WL_LINE
5608#ifdef FEAT_DIFF
5609 && filler_todo <= 0
5610#endif
5611 )
5612 {
5613 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5614#ifdef FEAT_VERTSPLIT
5615 draw_vsep_win(wp, row);
5616#endif
5617 row = endrow;
5618 }
5619
5620 /* When line got too long for screen break here. */
5621 if (row == endrow)
5622 {
5623 ++row;
5624 break;
5625 }
5626
5627 if (screen_cur_row == screen_row - 1
5628#ifdef FEAT_DIFF
5629 && filler_todo <= 0
5630#endif
5631 && W_WIDTH(wp) == Columns)
5632 {
5633 /* Remember that the line wraps, used for modeless copy. */
5634 LineWraps[screen_row - 1] = TRUE;
5635
5636 /*
5637 * Special trick to make copy/paste of wrapped lines work with
5638 * xterm/screen: write an extra character beyond the end of
5639 * the line. This will work with all terminal types
5640 * (regardless of the xn,am settings).
5641 * Only do this on a fast tty.
5642 * Only do this if the cursor is on the current line
5643 * (something has been written in it).
5644 * Don't do this for the GUI.
5645 * Don't do this for double-width characters.
5646 * Don't do this for a window not at the right screen border.
5647 */
5648 if (p_tf
5649#ifdef FEAT_GUI
5650 && !gui.in_use
5651#endif
5652#ifdef FEAT_MBYTE
5653 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005654 && ((*mb_off2cells)(LineOffset[screen_row],
5655 LineOffset[screen_row] + screen_Columns)
5656 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005658 + (int)Columns - 2,
5659 LineOffset[screen_row] + screen_Columns)
5660 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661#endif
5662 )
5663 {
5664 /* First make sure we are at the end of the screen line,
5665 * then output the same character again to let the
5666 * terminal know about the wrap. If the terminal doesn't
5667 * auto-wrap, we overwrite the character. */
5668 if (screen_cur_col != W_WIDTH(wp))
5669 screen_char(LineOffset[screen_row - 1]
5670 + (unsigned)Columns - 1,
5671 screen_row - 1, (int)(Columns - 1));
5672
5673#ifdef FEAT_MBYTE
5674 /* When there is a multi-byte character, just output a
5675 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005676 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5677 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 out_char(' ');
5679 else
5680#endif
5681 out_char(ScreenLines[LineOffset[screen_row - 1]
5682 + (Columns - 1)]);
5683 /* force a redraw of the first char on the next line */
5684 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5685 screen_start(); /* don't know where cursor is now */
5686 }
5687 }
5688
5689 col = 0;
5690 off = (unsigned)(current_ScreenLine - ScreenLines);
5691#ifdef FEAT_RIGHTLEFT
5692 if (wp->w_p_rl)
5693 {
5694 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5695 off += col;
5696 }
5697#endif
5698
5699 /* reset the drawing state for the start of a wrapped line */
5700 draw_state = WL_START;
5701 saved_n_extra = n_extra;
5702 saved_p_extra = p_extra;
5703 saved_c_extra = c_extra;
5704 saved_char_attr = char_attr;
5705 n_extra = 0;
5706 lcs_prec_todo = lcs_prec;
5707#ifdef FEAT_LINEBREAK
5708# ifdef FEAT_DIFF
5709 if (filler_todo <= 0)
5710# endif
5711 need_showbreak = TRUE;
5712#endif
5713#ifdef FEAT_DIFF
5714 --filler_todo;
5715 /* When the filler lines are actually below the last line of the
5716 * file, don't draw the line itself, break here. */
5717 if (filler_todo == 0 && wp->w_botfill)
5718 break;
5719#endif
5720 }
5721
5722 } /* for every character in the line */
5723
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005724#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005725 /* After an empty line check first word for capital. */
5726 if (*skipwhite(line) == NUL)
5727 {
5728 capcol_lnum = lnum + 1;
5729 cap_col = 0;
5730 }
5731#endif
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 return row;
5734}
5735
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005736#ifdef FEAT_MBYTE
5737static int comp_char_differs __ARGS((int, int));
5738
5739/*
5740 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005741 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005742 */
5743 static int
5744comp_char_differs(off_from, off_to)
5745 int off_from;
5746 int off_to;
5747{
5748 int i;
5749
5750 for (i = 0; i < Screen_mco; ++i)
5751 {
5752 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5753 return TRUE;
5754 if (ScreenLinesC[i][off_from] == 0)
5755 break;
5756 }
5757 return FALSE;
5758}
5759#endif
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761/*
5762 * Check whether the given character needs redrawing:
5763 * - the (first byte of the) character is different
5764 * - the attributes are different
5765 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005766 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 */
5768 static int
5769char_needs_redraw(off_from, off_to, cols)
5770 int off_from;
5771 int off_to;
5772 int cols;
5773{
5774 if (cols > 0
5775 && ((ScreenLines[off_from] != ScreenLines[off_to]
5776 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5777
5778#ifdef FEAT_MBYTE
5779 || (enc_dbcs != 0
5780 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5781 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5782 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5783 : (cols > 1 && ScreenLines[off_from + 1]
5784 != ScreenLines[off_to + 1])))
5785 || (enc_utf8
5786 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5787 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005788 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005789 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5790 && ScreenLines[off_from + 1]
5791 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792#endif
5793 ))
5794 return TRUE;
5795 return FALSE;
5796}
5797
5798/*
5799 * Move one "cooked" screen line to the screen, but only the characters that
5800 * have actually changed. Handle insert/delete character.
5801 * "coloff" gives the first column on the screen for this line.
5802 * "endcol" gives the columns where valid characters are.
5803 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5804 * needs to be cleared, negative otherwise.
5805 * "rlflag" is TRUE in a rightleft window:
5806 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5807 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5808 */
5809 static void
5810screen_line(row, coloff, endcol, clear_width
5811#ifdef FEAT_RIGHTLEFT
5812 , rlflag
5813#endif
5814 )
5815 int row;
5816 int coloff;
5817 int endcol;
5818 int clear_width;
5819#ifdef FEAT_RIGHTLEFT
5820 int rlflag;
5821#endif
5822{
5823 unsigned off_from;
5824 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005825#ifdef FEAT_MBYTE
5826 unsigned max_off_from;
5827 unsigned max_off_to;
5828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 int col = 0;
5830#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5831 int hl;
5832#endif
5833 int force = FALSE; /* force update rest of the line */
5834 int redraw_this /* bool: does character need redraw? */
5835#ifdef FEAT_GUI
5836 = TRUE /* For GUI when while-loop empty */
5837#endif
5838 ;
5839 int redraw_next; /* redraw_this for next character */
5840#ifdef FEAT_MBYTE
5841 int clear_next = FALSE;
5842 int char_cells; /* 1: normal char */
5843 /* 2: occupies two display cells */
5844# define CHAR_CELLS char_cells
5845#else
5846# define CHAR_CELLS 1
5847#endif
5848
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005849 /* Check for illegal row and col, just in case. */
5850 if (row >= Rows)
5851 row = Rows - 1;
5852 if (endcol > Columns)
5853 endcol = Columns;
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855# ifdef FEAT_CLIPBOARD
5856 clip_may_clear_selection(row, row);
5857# endif
5858
5859 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5860 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005861#ifdef FEAT_MBYTE
5862 max_off_from = off_from + screen_Columns;
5863 max_off_to = LineOffset[row] + screen_Columns;
5864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866#ifdef FEAT_RIGHTLEFT
5867 if (rlflag)
5868 {
5869 /* Clear rest first, because it's left of the text. */
5870 if (clear_width > 0)
5871 {
5872 while (col <= endcol && ScreenLines[off_to] == ' '
5873 && ScreenAttrs[off_to] == 0
5874# ifdef FEAT_MBYTE
5875 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5876# endif
5877 )
5878 {
5879 ++off_to;
5880 ++col;
5881 }
5882 if (col <= endcol)
5883 screen_fill(row, row + 1, col + coloff,
5884 endcol + coloff + 1, ' ', ' ', 0);
5885 }
5886 col = endcol + 1;
5887 off_to = LineOffset[row] + col + coloff;
5888 off_from += col;
5889 endcol = (clear_width > 0 ? clear_width : -clear_width);
5890 }
5891#endif /* FEAT_RIGHTLEFT */
5892
5893 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5894
5895 while (col < endcol)
5896 {
5897#ifdef FEAT_MBYTE
5898 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005899 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 else
5901 char_cells = 1;
5902#endif
5903
5904 redraw_this = redraw_next;
5905 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5906 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5907
5908#ifdef FEAT_GUI
5909 /* If the next character was bold, then redraw the current character to
5910 * remove any pixels that might have spilt over into us. This only
5911 * happens in the GUI.
5912 */
5913 if (redraw_next && gui.in_use)
5914 {
5915 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005916 if (hl > HL_ALL)
5917 hl = syn_attr2attr(hl);
5918 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 redraw_this = TRUE;
5920 }
5921#endif
5922
5923 if (redraw_this)
5924 {
5925 /*
5926 * Special handling when 'xs' termcap flag set (hpterm):
5927 * Attributes for characters are stored at the position where the
5928 * cursor is when writing the highlighting code. The
5929 * start-highlighting code must be written with the cursor on the
5930 * first highlighted character. The stop-highlighting code must
5931 * be written with the cursor just after the last highlighted
5932 * character.
5933 * Overwriting a character doesn't remove it's highlighting. Need
5934 * to clear the rest of the line, and force redrawing it
5935 * completely.
5936 */
5937 if ( p_wiv
5938 && !force
5939#ifdef FEAT_GUI
5940 && !gui.in_use
5941#endif
5942 && ScreenAttrs[off_to] != 0
5943 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5944 {
5945 /*
5946 * Need to remove highlighting attributes here.
5947 */
5948 windgoto(row, col + coloff);
5949 out_str(T_CE); /* clear rest of this screen line */
5950 screen_start(); /* don't know where cursor is now */
5951 force = TRUE; /* force redraw of rest of the line */
5952 redraw_next = TRUE; /* or else next char would miss out */
5953
5954 /*
5955 * If the previous character was highlighted, need to stop
5956 * highlighting at this character.
5957 */
5958 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5959 {
5960 screen_attr = ScreenAttrs[off_to - 1];
5961 term_windgoto(row, col + coloff);
5962 screen_stop_highlight();
5963 }
5964 else
5965 screen_attr = 0; /* highlighting has stopped */
5966 }
5967#ifdef FEAT_MBYTE
5968 if (enc_dbcs != 0)
5969 {
5970 /* Check if overwriting a double-byte with a single-byte or
5971 * the other way around requires another character to be
5972 * redrawn. For UTF-8 this isn't needed, because comparing
5973 * ScreenLinesUC[] is sufficient. */
5974 if (char_cells == 1
5975 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005976 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
5978 /* Writing a single-cell character over a double-cell
5979 * character: need to redraw the next cell. */
5980 ScreenLines[off_to + 1] = 0;
5981 redraw_next = TRUE;
5982 }
5983 else if (char_cells == 2
5984 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005985 && (*mb_off2cells)(off_to, max_off_to) == 1
5986 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
5988 /* Writing the second half of a double-cell character over
5989 * a double-cell character: need to redraw the second
5990 * cell. */
5991 ScreenLines[off_to + 2] = 0;
5992 redraw_next = TRUE;
5993 }
5994
5995 if (enc_dbcs == DBCS_JPNU)
5996 ScreenLines2[off_to] = ScreenLines2[off_from];
5997 }
5998 /* When writing a single-width character over a double-width
5999 * character and at the end of the redrawn text, need to clear out
6000 * the right halve of the old character.
6001 * Also required when writing the right halve of a double-width
6002 * char over the left halve of an existing one. */
6003 if (has_mbyte && col + char_cells == endcol
6004 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006005 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006007 && (*mb_off2cells)(off_to, max_off_to) == 1
6008 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 clear_next = TRUE;
6010#endif
6011
6012 ScreenLines[off_to] = ScreenLines[off_from];
6013#ifdef FEAT_MBYTE
6014 if (enc_utf8)
6015 {
6016 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6017 if (ScreenLinesUC[off_from] != 0)
6018 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006019 int i;
6020
6021 for (i = 0; i < Screen_mco; ++i)
6022 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024 }
6025 if (char_cells == 2)
6026 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6027#endif
6028
6029#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006030 /* The bold trick makes a single column of pixels appear in the
6031 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 * character should be redrawn too. This happens for our own GUI
6033 * and for some xterms. */
6034 if (
6035# ifdef FEAT_GUI
6036 gui.in_use
6037# endif
6038# if defined(FEAT_GUI) && defined(UNIX)
6039 ||
6040# endif
6041# ifdef UNIX
6042 term_is_xterm
6043# endif
6044 )
6045 {
6046 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006047 if (hl > HL_ALL)
6048 hl = syn_attr2attr(hl);
6049 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 redraw_next = TRUE;
6051 }
6052#endif
6053 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6054#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006055 /* For simplicity set the attributes of second half of a
6056 * double-wide character equal to the first half. */
6057 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006059
6060 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 else
6063#endif
6064 screen_char(off_to, row, col + coloff);
6065 }
6066 else if ( p_wiv
6067#ifdef FEAT_GUI
6068 && !gui.in_use
6069#endif
6070 && col + coloff > 0)
6071 {
6072 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6073 {
6074 /*
6075 * Don't output stop-highlight when moving the cursor, it will
6076 * stop the highlighting when it should continue.
6077 */
6078 screen_attr = 0;
6079 }
6080 else if (screen_attr != 0)
6081 screen_stop_highlight();
6082 }
6083
6084 off_to += CHAR_CELLS;
6085 off_from += CHAR_CELLS;
6086 col += CHAR_CELLS;
6087 }
6088
6089#ifdef FEAT_MBYTE
6090 if (clear_next)
6091 {
6092 /* Clear the second half of a double-wide character of which the left
6093 * half was overwritten with a single-wide character. */
6094 ScreenLines[off_to] = ' ';
6095 if (enc_utf8)
6096 ScreenLinesUC[off_to] = 0;
6097 screen_char(off_to, row, col + coloff);
6098 }
6099#endif
6100
6101 if (clear_width > 0
6102#ifdef FEAT_RIGHTLEFT
6103 && !rlflag
6104#endif
6105 )
6106 {
6107#ifdef FEAT_GUI
6108 int startCol = col;
6109#endif
6110
6111 /* blank out the rest of the line */
6112 while (col < clear_width && ScreenLines[off_to] == ' '
6113 && ScreenAttrs[off_to] == 0
6114#ifdef FEAT_MBYTE
6115 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6116#endif
6117 )
6118 {
6119 ++off_to;
6120 ++col;
6121 }
6122 if (col < clear_width)
6123 {
6124#ifdef FEAT_GUI
6125 /*
6126 * In the GUI, clearing the rest of the line may leave pixels
6127 * behind if the first character cleared was bold. Some bold
6128 * fonts spill over the left. In this case we redraw the previous
6129 * character too. If we didn't skip any blanks above, then we
6130 * only redraw if the character wasn't already redrawn anyway.
6131 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006132 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 hl = ScreenAttrs[off_to];
6135 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006136 {
6137 int prev_cells = 1;
6138# ifdef FEAT_MBYTE
6139 if (enc_utf8)
6140 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6141 * that its width is 2. */
6142 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6143 else if (enc_dbcs != 0)
6144 {
6145 /* find previous character by counting from first
6146 * column and get its width. */
6147 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006148 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006149
6150 while (off < off_to)
6151 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006152 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006153 off += prev_cells;
6154 }
6155 }
6156
6157 if (enc_dbcs != 0 && prev_cells > 1)
6158 screen_char_2(off_to - prev_cells, row,
6159 col + coloff - prev_cells);
6160 else
6161# endif
6162 screen_char(off_to - prev_cells, row,
6163 col + coloff - prev_cells);
6164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 }
6166#endif
6167 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6168 ' ', ' ', 0);
6169#ifdef FEAT_VERTSPLIT
6170 off_to += clear_width - col;
6171 col = clear_width;
6172#endif
6173 }
6174 }
6175
6176 if (clear_width > 0)
6177 {
6178#ifdef FEAT_VERTSPLIT
6179 /* For a window that's left of another, draw the separator char. */
6180 if (col + coloff < Columns)
6181 {
6182 int c;
6183
6184 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006185 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006187 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6188 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189# endif
6190 || ScreenAttrs[off_to] != hl)
6191 {
6192 ScreenLines[off_to] = c;
6193 ScreenAttrs[off_to] = hl;
6194# ifdef FEAT_MBYTE
6195 if (enc_utf8)
6196 {
6197 if (c >= 0x80)
6198 {
6199 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006200 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 }
6202 else
6203 ScreenLinesUC[off_to] = 0;
6204 }
6205# endif
6206 screen_char(off_to, row, col + coloff);
6207 }
6208 }
6209 else
6210#endif
6211 LineWraps[row] = FALSE;
6212 }
6213}
6214
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006215#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006217 * Mirror text "str" for right-left displaying.
6218 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006220 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221rl_mirror(str)
6222 char_u *str;
6223{
6224 char_u *p1, *p2;
6225 int t;
6226
6227 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6228 {
6229 t = *p1;
6230 *p1 = *p2;
6231 *p2 = t;
6232 }
6233}
6234#endif
6235
6236#if defined(FEAT_WINDOWS) || defined(PROTO)
6237/*
6238 * mark all status lines for redraw; used after first :cd
6239 */
6240 void
6241status_redraw_all()
6242{
6243 win_T *wp;
6244
6245 for (wp = firstwin; wp; wp = wp->w_next)
6246 if (wp->w_status_height)
6247 {
6248 wp->w_redr_status = TRUE;
6249 redraw_later(VALID);
6250 }
6251}
6252
6253/*
6254 * mark all status lines of the current buffer for redraw
6255 */
6256 void
6257status_redraw_curbuf()
6258{
6259 win_T *wp;
6260
6261 for (wp = firstwin; wp; wp = wp->w_next)
6262 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6263 {
6264 wp->w_redr_status = TRUE;
6265 redraw_later(VALID);
6266 }
6267}
6268
6269/*
6270 * Redraw all status lines that need to be redrawn.
6271 */
6272 void
6273redraw_statuslines()
6274{
6275 win_T *wp;
6276
6277 for (wp = firstwin; wp; wp = wp->w_next)
6278 if (wp->w_redr_status)
6279 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006280 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006281 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282}
6283#endif
6284
6285#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6286/*
6287 * Redraw all status lines at the bottom of frame "frp".
6288 */
6289 void
6290win_redraw_last_status(frp)
6291 frame_T *frp;
6292{
6293 if (frp->fr_layout == FR_LEAF)
6294 frp->fr_win->w_redr_status = TRUE;
6295 else if (frp->fr_layout == FR_ROW)
6296 {
6297 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6298 win_redraw_last_status(frp);
6299 }
6300 else /* frp->fr_layout == FR_COL */
6301 {
6302 frp = frp->fr_child;
6303 while (frp->fr_next != NULL)
6304 frp = frp->fr_next;
6305 win_redraw_last_status(frp);
6306 }
6307}
6308#endif
6309
6310#ifdef FEAT_VERTSPLIT
6311/*
6312 * Draw the verticap separator right of window "wp" starting with line "row".
6313 */
6314 static void
6315draw_vsep_win(wp, row)
6316 win_T *wp;
6317 int row;
6318{
6319 int hl;
6320 int c;
6321
6322 if (wp->w_vsep_width)
6323 {
6324 /* draw the vertical separator right of this window */
6325 c = fillchar_vsep(&hl);
6326 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6327 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6328 c, ' ', hl);
6329 }
6330}
6331#endif
6332
6333#ifdef FEAT_WILDMENU
6334static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006335static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006338 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 */
6340 static int
6341status_match_len(xp, s)
6342 expand_T *xp;
6343 char_u *s;
6344{
6345 int len = 0;
6346
6347#ifdef FEAT_MENU
6348 int emenu = (xp->xp_context == EXPAND_MENUS
6349 || xp->xp_context == EXPAND_MENUNAMES);
6350
6351 /* Check for menu separators - replace with '|'. */
6352 if (emenu && menu_is_separator(s))
6353 return 1;
6354#endif
6355
6356 while (*s != NUL)
6357 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006358 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006359 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006360 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362
6363 return len;
6364}
6365
6366/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006367 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006368 * These are backslashes used for escaping. Do show backslashes in help tags.
6369 */
6370 static int
6371skip_status_match_char(xp, s)
6372 expand_T *xp;
6373 char_u *s;
6374{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006375 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006376#ifdef FEAT_MENU
6377 || ((xp->xp_context == EXPAND_MENUS
6378 || xp->xp_context == EXPAND_MENUNAMES)
6379 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6380#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006381 )
6382 {
6383#ifndef BACKSLASH_IN_FILENAME
6384 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6385 return 2;
6386#endif
6387 return 1;
6388 }
6389 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006390}
6391
6392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Show wildchar matches in the status line.
6394 * Show at least the "match" item.
6395 * We start at item 'first_match' in the list and show all matches that fit.
6396 *
6397 * If inversion is possible we use it. Else '=' characters are used.
6398 */
6399 void
6400win_redr_status_matches(xp, num_matches, matches, match, showtail)
6401 expand_T *xp;
6402 int num_matches;
6403 char_u **matches; /* list of matches */
6404 int match;
6405 int showtail;
6406{
6407#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6408 int row;
6409 char_u *buf;
6410 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006411 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 int fillchar;
6413 int attr;
6414 int i;
6415 int highlight = TRUE;
6416 char_u *selstart = NULL;
6417 int selstart_col = 0;
6418 char_u *selend = NULL;
6419 static int first_match = 0;
6420 int add_left = FALSE;
6421 char_u *s;
6422#ifdef FEAT_MENU
6423 int emenu;
6424#endif
6425#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6426 int l;
6427#endif
6428
6429 if (matches == NULL) /* interrupted completion? */
6430 return;
6431
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006432#ifdef FEAT_MBYTE
6433 if (has_mbyte)
6434 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6435 else
6436#endif
6437 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (buf == NULL)
6439 return;
6440
6441 if (match == -1) /* don't show match but original text */
6442 {
6443 match = 0;
6444 highlight = FALSE;
6445 }
6446 /* count 1 for the ending ">" */
6447 clen = status_match_len(xp, L_MATCH(match)) + 3;
6448 if (match == 0)
6449 first_match = 0;
6450 else if (match < first_match)
6451 {
6452 /* jumping left, as far as we can go */
6453 first_match = match;
6454 add_left = TRUE;
6455 }
6456 else
6457 {
6458 /* check if match fits on the screen */
6459 for (i = first_match; i < match; ++i)
6460 clen += status_match_len(xp, L_MATCH(i)) + 2;
6461 if (first_match > 0)
6462 clen += 2;
6463 /* jumping right, put match at the left */
6464 if ((long)clen > Columns)
6465 {
6466 first_match = match;
6467 /* if showing the last match, we can add some on the left */
6468 clen = 2;
6469 for (i = match; i < num_matches; ++i)
6470 {
6471 clen += status_match_len(xp, L_MATCH(i)) + 2;
6472 if ((long)clen >= Columns)
6473 break;
6474 }
6475 if (i == num_matches)
6476 add_left = TRUE;
6477 }
6478 }
6479 if (add_left)
6480 while (first_match > 0)
6481 {
6482 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6483 if ((long)clen >= Columns)
6484 break;
6485 --first_match;
6486 }
6487
6488 fillchar = fillchar_status(&attr, TRUE);
6489
6490 if (first_match == 0)
6491 {
6492 *buf = NUL;
6493 len = 0;
6494 }
6495 else
6496 {
6497 STRCPY(buf, "< ");
6498 len = 2;
6499 }
6500 clen = len;
6501
6502 i = first_match;
6503 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6504 {
6505 if (i == match)
6506 {
6507 selstart = buf + len;
6508 selstart_col = clen;
6509 }
6510
6511 s = L_MATCH(i);
6512 /* Check for menu separators - replace with '|' */
6513#ifdef FEAT_MENU
6514 emenu = (xp->xp_context == EXPAND_MENUS
6515 || xp->xp_context == EXPAND_MENUNAMES);
6516 if (emenu && menu_is_separator(s))
6517 {
6518 STRCPY(buf + len, transchar('|'));
6519 l = (int)STRLEN(buf + len);
6520 len += l;
6521 clen += l;
6522 }
6523 else
6524#endif
6525 for ( ; *s != NUL; ++s)
6526 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006527 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 clen += ptr2cells(s);
6529#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006530 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
6532 STRNCPY(buf + len, s, l);
6533 s += l - 1;
6534 len += l;
6535 }
6536 else
6537#endif
6538 {
6539 STRCPY(buf + len, transchar_byte(*s));
6540 len += (int)STRLEN(buf + len);
6541 }
6542 }
6543 if (i == match)
6544 selend = buf + len;
6545
6546 *(buf + len++) = ' ';
6547 *(buf + len++) = ' ';
6548 clen += 2;
6549 if (++i == num_matches)
6550 break;
6551 }
6552
6553 if (i != num_matches)
6554 {
6555 *(buf + len++) = '>';
6556 ++clen;
6557 }
6558
6559 buf[len] = NUL;
6560
6561 row = cmdline_row - 1;
6562 if (row >= 0)
6563 {
6564 if (wild_menu_showing == 0)
6565 {
6566 if (msg_scrolled > 0)
6567 {
6568 /* Put the wildmenu just above the command line. If there is
6569 * no room, scroll the screen one line up. */
6570 if (cmdline_row == Rows - 1)
6571 {
6572 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6573 ++msg_scrolled;
6574 }
6575 else
6576 {
6577 ++cmdline_row;
6578 ++row;
6579 }
6580 wild_menu_showing = WM_SCROLLED;
6581 }
6582 else
6583 {
6584 /* Create status line if needed by setting 'laststatus' to 2.
6585 * Set 'winminheight' to zero to avoid that the window is
6586 * resized. */
6587 if (lastwin->w_status_height == 0)
6588 {
6589 save_p_ls = p_ls;
6590 save_p_wmh = p_wmh;
6591 p_ls = 2;
6592 p_wmh = 0;
6593 last_status(FALSE);
6594 }
6595 wild_menu_showing = WM_SHOWN;
6596 }
6597 }
6598
6599 screen_puts(buf, row, 0, attr);
6600 if (selstart != NULL && highlight)
6601 {
6602 *selend = NUL;
6603 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6604 }
6605
6606 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6607 }
6608
6609#ifdef FEAT_VERTSPLIT
6610 win_redraw_last_status(topframe);
6611#else
6612 lastwin->w_redr_status = TRUE;
6613#endif
6614 vim_free(buf);
6615}
6616#endif
6617
6618#if defined(FEAT_WINDOWS) || defined(PROTO)
6619/*
6620 * Redraw the status line of window wp.
6621 *
6622 * If inversion is possible we use it. Else '=' characters are used.
6623 */
6624 void
6625win_redr_status(wp)
6626 win_T *wp;
6627{
6628 int row;
6629 char_u *p;
6630 int len;
6631 int fillchar;
6632 int attr;
6633 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006634 static int busy = FALSE;
6635
6636 /* It's possible to get here recursively when 'statusline' (indirectly)
6637 * invokes ":redrawstatus". Simply ignore the call then. */
6638 if (busy)
6639 return;
6640 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642 wp->w_redr_status = FALSE;
6643 if (wp->w_status_height == 0)
6644 {
6645 /* no status line, can only be last window */
6646 redraw_cmdline = TRUE;
6647 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006648 else if (!redrawing()
6649#ifdef FEAT_INS_EXPAND
6650 /* don't update status line when popup menu is visible and may be
6651 * drawn over it */
6652 || pum_visible()
6653#endif
6654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 /* Don't redraw right now, do it later. */
6657 wp->w_redr_status = TRUE;
6658 }
6659#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006660 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006663 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 }
6665#endif
6666 else
6667 {
6668 fillchar = fillchar_status(&attr, wp == curwin);
6669
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006670 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 p = NameBuff;
6672 len = (int)STRLEN(p);
6673
6674 if (wp->w_buffer->b_help
6675#ifdef FEAT_QUICKFIX
6676 || wp->w_p_pvw
6677#endif
6678 || bufIsChanged(wp->w_buffer)
6679 || wp->w_buffer->b_p_ro)
6680 *(p + len++) = ' ';
6681 if (wp->w_buffer->b_help)
6682 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006683 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 len += (int)STRLEN(p + len);
6685 }
6686#ifdef FEAT_QUICKFIX
6687 if (wp->w_p_pvw)
6688 {
6689 STRCPY(p + len, _("[Preview]"));
6690 len += (int)STRLEN(p + len);
6691 }
6692#endif
6693 if (bufIsChanged(wp->w_buffer))
6694 {
6695 STRCPY(p + len, "[+]");
6696 len += 3;
6697 }
6698 if (wp->w_buffer->b_p_ro)
6699 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006700 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 len += 4;
6702 }
6703
6704#ifndef FEAT_VERTSPLIT
6705 this_ru_col = ru_col;
6706 if (this_ru_col < (Columns + 1) / 2)
6707 this_ru_col = (Columns + 1) / 2;
6708#else
6709 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6710 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6711 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6712 if (this_ru_col <= 1)
6713 {
6714 p = (char_u *)"<"; /* No room for file name! */
6715 len = 1;
6716 }
6717 else
6718#endif
6719#ifdef FEAT_MBYTE
6720 if (has_mbyte)
6721 {
6722 int clen = 0, i;
6723
6724 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006725 clen = mb_string2cells(p, -1);
6726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 /* Find first character that will fit.
6728 * Going from start to end is much faster for DBCS. */
6729 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006730 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 clen -= (*mb_ptr2cells)(p + i);
6732 len = clen;
6733 if (i > 0)
6734 {
6735 p = p + i - 1;
6736 *p = '<';
6737 ++len;
6738 }
6739
6740 }
6741 else
6742#endif
6743 if (len > this_ru_col - 1)
6744 {
6745 p += len - (this_ru_col - 1);
6746 *p = '<';
6747 len = this_ru_col - 1;
6748 }
6749
6750 row = W_WINROW(wp) + wp->w_height;
6751 screen_puts(p, row, W_WINCOL(wp), attr);
6752 screen_fill(row, row + 1, len + W_WINCOL(wp),
6753 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6754
6755 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6756 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6757 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6758 - 1 + W_WINCOL(wp)), attr);
6759
6760#ifdef FEAT_CMDL_INFO
6761 win_redr_ruler(wp, TRUE);
6762#endif
6763 }
6764
6765#ifdef FEAT_VERTSPLIT
6766 /*
6767 * May need to draw the character below the vertical separator.
6768 */
6769 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6770 {
6771 if (stl_connected(wp))
6772 fillchar = fillchar_status(&attr, wp == curwin);
6773 else
6774 fillchar = fillchar_vsep(&attr);
6775 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6776 attr);
6777 }
6778#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006779 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780}
6781
Bram Moolenaar238a5642006-02-21 22:12:05 +00006782#ifdef FEAT_STL_OPT
6783/*
6784 * Redraw the status line according to 'statusline' and take care of any
6785 * errors encountered.
6786 */
6787 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006788redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006789 win_T *wp;
6790{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006791 static int entered = FALSE;
6792 int save_called_emsg = called_emsg;
6793
6794 /* When called recursively return. This can happen when the statusline
6795 * contains an expression that triggers a redraw. */
6796 if (entered)
6797 return;
6798 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006799
6800 called_emsg = FALSE;
6801 win_redr_custom(wp, FALSE);
6802 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006803 {
6804 /* When there is an error disable the statusline, otherwise the
6805 * display is messed up with errors and a redraw triggers the problem
6806 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006807 set_string_option_direct((char_u *)"statusline", -1,
6808 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006809 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006810 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006811 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006812 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006813}
6814#endif
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816# ifdef FEAT_VERTSPLIT
6817/*
6818 * Return TRUE if the status line of window "wp" is connected to the status
6819 * line of the window right of it. If not, then it's a vertical separator.
6820 * Only call if (wp->w_vsep_width != 0).
6821 */
6822 int
6823stl_connected(wp)
6824 win_T *wp;
6825{
6826 frame_T *fr;
6827
6828 fr = wp->w_frame;
6829 while (fr->fr_parent != NULL)
6830 {
6831 if (fr->fr_parent->fr_layout == FR_COL)
6832 {
6833 if (fr->fr_next != NULL)
6834 break;
6835 }
6836 else
6837 {
6838 if (fr->fr_next != NULL)
6839 return TRUE;
6840 }
6841 fr = fr->fr_parent;
6842 }
6843 return FALSE;
6844}
6845# endif
6846
6847#endif /* FEAT_WINDOWS */
6848
6849#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6850/*
6851 * Get the value to show for the language mappings, active 'keymap'.
6852 */
6853 int
6854get_keymap_str(wp, buf, len)
6855 win_T *wp;
6856 char_u *buf; /* buffer for the result */
6857 int len; /* length of buffer */
6858{
6859 char_u *p;
6860
6861 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6862 return FALSE;
6863
6864 {
6865#ifdef FEAT_EVAL
6866 buf_T *old_curbuf = curbuf;
6867 win_T *old_curwin = curwin;
6868 char_u *s;
6869
6870 curbuf = wp->w_buffer;
6871 curwin = wp;
6872 STRCPY(buf, "b:keymap_name"); /* must be writable */
6873 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006874 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 --emsg_skip;
6876 curbuf = old_curbuf;
6877 curwin = old_curwin;
6878 if (p == NULL || *p == NUL)
6879#endif
6880 {
6881#ifdef FEAT_KEYMAP
6882 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6883 p = wp->w_buffer->b_p_keymap;
6884 else
6885#endif
6886 p = (char_u *)"lang";
6887 }
6888 if ((int)(STRLEN(p) + 3) < len)
6889 sprintf((char *)buf, "<%s>", p);
6890 else
6891 buf[0] = NUL;
6892#ifdef FEAT_EVAL
6893 vim_free(s);
6894#endif
6895 }
6896 return buf[0] != NUL;
6897}
6898#endif
6899
6900#if defined(FEAT_STL_OPT) || defined(PROTO)
6901/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006902 * Redraw the status line or ruler of window "wp".
6903 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 */
6905 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006906win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006908 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006910 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 int attr;
6912 int curattr;
6913 int row;
6914 int col = 0;
6915 int maxwidth;
6916 int width;
6917 int n;
6918 int len;
6919 int fillchar;
6920 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006921 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006923 struct stl_hlrec hltab[STL_MAX_ITEM];
6924 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006925 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006926 win_T *ewp;
6927 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
Bram Moolenaar1d633412013-12-11 15:52:01 +01006929 /* There is a tiny chance that this gets called recursively: When
6930 * redrawing a status line triggers redrawing the ruler or tabline.
6931 * Avoid trouble by not allowing recursion. */
6932 if (entered)
6933 return;
6934 entered = TRUE;
6935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006937 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006939 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006940 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006941 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006942 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006943 attr = hl_attr(HLF_TPF);
6944 maxwidth = Columns;
6945# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006946 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006949 else
6950 {
6951 row = W_WINROW(wp) + wp->w_height;
6952 fillchar = fillchar_status(&attr, wp == curwin);
6953 maxwidth = W_WIDTH(wp);
6954
6955 if (draw_ruler)
6956 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006957 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006958 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006959 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006960 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006961 if (*++stl == '-')
6962 stl++;
6963 if (atoi((char *)stl))
6964 while (VIM_ISDIGIT(*stl))
6965 stl++;
6966 if (*stl++ != '(')
6967 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006968 }
6969#ifdef FEAT_VERTSPLIT
6970 col = ru_col - (Columns - W_WIDTH(wp));
6971 if (col < (W_WIDTH(wp) + 1) / 2)
6972 col = (W_WIDTH(wp) + 1) / 2;
6973#else
6974 col = ru_col;
6975 if (col > (Columns + 1) / 2)
6976 col = (Columns + 1) / 2;
6977#endif
6978 maxwidth = W_WIDTH(wp) - col;
6979#ifdef FEAT_WINDOWS
6980 if (!wp->w_status_height)
6981#endif
6982 {
6983 row = Rows - 1;
6984 --maxwidth; /* writing in last column may cause scrolling */
6985 fillchar = ' ';
6986 attr = 0;
6987 }
6988
6989# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006990 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006991# endif
6992 }
6993 else
6994 {
6995 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006996 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006997 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006998 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006999# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007000 use_sandbox = was_set_insecurely((char_u *)"statusline",
7001 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007002# endif
7003 }
7004
7005#ifdef FEAT_VERTSPLIT
7006 col += W_WINCOL(wp);
7007#endif
7008 }
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007011 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar61452852011-02-01 18:01:11 +01007013 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7014 * the cursor away and back. */
7015 ewp = wp == NULL ? curwin : wp;
7016 p_crb_save = ewp->w_p_crb;
7017 ewp->w_p_crb = FALSE;
7018
Bram Moolenaar362f3562009-11-03 16:20:34 +00007019 /* Make a copy, because the statusline may include a function call that
7020 * might change the option value and free the memory. */
7021 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007022 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007023 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007024 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007025 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007026 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007028 /* Make all characters printable. */
7029 p = transstr(buf);
7030 if (p != NULL)
7031 {
7032 vim_strncpy(buf, p, sizeof(buf) - 1);
7033 vim_free(p);
7034 }
7035
7036 /* fill up with "fillchar" */
7037 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007038 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
7040#ifdef FEAT_MBYTE
7041 len += (*mb_char2bytes)(fillchar, buf + len);
7042#else
7043 buf[len++] = fillchar;
7044#endif
7045 ++width;
7046 }
7047 buf[len] = NUL;
7048
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007049 /*
7050 * Draw each snippet with the specified highlighting.
7051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 curattr = attr;
7053 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007054 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007056 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 screen_puts_len(p, len, row, col, curattr);
7058 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007059 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007061 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007063 else if (hltab[n].userhl < 0)
7064 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00007066 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007067 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#endif
7069 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007070 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 }
7072 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007073
7074 if (wp == NULL)
7075 {
7076 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7077 col = 0;
7078 len = 0;
7079 p = buf;
7080 fillchar = 0;
7081 for (n = 0; tabtab[n].start != NULL; n++)
7082 {
7083 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7084 while (col < len)
7085 TabPageIdxs[col++] = fillchar;
7086 p = tabtab[n].start;
7087 fillchar = tabtab[n].userhl;
7088 }
7089 while (col < Columns)
7090 TabPageIdxs[col++] = fillchar;
7091 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007092
7093theend:
7094 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}
7096
7097#endif /* FEAT_STL_OPT */
7098
7099/*
7100 * Output a single character directly to the screen and update ScreenLines.
7101 */
7102 void
7103screen_putchar(c, row, col, attr)
7104 int c;
7105 int row, col;
7106 int attr;
7107{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 char_u buf[MB_MAXBYTES + 1];
7109
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007110#ifdef FEAT_MBYTE
7111 if (has_mbyte)
7112 buf[(*mb_char2bytes)(c, buf)] = NUL;
7113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007115 {
7116 buf[0] = c;
7117 buf[1] = NUL;
7118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 screen_puts(buf, row, col, attr);
7120}
7121
7122/*
7123 * Get a single character directly from ScreenLines into "bytes[]".
7124 * Also return its attribute in *attrp;
7125 */
7126 void
7127screen_getbytes(row, col, bytes, attrp)
7128 int row, col;
7129 char_u *bytes;
7130 int *attrp;
7131{
7132 unsigned off;
7133
7134 /* safety check */
7135 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7136 {
7137 off = LineOffset[row] + col;
7138 *attrp = ScreenAttrs[off];
7139 bytes[0] = ScreenLines[off];
7140 bytes[1] = NUL;
7141
7142#ifdef FEAT_MBYTE
7143 if (enc_utf8 && ScreenLinesUC[off] != 0)
7144 bytes[utfc_char2bytes(off, bytes)] = NUL;
7145 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7146 {
7147 bytes[0] = ScreenLines[off];
7148 bytes[1] = ScreenLines2[off];
7149 bytes[2] = NUL;
7150 }
7151 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7152 {
7153 bytes[1] = ScreenLines[off + 1];
7154 bytes[2] = NUL;
7155 }
7156#endif
7157 }
7158}
7159
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007160#ifdef FEAT_MBYTE
7161static int screen_comp_differs __ARGS((int, int*));
7162
7163/*
7164 * Return TRUE if composing characters for screen posn "off" differs from
7165 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007166 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007167 */
7168 static int
7169screen_comp_differs(off, u8cc)
7170 int off;
7171 int *u8cc;
7172{
7173 int i;
7174
7175 for (i = 0; i < Screen_mco; ++i)
7176 {
7177 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7178 return TRUE;
7179 if (u8cc[i] == 0)
7180 break;
7181 }
7182 return FALSE;
7183}
7184#endif
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186/*
7187 * Put string '*text' on the screen at position 'row' and 'col', with
7188 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7189 * Note: only outputs within one row, message is truncated at screen boundary!
7190 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7191 */
7192 void
7193screen_puts(text, row, col, attr)
7194 char_u *text;
7195 int row;
7196 int col;
7197 int attr;
7198{
7199 screen_puts_len(text, -1, row, col, attr);
7200}
7201
7202/*
7203 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7204 * a NUL.
7205 */
7206 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007207screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007209 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 int row;
7211 int col;
7212 int attr;
7213{
7214 unsigned off;
7215 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007216 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 int c;
7218#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007219 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 int mbyte_blen = 1;
7221 int mbyte_cells = 1;
7222 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007223 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int clear_next_cell = FALSE;
7225# ifdef FEAT_ARABIC
7226 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007227 int pc, nc, nc1;
7228 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229# endif
7230#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007231#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7232 int force_redraw_this;
7233 int force_redraw_next = FALSE;
7234#endif
7235 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7238 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007239 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaarc236c162008-07-13 17:41:49 +00007241#ifdef FEAT_MBYTE
7242 /* When drawing over the right halve of a double-wide char clear out the
7243 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007244 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007245# ifdef FEAT_GUI
7246 && !gui.in_use
7247# endif
7248 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007249 {
7250 ScreenLines[off - 1] = ' ';
7251 ScreenAttrs[off - 1] = 0;
7252 if (enc_utf8)
7253 {
7254 ScreenLinesUC[off - 1] = 0;
7255 ScreenLinesC[0][off - 1] = 0;
7256 }
7257 /* redraw the previous cell, make it empty */
7258 screen_char(off - 1, row, col - 1);
7259 /* force the cell at "col" to be redrawn */
7260 force_redraw_next = TRUE;
7261 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007262#endif
7263
Bram Moolenaar367329b2007-08-30 11:53:22 +00007264#ifdef FEAT_MBYTE
7265 max_off = LineOffset[row] + screen_Columns;
7266#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007267 while (col < screen_Columns
7268 && (len < 0 || (int)(ptr - text) < len)
7269 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 {
7271 c = *ptr;
7272#ifdef FEAT_MBYTE
7273 /* check if this is the first byte of a multibyte */
7274 if (has_mbyte)
7275 {
7276 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007277 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007279 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7281 mbyte_cells = 1;
7282 else if (enc_dbcs != 0)
7283 mbyte_cells = mbyte_blen;
7284 else /* enc_utf8 */
7285 {
7286 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007287 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 (int)((text + len) - ptr));
7289 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007290 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007292# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 /* Non-BMP character: display as ? or fullwidth ?. */
7294 if (u8c >= 0x10000)
7295 {
7296 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7297 if (attr == 0)
7298 attr = hl_attr(HLF_8);
7299 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301# ifdef FEAT_ARABIC
7302 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7303 {
7304 /* Do Arabic shaping. */
7305 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7306 {
7307 /* Past end of string to be displayed. */
7308 nc = NUL;
7309 nc1 = NUL;
7310 }
7311 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007312 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007313 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7314 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007315 nc1 = pcc[0];
7316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 pc = prev_c;
7318 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007319 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321 else
7322 prev_c = u8c;
7323# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007324 if (col + mbyte_cells > screen_Columns)
7325 {
7326 /* Only 1 cell left, but character requires 2 cells:
7327 * display a '>' in the last column to avoid wrapping. */
7328 c = '>';
7329 mbyte_cells = 1;
7330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 }
7332 }
7333#endif
7334
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007335#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7336 force_redraw_this = force_redraw_next;
7337 force_redraw_next = FALSE;
7338#endif
7339
7340 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341#ifdef FEAT_MBYTE
7342 || (mbyte_cells == 2
7343 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7344 || (enc_dbcs == DBCS_JPNU
7345 && c == 0x8e
7346 && ScreenLines2[off] != ptr[1])
7347 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007348 && (ScreenLinesUC[off] !=
7349 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7350 || (ScreenLinesUC[off] != 0
7351 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352#endif
7353 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007354 || exmode_active;
7355
7356 if (need_redraw
7357#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7358 || force_redraw_this
7359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 )
7361 {
7362#if defined(FEAT_GUI) || defined(UNIX)
7363 /* The bold trick makes a single row of pixels appear in the next
7364 * character. When a bold character is removed, the next
7365 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007366 * and for some xterms. */
7367 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368# ifdef FEAT_GUI
7369 gui.in_use
7370# endif
7371# if defined(FEAT_GUI) && defined(UNIX)
7372 ||
7373# endif
7374# ifdef UNIX
7375 term_is_xterm
7376# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007377 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007379 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007381 if (n > HL_ALL)
7382 n = syn_attr2attr(n);
7383 if (n & HL_BOLD)
7384 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386#endif
7387#ifdef FEAT_MBYTE
7388 /* When at the end of the text and overwriting a two-cell
7389 * character with a one-cell character, need to clear the next
7390 * cell. Also when overwriting the left halve of a two-cell char
7391 * with the right halve of a two-cell char. Do this only once
7392 * (mb_off2cells() may return 2 on the right halve). */
7393 if (clear_next_cell)
7394 clear_next_cell = FALSE;
7395 else if (has_mbyte
7396 && (len < 0 ? ptr[mbyte_blen] == NUL
7397 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007398 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007400 && (*mb_off2cells)(off, max_off) == 1
7401 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 clear_next_cell = TRUE;
7403
7404 /* Make sure we never leave a second byte of a double-byte behind,
7405 * it confuses mb_off2cells(). */
7406 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007407 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007409 && (*mb_off2cells)(off, max_off) == 1
7410 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 ScreenLines[off + mbyte_blen] = 0;
7412#endif
7413 ScreenLines[off] = c;
7414 ScreenAttrs[off] = attr;
7415#ifdef FEAT_MBYTE
7416 if (enc_utf8)
7417 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 ScreenLinesUC[off] = 0;
7420 else
7421 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007422 int i;
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007425 for (i = 0; i < Screen_mco; ++i)
7426 {
7427 ScreenLinesC[i][off] = u8cc[i];
7428 if (u8cc[i] == 0)
7429 break;
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 if (mbyte_cells == 2)
7433 {
7434 ScreenLines[off + 1] = 0;
7435 ScreenAttrs[off + 1] = attr;
7436 }
7437 screen_char(off, row, col);
7438 }
7439 else if (mbyte_cells == 2)
7440 {
7441 ScreenLines[off + 1] = ptr[1];
7442 ScreenAttrs[off + 1] = attr;
7443 screen_char_2(off, row, col);
7444 }
7445 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7446 {
7447 ScreenLines2[off] = ptr[1];
7448 screen_char(off, row, col);
7449 }
7450 else
7451#endif
7452 screen_char(off, row, col);
7453 }
7454#ifdef FEAT_MBYTE
7455 if (has_mbyte)
7456 {
7457 off += mbyte_cells;
7458 col += mbyte_cells;
7459 ptr += mbyte_blen;
7460 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007461 {
7462 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007464 len = -1;
7465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467 else
7468#endif
7469 {
7470 ++off;
7471 ++col;
7472 ++ptr;
7473 }
7474 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007475
7476#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7477 /* If we detected the next character needs to be redrawn, but the text
7478 * doesn't extend up to there, update the character here. */
7479 if (force_redraw_next && col < screen_Columns)
7480 {
7481# ifdef FEAT_MBYTE
7482 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7483 screen_char_2(off, row, col);
7484 else
7485# endif
7486 screen_char(off, row, col);
7487 }
7488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489}
7490
7491#ifdef FEAT_SEARCH_EXTRA
7492/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007493 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 */
7495 static void
7496start_search_hl()
7497{
7498 if (p_hls && !no_hlsearch)
7499 {
7500 last_pat_prog(&search_hl.rm);
7501 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007502# ifdef FEAT_RELTIME
7503 /* Set the time limit to 'redrawtime'. */
7504 profile_setlimit(p_rdt, &search_hl.tm);
7505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507}
7508
7509/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007510 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
7512 static void
7513end_search_hl()
7514{
7515 if (search_hl.rm.regprog != NULL)
7516 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007517 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 search_hl.rm.regprog = NULL;
7519 }
7520}
7521
7522/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007523 * Init for calling prepare_search_hl().
7524 */
7525 static void
7526init_search_hl(wp)
7527 win_T *wp;
7528{
7529 matchitem_T *cur;
7530
7531 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7532 * match */
7533 cur = wp->w_match_head;
7534 while (cur != NULL)
7535 {
7536 cur->hl.rm = cur->match;
7537 if (cur->hlg_id == 0)
7538 cur->hl.attr = 0;
7539 else
7540 cur->hl.attr = syn_id2attr(cur->hlg_id);
7541 cur->hl.buf = wp->w_buffer;
7542 cur->hl.lnum = 0;
7543 cur->hl.first_lnum = 0;
7544# ifdef FEAT_RELTIME
7545 /* Set the time limit to 'redrawtime'. */
7546 profile_setlimit(p_rdt, &(cur->hl.tm));
7547# endif
7548 cur = cur->next;
7549 }
7550 search_hl.buf = wp->w_buffer;
7551 search_hl.lnum = 0;
7552 search_hl.first_lnum = 0;
7553 /* time limit is set at the toplevel, for all windows */
7554}
7555
7556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 * Advance to the match in window "wp" line "lnum" or past it.
7558 */
7559 static void
7560prepare_search_hl(wp, lnum)
7561 win_T *wp;
7562 linenr_T lnum;
7563{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007564 matchitem_T *cur; /* points to the match list */
7565 match_T *shl; /* points to search_hl or a match */
7566 int shl_flag; /* flag to indicate whether search_hl
7567 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007568 int pos_inprogress; /* marks that position match search is
7569 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 int n;
7571
7572 /*
7573 * When using a multi-line pattern, start searching at the top
7574 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007575 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007577 cur = wp->w_match_head;
7578 shl_flag = FALSE;
7579 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007581 if (shl_flag == FALSE)
7582 {
7583 shl = &search_hl;
7584 shl_flag = TRUE;
7585 }
7586 else
7587 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (shl->rm.regprog != NULL
7589 && shl->lnum == 0
7590 && re_multiline(shl->rm.regprog))
7591 {
7592 if (shl->first_lnum == 0)
7593 {
7594# ifdef FEAT_FOLDING
7595 for (shl->first_lnum = lnum;
7596 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7597 if (hasFoldingWin(wp, shl->first_lnum - 1,
7598 NULL, NULL, TRUE, NULL))
7599 break;
7600# else
7601 shl->first_lnum = wp->w_topline;
7602# endif
7603 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007604 if (cur != NULL)
7605 cur->pos.cur = 0;
7606 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007608 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7609 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007611 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7612 pos_inprogress = cur == NULL || cur->pos.cur == 0
7613 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (shl->lnum != 0)
7615 {
7616 shl->first_lnum = shl->lnum
7617 + shl->rm.endpos[0].lnum
7618 - shl->rm.startpos[0].lnum;
7619 n = shl->rm.endpos[0].col;
7620 }
7621 else
7622 {
7623 ++shl->first_lnum;
7624 n = 0;
7625 }
7626 }
7627 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007628 if (shl != &search_hl && cur != NULL)
7629 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631}
7632
7633/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007634 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 * Uses shl->buf.
7636 * Sets shl->lnum and shl->rm contents.
7637 * Note: Assumes a previous match is always before "lnum", unless
7638 * shl->lnum is zero.
7639 * Careful: Any pointers for buffer lines will become invalid.
7640 */
7641 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007642next_search_hl(win, shl, lnum, mincol, cur)
7643 win_T *win;
7644 match_T *shl; /* points to search_hl or a match */
7645 linenr_T lnum;
7646 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007647 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
7649 linenr_T l;
7650 colnr_T matchcol;
7651 long nmatched;
7652
7653 if (shl->lnum != 0)
7654 {
7655 /* Check for three situations:
7656 * 1. If the "lnum" is below a previous match, start a new search.
7657 * 2. If the previous match includes "mincol", use it.
7658 * 3. Continue after the previous match.
7659 */
7660 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7661 if (lnum > l)
7662 shl->lnum = 0;
7663 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7664 return;
7665 }
7666
7667 /*
7668 * Repeat searching for a match until one is found that includes "mincol"
7669 * or none is found in this line.
7670 */
7671 called_emsg = FALSE;
7672 for (;;)
7673 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007674#ifdef FEAT_RELTIME
7675 /* Stop searching after passing the time limit. */
7676 if (profile_passed_limit(&(shl->tm)))
7677 {
7678 shl->lnum = 0; /* no match found in time */
7679 break;
7680 }
7681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 /* Three situations:
7683 * 1. No useful previous match: search from start of line.
7684 * 2. Not Vi compatible or empty match: continue at next character.
7685 * Break the loop if this is beyond the end of the line.
7686 * 3. Vi compatible searching: continue at end of previous match.
7687 */
7688 if (shl->lnum == 0)
7689 matchcol = 0;
7690 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7691 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007692 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007694 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007695
7696 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007697 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007698 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007700 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 shl->lnum = 0;
7702 break;
7703 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007704#ifdef FEAT_MBYTE
7705 if (has_mbyte)
7706 matchcol += mb_ptr2len(ml);
7707 else
7708#endif
7709 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 else
7712 matchcol = shl->rm.endpos[0].col;
7713
7714 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007715 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007717 /* Remember whether shl->rm is using a copy of the regprog in
7718 * cur->match. */
7719 int regprog_is_copy = (shl != &search_hl && cur != NULL
7720 && shl == &cur->hl
7721 && cur->match.regprog == cur->hl.rm.regprog);
7722
Bram Moolenaarb3414592014-06-17 17:48:32 +02007723 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7724 matchcol,
7725#ifdef FEAT_RELTIME
7726 &(shl->tm)
7727#else
7728 NULL
7729#endif
7730 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007731 /* Copy the regprog, in case it got freed and recompiled. */
7732 if (regprog_is_copy)
7733 cur->match.regprog = cur->hl.rm.regprog;
7734
Bram Moolenaarb3414592014-06-17 17:48:32 +02007735 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007736 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007737 /* Error while handling regexp: stop using this regexp. */
7738 if (shl == &search_hl)
7739 {
7740 /* don't free regprog in the match list, it's a copy */
7741 vim_regfree(shl->rm.regprog);
7742 SET_NO_HLSEARCH(TRUE);
7743 }
7744 shl->rm.regprog = NULL;
7745 shl->lnum = 0;
7746 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7747 message */
7748 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007749 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007750 }
7751 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007752 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007753 else
7754 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 if (nmatched == 0)
7756 {
7757 shl->lnum = 0; /* no match found */
7758 break;
7759 }
7760 if (shl->rm.startpos[0].lnum > 0
7761 || shl->rm.startpos[0].col >= mincol
7762 || nmatched > 1
7763 || shl->rm.endpos[0].col > mincol)
7764 {
7765 shl->lnum += shl->rm.startpos[0].lnum;
7766 break; /* useful match found */
7767 }
7768 }
7769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaarb3414592014-06-17 17:48:32 +02007771 static int
7772next_search_hl_pos(shl, lnum, posmatch, mincol)
7773 match_T *shl; /* points to a match */
7774 linenr_T lnum;
7775 posmatch_T *posmatch; /* match positions */
7776 colnr_T mincol; /* minimal column for a match */
7777{
7778 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007779 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007780
7781 shl->lnum = 0;
7782 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7783 {
7784 if (posmatch->pos[i].lnum == 0)
7785 break;
7786 if (posmatch->pos[i].col < mincol)
7787 continue;
7788 if (posmatch->pos[i].lnum == lnum)
7789 {
7790 if (shl->lnum == lnum)
7791 {
7792 /* partially sort positions by column numbers
7793 * on the same line */
7794 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7795 {
7796 llpos_T tmp = posmatch->pos[i];
7797
7798 posmatch->pos[i] = posmatch->pos[bot];
7799 posmatch->pos[bot] = tmp;
7800 }
7801 }
7802 else
7803 {
7804 bot = i;
7805 shl->lnum = lnum;
7806 }
7807 }
7808 }
7809 posmatch->cur = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02007810 if (shl->lnum == lnum && bot >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007811 {
7812 colnr_T start = posmatch->pos[bot].col == 0
7813 ? 0 : posmatch->pos[bot].col - 1;
7814 colnr_T end = posmatch->pos[bot].col == 0
7815 ? MAXCOL : start + posmatch->pos[bot].len;
7816
7817 shl->rm.startpos[0].lnum = 0;
7818 shl->rm.startpos[0].col = start;
7819 shl->rm.endpos[0].lnum = 0;
7820 shl->rm.endpos[0].col = end;
7821 posmatch->cur = bot + 1;
7822 return TRUE;
7823 }
7824 return FALSE;
7825}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007826#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 static void
7829screen_start_highlight(attr)
7830 int attr;
7831{
7832 attrentry_T *aep = NULL;
7833
7834 screen_attr = attr;
7835 if (full_screen
7836#ifdef WIN3264
7837 && termcap_active
7838#endif
7839 )
7840 {
7841#ifdef FEAT_GUI
7842 if (gui.in_use)
7843 {
7844 char buf[20];
7845
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007846 /* The GUI handles this internally. */
7847 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 OUT_STR(buf);
7849 }
7850 else
7851#endif
7852 {
7853 if (attr > HL_ALL) /* special HL attr. */
7854 {
7855 if (t_colors > 1)
7856 aep = syn_cterm_attr2entry(attr);
7857 else
7858 aep = syn_term_attr2entry(attr);
7859 if (aep == NULL) /* did ":syntax clear" */
7860 attr = 0;
7861 else
7862 attr = aep->ae_attr;
7863 }
7864 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7865 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007866 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7867 && cterm_normal_fg_bold)
7868 /* If the Normal FG color has BOLD attribute and the new HL
7869 * has a FG color defined, clear BOLD. */
7870 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7872 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007873 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7874 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 out_str(T_US);
7876 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7877 out_str(T_CZH);
7878 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7879 out_str(T_MR);
7880
7881 /*
7882 * Output the color or start string after bold etc., in case the
7883 * bold etc. override the color setting.
7884 */
7885 if (aep != NULL)
7886 {
7887 if (t_colors > 1)
7888 {
7889 if (aep->ae_u.cterm.fg_color)
7890 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7891 if (aep->ae_u.cterm.bg_color)
7892 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7893 }
7894 else
7895 {
7896 if (aep->ae_u.term.start != NULL)
7897 out_str(aep->ae_u.term.start);
7898 }
7899 }
7900 }
7901 }
7902}
7903
7904 void
7905screen_stop_highlight()
7906{
7907 int do_ME = FALSE; /* output T_ME code */
7908
7909 if (screen_attr != 0
7910#ifdef WIN3264
7911 && termcap_active
7912#endif
7913 )
7914 {
7915#ifdef FEAT_GUI
7916 if (gui.in_use)
7917 {
7918 char buf[20];
7919
7920 /* use internal GUI code */
7921 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7922 OUT_STR(buf);
7923 }
7924 else
7925#endif
7926 {
7927 if (screen_attr > HL_ALL) /* special HL attr. */
7928 {
7929 attrentry_T *aep;
7930
7931 if (t_colors > 1)
7932 {
7933 /*
7934 * Assume that t_me restores the original colors!
7935 */
7936 aep = syn_cterm_attr2entry(screen_attr);
7937 if (aep != NULL && (aep->ae_u.cterm.fg_color
7938 || aep->ae_u.cterm.bg_color))
7939 do_ME = TRUE;
7940 }
7941 else
7942 {
7943 aep = syn_term_attr2entry(screen_attr);
7944 if (aep != NULL && aep->ae_u.term.stop != NULL)
7945 {
7946 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7947 do_ME = TRUE;
7948 else
7949 out_str(aep->ae_u.term.stop);
7950 }
7951 }
7952 if (aep == NULL) /* did ":syntax clear" */
7953 screen_attr = 0;
7954 else
7955 screen_attr = aep->ae_attr;
7956 }
7957
7958 /*
7959 * Often all ending-codes are equal to T_ME. Avoid outputting the
7960 * same sequence several times.
7961 */
7962 if (screen_attr & HL_STANDOUT)
7963 {
7964 if (STRCMP(T_SE, T_ME) == 0)
7965 do_ME = TRUE;
7966 else
7967 out_str(T_SE);
7968 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007969 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {
7971 if (STRCMP(T_UE, T_ME) == 0)
7972 do_ME = TRUE;
7973 else
7974 out_str(T_UE);
7975 }
7976 if (screen_attr & HL_ITALIC)
7977 {
7978 if (STRCMP(T_CZR, T_ME) == 0)
7979 do_ME = TRUE;
7980 else
7981 out_str(T_CZR);
7982 }
7983 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7984 out_str(T_ME);
7985
7986 if (t_colors > 1)
7987 {
7988 /* set Normal cterm colors */
7989 if (cterm_normal_fg_color != 0)
7990 term_fg_color(cterm_normal_fg_color - 1);
7991 if (cterm_normal_bg_color != 0)
7992 term_bg_color(cterm_normal_bg_color - 1);
7993 if (cterm_normal_fg_bold)
7994 out_str(T_MD);
7995 }
7996 }
7997 }
7998 screen_attr = 0;
7999}
8000
8001/*
8002 * Reset the colors for a cterm. Used when leaving Vim.
8003 * The machine specific code may override this again.
8004 */
8005 void
8006reset_cterm_colors()
8007{
8008 if (t_colors > 1)
8009 {
8010 /* set Normal cterm colors */
8011 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
8012 {
8013 out_str(T_OP);
8014 screen_attr = -1;
8015 }
8016 if (cterm_normal_fg_bold)
8017 {
8018 out_str(T_ME);
8019 screen_attr = -1;
8020 }
8021 }
8022}
8023
8024/*
8025 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8026 * using the attributes from ScreenAttrs["off"].
8027 */
8028 static void
8029screen_char(off, row, col)
8030 unsigned off;
8031 int row;
8032 int col;
8033{
8034 int attr;
8035
8036 /* Check for illegal values, just in case (could happen just after
8037 * resizing). */
8038 if (row >= screen_Rows || col >= screen_Columns)
8039 return;
8040
Bram Moolenaar494838a2015-02-10 19:20:37 +01008041 /* Outputting a character in the last cell on the screen may scroll the
8042 * screen up. Only do it when the "xn" termcap property is set, otherwise
8043 * mark the character invalid (update it when scrolled up). */
8044 if (*T_XN == NUL
8045 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046#ifdef FEAT_RIGHTLEFT
8047 /* account for first command-line character in rightleft mode */
8048 && !cmdmsg_rl
8049#endif
8050 )
8051 {
8052 ScreenAttrs[off] = (sattr_T)-1;
8053 return;
8054 }
8055
8056 /*
8057 * Stop highlighting first, so it's easier to move the cursor.
8058 */
8059#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
8060 if (screen_char_attr != 0)
8061 attr = screen_char_attr;
8062 else
8063#endif
8064 attr = ScreenAttrs[off];
8065 if (screen_attr != attr)
8066 screen_stop_highlight();
8067
8068 windgoto(row, col);
8069
8070 if (screen_attr != attr)
8071 screen_start_highlight(attr);
8072
8073#ifdef FEAT_MBYTE
8074 if (enc_utf8 && ScreenLinesUC[off] != 0)
8075 {
8076 char_u buf[MB_MAXBYTES + 1];
8077
8078 /* Convert UTF-8 character to bytes and write it. */
8079
8080 buf[utfc_char2bytes(off, buf)] = NUL;
8081
8082 out_str(buf);
8083 if (utf_char2cells(ScreenLinesUC[off]) > 1)
8084 ++screen_cur_col;
8085 }
8086 else
8087#endif
8088 {
8089#ifdef FEAT_MBYTE
8090 out_flush_check();
8091#endif
8092 out_char(ScreenLines[off]);
8093#ifdef FEAT_MBYTE
8094 /* double-byte character in single-width cell */
8095 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8096 out_char(ScreenLines2[off]);
8097#endif
8098 }
8099
8100 screen_cur_col++;
8101}
8102
8103#ifdef FEAT_MBYTE
8104
8105/*
8106 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8107 * on the screen at position 'row' and 'col'.
8108 * The attributes of the first byte is used for all. This is required to
8109 * output the two bytes of a double-byte character with nothing in between.
8110 */
8111 static void
8112screen_char_2(off, row, col)
8113 unsigned off;
8114 int row;
8115 int col;
8116{
8117 /* Check for illegal values (could be wrong when screen was resized). */
8118 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8119 return;
8120
8121 /* Outputting the last character on the screen may scrollup the screen.
8122 * Don't to it! Mark the character invalid (update it when scrolled up) */
8123 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8124 {
8125 ScreenAttrs[off] = (sattr_T)-1;
8126 return;
8127 }
8128
8129 /* Output the first byte normally (positions the cursor), then write the
8130 * second byte directly. */
8131 screen_char(off, row, col);
8132 out_char(ScreenLines[off + 1]);
8133 ++screen_cur_col;
8134}
8135#endif
8136
8137#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
8138/*
8139 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8140 * This uses the contents of ScreenLines[] and doesn't change it.
8141 */
8142 void
8143screen_draw_rectangle(row, col, height, width, invert)
8144 int row;
8145 int col;
8146 int height;
8147 int width;
8148 int invert;
8149{
8150 int r, c;
8151 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008152#ifdef FEAT_MBYTE
8153 int max_off;
8154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008156 /* Can't use ScreenLines unless initialized */
8157 if (ScreenLines == NULL)
8158 return;
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 if (invert)
8161 screen_char_attr = HL_INVERSE;
8162 for (r = row; r < row + height; ++r)
8163 {
8164 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008165#ifdef FEAT_MBYTE
8166 max_off = off + screen_Columns;
8167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 for (c = col; c < col + width; ++c)
8169 {
8170#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008171 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {
8173 screen_char_2(off + c, r, c);
8174 ++c;
8175 }
8176 else
8177#endif
8178 {
8179 screen_char(off + c, r, c);
8180#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008181 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 ++c;
8183#endif
8184 }
8185 }
8186 }
8187 screen_char_attr = 0;
8188}
8189#endif
8190
8191#ifdef FEAT_VERTSPLIT
8192/*
8193 * Redraw the characters for a vertically split window.
8194 */
8195 static void
8196redraw_block(row, end, wp)
8197 int row;
8198 int end;
8199 win_T *wp;
8200{
8201 int col;
8202 int width;
8203
8204# ifdef FEAT_CLIPBOARD
8205 clip_may_clear_selection(row, end - 1);
8206# endif
8207
8208 if (wp == NULL)
8209 {
8210 col = 0;
8211 width = Columns;
8212 }
8213 else
8214 {
8215 col = wp->w_wincol;
8216 width = wp->w_width;
8217 }
8218 screen_draw_rectangle(row, col, end - row, width, FALSE);
8219}
8220#endif
8221
8222/*
8223 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8224 * with character 'c1' in first column followed by 'c2' in the other columns.
8225 * Use attributes 'attr'.
8226 */
8227 void
8228screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8229 int start_row, end_row;
8230 int start_col, end_col;
8231 int c1, c2;
8232 int attr;
8233{
8234 int row;
8235 int col;
8236 int off;
8237 int end_off;
8238 int did_delete;
8239 int c;
8240 int norm_term;
8241#if defined(FEAT_GUI) || defined(UNIX)
8242 int force_next = FALSE;
8243#endif
8244
8245 if (end_row > screen_Rows) /* safety check */
8246 end_row = screen_Rows;
8247 if (end_col > screen_Columns) /* safety check */
8248 end_col = screen_Columns;
8249 if (ScreenLines == NULL
8250 || start_row >= end_row
8251 || start_col >= end_col) /* nothing to do */
8252 return;
8253
8254 /* it's a "normal" terminal when not in a GUI or cterm */
8255 norm_term = (
8256#ifdef FEAT_GUI
8257 !gui.in_use &&
8258#endif
8259 t_colors <= 1);
8260 for (row = start_row; row < end_row; ++row)
8261 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008262#ifdef FEAT_MBYTE
8263 if (has_mbyte
8264# ifdef FEAT_GUI
8265 && !gui.in_use
8266# endif
8267 )
8268 {
8269 /* When drawing over the right halve of a double-wide char clear
8270 * out the left halve. When drawing over the left halve of a
8271 * double wide-char clear out the right halve. Only needed in a
8272 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008273 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008274 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008275 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008276 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008277 }
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 /*
8280 * Try to use delete-line termcap code, when no attributes or in a
8281 * "normal" terminal, where a bold/italic space is just a
8282 * space.
8283 */
8284 did_delete = FALSE;
8285 if (c2 == ' '
8286 && end_col == Columns
8287 && can_clear(T_CE)
8288 && (attr == 0
8289 || (norm_term
8290 && attr <= HL_ALL
8291 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8292 {
8293 /*
8294 * check if we really need to clear something
8295 */
8296 col = start_col;
8297 if (c1 != ' ') /* don't clear first char */
8298 ++col;
8299
8300 off = LineOffset[row] + col;
8301 end_off = LineOffset[row] + end_col;
8302
8303 /* skip blanks (used often, keep it fast!) */
8304#ifdef FEAT_MBYTE
8305 if (enc_utf8)
8306 while (off < end_off && ScreenLines[off] == ' '
8307 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8308 ++off;
8309 else
8310#endif
8311 while (off < end_off && ScreenLines[off] == ' '
8312 && ScreenAttrs[off] == 0)
8313 ++off;
8314 if (off < end_off) /* something to be cleared */
8315 {
8316 col = off - LineOffset[row];
8317 screen_stop_highlight();
8318 term_windgoto(row, col);/* clear rest of this screen line */
8319 out_str(T_CE);
8320 screen_start(); /* don't know where cursor is now */
8321 col = end_col - col;
8322 while (col--) /* clear chars in ScreenLines */
8323 {
8324 ScreenLines[off] = ' ';
8325#ifdef FEAT_MBYTE
8326 if (enc_utf8)
8327 ScreenLinesUC[off] = 0;
8328#endif
8329 ScreenAttrs[off] = 0;
8330 ++off;
8331 }
8332 }
8333 did_delete = TRUE; /* the chars are cleared now */
8334 }
8335
8336 off = LineOffset[row] + start_col;
8337 c = c1;
8338 for (col = start_col; col < end_col; ++col)
8339 {
8340 if (ScreenLines[off] != c
8341#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008342 || (enc_utf8 && (int)ScreenLinesUC[off]
8343 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344#endif
8345 || ScreenAttrs[off] != attr
8346#if defined(FEAT_GUI) || defined(UNIX)
8347 || force_next
8348#endif
8349 )
8350 {
8351#if defined(FEAT_GUI) || defined(UNIX)
8352 /* The bold trick may make a single row of pixels appear in
8353 * the next character. When a bold character is removed, the
8354 * next character should be redrawn too. This happens for our
8355 * own GUI and for some xterms. */
8356 if (
8357# ifdef FEAT_GUI
8358 gui.in_use
8359# endif
8360# if defined(FEAT_GUI) && defined(UNIX)
8361 ||
8362# endif
8363# ifdef UNIX
8364 term_is_xterm
8365# endif
8366 )
8367 {
8368 if (ScreenLines[off] != ' '
8369 && (ScreenAttrs[off] > HL_ALL
8370 || ScreenAttrs[off] & HL_BOLD))
8371 force_next = TRUE;
8372 else
8373 force_next = FALSE;
8374 }
8375#endif
8376 ScreenLines[off] = c;
8377#ifdef FEAT_MBYTE
8378 if (enc_utf8)
8379 {
8380 if (c >= 0x80)
8381 {
8382 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008383 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385 else
8386 ScreenLinesUC[off] = 0;
8387 }
8388#endif
8389 ScreenAttrs[off] = attr;
8390 if (!did_delete || c != ' ')
8391 screen_char(off, row, col);
8392 }
8393 ++off;
8394 if (col == start_col)
8395 {
8396 if (did_delete)
8397 break;
8398 c = c2;
8399 }
8400 }
8401 if (end_col == Columns)
8402 LineWraps[row] = FALSE;
8403 if (row == Rows - 1) /* overwritten the command line */
8404 {
8405 redraw_cmdline = TRUE;
8406 if (c1 == ' ' && c2 == ' ')
8407 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008408 if (start_col == 0)
8409 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
8411 }
8412}
8413
8414/*
8415 * Check if there should be a delay. Used before clearing or redrawing the
8416 * screen or the command line.
8417 */
8418 void
8419check_for_delay(check_msg_scroll)
8420 int check_msg_scroll;
8421{
8422 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8423 && !did_wait_return
8424 && emsg_silent == 0)
8425 {
8426 out_flush();
8427 ui_delay(1000L, TRUE);
8428 emsg_on_display = FALSE;
8429 if (check_msg_scroll)
8430 msg_scroll = FALSE;
8431 }
8432}
8433
8434/*
8435 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008436 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 * Returns TRUE if there is a valid screen to write to.
8438 * Returns FALSE when starting up and screen not initialized yet.
8439 */
8440 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008441screen_valid(doclear)
8442 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008444 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 return (ScreenLines != NULL);
8446}
8447
8448/*
8449 * Resize the shell to Rows and Columns.
8450 * Allocate ScreenLines[] and associated items.
8451 *
8452 * There may be some time between setting Rows and Columns and (re)allocating
8453 * ScreenLines[]. This happens when starting up and when (manually) changing
8454 * the shell size. Always use screen_Rows and screen_Columns to access items
8455 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8456 * final size of the shell is needed.
8457 */
8458 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008459screenalloc(doclear)
8460 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461{
8462 int new_row, old_row;
8463#ifdef FEAT_GUI
8464 int old_Rows;
8465#endif
8466 win_T *wp;
8467 int outofmem = FALSE;
8468 int len;
8469 schar_T *new_ScreenLines;
8470#ifdef FEAT_MBYTE
8471 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008472 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008474 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475#endif
8476 sattr_T *new_ScreenAttrs;
8477 unsigned *new_LineOffset;
8478 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008479#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008480 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008481 tabpage_T *tp;
8482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008484 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008485#ifdef FEAT_AUTOCMD
8486 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008488retry:
8489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 /*
8491 * Allocation of the screen buffers is done only when the size changes and
8492 * when Rows and Columns have been set and we have started doing full
8493 * screen stuff.
8494 */
8495 if ((ScreenLines != NULL
8496 && Rows == screen_Rows
8497 && Columns == screen_Columns
8498#ifdef FEAT_MBYTE
8499 && enc_utf8 == (ScreenLinesUC != NULL)
8500 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008501 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502#endif
8503 )
8504 || Rows == 0
8505 || Columns == 0
8506 || (!full_screen && ScreenLines == NULL))
8507 return;
8508
8509 /*
8510 * It's possible that we produce an out-of-memory message below, which
8511 * will cause this function to be called again. To break the loop, just
8512 * return here.
8513 */
8514 if (entered)
8515 return;
8516 entered = TRUE;
8517
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008518 /*
8519 * Note that the window sizes are updated before reallocating the arrays,
8520 * thus we must not redraw here!
8521 */
8522 ++RedrawingDisabled;
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 win_new_shellsize(); /* fit the windows in the new sized shell */
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 comp_col(); /* recompute columns for shown command and ruler */
8527
8528 /*
8529 * We're changing the size of the screen.
8530 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8531 * - Move lines from the old arrays into the new arrays, clear extra
8532 * lines (unless the screen is going to be cleared).
8533 * - Free the old arrays.
8534 *
8535 * If anything fails, make ScreenLines NULL, so we don't do anything!
8536 * Continuing with the old ScreenLines may result in a crash, because the
8537 * size is wrong.
8538 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008539 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008541#ifdef FEAT_AUTOCMD
8542 if (aucmd_win != NULL)
8543 win_free_lsize(aucmd_win);
8544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
8546 new_ScreenLines = (schar_T *)lalloc((long_u)(
8547 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8548#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008549 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (enc_utf8)
8551 {
8552 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8553 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008554 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008555 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8557 }
8558 if (enc_dbcs == DBCS_JPNU)
8559 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8560 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8561#endif
8562 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8563 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8564 new_LineOffset = (unsigned *)lalloc((long_u)(
8565 Rows * sizeof(unsigned)), FALSE);
8566 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008567#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008568 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008571 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {
8573 if (win_alloc_lines(wp) == FAIL)
8574 {
8575 outofmem = TRUE;
8576#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008577 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578#endif
8579 }
8580 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008581#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008582 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8583 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008584 outofmem = TRUE;
8585#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008586#ifdef FEAT_WINDOWS
8587give_up:
8588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008590#ifdef FEAT_MBYTE
8591 for (i = 0; i < p_mco; ++i)
8592 if (new_ScreenLinesC[i] == NULL)
8593 break;
8594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (new_ScreenLines == NULL
8596#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008597 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8599#endif
8600 || new_ScreenAttrs == NULL
8601 || new_LineOffset == NULL
8602 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008603#ifdef FEAT_WINDOWS
8604 || new_TabPageIdxs == NULL
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 || outofmem)
8607 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008608 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008609 {
8610 /* guess the size */
8611 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8612
8613 /* Remember we did this to avoid getting outofmem messages over
8614 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008615 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 vim_free(new_ScreenLines);
8618 new_ScreenLines = NULL;
8619#ifdef FEAT_MBYTE
8620 vim_free(new_ScreenLinesUC);
8621 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008622 for (i = 0; i < p_mco; ++i)
8623 {
8624 vim_free(new_ScreenLinesC[i]);
8625 new_ScreenLinesC[i] = NULL;
8626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 vim_free(new_ScreenLines2);
8628 new_ScreenLines2 = NULL;
8629#endif
8630 vim_free(new_ScreenAttrs);
8631 new_ScreenAttrs = NULL;
8632 vim_free(new_LineOffset);
8633 new_LineOffset = NULL;
8634 vim_free(new_LineWraps);
8635 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008636#ifdef FEAT_WINDOWS
8637 vim_free(new_TabPageIdxs);
8638 new_TabPageIdxs = NULL;
8639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else
8642 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008643 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 for (new_row = 0; new_row < Rows; ++new_row)
8646 {
8647 new_LineOffset[new_row] = new_row * Columns;
8648 new_LineWraps[new_row] = FALSE;
8649
8650 /*
8651 * If the screen is not going to be cleared, copy as much as
8652 * possible from the old screen to the new one and clear the rest
8653 * (used when resizing the window at the "--more--" prompt or when
8654 * executing an external command, for the GUI).
8655 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008656 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
8658 (void)vim_memset(new_ScreenLines + new_row * Columns,
8659 ' ', (size_t)Columns * sizeof(schar_T));
8660#ifdef FEAT_MBYTE
8661 if (enc_utf8)
8662 {
8663 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8664 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008665 for (i = 0; i < p_mco; ++i)
8666 (void)vim_memset(new_ScreenLinesC[i]
8667 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 0, (size_t)Columns * sizeof(u8char_T));
8669 }
8670 if (enc_dbcs == DBCS_JPNU)
8671 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8672 0, (size_t)Columns * sizeof(schar_T));
8673#endif
8674 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8675 0, (size_t)Columns * sizeof(sattr_T));
8676 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008677 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {
8679 if (screen_Columns < Columns)
8680 len = screen_Columns;
8681 else
8682 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008683#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008684 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008685 * may be invalid now. Also when p_mco changes. */
8686 if (!(enc_utf8 && ScreenLinesUC == NULL)
8687 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008688#endif
8689 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8690 ScreenLines + LineOffset[old_row],
8691 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008693 if (enc_utf8 && ScreenLinesUC != NULL
8694 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8697 ScreenLinesUC + LineOffset[old_row],
8698 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008699 for (i = 0; i < p_mco; ++i)
8700 mch_memmove(new_ScreenLinesC[i]
8701 + new_LineOffset[new_row],
8702 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 (size_t)len * sizeof(u8char_T));
8704 }
8705 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8706 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8707 ScreenLines2 + LineOffset[old_row],
8708 (size_t)len * sizeof(schar_T));
8709#endif
8710 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8711 ScreenAttrs + LineOffset[old_row],
8712 (size_t)len * sizeof(sattr_T));
8713 }
8714 }
8715 }
8716 /* Use the last line of the screen for the current line. */
8717 current_ScreenLine = new_ScreenLines + Rows * Columns;
8718 }
8719
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008720 free_screenlines();
8721
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 ScreenLines = new_ScreenLines;
8723#ifdef FEAT_MBYTE
8724 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008725 for (i = 0; i < p_mco; ++i)
8726 ScreenLinesC[i] = new_ScreenLinesC[i];
8727 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 ScreenLines2 = new_ScreenLines2;
8729#endif
8730 ScreenAttrs = new_ScreenAttrs;
8731 LineOffset = new_LineOffset;
8732 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008733#ifdef FEAT_WINDOWS
8734 TabPageIdxs = new_TabPageIdxs;
8735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 /* It's important that screen_Rows and screen_Columns reflect the actual
8738 * size of ScreenLines[]. Set them before calling anything. */
8739#ifdef FEAT_GUI
8740 old_Rows = screen_Rows;
8741#endif
8742 screen_Rows = Rows;
8743 screen_Columns = Columns;
8744
8745 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008746 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 screenclear2();
8748
8749#ifdef FEAT_GUI
8750 else if (gui.in_use
8751 && !gui.starting
8752 && ScreenLines != NULL
8753 && old_Rows != Rows)
8754 {
8755 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8756 /*
8757 * Adjust the position of the cursor, for when executing an external
8758 * command.
8759 */
8760 if (msg_row >= Rows) /* Rows got smaller */
8761 msg_row = Rows - 1; /* put cursor at last row */
8762 else if (Rows > old_Rows) /* Rows got bigger */
8763 msg_row += Rows - old_Rows; /* put cursor in same place */
8764 if (msg_col >= Columns) /* Columns got smaller */
8765 msg_col = Columns - 1; /* put cursor at last column */
8766 }
8767#endif
8768
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008770 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008771
8772#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008773 /*
8774 * Do not apply autocommands more than 3 times to avoid an endless loop
8775 * in case applying autocommands always changes Rows or Columns.
8776 */
8777 if (starting == 0 && ++retry_count <= 3)
8778 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008779 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008780 /* In rare cases, autocommands may have altered Rows or Columns,
8781 * jump back to check if we need to allocate the screen again. */
8782 goto retry;
8783 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008788free_screenlines()
8789{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008790#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008791 int i;
8792
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008793 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008794 for (i = 0; i < Screen_mco; ++i)
8795 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008796 vim_free(ScreenLines2);
8797#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008798 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008799 vim_free(ScreenAttrs);
8800 vim_free(LineOffset);
8801 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008802#ifdef FEAT_WINDOWS
8803 vim_free(TabPageIdxs);
8804#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008805}
8806
8807 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808screenclear()
8809{
8810 check_for_delay(FALSE);
8811 screenalloc(FALSE); /* allocate screen buffers if size changed */
8812 screenclear2(); /* clear the screen */
8813}
8814
8815 static void
8816screenclear2()
8817{
8818 int i;
8819
8820 if (starting == NO_SCREEN || ScreenLines == NULL
8821#ifdef FEAT_GUI
8822 || (gui.in_use && gui.starting)
8823#endif
8824 )
8825 return;
8826
8827#ifdef FEAT_GUI
8828 if (!gui.in_use)
8829#endif
8830 screen_attr = -1; /* force setting the Normal colors */
8831 screen_stop_highlight(); /* don't want highlighting here */
8832
8833#ifdef FEAT_CLIPBOARD
8834 /* disable selection without redrawing it */
8835 clip_scroll_selection(9999);
8836#endif
8837
8838 /* blank out ScreenLines */
8839 for (i = 0; i < Rows; ++i)
8840 {
8841 lineclear(LineOffset[i], (int)Columns);
8842 LineWraps[i] = FALSE;
8843 }
8844
8845 if (can_clear(T_CL))
8846 {
8847 out_str(T_CL); /* clear the display */
8848 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008849 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851 else
8852 {
8853 /* can't clear the screen, mark all chars with invalid attributes */
8854 for (i = 0; i < Rows; ++i)
8855 lineinvalid(LineOffset[i], (int)Columns);
8856 clear_cmdline = TRUE;
8857 }
8858
8859 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8860
8861 win_rest_invalid(firstwin);
8862 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008863#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008864 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (must_redraw == CLEAR) /* no need to clear again */
8867 must_redraw = NOT_VALID;
8868 compute_cmdrow();
8869 msg_row = cmdline_row; /* put cursor on last line for messages */
8870 msg_col = 0;
8871 screen_start(); /* don't know where cursor is now */
8872 msg_scrolled = 0; /* can't scroll back */
8873 msg_didany = FALSE;
8874 msg_didout = FALSE;
8875}
8876
8877/*
8878 * Clear one line in ScreenLines.
8879 */
8880 static void
8881lineclear(off, width)
8882 unsigned off;
8883 int width;
8884{
8885 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8886#ifdef FEAT_MBYTE
8887 if (enc_utf8)
8888 (void)vim_memset(ScreenLinesUC + off, 0,
8889 (size_t)width * sizeof(u8char_T));
8890#endif
8891 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8892}
8893
8894/*
8895 * Mark one line in ScreenLines invalid by setting the attributes to an
8896 * invalid value.
8897 */
8898 static void
8899lineinvalid(off, width)
8900 unsigned off;
8901 int width;
8902{
8903 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8904}
8905
8906#ifdef FEAT_VERTSPLIT
8907/*
8908 * Copy part of a Screenline for vertically split window "wp".
8909 */
8910 static void
8911linecopy(to, from, wp)
8912 int to;
8913 int from;
8914 win_T *wp;
8915{
8916 unsigned off_to = LineOffset[to] + wp->w_wincol;
8917 unsigned off_from = LineOffset[from] + wp->w_wincol;
8918
8919 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8920 wp->w_width * sizeof(schar_T));
8921# ifdef FEAT_MBYTE
8922 if (enc_utf8)
8923 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 int i;
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8927 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008928 for (i = 0; i < p_mco; ++i)
8929 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8930 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932 if (enc_dbcs == DBCS_JPNU)
8933 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8934 wp->w_width * sizeof(schar_T));
8935# endif
8936 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8937 wp->w_width * sizeof(sattr_T));
8938}
8939#endif
8940
8941/*
8942 * Return TRUE if clearing with term string "p" would work.
8943 * It can't work when the string is empty or it won't set the right background.
8944 */
8945 int
8946can_clear(p)
8947 char_u *p;
8948{
8949 return (*p != NUL && (t_colors <= 1
8950#ifdef FEAT_GUI
8951 || gui.in_use
8952#endif
8953 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8954}
8955
8956/*
8957 * Reset cursor position. Use whenever cursor was moved because of outputting
8958 * something directly to the screen (shell commands) or a terminal control
8959 * code.
8960 */
8961 void
8962screen_start()
8963{
8964 screen_cur_row = screen_cur_col = 9999;
8965}
8966
8967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 * Move the cursor to position "row","col" in the screen.
8969 * This tries to find the most efficient way to move, minimizing the number of
8970 * characters sent to the terminal.
8971 */
8972 void
8973windgoto(row, col)
8974 int row;
8975 int col;
8976{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008977 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 int i;
8979 int plan;
8980 int cost;
8981 int wouldbe_col;
8982 int noinvcurs;
8983 char_u *bs;
8984 int goto_cost;
8985 int attr;
8986
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008987#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8989
8990#define PLAN_LE 1
8991#define PLAN_CR 2
8992#define PLAN_NL 3
8993#define PLAN_WRITE 4
8994 /* Can't use ScreenLines unless initialized */
8995 if (ScreenLines == NULL)
8996 return;
8997
8998 if (col != screen_cur_col || row != screen_cur_row)
8999 {
9000 /* Check for valid position. */
9001 if (row < 0) /* window without text lines? */
9002 row = 0;
9003 if (row >= screen_Rows)
9004 row = screen_Rows - 1;
9005 if (col >= screen_Columns)
9006 col = screen_Columns - 1;
9007
9008 /* check if no cursor movement is allowed in highlight mode */
9009 if (screen_attr && *T_MS == NUL)
9010 noinvcurs = HIGHL_COST;
9011 else
9012 noinvcurs = 0;
9013 goto_cost = GOTO_COST + noinvcurs;
9014
9015 /*
9016 * Plan how to do the positioning:
9017 * 1. Use CR to move it to column 0, same row.
9018 * 2. Use T_LE to move it a few columns to the left.
9019 * 3. Use NL to move a few lines down, column 0.
9020 * 4. Move a few columns to the right with T_ND or by writing chars.
9021 *
9022 * Don't do this if the cursor went beyond the last column, the cursor
9023 * position is unknown then (some terminals wrap, some don't )
9024 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009025 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 * characters to move the cursor to the right.
9027 */
9028 if (row >= screen_cur_row && screen_cur_col < Columns)
9029 {
9030 /*
9031 * If the cursor is in the same row, bigger col, we can use CR
9032 * or T_LE.
9033 */
9034 bs = NULL; /* init for GCC */
9035 attr = screen_attr;
9036 if (row == screen_cur_row && col < screen_cur_col)
9037 {
9038 /* "le" is preferred over "bc", because "bc" is obsolete */
9039 if (*T_LE)
9040 bs = T_LE; /* "cursor left" */
9041 else
9042 bs = T_BC; /* "backspace character (old) */
9043 if (*bs)
9044 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9045 else
9046 cost = 999;
9047 if (col + 1 < cost) /* using CR is less characters */
9048 {
9049 plan = PLAN_CR;
9050 wouldbe_col = 0;
9051 cost = 1; /* CR is just one character */
9052 }
9053 else
9054 {
9055 plan = PLAN_LE;
9056 wouldbe_col = col;
9057 }
9058 if (noinvcurs) /* will stop highlighting */
9059 {
9060 cost += noinvcurs;
9061 attr = 0;
9062 }
9063 }
9064
9065 /*
9066 * If the cursor is above where we want to be, we can use CR LF.
9067 */
9068 else if (row > screen_cur_row)
9069 {
9070 plan = PLAN_NL;
9071 wouldbe_col = 0;
9072 cost = (row - screen_cur_row) * 2; /* CR LF */
9073 if (noinvcurs) /* will stop highlighting */
9074 {
9075 cost += noinvcurs;
9076 attr = 0;
9077 }
9078 }
9079
9080 /*
9081 * If the cursor is in the same row, smaller col, just use write.
9082 */
9083 else
9084 {
9085 plan = PLAN_WRITE;
9086 wouldbe_col = screen_cur_col;
9087 cost = 0;
9088 }
9089
9090 /*
9091 * Check if any characters that need to be written have the
9092 * correct attributes. Also avoid UTF-8 characters.
9093 */
9094 i = col - wouldbe_col;
9095 if (i > 0)
9096 cost += i;
9097 if (cost < goto_cost && i > 0)
9098 {
9099 /*
9100 * Check if the attributes are correct without additionally
9101 * stopping highlighting.
9102 */
9103 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9104 while (i && *p++ == attr)
9105 --i;
9106 if (i != 0)
9107 {
9108 /*
9109 * Try if it works when highlighting is stopped here.
9110 */
9111 if (*--p == 0)
9112 {
9113 cost += noinvcurs;
9114 while (i && *p++ == 0)
9115 --i;
9116 }
9117 if (i != 0)
9118 cost = 999; /* different attributes, don't do it */
9119 }
9120#ifdef FEAT_MBYTE
9121 if (enc_utf8)
9122 {
9123 /* Don't use an UTF-8 char for positioning, it's slow. */
9124 for (i = wouldbe_col; i < col; ++i)
9125 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9126 {
9127 cost = 999;
9128 break;
9129 }
9130 }
9131#endif
9132 }
9133
9134 /*
9135 * We can do it without term_windgoto()!
9136 */
9137 if (cost < goto_cost)
9138 {
9139 if (plan == PLAN_LE)
9140 {
9141 if (noinvcurs)
9142 screen_stop_highlight();
9143 while (screen_cur_col > col)
9144 {
9145 out_str(bs);
9146 --screen_cur_col;
9147 }
9148 }
9149 else if (plan == PLAN_CR)
9150 {
9151 if (noinvcurs)
9152 screen_stop_highlight();
9153 out_char('\r');
9154 screen_cur_col = 0;
9155 }
9156 else if (plan == PLAN_NL)
9157 {
9158 if (noinvcurs)
9159 screen_stop_highlight();
9160 while (screen_cur_row < row)
9161 {
9162 out_char('\n');
9163 ++screen_cur_row;
9164 }
9165 screen_cur_col = 0;
9166 }
9167
9168 i = col - screen_cur_col;
9169 if (i > 0)
9170 {
9171 /*
9172 * Use cursor-right if it's one character only. Avoids
9173 * removing a line of pixels from the last bold char, when
9174 * using the bold trick in the GUI.
9175 */
9176 if (T_ND[0] != NUL && T_ND[1] == NUL)
9177 {
9178 while (i-- > 0)
9179 out_char(*T_ND);
9180 }
9181 else
9182 {
9183 int off;
9184
9185 off = LineOffset[row] + screen_cur_col;
9186 while (i-- > 0)
9187 {
9188 if (ScreenAttrs[off] != screen_attr)
9189 screen_stop_highlight();
9190#ifdef FEAT_MBYTE
9191 out_flush_check();
9192#endif
9193 out_char(ScreenLines[off]);
9194#ifdef FEAT_MBYTE
9195 if (enc_dbcs == DBCS_JPNU
9196 && ScreenLines[off] == 0x8e)
9197 out_char(ScreenLines2[off]);
9198#endif
9199 ++off;
9200 }
9201 }
9202 }
9203 }
9204 }
9205 else
9206 cost = 999;
9207
9208 if (cost >= goto_cost)
9209 {
9210 if (noinvcurs)
9211 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009212 if (row == screen_cur_row && (col > screen_cur_col)
9213 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 term_cursor_right(col - screen_cur_col);
9215 else
9216 term_windgoto(row, col);
9217 }
9218 screen_cur_row = row;
9219 screen_cur_col = col;
9220 }
9221}
9222
9223/*
9224 * Set cursor to its position in the current window.
9225 */
9226 void
9227setcursor()
9228{
9229 if (redrawing())
9230 {
9231 validate_cursor();
9232 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9233 W_WINCOL(curwin) + (
9234#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009235 /* With 'rightleft' set and the cursor on a double-wide
9236 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9238# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009239 (has_mbyte
9240 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9241 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242# endif
9243 1)) :
9244#endif
9245 curwin->w_wcol));
9246 }
9247}
9248
9249
9250/*
9251 * insert 'line_count' lines at 'row' in window 'wp'
9252 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9253 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9254 * scrolling.
9255 * Returns FAIL if the lines are not inserted, OK for success.
9256 */
9257 int
9258win_ins_lines(wp, row, line_count, invalid, mayclear)
9259 win_T *wp;
9260 int row;
9261 int line_count;
9262 int invalid;
9263 int mayclear;
9264{
9265 int did_delete;
9266 int nextrow;
9267 int lastrow;
9268 int retval;
9269
9270 if (invalid)
9271 wp->w_lines_valid = 0;
9272
9273 if (wp->w_height < 5)
9274 return FAIL;
9275
9276 if (line_count > wp->w_height - row)
9277 line_count = wp->w_height - row;
9278
9279 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9280 if (retval != MAYBE)
9281 return retval;
9282
9283 /*
9284 * If there is a next window or a status line, we first try to delete the
9285 * lines at the bottom to avoid messing what is after the window.
9286 * If this fails and there are following windows, don't do anything to avoid
9287 * messing up those windows, better just redraw.
9288 */
9289 did_delete = FALSE;
9290#ifdef FEAT_WINDOWS
9291 if (wp->w_next != NULL || wp->w_status_height)
9292 {
9293 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9294 line_count, (int)Rows, FALSE, NULL) == OK)
9295 did_delete = TRUE;
9296 else if (wp->w_next)
9297 return FAIL;
9298 }
9299#endif
9300 /*
9301 * if no lines deleted, blank the lines that will end up below the window
9302 */
9303 if (!did_delete)
9304 {
9305#ifdef FEAT_WINDOWS
9306 wp->w_redr_status = TRUE;
9307#endif
9308 redraw_cmdline = TRUE;
9309 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9310 lastrow = nextrow + line_count;
9311 if (lastrow > Rows)
9312 lastrow = Rows;
9313 screen_fill(nextrow - line_count, lastrow - line_count,
9314 W_WINCOL(wp), (int)W_ENDCOL(wp),
9315 ' ', ' ', 0);
9316 }
9317
9318 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9319 == FAIL)
9320 {
9321 /* deletion will have messed up other windows */
9322 if (did_delete)
9323 {
9324#ifdef FEAT_WINDOWS
9325 wp->w_redr_status = TRUE;
9326#endif
9327 win_rest_invalid(W_NEXT(wp));
9328 }
9329 return FAIL;
9330 }
9331
9332 return OK;
9333}
9334
9335/*
9336 * delete "line_count" window lines at "row" in window "wp"
9337 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9338 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9339 * scrolling
9340 * Return OK for success, FAIL if the lines are not deleted.
9341 */
9342 int
9343win_del_lines(wp, row, line_count, invalid, mayclear)
9344 win_T *wp;
9345 int row;
9346 int line_count;
9347 int invalid;
9348 int mayclear;
9349{
9350 int retval;
9351
9352 if (invalid)
9353 wp->w_lines_valid = 0;
9354
9355 if (line_count > wp->w_height - row)
9356 line_count = wp->w_height - row;
9357
9358 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9359 if (retval != MAYBE)
9360 return retval;
9361
9362 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9363 (int)Rows, FALSE, NULL) == FAIL)
9364 return FAIL;
9365
9366#ifdef FEAT_WINDOWS
9367 /*
9368 * If there are windows or status lines below, try to put them at the
9369 * correct place. If we can't do that, they have to be redrawn.
9370 */
9371 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9372 {
9373 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9374 line_count, (int)Rows, NULL) == FAIL)
9375 {
9376 wp->w_redr_status = TRUE;
9377 win_rest_invalid(wp->w_next);
9378 }
9379 }
9380 /*
9381 * If this is the last window and there is no status line, redraw the
9382 * command line later.
9383 */
9384 else
9385#endif
9386 redraw_cmdline = TRUE;
9387 return OK;
9388}
9389
9390/*
9391 * Common code for win_ins_lines() and win_del_lines().
9392 * Returns OK or FAIL when the work has been done.
9393 * Returns MAYBE when not finished yet.
9394 */
9395 static int
9396win_do_lines(wp, row, line_count, mayclear, del)
9397 win_T *wp;
9398 int row;
9399 int line_count;
9400 int mayclear;
9401 int del;
9402{
9403 int retval;
9404
9405 if (!redrawing() || line_count <= 0)
9406 return FAIL;
9407
9408 /* only a few lines left: redraw is faster */
9409 if (mayclear && Rows - line_count < 5
9410#ifdef FEAT_VERTSPLIT
9411 && wp->w_width == Columns
9412#endif
9413 )
9414 {
9415 screenclear(); /* will set wp->w_lines_valid to 0 */
9416 return FAIL;
9417 }
9418
9419 /*
9420 * Delete all remaining lines
9421 */
9422 if (row + line_count >= wp->w_height)
9423 {
9424 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9425 W_WINCOL(wp), (int)W_ENDCOL(wp),
9426 ' ', ' ', 0);
9427 return OK;
9428 }
9429
9430 /*
9431 * when scrolling, the message on the command line should be cleared,
9432 * otherwise it will stay there forever.
9433 */
9434 clear_cmdline = TRUE;
9435
9436 /*
9437 * If the terminal can set a scroll region, use that.
9438 * Always do this in a vertically split window. This will redraw from
9439 * ScreenLines[] when t_CV isn't defined. That's faster than using
9440 * win_line().
9441 * Don't use a scroll region when we are going to redraw the text, writing
9442 * a character in the lower right corner of the scroll region causes a
9443 * scroll-up in the DJGPP version.
9444 */
9445 if (scroll_region
9446#ifdef FEAT_VERTSPLIT
9447 || W_WIDTH(wp) != Columns
9448#endif
9449 )
9450 {
9451#ifdef FEAT_VERTSPLIT
9452 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9453#endif
9454 scroll_region_set(wp, row);
9455 if (del)
9456 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9457 wp->w_height - row, FALSE, wp);
9458 else
9459 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9460 wp->w_height - row, wp);
9461#ifdef FEAT_VERTSPLIT
9462 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9463#endif
9464 scroll_region_reset();
9465 return retval;
9466 }
9467
9468#ifdef FEAT_WINDOWS
9469 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9470 return FAIL;
9471#endif
9472
9473 return MAYBE;
9474}
9475
9476/*
9477 * window 'wp' and everything after it is messed up, mark it for redraw
9478 */
9479 static void
9480win_rest_invalid(wp)
9481 win_T *wp;
9482{
9483#ifdef FEAT_WINDOWS
9484 while (wp != NULL)
9485#else
9486 if (wp != NULL)
9487#endif
9488 {
9489 redraw_win_later(wp, NOT_VALID);
9490#ifdef FEAT_WINDOWS
9491 wp->w_redr_status = TRUE;
9492 wp = wp->w_next;
9493#endif
9494 }
9495 redraw_cmdline = TRUE;
9496}
9497
9498/*
9499 * The rest of the routines in this file perform screen manipulations. The
9500 * given operation is performed physically on the screen. The corresponding
9501 * change is also made to the internal screen image. In this way, the editor
9502 * anticipates the effect of editing changes on the appearance of the screen.
9503 * That way, when we call screenupdate a complete redraw isn't usually
9504 * necessary. Another advantage is that we can keep adding code to anticipate
9505 * screen changes, and in the meantime, everything still works.
9506 */
9507
9508/*
9509 * types for inserting or deleting lines
9510 */
9511#define USE_T_CAL 1
9512#define USE_T_CDL 2
9513#define USE_T_AL 3
9514#define USE_T_CE 4
9515#define USE_T_DL 5
9516#define USE_T_SR 6
9517#define USE_NL 7
9518#define USE_T_CD 8
9519#define USE_REDRAW 9
9520
9521/*
9522 * insert lines on the screen and update ScreenLines[]
9523 * 'end' is the line after the scrolled part. Normally it is Rows.
9524 * When scrolling region used 'off' is the offset from the top for the region.
9525 * 'row' and 'end' are relative to the start of the region.
9526 *
9527 * return FAIL for failure, OK for success.
9528 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009529 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530screen_ins_lines(off, row, line_count, end, wp)
9531 int off;
9532 int row;
9533 int line_count;
9534 int end;
9535 win_T *wp; /* NULL or window to use width from */
9536{
9537 int i;
9538 int j;
9539 unsigned temp;
9540 int cursor_row;
9541 int type;
9542 int result_empty;
9543 int can_ce = can_clear(T_CE);
9544
9545 /*
9546 * FAIL if
9547 * - there is no valid screen
9548 * - the screen has to be redrawn completely
9549 * - the line count is less than one
9550 * - the line count is more than 'ttyscroll'
9551 */
9552 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9553 return FAIL;
9554
9555 /*
9556 * There are seven ways to insert lines:
9557 * 0. When in a vertically split window and t_CV isn't set, redraw the
9558 * characters from ScreenLines[].
9559 * 1. Use T_CD (clear to end of display) if it exists and the result of
9560 * the insert is just empty lines
9561 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9562 * present or line_count > 1. It looks better if we do all the inserts
9563 * at once.
9564 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9565 * insert is just empty lines and T_CE is not present or line_count >
9566 * 1.
9567 * 4. Use T_AL (insert line) if it exists.
9568 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9569 * just empty lines.
9570 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9571 * just empty lines.
9572 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9573 * the 'da' flag is not set or we have clear line capability.
9574 * 8. redraw the characters from ScreenLines[].
9575 *
9576 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9577 * the scrollbar for the window. It does have insert line, use that if it
9578 * exists.
9579 */
9580 result_empty = (row + line_count >= end);
9581#ifdef FEAT_VERTSPLIT
9582 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9583 type = USE_REDRAW;
9584 else
9585#endif
9586 if (can_clear(T_CD) && result_empty)
9587 type = USE_T_CD;
9588 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9589 type = USE_T_CAL;
9590 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9591 type = USE_T_CDL;
9592 else if (*T_AL != NUL)
9593 type = USE_T_AL;
9594 else if (can_ce && result_empty)
9595 type = USE_T_CE;
9596 else if (*T_DL != NUL && result_empty)
9597 type = USE_T_DL;
9598 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9599 type = USE_T_SR;
9600 else
9601 return FAIL;
9602
9603 /*
9604 * For clearing the lines screen_del_lines() is used. This will also take
9605 * care of t_db if necessary.
9606 */
9607 if (type == USE_T_CD || type == USE_T_CDL ||
9608 type == USE_T_CE || type == USE_T_DL)
9609 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9610
9611 /*
9612 * If text is retained below the screen, first clear or delete as many
9613 * lines at the bottom of the window as are about to be inserted so that
9614 * the deleted lines won't later surface during a screen_del_lines.
9615 */
9616 if (*T_DB)
9617 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9618
9619#ifdef FEAT_CLIPBOARD
9620 /* Remove a modeless selection when inserting lines halfway the screen
9621 * or not the full width of the screen. */
9622 if (off + row > 0
9623# ifdef FEAT_VERTSPLIT
9624 || (wp != NULL && wp->w_width != Columns)
9625# endif
9626 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009627 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 else
9629 clip_scroll_selection(-line_count);
9630#endif
9631
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632#ifdef FEAT_GUI
9633 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9634 * scrolling is actually carried out. */
9635 gui_dont_update_cursor();
9636#endif
9637
9638 if (*T_CCS != NUL) /* cursor relative to region */
9639 cursor_row = row;
9640 else
9641 cursor_row = row + off;
9642
9643 /*
9644 * Shift LineOffset[] line_count down to reflect the inserted lines.
9645 * Clear the inserted lines in ScreenLines[].
9646 */
9647 row += off;
9648 end += off;
9649 for (i = 0; i < line_count; ++i)
9650 {
9651#ifdef FEAT_VERTSPLIT
9652 if (wp != NULL && wp->w_width != Columns)
9653 {
9654 /* need to copy part of a line */
9655 j = end - 1 - i;
9656 while ((j -= line_count) >= row)
9657 linecopy(j + line_count, j, wp);
9658 j += line_count;
9659 if (can_clear((char_u *)" "))
9660 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9661 else
9662 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9663 LineWraps[j] = FALSE;
9664 }
9665 else
9666#endif
9667 {
9668 j = end - 1 - i;
9669 temp = LineOffset[j];
9670 while ((j -= line_count) >= row)
9671 {
9672 LineOffset[j + line_count] = LineOffset[j];
9673 LineWraps[j + line_count] = LineWraps[j];
9674 }
9675 LineOffset[j + line_count] = temp;
9676 LineWraps[j + line_count] = FALSE;
9677 if (can_clear((char_u *)" "))
9678 lineclear(temp, (int)Columns);
9679 else
9680 lineinvalid(temp, (int)Columns);
9681 }
9682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683
9684 screen_stop_highlight();
9685 windgoto(cursor_row, 0);
9686
9687#ifdef FEAT_VERTSPLIT
9688 /* redraw the characters */
9689 if (type == USE_REDRAW)
9690 redraw_block(row, end, wp);
9691 else
9692#endif
9693 if (type == USE_T_CAL)
9694 {
9695 term_append_lines(line_count);
9696 screen_start(); /* don't know where cursor is now */
9697 }
9698 else
9699 {
9700 for (i = 0; i < line_count; i++)
9701 {
9702 if (type == USE_T_AL)
9703 {
9704 if (i && cursor_row != 0)
9705 windgoto(cursor_row, 0);
9706 out_str(T_AL);
9707 }
9708 else /* type == USE_T_SR */
9709 out_str(T_SR);
9710 screen_start(); /* don't know where cursor is now */
9711 }
9712 }
9713
9714 /*
9715 * With scroll-reverse and 'da' flag set we need to clear the lines that
9716 * have been scrolled down into the region.
9717 */
9718 if (type == USE_T_SR && *T_DA)
9719 {
9720 for (i = 0; i < line_count; ++i)
9721 {
9722 windgoto(off + i, 0);
9723 out_str(T_CE);
9724 screen_start(); /* don't know where cursor is now */
9725 }
9726 }
9727
9728#ifdef FEAT_GUI
9729 gui_can_update_cursor();
9730 if (gui.in_use)
9731 out_flush(); /* always flush after a scroll */
9732#endif
9733 return OK;
9734}
9735
9736/*
9737 * delete lines on the screen and update ScreenLines[]
9738 * 'end' is the line after the scrolled part. Normally it is Rows.
9739 * When scrolling region used 'off' is the offset from the top for the region.
9740 * 'row' and 'end' are relative to the start of the region.
9741 *
9742 * Return OK for success, FAIL if the lines are not deleted.
9743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 int
9745screen_del_lines(off, row, line_count, end, force, wp)
9746 int off;
9747 int row;
9748 int line_count;
9749 int end;
9750 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009751 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753 int j;
9754 int i;
9755 unsigned temp;
9756 int cursor_row;
9757 int cursor_end;
9758 int result_empty; /* result is empty until end of region */
9759 int can_delete; /* deleting line codes can be used */
9760 int type;
9761
9762 /*
9763 * FAIL if
9764 * - there is no valid screen
9765 * - the screen has to be redrawn completely
9766 * - the line count is less than one
9767 * - the line count is more than 'ttyscroll'
9768 */
9769 if (!screen_valid(TRUE) || line_count <= 0 ||
9770 (!force && line_count > p_ttyscroll))
9771 return FAIL;
9772
9773 /*
9774 * Check if the rest of the current region will become empty.
9775 */
9776 result_empty = row + line_count >= end;
9777
9778 /*
9779 * We can delete lines only when 'db' flag not set or when 'ce' option
9780 * available.
9781 */
9782 can_delete = (*T_DB == NUL || can_clear(T_CE));
9783
9784 /*
9785 * There are six ways to delete lines:
9786 * 0. When in a vertically split window and t_CV isn't set, redraw the
9787 * characters from ScreenLines[].
9788 * 1. Use T_CD if it exists and the result is empty.
9789 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9790 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9791 * none of the other ways work.
9792 * 4. Use T_CE (erase line) if the result is empty.
9793 * 5. Use T_DL (delete line) if it exists.
9794 * 6. redraw the characters from ScreenLines[].
9795 */
9796#ifdef FEAT_VERTSPLIT
9797 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9798 type = USE_REDRAW;
9799 else
9800#endif
9801 if (can_clear(T_CD) && result_empty)
9802 type = USE_T_CD;
9803#if defined(__BEOS__) && defined(BEOS_DR8)
9804 /*
9805 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9806 * its internal termcap... this works okay for tests which test *T_DB !=
9807 * NUL. It has the disadvantage that the user cannot use any :set t_*
9808 * command to get T_DB (back) to empty_option, only :set term=... will do
9809 * the trick...
9810 * Anyway, this hack will hopefully go away with the next OS release.
9811 * (Olaf Seibert)
9812 */
9813 else if (row == 0 && T_DB == empty_option
9814 && (line_count == 1 || *T_CDL == NUL))
9815#else
9816 else if (row == 0 && (
9817#ifndef AMIGA
9818 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9819 * up, so use delete-line command */
9820 line_count == 1 ||
9821#endif
9822 *T_CDL == NUL))
9823#endif
9824 type = USE_NL;
9825 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9826 type = USE_T_CDL;
9827 else if (can_clear(T_CE) && result_empty
9828#ifdef FEAT_VERTSPLIT
9829 && (wp == NULL || wp->w_width == Columns)
9830#endif
9831 )
9832 type = USE_T_CE;
9833 else if (*T_DL != NUL && can_delete)
9834 type = USE_T_DL;
9835 else if (*T_CDL != NUL && can_delete)
9836 type = USE_T_CDL;
9837 else
9838 return FAIL;
9839
9840#ifdef FEAT_CLIPBOARD
9841 /* Remove a modeless selection when deleting lines halfway the screen or
9842 * not the full width of the screen. */
9843 if (off + row > 0
9844# ifdef FEAT_VERTSPLIT
9845 || (wp != NULL && wp->w_width != Columns)
9846# endif
9847 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009848 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 else
9850 clip_scroll_selection(line_count);
9851#endif
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853#ifdef FEAT_GUI
9854 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9855 * scrolling is actually carried out. */
9856 gui_dont_update_cursor();
9857#endif
9858
9859 if (*T_CCS != NUL) /* cursor relative to region */
9860 {
9861 cursor_row = row;
9862 cursor_end = end;
9863 }
9864 else
9865 {
9866 cursor_row = row + off;
9867 cursor_end = end + off;
9868 }
9869
9870 /*
9871 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9872 * Clear the inserted lines in ScreenLines[].
9873 */
9874 row += off;
9875 end += off;
9876 for (i = 0; i < line_count; ++i)
9877 {
9878#ifdef FEAT_VERTSPLIT
9879 if (wp != NULL && wp->w_width != Columns)
9880 {
9881 /* need to copy part of a line */
9882 j = row + i;
9883 while ((j += line_count) <= end - 1)
9884 linecopy(j - line_count, j, wp);
9885 j -= line_count;
9886 if (can_clear((char_u *)" "))
9887 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9888 else
9889 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9890 LineWraps[j] = FALSE;
9891 }
9892 else
9893#endif
9894 {
9895 /* whole width, moving the line pointers is faster */
9896 j = row + i;
9897 temp = LineOffset[j];
9898 while ((j += line_count) <= end - 1)
9899 {
9900 LineOffset[j - line_count] = LineOffset[j];
9901 LineWraps[j - line_count] = LineWraps[j];
9902 }
9903 LineOffset[j - line_count] = temp;
9904 LineWraps[j - line_count] = FALSE;
9905 if (can_clear((char_u *)" "))
9906 lineclear(temp, (int)Columns);
9907 else
9908 lineinvalid(temp, (int)Columns);
9909 }
9910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
9912 screen_stop_highlight();
9913
9914#ifdef FEAT_VERTSPLIT
9915 /* redraw the characters */
9916 if (type == USE_REDRAW)
9917 redraw_block(row, end, wp);
9918 else
9919#endif
9920 if (type == USE_T_CD) /* delete the lines */
9921 {
9922 windgoto(cursor_row, 0);
9923 out_str(T_CD);
9924 screen_start(); /* don't know where cursor is now */
9925 }
9926 else if (type == USE_T_CDL)
9927 {
9928 windgoto(cursor_row, 0);
9929 term_delete_lines(line_count);
9930 screen_start(); /* don't know where cursor is now */
9931 }
9932 /*
9933 * Deleting lines at top of the screen or scroll region: Just scroll
9934 * the whole screen (scroll region) up by outputting newlines on the
9935 * last line.
9936 */
9937 else if (type == USE_NL)
9938 {
9939 windgoto(cursor_end - 1, 0);
9940 for (i = line_count; --i >= 0; )
9941 out_char('\n'); /* cursor will remain on same line */
9942 }
9943 else
9944 {
9945 for (i = line_count; --i >= 0; )
9946 {
9947 if (type == USE_T_DL)
9948 {
9949 windgoto(cursor_row, 0);
9950 out_str(T_DL); /* delete a line */
9951 }
9952 else /* type == USE_T_CE */
9953 {
9954 windgoto(cursor_row + i, 0);
9955 out_str(T_CE); /* erase a line */
9956 }
9957 screen_start(); /* don't know where cursor is now */
9958 }
9959 }
9960
9961 /*
9962 * If the 'db' flag is set, we need to clear the lines that have been
9963 * scrolled up at the bottom of the region.
9964 */
9965 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9966 {
9967 for (i = line_count; i > 0; --i)
9968 {
9969 windgoto(cursor_end - i, 0);
9970 out_str(T_CE); /* erase a line */
9971 screen_start(); /* don't know where cursor is now */
9972 }
9973 }
9974
9975#ifdef FEAT_GUI
9976 gui_can_update_cursor();
9977 if (gui.in_use)
9978 out_flush(); /* always flush after a scroll */
9979#endif
9980
9981 return OK;
9982}
9983
9984/*
9985 * show the current mode and ruler
9986 *
9987 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9988 * If clear_cmdline is FALSE there may be a message there that needs to be
9989 * cleared only if a mode is shown.
9990 * Return the length of the message (0 if no message).
9991 */
9992 int
9993showmode()
9994{
9995 int need_clear;
9996 int length = 0;
9997 int do_mode;
9998 int attr;
9999 int nwr_save;
10000#ifdef FEAT_INS_EXPAND
10001 int sub_attr;
10002#endif
10003
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010004 do_mode = ((p_smd && msg_silent == 0)
10005 && ((State & INSERT)
10006 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010007 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (do_mode || Recording)
10009 {
10010 /*
10011 * Don't show mode right now, when not redrawing or inside a mapping.
10012 * Call char_avail() only when we are going to show something, because
10013 * it takes a bit of time.
10014 */
10015 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10016 {
10017 redraw_cmdline = TRUE; /* show mode later */
10018 return 0;
10019 }
10020
10021 nwr_save = need_wait_return;
10022
10023 /* wait a bit before overwriting an important message */
10024 check_for_delay(FALSE);
10025
10026 /* if the cmdline is more than one line high, erase top lines */
10027 need_clear = clear_cmdline;
10028 if (clear_cmdline && cmdline_row < Rows - 1)
10029 msg_clr_cmdline(); /* will reset clear_cmdline */
10030
10031 /* Position on the last line in the window, column 0 */
10032 msg_pos_mode();
10033 cursor_off();
10034 attr = hl_attr(HLF_CM); /* Highlight mode */
10035 if (do_mode)
10036 {
10037 MSG_PUTS_ATTR("--", attr);
10038#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010039 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010040# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010041 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010042# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010043 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010044# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010045 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010046# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 MSG_PUTS_ATTR(" IM", attr);
10048# else
10049 MSG_PUTS_ATTR(" XIM", attr);
10050# endif
10051#endif
10052#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10053 if (gui.in_use)
10054 {
10055 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010056 {
10057 /* HANGUL */
10058 if (enc_utf8)
10059 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10060 else
10061 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 }
10064#endif
10065#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010066 /* CTRL-X in Insert mode */
10067 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
10069 /* These messages can get long, avoid a wrap in a narrow
10070 * window. Prefer showing edit_submode_extra. */
10071 length = (Rows - msg_row) * Columns - 3;
10072 if (edit_submode_extra != NULL)
10073 length -= vim_strsize(edit_submode_extra);
10074 if (length > 0)
10075 {
10076 if (edit_submode_pre != NULL)
10077 length -= vim_strsize(edit_submode_pre);
10078 if (length - vim_strsize(edit_submode) > 0)
10079 {
10080 if (edit_submode_pre != NULL)
10081 msg_puts_attr(edit_submode_pre, attr);
10082 msg_puts_attr(edit_submode, attr);
10083 }
10084 if (edit_submode_extra != NULL)
10085 {
10086 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10087 if ((int)edit_submode_highl < (int)HLF_COUNT)
10088 sub_attr = hl_attr(edit_submode_highl);
10089 else
10090 sub_attr = attr;
10091 msg_puts_attr(edit_submode_extra, sub_attr);
10092 }
10093 }
10094 length = 0;
10095 }
10096 else
10097#endif
10098 {
10099#ifdef FEAT_VREPLACE
10100 if (State & VREPLACE_FLAG)
10101 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10102 else
10103#endif
10104 if (State & REPLACE_FLAG)
10105 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10106 else if (State & INSERT)
10107 {
10108#ifdef FEAT_RIGHTLEFT
10109 if (p_ri)
10110 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10111#endif
10112 MSG_PUTS_ATTR(_(" INSERT"), attr);
10113 }
10114 else if (restart_edit == 'I')
10115 MSG_PUTS_ATTR(_(" (insert)"), attr);
10116 else if (restart_edit == 'R')
10117 MSG_PUTS_ATTR(_(" (replace)"), attr);
10118 else if (restart_edit == 'V')
10119 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10120#ifdef FEAT_RIGHTLEFT
10121 if (p_hkmap)
10122 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10123# ifdef FEAT_FKMAP
10124 if (p_fkmap)
10125 MSG_PUTS_ATTR(farsi_text_5, attr);
10126# endif
10127#endif
10128#ifdef FEAT_KEYMAP
10129 if (State & LANGMAP)
10130 {
10131# ifdef FEAT_ARABIC
10132 if (curwin->w_p_arab)
10133 MSG_PUTS_ATTR(_(" Arabic"), attr);
10134 else
10135# endif
10136 MSG_PUTS_ATTR(_(" (lang)"), attr);
10137 }
10138#endif
10139 if ((State & INSERT) && p_paste)
10140 MSG_PUTS_ATTR(_(" (paste)"), attr);
10141
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (VIsual_active)
10143 {
10144 char *p;
10145
10146 /* Don't concatenate separate words to avoid translation
10147 * problems. */
10148 switch ((VIsual_select ? 4 : 0)
10149 + (VIsual_mode == Ctrl_V) * 2
10150 + (VIsual_mode == 'V'))
10151 {
10152 case 0: p = N_(" VISUAL"); break;
10153 case 1: p = N_(" VISUAL LINE"); break;
10154 case 2: p = N_(" VISUAL BLOCK"); break;
10155 case 4: p = N_(" SELECT"); break;
10156 case 5: p = N_(" SELECT LINE"); break;
10157 default: p = N_(" SELECT BLOCK"); break;
10158 }
10159 MSG_PUTS_ATTR(_(p), attr);
10160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 MSG_PUTS_ATTR(" --", attr);
10162 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010163
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 need_clear = TRUE;
10165 }
10166 if (Recording
10167#ifdef FEAT_INS_EXPAND
10168 && edit_submode == NULL /* otherwise it gets too long */
10169#endif
10170 )
10171 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010172 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 need_clear = TRUE;
10174 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010175
10176 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 if (need_clear || clear_cmdline)
10178 msg_clr_eos();
10179 msg_didout = FALSE; /* overwrite this message */
10180 length = msg_col;
10181 msg_col = 0;
10182 need_wait_return = nwr_save; /* never ask for hit-return for this */
10183 }
10184 else if (clear_cmdline && msg_silent == 0)
10185 /* Clear the whole command line. Will reset "clear_cmdline". */
10186 msg_clr_cmdline();
10187
10188#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 /* In Visual mode the size of the selected area must be redrawn. */
10190 if (VIsual_active)
10191 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
10193 /* If the last window has no status line, the ruler is after the mode
10194 * message and must be redrawn */
10195 if (redrawing()
10196# ifdef FEAT_WINDOWS
10197 && lastwin->w_status_height == 0
10198# endif
10199 )
10200 win_redr_ruler(lastwin, TRUE);
10201#endif
10202 redraw_cmdline = FALSE;
10203 clear_cmdline = FALSE;
10204
10205 return length;
10206}
10207
10208/*
10209 * Position for a mode message.
10210 */
10211 static void
10212msg_pos_mode()
10213{
10214 msg_col = 0;
10215 msg_row = Rows - 1;
10216}
10217
10218/*
10219 * Delete mode message. Used when ESC is typed which is expected to end
10220 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010221 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 */
10223 void
10224unshowmode(force)
10225 int force;
10226{
10227 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010228 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 */
10230 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10231 redraw_cmdline = TRUE; /* delete mode later */
10232 else
10233 {
10234 msg_pos_mode();
10235 if (Recording)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010236 recording_mode(hl_attr(HLF_CM));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 msg_clr_eos();
10238 }
10239}
10240
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010241 static void
10242recording_mode(attr)
10243 int attr;
10244{
10245 MSG_PUTS_ATTR(_("recording"), attr);
10246 if (!shortmess(SHM_RECORDING))
10247 {
10248 char_u s[4];
10249 sprintf((char *)s, " @%c", Recording);
10250 MSG_PUTS_ATTR(s, attr);
10251 }
10252}
10253
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010254#if defined(FEAT_WINDOWS)
10255/*
10256 * Draw the tab pages line at the top of the Vim window.
10257 */
10258 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010259draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010260{
10261 int tabcount = 0;
10262 tabpage_T *tp;
10263 int tabwidth;
10264 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010265 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010266 int attr;
10267 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010268 win_T *cwp;
10269 int wincount;
10270 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010271 int c;
10272 int len;
10273 int attr_sel = hl_attr(HLF_TPS);
10274 int attr_nosel = hl_attr(HLF_TP);
10275 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010276 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010277 int room;
10278 int use_sep_chars = (t_colors < 8
10279#ifdef FEAT_GUI
10280 && !gui.in_use
10281#endif
10282 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010283
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010284 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010285
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010286#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010287 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010288 if (gui_use_tabline())
10289 {
10290 gui_update_tabline();
10291 return;
10292 }
10293#endif
10294
10295 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010296 return;
10297
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010298#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010299
10300 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10301 for (scol = 0; scol < Columns; ++scol)
10302 TabPageIdxs[scol] = 0;
10303
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010304 /* Use the 'tabline' option if it's set. */
10305 if (*p_tal != NUL)
10306 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010307 int save_called_emsg = called_emsg;
10308
10309 /* Check for an error. If there is one we would loop in redrawing the
10310 * screen. Avoid that by making 'tabline' empty. */
10311 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010312 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010313 if (called_emsg)
10314 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010315 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010316 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010317 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010318 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010319#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010320 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010321 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10322 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010323
Bram Moolenaar238a5642006-02-21 22:12:05 +000010324 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10325 if (tabwidth < 6)
10326 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010327
Bram Moolenaar238a5642006-02-21 22:12:05 +000010328 attr = attr_nosel;
10329 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010330 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010331 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10332 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010333 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010334 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010335
Bram Moolenaar238a5642006-02-21 22:12:05 +000010336 if (tp->tp_topframe == topframe)
10337 attr = attr_sel;
10338 if (use_sep_chars && col > 0)
10339 screen_putchar('|', 0, col++, attr);
10340
10341 if (tp->tp_topframe != topframe)
10342 attr = attr_nosel;
10343
10344 screen_putchar(' ', 0, col++, attr);
10345
10346 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010347 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010348 cwp = curwin;
10349 wp = firstwin;
10350 }
10351 else
10352 {
10353 cwp = tp->tp_curwin;
10354 wp = tp->tp_firstwin;
10355 }
10356
10357 modified = FALSE;
10358 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10359 if (bufIsChanged(wp->w_buffer))
10360 modified = TRUE;
10361 if (modified || wincount > 1)
10362 {
10363 if (wincount > 1)
10364 {
10365 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010366 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010367 if (col + len >= Columns - 3)
10368 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010369 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010370#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010371 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010372#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010373 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010374#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010375 );
10376 col += len;
10377 }
10378 if (modified)
10379 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10380 screen_putchar(' ', 0, col++, attr);
10381 }
10382
10383 room = scol - col + tabwidth - 1;
10384 if (room > 0)
10385 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010386 /* Get buffer name in NameBuff[] */
10387 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010388 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010389 len = vim_strsize(NameBuff);
10390 p = NameBuff;
10391#ifdef FEAT_MBYTE
10392 if (has_mbyte)
10393 while (len > room)
10394 {
10395 len -= ptr2cells(p);
10396 mb_ptr_adv(p);
10397 }
10398 else
10399#endif
10400 if (len > room)
10401 {
10402 p += len - room;
10403 len = room;
10404 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010405 if (len > Columns - col - 1)
10406 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010408 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010409 col += len;
10410 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010411 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010412
10413 /* Store the tab page number in TabPageIdxs[], so that
10414 * jump_to_mouse() knows where each one is. */
10415 ++tabcount;
10416 while (scol < col)
10417 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010418 }
10419
Bram Moolenaar238a5642006-02-21 22:12:05 +000010420 if (use_sep_chars)
10421 c = '_';
10422 else
10423 c = ' ';
10424 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010425
10426 /* Put an "X" for closing the current tab if there are several. */
10427 if (first_tabpage->tp_next != NULL)
10428 {
10429 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10430 TabPageIdxs[Columns - 1] = -999;
10431 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010432 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010433
10434 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10435 * set. */
10436 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010437}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010438
10439/*
10440 * Get buffer name for "buf" into NameBuff[].
10441 * Takes care of special buffer names and translates special characters.
10442 */
10443 void
10444get_trans_bufname(buf)
10445 buf_T *buf;
10446{
10447 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010448 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010449 else
10450 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10451 trans_characters(NameBuff, MAXPATHL);
10452}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010453#endif
10454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10456/*
10457 * Get the character to use in a status line. Get its attributes in "*attr".
10458 */
10459 static int
10460fillchar_status(attr, is_curwin)
10461 int *attr;
10462 int is_curwin;
10463{
10464 int fill;
10465 if (is_curwin)
10466 {
10467 *attr = hl_attr(HLF_S);
10468 fill = fill_stl;
10469 }
10470 else
10471 {
10472 *attr = hl_attr(HLF_SNC);
10473 fill = fill_stlnc;
10474 }
10475 /* Use fill when there is highlighting, and highlighting of current
10476 * window differs, or the fillchars differ, or this is not the
10477 * current window */
10478 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10479 || !is_curwin || firstwin == lastwin)
10480 || (fill_stl != fill_stlnc)))
10481 return fill;
10482 if (is_curwin)
10483 return '^';
10484 return '=';
10485}
10486#endif
10487
10488#ifdef FEAT_VERTSPLIT
10489/*
10490 * Get the character to use in a separator between vertically split windows.
10491 * Get its attributes in "*attr".
10492 */
10493 static int
10494fillchar_vsep(attr)
10495 int *attr;
10496{
10497 *attr = hl_attr(HLF_C);
10498 if (*attr == 0 && fill_vert == ' ')
10499 return '|';
10500 else
10501 return fill_vert;
10502}
10503#endif
10504
10505/*
10506 * Return TRUE if redrawing should currently be done.
10507 */
10508 int
10509redrawing()
10510{
10511 return (!RedrawingDisabled
10512 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10513}
10514
10515/*
10516 * Return TRUE if printing messages should currently be done.
10517 */
10518 int
10519messaging()
10520{
10521 return (!(p_lz && char_avail() && !KeyTyped));
10522}
10523
10524/*
10525 * Show current status info in ruler and various other places
10526 * If always is FALSE, only show ruler if position has changed.
10527 */
10528 void
10529showruler(always)
10530 int always;
10531{
10532 if (!always && !redrawing())
10533 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010534#ifdef FEAT_INS_EXPAND
10535 if (pum_visible())
10536 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010537# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010538 /* Don't redraw right now, do it later. */
10539 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010540# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010541 return;
10542 }
10543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010545 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010546 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010547 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 else
10550#endif
10551#ifdef FEAT_CMDL_INFO
10552 win_redr_ruler(curwin, always);
10553#endif
10554
10555#ifdef FEAT_TITLE
10556 if (need_maketitle
10557# ifdef FEAT_STL_OPT
10558 || (p_icon && (stl_syntax & STL_IN_ICON))
10559 || (p_title && (stl_syntax & STL_IN_TITLE))
10560# endif
10561 )
10562 maketitle();
10563#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010564#ifdef FEAT_WINDOWS
10565 /* Redraw the tab pages line if needed. */
10566 if (redraw_tabline)
10567 draw_tabline();
10568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569}
10570
10571#ifdef FEAT_CMDL_INFO
10572 static void
10573win_redr_ruler(wp, always)
10574 win_T *wp;
10575 int always;
10576{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010577#define RULER_BUF_LEN 70
10578 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 int row;
10580 int fillchar;
10581 int attr;
10582 int empty_line = FALSE;
10583 colnr_T virtcol;
10584 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010585 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 int o;
10587#ifdef FEAT_VERTSPLIT
10588 int this_ru_col;
10589 int off = 0;
10590 int width = Columns;
10591# define WITH_OFF(x) x
10592# define WITH_WIDTH(x) x
10593#else
10594# define WITH_OFF(x) 0
10595# define WITH_WIDTH(x) Columns
10596# define this_ru_col ru_col
10597#endif
10598
10599 /* If 'ruler' off or redrawing disabled, don't do anything */
10600 if (!p_ru)
10601 return;
10602
10603 /*
10604 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10605 * after deleting lines, before cursor.lnum is corrected.
10606 */
10607 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10608 return;
10609
10610#ifdef FEAT_INS_EXPAND
10611 /* Don't draw the ruler while doing insert-completion, it might overwrite
10612 * the (long) mode message. */
10613# ifdef FEAT_WINDOWS
10614 if (wp == lastwin && lastwin->w_status_height == 0)
10615# endif
10616 if (edit_submode != NULL)
10617 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010618 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10619 if (pum_visible())
10620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#endif
10622
10623#ifdef FEAT_STL_OPT
10624 if (*p_ruf)
10625 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 int save_called_emsg = called_emsg;
10627
10628 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 if (called_emsg)
10631 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010632 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 return;
10635 }
10636#endif
10637
10638 /*
10639 * Check if not in Insert mode and the line is empty (will show "0-1").
10640 */
10641 if (!(State & INSERT)
10642 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10643 empty_line = TRUE;
10644
10645 /*
10646 * Only draw the ruler when something changed.
10647 */
10648 validate_virtcol_win(wp);
10649 if ( redraw_cmdline
10650 || always
10651 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10652 || wp->w_cursor.col != wp->w_ru_cursor.col
10653 || wp->w_virtcol != wp->w_ru_virtcol
10654#ifdef FEAT_VIRTUALEDIT
10655 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10656#endif
10657 || wp->w_topline != wp->w_ru_topline
10658 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10659#ifdef FEAT_DIFF
10660 || wp->w_topfill != wp->w_ru_topfill
10661#endif
10662 || empty_line != wp->w_ru_empty)
10663 {
10664 cursor_off();
10665#ifdef FEAT_WINDOWS
10666 if (wp->w_status_height)
10667 {
10668 row = W_WINROW(wp) + wp->w_height;
10669 fillchar = fillchar_status(&attr, wp == curwin);
10670# ifdef FEAT_VERTSPLIT
10671 off = W_WINCOL(wp);
10672 width = W_WIDTH(wp);
10673# endif
10674 }
10675 else
10676#endif
10677 {
10678 row = Rows - 1;
10679 fillchar = ' ';
10680 attr = 0;
10681#ifdef FEAT_VERTSPLIT
10682 width = Columns;
10683 off = 0;
10684#endif
10685 }
10686
10687 /* In list mode virtcol needs to be recomputed */
10688 virtcol = wp->w_virtcol;
10689 if (wp->w_p_list && lcs_tab1 == NUL)
10690 {
10691 wp->w_p_list = FALSE;
10692 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10693 wp->w_p_list = TRUE;
10694 }
10695
10696 /*
10697 * Some sprintfs return the length, some return a pointer.
10698 * To avoid portability problems we use strlen() here.
10699 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010700 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10702 ? 0L
10703 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010704 len = STRLEN(buffer);
10705 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10707 (int)virtcol + 1);
10708
10709 /*
10710 * Add a "50%" if there is room for it.
10711 * On the last line, don't print in the last column (scrolls the
10712 * screen up on some terminals).
10713 */
10714 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010715 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 o = i + vim_strsize(buffer + i + 1);
10717#ifdef FEAT_WINDOWS
10718 if (wp->w_status_height == 0) /* can't use last char of screen */
10719#endif
10720 ++o;
10721#ifdef FEAT_VERTSPLIT
10722 this_ru_col = ru_col - (Columns - width);
10723 if (this_ru_col < 0)
10724 this_ru_col = 0;
10725#endif
10726 /* Never use more than half the window/screen width, leave the other
10727 * half for the filename. */
10728 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10729 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10730 if (this_ru_col + o < WITH_WIDTH(width))
10731 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010732 /* need at least 3 chars left for get_rel_pos() + NUL */
10733 while (this_ru_col + o < WITH_WIDTH(width) && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 {
10735#ifdef FEAT_MBYTE
10736 if (has_mbyte)
10737 i += (*mb_char2bytes)(fillchar, buffer + i);
10738 else
10739#endif
10740 buffer[i++] = fillchar;
10741 ++o;
10742 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010743 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
10745 /* Truncate at window boundary. */
10746#ifdef FEAT_MBYTE
10747 if (has_mbyte)
10748 {
10749 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010750 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 {
10752 o += (*mb_ptr2cells)(buffer + i);
10753 if (this_ru_col + o > WITH_WIDTH(width))
10754 {
10755 buffer[i] = NUL;
10756 break;
10757 }
10758 }
10759 }
10760 else
10761#endif
10762 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10763 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10764
10765 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10766 i = redraw_cmdline;
10767 screen_fill(row, row + 1,
10768 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10769 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10770 fillchar, fillchar, attr);
10771 /* don't redraw the cmdline because of showing the ruler */
10772 redraw_cmdline = i;
10773 wp->w_ru_cursor = wp->w_cursor;
10774 wp->w_ru_virtcol = wp->w_virtcol;
10775 wp->w_ru_empty = empty_line;
10776 wp->w_ru_topline = wp->w_topline;
10777 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10778#ifdef FEAT_DIFF
10779 wp->w_ru_topfill = wp->w_topfill;
10780#endif
10781 }
10782}
10783#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010784
10785#if defined(FEAT_LINEBREAK) || defined(PROTO)
10786/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010787 * Return the width of the 'number' and 'relativenumber' column.
10788 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010789 * Otherwise it depends on 'numberwidth' and the line count.
10790 */
10791 int
10792number_width(wp)
10793 win_T *wp;
10794{
10795 int n;
10796 linenr_T lnum;
10797
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010798 if (wp->w_p_rnu && !wp->w_p_nu)
10799 /* cursor line shows "0" */
10800 lnum = wp->w_height;
10801 else
10802 /* cursor line shows absolute line number */
10803 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010804
Bram Moolenaar6b314672015-03-20 15:42:10 +010010805 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010806 return wp->w_nrwidth_width;
10807 wp->w_nrwidth_line_count = lnum;
10808
10809 n = 0;
10810 do
10811 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010812 lnum /= 10;
10813 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010814 } while (lnum > 0);
10815
10816 /* 'numberwidth' gives the minimal width plus one */
10817 if (n < wp->w_p_nuw - 1)
10818 n = wp->w_p_nuw - 1;
10819
10820 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010821 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010822 return n;
10823}
10824#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010825
10826/*
10827 * Return the current cursor column. This is the actual position on the
10828 * screen. First column is 0.
10829 */
10830 int
10831screen_screencol()
10832{
10833 return screen_cur_col;
10834}
10835
10836/*
10837 * Return the current cursor row. This is the actual position on the screen.
10838 * First row is 0.
10839 */
10840 int
10841screen_screenrow()
10842{
10843 return screen_cur_row;
10844}