blob: 7a9311a453f7ded3ad13a41cc826ed64f2940074 [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' */
112#endif
113
114/*
115 * Buffer for one screen line (characters and attributes).
116 */
117static schar_T *current_ScreenLine;
118
119static void win_update __ARGS((win_T *wp));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000120static 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 +0000121#ifdef FEAT_FOLDING
122static void fold_line __ARGS((win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row));
123static void fill_foldcolumn __ARGS((char_u *p, win_T *wp, int closed, linenr_T lnum));
124static void copy_text_attr __ARGS((int off, char_u *buf, int len, int attr));
125#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000126static int win_line __ARGS((win_T *, linenr_T, int, int, int nochange));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static int char_needs_redraw __ARGS((int off_from, int off_to, int cols));
128#ifdef FEAT_RIGHTLEFT
129static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width, int rlflag));
130# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c), (rl))
131#else
132static void screen_line __ARGS((int row, int coloff, int endcol, int clear_width));
133# define SCREEN_LINE(r, o, e, c, rl) screen_line((r), (o), (e), (c))
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_VERTSPLIT
136static void draw_vsep_win __ARGS((win_T *wp, int row));
137#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaar362f3562009-11-03 16:20:34 +0000139static void redraw_custom_statusline __ARGS((win_T *wp));
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void start_search_hl __ARGS((void));
144static void end_search_hl __ARGS((void));
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200145static void init_search_hl __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void prepare_search_hl __ARGS((win_T *wp, linenr_T lnum));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur));
148static 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 +0000149#endif
150static void screen_start_highlight __ARGS((int attr));
151static void screen_char __ARGS((unsigned off, int row, int col));
152#ifdef FEAT_MBYTE
153static void screen_char_2 __ARGS((unsigned off, int row, int col));
154#endif
155static void screenclear2 __ARGS((void));
156static void lineclear __ARGS((unsigned off, int width));
157static void lineinvalid __ARGS((unsigned off, int width));
158#ifdef FEAT_VERTSPLIT
159static void linecopy __ARGS((int to, int from, win_T *wp));
160static void redraw_block __ARGS((int row, int end, win_T *wp));
161#endif
162static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
163static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000166static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
169static int fillchar_status __ARGS((int *attr, int is_curwin));
170#endif
171#ifdef FEAT_VERTSPLIT
172static int fillchar_vsep __ARGS((int *attr));
173#endif
174#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000175static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177#ifdef FEAT_CMDL_INFO
178static void win_redr_ruler __ARGS((win_T *wp, int always));
179#endif
180
181#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
182/* Ugly global: overrule attribute used by screen_char() */
183static int screen_char_attr = 0;
184#endif
185
186/*
187 * Redraw the current window later, with update_screen(type).
188 * Set must_redraw only if not already set to a higher value.
189 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
190 */
191 void
192redraw_later(type)
193 int type;
194{
195 redraw_win_later(curwin, type);
196}
197
198 void
199redraw_win_later(wp, type)
200 win_T *wp;
201 int type;
202{
203 if (wp->w_redr_type < type)
204 {
205 wp->w_redr_type = type;
206 if (type >= NOT_VALID)
207 wp->w_lines_valid = 0;
208 if (must_redraw < type) /* must_redraw is the maximum of all windows */
209 must_redraw = type;
210 }
211}
212
213/*
214 * Force a complete redraw later. Also resets the highlighting. To be used
215 * after executing a shell command that messes up the screen.
216 */
217 void
218redraw_later_clear()
219{
220 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000221#ifdef FEAT_GUI
222 if (gui.in_use)
223 /* Use a code that will reset gui.highlight_mask in
224 * gui_stop_highlight(). */
225 screen_attr = HL_ALL + 1;
226 else
227#endif
228 /* Use attributes that is very unlikely to appear in text. */
229 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230}
231
232/*
233 * Mark all windows to be redrawn later.
234 */
235 void
236redraw_all_later(type)
237 int type;
238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
242 {
243 redraw_win_later(wp, type);
244 }
245}
246
247/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000248 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
251redraw_curbuf_later(type)
252 int type;
253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
258redraw_buf_later(buf, type)
259 buf_T *buf;
260 int type;
261{
262 win_T *wp;
263
264 FOR_ALL_WINDOWS(wp)
265 {
266 if (wp->w_buffer == buf)
267 redraw_win_later(wp, type);
268 }
269}
270
271/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200272 * Redraw as soon as possible. When the command line is not scrolled redraw
273 * right away and restore what was on the command line.
274 * Return a code indicating what happened.
275 */
276 int
277redraw_asap(type)
278 int type;
279{
280 int rows;
281 int r;
282 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200283 schar_T *screenline; /* copy from ScreenLines[] */
284 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200285#ifdef FEAT_MBYTE
286 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200287 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200289 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200290#endif
291
292 redraw_later(type);
293 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
294 return ret;
295
296 /* Allocate space to save the text displayed in the command line area. */
297 rows = Rows - cmdline_row;
298 screenline = (schar_T *)lalloc(
299 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
300 screenattr = (sattr_T *)lalloc(
301 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
302 if (screenline == NULL || screenattr == NULL)
303 ret = 2;
304#ifdef FEAT_MBYTE
305 if (enc_utf8)
306 {
307 screenlineUC = (u8char_T *)lalloc(
308 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
309 if (screenlineUC == NULL)
310 ret = 2;
311 for (i = 0; i < p_mco; ++i)
312 {
313 screenlineC[i] = (u8char_T *)lalloc(
314 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
315 if (screenlineC[i] == NULL)
316 ret = 2;
317 }
318 }
319 if (enc_dbcs == DBCS_JPNU)
320 {
321 screenline2 = (schar_T *)lalloc(
322 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
323 if (screenline2 == NULL)
324 ret = 2;
325 }
326#endif
327
328 if (ret != 2)
329 {
330 /* Save the text displayed in the command line area. */
331 for (r = 0; r < rows; ++r)
332 {
333 mch_memmove(screenline + r * Columns,
334 ScreenLines + LineOffset[cmdline_row + r],
335 (size_t)Columns * sizeof(schar_T));
336 mch_memmove(screenattr + r * Columns,
337 ScreenAttrs + LineOffset[cmdline_row + r],
338 (size_t)Columns * sizeof(sattr_T));
339#ifdef FEAT_MBYTE
340 if (enc_utf8)
341 {
342 mch_memmove(screenlineUC + r * Columns,
343 ScreenLinesUC + LineOffset[cmdline_row + r],
344 (size_t)Columns * sizeof(u8char_T));
345 for (i = 0; i < p_mco; ++i)
346 mch_memmove(screenlineC[i] + r * Columns,
347 ScreenLinesC[r] + LineOffset[cmdline_row + r],
348 (size_t)Columns * sizeof(u8char_T));
349 }
350 if (enc_dbcs == DBCS_JPNU)
351 mch_memmove(screenline2 + r * Columns,
352 ScreenLines2 + LineOffset[cmdline_row + r],
353 (size_t)Columns * sizeof(schar_T));
354#endif
355 }
356
357 update_screen(0);
358 ret = 3;
359
360 if (must_redraw == 0)
361 {
362 int off = (int)(current_ScreenLine - ScreenLines);
363
364 /* Restore the text displayed in the command line area. */
365 for (r = 0; r < rows; ++r)
366 {
367 mch_memmove(current_ScreenLine,
368 screenline + r * Columns,
369 (size_t)Columns * sizeof(schar_T));
370 mch_memmove(ScreenAttrs + off,
371 screenattr + r * Columns,
372 (size_t)Columns * sizeof(sattr_T));
373#ifdef FEAT_MBYTE
374 if (enc_utf8)
375 {
376 mch_memmove(ScreenLinesUC + off,
377 screenlineUC + r * Columns,
378 (size_t)Columns * sizeof(u8char_T));
379 for (i = 0; i < p_mco; ++i)
380 mch_memmove(ScreenLinesC[i] + off,
381 screenlineC[i] + r * Columns,
382 (size_t)Columns * sizeof(u8char_T));
383 }
384 if (enc_dbcs == DBCS_JPNU)
385 mch_memmove(ScreenLines2 + off,
386 screenline2 + r * Columns,
387 (size_t)Columns * sizeof(schar_T));
388#endif
389 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
390 }
391 ret = 4;
392 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394
395 vim_free(screenline);
396 vim_free(screenattr);
397#ifdef FEAT_MBYTE
398 if (enc_utf8)
399 {
400 vim_free(screenlineUC);
401 for (i = 0; i < p_mco; ++i)
402 vim_free(screenlineC[i]);
403 }
404 if (enc_dbcs == DBCS_JPNU)
405 vim_free(screenline2);
406#endif
407
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200408 /* Show the intro message when appropriate. */
409 maybe_intro_message();
410
411 setcursor();
412
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 return ret;
414}
415
416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * Changed something in the current window, at buffer line "lnum", that
418 * requires that line and possibly other lines to be redrawn.
419 * Used when entering/leaving Insert mode with the cursor on a folded line.
420 * Used to remove the "$" from a change command.
421 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
422 * may become invalid and the whole window will have to be redrawn.
423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 void
425redrawWinline(lnum, invalid)
426 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000427 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429#ifdef FEAT_FOLDING
430 int i;
431#endif
432
433 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
434 curwin->w_redraw_top = lnum;
435 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
436 curwin->w_redraw_bot = lnum;
437 redraw_later(VALID);
438
439#ifdef FEAT_FOLDING
440 if (invalid)
441 {
442 /* A w_lines[] entry for this lnum has become invalid. */
443 i = find_wl_entry(curwin, lnum);
444 if (i >= 0)
445 curwin->w_lines[i].wl_valid = FALSE;
446 }
447#endif
448}
449
450/*
451 * update all windows that are editing the current buffer
452 */
453 void
454update_curbuf(type)
455 int type;
456{
457 redraw_curbuf_later(type);
458 update_screen(type);
459}
460
461/*
462 * update_screen()
463 *
464 * Based on the current value of curwin->w_topline, transfer a screenfull
465 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
466 */
467 void
468update_screen(type)
469 int type;
470{
471 win_T *wp;
472 static int did_intro = FALSE;
473#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
474 int did_one;
475#endif
476
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000477 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (!screen_valid(TRUE))
479 return;
480
481 if (must_redraw)
482 {
483 if (type < must_redraw) /* use maximal type */
484 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000485
486 /* must_redraw is reset here, so that when we run into some weird
487 * reason to redraw while busy redrawing (e.g., asynchronous
488 * scrolling), or update_topline() in win_update() will cause a
489 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 must_redraw = 0;
491 }
492
493 /* Need to update w_lines[]. */
494 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
495 type = NOT_VALID;
496
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000497 /* Postpone the redrawing when it's not needed and when being called
498 * recursively. */
499 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {
501 redraw_later(type); /* remember type for next time */
502 must_redraw = type;
503 if (type > INVERTED_ALL)
504 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
505 return;
506 }
507
508 updating_screen = TRUE;
509#ifdef FEAT_SYN_HL
510 ++display_tick; /* let syntax code know we're in a next round of
511 * display updating */
512#endif
513
514 /*
515 * if the screen was scrolled up when displaying a message, scroll it down
516 */
517 if (msg_scrolled)
518 {
519 clear_cmdline = TRUE;
520 if (msg_scrolled > Rows - 5) /* clearing is faster */
521 type = CLEAR;
522 else if (type != CLEAR)
523 {
524 check_for_delay(FALSE);
525 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
526 type = CLEAR;
527 FOR_ALL_WINDOWS(wp)
528 {
529 if (W_WINROW(wp) < msg_scrolled)
530 {
531 if (W_WINROW(wp) + wp->w_height > msg_scrolled
532 && wp->w_redr_type < REDRAW_TOP
533 && wp->w_lines_valid > 0
534 && wp->w_topline == wp->w_lines[0].wl_lnum)
535 {
536 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
537 wp->w_redr_type = REDRAW_TOP;
538 }
539 else
540 {
541 wp->w_redr_type = NOT_VALID;
542#ifdef FEAT_WINDOWS
543 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
544 <= msg_scrolled)
545 wp->w_redr_status = TRUE;
546#endif
547 }
548 }
549 }
550 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000551#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000552 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555 msg_scrolled = 0;
556 need_wait_return = FALSE;
557 }
558
559 /* reset cmdline_row now (may have been changed temporarily) */
560 compute_cmdrow();
561
562 /* Check for changed highlighting */
563 if (need_highlight_changed)
564 highlight_changed();
565
566 if (type == CLEAR) /* first clear screen */
567 {
568 screenclear(); /* will reset clear_cmdline */
569 type = NOT_VALID;
570 }
571
572 if (clear_cmdline) /* going to clear cmdline (done below) */
573 check_for_delay(FALSE);
574
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000575#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200576 /* Force redraw when width of 'number' or 'relativenumber' column
577 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000578 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200579 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
580 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000581 curwin->w_redr_type = NOT_VALID;
582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 /*
585 * Only start redrawing if there is really something to do.
586 */
587 if (type == INVERTED)
588 update_curswant();
589 if (curwin->w_redr_type < type
590 && !((type == VALID
591 && curwin->w_lines[0].wl_valid
592#ifdef FEAT_DIFF
593 && curwin->w_topfill == curwin->w_old_topfill
594 && curwin->w_botfill == curwin->w_old_botfill
595#endif
596 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000598 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
600 && curwin->w_old_visual_mode == VIsual_mode
601 && (curwin->w_valid & VALID_VIRTCOL)
602 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 ))
604 curwin->w_redr_type = type;
605
Bram Moolenaar5a305422006-04-28 22:38:25 +0000606#ifdef FEAT_WINDOWS
607 /* Redraw the tab pages line if needed. */
608 if (redraw_tabline || type >= NOT_VALID)
609 draw_tabline();
610#endif
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_SYN_HL
613 /*
614 * Correct stored syntax highlighting info for changes in each displayed
615 * buffer. Each buffer must only be done once.
616 */
617 FOR_ALL_WINDOWS(wp)
618 {
619 if (wp->w_buffer->b_mod_set)
620 {
621# ifdef FEAT_WINDOWS
622 win_T *wwp;
623
624 /* Check if we already did this buffer. */
625 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
626 if (wwp->w_buffer == wp->w_buffer)
627 break;
628# endif
629 if (
630# ifdef FEAT_WINDOWS
631 wwp == wp &&
632# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200633 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 syn_stack_apply_changes(wp->w_buffer);
635 }
636 }
637#endif
638
639 /*
640 * Go from top to bottom through the windows, redrawing the ones that need
641 * it.
642 */
643#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
644 did_one = FALSE;
645#endif
646#ifdef FEAT_SEARCH_EXTRA
647 search_hl.rm.regprog = NULL;
648#endif
649 FOR_ALL_WINDOWS(wp)
650 {
651 if (wp->w_redr_type != 0)
652 {
653 cursor_off();
654#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
655 if (!did_one)
656 {
657 did_one = TRUE;
658# ifdef FEAT_SEARCH_EXTRA
659 start_search_hl();
660# endif
661# ifdef FEAT_CLIPBOARD
662 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200663 if (clip_star.available && clip_isautosel_star())
664 clip_update_selection(&clip_star);
665 if (clip_plus.available && clip_isautosel_plus())
666 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667# endif
668#ifdef FEAT_GUI
669 /* Remove the cursor before starting to do anything, because
670 * scrolling may make it difficult to redraw the text under
671 * it. */
672 if (gui.in_use)
673 gui_undraw_cursor();
674#endif
675 }
676#endif
677 win_update(wp);
678 }
679
680#ifdef FEAT_WINDOWS
681 /* redraw status line after the window to minimize cursor movement */
682 if (wp->w_redr_status)
683 {
684 cursor_off();
685 win_redr_status(wp);
686 }
687#endif
688 }
689#if defined(FEAT_SEARCH_EXTRA)
690 end_search_hl();
691#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100692#ifdef FEAT_INS_EXPAND
693 /* May need to redraw the popup menu. */
694 if (pum_visible())
695 pum_redraw();
696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698#ifdef FEAT_WINDOWS
699 /* Reset b_mod_set flags. Going through all windows is probably faster
700 * than going through all buffers (there could be many buffers). */
701 for (wp = firstwin; wp != NULL; wp = wp->w_next)
702 wp->w_buffer->b_mod_set = FALSE;
703#else
704 curbuf->b_mod_set = FALSE;
705#endif
706
707 updating_screen = FALSE;
708#ifdef FEAT_GUI
709 gui_may_resize_shell();
710#endif
711
712 /* Clear or redraw the command line. Done last, because scrolling may
713 * mess up the command line. */
714 if (clear_cmdline || redraw_cmdline)
715 showmode();
716
717 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200718 if (!did_intro)
719 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 did_intro = TRUE;
721
722#ifdef FEAT_GUI
723 /* Redraw the cursor and update the scrollbars when all screen updating is
724 * done. */
725 if (gui.in_use)
726 {
727 out_flush(); /* required before updating the cursor */
728 if (did_one)
729 gui_update_cursor(FALSE, FALSE);
730 gui_update_scrollbars(FALSE);
731 }
732#endif
733}
734
Bram Moolenaar860cae12010-06-05 23:22:07 +0200735#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200736/*
737 * Return TRUE if the cursor line in window "wp" may be concealed, according
738 * to the 'concealcursor' option.
739 */
740 int
741conceal_cursor_line(wp)
742 win_T *wp;
743{
744 int c;
745
746 if (*wp->w_p_cocu == NUL)
747 return FALSE;
748 if (get_real_state() & VISUAL)
749 c = 'v';
750 else if (State & INSERT)
751 c = 'i';
752 else if (State & NORMAL)
753 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200754 else if (State & CMDLINE)
755 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200756 else
757 return FALSE;
758 return vim_strchr(wp->w_p_cocu, c) != NULL;
759}
760
761/*
762 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
763 */
764 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200765conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200766{
767 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
768 {
769 need_cursor_line_redraw = TRUE;
770 /* Need to recompute cursor column, e.g., when starting Visual mode
771 * without concealing. */
772 curs_columns(TRUE);
773 }
774}
775
Bram Moolenaar860cae12010-06-05 23:22:07 +0200776 void
777update_single_line(wp, lnum)
778 win_T *wp;
779 linenr_T lnum;
780{
781 int row;
782 int j;
783
784 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200785 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200786 {
787# ifdef FEAT_GUI
788 /* Remove the cursor before starting to do anything, because scrolling
789 * may make it difficult to redraw the text under it. */
790 if (gui.in_use)
791 gui_undraw_cursor();
792# endif
793 row = 0;
794 for (j = 0; j < wp->w_lines_valid; ++j)
795 {
796 if (lnum == wp->w_lines[j].wl_lnum)
797 {
798 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200799# ifdef FEAT_SEARCH_EXTRA
800 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200801 start_search_hl();
802 prepare_search_hl(wp, lnum);
803# endif
804 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
805# if defined(FEAT_SEARCH_EXTRA)
806 end_search_hl();
807# endif
808 break;
809 }
810 row += wp->w_lines[j].wl_size;
811 }
812# ifdef FEAT_GUI
813 /* Redraw the cursor */
814 if (gui.in_use)
815 {
816 out_flush(); /* required before updating the cursor */
817 gui_update_cursor(FALSE, FALSE);
818 }
819# endif
820 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200821 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200822}
823#endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
826static void update_prepare __ARGS((void));
827static void update_finish __ARGS((void));
828
829/*
830 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000831 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
833 static void
834update_prepare()
835{
836 cursor_off();
837 updating_screen = TRUE;
838#ifdef FEAT_GUI
839 /* Remove the cursor before starting to do anything, because scrolling may
840 * make it difficult to redraw the text under it. */
841 if (gui.in_use)
842 gui_undraw_cursor();
843#endif
844#ifdef FEAT_SEARCH_EXTRA
845 start_search_hl();
846#endif
847}
848
849/*
850 * Finish updating one or more windows.
851 */
852 static void
853update_finish()
854{
855 if (redraw_cmdline)
856 showmode();
857
858# ifdef FEAT_SEARCH_EXTRA
859 end_search_hl();
860# endif
861
862 updating_screen = FALSE;
863
864# ifdef FEAT_GUI
865 gui_may_resize_shell();
866
867 /* Redraw the cursor and update the scrollbars when all screen updating is
868 * done. */
869 if (gui.in_use)
870 {
871 out_flush(); /* required before updating the cursor */
872 gui_update_cursor(FALSE, FALSE);
873 gui_update_scrollbars(FALSE);
874 }
875# endif
876}
877#endif
878
879#if defined(FEAT_SIGNS) || defined(PROTO)
880 void
881update_debug_sign(buf, lnum)
882 buf_T *buf;
883 linenr_T lnum;
884{
885 win_T *wp;
886 int doit = FALSE;
887
888# ifdef FEAT_FOLDING
889 win_foldinfo.fi_level = 0;
890# endif
891
892 /* update/delete a specific mark */
893 FOR_ALL_WINDOWS(wp)
894 {
895 if (buf != NULL && lnum > 0)
896 {
897 if (wp->w_buffer == buf && lnum >= wp->w_topline
898 && lnum < wp->w_botline)
899 {
900 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
901 wp->w_redraw_top = lnum;
902 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
903 wp->w_redraw_bot = lnum;
904 redraw_win_later(wp, VALID);
905 }
906 }
907 else
908 redraw_win_later(wp, VALID);
909 if (wp->w_redr_type != 0)
910 doit = TRUE;
911 }
912
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100913 /* Return when there is nothing to do, screen updating is already
914 * happening (recursive call) or still starting up. */
915 if (!doit || updating_screen
916#ifdef FEAT_GUI
917 || gui.starting
918#endif
919 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 return;
921
922 /* update all windows that need updating */
923 update_prepare();
924
925# ifdef FEAT_WINDOWS
926 for (wp = firstwin; wp; wp = wp->w_next)
927 {
928 if (wp->w_redr_type != 0)
929 win_update(wp);
930 if (wp->w_redr_status)
931 win_redr_status(wp);
932 }
933# else
934 if (curwin->w_redr_type != 0)
935 win_update(curwin);
936# endif
937
938 update_finish();
939}
940#endif
941
942
943#if defined(FEAT_GUI) || defined(PROTO)
944/*
945 * Update a single window, its status line and maybe the command line msg.
946 * Used for the GUI scrollbar.
947 */
948 void
949updateWindow(wp)
950 win_T *wp;
951{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000952 /* return if already busy updating */
953 if (updating_screen)
954 return;
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 update_prepare();
957
958#ifdef FEAT_CLIPBOARD
959 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200960 if (clip_star.available && clip_isautosel_star())
961 clip_update_selection(&clip_star);
962 if (clip_plus.available && clip_isautosel_plus())
963 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000969 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000970 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000971 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (wp->w_redr_status
974# ifdef FEAT_CMDL_INFO
975 || p_ru
976# endif
977# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000978 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979# endif
980 )
981 win_redr_status(wp);
982#endif
983
984 update_finish();
985}
986#endif
987
988/*
989 * Update a single window.
990 *
991 * This may cause the windows below it also to be redrawn (when clearing the
992 * screen or scrolling lines).
993 *
994 * How the window is redrawn depends on wp->w_redr_type. Each type also
995 * implies the one below it.
996 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000997 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
999 * INVERTED redraw the changed part of the Visual area
1000 * INVERTED_ALL redraw the whole Visual area
1001 * VALID 1. scroll up/down to adjust for a changed w_topline
1002 * 2. update lines at the top when scrolled down
1003 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001004 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * b_mod_top and b_mod_bot.
1006 * - if wp->w_redraw_top non-zero, redraw lines between
1007 * wp->w_redraw_top and wp->w_redr_bot.
1008 * - continue redrawing when syntax status is invalid.
1009 * 4. if scrolled up, update lines at the bottom.
1010 * This results in three areas that may need updating:
1011 * top: from first row to top_end (when scrolled down)
1012 * mid: from mid_start to mid_end (update inversion or changed text)
1013 * bot: from bot_start to last row (when scrolled up)
1014 */
1015 static void
1016win_update(wp)
1017 win_T *wp;
1018{
1019 buf_T *buf = wp->w_buffer;
1020 int type;
1021 int top_end = 0; /* Below last row of the top area that needs
1022 updating. 0 when no top area updating. */
1023 int mid_start = 999;/* first row of the mid area that needs
1024 updating. 999 when no mid area updating. */
1025 int mid_end = 0; /* Below last row of the mid area that needs
1026 updating. 0 when no mid area updating. */
1027 int bot_start = 999;/* first row of the bot area that needs
1028 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 int scrolled_down = FALSE; /* TRUE when scrolled down when
1030 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001032 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 int top_to_mod = FALSE; /* redraw above mod_top */
1034#endif
1035
1036 int row; /* current window row to display */
1037 linenr_T lnum; /* current buffer lnum to display */
1038 int idx; /* current index in w_lines[] */
1039 int srow; /* starting row of the current line */
1040
1041 int eof = FALSE; /* if TRUE, we hit the end of the file */
1042 int didline = FALSE; /* if TRUE, we finished the last line */
1043 int i;
1044 long j;
1045 static int recursive = FALSE; /* being called recursively */
1046 int old_botline = wp->w_botline;
1047#ifdef FEAT_FOLDING
1048 long fold_count;
1049#endif
1050#ifdef FEAT_SYN_HL
1051 /* remember what happened to the previous line, to know if
1052 * check_visual_highlight() can be used */
1053#define DID_NONE 1 /* didn't update a line */
1054#define DID_LINE 2 /* updated a normal line */
1055#define DID_FOLD 3 /* updated a folded line */
1056 int did_update = DID_NONE;
1057 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1058#endif
1059 linenr_T mod_top = 0;
1060 linenr_T mod_bot = 0;
1061#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1062 int save_got_int;
1063#endif
1064
1065 type = wp->w_redr_type;
1066
1067 if (type == NOT_VALID)
1068 {
1069#ifdef FEAT_WINDOWS
1070 wp->w_redr_status = TRUE;
1071#endif
1072 wp->w_lines_valid = 0;
1073 }
1074
1075 /* Window is zero-height: nothing to draw. */
1076 if (wp->w_height == 0)
1077 {
1078 wp->w_redr_type = 0;
1079 return;
1080 }
1081
1082#ifdef FEAT_VERTSPLIT
1083 /* Window is zero-width: Only need to draw the separator. */
1084 if (wp->w_width == 0)
1085 {
1086 /* draw the vertical separator right of this window */
1087 draw_vsep_win(wp, 0);
1088 wp->w_redr_type = 0;
1089 return;
1090 }
1091#endif
1092
1093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001094 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001097#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001098 /* Force redraw when width of 'number' or 'relativenumber' column
1099 * changes. */
1100 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001101 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001102 {
1103 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001104 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001105 }
1106 else
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1110 {
1111 /*
1112 * When there are both inserted/deleted lines and specific lines to be
1113 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1114 * everything (only happens when redrawing is off for while).
1115 */
1116 type = NOT_VALID;
1117 }
1118 else
1119 {
1120 /*
1121 * Set mod_top to the first line that needs displaying because of
1122 * changes. Set mod_bot to the first line after the changes.
1123 */
1124 mod_top = wp->w_redraw_top;
1125 if (wp->w_redraw_bot != 0)
1126 mod_bot = wp->w_redraw_bot + 1;
1127 else
1128 mod_bot = 0;
1129 wp->w_redraw_top = 0; /* reset for next time */
1130 wp->w_redraw_bot = 0;
1131 if (buf->b_mod_set)
1132 {
1133 if (mod_top == 0 || mod_top > buf->b_mod_top)
1134 {
1135 mod_top = buf->b_mod_top;
1136#ifdef FEAT_SYN_HL
1137 /* Need to redraw lines above the change that may be included
1138 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001139 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001141 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (mod_top < 1)
1143 mod_top = 1;
1144 }
1145#endif
1146 }
1147 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1148 mod_bot = buf->b_mod_bot;
1149
1150#ifdef FEAT_SEARCH_EXTRA
1151 /* When 'hlsearch' is on and using a multi-line search pattern, a
1152 * change in one line may make the Search highlighting in a
1153 * previous line invalid. Simple solution: redraw all visible
1154 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001155 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001157 if (search_hl.rm.regprog != NULL
1158 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001160 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001161 {
1162 cur = wp->w_match_head;
1163 while (cur != NULL)
1164 {
1165 if (cur->match.regprog != NULL
1166 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001167 {
1168 top_to_mod = TRUE;
1169 break;
1170 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001171 cur = cur->next;
1172 }
1173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#endif
1175 }
1176#ifdef FEAT_FOLDING
1177 if (mod_top != 0 && hasAnyFolding(wp))
1178 {
1179 linenr_T lnumt, lnumb;
1180
1181 /*
1182 * A change in a line can cause lines above it to become folded or
1183 * unfolded. Find the top most buffer line that may be affected.
1184 * If the line was previously folded and displayed, get the first
1185 * line of that fold. If the line is folded now, get the first
1186 * folded line. Use the minimum of these two.
1187 */
1188
1189 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1190 * the line below it. If there is no valid entry, use w_topline.
1191 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1192 * to this line. If there is no valid entry, use MAXLNUM. */
1193 lnumt = wp->w_topline;
1194 lnumb = MAXLNUM;
1195 for (i = 0; i < wp->w_lines_valid; ++i)
1196 if (wp->w_lines[i].wl_valid)
1197 {
1198 if (wp->w_lines[i].wl_lastlnum < mod_top)
1199 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1200 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1201 {
1202 lnumb = wp->w_lines[i].wl_lnum;
1203 /* When there is a fold column it might need updating
1204 * in the next line ("J" just above an open fold). */
1205 if (wp->w_p_fdc > 0)
1206 ++lnumb;
1207 }
1208 }
1209
1210 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1211 if (mod_top > lnumt)
1212 mod_top = lnumt;
1213
1214 /* Now do the same for the bottom line (one above mod_bot). */
1215 --mod_bot;
1216 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1217 ++mod_bot;
1218 if (mod_bot < lnumb)
1219 mod_bot = lnumb;
1220 }
1221#endif
1222
1223 /* When a change starts above w_topline and the end is below
1224 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * If the end of the change is above w_topline: do like no change was
1226 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (mod_top != 0 && mod_top < wp->w_topline)
1228 {
1229 if (mod_bot > wp->w_topline)
1230 mod_top = wp->w_topline;
1231#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001232 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 top_end = 1;
1234#endif
1235 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001236
1237 /* When line numbers are displayed need to redraw all lines below
1238 * inserted/deleted lines. */
1239 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1240 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242
1243 /*
1244 * When only displaying the lines at the top, set top_end. Used when
1245 * window has scrolled down for msg_scrolled.
1246 */
1247 if (type == REDRAW_TOP)
1248 {
1249 j = 0;
1250 for (i = 0; i < wp->w_lines_valid; ++i)
1251 {
1252 j += wp->w_lines[i].wl_size;
1253 if (j >= wp->w_upd_rows)
1254 {
1255 top_end = j;
1256 break;
1257 }
1258 }
1259 if (top_end == 0)
1260 /* not found (cannot happen?): redraw everything */
1261 type = NOT_VALID;
1262 else
1263 /* top area defined, the rest is VALID */
1264 type = VALID;
1265 }
1266
Bram Moolenaar367329b2007-08-30 11:53:22 +00001267 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001268 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1269 * non-zero and thus not FALSE) will indicate that screenclear() was not
1270 * called. */
1271 if (screen_cleared)
1272 screen_cleared = MAYBE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 /*
1275 * If there are no changes on the screen that require a complete redraw,
1276 * handle three cases:
1277 * 1: we are off the top of the screen by a few lines: scroll down
1278 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1279 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1280 * w_lines[] that needs updating.
1281 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001282 if ((type == VALID || type == SOME_VALID
1283 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284#ifdef FEAT_DIFF
1285 && !wp->w_botfill && !wp->w_old_botfill
1286#endif
1287 )
1288 {
1289 if (mod_top != 0 && wp->w_topline == mod_top)
1290 {
1291 /*
1292 * w_topline is the first changed line, the scrolling will be done
1293 * further down.
1294 */
1295 }
1296 else if (wp->w_lines[0].wl_valid
1297 && (wp->w_topline < wp->w_lines[0].wl_lnum
1298#ifdef FEAT_DIFF
1299 || (wp->w_topline == wp->w_lines[0].wl_lnum
1300 && wp->w_topfill > wp->w_old_topfill)
1301#endif
1302 ))
1303 {
1304 /*
1305 * New topline is above old topline: May scroll down.
1306 */
1307#ifdef FEAT_FOLDING
1308 if (hasAnyFolding(wp))
1309 {
1310 linenr_T ln;
1311
1312 /* count the number of lines we are off, counting a sequence
1313 * of folded lines as one */
1314 j = 0;
1315 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1316 {
1317 ++j;
1318 if (j >= wp->w_height - 2)
1319 break;
1320 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1321 }
1322 }
1323 else
1324#endif
1325 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1326 if (j < wp->w_height - 2) /* not too far off */
1327 {
1328 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1329#ifdef FEAT_DIFF
1330 /* insert extra lines for previously invisible filler lines */
1331 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1332 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1333 - wp->w_old_topfill;
1334#endif
1335 if (i < wp->w_height - 2) /* less than a screen off */
1336 {
1337 /*
1338 * Try to insert the correct number of lines.
1339 * If not the last window, delete the lines at the bottom.
1340 * win_ins_lines may fail when the terminal can't do it.
1341 */
1342 if (i > 0)
1343 check_for_delay(FALSE);
1344 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1345 {
1346 if (wp->w_lines_valid != 0)
1347 {
1348 /* Need to update rows that are new, stop at the
1349 * first one that scrolled down. */
1350 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 /* Move the entries that were scrolled, disable
1354 * the entries for the lines to be redrawn. */
1355 if ((wp->w_lines_valid += j) > wp->w_height)
1356 wp->w_lines_valid = wp->w_height;
1357 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1358 wp->w_lines[idx] = wp->w_lines[idx - j];
1359 while (idx >= 0)
1360 wp->w_lines[idx--].wl_valid = FALSE;
1361 }
1362 }
1363 else
1364 mid_start = 0; /* redraw all lines */
1365 }
1366 else
1367 mid_start = 0; /* redraw all lines */
1368 }
1369 else
1370 mid_start = 0; /* redraw all lines */
1371 }
1372 else
1373 {
1374 /*
1375 * New topline is at or below old topline: May scroll up.
1376 * When topline didn't change, find first entry in w_lines[] that
1377 * needs updating.
1378 */
1379
1380 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1381 j = -1;
1382 row = 0;
1383 for (i = 0; i < wp->w_lines_valid; i++)
1384 {
1385 if (wp->w_lines[i].wl_valid
1386 && wp->w_lines[i].wl_lnum == wp->w_topline)
1387 {
1388 j = i;
1389 break;
1390 }
1391 row += wp->w_lines[i].wl_size;
1392 }
1393 if (j == -1)
1394 {
1395 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1396 * lines */
1397 mid_start = 0;
1398 }
1399 else
1400 {
1401 /*
1402 * Try to delete the correct number of lines.
1403 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1404 */
1405#ifdef FEAT_DIFF
1406 /* If the topline didn't change, delete old filler lines,
1407 * otherwise delete filler lines of the new topline... */
1408 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1409 row += wp->w_old_topfill;
1410 else
1411 row += diff_check_fill(wp, wp->w_topline);
1412 /* ... but don't delete new filler lines. */
1413 row -= wp->w_topfill;
1414#endif
1415 if (row > 0)
1416 {
1417 check_for_delay(FALSE);
1418 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1419 bot_start = wp->w_height - row;
1420 else
1421 mid_start = 0; /* redraw all lines */
1422 }
1423 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1424 {
1425 /*
1426 * Skip the lines (below the deleted lines) that are still
1427 * valid and don't need redrawing. Copy their info
1428 * upwards, to compensate for the deleted lines. Set
1429 * bot_start to the first row that needs redrawing.
1430 */
1431 bot_start = 0;
1432 idx = 0;
1433 for (;;)
1434 {
1435 wp->w_lines[idx] = wp->w_lines[j];
1436 /* stop at line that didn't fit, unless it is still
1437 * valid (no lines deleted) */
1438 if (row > 0 && bot_start + row
1439 + (int)wp->w_lines[j].wl_size > wp->w_height)
1440 {
1441 wp->w_lines_valid = idx + 1;
1442 break;
1443 }
1444 bot_start += wp->w_lines[idx++].wl_size;
1445
1446 /* stop at the last valid entry in w_lines[].wl_size */
1447 if (++j >= wp->w_lines_valid)
1448 {
1449 wp->w_lines_valid = idx;
1450 break;
1451 }
1452 }
1453#ifdef FEAT_DIFF
1454 /* Correct the first entry for filler lines at the top
1455 * when it won't get updated below. */
1456 if (wp->w_p_diff && bot_start > 0)
1457 wp->w_lines[0].wl_size =
1458 plines_win_nofill(wp, wp->w_topline, TRUE)
1459 + wp->w_topfill;
1460#endif
1461 }
1462 }
1463 }
1464
1465 /* When starting redraw in the first line, redraw all lines. When
1466 * there is only one window it's probably faster to clear the screen
1467 * first. */
1468 if (mid_start == 0)
1469 {
1470 mid_end = wp->w_height;
1471 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001472 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473 /* Clear the screen when it was not done by win_del_lines() or
1474 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1475 * then. */
1476 if (screen_cleared != TRUE)
1477 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001478#ifdef FEAT_WINDOWS
1479 /* The screen was cleared, redraw the tab pages line. */
1480 if (redraw_tabline)
1481 draw_tabline();
1482#endif
1483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001485
1486 /* When win_del_lines() or win_ins_lines() caused the screen to be
1487 * cleared (only happens for the first window) or when screenclear()
1488 * was called directly above, "must_redraw" will have been set to
1489 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1490 if (screen_cleared == TRUE)
1491 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 else
1494 {
1495 /* Not VALID or INVERTED: redraw all lines. */
1496 mid_start = 0;
1497 mid_end = wp->w_height;
1498 }
1499
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001500 if (type == SOME_VALID)
1501 {
1502 /* SOME_VALID: redraw all lines. */
1503 mid_start = 0;
1504 mid_end = wp->w_height;
1505 type = NOT_VALID;
1506 }
1507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* check if we are updating or removing the inverted part */
1509 if ((VIsual_active && buf == curwin->w_buffer)
1510 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1511 {
1512 linenr_T from, to;
1513
1514 if (VIsual_active)
1515 {
1516 if (VIsual_active
1517 && (VIsual_mode != wp->w_old_visual_mode
1518 || type == INVERTED_ALL))
1519 {
1520 /*
1521 * If the type of Visual selection changed, redraw the whole
1522 * selection. Also when the ownership of the X selection is
1523 * gained or lost.
1524 */
1525 if (curwin->w_cursor.lnum < VIsual.lnum)
1526 {
1527 from = curwin->w_cursor.lnum;
1528 to = VIsual.lnum;
1529 }
1530 else
1531 {
1532 from = VIsual.lnum;
1533 to = curwin->w_cursor.lnum;
1534 }
1535 /* redraw more when the cursor moved as well */
1536 if (wp->w_old_cursor_lnum < from)
1537 from = wp->w_old_cursor_lnum;
1538 if (wp->w_old_cursor_lnum > to)
1539 to = wp->w_old_cursor_lnum;
1540 if (wp->w_old_visual_lnum < from)
1541 from = wp->w_old_visual_lnum;
1542 if (wp->w_old_visual_lnum > to)
1543 to = wp->w_old_visual_lnum;
1544 }
1545 else
1546 {
1547 /*
1548 * Find the line numbers that need to be updated: The lines
1549 * between the old cursor position and the current cursor
1550 * position. Also check if the Visual position changed.
1551 */
1552 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1553 {
1554 from = curwin->w_cursor.lnum;
1555 to = wp->w_old_cursor_lnum;
1556 }
1557 else
1558 {
1559 from = wp->w_old_cursor_lnum;
1560 to = curwin->w_cursor.lnum;
1561 if (from == 0) /* Visual mode just started */
1562 from = to;
1563 }
1564
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001565 if (VIsual.lnum != wp->w_old_visual_lnum
1566 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 if (wp->w_old_visual_lnum < from
1569 && wp->w_old_visual_lnum != 0)
1570 from = wp->w_old_visual_lnum;
1571 if (wp->w_old_visual_lnum > to)
1572 to = wp->w_old_visual_lnum;
1573 if (VIsual.lnum < from)
1574 from = VIsual.lnum;
1575 if (VIsual.lnum > to)
1576 to = VIsual.lnum;
1577 }
1578 }
1579
1580 /*
1581 * If in block mode and changed column or curwin->w_curswant:
1582 * update all lines.
1583 * First compute the actual start and end column.
1584 */
1585 if (VIsual_mode == Ctrl_V)
1586 {
1587 colnr_T fromc, toc;
1588
1589 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1590 ++toc;
1591 if (curwin->w_curswant == MAXCOL)
1592 toc = MAXCOL;
1593
1594 if (fromc != wp->w_old_cursor_fcol
1595 || toc != wp->w_old_cursor_lcol)
1596 {
1597 if (from > VIsual.lnum)
1598 from = VIsual.lnum;
1599 if (to < VIsual.lnum)
1600 to = VIsual.lnum;
1601 }
1602 wp->w_old_cursor_fcol = fromc;
1603 wp->w_old_cursor_lcol = toc;
1604 }
1605 }
1606 else
1607 {
1608 /* Use the line numbers of the old Visual area. */
1609 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1610 {
1611 from = wp->w_old_cursor_lnum;
1612 to = wp->w_old_visual_lnum;
1613 }
1614 else
1615 {
1616 from = wp->w_old_visual_lnum;
1617 to = wp->w_old_cursor_lnum;
1618 }
1619 }
1620
1621 /*
1622 * There is no need to update lines above the top of the window.
1623 */
1624 if (from < wp->w_topline)
1625 from = wp->w_topline;
1626
1627 /*
1628 * If we know the value of w_botline, use it to restrict the update to
1629 * the lines that are visible in the window.
1630 */
1631 if (wp->w_valid & VALID_BOTLINE)
1632 {
1633 if (from >= wp->w_botline)
1634 from = wp->w_botline - 1;
1635 if (to >= wp->w_botline)
1636 to = wp->w_botline - 1;
1637 }
1638
1639 /*
1640 * Find the minimal part to be updated.
1641 * Watch out for scrolling that made entries in w_lines[] invalid.
1642 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1643 * top_end; need to redraw from top_end to the "to" line.
1644 * A middle mouse click with a Visual selection may change the text
1645 * above the Visual area and reset wl_valid, do count these for
1646 * mid_end (in srow).
1647 */
1648 if (mid_start > 0)
1649 {
1650 lnum = wp->w_topline;
1651 idx = 0;
1652 srow = 0;
1653 if (scrolled_down)
1654 mid_start = top_end;
1655 else
1656 mid_start = 0;
1657 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1658 {
1659 if (wp->w_lines[idx].wl_valid)
1660 mid_start += wp->w_lines[idx].wl_size;
1661 else if (!scrolled_down)
1662 srow += wp->w_lines[idx].wl_size;
1663 ++idx;
1664# ifdef FEAT_FOLDING
1665 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1666 lnum = wp->w_lines[idx].wl_lnum;
1667 else
1668# endif
1669 ++lnum;
1670 }
1671 srow += mid_start;
1672 mid_end = wp->w_height;
1673 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1674 {
1675 if (wp->w_lines[idx].wl_valid
1676 && wp->w_lines[idx].wl_lnum >= to + 1)
1677 {
1678 /* Only update until first row of this line */
1679 mid_end = srow;
1680 break;
1681 }
1682 srow += wp->w_lines[idx].wl_size;
1683 }
1684 }
1685 }
1686
1687 if (VIsual_active && buf == curwin->w_buffer)
1688 {
1689 wp->w_old_visual_mode = VIsual_mode;
1690 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1691 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001692 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 wp->w_old_curswant = curwin->w_curswant;
1694 }
1695 else
1696 {
1697 wp->w_old_visual_mode = 0;
1698 wp->w_old_cursor_lnum = 0;
1699 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001700 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1704 /* reset got_int, otherwise regexp won't work */
1705 save_got_int = got_int;
1706 got_int = 0;
1707#endif
1708#ifdef FEAT_FOLDING
1709 win_foldinfo.fi_level = 0;
1710#endif
1711
1712 /*
1713 * Update all the window rows.
1714 */
1715 idx = 0; /* first entry in w_lines[].wl_size */
1716 row = 0;
1717 srow = 0;
1718 lnum = wp->w_topline; /* first line shown in window */
1719 for (;;)
1720 {
1721 /* stop updating when reached the end of the window (check for _past_
1722 * the end of the window is at the end of the loop) */
1723 if (row == wp->w_height)
1724 {
1725 didline = TRUE;
1726 break;
1727 }
1728
1729 /* stop updating when hit the end of the file */
1730 if (lnum > buf->b_ml.ml_line_count)
1731 {
1732 eof = TRUE;
1733 break;
1734 }
1735
1736 /* Remember the starting row of the line that is going to be dealt
1737 * with. It is used further down when the line doesn't fit. */
1738 srow = row;
1739
1740 /*
1741 * Update a line when it is in an area that needs updating, when it
1742 * has changes or w_lines[idx] is invalid.
1743 * bot_start may be halfway a wrapped line after using
1744 * win_del_lines(), check if the current line includes it.
1745 * When syntax folding is being used, the saved syntax states will
1746 * already have been updated, we can't see where the syntax state is
1747 * the same again, just update until the end of the window.
1748 */
1749 if (row < top_end
1750 || (row >= mid_start && row < mid_end)
1751#ifdef FEAT_SEARCH_EXTRA
1752 || top_to_mod
1753#endif
1754 || idx >= wp->w_lines_valid
1755 || (row + wp->w_lines[idx].wl_size > bot_start)
1756 || (mod_top != 0
1757 && (lnum == mod_top
1758 || (lnum >= mod_top
1759 && (lnum < mod_bot
1760#ifdef FEAT_SYN_HL
1761 || did_update == DID_FOLD
1762 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001763 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 && (
1765# ifdef FEAT_FOLDING
1766 (foldmethodIsSyntax(wp)
1767 && hasAnyFolding(wp)) ||
1768# endif
1769 syntax_check_changed(lnum)))
1770#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001771#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001772 /* match in fixed position might need redraw
1773 * if lines were inserted or deleted */
1774 || (wp->w_match_head != NULL
1775 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 )))))
1778 {
1779#ifdef FEAT_SEARCH_EXTRA
1780 if (lnum == mod_top)
1781 top_to_mod = FALSE;
1782#endif
1783
1784 /*
1785 * When at start of changed lines: May scroll following lines
1786 * up or down to minimize redrawing.
1787 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001788 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
1790 if (lnum == mod_top
1791 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001792 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 int old_rows = 0;
1795 int new_rows = 0;
1796 int xtra_rows;
1797 linenr_T l;
1798
1799 /* Count the old number of window rows, using w_lines[], which
1800 * should still contain the sizes for the lines as they are
1801 * currently displayed. */
1802 for (i = idx; i < wp->w_lines_valid; ++i)
1803 {
1804 /* Only valid lines have a meaningful wl_lnum. Invalid
1805 * lines are part of the changed area. */
1806 if (wp->w_lines[i].wl_valid
1807 && wp->w_lines[i].wl_lnum == mod_bot)
1808 break;
1809 old_rows += wp->w_lines[i].wl_size;
1810#ifdef FEAT_FOLDING
1811 if (wp->w_lines[i].wl_valid
1812 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1813 {
1814 /* Must have found the last valid entry above mod_bot.
1815 * Add following invalid entries. */
1816 ++i;
1817 while (i < wp->w_lines_valid
1818 && !wp->w_lines[i].wl_valid)
1819 old_rows += wp->w_lines[i++].wl_size;
1820 break;
1821 }
1822#endif
1823 }
1824
1825 if (i >= wp->w_lines_valid)
1826 {
1827 /* We can't find a valid line below the changed lines,
1828 * need to redraw until the end of the window.
1829 * Inserting/deleting lines has no use. */
1830 bot_start = 0;
1831 }
1832 else
1833 {
1834 /* Able to count old number of rows: Count new window
1835 * rows, and may insert/delete lines */
1836 j = idx;
1837 for (l = lnum; l < mod_bot; ++l)
1838 {
1839#ifdef FEAT_FOLDING
1840 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1841 ++new_rows;
1842 else
1843#endif
1844#ifdef FEAT_DIFF
1845 if (l == wp->w_topline)
1846 new_rows += plines_win_nofill(wp, l, TRUE)
1847 + wp->w_topfill;
1848 else
1849#endif
1850 new_rows += plines_win(wp, l, TRUE);
1851 ++j;
1852 if (new_rows > wp->w_height - row - 2)
1853 {
1854 /* it's getting too much, must redraw the rest */
1855 new_rows = 9999;
1856 break;
1857 }
1858 }
1859 xtra_rows = new_rows - old_rows;
1860 if (xtra_rows < 0)
1861 {
1862 /* May scroll text up. If there is not enough
1863 * remaining text or scrolling fails, must redraw the
1864 * rest. If scrolling works, must redraw the text
1865 * below the scrolled text. */
1866 if (row - xtra_rows >= wp->w_height - 2)
1867 mod_bot = MAXLNUM;
1868 else
1869 {
1870 check_for_delay(FALSE);
1871 if (win_del_lines(wp, row,
1872 -xtra_rows, FALSE, FALSE) == FAIL)
1873 mod_bot = MAXLNUM;
1874 else
1875 bot_start = wp->w_height + xtra_rows;
1876 }
1877 }
1878 else if (xtra_rows > 0)
1879 {
1880 /* May scroll text down. If there is not enough
1881 * remaining text of scrolling fails, must redraw the
1882 * rest. */
1883 if (row + xtra_rows >= wp->w_height - 2)
1884 mod_bot = MAXLNUM;
1885 else
1886 {
1887 check_for_delay(FALSE);
1888 if (win_ins_lines(wp, row + old_rows,
1889 xtra_rows, FALSE, FALSE) == FAIL)
1890 mod_bot = MAXLNUM;
1891 else if (top_end > row + old_rows)
1892 /* Scrolled the part at the top that requires
1893 * updating down. */
1894 top_end += xtra_rows;
1895 }
1896 }
1897
1898 /* When not updating the rest, may need to move w_lines[]
1899 * entries. */
1900 if (mod_bot != MAXLNUM && i != j)
1901 {
1902 if (j < i)
1903 {
1904 int x = row + new_rows;
1905
1906 /* move entries in w_lines[] upwards */
1907 for (;;)
1908 {
1909 /* stop at last valid entry in w_lines[] */
1910 if (i >= wp->w_lines_valid)
1911 {
1912 wp->w_lines_valid = j;
1913 break;
1914 }
1915 wp->w_lines[j] = wp->w_lines[i];
1916 /* stop at a line that won't fit */
1917 if (x + (int)wp->w_lines[j].wl_size
1918 > wp->w_height)
1919 {
1920 wp->w_lines_valid = j + 1;
1921 break;
1922 }
1923 x += wp->w_lines[j++].wl_size;
1924 ++i;
1925 }
1926 if (bot_start > x)
1927 bot_start = x;
1928 }
1929 else /* j > i */
1930 {
1931 /* move entries in w_lines[] downwards */
1932 j -= i;
1933 wp->w_lines_valid += j;
1934 if (wp->w_lines_valid > wp->w_height)
1935 wp->w_lines_valid = wp->w_height;
1936 for (i = wp->w_lines_valid; i - j >= idx; --i)
1937 wp->w_lines[i] = wp->w_lines[i - j];
1938
1939 /* The w_lines[] entries for inserted lines are
1940 * now invalid, but wl_size may be used above.
1941 * Reset to zero. */
1942 while (i >= idx)
1943 {
1944 wp->w_lines[i].wl_size = 0;
1945 wp->w_lines[i--].wl_valid = FALSE;
1946 }
1947 }
1948 }
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951
1952#ifdef FEAT_FOLDING
1953 /*
1954 * When lines are folded, display one line for all of them.
1955 * Otherwise, display normally (can be several display lines when
1956 * 'wrap' is on).
1957 */
1958 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1959 if (fold_count != 0)
1960 {
1961 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1962 ++row;
1963 --fold_count;
1964 wp->w_lines[idx].wl_folded = TRUE;
1965 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1966# ifdef FEAT_SYN_HL
1967 did_update = DID_FOLD;
1968# endif
1969 }
1970 else
1971#endif
1972 if (idx < wp->w_lines_valid
1973 && wp->w_lines[idx].wl_valid
1974 && wp->w_lines[idx].wl_lnum == lnum
1975 && lnum > wp->w_topline
1976 && !(dy_flags & DY_LASTLINE)
1977 && srow + wp->w_lines[idx].wl_size > wp->w_height
1978#ifdef FEAT_DIFF
1979 && diff_check_fill(wp, lnum) == 0
1980#endif
1981 )
1982 {
1983 /* This line is not going to fit. Don't draw anything here,
1984 * will draw "@ " lines below. */
1985 row = wp->w_height + 1;
1986 }
1987 else
1988 {
1989#ifdef FEAT_SEARCH_EXTRA
1990 prepare_search_hl(wp, lnum);
1991#endif
1992#ifdef FEAT_SYN_HL
1993 /* Let the syntax stuff know we skipped a few lines. */
1994 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001995 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 syntax_end_parsing(syntax_last_parsed + 1);
1997#endif
1998
1999 /*
2000 * Display one line.
2001 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002002 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004#ifdef FEAT_FOLDING
2005 wp->w_lines[idx].wl_folded = FALSE;
2006 wp->w_lines[idx].wl_lastlnum = lnum;
2007#endif
2008#ifdef FEAT_SYN_HL
2009 did_update = DID_LINE;
2010 syntax_last_parsed = lnum;
2011#endif
2012 }
2013
2014 wp->w_lines[idx].wl_lnum = lnum;
2015 wp->w_lines[idx].wl_valid = TRUE;
2016 if (row > wp->w_height) /* past end of screen */
2017 {
2018 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002019 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2021 ++idx;
2022 break;
2023 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002024 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 wp->w_lines[idx].wl_size = row - srow;
2026 ++idx;
2027#ifdef FEAT_FOLDING
2028 lnum += fold_count + 1;
2029#else
2030 ++lnum;
2031#endif
2032 }
2033 else
2034 {
2035 /* This line does not need updating, advance to the next one */
2036 row += wp->w_lines[idx++].wl_size;
2037 if (row > wp->w_height) /* past end of screen */
2038 break;
2039#ifdef FEAT_FOLDING
2040 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2041#else
2042 ++lnum;
2043#endif
2044#ifdef FEAT_SYN_HL
2045 did_update = DID_NONE;
2046#endif
2047 }
2048
2049 if (lnum > buf->b_ml.ml_line_count)
2050 {
2051 eof = TRUE;
2052 break;
2053 }
2054 }
2055 /*
2056 * End of loop over all window lines.
2057 */
2058
2059
2060 if (idx > wp->w_lines_valid)
2061 wp->w_lines_valid = idx;
2062
2063#ifdef FEAT_SYN_HL
2064 /*
2065 * Let the syntax stuff know we stop parsing here.
2066 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002067 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 syntax_end_parsing(syntax_last_parsed + 1);
2069#endif
2070
2071 /*
2072 * If we didn't hit the end of the file, and we didn't finish the last
2073 * line we were working on, then the line didn't fit.
2074 */
2075 wp->w_empty_rows = 0;
2076#ifdef FEAT_DIFF
2077 wp->w_filler_rows = 0;
2078#endif
2079 if (!eof && !didline)
2080 {
2081 if (lnum == wp->w_topline)
2082 {
2083 /*
2084 * Single line that does not fit!
2085 * Don't overwrite it, it can be edited.
2086 */
2087 wp->w_botline = lnum + 1;
2088 }
2089#ifdef FEAT_DIFF
2090 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2091 {
2092 /* Window ends in filler lines. */
2093 wp->w_botline = lnum;
2094 wp->w_filler_rows = wp->w_height - srow;
2095 }
2096#endif
2097 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2098 {
2099 /*
2100 * Last line isn't finished: Display "@@@" at the end.
2101 */
2102 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2103 W_WINROW(wp) + wp->w_height,
2104 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2105 '@', '@', hl_attr(HLF_AT));
2106 set_empty_rows(wp, srow);
2107 wp->w_botline = lnum;
2108 }
2109 else
2110 {
2111 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2112 wp->w_botline = lnum;
2113 }
2114 }
2115 else
2116 {
2117#ifdef FEAT_VERTSPLIT
2118 draw_vsep_win(wp, row);
2119#endif
2120 if (eof) /* we hit the end of the file */
2121 {
2122 wp->w_botline = buf->b_ml.ml_line_count + 1;
2123#ifdef FEAT_DIFF
2124 j = diff_check_fill(wp, wp->w_botline);
2125 if (j > 0 && !wp->w_botfill)
2126 {
2127 /*
2128 * Display filler lines at the end of the file
2129 */
2130 if (char2cells(fill_diff) > 1)
2131 i = '-';
2132 else
2133 i = fill_diff;
2134 if (row + j > wp->w_height)
2135 j = wp->w_height - row;
2136 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2137 row += j;
2138 }
2139#endif
2140 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002141 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 wp->w_botline = lnum;
2143
2144 /* make sure the rest of the screen is blank */
2145 /* put '~'s on rows that aren't part of the file. */
2146 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2147 }
2148
2149 /* Reset the type of redrawing required, the window has been updated. */
2150 wp->w_redr_type = 0;
2151#ifdef FEAT_DIFF
2152 wp->w_old_topfill = wp->w_topfill;
2153 wp->w_old_botfill = wp->w_botfill;
2154#endif
2155
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002156 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /*
2159 * There is a trick with w_botline. If we invalidate it on each
2160 * change that might modify it, this will cause a lot of expensive
2161 * calls to plines() in update_topline() each time. Therefore the
2162 * value of w_botline is often approximated, and this value is used to
2163 * compute the value of w_topline. If the value of w_botline was
2164 * wrong, check that the value of w_topline is correct (cursor is on
2165 * the visible part of the text). If it's not, we need to redraw
2166 * again. Mostly this just means scrolling up a few lines, so it
2167 * doesn't look too bad. Only do this for the current window (where
2168 * changes are relevant).
2169 */
2170 wp->w_valid |= VALID_BOTLINE;
2171 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2172 {
2173 recursive = TRUE;
2174 curwin->w_valid &= ~VALID_TOPLINE;
2175 update_topline(); /* may invalidate w_botline again */
2176 if (must_redraw != 0)
2177 {
2178 /* Don't update for changes in buffer again. */
2179 i = curbuf->b_mod_set;
2180 curbuf->b_mod_set = FALSE;
2181 win_update(curwin);
2182 must_redraw = 0;
2183 curbuf->b_mod_set = i;
2184 }
2185 recursive = FALSE;
2186 }
2187 }
2188
2189#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2190 /* restore got_int, unless CTRL-C was hit while redrawing */
2191 if (!got_int)
2192 got_int = save_got_int;
2193#endif
2194}
2195
2196#ifdef FEAT_SIGNS
2197static int draw_signcolumn __ARGS((win_T *wp));
2198
2199/*
2200 * Return TRUE when window "wp" has a column to draw signs in.
2201 */
2202 static int
2203draw_signcolumn(wp)
2204 win_T *wp;
2205{
2206 return (wp->w_buffer->b_signlist != NULL
2207# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002208 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# endif
2210 );
2211}
2212#endif
2213
2214/*
2215 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2216 * as the filler character.
2217 */
2218 static void
2219win_draw_end(wp, c1, c2, row, endrow, hl)
2220 win_T *wp;
2221 int c1;
2222 int c2;
2223 int row;
2224 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002225 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2228 int n = 0;
2229# define FDC_OFF n
2230#else
2231# define FDC_OFF 0
2232#endif
2233
2234#ifdef FEAT_RIGHTLEFT
2235 if (wp->w_p_rl)
2236 {
2237 /* No check for cmdline window: should never be right-left. */
2238# ifdef FEAT_FOLDING
2239 n = wp->w_p_fdc;
2240
2241 if (n > 0)
2242 {
2243 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002244 if (n > W_WIDTH(wp))
2245 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2247 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2248 ' ', ' ', hl_attr(HLF_FC));
2249 }
2250# endif
2251# ifdef FEAT_SIGNS
2252 if (draw_signcolumn(wp))
2253 {
2254 int nn = n + 2;
2255
2256 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002257 if (nn > W_WIDTH(wp))
2258 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2260 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2261 ' ', ' ', hl_attr(HLF_SC));
2262 n = nn;
2263 }
2264# endif
2265 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2266 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2267 c2, c2, hl_attr(hl));
2268 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2269 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2270 c1, c2, hl_attr(hl));
2271 }
2272 else
2273#endif
2274 {
2275#ifdef FEAT_CMDWIN
2276 if (cmdwin_type != 0 && wp == curwin)
2277 {
2278 /* draw the cmdline character in the leftmost column */
2279 n = 1;
2280 if (n > wp->w_width)
2281 n = wp->w_width;
2282 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2283 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2284 cmdwin_type, ' ', hl_attr(HLF_AT));
2285 }
2286#endif
2287#ifdef FEAT_FOLDING
2288 if (wp->w_p_fdc > 0)
2289 {
2290 int nn = n + wp->w_p_fdc;
2291
2292 /* draw the fold column at the left */
2293 if (nn > W_WIDTH(wp))
2294 nn = W_WIDTH(wp);
2295 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2296 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2297 ' ', ' ', hl_attr(HLF_FC));
2298 n = nn;
2299 }
2300#endif
2301#ifdef FEAT_SIGNS
2302 if (draw_signcolumn(wp))
2303 {
2304 int nn = n + 2;
2305
2306 /* draw the sign column after the fold column */
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_SC));
2312 n = nn;
2313 }
2314#endif
2315 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2316 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2317 c1, c2, hl_attr(hl));
2318 }
2319 set_empty_rows(wp, row);
2320}
2321
Bram Moolenaar1a384422010-07-14 19:53:30 +02002322#ifdef FEAT_SYN_HL
2323static int advance_color_col __ARGS((int vcol, int **color_cols));
2324
2325/*
2326 * Advance **color_cols and return TRUE when there are columns to draw.
2327 */
2328 static int
2329advance_color_col(vcol, color_cols)
2330 int vcol;
2331 int **color_cols;
2332{
2333 while (**color_cols >= 0 && vcol > **color_cols)
2334 ++*color_cols;
2335 return (**color_cols >= 0);
2336}
2337#endif
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339#ifdef FEAT_FOLDING
2340/*
2341 * Display one folded line.
2342 */
2343 static void
2344fold_line(wp, fold_count, foldinfo, lnum, row)
2345 win_T *wp;
2346 long fold_count;
2347 foldinfo_T *foldinfo;
2348 linenr_T lnum;
2349 int row;
2350{
2351 char_u buf[51];
2352 pos_T *top, *bot;
2353 linenr_T lnume = lnum + fold_count - 1;
2354 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002355 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int col;
2358 int txtcol;
2359 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002360 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 /* Build the fold line:
2363 * 1. Add the cmdwin_type for the command-line window
2364 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002365 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * 4. Compose the text
2367 * 5. Add the text
2368 * 6. set highlighting for the Visual area an other text
2369 */
2370 col = 0;
2371
2372 /*
2373 * 1. Add the cmdwin_type for the command-line window
2374 * Ignores 'rightleft', this window is never right-left.
2375 */
2376#ifdef FEAT_CMDWIN
2377 if (cmdwin_type != 0 && wp == curwin)
2378 {
2379 ScreenLines[off] = cmdwin_type;
2380 ScreenAttrs[off] = hl_attr(HLF_AT);
2381#ifdef FEAT_MBYTE
2382 if (enc_utf8)
2383 ScreenLinesUC[off] = 0;
2384#endif
2385 ++col;
2386 }
2387#endif
2388
2389 /*
2390 * 2. Add the 'foldcolumn'
2391 */
2392 fdc = wp->w_p_fdc;
2393 if (fdc > W_WIDTH(wp) - col)
2394 fdc = W_WIDTH(wp) - col;
2395 if (fdc > 0)
2396 {
2397 fill_foldcolumn(buf, wp, TRUE, lnum);
2398#ifdef FEAT_RIGHTLEFT
2399 if (wp->w_p_rl)
2400 {
2401 int i;
2402
2403 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2404 hl_attr(HLF_FC));
2405 /* reverse the fold column */
2406 for (i = 0; i < fdc; ++i)
2407 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2408 }
2409 else
2410#endif
2411 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2412 col += fdc;
2413 }
2414
2415#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002416# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2417 for (ri = 0; ri < l; ++ri) \
2418 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2419 else \
2420 for (ri = 0; ri < l; ++ri) \
2421 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002423# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2424 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#endif
2426
Bram Moolenaar64486672010-05-16 15:46:46 +02002427 /* Set all attributes of the 'number' or 'relativenumber' column and the
2428 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002429 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431#ifdef FEAT_SIGNS
2432 /* If signs are being displayed, add two spaces. */
2433 if (draw_signcolumn(wp))
2434 {
2435 len = W_WIDTH(wp) - col;
2436 if (len > 0)
2437 {
2438 if (len > 2)
2439 len = 2;
2440# ifdef FEAT_RIGHTLEFT
2441 if (wp->w_p_rl)
2442 /* the line number isn't reversed */
2443 copy_text_attr(off + W_WIDTH(wp) - len - col,
2444 (char_u *)" ", len, hl_attr(HLF_FL));
2445 else
2446# endif
2447 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2448 col += len;
2449 }
2450 }
2451#endif
2452
2453 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002454 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002456 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 len = W_WIDTH(wp) - col;
2459 if (len > 0)
2460 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002461 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002462 long num;
2463 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002464
2465 if (len > w + 1)
2466 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002467
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002468 if (wp->w_p_nu && !wp->w_p_rnu)
2469 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002470 num = (long)lnum;
2471 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002472 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002473 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002474 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002475 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002476 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002477 /* 'number' + 'relativenumber': cursor line shows absolute
2478 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002479 num = lnum;
2480 fmt = "%-*ld ";
2481 }
2482 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002483
Bram Moolenaar700e7342013-01-30 12:31:36 +01002484 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485#ifdef FEAT_RIGHTLEFT
2486 if (wp->w_p_rl)
2487 /* the line number isn't reversed */
2488 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2489 hl_attr(HLF_FL));
2490 else
2491#endif
2492 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2493 col += len;
2494 }
2495 }
2496
2497 /*
2498 * 4. Compose the folded-line string with 'foldtext', if set.
2499 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002500 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 txtcol = col; /* remember where text starts */
2503
2504 /*
2505 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2506 * Right-left text is put in columns 0 - number-col, normal text is put
2507 * in columns number-col - window-width.
2508 */
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
2511 {
2512 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002513 int u8c, u8cc[MAX_MCO];
2514 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int idx;
2516 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002517 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518# ifdef FEAT_ARABIC
2519 int prev_c = 0; /* previous Arabic character */
2520 int prev_c1 = 0; /* first composing char for prev_c */
2521# endif
2522
2523# ifdef FEAT_RIGHTLEFT
2524 if (wp->w_p_rl)
2525 idx = off;
2526 else
2527# endif
2528 idx = off + col;
2529
2530 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2531 for (p = text; *p != NUL; )
2532 {
2533 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002534 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (col + cells > W_WIDTH(wp)
2536# ifdef FEAT_RIGHTLEFT
2537 - (wp->w_p_rl ? col : 0)
2538# endif
2539 )
2540 break;
2541 ScreenLines[idx] = *p;
2542 if (enc_utf8)
2543 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002544 u8c = utfc_ptr2char(p, u8cc);
2545 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 ScreenLinesUC[idx] = 0;
2548#ifdef FEAT_ARABIC
2549 prev_c = u8c;
2550#endif
2551 }
2552 else
2553 {
2554#ifdef FEAT_ARABIC
2555 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2556 {
2557 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002558 int pc, pc1, nc;
2559 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int firstbyte = *p;
2561
2562 /* The idea of what is the previous and next
2563 * character depends on 'rightleft'. */
2564 if (wp->w_p_rl)
2565 {
2566 pc = prev_c;
2567 pc1 = prev_c1;
2568 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002569 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571 else
2572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002575 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 prev_c = u8c;
2578
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002579 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 pc, pc1, nc);
2581 ScreenLines[idx] = firstbyte;
2582 }
2583 else
2584 prev_c = u8c;
2585#endif
2586 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002587#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (u8c >= 0x10000)
2589 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2590 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002593 for (i = 0; i < Screen_mco; ++i)
2594 {
2595 ScreenLinesC[i][idx] = u8cc[i];
2596 if (u8cc[i] == 0)
2597 break;
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 if (cells > 1)
2601 ScreenLines[idx + 1] = 0;
2602 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002603 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2604 /* double-byte single width character */
2605 ScreenLines2[idx] = p[1];
2606 else if (cells > 1)
2607 /* double-width character */
2608 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 col += cells;
2610 idx += cells;
2611 p += c_len;
2612 }
2613 }
2614 else
2615#endif
2616 {
2617 len = (int)STRLEN(text);
2618 if (len > W_WIDTH(wp) - col)
2619 len = W_WIDTH(wp) - col;
2620 if (len > 0)
2621 {
2622#ifdef FEAT_RIGHTLEFT
2623 if (wp->w_p_rl)
2624 STRNCPY(current_ScreenLine, text, len);
2625 else
2626#endif
2627 STRNCPY(current_ScreenLine + col, text, len);
2628 col += len;
2629 }
2630 }
2631
2632 /* Fill the rest of the line with the fold filler */
2633#ifdef FEAT_RIGHTLEFT
2634 if (wp->w_p_rl)
2635 col -= txtcol;
2636#endif
2637 while (col < W_WIDTH(wp)
2638#ifdef FEAT_RIGHTLEFT
2639 - (wp->w_p_rl ? txtcol : 0)
2640#endif
2641 )
2642 {
2643#ifdef FEAT_MBYTE
2644 if (enc_utf8)
2645 {
2646 if (fill_fold >= 0x80)
2647 {
2648 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
2651 else
2652 ScreenLinesUC[off + col] = 0;
2653 }
2654#endif
2655 ScreenLines[off + col++] = fill_fold;
2656 }
2657
2658 if (text != buf)
2659 vim_free(text);
2660
2661 /*
2662 * 6. set highlighting for the Visual area an other text.
2663 * If all folded lines are in the Visual area, highlight the line.
2664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2666 {
2667 if (ltoreq(curwin->w_cursor, VIsual))
2668 {
2669 /* Visual is after curwin->w_cursor */
2670 top = &curwin->w_cursor;
2671 bot = &VIsual;
2672 }
2673 else
2674 {
2675 /* Visual is before curwin->w_cursor */
2676 top = &VIsual;
2677 bot = &curwin->w_cursor;
2678 }
2679 if (lnum >= top->lnum
2680 && lnume <= bot->lnum
2681 && (VIsual_mode != 'v'
2682 || ((lnum > top->lnum
2683 || (lnum == top->lnum
2684 && top->col == 0))
2685 && (lnume < bot->lnum
2686 || (lnume == bot->lnum
2687 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002688 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 if (VIsual_mode == Ctrl_V)
2691 {
2692 /* Visual block mode: highlight the chars part of the block */
2693 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2694 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002695 if (wp->w_old_cursor_lcol != MAXCOL
2696 && wp->w_old_cursor_lcol + txtcol
2697 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 len = wp->w_old_cursor_lcol;
2699 else
2700 len = W_WIDTH(wp) - txtcol;
2701 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002702 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 }
2705 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002708 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002713#ifdef FEAT_SYN_HL
2714 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002715 if (wp->w_p_cuc)
2716 {
2717 txtcol += wp->w_virtcol;
2718 if (wp->w_p_wrap)
2719 txtcol -= wp->w_skipcol;
2720 else
2721 txtcol -= wp->w_leftcol;
2722 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2723 ScreenAttrs[off + txtcol] = hl_combine_attr(
2724 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2725 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2729 (int)W_WIDTH(wp), FALSE);
2730
2731 /*
2732 * Update w_cline_height and w_cline_folded if the cursor line was
2733 * updated (saves a call to plines() later).
2734 */
2735 if (wp == curwin
2736 && lnum <= curwin->w_cursor.lnum
2737 && lnume >= curwin->w_cursor.lnum)
2738 {
2739 curwin->w_cline_row = row;
2740 curwin->w_cline_height = 1;
2741 curwin->w_cline_folded = TRUE;
2742 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2743 }
2744}
2745
2746/*
2747 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2748 */
2749 static void
2750copy_text_attr(off, buf, len, attr)
2751 int off;
2752 char_u *buf;
2753 int len;
2754 int attr;
2755{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002756 int i;
2757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 mch_memmove(ScreenLines + off, buf, (size_t)len);
2759# ifdef FEAT_MBYTE
2760 if (enc_utf8)
2761 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2762# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002763 for (i = 0; i < len; ++i)
2764 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765}
2766
2767/*
2768 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002769 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 */
2771 static void
2772fill_foldcolumn(p, wp, closed, lnum)
2773 char_u *p;
2774 win_T *wp;
2775 int closed; /* TRUE of FALSE */
2776 linenr_T lnum; /* current line number */
2777{
2778 int i = 0;
2779 int level;
2780 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002781 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /* Init to all spaces. */
2784 copy_spaces(p, (size_t)wp->w_p_fdc);
2785
2786 level = win_foldinfo.fi_level;
2787 if (level > 0)
2788 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002789 /* If there is only one column put more info in it. */
2790 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /* If the column is too narrow, we start at the lowest level that
2793 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002794 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (first_level < 1)
2796 first_level = 1;
2797
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002798 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
2800 if (win_foldinfo.fi_lnum == lnum
2801 && first_level + i >= win_foldinfo.fi_low_level)
2802 p[i] = '-';
2803 else if (first_level == 1)
2804 p[i] = '|';
2805 else if (first_level + i <= 9)
2806 p[i] = '0' + first_level + i;
2807 else
2808 p[i] = '>';
2809 if (first_level + i == level)
2810 break;
2811 }
2812 }
2813 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002814 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815}
2816#endif /* FEAT_FOLDING */
2817
2818/*
2819 * Display line "lnum" of window 'wp' on the screen.
2820 * Start at row "startrow", stop when "endrow" is reached.
2821 * wp->w_virtcol needs to be valid.
2822 *
2823 * Return the number of last row the line occupies.
2824 */
2825 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002826win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 win_T *wp;
2828 linenr_T lnum;
2829 int startrow;
2830 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002831 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
2833 int col; /* visual column on screen */
2834 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2835 int c = 0; /* init for GCC */
2836 long vcol = 0; /* virtual column (for tabs) */
2837 long vcol_prev = -1; /* "vcol" of previous character */
2838 char_u *line; /* current line */
2839 char_u *ptr; /* current position in "line" */
2840 int row; /* row in the window, excl w_winrow */
2841 int screen_row; /* row on the screen, incl w_winrow */
2842
2843 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2844 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002845 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02002846 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 int c_extra = NUL; /* extra chars, all the same */
2848 int extra_attr = 0; /* attributes when n_extra != 0 */
2849 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2850 displaying lcs_eol at end-of-line */
2851 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2852 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2853
2854 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2855 int saved_n_extra = 0;
2856 char_u *saved_p_extra = NULL;
2857 int saved_c_extra = 0;
2858 int saved_char_attr = 0;
2859
2860 int n_attr = 0; /* chars with special attr */
2861 int saved_attr2 = 0; /* char_attr saved for n_attr */
2862 int n_attr3 = 0; /* chars with overruling special attr */
2863 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2864
2865 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2866
2867 int fromcol, tocol; /* start/end of inverting */
2868 int fromcol_prev = -2; /* start of inverting after cursor */
2869 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002871 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 pos_T pos;
2873 long v;
2874
2875 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002876 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2878 in this line */
2879 int attr = 0; /* attributes for area highlighting */
2880 int area_attr = 0; /* attributes desired by highlighting */
2881 int search_attr = 0; /* attributes desired by 'hlsearch' */
2882#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002883 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 int syntax_attr = 0; /* attributes desired by syntax */
2885 int has_syntax = FALSE; /* this buffer has syntax highl. */
2886 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002887 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002888 int draw_color_col = FALSE; /* highlight colorcolumn */
2889 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002890#endif
2891#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002892 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002893# define SPWORDLEN 150
2894 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002895 int nextlinecol = 0; /* column where nextline[] starts */
2896 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002897 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002898 int spell_attr = 0; /* attributes desired by spelling */
2899 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002900 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2901 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002902 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002903 static int cap_col = -1; /* column to check for Cap word */
2904 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002905 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906#endif
2907 int extra_check; /* has syntax or linebreak */
2908#ifdef FEAT_MBYTE
2909 int multi_attr = 0; /* attributes desired by multibyte */
2910 int mb_l = 1; /* multi-byte byte length */
2911 int mb_c = 0; /* decoded multi-byte character */
2912 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002913 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
2915#ifdef FEAT_DIFF
2916 int filler_lines; /* nr of filler lines to be drawn */
2917 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002918 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 int change_start = MAXCOL; /* first col of changed area */
2920 int change_end = -1; /* last col of changed area */
2921#endif
2922 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2923#ifdef FEAT_LINEBREAK
2924 int need_showbreak = FALSE;
2925#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002926#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2927 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002929 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930#endif
2931#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002932 matchitem_T *cur; /* points to the match list */
2933 match_T *shl; /* points to search_hl or a match */
2934 int shl_flag; /* flag to indicate whether search_hl
2935 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002936 int pos_inprogress; /* marks that position match search is
2937 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002938 int prevcol_hl_flag; /* flag to indicate whether prevcol
2939 equals startcol of search_hl or one
2940 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941#endif
2942#ifdef FEAT_ARABIC
2943 int prev_c = 0; /* previous Arabic character */
2944 int prev_c1 = 0; /* first composing char for prev_c */
2945#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002946#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002947 int did_line_attr = 0;
2948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /* draw_state: items that are drawn in sequence: */
2951#define WL_START 0 /* nothing done yet */
2952#ifdef FEAT_CMDWIN
2953# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2954#else
2955# define WL_CMDLINE WL_START
2956#endif
2957#ifdef FEAT_FOLDING
2958# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2959#else
2960# define WL_FOLD WL_CMDLINE
2961#endif
2962#ifdef FEAT_SIGNS
2963# define WL_SIGN WL_FOLD + 1 /* column for signs */
2964#else
2965# define WL_SIGN WL_FOLD /* column for signs */
2966#endif
2967#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02002968#ifdef FEAT_LINEBREAK
2969# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02002971# define WL_BRI WL_NR
2972#endif
2973#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2974# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
2975#else
2976# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#endif
2978#define WL_LINE WL_SBR + 1 /* text in the line */
2979 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002980#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 int feedback_col = 0;
2982 int feedback_old_attr = -1;
2983#endif
2984
Bram Moolenaar860cae12010-06-05 23:22:07 +02002985#ifdef FEAT_CONCEAL
2986 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002987 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002988 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002989 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002990 int is_concealing = FALSE;
2991 int boguscols = 0; /* nonexistent columns added to force
2992 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002993 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002994 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002995# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02002996# define FIX_FOR_BOGUSCOLS \
2997 { \
2998 n_extra += vcol_off; \
2999 vcol -= vcol_off; \
3000 vcol_off = 0; \
3001 col -= boguscols; \
3002 boguscols = 0; \
3003 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003004#else
3005# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 if (startrow > endrow) /* past the end already! */
3009 return startrow;
3010
3011 row = startrow;
3012 screen_row = row + W_WINROW(wp);
3013
3014 /*
3015 * To speed up the loop below, set extra_check when there is linebreak,
3016 * trailing white space and/or syntax processing to be done.
3017 */
3018#ifdef FEAT_LINEBREAK
3019 extra_check = wp->w_p_lbr;
3020#else
3021 extra_check = 0;
3022#endif
3023#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003024 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
3026 /* Prepare for syntax highlighting in this line. When there is an
3027 * error, stop syntax highlighting. */
3028 save_did_emsg = did_emsg;
3029 did_emsg = FALSE;
3030 syntax_start(wp, lnum);
3031 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 {
3035 did_emsg = save_did_emsg;
3036 has_syntax = TRUE;
3037 extra_check = TRUE;
3038 }
3039 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003040
3041 /* Check for columns to display for 'colorcolumn'. */
3042 color_cols = wp->w_p_cc_cols;
3043 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003044 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003045#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003046
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003047#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003048 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003049 && *wp->w_s->b_p_spl != NUL
3050 && wp->w_s->b_langp.ga_len > 0
3051 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003052 {
3053 /* Prepare for spell checking. */
3054 has_spell = TRUE;
3055 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003056
3057 /* Get the start of the next line, so that words that wrap to the next
3058 * line are found too: "et<line-break>al.".
3059 * Trick: skip a few chars for C/shell/Vim comments */
3060 nextline[SPWORDLEN] = NUL;
3061 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3062 {
3063 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3064 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3065 }
3066
3067 /* When a word wrapped from the previous line the start of the current
3068 * line is valid. */
3069 if (lnum == checked_lnum)
3070 cur_checked_col = checked_col;
3071 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003072
3073 /* When there was a sentence end in the previous line may require a
3074 * word starting with capital in this line. In line 1 always check
3075 * the first word. */
3076 if (lnum != capcol_lnum)
3077 cap_col = -1;
3078 if (lnum == 1)
3079 cap_col = 0;
3080 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082#endif
3083
3084 /*
3085 * handle visual active in this window
3086 */
3087 fromcol = -10;
3088 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3090 {
3091 /* Visual is after curwin->w_cursor */
3092 if (ltoreq(curwin->w_cursor, VIsual))
3093 {
3094 top = &curwin->w_cursor;
3095 bot = &VIsual;
3096 }
3097 else /* Visual is before curwin->w_cursor */
3098 {
3099 top = &VIsual;
3100 bot = &curwin->w_cursor;
3101 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003102 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (VIsual_mode == Ctrl_V) /* block mode */
3104 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003105 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 fromcol = wp->w_old_cursor_fcol;
3108 tocol = wp->w_old_cursor_lcol;
3109 }
3110 }
3111 else /* non-block mode */
3112 {
3113 if (lnum > top->lnum && lnum <= bot->lnum)
3114 fromcol = 0;
3115 else if (lnum == top->lnum)
3116 {
3117 if (VIsual_mode == 'V') /* linewise */
3118 fromcol = 0;
3119 else
3120 {
3121 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3122 if (gchar_pos(top) == NUL)
3123 tocol = fromcol + 1;
3124 }
3125 }
3126 if (VIsual_mode != 'V' && lnum == bot->lnum)
3127 {
3128 if (*p_sel == 'e' && bot->col == 0
3129#ifdef FEAT_VIRTUALEDIT
3130 && bot->coladd == 0
3131#endif
3132 )
3133 {
3134 fromcol = -10;
3135 tocol = MAXCOL;
3136 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003137 else if (bot->col == MAXCOL)
3138 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 else
3140 {
3141 pos = *bot;
3142 if (*p_sel == 'e')
3143 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3144 else
3145 {
3146 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3147 ++tocol;
3148 }
3149 }
3150 }
3151 }
3152
3153#ifndef MSDOS
3154 /* Check if the character under the cursor should not be inverted */
3155 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3156# ifdef FEAT_GUI
3157 && !gui.in_use
3158# endif
3159 )
3160 noinvcur = TRUE;
3161#endif
3162
3163 /* if inverting in this line set area_highlighting */
3164 if (fromcol >= 0)
3165 {
3166 area_highlighting = TRUE;
3167 attr = hl_attr(HLF_V);
3168#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003169 if ((clip_star.available && !clip_star.owned
3170 && clip_isautosel_star())
3171 || (clip_plus.available && !clip_plus.owned
3172 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 attr = hl_attr(HLF_VNC);
3174#endif
3175 }
3176 }
3177
3178 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003179 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003181 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 && wp == curwin
3183 && lnum >= curwin->w_cursor.lnum
3184 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3185 {
3186 if (lnum == curwin->w_cursor.lnum)
3187 getvcol(curwin, &(curwin->w_cursor),
3188 (colnr_T *)&fromcol, NULL, NULL);
3189 else
3190 fromcol = 0;
3191 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3192 {
3193 pos.lnum = lnum;
3194 pos.col = search_match_endcol;
3195 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3196 }
3197 else
3198 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003199 /* do at least one character; happens when past end of line */
3200 if (fromcol == tocol)
3201 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 area_highlighting = TRUE;
3203 attr = hl_attr(HLF_I);
3204 }
3205
3206#ifdef FEAT_DIFF
3207 filler_lines = diff_check(wp, lnum);
3208 if (filler_lines < 0)
3209 {
3210 if (filler_lines == -1)
3211 {
3212 if (diff_find_change(wp, lnum, &change_start, &change_end))
3213 diff_hlf = HLF_ADD; /* added line */
3214 else if (change_start == 0)
3215 diff_hlf = HLF_TXD; /* changed text */
3216 else
3217 diff_hlf = HLF_CHD; /* changed line */
3218 }
3219 else
3220 diff_hlf = HLF_ADD; /* added line */
3221 filler_lines = 0;
3222 area_highlighting = TRUE;
3223 }
3224 if (lnum == wp->w_topline)
3225 filler_lines = wp->w_topfill;
3226 filler_todo = filler_lines;
3227#endif
3228
3229#ifdef LINE_ATTR
3230# ifdef FEAT_SIGNS
3231 /* If this line has a sign with line highlighting set line_attr. */
3232 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3233 if (v != 0)
3234 line_attr = sign_get_attr((int)v, TRUE);
3235# endif
3236# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3237 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003238 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 line_attr = hl_attr(HLF_L);
3240# endif
3241 if (line_attr != 0)
3242 area_highlighting = TRUE;
3243#endif
3244
3245 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3246 ptr = line;
3247
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003248#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003249 if (has_spell)
3250 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003251 /* For checking first word with a capital skip white space. */
3252 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003253 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003254
Bram Moolenaar30abd282005-06-22 22:35:10 +00003255 /* To be able to spell-check over line boundaries copy the end of the
3256 * current line into nextline[]. Above the start of the next line was
3257 * copied to nextline[SPWORDLEN]. */
3258 if (nextline[SPWORDLEN] == NUL)
3259 {
3260 /* No next line or it is empty. */
3261 nextlinecol = MAXCOL;
3262 nextline_idx = 0;
3263 }
3264 else
3265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003266 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003267 if (v < SPWORDLEN)
3268 {
3269 /* Short line, use it completely and append the start of the
3270 * next line. */
3271 nextlinecol = 0;
3272 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003273 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003274 nextline_idx = v + 1;
3275 }
3276 else
3277 {
3278 /* Long line, use only the last SPWORDLEN bytes. */
3279 nextlinecol = v - SPWORDLEN;
3280 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3281 nextline_idx = SPWORDLEN + 1;
3282 }
3283 }
3284 }
3285#endif
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /* find start of trailing whitespace */
3288 if (wp->w_p_list && lcs_trail)
3289 {
3290 trailcol = (colnr_T)STRLEN(ptr);
3291 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3292 --trailcol;
3293 trailcol += (colnr_T) (ptr - line);
3294 extra_check = TRUE;
3295 }
3296
3297 /*
3298 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3299 * first character to be displayed.
3300 */
3301 if (wp->w_p_wrap)
3302 v = wp->w_skipcol;
3303 else
3304 v = wp->w_leftcol;
3305 if (v > 0)
3306 {
3307#ifdef FEAT_MBYTE
3308 char_u *prev_ptr = ptr;
3309#endif
3310 while (vcol < v && *ptr != NUL)
3311 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003312 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 vcol += c;
3314#ifdef FEAT_MBYTE
3315 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003317 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003320 /* When:
3321 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003322 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003323 * - 'virtualedit' is set, or
3324 * - the visual mode is active,
3325 * the end of the line may be before the start of the displayed part.
3326 */
3327 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003328#ifdef FEAT_SYN_HL
3329 wp->w_p_cuc || draw_color_col ||
3330#endif
3331#ifdef FEAT_VIRTUALEDIT
3332 virtual_active() ||
3333#endif
3334 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
3339 /* Handle a character that's not completely on the screen: Put ptr at
3340 * that character but skip the first few screen characters. */
3341 if (vcol > v)
3342 {
3343 vcol -= c;
3344#ifdef FEAT_MBYTE
3345 ptr = prev_ptr;
3346#else
3347 --ptr;
3348#endif
3349 n_skip = v - vcol;
3350 }
3351
3352 /*
3353 * Adjust for when the inverted text is before the screen,
3354 * and when the start of the inverted text is before the screen.
3355 */
3356 if (tocol <= vcol)
3357 fromcol = 0;
3358 else if (fromcol >= 0 && fromcol < vcol)
3359 fromcol = vcol;
3360
3361#ifdef FEAT_LINEBREAK
3362 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3363 if (wp->w_p_wrap)
3364 need_showbreak = TRUE;
3365#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003366#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003367 /* When spell checking a word we need to figure out the start of the
3368 * word and if it's badly spelled or not. */
3369 if (has_spell)
3370 {
3371 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003372 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003373 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003374
3375 pos = wp->w_cursor;
3376 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003377 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003378 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003379
3380 /* spell_move_to() may call ml_get() and make "line" invalid */
3381 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3382 ptr = line + linecol;
3383
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003384 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003385 {
3386 /* no bad word found at line start, don't check until end of a
3387 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003388 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003389 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003390 }
3391 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003392 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003393 /* bad word found, use attributes until end of word */
3394 word_end = wp->w_cursor.col + len + 1;
3395
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 /* Turn index into actual attributes. */
3397 if (spell_hlf != HLF_COUNT)
3398 spell_attr = highlight_attr[spell_hlf];
3399 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003400 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003401
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003402# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003403 /* Need to restart syntax highlighting for this line. */
3404 if (has_syntax)
3405 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003406# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003407 }
3408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410
3411 /*
3412 * Correct highlighting for cursor that can't be disabled.
3413 * Avoids having to check this for each character.
3414 */
3415 if (fromcol >= 0)
3416 {
3417 if (noinvcur)
3418 {
3419 if ((colnr_T)fromcol == wp->w_virtcol)
3420 {
3421 /* highlighting starts at cursor, let it start just after the
3422 * cursor */
3423 fromcol_prev = fromcol;
3424 fromcol = -1;
3425 }
3426 else if ((colnr_T)fromcol < wp->w_virtcol)
3427 /* restart highlighting after the cursor */
3428 fromcol_prev = wp->w_virtcol;
3429 }
3430 if (fromcol >= tocol)
3431 fromcol = -1;
3432 }
3433
3434#ifdef FEAT_SEARCH_EXTRA
3435 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003436 * Handle highlighting the last used search pattern and matches.
3437 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003439 cur = wp->w_match_head;
3440 shl_flag = FALSE;
3441 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003443 if (shl_flag == FALSE)
3444 {
3445 shl = &search_hl;
3446 shl_flag = TRUE;
3447 }
3448 else
3449 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003450 shl->startcol = MAXCOL;
3451 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003453 v = (long)(ptr - line);
3454 if (cur != NULL)
3455 cur->pos.cur = 0;
3456 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3457
3458 /* Need to get the line again, a multi-line regexp may have made it
3459 * invalid. */
3460 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3461 ptr = line + v;
3462
3463 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003465 if (shl->lnum == lnum)
3466 shl->startcol = shl->rm.startpos[0].col;
3467 else
3468 shl->startcol = 0;
3469 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3470 - shl->rm.startpos[0].lnum)
3471 shl->endcol = shl->rm.endpos[0].col;
3472 else
3473 shl->endcol = MAXCOL;
3474 /* Highlight one character for an empty match. */
3475 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003478 if (has_mbyte && line[shl->endcol] != NUL)
3479 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003482 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003484 if ((long)shl->startcol < v) /* match at leftcol */
3485 {
3486 shl->attr_cur = shl->attr;
3487 search_attr = shl->attr;
3488 }
3489 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003491 if (shl != &search_hl && cur != NULL)
3492 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494#endif
3495
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003496#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003497 /* Cursor line highlighting for 'cursorline' in the current window. Not
3498 * when Visual mode is active, because it's not clear what is selected
3499 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003500 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003501 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003502 {
3503 line_attr = hl_attr(HLF_CUL);
3504 area_highlighting = TRUE;
3505 }
3506#endif
3507
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003508 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 col = 0;
3510#ifdef FEAT_RIGHTLEFT
3511 if (wp->w_p_rl)
3512 {
3513 /* Rightleft window: process the text in the normal direction, but put
3514 * it in current_ScreenLine[] from right to left. Start at the
3515 * rightmost column of the window. */
3516 col = W_WIDTH(wp) - 1;
3517 off += col;
3518 }
3519#endif
3520
3521 /*
3522 * Repeat for the whole displayed line.
3523 */
3524 for (;;)
3525 {
3526 /* Skip this quickly when working on the text. */
3527 if (draw_state != WL_LINE)
3528 {
3529#ifdef FEAT_CMDWIN
3530 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3531 {
3532 draw_state = WL_CMDLINE;
3533 if (cmdwin_type != 0 && wp == curwin)
3534 {
3535 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003537 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 char_attr = hl_attr(HLF_AT);
3539 }
3540 }
3541#endif
3542
3543#ifdef FEAT_FOLDING
3544 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3545 {
3546 draw_state = WL_FOLD;
3547 if (wp->w_p_fdc > 0)
3548 {
3549 /* Draw the 'foldcolumn'. */
3550 fill_foldcolumn(extra, wp, FALSE, lnum);
3551 n_extra = wp->w_p_fdc;
3552 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003553 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 c_extra = NUL;
3555 char_attr = hl_attr(HLF_FC);
3556 }
3557 }
3558#endif
3559
3560#ifdef FEAT_SIGNS
3561 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3562 {
3563 draw_state = WL_SIGN;
3564 /* Show the sign column when there are any signs in this
3565 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003566 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003568 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003570 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571# endif
3572
3573 /* Draw two cells with the sign value or blank. */
3574 c_extra = ' ';
3575 char_attr = hl_attr(HLF_SC);
3576 n_extra = 2;
3577
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003578 if (row == startrow
3579#ifdef FEAT_DIFF
3580 + filler_lines && filler_todo <= 0
3581#endif
3582 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3585 SIGN_TEXT);
3586# ifdef FEAT_SIGN_ICONS
3587 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3588 SIGN_ICON);
3589 if (gui.in_use && icon_sign != 0)
3590 {
3591 /* Use the image in this position. */
3592 c_extra = SIGN_BYTE;
3593# ifdef FEAT_NETBEANS_INTG
3594 if (buf_signcount(wp->w_buffer, lnum) > 1)
3595 c_extra = MULTISIGN_BYTE;
3596# endif
3597 char_attr = icon_sign;
3598 }
3599 else
3600# endif
3601 if (text_sign != 0)
3602 {
3603 p_extra = sign_get_text(text_sign);
3604 if (p_extra != NULL)
3605 {
3606 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003607 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609 char_attr = sign_get_attr(text_sign, FALSE);
3610 }
3611 }
3612 }
3613 }
3614#endif
3615
3616 if (draw_state == WL_NR - 1 && n_extra == 0)
3617 {
3618 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003619 /* Display the absolute or relative line number. After the
3620 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3621 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 && (row == startrow
3623#ifdef FEAT_DIFF
3624 + filler_lines
3625#endif
3626 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3627 {
3628 /* Draw the line number (empty space after wrapping). */
3629 if (row == startrow
3630#ifdef FEAT_DIFF
3631 + filler_lines
3632#endif
3633 )
3634 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003635 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003636 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003637
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003638 if (wp->w_p_nu && !wp->w_p_rnu)
3639 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003640 num = (long)lnum;
3641 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003642 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003643 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003644 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003645 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003646 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003647 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003648 num = lnum;
3649 fmt = "%-*ld ";
3650 }
3651 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003652
Bram Moolenaar700e7342013-01-30 12:31:36 +01003653 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003654 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (wp->w_skipcol > 0)
3656 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3657 *p_extra = '-';
3658#ifdef FEAT_RIGHTLEFT
3659 if (wp->w_p_rl) /* reverse line numbers */
3660 rl_mirror(extra);
3661#endif
3662 p_extra = extra;
3663 c_extra = NUL;
3664 }
3665 else
3666 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003667 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003669#ifdef FEAT_SYN_HL
3670 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003671 * the current line differently.
3672 * TODO: Can we use CursorLine instead of CursorLineNr
3673 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003674 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003675 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003676 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 }
3680
Bram Moolenaar597a4222014-06-25 14:39:50 +02003681#ifdef FEAT_LINEBREAK
3682 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3683 && n_extra == 0 && *p_sbr != NUL)
3684 /* draw indent after showbreak value */
3685 draw_state = WL_BRI;
3686 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3687 /* After the showbreak, draw the breakindent */
3688 draw_state = WL_BRI - 1;
3689
3690 /* draw 'breakindent': indent wrapped text accordingly */
3691 if (draw_state == WL_BRI - 1 && n_extra == 0)
3692 {
3693 draw_state = WL_BRI;
3694# ifdef FEAT_DIFF
3695# endif
3696 if (wp->w_p_bri && n_extra == 0 && row != startrow
3697#ifdef FEAT_DIFF
3698 && filler_lines == 0
3699#endif
3700 )
3701 {
3702 char_attr = 0; /* was: hl_attr(HLF_AT); */
3703#ifdef FEAT_DIFF
3704 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003705 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003706 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003707 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3708 char_attr = hl_combine_attr(char_attr,
3709 hl_attr(HLF_CUL));
3710 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02003711#endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003712 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003713 c_extra = ' ';
3714 n_extra = get_breakindent_win(wp,
3715 ml_get_buf(wp->w_buffer, lnum, FALSE));
3716 /* Correct end of highlighted area for 'breakindent',
3717 * required when 'linebreak' is also set. */
3718 if (tocol == vcol)
3719 tocol += n_extra;
3720 }
3721 }
3722#endif
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3725 if (draw_state == WL_SBR - 1 && n_extra == 0)
3726 {
3727 draw_state = WL_SBR;
3728# ifdef FEAT_DIFF
3729 if (filler_todo > 0)
3730 {
3731 /* Draw "deleted" diff line(s). */
3732 if (char2cells(fill_diff) > 1)
3733 c_extra = '-';
3734 else
3735 c_extra = fill_diff;
3736# ifdef FEAT_RIGHTLEFT
3737 if (wp->w_p_rl)
3738 n_extra = col + 1;
3739 else
3740# endif
3741 n_extra = W_WIDTH(wp) - col;
3742 char_attr = hl_attr(HLF_DED);
3743 }
3744# endif
3745# ifdef FEAT_LINEBREAK
3746 if (*p_sbr != NUL && need_showbreak)
3747 {
3748 /* Draw 'showbreak' at the start of each broken line. */
3749 p_extra = p_sbr;
3750 c_extra = NUL;
3751 n_extra = (int)STRLEN(p_sbr);
3752 char_attr = hl_attr(HLF_AT);
3753 need_showbreak = FALSE;
3754 /* Correct end of highlighted area for 'showbreak',
3755 * required when 'linebreak' is also set. */
3756 if (tocol == vcol)
3757 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003758#ifdef FEAT_SYN_HL
3759 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003760 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003761 char_attr = hl_combine_attr(char_attr,
3762 hl_attr(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765# endif
3766 }
3767#endif
3768
3769 if (draw_state == WL_LINE - 1 && n_extra == 0)
3770 {
3771 draw_state = WL_LINE;
3772 if (saved_n_extra)
3773 {
3774 /* Continue item from end of wrapped line. */
3775 n_extra = saved_n_extra;
3776 c_extra = saved_c_extra;
3777 p_extra = saved_p_extra;
3778 char_attr = saved_char_attr;
3779 }
3780 else
3781 char_attr = 0;
3782 }
3783 }
3784
3785 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003786 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003787 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788#ifdef FEAT_DIFF
3789 && filler_todo <= 0
3790#endif
3791 )
3792 {
3793 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3794 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003795 /* Pretend we have finished updating the window. Except when
3796 * 'cursorcolumn' is set. */
3797#ifdef FEAT_SYN_HL
3798 if (wp->w_p_cuc)
3799 row = wp->w_cline_row + wp->w_cline_height;
3800 else
3801#endif
3802 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 break;
3804 }
3805
3806 if (draw_state == WL_LINE && area_highlighting)
3807 {
3808 /* handle Visual or match highlighting in this line */
3809 if (vcol == fromcol
3810#ifdef FEAT_MBYTE
3811 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3812 && (*mb_ptr2cells)(ptr) > 1)
3813#endif
3814 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003815 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 && vcol < tocol))
3817 area_attr = attr; /* start highlighting */
3818 else if (area_attr != 0
3819 && (vcol == tocol
3820 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822
3823#ifdef FEAT_SEARCH_EXTRA
3824 if (!n_extra)
3825 {
3826 /*
3827 * Check for start/end of search pattern match.
3828 * After end, check for start/end of next match.
3829 * When another match, have to check for start again.
3830 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003831 * Do this for 'search_hl' and the match list (ordered by
3832 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003834 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003835 cur = wp->w_match_head;
3836 shl_flag = FALSE;
3837 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003839 if (shl_flag == FALSE
3840 && ((cur != NULL
3841 && cur->priority > SEARCH_HL_PRIORITY)
3842 || cur == NULL))
3843 {
3844 shl = &search_hl;
3845 shl_flag = TRUE;
3846 }
3847 else
3848 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003849 if (cur != NULL)
3850 cur->pos.cur = 0;
3851 pos_inprogress = TRUE;
3852 while (shl->rm.regprog != NULL
3853 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003855 if (shl->startcol != MAXCOL
3856 && v >= (long)shl->startcol
3857 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 shl->attr_cur = shl->attr;
3860 }
Bram Moolenaar5307de02014-08-16 16:28:36 +02003861 else if (v >= (long)shl->endcol && shl->lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
3863 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003864 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3865 pos_inprogress = cur == NULL || cur->pos.cur == 0
3866 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /* Need to get the line again, a multi-line regexp
3869 * may have made it invalid. */
3870 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3871 ptr = line + v;
3872
3873 if (shl->lnum == lnum)
3874 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003875 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003877 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003879 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003881 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 /* highlight empty match, try again after
3884 * it */
3885#ifdef FEAT_MBYTE
3886 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003887 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003888 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 else
3890#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003891 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893
3894 /* Loop to check if the match starts at the
3895 * current position */
3896 continue;
3897 }
3898 }
3899 break;
3900 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003901 if (shl != &search_hl && cur != NULL)
3902 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003904
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003905 /* Use attributes from match with highest priority among
3906 * 'search_hl' and the match list. */
3907 search_attr = search_hl.attr_cur;
3908 cur = wp->w_match_head;
3909 shl_flag = FALSE;
3910 while (cur != NULL || shl_flag == FALSE)
3911 {
3912 if (shl_flag == FALSE
3913 && ((cur != NULL
3914 && cur->priority > SEARCH_HL_PRIORITY)
3915 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003916 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003917 shl = &search_hl;
3918 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003919 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003920 else
3921 shl = &cur->hl;
3922 if (shl->attr_cur != 0)
3923 search_attr = shl->attr_cur;
3924 if (shl != &search_hl && cur != NULL)
3925 cur = cur->next;
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 }
3928#endif
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003931 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003933 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3934 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003936 if (diff_hlf == HLF_TXD && ptr - line > change_end
3937 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 line_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02003940 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3941 line_attr = hl_combine_attr(line_attr, hl_attr(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003944
3945 /* Decide which of the highlight attributes to use. */
3946 attr_pri = TRUE;
3947 if (area_attr != 0)
3948 char_attr = area_attr;
3949 else if (search_attr != 0)
3950 char_attr = search_attr;
3951#ifdef LINE_ATTR
3952 /* Use line_attr when not in the Visual or 'incsearch' area
3953 * (area_attr may be 0 when "noinvcur" is set). */
3954 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003955 || vcol < fromcol || vcol_prev < fromcol_prev
3956 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003957 char_attr = line_attr;
3958#endif
3959 else
3960 {
3961 attr_pri = FALSE;
3962#ifdef FEAT_SYN_HL
3963 if (has_syntax)
3964 char_attr = syntax_attr;
3965 else
3966#endif
3967 char_attr = 0;
3968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970
3971 /*
3972 * Get the next character to put on the screen.
3973 */
3974 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003975 * The "p_extra" points to the extra stuff that is inserted to
3976 * represent special characters (non-printable stuff) and other
3977 * things. When all characters are the same, c_extra is used.
3978 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3979 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3981 */
3982 if (n_extra > 0)
3983 {
3984 if (c_extra != NUL)
3985 {
3986 c = c_extra;
3987#ifdef FEAT_MBYTE
3988 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3989 if (enc_utf8 && (*mb_char2len)(c) > 1)
3990 {
3991 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003992 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003993 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995 else
3996 mb_utf8 = FALSE;
3997#endif
3998 }
3999 else
4000 {
4001 c = *p_extra;
4002#ifdef FEAT_MBYTE
4003 if (has_mbyte)
4004 {
4005 mb_c = c;
4006 if (enc_utf8)
4007 {
4008 /* If the UTF-8 character is more than one byte:
4009 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004010 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 mb_utf8 = FALSE;
4012 if (mb_l > n_extra)
4013 mb_l = 1;
4014 else if (mb_l > 1)
4015 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004016 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 }
4021 else
4022 {
4023 /* if this is a DBCS character, put it in "mb_c" */
4024 mb_l = MB_BYTE2LEN(c);
4025 if (mb_l >= n_extra)
4026 mb_l = 1;
4027 else if (mb_l > 1)
4028 mb_c = (c << 8) + p_extra[1];
4029 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004030 if (mb_l == 0) /* at the NUL at end-of-line */
4031 mb_l = 1;
4032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 /* If a double-width char doesn't fit display a '>' in the
4034 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004035 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036# ifdef FEAT_RIGHTLEFT
4037 wp->w_p_rl ? (col <= 0) :
4038# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004039 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 && (*mb_char2cells)(mb_c) == 2)
4041 {
4042 c = '>';
4043 mb_c = c;
4044 mb_l = 1;
4045 mb_utf8 = FALSE;
4046 multi_attr = hl_attr(HLF_AT);
4047 /* put the pointer back to output the double-width
4048 * character at the start of the next line. */
4049 ++n_extra;
4050 --p_extra;
4051 }
4052 else
4053 {
4054 n_extra -= mb_l - 1;
4055 p_extra += mb_l - 1;
4056 }
4057 }
4058#endif
4059 ++p_extra;
4060 }
4061 --n_extra;
4062 }
4063 else
4064 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004065 if (p_extra_free != NULL)
4066 {
4067 vim_free(p_extra_free);
4068 p_extra_free = NULL;
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 /*
4071 * Get a character from the line itself.
4072 */
4073 c = *ptr;
4074#ifdef FEAT_MBYTE
4075 if (has_mbyte)
4076 {
4077 mb_c = c;
4078 if (enc_utf8)
4079 {
4080 /* If the UTF-8 character is more than one byte: Decode it
4081 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004082 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 mb_utf8 = FALSE;
4084 if (mb_l > 1)
4085 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004086 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 /* Overlong encoded ASCII or ASCII with composing char
4088 * is displayed normally, except a NUL. */
4089 if (mb_c < 0x80)
4090 c = mb_c;
4091 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004092
4093 /* At start of the line we can have a composing char.
4094 * Draw it as a space with a composing char. */
4095 if (utf_iscomposing(mb_c))
4096 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004097 int i;
4098
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004099 for (i = Screen_mco - 1; i > 0; --i)
4100 u8cc[i] = u8cc[i - 1];
4101 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004102 mb_c = ' ';
4103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
4105
4106 if ((mb_l == 1 && c >= 0x80)
4107 || (mb_l >= 1 && mb_c == 0)
4108 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004109# ifdef UNICODE16
4110 || mb_c >= 0x10000
4111# endif
4112 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
4114 /*
4115 * Illegal UTF-8 byte: display as <xx>.
4116 * Non-BMP character : display as ? or fullwidth ?.
4117 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004118# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004120# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004123# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 if (wp->w_p_rl) /* reverse */
4125 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004128# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 else if (utf_char2cells(mb_c) != 2)
4130 STRCPY(extra, "?");
4131 else
4132 /* 0xff1f in UTF-8: full-width '?' */
4133 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 p_extra = extra;
4137 c = *p_extra;
4138 mb_c = mb_ptr2char_adv(&p_extra);
4139 mb_utf8 = (c >= 0x80);
4140 n_extra = (int)STRLEN(p_extra);
4141 c_extra = NUL;
4142 if (area_attr == 0 && search_attr == 0)
4143 {
4144 n_attr = n_extra + 1;
4145 extra_attr = hl_attr(HLF_8);
4146 saved_attr2 = char_attr; /* save current attr */
4147 }
4148 }
4149 else if (mb_l == 0) /* at the NUL at end-of-line */
4150 mb_l = 1;
4151#ifdef FEAT_ARABIC
4152 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4153 {
4154 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004155 int pc, pc1, nc;
4156 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 /* The idea of what is the previous and next
4159 * character depends on 'rightleft'. */
4160 if (wp->w_p_rl)
4161 {
4162 pc = prev_c;
4163 pc1 = prev_c1;
4164 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004165 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
4167 else
4168 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004169 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004171 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 prev_c = mb_c;
4174
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004175 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177 else
4178 prev_c = mb_c;
4179#endif
4180 }
4181 else /* enc_dbcs */
4182 {
4183 mb_l = MB_BYTE2LEN(c);
4184 if (mb_l == 0) /* at the NUL at end-of-line */
4185 mb_l = 1;
4186 else if (mb_l > 1)
4187 {
4188 /* We assume a second byte below 32 is illegal.
4189 * Hopefully this is OK for all double-byte encodings!
4190 */
4191 if (ptr[1] >= 32)
4192 mb_c = (c << 8) + ptr[1];
4193 else
4194 {
4195 if (ptr[1] == NUL)
4196 {
4197 /* head byte at end of line */
4198 mb_l = 1;
4199 transchar_nonprint(extra, c);
4200 }
4201 else
4202 {
4203 /* illegal tail byte */
4204 mb_l = 2;
4205 STRCPY(extra, "XX");
4206 }
4207 p_extra = extra;
4208 n_extra = (int)STRLEN(extra) - 1;
4209 c_extra = NUL;
4210 c = *p_extra++;
4211 if (area_attr == 0 && search_attr == 0)
4212 {
4213 n_attr = n_extra + 1;
4214 extra_attr = hl_attr(HLF_8);
4215 saved_attr2 = char_attr; /* save current attr */
4216 }
4217 mb_c = c;
4218 }
4219 }
4220 }
4221 /* If a double-width char doesn't fit display a '>' in the
4222 * last column; the character is displayed at the start of the
4223 * next line. */
4224 if ((
4225# ifdef FEAT_RIGHTLEFT
4226 wp->w_p_rl ? (col <= 0) :
4227# endif
4228 (col >= W_WIDTH(wp) - 1))
4229 && (*mb_char2cells)(mb_c) == 2)
4230 {
4231 c = '>';
4232 mb_c = c;
4233 mb_utf8 = FALSE;
4234 mb_l = 1;
4235 multi_attr = hl_attr(HLF_AT);
4236 /* Put pointer back so that the character will be
4237 * displayed at the start of the next line. */
4238 --ptr;
4239 }
4240 else if (*ptr != NUL)
4241 ptr += mb_l - 1;
4242
4243 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004244 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004245 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004246 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004249 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 c = ' ';
4251 if (area_attr == 0 && search_attr == 0)
4252 {
4253 n_attr = n_extra + 1;
4254 extra_attr = hl_attr(HLF_AT);
4255 saved_attr2 = char_attr; /* save current attr */
4256 }
4257 mb_c = c;
4258 mb_utf8 = FALSE;
4259 mb_l = 1;
4260 }
4261
4262 }
4263#endif
4264 ++ptr;
4265
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004266 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004267 if (wp->w_p_list && (c == 160
4268#ifdef FEAT_MBYTE
4269 || (mb_utf8 && mb_c == 160)
4270#endif
4271 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004272 {
4273 c = lcs_nbsp;
4274 if (area_attr == 0 && search_attr == 0)
4275 {
4276 n_attr = 1;
4277 extra_attr = hl_attr(HLF_8);
4278 saved_attr2 = char_attr; /* save current attr */
4279 }
4280#ifdef FEAT_MBYTE
4281 mb_c = c;
4282 if (enc_utf8 && (*mb_char2len)(c) > 1)
4283 {
4284 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004285 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004286 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004287 }
4288 else
4289 mb_utf8 = FALSE;
4290#endif
4291 }
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (extra_check)
4294 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004295#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004296 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004297#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004298
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004299#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 /* Get syntax attribute, unless still at the start of the line
4301 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004302 v = (long)(ptr - line);
4303 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 {
4305 /* Get the syntax attribute for the character. If there
4306 * is an error, disable syntax highlighting. */
4307 save_did_emsg = did_emsg;
4308 did_emsg = FALSE;
4309
Bram Moolenaar217ad922005-03-20 22:37:15 +00004310 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004311# ifdef FEAT_SPELL
4312 has_spell ? &can_spell :
4313# endif
4314 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004317 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004318 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004319 has_syntax = FALSE;
4320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 else
4322 did_emsg = save_did_emsg;
4323
4324 /* Need to get the line again, a multi-line regexp may
4325 * have made it invalid. */
4326 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4327 ptr = line + v;
4328
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004329 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004331 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004332 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004333# ifdef FEAT_CONCEAL
4334 /* no concealing past the end of the line, it interferes
4335 * with line highlighting */
4336 if (c == NUL)
4337 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004338 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004339 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004342#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004343
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004344#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004345 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004346 * Only do this when there is no syntax highlighting, the
4347 * @Spell cluster is not used or the current syntax item
4348 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004349 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004350 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004351 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004352# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004353 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004354 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004355# endif
4356 if (c != 0 && (
4357# ifdef FEAT_SYN_HL
4358 !has_syntax ||
4359# endif
4360 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004361 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004362 char_u *prev_ptr, *p;
4363 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004364 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004365# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004366 if (has_mbyte)
4367 {
4368 prev_ptr = ptr - mb_l;
4369 v -= mb_l - 1;
4370 }
4371 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004372# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004373 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004374
4375 /* Use nextline[] if possible, it has the start of the
4376 * next line concatenated. */
4377 if ((prev_ptr - line) - nextlinecol >= 0)
4378 p = nextline + (prev_ptr - line) - nextlinecol;
4379 else
4380 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004381 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004382 len = spell_check(wp, p, &spell_hlf, &cap_col,
4383 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004384 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004385
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004386 /* In Insert mode only highlight a word that
4387 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004388 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004389 && (State & INSERT) != 0
4390 && wp->w_cursor.lnum == lnum
4391 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004393 && wp->w_cursor.col < (colnr_T)word_end)
4394 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004395 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004396 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004397 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004398
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004399 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004400 && (p - nextline) + len > nextline_idx)
4401 {
4402 /* Remember that the good word continues at the
4403 * start of the next line. */
4404 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004405 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004406 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004407
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004408 /* Turn index into actual attributes. */
4409 if (spell_hlf != HLF_COUNT)
4410 spell_attr = highlight_attr[spell_hlf];
4411
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004412 if (cap_col > 0)
4413 {
4414 if (p != prev_ptr
4415 && (p - nextline) + cap_col >= nextline_idx)
4416 {
4417 /* Remember that the word in the next line
4418 * must start with a capital. */
4419 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004420 cap_col = (int)((p - nextline) + cap_col
4421 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004422 }
4423 else
4424 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004425 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004426 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004427 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004428 }
4429 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004430 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004431 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004432 char_attr = hl_combine_attr(char_attr, spell_attr);
4433 else
4434 char_attr = hl_combine_attr(spell_attr, char_attr);
4435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#endif
4437#ifdef FEAT_LINEBREAK
4438 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004439 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004441 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02004443 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444# ifdef FEAT_MBYTE
4445 has_mbyte ? mb_l :
4446# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004447 1);
4448 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004449 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004450 NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 c_extra = ' ';
4452 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004453 {
4454#ifdef FEAT_CONCEAL
4455 if (c == TAB)
4456 /* See "Tab alignment" below. */
4457 FIX_FOR_BOGUSCOLS;
4458#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004459 if (!wp->w_p_list)
4460 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463#endif
4464
4465 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4466 {
4467 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004468 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 n_attr = 1;
4471 extra_attr = hl_attr(HLF_8);
4472 saved_attr2 = char_attr; /* save current attr */
4473 }
4474#ifdef FEAT_MBYTE
4475 mb_c = c;
4476 if (enc_utf8 && (*mb_char2len)(c) > 1)
4477 {
4478 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004479 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004480 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482 else
4483 mb_utf8 = FALSE;
4484#endif
4485 }
4486 }
4487
4488 /*
4489 * Handling of non-printable characters.
4490 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004491 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 /*
4494 * when getting a character from the file, we may have to
4495 * turn it into something else on the way to putting it
4496 * into "ScreenLines".
4497 */
4498 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4499 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004500 int tab_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 /* tab amount depends on current column */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004502 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004503 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004504#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004505 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004506#endif
4507 /* tab amount depends on current column */
4508 n_extra = tab_len;
4509#ifdef FEAT_LINEBREAK
4510 else
4511 {
4512 char_u *p;
4513 int len = n_extra;
4514 int i;
4515 int saved_nextra = n_extra;
4516
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004517#ifdef FEAT_CONCEAL
4518 if (is_concealing && vcol_off > 0)
4519 /* there are characters to conceal */
4520 tab_len += vcol_off;
4521#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004522 /* if n_extra > 0, it gives the number of chars, to
4523 * use for a tab, else we need to calculate the width
4524 * for a tab */
4525#ifdef FEAT_MBYTE
4526 len = (tab_len * mb_char2len(lcs_tab2));
4527 if (n_extra > 0)
4528 len += n_extra - tab_len;
4529#endif
4530 c = lcs_tab1;
4531 p = alloc((unsigned)(len + 1));
4532 vim_memset(p, ' ', len);
4533 p[len] = NUL;
4534 p_extra_free = p;
4535 for (i = 0; i < tab_len; i++)
4536 {
4537#ifdef FEAT_MBYTE
4538 mb_char2bytes(lcs_tab2, p);
4539 p += mb_char2len(lcs_tab2);
4540 n_extra += mb_char2len(lcs_tab2)
4541 - (saved_nextra > 0 ? 1 : 0);
4542#else
4543 p[i] = lcs_tab2;
4544#endif
4545 }
4546 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004547#ifdef FEAT_CONCEAL
4548 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4549 * macro below, so need to adjust for that here */
4550 if (is_concealing && vcol_off > 0)
4551 n_extra -= vcol_off;
4552#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004553 }
4554#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004555#ifdef FEAT_CONCEAL
4556 /* Tab alignment should be identical regardless of
4557 * 'conceallevel' value. So tab compensates of all
4558 * previous concealed characters, and thus resets vcol_off
4559 * and boguscols accumulated so far in the line. Note that
4560 * the tab can be longer than 'tabstop' when there
4561 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004562 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#ifdef FEAT_MBYTE
4565 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4566#endif
4567 if (wp->w_p_list)
4568 {
4569 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004570#ifdef FEAT_LINEBREAK
4571 if (wp->w_p_lbr)
4572 c_extra = NUL; /* using p_extra from above */
4573 else
4574#endif
4575 c_extra = lcs_tab2;
4576 n_attr = tab_len + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 extra_attr = hl_attr(HLF_8);
4578 saved_attr2 = char_attr; /* save current attr */
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#endif
4588 }
4589 else
4590 {
4591 c_extra = ' ';
4592 c = ' ';
4593 }
4594 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004595 else if (c == NUL
4596 && ((wp->w_p_list && lcs_eol > 0)
4597 || ((fromcol >= 0 || fromcol_prev >= 0)
4598 && tocol > vcol
4599 && VIsual_mode != Ctrl_V
4600 && (
4601# ifdef FEAT_RIGHTLEFT
4602 wp->w_p_rl ? (col >= 0) :
4603# endif
4604 (col < W_WIDTH(wp)))
4605 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004606 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004607 && (colnr_T)vcol == wp->w_virtcol)))
4608 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004610 /* Display a '$' after the line or highlight an extra
4611 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4613 /* For a diff line the highlighting continues after the
4614 * "$". */
4615 if (
4616# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004617 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618# ifdef LINE_ATTR
4619 &&
4620# endif
4621# endif
4622# ifdef LINE_ATTR
4623 line_attr == 0
4624# endif
4625 )
4626#endif
4627 {
4628#ifdef FEAT_VIRTUALEDIT
4629 /* In virtualedit, visual selections may extend
4630 * beyond end of line. */
4631 if (area_highlighting && virtual_active()
4632 && tocol != MAXCOL && vcol < tocol)
4633 n_extra = 0;
4634 else
4635#endif
4636 {
4637 p_extra = at_end_str;
4638 n_extra = 1;
4639 c_extra = NUL;
4640 }
4641 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004642 if (wp->w_p_list)
4643 c = lcs_eol;
4644 else
4645 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 lcs_eol_one = -1;
4647 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004648 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
4650 extra_attr = hl_attr(HLF_AT);
4651 n_attr = 1;
4652 }
4653#ifdef FEAT_MBYTE
4654 mb_c = c;
4655 if (enc_utf8 && (*mb_char2len)(c) > 1)
4656 {
4657 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004658 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004659 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
4661 else
4662 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4663#endif
4664 }
4665 else if (c != NUL)
4666 {
4667 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02004668 if (n_extra == 0)
4669 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#ifdef FEAT_RIGHTLEFT
4671 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4672 rl_mirror(p_extra); /* reverse "<12>" */
4673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004675#ifdef FEAT_LINEBREAK
4676 if (wp->w_p_lbr)
4677 {
4678 char_u *p;
4679
4680 c = *p_extra;
4681 p = alloc((unsigned)n_extra + 1);
4682 vim_memset(p, ' ', n_extra);
4683 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
4684 p[n_extra] = NUL;
4685 p_extra_free = p_extra = p;
4686 }
4687 else
4688#endif
4689 {
4690 n_extra = byte2cells(c) - 1;
4691 c = *p_extra++;
4692 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004693 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 n_attr = n_extra + 1;
4696 extra_attr = hl_attr(HLF_8);
4697 saved_attr2 = char_attr; /* save current attr */
4698 }
4699#ifdef FEAT_MBYTE
4700 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4701#endif
4702 }
4703#ifdef FEAT_VIRTUALEDIT
4704 else if (VIsual_active
4705 && (VIsual_mode == Ctrl_V
4706 || VIsual_mode == 'v')
4707 && virtual_active()
4708 && tocol != MAXCOL
4709 && vcol < tocol
4710 && (
4711# ifdef FEAT_RIGHTLEFT
4712 wp->w_p_rl ? (col >= 0) :
4713# endif
4714 (col < W_WIDTH(wp))))
4715 {
4716 c = ' ';
4717 --ptr; /* put it back at the NUL */
4718 }
4719#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004720#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 else if ((
4722# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004723 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 ) && (
4727# ifdef FEAT_RIGHTLEFT
4728 wp->w_p_rl ? (col >= 0) :
4729# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004730 (col
4731# ifdef FEAT_CONCEAL
4732 - boguscols
4733# endif
4734 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 /* Highlight until the right side of the window */
4737 c = ' ';
4738 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004739
4740 /* Remember we do the char for line highlighting. */
4741 ++did_line_attr;
4742
4743 /* don't do search HL for the rest of the line */
4744 if (line_attr != 0 && char_attr == search_attr && col > 0)
4745 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746# ifdef FEAT_DIFF
4747 if (diff_hlf == HLF_TXD)
4748 {
4749 diff_hlf = HLF_CHD;
4750 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 char_attr = hl_attr(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004753 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4754 char_attr = hl_combine_attr(char_attr,
4755 hl_attr(HLF_CUL));
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758# endif
4759 }
4760#endif
4761 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004762
4763#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004764 if ( wp->w_p_cole > 0
4765 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4766 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004767 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004768 && !(lnum_in_visual_area
4769 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770 {
4771 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004772 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004773 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4774 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004775 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004776 /* First time at this concealed item: display one
4777 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778 if (syn_get_sub_char() != NUL)
4779 c = syn_get_sub_char();
4780 else if (lcs_conceal != NUL)
4781 c = lcs_conceal;
4782 else
4783 c = ' ';
4784
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004785 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004786
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004787 if (n_extra > 0)
4788 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004789 vcol += n_extra;
4790 if (wp->w_p_wrap && n_extra > 0)
4791 {
4792# ifdef FEAT_RIGHTLEFT
4793 if (wp->w_p_rl)
4794 {
4795 col -= n_extra;
4796 boguscols -= n_extra;
4797 }
4798 else
4799# endif
4800 {
4801 boguscols += n_extra;
4802 col += n_extra;
4803 }
4804 }
4805 n_extra = 0;
4806 n_attr = 0;
4807 }
4808 else if (n_skip == 0)
4809 {
4810 is_concealing = TRUE;
4811 n_skip = 1;
4812 }
4813# ifdef FEAT_MBYTE
4814 mb_c = c;
4815 if (enc_utf8 && (*mb_char2len)(c) > 1)
4816 {
4817 mb_utf8 = TRUE;
4818 u8cc[0] = 0;
4819 c = 0xc0;
4820 }
4821 else
4822 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4823# endif
4824 }
4825 else
4826 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004827 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004828 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004829 }
4830#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004833#ifdef FEAT_CONCEAL
4834 /* In the cursor line and we may be concealing characters: correct
4835 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004836 if (!did_wcol && draw_state == WL_LINE
4837 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004838 && conceal_cursor_line(wp)
4839 && (int)wp->w_virtcol <= vcol + n_skip)
4840 {
4841 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004842 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004843 did_wcol = TRUE;
4844 }
4845#endif
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 /* Don't override visual selection highlighting. */
4848 if (n_attr > 0
4849 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004850 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 char_attr = extra_attr;
4852
Bram Moolenaar81695252004-12-29 20:58:21 +00004853#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 /* XIM don't send preedit_start and preedit_end, but they send
4855 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4856 * im_is_preediting() here. */
4857 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004858 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 && (State & INSERT)
4860 && !p_imdisable
4861 && im_is_preediting()
4862 && draw_state == WL_LINE)
4863 {
4864 colnr_T tcol;
4865
4866 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004867 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 else
4869 tcol = preedit_end_col;
4870 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4871 {
4872 if (feedback_old_attr < 0)
4873 {
4874 feedback_col = 0;
4875 feedback_old_attr = char_attr;
4876 }
4877 char_attr = im_get_feedback_attr(feedback_col);
4878 if (char_attr < 0)
4879 char_attr = feedback_old_attr;
4880 feedback_col++;
4881 }
4882 else if (feedback_old_attr >= 0)
4883 {
4884 char_attr = feedback_old_attr;
4885 feedback_old_attr = -1;
4886 feedback_col = 0;
4887 }
4888 }
4889#endif
4890 /*
4891 * Handle the case where we are in column 0 but not on the first
4892 * character of the line and the user wants us to show us a
4893 * special character (via 'listchars' option "precedes:<char>".
4894 */
4895 if (lcs_prec_todo != NUL
4896 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4897#ifdef FEAT_DIFF
4898 && filler_todo <= 0
4899#endif
4900 && draw_state > WL_NR
4901 && c != NUL)
4902 {
4903 c = lcs_prec;
4904 lcs_prec_todo = NUL;
4905#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004906 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4907 {
4908 /* Double-width character being overwritten by the "precedes"
4909 * character, need to fill up half the character. */
4910 c_extra = MB_FILLER_CHAR;
4911 n_extra = 1;
4912 n_attr = 2;
4913 extra_attr = hl_attr(HLF_AT);
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 mb_c = c;
4916 if (enc_utf8 && (*mb_char2len)(c) > 1)
4917 {
4918 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004919 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004920 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 else
4923 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4924#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004925 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 saved_attr3 = char_attr; /* save current attr */
4928 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4929 n_attr3 = 1;
4930 }
4931 }
4932
4933 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004934 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004936 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004937#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004938 || did_line_attr == 1
4939#endif
4940 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004942#ifdef FEAT_SEARCH_EXTRA
4943 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004944
4945 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004946 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004947 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004948#endif
4949
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004950 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 * highlight match at end of line. If it's beyond the last
4952 * char on the screen, just overwrite that one (tricky!) Not
4953 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004954#ifdef FEAT_SEARCH_EXTRA
4955 prevcol_hl_flag = FALSE;
4956 if (prevcol == (long)search_hl.startcol)
4957 prevcol_hl_flag = TRUE;
4958 else
4959 {
4960 cur = wp->w_match_head;
4961 while (cur != NULL)
4962 {
4963 if (prevcol == (long)cur->hl.startcol)
4964 {
4965 prevcol_hl_flag = TRUE;
4966 break;
4967 }
4968 cur = cur->next;
4969 }
4970 }
4971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004973 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004974 && (VIsual_mode != Ctrl_V
4975 || lnum == VIsual.lnum
4976 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004977 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978#ifdef FEAT_SEARCH_EXTRA
4979 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004980 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004981# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004982 && did_line_attr <= 1
4983# endif
4984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
4986 ))
4987 {
4988 int n = 0;
4989
4990#ifdef FEAT_RIGHTLEFT
4991 if (wp->w_p_rl)
4992 {
4993 if (col < 0)
4994 n = 1;
4995 }
4996 else
4997#endif
4998 {
4999 if (col >= W_WIDTH(wp))
5000 n = -1;
5001 }
5002 if (n != 0)
5003 {
5004 /* At the window boundary, highlight the last character
5005 * instead (better than nothing). */
5006 off += n;
5007 col += n;
5008 }
5009 else
5010 {
5011 /* Add a blank character to highlight. */
5012 ScreenLines[off] = ' ';
5013#ifdef FEAT_MBYTE
5014 if (enc_utf8)
5015 ScreenLinesUC[off] = 0;
5016#endif
5017 }
5018#ifdef FEAT_SEARCH_EXTRA
5019 if (area_attr == 0)
5020 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005021 /* Use attributes from match with highest priority among
5022 * 'search_hl' and the match list. */
5023 char_attr = search_hl.attr;
5024 cur = wp->w_match_head;
5025 shl_flag = FALSE;
5026 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005027 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005028 if (shl_flag == FALSE
5029 && ((cur != NULL
5030 && cur->priority > SEARCH_HL_PRIORITY)
5031 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005032 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005033 shl = &search_hl;
5034 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005035 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005036 else
5037 shl = &cur->hl;
5038 if ((ptr - line) - 1 == (long)shl->startcol)
5039 char_attr = shl->attr;
5040 if (shl != &search_hl && cur != NULL)
5041 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044#endif
5045 ScreenAttrs[off] = char_attr;
5046#ifdef FEAT_RIGHTLEFT
5047 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005050 --off;
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else
5053#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005056 ++off;
5057 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005058 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005059#ifdef FEAT_SYN_HL
5060 eol_hl_off = 1;
5061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar91170f82006-05-05 21:15:17 +00005065 /*
5066 * At end of the text line.
5067 */
5068 if (c == NUL)
5069 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005070#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005071 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5072 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005073 {
5074 /* highlight last char after line */
5075 --col;
5076 --off;
5077 --vcol;
5078 }
5079
Bram Moolenaar1a384422010-07-14 19:53:30 +02005080 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005081 if (wp->w_p_wrap)
5082 v = wp->w_skipcol;
5083 else
5084 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005085
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005086 /* check if line ends before left margin */
5087 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005088 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005089#ifdef FEAT_CONCEAL
5090 /* Get rid of the boguscols now, we want to draw until the right
5091 * edge for 'cursorcolumn'. */
5092 col -= boguscols;
5093 boguscols = 0;
5094#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005095
5096 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005097 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005098
5099 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005100 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5101 && (int)wp->w_virtcol <
5102 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005103 && lnum != wp->w_cursor.lnum)
5104 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005105# ifdef FEAT_RIGHTLEFT
5106 && !wp->w_p_rl
5107# endif
5108 )
5109 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005110 int rightmost_vcol = 0;
5111 int i;
5112
5113 if (wp->w_p_cuc)
5114 rightmost_vcol = wp->w_virtcol;
5115 if (draw_color_col)
5116 /* determine rightmost colorcolumn to possibly draw */
5117 for (i = 0; color_cols[i] >= 0; ++i)
5118 if (rightmost_vcol < color_cols[i])
5119 rightmost_vcol = color_cols[i];
5120
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005121 while (col < W_WIDTH(wp))
5122 {
5123 ScreenLines[off] = ' ';
5124#ifdef FEAT_MBYTE
5125 if (enc_utf8)
5126 ScreenLinesUC[off] = 0;
5127#endif
5128 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005129 if (draw_color_col)
5130 draw_color_col = advance_color_col(VCOL_HLC,
5131 &color_cols);
5132
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005133 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005134 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005135 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005136 ScreenAttrs[off++] = hl_attr(HLF_MC);
5137 else
5138 ScreenAttrs[off++] = 0;
5139
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005140 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005141 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005142
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005143 ++vcol;
5144 }
5145 }
5146#endif
5147
Bram Moolenaar860cae12010-06-05 23:22:07 +02005148 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5149 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 row++;
5151
5152 /*
5153 * Update w_cline_height and w_cline_folded if the cursor line was
5154 * updated (saves a call to plines() later).
5155 */
5156 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5157 {
5158 curwin->w_cline_row = startrow;
5159 curwin->w_cline_height = row - startrow;
5160#ifdef FEAT_FOLDING
5161 curwin->w_cline_folded = FALSE;
5162#endif
5163 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5164 }
5165
5166 break;
5167 }
5168
5169 /* line continues beyond line end */
5170 if (lcs_ext
5171 && !wp->w_p_wrap
5172#ifdef FEAT_DIFF
5173 && filler_todo <= 0
5174#endif
5175 && (
5176#ifdef FEAT_RIGHTLEFT
5177 wp->w_p_rl ? col == 0 :
5178#endif
5179 col == W_WIDTH(wp) - 1)
5180 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005181 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5183 {
5184 c = lcs_ext;
5185 char_attr = hl_attr(HLF_AT);
5186#ifdef FEAT_MBYTE
5187 mb_c = c;
5188 if (enc_utf8 && (*mb_char2len)(c) > 1)
5189 {
5190 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005191 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005192 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 else
5195 mb_utf8 = FALSE;
5196#endif
5197 }
5198
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005199#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005200 /* advance to the next 'colorcolumn' */
5201 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005202 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005203
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005204 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005205 * highlight the cursor position itself.
5206 * Also highlight the 'colorcolumn' if it is different than
5207 * 'cursorcolumn' */
5208 vcol_save_attr = -1;
5209 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005210 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005211 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005212 && lnum != wp->w_cursor.lnum)
5213 {
5214 vcol_save_attr = char_attr;
5215 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5216 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005217 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005218 {
5219 vcol_save_attr = char_attr;
5220 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5221 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005222 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005223#endif
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 /*
5226 * Store character to be displayed.
5227 * Skip characters that are left of the screen for 'nowrap'.
5228 */
5229 vcol_prev = vcol;
5230 if (draw_state < WL_LINE || n_skip <= 0)
5231 {
5232 /*
5233 * Store the character.
5234 */
5235#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5236 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5237 {
5238 /* A double-wide character is: put first halve in left cell. */
5239 --off;
5240 --col;
5241 }
5242#endif
5243 ScreenLines[off] = c;
5244#ifdef FEAT_MBYTE
5245 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005246 {
5247 if ((mb_c & 0xff00) == 0x8e00)
5248 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 else if (enc_utf8)
5252 {
5253 if (mb_utf8)
5254 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005255 int i;
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005258 if ((c & 0xff) == 0)
5259 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005260 for (i = 0; i < Screen_mco; ++i)
5261 {
5262 ScreenLinesC[i][off] = u8cc[i];
5263 if (u8cc[i] == 0)
5264 break;
5265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 else
5268 ScreenLinesUC[off] = 0;
5269 }
5270 if (multi_attr)
5271 {
5272 ScreenAttrs[off] = multi_attr;
5273 multi_attr = 0;
5274 }
5275 else
5276#endif
5277 ScreenAttrs[off] = char_attr;
5278
5279#ifdef FEAT_MBYTE
5280 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5281 {
5282 /* Need to fill two screen columns. */
5283 ++off;
5284 ++col;
5285 if (enc_utf8)
5286 /* UTF-8: Put a 0 in the second screen char. */
5287 ScreenLines[off] = 0;
5288 else
5289 /* DBCS: Put second byte in the second screen char. */
5290 ScreenLines[off] = mb_c & 0xff;
5291 ++vcol;
5292 /* When "tocol" is halfway a character, set it to the end of
5293 * the character, otherwise highlighting won't stop. */
5294 if (tocol == vcol)
5295 ++tocol;
5296#ifdef FEAT_RIGHTLEFT
5297 if (wp->w_p_rl)
5298 {
5299 /* now it's time to backup one cell */
5300 --off;
5301 --col;
5302 }
5303#endif
5304 }
5305#endif
5306#ifdef FEAT_RIGHTLEFT
5307 if (wp->w_p_rl)
5308 {
5309 --off;
5310 --col;
5311 }
5312 else
5313#endif
5314 {
5315 ++off;
5316 ++col;
5317 }
5318 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005319#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005320 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 {
5322 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005323 ++vcol_off;
5324 if (n_extra > 0)
5325 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005326 if (wp->w_p_wrap)
5327 {
5328 /*
5329 * Special voodoo required if 'wrap' is on.
5330 *
5331 * Advance the column indicator to force the line
5332 * drawing to wrap early. This will make the line
5333 * take up the same screen space when parts are concealed,
5334 * so that cursor line computations aren't messed up.
5335 *
5336 * To avoid the fictitious advance of 'col' causing
5337 * trailing junk to be written out of the screen line
5338 * we are building, 'boguscols' keeps track of the number
5339 * of bad columns we have advanced.
5340 */
5341 if (n_extra > 0)
5342 {
5343 vcol += n_extra;
5344# ifdef FEAT_RIGHTLEFT
5345 if (wp->w_p_rl)
5346 {
5347 col -= n_extra;
5348 boguscols -= n_extra;
5349 }
5350 else
5351# endif
5352 {
5353 col += n_extra;
5354 boguscols += n_extra;
5355 }
5356 n_extra = 0;
5357 n_attr = 0;
5358 }
5359
5360
5361# ifdef FEAT_MBYTE
5362 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5363 {
5364 /* Need to fill two screen columns. */
5365# ifdef FEAT_RIGHTLEFT
5366 if (wp->w_p_rl)
5367 {
5368 --boguscols;
5369 --col;
5370 }
5371 else
5372# endif
5373 {
5374 ++boguscols;
5375 ++col;
5376 }
5377 }
5378# endif
5379
5380# ifdef FEAT_RIGHTLEFT
5381 if (wp->w_p_rl)
5382 {
5383 --boguscols;
5384 --col;
5385 }
5386 else
5387# endif
5388 {
5389 ++boguscols;
5390 ++col;
5391 }
5392 }
5393 else
5394 {
5395 if (n_extra > 0)
5396 {
5397 vcol += n_extra;
5398 n_extra = 0;
5399 n_attr = 0;
5400 }
5401 }
5402
5403 }
5404#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 else
5406 --n_skip;
5407
Bram Moolenaar64486672010-05-16 15:46:46 +02005408 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5409 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005410 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411#ifdef FEAT_DIFF
5412 && filler_todo <= 0
5413#endif
5414 )
5415 ++vcol;
5416
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005417#ifdef FEAT_SYN_HL
5418 if (vcol_save_attr >= 0)
5419 char_attr = vcol_save_attr;
5420#endif
5421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 /* restore attributes after "predeces" in 'listchars' */
5423 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5424 char_attr = saved_attr3;
5425
5426 /* restore attributes after last 'listchars' or 'number' char */
5427 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5428 char_attr = saved_attr2;
5429
5430 /*
5431 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005432 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 */
5434 if ((
5435#ifdef FEAT_RIGHTLEFT
5436 wp->w_p_rl ? (col < 0) :
5437#endif
5438 (col >= W_WIDTH(wp)))
5439 && (*ptr != NUL
5440#ifdef FEAT_DIFF
5441 || filler_todo > 0
5442#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005443 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5445 )
5446 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005447#ifdef FEAT_CONCEAL
5448 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5449 (int)W_WIDTH(wp), wp->w_p_rl);
5450 boguscols = 0;
5451#else
5452 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5453 (int)W_WIDTH(wp), wp->w_p_rl);
5454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 ++row;
5456 ++screen_row;
5457
5458 /* When not wrapping and finished diff lines, or when displayed
5459 * '$' and highlighting until last column, break here. */
5460 if ((!wp->w_p_wrap
5461#ifdef FEAT_DIFF
5462 && filler_todo <= 0
5463#endif
5464 ) || lcs_eol_one == -1)
5465 break;
5466
5467 /* When the window is too narrow draw all "@" lines. */
5468 if (draw_state != WL_LINE
5469#ifdef FEAT_DIFF
5470 && filler_todo <= 0
5471#endif
5472 )
5473 {
5474 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5475#ifdef FEAT_VERTSPLIT
5476 draw_vsep_win(wp, row);
5477#endif
5478 row = endrow;
5479 }
5480
5481 /* When line got too long for screen break here. */
5482 if (row == endrow)
5483 {
5484 ++row;
5485 break;
5486 }
5487
5488 if (screen_cur_row == screen_row - 1
5489#ifdef FEAT_DIFF
5490 && filler_todo <= 0
5491#endif
5492 && W_WIDTH(wp) == Columns)
5493 {
5494 /* Remember that the line wraps, used for modeless copy. */
5495 LineWraps[screen_row - 1] = TRUE;
5496
5497 /*
5498 * Special trick to make copy/paste of wrapped lines work with
5499 * xterm/screen: write an extra character beyond the end of
5500 * the line. This will work with all terminal types
5501 * (regardless of the xn,am settings).
5502 * Only do this on a fast tty.
5503 * Only do this if the cursor is on the current line
5504 * (something has been written in it).
5505 * Don't do this for the GUI.
5506 * Don't do this for double-width characters.
5507 * Don't do this for a window not at the right screen border.
5508 */
5509 if (p_tf
5510#ifdef FEAT_GUI
5511 && !gui.in_use
5512#endif
5513#ifdef FEAT_MBYTE
5514 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005515 && ((*mb_off2cells)(LineOffset[screen_row],
5516 LineOffset[screen_row] + screen_Columns)
5517 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005519 + (int)Columns - 2,
5520 LineOffset[screen_row] + screen_Columns)
5521 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522#endif
5523 )
5524 {
5525 /* First make sure we are at the end of the screen line,
5526 * then output the same character again to let the
5527 * terminal know about the wrap. If the terminal doesn't
5528 * auto-wrap, we overwrite the character. */
5529 if (screen_cur_col != W_WIDTH(wp))
5530 screen_char(LineOffset[screen_row - 1]
5531 + (unsigned)Columns - 1,
5532 screen_row - 1, (int)(Columns - 1));
5533
5534#ifdef FEAT_MBYTE
5535 /* When there is a multi-byte character, just output a
5536 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005537 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5538 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 out_char(' ');
5540 else
5541#endif
5542 out_char(ScreenLines[LineOffset[screen_row - 1]
5543 + (Columns - 1)]);
5544 /* force a redraw of the first char on the next line */
5545 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5546 screen_start(); /* don't know where cursor is now */
5547 }
5548 }
5549
5550 col = 0;
5551 off = (unsigned)(current_ScreenLine - ScreenLines);
5552#ifdef FEAT_RIGHTLEFT
5553 if (wp->w_p_rl)
5554 {
5555 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5556 off += col;
5557 }
5558#endif
5559
5560 /* reset the drawing state for the start of a wrapped line */
5561 draw_state = WL_START;
5562 saved_n_extra = n_extra;
5563 saved_p_extra = p_extra;
5564 saved_c_extra = c_extra;
5565 saved_char_attr = char_attr;
5566 n_extra = 0;
5567 lcs_prec_todo = lcs_prec;
5568#ifdef FEAT_LINEBREAK
5569# ifdef FEAT_DIFF
5570 if (filler_todo <= 0)
5571# endif
5572 need_showbreak = TRUE;
5573#endif
5574#ifdef FEAT_DIFF
5575 --filler_todo;
5576 /* When the filler lines are actually below the last line of the
5577 * file, don't draw the line itself, break here. */
5578 if (filler_todo == 0 && wp->w_botfill)
5579 break;
5580#endif
5581 }
5582
5583 } /* for every character in the line */
5584
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005585#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005586 /* After an empty line check first word for capital. */
5587 if (*skipwhite(line) == NUL)
5588 {
5589 capcol_lnum = lnum + 1;
5590 cap_col = 0;
5591 }
5592#endif
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 return row;
5595}
5596
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005597#ifdef FEAT_MBYTE
5598static int comp_char_differs __ARGS((int, int));
5599
5600/*
5601 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005602 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005603 */
5604 static int
5605comp_char_differs(off_from, off_to)
5606 int off_from;
5607 int off_to;
5608{
5609 int i;
5610
5611 for (i = 0; i < Screen_mco; ++i)
5612 {
5613 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5614 return TRUE;
5615 if (ScreenLinesC[i][off_from] == 0)
5616 break;
5617 }
5618 return FALSE;
5619}
5620#endif
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622/*
5623 * Check whether the given character needs redrawing:
5624 * - the (first byte of the) character is different
5625 * - the attributes are different
5626 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005627 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 */
5629 static int
5630char_needs_redraw(off_from, off_to, cols)
5631 int off_from;
5632 int off_to;
5633 int cols;
5634{
5635 if (cols > 0
5636 && ((ScreenLines[off_from] != ScreenLines[off_to]
5637 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5638
5639#ifdef FEAT_MBYTE
5640 || (enc_dbcs != 0
5641 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5642 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5643 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5644 : (cols > 1 && ScreenLines[off_from + 1]
5645 != ScreenLines[off_to + 1])))
5646 || (enc_utf8
5647 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5648 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005649 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005650 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5651 && ScreenLines[off_from + 1]
5652 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653#endif
5654 ))
5655 return TRUE;
5656 return FALSE;
5657}
5658
5659/*
5660 * Move one "cooked" screen line to the screen, but only the characters that
5661 * have actually changed. Handle insert/delete character.
5662 * "coloff" gives the first column on the screen for this line.
5663 * "endcol" gives the columns where valid characters are.
5664 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5665 * needs to be cleared, negative otherwise.
5666 * "rlflag" is TRUE in a rightleft window:
5667 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5668 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5669 */
5670 static void
5671screen_line(row, coloff, endcol, clear_width
5672#ifdef FEAT_RIGHTLEFT
5673 , rlflag
5674#endif
5675 )
5676 int row;
5677 int coloff;
5678 int endcol;
5679 int clear_width;
5680#ifdef FEAT_RIGHTLEFT
5681 int rlflag;
5682#endif
5683{
5684 unsigned off_from;
5685 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005686#ifdef FEAT_MBYTE
5687 unsigned max_off_from;
5688 unsigned max_off_to;
5689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 int col = 0;
5691#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5692 int hl;
5693#endif
5694 int force = FALSE; /* force update rest of the line */
5695 int redraw_this /* bool: does character need redraw? */
5696#ifdef FEAT_GUI
5697 = TRUE /* For GUI when while-loop empty */
5698#endif
5699 ;
5700 int redraw_next; /* redraw_this for next character */
5701#ifdef FEAT_MBYTE
5702 int clear_next = FALSE;
5703 int char_cells; /* 1: normal char */
5704 /* 2: occupies two display cells */
5705# define CHAR_CELLS char_cells
5706#else
5707# define CHAR_CELLS 1
5708#endif
5709
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005710 /* Check for illegal row and col, just in case. */
5711 if (row >= Rows)
5712 row = Rows - 1;
5713 if (endcol > Columns)
5714 endcol = Columns;
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716# ifdef FEAT_CLIPBOARD
5717 clip_may_clear_selection(row, row);
5718# endif
5719
5720 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5721 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005722#ifdef FEAT_MBYTE
5723 max_off_from = off_from + screen_Columns;
5724 max_off_to = LineOffset[row] + screen_Columns;
5725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727#ifdef FEAT_RIGHTLEFT
5728 if (rlflag)
5729 {
5730 /* Clear rest first, because it's left of the text. */
5731 if (clear_width > 0)
5732 {
5733 while (col <= endcol && ScreenLines[off_to] == ' '
5734 && ScreenAttrs[off_to] == 0
5735# ifdef FEAT_MBYTE
5736 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5737# endif
5738 )
5739 {
5740 ++off_to;
5741 ++col;
5742 }
5743 if (col <= endcol)
5744 screen_fill(row, row + 1, col + coloff,
5745 endcol + coloff + 1, ' ', ' ', 0);
5746 }
5747 col = endcol + 1;
5748 off_to = LineOffset[row] + col + coloff;
5749 off_from += col;
5750 endcol = (clear_width > 0 ? clear_width : -clear_width);
5751 }
5752#endif /* FEAT_RIGHTLEFT */
5753
5754 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5755
5756 while (col < endcol)
5757 {
5758#ifdef FEAT_MBYTE
5759 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005760 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 else
5762 char_cells = 1;
5763#endif
5764
5765 redraw_this = redraw_next;
5766 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5767 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5768
5769#ifdef FEAT_GUI
5770 /* If the next character was bold, then redraw the current character to
5771 * remove any pixels that might have spilt over into us. This only
5772 * happens in the GUI.
5773 */
5774 if (redraw_next && gui.in_use)
5775 {
5776 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005777 if (hl > HL_ALL)
5778 hl = syn_attr2attr(hl);
5779 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 redraw_this = TRUE;
5781 }
5782#endif
5783
5784 if (redraw_this)
5785 {
5786 /*
5787 * Special handling when 'xs' termcap flag set (hpterm):
5788 * Attributes for characters are stored at the position where the
5789 * cursor is when writing the highlighting code. The
5790 * start-highlighting code must be written with the cursor on the
5791 * first highlighted character. The stop-highlighting code must
5792 * be written with the cursor just after the last highlighted
5793 * character.
5794 * Overwriting a character doesn't remove it's highlighting. Need
5795 * to clear the rest of the line, and force redrawing it
5796 * completely.
5797 */
5798 if ( p_wiv
5799 && !force
5800#ifdef FEAT_GUI
5801 && !gui.in_use
5802#endif
5803 && ScreenAttrs[off_to] != 0
5804 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5805 {
5806 /*
5807 * Need to remove highlighting attributes here.
5808 */
5809 windgoto(row, col + coloff);
5810 out_str(T_CE); /* clear rest of this screen line */
5811 screen_start(); /* don't know where cursor is now */
5812 force = TRUE; /* force redraw of rest of the line */
5813 redraw_next = TRUE; /* or else next char would miss out */
5814
5815 /*
5816 * If the previous character was highlighted, need to stop
5817 * highlighting at this character.
5818 */
5819 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5820 {
5821 screen_attr = ScreenAttrs[off_to - 1];
5822 term_windgoto(row, col + coloff);
5823 screen_stop_highlight();
5824 }
5825 else
5826 screen_attr = 0; /* highlighting has stopped */
5827 }
5828#ifdef FEAT_MBYTE
5829 if (enc_dbcs != 0)
5830 {
5831 /* Check if overwriting a double-byte with a single-byte or
5832 * the other way around requires another character to be
5833 * redrawn. For UTF-8 this isn't needed, because comparing
5834 * ScreenLinesUC[] is sufficient. */
5835 if (char_cells == 1
5836 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005837 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
5839 /* Writing a single-cell character over a double-cell
5840 * character: need to redraw the next cell. */
5841 ScreenLines[off_to + 1] = 0;
5842 redraw_next = TRUE;
5843 }
5844 else if (char_cells == 2
5845 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005846 && (*mb_off2cells)(off_to, max_off_to) == 1
5847 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
5849 /* Writing the second half of a double-cell character over
5850 * a double-cell character: need to redraw the second
5851 * cell. */
5852 ScreenLines[off_to + 2] = 0;
5853 redraw_next = TRUE;
5854 }
5855
5856 if (enc_dbcs == DBCS_JPNU)
5857 ScreenLines2[off_to] = ScreenLines2[off_from];
5858 }
5859 /* When writing a single-width character over a double-width
5860 * character and at the end of the redrawn text, need to clear out
5861 * the right halve of the old character.
5862 * Also required when writing the right halve of a double-width
5863 * char over the left halve of an existing one. */
5864 if (has_mbyte && col + char_cells == endcol
5865 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005866 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005868 && (*mb_off2cells)(off_to, max_off_to) == 1
5869 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 clear_next = TRUE;
5871#endif
5872
5873 ScreenLines[off_to] = ScreenLines[off_from];
5874#ifdef FEAT_MBYTE
5875 if (enc_utf8)
5876 {
5877 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5878 if (ScreenLinesUC[off_from] != 0)
5879 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005880 int i;
5881
5882 for (i = 0; i < Screen_mco; ++i)
5883 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885 }
5886 if (char_cells == 2)
5887 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5888#endif
5889
5890#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005891 /* The bold trick makes a single column of pixels appear in the
5892 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 * character should be redrawn too. This happens for our own GUI
5894 * and for some xterms. */
5895 if (
5896# ifdef FEAT_GUI
5897 gui.in_use
5898# endif
5899# if defined(FEAT_GUI) && defined(UNIX)
5900 ||
5901# endif
5902# ifdef UNIX
5903 term_is_xterm
5904# endif
5905 )
5906 {
5907 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005908 if (hl > HL_ALL)
5909 hl = syn_attr2attr(hl);
5910 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 redraw_next = TRUE;
5912 }
5913#endif
5914 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5915#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005916 /* For simplicity set the attributes of second half of a
5917 * double-wide character equal to the first half. */
5918 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005920
5921 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 else
5924#endif
5925 screen_char(off_to, row, col + coloff);
5926 }
5927 else if ( p_wiv
5928#ifdef FEAT_GUI
5929 && !gui.in_use
5930#endif
5931 && col + coloff > 0)
5932 {
5933 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5934 {
5935 /*
5936 * Don't output stop-highlight when moving the cursor, it will
5937 * stop the highlighting when it should continue.
5938 */
5939 screen_attr = 0;
5940 }
5941 else if (screen_attr != 0)
5942 screen_stop_highlight();
5943 }
5944
5945 off_to += CHAR_CELLS;
5946 off_from += CHAR_CELLS;
5947 col += CHAR_CELLS;
5948 }
5949
5950#ifdef FEAT_MBYTE
5951 if (clear_next)
5952 {
5953 /* Clear the second half of a double-wide character of which the left
5954 * half was overwritten with a single-wide character. */
5955 ScreenLines[off_to] = ' ';
5956 if (enc_utf8)
5957 ScreenLinesUC[off_to] = 0;
5958 screen_char(off_to, row, col + coloff);
5959 }
5960#endif
5961
5962 if (clear_width > 0
5963#ifdef FEAT_RIGHTLEFT
5964 && !rlflag
5965#endif
5966 )
5967 {
5968#ifdef FEAT_GUI
5969 int startCol = col;
5970#endif
5971
5972 /* blank out the rest of the line */
5973 while (col < clear_width && ScreenLines[off_to] == ' '
5974 && ScreenAttrs[off_to] == 0
5975#ifdef FEAT_MBYTE
5976 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5977#endif
5978 )
5979 {
5980 ++off_to;
5981 ++col;
5982 }
5983 if (col < clear_width)
5984 {
5985#ifdef FEAT_GUI
5986 /*
5987 * In the GUI, clearing the rest of the line may leave pixels
5988 * behind if the first character cleared was bold. Some bold
5989 * fonts spill over the left. In this case we redraw the previous
5990 * character too. If we didn't skip any blanks above, then we
5991 * only redraw if the character wasn't already redrawn anyway.
5992 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005993 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 {
5995 hl = ScreenAttrs[off_to];
5996 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005997 {
5998 int prev_cells = 1;
5999# ifdef FEAT_MBYTE
6000 if (enc_utf8)
6001 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6002 * that its width is 2. */
6003 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6004 else if (enc_dbcs != 0)
6005 {
6006 /* find previous character by counting from first
6007 * column and get its width. */
6008 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006009 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006010
6011 while (off < off_to)
6012 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006013 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006014 off += prev_cells;
6015 }
6016 }
6017
6018 if (enc_dbcs != 0 && prev_cells > 1)
6019 screen_char_2(off_to - prev_cells, row,
6020 col + coloff - prev_cells);
6021 else
6022# endif
6023 screen_char(off_to - prev_cells, row,
6024 col + coloff - prev_cells);
6025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
6027#endif
6028 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6029 ' ', ' ', 0);
6030#ifdef FEAT_VERTSPLIT
6031 off_to += clear_width - col;
6032 col = clear_width;
6033#endif
6034 }
6035 }
6036
6037 if (clear_width > 0)
6038 {
6039#ifdef FEAT_VERTSPLIT
6040 /* For a window that's left of another, draw the separator char. */
6041 if (col + coloff < Columns)
6042 {
6043 int c;
6044
6045 c = fillchar_vsep(&hl);
6046 if (ScreenLines[off_to] != c
6047# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006048 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6049 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050# endif
6051 || ScreenAttrs[off_to] != hl)
6052 {
6053 ScreenLines[off_to] = c;
6054 ScreenAttrs[off_to] = hl;
6055# ifdef FEAT_MBYTE
6056 if (enc_utf8)
6057 {
6058 if (c >= 0x80)
6059 {
6060 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006061 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063 else
6064 ScreenLinesUC[off_to] = 0;
6065 }
6066# endif
6067 screen_char(off_to, row, col + coloff);
6068 }
6069 }
6070 else
6071#endif
6072 LineWraps[row] = FALSE;
6073 }
6074}
6075
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006076#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006078 * Mirror text "str" for right-left displaying.
6079 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006081 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082rl_mirror(str)
6083 char_u *str;
6084{
6085 char_u *p1, *p2;
6086 int t;
6087
6088 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6089 {
6090 t = *p1;
6091 *p1 = *p2;
6092 *p2 = t;
6093 }
6094}
6095#endif
6096
6097#if defined(FEAT_WINDOWS) || defined(PROTO)
6098/*
6099 * mark all status lines for redraw; used after first :cd
6100 */
6101 void
6102status_redraw_all()
6103{
6104 win_T *wp;
6105
6106 for (wp = firstwin; wp; wp = wp->w_next)
6107 if (wp->w_status_height)
6108 {
6109 wp->w_redr_status = TRUE;
6110 redraw_later(VALID);
6111 }
6112}
6113
6114/*
6115 * mark all status lines of the current buffer for redraw
6116 */
6117 void
6118status_redraw_curbuf()
6119{
6120 win_T *wp;
6121
6122 for (wp = firstwin; wp; wp = wp->w_next)
6123 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6124 {
6125 wp->w_redr_status = TRUE;
6126 redraw_later(VALID);
6127 }
6128}
6129
6130/*
6131 * Redraw all status lines that need to be redrawn.
6132 */
6133 void
6134redraw_statuslines()
6135{
6136 win_T *wp;
6137
6138 for (wp = firstwin; wp; wp = wp->w_next)
6139 if (wp->w_redr_status)
6140 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006141 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006142 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143}
6144#endif
6145
6146#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6147/*
6148 * Redraw all status lines at the bottom of frame "frp".
6149 */
6150 void
6151win_redraw_last_status(frp)
6152 frame_T *frp;
6153{
6154 if (frp->fr_layout == FR_LEAF)
6155 frp->fr_win->w_redr_status = TRUE;
6156 else if (frp->fr_layout == FR_ROW)
6157 {
6158 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6159 win_redraw_last_status(frp);
6160 }
6161 else /* frp->fr_layout == FR_COL */
6162 {
6163 frp = frp->fr_child;
6164 while (frp->fr_next != NULL)
6165 frp = frp->fr_next;
6166 win_redraw_last_status(frp);
6167 }
6168}
6169#endif
6170
6171#ifdef FEAT_VERTSPLIT
6172/*
6173 * Draw the verticap separator right of window "wp" starting with line "row".
6174 */
6175 static void
6176draw_vsep_win(wp, row)
6177 win_T *wp;
6178 int row;
6179{
6180 int hl;
6181 int c;
6182
6183 if (wp->w_vsep_width)
6184 {
6185 /* draw the vertical separator right of this window */
6186 c = fillchar_vsep(&hl);
6187 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6188 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6189 c, ' ', hl);
6190 }
6191}
6192#endif
6193
6194#ifdef FEAT_WILDMENU
6195static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006196static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
6198/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006199 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
6201 static int
6202status_match_len(xp, s)
6203 expand_T *xp;
6204 char_u *s;
6205{
6206 int len = 0;
6207
6208#ifdef FEAT_MENU
6209 int emenu = (xp->xp_context == EXPAND_MENUS
6210 || xp->xp_context == EXPAND_MENUNAMES);
6211
6212 /* Check for menu separators - replace with '|'. */
6213 if (emenu && menu_is_separator(s))
6214 return 1;
6215#endif
6216
6217 while (*s != NUL)
6218 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006219 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006220 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006221 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223
6224 return len;
6225}
6226
6227/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006228 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006229 * These are backslashes used for escaping. Do show backslashes in help tags.
6230 */
6231 static int
6232skip_status_match_char(xp, s)
6233 expand_T *xp;
6234 char_u *s;
6235{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006236 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006237#ifdef FEAT_MENU
6238 || ((xp->xp_context == EXPAND_MENUS
6239 || xp->xp_context == EXPAND_MENUNAMES)
6240 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6241#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006242 )
6243 {
6244#ifndef BACKSLASH_IN_FILENAME
6245 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6246 return 2;
6247#endif
6248 return 1;
6249 }
6250 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006251}
6252
6253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 * Show wildchar matches in the status line.
6255 * Show at least the "match" item.
6256 * We start at item 'first_match' in the list and show all matches that fit.
6257 *
6258 * If inversion is possible we use it. Else '=' characters are used.
6259 */
6260 void
6261win_redr_status_matches(xp, num_matches, matches, match, showtail)
6262 expand_T *xp;
6263 int num_matches;
6264 char_u **matches; /* list of matches */
6265 int match;
6266 int showtail;
6267{
6268#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6269 int row;
6270 char_u *buf;
6271 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006272 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 int fillchar;
6274 int attr;
6275 int i;
6276 int highlight = TRUE;
6277 char_u *selstart = NULL;
6278 int selstart_col = 0;
6279 char_u *selend = NULL;
6280 static int first_match = 0;
6281 int add_left = FALSE;
6282 char_u *s;
6283#ifdef FEAT_MENU
6284 int emenu;
6285#endif
6286#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6287 int l;
6288#endif
6289
6290 if (matches == NULL) /* interrupted completion? */
6291 return;
6292
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006293#ifdef FEAT_MBYTE
6294 if (has_mbyte)
6295 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6296 else
6297#endif
6298 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (buf == NULL)
6300 return;
6301
6302 if (match == -1) /* don't show match but original text */
6303 {
6304 match = 0;
6305 highlight = FALSE;
6306 }
6307 /* count 1 for the ending ">" */
6308 clen = status_match_len(xp, L_MATCH(match)) + 3;
6309 if (match == 0)
6310 first_match = 0;
6311 else if (match < first_match)
6312 {
6313 /* jumping left, as far as we can go */
6314 first_match = match;
6315 add_left = TRUE;
6316 }
6317 else
6318 {
6319 /* check if match fits on the screen */
6320 for (i = first_match; i < match; ++i)
6321 clen += status_match_len(xp, L_MATCH(i)) + 2;
6322 if (first_match > 0)
6323 clen += 2;
6324 /* jumping right, put match at the left */
6325 if ((long)clen > Columns)
6326 {
6327 first_match = match;
6328 /* if showing the last match, we can add some on the left */
6329 clen = 2;
6330 for (i = match; i < num_matches; ++i)
6331 {
6332 clen += status_match_len(xp, L_MATCH(i)) + 2;
6333 if ((long)clen >= Columns)
6334 break;
6335 }
6336 if (i == num_matches)
6337 add_left = TRUE;
6338 }
6339 }
6340 if (add_left)
6341 while (first_match > 0)
6342 {
6343 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6344 if ((long)clen >= Columns)
6345 break;
6346 --first_match;
6347 }
6348
6349 fillchar = fillchar_status(&attr, TRUE);
6350
6351 if (first_match == 0)
6352 {
6353 *buf = NUL;
6354 len = 0;
6355 }
6356 else
6357 {
6358 STRCPY(buf, "< ");
6359 len = 2;
6360 }
6361 clen = len;
6362
6363 i = first_match;
6364 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6365 {
6366 if (i == match)
6367 {
6368 selstart = buf + len;
6369 selstart_col = clen;
6370 }
6371
6372 s = L_MATCH(i);
6373 /* Check for menu separators - replace with '|' */
6374#ifdef FEAT_MENU
6375 emenu = (xp->xp_context == EXPAND_MENUS
6376 || xp->xp_context == EXPAND_MENUNAMES);
6377 if (emenu && menu_is_separator(s))
6378 {
6379 STRCPY(buf + len, transchar('|'));
6380 l = (int)STRLEN(buf + len);
6381 len += l;
6382 clen += l;
6383 }
6384 else
6385#endif
6386 for ( ; *s != NUL; ++s)
6387 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006388 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 clen += ptr2cells(s);
6390#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006391 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 STRNCPY(buf + len, s, l);
6394 s += l - 1;
6395 len += l;
6396 }
6397 else
6398#endif
6399 {
6400 STRCPY(buf + len, transchar_byte(*s));
6401 len += (int)STRLEN(buf + len);
6402 }
6403 }
6404 if (i == match)
6405 selend = buf + len;
6406
6407 *(buf + len++) = ' ';
6408 *(buf + len++) = ' ';
6409 clen += 2;
6410 if (++i == num_matches)
6411 break;
6412 }
6413
6414 if (i != num_matches)
6415 {
6416 *(buf + len++) = '>';
6417 ++clen;
6418 }
6419
6420 buf[len] = NUL;
6421
6422 row = cmdline_row - 1;
6423 if (row >= 0)
6424 {
6425 if (wild_menu_showing == 0)
6426 {
6427 if (msg_scrolled > 0)
6428 {
6429 /* Put the wildmenu just above the command line. If there is
6430 * no room, scroll the screen one line up. */
6431 if (cmdline_row == Rows - 1)
6432 {
6433 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6434 ++msg_scrolled;
6435 }
6436 else
6437 {
6438 ++cmdline_row;
6439 ++row;
6440 }
6441 wild_menu_showing = WM_SCROLLED;
6442 }
6443 else
6444 {
6445 /* Create status line if needed by setting 'laststatus' to 2.
6446 * Set 'winminheight' to zero to avoid that the window is
6447 * resized. */
6448 if (lastwin->w_status_height == 0)
6449 {
6450 save_p_ls = p_ls;
6451 save_p_wmh = p_wmh;
6452 p_ls = 2;
6453 p_wmh = 0;
6454 last_status(FALSE);
6455 }
6456 wild_menu_showing = WM_SHOWN;
6457 }
6458 }
6459
6460 screen_puts(buf, row, 0, attr);
6461 if (selstart != NULL && highlight)
6462 {
6463 *selend = NUL;
6464 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6465 }
6466
6467 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6468 }
6469
6470#ifdef FEAT_VERTSPLIT
6471 win_redraw_last_status(topframe);
6472#else
6473 lastwin->w_redr_status = TRUE;
6474#endif
6475 vim_free(buf);
6476}
6477#endif
6478
6479#if defined(FEAT_WINDOWS) || defined(PROTO)
6480/*
6481 * Redraw the status line of window wp.
6482 *
6483 * If inversion is possible we use it. Else '=' characters are used.
6484 */
6485 void
6486win_redr_status(wp)
6487 win_T *wp;
6488{
6489 int row;
6490 char_u *p;
6491 int len;
6492 int fillchar;
6493 int attr;
6494 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006495 static int busy = FALSE;
6496
6497 /* It's possible to get here recursively when 'statusline' (indirectly)
6498 * invokes ":redrawstatus". Simply ignore the call then. */
6499 if (busy)
6500 return;
6501 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 wp->w_redr_status = FALSE;
6504 if (wp->w_status_height == 0)
6505 {
6506 /* no status line, can only be last window */
6507 redraw_cmdline = TRUE;
6508 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006509 else if (!redrawing()
6510#ifdef FEAT_INS_EXPAND
6511 /* don't update status line when popup menu is visible and may be
6512 * drawn over it */
6513 || pum_visible()
6514#endif
6515 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
6517 /* Don't redraw right now, do it later. */
6518 wp->w_redr_status = TRUE;
6519 }
6520#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006521 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006524 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526#endif
6527 else
6528 {
6529 fillchar = fillchar_status(&attr, wp == curwin);
6530
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006531 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 p = NameBuff;
6533 len = (int)STRLEN(p);
6534
6535 if (wp->w_buffer->b_help
6536#ifdef FEAT_QUICKFIX
6537 || wp->w_p_pvw
6538#endif
6539 || bufIsChanged(wp->w_buffer)
6540 || wp->w_buffer->b_p_ro)
6541 *(p + len++) = ' ';
6542 if (wp->w_buffer->b_help)
6543 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006544 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 len += (int)STRLEN(p + len);
6546 }
6547#ifdef FEAT_QUICKFIX
6548 if (wp->w_p_pvw)
6549 {
6550 STRCPY(p + len, _("[Preview]"));
6551 len += (int)STRLEN(p + len);
6552 }
6553#endif
6554 if (bufIsChanged(wp->w_buffer))
6555 {
6556 STRCPY(p + len, "[+]");
6557 len += 3;
6558 }
6559 if (wp->w_buffer->b_p_ro)
6560 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006561 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 len += 4;
6563 }
6564
6565#ifndef FEAT_VERTSPLIT
6566 this_ru_col = ru_col;
6567 if (this_ru_col < (Columns + 1) / 2)
6568 this_ru_col = (Columns + 1) / 2;
6569#else
6570 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6571 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6572 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6573 if (this_ru_col <= 1)
6574 {
6575 p = (char_u *)"<"; /* No room for file name! */
6576 len = 1;
6577 }
6578 else
6579#endif
6580#ifdef FEAT_MBYTE
6581 if (has_mbyte)
6582 {
6583 int clen = 0, i;
6584
6585 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006586 clen = mb_string2cells(p, -1);
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 /* Find first character that will fit.
6589 * Going from start to end is much faster for DBCS. */
6590 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006591 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 clen -= (*mb_ptr2cells)(p + i);
6593 len = clen;
6594 if (i > 0)
6595 {
6596 p = p + i - 1;
6597 *p = '<';
6598 ++len;
6599 }
6600
6601 }
6602 else
6603#endif
6604 if (len > this_ru_col - 1)
6605 {
6606 p += len - (this_ru_col - 1);
6607 *p = '<';
6608 len = this_ru_col - 1;
6609 }
6610
6611 row = W_WINROW(wp) + wp->w_height;
6612 screen_puts(p, row, W_WINCOL(wp), attr);
6613 screen_fill(row, row + 1, len + W_WINCOL(wp),
6614 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6615
6616 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6617 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6618 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6619 - 1 + W_WINCOL(wp)), attr);
6620
6621#ifdef FEAT_CMDL_INFO
6622 win_redr_ruler(wp, TRUE);
6623#endif
6624 }
6625
6626#ifdef FEAT_VERTSPLIT
6627 /*
6628 * May need to draw the character below the vertical separator.
6629 */
6630 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6631 {
6632 if (stl_connected(wp))
6633 fillchar = fillchar_status(&attr, wp == curwin);
6634 else
6635 fillchar = fillchar_vsep(&attr);
6636 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6637 attr);
6638 }
6639#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006640 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
Bram Moolenaar238a5642006-02-21 22:12:05 +00006643#ifdef FEAT_STL_OPT
6644/*
6645 * Redraw the status line according to 'statusline' and take care of any
6646 * errors encountered.
6647 */
6648 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006649redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006650 win_T *wp;
6651{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006652 static int entered = FALSE;
6653 int save_called_emsg = called_emsg;
6654
6655 /* When called recursively return. This can happen when the statusline
6656 * contains an expression that triggers a redraw. */
6657 if (entered)
6658 return;
6659 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006660
6661 called_emsg = FALSE;
6662 win_redr_custom(wp, FALSE);
6663 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006664 {
6665 /* When there is an error disable the statusline, otherwise the
6666 * display is messed up with errors and a redraw triggers the problem
6667 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006668 set_string_option_direct((char_u *)"statusline", -1,
6669 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006670 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006671 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006672 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006673 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006674}
6675#endif
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677# ifdef FEAT_VERTSPLIT
6678/*
6679 * Return TRUE if the status line of window "wp" is connected to the status
6680 * line of the window right of it. If not, then it's a vertical separator.
6681 * Only call if (wp->w_vsep_width != 0).
6682 */
6683 int
6684stl_connected(wp)
6685 win_T *wp;
6686{
6687 frame_T *fr;
6688
6689 fr = wp->w_frame;
6690 while (fr->fr_parent != NULL)
6691 {
6692 if (fr->fr_parent->fr_layout == FR_COL)
6693 {
6694 if (fr->fr_next != NULL)
6695 break;
6696 }
6697 else
6698 {
6699 if (fr->fr_next != NULL)
6700 return TRUE;
6701 }
6702 fr = fr->fr_parent;
6703 }
6704 return FALSE;
6705}
6706# endif
6707
6708#endif /* FEAT_WINDOWS */
6709
6710#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6711/*
6712 * Get the value to show for the language mappings, active 'keymap'.
6713 */
6714 int
6715get_keymap_str(wp, buf, len)
6716 win_T *wp;
6717 char_u *buf; /* buffer for the result */
6718 int len; /* length of buffer */
6719{
6720 char_u *p;
6721
6722 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6723 return FALSE;
6724
6725 {
6726#ifdef FEAT_EVAL
6727 buf_T *old_curbuf = curbuf;
6728 win_T *old_curwin = curwin;
6729 char_u *s;
6730
6731 curbuf = wp->w_buffer;
6732 curwin = wp;
6733 STRCPY(buf, "b:keymap_name"); /* must be writable */
6734 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 --emsg_skip;
6737 curbuf = old_curbuf;
6738 curwin = old_curwin;
6739 if (p == NULL || *p == NUL)
6740#endif
6741 {
6742#ifdef FEAT_KEYMAP
6743 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6744 p = wp->w_buffer->b_p_keymap;
6745 else
6746#endif
6747 p = (char_u *)"lang";
6748 }
6749 if ((int)(STRLEN(p) + 3) < len)
6750 sprintf((char *)buf, "<%s>", p);
6751 else
6752 buf[0] = NUL;
6753#ifdef FEAT_EVAL
6754 vim_free(s);
6755#endif
6756 }
6757 return buf[0] != NUL;
6758}
6759#endif
6760
6761#if defined(FEAT_STL_OPT) || defined(PROTO)
6762/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006763 * Redraw the status line or ruler of window "wp".
6764 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 */
6766 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006767win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006769 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006771 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 int attr;
6773 int curattr;
6774 int row;
6775 int col = 0;
6776 int maxwidth;
6777 int width;
6778 int n;
6779 int len;
6780 int fillchar;
6781 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006782 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006784 struct stl_hlrec hltab[STL_MAX_ITEM];
6785 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006786 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006787 win_T *ewp;
6788 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789
Bram Moolenaar1d633412013-12-11 15:52:01 +01006790 /* There is a tiny chance that this gets called recursively: When
6791 * redrawing a status line triggers redrawing the ruler or tabline.
6792 * Avoid trouble by not allowing recursion. */
6793 if (entered)
6794 return;
6795 entered = TRUE;
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006798 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006800 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006801 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006802 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006803 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006804 attr = hl_attr(HLF_TPF);
6805 maxwidth = Columns;
6806# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006807 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006810 else
6811 {
6812 row = W_WINROW(wp) + wp->w_height;
6813 fillchar = fillchar_status(&attr, wp == curwin);
6814 maxwidth = W_WIDTH(wp);
6815
6816 if (draw_ruler)
6817 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006818 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006819 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006820 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006821 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006822 if (*++stl == '-')
6823 stl++;
6824 if (atoi((char *)stl))
6825 while (VIM_ISDIGIT(*stl))
6826 stl++;
6827 if (*stl++ != '(')
6828 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006829 }
6830#ifdef FEAT_VERTSPLIT
6831 col = ru_col - (Columns - W_WIDTH(wp));
6832 if (col < (W_WIDTH(wp) + 1) / 2)
6833 col = (W_WIDTH(wp) + 1) / 2;
6834#else
6835 col = ru_col;
6836 if (col > (Columns + 1) / 2)
6837 col = (Columns + 1) / 2;
6838#endif
6839 maxwidth = W_WIDTH(wp) - col;
6840#ifdef FEAT_WINDOWS
6841 if (!wp->w_status_height)
6842#endif
6843 {
6844 row = Rows - 1;
6845 --maxwidth; /* writing in last column may cause scrolling */
6846 fillchar = ' ';
6847 attr = 0;
6848 }
6849
6850# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006851 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006852# endif
6853 }
6854 else
6855 {
6856 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006857 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006858 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006859 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006860# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006861 use_sandbox = was_set_insecurely((char_u *)"statusline",
6862 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006863# endif
6864 }
6865
6866#ifdef FEAT_VERTSPLIT
6867 col += W_WINCOL(wp);
6868#endif
6869 }
6870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006872 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
Bram Moolenaar61452852011-02-01 18:01:11 +01006874 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6875 * the cursor away and back. */
6876 ewp = wp == NULL ? curwin : wp;
6877 p_crb_save = ewp->w_p_crb;
6878 ewp->w_p_crb = FALSE;
6879
Bram Moolenaar362f3562009-11-03 16:20:34 +00006880 /* Make a copy, because the statusline may include a function call that
6881 * might change the option value and free the memory. */
6882 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006883 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006884 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006885 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006886 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006887 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006889 /* Make all characters printable. */
6890 p = transstr(buf);
6891 if (p != NULL)
6892 {
6893 vim_strncpy(buf, p, sizeof(buf) - 1);
6894 vim_free(p);
6895 }
6896
6897 /* fill up with "fillchar" */
6898 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006899 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901#ifdef FEAT_MBYTE
6902 len += (*mb_char2bytes)(fillchar, buf + len);
6903#else
6904 buf[len++] = fillchar;
6905#endif
6906 ++width;
6907 }
6908 buf[len] = NUL;
6909
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006910 /*
6911 * Draw each snippet with the specified highlighting.
6912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 curattr = attr;
6914 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006915 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006917 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 screen_puts_len(p, len, row, col, curattr);
6919 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006920 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006922 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006924 else if (hltab[n].userhl < 0)
6925 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006927 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006928 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929#endif
6930 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006931 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006934
6935 if (wp == NULL)
6936 {
6937 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6938 col = 0;
6939 len = 0;
6940 p = buf;
6941 fillchar = 0;
6942 for (n = 0; tabtab[n].start != NULL; n++)
6943 {
6944 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6945 while (col < len)
6946 TabPageIdxs[col++] = fillchar;
6947 p = tabtab[n].start;
6948 fillchar = tabtab[n].userhl;
6949 }
6950 while (col < Columns)
6951 TabPageIdxs[col++] = fillchar;
6952 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006953
6954theend:
6955 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956}
6957
6958#endif /* FEAT_STL_OPT */
6959
6960/*
6961 * Output a single character directly to the screen and update ScreenLines.
6962 */
6963 void
6964screen_putchar(c, row, col, attr)
6965 int c;
6966 int row, col;
6967 int attr;
6968{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 char_u buf[MB_MAXBYTES + 1];
6970
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006971#ifdef FEAT_MBYTE
6972 if (has_mbyte)
6973 buf[(*mb_char2bytes)(c, buf)] = NUL;
6974 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006976 {
6977 buf[0] = c;
6978 buf[1] = NUL;
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 screen_puts(buf, row, col, attr);
6981}
6982
6983/*
6984 * Get a single character directly from ScreenLines into "bytes[]".
6985 * Also return its attribute in *attrp;
6986 */
6987 void
6988screen_getbytes(row, col, bytes, attrp)
6989 int row, col;
6990 char_u *bytes;
6991 int *attrp;
6992{
6993 unsigned off;
6994
6995 /* safety check */
6996 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6997 {
6998 off = LineOffset[row] + col;
6999 *attrp = ScreenAttrs[off];
7000 bytes[0] = ScreenLines[off];
7001 bytes[1] = NUL;
7002
7003#ifdef FEAT_MBYTE
7004 if (enc_utf8 && ScreenLinesUC[off] != 0)
7005 bytes[utfc_char2bytes(off, bytes)] = NUL;
7006 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7007 {
7008 bytes[0] = ScreenLines[off];
7009 bytes[1] = ScreenLines2[off];
7010 bytes[2] = NUL;
7011 }
7012 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7013 {
7014 bytes[1] = ScreenLines[off + 1];
7015 bytes[2] = NUL;
7016 }
7017#endif
7018 }
7019}
7020
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007021#ifdef FEAT_MBYTE
7022static int screen_comp_differs __ARGS((int, int*));
7023
7024/*
7025 * Return TRUE if composing characters for screen posn "off" differs from
7026 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007027 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007028 */
7029 static int
7030screen_comp_differs(off, u8cc)
7031 int off;
7032 int *u8cc;
7033{
7034 int i;
7035
7036 for (i = 0; i < Screen_mco; ++i)
7037 {
7038 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7039 return TRUE;
7040 if (u8cc[i] == 0)
7041 break;
7042 }
7043 return FALSE;
7044}
7045#endif
7046
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047/*
7048 * Put string '*text' on the screen at position 'row' and 'col', with
7049 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7050 * Note: only outputs within one row, message is truncated at screen boundary!
7051 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7052 */
7053 void
7054screen_puts(text, row, col, attr)
7055 char_u *text;
7056 int row;
7057 int col;
7058 int attr;
7059{
7060 screen_puts_len(text, -1, row, col, attr);
7061}
7062
7063/*
7064 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7065 * a NUL.
7066 */
7067 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007068screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007070 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 int row;
7072 int col;
7073 int attr;
7074{
7075 unsigned off;
7076 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007077 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 int c;
7079#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007080 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 int mbyte_blen = 1;
7082 int mbyte_cells = 1;
7083 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007084 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 int clear_next_cell = FALSE;
7086# ifdef FEAT_ARABIC
7087 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007088 int pc, nc, nc1;
7089 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090# endif
7091#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007092#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7093 int force_redraw_this;
7094 int force_redraw_next = FALSE;
7095#endif
7096 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
7098 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7099 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101
Bram Moolenaarc236c162008-07-13 17:41:49 +00007102#ifdef FEAT_MBYTE
7103 /* When drawing over the right halve of a double-wide char clear out the
7104 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007105 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007106# ifdef FEAT_GUI
7107 && !gui.in_use
7108# endif
7109 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007110 {
7111 ScreenLines[off - 1] = ' ';
7112 ScreenAttrs[off - 1] = 0;
7113 if (enc_utf8)
7114 {
7115 ScreenLinesUC[off - 1] = 0;
7116 ScreenLinesC[0][off - 1] = 0;
7117 }
7118 /* redraw the previous cell, make it empty */
7119 screen_char(off - 1, row, col - 1);
7120 /* force the cell at "col" to be redrawn */
7121 force_redraw_next = TRUE;
7122 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007123#endif
7124
Bram Moolenaar367329b2007-08-30 11:53:22 +00007125#ifdef FEAT_MBYTE
7126 max_off = LineOffset[row] + screen_Columns;
7127#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007128 while (col < screen_Columns
7129 && (len < 0 || (int)(ptr - text) < len)
7130 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 {
7132 c = *ptr;
7133#ifdef FEAT_MBYTE
7134 /* check if this is the first byte of a multibyte */
7135 if (has_mbyte)
7136 {
7137 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007138 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007140 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7142 mbyte_cells = 1;
7143 else if (enc_dbcs != 0)
7144 mbyte_cells = mbyte_blen;
7145 else /* enc_utf8 */
7146 {
7147 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007148 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 (int)((text + len) - ptr));
7150 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007151 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007153# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 /* Non-BMP character: display as ? or fullwidth ?. */
7155 if (u8c >= 0x10000)
7156 {
7157 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7158 if (attr == 0)
7159 attr = hl_attr(HLF_8);
7160 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162# ifdef FEAT_ARABIC
7163 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7164 {
7165 /* Do Arabic shaping. */
7166 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7167 {
7168 /* Past end of string to be displayed. */
7169 nc = NUL;
7170 nc1 = NUL;
7171 }
7172 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007173 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007174 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7175 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007176 nc1 = pcc[0];
7177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 pc = prev_c;
7179 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007180 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182 else
7183 prev_c = u8c;
7184# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007185 if (col + mbyte_cells > screen_Columns)
7186 {
7187 /* Only 1 cell left, but character requires 2 cells:
7188 * display a '>' in the last column to avoid wrapping. */
7189 c = '>';
7190 mbyte_cells = 1;
7191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
7193 }
7194#endif
7195
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007196#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7197 force_redraw_this = force_redraw_next;
7198 force_redraw_next = FALSE;
7199#endif
7200
7201 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202#ifdef FEAT_MBYTE
7203 || (mbyte_cells == 2
7204 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7205 || (enc_dbcs == DBCS_JPNU
7206 && c == 0x8e
7207 && ScreenLines2[off] != ptr[1])
7208 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007209 && (ScreenLinesUC[off] !=
7210 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7211 || (ScreenLinesUC[off] != 0
7212 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#endif
7214 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007215 || exmode_active;
7216
7217 if (need_redraw
7218#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7219 || force_redraw_this
7220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 )
7222 {
7223#if defined(FEAT_GUI) || defined(UNIX)
7224 /* The bold trick makes a single row of pixels appear in the next
7225 * character. When a bold character is removed, the next
7226 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007227 * and for some xterms. */
7228 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229# ifdef FEAT_GUI
7230 gui.in_use
7231# endif
7232# if defined(FEAT_GUI) && defined(UNIX)
7233 ||
7234# endif
7235# ifdef UNIX
7236 term_is_xterm
7237# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007238 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007240 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007242 if (n > HL_ALL)
7243 n = syn_attr2attr(n);
7244 if (n & HL_BOLD)
7245 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 }
7247#endif
7248#ifdef FEAT_MBYTE
7249 /* When at the end of the text and overwriting a two-cell
7250 * character with a one-cell character, need to clear the next
7251 * cell. Also when overwriting the left halve of a two-cell char
7252 * with the right halve of a two-cell char. Do this only once
7253 * (mb_off2cells() may return 2 on the right halve). */
7254 if (clear_next_cell)
7255 clear_next_cell = FALSE;
7256 else if (has_mbyte
7257 && (len < 0 ? ptr[mbyte_blen] == NUL
7258 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007259 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007261 && (*mb_off2cells)(off, max_off) == 1
7262 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 clear_next_cell = TRUE;
7264
7265 /* Make sure we never leave a second byte of a double-byte behind,
7266 * it confuses mb_off2cells(). */
7267 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007268 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007270 && (*mb_off2cells)(off, max_off) == 1
7271 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 ScreenLines[off + mbyte_blen] = 0;
7273#endif
7274 ScreenLines[off] = c;
7275 ScreenAttrs[off] = attr;
7276#ifdef FEAT_MBYTE
7277 if (enc_utf8)
7278 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007279 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 ScreenLinesUC[off] = 0;
7281 else
7282 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007283 int i;
7284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007286 for (i = 0; i < Screen_mco; ++i)
7287 {
7288 ScreenLinesC[i][off] = u8cc[i];
7289 if (u8cc[i] == 0)
7290 break;
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293 if (mbyte_cells == 2)
7294 {
7295 ScreenLines[off + 1] = 0;
7296 ScreenAttrs[off + 1] = attr;
7297 }
7298 screen_char(off, row, col);
7299 }
7300 else if (mbyte_cells == 2)
7301 {
7302 ScreenLines[off + 1] = ptr[1];
7303 ScreenAttrs[off + 1] = attr;
7304 screen_char_2(off, row, col);
7305 }
7306 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7307 {
7308 ScreenLines2[off] = ptr[1];
7309 screen_char(off, row, col);
7310 }
7311 else
7312#endif
7313 screen_char(off, row, col);
7314 }
7315#ifdef FEAT_MBYTE
7316 if (has_mbyte)
7317 {
7318 off += mbyte_cells;
7319 col += mbyte_cells;
7320 ptr += mbyte_blen;
7321 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007322 {
7323 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007325 len = -1;
7326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328 else
7329#endif
7330 {
7331 ++off;
7332 ++col;
7333 ++ptr;
7334 }
7335 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007336
7337#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7338 /* If we detected the next character needs to be redrawn, but the text
7339 * doesn't extend up to there, update the character here. */
7340 if (force_redraw_next && col < screen_Columns)
7341 {
7342# ifdef FEAT_MBYTE
7343 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7344 screen_char_2(off, row, col);
7345 else
7346# endif
7347 screen_char(off, row, col);
7348 }
7349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351
7352#ifdef FEAT_SEARCH_EXTRA
7353/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007354 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 */
7356 static void
7357start_search_hl()
7358{
7359 if (p_hls && !no_hlsearch)
7360 {
7361 last_pat_prog(&search_hl.rm);
7362 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007363# ifdef FEAT_RELTIME
7364 /* Set the time limit to 'redrawtime'. */
7365 profile_setlimit(p_rdt, &search_hl.tm);
7366# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368}
7369
7370/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007371 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 */
7373 static void
7374end_search_hl()
7375{
7376 if (search_hl.rm.regprog != NULL)
7377 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007378 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 search_hl.rm.regprog = NULL;
7380 }
7381}
7382
7383/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007384 * Init for calling prepare_search_hl().
7385 */
7386 static void
7387init_search_hl(wp)
7388 win_T *wp;
7389{
7390 matchitem_T *cur;
7391
7392 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7393 * match */
7394 cur = wp->w_match_head;
7395 while (cur != NULL)
7396 {
7397 cur->hl.rm = cur->match;
7398 if (cur->hlg_id == 0)
7399 cur->hl.attr = 0;
7400 else
7401 cur->hl.attr = syn_id2attr(cur->hlg_id);
7402 cur->hl.buf = wp->w_buffer;
7403 cur->hl.lnum = 0;
7404 cur->hl.first_lnum = 0;
7405# ifdef FEAT_RELTIME
7406 /* Set the time limit to 'redrawtime'. */
7407 profile_setlimit(p_rdt, &(cur->hl.tm));
7408# endif
7409 cur = cur->next;
7410 }
7411 search_hl.buf = wp->w_buffer;
7412 search_hl.lnum = 0;
7413 search_hl.first_lnum = 0;
7414 /* time limit is set at the toplevel, for all windows */
7415}
7416
7417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 * Advance to the match in window "wp" line "lnum" or past it.
7419 */
7420 static void
7421prepare_search_hl(wp, lnum)
7422 win_T *wp;
7423 linenr_T lnum;
7424{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007425 matchitem_T *cur; /* points to the match list */
7426 match_T *shl; /* points to search_hl or a match */
7427 int shl_flag; /* flag to indicate whether search_hl
7428 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007429 int pos_inprogress; /* marks that position match search is
7430 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 int n;
7432
7433 /*
7434 * When using a multi-line pattern, start searching at the top
7435 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007436 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007438 cur = wp->w_match_head;
7439 shl_flag = FALSE;
7440 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007442 if (shl_flag == FALSE)
7443 {
7444 shl = &search_hl;
7445 shl_flag = TRUE;
7446 }
7447 else
7448 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (shl->rm.regprog != NULL
7450 && shl->lnum == 0
7451 && re_multiline(shl->rm.regprog))
7452 {
7453 if (shl->first_lnum == 0)
7454 {
7455# ifdef FEAT_FOLDING
7456 for (shl->first_lnum = lnum;
7457 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7458 if (hasFoldingWin(wp, shl->first_lnum - 1,
7459 NULL, NULL, TRUE, NULL))
7460 break;
7461# else
7462 shl->first_lnum = wp->w_topline;
7463# endif
7464 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007465 if (cur != NULL)
7466 cur->pos.cur = 0;
7467 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007469 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7470 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007472 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7473 pos_inprogress = cur == NULL || cur->pos.cur == 0
7474 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 if (shl->lnum != 0)
7476 {
7477 shl->first_lnum = shl->lnum
7478 + shl->rm.endpos[0].lnum
7479 - shl->rm.startpos[0].lnum;
7480 n = shl->rm.endpos[0].col;
7481 }
7482 else
7483 {
7484 ++shl->first_lnum;
7485 n = 0;
7486 }
7487 }
7488 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007489 if (shl != &search_hl && cur != NULL)
7490 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
7492}
7493
7494/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007495 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 * Uses shl->buf.
7497 * Sets shl->lnum and shl->rm contents.
7498 * Note: Assumes a previous match is always before "lnum", unless
7499 * shl->lnum is zero.
7500 * Careful: Any pointers for buffer lines will become invalid.
7501 */
7502 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007503next_search_hl(win, shl, lnum, mincol, cur)
7504 win_T *win;
7505 match_T *shl; /* points to search_hl or a match */
7506 linenr_T lnum;
7507 colnr_T mincol; /* minimal column for a match */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007508 matchitem_T *cur; /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 linenr_T l;
7511 colnr_T matchcol;
7512 long nmatched;
7513
7514 if (shl->lnum != 0)
7515 {
7516 /* Check for three situations:
7517 * 1. If the "lnum" is below a previous match, start a new search.
7518 * 2. If the previous match includes "mincol", use it.
7519 * 3. Continue after the previous match.
7520 */
7521 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7522 if (lnum > l)
7523 shl->lnum = 0;
7524 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7525 return;
7526 }
7527
7528 /*
7529 * Repeat searching for a match until one is found that includes "mincol"
7530 * or none is found in this line.
7531 */
7532 called_emsg = FALSE;
7533 for (;;)
7534 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007535#ifdef FEAT_RELTIME
7536 /* Stop searching after passing the time limit. */
7537 if (profile_passed_limit(&(shl->tm)))
7538 {
7539 shl->lnum = 0; /* no match found in time */
7540 break;
7541 }
7542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 /* Three situations:
7544 * 1. No useful previous match: search from start of line.
7545 * 2. Not Vi compatible or empty match: continue at next character.
7546 * Break the loop if this is beyond the end of the line.
7547 * 3. Vi compatible searching: continue at end of previous match.
7548 */
7549 if (shl->lnum == 0)
7550 matchcol = 0;
7551 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7552 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007553 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007555 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007556
7557 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007558 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007559 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007561 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 shl->lnum = 0;
7563 break;
7564 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007565#ifdef FEAT_MBYTE
7566 if (has_mbyte)
7567 matchcol += mb_ptr2len(ml);
7568 else
7569#endif
7570 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572 else
7573 matchcol = shl->rm.endpos[0].col;
7574
7575 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007576 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007578 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7579 matchcol,
7580#ifdef FEAT_RELTIME
7581 &(shl->tm)
7582#else
7583 NULL
7584#endif
7585 );
7586 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007587 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007588 /* Error while handling regexp: stop using this regexp. */
7589 if (shl == &search_hl)
7590 {
7591 /* don't free regprog in the match list, it's a copy */
7592 vim_regfree(shl->rm.regprog);
7593 SET_NO_HLSEARCH(TRUE);
7594 }
7595 shl->rm.regprog = NULL;
7596 shl->lnum = 0;
7597 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7598 message */
7599 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007600 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007601 }
7602 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007603 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007604 else
7605 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (nmatched == 0)
7607 {
7608 shl->lnum = 0; /* no match found */
7609 break;
7610 }
7611 if (shl->rm.startpos[0].lnum > 0
7612 || shl->rm.startpos[0].col >= mincol
7613 || nmatched > 1
7614 || shl->rm.endpos[0].col > mincol)
7615 {
7616 shl->lnum += shl->rm.startpos[0].lnum;
7617 break; /* useful match found */
7618 }
7619 }
7620}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
Bram Moolenaarb3414592014-06-17 17:48:32 +02007622 static int
7623next_search_hl_pos(shl, lnum, posmatch, mincol)
7624 match_T *shl; /* points to a match */
7625 linenr_T lnum;
7626 posmatch_T *posmatch; /* match positions */
7627 colnr_T mincol; /* minimal column for a match */
7628{
7629 int i;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007630 int bot = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007631
7632 shl->lnum = 0;
7633 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7634 {
7635 if (posmatch->pos[i].lnum == 0)
7636 break;
7637 if (posmatch->pos[i].col < mincol)
7638 continue;
7639 if (posmatch->pos[i].lnum == lnum)
7640 {
7641 if (shl->lnum == lnum)
7642 {
7643 /* partially sort positions by column numbers
7644 * on the same line */
7645 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7646 {
7647 llpos_T tmp = posmatch->pos[i];
7648
7649 posmatch->pos[i] = posmatch->pos[bot];
7650 posmatch->pos[bot] = tmp;
7651 }
7652 }
7653 else
7654 {
7655 bot = i;
7656 shl->lnum = lnum;
7657 }
7658 }
7659 }
7660 posmatch->cur = 0;
7661 if (shl->lnum == lnum)
7662 {
7663 colnr_T start = posmatch->pos[bot].col == 0
7664 ? 0 : posmatch->pos[bot].col - 1;
7665 colnr_T end = posmatch->pos[bot].col == 0
7666 ? MAXCOL : start + posmatch->pos[bot].len;
7667
7668 shl->rm.startpos[0].lnum = 0;
7669 shl->rm.startpos[0].col = start;
7670 shl->rm.endpos[0].lnum = 0;
7671 shl->rm.endpos[0].col = end;
7672 posmatch->cur = bot + 1;
7673 return TRUE;
7674 }
7675 return FALSE;
7676}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007677#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007678
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 static void
7680screen_start_highlight(attr)
7681 int attr;
7682{
7683 attrentry_T *aep = NULL;
7684
7685 screen_attr = attr;
7686 if (full_screen
7687#ifdef WIN3264
7688 && termcap_active
7689#endif
7690 )
7691 {
7692#ifdef FEAT_GUI
7693 if (gui.in_use)
7694 {
7695 char buf[20];
7696
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007697 /* The GUI handles this internally. */
7698 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 OUT_STR(buf);
7700 }
7701 else
7702#endif
7703 {
7704 if (attr > HL_ALL) /* special HL attr. */
7705 {
7706 if (t_colors > 1)
7707 aep = syn_cterm_attr2entry(attr);
7708 else
7709 aep = syn_term_attr2entry(attr);
7710 if (aep == NULL) /* did ":syntax clear" */
7711 attr = 0;
7712 else
7713 attr = aep->ae_attr;
7714 }
7715 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7716 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007717 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7718 && cterm_normal_fg_bold)
7719 /* If the Normal FG color has BOLD attribute and the new HL
7720 * has a FG color defined, clear BOLD. */
7721 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7723 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007724 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7725 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 out_str(T_US);
7727 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7728 out_str(T_CZH);
7729 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7730 out_str(T_MR);
7731
7732 /*
7733 * Output the color or start string after bold etc., in case the
7734 * bold etc. override the color setting.
7735 */
7736 if (aep != NULL)
7737 {
7738 if (t_colors > 1)
7739 {
7740 if (aep->ae_u.cterm.fg_color)
7741 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7742 if (aep->ae_u.cterm.bg_color)
7743 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7744 }
7745 else
7746 {
7747 if (aep->ae_u.term.start != NULL)
7748 out_str(aep->ae_u.term.start);
7749 }
7750 }
7751 }
7752 }
7753}
7754
7755 void
7756screen_stop_highlight()
7757{
7758 int do_ME = FALSE; /* output T_ME code */
7759
7760 if (screen_attr != 0
7761#ifdef WIN3264
7762 && termcap_active
7763#endif
7764 )
7765 {
7766#ifdef FEAT_GUI
7767 if (gui.in_use)
7768 {
7769 char buf[20];
7770
7771 /* use internal GUI code */
7772 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7773 OUT_STR(buf);
7774 }
7775 else
7776#endif
7777 {
7778 if (screen_attr > HL_ALL) /* special HL attr. */
7779 {
7780 attrentry_T *aep;
7781
7782 if (t_colors > 1)
7783 {
7784 /*
7785 * Assume that t_me restores the original colors!
7786 */
7787 aep = syn_cterm_attr2entry(screen_attr);
7788 if (aep != NULL && (aep->ae_u.cterm.fg_color
7789 || aep->ae_u.cterm.bg_color))
7790 do_ME = TRUE;
7791 }
7792 else
7793 {
7794 aep = syn_term_attr2entry(screen_attr);
7795 if (aep != NULL && aep->ae_u.term.stop != NULL)
7796 {
7797 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7798 do_ME = TRUE;
7799 else
7800 out_str(aep->ae_u.term.stop);
7801 }
7802 }
7803 if (aep == NULL) /* did ":syntax clear" */
7804 screen_attr = 0;
7805 else
7806 screen_attr = aep->ae_attr;
7807 }
7808
7809 /*
7810 * Often all ending-codes are equal to T_ME. Avoid outputting the
7811 * same sequence several times.
7812 */
7813 if (screen_attr & HL_STANDOUT)
7814 {
7815 if (STRCMP(T_SE, T_ME) == 0)
7816 do_ME = TRUE;
7817 else
7818 out_str(T_SE);
7819 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007820 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 {
7822 if (STRCMP(T_UE, T_ME) == 0)
7823 do_ME = TRUE;
7824 else
7825 out_str(T_UE);
7826 }
7827 if (screen_attr & HL_ITALIC)
7828 {
7829 if (STRCMP(T_CZR, T_ME) == 0)
7830 do_ME = TRUE;
7831 else
7832 out_str(T_CZR);
7833 }
7834 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7835 out_str(T_ME);
7836
7837 if (t_colors > 1)
7838 {
7839 /* set Normal cterm colors */
7840 if (cterm_normal_fg_color != 0)
7841 term_fg_color(cterm_normal_fg_color - 1);
7842 if (cterm_normal_bg_color != 0)
7843 term_bg_color(cterm_normal_bg_color - 1);
7844 if (cterm_normal_fg_bold)
7845 out_str(T_MD);
7846 }
7847 }
7848 }
7849 screen_attr = 0;
7850}
7851
7852/*
7853 * Reset the colors for a cterm. Used when leaving Vim.
7854 * The machine specific code may override this again.
7855 */
7856 void
7857reset_cterm_colors()
7858{
7859 if (t_colors > 1)
7860 {
7861 /* set Normal cterm colors */
7862 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7863 {
7864 out_str(T_OP);
7865 screen_attr = -1;
7866 }
7867 if (cterm_normal_fg_bold)
7868 {
7869 out_str(T_ME);
7870 screen_attr = -1;
7871 }
7872 }
7873}
7874
7875/*
7876 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7877 * using the attributes from ScreenAttrs["off"].
7878 */
7879 static void
7880screen_char(off, row, col)
7881 unsigned off;
7882 int row;
7883 int col;
7884{
7885 int attr;
7886
7887 /* Check for illegal values, just in case (could happen just after
7888 * resizing). */
7889 if (row >= screen_Rows || col >= screen_Columns)
7890 return;
7891
7892 /* Outputting the last character on the screen may scrollup the screen.
7893 * Don't to it! Mark the character invalid (update it when scrolled up) */
7894 if (row == screen_Rows - 1 && col == screen_Columns - 1
7895#ifdef FEAT_RIGHTLEFT
7896 /* account for first command-line character in rightleft mode */
7897 && !cmdmsg_rl
7898#endif
7899 )
7900 {
7901 ScreenAttrs[off] = (sattr_T)-1;
7902 return;
7903 }
7904
7905 /*
7906 * Stop highlighting first, so it's easier to move the cursor.
7907 */
7908#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7909 if (screen_char_attr != 0)
7910 attr = screen_char_attr;
7911 else
7912#endif
7913 attr = ScreenAttrs[off];
7914 if (screen_attr != attr)
7915 screen_stop_highlight();
7916
7917 windgoto(row, col);
7918
7919 if (screen_attr != attr)
7920 screen_start_highlight(attr);
7921
7922#ifdef FEAT_MBYTE
7923 if (enc_utf8 && ScreenLinesUC[off] != 0)
7924 {
7925 char_u buf[MB_MAXBYTES + 1];
7926
7927 /* Convert UTF-8 character to bytes and write it. */
7928
7929 buf[utfc_char2bytes(off, buf)] = NUL;
7930
7931 out_str(buf);
7932 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7933 ++screen_cur_col;
7934 }
7935 else
7936#endif
7937 {
7938#ifdef FEAT_MBYTE
7939 out_flush_check();
7940#endif
7941 out_char(ScreenLines[off]);
7942#ifdef FEAT_MBYTE
7943 /* double-byte character in single-width cell */
7944 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7945 out_char(ScreenLines2[off]);
7946#endif
7947 }
7948
7949 screen_cur_col++;
7950}
7951
7952#ifdef FEAT_MBYTE
7953
7954/*
7955 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7956 * on the screen at position 'row' and 'col'.
7957 * The attributes of the first byte is used for all. This is required to
7958 * output the two bytes of a double-byte character with nothing in between.
7959 */
7960 static void
7961screen_char_2(off, row, col)
7962 unsigned off;
7963 int row;
7964 int col;
7965{
7966 /* Check for illegal values (could be wrong when screen was resized). */
7967 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7968 return;
7969
7970 /* Outputting the last character on the screen may scrollup the screen.
7971 * Don't to it! Mark the character invalid (update it when scrolled up) */
7972 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7973 {
7974 ScreenAttrs[off] = (sattr_T)-1;
7975 return;
7976 }
7977
7978 /* Output the first byte normally (positions the cursor), then write the
7979 * second byte directly. */
7980 screen_char(off, row, col);
7981 out_char(ScreenLines[off + 1]);
7982 ++screen_cur_col;
7983}
7984#endif
7985
7986#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7987/*
7988 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7989 * This uses the contents of ScreenLines[] and doesn't change it.
7990 */
7991 void
7992screen_draw_rectangle(row, col, height, width, invert)
7993 int row;
7994 int col;
7995 int height;
7996 int width;
7997 int invert;
7998{
7999 int r, c;
8000 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008001#ifdef FEAT_MBYTE
8002 int max_off;
8003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008005 /* Can't use ScreenLines unless initialized */
8006 if (ScreenLines == NULL)
8007 return;
8008
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (invert)
8010 screen_char_attr = HL_INVERSE;
8011 for (r = row; r < row + height; ++r)
8012 {
8013 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008014#ifdef FEAT_MBYTE
8015 max_off = off + screen_Columns;
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 for (c = col; c < col + width; ++c)
8018 {
8019#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008020 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
8022 screen_char_2(off + c, r, c);
8023 ++c;
8024 }
8025 else
8026#endif
8027 {
8028 screen_char(off + c, r, c);
8029#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008030 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 ++c;
8032#endif
8033 }
8034 }
8035 }
8036 screen_char_attr = 0;
8037}
8038#endif
8039
8040#ifdef FEAT_VERTSPLIT
8041/*
8042 * Redraw the characters for a vertically split window.
8043 */
8044 static void
8045redraw_block(row, end, wp)
8046 int row;
8047 int end;
8048 win_T *wp;
8049{
8050 int col;
8051 int width;
8052
8053# ifdef FEAT_CLIPBOARD
8054 clip_may_clear_selection(row, end - 1);
8055# endif
8056
8057 if (wp == NULL)
8058 {
8059 col = 0;
8060 width = Columns;
8061 }
8062 else
8063 {
8064 col = wp->w_wincol;
8065 width = wp->w_width;
8066 }
8067 screen_draw_rectangle(row, col, end - row, width, FALSE);
8068}
8069#endif
8070
8071/*
8072 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8073 * with character 'c1' in first column followed by 'c2' in the other columns.
8074 * Use attributes 'attr'.
8075 */
8076 void
8077screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
8078 int start_row, end_row;
8079 int start_col, end_col;
8080 int c1, c2;
8081 int attr;
8082{
8083 int row;
8084 int col;
8085 int off;
8086 int end_off;
8087 int did_delete;
8088 int c;
8089 int norm_term;
8090#if defined(FEAT_GUI) || defined(UNIX)
8091 int force_next = FALSE;
8092#endif
8093
8094 if (end_row > screen_Rows) /* safety check */
8095 end_row = screen_Rows;
8096 if (end_col > screen_Columns) /* safety check */
8097 end_col = screen_Columns;
8098 if (ScreenLines == NULL
8099 || start_row >= end_row
8100 || start_col >= end_col) /* nothing to do */
8101 return;
8102
8103 /* it's a "normal" terminal when not in a GUI or cterm */
8104 norm_term = (
8105#ifdef FEAT_GUI
8106 !gui.in_use &&
8107#endif
8108 t_colors <= 1);
8109 for (row = start_row; row < end_row; ++row)
8110 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008111#ifdef FEAT_MBYTE
8112 if (has_mbyte
8113# ifdef FEAT_GUI
8114 && !gui.in_use
8115# endif
8116 )
8117 {
8118 /* When drawing over the right halve of a double-wide char clear
8119 * out the left halve. When drawing over the left halve of a
8120 * double wide-char clear out the right halve. Only needed in a
8121 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008122 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008123 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008124 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008125 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008126 }
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 /*
8129 * Try to use delete-line termcap code, when no attributes or in a
8130 * "normal" terminal, where a bold/italic space is just a
8131 * space.
8132 */
8133 did_delete = FALSE;
8134 if (c2 == ' '
8135 && end_col == Columns
8136 && can_clear(T_CE)
8137 && (attr == 0
8138 || (norm_term
8139 && attr <= HL_ALL
8140 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8141 {
8142 /*
8143 * check if we really need to clear something
8144 */
8145 col = start_col;
8146 if (c1 != ' ') /* don't clear first char */
8147 ++col;
8148
8149 off = LineOffset[row] + col;
8150 end_off = LineOffset[row] + end_col;
8151
8152 /* skip blanks (used often, keep it fast!) */
8153#ifdef FEAT_MBYTE
8154 if (enc_utf8)
8155 while (off < end_off && ScreenLines[off] == ' '
8156 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8157 ++off;
8158 else
8159#endif
8160 while (off < end_off && ScreenLines[off] == ' '
8161 && ScreenAttrs[off] == 0)
8162 ++off;
8163 if (off < end_off) /* something to be cleared */
8164 {
8165 col = off - LineOffset[row];
8166 screen_stop_highlight();
8167 term_windgoto(row, col);/* clear rest of this screen line */
8168 out_str(T_CE);
8169 screen_start(); /* don't know where cursor is now */
8170 col = end_col - col;
8171 while (col--) /* clear chars in ScreenLines */
8172 {
8173 ScreenLines[off] = ' ';
8174#ifdef FEAT_MBYTE
8175 if (enc_utf8)
8176 ScreenLinesUC[off] = 0;
8177#endif
8178 ScreenAttrs[off] = 0;
8179 ++off;
8180 }
8181 }
8182 did_delete = TRUE; /* the chars are cleared now */
8183 }
8184
8185 off = LineOffset[row] + start_col;
8186 c = c1;
8187 for (col = start_col; col < end_col; ++col)
8188 {
8189 if (ScreenLines[off] != c
8190#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008191 || (enc_utf8 && (int)ScreenLinesUC[off]
8192 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193#endif
8194 || ScreenAttrs[off] != attr
8195#if defined(FEAT_GUI) || defined(UNIX)
8196 || force_next
8197#endif
8198 )
8199 {
8200#if defined(FEAT_GUI) || defined(UNIX)
8201 /* The bold trick may make a single row of pixels appear in
8202 * the next character. When a bold character is removed, the
8203 * next character should be redrawn too. This happens for our
8204 * own GUI and for some xterms. */
8205 if (
8206# ifdef FEAT_GUI
8207 gui.in_use
8208# endif
8209# if defined(FEAT_GUI) && defined(UNIX)
8210 ||
8211# endif
8212# ifdef UNIX
8213 term_is_xterm
8214# endif
8215 )
8216 {
8217 if (ScreenLines[off] != ' '
8218 && (ScreenAttrs[off] > HL_ALL
8219 || ScreenAttrs[off] & HL_BOLD))
8220 force_next = TRUE;
8221 else
8222 force_next = FALSE;
8223 }
8224#endif
8225 ScreenLines[off] = c;
8226#ifdef FEAT_MBYTE
8227 if (enc_utf8)
8228 {
8229 if (c >= 0x80)
8230 {
8231 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008232 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234 else
8235 ScreenLinesUC[off] = 0;
8236 }
8237#endif
8238 ScreenAttrs[off] = attr;
8239 if (!did_delete || c != ' ')
8240 screen_char(off, row, col);
8241 }
8242 ++off;
8243 if (col == start_col)
8244 {
8245 if (did_delete)
8246 break;
8247 c = c2;
8248 }
8249 }
8250 if (end_col == Columns)
8251 LineWraps[row] = FALSE;
8252 if (row == Rows - 1) /* overwritten the command line */
8253 {
8254 redraw_cmdline = TRUE;
8255 if (c1 == ' ' && c2 == ' ')
8256 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008257 if (start_col == 0)
8258 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260 }
8261}
8262
8263/*
8264 * Check if there should be a delay. Used before clearing or redrawing the
8265 * screen or the command line.
8266 */
8267 void
8268check_for_delay(check_msg_scroll)
8269 int check_msg_scroll;
8270{
8271 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8272 && !did_wait_return
8273 && emsg_silent == 0)
8274 {
8275 out_flush();
8276 ui_delay(1000L, TRUE);
8277 emsg_on_display = FALSE;
8278 if (check_msg_scroll)
8279 msg_scroll = FALSE;
8280 }
8281}
8282
8283/*
8284 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008285 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 * Returns TRUE if there is a valid screen to write to.
8287 * Returns FALSE when starting up and screen not initialized yet.
8288 */
8289 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008290screen_valid(doclear)
8291 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008293 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 return (ScreenLines != NULL);
8295}
8296
8297/*
8298 * Resize the shell to Rows and Columns.
8299 * Allocate ScreenLines[] and associated items.
8300 *
8301 * There may be some time between setting Rows and Columns and (re)allocating
8302 * ScreenLines[]. This happens when starting up and when (manually) changing
8303 * the shell size. Always use screen_Rows and screen_Columns to access items
8304 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8305 * final size of the shell is needed.
8306 */
8307 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008308screenalloc(doclear)
8309 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 int new_row, old_row;
8312#ifdef FEAT_GUI
8313 int old_Rows;
8314#endif
8315 win_T *wp;
8316 int outofmem = FALSE;
8317 int len;
8318 schar_T *new_ScreenLines;
8319#ifdef FEAT_MBYTE
8320 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008321 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008323 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#endif
8325 sattr_T *new_ScreenAttrs;
8326 unsigned *new_LineOffset;
8327 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008328#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008329 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008330 tabpage_T *tp;
8331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008333 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008334#ifdef FEAT_AUTOCMD
8335 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008337retry:
8338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 /*
8340 * Allocation of the screen buffers is done only when the size changes and
8341 * when Rows and Columns have been set and we have started doing full
8342 * screen stuff.
8343 */
8344 if ((ScreenLines != NULL
8345 && Rows == screen_Rows
8346 && Columns == screen_Columns
8347#ifdef FEAT_MBYTE
8348 && enc_utf8 == (ScreenLinesUC != NULL)
8349 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008350 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351#endif
8352 )
8353 || Rows == 0
8354 || Columns == 0
8355 || (!full_screen && ScreenLines == NULL))
8356 return;
8357
8358 /*
8359 * It's possible that we produce an out-of-memory message below, which
8360 * will cause this function to be called again. To break the loop, just
8361 * return here.
8362 */
8363 if (entered)
8364 return;
8365 entered = TRUE;
8366
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008367 /*
8368 * Note that the window sizes are updated before reallocating the arrays,
8369 * thus we must not redraw here!
8370 */
8371 ++RedrawingDisabled;
8372
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 win_new_shellsize(); /* fit the windows in the new sized shell */
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 comp_col(); /* recompute columns for shown command and ruler */
8376
8377 /*
8378 * We're changing the size of the screen.
8379 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8380 * - Move lines from the old arrays into the new arrays, clear extra
8381 * lines (unless the screen is going to be cleared).
8382 * - Free the old arrays.
8383 *
8384 * If anything fails, make ScreenLines NULL, so we don't do anything!
8385 * Continuing with the old ScreenLines may result in a crash, because the
8386 * size is wrong.
8387 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008388 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008390#ifdef FEAT_AUTOCMD
8391 if (aucmd_win != NULL)
8392 win_free_lsize(aucmd_win);
8393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 new_ScreenLines = (schar_T *)lalloc((long_u)(
8396 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8397#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008398 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (enc_utf8)
8400 {
8401 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8402 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008403 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008404 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8406 }
8407 if (enc_dbcs == DBCS_JPNU)
8408 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8409 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8410#endif
8411 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8412 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8413 new_LineOffset = (unsigned *)lalloc((long_u)(
8414 Rows * sizeof(unsigned)), FALSE);
8415 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008416#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008417 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008420 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {
8422 if (win_alloc_lines(wp) == FAIL)
8423 {
8424 outofmem = TRUE;
8425#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008426 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427#endif
8428 }
8429 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008430#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008431 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8432 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008433 outofmem = TRUE;
8434#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008435#ifdef FEAT_WINDOWS
8436give_up:
8437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008439#ifdef FEAT_MBYTE
8440 for (i = 0; i < p_mco; ++i)
8441 if (new_ScreenLinesC[i] == NULL)
8442 break;
8443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (new_ScreenLines == NULL
8445#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008446 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8448#endif
8449 || new_ScreenAttrs == NULL
8450 || new_LineOffset == NULL
8451 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008452#ifdef FEAT_WINDOWS
8453 || new_TabPageIdxs == NULL
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 || outofmem)
8456 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008457 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008458 {
8459 /* guess the size */
8460 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8461
8462 /* Remember we did this to avoid getting outofmem messages over
8463 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008464 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 vim_free(new_ScreenLines);
8467 new_ScreenLines = NULL;
8468#ifdef FEAT_MBYTE
8469 vim_free(new_ScreenLinesUC);
8470 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008471 for (i = 0; i < p_mco; ++i)
8472 {
8473 vim_free(new_ScreenLinesC[i]);
8474 new_ScreenLinesC[i] = NULL;
8475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 vim_free(new_ScreenLines2);
8477 new_ScreenLines2 = NULL;
8478#endif
8479 vim_free(new_ScreenAttrs);
8480 new_ScreenAttrs = NULL;
8481 vim_free(new_LineOffset);
8482 new_LineOffset = NULL;
8483 vim_free(new_LineWraps);
8484 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008485#ifdef FEAT_WINDOWS
8486 vim_free(new_TabPageIdxs);
8487 new_TabPageIdxs = NULL;
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490 else
8491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008492 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 for (new_row = 0; new_row < Rows; ++new_row)
8495 {
8496 new_LineOffset[new_row] = new_row * Columns;
8497 new_LineWraps[new_row] = FALSE;
8498
8499 /*
8500 * If the screen is not going to be cleared, copy as much as
8501 * possible from the old screen to the new one and clear the rest
8502 * (used when resizing the window at the "--more--" prompt or when
8503 * executing an external command, for the GUI).
8504 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008505 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {
8507 (void)vim_memset(new_ScreenLines + new_row * Columns,
8508 ' ', (size_t)Columns * sizeof(schar_T));
8509#ifdef FEAT_MBYTE
8510 if (enc_utf8)
8511 {
8512 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8513 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008514 for (i = 0; i < p_mco; ++i)
8515 (void)vim_memset(new_ScreenLinesC[i]
8516 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 0, (size_t)Columns * sizeof(u8char_T));
8518 }
8519 if (enc_dbcs == DBCS_JPNU)
8520 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8521 0, (size_t)Columns * sizeof(schar_T));
8522#endif
8523 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8524 0, (size_t)Columns * sizeof(sattr_T));
8525 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008526 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {
8528 if (screen_Columns < Columns)
8529 len = screen_Columns;
8530 else
8531 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008532#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008533 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008534 * may be invalid now. Also when p_mco changes. */
8535 if (!(enc_utf8 && ScreenLinesUC == NULL)
8536 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008537#endif
8538 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8539 ScreenLines + LineOffset[old_row],
8540 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008542 if (enc_utf8 && ScreenLinesUC != NULL
8543 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
8545 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8546 ScreenLinesUC + LineOffset[old_row],
8547 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008548 for (i = 0; i < p_mco; ++i)
8549 mch_memmove(new_ScreenLinesC[i]
8550 + new_LineOffset[new_row],
8551 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 (size_t)len * sizeof(u8char_T));
8553 }
8554 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8555 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8556 ScreenLines2 + LineOffset[old_row],
8557 (size_t)len * sizeof(schar_T));
8558#endif
8559 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8560 ScreenAttrs + LineOffset[old_row],
8561 (size_t)len * sizeof(sattr_T));
8562 }
8563 }
8564 }
8565 /* Use the last line of the screen for the current line. */
8566 current_ScreenLine = new_ScreenLines + Rows * Columns;
8567 }
8568
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008569 free_screenlines();
8570
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 ScreenLines = new_ScreenLines;
8572#ifdef FEAT_MBYTE
8573 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008574 for (i = 0; i < p_mco; ++i)
8575 ScreenLinesC[i] = new_ScreenLinesC[i];
8576 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 ScreenLines2 = new_ScreenLines2;
8578#endif
8579 ScreenAttrs = new_ScreenAttrs;
8580 LineOffset = new_LineOffset;
8581 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008582#ifdef FEAT_WINDOWS
8583 TabPageIdxs = new_TabPageIdxs;
8584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 /* It's important that screen_Rows and screen_Columns reflect the actual
8587 * size of ScreenLines[]. Set them before calling anything. */
8588#ifdef FEAT_GUI
8589 old_Rows = screen_Rows;
8590#endif
8591 screen_Rows = Rows;
8592 screen_Columns = Columns;
8593
8594 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008595 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 screenclear2();
8597
8598#ifdef FEAT_GUI
8599 else if (gui.in_use
8600 && !gui.starting
8601 && ScreenLines != NULL
8602 && old_Rows != Rows)
8603 {
8604 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8605 /*
8606 * Adjust the position of the cursor, for when executing an external
8607 * command.
8608 */
8609 if (msg_row >= Rows) /* Rows got smaller */
8610 msg_row = Rows - 1; /* put cursor at last row */
8611 else if (Rows > old_Rows) /* Rows got bigger */
8612 msg_row += Rows - old_Rows; /* put cursor in same place */
8613 if (msg_col >= Columns) /* Columns got smaller */
8614 msg_col = Columns - 1; /* put cursor at last column */
8615 }
8616#endif
8617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008619 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008620
8621#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008622 /*
8623 * Do not apply autocommands more than 3 times to avoid an endless loop
8624 * in case applying autocommands always changes Rows or Columns.
8625 */
8626 if (starting == 0 && ++retry_count <= 3)
8627 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008628 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008629 /* In rare cases, autocommands may have altered Rows or Columns,
8630 * jump back to check if we need to allocate the screen again. */
8631 goto retry;
8632 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635
8636 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008637free_screenlines()
8638{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008639#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008640 int i;
8641
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008642 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008643 for (i = 0; i < Screen_mco; ++i)
8644 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008645 vim_free(ScreenLines2);
8646#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008647 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008648 vim_free(ScreenAttrs);
8649 vim_free(LineOffset);
8650 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008651#ifdef FEAT_WINDOWS
8652 vim_free(TabPageIdxs);
8653#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008654}
8655
8656 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657screenclear()
8658{
8659 check_for_delay(FALSE);
8660 screenalloc(FALSE); /* allocate screen buffers if size changed */
8661 screenclear2(); /* clear the screen */
8662}
8663
8664 static void
8665screenclear2()
8666{
8667 int i;
8668
8669 if (starting == NO_SCREEN || ScreenLines == NULL
8670#ifdef FEAT_GUI
8671 || (gui.in_use && gui.starting)
8672#endif
8673 )
8674 return;
8675
8676#ifdef FEAT_GUI
8677 if (!gui.in_use)
8678#endif
8679 screen_attr = -1; /* force setting the Normal colors */
8680 screen_stop_highlight(); /* don't want highlighting here */
8681
8682#ifdef FEAT_CLIPBOARD
8683 /* disable selection without redrawing it */
8684 clip_scroll_selection(9999);
8685#endif
8686
8687 /* blank out ScreenLines */
8688 for (i = 0; i < Rows; ++i)
8689 {
8690 lineclear(LineOffset[i], (int)Columns);
8691 LineWraps[i] = FALSE;
8692 }
8693
8694 if (can_clear(T_CL))
8695 {
8696 out_str(T_CL); /* clear the display */
8697 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008698 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 }
8700 else
8701 {
8702 /* can't clear the screen, mark all chars with invalid attributes */
8703 for (i = 0; i < Rows; ++i)
8704 lineinvalid(LineOffset[i], (int)Columns);
8705 clear_cmdline = TRUE;
8706 }
8707
8708 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8709
8710 win_rest_invalid(firstwin);
8711 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008712#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008713 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 if (must_redraw == CLEAR) /* no need to clear again */
8716 must_redraw = NOT_VALID;
8717 compute_cmdrow();
8718 msg_row = cmdline_row; /* put cursor on last line for messages */
8719 msg_col = 0;
8720 screen_start(); /* don't know where cursor is now */
8721 msg_scrolled = 0; /* can't scroll back */
8722 msg_didany = FALSE;
8723 msg_didout = FALSE;
8724}
8725
8726/*
8727 * Clear one line in ScreenLines.
8728 */
8729 static void
8730lineclear(off, width)
8731 unsigned off;
8732 int width;
8733{
8734 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8735#ifdef FEAT_MBYTE
8736 if (enc_utf8)
8737 (void)vim_memset(ScreenLinesUC + off, 0,
8738 (size_t)width * sizeof(u8char_T));
8739#endif
8740 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8741}
8742
8743/*
8744 * Mark one line in ScreenLines invalid by setting the attributes to an
8745 * invalid value.
8746 */
8747 static void
8748lineinvalid(off, width)
8749 unsigned off;
8750 int width;
8751{
8752 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8753}
8754
8755#ifdef FEAT_VERTSPLIT
8756/*
8757 * Copy part of a Screenline for vertically split window "wp".
8758 */
8759 static void
8760linecopy(to, from, wp)
8761 int to;
8762 int from;
8763 win_T *wp;
8764{
8765 unsigned off_to = LineOffset[to] + wp->w_wincol;
8766 unsigned off_from = LineOffset[from] + wp->w_wincol;
8767
8768 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8769 wp->w_width * sizeof(schar_T));
8770# ifdef FEAT_MBYTE
8771 if (enc_utf8)
8772 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008773 int i;
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8776 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008777 for (i = 0; i < p_mco; ++i)
8778 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8779 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 }
8781 if (enc_dbcs == DBCS_JPNU)
8782 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8783 wp->w_width * sizeof(schar_T));
8784# endif
8785 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8786 wp->w_width * sizeof(sattr_T));
8787}
8788#endif
8789
8790/*
8791 * Return TRUE if clearing with term string "p" would work.
8792 * It can't work when the string is empty or it won't set the right background.
8793 */
8794 int
8795can_clear(p)
8796 char_u *p;
8797{
8798 return (*p != NUL && (t_colors <= 1
8799#ifdef FEAT_GUI
8800 || gui.in_use
8801#endif
8802 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8803}
8804
8805/*
8806 * Reset cursor position. Use whenever cursor was moved because of outputting
8807 * something directly to the screen (shell commands) or a terminal control
8808 * code.
8809 */
8810 void
8811screen_start()
8812{
8813 screen_cur_row = screen_cur_col = 9999;
8814}
8815
8816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 * Move the cursor to position "row","col" in the screen.
8818 * This tries to find the most efficient way to move, minimizing the number of
8819 * characters sent to the terminal.
8820 */
8821 void
8822windgoto(row, col)
8823 int row;
8824 int col;
8825{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008826 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 int i;
8828 int plan;
8829 int cost;
8830 int wouldbe_col;
8831 int noinvcurs;
8832 char_u *bs;
8833 int goto_cost;
8834 int attr;
8835
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008836#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8838
8839#define PLAN_LE 1
8840#define PLAN_CR 2
8841#define PLAN_NL 3
8842#define PLAN_WRITE 4
8843 /* Can't use ScreenLines unless initialized */
8844 if (ScreenLines == NULL)
8845 return;
8846
8847 if (col != screen_cur_col || row != screen_cur_row)
8848 {
8849 /* Check for valid position. */
8850 if (row < 0) /* window without text lines? */
8851 row = 0;
8852 if (row >= screen_Rows)
8853 row = screen_Rows - 1;
8854 if (col >= screen_Columns)
8855 col = screen_Columns - 1;
8856
8857 /* check if no cursor movement is allowed in highlight mode */
8858 if (screen_attr && *T_MS == NUL)
8859 noinvcurs = HIGHL_COST;
8860 else
8861 noinvcurs = 0;
8862 goto_cost = GOTO_COST + noinvcurs;
8863
8864 /*
8865 * Plan how to do the positioning:
8866 * 1. Use CR to move it to column 0, same row.
8867 * 2. Use T_LE to move it a few columns to the left.
8868 * 3. Use NL to move a few lines down, column 0.
8869 * 4. Move a few columns to the right with T_ND or by writing chars.
8870 *
8871 * Don't do this if the cursor went beyond the last column, the cursor
8872 * position is unknown then (some terminals wrap, some don't )
8873 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008874 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 * characters to move the cursor to the right.
8876 */
8877 if (row >= screen_cur_row && screen_cur_col < Columns)
8878 {
8879 /*
8880 * If the cursor is in the same row, bigger col, we can use CR
8881 * or T_LE.
8882 */
8883 bs = NULL; /* init for GCC */
8884 attr = screen_attr;
8885 if (row == screen_cur_row && col < screen_cur_col)
8886 {
8887 /* "le" is preferred over "bc", because "bc" is obsolete */
8888 if (*T_LE)
8889 bs = T_LE; /* "cursor left" */
8890 else
8891 bs = T_BC; /* "backspace character (old) */
8892 if (*bs)
8893 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8894 else
8895 cost = 999;
8896 if (col + 1 < cost) /* using CR is less characters */
8897 {
8898 plan = PLAN_CR;
8899 wouldbe_col = 0;
8900 cost = 1; /* CR is just one character */
8901 }
8902 else
8903 {
8904 plan = PLAN_LE;
8905 wouldbe_col = col;
8906 }
8907 if (noinvcurs) /* will stop highlighting */
8908 {
8909 cost += noinvcurs;
8910 attr = 0;
8911 }
8912 }
8913
8914 /*
8915 * If the cursor is above where we want to be, we can use CR LF.
8916 */
8917 else if (row > screen_cur_row)
8918 {
8919 plan = PLAN_NL;
8920 wouldbe_col = 0;
8921 cost = (row - screen_cur_row) * 2; /* CR LF */
8922 if (noinvcurs) /* will stop highlighting */
8923 {
8924 cost += noinvcurs;
8925 attr = 0;
8926 }
8927 }
8928
8929 /*
8930 * If the cursor is in the same row, smaller col, just use write.
8931 */
8932 else
8933 {
8934 plan = PLAN_WRITE;
8935 wouldbe_col = screen_cur_col;
8936 cost = 0;
8937 }
8938
8939 /*
8940 * Check if any characters that need to be written have the
8941 * correct attributes. Also avoid UTF-8 characters.
8942 */
8943 i = col - wouldbe_col;
8944 if (i > 0)
8945 cost += i;
8946 if (cost < goto_cost && i > 0)
8947 {
8948 /*
8949 * Check if the attributes are correct without additionally
8950 * stopping highlighting.
8951 */
8952 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8953 while (i && *p++ == attr)
8954 --i;
8955 if (i != 0)
8956 {
8957 /*
8958 * Try if it works when highlighting is stopped here.
8959 */
8960 if (*--p == 0)
8961 {
8962 cost += noinvcurs;
8963 while (i && *p++ == 0)
8964 --i;
8965 }
8966 if (i != 0)
8967 cost = 999; /* different attributes, don't do it */
8968 }
8969#ifdef FEAT_MBYTE
8970 if (enc_utf8)
8971 {
8972 /* Don't use an UTF-8 char for positioning, it's slow. */
8973 for (i = wouldbe_col; i < col; ++i)
8974 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8975 {
8976 cost = 999;
8977 break;
8978 }
8979 }
8980#endif
8981 }
8982
8983 /*
8984 * We can do it without term_windgoto()!
8985 */
8986 if (cost < goto_cost)
8987 {
8988 if (plan == PLAN_LE)
8989 {
8990 if (noinvcurs)
8991 screen_stop_highlight();
8992 while (screen_cur_col > col)
8993 {
8994 out_str(bs);
8995 --screen_cur_col;
8996 }
8997 }
8998 else if (plan == PLAN_CR)
8999 {
9000 if (noinvcurs)
9001 screen_stop_highlight();
9002 out_char('\r');
9003 screen_cur_col = 0;
9004 }
9005 else if (plan == PLAN_NL)
9006 {
9007 if (noinvcurs)
9008 screen_stop_highlight();
9009 while (screen_cur_row < row)
9010 {
9011 out_char('\n');
9012 ++screen_cur_row;
9013 }
9014 screen_cur_col = 0;
9015 }
9016
9017 i = col - screen_cur_col;
9018 if (i > 0)
9019 {
9020 /*
9021 * Use cursor-right if it's one character only. Avoids
9022 * removing a line of pixels from the last bold char, when
9023 * using the bold trick in the GUI.
9024 */
9025 if (T_ND[0] != NUL && T_ND[1] == NUL)
9026 {
9027 while (i-- > 0)
9028 out_char(*T_ND);
9029 }
9030 else
9031 {
9032 int off;
9033
9034 off = LineOffset[row] + screen_cur_col;
9035 while (i-- > 0)
9036 {
9037 if (ScreenAttrs[off] != screen_attr)
9038 screen_stop_highlight();
9039#ifdef FEAT_MBYTE
9040 out_flush_check();
9041#endif
9042 out_char(ScreenLines[off]);
9043#ifdef FEAT_MBYTE
9044 if (enc_dbcs == DBCS_JPNU
9045 && ScreenLines[off] == 0x8e)
9046 out_char(ScreenLines2[off]);
9047#endif
9048 ++off;
9049 }
9050 }
9051 }
9052 }
9053 }
9054 else
9055 cost = 999;
9056
9057 if (cost >= goto_cost)
9058 {
9059 if (noinvcurs)
9060 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009061 if (row == screen_cur_row && (col > screen_cur_col)
9062 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 term_cursor_right(col - screen_cur_col);
9064 else
9065 term_windgoto(row, col);
9066 }
9067 screen_cur_row = row;
9068 screen_cur_col = col;
9069 }
9070}
9071
9072/*
9073 * Set cursor to its position in the current window.
9074 */
9075 void
9076setcursor()
9077{
9078 if (redrawing())
9079 {
9080 validate_cursor();
9081 windgoto(W_WINROW(curwin) + curwin->w_wrow,
9082 W_WINCOL(curwin) + (
9083#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009084 /* With 'rightleft' set and the cursor on a double-wide
9085 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
9087# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009088 (has_mbyte
9089 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9090 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091# endif
9092 1)) :
9093#endif
9094 curwin->w_wcol));
9095 }
9096}
9097
9098
9099/*
9100 * insert 'line_count' lines at 'row' in window 'wp'
9101 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9102 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
9103 * scrolling.
9104 * Returns FAIL if the lines are not inserted, OK for success.
9105 */
9106 int
9107win_ins_lines(wp, row, line_count, invalid, mayclear)
9108 win_T *wp;
9109 int row;
9110 int line_count;
9111 int invalid;
9112 int mayclear;
9113{
9114 int did_delete;
9115 int nextrow;
9116 int lastrow;
9117 int retval;
9118
9119 if (invalid)
9120 wp->w_lines_valid = 0;
9121
9122 if (wp->w_height < 5)
9123 return FAIL;
9124
9125 if (line_count > wp->w_height - row)
9126 line_count = wp->w_height - row;
9127
9128 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
9129 if (retval != MAYBE)
9130 return retval;
9131
9132 /*
9133 * If there is a next window or a status line, we first try to delete the
9134 * lines at the bottom to avoid messing what is after the window.
9135 * If this fails and there are following windows, don't do anything to avoid
9136 * messing up those windows, better just redraw.
9137 */
9138 did_delete = FALSE;
9139#ifdef FEAT_WINDOWS
9140 if (wp->w_next != NULL || wp->w_status_height)
9141 {
9142 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9143 line_count, (int)Rows, FALSE, NULL) == OK)
9144 did_delete = TRUE;
9145 else if (wp->w_next)
9146 return FAIL;
9147 }
9148#endif
9149 /*
9150 * if no lines deleted, blank the lines that will end up below the window
9151 */
9152 if (!did_delete)
9153 {
9154#ifdef FEAT_WINDOWS
9155 wp->w_redr_status = TRUE;
9156#endif
9157 redraw_cmdline = TRUE;
9158 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9159 lastrow = nextrow + line_count;
9160 if (lastrow > Rows)
9161 lastrow = Rows;
9162 screen_fill(nextrow - line_count, lastrow - line_count,
9163 W_WINCOL(wp), (int)W_ENDCOL(wp),
9164 ' ', ' ', 0);
9165 }
9166
9167 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9168 == FAIL)
9169 {
9170 /* deletion will have messed up other windows */
9171 if (did_delete)
9172 {
9173#ifdef FEAT_WINDOWS
9174 wp->w_redr_status = TRUE;
9175#endif
9176 win_rest_invalid(W_NEXT(wp));
9177 }
9178 return FAIL;
9179 }
9180
9181 return OK;
9182}
9183
9184/*
9185 * delete "line_count" window lines at "row" in window "wp"
9186 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9187 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9188 * scrolling
9189 * Return OK for success, FAIL if the lines are not deleted.
9190 */
9191 int
9192win_del_lines(wp, row, line_count, invalid, mayclear)
9193 win_T *wp;
9194 int row;
9195 int line_count;
9196 int invalid;
9197 int mayclear;
9198{
9199 int retval;
9200
9201 if (invalid)
9202 wp->w_lines_valid = 0;
9203
9204 if (line_count > wp->w_height - row)
9205 line_count = wp->w_height - row;
9206
9207 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9208 if (retval != MAYBE)
9209 return retval;
9210
9211 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9212 (int)Rows, FALSE, NULL) == FAIL)
9213 return FAIL;
9214
9215#ifdef FEAT_WINDOWS
9216 /*
9217 * If there are windows or status lines below, try to put them at the
9218 * correct place. If we can't do that, they have to be redrawn.
9219 */
9220 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9221 {
9222 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9223 line_count, (int)Rows, NULL) == FAIL)
9224 {
9225 wp->w_redr_status = TRUE;
9226 win_rest_invalid(wp->w_next);
9227 }
9228 }
9229 /*
9230 * If this is the last window and there is no status line, redraw the
9231 * command line later.
9232 */
9233 else
9234#endif
9235 redraw_cmdline = TRUE;
9236 return OK;
9237}
9238
9239/*
9240 * Common code for win_ins_lines() and win_del_lines().
9241 * Returns OK or FAIL when the work has been done.
9242 * Returns MAYBE when not finished yet.
9243 */
9244 static int
9245win_do_lines(wp, row, line_count, mayclear, del)
9246 win_T *wp;
9247 int row;
9248 int line_count;
9249 int mayclear;
9250 int del;
9251{
9252 int retval;
9253
9254 if (!redrawing() || line_count <= 0)
9255 return FAIL;
9256
9257 /* only a few lines left: redraw is faster */
9258 if (mayclear && Rows - line_count < 5
9259#ifdef FEAT_VERTSPLIT
9260 && wp->w_width == Columns
9261#endif
9262 )
9263 {
9264 screenclear(); /* will set wp->w_lines_valid to 0 */
9265 return FAIL;
9266 }
9267
9268 /*
9269 * Delete all remaining lines
9270 */
9271 if (row + line_count >= wp->w_height)
9272 {
9273 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9274 W_WINCOL(wp), (int)W_ENDCOL(wp),
9275 ' ', ' ', 0);
9276 return OK;
9277 }
9278
9279 /*
9280 * when scrolling, the message on the command line should be cleared,
9281 * otherwise it will stay there forever.
9282 */
9283 clear_cmdline = TRUE;
9284
9285 /*
9286 * If the terminal can set a scroll region, use that.
9287 * Always do this in a vertically split window. This will redraw from
9288 * ScreenLines[] when t_CV isn't defined. That's faster than using
9289 * win_line().
9290 * Don't use a scroll region when we are going to redraw the text, writing
9291 * a character in the lower right corner of the scroll region causes a
9292 * scroll-up in the DJGPP version.
9293 */
9294 if (scroll_region
9295#ifdef FEAT_VERTSPLIT
9296 || W_WIDTH(wp) != Columns
9297#endif
9298 )
9299 {
9300#ifdef FEAT_VERTSPLIT
9301 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9302#endif
9303 scroll_region_set(wp, row);
9304 if (del)
9305 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9306 wp->w_height - row, FALSE, wp);
9307 else
9308 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9309 wp->w_height - row, wp);
9310#ifdef FEAT_VERTSPLIT
9311 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9312#endif
9313 scroll_region_reset();
9314 return retval;
9315 }
9316
9317#ifdef FEAT_WINDOWS
9318 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9319 return FAIL;
9320#endif
9321
9322 return MAYBE;
9323}
9324
9325/*
9326 * window 'wp' and everything after it is messed up, mark it for redraw
9327 */
9328 static void
9329win_rest_invalid(wp)
9330 win_T *wp;
9331{
9332#ifdef FEAT_WINDOWS
9333 while (wp != NULL)
9334#else
9335 if (wp != NULL)
9336#endif
9337 {
9338 redraw_win_later(wp, NOT_VALID);
9339#ifdef FEAT_WINDOWS
9340 wp->w_redr_status = TRUE;
9341 wp = wp->w_next;
9342#endif
9343 }
9344 redraw_cmdline = TRUE;
9345}
9346
9347/*
9348 * The rest of the routines in this file perform screen manipulations. The
9349 * given operation is performed physically on the screen. The corresponding
9350 * change is also made to the internal screen image. In this way, the editor
9351 * anticipates the effect of editing changes on the appearance of the screen.
9352 * That way, when we call screenupdate a complete redraw isn't usually
9353 * necessary. Another advantage is that we can keep adding code to anticipate
9354 * screen changes, and in the meantime, everything still works.
9355 */
9356
9357/*
9358 * types for inserting or deleting lines
9359 */
9360#define USE_T_CAL 1
9361#define USE_T_CDL 2
9362#define USE_T_AL 3
9363#define USE_T_CE 4
9364#define USE_T_DL 5
9365#define USE_T_SR 6
9366#define USE_NL 7
9367#define USE_T_CD 8
9368#define USE_REDRAW 9
9369
9370/*
9371 * insert lines on the screen and update ScreenLines[]
9372 * 'end' is the line after the scrolled part. Normally it is Rows.
9373 * When scrolling region used 'off' is the offset from the top for the region.
9374 * 'row' and 'end' are relative to the start of the region.
9375 *
9376 * return FAIL for failure, OK for success.
9377 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009378 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379screen_ins_lines(off, row, line_count, end, wp)
9380 int off;
9381 int row;
9382 int line_count;
9383 int end;
9384 win_T *wp; /* NULL or window to use width from */
9385{
9386 int i;
9387 int j;
9388 unsigned temp;
9389 int cursor_row;
9390 int type;
9391 int result_empty;
9392 int can_ce = can_clear(T_CE);
9393
9394 /*
9395 * FAIL if
9396 * - there is no valid screen
9397 * - the screen has to be redrawn completely
9398 * - the line count is less than one
9399 * - the line count is more than 'ttyscroll'
9400 */
9401 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9402 return FAIL;
9403
9404 /*
9405 * There are seven ways to insert lines:
9406 * 0. When in a vertically split window and t_CV isn't set, redraw the
9407 * characters from ScreenLines[].
9408 * 1. Use T_CD (clear to end of display) if it exists and the result of
9409 * the insert is just empty lines
9410 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9411 * present or line_count > 1. It looks better if we do all the inserts
9412 * at once.
9413 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9414 * insert is just empty lines and T_CE is not present or line_count >
9415 * 1.
9416 * 4. Use T_AL (insert line) if it exists.
9417 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9418 * just empty lines.
9419 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9420 * just empty lines.
9421 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9422 * the 'da' flag is not set or we have clear line capability.
9423 * 8. redraw the characters from ScreenLines[].
9424 *
9425 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9426 * the scrollbar for the window. It does have insert line, use that if it
9427 * exists.
9428 */
9429 result_empty = (row + line_count >= end);
9430#ifdef FEAT_VERTSPLIT
9431 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9432 type = USE_REDRAW;
9433 else
9434#endif
9435 if (can_clear(T_CD) && result_empty)
9436 type = USE_T_CD;
9437 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9438 type = USE_T_CAL;
9439 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9440 type = USE_T_CDL;
9441 else if (*T_AL != NUL)
9442 type = USE_T_AL;
9443 else if (can_ce && result_empty)
9444 type = USE_T_CE;
9445 else if (*T_DL != NUL && result_empty)
9446 type = USE_T_DL;
9447 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9448 type = USE_T_SR;
9449 else
9450 return FAIL;
9451
9452 /*
9453 * For clearing the lines screen_del_lines() is used. This will also take
9454 * care of t_db if necessary.
9455 */
9456 if (type == USE_T_CD || type == USE_T_CDL ||
9457 type == USE_T_CE || type == USE_T_DL)
9458 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9459
9460 /*
9461 * If text is retained below the screen, first clear or delete as many
9462 * lines at the bottom of the window as are about to be inserted so that
9463 * the deleted lines won't later surface during a screen_del_lines.
9464 */
9465 if (*T_DB)
9466 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9467
9468#ifdef FEAT_CLIPBOARD
9469 /* Remove a modeless selection when inserting lines halfway the screen
9470 * or not the full width of the screen. */
9471 if (off + row > 0
9472# ifdef FEAT_VERTSPLIT
9473 || (wp != NULL && wp->w_width != Columns)
9474# endif
9475 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009476 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 else
9478 clip_scroll_selection(-line_count);
9479#endif
9480
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481#ifdef FEAT_GUI
9482 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9483 * scrolling is actually carried out. */
9484 gui_dont_update_cursor();
9485#endif
9486
9487 if (*T_CCS != NUL) /* cursor relative to region */
9488 cursor_row = row;
9489 else
9490 cursor_row = row + off;
9491
9492 /*
9493 * Shift LineOffset[] line_count down to reflect the inserted lines.
9494 * Clear the inserted lines in ScreenLines[].
9495 */
9496 row += off;
9497 end += off;
9498 for (i = 0; i < line_count; ++i)
9499 {
9500#ifdef FEAT_VERTSPLIT
9501 if (wp != NULL && wp->w_width != Columns)
9502 {
9503 /* need to copy part of a line */
9504 j = end - 1 - i;
9505 while ((j -= line_count) >= row)
9506 linecopy(j + line_count, j, wp);
9507 j += line_count;
9508 if (can_clear((char_u *)" "))
9509 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9510 else
9511 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9512 LineWraps[j] = FALSE;
9513 }
9514 else
9515#endif
9516 {
9517 j = end - 1 - i;
9518 temp = LineOffset[j];
9519 while ((j -= line_count) >= row)
9520 {
9521 LineOffset[j + line_count] = LineOffset[j];
9522 LineWraps[j + line_count] = LineWraps[j];
9523 }
9524 LineOffset[j + line_count] = temp;
9525 LineWraps[j + line_count] = FALSE;
9526 if (can_clear((char_u *)" "))
9527 lineclear(temp, (int)Columns);
9528 else
9529 lineinvalid(temp, (int)Columns);
9530 }
9531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
9533 screen_stop_highlight();
9534 windgoto(cursor_row, 0);
9535
9536#ifdef FEAT_VERTSPLIT
9537 /* redraw the characters */
9538 if (type == USE_REDRAW)
9539 redraw_block(row, end, wp);
9540 else
9541#endif
9542 if (type == USE_T_CAL)
9543 {
9544 term_append_lines(line_count);
9545 screen_start(); /* don't know where cursor is now */
9546 }
9547 else
9548 {
9549 for (i = 0; i < line_count; i++)
9550 {
9551 if (type == USE_T_AL)
9552 {
9553 if (i && cursor_row != 0)
9554 windgoto(cursor_row, 0);
9555 out_str(T_AL);
9556 }
9557 else /* type == USE_T_SR */
9558 out_str(T_SR);
9559 screen_start(); /* don't know where cursor is now */
9560 }
9561 }
9562
9563 /*
9564 * With scroll-reverse and 'da' flag set we need to clear the lines that
9565 * have been scrolled down into the region.
9566 */
9567 if (type == USE_T_SR && *T_DA)
9568 {
9569 for (i = 0; i < line_count; ++i)
9570 {
9571 windgoto(off + i, 0);
9572 out_str(T_CE);
9573 screen_start(); /* don't know where cursor is now */
9574 }
9575 }
9576
9577#ifdef FEAT_GUI
9578 gui_can_update_cursor();
9579 if (gui.in_use)
9580 out_flush(); /* always flush after a scroll */
9581#endif
9582 return OK;
9583}
9584
9585/*
9586 * delete lines on the screen and update ScreenLines[]
9587 * 'end' is the line after the scrolled part. Normally it is Rows.
9588 * When scrolling region used 'off' is the offset from the top for the region.
9589 * 'row' and 'end' are relative to the start of the region.
9590 *
9591 * Return OK for success, FAIL if the lines are not deleted.
9592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 int
9594screen_del_lines(off, row, line_count, end, force, wp)
9595 int off;
9596 int row;
9597 int line_count;
9598 int end;
9599 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009600 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 int j;
9603 int i;
9604 unsigned temp;
9605 int cursor_row;
9606 int cursor_end;
9607 int result_empty; /* result is empty until end of region */
9608 int can_delete; /* deleting line codes can be used */
9609 int type;
9610
9611 /*
9612 * FAIL if
9613 * - there is no valid screen
9614 * - the screen has to be redrawn completely
9615 * - the line count is less than one
9616 * - the line count is more than 'ttyscroll'
9617 */
9618 if (!screen_valid(TRUE) || line_count <= 0 ||
9619 (!force && line_count > p_ttyscroll))
9620 return FAIL;
9621
9622 /*
9623 * Check if the rest of the current region will become empty.
9624 */
9625 result_empty = row + line_count >= end;
9626
9627 /*
9628 * We can delete lines only when 'db' flag not set or when 'ce' option
9629 * available.
9630 */
9631 can_delete = (*T_DB == NUL || can_clear(T_CE));
9632
9633 /*
9634 * There are six ways to delete lines:
9635 * 0. When in a vertically split window and t_CV isn't set, redraw the
9636 * characters from ScreenLines[].
9637 * 1. Use T_CD if it exists and the result is empty.
9638 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9639 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9640 * none of the other ways work.
9641 * 4. Use T_CE (erase line) if the result is empty.
9642 * 5. Use T_DL (delete line) if it exists.
9643 * 6. redraw the characters from ScreenLines[].
9644 */
9645#ifdef FEAT_VERTSPLIT
9646 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9647 type = USE_REDRAW;
9648 else
9649#endif
9650 if (can_clear(T_CD) && result_empty)
9651 type = USE_T_CD;
9652#if defined(__BEOS__) && defined(BEOS_DR8)
9653 /*
9654 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9655 * its internal termcap... this works okay for tests which test *T_DB !=
9656 * NUL. It has the disadvantage that the user cannot use any :set t_*
9657 * command to get T_DB (back) to empty_option, only :set term=... will do
9658 * the trick...
9659 * Anyway, this hack will hopefully go away with the next OS release.
9660 * (Olaf Seibert)
9661 */
9662 else if (row == 0 && T_DB == empty_option
9663 && (line_count == 1 || *T_CDL == NUL))
9664#else
9665 else if (row == 0 && (
9666#ifndef AMIGA
9667 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9668 * up, so use delete-line command */
9669 line_count == 1 ||
9670#endif
9671 *T_CDL == NUL))
9672#endif
9673 type = USE_NL;
9674 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9675 type = USE_T_CDL;
9676 else if (can_clear(T_CE) && result_empty
9677#ifdef FEAT_VERTSPLIT
9678 && (wp == NULL || wp->w_width == Columns)
9679#endif
9680 )
9681 type = USE_T_CE;
9682 else if (*T_DL != NUL && can_delete)
9683 type = USE_T_DL;
9684 else if (*T_CDL != NUL && can_delete)
9685 type = USE_T_CDL;
9686 else
9687 return FAIL;
9688
9689#ifdef FEAT_CLIPBOARD
9690 /* Remove a modeless selection when deleting lines halfway the screen or
9691 * not the full width of the screen. */
9692 if (off + row > 0
9693# ifdef FEAT_VERTSPLIT
9694 || (wp != NULL && wp->w_width != Columns)
9695# endif
9696 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009697 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 else
9699 clip_scroll_selection(line_count);
9700#endif
9701
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702#ifdef FEAT_GUI
9703 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9704 * scrolling is actually carried out. */
9705 gui_dont_update_cursor();
9706#endif
9707
9708 if (*T_CCS != NUL) /* cursor relative to region */
9709 {
9710 cursor_row = row;
9711 cursor_end = end;
9712 }
9713 else
9714 {
9715 cursor_row = row + off;
9716 cursor_end = end + off;
9717 }
9718
9719 /*
9720 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9721 * Clear the inserted lines in ScreenLines[].
9722 */
9723 row += off;
9724 end += off;
9725 for (i = 0; i < line_count; ++i)
9726 {
9727#ifdef FEAT_VERTSPLIT
9728 if (wp != NULL && wp->w_width != Columns)
9729 {
9730 /* need to copy part of a line */
9731 j = row + i;
9732 while ((j += line_count) <= end - 1)
9733 linecopy(j - line_count, j, wp);
9734 j -= line_count;
9735 if (can_clear((char_u *)" "))
9736 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9737 else
9738 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9739 LineWraps[j] = FALSE;
9740 }
9741 else
9742#endif
9743 {
9744 /* whole width, moving the line pointers is faster */
9745 j = row + i;
9746 temp = LineOffset[j];
9747 while ((j += line_count) <= end - 1)
9748 {
9749 LineOffset[j - line_count] = LineOffset[j];
9750 LineWraps[j - line_count] = LineWraps[j];
9751 }
9752 LineOffset[j - line_count] = temp;
9753 LineWraps[j - line_count] = FALSE;
9754 if (can_clear((char_u *)" "))
9755 lineclear(temp, (int)Columns);
9756 else
9757 lineinvalid(temp, (int)Columns);
9758 }
9759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 screen_stop_highlight();
9762
9763#ifdef FEAT_VERTSPLIT
9764 /* redraw the characters */
9765 if (type == USE_REDRAW)
9766 redraw_block(row, end, wp);
9767 else
9768#endif
9769 if (type == USE_T_CD) /* delete the lines */
9770 {
9771 windgoto(cursor_row, 0);
9772 out_str(T_CD);
9773 screen_start(); /* don't know where cursor is now */
9774 }
9775 else if (type == USE_T_CDL)
9776 {
9777 windgoto(cursor_row, 0);
9778 term_delete_lines(line_count);
9779 screen_start(); /* don't know where cursor is now */
9780 }
9781 /*
9782 * Deleting lines at top of the screen or scroll region: Just scroll
9783 * the whole screen (scroll region) up by outputting newlines on the
9784 * last line.
9785 */
9786 else if (type == USE_NL)
9787 {
9788 windgoto(cursor_end - 1, 0);
9789 for (i = line_count; --i >= 0; )
9790 out_char('\n'); /* cursor will remain on same line */
9791 }
9792 else
9793 {
9794 for (i = line_count; --i >= 0; )
9795 {
9796 if (type == USE_T_DL)
9797 {
9798 windgoto(cursor_row, 0);
9799 out_str(T_DL); /* delete a line */
9800 }
9801 else /* type == USE_T_CE */
9802 {
9803 windgoto(cursor_row + i, 0);
9804 out_str(T_CE); /* erase a line */
9805 }
9806 screen_start(); /* don't know where cursor is now */
9807 }
9808 }
9809
9810 /*
9811 * If the 'db' flag is set, we need to clear the lines that have been
9812 * scrolled up at the bottom of the region.
9813 */
9814 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9815 {
9816 for (i = line_count; i > 0; --i)
9817 {
9818 windgoto(cursor_end - i, 0);
9819 out_str(T_CE); /* erase a line */
9820 screen_start(); /* don't know where cursor is now */
9821 }
9822 }
9823
9824#ifdef FEAT_GUI
9825 gui_can_update_cursor();
9826 if (gui.in_use)
9827 out_flush(); /* always flush after a scroll */
9828#endif
9829
9830 return OK;
9831}
9832
9833/*
9834 * show the current mode and ruler
9835 *
9836 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9837 * If clear_cmdline is FALSE there may be a message there that needs to be
9838 * cleared only if a mode is shown.
9839 * Return the length of the message (0 if no message).
9840 */
9841 int
9842showmode()
9843{
9844 int need_clear;
9845 int length = 0;
9846 int do_mode;
9847 int attr;
9848 int nwr_save;
9849#ifdef FEAT_INS_EXPAND
9850 int sub_attr;
9851#endif
9852
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009853 do_mode = ((p_smd && msg_silent == 0)
9854 && ((State & INSERT)
9855 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009856 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 if (do_mode || Recording)
9858 {
9859 /*
9860 * Don't show mode right now, when not redrawing or inside a mapping.
9861 * Call char_avail() only when we are going to show something, because
9862 * it takes a bit of time.
9863 */
9864 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9865 {
9866 redraw_cmdline = TRUE; /* show mode later */
9867 return 0;
9868 }
9869
9870 nwr_save = need_wait_return;
9871
9872 /* wait a bit before overwriting an important message */
9873 check_for_delay(FALSE);
9874
9875 /* if the cmdline is more than one line high, erase top lines */
9876 need_clear = clear_cmdline;
9877 if (clear_cmdline && cmdline_row < Rows - 1)
9878 msg_clr_cmdline(); /* will reset clear_cmdline */
9879
9880 /* Position on the last line in the window, column 0 */
9881 msg_pos_mode();
9882 cursor_off();
9883 attr = hl_attr(HLF_CM); /* Highlight mode */
9884 if (do_mode)
9885 {
9886 MSG_PUTS_ATTR("--", attr);
9887#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009888 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009889# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009890 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009891# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009892 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009893# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009894 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009895# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 MSG_PUTS_ATTR(" IM", attr);
9897# else
9898 MSG_PUTS_ATTR(" XIM", attr);
9899# endif
9900#endif
9901#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9902 if (gui.in_use)
9903 {
9904 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009905 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 }
9907#endif
9908#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009909 /* CTRL-X in Insert mode */
9910 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
9912 /* These messages can get long, avoid a wrap in a narrow
9913 * window. Prefer showing edit_submode_extra. */
9914 length = (Rows - msg_row) * Columns - 3;
9915 if (edit_submode_extra != NULL)
9916 length -= vim_strsize(edit_submode_extra);
9917 if (length > 0)
9918 {
9919 if (edit_submode_pre != NULL)
9920 length -= vim_strsize(edit_submode_pre);
9921 if (length - vim_strsize(edit_submode) > 0)
9922 {
9923 if (edit_submode_pre != NULL)
9924 msg_puts_attr(edit_submode_pre, attr);
9925 msg_puts_attr(edit_submode, attr);
9926 }
9927 if (edit_submode_extra != NULL)
9928 {
9929 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9930 if ((int)edit_submode_highl < (int)HLF_COUNT)
9931 sub_attr = hl_attr(edit_submode_highl);
9932 else
9933 sub_attr = attr;
9934 msg_puts_attr(edit_submode_extra, sub_attr);
9935 }
9936 }
9937 length = 0;
9938 }
9939 else
9940#endif
9941 {
9942#ifdef FEAT_VREPLACE
9943 if (State & VREPLACE_FLAG)
9944 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9945 else
9946#endif
9947 if (State & REPLACE_FLAG)
9948 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9949 else if (State & INSERT)
9950 {
9951#ifdef FEAT_RIGHTLEFT
9952 if (p_ri)
9953 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9954#endif
9955 MSG_PUTS_ATTR(_(" INSERT"), attr);
9956 }
9957 else if (restart_edit == 'I')
9958 MSG_PUTS_ATTR(_(" (insert)"), attr);
9959 else if (restart_edit == 'R')
9960 MSG_PUTS_ATTR(_(" (replace)"), attr);
9961 else if (restart_edit == 'V')
9962 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9963#ifdef FEAT_RIGHTLEFT
9964 if (p_hkmap)
9965 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9966# ifdef FEAT_FKMAP
9967 if (p_fkmap)
9968 MSG_PUTS_ATTR(farsi_text_5, attr);
9969# endif
9970#endif
9971#ifdef FEAT_KEYMAP
9972 if (State & LANGMAP)
9973 {
9974# ifdef FEAT_ARABIC
9975 if (curwin->w_p_arab)
9976 MSG_PUTS_ATTR(_(" Arabic"), attr);
9977 else
9978# endif
9979 MSG_PUTS_ATTR(_(" (lang)"), attr);
9980 }
9981#endif
9982 if ((State & INSERT) && p_paste)
9983 MSG_PUTS_ATTR(_(" (paste)"), attr);
9984
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 if (VIsual_active)
9986 {
9987 char *p;
9988
9989 /* Don't concatenate separate words to avoid translation
9990 * problems. */
9991 switch ((VIsual_select ? 4 : 0)
9992 + (VIsual_mode == Ctrl_V) * 2
9993 + (VIsual_mode == 'V'))
9994 {
9995 case 0: p = N_(" VISUAL"); break;
9996 case 1: p = N_(" VISUAL LINE"); break;
9997 case 2: p = N_(" VISUAL BLOCK"); break;
9998 case 4: p = N_(" SELECT"); break;
9999 case 5: p = N_(" SELECT LINE"); break;
10000 default: p = N_(" SELECT BLOCK"); break;
10001 }
10002 MSG_PUTS_ATTR(_(p), attr);
10003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 MSG_PUTS_ATTR(" --", attr);
10005 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010006
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 need_clear = TRUE;
10008 }
10009 if (Recording
10010#ifdef FEAT_INS_EXPAND
10011 && edit_submode == NULL /* otherwise it gets too long */
10012#endif
10013 )
10014 {
10015 MSG_PUTS_ATTR(_("recording"), attr);
10016 need_clear = TRUE;
10017 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010018
10019 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (need_clear || clear_cmdline)
10021 msg_clr_eos();
10022 msg_didout = FALSE; /* overwrite this message */
10023 length = msg_col;
10024 msg_col = 0;
10025 need_wait_return = nwr_save; /* never ask for hit-return for this */
10026 }
10027 else if (clear_cmdline && msg_silent == 0)
10028 /* Clear the whole command line. Will reset "clear_cmdline". */
10029 msg_clr_cmdline();
10030
10031#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 /* In Visual mode the size of the selected area must be redrawn. */
10033 if (VIsual_active)
10034 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035
10036 /* If the last window has no status line, the ruler is after the mode
10037 * message and must be redrawn */
10038 if (redrawing()
10039# ifdef FEAT_WINDOWS
10040 && lastwin->w_status_height == 0
10041# endif
10042 )
10043 win_redr_ruler(lastwin, TRUE);
10044#endif
10045 redraw_cmdline = FALSE;
10046 clear_cmdline = FALSE;
10047
10048 return length;
10049}
10050
10051/*
10052 * Position for a mode message.
10053 */
10054 static void
10055msg_pos_mode()
10056{
10057 msg_col = 0;
10058 msg_row = Rows - 1;
10059}
10060
10061/*
10062 * Delete mode message. Used when ESC is typed which is expected to end
10063 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010064 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 */
10066 void
10067unshowmode(force)
10068 int force;
10069{
10070 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010071 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 */
10073 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10074 redraw_cmdline = TRUE; /* delete mode later */
10075 else
10076 {
10077 msg_pos_mode();
10078 if (Recording)
10079 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
10080 msg_clr_eos();
10081 }
10082}
10083
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010084#if defined(FEAT_WINDOWS)
10085/*
10086 * Draw the tab pages line at the top of the Vim window.
10087 */
10088 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010089draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010090{
10091 int tabcount = 0;
10092 tabpage_T *tp;
10093 int tabwidth;
10094 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010095 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010096 int attr;
10097 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010098 win_T *cwp;
10099 int wincount;
10100 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010101 int c;
10102 int len;
10103 int attr_sel = hl_attr(HLF_TPS);
10104 int attr_nosel = hl_attr(HLF_TP);
10105 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010106 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010107 int room;
10108 int use_sep_chars = (t_colors < 8
10109#ifdef FEAT_GUI
10110 && !gui.in_use
10111#endif
10112 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010113
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010114 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010115
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010116#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010117 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010118 if (gui_use_tabline())
10119 {
10120 gui_update_tabline();
10121 return;
10122 }
10123#endif
10124
10125 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010126 return;
10127
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010128#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010129
10130 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10131 for (scol = 0; scol < Columns; ++scol)
10132 TabPageIdxs[scol] = 0;
10133
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010134 /* Use the 'tabline' option if it's set. */
10135 if (*p_tal != NUL)
10136 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010137 int save_called_emsg = called_emsg;
10138
10139 /* Check for an error. If there is one we would loop in redrawing the
10140 * screen. Avoid that by making 'tabline' empty. */
10141 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010142 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010143 if (called_emsg)
10144 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010145 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010146 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010147 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010148 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010149#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010150 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010151 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10152 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010153
Bram Moolenaar238a5642006-02-21 22:12:05 +000010154 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10155 if (tabwidth < 6)
10156 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010157
Bram Moolenaar238a5642006-02-21 22:12:05 +000010158 attr = attr_nosel;
10159 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010160 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010161 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10162 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010163 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010164 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010165
Bram Moolenaar238a5642006-02-21 22:12:05 +000010166 if (tp->tp_topframe == topframe)
10167 attr = attr_sel;
10168 if (use_sep_chars && col > 0)
10169 screen_putchar('|', 0, col++, attr);
10170
10171 if (tp->tp_topframe != topframe)
10172 attr = attr_nosel;
10173
10174 screen_putchar(' ', 0, col++, attr);
10175
10176 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010177 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010178 cwp = curwin;
10179 wp = firstwin;
10180 }
10181 else
10182 {
10183 cwp = tp->tp_curwin;
10184 wp = tp->tp_firstwin;
10185 }
10186
10187 modified = FALSE;
10188 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10189 if (bufIsChanged(wp->w_buffer))
10190 modified = TRUE;
10191 if (modified || wincount > 1)
10192 {
10193 if (wincount > 1)
10194 {
10195 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010196 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010197 if (col + len >= Columns - 3)
10198 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010199 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010200#if defined(FEAT_SYN_HL)
Bram Moolenaare0f14822014-08-06 13:20:56 +020010201 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010202#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010203 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010204#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010205 );
10206 col += len;
10207 }
10208 if (modified)
10209 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10210 screen_putchar(' ', 0, col++, attr);
10211 }
10212
10213 room = scol - col + tabwidth - 1;
10214 if (room > 0)
10215 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010216 /* Get buffer name in NameBuff[] */
10217 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010218 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010219 len = vim_strsize(NameBuff);
10220 p = NameBuff;
10221#ifdef FEAT_MBYTE
10222 if (has_mbyte)
10223 while (len > room)
10224 {
10225 len -= ptr2cells(p);
10226 mb_ptr_adv(p);
10227 }
10228 else
10229#endif
10230 if (len > room)
10231 {
10232 p += len - room;
10233 len = room;
10234 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010235 if (len > Columns - col - 1)
10236 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010238 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010239 col += len;
10240 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010241 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010242
10243 /* Store the tab page number in TabPageIdxs[], so that
10244 * jump_to_mouse() knows where each one is. */
10245 ++tabcount;
10246 while (scol < col)
10247 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010248 }
10249
Bram Moolenaar238a5642006-02-21 22:12:05 +000010250 if (use_sep_chars)
10251 c = '_';
10252 else
10253 c = ' ';
10254 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010255
10256 /* Put an "X" for closing the current tab if there are several. */
10257 if (first_tabpage->tp_next != NULL)
10258 {
10259 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10260 TabPageIdxs[Columns - 1] = -999;
10261 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010262 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010263
10264 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10265 * set. */
10266 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010267}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010268
10269/*
10270 * Get buffer name for "buf" into NameBuff[].
10271 * Takes care of special buffer names and translates special characters.
10272 */
10273 void
10274get_trans_bufname(buf)
10275 buf_T *buf;
10276{
10277 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010278 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010279 else
10280 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10281 trans_characters(NameBuff, MAXPATHL);
10282}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010283#endif
10284
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10286/*
10287 * Get the character to use in a status line. Get its attributes in "*attr".
10288 */
10289 static int
10290fillchar_status(attr, is_curwin)
10291 int *attr;
10292 int is_curwin;
10293{
10294 int fill;
10295 if (is_curwin)
10296 {
10297 *attr = hl_attr(HLF_S);
10298 fill = fill_stl;
10299 }
10300 else
10301 {
10302 *attr = hl_attr(HLF_SNC);
10303 fill = fill_stlnc;
10304 }
10305 /* Use fill when there is highlighting, and highlighting of current
10306 * window differs, or the fillchars differ, or this is not the
10307 * current window */
10308 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10309 || !is_curwin || firstwin == lastwin)
10310 || (fill_stl != fill_stlnc)))
10311 return fill;
10312 if (is_curwin)
10313 return '^';
10314 return '=';
10315}
10316#endif
10317
10318#ifdef FEAT_VERTSPLIT
10319/*
10320 * Get the character to use in a separator between vertically split windows.
10321 * Get its attributes in "*attr".
10322 */
10323 static int
10324fillchar_vsep(attr)
10325 int *attr;
10326{
10327 *attr = hl_attr(HLF_C);
10328 if (*attr == 0 && fill_vert == ' ')
10329 return '|';
10330 else
10331 return fill_vert;
10332}
10333#endif
10334
10335/*
10336 * Return TRUE if redrawing should currently be done.
10337 */
10338 int
10339redrawing()
10340{
10341 return (!RedrawingDisabled
10342 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10343}
10344
10345/*
10346 * Return TRUE if printing messages should currently be done.
10347 */
10348 int
10349messaging()
10350{
10351 return (!(p_lz && char_avail() && !KeyTyped));
10352}
10353
10354/*
10355 * Show current status info in ruler and various other places
10356 * If always is FALSE, only show ruler if position has changed.
10357 */
10358 void
10359showruler(always)
10360 int always;
10361{
10362 if (!always && !redrawing())
10363 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010364#ifdef FEAT_INS_EXPAND
10365 if (pum_visible())
10366 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010367# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010368 /* Don't redraw right now, do it later. */
10369 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010370# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010371 return;
10372 }
10373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010375 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010376 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010377 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 else
10380#endif
10381#ifdef FEAT_CMDL_INFO
10382 win_redr_ruler(curwin, always);
10383#endif
10384
10385#ifdef FEAT_TITLE
10386 if (need_maketitle
10387# ifdef FEAT_STL_OPT
10388 || (p_icon && (stl_syntax & STL_IN_ICON))
10389 || (p_title && (stl_syntax & STL_IN_TITLE))
10390# endif
10391 )
10392 maketitle();
10393#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010394#ifdef FEAT_WINDOWS
10395 /* Redraw the tab pages line if needed. */
10396 if (redraw_tabline)
10397 draw_tabline();
10398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
10401#ifdef FEAT_CMDL_INFO
10402 static void
10403win_redr_ruler(wp, always)
10404 win_T *wp;
10405 int always;
10406{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010407#define RULER_BUF_LEN 70
10408 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 int row;
10410 int fillchar;
10411 int attr;
10412 int empty_line = FALSE;
10413 colnr_T virtcol;
10414 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010415 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 int o;
10417#ifdef FEAT_VERTSPLIT
10418 int this_ru_col;
10419 int off = 0;
10420 int width = Columns;
10421# define WITH_OFF(x) x
10422# define WITH_WIDTH(x) x
10423#else
10424# define WITH_OFF(x) 0
10425# define WITH_WIDTH(x) Columns
10426# define this_ru_col ru_col
10427#endif
10428
10429 /* If 'ruler' off or redrawing disabled, don't do anything */
10430 if (!p_ru)
10431 return;
10432
10433 /*
10434 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10435 * after deleting lines, before cursor.lnum is corrected.
10436 */
10437 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10438 return;
10439
10440#ifdef FEAT_INS_EXPAND
10441 /* Don't draw the ruler while doing insert-completion, it might overwrite
10442 * the (long) mode message. */
10443# ifdef FEAT_WINDOWS
10444 if (wp == lastwin && lastwin->w_status_height == 0)
10445# endif
10446 if (edit_submode != NULL)
10447 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010448 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10449 if (pum_visible())
10450 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#endif
10452
10453#ifdef FEAT_STL_OPT
10454 if (*p_ruf)
10455 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010456 int save_called_emsg = called_emsg;
10457
10458 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010460 if (called_emsg)
10461 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010462 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010463 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 return;
10465 }
10466#endif
10467
10468 /*
10469 * Check if not in Insert mode and the line is empty (will show "0-1").
10470 */
10471 if (!(State & INSERT)
10472 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10473 empty_line = TRUE;
10474
10475 /*
10476 * Only draw the ruler when something changed.
10477 */
10478 validate_virtcol_win(wp);
10479 if ( redraw_cmdline
10480 || always
10481 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10482 || wp->w_cursor.col != wp->w_ru_cursor.col
10483 || wp->w_virtcol != wp->w_ru_virtcol
10484#ifdef FEAT_VIRTUALEDIT
10485 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10486#endif
10487 || wp->w_topline != wp->w_ru_topline
10488 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10489#ifdef FEAT_DIFF
10490 || wp->w_topfill != wp->w_ru_topfill
10491#endif
10492 || empty_line != wp->w_ru_empty)
10493 {
10494 cursor_off();
10495#ifdef FEAT_WINDOWS
10496 if (wp->w_status_height)
10497 {
10498 row = W_WINROW(wp) + wp->w_height;
10499 fillchar = fillchar_status(&attr, wp == curwin);
10500# ifdef FEAT_VERTSPLIT
10501 off = W_WINCOL(wp);
10502 width = W_WIDTH(wp);
10503# endif
10504 }
10505 else
10506#endif
10507 {
10508 row = Rows - 1;
10509 fillchar = ' ';
10510 attr = 0;
10511#ifdef FEAT_VERTSPLIT
10512 width = Columns;
10513 off = 0;
10514#endif
10515 }
10516
10517 /* In list mode virtcol needs to be recomputed */
10518 virtcol = wp->w_virtcol;
10519 if (wp->w_p_list && lcs_tab1 == NUL)
10520 {
10521 wp->w_p_list = FALSE;
10522 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10523 wp->w_p_list = TRUE;
10524 }
10525
10526 /*
10527 * Some sprintfs return the length, some return a pointer.
10528 * To avoid portability problems we use strlen() here.
10529 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010530 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10532 ? 0L
10533 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010534 len = STRLEN(buffer);
10535 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10537 (int)virtcol + 1);
10538
10539 /*
10540 * Add a "50%" if there is room for it.
10541 * On the last line, don't print in the last column (scrolls the
10542 * screen up on some terminals).
10543 */
10544 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010545 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 o = i + vim_strsize(buffer + i + 1);
10547#ifdef FEAT_WINDOWS
10548 if (wp->w_status_height == 0) /* can't use last char of screen */
10549#endif
10550 ++o;
10551#ifdef FEAT_VERTSPLIT
10552 this_ru_col = ru_col - (Columns - width);
10553 if (this_ru_col < 0)
10554 this_ru_col = 0;
10555#endif
10556 /* Never use more than half the window/screen width, leave the other
10557 * half for the filename. */
10558 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10559 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10560 if (this_ru_col + o < WITH_WIDTH(width))
10561 {
10562 while (this_ru_col + o < WITH_WIDTH(width))
10563 {
10564#ifdef FEAT_MBYTE
10565 if (has_mbyte)
10566 i += (*mb_char2bytes)(fillchar, buffer + i);
10567 else
10568#endif
10569 buffer[i++] = fillchar;
10570 ++o;
10571 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010572 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 }
10574 /* Truncate at window boundary. */
10575#ifdef FEAT_MBYTE
10576 if (has_mbyte)
10577 {
10578 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010579 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 {
10581 o += (*mb_ptr2cells)(buffer + i);
10582 if (this_ru_col + o > WITH_WIDTH(width))
10583 {
10584 buffer[i] = NUL;
10585 break;
10586 }
10587 }
10588 }
10589 else
10590#endif
10591 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10592 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10593
10594 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10595 i = redraw_cmdline;
10596 screen_fill(row, row + 1,
10597 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10598 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10599 fillchar, fillchar, attr);
10600 /* don't redraw the cmdline because of showing the ruler */
10601 redraw_cmdline = i;
10602 wp->w_ru_cursor = wp->w_cursor;
10603 wp->w_ru_virtcol = wp->w_virtcol;
10604 wp->w_ru_empty = empty_line;
10605 wp->w_ru_topline = wp->w_topline;
10606 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10607#ifdef FEAT_DIFF
10608 wp->w_ru_topfill = wp->w_topfill;
10609#endif
10610 }
10611}
10612#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010613
10614#if defined(FEAT_LINEBREAK) || defined(PROTO)
10615/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010616 * Return the width of the 'number' and 'relativenumber' column.
10617 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010618 * Otherwise it depends on 'numberwidth' and the line count.
10619 */
10620 int
10621number_width(wp)
10622 win_T *wp;
10623{
10624 int n;
10625 linenr_T lnum;
10626
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010627 if (wp->w_p_rnu && !wp->w_p_nu)
10628 /* cursor line shows "0" */
10629 lnum = wp->w_height;
10630 else
10631 /* cursor line shows absolute line number */
10632 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010633
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010634 if (lnum == wp->w_nrwidth_line_count)
10635 return wp->w_nrwidth_width;
10636 wp->w_nrwidth_line_count = lnum;
10637
10638 n = 0;
10639 do
10640 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010641 lnum /= 10;
10642 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010643 } while (lnum > 0);
10644
10645 /* 'numberwidth' gives the minimal width plus one */
10646 if (n < wp->w_p_nuw - 1)
10647 n = wp->w_p_nuw - 1;
10648
10649 wp->w_nrwidth_width = n;
10650 return n;
10651}
10652#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010653
10654/*
10655 * Return the current cursor column. This is the actual position on the
10656 * screen. First column is 0.
10657 */
10658 int
10659screen_screencol()
10660{
10661 return screen_cur_col;
10662}
10663
10664/*
10665 * Return the current cursor row. This is the actual position on the screen.
10666 * First row is 0.
10667 */
10668 int
10669screen_screenrow()
10670{
10671 return screen_cur_row;
10672}