blob: 87b12591101e4e483e46a10a9f17e6df33869aa6 [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));
147static void next_search_hl __ARGS((win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol));
148#endif
149static void screen_start_highlight __ARGS((int attr));
150static void screen_char __ARGS((unsigned off, int row, int col));
151#ifdef FEAT_MBYTE
152static void screen_char_2 __ARGS((unsigned off, int row, int col));
153#endif
154static void screenclear2 __ARGS((void));
155static void lineclear __ARGS((unsigned off, int width));
156static void lineinvalid __ARGS((unsigned off, int width));
157#ifdef FEAT_VERTSPLIT
158static void linecopy __ARGS((int to, int from, win_T *wp));
159static void redraw_block __ARGS((int row, int end, win_T *wp));
160#endif
161static int win_do_lines __ARGS((win_T *wp, int row, int line_count, int mayclear, int del));
162static void win_rest_invalid __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void msg_pos_mode __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000164#if defined(FEAT_WINDOWS)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000165static void draw_tabline __ARGS((void));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
168static int fillchar_status __ARGS((int *attr, int is_curwin));
169#endif
170#ifdef FEAT_VERTSPLIT
171static int fillchar_vsep __ARGS((int *attr));
172#endif
173#ifdef FEAT_STL_OPT
Bram Moolenaar9372a112005-12-06 19:59:18 +0000174static void win_redr_custom __ARGS((win_T *wp, int draw_ruler));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#ifdef FEAT_CMDL_INFO
177static void win_redr_ruler __ARGS((win_T *wp, int always));
178#endif
179
180#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
181/* Ugly global: overrule attribute used by screen_char() */
182static int screen_char_attr = 0;
183#endif
184
185/*
186 * Redraw the current window later, with update_screen(type).
187 * Set must_redraw only if not already set to a higher value.
188 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
189 */
190 void
191redraw_later(type)
192 int type;
193{
194 redraw_win_later(curwin, type);
195}
196
197 void
198redraw_win_later(wp, type)
199 win_T *wp;
200 int type;
201{
202 if (wp->w_redr_type < type)
203 {
204 wp->w_redr_type = type;
205 if (type >= NOT_VALID)
206 wp->w_lines_valid = 0;
207 if (must_redraw < type) /* must_redraw is the maximum of all windows */
208 must_redraw = type;
209 }
210}
211
212/*
213 * Force a complete redraw later. Also resets the highlighting. To be used
214 * after executing a shell command that messes up the screen.
215 */
216 void
217redraw_later_clear()
218{
219 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000220#ifdef FEAT_GUI
221 if (gui.in_use)
222 /* Use a code that will reset gui.highlight_mask in
223 * gui_stop_highlight(). */
224 screen_attr = HL_ALL + 1;
225 else
226#endif
227 /* Use attributes that is very unlikely to appear in text. */
228 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229}
230
231/*
232 * Mark all windows to be redrawn later.
233 */
234 void
235redraw_all_later(type)
236 int type;
237{
238 win_T *wp;
239
240 FOR_ALL_WINDOWS(wp)
241 {
242 redraw_win_later(wp, type);
243 }
244}
245
246/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000247 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 */
249 void
250redraw_curbuf_later(type)
251 int type;
252{
253 redraw_buf_later(curbuf, type);
254}
255
256 void
257redraw_buf_later(buf, type)
258 buf_T *buf;
259 int type;
260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
270/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200271 * Redraw as soon as possible. When the command line is not scrolled redraw
272 * right away and restore what was on the command line.
273 * Return a code indicating what happened.
274 */
275 int
276redraw_asap(type)
277 int type;
278{
279 int rows;
280 int r;
281 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200282 schar_T *screenline; /* copy from ScreenLines[] */
283 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200284#ifdef FEAT_MBYTE
285 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200286 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200287 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200288 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200289#endif
290
291 redraw_later(type);
292 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY))
293 return ret;
294
295 /* Allocate space to save the text displayed in the command line area. */
296 rows = Rows - cmdline_row;
297 screenline = (schar_T *)lalloc(
298 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
299 screenattr = (sattr_T *)lalloc(
300 (long_u)(rows * Columns * sizeof(sattr_T)), FALSE);
301 if (screenline == NULL || screenattr == NULL)
302 ret = 2;
303#ifdef FEAT_MBYTE
304 if (enc_utf8)
305 {
306 screenlineUC = (u8char_T *)lalloc(
307 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
308 if (screenlineUC == NULL)
309 ret = 2;
310 for (i = 0; i < p_mco; ++i)
311 {
312 screenlineC[i] = (u8char_T *)lalloc(
313 (long_u)(rows * Columns * sizeof(u8char_T)), FALSE);
314 if (screenlineC[i] == NULL)
315 ret = 2;
316 }
317 }
318 if (enc_dbcs == DBCS_JPNU)
319 {
320 screenline2 = (schar_T *)lalloc(
321 (long_u)(rows * Columns * sizeof(schar_T)), FALSE);
322 if (screenline2 == NULL)
323 ret = 2;
324 }
325#endif
326
327 if (ret != 2)
328 {
329 /* Save the text displayed in the command line area. */
330 for (r = 0; r < rows; ++r)
331 {
332 mch_memmove(screenline + r * Columns,
333 ScreenLines + LineOffset[cmdline_row + r],
334 (size_t)Columns * sizeof(schar_T));
335 mch_memmove(screenattr + r * Columns,
336 ScreenAttrs + LineOffset[cmdline_row + r],
337 (size_t)Columns * sizeof(sattr_T));
338#ifdef FEAT_MBYTE
339 if (enc_utf8)
340 {
341 mch_memmove(screenlineUC + r * Columns,
342 ScreenLinesUC + LineOffset[cmdline_row + r],
343 (size_t)Columns * sizeof(u8char_T));
344 for (i = 0; i < p_mco; ++i)
345 mch_memmove(screenlineC[i] + r * Columns,
346 ScreenLinesC[r] + LineOffset[cmdline_row + r],
347 (size_t)Columns * sizeof(u8char_T));
348 }
349 if (enc_dbcs == DBCS_JPNU)
350 mch_memmove(screenline2 + r * Columns,
351 ScreenLines2 + LineOffset[cmdline_row + r],
352 (size_t)Columns * sizeof(schar_T));
353#endif
354 }
355
356 update_screen(0);
357 ret = 3;
358
359 if (must_redraw == 0)
360 {
361 int off = (int)(current_ScreenLine - ScreenLines);
362
363 /* Restore the text displayed in the command line area. */
364 for (r = 0; r < rows; ++r)
365 {
366 mch_memmove(current_ScreenLine,
367 screenline + r * Columns,
368 (size_t)Columns * sizeof(schar_T));
369 mch_memmove(ScreenAttrs + off,
370 screenattr + r * Columns,
371 (size_t)Columns * sizeof(sattr_T));
372#ifdef FEAT_MBYTE
373 if (enc_utf8)
374 {
375 mch_memmove(ScreenLinesUC + off,
376 screenlineUC + r * Columns,
377 (size_t)Columns * sizeof(u8char_T));
378 for (i = 0; i < p_mco; ++i)
379 mch_memmove(ScreenLinesC[i] + off,
380 screenlineC[i] + r * Columns,
381 (size_t)Columns * sizeof(u8char_T));
382 }
383 if (enc_dbcs == DBCS_JPNU)
384 mch_memmove(ScreenLines2 + off,
385 screenline2 + r * Columns,
386 (size_t)Columns * sizeof(schar_T));
387#endif
388 SCREEN_LINE(cmdline_row + r, 0, Columns, Columns, FALSE);
389 }
390 ret = 4;
391 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200392 }
393
394 vim_free(screenline);
395 vim_free(screenattr);
396#ifdef FEAT_MBYTE
397 if (enc_utf8)
398 {
399 vim_free(screenlineUC);
400 for (i = 0; i < p_mco; ++i)
401 vim_free(screenlineC[i]);
402 }
403 if (enc_dbcs == DBCS_JPNU)
404 vim_free(screenline2);
405#endif
406
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200407 /* Show the intro message when appropriate. */
408 maybe_intro_message();
409
410 setcursor();
411
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 return ret;
413}
414
415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 * Changed something in the current window, at buffer line "lnum", that
417 * requires that line and possibly other lines to be redrawn.
418 * Used when entering/leaving Insert mode with the cursor on a folded line.
419 * Used to remove the "$" from a change command.
420 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
421 * may become invalid and the whole window will have to be redrawn.
422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 void
424redrawWinline(lnum, invalid)
425 linenr_T lnum;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000426 int invalid UNUSED; /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
428#ifdef FEAT_FOLDING
429 int i;
430#endif
431
432 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
433 curwin->w_redraw_top = lnum;
434 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
435 curwin->w_redraw_bot = lnum;
436 redraw_later(VALID);
437
438#ifdef FEAT_FOLDING
439 if (invalid)
440 {
441 /* A w_lines[] entry for this lnum has become invalid. */
442 i = find_wl_entry(curwin, lnum);
443 if (i >= 0)
444 curwin->w_lines[i].wl_valid = FALSE;
445 }
446#endif
447}
448
449/*
450 * update all windows that are editing the current buffer
451 */
452 void
453update_curbuf(type)
454 int type;
455{
456 redraw_curbuf_later(type);
457 update_screen(type);
458}
459
460/*
461 * update_screen()
462 *
463 * Based on the current value of curwin->w_topline, transfer a screenfull
464 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
465 */
466 void
467update_screen(type)
468 int type;
469{
470 win_T *wp;
471 static int did_intro = FALSE;
472#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
473 int did_one;
474#endif
475
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000476 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 if (!screen_valid(TRUE))
478 return;
479
480 if (must_redraw)
481 {
482 if (type < must_redraw) /* use maximal type */
483 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000484
485 /* must_redraw is reset here, so that when we run into some weird
486 * reason to redraw while busy redrawing (e.g., asynchronous
487 * scrolling), or update_topline() in win_update() will cause a
488 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 must_redraw = 0;
490 }
491
492 /* Need to update w_lines[]. */
493 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
494 type = NOT_VALID;
495
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000496 /* Postpone the redrawing when it's not needed and when being called
497 * recursively. */
498 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
500 redraw_later(type); /* remember type for next time */
501 must_redraw = type;
502 if (type > INVERTED_ALL)
503 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
504 return;
505 }
506
507 updating_screen = TRUE;
508#ifdef FEAT_SYN_HL
509 ++display_tick; /* let syntax code know we're in a next round of
510 * display updating */
511#endif
512
513 /*
514 * if the screen was scrolled up when displaying a message, scroll it down
515 */
516 if (msg_scrolled)
517 {
518 clear_cmdline = TRUE;
519 if (msg_scrolled > Rows - 5) /* clearing is faster */
520 type = CLEAR;
521 else if (type != CLEAR)
522 {
523 check_for_delay(FALSE);
524 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
525 type = CLEAR;
526 FOR_ALL_WINDOWS(wp)
527 {
528 if (W_WINROW(wp) < msg_scrolled)
529 {
530 if (W_WINROW(wp) + wp->w_height > msg_scrolled
531 && wp->w_redr_type < REDRAW_TOP
532 && wp->w_lines_valid > 0
533 && wp->w_topline == wp->w_lines[0].wl_lnum)
534 {
535 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
536 wp->w_redr_type = REDRAW_TOP;
537 }
538 else
539 {
540 wp->w_redr_type = NOT_VALID;
541#ifdef FEAT_WINDOWS
542 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
543 <= msg_scrolled)
544 wp->w_redr_status = TRUE;
545#endif
546 }
547 }
548 }
549 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000550#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000551 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554 msg_scrolled = 0;
555 need_wait_return = FALSE;
556 }
557
558 /* reset cmdline_row now (may have been changed temporarily) */
559 compute_cmdrow();
560
561 /* Check for changed highlighting */
562 if (need_highlight_changed)
563 highlight_changed();
564
565 if (type == CLEAR) /* first clear screen */
566 {
567 screenclear(); /* will reset clear_cmdline */
568 type = NOT_VALID;
569 }
570
571 if (clear_cmdline) /* going to clear cmdline (done below) */
572 check_for_delay(FALSE);
573
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000574#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200575 /* Force redraw when width of 'number' or 'relativenumber' column
576 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000577 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200578 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
579 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000580 curwin->w_redr_type = NOT_VALID;
581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 /*
584 * Only start redrawing if there is really something to do.
585 */
586 if (type == INVERTED)
587 update_curswant();
588 if (curwin->w_redr_type < type
589 && !((type == VALID
590 && curwin->w_lines[0].wl_valid
591#ifdef FEAT_DIFF
592 && curwin->w_topfill == curwin->w_old_topfill
593 && curwin->w_botfill == curwin->w_old_botfill
594#endif
595 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000597 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
599 && curwin->w_old_visual_mode == VIsual_mode
600 && (curwin->w_valid & VALID_VIRTCOL)
601 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 ))
603 curwin->w_redr_type = type;
604
Bram Moolenaar5a305422006-04-28 22:38:25 +0000605#ifdef FEAT_WINDOWS
606 /* Redraw the tab pages line if needed. */
607 if (redraw_tabline || type >= NOT_VALID)
608 draw_tabline();
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611#ifdef FEAT_SYN_HL
612 /*
613 * Correct stored syntax highlighting info for changes in each displayed
614 * buffer. Each buffer must only be done once.
615 */
616 FOR_ALL_WINDOWS(wp)
617 {
618 if (wp->w_buffer->b_mod_set)
619 {
620# ifdef FEAT_WINDOWS
621 win_T *wwp;
622
623 /* Check if we already did this buffer. */
624 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
625 if (wwp->w_buffer == wp->w_buffer)
626 break;
627# endif
628 if (
629# ifdef FEAT_WINDOWS
630 wwp == wp &&
631# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200632 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 syn_stack_apply_changes(wp->w_buffer);
634 }
635 }
636#endif
637
638 /*
639 * Go from top to bottom through the windows, redrawing the ones that need
640 * it.
641 */
642#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
643 did_one = FALSE;
644#endif
645#ifdef FEAT_SEARCH_EXTRA
646 search_hl.rm.regprog = NULL;
647#endif
648 FOR_ALL_WINDOWS(wp)
649 {
650 if (wp->w_redr_type != 0)
651 {
652 cursor_off();
653#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
654 if (!did_one)
655 {
656 did_one = TRUE;
657# ifdef FEAT_SEARCH_EXTRA
658 start_search_hl();
659# endif
660# ifdef FEAT_CLIPBOARD
661 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200662 if (clip_star.available && clip_isautosel_star())
663 clip_update_selection(&clip_star);
664 if (clip_plus.available && clip_isautosel_plus())
665 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666# endif
667#ifdef FEAT_GUI
668 /* Remove the cursor before starting to do anything, because
669 * scrolling may make it difficult to redraw the text under
670 * it. */
671 if (gui.in_use)
672 gui_undraw_cursor();
673#endif
674 }
675#endif
676 win_update(wp);
677 }
678
679#ifdef FEAT_WINDOWS
680 /* redraw status line after the window to minimize cursor movement */
681 if (wp->w_redr_status)
682 {
683 cursor_off();
684 win_redr_status(wp);
685 }
686#endif
687 }
688#if defined(FEAT_SEARCH_EXTRA)
689 end_search_hl();
690#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100691#ifdef FEAT_INS_EXPAND
692 /* May need to redraw the popup menu. */
693 if (pum_visible())
694 pum_redraw();
695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697#ifdef FEAT_WINDOWS
698 /* Reset b_mod_set flags. Going through all windows is probably faster
699 * than going through all buffers (there could be many buffers). */
700 for (wp = firstwin; wp != NULL; wp = wp->w_next)
701 wp->w_buffer->b_mod_set = FALSE;
702#else
703 curbuf->b_mod_set = FALSE;
704#endif
705
706 updating_screen = FALSE;
707#ifdef FEAT_GUI
708 gui_may_resize_shell();
709#endif
710
711 /* Clear or redraw the command line. Done last, because scrolling may
712 * mess up the command line. */
713 if (clear_cmdline || redraw_cmdline)
714 showmode();
715
716 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200717 if (!did_intro)
718 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 did_intro = TRUE;
720
721#ifdef FEAT_GUI
722 /* Redraw the cursor and update the scrollbars when all screen updating is
723 * done. */
724 if (gui.in_use)
725 {
726 out_flush(); /* required before updating the cursor */
727 if (did_one)
728 gui_update_cursor(FALSE, FALSE);
729 gui_update_scrollbars(FALSE);
730 }
731#endif
732}
733
Bram Moolenaar860cae12010-06-05 23:22:07 +0200734#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200735/*
736 * Return TRUE if the cursor line in window "wp" may be concealed, according
737 * to the 'concealcursor' option.
738 */
739 int
740conceal_cursor_line(wp)
741 win_T *wp;
742{
743 int c;
744
745 if (*wp->w_p_cocu == NUL)
746 return FALSE;
747 if (get_real_state() & VISUAL)
748 c = 'v';
749 else if (State & INSERT)
750 c = 'i';
751 else if (State & NORMAL)
752 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200753 else if (State & CMDLINE)
754 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200755 else
756 return FALSE;
757 return vim_strchr(wp->w_p_cocu, c) != NULL;
758}
759
760/*
761 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
762 */
763 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200764conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200765{
766 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
767 {
768 need_cursor_line_redraw = TRUE;
769 /* Need to recompute cursor column, e.g., when starting Visual mode
770 * without concealing. */
771 curs_columns(TRUE);
772 }
773}
774
Bram Moolenaar860cae12010-06-05 23:22:07 +0200775 void
776update_single_line(wp, lnum)
777 win_T *wp;
778 linenr_T lnum;
779{
780 int row;
781 int j;
782
783 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200784 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200785 {
786# ifdef FEAT_GUI
787 /* Remove the cursor before starting to do anything, because scrolling
788 * may make it difficult to redraw the text under it. */
789 if (gui.in_use)
790 gui_undraw_cursor();
791# endif
792 row = 0;
793 for (j = 0; j < wp->w_lines_valid; ++j)
794 {
795 if (lnum == wp->w_lines[j].wl_lnum)
796 {
797 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200798# ifdef FEAT_SEARCH_EXTRA
799 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200800 start_search_hl();
801 prepare_search_hl(wp, lnum);
802# endif
803 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
804# if defined(FEAT_SEARCH_EXTRA)
805 end_search_hl();
806# endif
807 break;
808 }
809 row += wp->w_lines[j].wl_size;
810 }
811# ifdef FEAT_GUI
812 /* Redraw the cursor */
813 if (gui.in_use)
814 {
815 out_flush(); /* required before updating the cursor */
816 gui_update_cursor(FALSE, FALSE);
817 }
818# endif
819 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200820 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200821}
822#endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
825static void update_prepare __ARGS((void));
826static void update_finish __ARGS((void));
827
828/*
829 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000830 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 */
832 static void
833update_prepare()
834{
835 cursor_off();
836 updating_screen = TRUE;
837#ifdef FEAT_GUI
838 /* Remove the cursor before starting to do anything, because scrolling may
839 * make it difficult to redraw the text under it. */
840 if (gui.in_use)
841 gui_undraw_cursor();
842#endif
843#ifdef FEAT_SEARCH_EXTRA
844 start_search_hl();
845#endif
846}
847
848/*
849 * Finish updating one or more windows.
850 */
851 static void
852update_finish()
853{
854 if (redraw_cmdline)
855 showmode();
856
857# ifdef FEAT_SEARCH_EXTRA
858 end_search_hl();
859# endif
860
861 updating_screen = FALSE;
862
863# ifdef FEAT_GUI
864 gui_may_resize_shell();
865
866 /* Redraw the cursor and update the scrollbars when all screen updating is
867 * done. */
868 if (gui.in_use)
869 {
870 out_flush(); /* required before updating the cursor */
871 gui_update_cursor(FALSE, FALSE);
872 gui_update_scrollbars(FALSE);
873 }
874# endif
875}
876#endif
877
878#if defined(FEAT_SIGNS) || defined(PROTO)
879 void
880update_debug_sign(buf, lnum)
881 buf_T *buf;
882 linenr_T lnum;
883{
884 win_T *wp;
885 int doit = FALSE;
886
887# ifdef FEAT_FOLDING
888 win_foldinfo.fi_level = 0;
889# endif
890
891 /* update/delete a specific mark */
892 FOR_ALL_WINDOWS(wp)
893 {
894 if (buf != NULL && lnum > 0)
895 {
896 if (wp->w_buffer == buf && lnum >= wp->w_topline
897 && lnum < wp->w_botline)
898 {
899 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
900 wp->w_redraw_top = lnum;
901 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
902 wp->w_redraw_bot = lnum;
903 redraw_win_later(wp, VALID);
904 }
905 }
906 else
907 redraw_win_later(wp, VALID);
908 if (wp->w_redr_type != 0)
909 doit = TRUE;
910 }
911
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100912 /* Return when there is nothing to do, screen updating is already
913 * happening (recursive call) or still starting up. */
914 if (!doit || updating_screen
915#ifdef FEAT_GUI
916 || gui.starting
917#endif
918 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 return;
920
921 /* update all windows that need updating */
922 update_prepare();
923
924# ifdef FEAT_WINDOWS
925 for (wp = firstwin; wp; wp = wp->w_next)
926 {
927 if (wp->w_redr_type != 0)
928 win_update(wp);
929 if (wp->w_redr_status)
930 win_redr_status(wp);
931 }
932# else
933 if (curwin->w_redr_type != 0)
934 win_update(curwin);
935# endif
936
937 update_finish();
938}
939#endif
940
941
942#if defined(FEAT_GUI) || defined(PROTO)
943/*
944 * Update a single window, its status line and maybe the command line msg.
945 * Used for the GUI scrollbar.
946 */
947 void
948updateWindow(wp)
949 win_T *wp;
950{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000951 /* return if already busy updating */
952 if (updating_screen)
953 return;
954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 update_prepare();
956
957#ifdef FEAT_CLIPBOARD
958 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200959 if (clip_star.available && clip_isautosel_star())
960 clip_update_selection(&clip_star);
961 if (clip_plus.available && clip_isautosel_plus())
962 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000968 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000969 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000970 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (wp->w_redr_status
973# ifdef FEAT_CMDL_INFO
974 || p_ru
975# endif
976# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000977 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978# endif
979 )
980 win_redr_status(wp);
981#endif
982
983 update_finish();
984}
985#endif
986
987/*
988 * Update a single window.
989 *
990 * This may cause the windows below it also to be redrawn (when clearing the
991 * screen or scrolling lines).
992 *
993 * How the window is redrawn depends on wp->w_redr_type. Each type also
994 * implies the one below it.
995 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +0000996 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
998 * INVERTED redraw the changed part of the Visual area
999 * INVERTED_ALL redraw the whole Visual area
1000 * VALID 1. scroll up/down to adjust for a changed w_topline
1001 * 2. update lines at the top when scrolled down
1002 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001003 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * b_mod_top and b_mod_bot.
1005 * - if wp->w_redraw_top non-zero, redraw lines between
1006 * wp->w_redraw_top and wp->w_redr_bot.
1007 * - continue redrawing when syntax status is invalid.
1008 * 4. if scrolled up, update lines at the bottom.
1009 * This results in three areas that may need updating:
1010 * top: from first row to top_end (when scrolled down)
1011 * mid: from mid_start to mid_end (update inversion or changed text)
1012 * bot: from bot_start to last row (when scrolled up)
1013 */
1014 static void
1015win_update(wp)
1016 win_T *wp;
1017{
1018 buf_T *buf = wp->w_buffer;
1019 int type;
1020 int top_end = 0; /* Below last row of the top area that needs
1021 updating. 0 when no top area updating. */
1022 int mid_start = 999;/* first row of the mid area that needs
1023 updating. 999 when no mid area updating. */
1024 int mid_end = 0; /* Below last row of the mid area that needs
1025 updating. 0 when no mid area updating. */
1026 int bot_start = 999;/* first row of the bot area that needs
1027 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 int scrolled_down = FALSE; /* TRUE when scrolled down when
1029 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001031 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 int top_to_mod = FALSE; /* redraw above mod_top */
1033#endif
1034
1035 int row; /* current window row to display */
1036 linenr_T lnum; /* current buffer lnum to display */
1037 int idx; /* current index in w_lines[] */
1038 int srow; /* starting row of the current line */
1039
1040 int eof = FALSE; /* if TRUE, we hit the end of the file */
1041 int didline = FALSE; /* if TRUE, we finished the last line */
1042 int i;
1043 long j;
1044 static int recursive = FALSE; /* being called recursively */
1045 int old_botline = wp->w_botline;
1046#ifdef FEAT_FOLDING
1047 long fold_count;
1048#endif
1049#ifdef FEAT_SYN_HL
1050 /* remember what happened to the previous line, to know if
1051 * check_visual_highlight() can be used */
1052#define DID_NONE 1 /* didn't update a line */
1053#define DID_LINE 2 /* updated a normal line */
1054#define DID_FOLD 3 /* updated a folded line */
1055 int did_update = DID_NONE;
1056 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1057#endif
1058 linenr_T mod_top = 0;
1059 linenr_T mod_bot = 0;
1060#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1061 int save_got_int;
1062#endif
1063
1064 type = wp->w_redr_type;
1065
1066 if (type == NOT_VALID)
1067 {
1068#ifdef FEAT_WINDOWS
1069 wp->w_redr_status = TRUE;
1070#endif
1071 wp->w_lines_valid = 0;
1072 }
1073
1074 /* Window is zero-height: nothing to draw. */
1075 if (wp->w_height == 0)
1076 {
1077 wp->w_redr_type = 0;
1078 return;
1079 }
1080
1081#ifdef FEAT_VERTSPLIT
1082 /* Window is zero-width: Only need to draw the separator. */
1083 if (wp->w_width == 0)
1084 {
1085 /* draw the vertical separator right of this window */
1086 draw_vsep_win(wp, 0);
1087 wp->w_redr_type = 0;
1088 return;
1089 }
1090#endif
1091
1092#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001093 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#endif
1095
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001096#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001097 /* Force redraw when width of 'number' or 'relativenumber' column
1098 * changes. */
1099 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001100 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001101 {
1102 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001103 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001104 }
1105 else
1106#endif
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1109 {
1110 /*
1111 * When there are both inserted/deleted lines and specific lines to be
1112 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1113 * everything (only happens when redrawing is off for while).
1114 */
1115 type = NOT_VALID;
1116 }
1117 else
1118 {
1119 /*
1120 * Set mod_top to the first line that needs displaying because of
1121 * changes. Set mod_bot to the first line after the changes.
1122 */
1123 mod_top = wp->w_redraw_top;
1124 if (wp->w_redraw_bot != 0)
1125 mod_bot = wp->w_redraw_bot + 1;
1126 else
1127 mod_bot = 0;
1128 wp->w_redraw_top = 0; /* reset for next time */
1129 wp->w_redraw_bot = 0;
1130 if (buf->b_mod_set)
1131 {
1132 if (mod_top == 0 || mod_top > buf->b_mod_top)
1133 {
1134 mod_top = buf->b_mod_top;
1135#ifdef FEAT_SYN_HL
1136 /* Need to redraw lines above the change that may be included
1137 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001138 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001140 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (mod_top < 1)
1142 mod_top = 1;
1143 }
1144#endif
1145 }
1146 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1147 mod_bot = buf->b_mod_bot;
1148
1149#ifdef FEAT_SEARCH_EXTRA
1150 /* When 'hlsearch' is on and using a multi-line search pattern, a
1151 * change in one line may make the Search highlighting in a
1152 * previous line invalid. Simple solution: redraw all visible
1153 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001154 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001156 if (search_hl.rm.regprog != NULL
1157 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001159 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001160 {
1161 cur = wp->w_match_head;
1162 while (cur != NULL)
1163 {
1164 if (cur->match.regprog != NULL
1165 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001166 {
1167 top_to_mod = TRUE;
1168 break;
1169 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001170 cur = cur->next;
1171 }
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 }
1175#ifdef FEAT_FOLDING
1176 if (mod_top != 0 && hasAnyFolding(wp))
1177 {
1178 linenr_T lnumt, lnumb;
1179
1180 /*
1181 * A change in a line can cause lines above it to become folded or
1182 * unfolded. Find the top most buffer line that may be affected.
1183 * If the line was previously folded and displayed, get the first
1184 * line of that fold. If the line is folded now, get the first
1185 * folded line. Use the minimum of these two.
1186 */
1187
1188 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1189 * the line below it. If there is no valid entry, use w_topline.
1190 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1191 * to this line. If there is no valid entry, use MAXLNUM. */
1192 lnumt = wp->w_topline;
1193 lnumb = MAXLNUM;
1194 for (i = 0; i < wp->w_lines_valid; ++i)
1195 if (wp->w_lines[i].wl_valid)
1196 {
1197 if (wp->w_lines[i].wl_lastlnum < mod_top)
1198 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1199 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1200 {
1201 lnumb = wp->w_lines[i].wl_lnum;
1202 /* When there is a fold column it might need updating
1203 * in the next line ("J" just above an open fold). */
1204 if (wp->w_p_fdc > 0)
1205 ++lnumb;
1206 }
1207 }
1208
1209 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1210 if (mod_top > lnumt)
1211 mod_top = lnumt;
1212
1213 /* Now do the same for the bottom line (one above mod_bot). */
1214 --mod_bot;
1215 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1216 ++mod_bot;
1217 if (mod_bot < lnumb)
1218 mod_bot = lnumb;
1219 }
1220#endif
1221
1222 /* When a change starts above w_topline and the end is below
1223 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224 * If the end of the change is above w_topline: do like no change was
1225 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (mod_top != 0 && mod_top < wp->w_topline)
1227 {
1228 if (mod_bot > wp->w_topline)
1229 mod_top = wp->w_topline;
1230#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001231 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 top_end = 1;
1233#endif
1234 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001235
1236 /* When line numbers are displayed need to redraw all lines below
1237 * inserted/deleted lines. */
1238 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1239 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241
1242 /*
1243 * When only displaying the lines at the top, set top_end. Used when
1244 * window has scrolled down for msg_scrolled.
1245 */
1246 if (type == REDRAW_TOP)
1247 {
1248 j = 0;
1249 for (i = 0; i < wp->w_lines_valid; ++i)
1250 {
1251 j += wp->w_lines[i].wl_size;
1252 if (j >= wp->w_upd_rows)
1253 {
1254 top_end = j;
1255 break;
1256 }
1257 }
1258 if (top_end == 0)
1259 /* not found (cannot happen?): redraw everything */
1260 type = NOT_VALID;
1261 else
1262 /* top area defined, the rest is VALID */
1263 type = VALID;
1264 }
1265
Bram Moolenaar367329b2007-08-30 11:53:22 +00001266 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001267 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1268 * non-zero and thus not FALSE) will indicate that screenclear() was not
1269 * called. */
1270 if (screen_cleared)
1271 screen_cleared = MAYBE;
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 /*
1274 * If there are no changes on the screen that require a complete redraw,
1275 * handle three cases:
1276 * 1: we are off the top of the screen by a few lines: scroll down
1277 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1278 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1279 * w_lines[] that needs updating.
1280 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001281 if ((type == VALID || type == SOME_VALID
1282 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283#ifdef FEAT_DIFF
1284 && !wp->w_botfill && !wp->w_old_botfill
1285#endif
1286 )
1287 {
1288 if (mod_top != 0 && wp->w_topline == mod_top)
1289 {
1290 /*
1291 * w_topline is the first changed line, the scrolling will be done
1292 * further down.
1293 */
1294 }
1295 else if (wp->w_lines[0].wl_valid
1296 && (wp->w_topline < wp->w_lines[0].wl_lnum
1297#ifdef FEAT_DIFF
1298 || (wp->w_topline == wp->w_lines[0].wl_lnum
1299 && wp->w_topfill > wp->w_old_topfill)
1300#endif
1301 ))
1302 {
1303 /*
1304 * New topline is above old topline: May scroll down.
1305 */
1306#ifdef FEAT_FOLDING
1307 if (hasAnyFolding(wp))
1308 {
1309 linenr_T ln;
1310
1311 /* count the number of lines we are off, counting a sequence
1312 * of folded lines as one */
1313 j = 0;
1314 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1315 {
1316 ++j;
1317 if (j >= wp->w_height - 2)
1318 break;
1319 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1320 }
1321 }
1322 else
1323#endif
1324 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1325 if (j < wp->w_height - 2) /* not too far off */
1326 {
1327 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1328#ifdef FEAT_DIFF
1329 /* insert extra lines for previously invisible filler lines */
1330 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1331 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1332 - wp->w_old_topfill;
1333#endif
1334 if (i < wp->w_height - 2) /* less than a screen off */
1335 {
1336 /*
1337 * Try to insert the correct number of lines.
1338 * If not the last window, delete the lines at the bottom.
1339 * win_ins_lines may fail when the terminal can't do it.
1340 */
1341 if (i > 0)
1342 check_for_delay(FALSE);
1343 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1344 {
1345 if (wp->w_lines_valid != 0)
1346 {
1347 /* Need to update rows that are new, stop at the
1348 * first one that scrolled down. */
1349 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352 /* Move the entries that were scrolled, disable
1353 * the entries for the lines to be redrawn. */
1354 if ((wp->w_lines_valid += j) > wp->w_height)
1355 wp->w_lines_valid = wp->w_height;
1356 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1357 wp->w_lines[idx] = wp->w_lines[idx - j];
1358 while (idx >= 0)
1359 wp->w_lines[idx--].wl_valid = FALSE;
1360 }
1361 }
1362 else
1363 mid_start = 0; /* redraw all lines */
1364 }
1365 else
1366 mid_start = 0; /* redraw all lines */
1367 }
1368 else
1369 mid_start = 0; /* redraw all lines */
1370 }
1371 else
1372 {
1373 /*
1374 * New topline is at or below old topline: May scroll up.
1375 * When topline didn't change, find first entry in w_lines[] that
1376 * needs updating.
1377 */
1378
1379 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1380 j = -1;
1381 row = 0;
1382 for (i = 0; i < wp->w_lines_valid; i++)
1383 {
1384 if (wp->w_lines[i].wl_valid
1385 && wp->w_lines[i].wl_lnum == wp->w_topline)
1386 {
1387 j = i;
1388 break;
1389 }
1390 row += wp->w_lines[i].wl_size;
1391 }
1392 if (j == -1)
1393 {
1394 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1395 * lines */
1396 mid_start = 0;
1397 }
1398 else
1399 {
1400 /*
1401 * Try to delete the correct number of lines.
1402 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1403 */
1404#ifdef FEAT_DIFF
1405 /* If the topline didn't change, delete old filler lines,
1406 * otherwise delete filler lines of the new topline... */
1407 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1408 row += wp->w_old_topfill;
1409 else
1410 row += diff_check_fill(wp, wp->w_topline);
1411 /* ... but don't delete new filler lines. */
1412 row -= wp->w_topfill;
1413#endif
1414 if (row > 0)
1415 {
1416 check_for_delay(FALSE);
1417 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1418 bot_start = wp->w_height - row;
1419 else
1420 mid_start = 0; /* redraw all lines */
1421 }
1422 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1423 {
1424 /*
1425 * Skip the lines (below the deleted lines) that are still
1426 * valid and don't need redrawing. Copy their info
1427 * upwards, to compensate for the deleted lines. Set
1428 * bot_start to the first row that needs redrawing.
1429 */
1430 bot_start = 0;
1431 idx = 0;
1432 for (;;)
1433 {
1434 wp->w_lines[idx] = wp->w_lines[j];
1435 /* stop at line that didn't fit, unless it is still
1436 * valid (no lines deleted) */
1437 if (row > 0 && bot_start + row
1438 + (int)wp->w_lines[j].wl_size > wp->w_height)
1439 {
1440 wp->w_lines_valid = idx + 1;
1441 break;
1442 }
1443 bot_start += wp->w_lines[idx++].wl_size;
1444
1445 /* stop at the last valid entry in w_lines[].wl_size */
1446 if (++j >= wp->w_lines_valid)
1447 {
1448 wp->w_lines_valid = idx;
1449 break;
1450 }
1451 }
1452#ifdef FEAT_DIFF
1453 /* Correct the first entry for filler lines at the top
1454 * when it won't get updated below. */
1455 if (wp->w_p_diff && bot_start > 0)
1456 wp->w_lines[0].wl_size =
1457 plines_win_nofill(wp, wp->w_topline, TRUE)
1458 + wp->w_topfill;
1459#endif
1460 }
1461 }
1462 }
1463
1464 /* When starting redraw in the first line, redraw all lines. When
1465 * there is only one window it's probably faster to clear the screen
1466 * first. */
1467 if (mid_start == 0)
1468 {
1469 mid_end = wp->w_height;
1470 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001471 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001472 /* Clear the screen when it was not done by win_del_lines() or
1473 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1474 * then. */
1475 if (screen_cleared != TRUE)
1476 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001477#ifdef FEAT_WINDOWS
1478 /* The screen was cleared, redraw the tab pages line. */
1479 if (redraw_tabline)
1480 draw_tabline();
1481#endif
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001484
1485 /* When win_del_lines() or win_ins_lines() caused the screen to be
1486 * cleared (only happens for the first window) or when screenclear()
1487 * was called directly above, "must_redraw" will have been set to
1488 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1489 if (screen_cleared == TRUE)
1490 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492 else
1493 {
1494 /* Not VALID or INVERTED: redraw all lines. */
1495 mid_start = 0;
1496 mid_end = wp->w_height;
1497 }
1498
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001499 if (type == SOME_VALID)
1500 {
1501 /* SOME_VALID: redraw all lines. */
1502 mid_start = 0;
1503 mid_end = wp->w_height;
1504 type = NOT_VALID;
1505 }
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 /* check if we are updating or removing the inverted part */
1508 if ((VIsual_active && buf == curwin->w_buffer)
1509 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1510 {
1511 linenr_T from, to;
1512
1513 if (VIsual_active)
1514 {
1515 if (VIsual_active
1516 && (VIsual_mode != wp->w_old_visual_mode
1517 || type == INVERTED_ALL))
1518 {
1519 /*
1520 * If the type of Visual selection changed, redraw the whole
1521 * selection. Also when the ownership of the X selection is
1522 * gained or lost.
1523 */
1524 if (curwin->w_cursor.lnum < VIsual.lnum)
1525 {
1526 from = curwin->w_cursor.lnum;
1527 to = VIsual.lnum;
1528 }
1529 else
1530 {
1531 from = VIsual.lnum;
1532 to = curwin->w_cursor.lnum;
1533 }
1534 /* redraw more when the cursor moved as well */
1535 if (wp->w_old_cursor_lnum < from)
1536 from = wp->w_old_cursor_lnum;
1537 if (wp->w_old_cursor_lnum > to)
1538 to = wp->w_old_cursor_lnum;
1539 if (wp->w_old_visual_lnum < from)
1540 from = wp->w_old_visual_lnum;
1541 if (wp->w_old_visual_lnum > to)
1542 to = wp->w_old_visual_lnum;
1543 }
1544 else
1545 {
1546 /*
1547 * Find the line numbers that need to be updated: The lines
1548 * between the old cursor position and the current cursor
1549 * position. Also check if the Visual position changed.
1550 */
1551 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1552 {
1553 from = curwin->w_cursor.lnum;
1554 to = wp->w_old_cursor_lnum;
1555 }
1556 else
1557 {
1558 from = wp->w_old_cursor_lnum;
1559 to = curwin->w_cursor.lnum;
1560 if (from == 0) /* Visual mode just started */
1561 from = to;
1562 }
1563
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001564 if (VIsual.lnum != wp->w_old_visual_lnum
1565 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 if (wp->w_old_visual_lnum < from
1568 && wp->w_old_visual_lnum != 0)
1569 from = wp->w_old_visual_lnum;
1570 if (wp->w_old_visual_lnum > to)
1571 to = wp->w_old_visual_lnum;
1572 if (VIsual.lnum < from)
1573 from = VIsual.lnum;
1574 if (VIsual.lnum > to)
1575 to = VIsual.lnum;
1576 }
1577 }
1578
1579 /*
1580 * If in block mode and changed column or curwin->w_curswant:
1581 * update all lines.
1582 * First compute the actual start and end column.
1583 */
1584 if (VIsual_mode == Ctrl_V)
1585 {
1586 colnr_T fromc, toc;
1587
1588 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1589 ++toc;
1590 if (curwin->w_curswant == MAXCOL)
1591 toc = MAXCOL;
1592
1593 if (fromc != wp->w_old_cursor_fcol
1594 || toc != wp->w_old_cursor_lcol)
1595 {
1596 if (from > VIsual.lnum)
1597 from = VIsual.lnum;
1598 if (to < VIsual.lnum)
1599 to = VIsual.lnum;
1600 }
1601 wp->w_old_cursor_fcol = fromc;
1602 wp->w_old_cursor_lcol = toc;
1603 }
1604 }
1605 else
1606 {
1607 /* Use the line numbers of the old Visual area. */
1608 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1609 {
1610 from = wp->w_old_cursor_lnum;
1611 to = wp->w_old_visual_lnum;
1612 }
1613 else
1614 {
1615 from = wp->w_old_visual_lnum;
1616 to = wp->w_old_cursor_lnum;
1617 }
1618 }
1619
1620 /*
1621 * There is no need to update lines above the top of the window.
1622 */
1623 if (from < wp->w_topline)
1624 from = wp->w_topline;
1625
1626 /*
1627 * If we know the value of w_botline, use it to restrict the update to
1628 * the lines that are visible in the window.
1629 */
1630 if (wp->w_valid & VALID_BOTLINE)
1631 {
1632 if (from >= wp->w_botline)
1633 from = wp->w_botline - 1;
1634 if (to >= wp->w_botline)
1635 to = wp->w_botline - 1;
1636 }
1637
1638 /*
1639 * Find the minimal part to be updated.
1640 * Watch out for scrolling that made entries in w_lines[] invalid.
1641 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1642 * top_end; need to redraw from top_end to the "to" line.
1643 * A middle mouse click with a Visual selection may change the text
1644 * above the Visual area and reset wl_valid, do count these for
1645 * mid_end (in srow).
1646 */
1647 if (mid_start > 0)
1648 {
1649 lnum = wp->w_topline;
1650 idx = 0;
1651 srow = 0;
1652 if (scrolled_down)
1653 mid_start = top_end;
1654 else
1655 mid_start = 0;
1656 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1657 {
1658 if (wp->w_lines[idx].wl_valid)
1659 mid_start += wp->w_lines[idx].wl_size;
1660 else if (!scrolled_down)
1661 srow += wp->w_lines[idx].wl_size;
1662 ++idx;
1663# ifdef FEAT_FOLDING
1664 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1665 lnum = wp->w_lines[idx].wl_lnum;
1666 else
1667# endif
1668 ++lnum;
1669 }
1670 srow += mid_start;
1671 mid_end = wp->w_height;
1672 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1673 {
1674 if (wp->w_lines[idx].wl_valid
1675 && wp->w_lines[idx].wl_lnum >= to + 1)
1676 {
1677 /* Only update until first row of this line */
1678 mid_end = srow;
1679 break;
1680 }
1681 srow += wp->w_lines[idx].wl_size;
1682 }
1683 }
1684 }
1685
1686 if (VIsual_active && buf == curwin->w_buffer)
1687 {
1688 wp->w_old_visual_mode = VIsual_mode;
1689 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1690 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001691 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 wp->w_old_curswant = curwin->w_curswant;
1693 }
1694 else
1695 {
1696 wp->w_old_visual_mode = 0;
1697 wp->w_old_cursor_lnum = 0;
1698 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001699 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1703 /* reset got_int, otherwise regexp won't work */
1704 save_got_int = got_int;
1705 got_int = 0;
1706#endif
1707#ifdef FEAT_FOLDING
1708 win_foldinfo.fi_level = 0;
1709#endif
1710
1711 /*
1712 * Update all the window rows.
1713 */
1714 idx = 0; /* first entry in w_lines[].wl_size */
1715 row = 0;
1716 srow = 0;
1717 lnum = wp->w_topline; /* first line shown in window */
1718 for (;;)
1719 {
1720 /* stop updating when reached the end of the window (check for _past_
1721 * the end of the window is at the end of the loop) */
1722 if (row == wp->w_height)
1723 {
1724 didline = TRUE;
1725 break;
1726 }
1727
1728 /* stop updating when hit the end of the file */
1729 if (lnum > buf->b_ml.ml_line_count)
1730 {
1731 eof = TRUE;
1732 break;
1733 }
1734
1735 /* Remember the starting row of the line that is going to be dealt
1736 * with. It is used further down when the line doesn't fit. */
1737 srow = row;
1738
1739 /*
1740 * Update a line when it is in an area that needs updating, when it
1741 * has changes or w_lines[idx] is invalid.
1742 * bot_start may be halfway a wrapped line after using
1743 * win_del_lines(), check if the current line includes it.
1744 * When syntax folding is being used, the saved syntax states will
1745 * already have been updated, we can't see where the syntax state is
1746 * the same again, just update until the end of the window.
1747 */
1748 if (row < top_end
1749 || (row >= mid_start && row < mid_end)
1750#ifdef FEAT_SEARCH_EXTRA
1751 || top_to_mod
1752#endif
1753 || idx >= wp->w_lines_valid
1754 || (row + wp->w_lines[idx].wl_size > bot_start)
1755 || (mod_top != 0
1756 && (lnum == mod_top
1757 || (lnum >= mod_top
1758 && (lnum < mod_bot
1759#ifdef FEAT_SYN_HL
1760 || did_update == DID_FOLD
1761 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001762 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 && (
1764# ifdef FEAT_FOLDING
1765 (foldmethodIsSyntax(wp)
1766 && hasAnyFolding(wp)) ||
1767# endif
1768 syntax_check_changed(lnum)))
1769#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001770#ifdef FEAT_SEARCH_EXTRA
1771 /* match in fixed position might need redraw */
1772 || wp->w_match_head != NULL
1773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 )))))
1775 {
1776#ifdef FEAT_SEARCH_EXTRA
1777 if (lnum == mod_top)
1778 top_to_mod = FALSE;
1779#endif
1780
1781 /*
1782 * When at start of changed lines: May scroll following lines
1783 * up or down to minimize redrawing.
1784 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001785 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
1787 if (lnum == mod_top
1788 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001789 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
1791 int old_rows = 0;
1792 int new_rows = 0;
1793 int xtra_rows;
1794 linenr_T l;
1795
1796 /* Count the old number of window rows, using w_lines[], which
1797 * should still contain the sizes for the lines as they are
1798 * currently displayed. */
1799 for (i = idx; i < wp->w_lines_valid; ++i)
1800 {
1801 /* Only valid lines have a meaningful wl_lnum. Invalid
1802 * lines are part of the changed area. */
1803 if (wp->w_lines[i].wl_valid
1804 && wp->w_lines[i].wl_lnum == mod_bot)
1805 break;
1806 old_rows += wp->w_lines[i].wl_size;
1807#ifdef FEAT_FOLDING
1808 if (wp->w_lines[i].wl_valid
1809 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1810 {
1811 /* Must have found the last valid entry above mod_bot.
1812 * Add following invalid entries. */
1813 ++i;
1814 while (i < wp->w_lines_valid
1815 && !wp->w_lines[i].wl_valid)
1816 old_rows += wp->w_lines[i++].wl_size;
1817 break;
1818 }
1819#endif
1820 }
1821
1822 if (i >= wp->w_lines_valid)
1823 {
1824 /* We can't find a valid line below the changed lines,
1825 * need to redraw until the end of the window.
1826 * Inserting/deleting lines has no use. */
1827 bot_start = 0;
1828 }
1829 else
1830 {
1831 /* Able to count old number of rows: Count new window
1832 * rows, and may insert/delete lines */
1833 j = idx;
1834 for (l = lnum; l < mod_bot; ++l)
1835 {
1836#ifdef FEAT_FOLDING
1837 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1838 ++new_rows;
1839 else
1840#endif
1841#ifdef FEAT_DIFF
1842 if (l == wp->w_topline)
1843 new_rows += plines_win_nofill(wp, l, TRUE)
1844 + wp->w_topfill;
1845 else
1846#endif
1847 new_rows += plines_win(wp, l, TRUE);
1848 ++j;
1849 if (new_rows > wp->w_height - row - 2)
1850 {
1851 /* it's getting too much, must redraw the rest */
1852 new_rows = 9999;
1853 break;
1854 }
1855 }
1856 xtra_rows = new_rows - old_rows;
1857 if (xtra_rows < 0)
1858 {
1859 /* May scroll text up. If there is not enough
1860 * remaining text or scrolling fails, must redraw the
1861 * rest. If scrolling works, must redraw the text
1862 * below the scrolled text. */
1863 if (row - xtra_rows >= wp->w_height - 2)
1864 mod_bot = MAXLNUM;
1865 else
1866 {
1867 check_for_delay(FALSE);
1868 if (win_del_lines(wp, row,
1869 -xtra_rows, FALSE, FALSE) == FAIL)
1870 mod_bot = MAXLNUM;
1871 else
1872 bot_start = wp->w_height + xtra_rows;
1873 }
1874 }
1875 else if (xtra_rows > 0)
1876 {
1877 /* May scroll text down. If there is not enough
1878 * remaining text of scrolling fails, must redraw the
1879 * rest. */
1880 if (row + xtra_rows >= wp->w_height - 2)
1881 mod_bot = MAXLNUM;
1882 else
1883 {
1884 check_for_delay(FALSE);
1885 if (win_ins_lines(wp, row + old_rows,
1886 xtra_rows, FALSE, FALSE) == FAIL)
1887 mod_bot = MAXLNUM;
1888 else if (top_end > row + old_rows)
1889 /* Scrolled the part at the top that requires
1890 * updating down. */
1891 top_end += xtra_rows;
1892 }
1893 }
1894
1895 /* When not updating the rest, may need to move w_lines[]
1896 * entries. */
1897 if (mod_bot != MAXLNUM && i != j)
1898 {
1899 if (j < i)
1900 {
1901 int x = row + new_rows;
1902
1903 /* move entries in w_lines[] upwards */
1904 for (;;)
1905 {
1906 /* stop at last valid entry in w_lines[] */
1907 if (i >= wp->w_lines_valid)
1908 {
1909 wp->w_lines_valid = j;
1910 break;
1911 }
1912 wp->w_lines[j] = wp->w_lines[i];
1913 /* stop at a line that won't fit */
1914 if (x + (int)wp->w_lines[j].wl_size
1915 > wp->w_height)
1916 {
1917 wp->w_lines_valid = j + 1;
1918 break;
1919 }
1920 x += wp->w_lines[j++].wl_size;
1921 ++i;
1922 }
1923 if (bot_start > x)
1924 bot_start = x;
1925 }
1926 else /* j > i */
1927 {
1928 /* move entries in w_lines[] downwards */
1929 j -= i;
1930 wp->w_lines_valid += j;
1931 if (wp->w_lines_valid > wp->w_height)
1932 wp->w_lines_valid = wp->w_height;
1933 for (i = wp->w_lines_valid; i - j >= idx; --i)
1934 wp->w_lines[i] = wp->w_lines[i - j];
1935
1936 /* The w_lines[] entries for inserted lines are
1937 * now invalid, but wl_size may be used above.
1938 * Reset to zero. */
1939 while (i >= idx)
1940 {
1941 wp->w_lines[i].wl_size = 0;
1942 wp->w_lines[i--].wl_valid = FALSE;
1943 }
1944 }
1945 }
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948
1949#ifdef FEAT_FOLDING
1950 /*
1951 * When lines are folded, display one line for all of them.
1952 * Otherwise, display normally (can be several display lines when
1953 * 'wrap' is on).
1954 */
1955 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1956 if (fold_count != 0)
1957 {
1958 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1959 ++row;
1960 --fold_count;
1961 wp->w_lines[idx].wl_folded = TRUE;
1962 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1963# ifdef FEAT_SYN_HL
1964 did_update = DID_FOLD;
1965# endif
1966 }
1967 else
1968#endif
1969 if (idx < wp->w_lines_valid
1970 && wp->w_lines[idx].wl_valid
1971 && wp->w_lines[idx].wl_lnum == lnum
1972 && lnum > wp->w_topline
1973 && !(dy_flags & DY_LASTLINE)
1974 && srow + wp->w_lines[idx].wl_size > wp->w_height
1975#ifdef FEAT_DIFF
1976 && diff_check_fill(wp, lnum) == 0
1977#endif
1978 )
1979 {
1980 /* This line is not going to fit. Don't draw anything here,
1981 * will draw "@ " lines below. */
1982 row = wp->w_height + 1;
1983 }
1984 else
1985 {
1986#ifdef FEAT_SEARCH_EXTRA
1987 prepare_search_hl(wp, lnum);
1988#endif
1989#ifdef FEAT_SYN_HL
1990 /* Let the syntax stuff know we skipped a few lines. */
1991 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02001992 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 syntax_end_parsing(syntax_last_parsed + 1);
1994#endif
1995
1996 /*
1997 * Display one line.
1998 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001999 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001#ifdef FEAT_FOLDING
2002 wp->w_lines[idx].wl_folded = FALSE;
2003 wp->w_lines[idx].wl_lastlnum = lnum;
2004#endif
2005#ifdef FEAT_SYN_HL
2006 did_update = DID_LINE;
2007 syntax_last_parsed = lnum;
2008#endif
2009 }
2010
2011 wp->w_lines[idx].wl_lnum = lnum;
2012 wp->w_lines[idx].wl_valid = TRUE;
2013 if (row > wp->w_height) /* past end of screen */
2014 {
2015 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002016 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2018 ++idx;
2019 break;
2020 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002021 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 wp->w_lines[idx].wl_size = row - srow;
2023 ++idx;
2024#ifdef FEAT_FOLDING
2025 lnum += fold_count + 1;
2026#else
2027 ++lnum;
2028#endif
2029 }
2030 else
2031 {
2032 /* This line does not need updating, advance to the next one */
2033 row += wp->w_lines[idx++].wl_size;
2034 if (row > wp->w_height) /* past end of screen */
2035 break;
2036#ifdef FEAT_FOLDING
2037 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2038#else
2039 ++lnum;
2040#endif
2041#ifdef FEAT_SYN_HL
2042 did_update = DID_NONE;
2043#endif
2044 }
2045
2046 if (lnum > buf->b_ml.ml_line_count)
2047 {
2048 eof = TRUE;
2049 break;
2050 }
2051 }
2052 /*
2053 * End of loop over all window lines.
2054 */
2055
2056
2057 if (idx > wp->w_lines_valid)
2058 wp->w_lines_valid = idx;
2059
2060#ifdef FEAT_SYN_HL
2061 /*
2062 * Let the syntax stuff know we stop parsing here.
2063 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002064 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 syntax_end_parsing(syntax_last_parsed + 1);
2066#endif
2067
2068 /*
2069 * If we didn't hit the end of the file, and we didn't finish the last
2070 * line we were working on, then the line didn't fit.
2071 */
2072 wp->w_empty_rows = 0;
2073#ifdef FEAT_DIFF
2074 wp->w_filler_rows = 0;
2075#endif
2076 if (!eof && !didline)
2077 {
2078 if (lnum == wp->w_topline)
2079 {
2080 /*
2081 * Single line that does not fit!
2082 * Don't overwrite it, it can be edited.
2083 */
2084 wp->w_botline = lnum + 1;
2085 }
2086#ifdef FEAT_DIFF
2087 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2088 {
2089 /* Window ends in filler lines. */
2090 wp->w_botline = lnum;
2091 wp->w_filler_rows = wp->w_height - srow;
2092 }
2093#endif
2094 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2095 {
2096 /*
2097 * Last line isn't finished: Display "@@@" at the end.
2098 */
2099 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2100 W_WINROW(wp) + wp->w_height,
2101 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2102 '@', '@', hl_attr(HLF_AT));
2103 set_empty_rows(wp, srow);
2104 wp->w_botline = lnum;
2105 }
2106 else
2107 {
2108 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2109 wp->w_botline = lnum;
2110 }
2111 }
2112 else
2113 {
2114#ifdef FEAT_VERTSPLIT
2115 draw_vsep_win(wp, row);
2116#endif
2117 if (eof) /* we hit the end of the file */
2118 {
2119 wp->w_botline = buf->b_ml.ml_line_count + 1;
2120#ifdef FEAT_DIFF
2121 j = diff_check_fill(wp, wp->w_botline);
2122 if (j > 0 && !wp->w_botfill)
2123 {
2124 /*
2125 * Display filler lines at the end of the file
2126 */
2127 if (char2cells(fill_diff) > 1)
2128 i = '-';
2129 else
2130 i = fill_diff;
2131 if (row + j > wp->w_height)
2132 j = wp->w_height - row;
2133 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2134 row += j;
2135 }
2136#endif
2137 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002138 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 wp->w_botline = lnum;
2140
2141 /* make sure the rest of the screen is blank */
2142 /* put '~'s on rows that aren't part of the file. */
2143 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2144 }
2145
2146 /* Reset the type of redrawing required, the window has been updated. */
2147 wp->w_redr_type = 0;
2148#ifdef FEAT_DIFF
2149 wp->w_old_topfill = wp->w_topfill;
2150 wp->w_old_botfill = wp->w_botfill;
2151#endif
2152
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002153 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 /*
2156 * There is a trick with w_botline. If we invalidate it on each
2157 * change that might modify it, this will cause a lot of expensive
2158 * calls to plines() in update_topline() each time. Therefore the
2159 * value of w_botline is often approximated, and this value is used to
2160 * compute the value of w_topline. If the value of w_botline was
2161 * wrong, check that the value of w_topline is correct (cursor is on
2162 * the visible part of the text). If it's not, we need to redraw
2163 * again. Mostly this just means scrolling up a few lines, so it
2164 * doesn't look too bad. Only do this for the current window (where
2165 * changes are relevant).
2166 */
2167 wp->w_valid |= VALID_BOTLINE;
2168 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2169 {
2170 recursive = TRUE;
2171 curwin->w_valid &= ~VALID_TOPLINE;
2172 update_topline(); /* may invalidate w_botline again */
2173 if (must_redraw != 0)
2174 {
2175 /* Don't update for changes in buffer again. */
2176 i = curbuf->b_mod_set;
2177 curbuf->b_mod_set = FALSE;
2178 win_update(curwin);
2179 must_redraw = 0;
2180 curbuf->b_mod_set = i;
2181 }
2182 recursive = FALSE;
2183 }
2184 }
2185
2186#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2187 /* restore got_int, unless CTRL-C was hit while redrawing */
2188 if (!got_int)
2189 got_int = save_got_int;
2190#endif
2191}
2192
2193#ifdef FEAT_SIGNS
2194static int draw_signcolumn __ARGS((win_T *wp));
2195
2196/*
2197 * Return TRUE when window "wp" has a column to draw signs in.
2198 */
2199 static int
2200draw_signcolumn(wp)
2201 win_T *wp;
2202{
2203 return (wp->w_buffer->b_signlist != NULL
2204# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002205 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206# endif
2207 );
2208}
2209#endif
2210
2211/*
2212 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2213 * as the filler character.
2214 */
2215 static void
2216win_draw_end(wp, c1, c2, row, endrow, hl)
2217 win_T *wp;
2218 int c1;
2219 int c2;
2220 int row;
2221 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002222 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2225 int n = 0;
2226# define FDC_OFF n
2227#else
2228# define FDC_OFF 0
2229#endif
2230
2231#ifdef FEAT_RIGHTLEFT
2232 if (wp->w_p_rl)
2233 {
2234 /* No check for cmdline window: should never be right-left. */
2235# ifdef FEAT_FOLDING
2236 n = wp->w_p_fdc;
2237
2238 if (n > 0)
2239 {
2240 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002241 if (n > W_WIDTH(wp))
2242 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2244 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2245 ' ', ' ', hl_attr(HLF_FC));
2246 }
2247# endif
2248# ifdef FEAT_SIGNS
2249 if (draw_signcolumn(wp))
2250 {
2251 int nn = n + 2;
2252
2253 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002254 if (nn > W_WIDTH(wp))
2255 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2257 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2258 ' ', ' ', hl_attr(HLF_SC));
2259 n = nn;
2260 }
2261# endif
2262 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2263 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2264 c2, c2, hl_attr(hl));
2265 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2266 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2267 c1, c2, hl_attr(hl));
2268 }
2269 else
2270#endif
2271 {
2272#ifdef FEAT_CMDWIN
2273 if (cmdwin_type != 0 && wp == curwin)
2274 {
2275 /* draw the cmdline character in the leftmost column */
2276 n = 1;
2277 if (n > wp->w_width)
2278 n = wp->w_width;
2279 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2280 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2281 cmdwin_type, ' ', hl_attr(HLF_AT));
2282 }
2283#endif
2284#ifdef FEAT_FOLDING
2285 if (wp->w_p_fdc > 0)
2286 {
2287 int nn = n + wp->w_p_fdc;
2288
2289 /* draw the fold column at the left */
2290 if (nn > W_WIDTH(wp))
2291 nn = W_WIDTH(wp);
2292 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2293 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2294 ' ', ' ', hl_attr(HLF_FC));
2295 n = nn;
2296 }
2297#endif
2298#ifdef FEAT_SIGNS
2299 if (draw_signcolumn(wp))
2300 {
2301 int nn = n + 2;
2302
2303 /* draw the sign column after the fold column */
2304 if (nn > W_WIDTH(wp))
2305 nn = W_WIDTH(wp);
2306 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2307 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2308 ' ', ' ', hl_attr(HLF_SC));
2309 n = nn;
2310 }
2311#endif
2312 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2313 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2314 c1, c2, hl_attr(hl));
2315 }
2316 set_empty_rows(wp, row);
2317}
2318
Bram Moolenaar1a384422010-07-14 19:53:30 +02002319#ifdef FEAT_SYN_HL
2320static int advance_color_col __ARGS((int vcol, int **color_cols));
2321
2322/*
2323 * Advance **color_cols and return TRUE when there are columns to draw.
2324 */
2325 static int
2326advance_color_col(vcol, color_cols)
2327 int vcol;
2328 int **color_cols;
2329{
2330 while (**color_cols >= 0 && vcol > **color_cols)
2331 ++*color_cols;
2332 return (**color_cols >= 0);
2333}
2334#endif
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336#ifdef FEAT_FOLDING
2337/*
2338 * Display one folded line.
2339 */
2340 static void
2341fold_line(wp, fold_count, foldinfo, lnum, row)
2342 win_T *wp;
2343 long fold_count;
2344 foldinfo_T *foldinfo;
2345 linenr_T lnum;
2346 int row;
2347{
2348 char_u buf[51];
2349 pos_T *top, *bot;
2350 linenr_T lnume = lnum + fold_count - 1;
2351 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002352 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 int col;
2355 int txtcol;
2356 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002357 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 /* Build the fold line:
2360 * 1. Add the cmdwin_type for the command-line window
2361 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002362 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * 4. Compose the text
2364 * 5. Add the text
2365 * 6. set highlighting for the Visual area an other text
2366 */
2367 col = 0;
2368
2369 /*
2370 * 1. Add the cmdwin_type for the command-line window
2371 * Ignores 'rightleft', this window is never right-left.
2372 */
2373#ifdef FEAT_CMDWIN
2374 if (cmdwin_type != 0 && wp == curwin)
2375 {
2376 ScreenLines[off] = cmdwin_type;
2377 ScreenAttrs[off] = hl_attr(HLF_AT);
2378#ifdef FEAT_MBYTE
2379 if (enc_utf8)
2380 ScreenLinesUC[off] = 0;
2381#endif
2382 ++col;
2383 }
2384#endif
2385
2386 /*
2387 * 2. Add the 'foldcolumn'
2388 */
2389 fdc = wp->w_p_fdc;
2390 if (fdc > W_WIDTH(wp) - col)
2391 fdc = W_WIDTH(wp) - col;
2392 if (fdc > 0)
2393 {
2394 fill_foldcolumn(buf, wp, TRUE, lnum);
2395#ifdef FEAT_RIGHTLEFT
2396 if (wp->w_p_rl)
2397 {
2398 int i;
2399
2400 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2401 hl_attr(HLF_FC));
2402 /* reverse the fold column */
2403 for (i = 0; i < fdc; ++i)
2404 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2405 }
2406 else
2407#endif
2408 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2409 col += fdc;
2410 }
2411
2412#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002413# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2414 for (ri = 0; ri < l; ++ri) \
2415 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2416 else \
2417 for (ri = 0; ri < l; ++ri) \
2418 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002420# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2421 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#endif
2423
Bram Moolenaar64486672010-05-16 15:46:46 +02002424 /* Set all attributes of the 'number' or 'relativenumber' column and the
2425 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002426 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428#ifdef FEAT_SIGNS
2429 /* If signs are being displayed, add two spaces. */
2430 if (draw_signcolumn(wp))
2431 {
2432 len = W_WIDTH(wp) - col;
2433 if (len > 0)
2434 {
2435 if (len > 2)
2436 len = 2;
2437# ifdef FEAT_RIGHTLEFT
2438 if (wp->w_p_rl)
2439 /* the line number isn't reversed */
2440 copy_text_attr(off + W_WIDTH(wp) - len - col,
2441 (char_u *)" ", len, hl_attr(HLF_FL));
2442 else
2443# endif
2444 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2445 col += len;
2446 }
2447 }
2448#endif
2449
2450 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002451 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002453 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455 len = W_WIDTH(wp) - col;
2456 if (len > 0)
2457 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002458 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002459 long num;
2460 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002461
2462 if (len > w + 1)
2463 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002464
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002465 if (wp->w_p_nu && !wp->w_p_rnu)
2466 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002467 num = (long)lnum;
2468 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002469 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002470 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002471 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002472 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002473 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002474 /* 'number' + 'relativenumber': cursor line shows absolute
2475 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002476 num = lnum;
2477 fmt = "%-*ld ";
2478 }
2479 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002480
Bram Moolenaar700e7342013-01-30 12:31:36 +01002481 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482#ifdef FEAT_RIGHTLEFT
2483 if (wp->w_p_rl)
2484 /* the line number isn't reversed */
2485 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2486 hl_attr(HLF_FL));
2487 else
2488#endif
2489 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2490 col += len;
2491 }
2492 }
2493
2494 /*
2495 * 4. Compose the folded-line string with 'foldtext', if set.
2496 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002497 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 txtcol = col; /* remember where text starts */
2500
2501 /*
2502 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2503 * Right-left text is put in columns 0 - number-col, normal text is put
2504 * in columns number-col - window-width.
2505 */
2506#ifdef FEAT_MBYTE
2507 if (has_mbyte)
2508 {
2509 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002510 int u8c, u8cc[MAX_MCO];
2511 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 int idx;
2513 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002514 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515# ifdef FEAT_ARABIC
2516 int prev_c = 0; /* previous Arabic character */
2517 int prev_c1 = 0; /* first composing char for prev_c */
2518# endif
2519
2520# ifdef FEAT_RIGHTLEFT
2521 if (wp->w_p_rl)
2522 idx = off;
2523 else
2524# endif
2525 idx = off + col;
2526
2527 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2528 for (p = text; *p != NUL; )
2529 {
2530 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002531 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (col + cells > W_WIDTH(wp)
2533# ifdef FEAT_RIGHTLEFT
2534 - (wp->w_p_rl ? col : 0)
2535# endif
2536 )
2537 break;
2538 ScreenLines[idx] = *p;
2539 if (enc_utf8)
2540 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002541 u8c = utfc_ptr2char(p, u8cc);
2542 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
2544 ScreenLinesUC[idx] = 0;
2545#ifdef FEAT_ARABIC
2546 prev_c = u8c;
2547#endif
2548 }
2549 else
2550 {
2551#ifdef FEAT_ARABIC
2552 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2553 {
2554 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002555 int pc, pc1, nc;
2556 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 int firstbyte = *p;
2558
2559 /* The idea of what is the previous and next
2560 * character depends on 'rightleft'. */
2561 if (wp->w_p_rl)
2562 {
2563 pc = prev_c;
2564 pc1 = prev_c1;
2565 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002566 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 else
2569 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002570 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002572 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 prev_c = u8c;
2575
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002576 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 pc, pc1, nc);
2578 ScreenLines[idx] = firstbyte;
2579 }
2580 else
2581 prev_c = u8c;
2582#endif
2583 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002584#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (u8c >= 0x10000)
2586 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2587 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002590 for (i = 0; i < Screen_mco; ++i)
2591 {
2592 ScreenLinesC[i][idx] = u8cc[i];
2593 if (u8cc[i] == 0)
2594 break;
2595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 if (cells > 1)
2598 ScreenLines[idx + 1] = 0;
2599 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002600 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2601 /* double-byte single width character */
2602 ScreenLines2[idx] = p[1];
2603 else if (cells > 1)
2604 /* double-width character */
2605 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 col += cells;
2607 idx += cells;
2608 p += c_len;
2609 }
2610 }
2611 else
2612#endif
2613 {
2614 len = (int)STRLEN(text);
2615 if (len > W_WIDTH(wp) - col)
2616 len = W_WIDTH(wp) - col;
2617 if (len > 0)
2618 {
2619#ifdef FEAT_RIGHTLEFT
2620 if (wp->w_p_rl)
2621 STRNCPY(current_ScreenLine, text, len);
2622 else
2623#endif
2624 STRNCPY(current_ScreenLine + col, text, len);
2625 col += len;
2626 }
2627 }
2628
2629 /* Fill the rest of the line with the fold filler */
2630#ifdef FEAT_RIGHTLEFT
2631 if (wp->w_p_rl)
2632 col -= txtcol;
2633#endif
2634 while (col < W_WIDTH(wp)
2635#ifdef FEAT_RIGHTLEFT
2636 - (wp->w_p_rl ? txtcol : 0)
2637#endif
2638 )
2639 {
2640#ifdef FEAT_MBYTE
2641 if (enc_utf8)
2642 {
2643 if (fill_fold >= 0x80)
2644 {
2645 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002646 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648 else
2649 ScreenLinesUC[off + col] = 0;
2650 }
2651#endif
2652 ScreenLines[off + col++] = fill_fold;
2653 }
2654
2655 if (text != buf)
2656 vim_free(text);
2657
2658 /*
2659 * 6. set highlighting for the Visual area an other text.
2660 * If all folded lines are in the Visual area, highlight the line.
2661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2663 {
2664 if (ltoreq(curwin->w_cursor, VIsual))
2665 {
2666 /* Visual is after curwin->w_cursor */
2667 top = &curwin->w_cursor;
2668 bot = &VIsual;
2669 }
2670 else
2671 {
2672 /* Visual is before curwin->w_cursor */
2673 top = &VIsual;
2674 bot = &curwin->w_cursor;
2675 }
2676 if (lnum >= top->lnum
2677 && lnume <= bot->lnum
2678 && (VIsual_mode != 'v'
2679 || ((lnum > top->lnum
2680 || (lnum == top->lnum
2681 && top->col == 0))
2682 && (lnume < bot->lnum
2683 || (lnume == bot->lnum
2684 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002685 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 if (VIsual_mode == Ctrl_V)
2688 {
2689 /* Visual block mode: highlight the chars part of the block */
2690 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2691 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002692 if (wp->w_old_cursor_lcol != MAXCOL
2693 && wp->w_old_cursor_lcol + txtcol
2694 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 len = wp->w_old_cursor_lcol;
2696 else
2697 len = W_WIDTH(wp) - txtcol;
2698 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002699 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 }
2702 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002705 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002710#ifdef FEAT_SYN_HL
2711 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002712 if (wp->w_p_cuc)
2713 {
2714 txtcol += wp->w_virtcol;
2715 if (wp->w_p_wrap)
2716 txtcol -= wp->w_skipcol;
2717 else
2718 txtcol -= wp->w_leftcol;
2719 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2720 ScreenAttrs[off + txtcol] = hl_combine_attr(
2721 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2726 (int)W_WIDTH(wp), FALSE);
2727
2728 /*
2729 * Update w_cline_height and w_cline_folded if the cursor line was
2730 * updated (saves a call to plines() later).
2731 */
2732 if (wp == curwin
2733 && lnum <= curwin->w_cursor.lnum
2734 && lnume >= curwin->w_cursor.lnum)
2735 {
2736 curwin->w_cline_row = row;
2737 curwin->w_cline_height = 1;
2738 curwin->w_cline_folded = TRUE;
2739 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2740 }
2741}
2742
2743/*
2744 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2745 */
2746 static void
2747copy_text_attr(off, buf, len, attr)
2748 int off;
2749 char_u *buf;
2750 int len;
2751 int attr;
2752{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002753 int i;
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 mch_memmove(ScreenLines + off, buf, (size_t)len);
2756# ifdef FEAT_MBYTE
2757 if (enc_utf8)
2758 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2759# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002760 for (i = 0; i < len; ++i)
2761 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762}
2763
2764/*
2765 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002766 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 */
2768 static void
2769fill_foldcolumn(p, wp, closed, lnum)
2770 char_u *p;
2771 win_T *wp;
2772 int closed; /* TRUE of FALSE */
2773 linenr_T lnum; /* current line number */
2774{
2775 int i = 0;
2776 int level;
2777 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002778 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 /* Init to all spaces. */
2781 copy_spaces(p, (size_t)wp->w_p_fdc);
2782
2783 level = win_foldinfo.fi_level;
2784 if (level > 0)
2785 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002786 /* If there is only one column put more info in it. */
2787 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 /* If the column is too narrow, we start at the lowest level that
2790 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002791 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (first_level < 1)
2793 first_level = 1;
2794
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002795 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
2797 if (win_foldinfo.fi_lnum == lnum
2798 && first_level + i >= win_foldinfo.fi_low_level)
2799 p[i] = '-';
2800 else if (first_level == 1)
2801 p[i] = '|';
2802 else if (first_level + i <= 9)
2803 p[i] = '0' + first_level + i;
2804 else
2805 p[i] = '>';
2806 if (first_level + i == level)
2807 break;
2808 }
2809 }
2810 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002811 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812}
2813#endif /* FEAT_FOLDING */
2814
2815/*
2816 * Display line "lnum" of window 'wp' on the screen.
2817 * Start at row "startrow", stop when "endrow" is reached.
2818 * wp->w_virtcol needs to be valid.
2819 *
2820 * Return the number of last row the line occupies.
2821 */
2822 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002823win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 win_T *wp;
2825 linenr_T lnum;
2826 int startrow;
2827 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002828 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
2830 int col; /* visual column on screen */
2831 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2832 int c = 0; /* init for GCC */
2833 long vcol = 0; /* virtual column (for tabs) */
2834 long vcol_prev = -1; /* "vcol" of previous character */
2835 char_u *line; /* current line */
2836 char_u *ptr; /* current position in "line" */
2837 int row; /* row in the window, excl w_winrow */
2838 int screen_row; /* row on the screen, incl w_winrow */
2839
2840 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2841 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002842 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 int c_extra = NUL; /* extra chars, all the same */
2844 int extra_attr = 0; /* attributes when n_extra != 0 */
2845 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2846 displaying lcs_eol at end-of-line */
2847 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2848 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2849
2850 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2851 int saved_n_extra = 0;
2852 char_u *saved_p_extra = NULL;
2853 int saved_c_extra = 0;
2854 int saved_char_attr = 0;
2855
2856 int n_attr = 0; /* chars with special attr */
2857 int saved_attr2 = 0; /* char_attr saved for n_attr */
2858 int n_attr3 = 0; /* chars with overruling special attr */
2859 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2860
2861 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2862
2863 int fromcol, tocol; /* start/end of inverting */
2864 int fromcol_prev = -2; /* start of inverting after cursor */
2865 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002867 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 pos_T pos;
2869 long v;
2870
2871 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002872 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2874 in this line */
2875 int attr = 0; /* attributes for area highlighting */
2876 int area_attr = 0; /* attributes desired by highlighting */
2877 int search_attr = 0; /* attributes desired by 'hlsearch' */
2878#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002879 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 int syntax_attr = 0; /* attributes desired by syntax */
2881 int has_syntax = FALSE; /* this buffer has syntax highl. */
2882 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002883 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002884 int draw_color_col = FALSE; /* highlight colorcolumn */
2885 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002886#endif
2887#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002888 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002889# define SPWORDLEN 150
2890 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002891 int nextlinecol = 0; /* column where nextline[] starts */
2892 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002893 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002894 int spell_attr = 0; /* attributes desired by spelling */
2895 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002896 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2897 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002898 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002899 static int cap_col = -1; /* column to check for Cap word */
2900 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002901 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902#endif
2903 int extra_check; /* has syntax or linebreak */
2904#ifdef FEAT_MBYTE
2905 int multi_attr = 0; /* attributes desired by multibyte */
2906 int mb_l = 1; /* multi-byte byte length */
2907 int mb_c = 0; /* decoded multi-byte character */
2908 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002909 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#endif
2911#ifdef FEAT_DIFF
2912 int filler_lines; /* nr of filler lines to be drawn */
2913 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002914 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int change_start = MAXCOL; /* first col of changed area */
2916 int change_end = -1; /* last col of changed area */
2917#endif
2918 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2919#ifdef FEAT_LINEBREAK
2920 int need_showbreak = FALSE;
2921#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002922#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2923 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002925 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
2927#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002928 matchitem_T *cur; /* points to the match list */
2929 match_T *shl; /* points to search_hl or a match */
2930 int shl_flag; /* flag to indicate whether search_hl
2931 has been processed or not */
2932 int prevcol_hl_flag; /* flag to indicate whether prevcol
2933 equals startcol of search_hl or one
2934 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#endif
2936#ifdef FEAT_ARABIC
2937 int prev_c = 0; /* previous Arabic character */
2938 int prev_c1 = 0; /* first composing char for prev_c */
2939#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002940#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002941 int did_line_attr = 0;
2942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /* draw_state: items that are drawn in sequence: */
2945#define WL_START 0 /* nothing done yet */
2946#ifdef FEAT_CMDWIN
2947# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2948#else
2949# define WL_CMDLINE WL_START
2950#endif
2951#ifdef FEAT_FOLDING
2952# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2953#else
2954# define WL_FOLD WL_CMDLINE
2955#endif
2956#ifdef FEAT_SIGNS
2957# define WL_SIGN WL_FOLD + 1 /* column for signs */
2958#else
2959# define WL_SIGN WL_FOLD /* column for signs */
2960#endif
2961#define WL_NR WL_SIGN + 1 /* line number */
2962#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2963# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2964#else
2965# define WL_SBR WL_NR
2966#endif
2967#define WL_LINE WL_SBR + 1 /* text in the line */
2968 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002969#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 int feedback_col = 0;
2971 int feedback_old_attr = -1;
2972#endif
2973
Bram Moolenaar860cae12010-06-05 23:22:07 +02002974#ifdef FEAT_CONCEAL
2975 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002976 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002977 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002978 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 int is_concealing = FALSE;
2980 int boguscols = 0; /* nonexistent columns added to force
2981 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002982 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002983 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002984# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02002985# define FIX_FOR_BOGUSCOLS \
2986 { \
2987 n_extra += vcol_off; \
2988 vcol -= vcol_off; \
2989 vcol_off = 0; \
2990 col -= boguscols; \
2991 boguscols = 0; \
2992 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002993#else
2994# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
2997 if (startrow > endrow) /* past the end already! */
2998 return startrow;
2999
3000 row = startrow;
3001 screen_row = row + W_WINROW(wp);
3002
3003 /*
3004 * To speed up the loop below, set extra_check when there is linebreak,
3005 * trailing white space and/or syntax processing to be done.
3006 */
3007#ifdef FEAT_LINEBREAK
3008 extra_check = wp->w_p_lbr;
3009#else
3010 extra_check = 0;
3011#endif
3012#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003013 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
3015 /* Prepare for syntax highlighting in this line. When there is an
3016 * error, stop syntax highlighting. */
3017 save_did_emsg = did_emsg;
3018 did_emsg = FALSE;
3019 syntax_start(wp, lnum);
3020 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003021 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else
3023 {
3024 did_emsg = save_did_emsg;
3025 has_syntax = TRUE;
3026 extra_check = TRUE;
3027 }
3028 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003029
3030 /* Check for columns to display for 'colorcolumn'. */
3031 color_cols = wp->w_p_cc_cols;
3032 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003033 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003034#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003035
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003036#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003037 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003038 && *wp->w_s->b_p_spl != NUL
3039 && wp->w_s->b_langp.ga_len > 0
3040 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003041 {
3042 /* Prepare for spell checking. */
3043 has_spell = TRUE;
3044 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003045
3046 /* Get the start of the next line, so that words that wrap to the next
3047 * line are found too: "et<line-break>al.".
3048 * Trick: skip a few chars for C/shell/Vim comments */
3049 nextline[SPWORDLEN] = NUL;
3050 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3051 {
3052 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3053 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3054 }
3055
3056 /* When a word wrapped from the previous line the start of the current
3057 * line is valid. */
3058 if (lnum == checked_lnum)
3059 cur_checked_col = checked_col;
3060 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003061
3062 /* When there was a sentence end in the previous line may require a
3063 * word starting with capital in this line. In line 1 always check
3064 * the first word. */
3065 if (lnum != capcol_lnum)
3066 cap_col = -1;
3067 if (lnum == 1)
3068 cap_col = 0;
3069 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#endif
3072
3073 /*
3074 * handle visual active in this window
3075 */
3076 fromcol = -10;
3077 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3079 {
3080 /* Visual is after curwin->w_cursor */
3081 if (ltoreq(curwin->w_cursor, VIsual))
3082 {
3083 top = &curwin->w_cursor;
3084 bot = &VIsual;
3085 }
3086 else /* Visual is before curwin->w_cursor */
3087 {
3088 top = &VIsual;
3089 bot = &curwin->w_cursor;
3090 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003091 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (VIsual_mode == Ctrl_V) /* block mode */
3093 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003094 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 {
3096 fromcol = wp->w_old_cursor_fcol;
3097 tocol = wp->w_old_cursor_lcol;
3098 }
3099 }
3100 else /* non-block mode */
3101 {
3102 if (lnum > top->lnum && lnum <= bot->lnum)
3103 fromcol = 0;
3104 else if (lnum == top->lnum)
3105 {
3106 if (VIsual_mode == 'V') /* linewise */
3107 fromcol = 0;
3108 else
3109 {
3110 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3111 if (gchar_pos(top) == NUL)
3112 tocol = fromcol + 1;
3113 }
3114 }
3115 if (VIsual_mode != 'V' && lnum == bot->lnum)
3116 {
3117 if (*p_sel == 'e' && bot->col == 0
3118#ifdef FEAT_VIRTUALEDIT
3119 && bot->coladd == 0
3120#endif
3121 )
3122 {
3123 fromcol = -10;
3124 tocol = MAXCOL;
3125 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003126 else if (bot->col == MAXCOL)
3127 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
3129 {
3130 pos = *bot;
3131 if (*p_sel == 'e')
3132 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3133 else
3134 {
3135 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3136 ++tocol;
3137 }
3138 }
3139 }
3140 }
3141
3142#ifndef MSDOS
3143 /* Check if the character under the cursor should not be inverted */
3144 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3145# ifdef FEAT_GUI
3146 && !gui.in_use
3147# endif
3148 )
3149 noinvcur = TRUE;
3150#endif
3151
3152 /* if inverting in this line set area_highlighting */
3153 if (fromcol >= 0)
3154 {
3155 area_highlighting = TRUE;
3156 attr = hl_attr(HLF_V);
3157#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003158 if ((clip_star.available && !clip_star.owned
3159 && clip_isautosel_star())
3160 || (clip_plus.available && !clip_plus.owned
3161 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 attr = hl_attr(HLF_VNC);
3163#endif
3164 }
3165 }
3166
3167 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003168 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003170 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 && wp == curwin
3172 && lnum >= curwin->w_cursor.lnum
3173 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3174 {
3175 if (lnum == curwin->w_cursor.lnum)
3176 getvcol(curwin, &(curwin->w_cursor),
3177 (colnr_T *)&fromcol, NULL, NULL);
3178 else
3179 fromcol = 0;
3180 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3181 {
3182 pos.lnum = lnum;
3183 pos.col = search_match_endcol;
3184 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3185 }
3186 else
3187 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003188 /* do at least one character; happens when past end of line */
3189 if (fromcol == tocol)
3190 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 area_highlighting = TRUE;
3192 attr = hl_attr(HLF_I);
3193 }
3194
3195#ifdef FEAT_DIFF
3196 filler_lines = diff_check(wp, lnum);
3197 if (filler_lines < 0)
3198 {
3199 if (filler_lines == -1)
3200 {
3201 if (diff_find_change(wp, lnum, &change_start, &change_end))
3202 diff_hlf = HLF_ADD; /* added line */
3203 else if (change_start == 0)
3204 diff_hlf = HLF_TXD; /* changed text */
3205 else
3206 diff_hlf = HLF_CHD; /* changed line */
3207 }
3208 else
3209 diff_hlf = HLF_ADD; /* added line */
3210 filler_lines = 0;
3211 area_highlighting = TRUE;
3212 }
3213 if (lnum == wp->w_topline)
3214 filler_lines = wp->w_topfill;
3215 filler_todo = filler_lines;
3216#endif
3217
3218#ifdef LINE_ATTR
3219# ifdef FEAT_SIGNS
3220 /* If this line has a sign with line highlighting set line_attr. */
3221 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3222 if (v != 0)
3223 line_attr = sign_get_attr((int)v, TRUE);
3224# endif
3225# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3226 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003227 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 line_attr = hl_attr(HLF_L);
3229# endif
3230 if (line_attr != 0)
3231 area_highlighting = TRUE;
3232#endif
3233
3234 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3235 ptr = line;
3236
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003237#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003238 if (has_spell)
3239 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003240 /* For checking first word with a capital skip white space. */
3241 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003242 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003243
Bram Moolenaar30abd282005-06-22 22:35:10 +00003244 /* To be able to spell-check over line boundaries copy the end of the
3245 * current line into nextline[]. Above the start of the next line was
3246 * copied to nextline[SPWORDLEN]. */
3247 if (nextline[SPWORDLEN] == NUL)
3248 {
3249 /* No next line or it is empty. */
3250 nextlinecol = MAXCOL;
3251 nextline_idx = 0;
3252 }
3253 else
3254 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003255 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003256 if (v < SPWORDLEN)
3257 {
3258 /* Short line, use it completely and append the start of the
3259 * next line. */
3260 nextlinecol = 0;
3261 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003262 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003263 nextline_idx = v + 1;
3264 }
3265 else
3266 {
3267 /* Long line, use only the last SPWORDLEN bytes. */
3268 nextlinecol = v - SPWORDLEN;
3269 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3270 nextline_idx = SPWORDLEN + 1;
3271 }
3272 }
3273 }
3274#endif
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 /* find start of trailing whitespace */
3277 if (wp->w_p_list && lcs_trail)
3278 {
3279 trailcol = (colnr_T)STRLEN(ptr);
3280 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3281 --trailcol;
3282 trailcol += (colnr_T) (ptr - line);
3283 extra_check = TRUE;
3284 }
3285
3286 /*
3287 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3288 * first character to be displayed.
3289 */
3290 if (wp->w_p_wrap)
3291 v = wp->w_skipcol;
3292 else
3293 v = wp->w_leftcol;
3294 if (v > 0)
3295 {
3296#ifdef FEAT_MBYTE
3297 char_u *prev_ptr = ptr;
3298#endif
3299 while (vcol < v && *ptr != NUL)
3300 {
3301 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3302 vcol += c;
3303#ifdef FEAT_MBYTE
3304 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003306 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003309 /* When:
3310 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003311 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003312 * - 'virtualedit' is set, or
3313 * - the visual mode is active,
3314 * the end of the line may be before the start of the displayed part.
3315 */
3316 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003317#ifdef FEAT_SYN_HL
3318 wp->w_p_cuc || draw_color_col ||
3319#endif
3320#ifdef FEAT_VIRTUALEDIT
3321 virtual_active() ||
3322#endif
3323 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328 /* Handle a character that's not completely on the screen: Put ptr at
3329 * that character but skip the first few screen characters. */
3330 if (vcol > v)
3331 {
3332 vcol -= c;
3333#ifdef FEAT_MBYTE
3334 ptr = prev_ptr;
3335#else
3336 --ptr;
3337#endif
3338 n_skip = v - vcol;
3339 }
3340
3341 /*
3342 * Adjust for when the inverted text is before the screen,
3343 * and when the start of the inverted text is before the screen.
3344 */
3345 if (tocol <= vcol)
3346 fromcol = 0;
3347 else if (fromcol >= 0 && fromcol < vcol)
3348 fromcol = vcol;
3349
3350#ifdef FEAT_LINEBREAK
3351 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3352 if (wp->w_p_wrap)
3353 need_showbreak = TRUE;
3354#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003355#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003356 /* When spell checking a word we need to figure out the start of the
3357 * word and if it's badly spelled or not. */
3358 if (has_spell)
3359 {
3360 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003361 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003362 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003363
3364 pos = wp->w_cursor;
3365 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003366 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003367 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003368
3369 /* spell_move_to() may call ml_get() and make "line" invalid */
3370 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3371 ptr = line + linecol;
3372
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003373 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003374 {
3375 /* no bad word found at line start, don't check until end of a
3376 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003377 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003378 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003379 }
3380 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003381 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003382 /* bad word found, use attributes until end of word */
3383 word_end = wp->w_cursor.col + len + 1;
3384
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003385 /* Turn index into actual attributes. */
3386 if (spell_hlf != HLF_COUNT)
3387 spell_attr = highlight_attr[spell_hlf];
3388 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003389 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003390
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003391# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003392 /* Need to restart syntax highlighting for this line. */
3393 if (has_syntax)
3394 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003395# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003396 }
3397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399
3400 /*
3401 * Correct highlighting for cursor that can't be disabled.
3402 * Avoids having to check this for each character.
3403 */
3404 if (fromcol >= 0)
3405 {
3406 if (noinvcur)
3407 {
3408 if ((colnr_T)fromcol == wp->w_virtcol)
3409 {
3410 /* highlighting starts at cursor, let it start just after the
3411 * cursor */
3412 fromcol_prev = fromcol;
3413 fromcol = -1;
3414 }
3415 else if ((colnr_T)fromcol < wp->w_virtcol)
3416 /* restart highlighting after the cursor */
3417 fromcol_prev = wp->w_virtcol;
3418 }
3419 if (fromcol >= tocol)
3420 fromcol = -1;
3421 }
3422
3423#ifdef FEAT_SEARCH_EXTRA
3424 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003425 * Handle highlighting the last used search pattern and matches.
3426 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003428 cur = wp->w_match_head;
3429 shl_flag = FALSE;
3430 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003432 if (shl_flag == FALSE)
3433 {
3434 shl = &search_hl;
3435 shl_flag = TRUE;
3436 }
3437 else
3438 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003439 shl->startcol = MAXCOL;
3440 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 shl->attr_cur = 0;
3442 if (shl->rm.regprog != NULL)
3443 {
3444 v = (long)(ptr - line);
3445 next_search_hl(wp, shl, lnum, (colnr_T)v);
3446
3447 /* Need to get the line again, a multi-line regexp may have made it
3448 * invalid. */
3449 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3450 ptr = line + v;
3451
3452 if (shl->lnum != 0 && shl->lnum <= lnum)
3453 {
3454 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003455 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003457 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3459 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003460 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003462 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003464 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003467 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003468 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 else
3470#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003471 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003473 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 shl->attr_cur = shl->attr;
3476 search_attr = shl->attr;
3477 }
3478 area_highlighting = TRUE;
3479 }
3480 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003481 if (shl != &search_hl && cur != NULL)
3482 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484#endif
3485
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003486#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003487 /* Cursor line highlighting for 'cursorline' in the current window. Not
3488 * when Visual mode is active, because it's not clear what is selected
3489 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003490 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3491 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003492 {
3493 line_attr = hl_attr(HLF_CUL);
3494 area_highlighting = TRUE;
3495 }
3496#endif
3497
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003498 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 col = 0;
3500#ifdef FEAT_RIGHTLEFT
3501 if (wp->w_p_rl)
3502 {
3503 /* Rightleft window: process the text in the normal direction, but put
3504 * it in current_ScreenLine[] from right to left. Start at the
3505 * rightmost column of the window. */
3506 col = W_WIDTH(wp) - 1;
3507 off += col;
3508 }
3509#endif
3510
3511 /*
3512 * Repeat for the whole displayed line.
3513 */
3514 for (;;)
3515 {
3516 /* Skip this quickly when working on the text. */
3517 if (draw_state != WL_LINE)
3518 {
3519#ifdef FEAT_CMDWIN
3520 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3521 {
3522 draw_state = WL_CMDLINE;
3523 if (cmdwin_type != 0 && wp == curwin)
3524 {
3525 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003527 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_attr = hl_attr(HLF_AT);
3529 }
3530 }
3531#endif
3532
3533#ifdef FEAT_FOLDING
3534 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3535 {
3536 draw_state = WL_FOLD;
3537 if (wp->w_p_fdc > 0)
3538 {
3539 /* Draw the 'foldcolumn'. */
3540 fill_foldcolumn(extra, wp, FALSE, lnum);
3541 n_extra = wp->w_p_fdc;
3542 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003543 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 c_extra = NUL;
3545 char_attr = hl_attr(HLF_FC);
3546 }
3547 }
3548#endif
3549
3550#ifdef FEAT_SIGNS
3551 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3552 {
3553 draw_state = WL_SIGN;
3554 /* Show the sign column when there are any signs in this
3555 * buffer or when using Netbeans. */
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003556 if (draw_signcolumn(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003558 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003560 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561# endif
3562
3563 /* Draw two cells with the sign value or blank. */
3564 c_extra = ' ';
3565 char_attr = hl_attr(HLF_SC);
3566 n_extra = 2;
3567
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003568 if (row == startrow
3569#ifdef FEAT_DIFF
3570 + filler_lines && filler_todo <= 0
3571#endif
3572 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3575 SIGN_TEXT);
3576# ifdef FEAT_SIGN_ICONS
3577 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3578 SIGN_ICON);
3579 if (gui.in_use && icon_sign != 0)
3580 {
3581 /* Use the image in this position. */
3582 c_extra = SIGN_BYTE;
3583# ifdef FEAT_NETBEANS_INTG
3584 if (buf_signcount(wp->w_buffer, lnum) > 1)
3585 c_extra = MULTISIGN_BYTE;
3586# endif
3587 char_attr = icon_sign;
3588 }
3589 else
3590# endif
3591 if (text_sign != 0)
3592 {
3593 p_extra = sign_get_text(text_sign);
3594 if (p_extra != NULL)
3595 {
3596 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003597 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 }
3599 char_attr = sign_get_attr(text_sign, FALSE);
3600 }
3601 }
3602 }
3603 }
3604#endif
3605
3606 if (draw_state == WL_NR - 1 && n_extra == 0)
3607 {
3608 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003609 /* Display the absolute or relative line number. After the
3610 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3611 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 && (row == startrow
3613#ifdef FEAT_DIFF
3614 + filler_lines
3615#endif
3616 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3617 {
3618 /* Draw the line number (empty space after wrapping). */
3619 if (row == startrow
3620#ifdef FEAT_DIFF
3621 + filler_lines
3622#endif
3623 )
3624 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003625 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003626 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003627
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003628 if (wp->w_p_nu && !wp->w_p_rnu)
3629 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003630 num = (long)lnum;
3631 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003632 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003633 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003634 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003635 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003636 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003637 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003638 num = lnum;
3639 fmt = "%-*ld ";
3640 }
3641 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003642
Bram Moolenaar700e7342013-01-30 12:31:36 +01003643 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003644 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if (wp->w_skipcol > 0)
3646 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3647 *p_extra = '-';
3648#ifdef FEAT_RIGHTLEFT
3649 if (wp->w_p_rl) /* reverse line numbers */
3650 rl_mirror(extra);
3651#endif
3652 p_extra = extra;
3653 c_extra = NUL;
3654 }
3655 else
3656 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003657 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003659#ifdef FEAT_SYN_HL
3660 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003661 * the current line differently.
3662 * TODO: Can we use CursorLine instead of CursorLineNr
3663 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003664 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003665 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003666 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669 }
3670
3671#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3672 if (draw_state == WL_SBR - 1 && n_extra == 0)
3673 {
3674 draw_state = WL_SBR;
3675# ifdef FEAT_DIFF
3676 if (filler_todo > 0)
3677 {
3678 /* Draw "deleted" diff line(s). */
3679 if (char2cells(fill_diff) > 1)
3680 c_extra = '-';
3681 else
3682 c_extra = fill_diff;
3683# ifdef FEAT_RIGHTLEFT
3684 if (wp->w_p_rl)
3685 n_extra = col + 1;
3686 else
3687# endif
3688 n_extra = W_WIDTH(wp) - col;
3689 char_attr = hl_attr(HLF_DED);
3690 }
3691# endif
3692# ifdef FEAT_LINEBREAK
3693 if (*p_sbr != NUL && need_showbreak)
3694 {
3695 /* Draw 'showbreak' at the start of each broken line. */
3696 p_extra = p_sbr;
3697 c_extra = NUL;
3698 n_extra = (int)STRLEN(p_sbr);
3699 char_attr = hl_attr(HLF_AT);
3700 need_showbreak = FALSE;
3701 /* Correct end of highlighted area for 'showbreak',
3702 * required when 'linebreak' is also set. */
3703 if (tocol == vcol)
3704 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003705#ifdef FEAT_SYN_HL
3706 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003707 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003708 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711# endif
3712 }
3713#endif
3714
3715 if (draw_state == WL_LINE - 1 && n_extra == 0)
3716 {
3717 draw_state = WL_LINE;
3718 if (saved_n_extra)
3719 {
3720 /* Continue item from end of wrapped line. */
3721 n_extra = saved_n_extra;
3722 c_extra = saved_c_extra;
3723 p_extra = saved_p_extra;
3724 char_attr = saved_char_attr;
3725 }
3726 else
3727 char_attr = 0;
3728 }
3729 }
3730
3731 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003732 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003733 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734#ifdef FEAT_DIFF
3735 && filler_todo <= 0
3736#endif
3737 )
3738 {
3739 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3740 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003741 /* Pretend we have finished updating the window. Except when
3742 * 'cursorcolumn' is set. */
3743#ifdef FEAT_SYN_HL
3744 if (wp->w_p_cuc)
3745 row = wp->w_cline_row + wp->w_cline_height;
3746 else
3747#endif
3748 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 break;
3750 }
3751
3752 if (draw_state == WL_LINE && area_highlighting)
3753 {
3754 /* handle Visual or match highlighting in this line */
3755 if (vcol == fromcol
3756#ifdef FEAT_MBYTE
3757 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3758 && (*mb_ptr2cells)(ptr) > 1)
3759#endif
3760 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003761 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 && vcol < tocol))
3763 area_attr = attr; /* start highlighting */
3764 else if (area_attr != 0
3765 && (vcol == tocol
3766 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769#ifdef FEAT_SEARCH_EXTRA
3770 if (!n_extra)
3771 {
3772 /*
3773 * Check for start/end of search pattern match.
3774 * After end, check for start/end of next match.
3775 * When another match, have to check for start again.
3776 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003777 * Do this for 'search_hl' and the match list (ordered by
3778 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003780 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003781 cur = wp->w_match_head;
3782 shl_flag = FALSE;
3783 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003785 if (shl_flag == FALSE
3786 && ((cur != NULL
3787 && cur->priority > SEARCH_HL_PRIORITY)
3788 || cur == NULL))
3789 {
3790 shl = &search_hl;
3791 shl_flag = TRUE;
3792 }
3793 else
3794 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 while (shl->rm.regprog != NULL)
3796 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003797 if (shl->startcol != MAXCOL
3798 && v >= (long)shl->startcol
3799 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
3801 shl->attr_cur = shl->attr;
3802 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003803 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 shl->attr_cur = 0;
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 next_search_hl(wp, shl, lnum, (colnr_T)v);
3808
3809 /* Need to get the line again, a multi-line regexp
3810 * may have made it invalid. */
3811 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3812 ptr = line + v;
3813
3814 if (shl->lnum == lnum)
3815 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003816 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003818 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003820 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003822 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
3824 /* highlight empty match, try again after
3825 * it */
3826#ifdef FEAT_MBYTE
3827 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003828 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003829 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 else
3831#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003832 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835 /* Loop to check if the match starts at the
3836 * current position */
3837 continue;
3838 }
3839 }
3840 break;
3841 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003842 if (shl != &search_hl && cur != NULL)
3843 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003845
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003846 /* Use attributes from match with highest priority among
3847 * 'search_hl' and the match list. */
3848 search_attr = search_hl.attr_cur;
3849 cur = wp->w_match_head;
3850 shl_flag = FALSE;
3851 while (cur != NULL || shl_flag == FALSE)
3852 {
3853 if (shl_flag == FALSE
3854 && ((cur != NULL
3855 && cur->priority > SEARCH_HL_PRIORITY)
3856 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003857 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003858 shl = &search_hl;
3859 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003860 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003861 else
3862 shl = &cur->hl;
3863 if (shl->attr_cur != 0)
3864 search_attr = shl->attr_cur;
3865 if (shl != &search_hl && cur != NULL)
3866 cur = cur->next;
3867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869#endif
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003874 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3875 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003877 if (diff_hlf == HLF_TXD && ptr - line > change_end
3878 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003880 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003883
3884 /* Decide which of the highlight attributes to use. */
3885 attr_pri = TRUE;
3886 if (area_attr != 0)
3887 char_attr = area_attr;
3888 else if (search_attr != 0)
3889 char_attr = search_attr;
3890#ifdef LINE_ATTR
3891 /* Use line_attr when not in the Visual or 'incsearch' area
3892 * (area_attr may be 0 when "noinvcur" is set). */
3893 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003894 || vcol < fromcol || vcol_prev < fromcol_prev
3895 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003896 char_attr = line_attr;
3897#endif
3898 else
3899 {
3900 attr_pri = FALSE;
3901#ifdef FEAT_SYN_HL
3902 if (has_syntax)
3903 char_attr = syntax_attr;
3904 else
3905#endif
3906 char_attr = 0;
3907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909
3910 /*
3911 * Get the next character to put on the screen.
3912 */
3913 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003914 * The "p_extra" points to the extra stuff that is inserted to
3915 * represent special characters (non-printable stuff) and other
3916 * things. When all characters are the same, c_extra is used.
3917 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3918 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3920 */
3921 if (n_extra > 0)
3922 {
3923 if (c_extra != NUL)
3924 {
3925 c = c_extra;
3926#ifdef FEAT_MBYTE
3927 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3928 if (enc_utf8 && (*mb_char2len)(c) > 1)
3929 {
3930 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003931 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 else
3935 mb_utf8 = FALSE;
3936#endif
3937 }
3938 else
3939 {
3940 c = *p_extra;
3941#ifdef FEAT_MBYTE
3942 if (has_mbyte)
3943 {
3944 mb_c = c;
3945 if (enc_utf8)
3946 {
3947 /* If the UTF-8 character is more than one byte:
3948 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003949 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 mb_utf8 = FALSE;
3951 if (mb_l > n_extra)
3952 mb_l = 1;
3953 else if (mb_l > 1)
3954 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003955 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003957 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 }
3960 else
3961 {
3962 /* if this is a DBCS character, put it in "mb_c" */
3963 mb_l = MB_BYTE2LEN(c);
3964 if (mb_l >= n_extra)
3965 mb_l = 1;
3966 else if (mb_l > 1)
3967 mb_c = (c << 8) + p_extra[1];
3968 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003969 if (mb_l == 0) /* at the NUL at end-of-line */
3970 mb_l = 1;
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 /* If a double-width char doesn't fit display a '>' in the
3973 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003974 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975# ifdef FEAT_RIGHTLEFT
3976 wp->w_p_rl ? (col <= 0) :
3977# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003978 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 && (*mb_char2cells)(mb_c) == 2)
3980 {
3981 c = '>';
3982 mb_c = c;
3983 mb_l = 1;
3984 mb_utf8 = FALSE;
3985 multi_attr = hl_attr(HLF_AT);
3986 /* put the pointer back to output the double-width
3987 * character at the start of the next line. */
3988 ++n_extra;
3989 --p_extra;
3990 }
3991 else
3992 {
3993 n_extra -= mb_l - 1;
3994 p_extra += mb_l - 1;
3995 }
3996 }
3997#endif
3998 ++p_extra;
3999 }
4000 --n_extra;
4001 }
4002 else
4003 {
4004 /*
4005 * Get a character from the line itself.
4006 */
4007 c = *ptr;
4008#ifdef FEAT_MBYTE
4009 if (has_mbyte)
4010 {
4011 mb_c = c;
4012 if (enc_utf8)
4013 {
4014 /* If the UTF-8 character is more than one byte: Decode it
4015 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004016 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 mb_utf8 = FALSE;
4018 if (mb_l > 1)
4019 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 /* Overlong encoded ASCII or ASCII with composing char
4022 * is displayed normally, except a NUL. */
4023 if (mb_c < 0x80)
4024 c = mb_c;
4025 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004026
4027 /* At start of the line we can have a composing char.
4028 * Draw it as a space with a composing char. */
4029 if (utf_iscomposing(mb_c))
4030 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004031 int i;
4032
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004033 for (i = Screen_mco - 1; i > 0; --i)
4034 u8cc[i] = u8cc[i - 1];
4035 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004036 mb_c = ' ';
4037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039
4040 if ((mb_l == 1 && c >= 0x80)
4041 || (mb_l >= 1 && mb_c == 0)
4042 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004043# ifdef UNICODE16
4044 || mb_c >= 0x10000
4045# endif
4046 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
4048 /*
4049 * Illegal UTF-8 byte: display as <xx>.
4050 * Non-BMP character : display as ? or fullwidth ?.
4051 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004052# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004057# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 if (wp->w_p_rl) /* reverse */
4059 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004062# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else if (utf_char2cells(mb_c) != 2)
4064 STRCPY(extra, "?");
4065 else
4066 /* 0xff1f in UTF-8: full-width '?' */
4067 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 p_extra = extra;
4071 c = *p_extra;
4072 mb_c = mb_ptr2char_adv(&p_extra);
4073 mb_utf8 = (c >= 0x80);
4074 n_extra = (int)STRLEN(p_extra);
4075 c_extra = NUL;
4076 if (area_attr == 0 && search_attr == 0)
4077 {
4078 n_attr = n_extra + 1;
4079 extra_attr = hl_attr(HLF_8);
4080 saved_attr2 = char_attr; /* save current attr */
4081 }
4082 }
4083 else if (mb_l == 0) /* at the NUL at end-of-line */
4084 mb_l = 1;
4085#ifdef FEAT_ARABIC
4086 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4087 {
4088 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004089 int pc, pc1, nc;
4090 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092 /* The idea of what is the previous and next
4093 * character depends on 'rightleft'. */
4094 if (wp->w_p_rl)
4095 {
4096 pc = prev_c;
4097 pc1 = prev_c1;
4098 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004099 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 else
4102 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004103 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004105 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 prev_c = mb_c;
4108
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004109 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 else
4112 prev_c = mb_c;
4113#endif
4114 }
4115 else /* enc_dbcs */
4116 {
4117 mb_l = MB_BYTE2LEN(c);
4118 if (mb_l == 0) /* at the NUL at end-of-line */
4119 mb_l = 1;
4120 else if (mb_l > 1)
4121 {
4122 /* We assume a second byte below 32 is illegal.
4123 * Hopefully this is OK for all double-byte encodings!
4124 */
4125 if (ptr[1] >= 32)
4126 mb_c = (c << 8) + ptr[1];
4127 else
4128 {
4129 if (ptr[1] == NUL)
4130 {
4131 /* head byte at end of line */
4132 mb_l = 1;
4133 transchar_nonprint(extra, c);
4134 }
4135 else
4136 {
4137 /* illegal tail byte */
4138 mb_l = 2;
4139 STRCPY(extra, "XX");
4140 }
4141 p_extra = extra;
4142 n_extra = (int)STRLEN(extra) - 1;
4143 c_extra = NUL;
4144 c = *p_extra++;
4145 if (area_attr == 0 && search_attr == 0)
4146 {
4147 n_attr = n_extra + 1;
4148 extra_attr = hl_attr(HLF_8);
4149 saved_attr2 = char_attr; /* save current attr */
4150 }
4151 mb_c = c;
4152 }
4153 }
4154 }
4155 /* If a double-width char doesn't fit display a '>' in the
4156 * last column; the character is displayed at the start of the
4157 * next line. */
4158 if ((
4159# ifdef FEAT_RIGHTLEFT
4160 wp->w_p_rl ? (col <= 0) :
4161# endif
4162 (col >= W_WIDTH(wp) - 1))
4163 && (*mb_char2cells)(mb_c) == 2)
4164 {
4165 c = '>';
4166 mb_c = c;
4167 mb_utf8 = FALSE;
4168 mb_l = 1;
4169 multi_attr = hl_attr(HLF_AT);
4170 /* Put pointer back so that the character will be
4171 * displayed at the start of the next line. */
4172 --ptr;
4173 }
4174 else if (*ptr != NUL)
4175 ptr += mb_l - 1;
4176
4177 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004178 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004179 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004180 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004183 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 c = ' ';
4185 if (area_attr == 0 && search_attr == 0)
4186 {
4187 n_attr = n_extra + 1;
4188 extra_attr = hl_attr(HLF_AT);
4189 saved_attr2 = char_attr; /* save current attr */
4190 }
4191 mb_c = c;
4192 mb_utf8 = FALSE;
4193 mb_l = 1;
4194 }
4195
4196 }
4197#endif
4198 ++ptr;
4199
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004200 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004201 if (wp->w_p_list && (c == 160
4202#ifdef FEAT_MBYTE
4203 || (mb_utf8 && mb_c == 160)
4204#endif
4205 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004206 {
4207 c = lcs_nbsp;
4208 if (area_attr == 0 && search_attr == 0)
4209 {
4210 n_attr = 1;
4211 extra_attr = hl_attr(HLF_8);
4212 saved_attr2 = char_attr; /* save current attr */
4213 }
4214#ifdef FEAT_MBYTE
4215 mb_c = c;
4216 if (enc_utf8 && (*mb_char2len)(c) > 1)
4217 {
4218 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004220 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004221 }
4222 else
4223 mb_utf8 = FALSE;
4224#endif
4225 }
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (extra_check)
4228 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004229#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004230 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004231#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004232
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004233#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 /* Get syntax attribute, unless still at the start of the line
4235 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004236 v = (long)(ptr - line);
4237 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 /* Get the syntax attribute for the character. If there
4240 * is an error, disable syntax highlighting. */
4241 save_did_emsg = did_emsg;
4242 did_emsg = FALSE;
4243
Bram Moolenaar217ad922005-03-20 22:37:15 +00004244 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004245# ifdef FEAT_SPELL
4246 has_spell ? &can_spell :
4247# endif
4248 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004251 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004252 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004253 has_syntax = FALSE;
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 else
4256 did_emsg = save_did_emsg;
4257
4258 /* Need to get the line again, a multi-line regexp may
4259 * have made it invalid. */
4260 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4261 ptr = line + v;
4262
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004263 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004265 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004266 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004267# ifdef FEAT_CONCEAL
4268 /* no concealing past the end of the line, it interferes
4269 * with line highlighting */
4270 if (c == NUL)
4271 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004272 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004273 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004276#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004277
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004278#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004279 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004280 * Only do this when there is no syntax highlighting, the
4281 * @Spell cluster is not used or the current syntax item
4282 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004283 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004284 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004285 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004286# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004287 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004288 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004289# endif
4290 if (c != 0 && (
4291# ifdef FEAT_SYN_HL
4292 !has_syntax ||
4293# endif
4294 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004295 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004296 char_u *prev_ptr, *p;
4297 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004298 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004299# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004300 if (has_mbyte)
4301 {
4302 prev_ptr = ptr - mb_l;
4303 v -= mb_l - 1;
4304 }
4305 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004306# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004307 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004308
4309 /* Use nextline[] if possible, it has the start of the
4310 * next line concatenated. */
4311 if ((prev_ptr - line) - nextlinecol >= 0)
4312 p = nextline + (prev_ptr - line) - nextlinecol;
4313 else
4314 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004315 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004316 len = spell_check(wp, p, &spell_hlf, &cap_col,
4317 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004318 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004319
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004320 /* In Insert mode only highlight a word that
4321 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004322 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004323 && (State & INSERT) != 0
4324 && wp->w_cursor.lnum == lnum
4325 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004326 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004327 && wp->w_cursor.col < (colnr_T)word_end)
4328 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004329 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004330 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004331 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004332
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004333 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004334 && (p - nextline) + len > nextline_idx)
4335 {
4336 /* Remember that the good word continues at the
4337 * start of the next line. */
4338 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004339 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004340 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004341
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004342 /* Turn index into actual attributes. */
4343 if (spell_hlf != HLF_COUNT)
4344 spell_attr = highlight_attr[spell_hlf];
4345
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004346 if (cap_col > 0)
4347 {
4348 if (p != prev_ptr
4349 && (p - nextline) + cap_col >= nextline_idx)
4350 {
4351 /* Remember that the word in the next line
4352 * must start with a capital. */
4353 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004354 cap_col = (int)((p - nextline) + cap_col
4355 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004356 }
4357 else
4358 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004359 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004360 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004361 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004362 }
4363 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004364 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004365 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004366 char_attr = hl_combine_attr(char_attr, spell_attr);
4367 else
4368 char_attr = hl_combine_attr(spell_attr, char_attr);
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#endif
4371#ifdef FEAT_LINEBREAK
4372 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004373 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 */
4375 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004376 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
4378 n_extra = win_lbr_chartabsize(wp, ptr - (
4379# ifdef FEAT_MBYTE
4380 has_mbyte ? mb_l :
4381# endif
4382 1), (colnr_T)vcol, NULL) - 1;
4383 c_extra = ' ';
4384 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004385 {
4386#ifdef FEAT_CONCEAL
4387 if (c == TAB)
4388 /* See "Tab alignment" below. */
4389 FIX_FOR_BOGUSCOLS;
4390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394#endif
4395
4396 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4397 {
4398 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004399 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 {
4401 n_attr = 1;
4402 extra_attr = hl_attr(HLF_8);
4403 saved_attr2 = char_attr; /* save current attr */
4404 }
4405#ifdef FEAT_MBYTE
4406 mb_c = c;
4407 if (enc_utf8 && (*mb_char2len)(c) > 1)
4408 {
4409 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004411 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 else
4414 mb_utf8 = FALSE;
4415#endif
4416 }
4417 }
4418
4419 /*
4420 * Handling of non-printable characters.
4421 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004422 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /*
4425 * when getting a character from the file, we may have to
4426 * turn it into something else on the way to putting it
4427 * into "ScreenLines".
4428 */
4429 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4430 {
4431 /* tab amount depends on current column */
4432 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004433 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4434#ifdef FEAT_CONCEAL
4435 /* Tab alignment should be identical regardless of
4436 * 'conceallevel' value. So tab compensates of all
4437 * previous concealed characters, and thus resets vcol_off
4438 * and boguscols accumulated so far in the line. Note that
4439 * the tab can be longer than 'tabstop' when there
4440 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004441 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443#ifdef FEAT_MBYTE
4444 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4445#endif
4446 if (wp->w_p_list)
4447 {
4448 c = lcs_tab1;
4449 c_extra = lcs_tab2;
4450 n_attr = n_extra + 1;
4451 extra_attr = hl_attr(HLF_8);
4452 saved_attr2 = char_attr; /* save current attr */
4453#ifdef FEAT_MBYTE
4454 mb_c = c;
4455 if (enc_utf8 && (*mb_char2len)(c) > 1)
4456 {
4457 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004458 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004459 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461#endif
4462 }
4463 else
4464 {
4465 c_extra = ' ';
4466 c = ' ';
4467 }
4468 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004469 else if (c == NUL
4470 && ((wp->w_p_list && lcs_eol > 0)
4471 || ((fromcol >= 0 || fromcol_prev >= 0)
4472 && tocol > vcol
4473 && VIsual_mode != Ctrl_V
4474 && (
4475# ifdef FEAT_RIGHTLEFT
4476 wp->w_p_rl ? (col >= 0) :
4477# endif
4478 (col < W_WIDTH(wp)))
4479 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004480 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004481 && (colnr_T)vcol == wp->w_virtcol)))
4482 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004484 /* Display a '$' after the line or highlight an extra
4485 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4487 /* For a diff line the highlighting continues after the
4488 * "$". */
4489 if (
4490# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004491 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492# ifdef LINE_ATTR
4493 &&
4494# endif
4495# endif
4496# ifdef LINE_ATTR
4497 line_attr == 0
4498# endif
4499 )
4500#endif
4501 {
4502#ifdef FEAT_VIRTUALEDIT
4503 /* In virtualedit, visual selections may extend
4504 * beyond end of line. */
4505 if (area_highlighting && virtual_active()
4506 && tocol != MAXCOL && vcol < tocol)
4507 n_extra = 0;
4508 else
4509#endif
4510 {
4511 p_extra = at_end_str;
4512 n_extra = 1;
4513 c_extra = NUL;
4514 }
4515 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004516 if (wp->w_p_list)
4517 c = lcs_eol;
4518 else
4519 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 lcs_eol_one = -1;
4521 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 extra_attr = hl_attr(HLF_AT);
4525 n_attr = 1;
4526 }
4527#ifdef FEAT_MBYTE
4528 mb_c = c;
4529 if (enc_utf8 && (*mb_char2len)(c) > 1)
4530 {
4531 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004532 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004533 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535 else
4536 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4537#endif
4538 }
4539 else if (c != NUL)
4540 {
4541 p_extra = transchar(c);
4542#ifdef FEAT_RIGHTLEFT
4543 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4544 rl_mirror(p_extra); /* reverse "<12>" */
4545#endif
4546 n_extra = byte2cells(c) - 1;
4547 c_extra = NUL;
4548 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004549 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 n_attr = n_extra + 1;
4552 extra_attr = hl_attr(HLF_8);
4553 saved_attr2 = char_attr; /* save current attr */
4554 }
4555#ifdef FEAT_MBYTE
4556 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4557#endif
4558 }
4559#ifdef FEAT_VIRTUALEDIT
4560 else if (VIsual_active
4561 && (VIsual_mode == Ctrl_V
4562 || VIsual_mode == 'v')
4563 && virtual_active()
4564 && tocol != MAXCOL
4565 && vcol < tocol
4566 && (
4567# ifdef FEAT_RIGHTLEFT
4568 wp->w_p_rl ? (col >= 0) :
4569# endif
4570 (col < W_WIDTH(wp))))
4571 {
4572 c = ' ';
4573 --ptr; /* put it back at the NUL */
4574 }
4575#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004576#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else if ((
4578# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004579 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 ) && (
4583# ifdef FEAT_RIGHTLEFT
4584 wp->w_p_rl ? (col >= 0) :
4585# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004586 (col
4587# ifdef FEAT_CONCEAL
4588 - boguscols
4589# endif
4590 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 /* Highlight until the right side of the window */
4593 c = ' ';
4594 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004595
4596 /* Remember we do the char for line highlighting. */
4597 ++did_line_attr;
4598
4599 /* don't do search HL for the rest of the line */
4600 if (line_attr != 0 && char_attr == search_attr && col > 0)
4601 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602# ifdef FEAT_DIFF
4603 if (diff_hlf == HLF_TXD)
4604 {
4605 diff_hlf = HLF_CHD;
4606 if (attr == 0 || char_attr != attr)
4607 char_attr = hl_attr(diff_hlf);
4608 }
4609# endif
4610 }
4611#endif
4612 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004613
4614#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004615 if ( wp->w_p_cole > 0
4616 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4617 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004618 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004619 && !(lnum_in_visual_area
4620 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004621 {
4622 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004623 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004624 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4625 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004626 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004627 /* First time at this concealed item: display one
4628 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004629 if (syn_get_sub_char() != NUL)
4630 c = syn_get_sub_char();
4631 else if (lcs_conceal != NUL)
4632 c = lcs_conceal;
4633 else
4634 c = ' ';
4635
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004636 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004637
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004638 if (n_extra > 0)
4639 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004640 vcol += n_extra;
4641 if (wp->w_p_wrap && n_extra > 0)
4642 {
4643# ifdef FEAT_RIGHTLEFT
4644 if (wp->w_p_rl)
4645 {
4646 col -= n_extra;
4647 boguscols -= n_extra;
4648 }
4649 else
4650# endif
4651 {
4652 boguscols += n_extra;
4653 col += n_extra;
4654 }
4655 }
4656 n_extra = 0;
4657 n_attr = 0;
4658 }
4659 else if (n_skip == 0)
4660 {
4661 is_concealing = TRUE;
4662 n_skip = 1;
4663 }
4664# ifdef FEAT_MBYTE
4665 mb_c = c;
4666 if (enc_utf8 && (*mb_char2len)(c) > 1)
4667 {
4668 mb_utf8 = TRUE;
4669 u8cc[0] = 0;
4670 c = 0xc0;
4671 }
4672 else
4673 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4674# endif
4675 }
4676 else
4677 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004678 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004679 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004680 }
4681#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004684#ifdef FEAT_CONCEAL
4685 /* In the cursor line and we may be concealing characters: correct
4686 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004687 if (!did_wcol && draw_state == WL_LINE
4688 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004689 && conceal_cursor_line(wp)
4690 && (int)wp->w_virtcol <= vcol + n_skip)
4691 {
4692 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004693 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004694 did_wcol = TRUE;
4695 }
4696#endif
4697
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 /* Don't override visual selection highlighting. */
4699 if (n_attr > 0
4700 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004701 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 char_attr = extra_attr;
4703
Bram Moolenaar81695252004-12-29 20:58:21 +00004704#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 /* XIM don't send preedit_start and preedit_end, but they send
4706 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4707 * im_is_preediting() here. */
4708 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004709 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 && (State & INSERT)
4711 && !p_imdisable
4712 && im_is_preediting()
4713 && draw_state == WL_LINE)
4714 {
4715 colnr_T tcol;
4716
4717 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004718 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 else
4720 tcol = preedit_end_col;
4721 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4722 {
4723 if (feedback_old_attr < 0)
4724 {
4725 feedback_col = 0;
4726 feedback_old_attr = char_attr;
4727 }
4728 char_attr = im_get_feedback_attr(feedback_col);
4729 if (char_attr < 0)
4730 char_attr = feedback_old_attr;
4731 feedback_col++;
4732 }
4733 else if (feedback_old_attr >= 0)
4734 {
4735 char_attr = feedback_old_attr;
4736 feedback_old_attr = -1;
4737 feedback_col = 0;
4738 }
4739 }
4740#endif
4741 /*
4742 * Handle the case where we are in column 0 but not on the first
4743 * character of the line and the user wants us to show us a
4744 * special character (via 'listchars' option "precedes:<char>".
4745 */
4746 if (lcs_prec_todo != NUL
4747 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4748#ifdef FEAT_DIFF
4749 && filler_todo <= 0
4750#endif
4751 && draw_state > WL_NR
4752 && c != NUL)
4753 {
4754 c = lcs_prec;
4755 lcs_prec_todo = NUL;
4756#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004757 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4758 {
4759 /* Double-width character being overwritten by the "precedes"
4760 * character, need to fill up half the character. */
4761 c_extra = MB_FILLER_CHAR;
4762 n_extra = 1;
4763 n_attr = 2;
4764 extra_attr = hl_attr(HLF_AT);
4765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 mb_c = c;
4767 if (enc_utf8 && (*mb_char2len)(c) > 1)
4768 {
4769 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004770 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004771 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 else
4774 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4775#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004776 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 saved_attr3 = char_attr; /* save current attr */
4779 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4780 n_attr3 = 1;
4781 }
4782 }
4783
4784 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004785 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004787 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004788#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004789 || did_line_attr == 1
4790#endif
4791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004793#ifdef FEAT_SEARCH_EXTRA
4794 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004795
4796 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004797 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004798 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004799#endif
4800
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004801 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 * highlight match at end of line. If it's beyond the last
4803 * char on the screen, just overwrite that one (tricky!) Not
4804 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004805#ifdef FEAT_SEARCH_EXTRA
4806 prevcol_hl_flag = FALSE;
4807 if (prevcol == (long)search_hl.startcol)
4808 prevcol_hl_flag = TRUE;
4809 else
4810 {
4811 cur = wp->w_match_head;
4812 while (cur != NULL)
4813 {
4814 if (prevcol == (long)cur->hl.startcol)
4815 {
4816 prevcol_hl_flag = TRUE;
4817 break;
4818 }
4819 cur = cur->next;
4820 }
4821 }
4822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004824 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004825 && (VIsual_mode != Ctrl_V
4826 || lnum == VIsual.lnum
4827 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004828 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829#ifdef FEAT_SEARCH_EXTRA
4830 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004831 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004832# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004833 && did_line_attr <= 1
4834# endif
4835 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#endif
4837 ))
4838 {
4839 int n = 0;
4840
4841#ifdef FEAT_RIGHTLEFT
4842 if (wp->w_p_rl)
4843 {
4844 if (col < 0)
4845 n = 1;
4846 }
4847 else
4848#endif
4849 {
4850 if (col >= W_WIDTH(wp))
4851 n = -1;
4852 }
4853 if (n != 0)
4854 {
4855 /* At the window boundary, highlight the last character
4856 * instead (better than nothing). */
4857 off += n;
4858 col += n;
4859 }
4860 else
4861 {
4862 /* Add a blank character to highlight. */
4863 ScreenLines[off] = ' ';
4864#ifdef FEAT_MBYTE
4865 if (enc_utf8)
4866 ScreenLinesUC[off] = 0;
4867#endif
4868 }
4869#ifdef FEAT_SEARCH_EXTRA
4870 if (area_attr == 0)
4871 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004872 /* Use attributes from match with highest priority among
4873 * 'search_hl' and the match list. */
4874 char_attr = search_hl.attr;
4875 cur = wp->w_match_head;
4876 shl_flag = FALSE;
4877 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004878 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004879 if (shl_flag == FALSE
4880 && ((cur != NULL
4881 && cur->priority > SEARCH_HL_PRIORITY)
4882 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004883 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004884 shl = &search_hl;
4885 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004886 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004887 else
4888 shl = &cur->hl;
4889 if ((ptr - line) - 1 == (long)shl->startcol)
4890 char_attr = shl->attr;
4891 if (shl != &search_hl && cur != NULL)
4892 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895#endif
4896 ScreenAttrs[off] = char_attr;
4897#ifdef FEAT_RIGHTLEFT
4898 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004901 --off;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 else
4904#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004907 ++off;
4908 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004909 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004910#ifdef FEAT_SYN_HL
4911 eol_hl_off = 1;
4912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
Bram Moolenaar91170f82006-05-05 21:15:17 +00004916 /*
4917 * At end of the text line.
4918 */
4919 if (c == NUL)
4920 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004921#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004922 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4923 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004924 {
4925 /* highlight last char after line */
4926 --col;
4927 --off;
4928 --vcol;
4929 }
4930
Bram Moolenaar1a384422010-07-14 19:53:30 +02004931 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004932 if (wp->w_p_wrap)
4933 v = wp->w_skipcol;
4934 else
4935 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004936
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004937 /* check if line ends before left margin */
4938 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004939 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004940#ifdef FEAT_CONCEAL
4941 /* Get rid of the boguscols now, we want to draw until the right
4942 * edge for 'cursorcolumn'. */
4943 col -= boguscols;
4944 boguscols = 0;
4945#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004946
4947 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004948 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004949
4950 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004951 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4952 && (int)wp->w_virtcol <
4953 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004954 && lnum != wp->w_cursor.lnum)
4955 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004956# ifdef FEAT_RIGHTLEFT
4957 && !wp->w_p_rl
4958# endif
4959 )
4960 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004961 int rightmost_vcol = 0;
4962 int i;
4963
4964 if (wp->w_p_cuc)
4965 rightmost_vcol = wp->w_virtcol;
4966 if (draw_color_col)
4967 /* determine rightmost colorcolumn to possibly draw */
4968 for (i = 0; color_cols[i] >= 0; ++i)
4969 if (rightmost_vcol < color_cols[i])
4970 rightmost_vcol = color_cols[i];
4971
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004972 while (col < W_WIDTH(wp))
4973 {
4974 ScreenLines[off] = ' ';
4975#ifdef FEAT_MBYTE
4976 if (enc_utf8)
4977 ScreenLinesUC[off] = 0;
4978#endif
4979 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02004980 if (draw_color_col)
4981 draw_color_col = advance_color_col(VCOL_HLC,
4982 &color_cols);
4983
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004984 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004985 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004986 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02004987 ScreenAttrs[off++] = hl_attr(HLF_MC);
4988 else
4989 ScreenAttrs[off++] = 0;
4990
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004991 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004992 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004993
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004994 ++vcol;
4995 }
4996 }
4997#endif
4998
Bram Moolenaar860cae12010-06-05 23:22:07 +02004999 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5000 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 row++;
5002
5003 /*
5004 * Update w_cline_height and w_cline_folded if the cursor line was
5005 * updated (saves a call to plines() later).
5006 */
5007 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5008 {
5009 curwin->w_cline_row = startrow;
5010 curwin->w_cline_height = row - startrow;
5011#ifdef FEAT_FOLDING
5012 curwin->w_cline_folded = FALSE;
5013#endif
5014 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5015 }
5016
5017 break;
5018 }
5019
5020 /* line continues beyond line end */
5021 if (lcs_ext
5022 && !wp->w_p_wrap
5023#ifdef FEAT_DIFF
5024 && filler_todo <= 0
5025#endif
5026 && (
5027#ifdef FEAT_RIGHTLEFT
5028 wp->w_p_rl ? col == 0 :
5029#endif
5030 col == W_WIDTH(wp) - 1)
5031 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005032 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5034 {
5035 c = lcs_ext;
5036 char_attr = hl_attr(HLF_AT);
5037#ifdef FEAT_MBYTE
5038 mb_c = c;
5039 if (enc_utf8 && (*mb_char2len)(c) > 1)
5040 {
5041 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005042 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005043 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 else
5046 mb_utf8 = FALSE;
5047#endif
5048 }
5049
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005050#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005051 /* advance to the next 'colorcolumn' */
5052 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005053 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005054
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005055 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005056 * highlight the cursor position itself.
5057 * Also highlight the 'colorcolumn' if it is different than
5058 * 'cursorcolumn' */
5059 vcol_save_attr = -1;
5060 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005061 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005062 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005063 && lnum != wp->w_cursor.lnum)
5064 {
5065 vcol_save_attr = char_attr;
5066 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5067 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005068 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005069 {
5070 vcol_save_attr = char_attr;
5071 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5072 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005073 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005074#endif
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 /*
5077 * Store character to be displayed.
5078 * Skip characters that are left of the screen for 'nowrap'.
5079 */
5080 vcol_prev = vcol;
5081 if (draw_state < WL_LINE || n_skip <= 0)
5082 {
5083 /*
5084 * Store the character.
5085 */
5086#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5087 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5088 {
5089 /* A double-wide character is: put first halve in left cell. */
5090 --off;
5091 --col;
5092 }
5093#endif
5094 ScreenLines[off] = c;
5095#ifdef FEAT_MBYTE
5096 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005097 {
5098 if ((mb_c & 0xff00) == 0x8e00)
5099 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 else if (enc_utf8)
5103 {
5104 if (mb_utf8)
5105 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005106 int i;
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005109 if ((c & 0xff) == 0)
5110 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005111 for (i = 0; i < Screen_mco; ++i)
5112 {
5113 ScreenLinesC[i][off] = u8cc[i];
5114 if (u8cc[i] == 0)
5115 break;
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 else
5119 ScreenLinesUC[off] = 0;
5120 }
5121 if (multi_attr)
5122 {
5123 ScreenAttrs[off] = multi_attr;
5124 multi_attr = 0;
5125 }
5126 else
5127#endif
5128 ScreenAttrs[off] = char_attr;
5129
5130#ifdef FEAT_MBYTE
5131 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5132 {
5133 /* Need to fill two screen columns. */
5134 ++off;
5135 ++col;
5136 if (enc_utf8)
5137 /* UTF-8: Put a 0 in the second screen char. */
5138 ScreenLines[off] = 0;
5139 else
5140 /* DBCS: Put second byte in the second screen char. */
5141 ScreenLines[off] = mb_c & 0xff;
5142 ++vcol;
5143 /* When "tocol" is halfway a character, set it to the end of
5144 * the character, otherwise highlighting won't stop. */
5145 if (tocol == vcol)
5146 ++tocol;
5147#ifdef FEAT_RIGHTLEFT
5148 if (wp->w_p_rl)
5149 {
5150 /* now it's time to backup one cell */
5151 --off;
5152 --col;
5153 }
5154#endif
5155 }
5156#endif
5157#ifdef FEAT_RIGHTLEFT
5158 if (wp->w_p_rl)
5159 {
5160 --off;
5161 --col;
5162 }
5163 else
5164#endif
5165 {
5166 ++off;
5167 ++col;
5168 }
5169 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005171 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005172 {
5173 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005174 ++vcol_off;
5175 if (n_extra > 0)
5176 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005177 if (wp->w_p_wrap)
5178 {
5179 /*
5180 * Special voodoo required if 'wrap' is on.
5181 *
5182 * Advance the column indicator to force the line
5183 * drawing to wrap early. This will make the line
5184 * take up the same screen space when parts are concealed,
5185 * so that cursor line computations aren't messed up.
5186 *
5187 * To avoid the fictitious advance of 'col' causing
5188 * trailing junk to be written out of the screen line
5189 * we are building, 'boguscols' keeps track of the number
5190 * of bad columns we have advanced.
5191 */
5192 if (n_extra > 0)
5193 {
5194 vcol += n_extra;
5195# ifdef FEAT_RIGHTLEFT
5196 if (wp->w_p_rl)
5197 {
5198 col -= n_extra;
5199 boguscols -= n_extra;
5200 }
5201 else
5202# endif
5203 {
5204 col += n_extra;
5205 boguscols += n_extra;
5206 }
5207 n_extra = 0;
5208 n_attr = 0;
5209 }
5210
5211
5212# ifdef FEAT_MBYTE
5213 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5214 {
5215 /* Need to fill two screen columns. */
5216# ifdef FEAT_RIGHTLEFT
5217 if (wp->w_p_rl)
5218 {
5219 --boguscols;
5220 --col;
5221 }
5222 else
5223# endif
5224 {
5225 ++boguscols;
5226 ++col;
5227 }
5228 }
5229# endif
5230
5231# ifdef FEAT_RIGHTLEFT
5232 if (wp->w_p_rl)
5233 {
5234 --boguscols;
5235 --col;
5236 }
5237 else
5238# endif
5239 {
5240 ++boguscols;
5241 ++col;
5242 }
5243 }
5244 else
5245 {
5246 if (n_extra > 0)
5247 {
5248 vcol += n_extra;
5249 n_extra = 0;
5250 n_attr = 0;
5251 }
5252 }
5253
5254 }
5255#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else
5257 --n_skip;
5258
Bram Moolenaar64486672010-05-16 15:46:46 +02005259 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5260 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005261 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#ifdef FEAT_DIFF
5263 && filler_todo <= 0
5264#endif
5265 )
5266 ++vcol;
5267
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005268#ifdef FEAT_SYN_HL
5269 if (vcol_save_attr >= 0)
5270 char_attr = vcol_save_attr;
5271#endif
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 /* restore attributes after "predeces" in 'listchars' */
5274 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5275 char_attr = saved_attr3;
5276
5277 /* restore attributes after last 'listchars' or 'number' char */
5278 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5279 char_attr = saved_attr2;
5280
5281 /*
5282 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005283 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
5285 if ((
5286#ifdef FEAT_RIGHTLEFT
5287 wp->w_p_rl ? (col < 0) :
5288#endif
5289 (col >= W_WIDTH(wp)))
5290 && (*ptr != NUL
5291#ifdef FEAT_DIFF
5292 || filler_todo > 0
5293#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005294 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5296 )
5297 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005298#ifdef FEAT_CONCEAL
5299 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5300 (int)W_WIDTH(wp), wp->w_p_rl);
5301 boguscols = 0;
5302#else
5303 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5304 (int)W_WIDTH(wp), wp->w_p_rl);
5305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ++row;
5307 ++screen_row;
5308
5309 /* When not wrapping and finished diff lines, or when displayed
5310 * '$' and highlighting until last column, break here. */
5311 if ((!wp->w_p_wrap
5312#ifdef FEAT_DIFF
5313 && filler_todo <= 0
5314#endif
5315 ) || lcs_eol_one == -1)
5316 break;
5317
5318 /* When the window is too narrow draw all "@" lines. */
5319 if (draw_state != WL_LINE
5320#ifdef FEAT_DIFF
5321 && filler_todo <= 0
5322#endif
5323 )
5324 {
5325 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5326#ifdef FEAT_VERTSPLIT
5327 draw_vsep_win(wp, row);
5328#endif
5329 row = endrow;
5330 }
5331
5332 /* When line got too long for screen break here. */
5333 if (row == endrow)
5334 {
5335 ++row;
5336 break;
5337 }
5338
5339 if (screen_cur_row == screen_row - 1
5340#ifdef FEAT_DIFF
5341 && filler_todo <= 0
5342#endif
5343 && W_WIDTH(wp) == Columns)
5344 {
5345 /* Remember that the line wraps, used for modeless copy. */
5346 LineWraps[screen_row - 1] = TRUE;
5347
5348 /*
5349 * Special trick to make copy/paste of wrapped lines work with
5350 * xterm/screen: write an extra character beyond the end of
5351 * the line. This will work with all terminal types
5352 * (regardless of the xn,am settings).
5353 * Only do this on a fast tty.
5354 * Only do this if the cursor is on the current line
5355 * (something has been written in it).
5356 * Don't do this for the GUI.
5357 * Don't do this for double-width characters.
5358 * Don't do this for a window not at the right screen border.
5359 */
5360 if (p_tf
5361#ifdef FEAT_GUI
5362 && !gui.in_use
5363#endif
5364#ifdef FEAT_MBYTE
5365 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005366 && ((*mb_off2cells)(LineOffset[screen_row],
5367 LineOffset[screen_row] + screen_Columns)
5368 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005370 + (int)Columns - 2,
5371 LineOffset[screen_row] + screen_Columns)
5372 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374 )
5375 {
5376 /* First make sure we are at the end of the screen line,
5377 * then output the same character again to let the
5378 * terminal know about the wrap. If the terminal doesn't
5379 * auto-wrap, we overwrite the character. */
5380 if (screen_cur_col != W_WIDTH(wp))
5381 screen_char(LineOffset[screen_row - 1]
5382 + (unsigned)Columns - 1,
5383 screen_row - 1, (int)(Columns - 1));
5384
5385#ifdef FEAT_MBYTE
5386 /* When there is a multi-byte character, just output a
5387 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005388 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5389 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 out_char(' ');
5391 else
5392#endif
5393 out_char(ScreenLines[LineOffset[screen_row - 1]
5394 + (Columns - 1)]);
5395 /* force a redraw of the first char on the next line */
5396 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5397 screen_start(); /* don't know where cursor is now */
5398 }
5399 }
5400
5401 col = 0;
5402 off = (unsigned)(current_ScreenLine - ScreenLines);
5403#ifdef FEAT_RIGHTLEFT
5404 if (wp->w_p_rl)
5405 {
5406 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5407 off += col;
5408 }
5409#endif
5410
5411 /* reset the drawing state for the start of a wrapped line */
5412 draw_state = WL_START;
5413 saved_n_extra = n_extra;
5414 saved_p_extra = p_extra;
5415 saved_c_extra = c_extra;
5416 saved_char_attr = char_attr;
5417 n_extra = 0;
5418 lcs_prec_todo = lcs_prec;
5419#ifdef FEAT_LINEBREAK
5420# ifdef FEAT_DIFF
5421 if (filler_todo <= 0)
5422# endif
5423 need_showbreak = TRUE;
5424#endif
5425#ifdef FEAT_DIFF
5426 --filler_todo;
5427 /* When the filler lines are actually below the last line of the
5428 * file, don't draw the line itself, break here. */
5429 if (filler_todo == 0 && wp->w_botfill)
5430 break;
5431#endif
5432 }
5433
5434 } /* for every character in the line */
5435
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005436#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005437 /* After an empty line check first word for capital. */
5438 if (*skipwhite(line) == NUL)
5439 {
5440 capcol_lnum = lnum + 1;
5441 cap_col = 0;
5442 }
5443#endif
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return row;
5446}
5447
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005448#ifdef FEAT_MBYTE
5449static int comp_char_differs __ARGS((int, int));
5450
5451/*
5452 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005453 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005454 */
5455 static int
5456comp_char_differs(off_from, off_to)
5457 int off_from;
5458 int off_to;
5459{
5460 int i;
5461
5462 for (i = 0; i < Screen_mco; ++i)
5463 {
5464 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5465 return TRUE;
5466 if (ScreenLinesC[i][off_from] == 0)
5467 break;
5468 }
5469 return FALSE;
5470}
5471#endif
5472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473/*
5474 * Check whether the given character needs redrawing:
5475 * - the (first byte of the) character is different
5476 * - the attributes are different
5477 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005478 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
5480 static int
5481char_needs_redraw(off_from, off_to, cols)
5482 int off_from;
5483 int off_to;
5484 int cols;
5485{
5486 if (cols > 0
5487 && ((ScreenLines[off_from] != ScreenLines[off_to]
5488 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5489
5490#ifdef FEAT_MBYTE
5491 || (enc_dbcs != 0
5492 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5493 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5494 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5495 : (cols > 1 && ScreenLines[off_from + 1]
5496 != ScreenLines[off_to + 1])))
5497 || (enc_utf8
5498 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5499 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005500 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005501 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5502 && ScreenLines[off_from + 1]
5503 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504#endif
5505 ))
5506 return TRUE;
5507 return FALSE;
5508}
5509
5510/*
5511 * Move one "cooked" screen line to the screen, but only the characters that
5512 * have actually changed. Handle insert/delete character.
5513 * "coloff" gives the first column on the screen for this line.
5514 * "endcol" gives the columns where valid characters are.
5515 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5516 * needs to be cleared, negative otherwise.
5517 * "rlflag" is TRUE in a rightleft window:
5518 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5519 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5520 */
5521 static void
5522screen_line(row, coloff, endcol, clear_width
5523#ifdef FEAT_RIGHTLEFT
5524 , rlflag
5525#endif
5526 )
5527 int row;
5528 int coloff;
5529 int endcol;
5530 int clear_width;
5531#ifdef FEAT_RIGHTLEFT
5532 int rlflag;
5533#endif
5534{
5535 unsigned off_from;
5536 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005537#ifdef FEAT_MBYTE
5538 unsigned max_off_from;
5539 unsigned max_off_to;
5540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 int col = 0;
5542#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5543 int hl;
5544#endif
5545 int force = FALSE; /* force update rest of the line */
5546 int redraw_this /* bool: does character need redraw? */
5547#ifdef FEAT_GUI
5548 = TRUE /* For GUI when while-loop empty */
5549#endif
5550 ;
5551 int redraw_next; /* redraw_this for next character */
5552#ifdef FEAT_MBYTE
5553 int clear_next = FALSE;
5554 int char_cells; /* 1: normal char */
5555 /* 2: occupies two display cells */
5556# define CHAR_CELLS char_cells
5557#else
5558# define CHAR_CELLS 1
5559#endif
5560
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005561 /* Check for illegal row and col, just in case. */
5562 if (row >= Rows)
5563 row = Rows - 1;
5564 if (endcol > Columns)
5565 endcol = Columns;
5566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567# ifdef FEAT_CLIPBOARD
5568 clip_may_clear_selection(row, row);
5569# endif
5570
5571 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5572 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005573#ifdef FEAT_MBYTE
5574 max_off_from = off_from + screen_Columns;
5575 max_off_to = LineOffset[row] + screen_Columns;
5576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578#ifdef FEAT_RIGHTLEFT
5579 if (rlflag)
5580 {
5581 /* Clear rest first, because it's left of the text. */
5582 if (clear_width > 0)
5583 {
5584 while (col <= endcol && ScreenLines[off_to] == ' '
5585 && ScreenAttrs[off_to] == 0
5586# ifdef FEAT_MBYTE
5587 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5588# endif
5589 )
5590 {
5591 ++off_to;
5592 ++col;
5593 }
5594 if (col <= endcol)
5595 screen_fill(row, row + 1, col + coloff,
5596 endcol + coloff + 1, ' ', ' ', 0);
5597 }
5598 col = endcol + 1;
5599 off_to = LineOffset[row] + col + coloff;
5600 off_from += col;
5601 endcol = (clear_width > 0 ? clear_width : -clear_width);
5602 }
5603#endif /* FEAT_RIGHTLEFT */
5604
5605 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5606
5607 while (col < endcol)
5608 {
5609#ifdef FEAT_MBYTE
5610 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005611 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 else
5613 char_cells = 1;
5614#endif
5615
5616 redraw_this = redraw_next;
5617 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5618 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5619
5620#ifdef FEAT_GUI
5621 /* If the next character was bold, then redraw the current character to
5622 * remove any pixels that might have spilt over into us. This only
5623 * happens in the GUI.
5624 */
5625 if (redraw_next && gui.in_use)
5626 {
5627 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 if (hl > HL_ALL)
5629 hl = syn_attr2attr(hl);
5630 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 redraw_this = TRUE;
5632 }
5633#endif
5634
5635 if (redraw_this)
5636 {
5637 /*
5638 * Special handling when 'xs' termcap flag set (hpterm):
5639 * Attributes for characters are stored at the position where the
5640 * cursor is when writing the highlighting code. The
5641 * start-highlighting code must be written with the cursor on the
5642 * first highlighted character. The stop-highlighting code must
5643 * be written with the cursor just after the last highlighted
5644 * character.
5645 * Overwriting a character doesn't remove it's highlighting. Need
5646 * to clear the rest of the line, and force redrawing it
5647 * completely.
5648 */
5649 if ( p_wiv
5650 && !force
5651#ifdef FEAT_GUI
5652 && !gui.in_use
5653#endif
5654 && ScreenAttrs[off_to] != 0
5655 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5656 {
5657 /*
5658 * Need to remove highlighting attributes here.
5659 */
5660 windgoto(row, col + coloff);
5661 out_str(T_CE); /* clear rest of this screen line */
5662 screen_start(); /* don't know where cursor is now */
5663 force = TRUE; /* force redraw of rest of the line */
5664 redraw_next = TRUE; /* or else next char would miss out */
5665
5666 /*
5667 * If the previous character was highlighted, need to stop
5668 * highlighting at this character.
5669 */
5670 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5671 {
5672 screen_attr = ScreenAttrs[off_to - 1];
5673 term_windgoto(row, col + coloff);
5674 screen_stop_highlight();
5675 }
5676 else
5677 screen_attr = 0; /* highlighting has stopped */
5678 }
5679#ifdef FEAT_MBYTE
5680 if (enc_dbcs != 0)
5681 {
5682 /* Check if overwriting a double-byte with a single-byte or
5683 * the other way around requires another character to be
5684 * redrawn. For UTF-8 this isn't needed, because comparing
5685 * ScreenLinesUC[] is sufficient. */
5686 if (char_cells == 1
5687 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005688 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 /* Writing a single-cell character over a double-cell
5691 * character: need to redraw the next cell. */
5692 ScreenLines[off_to + 1] = 0;
5693 redraw_next = TRUE;
5694 }
5695 else if (char_cells == 2
5696 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005697 && (*mb_off2cells)(off_to, max_off_to) == 1
5698 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
5700 /* Writing the second half of a double-cell character over
5701 * a double-cell character: need to redraw the second
5702 * cell. */
5703 ScreenLines[off_to + 2] = 0;
5704 redraw_next = TRUE;
5705 }
5706
5707 if (enc_dbcs == DBCS_JPNU)
5708 ScreenLines2[off_to] = ScreenLines2[off_from];
5709 }
5710 /* When writing a single-width character over a double-width
5711 * character and at the end of the redrawn text, need to clear out
5712 * the right halve of the old character.
5713 * Also required when writing the right halve of a double-width
5714 * char over the left halve of an existing one. */
5715 if (has_mbyte && col + char_cells == endcol
5716 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005717 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005719 && (*mb_off2cells)(off_to, max_off_to) == 1
5720 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 clear_next = TRUE;
5722#endif
5723
5724 ScreenLines[off_to] = ScreenLines[off_from];
5725#ifdef FEAT_MBYTE
5726 if (enc_utf8)
5727 {
5728 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5729 if (ScreenLinesUC[off_from] != 0)
5730 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005731 int i;
5732
5733 for (i = 0; i < Screen_mco; ++i)
5734 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
5736 }
5737 if (char_cells == 2)
5738 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5739#endif
5740
5741#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005742 /* The bold trick makes a single column of pixels appear in the
5743 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 * character should be redrawn too. This happens for our own GUI
5745 * and for some xterms. */
5746 if (
5747# ifdef FEAT_GUI
5748 gui.in_use
5749# endif
5750# if defined(FEAT_GUI) && defined(UNIX)
5751 ||
5752# endif
5753# ifdef UNIX
5754 term_is_xterm
5755# endif
5756 )
5757 {
5758 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005759 if (hl > HL_ALL)
5760 hl = syn_attr2attr(hl);
5761 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 redraw_next = TRUE;
5763 }
5764#endif
5765 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5766#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005767 /* For simplicity set the attributes of second half of a
5768 * double-wide character equal to the first half. */
5769 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005771
5772 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
5775#endif
5776 screen_char(off_to, row, col + coloff);
5777 }
5778 else if ( p_wiv
5779#ifdef FEAT_GUI
5780 && !gui.in_use
5781#endif
5782 && col + coloff > 0)
5783 {
5784 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5785 {
5786 /*
5787 * Don't output stop-highlight when moving the cursor, it will
5788 * stop the highlighting when it should continue.
5789 */
5790 screen_attr = 0;
5791 }
5792 else if (screen_attr != 0)
5793 screen_stop_highlight();
5794 }
5795
5796 off_to += CHAR_CELLS;
5797 off_from += CHAR_CELLS;
5798 col += CHAR_CELLS;
5799 }
5800
5801#ifdef FEAT_MBYTE
5802 if (clear_next)
5803 {
5804 /* Clear the second half of a double-wide character of which the left
5805 * half was overwritten with a single-wide character. */
5806 ScreenLines[off_to] = ' ';
5807 if (enc_utf8)
5808 ScreenLinesUC[off_to] = 0;
5809 screen_char(off_to, row, col + coloff);
5810 }
5811#endif
5812
5813 if (clear_width > 0
5814#ifdef FEAT_RIGHTLEFT
5815 && !rlflag
5816#endif
5817 )
5818 {
5819#ifdef FEAT_GUI
5820 int startCol = col;
5821#endif
5822
5823 /* blank out the rest of the line */
5824 while (col < clear_width && ScreenLines[off_to] == ' '
5825 && ScreenAttrs[off_to] == 0
5826#ifdef FEAT_MBYTE
5827 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5828#endif
5829 )
5830 {
5831 ++off_to;
5832 ++col;
5833 }
5834 if (col < clear_width)
5835 {
5836#ifdef FEAT_GUI
5837 /*
5838 * In the GUI, clearing the rest of the line may leave pixels
5839 * behind if the first character cleared was bold. Some bold
5840 * fonts spill over the left. In this case we redraw the previous
5841 * character too. If we didn't skip any blanks above, then we
5842 * only redraw if the character wasn't already redrawn anyway.
5843 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005844 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
5846 hl = ScreenAttrs[off_to];
5847 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005848 {
5849 int prev_cells = 1;
5850# ifdef FEAT_MBYTE
5851 if (enc_utf8)
5852 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5853 * that its width is 2. */
5854 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5855 else if (enc_dbcs != 0)
5856 {
5857 /* find previous character by counting from first
5858 * column and get its width. */
5859 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005860 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005861
5862 while (off < off_to)
5863 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005864 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005865 off += prev_cells;
5866 }
5867 }
5868
5869 if (enc_dbcs != 0 && prev_cells > 1)
5870 screen_char_2(off_to - prev_cells, row,
5871 col + coloff - prev_cells);
5872 else
5873# endif
5874 screen_char(off_to - prev_cells, row,
5875 col + coloff - prev_cells);
5876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878#endif
5879 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5880 ' ', ' ', 0);
5881#ifdef FEAT_VERTSPLIT
5882 off_to += clear_width - col;
5883 col = clear_width;
5884#endif
5885 }
5886 }
5887
5888 if (clear_width > 0)
5889 {
5890#ifdef FEAT_VERTSPLIT
5891 /* For a window that's left of another, draw the separator char. */
5892 if (col + coloff < Columns)
5893 {
5894 int c;
5895
5896 c = fillchar_vsep(&hl);
5897 if (ScreenLines[off_to] != c
5898# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005899 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5900 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901# endif
5902 || ScreenAttrs[off_to] != hl)
5903 {
5904 ScreenLines[off_to] = c;
5905 ScreenAttrs[off_to] = hl;
5906# ifdef FEAT_MBYTE
5907 if (enc_utf8)
5908 {
5909 if (c >= 0x80)
5910 {
5911 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005912 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 else
5915 ScreenLinesUC[off_to] = 0;
5916 }
5917# endif
5918 screen_char(off_to, row, col + coloff);
5919 }
5920 }
5921 else
5922#endif
5923 LineWraps[row] = FALSE;
5924 }
5925}
5926
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005927#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005929 * Mirror text "str" for right-left displaying.
5930 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005932 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933rl_mirror(str)
5934 char_u *str;
5935{
5936 char_u *p1, *p2;
5937 int t;
5938
5939 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5940 {
5941 t = *p1;
5942 *p1 = *p2;
5943 *p2 = t;
5944 }
5945}
5946#endif
5947
5948#if defined(FEAT_WINDOWS) || defined(PROTO)
5949/*
5950 * mark all status lines for redraw; used after first :cd
5951 */
5952 void
5953status_redraw_all()
5954{
5955 win_T *wp;
5956
5957 for (wp = firstwin; wp; wp = wp->w_next)
5958 if (wp->w_status_height)
5959 {
5960 wp->w_redr_status = TRUE;
5961 redraw_later(VALID);
5962 }
5963}
5964
5965/*
5966 * mark all status lines of the current buffer for redraw
5967 */
5968 void
5969status_redraw_curbuf()
5970{
5971 win_T *wp;
5972
5973 for (wp = firstwin; wp; wp = wp->w_next)
5974 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5975 {
5976 wp->w_redr_status = TRUE;
5977 redraw_later(VALID);
5978 }
5979}
5980
5981/*
5982 * Redraw all status lines that need to be redrawn.
5983 */
5984 void
5985redraw_statuslines()
5986{
5987 win_T *wp;
5988
5989 for (wp = firstwin; wp; wp = wp->w_next)
5990 if (wp->w_redr_status)
5991 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005992 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00005993 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995#endif
5996
5997#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5998/*
5999 * Redraw all status lines at the bottom of frame "frp".
6000 */
6001 void
6002win_redraw_last_status(frp)
6003 frame_T *frp;
6004{
6005 if (frp->fr_layout == FR_LEAF)
6006 frp->fr_win->w_redr_status = TRUE;
6007 else if (frp->fr_layout == FR_ROW)
6008 {
6009 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6010 win_redraw_last_status(frp);
6011 }
6012 else /* frp->fr_layout == FR_COL */
6013 {
6014 frp = frp->fr_child;
6015 while (frp->fr_next != NULL)
6016 frp = frp->fr_next;
6017 win_redraw_last_status(frp);
6018 }
6019}
6020#endif
6021
6022#ifdef FEAT_VERTSPLIT
6023/*
6024 * Draw the verticap separator right of window "wp" starting with line "row".
6025 */
6026 static void
6027draw_vsep_win(wp, row)
6028 win_T *wp;
6029 int row;
6030{
6031 int hl;
6032 int c;
6033
6034 if (wp->w_vsep_width)
6035 {
6036 /* draw the vertical separator right of this window */
6037 c = fillchar_vsep(&hl);
6038 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6039 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6040 c, ' ', hl);
6041 }
6042}
6043#endif
6044
6045#ifdef FEAT_WILDMENU
6046static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006047static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006050 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
6052 static int
6053status_match_len(xp, s)
6054 expand_T *xp;
6055 char_u *s;
6056{
6057 int len = 0;
6058
6059#ifdef FEAT_MENU
6060 int emenu = (xp->xp_context == EXPAND_MENUS
6061 || xp->xp_context == EXPAND_MENUNAMES);
6062
6063 /* Check for menu separators - replace with '|'. */
6064 if (emenu && menu_is_separator(s))
6065 return 1;
6066#endif
6067
6068 while (*s != NUL)
6069 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006070 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006071 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006072 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 }
6074
6075 return len;
6076}
6077
6078/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006079 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006080 * These are backslashes used for escaping. Do show backslashes in help tags.
6081 */
6082 static int
6083skip_status_match_char(xp, s)
6084 expand_T *xp;
6085 char_u *s;
6086{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006087 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006088#ifdef FEAT_MENU
6089 || ((xp->xp_context == EXPAND_MENUS
6090 || xp->xp_context == EXPAND_MENUNAMES)
6091 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6092#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006093 )
6094 {
6095#ifndef BACKSLASH_IN_FILENAME
6096 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6097 return 2;
6098#endif
6099 return 1;
6100 }
6101 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006102}
6103
6104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 * Show wildchar matches in the status line.
6106 * Show at least the "match" item.
6107 * We start at item 'first_match' in the list and show all matches that fit.
6108 *
6109 * If inversion is possible we use it. Else '=' characters are used.
6110 */
6111 void
6112win_redr_status_matches(xp, num_matches, matches, match, showtail)
6113 expand_T *xp;
6114 int num_matches;
6115 char_u **matches; /* list of matches */
6116 int match;
6117 int showtail;
6118{
6119#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6120 int row;
6121 char_u *buf;
6122 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006123 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 int fillchar;
6125 int attr;
6126 int i;
6127 int highlight = TRUE;
6128 char_u *selstart = NULL;
6129 int selstart_col = 0;
6130 char_u *selend = NULL;
6131 static int first_match = 0;
6132 int add_left = FALSE;
6133 char_u *s;
6134#ifdef FEAT_MENU
6135 int emenu;
6136#endif
6137#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6138 int l;
6139#endif
6140
6141 if (matches == NULL) /* interrupted completion? */
6142 return;
6143
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006144#ifdef FEAT_MBYTE
6145 if (has_mbyte)
6146 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6147 else
6148#endif
6149 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (buf == NULL)
6151 return;
6152
6153 if (match == -1) /* don't show match but original text */
6154 {
6155 match = 0;
6156 highlight = FALSE;
6157 }
6158 /* count 1 for the ending ">" */
6159 clen = status_match_len(xp, L_MATCH(match)) + 3;
6160 if (match == 0)
6161 first_match = 0;
6162 else if (match < first_match)
6163 {
6164 /* jumping left, as far as we can go */
6165 first_match = match;
6166 add_left = TRUE;
6167 }
6168 else
6169 {
6170 /* check if match fits on the screen */
6171 for (i = first_match; i < match; ++i)
6172 clen += status_match_len(xp, L_MATCH(i)) + 2;
6173 if (first_match > 0)
6174 clen += 2;
6175 /* jumping right, put match at the left */
6176 if ((long)clen > Columns)
6177 {
6178 first_match = match;
6179 /* if showing the last match, we can add some on the left */
6180 clen = 2;
6181 for (i = match; i < num_matches; ++i)
6182 {
6183 clen += status_match_len(xp, L_MATCH(i)) + 2;
6184 if ((long)clen >= Columns)
6185 break;
6186 }
6187 if (i == num_matches)
6188 add_left = TRUE;
6189 }
6190 }
6191 if (add_left)
6192 while (first_match > 0)
6193 {
6194 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6195 if ((long)clen >= Columns)
6196 break;
6197 --first_match;
6198 }
6199
6200 fillchar = fillchar_status(&attr, TRUE);
6201
6202 if (first_match == 0)
6203 {
6204 *buf = NUL;
6205 len = 0;
6206 }
6207 else
6208 {
6209 STRCPY(buf, "< ");
6210 len = 2;
6211 }
6212 clen = len;
6213
6214 i = first_match;
6215 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6216 {
6217 if (i == match)
6218 {
6219 selstart = buf + len;
6220 selstart_col = clen;
6221 }
6222
6223 s = L_MATCH(i);
6224 /* Check for menu separators - replace with '|' */
6225#ifdef FEAT_MENU
6226 emenu = (xp->xp_context == EXPAND_MENUS
6227 || xp->xp_context == EXPAND_MENUNAMES);
6228 if (emenu && menu_is_separator(s))
6229 {
6230 STRCPY(buf + len, transchar('|'));
6231 l = (int)STRLEN(buf + len);
6232 len += l;
6233 clen += l;
6234 }
6235 else
6236#endif
6237 for ( ; *s != NUL; ++s)
6238 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006239 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 clen += ptr2cells(s);
6241#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006242 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
6244 STRNCPY(buf + len, s, l);
6245 s += l - 1;
6246 len += l;
6247 }
6248 else
6249#endif
6250 {
6251 STRCPY(buf + len, transchar_byte(*s));
6252 len += (int)STRLEN(buf + len);
6253 }
6254 }
6255 if (i == match)
6256 selend = buf + len;
6257
6258 *(buf + len++) = ' ';
6259 *(buf + len++) = ' ';
6260 clen += 2;
6261 if (++i == num_matches)
6262 break;
6263 }
6264
6265 if (i != num_matches)
6266 {
6267 *(buf + len++) = '>';
6268 ++clen;
6269 }
6270
6271 buf[len] = NUL;
6272
6273 row = cmdline_row - 1;
6274 if (row >= 0)
6275 {
6276 if (wild_menu_showing == 0)
6277 {
6278 if (msg_scrolled > 0)
6279 {
6280 /* Put the wildmenu just above the command line. If there is
6281 * no room, scroll the screen one line up. */
6282 if (cmdline_row == Rows - 1)
6283 {
6284 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6285 ++msg_scrolled;
6286 }
6287 else
6288 {
6289 ++cmdline_row;
6290 ++row;
6291 }
6292 wild_menu_showing = WM_SCROLLED;
6293 }
6294 else
6295 {
6296 /* Create status line if needed by setting 'laststatus' to 2.
6297 * Set 'winminheight' to zero to avoid that the window is
6298 * resized. */
6299 if (lastwin->w_status_height == 0)
6300 {
6301 save_p_ls = p_ls;
6302 save_p_wmh = p_wmh;
6303 p_ls = 2;
6304 p_wmh = 0;
6305 last_status(FALSE);
6306 }
6307 wild_menu_showing = WM_SHOWN;
6308 }
6309 }
6310
6311 screen_puts(buf, row, 0, attr);
6312 if (selstart != NULL && highlight)
6313 {
6314 *selend = NUL;
6315 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6316 }
6317
6318 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6319 }
6320
6321#ifdef FEAT_VERTSPLIT
6322 win_redraw_last_status(topframe);
6323#else
6324 lastwin->w_redr_status = TRUE;
6325#endif
6326 vim_free(buf);
6327}
6328#endif
6329
6330#if defined(FEAT_WINDOWS) || defined(PROTO)
6331/*
6332 * Redraw the status line of window wp.
6333 *
6334 * If inversion is possible we use it. Else '=' characters are used.
6335 */
6336 void
6337win_redr_status(wp)
6338 win_T *wp;
6339{
6340 int row;
6341 char_u *p;
6342 int len;
6343 int fillchar;
6344 int attr;
6345 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006346 static int busy = FALSE;
6347
6348 /* It's possible to get here recursively when 'statusline' (indirectly)
6349 * invokes ":redrawstatus". Simply ignore the call then. */
6350 if (busy)
6351 return;
6352 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 wp->w_redr_status = FALSE;
6355 if (wp->w_status_height == 0)
6356 {
6357 /* no status line, can only be last window */
6358 redraw_cmdline = TRUE;
6359 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006360 else if (!redrawing()
6361#ifdef FEAT_INS_EXPAND
6362 /* don't update status line when popup menu is visible and may be
6363 * drawn over it */
6364 || pum_visible()
6365#endif
6366 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
6368 /* Don't redraw right now, do it later. */
6369 wp->w_redr_status = TRUE;
6370 }
6371#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006372 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
6374 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006375 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 }
6377#endif
6378 else
6379 {
6380 fillchar = fillchar_status(&attr, wp == curwin);
6381
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006382 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 p = NameBuff;
6384 len = (int)STRLEN(p);
6385
6386 if (wp->w_buffer->b_help
6387#ifdef FEAT_QUICKFIX
6388 || wp->w_p_pvw
6389#endif
6390 || bufIsChanged(wp->w_buffer)
6391 || wp->w_buffer->b_p_ro)
6392 *(p + len++) = ' ';
6393 if (wp->w_buffer->b_help)
6394 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006395 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 len += (int)STRLEN(p + len);
6397 }
6398#ifdef FEAT_QUICKFIX
6399 if (wp->w_p_pvw)
6400 {
6401 STRCPY(p + len, _("[Preview]"));
6402 len += (int)STRLEN(p + len);
6403 }
6404#endif
6405 if (bufIsChanged(wp->w_buffer))
6406 {
6407 STRCPY(p + len, "[+]");
6408 len += 3;
6409 }
6410 if (wp->w_buffer->b_p_ro)
6411 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006412 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 len += 4;
6414 }
6415
6416#ifndef FEAT_VERTSPLIT
6417 this_ru_col = ru_col;
6418 if (this_ru_col < (Columns + 1) / 2)
6419 this_ru_col = (Columns + 1) / 2;
6420#else
6421 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6422 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6423 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6424 if (this_ru_col <= 1)
6425 {
6426 p = (char_u *)"<"; /* No room for file name! */
6427 len = 1;
6428 }
6429 else
6430#endif
6431#ifdef FEAT_MBYTE
6432 if (has_mbyte)
6433 {
6434 int clen = 0, i;
6435
6436 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006437 clen = mb_string2cells(p, -1);
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 /* Find first character that will fit.
6440 * Going from start to end is much faster for DBCS. */
6441 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006442 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 clen -= (*mb_ptr2cells)(p + i);
6444 len = clen;
6445 if (i > 0)
6446 {
6447 p = p + i - 1;
6448 *p = '<';
6449 ++len;
6450 }
6451
6452 }
6453 else
6454#endif
6455 if (len > this_ru_col - 1)
6456 {
6457 p += len - (this_ru_col - 1);
6458 *p = '<';
6459 len = this_ru_col - 1;
6460 }
6461
6462 row = W_WINROW(wp) + wp->w_height;
6463 screen_puts(p, row, W_WINCOL(wp), attr);
6464 screen_fill(row, row + 1, len + W_WINCOL(wp),
6465 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6466
6467 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6468 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6469 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6470 - 1 + W_WINCOL(wp)), attr);
6471
6472#ifdef FEAT_CMDL_INFO
6473 win_redr_ruler(wp, TRUE);
6474#endif
6475 }
6476
6477#ifdef FEAT_VERTSPLIT
6478 /*
6479 * May need to draw the character below the vertical separator.
6480 */
6481 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6482 {
6483 if (stl_connected(wp))
6484 fillchar = fillchar_status(&attr, wp == curwin);
6485 else
6486 fillchar = fillchar_vsep(&attr);
6487 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6488 attr);
6489 }
6490#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006491 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492}
6493
Bram Moolenaar238a5642006-02-21 22:12:05 +00006494#ifdef FEAT_STL_OPT
6495/*
6496 * Redraw the status line according to 'statusline' and take care of any
6497 * errors encountered.
6498 */
6499 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006500redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006501 win_T *wp;
6502{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006503 static int entered = FALSE;
6504 int save_called_emsg = called_emsg;
6505
6506 /* When called recursively return. This can happen when the statusline
6507 * contains an expression that triggers a redraw. */
6508 if (entered)
6509 return;
6510 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006511
6512 called_emsg = FALSE;
6513 win_redr_custom(wp, FALSE);
6514 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006515 {
6516 /* When there is an error disable the statusline, otherwise the
6517 * display is messed up with errors and a redraw triggers the problem
6518 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006519 set_string_option_direct((char_u *)"statusline", -1,
6520 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006521 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006522 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006523 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006524 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006525}
6526#endif
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528# ifdef FEAT_VERTSPLIT
6529/*
6530 * Return TRUE if the status line of window "wp" is connected to the status
6531 * line of the window right of it. If not, then it's a vertical separator.
6532 * Only call if (wp->w_vsep_width != 0).
6533 */
6534 int
6535stl_connected(wp)
6536 win_T *wp;
6537{
6538 frame_T *fr;
6539
6540 fr = wp->w_frame;
6541 while (fr->fr_parent != NULL)
6542 {
6543 if (fr->fr_parent->fr_layout == FR_COL)
6544 {
6545 if (fr->fr_next != NULL)
6546 break;
6547 }
6548 else
6549 {
6550 if (fr->fr_next != NULL)
6551 return TRUE;
6552 }
6553 fr = fr->fr_parent;
6554 }
6555 return FALSE;
6556}
6557# endif
6558
6559#endif /* FEAT_WINDOWS */
6560
6561#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6562/*
6563 * Get the value to show for the language mappings, active 'keymap'.
6564 */
6565 int
6566get_keymap_str(wp, buf, len)
6567 win_T *wp;
6568 char_u *buf; /* buffer for the result */
6569 int len; /* length of buffer */
6570{
6571 char_u *p;
6572
6573 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6574 return FALSE;
6575
6576 {
6577#ifdef FEAT_EVAL
6578 buf_T *old_curbuf = curbuf;
6579 win_T *old_curwin = curwin;
6580 char_u *s;
6581
6582 curbuf = wp->w_buffer;
6583 curwin = wp;
6584 STRCPY(buf, "b:keymap_name"); /* must be writable */
6585 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006586 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 --emsg_skip;
6588 curbuf = old_curbuf;
6589 curwin = old_curwin;
6590 if (p == NULL || *p == NUL)
6591#endif
6592 {
6593#ifdef FEAT_KEYMAP
6594 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6595 p = wp->w_buffer->b_p_keymap;
6596 else
6597#endif
6598 p = (char_u *)"lang";
6599 }
6600 if ((int)(STRLEN(p) + 3) < len)
6601 sprintf((char *)buf, "<%s>", p);
6602 else
6603 buf[0] = NUL;
6604#ifdef FEAT_EVAL
6605 vim_free(s);
6606#endif
6607 }
6608 return buf[0] != NUL;
6609}
6610#endif
6611
6612#if defined(FEAT_STL_OPT) || defined(PROTO)
6613/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006614 * Redraw the status line or ruler of window "wp".
6615 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 */
6617 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006618win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006620 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006622 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 int attr;
6624 int curattr;
6625 int row;
6626 int col = 0;
6627 int maxwidth;
6628 int width;
6629 int n;
6630 int len;
6631 int fillchar;
6632 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006633 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006635 struct stl_hlrec hltab[STL_MAX_ITEM];
6636 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006637 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006638 win_T *ewp;
6639 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
Bram Moolenaar1d633412013-12-11 15:52:01 +01006641 /* There is a tiny chance that this gets called recursively: When
6642 * redrawing a status line triggers redrawing the ruler or tabline.
6643 * Avoid trouble by not allowing recursion. */
6644 if (entered)
6645 return;
6646 entered = TRUE;
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006649 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006651 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006652 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006653 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006654 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006655 attr = hl_attr(HLF_TPF);
6656 maxwidth = Columns;
6657# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006658 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006661 else
6662 {
6663 row = W_WINROW(wp) + wp->w_height;
6664 fillchar = fillchar_status(&attr, wp == curwin);
6665 maxwidth = W_WIDTH(wp);
6666
6667 if (draw_ruler)
6668 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006669 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006670 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006671 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006672 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006673 if (*++stl == '-')
6674 stl++;
6675 if (atoi((char *)stl))
6676 while (VIM_ISDIGIT(*stl))
6677 stl++;
6678 if (*stl++ != '(')
6679 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006680 }
6681#ifdef FEAT_VERTSPLIT
6682 col = ru_col - (Columns - W_WIDTH(wp));
6683 if (col < (W_WIDTH(wp) + 1) / 2)
6684 col = (W_WIDTH(wp) + 1) / 2;
6685#else
6686 col = ru_col;
6687 if (col > (Columns + 1) / 2)
6688 col = (Columns + 1) / 2;
6689#endif
6690 maxwidth = W_WIDTH(wp) - col;
6691#ifdef FEAT_WINDOWS
6692 if (!wp->w_status_height)
6693#endif
6694 {
6695 row = Rows - 1;
6696 --maxwidth; /* writing in last column may cause scrolling */
6697 fillchar = ' ';
6698 attr = 0;
6699 }
6700
6701# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006702 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006703# endif
6704 }
6705 else
6706 {
6707 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006708 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006709 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006710 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006711# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006712 use_sandbox = was_set_insecurely((char_u *)"statusline",
6713 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006714# endif
6715 }
6716
6717#ifdef FEAT_VERTSPLIT
6718 col += W_WINCOL(wp);
6719#endif
6720 }
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006723 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
Bram Moolenaar61452852011-02-01 18:01:11 +01006725 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6726 * the cursor away and back. */
6727 ewp = wp == NULL ? curwin : wp;
6728 p_crb_save = ewp->w_p_crb;
6729 ewp->w_p_crb = FALSE;
6730
Bram Moolenaar362f3562009-11-03 16:20:34 +00006731 /* Make a copy, because the statusline may include a function call that
6732 * might change the option value and free the memory. */
6733 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006734 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006735 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006736 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006737 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006738 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006740 /* Make all characters printable. */
6741 p = transstr(buf);
6742 if (p != NULL)
6743 {
6744 vim_strncpy(buf, p, sizeof(buf) - 1);
6745 vim_free(p);
6746 }
6747
6748 /* fill up with "fillchar" */
6749 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006750 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {
6752#ifdef FEAT_MBYTE
6753 len += (*mb_char2bytes)(fillchar, buf + len);
6754#else
6755 buf[len++] = fillchar;
6756#endif
6757 ++width;
6758 }
6759 buf[len] = NUL;
6760
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006761 /*
6762 * Draw each snippet with the specified highlighting.
6763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 curattr = attr;
6765 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006766 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006768 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 screen_puts_len(p, len, row, col, curattr);
6770 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006771 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006773 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006775 else if (hltab[n].userhl < 0)
6776 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006778 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006779 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#endif
6781 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006782 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006785
6786 if (wp == NULL)
6787 {
6788 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6789 col = 0;
6790 len = 0;
6791 p = buf;
6792 fillchar = 0;
6793 for (n = 0; tabtab[n].start != NULL; n++)
6794 {
6795 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6796 while (col < len)
6797 TabPageIdxs[col++] = fillchar;
6798 p = tabtab[n].start;
6799 fillchar = tabtab[n].userhl;
6800 }
6801 while (col < Columns)
6802 TabPageIdxs[col++] = fillchar;
6803 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006804
6805theend:
6806 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807}
6808
6809#endif /* FEAT_STL_OPT */
6810
6811/*
6812 * Output a single character directly to the screen and update ScreenLines.
6813 */
6814 void
6815screen_putchar(c, row, col, attr)
6816 int c;
6817 int row, col;
6818 int attr;
6819{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 char_u buf[MB_MAXBYTES + 1];
6821
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006822#ifdef FEAT_MBYTE
6823 if (has_mbyte)
6824 buf[(*mb_char2bytes)(c, buf)] = NUL;
6825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006827 {
6828 buf[0] = c;
6829 buf[1] = NUL;
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 screen_puts(buf, row, col, attr);
6832}
6833
6834/*
6835 * Get a single character directly from ScreenLines into "bytes[]".
6836 * Also return its attribute in *attrp;
6837 */
6838 void
6839screen_getbytes(row, col, bytes, attrp)
6840 int row, col;
6841 char_u *bytes;
6842 int *attrp;
6843{
6844 unsigned off;
6845
6846 /* safety check */
6847 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6848 {
6849 off = LineOffset[row] + col;
6850 *attrp = ScreenAttrs[off];
6851 bytes[0] = ScreenLines[off];
6852 bytes[1] = NUL;
6853
6854#ifdef FEAT_MBYTE
6855 if (enc_utf8 && ScreenLinesUC[off] != 0)
6856 bytes[utfc_char2bytes(off, bytes)] = NUL;
6857 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6858 {
6859 bytes[0] = ScreenLines[off];
6860 bytes[1] = ScreenLines2[off];
6861 bytes[2] = NUL;
6862 }
6863 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6864 {
6865 bytes[1] = ScreenLines[off + 1];
6866 bytes[2] = NUL;
6867 }
6868#endif
6869 }
6870}
6871
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006872#ifdef FEAT_MBYTE
6873static int screen_comp_differs __ARGS((int, int*));
6874
6875/*
6876 * Return TRUE if composing characters for screen posn "off" differs from
6877 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006878 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006879 */
6880 static int
6881screen_comp_differs(off, u8cc)
6882 int off;
6883 int *u8cc;
6884{
6885 int i;
6886
6887 for (i = 0; i < Screen_mco; ++i)
6888 {
6889 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6890 return TRUE;
6891 if (u8cc[i] == 0)
6892 break;
6893 }
6894 return FALSE;
6895}
6896#endif
6897
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898/*
6899 * Put string '*text' on the screen at position 'row' and 'col', with
6900 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6901 * Note: only outputs within one row, message is truncated at screen boundary!
6902 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6903 */
6904 void
6905screen_puts(text, row, col, attr)
6906 char_u *text;
6907 int row;
6908 int col;
6909 int attr;
6910{
6911 screen_puts_len(text, -1, row, col, attr);
6912}
6913
6914/*
6915 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6916 * a NUL.
6917 */
6918 void
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006919screen_puts_len(text, textlen, row, col, attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 char_u *text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006921 int textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 int row;
6923 int col;
6924 int attr;
6925{
6926 unsigned off;
6927 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02006928 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 int c;
6930#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006931 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 int mbyte_blen = 1;
6933 int mbyte_cells = 1;
6934 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006935 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 int clear_next_cell = FALSE;
6937# ifdef FEAT_ARABIC
6938 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006939 int pc, nc, nc1;
6940 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941# endif
6942#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006943#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6944 int force_redraw_this;
6945 int force_redraw_next = FALSE;
6946#endif
6947 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6950 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006951 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
Bram Moolenaarc236c162008-07-13 17:41:49 +00006953#ifdef FEAT_MBYTE
6954 /* When drawing over the right halve of a double-wide char clear out the
6955 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006956 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006957# ifdef FEAT_GUI
6958 && !gui.in_use
6959# endif
6960 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006961 {
6962 ScreenLines[off - 1] = ' ';
6963 ScreenAttrs[off - 1] = 0;
6964 if (enc_utf8)
6965 {
6966 ScreenLinesUC[off - 1] = 0;
6967 ScreenLinesC[0][off - 1] = 0;
6968 }
6969 /* redraw the previous cell, make it empty */
6970 screen_char(off - 1, row, col - 1);
6971 /* force the cell at "col" to be redrawn */
6972 force_redraw_next = TRUE;
6973 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006974#endif
6975
Bram Moolenaar367329b2007-08-30 11:53:22 +00006976#ifdef FEAT_MBYTE
6977 max_off = LineOffset[row] + screen_Columns;
6978#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006979 while (col < screen_Columns
6980 && (len < 0 || (int)(ptr - text) < len)
6981 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 c = *ptr;
6984#ifdef FEAT_MBYTE
6985 /* check if this is the first byte of a multibyte */
6986 if (has_mbyte)
6987 {
6988 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006989 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006991 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6993 mbyte_cells = 1;
6994 else if (enc_dbcs != 0)
6995 mbyte_cells = mbyte_blen;
6996 else /* enc_utf8 */
6997 {
6998 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006999 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 (int)((text + len) - ptr));
7001 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007002 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007004# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 /* Non-BMP character: display as ? or fullwidth ?. */
7006 if (u8c >= 0x10000)
7007 {
7008 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7009 if (attr == 0)
7010 attr = hl_attr(HLF_8);
7011 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013# ifdef FEAT_ARABIC
7014 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7015 {
7016 /* Do Arabic shaping. */
7017 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7018 {
7019 /* Past end of string to be displayed. */
7020 nc = NUL;
7021 nc1 = NUL;
7022 }
7023 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007024 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007025 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7026 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007027 nc1 = pcc[0];
7028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 pc = prev_c;
7030 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007031 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
7033 else
7034 prev_c = u8c;
7035# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007036 if (col + mbyte_cells > screen_Columns)
7037 {
7038 /* Only 1 cell left, but character requires 2 cells:
7039 * display a '>' in the last column to avoid wrapping. */
7040 c = '>';
7041 mbyte_cells = 1;
7042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 }
7045#endif
7046
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007047#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7048 force_redraw_this = force_redraw_next;
7049 force_redraw_next = FALSE;
7050#endif
7051
7052 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053#ifdef FEAT_MBYTE
7054 || (mbyte_cells == 2
7055 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7056 || (enc_dbcs == DBCS_JPNU
7057 && c == 0x8e
7058 && ScreenLines2[off] != ptr[1])
7059 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007060 && (ScreenLinesUC[off] !=
7061 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7062 || (ScreenLinesUC[off] != 0
7063 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#endif
7065 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007066 || exmode_active;
7067
7068 if (need_redraw
7069#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7070 || force_redraw_this
7071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 )
7073 {
7074#if defined(FEAT_GUI) || defined(UNIX)
7075 /* The bold trick makes a single row of pixels appear in the next
7076 * character. When a bold character is removed, the next
7077 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007078 * and for some xterms. */
7079 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080# ifdef FEAT_GUI
7081 gui.in_use
7082# endif
7083# if defined(FEAT_GUI) && defined(UNIX)
7084 ||
7085# endif
7086# ifdef UNIX
7087 term_is_xterm
7088# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007089 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007091 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007093 if (n > HL_ALL)
7094 n = syn_attr2attr(n);
7095 if (n & HL_BOLD)
7096 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098#endif
7099#ifdef FEAT_MBYTE
7100 /* When at the end of the text and overwriting a two-cell
7101 * character with a one-cell character, need to clear the next
7102 * cell. Also when overwriting the left halve of a two-cell char
7103 * with the right halve of a two-cell char. Do this only once
7104 * (mb_off2cells() may return 2 on the right halve). */
7105 if (clear_next_cell)
7106 clear_next_cell = FALSE;
7107 else if (has_mbyte
7108 && (len < 0 ? ptr[mbyte_blen] == NUL
7109 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007110 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007112 && (*mb_off2cells)(off, max_off) == 1
7113 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 clear_next_cell = TRUE;
7115
7116 /* Make sure we never leave a second byte of a double-byte behind,
7117 * it confuses mb_off2cells(). */
7118 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007119 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007121 && (*mb_off2cells)(off, max_off) == 1
7122 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 ScreenLines[off + mbyte_blen] = 0;
7124#endif
7125 ScreenLines[off] = c;
7126 ScreenAttrs[off] = attr;
7127#ifdef FEAT_MBYTE
7128 if (enc_utf8)
7129 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007130 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 ScreenLinesUC[off] = 0;
7132 else
7133 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007134 int i;
7135
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007137 for (i = 0; i < Screen_mco; ++i)
7138 {
7139 ScreenLinesC[i][off] = u8cc[i];
7140 if (u8cc[i] == 0)
7141 break;
7142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144 if (mbyte_cells == 2)
7145 {
7146 ScreenLines[off + 1] = 0;
7147 ScreenAttrs[off + 1] = attr;
7148 }
7149 screen_char(off, row, col);
7150 }
7151 else if (mbyte_cells == 2)
7152 {
7153 ScreenLines[off + 1] = ptr[1];
7154 ScreenAttrs[off + 1] = attr;
7155 screen_char_2(off, row, col);
7156 }
7157 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7158 {
7159 ScreenLines2[off] = ptr[1];
7160 screen_char(off, row, col);
7161 }
7162 else
7163#endif
7164 screen_char(off, row, col);
7165 }
7166#ifdef FEAT_MBYTE
7167 if (has_mbyte)
7168 {
7169 off += mbyte_cells;
7170 col += mbyte_cells;
7171 ptr += mbyte_blen;
7172 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007173 {
7174 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007176 len = -1;
7177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 else
7180#endif
7181 {
7182 ++off;
7183 ++col;
7184 ++ptr;
7185 }
7186 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007187
7188#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7189 /* If we detected the next character needs to be redrawn, but the text
7190 * doesn't extend up to there, update the character here. */
7191 if (force_redraw_next && col < screen_Columns)
7192 {
7193# ifdef FEAT_MBYTE
7194 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7195 screen_char_2(off, row, col);
7196 else
7197# endif
7198 screen_char(off, row, col);
7199 }
7200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201}
7202
7203#ifdef FEAT_SEARCH_EXTRA
7204/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007205 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 */
7207 static void
7208start_search_hl()
7209{
7210 if (p_hls && !no_hlsearch)
7211 {
7212 last_pat_prog(&search_hl.rm);
7213 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007214# ifdef FEAT_RELTIME
7215 /* Set the time limit to 'redrawtime'. */
7216 profile_setlimit(p_rdt, &search_hl.tm);
7217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
7219}
7220
7221/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007222 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 */
7224 static void
7225end_search_hl()
7226{
7227 if (search_hl.rm.regprog != NULL)
7228 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007229 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 search_hl.rm.regprog = NULL;
7231 }
7232}
7233
7234/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007235 * Init for calling prepare_search_hl().
7236 */
7237 static void
7238init_search_hl(wp)
7239 win_T *wp;
7240{
7241 matchitem_T *cur;
7242
7243 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7244 * match */
7245 cur = wp->w_match_head;
7246 while (cur != NULL)
7247 {
7248 cur->hl.rm = cur->match;
7249 if (cur->hlg_id == 0)
7250 cur->hl.attr = 0;
7251 else
7252 cur->hl.attr = syn_id2attr(cur->hlg_id);
7253 cur->hl.buf = wp->w_buffer;
7254 cur->hl.lnum = 0;
7255 cur->hl.first_lnum = 0;
7256# ifdef FEAT_RELTIME
7257 /* Set the time limit to 'redrawtime'. */
7258 profile_setlimit(p_rdt, &(cur->hl.tm));
7259# endif
7260 cur = cur->next;
7261 }
7262 search_hl.buf = wp->w_buffer;
7263 search_hl.lnum = 0;
7264 search_hl.first_lnum = 0;
7265 /* time limit is set at the toplevel, for all windows */
7266}
7267
7268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 * Advance to the match in window "wp" line "lnum" or past it.
7270 */
7271 static void
7272prepare_search_hl(wp, lnum)
7273 win_T *wp;
7274 linenr_T lnum;
7275{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007276 matchitem_T *cur; /* points to the match list */
7277 match_T *shl; /* points to search_hl or a match */
7278 int shl_flag; /* flag to indicate whether search_hl
7279 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 int n;
7281
7282 /*
7283 * When using a multi-line pattern, start searching at the top
7284 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007285 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007287 cur = wp->w_match_head;
7288 shl_flag = FALSE;
7289 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007291 if (shl_flag == FALSE)
7292 {
7293 shl = &search_hl;
7294 shl_flag = TRUE;
7295 }
7296 else
7297 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (shl->rm.regprog != NULL
7299 && shl->lnum == 0
7300 && re_multiline(shl->rm.regprog))
7301 {
7302 if (shl->first_lnum == 0)
7303 {
7304# ifdef FEAT_FOLDING
7305 for (shl->first_lnum = lnum;
7306 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7307 if (hasFoldingWin(wp, shl->first_lnum - 1,
7308 NULL, NULL, TRUE, NULL))
7309 break;
7310# else
7311 shl->first_lnum = wp->w_topline;
7312# endif
7313 }
7314 n = 0;
7315 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7316 {
7317 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7318 if (shl->lnum != 0)
7319 {
7320 shl->first_lnum = shl->lnum
7321 + shl->rm.endpos[0].lnum
7322 - shl->rm.startpos[0].lnum;
7323 n = shl->rm.endpos[0].col;
7324 }
7325 else
7326 {
7327 ++shl->first_lnum;
7328 n = 0;
7329 }
7330 }
7331 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007332 if (shl != &search_hl && cur != NULL)
7333 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
7335}
7336
7337/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007338 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 * Uses shl->buf.
7340 * Sets shl->lnum and shl->rm contents.
7341 * Note: Assumes a previous match is always before "lnum", unless
7342 * shl->lnum is zero.
7343 * Careful: Any pointers for buffer lines will become invalid.
7344 */
7345 static void
7346next_search_hl(win, shl, lnum, mincol)
7347 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007348 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 linenr_T lnum;
7350 colnr_T mincol; /* minimal column for a match */
7351{
7352 linenr_T l;
7353 colnr_T matchcol;
7354 long nmatched;
7355
7356 if (shl->lnum != 0)
7357 {
7358 /* Check for three situations:
7359 * 1. If the "lnum" is below a previous match, start a new search.
7360 * 2. If the previous match includes "mincol", use it.
7361 * 3. Continue after the previous match.
7362 */
7363 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7364 if (lnum > l)
7365 shl->lnum = 0;
7366 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7367 return;
7368 }
7369
7370 /*
7371 * Repeat searching for a match until one is found that includes "mincol"
7372 * or none is found in this line.
7373 */
7374 called_emsg = FALSE;
7375 for (;;)
7376 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007377#ifdef FEAT_RELTIME
7378 /* Stop searching after passing the time limit. */
7379 if (profile_passed_limit(&(shl->tm)))
7380 {
7381 shl->lnum = 0; /* no match found in time */
7382 break;
7383 }
7384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 /* Three situations:
7386 * 1. No useful previous match: search from start of line.
7387 * 2. Not Vi compatible or empty match: continue at next character.
7388 * Break the loop if this is beyond the end of the line.
7389 * 3. Vi compatible searching: continue at end of previous match.
7390 */
7391 if (shl->lnum == 0)
7392 matchcol = 0;
7393 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7394 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007395 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007397 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007398
7399 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007400 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007401 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007403 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 shl->lnum = 0;
7405 break;
7406 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007407#ifdef FEAT_MBYTE
7408 if (has_mbyte)
7409 matchcol += mb_ptr2len(ml);
7410 else
7411#endif
7412 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 }
7414 else
7415 matchcol = shl->rm.endpos[0].col;
7416
7417 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007418 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7419#ifdef FEAT_RELTIME
7420 &(shl->tm)
7421#else
7422 NULL
7423#endif
7424 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007425 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
7427 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007428 if (shl == &search_hl)
7429 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007430 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007431 vim_regfree(shl->rm.regprog);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007432 SET_NO_HLSEARCH(TRUE);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007435 shl->lnum = 0;
7436 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 break;
7438 }
7439 if (nmatched == 0)
7440 {
7441 shl->lnum = 0; /* no match found */
7442 break;
7443 }
7444 if (shl->rm.startpos[0].lnum > 0
7445 || shl->rm.startpos[0].col >= mincol
7446 || nmatched > 1
7447 || shl->rm.endpos[0].col > mincol)
7448 {
7449 shl->lnum += shl->rm.startpos[0].lnum;
7450 break; /* useful match found */
7451 }
7452 }
7453}
7454#endif
7455
7456 static void
7457screen_start_highlight(attr)
7458 int attr;
7459{
7460 attrentry_T *aep = NULL;
7461
7462 screen_attr = attr;
7463 if (full_screen
7464#ifdef WIN3264
7465 && termcap_active
7466#endif
7467 )
7468 {
7469#ifdef FEAT_GUI
7470 if (gui.in_use)
7471 {
7472 char buf[20];
7473
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007474 /* The GUI handles this internally. */
7475 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 OUT_STR(buf);
7477 }
7478 else
7479#endif
7480 {
7481 if (attr > HL_ALL) /* special HL attr. */
7482 {
7483 if (t_colors > 1)
7484 aep = syn_cterm_attr2entry(attr);
7485 else
7486 aep = syn_term_attr2entry(attr);
7487 if (aep == NULL) /* did ":syntax clear" */
7488 attr = 0;
7489 else
7490 attr = aep->ae_attr;
7491 }
7492 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7493 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007494 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7495 && cterm_normal_fg_bold)
7496 /* If the Normal FG color has BOLD attribute and the new HL
7497 * has a FG color defined, clear BOLD. */
7498 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7500 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007501 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7502 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 out_str(T_US);
7504 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7505 out_str(T_CZH);
7506 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7507 out_str(T_MR);
7508
7509 /*
7510 * Output the color or start string after bold etc., in case the
7511 * bold etc. override the color setting.
7512 */
7513 if (aep != NULL)
7514 {
7515 if (t_colors > 1)
7516 {
7517 if (aep->ae_u.cterm.fg_color)
7518 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7519 if (aep->ae_u.cterm.bg_color)
7520 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7521 }
7522 else
7523 {
7524 if (aep->ae_u.term.start != NULL)
7525 out_str(aep->ae_u.term.start);
7526 }
7527 }
7528 }
7529 }
7530}
7531
7532 void
7533screen_stop_highlight()
7534{
7535 int do_ME = FALSE; /* output T_ME code */
7536
7537 if (screen_attr != 0
7538#ifdef WIN3264
7539 && termcap_active
7540#endif
7541 )
7542 {
7543#ifdef FEAT_GUI
7544 if (gui.in_use)
7545 {
7546 char buf[20];
7547
7548 /* use internal GUI code */
7549 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7550 OUT_STR(buf);
7551 }
7552 else
7553#endif
7554 {
7555 if (screen_attr > HL_ALL) /* special HL attr. */
7556 {
7557 attrentry_T *aep;
7558
7559 if (t_colors > 1)
7560 {
7561 /*
7562 * Assume that t_me restores the original colors!
7563 */
7564 aep = syn_cterm_attr2entry(screen_attr);
7565 if (aep != NULL && (aep->ae_u.cterm.fg_color
7566 || aep->ae_u.cterm.bg_color))
7567 do_ME = TRUE;
7568 }
7569 else
7570 {
7571 aep = syn_term_attr2entry(screen_attr);
7572 if (aep != NULL && aep->ae_u.term.stop != NULL)
7573 {
7574 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7575 do_ME = TRUE;
7576 else
7577 out_str(aep->ae_u.term.stop);
7578 }
7579 }
7580 if (aep == NULL) /* did ":syntax clear" */
7581 screen_attr = 0;
7582 else
7583 screen_attr = aep->ae_attr;
7584 }
7585
7586 /*
7587 * Often all ending-codes are equal to T_ME. Avoid outputting the
7588 * same sequence several times.
7589 */
7590 if (screen_attr & HL_STANDOUT)
7591 {
7592 if (STRCMP(T_SE, T_ME) == 0)
7593 do_ME = TRUE;
7594 else
7595 out_str(T_SE);
7596 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007597 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {
7599 if (STRCMP(T_UE, T_ME) == 0)
7600 do_ME = TRUE;
7601 else
7602 out_str(T_UE);
7603 }
7604 if (screen_attr & HL_ITALIC)
7605 {
7606 if (STRCMP(T_CZR, T_ME) == 0)
7607 do_ME = TRUE;
7608 else
7609 out_str(T_CZR);
7610 }
7611 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7612 out_str(T_ME);
7613
7614 if (t_colors > 1)
7615 {
7616 /* set Normal cterm colors */
7617 if (cterm_normal_fg_color != 0)
7618 term_fg_color(cterm_normal_fg_color - 1);
7619 if (cterm_normal_bg_color != 0)
7620 term_bg_color(cterm_normal_bg_color - 1);
7621 if (cterm_normal_fg_bold)
7622 out_str(T_MD);
7623 }
7624 }
7625 }
7626 screen_attr = 0;
7627}
7628
7629/*
7630 * Reset the colors for a cterm. Used when leaving Vim.
7631 * The machine specific code may override this again.
7632 */
7633 void
7634reset_cterm_colors()
7635{
7636 if (t_colors > 1)
7637 {
7638 /* set Normal cterm colors */
7639 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7640 {
7641 out_str(T_OP);
7642 screen_attr = -1;
7643 }
7644 if (cterm_normal_fg_bold)
7645 {
7646 out_str(T_ME);
7647 screen_attr = -1;
7648 }
7649 }
7650}
7651
7652/*
7653 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7654 * using the attributes from ScreenAttrs["off"].
7655 */
7656 static void
7657screen_char(off, row, col)
7658 unsigned off;
7659 int row;
7660 int col;
7661{
7662 int attr;
7663
7664 /* Check for illegal values, just in case (could happen just after
7665 * resizing). */
7666 if (row >= screen_Rows || col >= screen_Columns)
7667 return;
7668
7669 /* Outputting the last character on the screen may scrollup the screen.
7670 * Don't to it! Mark the character invalid (update it when scrolled up) */
7671 if (row == screen_Rows - 1 && col == screen_Columns - 1
7672#ifdef FEAT_RIGHTLEFT
7673 /* account for first command-line character in rightleft mode */
7674 && !cmdmsg_rl
7675#endif
7676 )
7677 {
7678 ScreenAttrs[off] = (sattr_T)-1;
7679 return;
7680 }
7681
7682 /*
7683 * Stop highlighting first, so it's easier to move the cursor.
7684 */
7685#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7686 if (screen_char_attr != 0)
7687 attr = screen_char_attr;
7688 else
7689#endif
7690 attr = ScreenAttrs[off];
7691 if (screen_attr != attr)
7692 screen_stop_highlight();
7693
7694 windgoto(row, col);
7695
7696 if (screen_attr != attr)
7697 screen_start_highlight(attr);
7698
7699#ifdef FEAT_MBYTE
7700 if (enc_utf8 && ScreenLinesUC[off] != 0)
7701 {
7702 char_u buf[MB_MAXBYTES + 1];
7703
7704 /* Convert UTF-8 character to bytes and write it. */
7705
7706 buf[utfc_char2bytes(off, buf)] = NUL;
7707
7708 out_str(buf);
7709 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7710 ++screen_cur_col;
7711 }
7712 else
7713#endif
7714 {
7715#ifdef FEAT_MBYTE
7716 out_flush_check();
7717#endif
7718 out_char(ScreenLines[off]);
7719#ifdef FEAT_MBYTE
7720 /* double-byte character in single-width cell */
7721 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7722 out_char(ScreenLines2[off]);
7723#endif
7724 }
7725
7726 screen_cur_col++;
7727}
7728
7729#ifdef FEAT_MBYTE
7730
7731/*
7732 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7733 * on the screen at position 'row' and 'col'.
7734 * The attributes of the first byte is used for all. This is required to
7735 * output the two bytes of a double-byte character with nothing in between.
7736 */
7737 static void
7738screen_char_2(off, row, col)
7739 unsigned off;
7740 int row;
7741 int col;
7742{
7743 /* Check for illegal values (could be wrong when screen was resized). */
7744 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7745 return;
7746
7747 /* Outputting the last character on the screen may scrollup the screen.
7748 * Don't to it! Mark the character invalid (update it when scrolled up) */
7749 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7750 {
7751 ScreenAttrs[off] = (sattr_T)-1;
7752 return;
7753 }
7754
7755 /* Output the first byte normally (positions the cursor), then write the
7756 * second byte directly. */
7757 screen_char(off, row, col);
7758 out_char(ScreenLines[off + 1]);
7759 ++screen_cur_col;
7760}
7761#endif
7762
7763#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7764/*
7765 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7766 * This uses the contents of ScreenLines[] and doesn't change it.
7767 */
7768 void
7769screen_draw_rectangle(row, col, height, width, invert)
7770 int row;
7771 int col;
7772 int height;
7773 int width;
7774 int invert;
7775{
7776 int r, c;
7777 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007778#ifdef FEAT_MBYTE
7779 int max_off;
7780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007782 /* Can't use ScreenLines unless initialized */
7783 if (ScreenLines == NULL)
7784 return;
7785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (invert)
7787 screen_char_attr = HL_INVERSE;
7788 for (r = row; r < row + height; ++r)
7789 {
7790 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007791#ifdef FEAT_MBYTE
7792 max_off = off + screen_Columns;
7793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 for (c = col; c < col + width; ++c)
7795 {
7796#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007797 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {
7799 screen_char_2(off + c, r, c);
7800 ++c;
7801 }
7802 else
7803#endif
7804 {
7805 screen_char(off + c, r, c);
7806#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007807 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 ++c;
7809#endif
7810 }
7811 }
7812 }
7813 screen_char_attr = 0;
7814}
7815#endif
7816
7817#ifdef FEAT_VERTSPLIT
7818/*
7819 * Redraw the characters for a vertically split window.
7820 */
7821 static void
7822redraw_block(row, end, wp)
7823 int row;
7824 int end;
7825 win_T *wp;
7826{
7827 int col;
7828 int width;
7829
7830# ifdef FEAT_CLIPBOARD
7831 clip_may_clear_selection(row, end - 1);
7832# endif
7833
7834 if (wp == NULL)
7835 {
7836 col = 0;
7837 width = Columns;
7838 }
7839 else
7840 {
7841 col = wp->w_wincol;
7842 width = wp->w_width;
7843 }
7844 screen_draw_rectangle(row, col, end - row, width, FALSE);
7845}
7846#endif
7847
7848/*
7849 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7850 * with character 'c1' in first column followed by 'c2' in the other columns.
7851 * Use attributes 'attr'.
7852 */
7853 void
7854screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7855 int start_row, end_row;
7856 int start_col, end_col;
7857 int c1, c2;
7858 int attr;
7859{
7860 int row;
7861 int col;
7862 int off;
7863 int end_off;
7864 int did_delete;
7865 int c;
7866 int norm_term;
7867#if defined(FEAT_GUI) || defined(UNIX)
7868 int force_next = FALSE;
7869#endif
7870
7871 if (end_row > screen_Rows) /* safety check */
7872 end_row = screen_Rows;
7873 if (end_col > screen_Columns) /* safety check */
7874 end_col = screen_Columns;
7875 if (ScreenLines == NULL
7876 || start_row >= end_row
7877 || start_col >= end_col) /* nothing to do */
7878 return;
7879
7880 /* it's a "normal" terminal when not in a GUI or cterm */
7881 norm_term = (
7882#ifdef FEAT_GUI
7883 !gui.in_use &&
7884#endif
7885 t_colors <= 1);
7886 for (row = start_row; row < end_row; ++row)
7887 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007888#ifdef FEAT_MBYTE
7889 if (has_mbyte
7890# ifdef FEAT_GUI
7891 && !gui.in_use
7892# endif
7893 )
7894 {
7895 /* When drawing over the right halve of a double-wide char clear
7896 * out the left halve. When drawing over the left halve of a
7897 * double wide-char clear out the right halve. Only needed in a
7898 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007899 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007900 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007901 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007902 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007903 }
7904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 /*
7906 * Try to use delete-line termcap code, when no attributes or in a
7907 * "normal" terminal, where a bold/italic space is just a
7908 * space.
7909 */
7910 did_delete = FALSE;
7911 if (c2 == ' '
7912 && end_col == Columns
7913 && can_clear(T_CE)
7914 && (attr == 0
7915 || (norm_term
7916 && attr <= HL_ALL
7917 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7918 {
7919 /*
7920 * check if we really need to clear something
7921 */
7922 col = start_col;
7923 if (c1 != ' ') /* don't clear first char */
7924 ++col;
7925
7926 off = LineOffset[row] + col;
7927 end_off = LineOffset[row] + end_col;
7928
7929 /* skip blanks (used often, keep it fast!) */
7930#ifdef FEAT_MBYTE
7931 if (enc_utf8)
7932 while (off < end_off && ScreenLines[off] == ' '
7933 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7934 ++off;
7935 else
7936#endif
7937 while (off < end_off && ScreenLines[off] == ' '
7938 && ScreenAttrs[off] == 0)
7939 ++off;
7940 if (off < end_off) /* something to be cleared */
7941 {
7942 col = off - LineOffset[row];
7943 screen_stop_highlight();
7944 term_windgoto(row, col);/* clear rest of this screen line */
7945 out_str(T_CE);
7946 screen_start(); /* don't know where cursor is now */
7947 col = end_col - col;
7948 while (col--) /* clear chars in ScreenLines */
7949 {
7950 ScreenLines[off] = ' ';
7951#ifdef FEAT_MBYTE
7952 if (enc_utf8)
7953 ScreenLinesUC[off] = 0;
7954#endif
7955 ScreenAttrs[off] = 0;
7956 ++off;
7957 }
7958 }
7959 did_delete = TRUE; /* the chars are cleared now */
7960 }
7961
7962 off = LineOffset[row] + start_col;
7963 c = c1;
7964 for (col = start_col; col < end_col; ++col)
7965 {
7966 if (ScreenLines[off] != c
7967#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007968 || (enc_utf8 && (int)ScreenLinesUC[off]
7969 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970#endif
7971 || ScreenAttrs[off] != attr
7972#if defined(FEAT_GUI) || defined(UNIX)
7973 || force_next
7974#endif
7975 )
7976 {
7977#if defined(FEAT_GUI) || defined(UNIX)
7978 /* The bold trick may make a single row of pixels appear in
7979 * the next character. When a bold character is removed, the
7980 * next character should be redrawn too. This happens for our
7981 * own GUI and for some xterms. */
7982 if (
7983# ifdef FEAT_GUI
7984 gui.in_use
7985# endif
7986# if defined(FEAT_GUI) && defined(UNIX)
7987 ||
7988# endif
7989# ifdef UNIX
7990 term_is_xterm
7991# endif
7992 )
7993 {
7994 if (ScreenLines[off] != ' '
7995 && (ScreenAttrs[off] > HL_ALL
7996 || ScreenAttrs[off] & HL_BOLD))
7997 force_next = TRUE;
7998 else
7999 force_next = FALSE;
8000 }
8001#endif
8002 ScreenLines[off] = c;
8003#ifdef FEAT_MBYTE
8004 if (enc_utf8)
8005 {
8006 if (c >= 0x80)
8007 {
8008 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008009 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011 else
8012 ScreenLinesUC[off] = 0;
8013 }
8014#endif
8015 ScreenAttrs[off] = attr;
8016 if (!did_delete || c != ' ')
8017 screen_char(off, row, col);
8018 }
8019 ++off;
8020 if (col == start_col)
8021 {
8022 if (did_delete)
8023 break;
8024 c = c2;
8025 }
8026 }
8027 if (end_col == Columns)
8028 LineWraps[row] = FALSE;
8029 if (row == Rows - 1) /* overwritten the command line */
8030 {
8031 redraw_cmdline = TRUE;
8032 if (c1 == ' ' && c2 == ' ')
8033 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008034 if (start_col == 0)
8035 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037 }
8038}
8039
8040/*
8041 * Check if there should be a delay. Used before clearing or redrawing the
8042 * screen or the command line.
8043 */
8044 void
8045check_for_delay(check_msg_scroll)
8046 int check_msg_scroll;
8047{
8048 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8049 && !did_wait_return
8050 && emsg_silent == 0)
8051 {
8052 out_flush();
8053 ui_delay(1000L, TRUE);
8054 emsg_on_display = FALSE;
8055 if (check_msg_scroll)
8056 msg_scroll = FALSE;
8057 }
8058}
8059
8060/*
8061 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008062 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 * Returns TRUE if there is a valid screen to write to.
8064 * Returns FALSE when starting up and screen not initialized yet.
8065 */
8066 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008067screen_valid(doclear)
8068 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008070 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 return (ScreenLines != NULL);
8072}
8073
8074/*
8075 * Resize the shell to Rows and Columns.
8076 * Allocate ScreenLines[] and associated items.
8077 *
8078 * There may be some time between setting Rows and Columns and (re)allocating
8079 * ScreenLines[]. This happens when starting up and when (manually) changing
8080 * the shell size. Always use screen_Rows and screen_Columns to access items
8081 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8082 * final size of the shell is needed.
8083 */
8084 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008085screenalloc(doclear)
8086 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087{
8088 int new_row, old_row;
8089#ifdef FEAT_GUI
8090 int old_Rows;
8091#endif
8092 win_T *wp;
8093 int outofmem = FALSE;
8094 int len;
8095 schar_T *new_ScreenLines;
8096#ifdef FEAT_MBYTE
8097 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008098 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008100 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101#endif
8102 sattr_T *new_ScreenAttrs;
8103 unsigned *new_LineOffset;
8104 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008105#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008106 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008107 tabpage_T *tp;
8108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008110 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008111#ifdef FEAT_AUTOCMD
8112 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008114retry:
8115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 /*
8117 * Allocation of the screen buffers is done only when the size changes and
8118 * when Rows and Columns have been set and we have started doing full
8119 * screen stuff.
8120 */
8121 if ((ScreenLines != NULL
8122 && Rows == screen_Rows
8123 && Columns == screen_Columns
8124#ifdef FEAT_MBYTE
8125 && enc_utf8 == (ScreenLinesUC != NULL)
8126 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008127 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128#endif
8129 )
8130 || Rows == 0
8131 || Columns == 0
8132 || (!full_screen && ScreenLines == NULL))
8133 return;
8134
8135 /*
8136 * It's possible that we produce an out-of-memory message below, which
8137 * will cause this function to be called again. To break the loop, just
8138 * return here.
8139 */
8140 if (entered)
8141 return;
8142 entered = TRUE;
8143
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008144 /*
8145 * Note that the window sizes are updated before reallocating the arrays,
8146 * thus we must not redraw here!
8147 */
8148 ++RedrawingDisabled;
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 win_new_shellsize(); /* fit the windows in the new sized shell */
8151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 comp_col(); /* recompute columns for shown command and ruler */
8153
8154 /*
8155 * We're changing the size of the screen.
8156 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8157 * - Move lines from the old arrays into the new arrays, clear extra
8158 * lines (unless the screen is going to be cleared).
8159 * - Free the old arrays.
8160 *
8161 * If anything fails, make ScreenLines NULL, so we don't do anything!
8162 * Continuing with the old ScreenLines may result in a crash, because the
8163 * size is wrong.
8164 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008165 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008167#ifdef FEAT_AUTOCMD
8168 if (aucmd_win != NULL)
8169 win_free_lsize(aucmd_win);
8170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171
8172 new_ScreenLines = (schar_T *)lalloc((long_u)(
8173 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8174#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008175 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (enc_utf8)
8177 {
8178 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8179 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008180 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008181 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8183 }
8184 if (enc_dbcs == DBCS_JPNU)
8185 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8186 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8187#endif
8188 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8189 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8190 new_LineOffset = (unsigned *)lalloc((long_u)(
8191 Rows * sizeof(unsigned)), FALSE);
8192 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008193#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008194 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008197 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 if (win_alloc_lines(wp) == FAIL)
8200 {
8201 outofmem = TRUE;
8202#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008203 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204#endif
8205 }
8206 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008207#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008208 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8209 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008210 outofmem = TRUE;
8211#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008212#ifdef FEAT_WINDOWS
8213give_up:
8214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008216#ifdef FEAT_MBYTE
8217 for (i = 0; i < p_mco; ++i)
8218 if (new_ScreenLinesC[i] == NULL)
8219 break;
8220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (new_ScreenLines == NULL
8222#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008223 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8225#endif
8226 || new_ScreenAttrs == NULL
8227 || new_LineOffset == NULL
8228 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008229#ifdef FEAT_WINDOWS
8230 || new_TabPageIdxs == NULL
8231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 || outofmem)
8233 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008234 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008235 {
8236 /* guess the size */
8237 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8238
8239 /* Remember we did this to avoid getting outofmem messages over
8240 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008241 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 vim_free(new_ScreenLines);
8244 new_ScreenLines = NULL;
8245#ifdef FEAT_MBYTE
8246 vim_free(new_ScreenLinesUC);
8247 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008248 for (i = 0; i < p_mco; ++i)
8249 {
8250 vim_free(new_ScreenLinesC[i]);
8251 new_ScreenLinesC[i] = NULL;
8252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 vim_free(new_ScreenLines2);
8254 new_ScreenLines2 = NULL;
8255#endif
8256 vim_free(new_ScreenAttrs);
8257 new_ScreenAttrs = NULL;
8258 vim_free(new_LineOffset);
8259 new_LineOffset = NULL;
8260 vim_free(new_LineWraps);
8261 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008262#ifdef FEAT_WINDOWS
8263 vim_free(new_TabPageIdxs);
8264 new_TabPageIdxs = NULL;
8265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 }
8267 else
8268 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008269 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008270
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 for (new_row = 0; new_row < Rows; ++new_row)
8272 {
8273 new_LineOffset[new_row] = new_row * Columns;
8274 new_LineWraps[new_row] = FALSE;
8275
8276 /*
8277 * If the screen is not going to be cleared, copy as much as
8278 * possible from the old screen to the new one and clear the rest
8279 * (used when resizing the window at the "--more--" prompt or when
8280 * executing an external command, for the GUI).
8281 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008282 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284 (void)vim_memset(new_ScreenLines + new_row * Columns,
8285 ' ', (size_t)Columns * sizeof(schar_T));
8286#ifdef FEAT_MBYTE
8287 if (enc_utf8)
8288 {
8289 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8290 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008291 for (i = 0; i < p_mco; ++i)
8292 (void)vim_memset(new_ScreenLinesC[i]
8293 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 0, (size_t)Columns * sizeof(u8char_T));
8295 }
8296 if (enc_dbcs == DBCS_JPNU)
8297 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8298 0, (size_t)Columns * sizeof(schar_T));
8299#endif
8300 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8301 0, (size_t)Columns * sizeof(sattr_T));
8302 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008303 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
8305 if (screen_Columns < Columns)
8306 len = screen_Columns;
8307 else
8308 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008309#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008310 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008311 * may be invalid now. Also when p_mco changes. */
8312 if (!(enc_utf8 && ScreenLinesUC == NULL)
8313 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008314#endif
8315 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8316 ScreenLines + LineOffset[old_row],
8317 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008319 if (enc_utf8 && ScreenLinesUC != NULL
8320 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
8322 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8323 ScreenLinesUC + LineOffset[old_row],
8324 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008325 for (i = 0; i < p_mco; ++i)
8326 mch_memmove(new_ScreenLinesC[i]
8327 + new_LineOffset[new_row],
8328 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 (size_t)len * sizeof(u8char_T));
8330 }
8331 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8332 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8333 ScreenLines2 + LineOffset[old_row],
8334 (size_t)len * sizeof(schar_T));
8335#endif
8336 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8337 ScreenAttrs + LineOffset[old_row],
8338 (size_t)len * sizeof(sattr_T));
8339 }
8340 }
8341 }
8342 /* Use the last line of the screen for the current line. */
8343 current_ScreenLine = new_ScreenLines + Rows * Columns;
8344 }
8345
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008346 free_screenlines();
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 ScreenLines = new_ScreenLines;
8349#ifdef FEAT_MBYTE
8350 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008351 for (i = 0; i < p_mco; ++i)
8352 ScreenLinesC[i] = new_ScreenLinesC[i];
8353 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 ScreenLines2 = new_ScreenLines2;
8355#endif
8356 ScreenAttrs = new_ScreenAttrs;
8357 LineOffset = new_LineOffset;
8358 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008359#ifdef FEAT_WINDOWS
8360 TabPageIdxs = new_TabPageIdxs;
8361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363 /* It's important that screen_Rows and screen_Columns reflect the actual
8364 * size of ScreenLines[]. Set them before calling anything. */
8365#ifdef FEAT_GUI
8366 old_Rows = screen_Rows;
8367#endif
8368 screen_Rows = Rows;
8369 screen_Columns = Columns;
8370
8371 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008372 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 screenclear2();
8374
8375#ifdef FEAT_GUI
8376 else if (gui.in_use
8377 && !gui.starting
8378 && ScreenLines != NULL
8379 && old_Rows != Rows)
8380 {
8381 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8382 /*
8383 * Adjust the position of the cursor, for when executing an external
8384 * command.
8385 */
8386 if (msg_row >= Rows) /* Rows got smaller */
8387 msg_row = Rows - 1; /* put cursor at last row */
8388 else if (Rows > old_Rows) /* Rows got bigger */
8389 msg_row += Rows - old_Rows; /* put cursor in same place */
8390 if (msg_col >= Columns) /* Columns got smaller */
8391 msg_col = Columns - 1; /* put cursor at last column */
8392 }
8393#endif
8394
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008396 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008397
8398#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008399 /*
8400 * Do not apply autocommands more than 3 times to avoid an endless loop
8401 * in case applying autocommands always changes Rows or Columns.
8402 */
8403 if (starting == 0 && ++retry_count <= 3)
8404 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008405 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008406 /* In rare cases, autocommands may have altered Rows or Columns,
8407 * jump back to check if we need to allocate the screen again. */
8408 goto retry;
8409 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412
8413 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008414free_screenlines()
8415{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008416#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008417 int i;
8418
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008419 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008420 for (i = 0; i < Screen_mco; ++i)
8421 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008422 vim_free(ScreenLines2);
8423#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008424 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008425 vim_free(ScreenAttrs);
8426 vim_free(LineOffset);
8427 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008428#ifdef FEAT_WINDOWS
8429 vim_free(TabPageIdxs);
8430#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008431}
8432
8433 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434screenclear()
8435{
8436 check_for_delay(FALSE);
8437 screenalloc(FALSE); /* allocate screen buffers if size changed */
8438 screenclear2(); /* clear the screen */
8439}
8440
8441 static void
8442screenclear2()
8443{
8444 int i;
8445
8446 if (starting == NO_SCREEN || ScreenLines == NULL
8447#ifdef FEAT_GUI
8448 || (gui.in_use && gui.starting)
8449#endif
8450 )
8451 return;
8452
8453#ifdef FEAT_GUI
8454 if (!gui.in_use)
8455#endif
8456 screen_attr = -1; /* force setting the Normal colors */
8457 screen_stop_highlight(); /* don't want highlighting here */
8458
8459#ifdef FEAT_CLIPBOARD
8460 /* disable selection without redrawing it */
8461 clip_scroll_selection(9999);
8462#endif
8463
8464 /* blank out ScreenLines */
8465 for (i = 0; i < Rows; ++i)
8466 {
8467 lineclear(LineOffset[i], (int)Columns);
8468 LineWraps[i] = FALSE;
8469 }
8470
8471 if (can_clear(T_CL))
8472 {
8473 out_str(T_CL); /* clear the display */
8474 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008475 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477 else
8478 {
8479 /* can't clear the screen, mark all chars with invalid attributes */
8480 for (i = 0; i < Rows; ++i)
8481 lineinvalid(LineOffset[i], (int)Columns);
8482 clear_cmdline = TRUE;
8483 }
8484
8485 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8486
8487 win_rest_invalid(firstwin);
8488 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008489#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008490 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 if (must_redraw == CLEAR) /* no need to clear again */
8493 must_redraw = NOT_VALID;
8494 compute_cmdrow();
8495 msg_row = cmdline_row; /* put cursor on last line for messages */
8496 msg_col = 0;
8497 screen_start(); /* don't know where cursor is now */
8498 msg_scrolled = 0; /* can't scroll back */
8499 msg_didany = FALSE;
8500 msg_didout = FALSE;
8501}
8502
8503/*
8504 * Clear one line in ScreenLines.
8505 */
8506 static void
8507lineclear(off, width)
8508 unsigned off;
8509 int width;
8510{
8511 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8512#ifdef FEAT_MBYTE
8513 if (enc_utf8)
8514 (void)vim_memset(ScreenLinesUC + off, 0,
8515 (size_t)width * sizeof(u8char_T));
8516#endif
8517 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8518}
8519
8520/*
8521 * Mark one line in ScreenLines invalid by setting the attributes to an
8522 * invalid value.
8523 */
8524 static void
8525lineinvalid(off, width)
8526 unsigned off;
8527 int width;
8528{
8529 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8530}
8531
8532#ifdef FEAT_VERTSPLIT
8533/*
8534 * Copy part of a Screenline for vertically split window "wp".
8535 */
8536 static void
8537linecopy(to, from, wp)
8538 int to;
8539 int from;
8540 win_T *wp;
8541{
8542 unsigned off_to = LineOffset[to] + wp->w_wincol;
8543 unsigned off_from = LineOffset[from] + wp->w_wincol;
8544
8545 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8546 wp->w_width * sizeof(schar_T));
8547# ifdef FEAT_MBYTE
8548 if (enc_utf8)
8549 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008550 int i;
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8553 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008554 for (i = 0; i < p_mco; ++i)
8555 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8556 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
8558 if (enc_dbcs == DBCS_JPNU)
8559 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8560 wp->w_width * sizeof(schar_T));
8561# endif
8562 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8563 wp->w_width * sizeof(sattr_T));
8564}
8565#endif
8566
8567/*
8568 * Return TRUE if clearing with term string "p" would work.
8569 * It can't work when the string is empty or it won't set the right background.
8570 */
8571 int
8572can_clear(p)
8573 char_u *p;
8574{
8575 return (*p != NUL && (t_colors <= 1
8576#ifdef FEAT_GUI
8577 || gui.in_use
8578#endif
8579 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8580}
8581
8582/*
8583 * Reset cursor position. Use whenever cursor was moved because of outputting
8584 * something directly to the screen (shell commands) or a terminal control
8585 * code.
8586 */
8587 void
8588screen_start()
8589{
8590 screen_cur_row = screen_cur_col = 9999;
8591}
8592
8593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 * Move the cursor to position "row","col" in the screen.
8595 * This tries to find the most efficient way to move, minimizing the number of
8596 * characters sent to the terminal.
8597 */
8598 void
8599windgoto(row, col)
8600 int row;
8601 int col;
8602{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008603 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 int i;
8605 int plan;
8606 int cost;
8607 int wouldbe_col;
8608 int noinvcurs;
8609 char_u *bs;
8610 int goto_cost;
8611 int attr;
8612
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008613#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8615
8616#define PLAN_LE 1
8617#define PLAN_CR 2
8618#define PLAN_NL 3
8619#define PLAN_WRITE 4
8620 /* Can't use ScreenLines unless initialized */
8621 if (ScreenLines == NULL)
8622 return;
8623
8624 if (col != screen_cur_col || row != screen_cur_row)
8625 {
8626 /* Check for valid position. */
8627 if (row < 0) /* window without text lines? */
8628 row = 0;
8629 if (row >= screen_Rows)
8630 row = screen_Rows - 1;
8631 if (col >= screen_Columns)
8632 col = screen_Columns - 1;
8633
8634 /* check if no cursor movement is allowed in highlight mode */
8635 if (screen_attr && *T_MS == NUL)
8636 noinvcurs = HIGHL_COST;
8637 else
8638 noinvcurs = 0;
8639 goto_cost = GOTO_COST + noinvcurs;
8640
8641 /*
8642 * Plan how to do the positioning:
8643 * 1. Use CR to move it to column 0, same row.
8644 * 2. Use T_LE to move it a few columns to the left.
8645 * 3. Use NL to move a few lines down, column 0.
8646 * 4. Move a few columns to the right with T_ND or by writing chars.
8647 *
8648 * Don't do this if the cursor went beyond the last column, the cursor
8649 * position is unknown then (some terminals wrap, some don't )
8650 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008651 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 * characters to move the cursor to the right.
8653 */
8654 if (row >= screen_cur_row && screen_cur_col < Columns)
8655 {
8656 /*
8657 * If the cursor is in the same row, bigger col, we can use CR
8658 * or T_LE.
8659 */
8660 bs = NULL; /* init for GCC */
8661 attr = screen_attr;
8662 if (row == screen_cur_row && col < screen_cur_col)
8663 {
8664 /* "le" is preferred over "bc", because "bc" is obsolete */
8665 if (*T_LE)
8666 bs = T_LE; /* "cursor left" */
8667 else
8668 bs = T_BC; /* "backspace character (old) */
8669 if (*bs)
8670 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8671 else
8672 cost = 999;
8673 if (col + 1 < cost) /* using CR is less characters */
8674 {
8675 plan = PLAN_CR;
8676 wouldbe_col = 0;
8677 cost = 1; /* CR is just one character */
8678 }
8679 else
8680 {
8681 plan = PLAN_LE;
8682 wouldbe_col = col;
8683 }
8684 if (noinvcurs) /* will stop highlighting */
8685 {
8686 cost += noinvcurs;
8687 attr = 0;
8688 }
8689 }
8690
8691 /*
8692 * If the cursor is above where we want to be, we can use CR LF.
8693 */
8694 else if (row > screen_cur_row)
8695 {
8696 plan = PLAN_NL;
8697 wouldbe_col = 0;
8698 cost = (row - screen_cur_row) * 2; /* CR LF */
8699 if (noinvcurs) /* will stop highlighting */
8700 {
8701 cost += noinvcurs;
8702 attr = 0;
8703 }
8704 }
8705
8706 /*
8707 * If the cursor is in the same row, smaller col, just use write.
8708 */
8709 else
8710 {
8711 plan = PLAN_WRITE;
8712 wouldbe_col = screen_cur_col;
8713 cost = 0;
8714 }
8715
8716 /*
8717 * Check if any characters that need to be written have the
8718 * correct attributes. Also avoid UTF-8 characters.
8719 */
8720 i = col - wouldbe_col;
8721 if (i > 0)
8722 cost += i;
8723 if (cost < goto_cost && i > 0)
8724 {
8725 /*
8726 * Check if the attributes are correct without additionally
8727 * stopping highlighting.
8728 */
8729 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8730 while (i && *p++ == attr)
8731 --i;
8732 if (i != 0)
8733 {
8734 /*
8735 * Try if it works when highlighting is stopped here.
8736 */
8737 if (*--p == 0)
8738 {
8739 cost += noinvcurs;
8740 while (i && *p++ == 0)
8741 --i;
8742 }
8743 if (i != 0)
8744 cost = 999; /* different attributes, don't do it */
8745 }
8746#ifdef FEAT_MBYTE
8747 if (enc_utf8)
8748 {
8749 /* Don't use an UTF-8 char for positioning, it's slow. */
8750 for (i = wouldbe_col; i < col; ++i)
8751 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8752 {
8753 cost = 999;
8754 break;
8755 }
8756 }
8757#endif
8758 }
8759
8760 /*
8761 * We can do it without term_windgoto()!
8762 */
8763 if (cost < goto_cost)
8764 {
8765 if (plan == PLAN_LE)
8766 {
8767 if (noinvcurs)
8768 screen_stop_highlight();
8769 while (screen_cur_col > col)
8770 {
8771 out_str(bs);
8772 --screen_cur_col;
8773 }
8774 }
8775 else if (plan == PLAN_CR)
8776 {
8777 if (noinvcurs)
8778 screen_stop_highlight();
8779 out_char('\r');
8780 screen_cur_col = 0;
8781 }
8782 else if (plan == PLAN_NL)
8783 {
8784 if (noinvcurs)
8785 screen_stop_highlight();
8786 while (screen_cur_row < row)
8787 {
8788 out_char('\n');
8789 ++screen_cur_row;
8790 }
8791 screen_cur_col = 0;
8792 }
8793
8794 i = col - screen_cur_col;
8795 if (i > 0)
8796 {
8797 /*
8798 * Use cursor-right if it's one character only. Avoids
8799 * removing a line of pixels from the last bold char, when
8800 * using the bold trick in the GUI.
8801 */
8802 if (T_ND[0] != NUL && T_ND[1] == NUL)
8803 {
8804 while (i-- > 0)
8805 out_char(*T_ND);
8806 }
8807 else
8808 {
8809 int off;
8810
8811 off = LineOffset[row] + screen_cur_col;
8812 while (i-- > 0)
8813 {
8814 if (ScreenAttrs[off] != screen_attr)
8815 screen_stop_highlight();
8816#ifdef FEAT_MBYTE
8817 out_flush_check();
8818#endif
8819 out_char(ScreenLines[off]);
8820#ifdef FEAT_MBYTE
8821 if (enc_dbcs == DBCS_JPNU
8822 && ScreenLines[off] == 0x8e)
8823 out_char(ScreenLines2[off]);
8824#endif
8825 ++off;
8826 }
8827 }
8828 }
8829 }
8830 }
8831 else
8832 cost = 999;
8833
8834 if (cost >= goto_cost)
8835 {
8836 if (noinvcurs)
8837 screen_stop_highlight();
8838 if (row == screen_cur_row && (col > screen_cur_col) &&
8839 *T_CRI != NUL)
8840 term_cursor_right(col - screen_cur_col);
8841 else
8842 term_windgoto(row, col);
8843 }
8844 screen_cur_row = row;
8845 screen_cur_col = col;
8846 }
8847}
8848
8849/*
8850 * Set cursor to its position in the current window.
8851 */
8852 void
8853setcursor()
8854{
8855 if (redrawing())
8856 {
8857 validate_cursor();
8858 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8859 W_WINCOL(curwin) + (
8860#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008861 /* With 'rightleft' set and the cursor on a double-wide
8862 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8864# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008865 (has_mbyte
8866 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8867 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868# endif
8869 1)) :
8870#endif
8871 curwin->w_wcol));
8872 }
8873}
8874
8875
8876/*
8877 * insert 'line_count' lines at 'row' in window 'wp'
8878 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8879 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8880 * scrolling.
8881 * Returns FAIL if the lines are not inserted, OK for success.
8882 */
8883 int
8884win_ins_lines(wp, row, line_count, invalid, mayclear)
8885 win_T *wp;
8886 int row;
8887 int line_count;
8888 int invalid;
8889 int mayclear;
8890{
8891 int did_delete;
8892 int nextrow;
8893 int lastrow;
8894 int retval;
8895
8896 if (invalid)
8897 wp->w_lines_valid = 0;
8898
8899 if (wp->w_height < 5)
8900 return FAIL;
8901
8902 if (line_count > wp->w_height - row)
8903 line_count = wp->w_height - row;
8904
8905 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8906 if (retval != MAYBE)
8907 return retval;
8908
8909 /*
8910 * If there is a next window or a status line, we first try to delete the
8911 * lines at the bottom to avoid messing what is after the window.
8912 * If this fails and there are following windows, don't do anything to avoid
8913 * messing up those windows, better just redraw.
8914 */
8915 did_delete = FALSE;
8916#ifdef FEAT_WINDOWS
8917 if (wp->w_next != NULL || wp->w_status_height)
8918 {
8919 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8920 line_count, (int)Rows, FALSE, NULL) == OK)
8921 did_delete = TRUE;
8922 else if (wp->w_next)
8923 return FAIL;
8924 }
8925#endif
8926 /*
8927 * if no lines deleted, blank the lines that will end up below the window
8928 */
8929 if (!did_delete)
8930 {
8931#ifdef FEAT_WINDOWS
8932 wp->w_redr_status = TRUE;
8933#endif
8934 redraw_cmdline = TRUE;
8935 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8936 lastrow = nextrow + line_count;
8937 if (lastrow > Rows)
8938 lastrow = Rows;
8939 screen_fill(nextrow - line_count, lastrow - line_count,
8940 W_WINCOL(wp), (int)W_ENDCOL(wp),
8941 ' ', ' ', 0);
8942 }
8943
8944 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8945 == FAIL)
8946 {
8947 /* deletion will have messed up other windows */
8948 if (did_delete)
8949 {
8950#ifdef FEAT_WINDOWS
8951 wp->w_redr_status = TRUE;
8952#endif
8953 win_rest_invalid(W_NEXT(wp));
8954 }
8955 return FAIL;
8956 }
8957
8958 return OK;
8959}
8960
8961/*
8962 * delete "line_count" window lines at "row" in window "wp"
8963 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8964 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8965 * scrolling
8966 * Return OK for success, FAIL if the lines are not deleted.
8967 */
8968 int
8969win_del_lines(wp, row, line_count, invalid, mayclear)
8970 win_T *wp;
8971 int row;
8972 int line_count;
8973 int invalid;
8974 int mayclear;
8975{
8976 int retval;
8977
8978 if (invalid)
8979 wp->w_lines_valid = 0;
8980
8981 if (line_count > wp->w_height - row)
8982 line_count = wp->w_height - row;
8983
8984 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8985 if (retval != MAYBE)
8986 return retval;
8987
8988 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8989 (int)Rows, FALSE, NULL) == FAIL)
8990 return FAIL;
8991
8992#ifdef FEAT_WINDOWS
8993 /*
8994 * If there are windows or status lines below, try to put them at the
8995 * correct place. If we can't do that, they have to be redrawn.
8996 */
8997 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8998 {
8999 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9000 line_count, (int)Rows, NULL) == FAIL)
9001 {
9002 wp->w_redr_status = TRUE;
9003 win_rest_invalid(wp->w_next);
9004 }
9005 }
9006 /*
9007 * If this is the last window and there is no status line, redraw the
9008 * command line later.
9009 */
9010 else
9011#endif
9012 redraw_cmdline = TRUE;
9013 return OK;
9014}
9015
9016/*
9017 * Common code for win_ins_lines() and win_del_lines().
9018 * Returns OK or FAIL when the work has been done.
9019 * Returns MAYBE when not finished yet.
9020 */
9021 static int
9022win_do_lines(wp, row, line_count, mayclear, del)
9023 win_T *wp;
9024 int row;
9025 int line_count;
9026 int mayclear;
9027 int del;
9028{
9029 int retval;
9030
9031 if (!redrawing() || line_count <= 0)
9032 return FAIL;
9033
9034 /* only a few lines left: redraw is faster */
9035 if (mayclear && Rows - line_count < 5
9036#ifdef FEAT_VERTSPLIT
9037 && wp->w_width == Columns
9038#endif
9039 )
9040 {
9041 screenclear(); /* will set wp->w_lines_valid to 0 */
9042 return FAIL;
9043 }
9044
9045 /*
9046 * Delete all remaining lines
9047 */
9048 if (row + line_count >= wp->w_height)
9049 {
9050 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9051 W_WINCOL(wp), (int)W_ENDCOL(wp),
9052 ' ', ' ', 0);
9053 return OK;
9054 }
9055
9056 /*
9057 * when scrolling, the message on the command line should be cleared,
9058 * otherwise it will stay there forever.
9059 */
9060 clear_cmdline = TRUE;
9061
9062 /*
9063 * If the terminal can set a scroll region, use that.
9064 * Always do this in a vertically split window. This will redraw from
9065 * ScreenLines[] when t_CV isn't defined. That's faster than using
9066 * win_line().
9067 * Don't use a scroll region when we are going to redraw the text, writing
9068 * a character in the lower right corner of the scroll region causes a
9069 * scroll-up in the DJGPP version.
9070 */
9071 if (scroll_region
9072#ifdef FEAT_VERTSPLIT
9073 || W_WIDTH(wp) != Columns
9074#endif
9075 )
9076 {
9077#ifdef FEAT_VERTSPLIT
9078 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9079#endif
9080 scroll_region_set(wp, row);
9081 if (del)
9082 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9083 wp->w_height - row, FALSE, wp);
9084 else
9085 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9086 wp->w_height - row, wp);
9087#ifdef FEAT_VERTSPLIT
9088 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9089#endif
9090 scroll_region_reset();
9091 return retval;
9092 }
9093
9094#ifdef FEAT_WINDOWS
9095 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9096 return FAIL;
9097#endif
9098
9099 return MAYBE;
9100}
9101
9102/*
9103 * window 'wp' and everything after it is messed up, mark it for redraw
9104 */
9105 static void
9106win_rest_invalid(wp)
9107 win_T *wp;
9108{
9109#ifdef FEAT_WINDOWS
9110 while (wp != NULL)
9111#else
9112 if (wp != NULL)
9113#endif
9114 {
9115 redraw_win_later(wp, NOT_VALID);
9116#ifdef FEAT_WINDOWS
9117 wp->w_redr_status = TRUE;
9118 wp = wp->w_next;
9119#endif
9120 }
9121 redraw_cmdline = TRUE;
9122}
9123
9124/*
9125 * The rest of the routines in this file perform screen manipulations. The
9126 * given operation is performed physically on the screen. The corresponding
9127 * change is also made to the internal screen image. In this way, the editor
9128 * anticipates the effect of editing changes on the appearance of the screen.
9129 * That way, when we call screenupdate a complete redraw isn't usually
9130 * necessary. Another advantage is that we can keep adding code to anticipate
9131 * screen changes, and in the meantime, everything still works.
9132 */
9133
9134/*
9135 * types for inserting or deleting lines
9136 */
9137#define USE_T_CAL 1
9138#define USE_T_CDL 2
9139#define USE_T_AL 3
9140#define USE_T_CE 4
9141#define USE_T_DL 5
9142#define USE_T_SR 6
9143#define USE_NL 7
9144#define USE_T_CD 8
9145#define USE_REDRAW 9
9146
9147/*
9148 * insert lines on the screen and update ScreenLines[]
9149 * 'end' is the line after the scrolled part. Normally it is Rows.
9150 * When scrolling region used 'off' is the offset from the top for the region.
9151 * 'row' and 'end' are relative to the start of the region.
9152 *
9153 * return FAIL for failure, OK for success.
9154 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009155 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156screen_ins_lines(off, row, line_count, end, wp)
9157 int off;
9158 int row;
9159 int line_count;
9160 int end;
9161 win_T *wp; /* NULL or window to use width from */
9162{
9163 int i;
9164 int j;
9165 unsigned temp;
9166 int cursor_row;
9167 int type;
9168 int result_empty;
9169 int can_ce = can_clear(T_CE);
9170
9171 /*
9172 * FAIL if
9173 * - there is no valid screen
9174 * - the screen has to be redrawn completely
9175 * - the line count is less than one
9176 * - the line count is more than 'ttyscroll'
9177 */
9178 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9179 return FAIL;
9180
9181 /*
9182 * There are seven ways to insert lines:
9183 * 0. When in a vertically split window and t_CV isn't set, redraw the
9184 * characters from ScreenLines[].
9185 * 1. Use T_CD (clear to end of display) if it exists and the result of
9186 * the insert is just empty lines
9187 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9188 * present or line_count > 1. It looks better if we do all the inserts
9189 * at once.
9190 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9191 * insert is just empty lines and T_CE is not present or line_count >
9192 * 1.
9193 * 4. Use T_AL (insert line) if it exists.
9194 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9195 * just empty lines.
9196 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9197 * just empty lines.
9198 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9199 * the 'da' flag is not set or we have clear line capability.
9200 * 8. redraw the characters from ScreenLines[].
9201 *
9202 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9203 * the scrollbar for the window. It does have insert line, use that if it
9204 * exists.
9205 */
9206 result_empty = (row + line_count >= end);
9207#ifdef FEAT_VERTSPLIT
9208 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9209 type = USE_REDRAW;
9210 else
9211#endif
9212 if (can_clear(T_CD) && result_empty)
9213 type = USE_T_CD;
9214 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9215 type = USE_T_CAL;
9216 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9217 type = USE_T_CDL;
9218 else if (*T_AL != NUL)
9219 type = USE_T_AL;
9220 else if (can_ce && result_empty)
9221 type = USE_T_CE;
9222 else if (*T_DL != NUL && result_empty)
9223 type = USE_T_DL;
9224 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9225 type = USE_T_SR;
9226 else
9227 return FAIL;
9228
9229 /*
9230 * For clearing the lines screen_del_lines() is used. This will also take
9231 * care of t_db if necessary.
9232 */
9233 if (type == USE_T_CD || type == USE_T_CDL ||
9234 type == USE_T_CE || type == USE_T_DL)
9235 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9236
9237 /*
9238 * If text is retained below the screen, first clear or delete as many
9239 * lines at the bottom of the window as are about to be inserted so that
9240 * the deleted lines won't later surface during a screen_del_lines.
9241 */
9242 if (*T_DB)
9243 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9244
9245#ifdef FEAT_CLIPBOARD
9246 /* Remove a modeless selection when inserting lines halfway the screen
9247 * or not the full width of the screen. */
9248 if (off + row > 0
9249# ifdef FEAT_VERTSPLIT
9250 || (wp != NULL && wp->w_width != Columns)
9251# endif
9252 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009253 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 else
9255 clip_scroll_selection(-line_count);
9256#endif
9257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258#ifdef FEAT_GUI
9259 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9260 * scrolling is actually carried out. */
9261 gui_dont_update_cursor();
9262#endif
9263
9264 if (*T_CCS != NUL) /* cursor relative to region */
9265 cursor_row = row;
9266 else
9267 cursor_row = row + off;
9268
9269 /*
9270 * Shift LineOffset[] line_count down to reflect the inserted lines.
9271 * Clear the inserted lines in ScreenLines[].
9272 */
9273 row += off;
9274 end += off;
9275 for (i = 0; i < line_count; ++i)
9276 {
9277#ifdef FEAT_VERTSPLIT
9278 if (wp != NULL && wp->w_width != Columns)
9279 {
9280 /* need to copy part of a line */
9281 j = end - 1 - i;
9282 while ((j -= line_count) >= row)
9283 linecopy(j + line_count, j, wp);
9284 j += line_count;
9285 if (can_clear((char_u *)" "))
9286 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9287 else
9288 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9289 LineWraps[j] = FALSE;
9290 }
9291 else
9292#endif
9293 {
9294 j = end - 1 - i;
9295 temp = LineOffset[j];
9296 while ((j -= line_count) >= row)
9297 {
9298 LineOffset[j + line_count] = LineOffset[j];
9299 LineWraps[j + line_count] = LineWraps[j];
9300 }
9301 LineOffset[j + line_count] = temp;
9302 LineWraps[j + line_count] = FALSE;
9303 if (can_clear((char_u *)" "))
9304 lineclear(temp, (int)Columns);
9305 else
9306 lineinvalid(temp, (int)Columns);
9307 }
9308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309
9310 screen_stop_highlight();
9311 windgoto(cursor_row, 0);
9312
9313#ifdef FEAT_VERTSPLIT
9314 /* redraw the characters */
9315 if (type == USE_REDRAW)
9316 redraw_block(row, end, wp);
9317 else
9318#endif
9319 if (type == USE_T_CAL)
9320 {
9321 term_append_lines(line_count);
9322 screen_start(); /* don't know where cursor is now */
9323 }
9324 else
9325 {
9326 for (i = 0; i < line_count; i++)
9327 {
9328 if (type == USE_T_AL)
9329 {
9330 if (i && cursor_row != 0)
9331 windgoto(cursor_row, 0);
9332 out_str(T_AL);
9333 }
9334 else /* type == USE_T_SR */
9335 out_str(T_SR);
9336 screen_start(); /* don't know where cursor is now */
9337 }
9338 }
9339
9340 /*
9341 * With scroll-reverse and 'da' flag set we need to clear the lines that
9342 * have been scrolled down into the region.
9343 */
9344 if (type == USE_T_SR && *T_DA)
9345 {
9346 for (i = 0; i < line_count; ++i)
9347 {
9348 windgoto(off + i, 0);
9349 out_str(T_CE);
9350 screen_start(); /* don't know where cursor is now */
9351 }
9352 }
9353
9354#ifdef FEAT_GUI
9355 gui_can_update_cursor();
9356 if (gui.in_use)
9357 out_flush(); /* always flush after a scroll */
9358#endif
9359 return OK;
9360}
9361
9362/*
9363 * delete lines on the screen and update ScreenLines[]
9364 * 'end' is the line after the scrolled part. Normally it is Rows.
9365 * When scrolling region used 'off' is the offset from the top for the region.
9366 * 'row' and 'end' are relative to the start of the region.
9367 *
9368 * Return OK for success, FAIL if the lines are not deleted.
9369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 int
9371screen_del_lines(off, row, line_count, end, force, wp)
9372 int off;
9373 int row;
9374 int line_count;
9375 int end;
9376 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009377 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 int j;
9380 int i;
9381 unsigned temp;
9382 int cursor_row;
9383 int cursor_end;
9384 int result_empty; /* result is empty until end of region */
9385 int can_delete; /* deleting line codes can be used */
9386 int type;
9387
9388 /*
9389 * FAIL if
9390 * - there is no valid screen
9391 * - the screen has to be redrawn completely
9392 * - the line count is less than one
9393 * - the line count is more than 'ttyscroll'
9394 */
9395 if (!screen_valid(TRUE) || line_count <= 0 ||
9396 (!force && line_count > p_ttyscroll))
9397 return FAIL;
9398
9399 /*
9400 * Check if the rest of the current region will become empty.
9401 */
9402 result_empty = row + line_count >= end;
9403
9404 /*
9405 * We can delete lines only when 'db' flag not set or when 'ce' option
9406 * available.
9407 */
9408 can_delete = (*T_DB == NUL || can_clear(T_CE));
9409
9410 /*
9411 * There are six ways to delete lines:
9412 * 0. When in a vertically split window and t_CV isn't set, redraw the
9413 * characters from ScreenLines[].
9414 * 1. Use T_CD if it exists and the result is empty.
9415 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9416 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9417 * none of the other ways work.
9418 * 4. Use T_CE (erase line) if the result is empty.
9419 * 5. Use T_DL (delete line) if it exists.
9420 * 6. redraw the characters from ScreenLines[].
9421 */
9422#ifdef FEAT_VERTSPLIT
9423 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9424 type = USE_REDRAW;
9425 else
9426#endif
9427 if (can_clear(T_CD) && result_empty)
9428 type = USE_T_CD;
9429#if defined(__BEOS__) && defined(BEOS_DR8)
9430 /*
9431 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9432 * its internal termcap... this works okay for tests which test *T_DB !=
9433 * NUL. It has the disadvantage that the user cannot use any :set t_*
9434 * command to get T_DB (back) to empty_option, only :set term=... will do
9435 * the trick...
9436 * Anyway, this hack will hopefully go away with the next OS release.
9437 * (Olaf Seibert)
9438 */
9439 else if (row == 0 && T_DB == empty_option
9440 && (line_count == 1 || *T_CDL == NUL))
9441#else
9442 else if (row == 0 && (
9443#ifndef AMIGA
9444 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9445 * up, so use delete-line command */
9446 line_count == 1 ||
9447#endif
9448 *T_CDL == NUL))
9449#endif
9450 type = USE_NL;
9451 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9452 type = USE_T_CDL;
9453 else if (can_clear(T_CE) && result_empty
9454#ifdef FEAT_VERTSPLIT
9455 && (wp == NULL || wp->w_width == Columns)
9456#endif
9457 )
9458 type = USE_T_CE;
9459 else if (*T_DL != NUL && can_delete)
9460 type = USE_T_DL;
9461 else if (*T_CDL != NUL && can_delete)
9462 type = USE_T_CDL;
9463 else
9464 return FAIL;
9465
9466#ifdef FEAT_CLIPBOARD
9467 /* Remove a modeless selection when deleting lines halfway the screen or
9468 * not the full width of the screen. */
9469 if (off + row > 0
9470# ifdef FEAT_VERTSPLIT
9471 || (wp != NULL && wp->w_width != Columns)
9472# endif
9473 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009474 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 else
9476 clip_scroll_selection(line_count);
9477#endif
9478
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479#ifdef FEAT_GUI
9480 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9481 * scrolling is actually carried out. */
9482 gui_dont_update_cursor();
9483#endif
9484
9485 if (*T_CCS != NUL) /* cursor relative to region */
9486 {
9487 cursor_row = row;
9488 cursor_end = end;
9489 }
9490 else
9491 {
9492 cursor_row = row + off;
9493 cursor_end = end + off;
9494 }
9495
9496 /*
9497 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9498 * Clear the inserted lines in ScreenLines[].
9499 */
9500 row += off;
9501 end += off;
9502 for (i = 0; i < line_count; ++i)
9503 {
9504#ifdef FEAT_VERTSPLIT
9505 if (wp != NULL && wp->w_width != Columns)
9506 {
9507 /* need to copy part of a line */
9508 j = row + i;
9509 while ((j += line_count) <= end - 1)
9510 linecopy(j - line_count, j, wp);
9511 j -= line_count;
9512 if (can_clear((char_u *)" "))
9513 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9514 else
9515 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9516 LineWraps[j] = FALSE;
9517 }
9518 else
9519#endif
9520 {
9521 /* whole width, moving the line pointers is faster */
9522 j = row + i;
9523 temp = LineOffset[j];
9524 while ((j += line_count) <= end - 1)
9525 {
9526 LineOffset[j - line_count] = LineOffset[j];
9527 LineWraps[j - line_count] = LineWraps[j];
9528 }
9529 LineOffset[j - line_count] = temp;
9530 LineWraps[j - line_count] = FALSE;
9531 if (can_clear((char_u *)" "))
9532 lineclear(temp, (int)Columns);
9533 else
9534 lineinvalid(temp, (int)Columns);
9535 }
9536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
9538 screen_stop_highlight();
9539
9540#ifdef FEAT_VERTSPLIT
9541 /* redraw the characters */
9542 if (type == USE_REDRAW)
9543 redraw_block(row, end, wp);
9544 else
9545#endif
9546 if (type == USE_T_CD) /* delete the lines */
9547 {
9548 windgoto(cursor_row, 0);
9549 out_str(T_CD);
9550 screen_start(); /* don't know where cursor is now */
9551 }
9552 else if (type == USE_T_CDL)
9553 {
9554 windgoto(cursor_row, 0);
9555 term_delete_lines(line_count);
9556 screen_start(); /* don't know where cursor is now */
9557 }
9558 /*
9559 * Deleting lines at top of the screen or scroll region: Just scroll
9560 * the whole screen (scroll region) up by outputting newlines on the
9561 * last line.
9562 */
9563 else if (type == USE_NL)
9564 {
9565 windgoto(cursor_end - 1, 0);
9566 for (i = line_count; --i >= 0; )
9567 out_char('\n'); /* cursor will remain on same line */
9568 }
9569 else
9570 {
9571 for (i = line_count; --i >= 0; )
9572 {
9573 if (type == USE_T_DL)
9574 {
9575 windgoto(cursor_row, 0);
9576 out_str(T_DL); /* delete a line */
9577 }
9578 else /* type == USE_T_CE */
9579 {
9580 windgoto(cursor_row + i, 0);
9581 out_str(T_CE); /* erase a line */
9582 }
9583 screen_start(); /* don't know where cursor is now */
9584 }
9585 }
9586
9587 /*
9588 * If the 'db' flag is set, we need to clear the lines that have been
9589 * scrolled up at the bottom of the region.
9590 */
9591 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9592 {
9593 for (i = line_count; i > 0; --i)
9594 {
9595 windgoto(cursor_end - i, 0);
9596 out_str(T_CE); /* erase a line */
9597 screen_start(); /* don't know where cursor is now */
9598 }
9599 }
9600
9601#ifdef FEAT_GUI
9602 gui_can_update_cursor();
9603 if (gui.in_use)
9604 out_flush(); /* always flush after a scroll */
9605#endif
9606
9607 return OK;
9608}
9609
9610/*
9611 * show the current mode and ruler
9612 *
9613 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9614 * If clear_cmdline is FALSE there may be a message there that needs to be
9615 * cleared only if a mode is shown.
9616 * Return the length of the message (0 if no message).
9617 */
9618 int
9619showmode()
9620{
9621 int need_clear;
9622 int length = 0;
9623 int do_mode;
9624 int attr;
9625 int nwr_save;
9626#ifdef FEAT_INS_EXPAND
9627 int sub_attr;
9628#endif
9629
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009630 do_mode = ((p_smd && msg_silent == 0)
9631 && ((State & INSERT)
9632 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009633 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 if (do_mode || Recording)
9635 {
9636 /*
9637 * Don't show mode right now, when not redrawing or inside a mapping.
9638 * Call char_avail() only when we are going to show something, because
9639 * it takes a bit of time.
9640 */
9641 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9642 {
9643 redraw_cmdline = TRUE; /* show mode later */
9644 return 0;
9645 }
9646
9647 nwr_save = need_wait_return;
9648
9649 /* wait a bit before overwriting an important message */
9650 check_for_delay(FALSE);
9651
9652 /* if the cmdline is more than one line high, erase top lines */
9653 need_clear = clear_cmdline;
9654 if (clear_cmdline && cmdline_row < Rows - 1)
9655 msg_clr_cmdline(); /* will reset clear_cmdline */
9656
9657 /* Position on the last line in the window, column 0 */
9658 msg_pos_mode();
9659 cursor_off();
9660 attr = hl_attr(HLF_CM); /* Highlight mode */
9661 if (do_mode)
9662 {
9663 MSG_PUTS_ATTR("--", attr);
9664#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009665 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009666# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009667 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009668# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009669 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009670# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009671 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009672# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 MSG_PUTS_ATTR(" IM", attr);
9674# else
9675 MSG_PUTS_ATTR(" XIM", attr);
9676# endif
9677#endif
9678#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9679 if (gui.in_use)
9680 {
9681 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009682 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 }
9684#endif
9685#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009686 /* CTRL-X in Insert mode */
9687 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 {
9689 /* These messages can get long, avoid a wrap in a narrow
9690 * window. Prefer showing edit_submode_extra. */
9691 length = (Rows - msg_row) * Columns - 3;
9692 if (edit_submode_extra != NULL)
9693 length -= vim_strsize(edit_submode_extra);
9694 if (length > 0)
9695 {
9696 if (edit_submode_pre != NULL)
9697 length -= vim_strsize(edit_submode_pre);
9698 if (length - vim_strsize(edit_submode) > 0)
9699 {
9700 if (edit_submode_pre != NULL)
9701 msg_puts_attr(edit_submode_pre, attr);
9702 msg_puts_attr(edit_submode, attr);
9703 }
9704 if (edit_submode_extra != NULL)
9705 {
9706 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9707 if ((int)edit_submode_highl < (int)HLF_COUNT)
9708 sub_attr = hl_attr(edit_submode_highl);
9709 else
9710 sub_attr = attr;
9711 msg_puts_attr(edit_submode_extra, sub_attr);
9712 }
9713 }
9714 length = 0;
9715 }
9716 else
9717#endif
9718 {
9719#ifdef FEAT_VREPLACE
9720 if (State & VREPLACE_FLAG)
9721 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9722 else
9723#endif
9724 if (State & REPLACE_FLAG)
9725 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9726 else if (State & INSERT)
9727 {
9728#ifdef FEAT_RIGHTLEFT
9729 if (p_ri)
9730 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9731#endif
9732 MSG_PUTS_ATTR(_(" INSERT"), attr);
9733 }
9734 else if (restart_edit == 'I')
9735 MSG_PUTS_ATTR(_(" (insert)"), attr);
9736 else if (restart_edit == 'R')
9737 MSG_PUTS_ATTR(_(" (replace)"), attr);
9738 else if (restart_edit == 'V')
9739 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9740#ifdef FEAT_RIGHTLEFT
9741 if (p_hkmap)
9742 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9743# ifdef FEAT_FKMAP
9744 if (p_fkmap)
9745 MSG_PUTS_ATTR(farsi_text_5, attr);
9746# endif
9747#endif
9748#ifdef FEAT_KEYMAP
9749 if (State & LANGMAP)
9750 {
9751# ifdef FEAT_ARABIC
9752 if (curwin->w_p_arab)
9753 MSG_PUTS_ATTR(_(" Arabic"), attr);
9754 else
9755# endif
9756 MSG_PUTS_ATTR(_(" (lang)"), attr);
9757 }
9758#endif
9759 if ((State & INSERT) && p_paste)
9760 MSG_PUTS_ATTR(_(" (paste)"), attr);
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (VIsual_active)
9763 {
9764 char *p;
9765
9766 /* Don't concatenate separate words to avoid translation
9767 * problems. */
9768 switch ((VIsual_select ? 4 : 0)
9769 + (VIsual_mode == Ctrl_V) * 2
9770 + (VIsual_mode == 'V'))
9771 {
9772 case 0: p = N_(" VISUAL"); break;
9773 case 1: p = N_(" VISUAL LINE"); break;
9774 case 2: p = N_(" VISUAL BLOCK"); break;
9775 case 4: p = N_(" SELECT"); break;
9776 case 5: p = N_(" SELECT LINE"); break;
9777 default: p = N_(" SELECT BLOCK"); break;
9778 }
9779 MSG_PUTS_ATTR(_(p), attr);
9780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 MSG_PUTS_ATTR(" --", attr);
9782 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 need_clear = TRUE;
9785 }
9786 if (Recording
9787#ifdef FEAT_INS_EXPAND
9788 && edit_submode == NULL /* otherwise it gets too long */
9789#endif
9790 )
9791 {
9792 MSG_PUTS_ATTR(_("recording"), attr);
9793 need_clear = TRUE;
9794 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009795
9796 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 if (need_clear || clear_cmdline)
9798 msg_clr_eos();
9799 msg_didout = FALSE; /* overwrite this message */
9800 length = msg_col;
9801 msg_col = 0;
9802 need_wait_return = nwr_save; /* never ask for hit-return for this */
9803 }
9804 else if (clear_cmdline && msg_silent == 0)
9805 /* Clear the whole command line. Will reset "clear_cmdline". */
9806 msg_clr_cmdline();
9807
9808#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 /* In Visual mode the size of the selected area must be redrawn. */
9810 if (VIsual_active)
9811 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812
9813 /* If the last window has no status line, the ruler is after the mode
9814 * message and must be redrawn */
9815 if (redrawing()
9816# ifdef FEAT_WINDOWS
9817 && lastwin->w_status_height == 0
9818# endif
9819 )
9820 win_redr_ruler(lastwin, TRUE);
9821#endif
9822 redraw_cmdline = FALSE;
9823 clear_cmdline = FALSE;
9824
9825 return length;
9826}
9827
9828/*
9829 * Position for a mode message.
9830 */
9831 static void
9832msg_pos_mode()
9833{
9834 msg_col = 0;
9835 msg_row = Rows - 1;
9836}
9837
9838/*
9839 * Delete mode message. Used when ESC is typed which is expected to end
9840 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009841 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 */
9843 void
9844unshowmode(force)
9845 int force;
9846{
9847 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009848 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 */
9850 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9851 redraw_cmdline = TRUE; /* delete mode later */
9852 else
9853 {
9854 msg_pos_mode();
9855 if (Recording)
9856 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9857 msg_clr_eos();
9858 }
9859}
9860
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009861#if defined(FEAT_WINDOWS)
9862/*
9863 * Draw the tab pages line at the top of the Vim window.
9864 */
9865 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009866draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009867{
9868 int tabcount = 0;
9869 tabpage_T *tp;
9870 int tabwidth;
9871 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009872 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009873 int attr;
9874 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009875 win_T *cwp;
9876 int wincount;
9877 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009878 int c;
9879 int len;
9880 int attr_sel = hl_attr(HLF_TPS);
9881 int attr_nosel = hl_attr(HLF_TP);
9882 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009883 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009884 int room;
9885 int use_sep_chars = (t_colors < 8
9886#ifdef FEAT_GUI
9887 && !gui.in_use
9888#endif
9889 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009890
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009891 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009892
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009893#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009894 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009895 if (gui_use_tabline())
9896 {
9897 gui_update_tabline();
9898 return;
9899 }
9900#endif
9901
9902 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009903 return;
9904
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009905#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009906
9907 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9908 for (scol = 0; scol < Columns; ++scol)
9909 TabPageIdxs[scol] = 0;
9910
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009911 /* Use the 'tabline' option if it's set. */
9912 if (*p_tal != NUL)
9913 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009914 int save_called_emsg = called_emsg;
9915
9916 /* Check for an error. If there is one we would loop in redrawing the
9917 * screen. Avoid that by making 'tabline' empty. */
9918 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009919 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009920 if (called_emsg)
9921 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009922 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009923 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009924 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009925 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009926#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009927 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009928 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9929 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009930
Bram Moolenaar238a5642006-02-21 22:12:05 +00009931 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9932 if (tabwidth < 6)
9933 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009934
Bram Moolenaar238a5642006-02-21 22:12:05 +00009935 attr = attr_nosel;
9936 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009937 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009938 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9939 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009940 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009941 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009942
Bram Moolenaar238a5642006-02-21 22:12:05 +00009943 if (tp->tp_topframe == topframe)
9944 attr = attr_sel;
9945 if (use_sep_chars && col > 0)
9946 screen_putchar('|', 0, col++, attr);
9947
9948 if (tp->tp_topframe != topframe)
9949 attr = attr_nosel;
9950
9951 screen_putchar(' ', 0, col++, attr);
9952
9953 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009954 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 cwp = curwin;
9956 wp = firstwin;
9957 }
9958 else
9959 {
9960 cwp = tp->tp_curwin;
9961 wp = tp->tp_firstwin;
9962 }
9963
9964 modified = FALSE;
9965 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9966 if (bufIsChanged(wp->w_buffer))
9967 modified = TRUE;
9968 if (modified || wincount > 1)
9969 {
9970 if (wincount > 1)
9971 {
9972 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009973 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009974 if (col + len >= Columns - 3)
9975 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009976 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009977#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009978 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009979#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009980 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009981#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009982 );
9983 col += len;
9984 }
9985 if (modified)
9986 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9987 screen_putchar(' ', 0, col++, attr);
9988 }
9989
9990 room = scol - col + tabwidth - 1;
9991 if (room > 0)
9992 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009993 /* Get buffer name in NameBuff[] */
9994 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009995 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009996 len = vim_strsize(NameBuff);
9997 p = NameBuff;
9998#ifdef FEAT_MBYTE
9999 if (has_mbyte)
10000 while (len > room)
10001 {
10002 len -= ptr2cells(p);
10003 mb_ptr_adv(p);
10004 }
10005 else
10006#endif
10007 if (len > room)
10008 {
10009 p += len - room;
10010 len = room;
10011 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010012 if (len > Columns - col - 1)
10013 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010014
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010015 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010016 col += len;
10017 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010018 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010019
10020 /* Store the tab page number in TabPageIdxs[], so that
10021 * jump_to_mouse() knows where each one is. */
10022 ++tabcount;
10023 while (scol < col)
10024 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010025 }
10026
Bram Moolenaar238a5642006-02-21 22:12:05 +000010027 if (use_sep_chars)
10028 c = '_';
10029 else
10030 c = ' ';
10031 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010032
10033 /* Put an "X" for closing the current tab if there are several. */
10034 if (first_tabpage->tp_next != NULL)
10035 {
10036 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10037 TabPageIdxs[Columns - 1] = -999;
10038 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010039 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010040
10041 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10042 * set. */
10043 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010044}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010045
10046/*
10047 * Get buffer name for "buf" into NameBuff[].
10048 * Takes care of special buffer names and translates special characters.
10049 */
10050 void
10051get_trans_bufname(buf)
10052 buf_T *buf;
10053{
10054 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010055 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010056 else
10057 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10058 trans_characters(NameBuff, MAXPATHL);
10059}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010060#endif
10061
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10063/*
10064 * Get the character to use in a status line. Get its attributes in "*attr".
10065 */
10066 static int
10067fillchar_status(attr, is_curwin)
10068 int *attr;
10069 int is_curwin;
10070{
10071 int fill;
10072 if (is_curwin)
10073 {
10074 *attr = hl_attr(HLF_S);
10075 fill = fill_stl;
10076 }
10077 else
10078 {
10079 *attr = hl_attr(HLF_SNC);
10080 fill = fill_stlnc;
10081 }
10082 /* Use fill when there is highlighting, and highlighting of current
10083 * window differs, or the fillchars differ, or this is not the
10084 * current window */
10085 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10086 || !is_curwin || firstwin == lastwin)
10087 || (fill_stl != fill_stlnc)))
10088 return fill;
10089 if (is_curwin)
10090 return '^';
10091 return '=';
10092}
10093#endif
10094
10095#ifdef FEAT_VERTSPLIT
10096/*
10097 * Get the character to use in a separator between vertically split windows.
10098 * Get its attributes in "*attr".
10099 */
10100 static int
10101fillchar_vsep(attr)
10102 int *attr;
10103{
10104 *attr = hl_attr(HLF_C);
10105 if (*attr == 0 && fill_vert == ' ')
10106 return '|';
10107 else
10108 return fill_vert;
10109}
10110#endif
10111
10112/*
10113 * Return TRUE if redrawing should currently be done.
10114 */
10115 int
10116redrawing()
10117{
10118 return (!RedrawingDisabled
10119 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10120}
10121
10122/*
10123 * Return TRUE if printing messages should currently be done.
10124 */
10125 int
10126messaging()
10127{
10128 return (!(p_lz && char_avail() && !KeyTyped));
10129}
10130
10131/*
10132 * Show current status info in ruler and various other places
10133 * If always is FALSE, only show ruler if position has changed.
10134 */
10135 void
10136showruler(always)
10137 int always;
10138{
10139 if (!always && !redrawing())
10140 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010141#ifdef FEAT_INS_EXPAND
10142 if (pum_visible())
10143 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010144# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010145 /* Don't redraw right now, do it later. */
10146 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010147# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010148 return;
10149 }
10150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010152 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010153 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010154 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 else
10157#endif
10158#ifdef FEAT_CMDL_INFO
10159 win_redr_ruler(curwin, always);
10160#endif
10161
10162#ifdef FEAT_TITLE
10163 if (need_maketitle
10164# ifdef FEAT_STL_OPT
10165 || (p_icon && (stl_syntax & STL_IN_ICON))
10166 || (p_title && (stl_syntax & STL_IN_TITLE))
10167# endif
10168 )
10169 maketitle();
10170#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010171#ifdef FEAT_WINDOWS
10172 /* Redraw the tab pages line if needed. */
10173 if (redraw_tabline)
10174 draw_tabline();
10175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176}
10177
10178#ifdef FEAT_CMDL_INFO
10179 static void
10180win_redr_ruler(wp, always)
10181 win_T *wp;
10182 int always;
10183{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010184#define RULER_BUF_LEN 70
10185 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 int row;
10187 int fillchar;
10188 int attr;
10189 int empty_line = FALSE;
10190 colnr_T virtcol;
10191 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010192 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 int o;
10194#ifdef FEAT_VERTSPLIT
10195 int this_ru_col;
10196 int off = 0;
10197 int width = Columns;
10198# define WITH_OFF(x) x
10199# define WITH_WIDTH(x) x
10200#else
10201# define WITH_OFF(x) 0
10202# define WITH_WIDTH(x) Columns
10203# define this_ru_col ru_col
10204#endif
10205
10206 /* If 'ruler' off or redrawing disabled, don't do anything */
10207 if (!p_ru)
10208 return;
10209
10210 /*
10211 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10212 * after deleting lines, before cursor.lnum is corrected.
10213 */
10214 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10215 return;
10216
10217#ifdef FEAT_INS_EXPAND
10218 /* Don't draw the ruler while doing insert-completion, it might overwrite
10219 * the (long) mode message. */
10220# ifdef FEAT_WINDOWS
10221 if (wp == lastwin && lastwin->w_status_height == 0)
10222# endif
10223 if (edit_submode != NULL)
10224 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010225 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10226 if (pum_visible())
10227 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228#endif
10229
10230#ifdef FEAT_STL_OPT
10231 if (*p_ruf)
10232 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010233 int save_called_emsg = called_emsg;
10234
10235 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010237 if (called_emsg)
10238 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010239 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010240 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 return;
10242 }
10243#endif
10244
10245 /*
10246 * Check if not in Insert mode and the line is empty (will show "0-1").
10247 */
10248 if (!(State & INSERT)
10249 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10250 empty_line = TRUE;
10251
10252 /*
10253 * Only draw the ruler when something changed.
10254 */
10255 validate_virtcol_win(wp);
10256 if ( redraw_cmdline
10257 || always
10258 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10259 || wp->w_cursor.col != wp->w_ru_cursor.col
10260 || wp->w_virtcol != wp->w_ru_virtcol
10261#ifdef FEAT_VIRTUALEDIT
10262 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10263#endif
10264 || wp->w_topline != wp->w_ru_topline
10265 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10266#ifdef FEAT_DIFF
10267 || wp->w_topfill != wp->w_ru_topfill
10268#endif
10269 || empty_line != wp->w_ru_empty)
10270 {
10271 cursor_off();
10272#ifdef FEAT_WINDOWS
10273 if (wp->w_status_height)
10274 {
10275 row = W_WINROW(wp) + wp->w_height;
10276 fillchar = fillchar_status(&attr, wp == curwin);
10277# ifdef FEAT_VERTSPLIT
10278 off = W_WINCOL(wp);
10279 width = W_WIDTH(wp);
10280# endif
10281 }
10282 else
10283#endif
10284 {
10285 row = Rows - 1;
10286 fillchar = ' ';
10287 attr = 0;
10288#ifdef FEAT_VERTSPLIT
10289 width = Columns;
10290 off = 0;
10291#endif
10292 }
10293
10294 /* In list mode virtcol needs to be recomputed */
10295 virtcol = wp->w_virtcol;
10296 if (wp->w_p_list && lcs_tab1 == NUL)
10297 {
10298 wp->w_p_list = FALSE;
10299 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10300 wp->w_p_list = TRUE;
10301 }
10302
10303 /*
10304 * Some sprintfs return the length, some return a pointer.
10305 * To avoid portability problems we use strlen() here.
10306 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010307 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10309 ? 0L
10310 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010311 len = STRLEN(buffer);
10312 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10314 (int)virtcol + 1);
10315
10316 /*
10317 * Add a "50%" if there is room for it.
10318 * On the last line, don't print in the last column (scrolls the
10319 * screen up on some terminals).
10320 */
10321 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010322 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 o = i + vim_strsize(buffer + i + 1);
10324#ifdef FEAT_WINDOWS
10325 if (wp->w_status_height == 0) /* can't use last char of screen */
10326#endif
10327 ++o;
10328#ifdef FEAT_VERTSPLIT
10329 this_ru_col = ru_col - (Columns - width);
10330 if (this_ru_col < 0)
10331 this_ru_col = 0;
10332#endif
10333 /* Never use more than half the window/screen width, leave the other
10334 * half for the filename. */
10335 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10336 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10337 if (this_ru_col + o < WITH_WIDTH(width))
10338 {
10339 while (this_ru_col + o < WITH_WIDTH(width))
10340 {
10341#ifdef FEAT_MBYTE
10342 if (has_mbyte)
10343 i += (*mb_char2bytes)(fillchar, buffer + i);
10344 else
10345#endif
10346 buffer[i++] = fillchar;
10347 ++o;
10348 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010349 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 }
10351 /* Truncate at window boundary. */
10352#ifdef FEAT_MBYTE
10353 if (has_mbyte)
10354 {
10355 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010356 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 o += (*mb_ptr2cells)(buffer + i);
10359 if (this_ru_col + o > WITH_WIDTH(width))
10360 {
10361 buffer[i] = NUL;
10362 break;
10363 }
10364 }
10365 }
10366 else
10367#endif
10368 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10369 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10370
10371 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10372 i = redraw_cmdline;
10373 screen_fill(row, row + 1,
10374 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10375 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10376 fillchar, fillchar, attr);
10377 /* don't redraw the cmdline because of showing the ruler */
10378 redraw_cmdline = i;
10379 wp->w_ru_cursor = wp->w_cursor;
10380 wp->w_ru_virtcol = wp->w_virtcol;
10381 wp->w_ru_empty = empty_line;
10382 wp->w_ru_topline = wp->w_topline;
10383 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10384#ifdef FEAT_DIFF
10385 wp->w_ru_topfill = wp->w_topfill;
10386#endif
10387 }
10388}
10389#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010390
10391#if defined(FEAT_LINEBREAK) || defined(PROTO)
10392/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010393 * Return the width of the 'number' and 'relativenumber' column.
10394 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010395 * Otherwise it depends on 'numberwidth' and the line count.
10396 */
10397 int
10398number_width(wp)
10399 win_T *wp;
10400{
10401 int n;
10402 linenr_T lnum;
10403
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010404 if (wp->w_p_rnu && !wp->w_p_nu)
10405 /* cursor line shows "0" */
10406 lnum = wp->w_height;
10407 else
10408 /* cursor line shows absolute line number */
10409 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010410
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010411 if (lnum == wp->w_nrwidth_line_count)
10412 return wp->w_nrwidth_width;
10413 wp->w_nrwidth_line_count = lnum;
10414
10415 n = 0;
10416 do
10417 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010418 lnum /= 10;
10419 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010420 } while (lnum > 0);
10421
10422 /* 'numberwidth' gives the minimal width plus one */
10423 if (n < wp->w_p_nuw - 1)
10424 n = wp->w_p_nuw - 1;
10425
10426 wp->w_nrwidth_width = n;
10427 return n;
10428}
10429#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010430
10431/*
10432 * Return the current cursor column. This is the actual position on the
10433 * screen. First column is 0.
10434 */
10435 int
10436screen_screencol()
10437{
10438 return screen_cur_col;
10439}
10440
10441/*
10442 * Return the current cursor row. This is the actual position on the screen.
10443 * First row is 0.
10444 */
10445 int
10446screen_screenrow()
10447{
10448 return screen_cur_row;
10449}