blob: dd8d9a54181a59f9fdc30680b87d514b16c43b85 [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 Moolenaar6ee10162007-07-26 20:58:42 +0000142#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
1772 /* match in fixed position might need redraw */
1773 || wp->w_match_head != NULL
1774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 )))))
1776 {
1777#ifdef FEAT_SEARCH_EXTRA
1778 if (lnum == mod_top)
1779 top_to_mod = FALSE;
1780#endif
1781
1782 /*
1783 * When at start of changed lines: May scroll following lines
1784 * up or down to minimize redrawing.
1785 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001786 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 */
1788 if (lnum == mod_top
1789 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001790 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 int old_rows = 0;
1793 int new_rows = 0;
1794 int xtra_rows;
1795 linenr_T l;
1796
1797 /* Count the old number of window rows, using w_lines[], which
1798 * should still contain the sizes for the lines as they are
1799 * currently displayed. */
1800 for (i = idx; i < wp->w_lines_valid; ++i)
1801 {
1802 /* Only valid lines have a meaningful wl_lnum. Invalid
1803 * lines are part of the changed area. */
1804 if (wp->w_lines[i].wl_valid
1805 && wp->w_lines[i].wl_lnum == mod_bot)
1806 break;
1807 old_rows += wp->w_lines[i].wl_size;
1808#ifdef FEAT_FOLDING
1809 if (wp->w_lines[i].wl_valid
1810 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1811 {
1812 /* Must have found the last valid entry above mod_bot.
1813 * Add following invalid entries. */
1814 ++i;
1815 while (i < wp->w_lines_valid
1816 && !wp->w_lines[i].wl_valid)
1817 old_rows += wp->w_lines[i++].wl_size;
1818 break;
1819 }
1820#endif
1821 }
1822
1823 if (i >= wp->w_lines_valid)
1824 {
1825 /* We can't find a valid line below the changed lines,
1826 * need to redraw until the end of the window.
1827 * Inserting/deleting lines has no use. */
1828 bot_start = 0;
1829 }
1830 else
1831 {
1832 /* Able to count old number of rows: Count new window
1833 * rows, and may insert/delete lines */
1834 j = idx;
1835 for (l = lnum; l < mod_bot; ++l)
1836 {
1837#ifdef FEAT_FOLDING
1838 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1839 ++new_rows;
1840 else
1841#endif
1842#ifdef FEAT_DIFF
1843 if (l == wp->w_topline)
1844 new_rows += plines_win_nofill(wp, l, TRUE)
1845 + wp->w_topfill;
1846 else
1847#endif
1848 new_rows += plines_win(wp, l, TRUE);
1849 ++j;
1850 if (new_rows > wp->w_height - row - 2)
1851 {
1852 /* it's getting too much, must redraw the rest */
1853 new_rows = 9999;
1854 break;
1855 }
1856 }
1857 xtra_rows = new_rows - old_rows;
1858 if (xtra_rows < 0)
1859 {
1860 /* May scroll text up. If there is not enough
1861 * remaining text or scrolling fails, must redraw the
1862 * rest. If scrolling works, must redraw the text
1863 * below the scrolled text. */
1864 if (row - xtra_rows >= wp->w_height - 2)
1865 mod_bot = MAXLNUM;
1866 else
1867 {
1868 check_for_delay(FALSE);
1869 if (win_del_lines(wp, row,
1870 -xtra_rows, FALSE, FALSE) == FAIL)
1871 mod_bot = MAXLNUM;
1872 else
1873 bot_start = wp->w_height + xtra_rows;
1874 }
1875 }
1876 else if (xtra_rows > 0)
1877 {
1878 /* May scroll text down. If there is not enough
1879 * remaining text of scrolling fails, must redraw the
1880 * rest. */
1881 if (row + xtra_rows >= wp->w_height - 2)
1882 mod_bot = MAXLNUM;
1883 else
1884 {
1885 check_for_delay(FALSE);
1886 if (win_ins_lines(wp, row + old_rows,
1887 xtra_rows, FALSE, FALSE) == FAIL)
1888 mod_bot = MAXLNUM;
1889 else if (top_end > row + old_rows)
1890 /* Scrolled the part at the top that requires
1891 * updating down. */
1892 top_end += xtra_rows;
1893 }
1894 }
1895
1896 /* When not updating the rest, may need to move w_lines[]
1897 * entries. */
1898 if (mod_bot != MAXLNUM && i != j)
1899 {
1900 if (j < i)
1901 {
1902 int x = row + new_rows;
1903
1904 /* move entries in w_lines[] upwards */
1905 for (;;)
1906 {
1907 /* stop at last valid entry in w_lines[] */
1908 if (i >= wp->w_lines_valid)
1909 {
1910 wp->w_lines_valid = j;
1911 break;
1912 }
1913 wp->w_lines[j] = wp->w_lines[i];
1914 /* stop at a line that won't fit */
1915 if (x + (int)wp->w_lines[j].wl_size
1916 > wp->w_height)
1917 {
1918 wp->w_lines_valid = j + 1;
1919 break;
1920 }
1921 x += wp->w_lines[j++].wl_size;
1922 ++i;
1923 }
1924 if (bot_start > x)
1925 bot_start = x;
1926 }
1927 else /* j > i */
1928 {
1929 /* move entries in w_lines[] downwards */
1930 j -= i;
1931 wp->w_lines_valid += j;
1932 if (wp->w_lines_valid > wp->w_height)
1933 wp->w_lines_valid = wp->w_height;
1934 for (i = wp->w_lines_valid; i - j >= idx; --i)
1935 wp->w_lines[i] = wp->w_lines[i - j];
1936
1937 /* The w_lines[] entries for inserted lines are
1938 * now invalid, but wl_size may be used above.
1939 * Reset to zero. */
1940 while (i >= idx)
1941 {
1942 wp->w_lines[i].wl_size = 0;
1943 wp->w_lines[i--].wl_valid = FALSE;
1944 }
1945 }
1946 }
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949
1950#ifdef FEAT_FOLDING
1951 /*
1952 * When lines are folded, display one line for all of them.
1953 * Otherwise, display normally (can be several display lines when
1954 * 'wrap' is on).
1955 */
1956 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1957 if (fold_count != 0)
1958 {
1959 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1960 ++row;
1961 --fold_count;
1962 wp->w_lines[idx].wl_folded = TRUE;
1963 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1964# ifdef FEAT_SYN_HL
1965 did_update = DID_FOLD;
1966# endif
1967 }
1968 else
1969#endif
1970 if (idx < wp->w_lines_valid
1971 && wp->w_lines[idx].wl_valid
1972 && wp->w_lines[idx].wl_lnum == lnum
1973 && lnum > wp->w_topline
1974 && !(dy_flags & DY_LASTLINE)
1975 && srow + wp->w_lines[idx].wl_size > wp->w_height
1976#ifdef FEAT_DIFF
1977 && diff_check_fill(wp, lnum) == 0
1978#endif
1979 )
1980 {
1981 /* This line is not going to fit. Don't draw anything here,
1982 * will draw "@ " lines below. */
1983 row = wp->w_height + 1;
1984 }
1985 else
1986 {
1987#ifdef FEAT_SEARCH_EXTRA
1988 prepare_search_hl(wp, lnum);
1989#endif
1990#ifdef FEAT_SYN_HL
1991 /* Let the syntax stuff know we skipped a few lines. */
1992 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001993 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 syntax_end_parsing(syntax_last_parsed + 1);
1995#endif
1996
1997 /*
1998 * Display one line.
1999 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002000 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002#ifdef FEAT_FOLDING
2003 wp->w_lines[idx].wl_folded = FALSE;
2004 wp->w_lines[idx].wl_lastlnum = lnum;
2005#endif
2006#ifdef FEAT_SYN_HL
2007 did_update = DID_LINE;
2008 syntax_last_parsed = lnum;
2009#endif
2010 }
2011
2012 wp->w_lines[idx].wl_lnum = lnum;
2013 wp->w_lines[idx].wl_valid = TRUE;
2014 if (row > wp->w_height) /* past end of screen */
2015 {
2016 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002017 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2019 ++idx;
2020 break;
2021 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002022 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 wp->w_lines[idx].wl_size = row - srow;
2024 ++idx;
2025#ifdef FEAT_FOLDING
2026 lnum += fold_count + 1;
2027#else
2028 ++lnum;
2029#endif
2030 }
2031 else
2032 {
2033 /* This line does not need updating, advance to the next one */
2034 row += wp->w_lines[idx++].wl_size;
2035 if (row > wp->w_height) /* past end of screen */
2036 break;
2037#ifdef FEAT_FOLDING
2038 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2039#else
2040 ++lnum;
2041#endif
2042#ifdef FEAT_SYN_HL
2043 did_update = DID_NONE;
2044#endif
2045 }
2046
2047 if (lnum > buf->b_ml.ml_line_count)
2048 {
2049 eof = TRUE;
2050 break;
2051 }
2052 }
2053 /*
2054 * End of loop over all window lines.
2055 */
2056
2057
2058 if (idx > wp->w_lines_valid)
2059 wp->w_lines_valid = idx;
2060
2061#ifdef FEAT_SYN_HL
2062 /*
2063 * Let the syntax stuff know we stop parsing here.
2064 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002065 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 syntax_end_parsing(syntax_last_parsed + 1);
2067#endif
2068
2069 /*
2070 * If we didn't hit the end of the file, and we didn't finish the last
2071 * line we were working on, then the line didn't fit.
2072 */
2073 wp->w_empty_rows = 0;
2074#ifdef FEAT_DIFF
2075 wp->w_filler_rows = 0;
2076#endif
2077 if (!eof && !didline)
2078 {
2079 if (lnum == wp->w_topline)
2080 {
2081 /*
2082 * Single line that does not fit!
2083 * Don't overwrite it, it can be edited.
2084 */
2085 wp->w_botline = lnum + 1;
2086 }
2087#ifdef FEAT_DIFF
2088 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2089 {
2090 /* Window ends in filler lines. */
2091 wp->w_botline = lnum;
2092 wp->w_filler_rows = wp->w_height - srow;
2093 }
2094#endif
2095 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2096 {
2097 /*
2098 * Last line isn't finished: Display "@@@" at the end.
2099 */
2100 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2101 W_WINROW(wp) + wp->w_height,
2102 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2103 '@', '@', hl_attr(HLF_AT));
2104 set_empty_rows(wp, srow);
2105 wp->w_botline = lnum;
2106 }
2107 else
2108 {
2109 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2110 wp->w_botline = lnum;
2111 }
2112 }
2113 else
2114 {
2115#ifdef FEAT_VERTSPLIT
2116 draw_vsep_win(wp, row);
2117#endif
2118 if (eof) /* we hit the end of the file */
2119 {
2120 wp->w_botline = buf->b_ml.ml_line_count + 1;
2121#ifdef FEAT_DIFF
2122 j = diff_check_fill(wp, wp->w_botline);
2123 if (j > 0 && !wp->w_botfill)
2124 {
2125 /*
2126 * Display filler lines at the end of the file
2127 */
2128 if (char2cells(fill_diff) > 1)
2129 i = '-';
2130 else
2131 i = fill_diff;
2132 if (row + j > wp->w_height)
2133 j = wp->w_height - row;
2134 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2135 row += j;
2136 }
2137#endif
2138 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002139 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 wp->w_botline = lnum;
2141
2142 /* make sure the rest of the screen is blank */
2143 /* put '~'s on rows that aren't part of the file. */
2144 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2145 }
2146
2147 /* Reset the type of redrawing required, the window has been updated. */
2148 wp->w_redr_type = 0;
2149#ifdef FEAT_DIFF
2150 wp->w_old_topfill = wp->w_topfill;
2151 wp->w_old_botfill = wp->w_botfill;
2152#endif
2153
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 /*
2157 * There is a trick with w_botline. If we invalidate it on each
2158 * change that might modify it, this will cause a lot of expensive
2159 * calls to plines() in update_topline() each time. Therefore the
2160 * value of w_botline is often approximated, and this value is used to
2161 * compute the value of w_topline. If the value of w_botline was
2162 * wrong, check that the value of w_topline is correct (cursor is on
2163 * the visible part of the text). If it's not, we need to redraw
2164 * again. Mostly this just means scrolling up a few lines, so it
2165 * doesn't look too bad. Only do this for the current window (where
2166 * changes are relevant).
2167 */
2168 wp->w_valid |= VALID_BOTLINE;
2169 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2170 {
2171 recursive = TRUE;
2172 curwin->w_valid &= ~VALID_TOPLINE;
2173 update_topline(); /* may invalidate w_botline again */
2174 if (must_redraw != 0)
2175 {
2176 /* Don't update for changes in buffer again. */
2177 i = curbuf->b_mod_set;
2178 curbuf->b_mod_set = FALSE;
2179 win_update(curwin);
2180 must_redraw = 0;
2181 curbuf->b_mod_set = i;
2182 }
2183 recursive = FALSE;
2184 }
2185 }
2186
2187#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2188 /* restore got_int, unless CTRL-C was hit while redrawing */
2189 if (!got_int)
2190 got_int = save_got_int;
2191#endif
2192}
2193
2194#ifdef FEAT_SIGNS
2195static int draw_signcolumn __ARGS((win_T *wp));
2196
2197/*
2198 * Return TRUE when window "wp" has a column to draw signs in.
2199 */
2200 static int
2201draw_signcolumn(wp)
2202 win_T *wp;
2203{
2204 return (wp->w_buffer->b_signlist != NULL
2205# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002206 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207# endif
2208 );
2209}
2210#endif
2211
2212/*
2213 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2214 * as the filler character.
2215 */
2216 static void
2217win_draw_end(wp, c1, c2, row, endrow, hl)
2218 win_T *wp;
2219 int c1;
2220 int c2;
2221 int row;
2222 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002223 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2226 int n = 0;
2227# define FDC_OFF n
2228#else
2229# define FDC_OFF 0
2230#endif
2231
2232#ifdef FEAT_RIGHTLEFT
2233 if (wp->w_p_rl)
2234 {
2235 /* No check for cmdline window: should never be right-left. */
2236# ifdef FEAT_FOLDING
2237 n = wp->w_p_fdc;
2238
2239 if (n > 0)
2240 {
2241 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002242 if (n > W_WIDTH(wp))
2243 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2245 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2246 ' ', ' ', hl_attr(HLF_FC));
2247 }
2248# endif
2249# ifdef FEAT_SIGNS
2250 if (draw_signcolumn(wp))
2251 {
2252 int nn = n + 2;
2253
2254 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002255 if (nn > W_WIDTH(wp))
2256 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2258 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2259 ' ', ' ', hl_attr(HLF_SC));
2260 n = nn;
2261 }
2262# endif
2263 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2264 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2265 c2, c2, hl_attr(hl));
2266 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2267 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2268 c1, c2, hl_attr(hl));
2269 }
2270 else
2271#endif
2272 {
2273#ifdef FEAT_CMDWIN
2274 if (cmdwin_type != 0 && wp == curwin)
2275 {
2276 /* draw the cmdline character in the leftmost column */
2277 n = 1;
2278 if (n > wp->w_width)
2279 n = wp->w_width;
2280 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2281 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2282 cmdwin_type, ' ', hl_attr(HLF_AT));
2283 }
2284#endif
2285#ifdef FEAT_FOLDING
2286 if (wp->w_p_fdc > 0)
2287 {
2288 int nn = n + wp->w_p_fdc;
2289
2290 /* draw the fold column at the left */
2291 if (nn > W_WIDTH(wp))
2292 nn = W_WIDTH(wp);
2293 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2294 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2295 ' ', ' ', hl_attr(HLF_FC));
2296 n = nn;
2297 }
2298#endif
2299#ifdef FEAT_SIGNS
2300 if (draw_signcolumn(wp))
2301 {
2302 int nn = n + 2;
2303
2304 /* draw the sign column after the fold column */
2305 if (nn > W_WIDTH(wp))
2306 nn = W_WIDTH(wp);
2307 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2308 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2309 ' ', ' ', hl_attr(HLF_SC));
2310 n = nn;
2311 }
2312#endif
2313 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2314 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2315 c1, c2, hl_attr(hl));
2316 }
2317 set_empty_rows(wp, row);
2318}
2319
Bram Moolenaar1a384422010-07-14 19:53:30 +02002320#ifdef FEAT_SYN_HL
2321static int advance_color_col __ARGS((int vcol, int **color_cols));
2322
2323/*
2324 * Advance **color_cols and return TRUE when there are columns to draw.
2325 */
2326 static int
2327advance_color_col(vcol, color_cols)
2328 int vcol;
2329 int **color_cols;
2330{
2331 while (**color_cols >= 0 && vcol > **color_cols)
2332 ++*color_cols;
2333 return (**color_cols >= 0);
2334}
2335#endif
2336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337#ifdef FEAT_FOLDING
2338/*
2339 * Display one folded line.
2340 */
2341 static void
2342fold_line(wp, fold_count, foldinfo, lnum, row)
2343 win_T *wp;
2344 long fold_count;
2345 foldinfo_T *foldinfo;
2346 linenr_T lnum;
2347 int row;
2348{
2349 char_u buf[51];
2350 pos_T *top, *bot;
2351 linenr_T lnume = lnum + fold_count - 1;
2352 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002353 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 int col;
2356 int txtcol;
2357 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002358 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /* Build the fold line:
2361 * 1. Add the cmdwin_type for the command-line window
2362 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002363 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 * 4. Compose the text
2365 * 5. Add the text
2366 * 6. set highlighting for the Visual area an other text
2367 */
2368 col = 0;
2369
2370 /*
2371 * 1. Add the cmdwin_type for the command-line window
2372 * Ignores 'rightleft', this window is never right-left.
2373 */
2374#ifdef FEAT_CMDWIN
2375 if (cmdwin_type != 0 && wp == curwin)
2376 {
2377 ScreenLines[off] = cmdwin_type;
2378 ScreenAttrs[off] = hl_attr(HLF_AT);
2379#ifdef FEAT_MBYTE
2380 if (enc_utf8)
2381 ScreenLinesUC[off] = 0;
2382#endif
2383 ++col;
2384 }
2385#endif
2386
2387 /*
2388 * 2. Add the 'foldcolumn'
2389 */
2390 fdc = wp->w_p_fdc;
2391 if (fdc > W_WIDTH(wp) - col)
2392 fdc = W_WIDTH(wp) - col;
2393 if (fdc > 0)
2394 {
2395 fill_foldcolumn(buf, wp, TRUE, lnum);
2396#ifdef FEAT_RIGHTLEFT
2397 if (wp->w_p_rl)
2398 {
2399 int i;
2400
2401 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2402 hl_attr(HLF_FC));
2403 /* reverse the fold column */
2404 for (i = 0; i < fdc; ++i)
2405 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2406 }
2407 else
2408#endif
2409 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2410 col += fdc;
2411 }
2412
2413#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002414# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2415 for (ri = 0; ri < l; ++ri) \
2416 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2417 else \
2418 for (ri = 0; ri < l; ++ri) \
2419 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002421# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2422 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423#endif
2424
Bram Moolenaar64486672010-05-16 15:46:46 +02002425 /* Set all attributes of the 'number' or 'relativenumber' column and the
2426 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002427 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429#ifdef FEAT_SIGNS
2430 /* If signs are being displayed, add two spaces. */
2431 if (draw_signcolumn(wp))
2432 {
2433 len = W_WIDTH(wp) - col;
2434 if (len > 0)
2435 {
2436 if (len > 2)
2437 len = 2;
2438# ifdef FEAT_RIGHTLEFT
2439 if (wp->w_p_rl)
2440 /* the line number isn't reversed */
2441 copy_text_attr(off + W_WIDTH(wp) - len - col,
2442 (char_u *)" ", len, hl_attr(HLF_FL));
2443 else
2444# endif
2445 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2446 col += len;
2447 }
2448 }
2449#endif
2450
2451 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002452 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002454 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
2456 len = W_WIDTH(wp) - col;
2457 if (len > 0)
2458 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002459 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002460 long num;
2461 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002462
2463 if (len > w + 1)
2464 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002465
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002466 if (wp->w_p_nu && !wp->w_p_rnu)
2467 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002468 num = (long)lnum;
2469 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002470 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002471 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002472 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002473 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002474 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002475 /* 'number' + 'relativenumber': cursor line shows absolute
2476 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002477 num = lnum;
2478 fmt = "%-*ld ";
2479 }
2480 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002481
Bram Moolenaar700e7342013-01-30 12:31:36 +01002482 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483#ifdef FEAT_RIGHTLEFT
2484 if (wp->w_p_rl)
2485 /* the line number isn't reversed */
2486 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2487 hl_attr(HLF_FL));
2488 else
2489#endif
2490 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2491 col += len;
2492 }
2493 }
2494
2495 /*
2496 * 4. Compose the folded-line string with 'foldtext', if set.
2497 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002498 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 txtcol = col; /* remember where text starts */
2501
2502 /*
2503 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2504 * Right-left text is put in columns 0 - number-col, normal text is put
2505 * in columns number-col - window-width.
2506 */
2507#ifdef FEAT_MBYTE
2508 if (has_mbyte)
2509 {
2510 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002511 int u8c, u8cc[MAX_MCO];
2512 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int idx;
2514 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002515 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516# ifdef FEAT_ARABIC
2517 int prev_c = 0; /* previous Arabic character */
2518 int prev_c1 = 0; /* first composing char for prev_c */
2519# endif
2520
2521# ifdef FEAT_RIGHTLEFT
2522 if (wp->w_p_rl)
2523 idx = off;
2524 else
2525# endif
2526 idx = off + col;
2527
2528 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2529 for (p = text; *p != NUL; )
2530 {
2531 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002532 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (col + cells > W_WIDTH(wp)
2534# ifdef FEAT_RIGHTLEFT
2535 - (wp->w_p_rl ? col : 0)
2536# endif
2537 )
2538 break;
2539 ScreenLines[idx] = *p;
2540 if (enc_utf8)
2541 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002542 u8c = utfc_ptr2char(p, u8cc);
2543 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 ScreenLinesUC[idx] = 0;
2546#ifdef FEAT_ARABIC
2547 prev_c = u8c;
2548#endif
2549 }
2550 else
2551 {
2552#ifdef FEAT_ARABIC
2553 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2554 {
2555 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002556 int pc, pc1, nc;
2557 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int firstbyte = *p;
2559
2560 /* The idea of what is the previous and next
2561 * character depends on 'rightleft'. */
2562 if (wp->w_p_rl)
2563 {
2564 pc = prev_c;
2565 pc1 = prev_c1;
2566 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002567 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 else
2570 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002571 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575 prev_c = u8c;
2576
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 pc, pc1, nc);
2579 ScreenLines[idx] = firstbyte;
2580 }
2581 else
2582 prev_c = u8c;
2583#endif
2584 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002585#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (u8c >= 0x10000)
2587 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2588 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002591 for (i = 0; i < Screen_mco; ++i)
2592 {
2593 ScreenLinesC[i][idx] = u8cc[i];
2594 if (u8cc[i] == 0)
2595 break;
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598 if (cells > 1)
2599 ScreenLines[idx + 1] = 0;
2600 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002601 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2602 /* double-byte single width character */
2603 ScreenLines2[idx] = p[1];
2604 else if (cells > 1)
2605 /* double-width character */
2606 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 col += cells;
2608 idx += cells;
2609 p += c_len;
2610 }
2611 }
2612 else
2613#endif
2614 {
2615 len = (int)STRLEN(text);
2616 if (len > W_WIDTH(wp) - col)
2617 len = W_WIDTH(wp) - col;
2618 if (len > 0)
2619 {
2620#ifdef FEAT_RIGHTLEFT
2621 if (wp->w_p_rl)
2622 STRNCPY(current_ScreenLine, text, len);
2623 else
2624#endif
2625 STRNCPY(current_ScreenLine + col, text, len);
2626 col += len;
2627 }
2628 }
2629
2630 /* Fill the rest of the line with the fold filler */
2631#ifdef FEAT_RIGHTLEFT
2632 if (wp->w_p_rl)
2633 col -= txtcol;
2634#endif
2635 while (col < W_WIDTH(wp)
2636#ifdef FEAT_RIGHTLEFT
2637 - (wp->w_p_rl ? txtcol : 0)
2638#endif
2639 )
2640 {
2641#ifdef FEAT_MBYTE
2642 if (enc_utf8)
2643 {
2644 if (fill_fold >= 0x80)
2645 {
2646 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002647 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else
2650 ScreenLinesUC[off + col] = 0;
2651 }
2652#endif
2653 ScreenLines[off + col++] = fill_fold;
2654 }
2655
2656 if (text != buf)
2657 vim_free(text);
2658
2659 /*
2660 * 6. set highlighting for the Visual area an other text.
2661 * If all folded lines are in the Visual area, highlight the line.
2662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2664 {
2665 if (ltoreq(curwin->w_cursor, VIsual))
2666 {
2667 /* Visual is after curwin->w_cursor */
2668 top = &curwin->w_cursor;
2669 bot = &VIsual;
2670 }
2671 else
2672 {
2673 /* Visual is before curwin->w_cursor */
2674 top = &VIsual;
2675 bot = &curwin->w_cursor;
2676 }
2677 if (lnum >= top->lnum
2678 && lnume <= bot->lnum
2679 && (VIsual_mode != 'v'
2680 || ((lnum > top->lnum
2681 || (lnum == top->lnum
2682 && top->col == 0))
2683 && (lnume < bot->lnum
2684 || (lnume == bot->lnum
2685 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002686 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 if (VIsual_mode == Ctrl_V)
2689 {
2690 /* Visual block mode: highlight the chars part of the block */
2691 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2692 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002693 if (wp->w_old_cursor_lcol != MAXCOL
2694 && wp->w_old_cursor_lcol + txtcol
2695 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 len = wp->w_old_cursor_lcol;
2697 else
2698 len = W_WIDTH(wp) - txtcol;
2699 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002700 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 }
2703 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002706 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002711#ifdef FEAT_SYN_HL
2712 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002713 if (wp->w_p_cuc)
2714 {
2715 txtcol += wp->w_virtcol;
2716 if (wp->w_p_wrap)
2717 txtcol -= wp->w_skipcol;
2718 else
2719 txtcol -= wp->w_leftcol;
2720 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2721 ScreenAttrs[off + txtcol] = hl_combine_attr(
2722 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2723 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2727 (int)W_WIDTH(wp), FALSE);
2728
2729 /*
2730 * Update w_cline_height and w_cline_folded if the cursor line was
2731 * updated (saves a call to plines() later).
2732 */
2733 if (wp == curwin
2734 && lnum <= curwin->w_cursor.lnum
2735 && lnume >= curwin->w_cursor.lnum)
2736 {
2737 curwin->w_cline_row = row;
2738 curwin->w_cline_height = 1;
2739 curwin->w_cline_folded = TRUE;
2740 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2741 }
2742}
2743
2744/*
2745 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2746 */
2747 static void
2748copy_text_attr(off, buf, len, attr)
2749 int off;
2750 char_u *buf;
2751 int len;
2752 int attr;
2753{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002754 int i;
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 mch_memmove(ScreenLines + off, buf, (size_t)len);
2757# ifdef FEAT_MBYTE
2758 if (enc_utf8)
2759 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2760# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002761 for (i = 0; i < len; ++i)
2762 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763}
2764
2765/*
2766 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002767 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
2769 static void
2770fill_foldcolumn(p, wp, closed, lnum)
2771 char_u *p;
2772 win_T *wp;
2773 int closed; /* TRUE of FALSE */
2774 linenr_T lnum; /* current line number */
2775{
2776 int i = 0;
2777 int level;
2778 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002779 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780
2781 /* Init to all spaces. */
2782 copy_spaces(p, (size_t)wp->w_p_fdc);
2783
2784 level = win_foldinfo.fi_level;
2785 if (level > 0)
2786 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002787 /* If there is only one column put more info in it. */
2788 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 /* If the column is too narrow, we start at the lowest level that
2791 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002792 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (first_level < 1)
2794 first_level = 1;
2795
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002796 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 if (win_foldinfo.fi_lnum == lnum
2799 && first_level + i >= win_foldinfo.fi_low_level)
2800 p[i] = '-';
2801 else if (first_level == 1)
2802 p[i] = '|';
2803 else if (first_level + i <= 9)
2804 p[i] = '0' + first_level + i;
2805 else
2806 p[i] = '>';
2807 if (first_level + i == level)
2808 break;
2809 }
2810 }
2811 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002812 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813}
2814#endif /* FEAT_FOLDING */
2815
2816/*
2817 * Display line "lnum" of window 'wp' on the screen.
2818 * Start at row "startrow", stop when "endrow" is reached.
2819 * wp->w_virtcol needs to be valid.
2820 *
2821 * Return the number of last row the line occupies.
2822 */
2823 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002824win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 win_T *wp;
2826 linenr_T lnum;
2827 int startrow;
2828 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002829 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 int col; /* visual column on screen */
2832 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2833 int c = 0; /* init for GCC */
2834 long vcol = 0; /* virtual column (for tabs) */
2835 long vcol_prev = -1; /* "vcol" of previous character */
2836 char_u *line; /* current line */
2837 char_u *ptr; /* current position in "line" */
2838 int row; /* row in the window, excl w_winrow */
2839 int screen_row; /* row on the screen, incl w_winrow */
2840
2841 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2842 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002843 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 int c_extra = NUL; /* extra chars, all the same */
2845 int extra_attr = 0; /* attributes when n_extra != 0 */
2846 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2847 displaying lcs_eol at end-of-line */
2848 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2849 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2850
2851 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2852 int saved_n_extra = 0;
2853 char_u *saved_p_extra = NULL;
2854 int saved_c_extra = 0;
2855 int saved_char_attr = 0;
2856
2857 int n_attr = 0; /* chars with special attr */
2858 int saved_attr2 = 0; /* char_attr saved for n_attr */
2859 int n_attr3 = 0; /* chars with overruling special attr */
2860 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2861
2862 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2863
2864 int fromcol, tocol; /* start/end of inverting */
2865 int fromcol_prev = -2; /* start of inverting after cursor */
2866 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002868 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 pos_T pos;
2870 long v;
2871
2872 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002873 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2875 in this line */
2876 int attr = 0; /* attributes for area highlighting */
2877 int area_attr = 0; /* attributes desired by highlighting */
2878 int search_attr = 0; /* attributes desired by 'hlsearch' */
2879#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002880 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 int syntax_attr = 0; /* attributes desired by syntax */
2882 int has_syntax = FALSE; /* this buffer has syntax highl. */
2883 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002884 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002885 int draw_color_col = FALSE; /* highlight colorcolumn */
2886 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002887#endif
2888#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002889 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002890# define SPWORDLEN 150
2891 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002892 int nextlinecol = 0; /* column where nextline[] starts */
2893 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002894 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002895 int spell_attr = 0; /* attributes desired by spelling */
2896 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002897 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2898 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002899 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002900 static int cap_col = -1; /* column to check for Cap word */
2901 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002902 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
2904 int extra_check; /* has syntax or linebreak */
2905#ifdef FEAT_MBYTE
2906 int multi_attr = 0; /* attributes desired by multibyte */
2907 int mb_l = 1; /* multi-byte byte length */
2908 int mb_c = 0; /* decoded multi-byte character */
2909 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002910 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911#endif
2912#ifdef FEAT_DIFF
2913 int filler_lines; /* nr of filler lines to be drawn */
2914 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002915 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int change_start = MAXCOL; /* first col of changed area */
2917 int change_end = -1; /* last col of changed area */
2918#endif
2919 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2920#ifdef FEAT_LINEBREAK
2921 int need_showbreak = FALSE;
2922#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002923#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2924 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002926 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#endif
2928#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002929 matchitem_T *cur; /* points to the match list */
2930 match_T *shl; /* points to search_hl or a match */
2931 int shl_flag; /* flag to indicate whether search_hl
2932 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02002933 int pos_inprogress; /* marks that position match search is
2934 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002935 int prevcol_hl_flag; /* flag to indicate whether prevcol
2936 equals startcol of search_hl or one
2937 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938#endif
2939#ifdef FEAT_ARABIC
2940 int prev_c = 0; /* previous Arabic character */
2941 int prev_c1 = 0; /* first composing char for prev_c */
2942#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002943#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002944 int did_line_attr = 0;
2945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /* draw_state: items that are drawn in sequence: */
2948#define WL_START 0 /* nothing done yet */
2949#ifdef FEAT_CMDWIN
2950# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2951#else
2952# define WL_CMDLINE WL_START
2953#endif
2954#ifdef FEAT_FOLDING
2955# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2956#else
2957# define WL_FOLD WL_CMDLINE
2958#endif
2959#ifdef FEAT_SIGNS
2960# define WL_SIGN WL_FOLD + 1 /* column for signs */
2961#else
2962# define WL_SIGN WL_FOLD /* column for signs */
2963#endif
2964#define WL_NR WL_SIGN + 1 /* line number */
2965#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2966# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2967#else
2968# define WL_SBR WL_NR
2969#endif
2970#define WL_LINE WL_SBR + 1 /* text in the line */
2971 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002972#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 int feedback_col = 0;
2974 int feedback_old_attr = -1;
2975#endif
2976
Bram Moolenaar860cae12010-06-05 23:22:07 +02002977#ifdef FEAT_CONCEAL
2978 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002979 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002980 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002981 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002982 int is_concealing = FALSE;
2983 int boguscols = 0; /* nonexistent columns added to force
2984 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002985 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002986 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002987# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02002988# define FIX_FOR_BOGUSCOLS \
2989 { \
2990 n_extra += vcol_off; \
2991 vcol -= vcol_off; \
2992 vcol_off = 0; \
2993 col -= boguscols; \
2994 boguscols = 0; \
2995 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002996#else
2997# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 if (startrow > endrow) /* past the end already! */
3001 return startrow;
3002
3003 row = startrow;
3004 screen_row = row + W_WINROW(wp);
3005
3006 /*
3007 * To speed up the loop below, set extra_check when there is linebreak,
3008 * trailing white space and/or syntax processing to be done.
3009 */
3010#ifdef FEAT_LINEBREAK
3011 extra_check = wp->w_p_lbr;
3012#else
3013 extra_check = 0;
3014#endif
3015#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003016 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 /* Prepare for syntax highlighting in this line. When there is an
3019 * error, stop syntax highlighting. */
3020 save_did_emsg = did_emsg;
3021 did_emsg = FALSE;
3022 syntax_start(wp, lnum);
3023 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003024 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 else
3026 {
3027 did_emsg = save_did_emsg;
3028 has_syntax = TRUE;
3029 extra_check = TRUE;
3030 }
3031 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003032
3033 /* Check for columns to display for 'colorcolumn'. */
3034 color_cols = wp->w_p_cc_cols;
3035 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003036 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003037#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003038
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003039#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003040 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003041 && *wp->w_s->b_p_spl != NUL
3042 && wp->w_s->b_langp.ga_len > 0
3043 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003044 {
3045 /* Prepare for spell checking. */
3046 has_spell = TRUE;
3047 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003048
3049 /* Get the start of the next line, so that words that wrap to the next
3050 * line are found too: "et<line-break>al.".
3051 * Trick: skip a few chars for C/shell/Vim comments */
3052 nextline[SPWORDLEN] = NUL;
3053 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3054 {
3055 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3056 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3057 }
3058
3059 /* When a word wrapped from the previous line the start of the current
3060 * line is valid. */
3061 if (lnum == checked_lnum)
3062 cur_checked_col = checked_col;
3063 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003064
3065 /* When there was a sentence end in the previous line may require a
3066 * word starting with capital in this line. In line 1 always check
3067 * the first word. */
3068 if (lnum != capcol_lnum)
3069 cap_col = -1;
3070 if (lnum == 1)
3071 cap_col = 0;
3072 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075
3076 /*
3077 * handle visual active in this window
3078 */
3079 fromcol = -10;
3080 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3082 {
3083 /* Visual is after curwin->w_cursor */
3084 if (ltoreq(curwin->w_cursor, VIsual))
3085 {
3086 top = &curwin->w_cursor;
3087 bot = &VIsual;
3088 }
3089 else /* Visual is before curwin->w_cursor */
3090 {
3091 top = &VIsual;
3092 bot = &curwin->w_cursor;
3093 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003094 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (VIsual_mode == Ctrl_V) /* block mode */
3096 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003097 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
3099 fromcol = wp->w_old_cursor_fcol;
3100 tocol = wp->w_old_cursor_lcol;
3101 }
3102 }
3103 else /* non-block mode */
3104 {
3105 if (lnum > top->lnum && lnum <= bot->lnum)
3106 fromcol = 0;
3107 else if (lnum == top->lnum)
3108 {
3109 if (VIsual_mode == 'V') /* linewise */
3110 fromcol = 0;
3111 else
3112 {
3113 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3114 if (gchar_pos(top) == NUL)
3115 tocol = fromcol + 1;
3116 }
3117 }
3118 if (VIsual_mode != 'V' && lnum == bot->lnum)
3119 {
3120 if (*p_sel == 'e' && bot->col == 0
3121#ifdef FEAT_VIRTUALEDIT
3122 && bot->coladd == 0
3123#endif
3124 )
3125 {
3126 fromcol = -10;
3127 tocol = MAXCOL;
3128 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003129 else if (bot->col == MAXCOL)
3130 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 else
3132 {
3133 pos = *bot;
3134 if (*p_sel == 'e')
3135 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3136 else
3137 {
3138 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3139 ++tocol;
3140 }
3141 }
3142 }
3143 }
3144
3145#ifndef MSDOS
3146 /* Check if the character under the cursor should not be inverted */
3147 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3148# ifdef FEAT_GUI
3149 && !gui.in_use
3150# endif
3151 )
3152 noinvcur = TRUE;
3153#endif
3154
3155 /* if inverting in this line set area_highlighting */
3156 if (fromcol >= 0)
3157 {
3158 area_highlighting = TRUE;
3159 attr = hl_attr(HLF_V);
3160#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003161 if ((clip_star.available && !clip_star.owned
3162 && clip_isautosel_star())
3163 || (clip_plus.available && !clip_plus.owned
3164 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 attr = hl_attr(HLF_VNC);
3166#endif
3167 }
3168 }
3169
3170 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003171 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003173 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 && wp == curwin
3175 && lnum >= curwin->w_cursor.lnum
3176 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3177 {
3178 if (lnum == curwin->w_cursor.lnum)
3179 getvcol(curwin, &(curwin->w_cursor),
3180 (colnr_T *)&fromcol, NULL, NULL);
3181 else
3182 fromcol = 0;
3183 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3184 {
3185 pos.lnum = lnum;
3186 pos.col = search_match_endcol;
3187 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3188 }
3189 else
3190 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003191 /* do at least one character; happens when past end of line */
3192 if (fromcol == tocol)
3193 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 area_highlighting = TRUE;
3195 attr = hl_attr(HLF_I);
3196 }
3197
3198#ifdef FEAT_DIFF
3199 filler_lines = diff_check(wp, lnum);
3200 if (filler_lines < 0)
3201 {
3202 if (filler_lines == -1)
3203 {
3204 if (diff_find_change(wp, lnum, &change_start, &change_end))
3205 diff_hlf = HLF_ADD; /* added line */
3206 else if (change_start == 0)
3207 diff_hlf = HLF_TXD; /* changed text */
3208 else
3209 diff_hlf = HLF_CHD; /* changed line */
3210 }
3211 else
3212 diff_hlf = HLF_ADD; /* added line */
3213 filler_lines = 0;
3214 area_highlighting = TRUE;
3215 }
3216 if (lnum == wp->w_topline)
3217 filler_lines = wp->w_topfill;
3218 filler_todo = filler_lines;
3219#endif
3220
3221#ifdef LINE_ATTR
3222# ifdef FEAT_SIGNS
3223 /* If this line has a sign with line highlighting set line_attr. */
3224 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3225 if (v != 0)
3226 line_attr = sign_get_attr((int)v, TRUE);
3227# endif
3228# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3229 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003230 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 line_attr = hl_attr(HLF_L);
3232# endif
3233 if (line_attr != 0)
3234 area_highlighting = TRUE;
3235#endif
3236
3237 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3238 ptr = line;
3239
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003240#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003241 if (has_spell)
3242 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003243 /* For checking first word with a capital skip white space. */
3244 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003245 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003246
Bram Moolenaar30abd282005-06-22 22:35:10 +00003247 /* To be able to spell-check over line boundaries copy the end of the
3248 * current line into nextline[]. Above the start of the next line was
3249 * copied to nextline[SPWORDLEN]. */
3250 if (nextline[SPWORDLEN] == NUL)
3251 {
3252 /* No next line or it is empty. */
3253 nextlinecol = MAXCOL;
3254 nextline_idx = 0;
3255 }
3256 else
3257 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003258 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003259 if (v < SPWORDLEN)
3260 {
3261 /* Short line, use it completely and append the start of the
3262 * next line. */
3263 nextlinecol = 0;
3264 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003265 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003266 nextline_idx = v + 1;
3267 }
3268 else
3269 {
3270 /* Long line, use only the last SPWORDLEN bytes. */
3271 nextlinecol = v - SPWORDLEN;
3272 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3273 nextline_idx = SPWORDLEN + 1;
3274 }
3275 }
3276 }
3277#endif
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 /* find start of trailing whitespace */
3280 if (wp->w_p_list && lcs_trail)
3281 {
3282 trailcol = (colnr_T)STRLEN(ptr);
3283 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3284 --trailcol;
3285 trailcol += (colnr_T) (ptr - line);
3286 extra_check = TRUE;
3287 }
3288
3289 /*
3290 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3291 * first character to be displayed.
3292 */
3293 if (wp->w_p_wrap)
3294 v = wp->w_skipcol;
3295 else
3296 v = wp->w_leftcol;
3297 if (v > 0)
3298 {
3299#ifdef FEAT_MBYTE
3300 char_u *prev_ptr = ptr;
3301#endif
3302 while (vcol < v && *ptr != NUL)
3303 {
3304 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3305 vcol += c;
3306#ifdef FEAT_MBYTE
3307 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003309 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 }
3311
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003312 /* When:
3313 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003314 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003315 * - 'virtualedit' is set, or
3316 * - the visual mode is active,
3317 * the end of the line may be before the start of the displayed part.
3318 */
3319 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003320#ifdef FEAT_SYN_HL
3321 wp->w_p_cuc || draw_color_col ||
3322#endif
3323#ifdef FEAT_VIRTUALEDIT
3324 virtual_active() ||
3325#endif
3326 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331 /* Handle a character that's not completely on the screen: Put ptr at
3332 * that character but skip the first few screen characters. */
3333 if (vcol > v)
3334 {
3335 vcol -= c;
3336#ifdef FEAT_MBYTE
3337 ptr = prev_ptr;
3338#else
3339 --ptr;
3340#endif
3341 n_skip = v - vcol;
3342 }
3343
3344 /*
3345 * Adjust for when the inverted text is before the screen,
3346 * and when the start of the inverted text is before the screen.
3347 */
3348 if (tocol <= vcol)
3349 fromcol = 0;
3350 else if (fromcol >= 0 && fromcol < vcol)
3351 fromcol = vcol;
3352
3353#ifdef FEAT_LINEBREAK
3354 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3355 if (wp->w_p_wrap)
3356 need_showbreak = TRUE;
3357#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003358#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003359 /* When spell checking a word we need to figure out the start of the
3360 * word and if it's badly spelled or not. */
3361 if (has_spell)
3362 {
3363 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003364 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003365 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003366
3367 pos = wp->w_cursor;
3368 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003369 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003370 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003371
3372 /* spell_move_to() may call ml_get() and make "line" invalid */
3373 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3374 ptr = line + linecol;
3375
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003376 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003377 {
3378 /* no bad word found at line start, don't check until end of a
3379 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003380 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003381 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003382 }
3383 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003384 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003385 /* bad word found, use attributes until end of word */
3386 word_end = wp->w_cursor.col + len + 1;
3387
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003388 /* Turn index into actual attributes. */
3389 if (spell_hlf != HLF_COUNT)
3390 spell_attr = highlight_attr[spell_hlf];
3391 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003392 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003393
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003394# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003395 /* Need to restart syntax highlighting for this line. */
3396 if (has_syntax)
3397 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003398# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003399 }
3400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402
3403 /*
3404 * Correct highlighting for cursor that can't be disabled.
3405 * Avoids having to check this for each character.
3406 */
3407 if (fromcol >= 0)
3408 {
3409 if (noinvcur)
3410 {
3411 if ((colnr_T)fromcol == wp->w_virtcol)
3412 {
3413 /* highlighting starts at cursor, let it start just after the
3414 * cursor */
3415 fromcol_prev = fromcol;
3416 fromcol = -1;
3417 }
3418 else if ((colnr_T)fromcol < wp->w_virtcol)
3419 /* restart highlighting after the cursor */
3420 fromcol_prev = wp->w_virtcol;
3421 }
3422 if (fromcol >= tocol)
3423 fromcol = -1;
3424 }
3425
3426#ifdef FEAT_SEARCH_EXTRA
3427 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003428 * Handle highlighting the last used search pattern and matches.
3429 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003431 cur = wp->w_match_head;
3432 shl_flag = FALSE;
3433 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003435 if (shl_flag == FALSE)
3436 {
3437 shl = &search_hl;
3438 shl_flag = TRUE;
3439 }
3440 else
3441 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003442 shl->startcol = MAXCOL;
3443 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003445 v = (long)(ptr - line);
3446 if (cur != NULL)
3447 cur->pos.cur = 0;
3448 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3449
3450 /* Need to get the line again, a multi-line regexp may have made it
3451 * invalid. */
3452 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3453 ptr = line + v;
3454
3455 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003457 if (shl->lnum == lnum)
3458 shl->startcol = shl->rm.startpos[0].col;
3459 else
3460 shl->startcol = 0;
3461 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3462 - shl->rm.startpos[0].lnum)
3463 shl->endcol = shl->rm.endpos[0].col;
3464 else
3465 shl->endcol = MAXCOL;
3466 /* Highlight one character for an empty match. */
3467 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003470 if (has_mbyte && line[shl->endcol] != NUL)
3471 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003474 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003476 if ((long)shl->startcol < v) /* match at leftcol */
3477 {
3478 shl->attr_cur = shl->attr;
3479 search_attr = shl->attr;
3480 }
3481 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003483 if (shl != &search_hl && cur != NULL)
3484 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486#endif
3487
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003488#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003489 /* Cursor line highlighting for 'cursorline' in the current window. Not
3490 * when Visual mode is active, because it's not clear what is selected
3491 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003492 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003493 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003494 {
3495 line_attr = hl_attr(HLF_CUL);
3496 area_highlighting = TRUE;
3497 }
3498#endif
3499
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003500 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 col = 0;
3502#ifdef FEAT_RIGHTLEFT
3503 if (wp->w_p_rl)
3504 {
3505 /* Rightleft window: process the text in the normal direction, but put
3506 * it in current_ScreenLine[] from right to left. Start at the
3507 * rightmost column of the window. */
3508 col = W_WIDTH(wp) - 1;
3509 off += col;
3510 }
3511#endif
3512
3513 /*
3514 * Repeat for the whole displayed line.
3515 */
3516 for (;;)
3517 {
3518 /* Skip this quickly when working on the text. */
3519 if (draw_state != WL_LINE)
3520 {
3521#ifdef FEAT_CMDWIN
3522 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3523 {
3524 draw_state = WL_CMDLINE;
3525 if (cmdwin_type != 0 && wp == curwin)
3526 {
3527 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003529 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_attr = hl_attr(HLF_AT);
3531 }
3532 }
3533#endif
3534
3535#ifdef FEAT_FOLDING
3536 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3537 {
3538 draw_state = WL_FOLD;
3539 if (wp->w_p_fdc > 0)
3540 {
3541 /* Draw the 'foldcolumn'. */
3542 fill_foldcolumn(extra, wp, FALSE, lnum);
3543 n_extra = wp->w_p_fdc;
3544 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003545 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 c_extra = NUL;
3547 char_attr = hl_attr(HLF_FC);
3548 }
3549 }
3550#endif
3551
3552#ifdef FEAT_SIGNS
3553 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3554 {
3555 draw_state = WL_SIGN;
3556 /* Show the sign column when there are any signs in this
3557 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003558 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003560 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003562 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563# endif
3564
3565 /* Draw two cells with the sign value or blank. */
3566 c_extra = ' ';
3567 char_attr = hl_attr(HLF_SC);
3568 n_extra = 2;
3569
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003570 if (row == startrow
3571#ifdef FEAT_DIFF
3572 + filler_lines && filler_todo <= 0
3573#endif
3574 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3577 SIGN_TEXT);
3578# ifdef FEAT_SIGN_ICONS
3579 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3580 SIGN_ICON);
3581 if (gui.in_use && icon_sign != 0)
3582 {
3583 /* Use the image in this position. */
3584 c_extra = SIGN_BYTE;
3585# ifdef FEAT_NETBEANS_INTG
3586 if (buf_signcount(wp->w_buffer, lnum) > 1)
3587 c_extra = MULTISIGN_BYTE;
3588# endif
3589 char_attr = icon_sign;
3590 }
3591 else
3592# endif
3593 if (text_sign != 0)
3594 {
3595 p_extra = sign_get_text(text_sign);
3596 if (p_extra != NULL)
3597 {
3598 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003599 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601 char_attr = sign_get_attr(text_sign, FALSE);
3602 }
3603 }
3604 }
3605 }
3606#endif
3607
3608 if (draw_state == WL_NR - 1 && n_extra == 0)
3609 {
3610 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003611 /* Display the absolute or relative line number. After the
3612 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3613 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 && (row == startrow
3615#ifdef FEAT_DIFF
3616 + filler_lines
3617#endif
3618 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3619 {
3620 /* Draw the line number (empty space after wrapping). */
3621 if (row == startrow
3622#ifdef FEAT_DIFF
3623 + filler_lines
3624#endif
3625 )
3626 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003627 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003628 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003629
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003630 if (wp->w_p_nu && !wp->w_p_rnu)
3631 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003632 num = (long)lnum;
3633 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003634 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003635 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003636 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003637 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003638 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003639 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003640 num = lnum;
3641 fmt = "%-*ld ";
3642 }
3643 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003644
Bram Moolenaar700e7342013-01-30 12:31:36 +01003645 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003646 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (wp->w_skipcol > 0)
3648 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3649 *p_extra = '-';
3650#ifdef FEAT_RIGHTLEFT
3651 if (wp->w_p_rl) /* reverse line numbers */
3652 rl_mirror(extra);
3653#endif
3654 p_extra = extra;
3655 c_extra = NUL;
3656 }
3657 else
3658 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003659 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003661#ifdef FEAT_SYN_HL
3662 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003663 * the current line differently.
3664 * TODO: Can we use CursorLine instead of CursorLineNr
3665 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003666 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003667 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003668 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 }
3672
3673#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3674 if (draw_state == WL_SBR - 1 && n_extra == 0)
3675 {
3676 draw_state = WL_SBR;
3677# ifdef FEAT_DIFF
3678 if (filler_todo > 0)
3679 {
3680 /* Draw "deleted" diff line(s). */
3681 if (char2cells(fill_diff) > 1)
3682 c_extra = '-';
3683 else
3684 c_extra = fill_diff;
3685# ifdef FEAT_RIGHTLEFT
3686 if (wp->w_p_rl)
3687 n_extra = col + 1;
3688 else
3689# endif
3690 n_extra = W_WIDTH(wp) - col;
3691 char_attr = hl_attr(HLF_DED);
3692 }
3693# endif
3694# ifdef FEAT_LINEBREAK
3695 if (*p_sbr != NUL && need_showbreak)
3696 {
3697 /* Draw 'showbreak' at the start of each broken line. */
3698 p_extra = p_sbr;
3699 c_extra = NUL;
3700 n_extra = (int)STRLEN(p_sbr);
3701 char_attr = hl_attr(HLF_AT);
3702 need_showbreak = FALSE;
3703 /* Correct end of highlighted area for 'showbreak',
3704 * required when 'linebreak' is also set. */
3705 if (tocol == vcol)
3706 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003707#ifdef FEAT_SYN_HL
3708 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003709 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003710 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713# endif
3714 }
3715#endif
3716
3717 if (draw_state == WL_LINE - 1 && n_extra == 0)
3718 {
3719 draw_state = WL_LINE;
3720 if (saved_n_extra)
3721 {
3722 /* Continue item from end of wrapped line. */
3723 n_extra = saved_n_extra;
3724 c_extra = saved_c_extra;
3725 p_extra = saved_p_extra;
3726 char_attr = saved_char_attr;
3727 }
3728 else
3729 char_attr = 0;
3730 }
3731 }
3732
3733 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003734 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003735 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#ifdef FEAT_DIFF
3737 && filler_todo <= 0
3738#endif
3739 )
3740 {
3741 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3742 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003743 /* Pretend we have finished updating the window. Except when
3744 * 'cursorcolumn' is set. */
3745#ifdef FEAT_SYN_HL
3746 if (wp->w_p_cuc)
3747 row = wp->w_cline_row + wp->w_cline_height;
3748 else
3749#endif
3750 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 break;
3752 }
3753
3754 if (draw_state == WL_LINE && area_highlighting)
3755 {
3756 /* handle Visual or match highlighting in this line */
3757 if (vcol == fromcol
3758#ifdef FEAT_MBYTE
3759 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3760 && (*mb_ptr2cells)(ptr) > 1)
3761#endif
3762 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003763 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 && vcol < tocol))
3765 area_attr = attr; /* start highlighting */
3766 else if (area_attr != 0
3767 && (vcol == tocol
3768 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771#ifdef FEAT_SEARCH_EXTRA
3772 if (!n_extra)
3773 {
3774 /*
3775 * Check for start/end of search pattern match.
3776 * After end, check for start/end of next match.
3777 * When another match, have to check for start again.
3778 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003779 * Do this for 'search_hl' and the match list (ordered by
3780 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003782 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003783 cur = wp->w_match_head;
3784 shl_flag = FALSE;
3785 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003787 if (shl_flag == FALSE
3788 && ((cur != NULL
3789 && cur->priority > SEARCH_HL_PRIORITY)
3790 || cur == NULL))
3791 {
3792 shl = &search_hl;
3793 shl_flag = TRUE;
3794 }
3795 else
3796 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003797 if (cur != NULL)
3798 cur->pos.cur = 0;
3799 pos_inprogress = TRUE;
3800 while (shl->rm.regprog != NULL
3801 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003803 if (shl->startcol != MAXCOL
3804 && v >= (long)shl->startcol
3805 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
3807 shl->attr_cur = shl->attr;
3808 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003809 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
3811 shl->attr_cur = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003812 next_search_hl(wp, shl, lnum, (colnr_T)v, cur);
3813 pos_inprogress = cur == NULL || cur->pos.cur == 0
3814 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 /* Need to get the line again, a multi-line regexp
3817 * may have made it invalid. */
3818 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3819 ptr = line + v;
3820
3821 if (shl->lnum == lnum)
3822 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003823 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003825 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003829 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 /* highlight empty match, try again after
3832 * it */
3833#ifdef FEAT_MBYTE
3834 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003835 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003836 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else
3838#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003839 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
3842 /* Loop to check if the match starts at the
3843 * current position */
3844 continue;
3845 }
3846 }
3847 break;
3848 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003849 if (shl != &search_hl && cur != NULL)
3850 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003852
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003853 /* Use attributes from match with highest priority among
3854 * 'search_hl' and the match list. */
3855 search_attr = search_hl.attr_cur;
3856 cur = wp->w_match_head;
3857 shl_flag = FALSE;
3858 while (cur != NULL || shl_flag == FALSE)
3859 {
3860 if (shl_flag == FALSE
3861 && ((cur != NULL
3862 && cur->priority > SEARCH_HL_PRIORITY)
3863 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003864 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003865 shl = &search_hl;
3866 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003867 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003868 else
3869 shl = &cur->hl;
3870 if (shl->attr_cur != 0)
3871 search_attr = shl->attr_cur;
3872 if (shl != &search_hl && cur != NULL)
3873 cur = cur->next;
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876#endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003879 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003881 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3882 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003884 if (diff_hlf == HLF_TXD && ptr - line > change_end
3885 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003887 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 }
3889#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003890
3891 /* Decide which of the highlight attributes to use. */
3892 attr_pri = TRUE;
3893 if (area_attr != 0)
3894 char_attr = area_attr;
3895 else if (search_attr != 0)
3896 char_attr = search_attr;
3897#ifdef LINE_ATTR
3898 /* Use line_attr when not in the Visual or 'incsearch' area
3899 * (area_attr may be 0 when "noinvcur" is set). */
3900 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003901 || vcol < fromcol || vcol_prev < fromcol_prev
3902 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903 char_attr = line_attr;
3904#endif
3905 else
3906 {
3907 attr_pri = FALSE;
3908#ifdef FEAT_SYN_HL
3909 if (has_syntax)
3910 char_attr = syntax_attr;
3911 else
3912#endif
3913 char_attr = 0;
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916
3917 /*
3918 * Get the next character to put on the screen.
3919 */
3920 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003921 * The "p_extra" points to the extra stuff that is inserted to
3922 * represent special characters (non-printable stuff) and other
3923 * things. When all characters are the same, c_extra is used.
3924 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3925 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3927 */
3928 if (n_extra > 0)
3929 {
3930 if (c_extra != NUL)
3931 {
3932 c = c_extra;
3933#ifdef FEAT_MBYTE
3934 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3935 if (enc_utf8 && (*mb_char2len)(c) > 1)
3936 {
3937 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
3941 else
3942 mb_utf8 = FALSE;
3943#endif
3944 }
3945 else
3946 {
3947 c = *p_extra;
3948#ifdef FEAT_MBYTE
3949 if (has_mbyte)
3950 {
3951 mb_c = c;
3952 if (enc_utf8)
3953 {
3954 /* If the UTF-8 character is more than one byte:
3955 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003956 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 mb_utf8 = FALSE;
3958 if (mb_l > n_extra)
3959 mb_l = 1;
3960 else if (mb_l > 1)
3961 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003962 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003964 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 }
3967 else
3968 {
3969 /* if this is a DBCS character, put it in "mb_c" */
3970 mb_l = MB_BYTE2LEN(c);
3971 if (mb_l >= n_extra)
3972 mb_l = 1;
3973 else if (mb_l > 1)
3974 mb_c = (c << 8) + p_extra[1];
3975 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003976 if (mb_l == 0) /* at the NUL at end-of-line */
3977 mb_l = 1;
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 /* If a double-width char doesn't fit display a '>' in the
3980 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003981 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982# ifdef FEAT_RIGHTLEFT
3983 wp->w_p_rl ? (col <= 0) :
3984# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003985 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 && (*mb_char2cells)(mb_c) == 2)
3987 {
3988 c = '>';
3989 mb_c = c;
3990 mb_l = 1;
3991 mb_utf8 = FALSE;
3992 multi_attr = hl_attr(HLF_AT);
3993 /* put the pointer back to output the double-width
3994 * character at the start of the next line. */
3995 ++n_extra;
3996 --p_extra;
3997 }
3998 else
3999 {
4000 n_extra -= mb_l - 1;
4001 p_extra += mb_l - 1;
4002 }
4003 }
4004#endif
4005 ++p_extra;
4006 }
4007 --n_extra;
4008 }
4009 else
4010 {
4011 /*
4012 * Get a character from the line itself.
4013 */
4014 c = *ptr;
4015#ifdef FEAT_MBYTE
4016 if (has_mbyte)
4017 {
4018 mb_c = c;
4019 if (enc_utf8)
4020 {
4021 /* If the UTF-8 character is more than one byte: Decode it
4022 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004023 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 mb_utf8 = FALSE;
4025 if (mb_l > 1)
4026 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004027 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 /* Overlong encoded ASCII or ASCII with composing char
4029 * is displayed normally, except a NUL. */
4030 if (mb_c < 0x80)
4031 c = mb_c;
4032 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004033
4034 /* At start of the line we can have a composing char.
4035 * Draw it as a space with a composing char. */
4036 if (utf_iscomposing(mb_c))
4037 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004038 int i;
4039
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004040 for (i = Screen_mco - 1; i > 0; --i)
4041 u8cc[i] = u8cc[i - 1];
4042 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004043 mb_c = ' ';
4044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046
4047 if ((mb_l == 1 && c >= 0x80)
4048 || (mb_l >= 1 && mb_c == 0)
4049 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004050# ifdef UNICODE16
4051 || mb_c >= 0x10000
4052# endif
4053 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 /*
4056 * Illegal UTF-8 byte: display as <xx>.
4057 * Non-BMP character : display as ? or fullwidth ?.
4058 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004059# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
4063 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004064# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (wp->w_p_rl) /* reverse */
4066 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004069# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else if (utf_char2cells(mb_c) != 2)
4071 STRCPY(extra, "?");
4072 else
4073 /* 0xff1f in UTF-8: full-width '?' */
4074 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 p_extra = extra;
4078 c = *p_extra;
4079 mb_c = mb_ptr2char_adv(&p_extra);
4080 mb_utf8 = (c >= 0x80);
4081 n_extra = (int)STRLEN(p_extra);
4082 c_extra = NUL;
4083 if (area_attr == 0 && search_attr == 0)
4084 {
4085 n_attr = n_extra + 1;
4086 extra_attr = hl_attr(HLF_8);
4087 saved_attr2 = char_attr; /* save current attr */
4088 }
4089 }
4090 else if (mb_l == 0) /* at the NUL at end-of-line */
4091 mb_l = 1;
4092#ifdef FEAT_ARABIC
4093 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4094 {
4095 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004096 int pc, pc1, nc;
4097 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 /* The idea of what is the previous and next
4100 * character depends on 'rightleft'. */
4101 if (wp->w_p_rl)
4102 {
4103 pc = prev_c;
4104 pc1 = prev_c1;
4105 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004106 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 else
4109 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004110 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004112 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114 prev_c = mb_c;
4115
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004116 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 else
4119 prev_c = mb_c;
4120#endif
4121 }
4122 else /* enc_dbcs */
4123 {
4124 mb_l = MB_BYTE2LEN(c);
4125 if (mb_l == 0) /* at the NUL at end-of-line */
4126 mb_l = 1;
4127 else if (mb_l > 1)
4128 {
4129 /* We assume a second byte below 32 is illegal.
4130 * Hopefully this is OK for all double-byte encodings!
4131 */
4132 if (ptr[1] >= 32)
4133 mb_c = (c << 8) + ptr[1];
4134 else
4135 {
4136 if (ptr[1] == NUL)
4137 {
4138 /* head byte at end of line */
4139 mb_l = 1;
4140 transchar_nonprint(extra, c);
4141 }
4142 else
4143 {
4144 /* illegal tail byte */
4145 mb_l = 2;
4146 STRCPY(extra, "XX");
4147 }
4148 p_extra = extra;
4149 n_extra = (int)STRLEN(extra) - 1;
4150 c_extra = NUL;
4151 c = *p_extra++;
4152 if (area_attr == 0 && search_attr == 0)
4153 {
4154 n_attr = n_extra + 1;
4155 extra_attr = hl_attr(HLF_8);
4156 saved_attr2 = char_attr; /* save current attr */
4157 }
4158 mb_c = c;
4159 }
4160 }
4161 }
4162 /* If a double-width char doesn't fit display a '>' in the
4163 * last column; the character is displayed at the start of the
4164 * next line. */
4165 if ((
4166# ifdef FEAT_RIGHTLEFT
4167 wp->w_p_rl ? (col <= 0) :
4168# endif
4169 (col >= W_WIDTH(wp) - 1))
4170 && (*mb_char2cells)(mb_c) == 2)
4171 {
4172 c = '>';
4173 mb_c = c;
4174 mb_utf8 = FALSE;
4175 mb_l = 1;
4176 multi_attr = hl_attr(HLF_AT);
4177 /* Put pointer back so that the character will be
4178 * displayed at the start of the next line. */
4179 --ptr;
4180 }
4181 else if (*ptr != NUL)
4182 ptr += mb_l - 1;
4183
4184 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004185 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004186 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004187 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004190 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 c = ' ';
4192 if (area_attr == 0 && search_attr == 0)
4193 {
4194 n_attr = n_extra + 1;
4195 extra_attr = hl_attr(HLF_AT);
4196 saved_attr2 = char_attr; /* save current attr */
4197 }
4198 mb_c = c;
4199 mb_utf8 = FALSE;
4200 mb_l = 1;
4201 }
4202
4203 }
4204#endif
4205 ++ptr;
4206
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004208 if (wp->w_p_list && (c == 160
4209#ifdef FEAT_MBYTE
4210 || (mb_utf8 && mb_c == 160)
4211#endif
4212 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004213 {
4214 c = lcs_nbsp;
4215 if (area_attr == 0 && search_attr == 0)
4216 {
4217 n_attr = 1;
4218 extra_attr = hl_attr(HLF_8);
4219 saved_attr2 = char_attr; /* save current attr */
4220 }
4221#ifdef FEAT_MBYTE
4222 mb_c = c;
4223 if (enc_utf8 && (*mb_char2len)(c) > 1)
4224 {
4225 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004226 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004227 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228 }
4229 else
4230 mb_utf8 = FALSE;
4231#endif
4232 }
4233
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 if (extra_check)
4235 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004236#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004237 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004238#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004239
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004240#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 /* Get syntax attribute, unless still at the start of the line
4242 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004243 v = (long)(ptr - line);
4244 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 /* Get the syntax attribute for the character. If there
4247 * is an error, disable syntax highlighting. */
4248 save_did_emsg = did_emsg;
4249 did_emsg = FALSE;
4250
Bram Moolenaar217ad922005-03-20 22:37:15 +00004251 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004252# ifdef FEAT_SPELL
4253 has_spell ? &can_spell :
4254# endif
4255 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004258 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004259 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004260 has_syntax = FALSE;
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
4263 did_emsg = save_did_emsg;
4264
4265 /* Need to get the line again, a multi-line regexp may
4266 * have made it invalid. */
4267 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4268 ptr = line + v;
4269
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004270 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004272 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004273 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004274# ifdef FEAT_CONCEAL
4275 /* no concealing past the end of the line, it interferes
4276 * with line highlighting */
4277 if (c == NUL)
4278 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004279 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004280 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004283#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004284
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004285#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004286 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004287 * Only do this when there is no syntax highlighting, the
4288 * @Spell cluster is not used or the current syntax item
4289 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004290 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004291 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004292 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004293# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004294 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004295 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004296# endif
4297 if (c != 0 && (
4298# ifdef FEAT_SYN_HL
4299 !has_syntax ||
4300# endif
4301 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004302 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004303 char_u *prev_ptr, *p;
4304 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004305 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004306# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004307 if (has_mbyte)
4308 {
4309 prev_ptr = ptr - mb_l;
4310 v -= mb_l - 1;
4311 }
4312 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004313# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004314 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004315
4316 /* Use nextline[] if possible, it has the start of the
4317 * next line concatenated. */
4318 if ((prev_ptr - line) - nextlinecol >= 0)
4319 p = nextline + (prev_ptr - line) - nextlinecol;
4320 else
4321 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004322 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004323 len = spell_check(wp, p, &spell_hlf, &cap_col,
4324 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004325 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004326
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004327 /* In Insert mode only highlight a word that
4328 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004329 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004330 && (State & INSERT) != 0
4331 && wp->w_cursor.lnum == lnum
4332 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004333 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004334 && wp->w_cursor.col < (colnr_T)word_end)
4335 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004336 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004337 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004338 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004339
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004340 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004341 && (p - nextline) + len > nextline_idx)
4342 {
4343 /* Remember that the good word continues at the
4344 * start of the next line. */
4345 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004346 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004347 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004348
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004349 /* Turn index into actual attributes. */
4350 if (spell_hlf != HLF_COUNT)
4351 spell_attr = highlight_attr[spell_hlf];
4352
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004353 if (cap_col > 0)
4354 {
4355 if (p != prev_ptr
4356 && (p - nextline) + cap_col >= nextline_idx)
4357 {
4358 /* Remember that the word in the next line
4359 * must start with a capital. */
4360 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004361 cap_col = (int)((p - nextline) + cap_col
4362 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004363 }
4364 else
4365 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004366 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004367 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004368 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004369 }
4370 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004371 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004372 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004373 char_attr = hl_combine_attr(char_attr, spell_attr);
4374 else
4375 char_attr = hl_combine_attr(spell_attr, char_attr);
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377#endif
4378#ifdef FEAT_LINEBREAK
4379 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004380 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
4382 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004383 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 n_extra = win_lbr_chartabsize(wp, ptr - (
4386# ifdef FEAT_MBYTE
4387 has_mbyte ? mb_l :
4388# endif
4389 1), (colnr_T)vcol, NULL) - 1;
4390 c_extra = ' ';
4391 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004392 {
4393#ifdef FEAT_CONCEAL
4394 if (c == TAB)
4395 /* See "Tab alignment" below. */
4396 FIX_FOR_BOGUSCOLS;
4397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401#endif
4402
4403 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4404 {
4405 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004406 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 n_attr = 1;
4409 extra_attr = hl_attr(HLF_8);
4410 saved_attr2 = char_attr; /* save current attr */
4411 }
4412#ifdef FEAT_MBYTE
4413 mb_c = c;
4414 if (enc_utf8 && (*mb_char2len)(c) > 1)
4415 {
4416 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004417 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004418 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 else
4421 mb_utf8 = FALSE;
4422#endif
4423 }
4424 }
4425
4426 /*
4427 * Handling of non-printable characters.
4428 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004429 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 /*
4432 * when getting a character from the file, we may have to
4433 * turn it into something else on the way to putting it
4434 * into "ScreenLines".
4435 */
4436 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4437 {
4438 /* tab amount depends on current column */
4439 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004440 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4441#ifdef FEAT_CONCEAL
4442 /* Tab alignment should be identical regardless of
4443 * 'conceallevel' value. So tab compensates of all
4444 * previous concealed characters, and thus resets vcol_off
4445 * and boguscols accumulated so far in the line. Note that
4446 * the tab can be longer than 'tabstop' when there
4447 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004448 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#ifdef FEAT_MBYTE
4451 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4452#endif
4453 if (wp->w_p_list)
4454 {
4455 c = lcs_tab1;
4456 c_extra = lcs_tab2;
4457 n_attr = n_extra + 1;
4458 extra_attr = hl_attr(HLF_8);
4459 saved_attr2 = char_attr; /* save current attr */
4460#ifdef FEAT_MBYTE
4461 mb_c = c;
4462 if (enc_utf8 && (*mb_char2len)(c) > 1)
4463 {
4464 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004466 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468#endif
4469 }
4470 else
4471 {
4472 c_extra = ' ';
4473 c = ' ';
4474 }
4475 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004476 else if (c == NUL
4477 && ((wp->w_p_list && lcs_eol > 0)
4478 || ((fromcol >= 0 || fromcol_prev >= 0)
4479 && tocol > vcol
4480 && VIsual_mode != Ctrl_V
4481 && (
4482# ifdef FEAT_RIGHTLEFT
4483 wp->w_p_rl ? (col >= 0) :
4484# endif
4485 (col < W_WIDTH(wp)))
4486 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004487 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004488 && (colnr_T)vcol == wp->w_virtcol)))
4489 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004491 /* Display a '$' after the line or highlight an extra
4492 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4494 /* For a diff line the highlighting continues after the
4495 * "$". */
4496 if (
4497# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004498 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499# ifdef LINE_ATTR
4500 &&
4501# endif
4502# endif
4503# ifdef LINE_ATTR
4504 line_attr == 0
4505# endif
4506 )
4507#endif
4508 {
4509#ifdef FEAT_VIRTUALEDIT
4510 /* In virtualedit, visual selections may extend
4511 * beyond end of line. */
4512 if (area_highlighting && virtual_active()
4513 && tocol != MAXCOL && vcol < tocol)
4514 n_extra = 0;
4515 else
4516#endif
4517 {
4518 p_extra = at_end_str;
4519 n_extra = 1;
4520 c_extra = NUL;
4521 }
4522 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004523 if (wp->w_p_list)
4524 c = lcs_eol;
4525 else
4526 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 lcs_eol_one = -1;
4528 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004529 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 extra_attr = hl_attr(HLF_AT);
4532 n_attr = 1;
4533 }
4534#ifdef FEAT_MBYTE
4535 mb_c = c;
4536 if (enc_utf8 && (*mb_char2len)(c) > 1)
4537 {
4538 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004539 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004540 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
4542 else
4543 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4544#endif
4545 }
4546 else if (c != NUL)
4547 {
4548 p_extra = transchar(c);
4549#ifdef FEAT_RIGHTLEFT
4550 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4551 rl_mirror(p_extra); /* reverse "<12>" */
4552#endif
4553 n_extra = byte2cells(c) - 1;
4554 c_extra = NUL;
4555 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004556 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 n_attr = n_extra + 1;
4559 extra_attr = hl_attr(HLF_8);
4560 saved_attr2 = char_attr; /* save current attr */
4561 }
4562#ifdef FEAT_MBYTE
4563 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4564#endif
4565 }
4566#ifdef FEAT_VIRTUALEDIT
4567 else if (VIsual_active
4568 && (VIsual_mode == Ctrl_V
4569 || VIsual_mode == 'v')
4570 && virtual_active()
4571 && tocol != MAXCOL
4572 && vcol < tocol
4573 && (
4574# ifdef FEAT_RIGHTLEFT
4575 wp->w_p_rl ? (col >= 0) :
4576# endif
4577 (col < W_WIDTH(wp))))
4578 {
4579 c = ' ';
4580 --ptr; /* put it back at the NUL */
4581 }
4582#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004583#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 else if ((
4585# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004586 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 ) && (
4590# ifdef FEAT_RIGHTLEFT
4591 wp->w_p_rl ? (col >= 0) :
4592# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004593 (col
4594# ifdef FEAT_CONCEAL
4595 - boguscols
4596# endif
4597 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
4599 /* Highlight until the right side of the window */
4600 c = ' ';
4601 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004602
4603 /* Remember we do the char for line highlighting. */
4604 ++did_line_attr;
4605
4606 /* don't do search HL for the rest of the line */
4607 if (line_attr != 0 && char_attr == search_attr && col > 0)
4608 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609# ifdef FEAT_DIFF
4610 if (diff_hlf == HLF_TXD)
4611 {
4612 diff_hlf = HLF_CHD;
4613 if (attr == 0 || char_attr != attr)
4614 char_attr = hl_attr(diff_hlf);
4615 }
4616# endif
4617 }
4618#endif
4619 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004620
4621#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004622 if ( wp->w_p_cole > 0
4623 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4624 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004625 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004626 && !(lnum_in_visual_area
4627 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004628 {
4629 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004630 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004631 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4632 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004633 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004634 /* First time at this concealed item: display one
4635 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004636 if (syn_get_sub_char() != NUL)
4637 c = syn_get_sub_char();
4638 else if (lcs_conceal != NUL)
4639 c = lcs_conceal;
4640 else
4641 c = ' ';
4642
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004643 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004644
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004645 if (n_extra > 0)
4646 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004647 vcol += n_extra;
4648 if (wp->w_p_wrap && n_extra > 0)
4649 {
4650# ifdef FEAT_RIGHTLEFT
4651 if (wp->w_p_rl)
4652 {
4653 col -= n_extra;
4654 boguscols -= n_extra;
4655 }
4656 else
4657# endif
4658 {
4659 boguscols += n_extra;
4660 col += n_extra;
4661 }
4662 }
4663 n_extra = 0;
4664 n_attr = 0;
4665 }
4666 else if (n_skip == 0)
4667 {
4668 is_concealing = TRUE;
4669 n_skip = 1;
4670 }
4671# ifdef FEAT_MBYTE
4672 mb_c = c;
4673 if (enc_utf8 && (*mb_char2len)(c) > 1)
4674 {
4675 mb_utf8 = TRUE;
4676 u8cc[0] = 0;
4677 c = 0xc0;
4678 }
4679 else
4680 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4681# endif
4682 }
4683 else
4684 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004685 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004686 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004687 }
4688#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004691#ifdef FEAT_CONCEAL
4692 /* In the cursor line and we may be concealing characters: correct
4693 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004694 if (!did_wcol && draw_state == WL_LINE
4695 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004696 && conceal_cursor_line(wp)
4697 && (int)wp->w_virtcol <= vcol + n_skip)
4698 {
4699 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004700 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004701 did_wcol = TRUE;
4702 }
4703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 /* Don't override visual selection highlighting. */
4706 if (n_attr > 0
4707 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004708 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 char_attr = extra_attr;
4710
Bram Moolenaar81695252004-12-29 20:58:21 +00004711#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 /* XIM don't send preedit_start and preedit_end, but they send
4713 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4714 * im_is_preediting() here. */
4715 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004716 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 && (State & INSERT)
4718 && !p_imdisable
4719 && im_is_preediting()
4720 && draw_state == WL_LINE)
4721 {
4722 colnr_T tcol;
4723
4724 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004725 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 else
4727 tcol = preedit_end_col;
4728 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4729 {
4730 if (feedback_old_attr < 0)
4731 {
4732 feedback_col = 0;
4733 feedback_old_attr = char_attr;
4734 }
4735 char_attr = im_get_feedback_attr(feedback_col);
4736 if (char_attr < 0)
4737 char_attr = feedback_old_attr;
4738 feedback_col++;
4739 }
4740 else if (feedback_old_attr >= 0)
4741 {
4742 char_attr = feedback_old_attr;
4743 feedback_old_attr = -1;
4744 feedback_col = 0;
4745 }
4746 }
4747#endif
4748 /*
4749 * Handle the case where we are in column 0 but not on the first
4750 * character of the line and the user wants us to show us a
4751 * special character (via 'listchars' option "precedes:<char>".
4752 */
4753 if (lcs_prec_todo != NUL
4754 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4755#ifdef FEAT_DIFF
4756 && filler_todo <= 0
4757#endif
4758 && draw_state > WL_NR
4759 && c != NUL)
4760 {
4761 c = lcs_prec;
4762 lcs_prec_todo = NUL;
4763#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004764 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4765 {
4766 /* Double-width character being overwritten by the "precedes"
4767 * character, need to fill up half the character. */
4768 c_extra = MB_FILLER_CHAR;
4769 n_extra = 1;
4770 n_attr = 2;
4771 extra_attr = hl_attr(HLF_AT);
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 mb_c = c;
4774 if (enc_utf8 && (*mb_char2len)(c) > 1)
4775 {
4776 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004777 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004778 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 else
4781 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4782#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004783 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 saved_attr3 = char_attr; /* save current attr */
4786 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4787 n_attr3 = 1;
4788 }
4789 }
4790
4791 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004792 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004794 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004795#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004796 || did_line_attr == 1
4797#endif
4798 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004800#ifdef FEAT_SEARCH_EXTRA
4801 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004802
4803 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004804 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004805 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004806#endif
4807
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004808 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 * highlight match at end of line. If it's beyond the last
4810 * char on the screen, just overwrite that one (tricky!) Not
4811 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004812#ifdef FEAT_SEARCH_EXTRA
4813 prevcol_hl_flag = FALSE;
4814 if (prevcol == (long)search_hl.startcol)
4815 prevcol_hl_flag = TRUE;
4816 else
4817 {
4818 cur = wp->w_match_head;
4819 while (cur != NULL)
4820 {
4821 if (prevcol == (long)cur->hl.startcol)
4822 {
4823 prevcol_hl_flag = TRUE;
4824 break;
4825 }
4826 cur = cur->next;
4827 }
4828 }
4829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004831 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004832 && (VIsual_mode != Ctrl_V
4833 || lnum == VIsual.lnum
4834 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004835 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#ifdef FEAT_SEARCH_EXTRA
4837 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004838 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004839# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004840 && did_line_attr <= 1
4841# endif
4842 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#endif
4844 ))
4845 {
4846 int n = 0;
4847
4848#ifdef FEAT_RIGHTLEFT
4849 if (wp->w_p_rl)
4850 {
4851 if (col < 0)
4852 n = 1;
4853 }
4854 else
4855#endif
4856 {
4857 if (col >= W_WIDTH(wp))
4858 n = -1;
4859 }
4860 if (n != 0)
4861 {
4862 /* At the window boundary, highlight the last character
4863 * instead (better than nothing). */
4864 off += n;
4865 col += n;
4866 }
4867 else
4868 {
4869 /* Add a blank character to highlight. */
4870 ScreenLines[off] = ' ';
4871#ifdef FEAT_MBYTE
4872 if (enc_utf8)
4873 ScreenLinesUC[off] = 0;
4874#endif
4875 }
4876#ifdef FEAT_SEARCH_EXTRA
4877 if (area_attr == 0)
4878 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004879 /* Use attributes from match with highest priority among
4880 * 'search_hl' and the match list. */
4881 char_attr = search_hl.attr;
4882 cur = wp->w_match_head;
4883 shl_flag = FALSE;
4884 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004885 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004886 if (shl_flag == FALSE
4887 && ((cur != NULL
4888 && cur->priority > SEARCH_HL_PRIORITY)
4889 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004890 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004891 shl = &search_hl;
4892 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004893 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004894 else
4895 shl = &cur->hl;
4896 if ((ptr - line) - 1 == (long)shl->startcol)
4897 char_attr = shl->attr;
4898 if (shl != &search_hl && cur != NULL)
4899 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902#endif
4903 ScreenAttrs[off] = char_attr;
4904#ifdef FEAT_RIGHTLEFT
4905 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004908 --off;
4909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 else
4911#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004914 ++off;
4915 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004916 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004917#ifdef FEAT_SYN_HL
4918 eol_hl_off = 1;
4919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
Bram Moolenaar91170f82006-05-05 21:15:17 +00004923 /*
4924 * At end of the text line.
4925 */
4926 if (c == NUL)
4927 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004928#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004929 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4930 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004931 {
4932 /* highlight last char after line */
4933 --col;
4934 --off;
4935 --vcol;
4936 }
4937
Bram Moolenaar1a384422010-07-14 19:53:30 +02004938 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004939 if (wp->w_p_wrap)
4940 v = wp->w_skipcol;
4941 else
4942 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004943
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004944 /* check if line ends before left margin */
4945 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004946 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004947#ifdef FEAT_CONCEAL
4948 /* Get rid of the boguscols now, we want to draw until the right
4949 * edge for 'cursorcolumn'. */
4950 col -= boguscols;
4951 boguscols = 0;
4952#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004953
4954 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004955 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004956
4957 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004958 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4959 && (int)wp->w_virtcol <
4960 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004961 && lnum != wp->w_cursor.lnum)
4962 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004963# ifdef FEAT_RIGHTLEFT
4964 && !wp->w_p_rl
4965# endif
4966 )
4967 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004968 int rightmost_vcol = 0;
4969 int i;
4970
4971 if (wp->w_p_cuc)
4972 rightmost_vcol = wp->w_virtcol;
4973 if (draw_color_col)
4974 /* determine rightmost colorcolumn to possibly draw */
4975 for (i = 0; color_cols[i] >= 0; ++i)
4976 if (rightmost_vcol < color_cols[i])
4977 rightmost_vcol = color_cols[i];
4978
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004979 while (col < W_WIDTH(wp))
4980 {
4981 ScreenLines[off] = ' ';
4982#ifdef FEAT_MBYTE
4983 if (enc_utf8)
4984 ScreenLinesUC[off] = 0;
4985#endif
4986 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004987 if (draw_color_col)
4988 draw_color_col = advance_color_col(VCOL_HLC,
4989 &color_cols);
4990
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004991 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004992 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004993 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004994 ScreenAttrs[off++] = hl_attr(HLF_MC);
4995 else
4996 ScreenAttrs[off++] = 0;
4997
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004998 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004999 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005000
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005001 ++vcol;
5002 }
5003 }
5004#endif
5005
Bram Moolenaar860cae12010-06-05 23:22:07 +02005006 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5007 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 row++;
5009
5010 /*
5011 * Update w_cline_height and w_cline_folded if the cursor line was
5012 * updated (saves a call to plines() later).
5013 */
5014 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5015 {
5016 curwin->w_cline_row = startrow;
5017 curwin->w_cline_height = row - startrow;
5018#ifdef FEAT_FOLDING
5019 curwin->w_cline_folded = FALSE;
5020#endif
5021 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5022 }
5023
5024 break;
5025 }
5026
5027 /* line continues beyond line end */
5028 if (lcs_ext
5029 && !wp->w_p_wrap
5030#ifdef FEAT_DIFF
5031 && filler_todo <= 0
5032#endif
5033 && (
5034#ifdef FEAT_RIGHTLEFT
5035 wp->w_p_rl ? col == 0 :
5036#endif
5037 col == W_WIDTH(wp) - 1)
5038 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005039 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5041 {
5042 c = lcs_ext;
5043 char_attr = hl_attr(HLF_AT);
5044#ifdef FEAT_MBYTE
5045 mb_c = c;
5046 if (enc_utf8 && (*mb_char2len)(c) > 1)
5047 {
5048 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005049 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005050 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 else
5053 mb_utf8 = FALSE;
5054#endif
5055 }
5056
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005057#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005058 /* advance to the next 'colorcolumn' */
5059 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005060 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005061
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005062 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005063 * highlight the cursor position itself.
5064 * Also highlight the 'colorcolumn' if it is different than
5065 * 'cursorcolumn' */
5066 vcol_save_attr = -1;
5067 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005068 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005069 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005070 && lnum != wp->w_cursor.lnum)
5071 {
5072 vcol_save_attr = char_attr;
5073 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5074 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005075 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005076 {
5077 vcol_save_attr = char_attr;
5078 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5079 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005080 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005081#endif
5082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 /*
5084 * Store character to be displayed.
5085 * Skip characters that are left of the screen for 'nowrap'.
5086 */
5087 vcol_prev = vcol;
5088 if (draw_state < WL_LINE || n_skip <= 0)
5089 {
5090 /*
5091 * Store the character.
5092 */
5093#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5094 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5095 {
5096 /* A double-wide character is: put first halve in left cell. */
5097 --off;
5098 --col;
5099 }
5100#endif
5101 ScreenLines[off] = c;
5102#ifdef FEAT_MBYTE
5103 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005104 {
5105 if ((mb_c & 0xff00) == 0x8e00)
5106 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 else if (enc_utf8)
5110 {
5111 if (mb_utf8)
5112 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005113 int i;
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005116 if ((c & 0xff) == 0)
5117 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005118 for (i = 0; i < Screen_mco; ++i)
5119 {
5120 ScreenLinesC[i][off] = u8cc[i];
5121 if (u8cc[i] == 0)
5122 break;
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 else
5126 ScreenLinesUC[off] = 0;
5127 }
5128 if (multi_attr)
5129 {
5130 ScreenAttrs[off] = multi_attr;
5131 multi_attr = 0;
5132 }
5133 else
5134#endif
5135 ScreenAttrs[off] = char_attr;
5136
5137#ifdef FEAT_MBYTE
5138 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5139 {
5140 /* Need to fill two screen columns. */
5141 ++off;
5142 ++col;
5143 if (enc_utf8)
5144 /* UTF-8: Put a 0 in the second screen char. */
5145 ScreenLines[off] = 0;
5146 else
5147 /* DBCS: Put second byte in the second screen char. */
5148 ScreenLines[off] = mb_c & 0xff;
5149 ++vcol;
5150 /* When "tocol" is halfway a character, set it to the end of
5151 * the character, otherwise highlighting won't stop. */
5152 if (tocol == vcol)
5153 ++tocol;
5154#ifdef FEAT_RIGHTLEFT
5155 if (wp->w_p_rl)
5156 {
5157 /* now it's time to backup one cell */
5158 --off;
5159 --col;
5160 }
5161#endif
5162 }
5163#endif
5164#ifdef FEAT_RIGHTLEFT
5165 if (wp->w_p_rl)
5166 {
5167 --off;
5168 --col;
5169 }
5170 else
5171#endif
5172 {
5173 ++off;
5174 ++col;
5175 }
5176 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005177#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005178 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005179 {
5180 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005181 ++vcol_off;
5182 if (n_extra > 0)
5183 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005184 if (wp->w_p_wrap)
5185 {
5186 /*
5187 * Special voodoo required if 'wrap' is on.
5188 *
5189 * Advance the column indicator to force the line
5190 * drawing to wrap early. This will make the line
5191 * take up the same screen space when parts are concealed,
5192 * so that cursor line computations aren't messed up.
5193 *
5194 * To avoid the fictitious advance of 'col' causing
5195 * trailing junk to be written out of the screen line
5196 * we are building, 'boguscols' keeps track of the number
5197 * of bad columns we have advanced.
5198 */
5199 if (n_extra > 0)
5200 {
5201 vcol += n_extra;
5202# ifdef FEAT_RIGHTLEFT
5203 if (wp->w_p_rl)
5204 {
5205 col -= n_extra;
5206 boguscols -= n_extra;
5207 }
5208 else
5209# endif
5210 {
5211 col += n_extra;
5212 boguscols += n_extra;
5213 }
5214 n_extra = 0;
5215 n_attr = 0;
5216 }
5217
5218
5219# ifdef FEAT_MBYTE
5220 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5221 {
5222 /* Need to fill two screen columns. */
5223# ifdef FEAT_RIGHTLEFT
5224 if (wp->w_p_rl)
5225 {
5226 --boguscols;
5227 --col;
5228 }
5229 else
5230# endif
5231 {
5232 ++boguscols;
5233 ++col;
5234 }
5235 }
5236# endif
5237
5238# ifdef FEAT_RIGHTLEFT
5239 if (wp->w_p_rl)
5240 {
5241 --boguscols;
5242 --col;
5243 }
5244 else
5245# endif
5246 {
5247 ++boguscols;
5248 ++col;
5249 }
5250 }
5251 else
5252 {
5253 if (n_extra > 0)
5254 {
5255 vcol += n_extra;
5256 n_extra = 0;
5257 n_attr = 0;
5258 }
5259 }
5260
5261 }
5262#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else
5264 --n_skip;
5265
Bram Moolenaar64486672010-05-16 15:46:46 +02005266 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5267 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005268 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#ifdef FEAT_DIFF
5270 && filler_todo <= 0
5271#endif
5272 )
5273 ++vcol;
5274
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005275#ifdef FEAT_SYN_HL
5276 if (vcol_save_attr >= 0)
5277 char_attr = vcol_save_attr;
5278#endif
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 /* restore attributes after "predeces" in 'listchars' */
5281 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5282 char_attr = saved_attr3;
5283
5284 /* restore attributes after last 'listchars' or 'number' char */
5285 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5286 char_attr = saved_attr2;
5287
5288 /*
5289 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005290 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 */
5292 if ((
5293#ifdef FEAT_RIGHTLEFT
5294 wp->w_p_rl ? (col < 0) :
5295#endif
5296 (col >= W_WIDTH(wp)))
5297 && (*ptr != NUL
5298#ifdef FEAT_DIFF
5299 || filler_todo > 0
5300#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005301 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5303 )
5304 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005305#ifdef FEAT_CONCEAL
5306 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5307 (int)W_WIDTH(wp), wp->w_p_rl);
5308 boguscols = 0;
5309#else
5310 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5311 (int)W_WIDTH(wp), wp->w_p_rl);
5312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 ++row;
5314 ++screen_row;
5315
5316 /* When not wrapping and finished diff lines, or when displayed
5317 * '$' and highlighting until last column, break here. */
5318 if ((!wp->w_p_wrap
5319#ifdef FEAT_DIFF
5320 && filler_todo <= 0
5321#endif
5322 ) || lcs_eol_one == -1)
5323 break;
5324
5325 /* When the window is too narrow draw all "@" lines. */
5326 if (draw_state != WL_LINE
5327#ifdef FEAT_DIFF
5328 && filler_todo <= 0
5329#endif
5330 )
5331 {
5332 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5333#ifdef FEAT_VERTSPLIT
5334 draw_vsep_win(wp, row);
5335#endif
5336 row = endrow;
5337 }
5338
5339 /* When line got too long for screen break here. */
5340 if (row == endrow)
5341 {
5342 ++row;
5343 break;
5344 }
5345
5346 if (screen_cur_row == screen_row - 1
5347#ifdef FEAT_DIFF
5348 && filler_todo <= 0
5349#endif
5350 && W_WIDTH(wp) == Columns)
5351 {
5352 /* Remember that the line wraps, used for modeless copy. */
5353 LineWraps[screen_row - 1] = TRUE;
5354
5355 /*
5356 * Special trick to make copy/paste of wrapped lines work with
5357 * xterm/screen: write an extra character beyond the end of
5358 * the line. This will work with all terminal types
5359 * (regardless of the xn,am settings).
5360 * Only do this on a fast tty.
5361 * Only do this if the cursor is on the current line
5362 * (something has been written in it).
5363 * Don't do this for the GUI.
5364 * Don't do this for double-width characters.
5365 * Don't do this for a window not at the right screen border.
5366 */
5367 if (p_tf
5368#ifdef FEAT_GUI
5369 && !gui.in_use
5370#endif
5371#ifdef FEAT_MBYTE
5372 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005373 && ((*mb_off2cells)(LineOffset[screen_row],
5374 LineOffset[screen_row] + screen_Columns)
5375 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005377 + (int)Columns - 2,
5378 LineOffset[screen_row] + screen_Columns)
5379 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380#endif
5381 )
5382 {
5383 /* First make sure we are at the end of the screen line,
5384 * then output the same character again to let the
5385 * terminal know about the wrap. If the terminal doesn't
5386 * auto-wrap, we overwrite the character. */
5387 if (screen_cur_col != W_WIDTH(wp))
5388 screen_char(LineOffset[screen_row - 1]
5389 + (unsigned)Columns - 1,
5390 screen_row - 1, (int)(Columns - 1));
5391
5392#ifdef FEAT_MBYTE
5393 /* When there is a multi-byte character, just output a
5394 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005395 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5396 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 out_char(' ');
5398 else
5399#endif
5400 out_char(ScreenLines[LineOffset[screen_row - 1]
5401 + (Columns - 1)]);
5402 /* force a redraw of the first char on the next line */
5403 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5404 screen_start(); /* don't know where cursor is now */
5405 }
5406 }
5407
5408 col = 0;
5409 off = (unsigned)(current_ScreenLine - ScreenLines);
5410#ifdef FEAT_RIGHTLEFT
5411 if (wp->w_p_rl)
5412 {
5413 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5414 off += col;
5415 }
5416#endif
5417
5418 /* reset the drawing state for the start of a wrapped line */
5419 draw_state = WL_START;
5420 saved_n_extra = n_extra;
5421 saved_p_extra = p_extra;
5422 saved_c_extra = c_extra;
5423 saved_char_attr = char_attr;
5424 n_extra = 0;
5425 lcs_prec_todo = lcs_prec;
5426#ifdef FEAT_LINEBREAK
5427# ifdef FEAT_DIFF
5428 if (filler_todo <= 0)
5429# endif
5430 need_showbreak = TRUE;
5431#endif
5432#ifdef FEAT_DIFF
5433 --filler_todo;
5434 /* When the filler lines are actually below the last line of the
5435 * file, don't draw the line itself, break here. */
5436 if (filler_todo == 0 && wp->w_botfill)
5437 break;
5438#endif
5439 }
5440
5441 } /* for every character in the line */
5442
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005443#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005444 /* After an empty line check first word for capital. */
5445 if (*skipwhite(line) == NUL)
5446 {
5447 capcol_lnum = lnum + 1;
5448 cap_col = 0;
5449 }
5450#endif
5451
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 return row;
5453}
5454
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005455#ifdef FEAT_MBYTE
5456static int comp_char_differs __ARGS((int, int));
5457
5458/*
5459 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005460 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005461 */
5462 static int
5463comp_char_differs(off_from, off_to)
5464 int off_from;
5465 int off_to;
5466{
5467 int i;
5468
5469 for (i = 0; i < Screen_mco; ++i)
5470 {
5471 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5472 return TRUE;
5473 if (ScreenLinesC[i][off_from] == 0)
5474 break;
5475 }
5476 return FALSE;
5477}
5478#endif
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480/*
5481 * Check whether the given character needs redrawing:
5482 * - the (first byte of the) character is different
5483 * - the attributes are different
5484 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005485 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 */
5487 static int
5488char_needs_redraw(off_from, off_to, cols)
5489 int off_from;
5490 int off_to;
5491 int cols;
5492{
5493 if (cols > 0
5494 && ((ScreenLines[off_from] != ScreenLines[off_to]
5495 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5496
5497#ifdef FEAT_MBYTE
5498 || (enc_dbcs != 0
5499 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5500 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5501 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5502 : (cols > 1 && ScreenLines[off_from + 1]
5503 != ScreenLines[off_to + 1])))
5504 || (enc_utf8
5505 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5506 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005507 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005508 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5509 && ScreenLines[off_from + 1]
5510 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#endif
5512 ))
5513 return TRUE;
5514 return FALSE;
5515}
5516
5517/*
5518 * Move one "cooked" screen line to the screen, but only the characters that
5519 * have actually changed. Handle insert/delete character.
5520 * "coloff" gives the first column on the screen for this line.
5521 * "endcol" gives the columns where valid characters are.
5522 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5523 * needs to be cleared, negative otherwise.
5524 * "rlflag" is TRUE in a rightleft window:
5525 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5526 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5527 */
5528 static void
5529screen_line(row, coloff, endcol, clear_width
5530#ifdef FEAT_RIGHTLEFT
5531 , rlflag
5532#endif
5533 )
5534 int row;
5535 int coloff;
5536 int endcol;
5537 int clear_width;
5538#ifdef FEAT_RIGHTLEFT
5539 int rlflag;
5540#endif
5541{
5542 unsigned off_from;
5543 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005544#ifdef FEAT_MBYTE
5545 unsigned max_off_from;
5546 unsigned max_off_to;
5547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 int col = 0;
5549#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5550 int hl;
5551#endif
5552 int force = FALSE; /* force update rest of the line */
5553 int redraw_this /* bool: does character need redraw? */
5554#ifdef FEAT_GUI
5555 = TRUE /* For GUI when while-loop empty */
5556#endif
5557 ;
5558 int redraw_next; /* redraw_this for next character */
5559#ifdef FEAT_MBYTE
5560 int clear_next = FALSE;
5561 int char_cells; /* 1: normal char */
5562 /* 2: occupies two display cells */
5563# define CHAR_CELLS char_cells
5564#else
5565# define CHAR_CELLS 1
5566#endif
5567
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005568 /* Check for illegal row and col, just in case. */
5569 if (row >= Rows)
5570 row = Rows - 1;
5571 if (endcol > Columns)
5572 endcol = Columns;
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574# ifdef FEAT_CLIPBOARD
5575 clip_may_clear_selection(row, row);
5576# endif
5577
5578 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5579 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005580#ifdef FEAT_MBYTE
5581 max_off_from = off_from + screen_Columns;
5582 max_off_to = LineOffset[row] + screen_Columns;
5583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585#ifdef FEAT_RIGHTLEFT
5586 if (rlflag)
5587 {
5588 /* Clear rest first, because it's left of the text. */
5589 if (clear_width > 0)
5590 {
5591 while (col <= endcol && ScreenLines[off_to] == ' '
5592 && ScreenAttrs[off_to] == 0
5593# ifdef FEAT_MBYTE
5594 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5595# endif
5596 )
5597 {
5598 ++off_to;
5599 ++col;
5600 }
5601 if (col <= endcol)
5602 screen_fill(row, row + 1, col + coloff,
5603 endcol + coloff + 1, ' ', ' ', 0);
5604 }
5605 col = endcol + 1;
5606 off_to = LineOffset[row] + col + coloff;
5607 off_from += col;
5608 endcol = (clear_width > 0 ? clear_width : -clear_width);
5609 }
5610#endif /* FEAT_RIGHTLEFT */
5611
5612 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5613
5614 while (col < endcol)
5615 {
5616#ifdef FEAT_MBYTE
5617 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005618 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 else
5620 char_cells = 1;
5621#endif
5622
5623 redraw_this = redraw_next;
5624 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5625 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5626
5627#ifdef FEAT_GUI
5628 /* If the next character was bold, then redraw the current character to
5629 * remove any pixels that might have spilt over into us. This only
5630 * happens in the GUI.
5631 */
5632 if (redraw_next && gui.in_use)
5633 {
5634 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635 if (hl > HL_ALL)
5636 hl = syn_attr2attr(hl);
5637 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 redraw_this = TRUE;
5639 }
5640#endif
5641
5642 if (redraw_this)
5643 {
5644 /*
5645 * Special handling when 'xs' termcap flag set (hpterm):
5646 * Attributes for characters are stored at the position where the
5647 * cursor is when writing the highlighting code. The
5648 * start-highlighting code must be written with the cursor on the
5649 * first highlighted character. The stop-highlighting code must
5650 * be written with the cursor just after the last highlighted
5651 * character.
5652 * Overwriting a character doesn't remove it's highlighting. Need
5653 * to clear the rest of the line, and force redrawing it
5654 * completely.
5655 */
5656 if ( p_wiv
5657 && !force
5658#ifdef FEAT_GUI
5659 && !gui.in_use
5660#endif
5661 && ScreenAttrs[off_to] != 0
5662 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5663 {
5664 /*
5665 * Need to remove highlighting attributes here.
5666 */
5667 windgoto(row, col + coloff);
5668 out_str(T_CE); /* clear rest of this screen line */
5669 screen_start(); /* don't know where cursor is now */
5670 force = TRUE; /* force redraw of rest of the line */
5671 redraw_next = TRUE; /* or else next char would miss out */
5672
5673 /*
5674 * If the previous character was highlighted, need to stop
5675 * highlighting at this character.
5676 */
5677 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5678 {
5679 screen_attr = ScreenAttrs[off_to - 1];
5680 term_windgoto(row, col + coloff);
5681 screen_stop_highlight();
5682 }
5683 else
5684 screen_attr = 0; /* highlighting has stopped */
5685 }
5686#ifdef FEAT_MBYTE
5687 if (enc_dbcs != 0)
5688 {
5689 /* Check if overwriting a double-byte with a single-byte or
5690 * the other way around requires another character to be
5691 * redrawn. For UTF-8 this isn't needed, because comparing
5692 * ScreenLinesUC[] is sufficient. */
5693 if (char_cells == 1
5694 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005695 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 /* Writing a single-cell character over a double-cell
5698 * character: need to redraw the next cell. */
5699 ScreenLines[off_to + 1] = 0;
5700 redraw_next = TRUE;
5701 }
5702 else if (char_cells == 2
5703 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005704 && (*mb_off2cells)(off_to, max_off_to) == 1
5705 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 /* Writing the second half of a double-cell character over
5708 * a double-cell character: need to redraw the second
5709 * cell. */
5710 ScreenLines[off_to + 2] = 0;
5711 redraw_next = TRUE;
5712 }
5713
5714 if (enc_dbcs == DBCS_JPNU)
5715 ScreenLines2[off_to] = ScreenLines2[off_from];
5716 }
5717 /* When writing a single-width character over a double-width
5718 * character and at the end of the redrawn text, need to clear out
5719 * the right halve of the old character.
5720 * Also required when writing the right halve of a double-width
5721 * char over the left halve of an existing one. */
5722 if (has_mbyte && col + char_cells == endcol
5723 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005724 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005726 && (*mb_off2cells)(off_to, max_off_to) == 1
5727 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 clear_next = TRUE;
5729#endif
5730
5731 ScreenLines[off_to] = ScreenLines[off_from];
5732#ifdef FEAT_MBYTE
5733 if (enc_utf8)
5734 {
5735 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5736 if (ScreenLinesUC[off_from] != 0)
5737 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005738 int i;
5739
5740 for (i = 0; i < Screen_mco; ++i)
5741 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 }
5744 if (char_cells == 2)
5745 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5746#endif
5747
5748#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005749 /* The bold trick makes a single column of pixels appear in the
5750 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 * character should be redrawn too. This happens for our own GUI
5752 * and for some xterms. */
5753 if (
5754# ifdef FEAT_GUI
5755 gui.in_use
5756# endif
5757# if defined(FEAT_GUI) && defined(UNIX)
5758 ||
5759# endif
5760# ifdef UNIX
5761 term_is_xterm
5762# endif
5763 )
5764 {
5765 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005766 if (hl > HL_ALL)
5767 hl = syn_attr2attr(hl);
5768 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 redraw_next = TRUE;
5770 }
5771#endif
5772 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5773#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005774 /* For simplicity set the attributes of second half of a
5775 * double-wide character equal to the first half. */
5776 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005778
5779 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else
5782#endif
5783 screen_char(off_to, row, col + coloff);
5784 }
5785 else if ( p_wiv
5786#ifdef FEAT_GUI
5787 && !gui.in_use
5788#endif
5789 && col + coloff > 0)
5790 {
5791 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5792 {
5793 /*
5794 * Don't output stop-highlight when moving the cursor, it will
5795 * stop the highlighting when it should continue.
5796 */
5797 screen_attr = 0;
5798 }
5799 else if (screen_attr != 0)
5800 screen_stop_highlight();
5801 }
5802
5803 off_to += CHAR_CELLS;
5804 off_from += CHAR_CELLS;
5805 col += CHAR_CELLS;
5806 }
5807
5808#ifdef FEAT_MBYTE
5809 if (clear_next)
5810 {
5811 /* Clear the second half of a double-wide character of which the left
5812 * half was overwritten with a single-wide character. */
5813 ScreenLines[off_to] = ' ';
5814 if (enc_utf8)
5815 ScreenLinesUC[off_to] = 0;
5816 screen_char(off_to, row, col + coloff);
5817 }
5818#endif
5819
5820 if (clear_width > 0
5821#ifdef FEAT_RIGHTLEFT
5822 && !rlflag
5823#endif
5824 )
5825 {
5826#ifdef FEAT_GUI
5827 int startCol = col;
5828#endif
5829
5830 /* blank out the rest of the line */
5831 while (col < clear_width && ScreenLines[off_to] == ' '
5832 && ScreenAttrs[off_to] == 0
5833#ifdef FEAT_MBYTE
5834 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5835#endif
5836 )
5837 {
5838 ++off_to;
5839 ++col;
5840 }
5841 if (col < clear_width)
5842 {
5843#ifdef FEAT_GUI
5844 /*
5845 * In the GUI, clearing the rest of the line may leave pixels
5846 * behind if the first character cleared was bold. Some bold
5847 * fonts spill over the left. In this case we redraw the previous
5848 * character too. If we didn't skip any blanks above, then we
5849 * only redraw if the character wasn't already redrawn anyway.
5850 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005851 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
5853 hl = ScreenAttrs[off_to];
5854 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005855 {
5856 int prev_cells = 1;
5857# ifdef FEAT_MBYTE
5858 if (enc_utf8)
5859 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5860 * that its width is 2. */
5861 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5862 else if (enc_dbcs != 0)
5863 {
5864 /* find previous character by counting from first
5865 * column and get its width. */
5866 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005867 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005868
5869 while (off < off_to)
5870 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005871 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005872 off += prev_cells;
5873 }
5874 }
5875
5876 if (enc_dbcs != 0 && prev_cells > 1)
5877 screen_char_2(off_to - prev_cells, row,
5878 col + coloff - prev_cells);
5879 else
5880# endif
5881 screen_char(off_to - prev_cells, row,
5882 col + coloff - prev_cells);
5883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885#endif
5886 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5887 ' ', ' ', 0);
5888#ifdef FEAT_VERTSPLIT
5889 off_to += clear_width - col;
5890 col = clear_width;
5891#endif
5892 }
5893 }
5894
5895 if (clear_width > 0)
5896 {
5897#ifdef FEAT_VERTSPLIT
5898 /* For a window that's left of another, draw the separator char. */
5899 if (col + coloff < Columns)
5900 {
5901 int c;
5902
5903 c = fillchar_vsep(&hl);
5904 if (ScreenLines[off_to] != c
5905# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005906 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5907 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908# endif
5909 || ScreenAttrs[off_to] != hl)
5910 {
5911 ScreenLines[off_to] = c;
5912 ScreenAttrs[off_to] = hl;
5913# ifdef FEAT_MBYTE
5914 if (enc_utf8)
5915 {
5916 if (c >= 0x80)
5917 {
5918 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005919 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
5921 else
5922 ScreenLinesUC[off_to] = 0;
5923 }
5924# endif
5925 screen_char(off_to, row, col + coloff);
5926 }
5927 }
5928 else
5929#endif
5930 LineWraps[row] = FALSE;
5931 }
5932}
5933
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005934#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005936 * Mirror text "str" for right-left displaying.
5937 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005939 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940rl_mirror(str)
5941 char_u *str;
5942{
5943 char_u *p1, *p2;
5944 int t;
5945
5946 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5947 {
5948 t = *p1;
5949 *p1 = *p2;
5950 *p2 = t;
5951 }
5952}
5953#endif
5954
5955#if defined(FEAT_WINDOWS) || defined(PROTO)
5956/*
5957 * mark all status lines for redraw; used after first :cd
5958 */
5959 void
5960status_redraw_all()
5961{
5962 win_T *wp;
5963
5964 for (wp = firstwin; wp; wp = wp->w_next)
5965 if (wp->w_status_height)
5966 {
5967 wp->w_redr_status = TRUE;
5968 redraw_later(VALID);
5969 }
5970}
5971
5972/*
5973 * mark all status lines of the current buffer for redraw
5974 */
5975 void
5976status_redraw_curbuf()
5977{
5978 win_T *wp;
5979
5980 for (wp = firstwin; wp; wp = wp->w_next)
5981 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5982 {
5983 wp->w_redr_status = TRUE;
5984 redraw_later(VALID);
5985 }
5986}
5987
5988/*
5989 * Redraw all status lines that need to be redrawn.
5990 */
5991 void
5992redraw_statuslines()
5993{
5994 win_T *wp;
5995
5996 for (wp = firstwin; wp; wp = wp->w_next)
5997 if (wp->w_redr_status)
5998 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005999 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006000 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001}
6002#endif
6003
6004#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6005/*
6006 * Redraw all status lines at the bottom of frame "frp".
6007 */
6008 void
6009win_redraw_last_status(frp)
6010 frame_T *frp;
6011{
6012 if (frp->fr_layout == FR_LEAF)
6013 frp->fr_win->w_redr_status = TRUE;
6014 else if (frp->fr_layout == FR_ROW)
6015 {
6016 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6017 win_redraw_last_status(frp);
6018 }
6019 else /* frp->fr_layout == FR_COL */
6020 {
6021 frp = frp->fr_child;
6022 while (frp->fr_next != NULL)
6023 frp = frp->fr_next;
6024 win_redraw_last_status(frp);
6025 }
6026}
6027#endif
6028
6029#ifdef FEAT_VERTSPLIT
6030/*
6031 * Draw the verticap separator right of window "wp" starting with line "row".
6032 */
6033 static void
6034draw_vsep_win(wp, row)
6035 win_T *wp;
6036 int row;
6037{
6038 int hl;
6039 int c;
6040
6041 if (wp->w_vsep_width)
6042 {
6043 /* draw the vertical separator right of this window */
6044 c = fillchar_vsep(&hl);
6045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6046 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6047 c, ' ', hl);
6048 }
6049}
6050#endif
6051
6052#ifdef FEAT_WILDMENU
6053static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006054static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006057 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 */
6059 static int
6060status_match_len(xp, s)
6061 expand_T *xp;
6062 char_u *s;
6063{
6064 int len = 0;
6065
6066#ifdef FEAT_MENU
6067 int emenu = (xp->xp_context == EXPAND_MENUS
6068 || xp->xp_context == EXPAND_MENUNAMES);
6069
6070 /* Check for menu separators - replace with '|'. */
6071 if (emenu && menu_is_separator(s))
6072 return 1;
6073#endif
6074
6075 while (*s != NUL)
6076 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006077 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006078 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006079 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 }
6081
6082 return len;
6083}
6084
6085/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006086 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006087 * These are backslashes used for escaping. Do show backslashes in help tags.
6088 */
6089 static int
6090skip_status_match_char(xp, s)
6091 expand_T *xp;
6092 char_u *s;
6093{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006094 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006095#ifdef FEAT_MENU
6096 || ((xp->xp_context == EXPAND_MENUS
6097 || xp->xp_context == EXPAND_MENUNAMES)
6098 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6099#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006100 )
6101 {
6102#ifndef BACKSLASH_IN_FILENAME
6103 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6104 return 2;
6105#endif
6106 return 1;
6107 }
6108 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006109}
6110
6111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 * Show wildchar matches in the status line.
6113 * Show at least the "match" item.
6114 * We start at item 'first_match' in the list and show all matches that fit.
6115 *
6116 * If inversion is possible we use it. Else '=' characters are used.
6117 */
6118 void
6119win_redr_status_matches(xp, num_matches, matches, match, showtail)
6120 expand_T *xp;
6121 int num_matches;
6122 char_u **matches; /* list of matches */
6123 int match;
6124 int showtail;
6125{
6126#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6127 int row;
6128 char_u *buf;
6129 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006130 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int fillchar;
6132 int attr;
6133 int i;
6134 int highlight = TRUE;
6135 char_u *selstart = NULL;
6136 int selstart_col = 0;
6137 char_u *selend = NULL;
6138 static int first_match = 0;
6139 int add_left = FALSE;
6140 char_u *s;
6141#ifdef FEAT_MENU
6142 int emenu;
6143#endif
6144#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6145 int l;
6146#endif
6147
6148 if (matches == NULL) /* interrupted completion? */
6149 return;
6150
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006151#ifdef FEAT_MBYTE
6152 if (has_mbyte)
6153 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6154 else
6155#endif
6156 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (buf == NULL)
6158 return;
6159
6160 if (match == -1) /* don't show match but original text */
6161 {
6162 match = 0;
6163 highlight = FALSE;
6164 }
6165 /* count 1 for the ending ">" */
6166 clen = status_match_len(xp, L_MATCH(match)) + 3;
6167 if (match == 0)
6168 first_match = 0;
6169 else if (match < first_match)
6170 {
6171 /* jumping left, as far as we can go */
6172 first_match = match;
6173 add_left = TRUE;
6174 }
6175 else
6176 {
6177 /* check if match fits on the screen */
6178 for (i = first_match; i < match; ++i)
6179 clen += status_match_len(xp, L_MATCH(i)) + 2;
6180 if (first_match > 0)
6181 clen += 2;
6182 /* jumping right, put match at the left */
6183 if ((long)clen > Columns)
6184 {
6185 first_match = match;
6186 /* if showing the last match, we can add some on the left */
6187 clen = 2;
6188 for (i = match; i < num_matches; ++i)
6189 {
6190 clen += status_match_len(xp, L_MATCH(i)) + 2;
6191 if ((long)clen >= Columns)
6192 break;
6193 }
6194 if (i == num_matches)
6195 add_left = TRUE;
6196 }
6197 }
6198 if (add_left)
6199 while (first_match > 0)
6200 {
6201 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6202 if ((long)clen >= Columns)
6203 break;
6204 --first_match;
6205 }
6206
6207 fillchar = fillchar_status(&attr, TRUE);
6208
6209 if (first_match == 0)
6210 {
6211 *buf = NUL;
6212 len = 0;
6213 }
6214 else
6215 {
6216 STRCPY(buf, "< ");
6217 len = 2;
6218 }
6219 clen = len;
6220
6221 i = first_match;
6222 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6223 {
6224 if (i == match)
6225 {
6226 selstart = buf + len;
6227 selstart_col = clen;
6228 }
6229
6230 s = L_MATCH(i);
6231 /* Check for menu separators - replace with '|' */
6232#ifdef FEAT_MENU
6233 emenu = (xp->xp_context == EXPAND_MENUS
6234 || xp->xp_context == EXPAND_MENUNAMES);
6235 if (emenu && menu_is_separator(s))
6236 {
6237 STRCPY(buf + len, transchar('|'));
6238 l = (int)STRLEN(buf + len);
6239 len += l;
6240 clen += l;
6241 }
6242 else
6243#endif
6244 for ( ; *s != NUL; ++s)
6245 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006246 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 clen += ptr2cells(s);
6248#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006249 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 STRNCPY(buf + len, s, l);
6252 s += l - 1;
6253 len += l;
6254 }
6255 else
6256#endif
6257 {
6258 STRCPY(buf + len, transchar_byte(*s));
6259 len += (int)STRLEN(buf + len);
6260 }
6261 }
6262 if (i == match)
6263 selend = buf + len;
6264
6265 *(buf + len++) = ' ';
6266 *(buf + len++) = ' ';
6267 clen += 2;
6268 if (++i == num_matches)
6269 break;
6270 }
6271
6272 if (i != num_matches)
6273 {
6274 *(buf + len++) = '>';
6275 ++clen;
6276 }
6277
6278 buf[len] = NUL;
6279
6280 row = cmdline_row - 1;
6281 if (row >= 0)
6282 {
6283 if (wild_menu_showing == 0)
6284 {
6285 if (msg_scrolled > 0)
6286 {
6287 /* Put the wildmenu just above the command line. If there is
6288 * no room, scroll the screen one line up. */
6289 if (cmdline_row == Rows - 1)
6290 {
6291 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6292 ++msg_scrolled;
6293 }
6294 else
6295 {
6296 ++cmdline_row;
6297 ++row;
6298 }
6299 wild_menu_showing = WM_SCROLLED;
6300 }
6301 else
6302 {
6303 /* Create status line if needed by setting 'laststatus' to 2.
6304 * Set 'winminheight' to zero to avoid that the window is
6305 * resized. */
6306 if (lastwin->w_status_height == 0)
6307 {
6308 save_p_ls = p_ls;
6309 save_p_wmh = p_wmh;
6310 p_ls = 2;
6311 p_wmh = 0;
6312 last_status(FALSE);
6313 }
6314 wild_menu_showing = WM_SHOWN;
6315 }
6316 }
6317
6318 screen_puts(buf, row, 0, attr);
6319 if (selstart != NULL && highlight)
6320 {
6321 *selend = NUL;
6322 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6323 }
6324
6325 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6326 }
6327
6328#ifdef FEAT_VERTSPLIT
6329 win_redraw_last_status(topframe);
6330#else
6331 lastwin->w_redr_status = TRUE;
6332#endif
6333 vim_free(buf);
6334}
6335#endif
6336
6337#if defined(FEAT_WINDOWS) || defined(PROTO)
6338/*
6339 * Redraw the status line of window wp.
6340 *
6341 * If inversion is possible we use it. Else '=' characters are used.
6342 */
6343 void
6344win_redr_status(wp)
6345 win_T *wp;
6346{
6347 int row;
6348 char_u *p;
6349 int len;
6350 int fillchar;
6351 int attr;
6352 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006353 static int busy = FALSE;
6354
6355 /* It's possible to get here recursively when 'statusline' (indirectly)
6356 * invokes ":redrawstatus". Simply ignore the call then. */
6357 if (busy)
6358 return;
6359 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 wp->w_redr_status = FALSE;
6362 if (wp->w_status_height == 0)
6363 {
6364 /* no status line, can only be last window */
6365 redraw_cmdline = TRUE;
6366 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006367 else if (!redrawing()
6368#ifdef FEAT_INS_EXPAND
6369 /* don't update status line when popup menu is visible and may be
6370 * drawn over it */
6371 || pum_visible()
6372#endif
6373 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 /* Don't redraw right now, do it later. */
6376 wp->w_redr_status = TRUE;
6377 }
6378#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006379 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006382 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 }
6384#endif
6385 else
6386 {
6387 fillchar = fillchar_status(&attr, wp == curwin);
6388
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006389 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 p = NameBuff;
6391 len = (int)STRLEN(p);
6392
6393 if (wp->w_buffer->b_help
6394#ifdef FEAT_QUICKFIX
6395 || wp->w_p_pvw
6396#endif
6397 || bufIsChanged(wp->w_buffer)
6398 || wp->w_buffer->b_p_ro)
6399 *(p + len++) = ' ';
6400 if (wp->w_buffer->b_help)
6401 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006402 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 len += (int)STRLEN(p + len);
6404 }
6405#ifdef FEAT_QUICKFIX
6406 if (wp->w_p_pvw)
6407 {
6408 STRCPY(p + len, _("[Preview]"));
6409 len += (int)STRLEN(p + len);
6410 }
6411#endif
6412 if (bufIsChanged(wp->w_buffer))
6413 {
6414 STRCPY(p + len, "[+]");
6415 len += 3;
6416 }
6417 if (wp->w_buffer->b_p_ro)
6418 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006419 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 len += 4;
6421 }
6422
6423#ifndef FEAT_VERTSPLIT
6424 this_ru_col = ru_col;
6425 if (this_ru_col < (Columns + 1) / 2)
6426 this_ru_col = (Columns + 1) / 2;
6427#else
6428 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6429 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6430 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6431 if (this_ru_col <= 1)
6432 {
6433 p = (char_u *)"<"; /* No room for file name! */
6434 len = 1;
6435 }
6436 else
6437#endif
6438#ifdef FEAT_MBYTE
6439 if (has_mbyte)
6440 {
6441 int clen = 0, i;
6442
6443 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006444 clen = mb_string2cells(p, -1);
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 /* Find first character that will fit.
6447 * Going from start to end is much faster for DBCS. */
6448 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006449 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 clen -= (*mb_ptr2cells)(p + i);
6451 len = clen;
6452 if (i > 0)
6453 {
6454 p = p + i - 1;
6455 *p = '<';
6456 ++len;
6457 }
6458
6459 }
6460 else
6461#endif
6462 if (len > this_ru_col - 1)
6463 {
6464 p += len - (this_ru_col - 1);
6465 *p = '<';
6466 len = this_ru_col - 1;
6467 }
6468
6469 row = W_WINROW(wp) + wp->w_height;
6470 screen_puts(p, row, W_WINCOL(wp), attr);
6471 screen_fill(row, row + 1, len + W_WINCOL(wp),
6472 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6473
6474 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6475 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6476 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6477 - 1 + W_WINCOL(wp)), attr);
6478
6479#ifdef FEAT_CMDL_INFO
6480 win_redr_ruler(wp, TRUE);
6481#endif
6482 }
6483
6484#ifdef FEAT_VERTSPLIT
6485 /*
6486 * May need to draw the character below the vertical separator.
6487 */
6488 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6489 {
6490 if (stl_connected(wp))
6491 fillchar = fillchar_status(&attr, wp == curwin);
6492 else
6493 fillchar = fillchar_vsep(&attr);
6494 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6495 attr);
6496 }
6497#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006498 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
6500
Bram Moolenaar238a5642006-02-21 22:12:05 +00006501#ifdef FEAT_STL_OPT
6502/*
6503 * Redraw the status line according to 'statusline' and take care of any
6504 * errors encountered.
6505 */
6506 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006507redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006508 win_T *wp;
6509{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006510 static int entered = FALSE;
6511 int save_called_emsg = called_emsg;
6512
6513 /* When called recursively return. This can happen when the statusline
6514 * contains an expression that triggers a redraw. */
6515 if (entered)
6516 return;
6517 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006518
6519 called_emsg = FALSE;
6520 win_redr_custom(wp, FALSE);
6521 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006522 {
6523 /* When there is an error disable the statusline, otherwise the
6524 * display is messed up with errors and a redraw triggers the problem
6525 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006526 set_string_option_direct((char_u *)"statusline", -1,
6527 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006528 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006529 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006530 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006531 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006532}
6533#endif
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535# ifdef FEAT_VERTSPLIT
6536/*
6537 * Return TRUE if the status line of window "wp" is connected to the status
6538 * line of the window right of it. If not, then it's a vertical separator.
6539 * Only call if (wp->w_vsep_width != 0).
6540 */
6541 int
6542stl_connected(wp)
6543 win_T *wp;
6544{
6545 frame_T *fr;
6546
6547 fr = wp->w_frame;
6548 while (fr->fr_parent != NULL)
6549 {
6550 if (fr->fr_parent->fr_layout == FR_COL)
6551 {
6552 if (fr->fr_next != NULL)
6553 break;
6554 }
6555 else
6556 {
6557 if (fr->fr_next != NULL)
6558 return TRUE;
6559 }
6560 fr = fr->fr_parent;
6561 }
6562 return FALSE;
6563}
6564# endif
6565
6566#endif /* FEAT_WINDOWS */
6567
6568#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6569/*
6570 * Get the value to show for the language mappings, active 'keymap'.
6571 */
6572 int
6573get_keymap_str(wp, buf, len)
6574 win_T *wp;
6575 char_u *buf; /* buffer for the result */
6576 int len; /* length of buffer */
6577{
6578 char_u *p;
6579
6580 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6581 return FALSE;
6582
6583 {
6584#ifdef FEAT_EVAL
6585 buf_T *old_curbuf = curbuf;
6586 win_T *old_curwin = curwin;
6587 char_u *s;
6588
6589 curbuf = wp->w_buffer;
6590 curwin = wp;
6591 STRCPY(buf, "b:keymap_name"); /* must be writable */
6592 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006593 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 --emsg_skip;
6595 curbuf = old_curbuf;
6596 curwin = old_curwin;
6597 if (p == NULL || *p == NUL)
6598#endif
6599 {
6600#ifdef FEAT_KEYMAP
6601 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6602 p = wp->w_buffer->b_p_keymap;
6603 else
6604#endif
6605 p = (char_u *)"lang";
6606 }
6607 if ((int)(STRLEN(p) + 3) < len)
6608 sprintf((char *)buf, "<%s>", p);
6609 else
6610 buf[0] = NUL;
6611#ifdef FEAT_EVAL
6612 vim_free(s);
6613#endif
6614 }
6615 return buf[0] != NUL;
6616}
6617#endif
6618
6619#if defined(FEAT_STL_OPT) || defined(PROTO)
6620/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006621 * Redraw the status line or ruler of window "wp".
6622 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 */
6624 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006625win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006627 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006629 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int attr;
6631 int curattr;
6632 int row;
6633 int col = 0;
6634 int maxwidth;
6635 int width;
6636 int n;
6637 int len;
6638 int fillchar;
6639 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006640 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006642 struct stl_hlrec hltab[STL_MAX_ITEM];
6643 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006644 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006645 win_T *ewp;
6646 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
Bram Moolenaar1d633412013-12-11 15:52:01 +01006648 /* There is a tiny chance that this gets called recursively: When
6649 * redrawing a status line triggers redrawing the ruler or tabline.
6650 * Avoid trouble by not allowing recursion. */
6651 if (entered)
6652 return;
6653 entered = TRUE;
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006656 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006658 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006659 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006660 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006661 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006662 attr = hl_attr(HLF_TPF);
6663 maxwidth = Columns;
6664# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006665 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006668 else
6669 {
6670 row = W_WINROW(wp) + wp->w_height;
6671 fillchar = fillchar_status(&attr, wp == curwin);
6672 maxwidth = W_WIDTH(wp);
6673
6674 if (draw_ruler)
6675 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006676 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006677 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006678 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006679 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006680 if (*++stl == '-')
6681 stl++;
6682 if (atoi((char *)stl))
6683 while (VIM_ISDIGIT(*stl))
6684 stl++;
6685 if (*stl++ != '(')
6686 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006687 }
6688#ifdef FEAT_VERTSPLIT
6689 col = ru_col - (Columns - W_WIDTH(wp));
6690 if (col < (W_WIDTH(wp) + 1) / 2)
6691 col = (W_WIDTH(wp) + 1) / 2;
6692#else
6693 col = ru_col;
6694 if (col > (Columns + 1) / 2)
6695 col = (Columns + 1) / 2;
6696#endif
6697 maxwidth = W_WIDTH(wp) - col;
6698#ifdef FEAT_WINDOWS
6699 if (!wp->w_status_height)
6700#endif
6701 {
6702 row = Rows - 1;
6703 --maxwidth; /* writing in last column may cause scrolling */
6704 fillchar = ' ';
6705 attr = 0;
6706 }
6707
6708# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006709 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006710# endif
6711 }
6712 else
6713 {
6714 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006715 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006716 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006717 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006718# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006719 use_sandbox = was_set_insecurely((char_u *)"statusline",
6720 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006721# endif
6722 }
6723
6724#ifdef FEAT_VERTSPLIT
6725 col += W_WINCOL(wp);
6726#endif
6727 }
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006730 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
Bram Moolenaar61452852011-02-01 18:01:11 +01006732 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6733 * the cursor away and back. */
6734 ewp = wp == NULL ? curwin : wp;
6735 p_crb_save = ewp->w_p_crb;
6736 ewp->w_p_crb = FALSE;
6737
Bram Moolenaar362f3562009-11-03 16:20:34 +00006738 /* Make a copy, because the statusline may include a function call that
6739 * might change the option value and free the memory. */
6740 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006741 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006742 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006743 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006744 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006745 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006747 /* Make all characters printable. */
6748 p = transstr(buf);
6749 if (p != NULL)
6750 {
6751 vim_strncpy(buf, p, sizeof(buf) - 1);
6752 vim_free(p);
6753 }
6754
6755 /* fill up with "fillchar" */
6756 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006757 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759#ifdef FEAT_MBYTE
6760 len += (*mb_char2bytes)(fillchar, buf + len);
6761#else
6762 buf[len++] = fillchar;
6763#endif
6764 ++width;
6765 }
6766 buf[len] = NUL;
6767
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006768 /*
6769 * Draw each snippet with the specified highlighting.
6770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 curattr = attr;
6772 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006773 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006775 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 screen_puts_len(p, len, row, col, curattr);
6777 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006778 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006780 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006782 else if (hltab[n].userhl < 0)
6783 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006785 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006786 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787#endif
6788 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006789 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
6791 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006792
6793 if (wp == NULL)
6794 {
6795 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6796 col = 0;
6797 len = 0;
6798 p = buf;
6799 fillchar = 0;
6800 for (n = 0; tabtab[n].start != NULL; n++)
6801 {
6802 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6803 while (col < len)
6804 TabPageIdxs[col++] = fillchar;
6805 p = tabtab[n].start;
6806 fillchar = tabtab[n].userhl;
6807 }
6808 while (col < Columns)
6809 TabPageIdxs[col++] = fillchar;
6810 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006811
6812theend:
6813 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816#endif /* FEAT_STL_OPT */
6817
6818/*
6819 * Output a single character directly to the screen and update ScreenLines.
6820 */
6821 void
6822screen_putchar(c, row, col, attr)
6823 int c;
6824 int row, col;
6825 int attr;
6826{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 char_u buf[MB_MAXBYTES + 1];
6828
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006829#ifdef FEAT_MBYTE
6830 if (has_mbyte)
6831 buf[(*mb_char2bytes)(c, buf)] = NUL;
6832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006834 {
6835 buf[0] = c;
6836 buf[1] = NUL;
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 screen_puts(buf, row, col, attr);
6839}
6840
6841/*
6842 * Get a single character directly from ScreenLines into "bytes[]".
6843 * Also return its attribute in *attrp;
6844 */
6845 void
6846screen_getbytes(row, col, bytes, attrp)
6847 int row, col;
6848 char_u *bytes;
6849 int *attrp;
6850{
6851 unsigned off;
6852
6853 /* safety check */
6854 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6855 {
6856 off = LineOffset[row] + col;
6857 *attrp = ScreenAttrs[off];
6858 bytes[0] = ScreenLines[off];
6859 bytes[1] = NUL;
6860
6861#ifdef FEAT_MBYTE
6862 if (enc_utf8 && ScreenLinesUC[off] != 0)
6863 bytes[utfc_char2bytes(off, bytes)] = NUL;
6864 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6865 {
6866 bytes[0] = ScreenLines[off];
6867 bytes[1] = ScreenLines2[off];
6868 bytes[2] = NUL;
6869 }
6870 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6871 {
6872 bytes[1] = ScreenLines[off + 1];
6873 bytes[2] = NUL;
6874 }
6875#endif
6876 }
6877}
6878
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006879#ifdef FEAT_MBYTE
6880static int screen_comp_differs __ARGS((int, int*));
6881
6882/*
6883 * Return TRUE if composing characters for screen posn "off" differs from
6884 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006885 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006886 */
6887 static int
6888screen_comp_differs(off, u8cc)
6889 int off;
6890 int *u8cc;
6891{
6892 int i;
6893
6894 for (i = 0; i < Screen_mco; ++i)
6895 {
6896 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6897 return TRUE;
6898 if (u8cc[i] == 0)
6899 break;
6900 }
6901 return FALSE;
6902}
6903#endif
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905/*
6906 * Put string '*text' on the screen at position 'row' and 'col', with
6907 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6908 * Note: only outputs within one row, message is truncated at screen boundary!
6909 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6910 */
6911 void
6912screen_puts(text, row, col, attr)
6913 char_u *text;
6914 int row;
6915 int col;
6916 int attr;
6917{
6918 screen_puts_len(text, -1, row, col, attr);
6919}
6920
6921/*
6922 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6923 * a NUL.
6924 */
6925 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006926screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006928 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 int row;
6930 int col;
6931 int attr;
6932{
6933 unsigned off;
6934 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006935 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 int c;
6937#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006938 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 int mbyte_blen = 1;
6940 int mbyte_cells = 1;
6941 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006942 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 int clear_next_cell = FALSE;
6944# ifdef FEAT_ARABIC
6945 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006946 int pc, nc, nc1;
6947 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948# endif
6949#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006950#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6951 int force_redraw_this;
6952 int force_redraw_next = FALSE;
6953#endif
6954 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6957 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006958 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaarc236c162008-07-13 17:41:49 +00006960#ifdef FEAT_MBYTE
6961 /* When drawing over the right halve of a double-wide char clear out the
6962 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006963 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006964# ifdef FEAT_GUI
6965 && !gui.in_use
6966# endif
6967 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006968 {
6969 ScreenLines[off - 1] = ' ';
6970 ScreenAttrs[off - 1] = 0;
6971 if (enc_utf8)
6972 {
6973 ScreenLinesUC[off - 1] = 0;
6974 ScreenLinesC[0][off - 1] = 0;
6975 }
6976 /* redraw the previous cell, make it empty */
6977 screen_char(off - 1, row, col - 1);
6978 /* force the cell at "col" to be redrawn */
6979 force_redraw_next = TRUE;
6980 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006981#endif
6982
Bram Moolenaar367329b2007-08-30 11:53:22 +00006983#ifdef FEAT_MBYTE
6984 max_off = LineOffset[row] + screen_Columns;
6985#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006986 while (col < screen_Columns
6987 && (len < 0 || (int)(ptr - text) < len)
6988 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
6990 c = *ptr;
6991#ifdef FEAT_MBYTE
6992 /* check if this is the first byte of a multibyte */
6993 if (has_mbyte)
6994 {
6995 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006996 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006998 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7000 mbyte_cells = 1;
7001 else if (enc_dbcs != 0)
7002 mbyte_cells = mbyte_blen;
7003 else /* enc_utf8 */
7004 {
7005 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007006 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 (int)((text + len) - ptr));
7008 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007009 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007011# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 /* Non-BMP character: display as ? or fullwidth ?. */
7013 if (u8c >= 0x10000)
7014 {
7015 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7016 if (attr == 0)
7017 attr = hl_attr(HLF_8);
7018 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020# ifdef FEAT_ARABIC
7021 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7022 {
7023 /* Do Arabic shaping. */
7024 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7025 {
7026 /* Past end of string to be displayed. */
7027 nc = NUL;
7028 nc1 = NUL;
7029 }
7030 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007031 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007032 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7033 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007034 nc1 = pcc[0];
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 pc = prev_c;
7037 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007038 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040 else
7041 prev_c = u8c;
7042# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007043 if (col + mbyte_cells > screen_Columns)
7044 {
7045 /* Only 1 cell left, but character requires 2 cells:
7046 * display a '>' in the last column to avoid wrapping. */
7047 c = '>';
7048 mbyte_cells = 1;
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 }
7051 }
7052#endif
7053
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007054#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7055 force_redraw_this = force_redraw_next;
7056 force_redraw_next = FALSE;
7057#endif
7058
7059 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#ifdef FEAT_MBYTE
7061 || (mbyte_cells == 2
7062 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7063 || (enc_dbcs == DBCS_JPNU
7064 && c == 0x8e
7065 && ScreenLines2[off] != ptr[1])
7066 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007067 && (ScreenLinesUC[off] !=
7068 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7069 || (ScreenLinesUC[off] != 0
7070 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#endif
7072 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007073 || exmode_active;
7074
7075 if (need_redraw
7076#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7077 || force_redraw_this
7078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 )
7080 {
7081#if defined(FEAT_GUI) || defined(UNIX)
7082 /* The bold trick makes a single row of pixels appear in the next
7083 * character. When a bold character is removed, the next
7084 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007085 * and for some xterms. */
7086 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087# ifdef FEAT_GUI
7088 gui.in_use
7089# endif
7090# if defined(FEAT_GUI) && defined(UNIX)
7091 ||
7092# endif
7093# ifdef UNIX
7094 term_is_xterm
7095# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007096 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007098 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100 if (n > HL_ALL)
7101 n = syn_attr2attr(n);
7102 if (n & HL_BOLD)
7103 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 }
7105#endif
7106#ifdef FEAT_MBYTE
7107 /* When at the end of the text and overwriting a two-cell
7108 * character with a one-cell character, need to clear the next
7109 * cell. Also when overwriting the left halve of a two-cell char
7110 * with the right halve of a two-cell char. Do this only once
7111 * (mb_off2cells() may return 2 on the right halve). */
7112 if (clear_next_cell)
7113 clear_next_cell = FALSE;
7114 else if (has_mbyte
7115 && (len < 0 ? ptr[mbyte_blen] == NUL
7116 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007117 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007119 && (*mb_off2cells)(off, max_off) == 1
7120 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 clear_next_cell = TRUE;
7122
7123 /* Make sure we never leave a second byte of a double-byte behind,
7124 * it confuses mb_off2cells(). */
7125 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007126 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007128 && (*mb_off2cells)(off, max_off) == 1
7129 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 ScreenLines[off + mbyte_blen] = 0;
7131#endif
7132 ScreenLines[off] = c;
7133 ScreenAttrs[off] = attr;
7134#ifdef FEAT_MBYTE
7135 if (enc_utf8)
7136 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007137 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 ScreenLinesUC[off] = 0;
7139 else
7140 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007141 int i;
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007144 for (i = 0; i < Screen_mco; ++i)
7145 {
7146 ScreenLinesC[i][off] = u8cc[i];
7147 if (u8cc[i] == 0)
7148 break;
7149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 }
7151 if (mbyte_cells == 2)
7152 {
7153 ScreenLines[off + 1] = 0;
7154 ScreenAttrs[off + 1] = attr;
7155 }
7156 screen_char(off, row, col);
7157 }
7158 else if (mbyte_cells == 2)
7159 {
7160 ScreenLines[off + 1] = ptr[1];
7161 ScreenAttrs[off + 1] = attr;
7162 screen_char_2(off, row, col);
7163 }
7164 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7165 {
7166 ScreenLines2[off] = ptr[1];
7167 screen_char(off, row, col);
7168 }
7169 else
7170#endif
7171 screen_char(off, row, col);
7172 }
7173#ifdef FEAT_MBYTE
7174 if (has_mbyte)
7175 {
7176 off += mbyte_cells;
7177 col += mbyte_cells;
7178 ptr += mbyte_blen;
7179 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007180 {
7181 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007183 len = -1;
7184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 else
7187#endif
7188 {
7189 ++off;
7190 ++col;
7191 ++ptr;
7192 }
7193 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007194
7195#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7196 /* If we detected the next character needs to be redrawn, but the text
7197 * doesn't extend up to there, update the character here. */
7198 if (force_redraw_next && col < screen_Columns)
7199 {
7200# ifdef FEAT_MBYTE
7201 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7202 screen_char_2(off, row, col);
7203 else
7204# endif
7205 screen_char(off, row, col);
7206 }
7207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208}
7209
7210#ifdef FEAT_SEARCH_EXTRA
7211/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007212 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 */
7214 static void
7215start_search_hl()
7216{
7217 if (p_hls && !no_hlsearch)
7218 {
7219 last_pat_prog(&search_hl.rm);
7220 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007221# ifdef FEAT_RELTIME
7222 /* Set the time limit to 'redrawtime'. */
7223 profile_setlimit(p_rdt, &search_hl.tm);
7224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226}
7227
7228/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007229 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 */
7231 static void
7232end_search_hl()
7233{
7234 if (search_hl.rm.regprog != NULL)
7235 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007236 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 search_hl.rm.regprog = NULL;
7238 }
7239}
7240
7241/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007242 * Init for calling prepare_search_hl().
7243 */
7244 static void
7245init_search_hl(wp)
7246 win_T *wp;
7247{
7248 matchitem_T *cur;
7249
7250 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7251 * match */
7252 cur = wp->w_match_head;
7253 while (cur != NULL)
7254 {
7255 cur->hl.rm = cur->match;
7256 if (cur->hlg_id == 0)
7257 cur->hl.attr = 0;
7258 else
7259 cur->hl.attr = syn_id2attr(cur->hlg_id);
7260 cur->hl.buf = wp->w_buffer;
7261 cur->hl.lnum = 0;
7262 cur->hl.first_lnum = 0;
7263# ifdef FEAT_RELTIME
7264 /* Set the time limit to 'redrawtime'. */
7265 profile_setlimit(p_rdt, &(cur->hl.tm));
7266# endif
7267 cur = cur->next;
7268 }
7269 search_hl.buf = wp->w_buffer;
7270 search_hl.lnum = 0;
7271 search_hl.first_lnum = 0;
7272 /* time limit is set at the toplevel, for all windows */
7273}
7274
7275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 * Advance to the match in window "wp" line "lnum" or past it.
7277 */
7278 static void
7279prepare_search_hl(wp, lnum)
7280 win_T *wp;
7281 linenr_T lnum;
7282{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007283 matchitem_T *cur; /* points to the match list */
7284 match_T *shl; /* points to search_hl or a match */
7285 int shl_flag; /* flag to indicate whether search_hl
7286 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007287 int pos_inprogress; /* marks that position match search is
7288 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 int n;
7290
7291 /*
7292 * When using a multi-line pattern, start searching at the top
7293 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007294 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007296 cur = wp->w_match_head;
7297 shl_flag = FALSE;
7298 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007300 if (shl_flag == FALSE)
7301 {
7302 shl = &search_hl;
7303 shl_flag = TRUE;
7304 }
7305 else
7306 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (shl->rm.regprog != NULL
7308 && shl->lnum == 0
7309 && re_multiline(shl->rm.regprog))
7310 {
7311 if (shl->first_lnum == 0)
7312 {
7313# ifdef FEAT_FOLDING
7314 for (shl->first_lnum = lnum;
7315 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7316 if (hasFoldingWin(wp, shl->first_lnum - 1,
7317 NULL, NULL, TRUE, NULL))
7318 break;
7319# else
7320 shl->first_lnum = wp->w_topline;
7321# endif
7322 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007323 if (cur != NULL)
7324 cur->pos.cur = 0;
7325 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007327 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7328 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007330 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n, cur);
7331 pos_inprogress = cur == NULL || cur->pos.cur == 0
7332 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (shl->lnum != 0)
7334 {
7335 shl->first_lnum = shl->lnum
7336 + shl->rm.endpos[0].lnum
7337 - shl->rm.startpos[0].lnum;
7338 n = shl->rm.endpos[0].col;
7339 }
7340 else
7341 {
7342 ++shl->first_lnum;
7343 n = 0;
7344 }
7345 }
7346 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007347 if (shl != &search_hl && cur != NULL)
7348 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350}
7351
7352/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007353 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 * Uses shl->buf.
7355 * Sets shl->lnum and shl->rm contents.
7356 * Note: Assumes a previous match is always before "lnum", unless
7357 * shl->lnum is zero.
7358 * Careful: Any pointers for buffer lines will become invalid.
7359 */
7360 static void
Bram Moolenaarb3414592014-06-17 17:48:32 +02007361next_search_hl(win, shl, lnum, mincol, cur)
7362 win_T *win;
7363 match_T *shl; /* points to search_hl or a match */
7364 linenr_T lnum;
7365 colnr_T mincol; /* minimal column for a match */
7366 matchitem_T *cur; /* to retrieve match postions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368 linenr_T l;
7369 colnr_T matchcol;
7370 long nmatched;
7371
7372 if (shl->lnum != 0)
7373 {
7374 /* Check for three situations:
7375 * 1. If the "lnum" is below a previous match, start a new search.
7376 * 2. If the previous match includes "mincol", use it.
7377 * 3. Continue after the previous match.
7378 */
7379 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7380 if (lnum > l)
7381 shl->lnum = 0;
7382 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7383 return;
7384 }
7385
7386 /*
7387 * Repeat searching for a match until one is found that includes "mincol"
7388 * or none is found in this line.
7389 */
7390 called_emsg = FALSE;
7391 for (;;)
7392 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007393#ifdef FEAT_RELTIME
7394 /* Stop searching after passing the time limit. */
7395 if (profile_passed_limit(&(shl->tm)))
7396 {
7397 shl->lnum = 0; /* no match found in time */
7398 break;
7399 }
7400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 /* Three situations:
7402 * 1. No useful previous match: search from start of line.
7403 * 2. Not Vi compatible or empty match: continue at next character.
7404 * Break the loop if this is beyond the end of the line.
7405 * 3. Vi compatible searching: continue at end of previous match.
7406 */
7407 if (shl->lnum == 0)
7408 matchcol = 0;
7409 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7410 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007411 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007413 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007414
7415 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007416 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007417 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007419 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 shl->lnum = 0;
7421 break;
7422 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007423#ifdef FEAT_MBYTE
7424 if (has_mbyte)
7425 matchcol += mb_ptr2len(ml);
7426 else
7427#endif
7428 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 else
7431 matchcol = shl->rm.endpos[0].col;
7432
7433 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007434 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007436 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7437 matchcol,
7438#ifdef FEAT_RELTIME
7439 &(shl->tm)
7440#else
7441 NULL
7442#endif
7443 );
7444 if (called_emsg || got_int)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007445 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007446 /* Error while handling regexp: stop using this regexp. */
7447 if (shl == &search_hl)
7448 {
7449 /* don't free regprog in the match list, it's a copy */
7450 vim_regfree(shl->rm.regprog);
7451 SET_NO_HLSEARCH(TRUE);
7452 }
7453 shl->rm.regprog = NULL;
7454 shl->lnum = 0;
7455 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7456 message */
7457 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007458 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007459 }
7460 else if (cur != NULL)
7461 {
7462 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464 if (nmatched == 0)
7465 {
7466 shl->lnum = 0; /* no match found */
7467 break;
7468 }
7469 if (shl->rm.startpos[0].lnum > 0
7470 || shl->rm.startpos[0].col >= mincol
7471 || nmatched > 1
7472 || shl->rm.endpos[0].col > mincol)
7473 {
7474 shl->lnum += shl->rm.startpos[0].lnum;
7475 break; /* useful match found */
7476 }
7477 }
7478}
7479#endif
7480
Bram Moolenaarb3414592014-06-17 17:48:32 +02007481 static int
7482next_search_hl_pos(shl, lnum, posmatch, mincol)
7483 match_T *shl; /* points to a match */
7484 linenr_T lnum;
7485 posmatch_T *posmatch; /* match positions */
7486 colnr_T mincol; /* minimal column for a match */
7487{
7488 int i;
7489 int bot = -1;
7490
7491 shl->lnum = 0;
7492 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
7493 {
7494 if (posmatch->pos[i].lnum == 0)
7495 break;
7496 if (posmatch->pos[i].col < mincol)
7497 continue;
7498 if (posmatch->pos[i].lnum == lnum)
7499 {
7500 if (shl->lnum == lnum)
7501 {
7502 /* partially sort positions by column numbers
7503 * on the same line */
7504 if (posmatch->pos[i].col < posmatch->pos[bot].col)
7505 {
7506 llpos_T tmp = posmatch->pos[i];
7507
7508 posmatch->pos[i] = posmatch->pos[bot];
7509 posmatch->pos[bot] = tmp;
7510 }
7511 }
7512 else
7513 {
7514 bot = i;
7515 shl->lnum = lnum;
7516 }
7517 }
7518 }
7519 posmatch->cur = 0;
7520 if (shl->lnum == lnum)
7521 {
7522 colnr_T start = posmatch->pos[bot].col == 0
7523 ? 0 : posmatch->pos[bot].col - 1;
7524 colnr_T end = posmatch->pos[bot].col == 0
7525 ? MAXCOL : start + posmatch->pos[bot].len;
7526
7527 shl->rm.startpos[0].lnum = 0;
7528 shl->rm.startpos[0].col = start;
7529 shl->rm.endpos[0].lnum = 0;
7530 shl->rm.endpos[0].col = end;
7531 posmatch->cur = bot + 1;
7532 return TRUE;
7533 }
7534 return FALSE;
7535}
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 static void
7538screen_start_highlight(attr)
7539 int attr;
7540{
7541 attrentry_T *aep = NULL;
7542
7543 screen_attr = attr;
7544 if (full_screen
7545#ifdef WIN3264
7546 && termcap_active
7547#endif
7548 )
7549 {
7550#ifdef FEAT_GUI
7551 if (gui.in_use)
7552 {
7553 char buf[20];
7554
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007555 /* The GUI handles this internally. */
7556 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 OUT_STR(buf);
7558 }
7559 else
7560#endif
7561 {
7562 if (attr > HL_ALL) /* special HL attr. */
7563 {
7564 if (t_colors > 1)
7565 aep = syn_cterm_attr2entry(attr);
7566 else
7567 aep = syn_term_attr2entry(attr);
7568 if (aep == NULL) /* did ":syntax clear" */
7569 attr = 0;
7570 else
7571 attr = aep->ae_attr;
7572 }
7573 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7574 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007575 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7576 && cterm_normal_fg_bold)
7577 /* If the Normal FG color has BOLD attribute and the new HL
7578 * has a FG color defined, clear BOLD. */
7579 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7581 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007582 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7583 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 out_str(T_US);
7585 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7586 out_str(T_CZH);
7587 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7588 out_str(T_MR);
7589
7590 /*
7591 * Output the color or start string after bold etc., in case the
7592 * bold etc. override the color setting.
7593 */
7594 if (aep != NULL)
7595 {
7596 if (t_colors > 1)
7597 {
7598 if (aep->ae_u.cterm.fg_color)
7599 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7600 if (aep->ae_u.cterm.bg_color)
7601 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7602 }
7603 else
7604 {
7605 if (aep->ae_u.term.start != NULL)
7606 out_str(aep->ae_u.term.start);
7607 }
7608 }
7609 }
7610 }
7611}
7612
7613 void
7614screen_stop_highlight()
7615{
7616 int do_ME = FALSE; /* output T_ME code */
7617
7618 if (screen_attr != 0
7619#ifdef WIN3264
7620 && termcap_active
7621#endif
7622 )
7623 {
7624#ifdef FEAT_GUI
7625 if (gui.in_use)
7626 {
7627 char buf[20];
7628
7629 /* use internal GUI code */
7630 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7631 OUT_STR(buf);
7632 }
7633 else
7634#endif
7635 {
7636 if (screen_attr > HL_ALL) /* special HL attr. */
7637 {
7638 attrentry_T *aep;
7639
7640 if (t_colors > 1)
7641 {
7642 /*
7643 * Assume that t_me restores the original colors!
7644 */
7645 aep = syn_cterm_attr2entry(screen_attr);
7646 if (aep != NULL && (aep->ae_u.cterm.fg_color
7647 || aep->ae_u.cterm.bg_color))
7648 do_ME = TRUE;
7649 }
7650 else
7651 {
7652 aep = syn_term_attr2entry(screen_attr);
7653 if (aep != NULL && aep->ae_u.term.stop != NULL)
7654 {
7655 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7656 do_ME = TRUE;
7657 else
7658 out_str(aep->ae_u.term.stop);
7659 }
7660 }
7661 if (aep == NULL) /* did ":syntax clear" */
7662 screen_attr = 0;
7663 else
7664 screen_attr = aep->ae_attr;
7665 }
7666
7667 /*
7668 * Often all ending-codes are equal to T_ME. Avoid outputting the
7669 * same sequence several times.
7670 */
7671 if (screen_attr & HL_STANDOUT)
7672 {
7673 if (STRCMP(T_SE, T_ME) == 0)
7674 do_ME = TRUE;
7675 else
7676 out_str(T_SE);
7677 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007678 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {
7680 if (STRCMP(T_UE, T_ME) == 0)
7681 do_ME = TRUE;
7682 else
7683 out_str(T_UE);
7684 }
7685 if (screen_attr & HL_ITALIC)
7686 {
7687 if (STRCMP(T_CZR, T_ME) == 0)
7688 do_ME = TRUE;
7689 else
7690 out_str(T_CZR);
7691 }
7692 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7693 out_str(T_ME);
7694
7695 if (t_colors > 1)
7696 {
7697 /* set Normal cterm colors */
7698 if (cterm_normal_fg_color != 0)
7699 term_fg_color(cterm_normal_fg_color - 1);
7700 if (cterm_normal_bg_color != 0)
7701 term_bg_color(cterm_normal_bg_color - 1);
7702 if (cterm_normal_fg_bold)
7703 out_str(T_MD);
7704 }
7705 }
7706 }
7707 screen_attr = 0;
7708}
7709
7710/*
7711 * Reset the colors for a cterm. Used when leaving Vim.
7712 * The machine specific code may override this again.
7713 */
7714 void
7715reset_cterm_colors()
7716{
7717 if (t_colors > 1)
7718 {
7719 /* set Normal cterm colors */
7720 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7721 {
7722 out_str(T_OP);
7723 screen_attr = -1;
7724 }
7725 if (cterm_normal_fg_bold)
7726 {
7727 out_str(T_ME);
7728 screen_attr = -1;
7729 }
7730 }
7731}
7732
7733/*
7734 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7735 * using the attributes from ScreenAttrs["off"].
7736 */
7737 static void
7738screen_char(off, row, col)
7739 unsigned off;
7740 int row;
7741 int col;
7742{
7743 int attr;
7744
7745 /* Check for illegal values, just in case (could happen just after
7746 * resizing). */
7747 if (row >= screen_Rows || col >= screen_Columns)
7748 return;
7749
7750 /* Outputting the last character on the screen may scrollup the screen.
7751 * Don't to it! Mark the character invalid (update it when scrolled up) */
7752 if (row == screen_Rows - 1 && col == screen_Columns - 1
7753#ifdef FEAT_RIGHTLEFT
7754 /* account for first command-line character in rightleft mode */
7755 && !cmdmsg_rl
7756#endif
7757 )
7758 {
7759 ScreenAttrs[off] = (sattr_T)-1;
7760 return;
7761 }
7762
7763 /*
7764 * Stop highlighting first, so it's easier to move the cursor.
7765 */
7766#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7767 if (screen_char_attr != 0)
7768 attr = screen_char_attr;
7769 else
7770#endif
7771 attr = ScreenAttrs[off];
7772 if (screen_attr != attr)
7773 screen_stop_highlight();
7774
7775 windgoto(row, col);
7776
7777 if (screen_attr != attr)
7778 screen_start_highlight(attr);
7779
7780#ifdef FEAT_MBYTE
7781 if (enc_utf8 && ScreenLinesUC[off] != 0)
7782 {
7783 char_u buf[MB_MAXBYTES + 1];
7784
7785 /* Convert UTF-8 character to bytes and write it. */
7786
7787 buf[utfc_char2bytes(off, buf)] = NUL;
7788
7789 out_str(buf);
7790 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7791 ++screen_cur_col;
7792 }
7793 else
7794#endif
7795 {
7796#ifdef FEAT_MBYTE
7797 out_flush_check();
7798#endif
7799 out_char(ScreenLines[off]);
7800#ifdef FEAT_MBYTE
7801 /* double-byte character in single-width cell */
7802 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7803 out_char(ScreenLines2[off]);
7804#endif
7805 }
7806
7807 screen_cur_col++;
7808}
7809
7810#ifdef FEAT_MBYTE
7811
7812/*
7813 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7814 * on the screen at position 'row' and 'col'.
7815 * The attributes of the first byte is used for all. This is required to
7816 * output the two bytes of a double-byte character with nothing in between.
7817 */
7818 static void
7819screen_char_2(off, row, col)
7820 unsigned off;
7821 int row;
7822 int col;
7823{
7824 /* Check for illegal values (could be wrong when screen was resized). */
7825 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7826 return;
7827
7828 /* Outputting the last character on the screen may scrollup the screen.
7829 * Don't to it! Mark the character invalid (update it when scrolled up) */
7830 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7831 {
7832 ScreenAttrs[off] = (sattr_T)-1;
7833 return;
7834 }
7835
7836 /* Output the first byte normally (positions the cursor), then write the
7837 * second byte directly. */
7838 screen_char(off, row, col);
7839 out_char(ScreenLines[off + 1]);
7840 ++screen_cur_col;
7841}
7842#endif
7843
7844#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7845/*
7846 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7847 * This uses the contents of ScreenLines[] and doesn't change it.
7848 */
7849 void
7850screen_draw_rectangle(row, col, height, width, invert)
7851 int row;
7852 int col;
7853 int height;
7854 int width;
7855 int invert;
7856{
7857 int r, c;
7858 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007859#ifdef FEAT_MBYTE
7860 int max_off;
7861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007863 /* Can't use ScreenLines unless initialized */
7864 if (ScreenLines == NULL)
7865 return;
7866
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (invert)
7868 screen_char_attr = HL_INVERSE;
7869 for (r = row; r < row + height; ++r)
7870 {
7871 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007872#ifdef FEAT_MBYTE
7873 max_off = off + screen_Columns;
7874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 for (c = col; c < col + width; ++c)
7876 {
7877#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007878 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
7880 screen_char_2(off + c, r, c);
7881 ++c;
7882 }
7883 else
7884#endif
7885 {
7886 screen_char(off + c, r, c);
7887#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007888 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 ++c;
7890#endif
7891 }
7892 }
7893 }
7894 screen_char_attr = 0;
7895}
7896#endif
7897
7898#ifdef FEAT_VERTSPLIT
7899/*
7900 * Redraw the characters for a vertically split window.
7901 */
7902 static void
7903redraw_block(row, end, wp)
7904 int row;
7905 int end;
7906 win_T *wp;
7907{
7908 int col;
7909 int width;
7910
7911# ifdef FEAT_CLIPBOARD
7912 clip_may_clear_selection(row, end - 1);
7913# endif
7914
7915 if (wp == NULL)
7916 {
7917 col = 0;
7918 width = Columns;
7919 }
7920 else
7921 {
7922 col = wp->w_wincol;
7923 width = wp->w_width;
7924 }
7925 screen_draw_rectangle(row, col, end - row, width, FALSE);
7926}
7927#endif
7928
7929/*
7930 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7931 * with character 'c1' in first column followed by 'c2' in the other columns.
7932 * Use attributes 'attr'.
7933 */
7934 void
7935screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7936 int start_row, end_row;
7937 int start_col, end_col;
7938 int c1, c2;
7939 int attr;
7940{
7941 int row;
7942 int col;
7943 int off;
7944 int end_off;
7945 int did_delete;
7946 int c;
7947 int norm_term;
7948#if defined(FEAT_GUI) || defined(UNIX)
7949 int force_next = FALSE;
7950#endif
7951
7952 if (end_row > screen_Rows) /* safety check */
7953 end_row = screen_Rows;
7954 if (end_col > screen_Columns) /* safety check */
7955 end_col = screen_Columns;
7956 if (ScreenLines == NULL
7957 || start_row >= end_row
7958 || start_col >= end_col) /* nothing to do */
7959 return;
7960
7961 /* it's a "normal" terminal when not in a GUI or cterm */
7962 norm_term = (
7963#ifdef FEAT_GUI
7964 !gui.in_use &&
7965#endif
7966 t_colors <= 1);
7967 for (row = start_row; row < end_row; ++row)
7968 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007969#ifdef FEAT_MBYTE
7970 if (has_mbyte
7971# ifdef FEAT_GUI
7972 && !gui.in_use
7973# endif
7974 )
7975 {
7976 /* When drawing over the right halve of a double-wide char clear
7977 * out the left halve. When drawing over the left halve of a
7978 * double wide-char clear out the right halve. Only needed in a
7979 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007980 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007981 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007982 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007983 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007984 }
7985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 /*
7987 * Try to use delete-line termcap code, when no attributes or in a
7988 * "normal" terminal, where a bold/italic space is just a
7989 * space.
7990 */
7991 did_delete = FALSE;
7992 if (c2 == ' '
7993 && end_col == Columns
7994 && can_clear(T_CE)
7995 && (attr == 0
7996 || (norm_term
7997 && attr <= HL_ALL
7998 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7999 {
8000 /*
8001 * check if we really need to clear something
8002 */
8003 col = start_col;
8004 if (c1 != ' ') /* don't clear first char */
8005 ++col;
8006
8007 off = LineOffset[row] + col;
8008 end_off = LineOffset[row] + end_col;
8009
8010 /* skip blanks (used often, keep it fast!) */
8011#ifdef FEAT_MBYTE
8012 if (enc_utf8)
8013 while (off < end_off && ScreenLines[off] == ' '
8014 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8015 ++off;
8016 else
8017#endif
8018 while (off < end_off && ScreenLines[off] == ' '
8019 && ScreenAttrs[off] == 0)
8020 ++off;
8021 if (off < end_off) /* something to be cleared */
8022 {
8023 col = off - LineOffset[row];
8024 screen_stop_highlight();
8025 term_windgoto(row, col);/* clear rest of this screen line */
8026 out_str(T_CE);
8027 screen_start(); /* don't know where cursor is now */
8028 col = end_col - col;
8029 while (col--) /* clear chars in ScreenLines */
8030 {
8031 ScreenLines[off] = ' ';
8032#ifdef FEAT_MBYTE
8033 if (enc_utf8)
8034 ScreenLinesUC[off] = 0;
8035#endif
8036 ScreenAttrs[off] = 0;
8037 ++off;
8038 }
8039 }
8040 did_delete = TRUE; /* the chars are cleared now */
8041 }
8042
8043 off = LineOffset[row] + start_col;
8044 c = c1;
8045 for (col = start_col; col < end_col; ++col)
8046 {
8047 if (ScreenLines[off] != c
8048#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008049 || (enc_utf8 && (int)ScreenLinesUC[off]
8050 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051#endif
8052 || ScreenAttrs[off] != attr
8053#if defined(FEAT_GUI) || defined(UNIX)
8054 || force_next
8055#endif
8056 )
8057 {
8058#if defined(FEAT_GUI) || defined(UNIX)
8059 /* The bold trick may make a single row of pixels appear in
8060 * the next character. When a bold character is removed, the
8061 * next character should be redrawn too. This happens for our
8062 * own GUI and for some xterms. */
8063 if (
8064# ifdef FEAT_GUI
8065 gui.in_use
8066# endif
8067# if defined(FEAT_GUI) && defined(UNIX)
8068 ||
8069# endif
8070# ifdef UNIX
8071 term_is_xterm
8072# endif
8073 )
8074 {
8075 if (ScreenLines[off] != ' '
8076 && (ScreenAttrs[off] > HL_ALL
8077 || ScreenAttrs[off] & HL_BOLD))
8078 force_next = TRUE;
8079 else
8080 force_next = FALSE;
8081 }
8082#endif
8083 ScreenLines[off] = c;
8084#ifdef FEAT_MBYTE
8085 if (enc_utf8)
8086 {
8087 if (c >= 0x80)
8088 {
8089 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008090 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
8093 ScreenLinesUC[off] = 0;
8094 }
8095#endif
8096 ScreenAttrs[off] = attr;
8097 if (!did_delete || c != ' ')
8098 screen_char(off, row, col);
8099 }
8100 ++off;
8101 if (col == start_col)
8102 {
8103 if (did_delete)
8104 break;
8105 c = c2;
8106 }
8107 }
8108 if (end_col == Columns)
8109 LineWraps[row] = FALSE;
8110 if (row == Rows - 1) /* overwritten the command line */
8111 {
8112 redraw_cmdline = TRUE;
8113 if (c1 == ' ' && c2 == ' ')
8114 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008115 if (start_col == 0)
8116 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 }
8118 }
8119}
8120
8121/*
8122 * Check if there should be a delay. Used before clearing or redrawing the
8123 * screen or the command line.
8124 */
8125 void
8126check_for_delay(check_msg_scroll)
8127 int check_msg_scroll;
8128{
8129 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8130 && !did_wait_return
8131 && emsg_silent == 0)
8132 {
8133 out_flush();
8134 ui_delay(1000L, TRUE);
8135 emsg_on_display = FALSE;
8136 if (check_msg_scroll)
8137 msg_scroll = FALSE;
8138 }
8139}
8140
8141/*
8142 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008143 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 * Returns TRUE if there is a valid screen to write to.
8145 * Returns FALSE when starting up and screen not initialized yet.
8146 */
8147 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008148screen_valid(doclear)
8149 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008151 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 return (ScreenLines != NULL);
8153}
8154
8155/*
8156 * Resize the shell to Rows and Columns.
8157 * Allocate ScreenLines[] and associated items.
8158 *
8159 * There may be some time between setting Rows and Columns and (re)allocating
8160 * ScreenLines[]. This happens when starting up and when (manually) changing
8161 * the shell size. Always use screen_Rows and screen_Columns to access items
8162 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8163 * final size of the shell is needed.
8164 */
8165 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008166screenalloc(doclear)
8167 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 int new_row, old_row;
8170#ifdef FEAT_GUI
8171 int old_Rows;
8172#endif
8173 win_T *wp;
8174 int outofmem = FALSE;
8175 int len;
8176 schar_T *new_ScreenLines;
8177#ifdef FEAT_MBYTE
8178 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008179 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008181 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182#endif
8183 sattr_T *new_ScreenAttrs;
8184 unsigned *new_LineOffset;
8185 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008186#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008187 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008188 tabpage_T *tp;
8189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008191 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008192#ifdef FEAT_AUTOCMD
8193 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008195retry:
8196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 /*
8198 * Allocation of the screen buffers is done only when the size changes and
8199 * when Rows and Columns have been set and we have started doing full
8200 * screen stuff.
8201 */
8202 if ((ScreenLines != NULL
8203 && Rows == screen_Rows
8204 && Columns == screen_Columns
8205#ifdef FEAT_MBYTE
8206 && enc_utf8 == (ScreenLinesUC != NULL)
8207 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008208 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209#endif
8210 )
8211 || Rows == 0
8212 || Columns == 0
8213 || (!full_screen && ScreenLines == NULL))
8214 return;
8215
8216 /*
8217 * It's possible that we produce an out-of-memory message below, which
8218 * will cause this function to be called again. To break the loop, just
8219 * return here.
8220 */
8221 if (entered)
8222 return;
8223 entered = TRUE;
8224
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008225 /*
8226 * Note that the window sizes are updated before reallocating the arrays,
8227 * thus we must not redraw here!
8228 */
8229 ++RedrawingDisabled;
8230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 win_new_shellsize(); /* fit the windows in the new sized shell */
8232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 comp_col(); /* recompute columns for shown command and ruler */
8234
8235 /*
8236 * We're changing the size of the screen.
8237 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8238 * - Move lines from the old arrays into the new arrays, clear extra
8239 * lines (unless the screen is going to be cleared).
8240 * - Free the old arrays.
8241 *
8242 * If anything fails, make ScreenLines NULL, so we don't do anything!
8243 * Continuing with the old ScreenLines may result in a crash, because the
8244 * size is wrong.
8245 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008246 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008248#ifdef FEAT_AUTOCMD
8249 if (aucmd_win != NULL)
8250 win_free_lsize(aucmd_win);
8251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252
8253 new_ScreenLines = (schar_T *)lalloc((long_u)(
8254 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8255#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008256 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (enc_utf8)
8258 {
8259 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8260 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008261 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008262 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8264 }
8265 if (enc_dbcs == DBCS_JPNU)
8266 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8267 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8268#endif
8269 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8270 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8271 new_LineOffset = (unsigned *)lalloc((long_u)(
8272 Rows * sizeof(unsigned)), FALSE);
8273 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008274#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008275 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008278 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
8280 if (win_alloc_lines(wp) == FAIL)
8281 {
8282 outofmem = TRUE;
8283#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008284 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285#endif
8286 }
8287 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008288#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008289 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8290 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008291 outofmem = TRUE;
8292#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008293#ifdef FEAT_WINDOWS
8294give_up:
8295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008297#ifdef FEAT_MBYTE
8298 for (i = 0; i < p_mco; ++i)
8299 if (new_ScreenLinesC[i] == NULL)
8300 break;
8301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (new_ScreenLines == NULL
8303#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008304 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8306#endif
8307 || new_ScreenAttrs == NULL
8308 || new_LineOffset == NULL
8309 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008310#ifdef FEAT_WINDOWS
8311 || new_TabPageIdxs == NULL
8312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 || outofmem)
8314 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008315 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008316 {
8317 /* guess the size */
8318 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8319
8320 /* Remember we did this to avoid getting outofmem messages over
8321 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008322 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 vim_free(new_ScreenLines);
8325 new_ScreenLines = NULL;
8326#ifdef FEAT_MBYTE
8327 vim_free(new_ScreenLinesUC);
8328 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008329 for (i = 0; i < p_mco; ++i)
8330 {
8331 vim_free(new_ScreenLinesC[i]);
8332 new_ScreenLinesC[i] = NULL;
8333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 vim_free(new_ScreenLines2);
8335 new_ScreenLines2 = NULL;
8336#endif
8337 vim_free(new_ScreenAttrs);
8338 new_ScreenAttrs = NULL;
8339 vim_free(new_LineOffset);
8340 new_LineOffset = NULL;
8341 vim_free(new_LineWraps);
8342 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008343#ifdef FEAT_WINDOWS
8344 vim_free(new_TabPageIdxs);
8345 new_TabPageIdxs = NULL;
8346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 else
8349 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008350 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008351
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 for (new_row = 0; new_row < Rows; ++new_row)
8353 {
8354 new_LineOffset[new_row] = new_row * Columns;
8355 new_LineWraps[new_row] = FALSE;
8356
8357 /*
8358 * If the screen is not going to be cleared, copy as much as
8359 * possible from the old screen to the new one and clear the rest
8360 * (used when resizing the window at the "--more--" prompt or when
8361 * executing an external command, for the GUI).
8362 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008363 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
8365 (void)vim_memset(new_ScreenLines + new_row * Columns,
8366 ' ', (size_t)Columns * sizeof(schar_T));
8367#ifdef FEAT_MBYTE
8368 if (enc_utf8)
8369 {
8370 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8371 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008372 for (i = 0; i < p_mco; ++i)
8373 (void)vim_memset(new_ScreenLinesC[i]
8374 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 0, (size_t)Columns * sizeof(u8char_T));
8376 }
8377 if (enc_dbcs == DBCS_JPNU)
8378 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8379 0, (size_t)Columns * sizeof(schar_T));
8380#endif
8381 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8382 0, (size_t)Columns * sizeof(sattr_T));
8383 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008384 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 if (screen_Columns < Columns)
8387 len = screen_Columns;
8388 else
8389 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008390#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008391 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008392 * may be invalid now. Also when p_mco changes. */
8393 if (!(enc_utf8 && ScreenLinesUC == NULL)
8394 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008395#endif
8396 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8397 ScreenLines + LineOffset[old_row],
8398 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008400 if (enc_utf8 && ScreenLinesUC != NULL
8401 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {
8403 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8404 ScreenLinesUC + LineOffset[old_row],
8405 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008406 for (i = 0; i < p_mco; ++i)
8407 mch_memmove(new_ScreenLinesC[i]
8408 + new_LineOffset[new_row],
8409 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 (size_t)len * sizeof(u8char_T));
8411 }
8412 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8413 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8414 ScreenLines2 + LineOffset[old_row],
8415 (size_t)len * sizeof(schar_T));
8416#endif
8417 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8418 ScreenAttrs + LineOffset[old_row],
8419 (size_t)len * sizeof(sattr_T));
8420 }
8421 }
8422 }
8423 /* Use the last line of the screen for the current line. */
8424 current_ScreenLine = new_ScreenLines + Rows * Columns;
8425 }
8426
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008427 free_screenlines();
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 ScreenLines = new_ScreenLines;
8430#ifdef FEAT_MBYTE
8431 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008432 for (i = 0; i < p_mco; ++i)
8433 ScreenLinesC[i] = new_ScreenLinesC[i];
8434 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 ScreenLines2 = new_ScreenLines2;
8436#endif
8437 ScreenAttrs = new_ScreenAttrs;
8438 LineOffset = new_LineOffset;
8439 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008440#ifdef FEAT_WINDOWS
8441 TabPageIdxs = new_TabPageIdxs;
8442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
8444 /* It's important that screen_Rows and screen_Columns reflect the actual
8445 * size of ScreenLines[]. Set them before calling anything. */
8446#ifdef FEAT_GUI
8447 old_Rows = screen_Rows;
8448#endif
8449 screen_Rows = Rows;
8450 screen_Columns = Columns;
8451
8452 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008453 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 screenclear2();
8455
8456#ifdef FEAT_GUI
8457 else if (gui.in_use
8458 && !gui.starting
8459 && ScreenLines != NULL
8460 && old_Rows != Rows)
8461 {
8462 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8463 /*
8464 * Adjust the position of the cursor, for when executing an external
8465 * command.
8466 */
8467 if (msg_row >= Rows) /* Rows got smaller */
8468 msg_row = Rows - 1; /* put cursor at last row */
8469 else if (Rows > old_Rows) /* Rows got bigger */
8470 msg_row += Rows - old_Rows; /* put cursor in same place */
8471 if (msg_col >= Columns) /* Columns got smaller */
8472 msg_col = Columns - 1; /* put cursor at last column */
8473 }
8474#endif
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008477 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008478
8479#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008480 /*
8481 * Do not apply autocommands more than 3 times to avoid an endless loop
8482 * in case applying autocommands always changes Rows or Columns.
8483 */
8484 if (starting == 0 && ++retry_count <= 3)
8485 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008486 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008487 /* In rare cases, autocommands may have altered Rows or Columns,
8488 * jump back to check if we need to allocate the screen again. */
8489 goto retry;
8490 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008495free_screenlines()
8496{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008497#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008498 int i;
8499
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008500 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008501 for (i = 0; i < Screen_mco; ++i)
8502 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008503 vim_free(ScreenLines2);
8504#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008505 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008506 vim_free(ScreenAttrs);
8507 vim_free(LineOffset);
8508 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008509#ifdef FEAT_WINDOWS
8510 vim_free(TabPageIdxs);
8511#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008512}
8513
8514 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515screenclear()
8516{
8517 check_for_delay(FALSE);
8518 screenalloc(FALSE); /* allocate screen buffers if size changed */
8519 screenclear2(); /* clear the screen */
8520}
8521
8522 static void
8523screenclear2()
8524{
8525 int i;
8526
8527 if (starting == NO_SCREEN || ScreenLines == NULL
8528#ifdef FEAT_GUI
8529 || (gui.in_use && gui.starting)
8530#endif
8531 )
8532 return;
8533
8534#ifdef FEAT_GUI
8535 if (!gui.in_use)
8536#endif
8537 screen_attr = -1; /* force setting the Normal colors */
8538 screen_stop_highlight(); /* don't want highlighting here */
8539
8540#ifdef FEAT_CLIPBOARD
8541 /* disable selection without redrawing it */
8542 clip_scroll_selection(9999);
8543#endif
8544
8545 /* blank out ScreenLines */
8546 for (i = 0; i < Rows; ++i)
8547 {
8548 lineclear(LineOffset[i], (int)Columns);
8549 LineWraps[i] = FALSE;
8550 }
8551
8552 if (can_clear(T_CL))
8553 {
8554 out_str(T_CL); /* clear the display */
8555 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008556 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
8558 else
8559 {
8560 /* can't clear the screen, mark all chars with invalid attributes */
8561 for (i = 0; i < Rows; ++i)
8562 lineinvalid(LineOffset[i], (int)Columns);
8563 clear_cmdline = TRUE;
8564 }
8565
8566 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8567
8568 win_rest_invalid(firstwin);
8569 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008570#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008571 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (must_redraw == CLEAR) /* no need to clear again */
8574 must_redraw = NOT_VALID;
8575 compute_cmdrow();
8576 msg_row = cmdline_row; /* put cursor on last line for messages */
8577 msg_col = 0;
8578 screen_start(); /* don't know where cursor is now */
8579 msg_scrolled = 0; /* can't scroll back */
8580 msg_didany = FALSE;
8581 msg_didout = FALSE;
8582}
8583
8584/*
8585 * Clear one line in ScreenLines.
8586 */
8587 static void
8588lineclear(off, width)
8589 unsigned off;
8590 int width;
8591{
8592 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8593#ifdef FEAT_MBYTE
8594 if (enc_utf8)
8595 (void)vim_memset(ScreenLinesUC + off, 0,
8596 (size_t)width * sizeof(u8char_T));
8597#endif
8598 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8599}
8600
8601/*
8602 * Mark one line in ScreenLines invalid by setting the attributes to an
8603 * invalid value.
8604 */
8605 static void
8606lineinvalid(off, width)
8607 unsigned off;
8608 int width;
8609{
8610 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8611}
8612
8613#ifdef FEAT_VERTSPLIT
8614/*
8615 * Copy part of a Screenline for vertically split window "wp".
8616 */
8617 static void
8618linecopy(to, from, wp)
8619 int to;
8620 int from;
8621 win_T *wp;
8622{
8623 unsigned off_to = LineOffset[to] + wp->w_wincol;
8624 unsigned off_from = LineOffset[from] + wp->w_wincol;
8625
8626 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8627 wp->w_width * sizeof(schar_T));
8628# ifdef FEAT_MBYTE
8629 if (enc_utf8)
8630 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008631 int i;
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8634 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008635 for (i = 0; i < p_mco; ++i)
8636 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8637 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639 if (enc_dbcs == DBCS_JPNU)
8640 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8641 wp->w_width * sizeof(schar_T));
8642# endif
8643 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8644 wp->w_width * sizeof(sattr_T));
8645}
8646#endif
8647
8648/*
8649 * Return TRUE if clearing with term string "p" would work.
8650 * It can't work when the string is empty or it won't set the right background.
8651 */
8652 int
8653can_clear(p)
8654 char_u *p;
8655{
8656 return (*p != NUL && (t_colors <= 1
8657#ifdef FEAT_GUI
8658 || gui.in_use
8659#endif
8660 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8661}
8662
8663/*
8664 * Reset cursor position. Use whenever cursor was moved because of outputting
8665 * something directly to the screen (shell commands) or a terminal control
8666 * code.
8667 */
8668 void
8669screen_start()
8670{
8671 screen_cur_row = screen_cur_col = 9999;
8672}
8673
8674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 * Move the cursor to position "row","col" in the screen.
8676 * This tries to find the most efficient way to move, minimizing the number of
8677 * characters sent to the terminal.
8678 */
8679 void
8680windgoto(row, col)
8681 int row;
8682 int col;
8683{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008684 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 int i;
8686 int plan;
8687 int cost;
8688 int wouldbe_col;
8689 int noinvcurs;
8690 char_u *bs;
8691 int goto_cost;
8692 int attr;
8693
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008694#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8696
8697#define PLAN_LE 1
8698#define PLAN_CR 2
8699#define PLAN_NL 3
8700#define PLAN_WRITE 4
8701 /* Can't use ScreenLines unless initialized */
8702 if (ScreenLines == NULL)
8703 return;
8704
8705 if (col != screen_cur_col || row != screen_cur_row)
8706 {
8707 /* Check for valid position. */
8708 if (row < 0) /* window without text lines? */
8709 row = 0;
8710 if (row >= screen_Rows)
8711 row = screen_Rows - 1;
8712 if (col >= screen_Columns)
8713 col = screen_Columns - 1;
8714
8715 /* check if no cursor movement is allowed in highlight mode */
8716 if (screen_attr && *T_MS == NUL)
8717 noinvcurs = HIGHL_COST;
8718 else
8719 noinvcurs = 0;
8720 goto_cost = GOTO_COST + noinvcurs;
8721
8722 /*
8723 * Plan how to do the positioning:
8724 * 1. Use CR to move it to column 0, same row.
8725 * 2. Use T_LE to move it a few columns to the left.
8726 * 3. Use NL to move a few lines down, column 0.
8727 * 4. Move a few columns to the right with T_ND or by writing chars.
8728 *
8729 * Don't do this if the cursor went beyond the last column, the cursor
8730 * position is unknown then (some terminals wrap, some don't )
8731 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008732 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 * characters to move the cursor to the right.
8734 */
8735 if (row >= screen_cur_row && screen_cur_col < Columns)
8736 {
8737 /*
8738 * If the cursor is in the same row, bigger col, we can use CR
8739 * or T_LE.
8740 */
8741 bs = NULL; /* init for GCC */
8742 attr = screen_attr;
8743 if (row == screen_cur_row && col < screen_cur_col)
8744 {
8745 /* "le" is preferred over "bc", because "bc" is obsolete */
8746 if (*T_LE)
8747 bs = T_LE; /* "cursor left" */
8748 else
8749 bs = T_BC; /* "backspace character (old) */
8750 if (*bs)
8751 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8752 else
8753 cost = 999;
8754 if (col + 1 < cost) /* using CR is less characters */
8755 {
8756 plan = PLAN_CR;
8757 wouldbe_col = 0;
8758 cost = 1; /* CR is just one character */
8759 }
8760 else
8761 {
8762 plan = PLAN_LE;
8763 wouldbe_col = col;
8764 }
8765 if (noinvcurs) /* will stop highlighting */
8766 {
8767 cost += noinvcurs;
8768 attr = 0;
8769 }
8770 }
8771
8772 /*
8773 * If the cursor is above where we want to be, we can use CR LF.
8774 */
8775 else if (row > screen_cur_row)
8776 {
8777 plan = PLAN_NL;
8778 wouldbe_col = 0;
8779 cost = (row - screen_cur_row) * 2; /* CR LF */
8780 if (noinvcurs) /* will stop highlighting */
8781 {
8782 cost += noinvcurs;
8783 attr = 0;
8784 }
8785 }
8786
8787 /*
8788 * If the cursor is in the same row, smaller col, just use write.
8789 */
8790 else
8791 {
8792 plan = PLAN_WRITE;
8793 wouldbe_col = screen_cur_col;
8794 cost = 0;
8795 }
8796
8797 /*
8798 * Check if any characters that need to be written have the
8799 * correct attributes. Also avoid UTF-8 characters.
8800 */
8801 i = col - wouldbe_col;
8802 if (i > 0)
8803 cost += i;
8804 if (cost < goto_cost && i > 0)
8805 {
8806 /*
8807 * Check if the attributes are correct without additionally
8808 * stopping highlighting.
8809 */
8810 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8811 while (i && *p++ == attr)
8812 --i;
8813 if (i != 0)
8814 {
8815 /*
8816 * Try if it works when highlighting is stopped here.
8817 */
8818 if (*--p == 0)
8819 {
8820 cost += noinvcurs;
8821 while (i && *p++ == 0)
8822 --i;
8823 }
8824 if (i != 0)
8825 cost = 999; /* different attributes, don't do it */
8826 }
8827#ifdef FEAT_MBYTE
8828 if (enc_utf8)
8829 {
8830 /* Don't use an UTF-8 char for positioning, it's slow. */
8831 for (i = wouldbe_col; i < col; ++i)
8832 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8833 {
8834 cost = 999;
8835 break;
8836 }
8837 }
8838#endif
8839 }
8840
8841 /*
8842 * We can do it without term_windgoto()!
8843 */
8844 if (cost < goto_cost)
8845 {
8846 if (plan == PLAN_LE)
8847 {
8848 if (noinvcurs)
8849 screen_stop_highlight();
8850 while (screen_cur_col > col)
8851 {
8852 out_str(bs);
8853 --screen_cur_col;
8854 }
8855 }
8856 else if (plan == PLAN_CR)
8857 {
8858 if (noinvcurs)
8859 screen_stop_highlight();
8860 out_char('\r');
8861 screen_cur_col = 0;
8862 }
8863 else if (plan == PLAN_NL)
8864 {
8865 if (noinvcurs)
8866 screen_stop_highlight();
8867 while (screen_cur_row < row)
8868 {
8869 out_char('\n');
8870 ++screen_cur_row;
8871 }
8872 screen_cur_col = 0;
8873 }
8874
8875 i = col - screen_cur_col;
8876 if (i > 0)
8877 {
8878 /*
8879 * Use cursor-right if it's one character only. Avoids
8880 * removing a line of pixels from the last bold char, when
8881 * using the bold trick in the GUI.
8882 */
8883 if (T_ND[0] != NUL && T_ND[1] == NUL)
8884 {
8885 while (i-- > 0)
8886 out_char(*T_ND);
8887 }
8888 else
8889 {
8890 int off;
8891
8892 off = LineOffset[row] + screen_cur_col;
8893 while (i-- > 0)
8894 {
8895 if (ScreenAttrs[off] != screen_attr)
8896 screen_stop_highlight();
8897#ifdef FEAT_MBYTE
8898 out_flush_check();
8899#endif
8900 out_char(ScreenLines[off]);
8901#ifdef FEAT_MBYTE
8902 if (enc_dbcs == DBCS_JPNU
8903 && ScreenLines[off] == 0x8e)
8904 out_char(ScreenLines2[off]);
8905#endif
8906 ++off;
8907 }
8908 }
8909 }
8910 }
8911 }
8912 else
8913 cost = 999;
8914
8915 if (cost >= goto_cost)
8916 {
8917 if (noinvcurs)
8918 screen_stop_highlight();
8919 if (row == screen_cur_row && (col > screen_cur_col) &&
8920 *T_CRI != NUL)
8921 term_cursor_right(col - screen_cur_col);
8922 else
8923 term_windgoto(row, col);
8924 }
8925 screen_cur_row = row;
8926 screen_cur_col = col;
8927 }
8928}
8929
8930/*
8931 * Set cursor to its position in the current window.
8932 */
8933 void
8934setcursor()
8935{
8936 if (redrawing())
8937 {
8938 validate_cursor();
8939 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8940 W_WINCOL(curwin) + (
8941#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008942 /* With 'rightleft' set and the cursor on a double-wide
8943 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8945# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008946 (has_mbyte
8947 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8948 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949# endif
8950 1)) :
8951#endif
8952 curwin->w_wcol));
8953 }
8954}
8955
8956
8957/*
8958 * insert 'line_count' lines at 'row' in window 'wp'
8959 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8960 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8961 * scrolling.
8962 * Returns FAIL if the lines are not inserted, OK for success.
8963 */
8964 int
8965win_ins_lines(wp, row, line_count, invalid, mayclear)
8966 win_T *wp;
8967 int row;
8968 int line_count;
8969 int invalid;
8970 int mayclear;
8971{
8972 int did_delete;
8973 int nextrow;
8974 int lastrow;
8975 int retval;
8976
8977 if (invalid)
8978 wp->w_lines_valid = 0;
8979
8980 if (wp->w_height < 5)
8981 return FAIL;
8982
8983 if (line_count > wp->w_height - row)
8984 line_count = wp->w_height - row;
8985
8986 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8987 if (retval != MAYBE)
8988 return retval;
8989
8990 /*
8991 * If there is a next window or a status line, we first try to delete the
8992 * lines at the bottom to avoid messing what is after the window.
8993 * If this fails and there are following windows, don't do anything to avoid
8994 * messing up those windows, better just redraw.
8995 */
8996 did_delete = FALSE;
8997#ifdef FEAT_WINDOWS
8998 if (wp->w_next != NULL || wp->w_status_height)
8999 {
9000 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9001 line_count, (int)Rows, FALSE, NULL) == OK)
9002 did_delete = TRUE;
9003 else if (wp->w_next)
9004 return FAIL;
9005 }
9006#endif
9007 /*
9008 * if no lines deleted, blank the lines that will end up below the window
9009 */
9010 if (!did_delete)
9011 {
9012#ifdef FEAT_WINDOWS
9013 wp->w_redr_status = TRUE;
9014#endif
9015 redraw_cmdline = TRUE;
9016 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
9017 lastrow = nextrow + line_count;
9018 if (lastrow > Rows)
9019 lastrow = Rows;
9020 screen_fill(nextrow - line_count, lastrow - line_count,
9021 W_WINCOL(wp), (int)W_ENDCOL(wp),
9022 ' ', ' ', 0);
9023 }
9024
9025 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
9026 == FAIL)
9027 {
9028 /* deletion will have messed up other windows */
9029 if (did_delete)
9030 {
9031#ifdef FEAT_WINDOWS
9032 wp->w_redr_status = TRUE;
9033#endif
9034 win_rest_invalid(W_NEXT(wp));
9035 }
9036 return FAIL;
9037 }
9038
9039 return OK;
9040}
9041
9042/*
9043 * delete "line_count" window lines at "row" in window "wp"
9044 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9045 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9046 * scrolling
9047 * Return OK for success, FAIL if the lines are not deleted.
9048 */
9049 int
9050win_del_lines(wp, row, line_count, invalid, mayclear)
9051 win_T *wp;
9052 int row;
9053 int line_count;
9054 int invalid;
9055 int mayclear;
9056{
9057 int retval;
9058
9059 if (invalid)
9060 wp->w_lines_valid = 0;
9061
9062 if (line_count > wp->w_height - row)
9063 line_count = wp->w_height - row;
9064
9065 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9066 if (retval != MAYBE)
9067 return retval;
9068
9069 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9070 (int)Rows, FALSE, NULL) == FAIL)
9071 return FAIL;
9072
9073#ifdef FEAT_WINDOWS
9074 /*
9075 * If there are windows or status lines below, try to put them at the
9076 * correct place. If we can't do that, they have to be redrawn.
9077 */
9078 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9079 {
9080 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9081 line_count, (int)Rows, NULL) == FAIL)
9082 {
9083 wp->w_redr_status = TRUE;
9084 win_rest_invalid(wp->w_next);
9085 }
9086 }
9087 /*
9088 * If this is the last window and there is no status line, redraw the
9089 * command line later.
9090 */
9091 else
9092#endif
9093 redraw_cmdline = TRUE;
9094 return OK;
9095}
9096
9097/*
9098 * Common code for win_ins_lines() and win_del_lines().
9099 * Returns OK or FAIL when the work has been done.
9100 * Returns MAYBE when not finished yet.
9101 */
9102 static int
9103win_do_lines(wp, row, line_count, mayclear, del)
9104 win_T *wp;
9105 int row;
9106 int line_count;
9107 int mayclear;
9108 int del;
9109{
9110 int retval;
9111
9112 if (!redrawing() || line_count <= 0)
9113 return FAIL;
9114
9115 /* only a few lines left: redraw is faster */
9116 if (mayclear && Rows - line_count < 5
9117#ifdef FEAT_VERTSPLIT
9118 && wp->w_width == Columns
9119#endif
9120 )
9121 {
9122 screenclear(); /* will set wp->w_lines_valid to 0 */
9123 return FAIL;
9124 }
9125
9126 /*
9127 * Delete all remaining lines
9128 */
9129 if (row + line_count >= wp->w_height)
9130 {
9131 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9132 W_WINCOL(wp), (int)W_ENDCOL(wp),
9133 ' ', ' ', 0);
9134 return OK;
9135 }
9136
9137 /*
9138 * when scrolling, the message on the command line should be cleared,
9139 * otherwise it will stay there forever.
9140 */
9141 clear_cmdline = TRUE;
9142
9143 /*
9144 * If the terminal can set a scroll region, use that.
9145 * Always do this in a vertically split window. This will redraw from
9146 * ScreenLines[] when t_CV isn't defined. That's faster than using
9147 * win_line().
9148 * Don't use a scroll region when we are going to redraw the text, writing
9149 * a character in the lower right corner of the scroll region causes a
9150 * scroll-up in the DJGPP version.
9151 */
9152 if (scroll_region
9153#ifdef FEAT_VERTSPLIT
9154 || W_WIDTH(wp) != Columns
9155#endif
9156 )
9157 {
9158#ifdef FEAT_VERTSPLIT
9159 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9160#endif
9161 scroll_region_set(wp, row);
9162 if (del)
9163 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9164 wp->w_height - row, FALSE, wp);
9165 else
9166 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9167 wp->w_height - row, wp);
9168#ifdef FEAT_VERTSPLIT
9169 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9170#endif
9171 scroll_region_reset();
9172 return retval;
9173 }
9174
9175#ifdef FEAT_WINDOWS
9176 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9177 return FAIL;
9178#endif
9179
9180 return MAYBE;
9181}
9182
9183/*
9184 * window 'wp' and everything after it is messed up, mark it for redraw
9185 */
9186 static void
9187win_rest_invalid(wp)
9188 win_T *wp;
9189{
9190#ifdef FEAT_WINDOWS
9191 while (wp != NULL)
9192#else
9193 if (wp != NULL)
9194#endif
9195 {
9196 redraw_win_later(wp, NOT_VALID);
9197#ifdef FEAT_WINDOWS
9198 wp->w_redr_status = TRUE;
9199 wp = wp->w_next;
9200#endif
9201 }
9202 redraw_cmdline = TRUE;
9203}
9204
9205/*
9206 * The rest of the routines in this file perform screen manipulations. The
9207 * given operation is performed physically on the screen. The corresponding
9208 * change is also made to the internal screen image. In this way, the editor
9209 * anticipates the effect of editing changes on the appearance of the screen.
9210 * That way, when we call screenupdate a complete redraw isn't usually
9211 * necessary. Another advantage is that we can keep adding code to anticipate
9212 * screen changes, and in the meantime, everything still works.
9213 */
9214
9215/*
9216 * types for inserting or deleting lines
9217 */
9218#define USE_T_CAL 1
9219#define USE_T_CDL 2
9220#define USE_T_AL 3
9221#define USE_T_CE 4
9222#define USE_T_DL 5
9223#define USE_T_SR 6
9224#define USE_NL 7
9225#define USE_T_CD 8
9226#define USE_REDRAW 9
9227
9228/*
9229 * insert lines on the screen and update ScreenLines[]
9230 * 'end' is the line after the scrolled part. Normally it is Rows.
9231 * When scrolling region used 'off' is the offset from the top for the region.
9232 * 'row' and 'end' are relative to the start of the region.
9233 *
9234 * return FAIL for failure, OK for success.
9235 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009236 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237screen_ins_lines(off, row, line_count, end, wp)
9238 int off;
9239 int row;
9240 int line_count;
9241 int end;
9242 win_T *wp; /* NULL or window to use width from */
9243{
9244 int i;
9245 int j;
9246 unsigned temp;
9247 int cursor_row;
9248 int type;
9249 int result_empty;
9250 int can_ce = can_clear(T_CE);
9251
9252 /*
9253 * FAIL if
9254 * - there is no valid screen
9255 * - the screen has to be redrawn completely
9256 * - the line count is less than one
9257 * - the line count is more than 'ttyscroll'
9258 */
9259 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9260 return FAIL;
9261
9262 /*
9263 * There are seven ways to insert lines:
9264 * 0. When in a vertically split window and t_CV isn't set, redraw the
9265 * characters from ScreenLines[].
9266 * 1. Use T_CD (clear to end of display) if it exists and the result of
9267 * the insert is just empty lines
9268 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9269 * present or line_count > 1. It looks better if we do all the inserts
9270 * at once.
9271 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9272 * insert is just empty lines and T_CE is not present or line_count >
9273 * 1.
9274 * 4. Use T_AL (insert line) if it exists.
9275 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9276 * just empty lines.
9277 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9278 * just empty lines.
9279 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9280 * the 'da' flag is not set or we have clear line capability.
9281 * 8. redraw the characters from ScreenLines[].
9282 *
9283 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9284 * the scrollbar for the window. It does have insert line, use that if it
9285 * exists.
9286 */
9287 result_empty = (row + line_count >= end);
9288#ifdef FEAT_VERTSPLIT
9289 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9290 type = USE_REDRAW;
9291 else
9292#endif
9293 if (can_clear(T_CD) && result_empty)
9294 type = USE_T_CD;
9295 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9296 type = USE_T_CAL;
9297 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9298 type = USE_T_CDL;
9299 else if (*T_AL != NUL)
9300 type = USE_T_AL;
9301 else if (can_ce && result_empty)
9302 type = USE_T_CE;
9303 else if (*T_DL != NUL && result_empty)
9304 type = USE_T_DL;
9305 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9306 type = USE_T_SR;
9307 else
9308 return FAIL;
9309
9310 /*
9311 * For clearing the lines screen_del_lines() is used. This will also take
9312 * care of t_db if necessary.
9313 */
9314 if (type == USE_T_CD || type == USE_T_CDL ||
9315 type == USE_T_CE || type == USE_T_DL)
9316 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9317
9318 /*
9319 * If text is retained below the screen, first clear or delete as many
9320 * lines at the bottom of the window as are about to be inserted so that
9321 * the deleted lines won't later surface during a screen_del_lines.
9322 */
9323 if (*T_DB)
9324 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9325
9326#ifdef FEAT_CLIPBOARD
9327 /* Remove a modeless selection when inserting lines halfway the screen
9328 * or not the full width of the screen. */
9329 if (off + row > 0
9330# ifdef FEAT_VERTSPLIT
9331 || (wp != NULL && wp->w_width != Columns)
9332# endif
9333 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009334 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
9336 clip_scroll_selection(-line_count);
9337#endif
9338
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339#ifdef FEAT_GUI
9340 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9341 * scrolling is actually carried out. */
9342 gui_dont_update_cursor();
9343#endif
9344
9345 if (*T_CCS != NUL) /* cursor relative to region */
9346 cursor_row = row;
9347 else
9348 cursor_row = row + off;
9349
9350 /*
9351 * Shift LineOffset[] line_count down to reflect the inserted lines.
9352 * Clear the inserted lines in ScreenLines[].
9353 */
9354 row += off;
9355 end += off;
9356 for (i = 0; i < line_count; ++i)
9357 {
9358#ifdef FEAT_VERTSPLIT
9359 if (wp != NULL && wp->w_width != Columns)
9360 {
9361 /* need to copy part of a line */
9362 j = end - 1 - i;
9363 while ((j -= line_count) >= row)
9364 linecopy(j + line_count, j, wp);
9365 j += line_count;
9366 if (can_clear((char_u *)" "))
9367 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9368 else
9369 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9370 LineWraps[j] = FALSE;
9371 }
9372 else
9373#endif
9374 {
9375 j = end - 1 - i;
9376 temp = LineOffset[j];
9377 while ((j -= line_count) >= row)
9378 {
9379 LineOffset[j + line_count] = LineOffset[j];
9380 LineWraps[j + line_count] = LineWraps[j];
9381 }
9382 LineOffset[j + line_count] = temp;
9383 LineWraps[j + line_count] = FALSE;
9384 if (can_clear((char_u *)" "))
9385 lineclear(temp, (int)Columns);
9386 else
9387 lineinvalid(temp, (int)Columns);
9388 }
9389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390
9391 screen_stop_highlight();
9392 windgoto(cursor_row, 0);
9393
9394#ifdef FEAT_VERTSPLIT
9395 /* redraw the characters */
9396 if (type == USE_REDRAW)
9397 redraw_block(row, end, wp);
9398 else
9399#endif
9400 if (type == USE_T_CAL)
9401 {
9402 term_append_lines(line_count);
9403 screen_start(); /* don't know where cursor is now */
9404 }
9405 else
9406 {
9407 for (i = 0; i < line_count; i++)
9408 {
9409 if (type == USE_T_AL)
9410 {
9411 if (i && cursor_row != 0)
9412 windgoto(cursor_row, 0);
9413 out_str(T_AL);
9414 }
9415 else /* type == USE_T_SR */
9416 out_str(T_SR);
9417 screen_start(); /* don't know where cursor is now */
9418 }
9419 }
9420
9421 /*
9422 * With scroll-reverse and 'da' flag set we need to clear the lines that
9423 * have been scrolled down into the region.
9424 */
9425 if (type == USE_T_SR && *T_DA)
9426 {
9427 for (i = 0; i < line_count; ++i)
9428 {
9429 windgoto(off + i, 0);
9430 out_str(T_CE);
9431 screen_start(); /* don't know where cursor is now */
9432 }
9433 }
9434
9435#ifdef FEAT_GUI
9436 gui_can_update_cursor();
9437 if (gui.in_use)
9438 out_flush(); /* always flush after a scroll */
9439#endif
9440 return OK;
9441}
9442
9443/*
9444 * delete lines on the screen and update ScreenLines[]
9445 * 'end' is the line after the scrolled part. Normally it is Rows.
9446 * When scrolling region used 'off' is the offset from the top for the region.
9447 * 'row' and 'end' are relative to the start of the region.
9448 *
9449 * Return OK for success, FAIL if the lines are not deleted.
9450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 int
9452screen_del_lines(off, row, line_count, end, force, wp)
9453 int off;
9454 int row;
9455 int line_count;
9456 int end;
9457 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009458 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 int j;
9461 int i;
9462 unsigned temp;
9463 int cursor_row;
9464 int cursor_end;
9465 int result_empty; /* result is empty until end of region */
9466 int can_delete; /* deleting line codes can be used */
9467 int type;
9468
9469 /*
9470 * FAIL if
9471 * - there is no valid screen
9472 * - the screen has to be redrawn completely
9473 * - the line count is less than one
9474 * - the line count is more than 'ttyscroll'
9475 */
9476 if (!screen_valid(TRUE) || line_count <= 0 ||
9477 (!force && line_count > p_ttyscroll))
9478 return FAIL;
9479
9480 /*
9481 * Check if the rest of the current region will become empty.
9482 */
9483 result_empty = row + line_count >= end;
9484
9485 /*
9486 * We can delete lines only when 'db' flag not set or when 'ce' option
9487 * available.
9488 */
9489 can_delete = (*T_DB == NUL || can_clear(T_CE));
9490
9491 /*
9492 * There are six ways to delete lines:
9493 * 0. When in a vertically split window and t_CV isn't set, redraw the
9494 * characters from ScreenLines[].
9495 * 1. Use T_CD if it exists and the result is empty.
9496 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9497 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9498 * none of the other ways work.
9499 * 4. Use T_CE (erase line) if the result is empty.
9500 * 5. Use T_DL (delete line) if it exists.
9501 * 6. redraw the characters from ScreenLines[].
9502 */
9503#ifdef FEAT_VERTSPLIT
9504 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9505 type = USE_REDRAW;
9506 else
9507#endif
9508 if (can_clear(T_CD) && result_empty)
9509 type = USE_T_CD;
9510#if defined(__BEOS__) && defined(BEOS_DR8)
9511 /*
9512 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9513 * its internal termcap... this works okay for tests which test *T_DB !=
9514 * NUL. It has the disadvantage that the user cannot use any :set t_*
9515 * command to get T_DB (back) to empty_option, only :set term=... will do
9516 * the trick...
9517 * Anyway, this hack will hopefully go away with the next OS release.
9518 * (Olaf Seibert)
9519 */
9520 else if (row == 0 && T_DB == empty_option
9521 && (line_count == 1 || *T_CDL == NUL))
9522#else
9523 else if (row == 0 && (
9524#ifndef AMIGA
9525 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9526 * up, so use delete-line command */
9527 line_count == 1 ||
9528#endif
9529 *T_CDL == NUL))
9530#endif
9531 type = USE_NL;
9532 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9533 type = USE_T_CDL;
9534 else if (can_clear(T_CE) && result_empty
9535#ifdef FEAT_VERTSPLIT
9536 && (wp == NULL || wp->w_width == Columns)
9537#endif
9538 )
9539 type = USE_T_CE;
9540 else if (*T_DL != NUL && can_delete)
9541 type = USE_T_DL;
9542 else if (*T_CDL != NUL && can_delete)
9543 type = USE_T_CDL;
9544 else
9545 return FAIL;
9546
9547#ifdef FEAT_CLIPBOARD
9548 /* Remove a modeless selection when deleting lines halfway the screen or
9549 * not the full width of the screen. */
9550 if (off + row > 0
9551# ifdef FEAT_VERTSPLIT
9552 || (wp != NULL && wp->w_width != Columns)
9553# endif
9554 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009555 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
9557 clip_scroll_selection(line_count);
9558#endif
9559
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560#ifdef FEAT_GUI
9561 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9562 * scrolling is actually carried out. */
9563 gui_dont_update_cursor();
9564#endif
9565
9566 if (*T_CCS != NUL) /* cursor relative to region */
9567 {
9568 cursor_row = row;
9569 cursor_end = end;
9570 }
9571 else
9572 {
9573 cursor_row = row + off;
9574 cursor_end = end + off;
9575 }
9576
9577 /*
9578 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9579 * Clear the inserted lines in ScreenLines[].
9580 */
9581 row += off;
9582 end += off;
9583 for (i = 0; i < line_count; ++i)
9584 {
9585#ifdef FEAT_VERTSPLIT
9586 if (wp != NULL && wp->w_width != Columns)
9587 {
9588 /* need to copy part of a line */
9589 j = row + i;
9590 while ((j += line_count) <= end - 1)
9591 linecopy(j - line_count, j, wp);
9592 j -= line_count;
9593 if (can_clear((char_u *)" "))
9594 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9595 else
9596 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9597 LineWraps[j] = FALSE;
9598 }
9599 else
9600#endif
9601 {
9602 /* whole width, moving the line pointers is faster */
9603 j = row + i;
9604 temp = LineOffset[j];
9605 while ((j += line_count) <= end - 1)
9606 {
9607 LineOffset[j - line_count] = LineOffset[j];
9608 LineWraps[j - line_count] = LineWraps[j];
9609 }
9610 LineOffset[j - line_count] = temp;
9611 LineWraps[j - line_count] = FALSE;
9612 if (can_clear((char_u *)" "))
9613 lineclear(temp, (int)Columns);
9614 else
9615 lineinvalid(temp, (int)Columns);
9616 }
9617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 screen_stop_highlight();
9620
9621#ifdef FEAT_VERTSPLIT
9622 /* redraw the characters */
9623 if (type == USE_REDRAW)
9624 redraw_block(row, end, wp);
9625 else
9626#endif
9627 if (type == USE_T_CD) /* delete the lines */
9628 {
9629 windgoto(cursor_row, 0);
9630 out_str(T_CD);
9631 screen_start(); /* don't know where cursor is now */
9632 }
9633 else if (type == USE_T_CDL)
9634 {
9635 windgoto(cursor_row, 0);
9636 term_delete_lines(line_count);
9637 screen_start(); /* don't know where cursor is now */
9638 }
9639 /*
9640 * Deleting lines at top of the screen or scroll region: Just scroll
9641 * the whole screen (scroll region) up by outputting newlines on the
9642 * last line.
9643 */
9644 else if (type == USE_NL)
9645 {
9646 windgoto(cursor_end - 1, 0);
9647 for (i = line_count; --i >= 0; )
9648 out_char('\n'); /* cursor will remain on same line */
9649 }
9650 else
9651 {
9652 for (i = line_count; --i >= 0; )
9653 {
9654 if (type == USE_T_DL)
9655 {
9656 windgoto(cursor_row, 0);
9657 out_str(T_DL); /* delete a line */
9658 }
9659 else /* type == USE_T_CE */
9660 {
9661 windgoto(cursor_row + i, 0);
9662 out_str(T_CE); /* erase a line */
9663 }
9664 screen_start(); /* don't know where cursor is now */
9665 }
9666 }
9667
9668 /*
9669 * If the 'db' flag is set, we need to clear the lines that have been
9670 * scrolled up at the bottom of the region.
9671 */
9672 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9673 {
9674 for (i = line_count; i > 0; --i)
9675 {
9676 windgoto(cursor_end - i, 0);
9677 out_str(T_CE); /* erase a line */
9678 screen_start(); /* don't know where cursor is now */
9679 }
9680 }
9681
9682#ifdef FEAT_GUI
9683 gui_can_update_cursor();
9684 if (gui.in_use)
9685 out_flush(); /* always flush after a scroll */
9686#endif
9687
9688 return OK;
9689}
9690
9691/*
9692 * show the current mode and ruler
9693 *
9694 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9695 * If clear_cmdline is FALSE there may be a message there that needs to be
9696 * cleared only if a mode is shown.
9697 * Return the length of the message (0 if no message).
9698 */
9699 int
9700showmode()
9701{
9702 int need_clear;
9703 int length = 0;
9704 int do_mode;
9705 int attr;
9706 int nwr_save;
9707#ifdef FEAT_INS_EXPAND
9708 int sub_attr;
9709#endif
9710
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009711 do_mode = ((p_smd && msg_silent == 0)
9712 && ((State & INSERT)
9713 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009714 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 if (do_mode || Recording)
9716 {
9717 /*
9718 * Don't show mode right now, when not redrawing or inside a mapping.
9719 * Call char_avail() only when we are going to show something, because
9720 * it takes a bit of time.
9721 */
9722 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9723 {
9724 redraw_cmdline = TRUE; /* show mode later */
9725 return 0;
9726 }
9727
9728 nwr_save = need_wait_return;
9729
9730 /* wait a bit before overwriting an important message */
9731 check_for_delay(FALSE);
9732
9733 /* if the cmdline is more than one line high, erase top lines */
9734 need_clear = clear_cmdline;
9735 if (clear_cmdline && cmdline_row < Rows - 1)
9736 msg_clr_cmdline(); /* will reset clear_cmdline */
9737
9738 /* Position on the last line in the window, column 0 */
9739 msg_pos_mode();
9740 cursor_off();
9741 attr = hl_attr(HLF_CM); /* Highlight mode */
9742 if (do_mode)
9743 {
9744 MSG_PUTS_ATTR("--", attr);
9745#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009746 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009747# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009748 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009749# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009750 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009751# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009752 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009753# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 MSG_PUTS_ATTR(" IM", attr);
9755# else
9756 MSG_PUTS_ATTR(" XIM", attr);
9757# endif
9758#endif
9759#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9760 if (gui.in_use)
9761 {
9762 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009763 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765#endif
9766#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009767 /* CTRL-X in Insert mode */
9768 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
9770 /* These messages can get long, avoid a wrap in a narrow
9771 * window. Prefer showing edit_submode_extra. */
9772 length = (Rows - msg_row) * Columns - 3;
9773 if (edit_submode_extra != NULL)
9774 length -= vim_strsize(edit_submode_extra);
9775 if (length > 0)
9776 {
9777 if (edit_submode_pre != NULL)
9778 length -= vim_strsize(edit_submode_pre);
9779 if (length - vim_strsize(edit_submode) > 0)
9780 {
9781 if (edit_submode_pre != NULL)
9782 msg_puts_attr(edit_submode_pre, attr);
9783 msg_puts_attr(edit_submode, attr);
9784 }
9785 if (edit_submode_extra != NULL)
9786 {
9787 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9788 if ((int)edit_submode_highl < (int)HLF_COUNT)
9789 sub_attr = hl_attr(edit_submode_highl);
9790 else
9791 sub_attr = attr;
9792 msg_puts_attr(edit_submode_extra, sub_attr);
9793 }
9794 }
9795 length = 0;
9796 }
9797 else
9798#endif
9799 {
9800#ifdef FEAT_VREPLACE
9801 if (State & VREPLACE_FLAG)
9802 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9803 else
9804#endif
9805 if (State & REPLACE_FLAG)
9806 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9807 else if (State & INSERT)
9808 {
9809#ifdef FEAT_RIGHTLEFT
9810 if (p_ri)
9811 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9812#endif
9813 MSG_PUTS_ATTR(_(" INSERT"), attr);
9814 }
9815 else if (restart_edit == 'I')
9816 MSG_PUTS_ATTR(_(" (insert)"), attr);
9817 else if (restart_edit == 'R')
9818 MSG_PUTS_ATTR(_(" (replace)"), attr);
9819 else if (restart_edit == 'V')
9820 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9821#ifdef FEAT_RIGHTLEFT
9822 if (p_hkmap)
9823 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9824# ifdef FEAT_FKMAP
9825 if (p_fkmap)
9826 MSG_PUTS_ATTR(farsi_text_5, attr);
9827# endif
9828#endif
9829#ifdef FEAT_KEYMAP
9830 if (State & LANGMAP)
9831 {
9832# ifdef FEAT_ARABIC
9833 if (curwin->w_p_arab)
9834 MSG_PUTS_ATTR(_(" Arabic"), attr);
9835 else
9836# endif
9837 MSG_PUTS_ATTR(_(" (lang)"), attr);
9838 }
9839#endif
9840 if ((State & INSERT) && p_paste)
9841 MSG_PUTS_ATTR(_(" (paste)"), attr);
9842
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 if (VIsual_active)
9844 {
9845 char *p;
9846
9847 /* Don't concatenate separate words to avoid translation
9848 * problems. */
9849 switch ((VIsual_select ? 4 : 0)
9850 + (VIsual_mode == Ctrl_V) * 2
9851 + (VIsual_mode == 'V'))
9852 {
9853 case 0: p = N_(" VISUAL"); break;
9854 case 1: p = N_(" VISUAL LINE"); break;
9855 case 2: p = N_(" VISUAL BLOCK"); break;
9856 case 4: p = N_(" SELECT"); break;
9857 case 5: p = N_(" SELECT LINE"); break;
9858 default: p = N_(" SELECT BLOCK"); break;
9859 }
9860 MSG_PUTS_ATTR(_(p), attr);
9861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 MSG_PUTS_ATTR(" --", attr);
9863 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009864
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 need_clear = TRUE;
9866 }
9867 if (Recording
9868#ifdef FEAT_INS_EXPAND
9869 && edit_submode == NULL /* otherwise it gets too long */
9870#endif
9871 )
9872 {
9873 MSG_PUTS_ATTR(_("recording"), attr);
9874 need_clear = TRUE;
9875 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009876
9877 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 if (need_clear || clear_cmdline)
9879 msg_clr_eos();
9880 msg_didout = FALSE; /* overwrite this message */
9881 length = msg_col;
9882 msg_col = 0;
9883 need_wait_return = nwr_save; /* never ask for hit-return for this */
9884 }
9885 else if (clear_cmdline && msg_silent == 0)
9886 /* Clear the whole command line. Will reset "clear_cmdline". */
9887 msg_clr_cmdline();
9888
9889#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 /* In Visual mode the size of the selected area must be redrawn. */
9891 if (VIsual_active)
9892 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
9894 /* If the last window has no status line, the ruler is after the mode
9895 * message and must be redrawn */
9896 if (redrawing()
9897# ifdef FEAT_WINDOWS
9898 && lastwin->w_status_height == 0
9899# endif
9900 )
9901 win_redr_ruler(lastwin, TRUE);
9902#endif
9903 redraw_cmdline = FALSE;
9904 clear_cmdline = FALSE;
9905
9906 return length;
9907}
9908
9909/*
9910 * Position for a mode message.
9911 */
9912 static void
9913msg_pos_mode()
9914{
9915 msg_col = 0;
9916 msg_row = Rows - 1;
9917}
9918
9919/*
9920 * Delete mode message. Used when ESC is typed which is expected to end
9921 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009922 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 */
9924 void
9925unshowmode(force)
9926 int force;
9927{
9928 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009929 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 */
9931 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9932 redraw_cmdline = TRUE; /* delete mode later */
9933 else
9934 {
9935 msg_pos_mode();
9936 if (Recording)
9937 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9938 msg_clr_eos();
9939 }
9940}
9941
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009942#if defined(FEAT_WINDOWS)
9943/*
9944 * Draw the tab pages line at the top of the Vim window.
9945 */
9946 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009947draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009948{
9949 int tabcount = 0;
9950 tabpage_T *tp;
9951 int tabwidth;
9952 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009953 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009954 int attr;
9955 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009956 win_T *cwp;
9957 int wincount;
9958 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009959 int c;
9960 int len;
9961 int attr_sel = hl_attr(HLF_TPS);
9962 int attr_nosel = hl_attr(HLF_TP);
9963 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009964 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009965 int room;
9966 int use_sep_chars = (t_colors < 8
9967#ifdef FEAT_GUI
9968 && !gui.in_use
9969#endif
9970 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009971
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009972 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009973
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009974#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009975 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009976 if (gui_use_tabline())
9977 {
9978 gui_update_tabline();
9979 return;
9980 }
9981#endif
9982
9983 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009984 return;
9985
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009986#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009987
9988 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9989 for (scol = 0; scol < Columns; ++scol)
9990 TabPageIdxs[scol] = 0;
9991
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009992 /* Use the 'tabline' option if it's set. */
9993 if (*p_tal != NUL)
9994 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009995 int save_called_emsg = called_emsg;
9996
9997 /* Check for an error. If there is one we would loop in redrawing the
9998 * screen. Avoid that by making 'tabline' empty. */
9999 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010000 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010001 if (called_emsg)
10002 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010003 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010004 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010005 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010006 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010007#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010008 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010009 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
10010 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010011
Bram Moolenaar238a5642006-02-21 22:12:05 +000010012 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10013 if (tabwidth < 6)
10014 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010015
Bram Moolenaar238a5642006-02-21 22:12:05 +000010016 attr = attr_nosel;
10017 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010018 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010019 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10020 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010021 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010022 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010023
Bram Moolenaar238a5642006-02-21 22:12:05 +000010024 if (tp->tp_topframe == topframe)
10025 attr = attr_sel;
10026 if (use_sep_chars && col > 0)
10027 screen_putchar('|', 0, col++, attr);
10028
10029 if (tp->tp_topframe != topframe)
10030 attr = attr_nosel;
10031
10032 screen_putchar(' ', 0, col++, attr);
10033
10034 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010035 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010036 cwp = curwin;
10037 wp = firstwin;
10038 }
10039 else
10040 {
10041 cwp = tp->tp_curwin;
10042 wp = tp->tp_firstwin;
10043 }
10044
10045 modified = FALSE;
10046 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10047 if (bufIsChanged(wp->w_buffer))
10048 modified = TRUE;
10049 if (modified || wincount > 1)
10050 {
10051 if (wincount > 1)
10052 {
10053 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010054 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010055 if (col + len >= Columns - 3)
10056 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010057 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010058#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010059 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010060#else
Bram Moolenaar238a5642006-02-21 22:12:05 +000010061 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010062#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010063 );
10064 col += len;
10065 }
10066 if (modified)
10067 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10068 screen_putchar(' ', 0, col++, attr);
10069 }
10070
10071 room = scol - col + tabwidth - 1;
10072 if (room > 0)
10073 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010074 /* Get buffer name in NameBuff[] */
10075 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010076 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010077 len = vim_strsize(NameBuff);
10078 p = NameBuff;
10079#ifdef FEAT_MBYTE
10080 if (has_mbyte)
10081 while (len > room)
10082 {
10083 len -= ptr2cells(p);
10084 mb_ptr_adv(p);
10085 }
10086 else
10087#endif
10088 if (len > room)
10089 {
10090 p += len - room;
10091 len = room;
10092 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010093 if (len > Columns - col - 1)
10094 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010095
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010096 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010097 col += len;
10098 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010099 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010100
10101 /* Store the tab page number in TabPageIdxs[], so that
10102 * jump_to_mouse() knows where each one is. */
10103 ++tabcount;
10104 while (scol < col)
10105 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010106 }
10107
Bram Moolenaar238a5642006-02-21 22:12:05 +000010108 if (use_sep_chars)
10109 c = '_';
10110 else
10111 c = ' ';
10112 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010113
10114 /* Put an "X" for closing the current tab if there are several. */
10115 if (first_tabpage->tp_next != NULL)
10116 {
10117 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10118 TabPageIdxs[Columns - 1] = -999;
10119 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010120 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010121
10122 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10123 * set. */
10124 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010125}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010126
10127/*
10128 * Get buffer name for "buf" into NameBuff[].
10129 * Takes care of special buffer names and translates special characters.
10130 */
10131 void
10132get_trans_bufname(buf)
10133 buf_T *buf;
10134{
10135 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010136 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010137 else
10138 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10139 trans_characters(NameBuff, MAXPATHL);
10140}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010141#endif
10142
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10144/*
10145 * Get the character to use in a status line. Get its attributes in "*attr".
10146 */
10147 static int
10148fillchar_status(attr, is_curwin)
10149 int *attr;
10150 int is_curwin;
10151{
10152 int fill;
10153 if (is_curwin)
10154 {
10155 *attr = hl_attr(HLF_S);
10156 fill = fill_stl;
10157 }
10158 else
10159 {
10160 *attr = hl_attr(HLF_SNC);
10161 fill = fill_stlnc;
10162 }
10163 /* Use fill when there is highlighting, and highlighting of current
10164 * window differs, or the fillchars differ, or this is not the
10165 * current window */
10166 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10167 || !is_curwin || firstwin == lastwin)
10168 || (fill_stl != fill_stlnc)))
10169 return fill;
10170 if (is_curwin)
10171 return '^';
10172 return '=';
10173}
10174#endif
10175
10176#ifdef FEAT_VERTSPLIT
10177/*
10178 * Get the character to use in a separator between vertically split windows.
10179 * Get its attributes in "*attr".
10180 */
10181 static int
10182fillchar_vsep(attr)
10183 int *attr;
10184{
10185 *attr = hl_attr(HLF_C);
10186 if (*attr == 0 && fill_vert == ' ')
10187 return '|';
10188 else
10189 return fill_vert;
10190}
10191#endif
10192
10193/*
10194 * Return TRUE if redrawing should currently be done.
10195 */
10196 int
10197redrawing()
10198{
10199 return (!RedrawingDisabled
10200 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10201}
10202
10203/*
10204 * Return TRUE if printing messages should currently be done.
10205 */
10206 int
10207messaging()
10208{
10209 return (!(p_lz && char_avail() && !KeyTyped));
10210}
10211
10212/*
10213 * Show current status info in ruler and various other places
10214 * If always is FALSE, only show ruler if position has changed.
10215 */
10216 void
10217showruler(always)
10218 int always;
10219{
10220 if (!always && !redrawing())
10221 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010222#ifdef FEAT_INS_EXPAND
10223 if (pum_visible())
10224 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010225# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010226 /* Don't redraw right now, do it later. */
10227 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010228# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010229 return;
10230 }
10231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010233 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010234 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010235 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 else
10238#endif
10239#ifdef FEAT_CMDL_INFO
10240 win_redr_ruler(curwin, always);
10241#endif
10242
10243#ifdef FEAT_TITLE
10244 if (need_maketitle
10245# ifdef FEAT_STL_OPT
10246 || (p_icon && (stl_syntax & STL_IN_ICON))
10247 || (p_title && (stl_syntax & STL_IN_TITLE))
10248# endif
10249 )
10250 maketitle();
10251#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010252#ifdef FEAT_WINDOWS
10253 /* Redraw the tab pages line if needed. */
10254 if (redraw_tabline)
10255 draw_tabline();
10256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
10259#ifdef FEAT_CMDL_INFO
10260 static void
10261win_redr_ruler(wp, always)
10262 win_T *wp;
10263 int always;
10264{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010265#define RULER_BUF_LEN 70
10266 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 int row;
10268 int fillchar;
10269 int attr;
10270 int empty_line = FALSE;
10271 colnr_T virtcol;
10272 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010273 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 int o;
10275#ifdef FEAT_VERTSPLIT
10276 int this_ru_col;
10277 int off = 0;
10278 int width = Columns;
10279# define WITH_OFF(x) x
10280# define WITH_WIDTH(x) x
10281#else
10282# define WITH_OFF(x) 0
10283# define WITH_WIDTH(x) Columns
10284# define this_ru_col ru_col
10285#endif
10286
10287 /* If 'ruler' off or redrawing disabled, don't do anything */
10288 if (!p_ru)
10289 return;
10290
10291 /*
10292 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10293 * after deleting lines, before cursor.lnum is corrected.
10294 */
10295 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10296 return;
10297
10298#ifdef FEAT_INS_EXPAND
10299 /* Don't draw the ruler while doing insert-completion, it might overwrite
10300 * the (long) mode message. */
10301# ifdef FEAT_WINDOWS
10302 if (wp == lastwin && lastwin->w_status_height == 0)
10303# endif
10304 if (edit_submode != NULL)
10305 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010306 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10307 if (pum_visible())
10308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309#endif
10310
10311#ifdef FEAT_STL_OPT
10312 if (*p_ruf)
10313 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010314 int save_called_emsg = called_emsg;
10315
10316 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010318 if (called_emsg)
10319 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010320 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010321 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 return;
10323 }
10324#endif
10325
10326 /*
10327 * Check if not in Insert mode and the line is empty (will show "0-1").
10328 */
10329 if (!(State & INSERT)
10330 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10331 empty_line = TRUE;
10332
10333 /*
10334 * Only draw the ruler when something changed.
10335 */
10336 validate_virtcol_win(wp);
10337 if ( redraw_cmdline
10338 || always
10339 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10340 || wp->w_cursor.col != wp->w_ru_cursor.col
10341 || wp->w_virtcol != wp->w_ru_virtcol
10342#ifdef FEAT_VIRTUALEDIT
10343 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10344#endif
10345 || wp->w_topline != wp->w_ru_topline
10346 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10347#ifdef FEAT_DIFF
10348 || wp->w_topfill != wp->w_ru_topfill
10349#endif
10350 || empty_line != wp->w_ru_empty)
10351 {
10352 cursor_off();
10353#ifdef FEAT_WINDOWS
10354 if (wp->w_status_height)
10355 {
10356 row = W_WINROW(wp) + wp->w_height;
10357 fillchar = fillchar_status(&attr, wp == curwin);
10358# ifdef FEAT_VERTSPLIT
10359 off = W_WINCOL(wp);
10360 width = W_WIDTH(wp);
10361# endif
10362 }
10363 else
10364#endif
10365 {
10366 row = Rows - 1;
10367 fillchar = ' ';
10368 attr = 0;
10369#ifdef FEAT_VERTSPLIT
10370 width = Columns;
10371 off = 0;
10372#endif
10373 }
10374
10375 /* In list mode virtcol needs to be recomputed */
10376 virtcol = wp->w_virtcol;
10377 if (wp->w_p_list && lcs_tab1 == NUL)
10378 {
10379 wp->w_p_list = FALSE;
10380 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10381 wp->w_p_list = TRUE;
10382 }
10383
10384 /*
10385 * Some sprintfs return the length, some return a pointer.
10386 * To avoid portability problems we use strlen() here.
10387 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010388 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10390 ? 0L
10391 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010392 len = STRLEN(buffer);
10393 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10395 (int)virtcol + 1);
10396
10397 /*
10398 * Add a "50%" if there is room for it.
10399 * On the last line, don't print in the last column (scrolls the
10400 * screen up on some terminals).
10401 */
10402 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010403 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 o = i + vim_strsize(buffer + i + 1);
10405#ifdef FEAT_WINDOWS
10406 if (wp->w_status_height == 0) /* can't use last char of screen */
10407#endif
10408 ++o;
10409#ifdef FEAT_VERTSPLIT
10410 this_ru_col = ru_col - (Columns - width);
10411 if (this_ru_col < 0)
10412 this_ru_col = 0;
10413#endif
10414 /* Never use more than half the window/screen width, leave the other
10415 * half for the filename. */
10416 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10417 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10418 if (this_ru_col + o < WITH_WIDTH(width))
10419 {
10420 while (this_ru_col + o < WITH_WIDTH(width))
10421 {
10422#ifdef FEAT_MBYTE
10423 if (has_mbyte)
10424 i += (*mb_char2bytes)(fillchar, buffer + i);
10425 else
10426#endif
10427 buffer[i++] = fillchar;
10428 ++o;
10429 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010430 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 }
10432 /* Truncate at window boundary. */
10433#ifdef FEAT_MBYTE
10434 if (has_mbyte)
10435 {
10436 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010437 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 {
10439 o += (*mb_ptr2cells)(buffer + i);
10440 if (this_ru_col + o > WITH_WIDTH(width))
10441 {
10442 buffer[i] = NUL;
10443 break;
10444 }
10445 }
10446 }
10447 else
10448#endif
10449 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10450 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10451
10452 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10453 i = redraw_cmdline;
10454 screen_fill(row, row + 1,
10455 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10456 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10457 fillchar, fillchar, attr);
10458 /* don't redraw the cmdline because of showing the ruler */
10459 redraw_cmdline = i;
10460 wp->w_ru_cursor = wp->w_cursor;
10461 wp->w_ru_virtcol = wp->w_virtcol;
10462 wp->w_ru_empty = empty_line;
10463 wp->w_ru_topline = wp->w_topline;
10464 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10465#ifdef FEAT_DIFF
10466 wp->w_ru_topfill = wp->w_topfill;
10467#endif
10468 }
10469}
10470#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010471
10472#if defined(FEAT_LINEBREAK) || defined(PROTO)
10473/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010474 * Return the width of the 'number' and 'relativenumber' column.
10475 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010476 * Otherwise it depends on 'numberwidth' and the line count.
10477 */
10478 int
10479number_width(wp)
10480 win_T *wp;
10481{
10482 int n;
10483 linenr_T lnum;
10484
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010485 if (wp->w_p_rnu && !wp->w_p_nu)
10486 /* cursor line shows "0" */
10487 lnum = wp->w_height;
10488 else
10489 /* cursor line shows absolute line number */
10490 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010491
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010492 if (lnum == wp->w_nrwidth_line_count)
10493 return wp->w_nrwidth_width;
10494 wp->w_nrwidth_line_count = lnum;
10495
10496 n = 0;
10497 do
10498 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010499 lnum /= 10;
10500 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010501 } while (lnum > 0);
10502
10503 /* 'numberwidth' gives the minimal width plus one */
10504 if (n < wp->w_p_nuw - 1)
10505 n = wp->w_p_nuw - 1;
10506
10507 wp->w_nrwidth_width = n;
10508 return n;
10509}
10510#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010511
10512/*
10513 * Return the current cursor column. This is the actual position on the
10514 * screen. First column is 0.
10515 */
10516 int
10517screen_screencol()
10518{
10519 return screen_cur_col;
10520}
10521
10522/*
10523 * Return the current cursor row. This is the actual position on the screen.
10524 * First row is 0.
10525 */
10526 int
10527screen_screenrow()
10528{
10529 return screen_cur_row;
10530}