blob: cdb416d03d8dadad74313fb821e07741c070119a [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)
45 * - w_topfill (filler line above the first line)
46 * - 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. */
3556 if (draw_signcolumn(wp)
3557# ifdef FEAT_DIFF
3558 && filler_todo <= 0
3559# endif
3560 )
3561 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003562 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003564 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565# endif
3566
3567 /* Draw two cells with the sign value or blank. */
3568 c_extra = ' ';
3569 char_attr = hl_attr(HLF_SC);
3570 n_extra = 2;
3571
3572 if (row == startrow)
3573 {
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
6919screen_puts_len(text, len, row, col, attr)
6920 char_u *text;
6921 int len;
6922 int row;
6923 int col;
6924 int attr;
6925{
6926 unsigned off;
6927 char_u *ptr = text;
6928 int c;
6929#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006930 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 int mbyte_blen = 1;
6932 int mbyte_cells = 1;
6933 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006934 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 int clear_next_cell = FALSE;
6936# ifdef FEAT_ARABIC
6937 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006938 int pc, nc, nc1;
6939 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940# endif
6941#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006942#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6943 int force_redraw_this;
6944 int force_redraw_next = FALSE;
6945#endif
6946 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6949 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006950 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaarc236c162008-07-13 17:41:49 +00006952#ifdef FEAT_MBYTE
6953 /* When drawing over the right halve of a double-wide char clear out the
6954 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006955 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006956# ifdef FEAT_GUI
6957 && !gui.in_use
6958# endif
6959 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006960 {
6961 ScreenLines[off - 1] = ' ';
6962 ScreenAttrs[off - 1] = 0;
6963 if (enc_utf8)
6964 {
6965 ScreenLinesUC[off - 1] = 0;
6966 ScreenLinesC[0][off - 1] = 0;
6967 }
6968 /* redraw the previous cell, make it empty */
6969 screen_char(off - 1, row, col - 1);
6970 /* force the cell at "col" to be redrawn */
6971 force_redraw_next = TRUE;
6972 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006973#endif
6974
Bram Moolenaar367329b2007-08-30 11:53:22 +00006975#ifdef FEAT_MBYTE
6976 max_off = LineOffset[row] + screen_Columns;
6977#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006978 while (col < screen_Columns
6979 && (len < 0 || (int)(ptr - text) < len)
6980 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
6982 c = *ptr;
6983#ifdef FEAT_MBYTE
6984 /* check if this is the first byte of a multibyte */
6985 if (has_mbyte)
6986 {
6987 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006988 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006990 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
6992 mbyte_cells = 1;
6993 else if (enc_dbcs != 0)
6994 mbyte_cells = mbyte_blen;
6995 else /* enc_utf8 */
6996 {
6997 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006998 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 (int)((text + len) - ptr));
7000 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007001 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007003# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /* Non-BMP character: display as ? or fullwidth ?. */
7005 if (u8c >= 0x10000)
7006 {
7007 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7008 if (attr == 0)
7009 attr = hl_attr(HLF_8);
7010 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012# ifdef FEAT_ARABIC
7013 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7014 {
7015 /* Do Arabic shaping. */
7016 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7017 {
7018 /* Past end of string to be displayed. */
7019 nc = NUL;
7020 nc1 = NUL;
7021 }
7022 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007023 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007024 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7025 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007026 nc1 = pcc[0];
7027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 pc = prev_c;
7029 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007030 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032 else
7033 prev_c = u8c;
7034# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007035 if (col + mbyte_cells > screen_Columns)
7036 {
7037 /* Only 1 cell left, but character requires 2 cells:
7038 * display a '>' in the last column to avoid wrapping. */
7039 c = '>';
7040 mbyte_cells = 1;
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 }
7044#endif
7045
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007046#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7047 force_redraw_this = force_redraw_next;
7048 force_redraw_next = FALSE;
7049#endif
7050
7051 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#ifdef FEAT_MBYTE
7053 || (mbyte_cells == 2
7054 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7055 || (enc_dbcs == DBCS_JPNU
7056 && c == 0x8e
7057 && ScreenLines2[off] != ptr[1])
7058 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007059 && (ScreenLinesUC[off] !=
7060 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7061 || (ScreenLinesUC[off] != 0
7062 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
7064 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007065 || exmode_active;
7066
7067 if (need_redraw
7068#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7069 || force_redraw_this
7070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 )
7072 {
7073#if defined(FEAT_GUI) || defined(UNIX)
7074 /* The bold trick makes a single row of pixels appear in the next
7075 * character. When a bold character is removed, the next
7076 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007077 * and for some xterms. */
7078 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079# ifdef FEAT_GUI
7080 gui.in_use
7081# endif
7082# if defined(FEAT_GUI) && defined(UNIX)
7083 ||
7084# endif
7085# ifdef UNIX
7086 term_is_xterm
7087# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007088 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007090 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007092 if (n > HL_ALL)
7093 n = syn_attr2attr(n);
7094 if (n & HL_BOLD)
7095 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097#endif
7098#ifdef FEAT_MBYTE
7099 /* When at the end of the text and overwriting a two-cell
7100 * character with a one-cell character, need to clear the next
7101 * cell. Also when overwriting the left halve of a two-cell char
7102 * with the right halve of a two-cell char. Do this only once
7103 * (mb_off2cells() may return 2 on the right halve). */
7104 if (clear_next_cell)
7105 clear_next_cell = FALSE;
7106 else if (has_mbyte
7107 && (len < 0 ? ptr[mbyte_blen] == NUL
7108 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007109 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007111 && (*mb_off2cells)(off, max_off) == 1
7112 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 clear_next_cell = TRUE;
7114
7115 /* Make sure we never leave a second byte of a double-byte behind,
7116 * it confuses mb_off2cells(). */
7117 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007118 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007120 && (*mb_off2cells)(off, max_off) == 1
7121 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 ScreenLines[off + mbyte_blen] = 0;
7123#endif
7124 ScreenLines[off] = c;
7125 ScreenAttrs[off] = attr;
7126#ifdef FEAT_MBYTE
7127 if (enc_utf8)
7128 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007129 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 ScreenLinesUC[off] = 0;
7131 else
7132 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007133 int i;
7134
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007136 for (i = 0; i < Screen_mco; ++i)
7137 {
7138 ScreenLinesC[i][off] = u8cc[i];
7139 if (u8cc[i] == 0)
7140 break;
7141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143 if (mbyte_cells == 2)
7144 {
7145 ScreenLines[off + 1] = 0;
7146 ScreenAttrs[off + 1] = attr;
7147 }
7148 screen_char(off, row, col);
7149 }
7150 else if (mbyte_cells == 2)
7151 {
7152 ScreenLines[off + 1] = ptr[1];
7153 ScreenAttrs[off + 1] = attr;
7154 screen_char_2(off, row, col);
7155 }
7156 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7157 {
7158 ScreenLines2[off] = ptr[1];
7159 screen_char(off, row, col);
7160 }
7161 else
7162#endif
7163 screen_char(off, row, col);
7164 }
7165#ifdef FEAT_MBYTE
7166 if (has_mbyte)
7167 {
7168 off += mbyte_cells;
7169 col += mbyte_cells;
7170 ptr += mbyte_blen;
7171 if (clear_next_cell)
7172 ptr = (char_u *)" ";
7173 }
7174 else
7175#endif
7176 {
7177 ++off;
7178 ++col;
7179 ++ptr;
7180 }
7181 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007182
7183#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7184 /* If we detected the next character needs to be redrawn, but the text
7185 * doesn't extend up to there, update the character here. */
7186 if (force_redraw_next && col < screen_Columns)
7187 {
7188# ifdef FEAT_MBYTE
7189 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7190 screen_char_2(off, row, col);
7191 else
7192# endif
7193 screen_char(off, row, col);
7194 }
7195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196}
7197
7198#ifdef FEAT_SEARCH_EXTRA
7199/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007200 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 */
7202 static void
7203start_search_hl()
7204{
7205 if (p_hls && !no_hlsearch)
7206 {
7207 last_pat_prog(&search_hl.rm);
7208 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007209# ifdef FEAT_RELTIME
7210 /* Set the time limit to 'redrawtime'. */
7211 profile_setlimit(p_rdt, &search_hl.tm);
7212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214}
7215
7216/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007217 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 */
7219 static void
7220end_search_hl()
7221{
7222 if (search_hl.rm.regprog != NULL)
7223 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007224 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 search_hl.rm.regprog = NULL;
7226 }
7227}
7228
7229/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007230 * Init for calling prepare_search_hl().
7231 */
7232 static void
7233init_search_hl(wp)
7234 win_T *wp;
7235{
7236 matchitem_T *cur;
7237
7238 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7239 * match */
7240 cur = wp->w_match_head;
7241 while (cur != NULL)
7242 {
7243 cur->hl.rm = cur->match;
7244 if (cur->hlg_id == 0)
7245 cur->hl.attr = 0;
7246 else
7247 cur->hl.attr = syn_id2attr(cur->hlg_id);
7248 cur->hl.buf = wp->w_buffer;
7249 cur->hl.lnum = 0;
7250 cur->hl.first_lnum = 0;
7251# ifdef FEAT_RELTIME
7252 /* Set the time limit to 'redrawtime'. */
7253 profile_setlimit(p_rdt, &(cur->hl.tm));
7254# endif
7255 cur = cur->next;
7256 }
7257 search_hl.buf = wp->w_buffer;
7258 search_hl.lnum = 0;
7259 search_hl.first_lnum = 0;
7260 /* time limit is set at the toplevel, for all windows */
7261}
7262
7263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 * Advance to the match in window "wp" line "lnum" or past it.
7265 */
7266 static void
7267prepare_search_hl(wp, lnum)
7268 win_T *wp;
7269 linenr_T lnum;
7270{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007271 matchitem_T *cur; /* points to the match list */
7272 match_T *shl; /* points to search_hl or a match */
7273 int shl_flag; /* flag to indicate whether search_hl
7274 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int n;
7276
7277 /*
7278 * When using a multi-line pattern, start searching at the top
7279 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007280 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007282 cur = wp->w_match_head;
7283 shl_flag = FALSE;
7284 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007286 if (shl_flag == FALSE)
7287 {
7288 shl = &search_hl;
7289 shl_flag = TRUE;
7290 }
7291 else
7292 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (shl->rm.regprog != NULL
7294 && shl->lnum == 0
7295 && re_multiline(shl->rm.regprog))
7296 {
7297 if (shl->first_lnum == 0)
7298 {
7299# ifdef FEAT_FOLDING
7300 for (shl->first_lnum = lnum;
7301 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7302 if (hasFoldingWin(wp, shl->first_lnum - 1,
7303 NULL, NULL, TRUE, NULL))
7304 break;
7305# else
7306 shl->first_lnum = wp->w_topline;
7307# endif
7308 }
7309 n = 0;
7310 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7311 {
7312 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7313 if (shl->lnum != 0)
7314 {
7315 shl->first_lnum = shl->lnum
7316 + shl->rm.endpos[0].lnum
7317 - shl->rm.startpos[0].lnum;
7318 n = shl->rm.endpos[0].col;
7319 }
7320 else
7321 {
7322 ++shl->first_lnum;
7323 n = 0;
7324 }
7325 }
7326 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007327 if (shl != &search_hl && cur != NULL)
7328 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330}
7331
7332/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007333 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 * Uses shl->buf.
7335 * Sets shl->lnum and shl->rm contents.
7336 * Note: Assumes a previous match is always before "lnum", unless
7337 * shl->lnum is zero.
7338 * Careful: Any pointers for buffer lines will become invalid.
7339 */
7340 static void
7341next_search_hl(win, shl, lnum, mincol)
7342 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007343 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 linenr_T lnum;
7345 colnr_T mincol; /* minimal column for a match */
7346{
7347 linenr_T l;
7348 colnr_T matchcol;
7349 long nmatched;
7350
7351 if (shl->lnum != 0)
7352 {
7353 /* Check for three situations:
7354 * 1. If the "lnum" is below a previous match, start a new search.
7355 * 2. If the previous match includes "mincol", use it.
7356 * 3. Continue after the previous match.
7357 */
7358 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7359 if (lnum > l)
7360 shl->lnum = 0;
7361 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7362 return;
7363 }
7364
7365 /*
7366 * Repeat searching for a match until one is found that includes "mincol"
7367 * or none is found in this line.
7368 */
7369 called_emsg = FALSE;
7370 for (;;)
7371 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007372#ifdef FEAT_RELTIME
7373 /* Stop searching after passing the time limit. */
7374 if (profile_passed_limit(&(shl->tm)))
7375 {
7376 shl->lnum = 0; /* no match found in time */
7377 break;
7378 }
7379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 /* Three situations:
7381 * 1. No useful previous match: search from start of line.
7382 * 2. Not Vi compatible or empty match: continue at next character.
7383 * Break the loop if this is beyond the end of the line.
7384 * 3. Vi compatible searching: continue at end of previous match.
7385 */
7386 if (shl->lnum == 0)
7387 matchcol = 0;
7388 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7389 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007390 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007392 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007393
7394 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007395 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007396 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007398 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 shl->lnum = 0;
7400 break;
7401 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007402#ifdef FEAT_MBYTE
7403 if (has_mbyte)
7404 matchcol += mb_ptr2len(ml);
7405 else
7406#endif
7407 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409 else
7410 matchcol = shl->rm.endpos[0].col;
7411
7412 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007413 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7414#ifdef FEAT_RELTIME
7415 &(shl->tm)
7416#else
7417 NULL
7418#endif
7419 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007420 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
7422 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007423 if (shl == &search_hl)
7424 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007425 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007426 vim_regfree(shl->rm.regprog);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007427 SET_NO_HLSEARCH(TRUE);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007430 shl->lnum = 0;
7431 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 break;
7433 }
7434 if (nmatched == 0)
7435 {
7436 shl->lnum = 0; /* no match found */
7437 break;
7438 }
7439 if (shl->rm.startpos[0].lnum > 0
7440 || shl->rm.startpos[0].col >= mincol
7441 || nmatched > 1
7442 || shl->rm.endpos[0].col > mincol)
7443 {
7444 shl->lnum += shl->rm.startpos[0].lnum;
7445 break; /* useful match found */
7446 }
7447 }
7448}
7449#endif
7450
7451 static void
7452screen_start_highlight(attr)
7453 int attr;
7454{
7455 attrentry_T *aep = NULL;
7456
7457 screen_attr = attr;
7458 if (full_screen
7459#ifdef WIN3264
7460 && termcap_active
7461#endif
7462 )
7463 {
7464#ifdef FEAT_GUI
7465 if (gui.in_use)
7466 {
7467 char buf[20];
7468
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007469 /* The GUI handles this internally. */
7470 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 OUT_STR(buf);
7472 }
7473 else
7474#endif
7475 {
7476 if (attr > HL_ALL) /* special HL attr. */
7477 {
7478 if (t_colors > 1)
7479 aep = syn_cterm_attr2entry(attr);
7480 else
7481 aep = syn_term_attr2entry(attr);
7482 if (aep == NULL) /* did ":syntax clear" */
7483 attr = 0;
7484 else
7485 attr = aep->ae_attr;
7486 }
7487 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7488 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007489 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7490 && cterm_normal_fg_bold)
7491 /* If the Normal FG color has BOLD attribute and the new HL
7492 * has a FG color defined, clear BOLD. */
7493 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7495 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007496 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7497 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 out_str(T_US);
7499 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7500 out_str(T_CZH);
7501 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7502 out_str(T_MR);
7503
7504 /*
7505 * Output the color or start string after bold etc., in case the
7506 * bold etc. override the color setting.
7507 */
7508 if (aep != NULL)
7509 {
7510 if (t_colors > 1)
7511 {
7512 if (aep->ae_u.cterm.fg_color)
7513 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7514 if (aep->ae_u.cterm.bg_color)
7515 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7516 }
7517 else
7518 {
7519 if (aep->ae_u.term.start != NULL)
7520 out_str(aep->ae_u.term.start);
7521 }
7522 }
7523 }
7524 }
7525}
7526
7527 void
7528screen_stop_highlight()
7529{
7530 int do_ME = FALSE; /* output T_ME code */
7531
7532 if (screen_attr != 0
7533#ifdef WIN3264
7534 && termcap_active
7535#endif
7536 )
7537 {
7538#ifdef FEAT_GUI
7539 if (gui.in_use)
7540 {
7541 char buf[20];
7542
7543 /* use internal GUI code */
7544 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7545 OUT_STR(buf);
7546 }
7547 else
7548#endif
7549 {
7550 if (screen_attr > HL_ALL) /* special HL attr. */
7551 {
7552 attrentry_T *aep;
7553
7554 if (t_colors > 1)
7555 {
7556 /*
7557 * Assume that t_me restores the original colors!
7558 */
7559 aep = syn_cterm_attr2entry(screen_attr);
7560 if (aep != NULL && (aep->ae_u.cterm.fg_color
7561 || aep->ae_u.cterm.bg_color))
7562 do_ME = TRUE;
7563 }
7564 else
7565 {
7566 aep = syn_term_attr2entry(screen_attr);
7567 if (aep != NULL && aep->ae_u.term.stop != NULL)
7568 {
7569 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7570 do_ME = TRUE;
7571 else
7572 out_str(aep->ae_u.term.stop);
7573 }
7574 }
7575 if (aep == NULL) /* did ":syntax clear" */
7576 screen_attr = 0;
7577 else
7578 screen_attr = aep->ae_attr;
7579 }
7580
7581 /*
7582 * Often all ending-codes are equal to T_ME. Avoid outputting the
7583 * same sequence several times.
7584 */
7585 if (screen_attr & HL_STANDOUT)
7586 {
7587 if (STRCMP(T_SE, T_ME) == 0)
7588 do_ME = TRUE;
7589 else
7590 out_str(T_SE);
7591 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007592 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
7594 if (STRCMP(T_UE, T_ME) == 0)
7595 do_ME = TRUE;
7596 else
7597 out_str(T_UE);
7598 }
7599 if (screen_attr & HL_ITALIC)
7600 {
7601 if (STRCMP(T_CZR, T_ME) == 0)
7602 do_ME = TRUE;
7603 else
7604 out_str(T_CZR);
7605 }
7606 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7607 out_str(T_ME);
7608
7609 if (t_colors > 1)
7610 {
7611 /* set Normal cterm colors */
7612 if (cterm_normal_fg_color != 0)
7613 term_fg_color(cterm_normal_fg_color - 1);
7614 if (cterm_normal_bg_color != 0)
7615 term_bg_color(cterm_normal_bg_color - 1);
7616 if (cterm_normal_fg_bold)
7617 out_str(T_MD);
7618 }
7619 }
7620 }
7621 screen_attr = 0;
7622}
7623
7624/*
7625 * Reset the colors for a cterm. Used when leaving Vim.
7626 * The machine specific code may override this again.
7627 */
7628 void
7629reset_cterm_colors()
7630{
7631 if (t_colors > 1)
7632 {
7633 /* set Normal cterm colors */
7634 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7635 {
7636 out_str(T_OP);
7637 screen_attr = -1;
7638 }
7639 if (cterm_normal_fg_bold)
7640 {
7641 out_str(T_ME);
7642 screen_attr = -1;
7643 }
7644 }
7645}
7646
7647/*
7648 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7649 * using the attributes from ScreenAttrs["off"].
7650 */
7651 static void
7652screen_char(off, row, col)
7653 unsigned off;
7654 int row;
7655 int col;
7656{
7657 int attr;
7658
7659 /* Check for illegal values, just in case (could happen just after
7660 * resizing). */
7661 if (row >= screen_Rows || col >= screen_Columns)
7662 return;
7663
7664 /* Outputting the last character on the screen may scrollup the screen.
7665 * Don't to it! Mark the character invalid (update it when scrolled up) */
7666 if (row == screen_Rows - 1 && col == screen_Columns - 1
7667#ifdef FEAT_RIGHTLEFT
7668 /* account for first command-line character in rightleft mode */
7669 && !cmdmsg_rl
7670#endif
7671 )
7672 {
7673 ScreenAttrs[off] = (sattr_T)-1;
7674 return;
7675 }
7676
7677 /*
7678 * Stop highlighting first, so it's easier to move the cursor.
7679 */
7680#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7681 if (screen_char_attr != 0)
7682 attr = screen_char_attr;
7683 else
7684#endif
7685 attr = ScreenAttrs[off];
7686 if (screen_attr != attr)
7687 screen_stop_highlight();
7688
7689 windgoto(row, col);
7690
7691 if (screen_attr != attr)
7692 screen_start_highlight(attr);
7693
7694#ifdef FEAT_MBYTE
7695 if (enc_utf8 && ScreenLinesUC[off] != 0)
7696 {
7697 char_u buf[MB_MAXBYTES + 1];
7698
7699 /* Convert UTF-8 character to bytes and write it. */
7700
7701 buf[utfc_char2bytes(off, buf)] = NUL;
7702
7703 out_str(buf);
7704 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7705 ++screen_cur_col;
7706 }
7707 else
7708#endif
7709 {
7710#ifdef FEAT_MBYTE
7711 out_flush_check();
7712#endif
7713 out_char(ScreenLines[off]);
7714#ifdef FEAT_MBYTE
7715 /* double-byte character in single-width cell */
7716 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7717 out_char(ScreenLines2[off]);
7718#endif
7719 }
7720
7721 screen_cur_col++;
7722}
7723
7724#ifdef FEAT_MBYTE
7725
7726/*
7727 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7728 * on the screen at position 'row' and 'col'.
7729 * The attributes of the first byte is used for all. This is required to
7730 * output the two bytes of a double-byte character with nothing in between.
7731 */
7732 static void
7733screen_char_2(off, row, col)
7734 unsigned off;
7735 int row;
7736 int col;
7737{
7738 /* Check for illegal values (could be wrong when screen was resized). */
7739 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7740 return;
7741
7742 /* Outputting the last character on the screen may scrollup the screen.
7743 * Don't to it! Mark the character invalid (update it when scrolled up) */
7744 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7745 {
7746 ScreenAttrs[off] = (sattr_T)-1;
7747 return;
7748 }
7749
7750 /* Output the first byte normally (positions the cursor), then write the
7751 * second byte directly. */
7752 screen_char(off, row, col);
7753 out_char(ScreenLines[off + 1]);
7754 ++screen_cur_col;
7755}
7756#endif
7757
7758#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7759/*
7760 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7761 * This uses the contents of ScreenLines[] and doesn't change it.
7762 */
7763 void
7764screen_draw_rectangle(row, col, height, width, invert)
7765 int row;
7766 int col;
7767 int height;
7768 int width;
7769 int invert;
7770{
7771 int r, c;
7772 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007773#ifdef FEAT_MBYTE
7774 int max_off;
7775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007777 /* Can't use ScreenLines unless initialized */
7778 if (ScreenLines == NULL)
7779 return;
7780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (invert)
7782 screen_char_attr = HL_INVERSE;
7783 for (r = row; r < row + height; ++r)
7784 {
7785 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007786#ifdef FEAT_MBYTE
7787 max_off = off + screen_Columns;
7788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 for (c = col; c < col + width; ++c)
7790 {
7791#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007792 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 screen_char_2(off + c, r, c);
7795 ++c;
7796 }
7797 else
7798#endif
7799 {
7800 screen_char(off + c, r, c);
7801#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007802 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 ++c;
7804#endif
7805 }
7806 }
7807 }
7808 screen_char_attr = 0;
7809}
7810#endif
7811
7812#ifdef FEAT_VERTSPLIT
7813/*
7814 * Redraw the characters for a vertically split window.
7815 */
7816 static void
7817redraw_block(row, end, wp)
7818 int row;
7819 int end;
7820 win_T *wp;
7821{
7822 int col;
7823 int width;
7824
7825# ifdef FEAT_CLIPBOARD
7826 clip_may_clear_selection(row, end - 1);
7827# endif
7828
7829 if (wp == NULL)
7830 {
7831 col = 0;
7832 width = Columns;
7833 }
7834 else
7835 {
7836 col = wp->w_wincol;
7837 width = wp->w_width;
7838 }
7839 screen_draw_rectangle(row, col, end - row, width, FALSE);
7840}
7841#endif
7842
7843/*
7844 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7845 * with character 'c1' in first column followed by 'c2' in the other columns.
7846 * Use attributes 'attr'.
7847 */
7848 void
7849screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7850 int start_row, end_row;
7851 int start_col, end_col;
7852 int c1, c2;
7853 int attr;
7854{
7855 int row;
7856 int col;
7857 int off;
7858 int end_off;
7859 int did_delete;
7860 int c;
7861 int norm_term;
7862#if defined(FEAT_GUI) || defined(UNIX)
7863 int force_next = FALSE;
7864#endif
7865
7866 if (end_row > screen_Rows) /* safety check */
7867 end_row = screen_Rows;
7868 if (end_col > screen_Columns) /* safety check */
7869 end_col = screen_Columns;
7870 if (ScreenLines == NULL
7871 || start_row >= end_row
7872 || start_col >= end_col) /* nothing to do */
7873 return;
7874
7875 /* it's a "normal" terminal when not in a GUI or cterm */
7876 norm_term = (
7877#ifdef FEAT_GUI
7878 !gui.in_use &&
7879#endif
7880 t_colors <= 1);
7881 for (row = start_row; row < end_row; ++row)
7882 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007883#ifdef FEAT_MBYTE
7884 if (has_mbyte
7885# ifdef FEAT_GUI
7886 && !gui.in_use
7887# endif
7888 )
7889 {
7890 /* When drawing over the right halve of a double-wide char clear
7891 * out the left halve. When drawing over the left halve of a
7892 * double wide-char clear out the right halve. Only needed in a
7893 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007894 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007895 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007896 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007897 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007898 }
7899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 /*
7901 * Try to use delete-line termcap code, when no attributes or in a
7902 * "normal" terminal, where a bold/italic space is just a
7903 * space.
7904 */
7905 did_delete = FALSE;
7906 if (c2 == ' '
7907 && end_col == Columns
7908 && can_clear(T_CE)
7909 && (attr == 0
7910 || (norm_term
7911 && attr <= HL_ALL
7912 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7913 {
7914 /*
7915 * check if we really need to clear something
7916 */
7917 col = start_col;
7918 if (c1 != ' ') /* don't clear first char */
7919 ++col;
7920
7921 off = LineOffset[row] + col;
7922 end_off = LineOffset[row] + end_col;
7923
7924 /* skip blanks (used often, keep it fast!) */
7925#ifdef FEAT_MBYTE
7926 if (enc_utf8)
7927 while (off < end_off && ScreenLines[off] == ' '
7928 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7929 ++off;
7930 else
7931#endif
7932 while (off < end_off && ScreenLines[off] == ' '
7933 && ScreenAttrs[off] == 0)
7934 ++off;
7935 if (off < end_off) /* something to be cleared */
7936 {
7937 col = off - LineOffset[row];
7938 screen_stop_highlight();
7939 term_windgoto(row, col);/* clear rest of this screen line */
7940 out_str(T_CE);
7941 screen_start(); /* don't know where cursor is now */
7942 col = end_col - col;
7943 while (col--) /* clear chars in ScreenLines */
7944 {
7945 ScreenLines[off] = ' ';
7946#ifdef FEAT_MBYTE
7947 if (enc_utf8)
7948 ScreenLinesUC[off] = 0;
7949#endif
7950 ScreenAttrs[off] = 0;
7951 ++off;
7952 }
7953 }
7954 did_delete = TRUE; /* the chars are cleared now */
7955 }
7956
7957 off = LineOffset[row] + start_col;
7958 c = c1;
7959 for (col = start_col; col < end_col; ++col)
7960 {
7961 if (ScreenLines[off] != c
7962#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007963 || (enc_utf8 && (int)ScreenLinesUC[off]
7964 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965#endif
7966 || ScreenAttrs[off] != attr
7967#if defined(FEAT_GUI) || defined(UNIX)
7968 || force_next
7969#endif
7970 )
7971 {
7972#if defined(FEAT_GUI) || defined(UNIX)
7973 /* The bold trick may make a single row of pixels appear in
7974 * the next character. When a bold character is removed, the
7975 * next character should be redrawn too. This happens for our
7976 * own GUI and for some xterms. */
7977 if (
7978# ifdef FEAT_GUI
7979 gui.in_use
7980# endif
7981# if defined(FEAT_GUI) && defined(UNIX)
7982 ||
7983# endif
7984# ifdef UNIX
7985 term_is_xterm
7986# endif
7987 )
7988 {
7989 if (ScreenLines[off] != ' '
7990 && (ScreenAttrs[off] > HL_ALL
7991 || ScreenAttrs[off] & HL_BOLD))
7992 force_next = TRUE;
7993 else
7994 force_next = FALSE;
7995 }
7996#endif
7997 ScreenLines[off] = c;
7998#ifdef FEAT_MBYTE
7999 if (enc_utf8)
8000 {
8001 if (c >= 0x80)
8002 {
8003 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008004 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006 else
8007 ScreenLinesUC[off] = 0;
8008 }
8009#endif
8010 ScreenAttrs[off] = attr;
8011 if (!did_delete || c != ' ')
8012 screen_char(off, row, col);
8013 }
8014 ++off;
8015 if (col == start_col)
8016 {
8017 if (did_delete)
8018 break;
8019 c = c2;
8020 }
8021 }
8022 if (end_col == Columns)
8023 LineWraps[row] = FALSE;
8024 if (row == Rows - 1) /* overwritten the command line */
8025 {
8026 redraw_cmdline = TRUE;
8027 if (c1 == ' ' && c2 == ' ')
8028 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008029 if (start_col == 0)
8030 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 }
8033}
8034
8035/*
8036 * Check if there should be a delay. Used before clearing or redrawing the
8037 * screen or the command line.
8038 */
8039 void
8040check_for_delay(check_msg_scroll)
8041 int check_msg_scroll;
8042{
8043 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8044 && !did_wait_return
8045 && emsg_silent == 0)
8046 {
8047 out_flush();
8048 ui_delay(1000L, TRUE);
8049 emsg_on_display = FALSE;
8050 if (check_msg_scroll)
8051 msg_scroll = FALSE;
8052 }
8053}
8054
8055/*
8056 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008057 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 * Returns TRUE if there is a valid screen to write to.
8059 * Returns FALSE when starting up and screen not initialized yet.
8060 */
8061 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008062screen_valid(doclear)
8063 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008065 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 return (ScreenLines != NULL);
8067}
8068
8069/*
8070 * Resize the shell to Rows and Columns.
8071 * Allocate ScreenLines[] and associated items.
8072 *
8073 * There may be some time between setting Rows and Columns and (re)allocating
8074 * ScreenLines[]. This happens when starting up and when (manually) changing
8075 * the shell size. Always use screen_Rows and screen_Columns to access items
8076 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8077 * final size of the shell is needed.
8078 */
8079 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008080screenalloc(doclear)
8081 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 int new_row, old_row;
8084#ifdef FEAT_GUI
8085 int old_Rows;
8086#endif
8087 win_T *wp;
8088 int outofmem = FALSE;
8089 int len;
8090 schar_T *new_ScreenLines;
8091#ifdef FEAT_MBYTE
8092 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008093 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008095 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096#endif
8097 sattr_T *new_ScreenAttrs;
8098 unsigned *new_LineOffset;
8099 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008100#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008101 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008102 tabpage_T *tp;
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008105 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008106#ifdef FEAT_AUTOCMD
8107 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008109retry:
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 /*
8112 * Allocation of the screen buffers is done only when the size changes and
8113 * when Rows and Columns have been set and we have started doing full
8114 * screen stuff.
8115 */
8116 if ((ScreenLines != NULL
8117 && Rows == screen_Rows
8118 && Columns == screen_Columns
8119#ifdef FEAT_MBYTE
8120 && enc_utf8 == (ScreenLinesUC != NULL)
8121 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008122 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123#endif
8124 )
8125 || Rows == 0
8126 || Columns == 0
8127 || (!full_screen && ScreenLines == NULL))
8128 return;
8129
8130 /*
8131 * It's possible that we produce an out-of-memory message below, which
8132 * will cause this function to be called again. To break the loop, just
8133 * return here.
8134 */
8135 if (entered)
8136 return;
8137 entered = TRUE;
8138
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008139 /*
8140 * Note that the window sizes are updated before reallocating the arrays,
8141 * thus we must not redraw here!
8142 */
8143 ++RedrawingDisabled;
8144
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 win_new_shellsize(); /* fit the windows in the new sized shell */
8146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 comp_col(); /* recompute columns for shown command and ruler */
8148
8149 /*
8150 * We're changing the size of the screen.
8151 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8152 * - Move lines from the old arrays into the new arrays, clear extra
8153 * lines (unless the screen is going to be cleared).
8154 * - Free the old arrays.
8155 *
8156 * If anything fails, make ScreenLines NULL, so we don't do anything!
8157 * Continuing with the old ScreenLines may result in a crash, because the
8158 * size is wrong.
8159 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008160 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008162#ifdef FEAT_AUTOCMD
8163 if (aucmd_win != NULL)
8164 win_free_lsize(aucmd_win);
8165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166
8167 new_ScreenLines = (schar_T *)lalloc((long_u)(
8168 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8169#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008170 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 if (enc_utf8)
8172 {
8173 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8174 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008175 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008176 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8178 }
8179 if (enc_dbcs == DBCS_JPNU)
8180 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8181 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8182#endif
8183 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8184 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8185 new_LineOffset = (unsigned *)lalloc((long_u)(
8186 Rows * sizeof(unsigned)), FALSE);
8187 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008188#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008189 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008192 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 if (win_alloc_lines(wp) == FAIL)
8195 {
8196 outofmem = TRUE;
8197#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008198 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199#endif
8200 }
8201 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008202#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008203 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8204 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008205 outofmem = TRUE;
8206#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008207#ifdef FEAT_WINDOWS
8208give_up:
8209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008211#ifdef FEAT_MBYTE
8212 for (i = 0; i < p_mco; ++i)
8213 if (new_ScreenLinesC[i] == NULL)
8214 break;
8215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 if (new_ScreenLines == NULL
8217#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008218 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8220#endif
8221 || new_ScreenAttrs == NULL
8222 || new_LineOffset == NULL
8223 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008224#ifdef FEAT_WINDOWS
8225 || new_TabPageIdxs == NULL
8226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 || outofmem)
8228 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008229 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008230 {
8231 /* guess the size */
8232 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8233
8234 /* Remember we did this to avoid getting outofmem messages over
8235 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008236 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 vim_free(new_ScreenLines);
8239 new_ScreenLines = NULL;
8240#ifdef FEAT_MBYTE
8241 vim_free(new_ScreenLinesUC);
8242 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008243 for (i = 0; i < p_mco; ++i)
8244 {
8245 vim_free(new_ScreenLinesC[i]);
8246 new_ScreenLinesC[i] = NULL;
8247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 vim_free(new_ScreenLines2);
8249 new_ScreenLines2 = NULL;
8250#endif
8251 vim_free(new_ScreenAttrs);
8252 new_ScreenAttrs = NULL;
8253 vim_free(new_LineOffset);
8254 new_LineOffset = NULL;
8255 vim_free(new_LineWraps);
8256 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008257#ifdef FEAT_WINDOWS
8258 vim_free(new_TabPageIdxs);
8259 new_TabPageIdxs = NULL;
8260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262 else
8263 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008264 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 for (new_row = 0; new_row < Rows; ++new_row)
8267 {
8268 new_LineOffset[new_row] = new_row * Columns;
8269 new_LineWraps[new_row] = FALSE;
8270
8271 /*
8272 * If the screen is not going to be cleared, copy as much as
8273 * possible from the old screen to the new one and clear the rest
8274 * (used when resizing the window at the "--more--" prompt or when
8275 * executing an external command, for the GUI).
8276 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008277 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {
8279 (void)vim_memset(new_ScreenLines + new_row * Columns,
8280 ' ', (size_t)Columns * sizeof(schar_T));
8281#ifdef FEAT_MBYTE
8282 if (enc_utf8)
8283 {
8284 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8285 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008286 for (i = 0; i < p_mco; ++i)
8287 (void)vim_memset(new_ScreenLinesC[i]
8288 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 0, (size_t)Columns * sizeof(u8char_T));
8290 }
8291 if (enc_dbcs == DBCS_JPNU)
8292 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8293 0, (size_t)Columns * sizeof(schar_T));
8294#endif
8295 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8296 0, (size_t)Columns * sizeof(sattr_T));
8297 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008298 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {
8300 if (screen_Columns < Columns)
8301 len = screen_Columns;
8302 else
8303 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008304#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008305 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008306 * may be invalid now. Also when p_mco changes. */
8307 if (!(enc_utf8 && ScreenLinesUC == NULL)
8308 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008309#endif
8310 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8311 ScreenLines + LineOffset[old_row],
8312 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008314 if (enc_utf8 && ScreenLinesUC != NULL
8315 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
8317 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8318 ScreenLinesUC + LineOffset[old_row],
8319 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008320 for (i = 0; i < p_mco; ++i)
8321 mch_memmove(new_ScreenLinesC[i]
8322 + new_LineOffset[new_row],
8323 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 (size_t)len * sizeof(u8char_T));
8325 }
8326 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8327 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8328 ScreenLines2 + LineOffset[old_row],
8329 (size_t)len * sizeof(schar_T));
8330#endif
8331 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8332 ScreenAttrs + LineOffset[old_row],
8333 (size_t)len * sizeof(sattr_T));
8334 }
8335 }
8336 }
8337 /* Use the last line of the screen for the current line. */
8338 current_ScreenLine = new_ScreenLines + Rows * Columns;
8339 }
8340
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008341 free_screenlines();
8342
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 ScreenLines = new_ScreenLines;
8344#ifdef FEAT_MBYTE
8345 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008346 for (i = 0; i < p_mco; ++i)
8347 ScreenLinesC[i] = new_ScreenLinesC[i];
8348 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 ScreenLines2 = new_ScreenLines2;
8350#endif
8351 ScreenAttrs = new_ScreenAttrs;
8352 LineOffset = new_LineOffset;
8353 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008354#ifdef FEAT_WINDOWS
8355 TabPageIdxs = new_TabPageIdxs;
8356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
8358 /* It's important that screen_Rows and screen_Columns reflect the actual
8359 * size of ScreenLines[]. Set them before calling anything. */
8360#ifdef FEAT_GUI
8361 old_Rows = screen_Rows;
8362#endif
8363 screen_Rows = Rows;
8364 screen_Columns = Columns;
8365
8366 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008367 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 screenclear2();
8369
8370#ifdef FEAT_GUI
8371 else if (gui.in_use
8372 && !gui.starting
8373 && ScreenLines != NULL
8374 && old_Rows != Rows)
8375 {
8376 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8377 /*
8378 * Adjust the position of the cursor, for when executing an external
8379 * command.
8380 */
8381 if (msg_row >= Rows) /* Rows got smaller */
8382 msg_row = Rows - 1; /* put cursor at last row */
8383 else if (Rows > old_Rows) /* Rows got bigger */
8384 msg_row += Rows - old_Rows; /* put cursor in same place */
8385 if (msg_col >= Columns) /* Columns got smaller */
8386 msg_col = Columns - 1; /* put cursor at last column */
8387 }
8388#endif
8389
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008391 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008392
8393#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008394 /*
8395 * Do not apply autocommands more than 3 times to avoid an endless loop
8396 * in case applying autocommands always changes Rows or Columns.
8397 */
8398 if (starting == 0 && ++retry_count <= 3)
8399 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008400 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008401 /* In rare cases, autocommands may have altered Rows or Columns,
8402 * jump back to check if we need to allocate the screen again. */
8403 goto retry;
8404 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
8408 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008409free_screenlines()
8410{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008411#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008412 int i;
8413
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008414 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008415 for (i = 0; i < Screen_mco; ++i)
8416 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008417 vim_free(ScreenLines2);
8418#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008419 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008420 vim_free(ScreenAttrs);
8421 vim_free(LineOffset);
8422 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008423#ifdef FEAT_WINDOWS
8424 vim_free(TabPageIdxs);
8425#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008426}
8427
8428 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429screenclear()
8430{
8431 check_for_delay(FALSE);
8432 screenalloc(FALSE); /* allocate screen buffers if size changed */
8433 screenclear2(); /* clear the screen */
8434}
8435
8436 static void
8437screenclear2()
8438{
8439 int i;
8440
8441 if (starting == NO_SCREEN || ScreenLines == NULL
8442#ifdef FEAT_GUI
8443 || (gui.in_use && gui.starting)
8444#endif
8445 )
8446 return;
8447
8448#ifdef FEAT_GUI
8449 if (!gui.in_use)
8450#endif
8451 screen_attr = -1; /* force setting the Normal colors */
8452 screen_stop_highlight(); /* don't want highlighting here */
8453
8454#ifdef FEAT_CLIPBOARD
8455 /* disable selection without redrawing it */
8456 clip_scroll_selection(9999);
8457#endif
8458
8459 /* blank out ScreenLines */
8460 for (i = 0; i < Rows; ++i)
8461 {
8462 lineclear(LineOffset[i], (int)Columns);
8463 LineWraps[i] = FALSE;
8464 }
8465
8466 if (can_clear(T_CL))
8467 {
8468 out_str(T_CL); /* clear the display */
8469 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008470 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 }
8472 else
8473 {
8474 /* can't clear the screen, mark all chars with invalid attributes */
8475 for (i = 0; i < Rows; ++i)
8476 lineinvalid(LineOffset[i], (int)Columns);
8477 clear_cmdline = TRUE;
8478 }
8479
8480 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8481
8482 win_rest_invalid(firstwin);
8483 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008484#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008485 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 if (must_redraw == CLEAR) /* no need to clear again */
8488 must_redraw = NOT_VALID;
8489 compute_cmdrow();
8490 msg_row = cmdline_row; /* put cursor on last line for messages */
8491 msg_col = 0;
8492 screen_start(); /* don't know where cursor is now */
8493 msg_scrolled = 0; /* can't scroll back */
8494 msg_didany = FALSE;
8495 msg_didout = FALSE;
8496}
8497
8498/*
8499 * Clear one line in ScreenLines.
8500 */
8501 static void
8502lineclear(off, width)
8503 unsigned off;
8504 int width;
8505{
8506 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8507#ifdef FEAT_MBYTE
8508 if (enc_utf8)
8509 (void)vim_memset(ScreenLinesUC + off, 0,
8510 (size_t)width * sizeof(u8char_T));
8511#endif
8512 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8513}
8514
8515/*
8516 * Mark one line in ScreenLines invalid by setting the attributes to an
8517 * invalid value.
8518 */
8519 static void
8520lineinvalid(off, width)
8521 unsigned off;
8522 int width;
8523{
8524 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8525}
8526
8527#ifdef FEAT_VERTSPLIT
8528/*
8529 * Copy part of a Screenline for vertically split window "wp".
8530 */
8531 static void
8532linecopy(to, from, wp)
8533 int to;
8534 int from;
8535 win_T *wp;
8536{
8537 unsigned off_to = LineOffset[to] + wp->w_wincol;
8538 unsigned off_from = LineOffset[from] + wp->w_wincol;
8539
8540 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8541 wp->w_width * sizeof(schar_T));
8542# ifdef FEAT_MBYTE
8543 if (enc_utf8)
8544 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008545 int i;
8546
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8548 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008549 for (i = 0; i < p_mco; ++i)
8550 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8551 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 }
8553 if (enc_dbcs == DBCS_JPNU)
8554 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8555 wp->w_width * sizeof(schar_T));
8556# endif
8557 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8558 wp->w_width * sizeof(sattr_T));
8559}
8560#endif
8561
8562/*
8563 * Return TRUE if clearing with term string "p" would work.
8564 * It can't work when the string is empty or it won't set the right background.
8565 */
8566 int
8567can_clear(p)
8568 char_u *p;
8569{
8570 return (*p != NUL && (t_colors <= 1
8571#ifdef FEAT_GUI
8572 || gui.in_use
8573#endif
8574 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8575}
8576
8577/*
8578 * Reset cursor position. Use whenever cursor was moved because of outputting
8579 * something directly to the screen (shell commands) or a terminal control
8580 * code.
8581 */
8582 void
8583screen_start()
8584{
8585 screen_cur_row = screen_cur_col = 9999;
8586}
8587
8588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 * Move the cursor to position "row","col" in the screen.
8590 * This tries to find the most efficient way to move, minimizing the number of
8591 * characters sent to the terminal.
8592 */
8593 void
8594windgoto(row, col)
8595 int row;
8596 int col;
8597{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008598 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 int i;
8600 int plan;
8601 int cost;
8602 int wouldbe_col;
8603 int noinvcurs;
8604 char_u *bs;
8605 int goto_cost;
8606 int attr;
8607
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008608#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8610
8611#define PLAN_LE 1
8612#define PLAN_CR 2
8613#define PLAN_NL 3
8614#define PLAN_WRITE 4
8615 /* Can't use ScreenLines unless initialized */
8616 if (ScreenLines == NULL)
8617 return;
8618
8619 if (col != screen_cur_col || row != screen_cur_row)
8620 {
8621 /* Check for valid position. */
8622 if (row < 0) /* window without text lines? */
8623 row = 0;
8624 if (row >= screen_Rows)
8625 row = screen_Rows - 1;
8626 if (col >= screen_Columns)
8627 col = screen_Columns - 1;
8628
8629 /* check if no cursor movement is allowed in highlight mode */
8630 if (screen_attr && *T_MS == NUL)
8631 noinvcurs = HIGHL_COST;
8632 else
8633 noinvcurs = 0;
8634 goto_cost = GOTO_COST + noinvcurs;
8635
8636 /*
8637 * Plan how to do the positioning:
8638 * 1. Use CR to move it to column 0, same row.
8639 * 2. Use T_LE to move it a few columns to the left.
8640 * 3. Use NL to move a few lines down, column 0.
8641 * 4. Move a few columns to the right with T_ND or by writing chars.
8642 *
8643 * Don't do this if the cursor went beyond the last column, the cursor
8644 * position is unknown then (some terminals wrap, some don't )
8645 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008646 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 * characters to move the cursor to the right.
8648 */
8649 if (row >= screen_cur_row && screen_cur_col < Columns)
8650 {
8651 /*
8652 * If the cursor is in the same row, bigger col, we can use CR
8653 * or T_LE.
8654 */
8655 bs = NULL; /* init for GCC */
8656 attr = screen_attr;
8657 if (row == screen_cur_row && col < screen_cur_col)
8658 {
8659 /* "le" is preferred over "bc", because "bc" is obsolete */
8660 if (*T_LE)
8661 bs = T_LE; /* "cursor left" */
8662 else
8663 bs = T_BC; /* "backspace character (old) */
8664 if (*bs)
8665 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8666 else
8667 cost = 999;
8668 if (col + 1 < cost) /* using CR is less characters */
8669 {
8670 plan = PLAN_CR;
8671 wouldbe_col = 0;
8672 cost = 1; /* CR is just one character */
8673 }
8674 else
8675 {
8676 plan = PLAN_LE;
8677 wouldbe_col = col;
8678 }
8679 if (noinvcurs) /* will stop highlighting */
8680 {
8681 cost += noinvcurs;
8682 attr = 0;
8683 }
8684 }
8685
8686 /*
8687 * If the cursor is above where we want to be, we can use CR LF.
8688 */
8689 else if (row > screen_cur_row)
8690 {
8691 plan = PLAN_NL;
8692 wouldbe_col = 0;
8693 cost = (row - screen_cur_row) * 2; /* CR LF */
8694 if (noinvcurs) /* will stop highlighting */
8695 {
8696 cost += noinvcurs;
8697 attr = 0;
8698 }
8699 }
8700
8701 /*
8702 * If the cursor is in the same row, smaller col, just use write.
8703 */
8704 else
8705 {
8706 plan = PLAN_WRITE;
8707 wouldbe_col = screen_cur_col;
8708 cost = 0;
8709 }
8710
8711 /*
8712 * Check if any characters that need to be written have the
8713 * correct attributes. Also avoid UTF-8 characters.
8714 */
8715 i = col - wouldbe_col;
8716 if (i > 0)
8717 cost += i;
8718 if (cost < goto_cost && i > 0)
8719 {
8720 /*
8721 * Check if the attributes are correct without additionally
8722 * stopping highlighting.
8723 */
8724 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8725 while (i && *p++ == attr)
8726 --i;
8727 if (i != 0)
8728 {
8729 /*
8730 * Try if it works when highlighting is stopped here.
8731 */
8732 if (*--p == 0)
8733 {
8734 cost += noinvcurs;
8735 while (i && *p++ == 0)
8736 --i;
8737 }
8738 if (i != 0)
8739 cost = 999; /* different attributes, don't do it */
8740 }
8741#ifdef FEAT_MBYTE
8742 if (enc_utf8)
8743 {
8744 /* Don't use an UTF-8 char for positioning, it's slow. */
8745 for (i = wouldbe_col; i < col; ++i)
8746 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8747 {
8748 cost = 999;
8749 break;
8750 }
8751 }
8752#endif
8753 }
8754
8755 /*
8756 * We can do it without term_windgoto()!
8757 */
8758 if (cost < goto_cost)
8759 {
8760 if (plan == PLAN_LE)
8761 {
8762 if (noinvcurs)
8763 screen_stop_highlight();
8764 while (screen_cur_col > col)
8765 {
8766 out_str(bs);
8767 --screen_cur_col;
8768 }
8769 }
8770 else if (plan == PLAN_CR)
8771 {
8772 if (noinvcurs)
8773 screen_stop_highlight();
8774 out_char('\r');
8775 screen_cur_col = 0;
8776 }
8777 else if (plan == PLAN_NL)
8778 {
8779 if (noinvcurs)
8780 screen_stop_highlight();
8781 while (screen_cur_row < row)
8782 {
8783 out_char('\n');
8784 ++screen_cur_row;
8785 }
8786 screen_cur_col = 0;
8787 }
8788
8789 i = col - screen_cur_col;
8790 if (i > 0)
8791 {
8792 /*
8793 * Use cursor-right if it's one character only. Avoids
8794 * removing a line of pixels from the last bold char, when
8795 * using the bold trick in the GUI.
8796 */
8797 if (T_ND[0] != NUL && T_ND[1] == NUL)
8798 {
8799 while (i-- > 0)
8800 out_char(*T_ND);
8801 }
8802 else
8803 {
8804 int off;
8805
8806 off = LineOffset[row] + screen_cur_col;
8807 while (i-- > 0)
8808 {
8809 if (ScreenAttrs[off] != screen_attr)
8810 screen_stop_highlight();
8811#ifdef FEAT_MBYTE
8812 out_flush_check();
8813#endif
8814 out_char(ScreenLines[off]);
8815#ifdef FEAT_MBYTE
8816 if (enc_dbcs == DBCS_JPNU
8817 && ScreenLines[off] == 0x8e)
8818 out_char(ScreenLines2[off]);
8819#endif
8820 ++off;
8821 }
8822 }
8823 }
8824 }
8825 }
8826 else
8827 cost = 999;
8828
8829 if (cost >= goto_cost)
8830 {
8831 if (noinvcurs)
8832 screen_stop_highlight();
8833 if (row == screen_cur_row && (col > screen_cur_col) &&
8834 *T_CRI != NUL)
8835 term_cursor_right(col - screen_cur_col);
8836 else
8837 term_windgoto(row, col);
8838 }
8839 screen_cur_row = row;
8840 screen_cur_col = col;
8841 }
8842}
8843
8844/*
8845 * Set cursor to its position in the current window.
8846 */
8847 void
8848setcursor()
8849{
8850 if (redrawing())
8851 {
8852 validate_cursor();
8853 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8854 W_WINCOL(curwin) + (
8855#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008856 /* With 'rightleft' set and the cursor on a double-wide
8857 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8859# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008860 (has_mbyte
8861 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8862 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863# endif
8864 1)) :
8865#endif
8866 curwin->w_wcol));
8867 }
8868}
8869
8870
8871/*
8872 * insert 'line_count' lines at 'row' in window 'wp'
8873 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8874 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8875 * scrolling.
8876 * Returns FAIL if the lines are not inserted, OK for success.
8877 */
8878 int
8879win_ins_lines(wp, row, line_count, invalid, mayclear)
8880 win_T *wp;
8881 int row;
8882 int line_count;
8883 int invalid;
8884 int mayclear;
8885{
8886 int did_delete;
8887 int nextrow;
8888 int lastrow;
8889 int retval;
8890
8891 if (invalid)
8892 wp->w_lines_valid = 0;
8893
8894 if (wp->w_height < 5)
8895 return FAIL;
8896
8897 if (line_count > wp->w_height - row)
8898 line_count = wp->w_height - row;
8899
8900 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8901 if (retval != MAYBE)
8902 return retval;
8903
8904 /*
8905 * If there is a next window or a status line, we first try to delete the
8906 * lines at the bottom to avoid messing what is after the window.
8907 * If this fails and there are following windows, don't do anything to avoid
8908 * messing up those windows, better just redraw.
8909 */
8910 did_delete = FALSE;
8911#ifdef FEAT_WINDOWS
8912 if (wp->w_next != NULL || wp->w_status_height)
8913 {
8914 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8915 line_count, (int)Rows, FALSE, NULL) == OK)
8916 did_delete = TRUE;
8917 else if (wp->w_next)
8918 return FAIL;
8919 }
8920#endif
8921 /*
8922 * if no lines deleted, blank the lines that will end up below the window
8923 */
8924 if (!did_delete)
8925 {
8926#ifdef FEAT_WINDOWS
8927 wp->w_redr_status = TRUE;
8928#endif
8929 redraw_cmdline = TRUE;
8930 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8931 lastrow = nextrow + line_count;
8932 if (lastrow > Rows)
8933 lastrow = Rows;
8934 screen_fill(nextrow - line_count, lastrow - line_count,
8935 W_WINCOL(wp), (int)W_ENDCOL(wp),
8936 ' ', ' ', 0);
8937 }
8938
8939 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8940 == FAIL)
8941 {
8942 /* deletion will have messed up other windows */
8943 if (did_delete)
8944 {
8945#ifdef FEAT_WINDOWS
8946 wp->w_redr_status = TRUE;
8947#endif
8948 win_rest_invalid(W_NEXT(wp));
8949 }
8950 return FAIL;
8951 }
8952
8953 return OK;
8954}
8955
8956/*
8957 * delete "line_count" window lines at "row" in window "wp"
8958 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8959 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8960 * scrolling
8961 * Return OK for success, FAIL if the lines are not deleted.
8962 */
8963 int
8964win_del_lines(wp, row, line_count, invalid, mayclear)
8965 win_T *wp;
8966 int row;
8967 int line_count;
8968 int invalid;
8969 int mayclear;
8970{
8971 int retval;
8972
8973 if (invalid)
8974 wp->w_lines_valid = 0;
8975
8976 if (line_count > wp->w_height - row)
8977 line_count = wp->w_height - row;
8978
8979 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8980 if (retval != MAYBE)
8981 return retval;
8982
8983 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8984 (int)Rows, FALSE, NULL) == FAIL)
8985 return FAIL;
8986
8987#ifdef FEAT_WINDOWS
8988 /*
8989 * If there are windows or status lines below, try to put them at the
8990 * correct place. If we can't do that, they have to be redrawn.
8991 */
8992 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
8993 {
8994 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8995 line_count, (int)Rows, NULL) == FAIL)
8996 {
8997 wp->w_redr_status = TRUE;
8998 win_rest_invalid(wp->w_next);
8999 }
9000 }
9001 /*
9002 * If this is the last window and there is no status line, redraw the
9003 * command line later.
9004 */
9005 else
9006#endif
9007 redraw_cmdline = TRUE;
9008 return OK;
9009}
9010
9011/*
9012 * Common code for win_ins_lines() and win_del_lines().
9013 * Returns OK or FAIL when the work has been done.
9014 * Returns MAYBE when not finished yet.
9015 */
9016 static int
9017win_do_lines(wp, row, line_count, mayclear, del)
9018 win_T *wp;
9019 int row;
9020 int line_count;
9021 int mayclear;
9022 int del;
9023{
9024 int retval;
9025
9026 if (!redrawing() || line_count <= 0)
9027 return FAIL;
9028
9029 /* only a few lines left: redraw is faster */
9030 if (mayclear && Rows - line_count < 5
9031#ifdef FEAT_VERTSPLIT
9032 && wp->w_width == Columns
9033#endif
9034 )
9035 {
9036 screenclear(); /* will set wp->w_lines_valid to 0 */
9037 return FAIL;
9038 }
9039
9040 /*
9041 * Delete all remaining lines
9042 */
9043 if (row + line_count >= wp->w_height)
9044 {
9045 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9046 W_WINCOL(wp), (int)W_ENDCOL(wp),
9047 ' ', ' ', 0);
9048 return OK;
9049 }
9050
9051 /*
9052 * when scrolling, the message on the command line should be cleared,
9053 * otherwise it will stay there forever.
9054 */
9055 clear_cmdline = TRUE;
9056
9057 /*
9058 * If the terminal can set a scroll region, use that.
9059 * Always do this in a vertically split window. This will redraw from
9060 * ScreenLines[] when t_CV isn't defined. That's faster than using
9061 * win_line().
9062 * Don't use a scroll region when we are going to redraw the text, writing
9063 * a character in the lower right corner of the scroll region causes a
9064 * scroll-up in the DJGPP version.
9065 */
9066 if (scroll_region
9067#ifdef FEAT_VERTSPLIT
9068 || W_WIDTH(wp) != Columns
9069#endif
9070 )
9071 {
9072#ifdef FEAT_VERTSPLIT
9073 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9074#endif
9075 scroll_region_set(wp, row);
9076 if (del)
9077 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9078 wp->w_height - row, FALSE, wp);
9079 else
9080 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9081 wp->w_height - row, wp);
9082#ifdef FEAT_VERTSPLIT
9083 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9084#endif
9085 scroll_region_reset();
9086 return retval;
9087 }
9088
9089#ifdef FEAT_WINDOWS
9090 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9091 return FAIL;
9092#endif
9093
9094 return MAYBE;
9095}
9096
9097/*
9098 * window 'wp' and everything after it is messed up, mark it for redraw
9099 */
9100 static void
9101win_rest_invalid(wp)
9102 win_T *wp;
9103{
9104#ifdef FEAT_WINDOWS
9105 while (wp != NULL)
9106#else
9107 if (wp != NULL)
9108#endif
9109 {
9110 redraw_win_later(wp, NOT_VALID);
9111#ifdef FEAT_WINDOWS
9112 wp->w_redr_status = TRUE;
9113 wp = wp->w_next;
9114#endif
9115 }
9116 redraw_cmdline = TRUE;
9117}
9118
9119/*
9120 * The rest of the routines in this file perform screen manipulations. The
9121 * given operation is performed physically on the screen. The corresponding
9122 * change is also made to the internal screen image. In this way, the editor
9123 * anticipates the effect of editing changes on the appearance of the screen.
9124 * That way, when we call screenupdate a complete redraw isn't usually
9125 * necessary. Another advantage is that we can keep adding code to anticipate
9126 * screen changes, and in the meantime, everything still works.
9127 */
9128
9129/*
9130 * types for inserting or deleting lines
9131 */
9132#define USE_T_CAL 1
9133#define USE_T_CDL 2
9134#define USE_T_AL 3
9135#define USE_T_CE 4
9136#define USE_T_DL 5
9137#define USE_T_SR 6
9138#define USE_NL 7
9139#define USE_T_CD 8
9140#define USE_REDRAW 9
9141
9142/*
9143 * insert lines on the screen and update ScreenLines[]
9144 * 'end' is the line after the scrolled part. Normally it is Rows.
9145 * When scrolling region used 'off' is the offset from the top for the region.
9146 * 'row' and 'end' are relative to the start of the region.
9147 *
9148 * return FAIL for failure, OK for success.
9149 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009150 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151screen_ins_lines(off, row, line_count, end, wp)
9152 int off;
9153 int row;
9154 int line_count;
9155 int end;
9156 win_T *wp; /* NULL or window to use width from */
9157{
9158 int i;
9159 int j;
9160 unsigned temp;
9161 int cursor_row;
9162 int type;
9163 int result_empty;
9164 int can_ce = can_clear(T_CE);
9165
9166 /*
9167 * FAIL if
9168 * - there is no valid screen
9169 * - the screen has to be redrawn completely
9170 * - the line count is less than one
9171 * - the line count is more than 'ttyscroll'
9172 */
9173 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9174 return FAIL;
9175
9176 /*
9177 * There are seven ways to insert lines:
9178 * 0. When in a vertically split window and t_CV isn't set, redraw the
9179 * characters from ScreenLines[].
9180 * 1. Use T_CD (clear to end of display) if it exists and the result of
9181 * the insert is just empty lines
9182 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9183 * present or line_count > 1. It looks better if we do all the inserts
9184 * at once.
9185 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9186 * insert is just empty lines and T_CE is not present or line_count >
9187 * 1.
9188 * 4. Use T_AL (insert line) if it exists.
9189 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9190 * just empty lines.
9191 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9192 * just empty lines.
9193 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9194 * the 'da' flag is not set or we have clear line capability.
9195 * 8. redraw the characters from ScreenLines[].
9196 *
9197 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9198 * the scrollbar for the window. It does have insert line, use that if it
9199 * exists.
9200 */
9201 result_empty = (row + line_count >= end);
9202#ifdef FEAT_VERTSPLIT
9203 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9204 type = USE_REDRAW;
9205 else
9206#endif
9207 if (can_clear(T_CD) && result_empty)
9208 type = USE_T_CD;
9209 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9210 type = USE_T_CAL;
9211 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9212 type = USE_T_CDL;
9213 else if (*T_AL != NUL)
9214 type = USE_T_AL;
9215 else if (can_ce && result_empty)
9216 type = USE_T_CE;
9217 else if (*T_DL != NUL && result_empty)
9218 type = USE_T_DL;
9219 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9220 type = USE_T_SR;
9221 else
9222 return FAIL;
9223
9224 /*
9225 * For clearing the lines screen_del_lines() is used. This will also take
9226 * care of t_db if necessary.
9227 */
9228 if (type == USE_T_CD || type == USE_T_CDL ||
9229 type == USE_T_CE || type == USE_T_DL)
9230 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9231
9232 /*
9233 * If text is retained below the screen, first clear or delete as many
9234 * lines at the bottom of the window as are about to be inserted so that
9235 * the deleted lines won't later surface during a screen_del_lines.
9236 */
9237 if (*T_DB)
9238 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9239
9240#ifdef FEAT_CLIPBOARD
9241 /* Remove a modeless selection when inserting lines halfway the screen
9242 * or not the full width of the screen. */
9243 if (off + row > 0
9244# ifdef FEAT_VERTSPLIT
9245 || (wp != NULL && wp->w_width != Columns)
9246# endif
9247 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009248 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 else
9250 clip_scroll_selection(-line_count);
9251#endif
9252
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253#ifdef FEAT_GUI
9254 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9255 * scrolling is actually carried out. */
9256 gui_dont_update_cursor();
9257#endif
9258
9259 if (*T_CCS != NUL) /* cursor relative to region */
9260 cursor_row = row;
9261 else
9262 cursor_row = row + off;
9263
9264 /*
9265 * Shift LineOffset[] line_count down to reflect the inserted lines.
9266 * Clear the inserted lines in ScreenLines[].
9267 */
9268 row += off;
9269 end += off;
9270 for (i = 0; i < line_count; ++i)
9271 {
9272#ifdef FEAT_VERTSPLIT
9273 if (wp != NULL && wp->w_width != Columns)
9274 {
9275 /* need to copy part of a line */
9276 j = end - 1 - i;
9277 while ((j -= line_count) >= row)
9278 linecopy(j + line_count, j, wp);
9279 j += line_count;
9280 if (can_clear((char_u *)" "))
9281 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9282 else
9283 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9284 LineWraps[j] = FALSE;
9285 }
9286 else
9287#endif
9288 {
9289 j = end - 1 - i;
9290 temp = LineOffset[j];
9291 while ((j -= line_count) >= row)
9292 {
9293 LineOffset[j + line_count] = LineOffset[j];
9294 LineWraps[j + line_count] = LineWraps[j];
9295 }
9296 LineOffset[j + line_count] = temp;
9297 LineWraps[j + line_count] = FALSE;
9298 if (can_clear((char_u *)" "))
9299 lineclear(temp, (int)Columns);
9300 else
9301 lineinvalid(temp, (int)Columns);
9302 }
9303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304
9305 screen_stop_highlight();
9306 windgoto(cursor_row, 0);
9307
9308#ifdef FEAT_VERTSPLIT
9309 /* redraw the characters */
9310 if (type == USE_REDRAW)
9311 redraw_block(row, end, wp);
9312 else
9313#endif
9314 if (type == USE_T_CAL)
9315 {
9316 term_append_lines(line_count);
9317 screen_start(); /* don't know where cursor is now */
9318 }
9319 else
9320 {
9321 for (i = 0; i < line_count; i++)
9322 {
9323 if (type == USE_T_AL)
9324 {
9325 if (i && cursor_row != 0)
9326 windgoto(cursor_row, 0);
9327 out_str(T_AL);
9328 }
9329 else /* type == USE_T_SR */
9330 out_str(T_SR);
9331 screen_start(); /* don't know where cursor is now */
9332 }
9333 }
9334
9335 /*
9336 * With scroll-reverse and 'da' flag set we need to clear the lines that
9337 * have been scrolled down into the region.
9338 */
9339 if (type == USE_T_SR && *T_DA)
9340 {
9341 for (i = 0; i < line_count; ++i)
9342 {
9343 windgoto(off + i, 0);
9344 out_str(T_CE);
9345 screen_start(); /* don't know where cursor is now */
9346 }
9347 }
9348
9349#ifdef FEAT_GUI
9350 gui_can_update_cursor();
9351 if (gui.in_use)
9352 out_flush(); /* always flush after a scroll */
9353#endif
9354 return OK;
9355}
9356
9357/*
9358 * delete lines on the screen and update ScreenLines[]
9359 * 'end' is the line after the scrolled part. Normally it is Rows.
9360 * When scrolling region used 'off' is the offset from the top for the region.
9361 * 'row' and 'end' are relative to the start of the region.
9362 *
9363 * Return OK for success, FAIL if the lines are not deleted.
9364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 int
9366screen_del_lines(off, row, line_count, end, force, wp)
9367 int off;
9368 int row;
9369 int line_count;
9370 int end;
9371 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009372 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374 int j;
9375 int i;
9376 unsigned temp;
9377 int cursor_row;
9378 int cursor_end;
9379 int result_empty; /* result is empty until end of region */
9380 int can_delete; /* deleting line codes can be used */
9381 int type;
9382
9383 /*
9384 * FAIL if
9385 * - there is no valid screen
9386 * - the screen has to be redrawn completely
9387 * - the line count is less than one
9388 * - the line count is more than 'ttyscroll'
9389 */
9390 if (!screen_valid(TRUE) || line_count <= 0 ||
9391 (!force && line_count > p_ttyscroll))
9392 return FAIL;
9393
9394 /*
9395 * Check if the rest of the current region will become empty.
9396 */
9397 result_empty = row + line_count >= end;
9398
9399 /*
9400 * We can delete lines only when 'db' flag not set or when 'ce' option
9401 * available.
9402 */
9403 can_delete = (*T_DB == NUL || can_clear(T_CE));
9404
9405 /*
9406 * There are six ways to delete lines:
9407 * 0. When in a vertically split window and t_CV isn't set, redraw the
9408 * characters from ScreenLines[].
9409 * 1. Use T_CD if it exists and the result is empty.
9410 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9411 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9412 * none of the other ways work.
9413 * 4. Use T_CE (erase line) if the result is empty.
9414 * 5. Use T_DL (delete line) if it exists.
9415 * 6. redraw the characters from ScreenLines[].
9416 */
9417#ifdef FEAT_VERTSPLIT
9418 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9419 type = USE_REDRAW;
9420 else
9421#endif
9422 if (can_clear(T_CD) && result_empty)
9423 type = USE_T_CD;
9424#if defined(__BEOS__) && defined(BEOS_DR8)
9425 /*
9426 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9427 * its internal termcap... this works okay for tests which test *T_DB !=
9428 * NUL. It has the disadvantage that the user cannot use any :set t_*
9429 * command to get T_DB (back) to empty_option, only :set term=... will do
9430 * the trick...
9431 * Anyway, this hack will hopefully go away with the next OS release.
9432 * (Olaf Seibert)
9433 */
9434 else if (row == 0 && T_DB == empty_option
9435 && (line_count == 1 || *T_CDL == NUL))
9436#else
9437 else if (row == 0 && (
9438#ifndef AMIGA
9439 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9440 * up, so use delete-line command */
9441 line_count == 1 ||
9442#endif
9443 *T_CDL == NUL))
9444#endif
9445 type = USE_NL;
9446 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9447 type = USE_T_CDL;
9448 else if (can_clear(T_CE) && result_empty
9449#ifdef FEAT_VERTSPLIT
9450 && (wp == NULL || wp->w_width == Columns)
9451#endif
9452 )
9453 type = USE_T_CE;
9454 else if (*T_DL != NUL && can_delete)
9455 type = USE_T_DL;
9456 else if (*T_CDL != NUL && can_delete)
9457 type = USE_T_CDL;
9458 else
9459 return FAIL;
9460
9461#ifdef FEAT_CLIPBOARD
9462 /* Remove a modeless selection when deleting lines halfway the screen or
9463 * not the full width of the screen. */
9464 if (off + row > 0
9465# ifdef FEAT_VERTSPLIT
9466 || (wp != NULL && wp->w_width != Columns)
9467# endif
9468 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009469 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 else
9471 clip_scroll_selection(line_count);
9472#endif
9473
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474#ifdef FEAT_GUI
9475 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9476 * scrolling is actually carried out. */
9477 gui_dont_update_cursor();
9478#endif
9479
9480 if (*T_CCS != NUL) /* cursor relative to region */
9481 {
9482 cursor_row = row;
9483 cursor_end = end;
9484 }
9485 else
9486 {
9487 cursor_row = row + off;
9488 cursor_end = end + off;
9489 }
9490
9491 /*
9492 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9493 * Clear the inserted lines in ScreenLines[].
9494 */
9495 row += off;
9496 end += off;
9497 for (i = 0; i < line_count; ++i)
9498 {
9499#ifdef FEAT_VERTSPLIT
9500 if (wp != NULL && wp->w_width != Columns)
9501 {
9502 /* need to copy part of a line */
9503 j = row + i;
9504 while ((j += line_count) <= end - 1)
9505 linecopy(j - line_count, j, wp);
9506 j -= line_count;
9507 if (can_clear((char_u *)" "))
9508 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9509 else
9510 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9511 LineWraps[j] = FALSE;
9512 }
9513 else
9514#endif
9515 {
9516 /* whole width, moving the line pointers is faster */
9517 j = row + i;
9518 temp = LineOffset[j];
9519 while ((j += line_count) <= end - 1)
9520 {
9521 LineOffset[j - line_count] = LineOffset[j];
9522 LineWraps[j - line_count] = LineWraps[j];
9523 }
9524 LineOffset[j - line_count] = temp;
9525 LineWraps[j - line_count] = FALSE;
9526 if (can_clear((char_u *)" "))
9527 lineclear(temp, (int)Columns);
9528 else
9529 lineinvalid(temp, (int)Columns);
9530 }
9531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
9533 screen_stop_highlight();
9534
9535#ifdef FEAT_VERTSPLIT
9536 /* redraw the characters */
9537 if (type == USE_REDRAW)
9538 redraw_block(row, end, wp);
9539 else
9540#endif
9541 if (type == USE_T_CD) /* delete the lines */
9542 {
9543 windgoto(cursor_row, 0);
9544 out_str(T_CD);
9545 screen_start(); /* don't know where cursor is now */
9546 }
9547 else if (type == USE_T_CDL)
9548 {
9549 windgoto(cursor_row, 0);
9550 term_delete_lines(line_count);
9551 screen_start(); /* don't know where cursor is now */
9552 }
9553 /*
9554 * Deleting lines at top of the screen or scroll region: Just scroll
9555 * the whole screen (scroll region) up by outputting newlines on the
9556 * last line.
9557 */
9558 else if (type == USE_NL)
9559 {
9560 windgoto(cursor_end - 1, 0);
9561 for (i = line_count; --i >= 0; )
9562 out_char('\n'); /* cursor will remain on same line */
9563 }
9564 else
9565 {
9566 for (i = line_count; --i >= 0; )
9567 {
9568 if (type == USE_T_DL)
9569 {
9570 windgoto(cursor_row, 0);
9571 out_str(T_DL); /* delete a line */
9572 }
9573 else /* type == USE_T_CE */
9574 {
9575 windgoto(cursor_row + i, 0);
9576 out_str(T_CE); /* erase a line */
9577 }
9578 screen_start(); /* don't know where cursor is now */
9579 }
9580 }
9581
9582 /*
9583 * If the 'db' flag is set, we need to clear the lines that have been
9584 * scrolled up at the bottom of the region.
9585 */
9586 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9587 {
9588 for (i = line_count; i > 0; --i)
9589 {
9590 windgoto(cursor_end - i, 0);
9591 out_str(T_CE); /* erase a line */
9592 screen_start(); /* don't know where cursor is now */
9593 }
9594 }
9595
9596#ifdef FEAT_GUI
9597 gui_can_update_cursor();
9598 if (gui.in_use)
9599 out_flush(); /* always flush after a scroll */
9600#endif
9601
9602 return OK;
9603}
9604
9605/*
9606 * show the current mode and ruler
9607 *
9608 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9609 * If clear_cmdline is FALSE there may be a message there that needs to be
9610 * cleared only if a mode is shown.
9611 * Return the length of the message (0 if no message).
9612 */
9613 int
9614showmode()
9615{
9616 int need_clear;
9617 int length = 0;
9618 int do_mode;
9619 int attr;
9620 int nwr_save;
9621#ifdef FEAT_INS_EXPAND
9622 int sub_attr;
9623#endif
9624
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009625 do_mode = ((p_smd && msg_silent == 0)
9626 && ((State & INSERT)
9627 || restart_edit
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009628 || VIsual_active));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 if (do_mode || Recording)
9630 {
9631 /*
9632 * Don't show mode right now, when not redrawing or inside a mapping.
9633 * Call char_avail() only when we are going to show something, because
9634 * it takes a bit of time.
9635 */
9636 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9637 {
9638 redraw_cmdline = TRUE; /* show mode later */
9639 return 0;
9640 }
9641
9642 nwr_save = need_wait_return;
9643
9644 /* wait a bit before overwriting an important message */
9645 check_for_delay(FALSE);
9646
9647 /* if the cmdline is more than one line high, erase top lines */
9648 need_clear = clear_cmdline;
9649 if (clear_cmdline && cmdline_row < Rows - 1)
9650 msg_clr_cmdline(); /* will reset clear_cmdline */
9651
9652 /* Position on the last line in the window, column 0 */
9653 msg_pos_mode();
9654 cursor_off();
9655 attr = hl_attr(HLF_CM); /* Highlight mode */
9656 if (do_mode)
9657 {
9658 MSG_PUTS_ATTR("--", attr);
9659#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009660 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009661# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009662 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009663# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009664 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009665# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009666 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009667# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 MSG_PUTS_ATTR(" IM", attr);
9669# else
9670 MSG_PUTS_ATTR(" XIM", attr);
9671# endif
9672#endif
9673#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9674 if (gui.in_use)
9675 {
9676 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009677 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 }
9679#endif
9680#ifdef FEAT_INS_EXPAND
9681 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9682 {
9683 /* These messages can get long, avoid a wrap in a narrow
9684 * window. Prefer showing edit_submode_extra. */
9685 length = (Rows - msg_row) * Columns - 3;
9686 if (edit_submode_extra != NULL)
9687 length -= vim_strsize(edit_submode_extra);
9688 if (length > 0)
9689 {
9690 if (edit_submode_pre != NULL)
9691 length -= vim_strsize(edit_submode_pre);
9692 if (length - vim_strsize(edit_submode) > 0)
9693 {
9694 if (edit_submode_pre != NULL)
9695 msg_puts_attr(edit_submode_pre, attr);
9696 msg_puts_attr(edit_submode, attr);
9697 }
9698 if (edit_submode_extra != NULL)
9699 {
9700 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9701 if ((int)edit_submode_highl < (int)HLF_COUNT)
9702 sub_attr = hl_attr(edit_submode_highl);
9703 else
9704 sub_attr = attr;
9705 msg_puts_attr(edit_submode_extra, sub_attr);
9706 }
9707 }
9708 length = 0;
9709 }
9710 else
9711#endif
9712 {
9713#ifdef FEAT_VREPLACE
9714 if (State & VREPLACE_FLAG)
9715 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9716 else
9717#endif
9718 if (State & REPLACE_FLAG)
9719 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9720 else if (State & INSERT)
9721 {
9722#ifdef FEAT_RIGHTLEFT
9723 if (p_ri)
9724 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9725#endif
9726 MSG_PUTS_ATTR(_(" INSERT"), attr);
9727 }
9728 else if (restart_edit == 'I')
9729 MSG_PUTS_ATTR(_(" (insert)"), attr);
9730 else if (restart_edit == 'R')
9731 MSG_PUTS_ATTR(_(" (replace)"), attr);
9732 else if (restart_edit == 'V')
9733 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9734#ifdef FEAT_RIGHTLEFT
9735 if (p_hkmap)
9736 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9737# ifdef FEAT_FKMAP
9738 if (p_fkmap)
9739 MSG_PUTS_ATTR(farsi_text_5, attr);
9740# endif
9741#endif
9742#ifdef FEAT_KEYMAP
9743 if (State & LANGMAP)
9744 {
9745# ifdef FEAT_ARABIC
9746 if (curwin->w_p_arab)
9747 MSG_PUTS_ATTR(_(" Arabic"), attr);
9748 else
9749# endif
9750 MSG_PUTS_ATTR(_(" (lang)"), attr);
9751 }
9752#endif
9753 if ((State & INSERT) && p_paste)
9754 MSG_PUTS_ATTR(_(" (paste)"), attr);
9755
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 if (VIsual_active)
9757 {
9758 char *p;
9759
9760 /* Don't concatenate separate words to avoid translation
9761 * problems. */
9762 switch ((VIsual_select ? 4 : 0)
9763 + (VIsual_mode == Ctrl_V) * 2
9764 + (VIsual_mode == 'V'))
9765 {
9766 case 0: p = N_(" VISUAL"); break;
9767 case 1: p = N_(" VISUAL LINE"); break;
9768 case 2: p = N_(" VISUAL BLOCK"); break;
9769 case 4: p = N_(" SELECT"); break;
9770 case 5: p = N_(" SELECT LINE"); break;
9771 default: p = N_(" SELECT BLOCK"); break;
9772 }
9773 MSG_PUTS_ATTR(_(p), attr);
9774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 MSG_PUTS_ATTR(" --", attr);
9776 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 need_clear = TRUE;
9779 }
9780 if (Recording
9781#ifdef FEAT_INS_EXPAND
9782 && edit_submode == NULL /* otherwise it gets too long */
9783#endif
9784 )
9785 {
9786 MSG_PUTS_ATTR(_("recording"), attr);
9787 need_clear = TRUE;
9788 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009789
9790 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 if (need_clear || clear_cmdline)
9792 msg_clr_eos();
9793 msg_didout = FALSE; /* overwrite this message */
9794 length = msg_col;
9795 msg_col = 0;
9796 need_wait_return = nwr_save; /* never ask for hit-return for this */
9797 }
9798 else if (clear_cmdline && msg_silent == 0)
9799 /* Clear the whole command line. Will reset "clear_cmdline". */
9800 msg_clr_cmdline();
9801
9802#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 /* In Visual mode the size of the selected area must be redrawn. */
9804 if (VIsual_active)
9805 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 /* If the last window has no status line, the ruler is after the mode
9808 * message and must be redrawn */
9809 if (redrawing()
9810# ifdef FEAT_WINDOWS
9811 && lastwin->w_status_height == 0
9812# endif
9813 )
9814 win_redr_ruler(lastwin, TRUE);
9815#endif
9816 redraw_cmdline = FALSE;
9817 clear_cmdline = FALSE;
9818
9819 return length;
9820}
9821
9822/*
9823 * Position for a mode message.
9824 */
9825 static void
9826msg_pos_mode()
9827{
9828 msg_col = 0;
9829 msg_row = Rows - 1;
9830}
9831
9832/*
9833 * Delete mode message. Used when ESC is typed which is expected to end
9834 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009835 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 */
9837 void
9838unshowmode(force)
9839 int force;
9840{
9841 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009842 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 */
9844 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9845 redraw_cmdline = TRUE; /* delete mode later */
9846 else
9847 {
9848 msg_pos_mode();
9849 if (Recording)
9850 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9851 msg_clr_eos();
9852 }
9853}
9854
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009855#if defined(FEAT_WINDOWS)
9856/*
9857 * Draw the tab pages line at the top of the Vim window.
9858 */
9859 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009860draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009861{
9862 int tabcount = 0;
9863 tabpage_T *tp;
9864 int tabwidth;
9865 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009866 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009867 int attr;
9868 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009869 win_T *cwp;
9870 int wincount;
9871 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009872 int c;
9873 int len;
9874 int attr_sel = hl_attr(HLF_TPS);
9875 int attr_nosel = hl_attr(HLF_TP);
9876 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009877 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009878 int room;
9879 int use_sep_chars = (t_colors < 8
9880#ifdef FEAT_GUI
9881 && !gui.in_use
9882#endif
9883 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009884
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009885 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009886
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009887#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009888 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009889 if (gui_use_tabline())
9890 {
9891 gui_update_tabline();
9892 return;
9893 }
9894#endif
9895
9896 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009897 return;
9898
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009899#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009900
9901 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9902 for (scol = 0; scol < Columns; ++scol)
9903 TabPageIdxs[scol] = 0;
9904
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009905 /* Use the 'tabline' option if it's set. */
9906 if (*p_tal != NUL)
9907 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009908 int save_called_emsg = called_emsg;
9909
9910 /* Check for an error. If there is one we would loop in redrawing the
9911 * screen. Avoid that by making 'tabline' empty. */
9912 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009913 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009914 if (called_emsg)
9915 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009916 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009917 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009918 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009919 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009920#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009921 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009922 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9923 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009924
Bram Moolenaar238a5642006-02-21 22:12:05 +00009925 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9926 if (tabwidth < 6)
9927 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009928
Bram Moolenaar238a5642006-02-21 22:12:05 +00009929 attr = attr_nosel;
9930 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009931 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009932 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9933 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009934 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009935 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009936
Bram Moolenaar238a5642006-02-21 22:12:05 +00009937 if (tp->tp_topframe == topframe)
9938 attr = attr_sel;
9939 if (use_sep_chars && col > 0)
9940 screen_putchar('|', 0, col++, attr);
9941
9942 if (tp->tp_topframe != topframe)
9943 attr = attr_nosel;
9944
9945 screen_putchar(' ', 0, col++, attr);
9946
9947 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009948 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 cwp = curwin;
9950 wp = firstwin;
9951 }
9952 else
9953 {
9954 cwp = tp->tp_curwin;
9955 wp = tp->tp_firstwin;
9956 }
9957
9958 modified = FALSE;
9959 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9960 if (bufIsChanged(wp->w_buffer))
9961 modified = TRUE;
9962 if (modified || wincount > 1)
9963 {
9964 if (wincount > 1)
9965 {
9966 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009967 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009968 if (col + len >= Columns - 3)
9969 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009970 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009971#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009972 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009973#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009974 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009975#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009976 );
9977 col += len;
9978 }
9979 if (modified)
9980 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
9981 screen_putchar(' ', 0, col++, attr);
9982 }
9983
9984 room = scol - col + tabwidth - 1;
9985 if (room > 0)
9986 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009987 /* Get buffer name in NameBuff[] */
9988 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009989 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009990 len = vim_strsize(NameBuff);
9991 p = NameBuff;
9992#ifdef FEAT_MBYTE
9993 if (has_mbyte)
9994 while (len > room)
9995 {
9996 len -= ptr2cells(p);
9997 mb_ptr_adv(p);
9998 }
9999 else
10000#endif
10001 if (len > room)
10002 {
10003 p += len - room;
10004 len = room;
10005 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010006 if (len > Columns - col - 1)
10007 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010008
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010009 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010010 col += len;
10011 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010012 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010013
10014 /* Store the tab page number in TabPageIdxs[], so that
10015 * jump_to_mouse() knows where each one is. */
10016 ++tabcount;
10017 while (scol < col)
10018 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010019 }
10020
Bram Moolenaar238a5642006-02-21 22:12:05 +000010021 if (use_sep_chars)
10022 c = '_';
10023 else
10024 c = ' ';
10025 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010026
10027 /* Put an "X" for closing the current tab if there are several. */
10028 if (first_tabpage->tp_next != NULL)
10029 {
10030 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10031 TabPageIdxs[Columns - 1] = -999;
10032 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010033 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010034
10035 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10036 * set. */
10037 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010038}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010039
10040/*
10041 * Get buffer name for "buf" into NameBuff[].
10042 * Takes care of special buffer names and translates special characters.
10043 */
10044 void
10045get_trans_bufname(buf)
10046 buf_T *buf;
10047{
10048 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010049 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010050 else
10051 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10052 trans_characters(NameBuff, MAXPATHL);
10053}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010054#endif
10055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10057/*
10058 * Get the character to use in a status line. Get its attributes in "*attr".
10059 */
10060 static int
10061fillchar_status(attr, is_curwin)
10062 int *attr;
10063 int is_curwin;
10064{
10065 int fill;
10066 if (is_curwin)
10067 {
10068 *attr = hl_attr(HLF_S);
10069 fill = fill_stl;
10070 }
10071 else
10072 {
10073 *attr = hl_attr(HLF_SNC);
10074 fill = fill_stlnc;
10075 }
10076 /* Use fill when there is highlighting, and highlighting of current
10077 * window differs, or the fillchars differ, or this is not the
10078 * current window */
10079 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10080 || !is_curwin || firstwin == lastwin)
10081 || (fill_stl != fill_stlnc)))
10082 return fill;
10083 if (is_curwin)
10084 return '^';
10085 return '=';
10086}
10087#endif
10088
10089#ifdef FEAT_VERTSPLIT
10090/*
10091 * Get the character to use in a separator between vertically split windows.
10092 * Get its attributes in "*attr".
10093 */
10094 static int
10095fillchar_vsep(attr)
10096 int *attr;
10097{
10098 *attr = hl_attr(HLF_C);
10099 if (*attr == 0 && fill_vert == ' ')
10100 return '|';
10101 else
10102 return fill_vert;
10103}
10104#endif
10105
10106/*
10107 * Return TRUE if redrawing should currently be done.
10108 */
10109 int
10110redrawing()
10111{
10112 return (!RedrawingDisabled
10113 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10114}
10115
10116/*
10117 * Return TRUE if printing messages should currently be done.
10118 */
10119 int
10120messaging()
10121{
10122 return (!(p_lz && char_avail() && !KeyTyped));
10123}
10124
10125/*
10126 * Show current status info in ruler and various other places
10127 * If always is FALSE, only show ruler if position has changed.
10128 */
10129 void
10130showruler(always)
10131 int always;
10132{
10133 if (!always && !redrawing())
10134 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010135#ifdef FEAT_INS_EXPAND
10136 if (pum_visible())
10137 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010138# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010139 /* Don't redraw right now, do it later. */
10140 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010141# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010142 return;
10143 }
10144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010146 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010147 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010148 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
10151#endif
10152#ifdef FEAT_CMDL_INFO
10153 win_redr_ruler(curwin, always);
10154#endif
10155
10156#ifdef FEAT_TITLE
10157 if (need_maketitle
10158# ifdef FEAT_STL_OPT
10159 || (p_icon && (stl_syntax & STL_IN_ICON))
10160 || (p_title && (stl_syntax & STL_IN_TITLE))
10161# endif
10162 )
10163 maketitle();
10164#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010165#ifdef FEAT_WINDOWS
10166 /* Redraw the tab pages line if needed. */
10167 if (redraw_tabline)
10168 draw_tabline();
10169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170}
10171
10172#ifdef FEAT_CMDL_INFO
10173 static void
10174win_redr_ruler(wp, always)
10175 win_T *wp;
10176 int always;
10177{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010178#define RULER_BUF_LEN 70
10179 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 int row;
10181 int fillchar;
10182 int attr;
10183 int empty_line = FALSE;
10184 colnr_T virtcol;
10185 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010186 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 int o;
10188#ifdef FEAT_VERTSPLIT
10189 int this_ru_col;
10190 int off = 0;
10191 int width = Columns;
10192# define WITH_OFF(x) x
10193# define WITH_WIDTH(x) x
10194#else
10195# define WITH_OFF(x) 0
10196# define WITH_WIDTH(x) Columns
10197# define this_ru_col ru_col
10198#endif
10199
10200 /* If 'ruler' off or redrawing disabled, don't do anything */
10201 if (!p_ru)
10202 return;
10203
10204 /*
10205 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10206 * after deleting lines, before cursor.lnum is corrected.
10207 */
10208 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10209 return;
10210
10211#ifdef FEAT_INS_EXPAND
10212 /* Don't draw the ruler while doing insert-completion, it might overwrite
10213 * the (long) mode message. */
10214# ifdef FEAT_WINDOWS
10215 if (wp == lastwin && lastwin->w_status_height == 0)
10216# endif
10217 if (edit_submode != NULL)
10218 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010219 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10220 if (pum_visible())
10221 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222#endif
10223
10224#ifdef FEAT_STL_OPT
10225 if (*p_ruf)
10226 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010227 int save_called_emsg = called_emsg;
10228
10229 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010231 if (called_emsg)
10232 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010233 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010234 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 return;
10236 }
10237#endif
10238
10239 /*
10240 * Check if not in Insert mode and the line is empty (will show "0-1").
10241 */
10242 if (!(State & INSERT)
10243 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10244 empty_line = TRUE;
10245
10246 /*
10247 * Only draw the ruler when something changed.
10248 */
10249 validate_virtcol_win(wp);
10250 if ( redraw_cmdline
10251 || always
10252 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10253 || wp->w_cursor.col != wp->w_ru_cursor.col
10254 || wp->w_virtcol != wp->w_ru_virtcol
10255#ifdef FEAT_VIRTUALEDIT
10256 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10257#endif
10258 || wp->w_topline != wp->w_ru_topline
10259 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10260#ifdef FEAT_DIFF
10261 || wp->w_topfill != wp->w_ru_topfill
10262#endif
10263 || empty_line != wp->w_ru_empty)
10264 {
10265 cursor_off();
10266#ifdef FEAT_WINDOWS
10267 if (wp->w_status_height)
10268 {
10269 row = W_WINROW(wp) + wp->w_height;
10270 fillchar = fillchar_status(&attr, wp == curwin);
10271# ifdef FEAT_VERTSPLIT
10272 off = W_WINCOL(wp);
10273 width = W_WIDTH(wp);
10274# endif
10275 }
10276 else
10277#endif
10278 {
10279 row = Rows - 1;
10280 fillchar = ' ';
10281 attr = 0;
10282#ifdef FEAT_VERTSPLIT
10283 width = Columns;
10284 off = 0;
10285#endif
10286 }
10287
10288 /* In list mode virtcol needs to be recomputed */
10289 virtcol = wp->w_virtcol;
10290 if (wp->w_p_list && lcs_tab1 == NUL)
10291 {
10292 wp->w_p_list = FALSE;
10293 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10294 wp->w_p_list = TRUE;
10295 }
10296
10297 /*
10298 * Some sprintfs return the length, some return a pointer.
10299 * To avoid portability problems we use strlen() here.
10300 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010301 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10303 ? 0L
10304 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010305 len = STRLEN(buffer);
10306 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10308 (int)virtcol + 1);
10309
10310 /*
10311 * Add a "50%" if there is room for it.
10312 * On the last line, don't print in the last column (scrolls the
10313 * screen up on some terminals).
10314 */
10315 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010316 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 o = i + vim_strsize(buffer + i + 1);
10318#ifdef FEAT_WINDOWS
10319 if (wp->w_status_height == 0) /* can't use last char of screen */
10320#endif
10321 ++o;
10322#ifdef FEAT_VERTSPLIT
10323 this_ru_col = ru_col - (Columns - width);
10324 if (this_ru_col < 0)
10325 this_ru_col = 0;
10326#endif
10327 /* Never use more than half the window/screen width, leave the other
10328 * half for the filename. */
10329 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10330 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10331 if (this_ru_col + o < WITH_WIDTH(width))
10332 {
10333 while (this_ru_col + o < WITH_WIDTH(width))
10334 {
10335#ifdef FEAT_MBYTE
10336 if (has_mbyte)
10337 i += (*mb_char2bytes)(fillchar, buffer + i);
10338 else
10339#endif
10340 buffer[i++] = fillchar;
10341 ++o;
10342 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010343 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 }
10345 /* Truncate at window boundary. */
10346#ifdef FEAT_MBYTE
10347 if (has_mbyte)
10348 {
10349 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010350 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 {
10352 o += (*mb_ptr2cells)(buffer + i);
10353 if (this_ru_col + o > WITH_WIDTH(width))
10354 {
10355 buffer[i] = NUL;
10356 break;
10357 }
10358 }
10359 }
10360 else
10361#endif
10362 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10363 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10364
10365 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10366 i = redraw_cmdline;
10367 screen_fill(row, row + 1,
10368 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10369 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10370 fillchar, fillchar, attr);
10371 /* don't redraw the cmdline because of showing the ruler */
10372 redraw_cmdline = i;
10373 wp->w_ru_cursor = wp->w_cursor;
10374 wp->w_ru_virtcol = wp->w_virtcol;
10375 wp->w_ru_empty = empty_line;
10376 wp->w_ru_topline = wp->w_topline;
10377 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10378#ifdef FEAT_DIFF
10379 wp->w_ru_topfill = wp->w_topfill;
10380#endif
10381 }
10382}
10383#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010384
10385#if defined(FEAT_LINEBREAK) || defined(PROTO)
10386/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010387 * Return the width of the 'number' and 'relativenumber' column.
10388 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010389 * Otherwise it depends on 'numberwidth' and the line count.
10390 */
10391 int
10392number_width(wp)
10393 win_T *wp;
10394{
10395 int n;
10396 linenr_T lnum;
10397
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010398 if (wp->w_p_rnu && !wp->w_p_nu)
10399 /* cursor line shows "0" */
10400 lnum = wp->w_height;
10401 else
10402 /* cursor line shows absolute line number */
10403 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010404
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010405 if (lnum == wp->w_nrwidth_line_count)
10406 return wp->w_nrwidth_width;
10407 wp->w_nrwidth_line_count = lnum;
10408
10409 n = 0;
10410 do
10411 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010412 lnum /= 10;
10413 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010414 } while (lnum > 0);
10415
10416 /* 'numberwidth' gives the minimal width plus one */
10417 if (n < wp->w_p_nuw - 1)
10418 n = wp->w_p_nuw - 1;
10419
10420 wp->w_nrwidth_width = n;
10421 return n;
10422}
10423#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010424
10425/*
10426 * Return the current cursor column. This is the actual position on the
10427 * screen. First column is 0.
10428 */
10429 int
10430screen_screencol()
10431{
10432 return screen_cur_col;
10433}
10434
10435/*
10436 * Return the current cursor row. This is the actual position on the screen.
10437 * First row is 0.
10438 */
10439 int
10440screen_screenrow()
10441{
10442 return screen_cur_row;
10443}