blob: e5636adbfb978000eef397a41aa9416ef4752cfb [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
Bram Moolenaar9d6650f2010-06-06 23:04:47 +0200449#if defined(FEAT_RUBY) || defined(FEAT_PERL) || defined(FEAT_VISUAL) || \
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200450 (defined(FEAT_CLIPBOARD) && defined(FEAT_X11)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451/*
452 * update all windows that are editing the current buffer
453 */
454 void
455update_curbuf(type)
456 int type;
457{
458 redraw_curbuf_later(type);
459 update_screen(type);
460}
Bram Moolenaarfd3e5dc2010-05-30 19:00:15 +0200461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463/*
464 * update_screen()
465 *
466 * Based on the current value of curwin->w_topline, transfer a screenfull
467 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
468 */
469 void
470update_screen(type)
471 int type;
472{
473 win_T *wp;
474 static int did_intro = FALSE;
475#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
476 int did_one;
477#endif
478
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000479 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 if (!screen_valid(TRUE))
481 return;
482
483 if (must_redraw)
484 {
485 if (type < must_redraw) /* use maximal type */
486 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000487
488 /* must_redraw is reset here, so that when we run into some weird
489 * reason to redraw while busy redrawing (e.g., asynchronous
490 * scrolling), or update_topline() in win_update() will cause a
491 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 must_redraw = 0;
493 }
494
495 /* Need to update w_lines[]. */
496 if (curwin->w_lines_valid == 0 && type < NOT_VALID)
497 type = NOT_VALID;
498
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000499 /* Postpone the redrawing when it's not needed and when being called
500 * recursively. */
501 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {
503 redraw_later(type); /* remember type for next time */
504 must_redraw = type;
505 if (type > INVERTED_ALL)
506 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
507 return;
508 }
509
510 updating_screen = TRUE;
511#ifdef FEAT_SYN_HL
512 ++display_tick; /* let syntax code know we're in a next round of
513 * display updating */
514#endif
515
516 /*
517 * if the screen was scrolled up when displaying a message, scroll it down
518 */
519 if (msg_scrolled)
520 {
521 clear_cmdline = TRUE;
522 if (msg_scrolled > Rows - 5) /* clearing is faster */
523 type = CLEAR;
524 else if (type != CLEAR)
525 {
526 check_for_delay(FALSE);
527 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, NULL) == FAIL)
528 type = CLEAR;
529 FOR_ALL_WINDOWS(wp)
530 {
531 if (W_WINROW(wp) < msg_scrolled)
532 {
533 if (W_WINROW(wp) + wp->w_height > msg_scrolled
534 && wp->w_redr_type < REDRAW_TOP
535 && wp->w_lines_valid > 0
536 && wp->w_topline == wp->w_lines[0].wl_lnum)
537 {
538 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
539 wp->w_redr_type = REDRAW_TOP;
540 }
541 else
542 {
543 wp->w_redr_type = NOT_VALID;
544#ifdef FEAT_WINDOWS
545 if (W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp)
546 <= msg_scrolled)
547 wp->w_redr_status = TRUE;
548#endif
549 }
550 }
551 }
552 redraw_cmdline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000554 redraw_tabline = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
557 msg_scrolled = 0;
558 need_wait_return = FALSE;
559 }
560
561 /* reset cmdline_row now (may have been changed temporarily) */
562 compute_cmdrow();
563
564 /* Check for changed highlighting */
565 if (need_highlight_changed)
566 highlight_changed();
567
568 if (type == CLEAR) /* first clear screen */
569 {
570 screenclear(); /* will reset clear_cmdline */
571 type = NOT_VALID;
572 }
573
574 if (clear_cmdline) /* going to clear cmdline (done below) */
575 check_for_delay(FALSE);
576
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000577#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200578 /* Force redraw when width of 'number' or 'relativenumber' column
579 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000580 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200581 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
582 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000583 curwin->w_redr_type = NOT_VALID;
584#endif
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * Only start redrawing if there is really something to do.
588 */
589 if (type == INVERTED)
590 update_curswant();
591 if (curwin->w_redr_type < type
592 && !((type == VALID
593 && curwin->w_lines[0].wl_valid
594#ifdef FEAT_DIFF
595 && curwin->w_topfill == curwin->w_old_topfill
596 && curwin->w_botfill == curwin->w_old_botfill
597#endif
598 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
599#ifdef FEAT_VISUAL
600 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000601 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
603 && curwin->w_old_visual_mode == VIsual_mode
604 && (curwin->w_valid & VALID_VIRTCOL)
605 && curwin->w_old_curswant == curwin->w_curswant)
606#endif
607 ))
608 curwin->w_redr_type = type;
609
Bram Moolenaar5a305422006-04-28 22:38:25 +0000610#ifdef FEAT_WINDOWS
611 /* Redraw the tab pages line if needed. */
612 if (redraw_tabline || type >= NOT_VALID)
613 draw_tabline();
614#endif
615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#ifdef FEAT_SYN_HL
617 /*
618 * Correct stored syntax highlighting info for changes in each displayed
619 * buffer. Each buffer must only be done once.
620 */
621 FOR_ALL_WINDOWS(wp)
622 {
623 if (wp->w_buffer->b_mod_set)
624 {
625# ifdef FEAT_WINDOWS
626 win_T *wwp;
627
628 /* Check if we already did this buffer. */
629 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
630 if (wwp->w_buffer == wp->w_buffer)
631 break;
632# endif
633 if (
634# ifdef FEAT_WINDOWS
635 wwp == wp &&
636# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200637 syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 syn_stack_apply_changes(wp->w_buffer);
639 }
640 }
641#endif
642
643 /*
644 * Go from top to bottom through the windows, redrawing the ones that need
645 * it.
646 */
647#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
648 did_one = FALSE;
649#endif
650#ifdef FEAT_SEARCH_EXTRA
651 search_hl.rm.regprog = NULL;
652#endif
653 FOR_ALL_WINDOWS(wp)
654 {
655 if (wp->w_redr_type != 0)
656 {
657 cursor_off();
658#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
659 if (!did_one)
660 {
661 did_one = TRUE;
662# ifdef FEAT_SEARCH_EXTRA
663 start_search_hl();
664# endif
665# ifdef FEAT_CLIPBOARD
666 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200667 if (clip_star.available && clip_isautosel_star())
668 clip_update_selection(&clip_star);
669 if (clip_plus.available && clip_isautosel_plus())
670 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671# endif
672#ifdef FEAT_GUI
673 /* Remove the cursor before starting to do anything, because
674 * scrolling may make it difficult to redraw the text under
675 * it. */
676 if (gui.in_use)
677 gui_undraw_cursor();
678#endif
679 }
680#endif
681 win_update(wp);
682 }
683
684#ifdef FEAT_WINDOWS
685 /* redraw status line after the window to minimize cursor movement */
686 if (wp->w_redr_status)
687 {
688 cursor_off();
689 win_redr_status(wp);
690 }
691#endif
692 }
693#if defined(FEAT_SEARCH_EXTRA)
694 end_search_hl();
695#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100696#ifdef FEAT_INS_EXPAND
697 /* May need to redraw the popup menu. */
698 if (pum_visible())
699 pum_redraw();
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702#ifdef FEAT_WINDOWS
703 /* Reset b_mod_set flags. Going through all windows is probably faster
704 * than going through all buffers (there could be many buffers). */
705 for (wp = firstwin; wp != NULL; wp = wp->w_next)
706 wp->w_buffer->b_mod_set = FALSE;
707#else
708 curbuf->b_mod_set = FALSE;
709#endif
710
711 updating_screen = FALSE;
712#ifdef FEAT_GUI
713 gui_may_resize_shell();
714#endif
715
716 /* Clear or redraw the command line. Done last, because scrolling may
717 * mess up the command line. */
718 if (clear_cmdline || redraw_cmdline)
719 showmode();
720
721 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200722 if (!did_intro)
723 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 did_intro = TRUE;
725
726#ifdef FEAT_GUI
727 /* Redraw the cursor and update the scrollbars when all screen updating is
728 * done. */
729 if (gui.in_use)
730 {
731 out_flush(); /* required before updating the cursor */
732 if (did_one)
733 gui_update_cursor(FALSE, FALSE);
734 gui_update_scrollbars(FALSE);
735 }
736#endif
737}
738
Bram Moolenaar860cae12010-06-05 23:22:07 +0200739#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200740/*
741 * Return TRUE if the cursor line in window "wp" may be concealed, according
742 * to the 'concealcursor' option.
743 */
744 int
745conceal_cursor_line(wp)
746 win_T *wp;
747{
748 int c;
749
750 if (*wp->w_p_cocu == NUL)
751 return FALSE;
752 if (get_real_state() & VISUAL)
753 c = 'v';
754 else if (State & INSERT)
755 c = 'i';
756 else if (State & NORMAL)
757 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200758 else if (State & CMDLINE)
759 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200760 else
761 return FALSE;
762 return vim_strchr(wp->w_p_cocu, c) != NULL;
763}
764
765/*
766 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
767 */
768 void
Bram Moolenaar8e469272010-07-28 19:38:16 +0200769conceal_check_cursur_line()
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200770{
771 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
772 {
773 need_cursor_line_redraw = TRUE;
774 /* Need to recompute cursor column, e.g., when starting Visual mode
775 * without concealing. */
776 curs_columns(TRUE);
777 }
778}
779
Bram Moolenaar860cae12010-06-05 23:22:07 +0200780 void
781update_single_line(wp, lnum)
782 win_T *wp;
783 linenr_T lnum;
784{
785 int row;
786 int j;
787
788 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200789 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200790 {
791# ifdef FEAT_GUI
792 /* Remove the cursor before starting to do anything, because scrolling
793 * may make it difficult to redraw the text under it. */
794 if (gui.in_use)
795 gui_undraw_cursor();
796# endif
797 row = 0;
798 for (j = 0; j < wp->w_lines_valid; ++j)
799 {
800 if (lnum == wp->w_lines[j].wl_lnum)
801 {
802 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200803# ifdef FEAT_SEARCH_EXTRA
804 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200805 start_search_hl();
806 prepare_search_hl(wp, lnum);
807# endif
808 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
809# if defined(FEAT_SEARCH_EXTRA)
810 end_search_hl();
811# endif
812 break;
813 }
814 row += wp->w_lines[j].wl_size;
815 }
816# ifdef FEAT_GUI
817 /* Redraw the cursor */
818 if (gui.in_use)
819 {
820 out_flush(); /* required before updating the cursor */
821 gui_update_cursor(FALSE, FALSE);
822 }
823# endif
824 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200825 need_cursor_line_redraw = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200826}
827#endif
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829#if defined(FEAT_SIGNS) || defined(FEAT_GUI)
830static void update_prepare __ARGS((void));
831static void update_finish __ARGS((void));
832
833/*
834 * Prepare for updating one or more windows.
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000835 * Caller must check for "updating_screen" already set to avoid recursiveness.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 */
837 static void
838update_prepare()
839{
840 cursor_off();
841 updating_screen = TRUE;
842#ifdef FEAT_GUI
843 /* Remove the cursor before starting to do anything, because scrolling may
844 * make it difficult to redraw the text under it. */
845 if (gui.in_use)
846 gui_undraw_cursor();
847#endif
848#ifdef FEAT_SEARCH_EXTRA
849 start_search_hl();
850#endif
851}
852
853/*
854 * Finish updating one or more windows.
855 */
856 static void
857update_finish()
858{
859 if (redraw_cmdline)
860 showmode();
861
862# ifdef FEAT_SEARCH_EXTRA
863 end_search_hl();
864# endif
865
866 updating_screen = FALSE;
867
868# ifdef FEAT_GUI
869 gui_may_resize_shell();
870
871 /* Redraw the cursor and update the scrollbars when all screen updating is
872 * done. */
873 if (gui.in_use)
874 {
875 out_flush(); /* required before updating the cursor */
876 gui_update_cursor(FALSE, FALSE);
877 gui_update_scrollbars(FALSE);
878 }
879# endif
880}
881#endif
882
883#if defined(FEAT_SIGNS) || defined(PROTO)
884 void
885update_debug_sign(buf, lnum)
886 buf_T *buf;
887 linenr_T lnum;
888{
889 win_T *wp;
890 int doit = FALSE;
891
892# ifdef FEAT_FOLDING
893 win_foldinfo.fi_level = 0;
894# endif
895
896 /* update/delete a specific mark */
897 FOR_ALL_WINDOWS(wp)
898 {
899 if (buf != NULL && lnum > 0)
900 {
901 if (wp->w_buffer == buf && lnum >= wp->w_topline
902 && lnum < wp->w_botline)
903 {
904 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
905 wp->w_redraw_top = lnum;
906 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
907 wp->w_redraw_bot = lnum;
908 redraw_win_later(wp, VALID);
909 }
910 }
911 else
912 redraw_win_later(wp, VALID);
913 if (wp->w_redr_type != 0)
914 doit = TRUE;
915 }
916
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100917 /* Return when there is nothing to do, screen updating is already
918 * happening (recursive call) or still starting up. */
919 if (!doit || updating_screen
920#ifdef FEAT_GUI
921 || gui.starting
922#endif
923 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 return;
925
926 /* update all windows that need updating */
927 update_prepare();
928
929# ifdef FEAT_WINDOWS
930 for (wp = firstwin; wp; wp = wp->w_next)
931 {
932 if (wp->w_redr_type != 0)
933 win_update(wp);
934 if (wp->w_redr_status)
935 win_redr_status(wp);
936 }
937# else
938 if (curwin->w_redr_type != 0)
939 win_update(curwin);
940# endif
941
942 update_finish();
943}
944#endif
945
946
947#if defined(FEAT_GUI) || defined(PROTO)
948/*
949 * Update a single window, its status line and maybe the command line msg.
950 * Used for the GUI scrollbar.
951 */
952 void
953updateWindow(wp)
954 win_T *wp;
955{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000956 /* return if already busy updating */
957 if (updating_screen)
958 return;
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 update_prepare();
961
962#ifdef FEAT_CLIPBOARD
963 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200964 if (clip_star.available && clip_isautosel_star())
965 clip_update_selection(&clip_star);
966 if (clip_plus.available && clip_isautosel_plus())
967 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_WINDOWS
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000973 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000974 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000975 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +0000976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (wp->w_redr_status
978# ifdef FEAT_CMDL_INFO
979 || p_ru
980# endif
981# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000982 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983# endif
984 )
985 win_redr_status(wp);
986#endif
987
988 update_finish();
989}
990#endif
991
992/*
993 * Update a single window.
994 *
995 * This may cause the windows below it also to be redrawn (when clearing the
996 * screen or scrolling lines).
997 *
998 * How the window is redrawn depends on wp->w_redr_type. Each type also
999 * implies the one below it.
1000 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001001 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1003 * INVERTED redraw the changed part of the Visual area
1004 * INVERTED_ALL redraw the whole Visual area
1005 * VALID 1. scroll up/down to adjust for a changed w_topline
1006 * 2. update lines at the top when scrolled down
1007 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001008 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 * b_mod_top and b_mod_bot.
1010 * - if wp->w_redraw_top non-zero, redraw lines between
1011 * wp->w_redraw_top and wp->w_redr_bot.
1012 * - continue redrawing when syntax status is invalid.
1013 * 4. if scrolled up, update lines at the bottom.
1014 * This results in three areas that may need updating:
1015 * top: from first row to top_end (when scrolled down)
1016 * mid: from mid_start to mid_end (update inversion or changed text)
1017 * bot: from bot_start to last row (when scrolled up)
1018 */
1019 static void
1020win_update(wp)
1021 win_T *wp;
1022{
1023 buf_T *buf = wp->w_buffer;
1024 int type;
1025 int top_end = 0; /* Below last row of the top area that needs
1026 updating. 0 when no top area updating. */
1027 int mid_start = 999;/* first row of the mid area that needs
1028 updating. 999 when no mid area updating. */
1029 int mid_end = 0; /* Below last row of the mid area that needs
1030 updating. 0 when no mid area updating. */
1031 int bot_start = 999;/* first row of the bot area that needs
1032 updating. 999 when no bot area updating */
1033#ifdef FEAT_VISUAL
1034 int scrolled_down = FALSE; /* TRUE when scrolled down when
1035 w_topline got smaller a bit */
1036#endif
1037#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001038 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 int top_to_mod = FALSE; /* redraw above mod_top */
1040#endif
1041
1042 int row; /* current window row to display */
1043 linenr_T lnum; /* current buffer lnum to display */
1044 int idx; /* current index in w_lines[] */
1045 int srow; /* starting row of the current line */
1046
1047 int eof = FALSE; /* if TRUE, we hit the end of the file */
1048 int didline = FALSE; /* if TRUE, we finished the last line */
1049 int i;
1050 long j;
1051 static int recursive = FALSE; /* being called recursively */
1052 int old_botline = wp->w_botline;
1053#ifdef FEAT_FOLDING
1054 long fold_count;
1055#endif
1056#ifdef FEAT_SYN_HL
1057 /* remember what happened to the previous line, to know if
1058 * check_visual_highlight() can be used */
1059#define DID_NONE 1 /* didn't update a line */
1060#define DID_LINE 2 /* updated a normal line */
1061#define DID_FOLD 3 /* updated a folded line */
1062 int did_update = DID_NONE;
1063 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1064#endif
1065 linenr_T mod_top = 0;
1066 linenr_T mod_bot = 0;
1067#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1068 int save_got_int;
1069#endif
1070
1071 type = wp->w_redr_type;
1072
1073 if (type == NOT_VALID)
1074 {
1075#ifdef FEAT_WINDOWS
1076 wp->w_redr_status = TRUE;
1077#endif
1078 wp->w_lines_valid = 0;
1079 }
1080
1081 /* Window is zero-height: nothing to draw. */
1082 if (wp->w_height == 0)
1083 {
1084 wp->w_redr_type = 0;
1085 return;
1086 }
1087
1088#ifdef FEAT_VERTSPLIT
1089 /* Window is zero-width: Only need to draw the separator. */
1090 if (wp->w_width == 0)
1091 {
1092 /* draw the vertical separator right of this window */
1093 draw_vsep_win(wp, 0);
1094 wp->w_redr_type = 0;
1095 return;
1096 }
1097#endif
1098
1099#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001100 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001103#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001104 /* Force redraw when width of 'number' or 'relativenumber' column
1105 * changes. */
1106 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001107 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001108 {
1109 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001110 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001111 }
1112 else
1113#endif
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1116 {
1117 /*
1118 * When there are both inserted/deleted lines and specific lines to be
1119 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1120 * everything (only happens when redrawing is off for while).
1121 */
1122 type = NOT_VALID;
1123 }
1124 else
1125 {
1126 /*
1127 * Set mod_top to the first line that needs displaying because of
1128 * changes. Set mod_bot to the first line after the changes.
1129 */
1130 mod_top = wp->w_redraw_top;
1131 if (wp->w_redraw_bot != 0)
1132 mod_bot = wp->w_redraw_bot + 1;
1133 else
1134 mod_bot = 0;
1135 wp->w_redraw_top = 0; /* reset for next time */
1136 wp->w_redraw_bot = 0;
1137 if (buf->b_mod_set)
1138 {
1139 if (mod_top == 0 || mod_top > buf->b_mod_top)
1140 {
1141 mod_top = buf->b_mod_top;
1142#ifdef FEAT_SYN_HL
1143 /* Need to redraw lines above the change that may be included
1144 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001145 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001147 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (mod_top < 1)
1149 mod_top = 1;
1150 }
1151#endif
1152 }
1153 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1154 mod_bot = buf->b_mod_bot;
1155
1156#ifdef FEAT_SEARCH_EXTRA
1157 /* When 'hlsearch' is on and using a multi-line search pattern, a
1158 * change in one line may make the Search highlighting in a
1159 * previous line invalid. Simple solution: redraw all visible
1160 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001161 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001163 if (search_hl.rm.regprog != NULL
1164 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001166 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001167 {
1168 cur = wp->w_match_head;
1169 while (cur != NULL)
1170 {
1171 if (cur->match.regprog != NULL
1172 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001173 {
1174 top_to_mod = TRUE;
1175 break;
1176 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001177 cur = cur->next;
1178 }
1179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180#endif
1181 }
1182#ifdef FEAT_FOLDING
1183 if (mod_top != 0 && hasAnyFolding(wp))
1184 {
1185 linenr_T lnumt, lnumb;
1186
1187 /*
1188 * A change in a line can cause lines above it to become folded or
1189 * unfolded. Find the top most buffer line that may be affected.
1190 * If the line was previously folded and displayed, get the first
1191 * line of that fold. If the line is folded now, get the first
1192 * folded line. Use the minimum of these two.
1193 */
1194
1195 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1196 * the line below it. If there is no valid entry, use w_topline.
1197 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1198 * to this line. If there is no valid entry, use MAXLNUM. */
1199 lnumt = wp->w_topline;
1200 lnumb = MAXLNUM;
1201 for (i = 0; i < wp->w_lines_valid; ++i)
1202 if (wp->w_lines[i].wl_valid)
1203 {
1204 if (wp->w_lines[i].wl_lastlnum < mod_top)
1205 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1206 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1207 {
1208 lnumb = wp->w_lines[i].wl_lnum;
1209 /* When there is a fold column it might need updating
1210 * in the next line ("J" just above an open fold). */
1211 if (wp->w_p_fdc > 0)
1212 ++lnumb;
1213 }
1214 }
1215
1216 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1217 if (mod_top > lnumt)
1218 mod_top = lnumt;
1219
1220 /* Now do the same for the bottom line (one above mod_bot). */
1221 --mod_bot;
1222 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1223 ++mod_bot;
1224 if (mod_bot < lnumb)
1225 mod_bot = lnumb;
1226 }
1227#endif
1228
1229 /* When a change starts above w_topline and the end is below
1230 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001231 * If the end of the change is above w_topline: do like no change was
1232 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (mod_top != 0 && mod_top < wp->w_topline)
1234 {
1235 if (mod_bot > wp->w_topline)
1236 mod_top = wp->w_topline;
1237#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 top_end = 1;
1240#endif
1241 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001242
1243 /* When line numbers are displayed need to redraw all lines below
1244 * inserted/deleted lines. */
1245 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1246 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248
1249 /*
1250 * When only displaying the lines at the top, set top_end. Used when
1251 * window has scrolled down for msg_scrolled.
1252 */
1253 if (type == REDRAW_TOP)
1254 {
1255 j = 0;
1256 for (i = 0; i < wp->w_lines_valid; ++i)
1257 {
1258 j += wp->w_lines[i].wl_size;
1259 if (j >= wp->w_upd_rows)
1260 {
1261 top_end = j;
1262 break;
1263 }
1264 }
1265 if (top_end == 0)
1266 /* not found (cannot happen?): redraw everything */
1267 type = NOT_VALID;
1268 else
1269 /* top area defined, the rest is VALID */
1270 type = VALID;
1271 }
1272
Bram Moolenaar367329b2007-08-30 11:53:22 +00001273 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001274 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1275 * non-zero and thus not FALSE) will indicate that screenclear() was not
1276 * called. */
1277 if (screen_cleared)
1278 screen_cleared = MAYBE;
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 /*
1281 * If there are no changes on the screen that require a complete redraw,
1282 * handle three cases:
1283 * 1: we are off the top of the screen by a few lines: scroll down
1284 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1285 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1286 * w_lines[] that needs updating.
1287 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001288 if ((type == VALID || type == SOME_VALID
1289 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290#ifdef FEAT_DIFF
1291 && !wp->w_botfill && !wp->w_old_botfill
1292#endif
1293 )
1294 {
1295 if (mod_top != 0 && wp->w_topline == mod_top)
1296 {
1297 /*
1298 * w_topline is the first changed line, the scrolling will be done
1299 * further down.
1300 */
1301 }
1302 else if (wp->w_lines[0].wl_valid
1303 && (wp->w_topline < wp->w_lines[0].wl_lnum
1304#ifdef FEAT_DIFF
1305 || (wp->w_topline == wp->w_lines[0].wl_lnum
1306 && wp->w_topfill > wp->w_old_topfill)
1307#endif
1308 ))
1309 {
1310 /*
1311 * New topline is above old topline: May scroll down.
1312 */
1313#ifdef FEAT_FOLDING
1314 if (hasAnyFolding(wp))
1315 {
1316 linenr_T ln;
1317
1318 /* count the number of lines we are off, counting a sequence
1319 * of folded lines as one */
1320 j = 0;
1321 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1322 {
1323 ++j;
1324 if (j >= wp->w_height - 2)
1325 break;
1326 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1327 }
1328 }
1329 else
1330#endif
1331 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1332 if (j < wp->w_height - 2) /* not too far off */
1333 {
1334 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1335#ifdef FEAT_DIFF
1336 /* insert extra lines for previously invisible filler lines */
1337 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1338 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1339 - wp->w_old_topfill;
1340#endif
1341 if (i < wp->w_height - 2) /* less than a screen off */
1342 {
1343 /*
1344 * Try to insert the correct number of lines.
1345 * If not the last window, delete the lines at the bottom.
1346 * win_ins_lines may fail when the terminal can't do it.
1347 */
1348 if (i > 0)
1349 check_for_delay(FALSE);
1350 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1351 {
1352 if (wp->w_lines_valid != 0)
1353 {
1354 /* Need to update rows that are new, stop at the
1355 * first one that scrolled down. */
1356 top_end = i;
1357#ifdef FEAT_VISUAL
1358 scrolled_down = TRUE;
1359#endif
1360
1361 /* Move the entries that were scrolled, disable
1362 * the entries for the lines to be redrawn. */
1363 if ((wp->w_lines_valid += j) > wp->w_height)
1364 wp->w_lines_valid = wp->w_height;
1365 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1366 wp->w_lines[idx] = wp->w_lines[idx - j];
1367 while (idx >= 0)
1368 wp->w_lines[idx--].wl_valid = FALSE;
1369 }
1370 }
1371 else
1372 mid_start = 0; /* redraw all lines */
1373 }
1374 else
1375 mid_start = 0; /* redraw all lines */
1376 }
1377 else
1378 mid_start = 0; /* redraw all lines */
1379 }
1380 else
1381 {
1382 /*
1383 * New topline is at or below old topline: May scroll up.
1384 * When topline didn't change, find first entry in w_lines[] that
1385 * needs updating.
1386 */
1387
1388 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1389 j = -1;
1390 row = 0;
1391 for (i = 0; i < wp->w_lines_valid; i++)
1392 {
1393 if (wp->w_lines[i].wl_valid
1394 && wp->w_lines[i].wl_lnum == wp->w_topline)
1395 {
1396 j = i;
1397 break;
1398 }
1399 row += wp->w_lines[i].wl_size;
1400 }
1401 if (j == -1)
1402 {
1403 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1404 * lines */
1405 mid_start = 0;
1406 }
1407 else
1408 {
1409 /*
1410 * Try to delete the correct number of lines.
1411 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1412 */
1413#ifdef FEAT_DIFF
1414 /* If the topline didn't change, delete old filler lines,
1415 * otherwise delete filler lines of the new topline... */
1416 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1417 row += wp->w_old_topfill;
1418 else
1419 row += diff_check_fill(wp, wp->w_topline);
1420 /* ... but don't delete new filler lines. */
1421 row -= wp->w_topfill;
1422#endif
1423 if (row > 0)
1424 {
1425 check_for_delay(FALSE);
1426 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin) == OK)
1427 bot_start = wp->w_height - row;
1428 else
1429 mid_start = 0; /* redraw all lines */
1430 }
1431 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1432 {
1433 /*
1434 * Skip the lines (below the deleted lines) that are still
1435 * valid and don't need redrawing. Copy their info
1436 * upwards, to compensate for the deleted lines. Set
1437 * bot_start to the first row that needs redrawing.
1438 */
1439 bot_start = 0;
1440 idx = 0;
1441 for (;;)
1442 {
1443 wp->w_lines[idx] = wp->w_lines[j];
1444 /* stop at line that didn't fit, unless it is still
1445 * valid (no lines deleted) */
1446 if (row > 0 && bot_start + row
1447 + (int)wp->w_lines[j].wl_size > wp->w_height)
1448 {
1449 wp->w_lines_valid = idx + 1;
1450 break;
1451 }
1452 bot_start += wp->w_lines[idx++].wl_size;
1453
1454 /* stop at the last valid entry in w_lines[].wl_size */
1455 if (++j >= wp->w_lines_valid)
1456 {
1457 wp->w_lines_valid = idx;
1458 break;
1459 }
1460 }
1461#ifdef FEAT_DIFF
1462 /* Correct the first entry for filler lines at the top
1463 * when it won't get updated below. */
1464 if (wp->w_p_diff && bot_start > 0)
1465 wp->w_lines[0].wl_size =
1466 plines_win_nofill(wp, wp->w_topline, TRUE)
1467 + wp->w_topfill;
1468#endif
1469 }
1470 }
1471 }
1472
1473 /* When starting redraw in the first line, redraw all lines. When
1474 * there is only one window it's probably faster to clear the screen
1475 * first. */
1476 if (mid_start == 0)
1477 {
1478 mid_end = wp->w_height;
1479 if (lastwin == firstwin)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001480 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001481 /* Clear the screen when it was not done by win_del_lines() or
1482 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1483 * then. */
1484 if (screen_cleared != TRUE)
1485 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001486#ifdef FEAT_WINDOWS
1487 /* The screen was cleared, redraw the tab pages line. */
1488 if (redraw_tabline)
1489 draw_tabline();
1490#endif
1491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001493
1494 /* When win_del_lines() or win_ins_lines() caused the screen to be
1495 * cleared (only happens for the first window) or when screenclear()
1496 * was called directly above, "must_redraw" will have been set to
1497 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1498 if (screen_cleared == TRUE)
1499 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 else
1502 {
1503 /* Not VALID or INVERTED: redraw all lines. */
1504 mid_start = 0;
1505 mid_end = wp->w_height;
1506 }
1507
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001508 if (type == SOME_VALID)
1509 {
1510 /* SOME_VALID: redraw all lines. */
1511 mid_start = 0;
1512 mid_end = wp->w_height;
1513 type = NOT_VALID;
1514 }
1515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#ifdef FEAT_VISUAL
1517 /* check if we are updating or removing the inverted part */
1518 if ((VIsual_active && buf == curwin->w_buffer)
1519 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1520 {
1521 linenr_T from, to;
1522
1523 if (VIsual_active)
1524 {
1525 if (VIsual_active
1526 && (VIsual_mode != wp->w_old_visual_mode
1527 || type == INVERTED_ALL))
1528 {
1529 /*
1530 * If the type of Visual selection changed, redraw the whole
1531 * selection. Also when the ownership of the X selection is
1532 * gained or lost.
1533 */
1534 if (curwin->w_cursor.lnum < VIsual.lnum)
1535 {
1536 from = curwin->w_cursor.lnum;
1537 to = VIsual.lnum;
1538 }
1539 else
1540 {
1541 from = VIsual.lnum;
1542 to = curwin->w_cursor.lnum;
1543 }
1544 /* redraw more when the cursor moved as well */
1545 if (wp->w_old_cursor_lnum < from)
1546 from = wp->w_old_cursor_lnum;
1547 if (wp->w_old_cursor_lnum > to)
1548 to = wp->w_old_cursor_lnum;
1549 if (wp->w_old_visual_lnum < from)
1550 from = wp->w_old_visual_lnum;
1551 if (wp->w_old_visual_lnum > to)
1552 to = wp->w_old_visual_lnum;
1553 }
1554 else
1555 {
1556 /*
1557 * Find the line numbers that need to be updated: The lines
1558 * between the old cursor position and the current cursor
1559 * position. Also check if the Visual position changed.
1560 */
1561 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1562 {
1563 from = curwin->w_cursor.lnum;
1564 to = wp->w_old_cursor_lnum;
1565 }
1566 else
1567 {
1568 from = wp->w_old_cursor_lnum;
1569 to = curwin->w_cursor.lnum;
1570 if (from == 0) /* Visual mode just started */
1571 from = to;
1572 }
1573
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001574 if (VIsual.lnum != wp->w_old_visual_lnum
1575 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577 if (wp->w_old_visual_lnum < from
1578 && wp->w_old_visual_lnum != 0)
1579 from = wp->w_old_visual_lnum;
1580 if (wp->w_old_visual_lnum > to)
1581 to = wp->w_old_visual_lnum;
1582 if (VIsual.lnum < from)
1583 from = VIsual.lnum;
1584 if (VIsual.lnum > to)
1585 to = VIsual.lnum;
1586 }
1587 }
1588
1589 /*
1590 * If in block mode and changed column or curwin->w_curswant:
1591 * update all lines.
1592 * First compute the actual start and end column.
1593 */
1594 if (VIsual_mode == Ctrl_V)
1595 {
1596 colnr_T fromc, toc;
1597
1598 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
1599 ++toc;
1600 if (curwin->w_curswant == MAXCOL)
1601 toc = MAXCOL;
1602
1603 if (fromc != wp->w_old_cursor_fcol
1604 || toc != wp->w_old_cursor_lcol)
1605 {
1606 if (from > VIsual.lnum)
1607 from = VIsual.lnum;
1608 if (to < VIsual.lnum)
1609 to = VIsual.lnum;
1610 }
1611 wp->w_old_cursor_fcol = fromc;
1612 wp->w_old_cursor_lcol = toc;
1613 }
1614 }
1615 else
1616 {
1617 /* Use the line numbers of the old Visual area. */
1618 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1619 {
1620 from = wp->w_old_cursor_lnum;
1621 to = wp->w_old_visual_lnum;
1622 }
1623 else
1624 {
1625 from = wp->w_old_visual_lnum;
1626 to = wp->w_old_cursor_lnum;
1627 }
1628 }
1629
1630 /*
1631 * There is no need to update lines above the top of the window.
1632 */
1633 if (from < wp->w_topline)
1634 from = wp->w_topline;
1635
1636 /*
1637 * If we know the value of w_botline, use it to restrict the update to
1638 * the lines that are visible in the window.
1639 */
1640 if (wp->w_valid & VALID_BOTLINE)
1641 {
1642 if (from >= wp->w_botline)
1643 from = wp->w_botline - 1;
1644 if (to >= wp->w_botline)
1645 to = wp->w_botline - 1;
1646 }
1647
1648 /*
1649 * Find the minimal part to be updated.
1650 * Watch out for scrolling that made entries in w_lines[] invalid.
1651 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1652 * top_end; need to redraw from top_end to the "to" line.
1653 * A middle mouse click with a Visual selection may change the text
1654 * above the Visual area and reset wl_valid, do count these for
1655 * mid_end (in srow).
1656 */
1657 if (mid_start > 0)
1658 {
1659 lnum = wp->w_topline;
1660 idx = 0;
1661 srow = 0;
1662 if (scrolled_down)
1663 mid_start = top_end;
1664 else
1665 mid_start = 0;
1666 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1667 {
1668 if (wp->w_lines[idx].wl_valid)
1669 mid_start += wp->w_lines[idx].wl_size;
1670 else if (!scrolled_down)
1671 srow += wp->w_lines[idx].wl_size;
1672 ++idx;
1673# ifdef FEAT_FOLDING
1674 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1675 lnum = wp->w_lines[idx].wl_lnum;
1676 else
1677# endif
1678 ++lnum;
1679 }
1680 srow += mid_start;
1681 mid_end = wp->w_height;
1682 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1683 {
1684 if (wp->w_lines[idx].wl_valid
1685 && wp->w_lines[idx].wl_lnum >= to + 1)
1686 {
1687 /* Only update until first row of this line */
1688 mid_end = srow;
1689 break;
1690 }
1691 srow += wp->w_lines[idx].wl_size;
1692 }
1693 }
1694 }
1695
1696 if (VIsual_active && buf == curwin->w_buffer)
1697 {
1698 wp->w_old_visual_mode = VIsual_mode;
1699 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1700 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001701 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 wp->w_old_curswant = curwin->w_curswant;
1703 }
1704 else
1705 {
1706 wp->w_old_visual_mode = 0;
1707 wp->w_old_cursor_lnum = 0;
1708 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001709 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711#endif /* FEAT_VISUAL */
1712
1713#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1714 /* reset got_int, otherwise regexp won't work */
1715 save_got_int = got_int;
1716 got_int = 0;
1717#endif
1718#ifdef FEAT_FOLDING
1719 win_foldinfo.fi_level = 0;
1720#endif
1721
1722 /*
1723 * Update all the window rows.
1724 */
1725 idx = 0; /* first entry in w_lines[].wl_size */
1726 row = 0;
1727 srow = 0;
1728 lnum = wp->w_topline; /* first line shown in window */
1729 for (;;)
1730 {
1731 /* stop updating when reached the end of the window (check for _past_
1732 * the end of the window is at the end of the loop) */
1733 if (row == wp->w_height)
1734 {
1735 didline = TRUE;
1736 break;
1737 }
1738
1739 /* stop updating when hit the end of the file */
1740 if (lnum > buf->b_ml.ml_line_count)
1741 {
1742 eof = TRUE;
1743 break;
1744 }
1745
1746 /* Remember the starting row of the line that is going to be dealt
1747 * with. It is used further down when the line doesn't fit. */
1748 srow = row;
1749
1750 /*
1751 * Update a line when it is in an area that needs updating, when it
1752 * has changes or w_lines[idx] is invalid.
1753 * bot_start may be halfway a wrapped line after using
1754 * win_del_lines(), check if the current line includes it.
1755 * When syntax folding is being used, the saved syntax states will
1756 * already have been updated, we can't see where the syntax state is
1757 * the same again, just update until the end of the window.
1758 */
1759 if (row < top_end
1760 || (row >= mid_start && row < mid_end)
1761#ifdef FEAT_SEARCH_EXTRA
1762 || top_to_mod
1763#endif
1764 || idx >= wp->w_lines_valid
1765 || (row + wp->w_lines[idx].wl_size > bot_start)
1766 || (mod_top != 0
1767 && (lnum == mod_top
1768 || (lnum >= mod_top
1769 && (lnum < mod_bot
1770#ifdef FEAT_SYN_HL
1771 || did_update == DID_FOLD
1772 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001773 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 && (
1775# ifdef FEAT_FOLDING
1776 (foldmethodIsSyntax(wp)
1777 && hasAnyFolding(wp)) ||
1778# endif
1779 syntax_check_changed(lnum)))
1780#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001781#ifdef FEAT_SEARCH_EXTRA
1782 /* match in fixed position might need redraw */
1783 || wp->w_match_head != NULL
1784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 )))))
1786 {
1787#ifdef FEAT_SEARCH_EXTRA
1788 if (lnum == mod_top)
1789 top_to_mod = FALSE;
1790#endif
1791
1792 /*
1793 * When at start of changed lines: May scroll following lines
1794 * up or down to minimize redrawing.
1795 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001796 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798 if (lnum == mod_top
1799 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001800 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 int old_rows = 0;
1803 int new_rows = 0;
1804 int xtra_rows;
1805 linenr_T l;
1806
1807 /* Count the old number of window rows, using w_lines[], which
1808 * should still contain the sizes for the lines as they are
1809 * currently displayed. */
1810 for (i = idx; i < wp->w_lines_valid; ++i)
1811 {
1812 /* Only valid lines have a meaningful wl_lnum. Invalid
1813 * lines are part of the changed area. */
1814 if (wp->w_lines[i].wl_valid
1815 && wp->w_lines[i].wl_lnum == mod_bot)
1816 break;
1817 old_rows += wp->w_lines[i].wl_size;
1818#ifdef FEAT_FOLDING
1819 if (wp->w_lines[i].wl_valid
1820 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1821 {
1822 /* Must have found the last valid entry above mod_bot.
1823 * Add following invalid entries. */
1824 ++i;
1825 while (i < wp->w_lines_valid
1826 && !wp->w_lines[i].wl_valid)
1827 old_rows += wp->w_lines[i++].wl_size;
1828 break;
1829 }
1830#endif
1831 }
1832
1833 if (i >= wp->w_lines_valid)
1834 {
1835 /* We can't find a valid line below the changed lines,
1836 * need to redraw until the end of the window.
1837 * Inserting/deleting lines has no use. */
1838 bot_start = 0;
1839 }
1840 else
1841 {
1842 /* Able to count old number of rows: Count new window
1843 * rows, and may insert/delete lines */
1844 j = idx;
1845 for (l = lnum; l < mod_bot; ++l)
1846 {
1847#ifdef FEAT_FOLDING
1848 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1849 ++new_rows;
1850 else
1851#endif
1852#ifdef FEAT_DIFF
1853 if (l == wp->w_topline)
1854 new_rows += plines_win_nofill(wp, l, TRUE)
1855 + wp->w_topfill;
1856 else
1857#endif
1858 new_rows += plines_win(wp, l, TRUE);
1859 ++j;
1860 if (new_rows > wp->w_height - row - 2)
1861 {
1862 /* it's getting too much, must redraw the rest */
1863 new_rows = 9999;
1864 break;
1865 }
1866 }
1867 xtra_rows = new_rows - old_rows;
1868 if (xtra_rows < 0)
1869 {
1870 /* May scroll text up. If there is not enough
1871 * remaining text or scrolling fails, must redraw the
1872 * rest. If scrolling works, must redraw the text
1873 * below the scrolled text. */
1874 if (row - xtra_rows >= wp->w_height - 2)
1875 mod_bot = MAXLNUM;
1876 else
1877 {
1878 check_for_delay(FALSE);
1879 if (win_del_lines(wp, row,
1880 -xtra_rows, FALSE, FALSE) == FAIL)
1881 mod_bot = MAXLNUM;
1882 else
1883 bot_start = wp->w_height + xtra_rows;
1884 }
1885 }
1886 else if (xtra_rows > 0)
1887 {
1888 /* May scroll text down. If there is not enough
1889 * remaining text of scrolling fails, must redraw the
1890 * rest. */
1891 if (row + xtra_rows >= wp->w_height - 2)
1892 mod_bot = MAXLNUM;
1893 else
1894 {
1895 check_for_delay(FALSE);
1896 if (win_ins_lines(wp, row + old_rows,
1897 xtra_rows, FALSE, FALSE) == FAIL)
1898 mod_bot = MAXLNUM;
1899 else if (top_end > row + old_rows)
1900 /* Scrolled the part at the top that requires
1901 * updating down. */
1902 top_end += xtra_rows;
1903 }
1904 }
1905
1906 /* When not updating the rest, may need to move w_lines[]
1907 * entries. */
1908 if (mod_bot != MAXLNUM && i != j)
1909 {
1910 if (j < i)
1911 {
1912 int x = row + new_rows;
1913
1914 /* move entries in w_lines[] upwards */
1915 for (;;)
1916 {
1917 /* stop at last valid entry in w_lines[] */
1918 if (i >= wp->w_lines_valid)
1919 {
1920 wp->w_lines_valid = j;
1921 break;
1922 }
1923 wp->w_lines[j] = wp->w_lines[i];
1924 /* stop at a line that won't fit */
1925 if (x + (int)wp->w_lines[j].wl_size
1926 > wp->w_height)
1927 {
1928 wp->w_lines_valid = j + 1;
1929 break;
1930 }
1931 x += wp->w_lines[j++].wl_size;
1932 ++i;
1933 }
1934 if (bot_start > x)
1935 bot_start = x;
1936 }
1937 else /* j > i */
1938 {
1939 /* move entries in w_lines[] downwards */
1940 j -= i;
1941 wp->w_lines_valid += j;
1942 if (wp->w_lines_valid > wp->w_height)
1943 wp->w_lines_valid = wp->w_height;
1944 for (i = wp->w_lines_valid; i - j >= idx; --i)
1945 wp->w_lines[i] = wp->w_lines[i - j];
1946
1947 /* The w_lines[] entries for inserted lines are
1948 * now invalid, but wl_size may be used above.
1949 * Reset to zero. */
1950 while (i >= idx)
1951 {
1952 wp->w_lines[i].wl_size = 0;
1953 wp->w_lines[i--].wl_valid = FALSE;
1954 }
1955 }
1956 }
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959
1960#ifdef FEAT_FOLDING
1961 /*
1962 * When lines are folded, display one line for all of them.
1963 * Otherwise, display normally (can be several display lines when
1964 * 'wrap' is on).
1965 */
1966 fold_count = foldedCount(wp, lnum, &win_foldinfo);
1967 if (fold_count != 0)
1968 {
1969 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
1970 ++row;
1971 --fold_count;
1972 wp->w_lines[idx].wl_folded = TRUE;
1973 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
1974# ifdef FEAT_SYN_HL
1975 did_update = DID_FOLD;
1976# endif
1977 }
1978 else
1979#endif
1980 if (idx < wp->w_lines_valid
1981 && wp->w_lines[idx].wl_valid
1982 && wp->w_lines[idx].wl_lnum == lnum
1983 && lnum > wp->w_topline
1984 && !(dy_flags & DY_LASTLINE)
1985 && srow + wp->w_lines[idx].wl_size > wp->w_height
1986#ifdef FEAT_DIFF
1987 && diff_check_fill(wp, lnum) == 0
1988#endif
1989 )
1990 {
1991 /* This line is not going to fit. Don't draw anything here,
1992 * will draw "@ " lines below. */
1993 row = wp->w_height + 1;
1994 }
1995 else
1996 {
1997#ifdef FEAT_SEARCH_EXTRA
1998 prepare_search_hl(wp, lnum);
1999#endif
2000#ifdef FEAT_SYN_HL
2001 /* Let the syntax stuff know we skipped a few lines. */
2002 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002003 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 syntax_end_parsing(syntax_last_parsed + 1);
2005#endif
2006
2007 /*
2008 * Display one line.
2009 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002010 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012#ifdef FEAT_FOLDING
2013 wp->w_lines[idx].wl_folded = FALSE;
2014 wp->w_lines[idx].wl_lastlnum = lnum;
2015#endif
2016#ifdef FEAT_SYN_HL
2017 did_update = DID_LINE;
2018 syntax_last_parsed = lnum;
2019#endif
2020 }
2021
2022 wp->w_lines[idx].wl_lnum = lnum;
2023 wp->w_lines[idx].wl_valid = TRUE;
2024 if (row > wp->w_height) /* past end of screen */
2025 {
2026 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002027 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2029 ++idx;
2030 break;
2031 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002032 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 wp->w_lines[idx].wl_size = row - srow;
2034 ++idx;
2035#ifdef FEAT_FOLDING
2036 lnum += fold_count + 1;
2037#else
2038 ++lnum;
2039#endif
2040 }
2041 else
2042 {
2043 /* This line does not need updating, advance to the next one */
2044 row += wp->w_lines[idx++].wl_size;
2045 if (row > wp->w_height) /* past end of screen */
2046 break;
2047#ifdef FEAT_FOLDING
2048 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2049#else
2050 ++lnum;
2051#endif
2052#ifdef FEAT_SYN_HL
2053 did_update = DID_NONE;
2054#endif
2055 }
2056
2057 if (lnum > buf->b_ml.ml_line_count)
2058 {
2059 eof = TRUE;
2060 break;
2061 }
2062 }
2063 /*
2064 * End of loop over all window lines.
2065 */
2066
2067
2068 if (idx > wp->w_lines_valid)
2069 wp->w_lines_valid = idx;
2070
2071#ifdef FEAT_SYN_HL
2072 /*
2073 * Let the syntax stuff know we stop parsing here.
2074 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002075 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 syntax_end_parsing(syntax_last_parsed + 1);
2077#endif
2078
2079 /*
2080 * If we didn't hit the end of the file, and we didn't finish the last
2081 * line we were working on, then the line didn't fit.
2082 */
2083 wp->w_empty_rows = 0;
2084#ifdef FEAT_DIFF
2085 wp->w_filler_rows = 0;
2086#endif
2087 if (!eof && !didline)
2088 {
2089 if (lnum == wp->w_topline)
2090 {
2091 /*
2092 * Single line that does not fit!
2093 * Don't overwrite it, it can be edited.
2094 */
2095 wp->w_botline = lnum + 1;
2096 }
2097#ifdef FEAT_DIFF
2098 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2099 {
2100 /* Window ends in filler lines. */
2101 wp->w_botline = lnum;
2102 wp->w_filler_rows = wp->w_height - srow;
2103 }
2104#endif
2105 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2106 {
2107 /*
2108 * Last line isn't finished: Display "@@@" at the end.
2109 */
2110 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2111 W_WINROW(wp) + wp->w_height,
2112 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
2113 '@', '@', hl_attr(HLF_AT));
2114 set_empty_rows(wp, srow);
2115 wp->w_botline = lnum;
2116 }
2117 else
2118 {
2119 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2120 wp->w_botline = lnum;
2121 }
2122 }
2123 else
2124 {
2125#ifdef FEAT_VERTSPLIT
2126 draw_vsep_win(wp, row);
2127#endif
2128 if (eof) /* we hit the end of the file */
2129 {
2130 wp->w_botline = buf->b_ml.ml_line_count + 1;
2131#ifdef FEAT_DIFF
2132 j = diff_check_fill(wp, wp->w_botline);
2133 if (j > 0 && !wp->w_botfill)
2134 {
2135 /*
2136 * Display filler lines at the end of the file
2137 */
2138 if (char2cells(fill_diff) > 1)
2139 i = '-';
2140 else
2141 i = fill_diff;
2142 if (row + j > wp->w_height)
2143 j = wp->w_height - row;
2144 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2145 row += j;
2146 }
2147#endif
2148 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_botline = lnum;
2151
2152 /* make sure the rest of the screen is blank */
2153 /* put '~'s on rows that aren't part of the file. */
2154 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_AT);
2155 }
2156
2157 /* Reset the type of redrawing required, the window has been updated. */
2158 wp->w_redr_type = 0;
2159#ifdef FEAT_DIFF
2160 wp->w_old_topfill = wp->w_topfill;
2161 wp->w_old_botfill = wp->w_botfill;
2162#endif
2163
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 /*
2167 * There is a trick with w_botline. If we invalidate it on each
2168 * change that might modify it, this will cause a lot of expensive
2169 * calls to plines() in update_topline() each time. Therefore the
2170 * value of w_botline is often approximated, and this value is used to
2171 * compute the value of w_topline. If the value of w_botline was
2172 * wrong, check that the value of w_topline is correct (cursor is on
2173 * the visible part of the text). If it's not, we need to redraw
2174 * again. Mostly this just means scrolling up a few lines, so it
2175 * doesn't look too bad. Only do this for the current window (where
2176 * changes are relevant).
2177 */
2178 wp->w_valid |= VALID_BOTLINE;
2179 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2180 {
2181 recursive = TRUE;
2182 curwin->w_valid &= ~VALID_TOPLINE;
2183 update_topline(); /* may invalidate w_botline again */
2184 if (must_redraw != 0)
2185 {
2186 /* Don't update for changes in buffer again. */
2187 i = curbuf->b_mod_set;
2188 curbuf->b_mod_set = FALSE;
2189 win_update(curwin);
2190 must_redraw = 0;
2191 curbuf->b_mod_set = i;
2192 }
2193 recursive = FALSE;
2194 }
2195 }
2196
2197#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2198 /* restore got_int, unless CTRL-C was hit while redrawing */
2199 if (!got_int)
2200 got_int = save_got_int;
2201#endif
2202}
2203
2204#ifdef FEAT_SIGNS
2205static int draw_signcolumn __ARGS((win_T *wp));
2206
2207/*
2208 * Return TRUE when window "wp" has a column to draw signs in.
2209 */
2210 static int
2211draw_signcolumn(wp)
2212 win_T *wp;
2213{
2214 return (wp->w_buffer->b_signlist != NULL
2215# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002216 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217# endif
2218 );
2219}
2220#endif
2221
2222/*
2223 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2224 * as the filler character.
2225 */
2226 static void
2227win_draw_end(wp, c1, c2, row, endrow, hl)
2228 win_T *wp;
2229 int c1;
2230 int c2;
2231 int row;
2232 int endrow;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002233 hlf_T hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2236 int n = 0;
2237# define FDC_OFF n
2238#else
2239# define FDC_OFF 0
2240#endif
2241
2242#ifdef FEAT_RIGHTLEFT
2243 if (wp->w_p_rl)
2244 {
2245 /* No check for cmdline window: should never be right-left. */
2246# ifdef FEAT_FOLDING
2247 n = wp->w_p_fdc;
2248
2249 if (n > 0)
2250 {
2251 /* draw the fold column at the right */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002252 if (n > W_WIDTH(wp))
2253 n = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2255 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
2256 ' ', ' ', hl_attr(HLF_FC));
2257 }
2258# endif
2259# ifdef FEAT_SIGNS
2260 if (draw_signcolumn(wp))
2261 {
2262 int nn = n + 2;
2263
2264 /* draw the sign column left of the fold column */
Bram Moolenaar383f9bc2005-01-19 22:18:32 +00002265 if (nn > W_WIDTH(wp))
2266 nn = W_WIDTH(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2268 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
2269 ' ', ' ', hl_attr(HLF_SC));
2270 n = nn;
2271 }
2272# endif
2273 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2274 W_WINCOL(wp), W_ENDCOL(wp) - 1 - FDC_OFF,
2275 c2, c2, hl_attr(hl));
2276 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2277 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
2278 c1, c2, hl_attr(hl));
2279 }
2280 else
2281#endif
2282 {
2283#ifdef FEAT_CMDWIN
2284 if (cmdwin_type != 0 && wp == curwin)
2285 {
2286 /* draw the cmdline character in the leftmost column */
2287 n = 1;
2288 if (n > wp->w_width)
2289 n = wp->w_width;
2290 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2291 W_WINCOL(wp), (int)W_WINCOL(wp) + n,
2292 cmdwin_type, ' ', hl_attr(HLF_AT));
2293 }
2294#endif
2295#ifdef FEAT_FOLDING
2296 if (wp->w_p_fdc > 0)
2297 {
2298 int nn = n + wp->w_p_fdc;
2299
2300 /* draw the fold column at the left */
2301 if (nn > W_WIDTH(wp))
2302 nn = W_WIDTH(wp);
2303 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2304 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2305 ' ', ' ', hl_attr(HLF_FC));
2306 n = nn;
2307 }
2308#endif
2309#ifdef FEAT_SIGNS
2310 if (draw_signcolumn(wp))
2311 {
2312 int nn = n + 2;
2313
2314 /* draw the sign column after the fold column */
2315 if (nn > W_WIDTH(wp))
2316 nn = W_WIDTH(wp);
2317 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2318 W_WINCOL(wp) + n, (int)W_WINCOL(wp) + nn,
2319 ' ', ' ', hl_attr(HLF_SC));
2320 n = nn;
2321 }
2322#endif
2323 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2324 W_WINCOL(wp) + FDC_OFF, (int)W_ENDCOL(wp),
2325 c1, c2, hl_attr(hl));
2326 }
2327 set_empty_rows(wp, row);
2328}
2329
Bram Moolenaar1a384422010-07-14 19:53:30 +02002330#ifdef FEAT_SYN_HL
2331static int advance_color_col __ARGS((int vcol, int **color_cols));
2332
2333/*
2334 * Advance **color_cols and return TRUE when there are columns to draw.
2335 */
2336 static int
2337advance_color_col(vcol, color_cols)
2338 int vcol;
2339 int **color_cols;
2340{
2341 while (**color_cols >= 0 && vcol > **color_cols)
2342 ++*color_cols;
2343 return (**color_cols >= 0);
2344}
2345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#ifdef FEAT_FOLDING
2348/*
2349 * Display one folded line.
2350 */
2351 static void
2352fold_line(wp, fold_count, foldinfo, lnum, row)
2353 win_T *wp;
2354 long fold_count;
2355 foldinfo_T *foldinfo;
2356 linenr_T lnum;
2357 int row;
2358{
2359 char_u buf[51];
2360 pos_T *top, *bot;
2361 linenr_T lnume = lnum + fold_count - 1;
2362 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002363 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int col;
2366 int txtcol;
2367 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002368 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* Build the fold line:
2371 * 1. Add the cmdwin_type for the command-line window
2372 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002373 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * 4. Compose the text
2375 * 5. Add the text
2376 * 6. set highlighting for the Visual area an other text
2377 */
2378 col = 0;
2379
2380 /*
2381 * 1. Add the cmdwin_type for the command-line window
2382 * Ignores 'rightleft', this window is never right-left.
2383 */
2384#ifdef FEAT_CMDWIN
2385 if (cmdwin_type != 0 && wp == curwin)
2386 {
2387 ScreenLines[off] = cmdwin_type;
2388 ScreenAttrs[off] = hl_attr(HLF_AT);
2389#ifdef FEAT_MBYTE
2390 if (enc_utf8)
2391 ScreenLinesUC[off] = 0;
2392#endif
2393 ++col;
2394 }
2395#endif
2396
2397 /*
2398 * 2. Add the 'foldcolumn'
2399 */
2400 fdc = wp->w_p_fdc;
2401 if (fdc > W_WIDTH(wp) - col)
2402 fdc = W_WIDTH(wp) - col;
2403 if (fdc > 0)
2404 {
2405 fill_foldcolumn(buf, wp, TRUE, lnum);
2406#ifdef FEAT_RIGHTLEFT
2407 if (wp->w_p_rl)
2408 {
2409 int i;
2410
2411 copy_text_attr(off + W_WIDTH(wp) - fdc - col, buf, fdc,
2412 hl_attr(HLF_FC));
2413 /* reverse the fold column */
2414 for (i = 0; i < fdc; ++i)
2415 ScreenLines[off + W_WIDTH(wp) - i - 1 - col] = buf[i];
2416 }
2417 else
2418#endif
2419 copy_text_attr(off + col, buf, fdc, hl_attr(HLF_FC));
2420 col += fdc;
2421 }
2422
2423#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002424# define RL_MEMSET(p, v, l) if (wp->w_p_rl) \
2425 for (ri = 0; ri < l; ++ri) \
2426 ScreenAttrs[off + (W_WIDTH(wp) - (p) - (l)) + ri] = v; \
2427 else \
2428 for (ri = 0; ri < l; ++ri) \
2429 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430#else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002431# define RL_MEMSET(p, v, l) for (ri = 0; ri < l; ++ri) \
2432 ScreenAttrs[off + (p) + ri] = v
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#endif
2434
Bram Moolenaar64486672010-05-16 15:46:46 +02002435 /* Set all attributes of the 'number' or 'relativenumber' column and the
2436 * text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002437 RL_MEMSET(col, hl_attr(HLF_FL), W_WIDTH(wp) - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
2439#ifdef FEAT_SIGNS
2440 /* If signs are being displayed, add two spaces. */
2441 if (draw_signcolumn(wp))
2442 {
2443 len = W_WIDTH(wp) - col;
2444 if (len > 0)
2445 {
2446 if (len > 2)
2447 len = 2;
2448# ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 /* the line number isn't reversed */
2451 copy_text_attr(off + W_WIDTH(wp) - len - col,
2452 (char_u *)" ", len, hl_attr(HLF_FL));
2453 else
2454# endif
2455 copy_text_attr(off + col, (char_u *)" ", len, hl_attr(HLF_FL));
2456 col += len;
2457 }
2458 }
2459#endif
2460
2461 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002462 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002464 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 len = W_WIDTH(wp) - col;
2467 if (len > 0)
2468 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002469 int w = number_width(wp);
Bram Moolenaar64486672010-05-16 15:46:46 +02002470 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01002471 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002472
2473 if (len > w + 1)
2474 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002475
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002476 if (wp->w_p_nu && !wp->w_p_rnu)
2477 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002478 num = (long)lnum;
2479 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002480 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002481 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002482 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002483 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002484 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002485 /* 'number' + 'relativenumber': cursor line shows absolute
2486 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002487 num = lnum;
2488 fmt = "%-*ld ";
2489 }
2490 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002491
Bram Moolenaar700e7342013-01-30 12:31:36 +01002492 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_RIGHTLEFT
2494 if (wp->w_p_rl)
2495 /* the line number isn't reversed */
2496 copy_text_attr(off + W_WIDTH(wp) - len - col, buf, len,
2497 hl_attr(HLF_FL));
2498 else
2499#endif
2500 copy_text_attr(off + col, buf, len, hl_attr(HLF_FL));
2501 col += len;
2502 }
2503 }
2504
2505 /*
2506 * 4. Compose the folded-line string with 'foldtext', if set.
2507 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002508 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 txtcol = col; /* remember where text starts */
2511
2512 /*
2513 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2514 * Right-left text is put in columns 0 - number-col, normal text is put
2515 * in columns number-col - window-width.
2516 */
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
2519 {
2520 int cells;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002521 int u8c, u8cc[MAX_MCO];
2522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 int idx;
2524 int c_len;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002525 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526# ifdef FEAT_ARABIC
2527 int prev_c = 0; /* previous Arabic character */
2528 int prev_c1 = 0; /* first composing char for prev_c */
2529# endif
2530
2531# ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 idx = off;
2534 else
2535# endif
2536 idx = off + col;
2537
2538 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2539 for (p = text; *p != NUL; )
2540 {
2541 cells = (*mb_ptr2cells)(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 c_len = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (col + cells > W_WIDTH(wp)
2544# ifdef FEAT_RIGHTLEFT
2545 - (wp->w_p_rl ? col : 0)
2546# endif
2547 )
2548 break;
2549 ScreenLines[idx] = *p;
2550 if (enc_utf8)
2551 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002552 u8c = utfc_ptr2char(p, u8cc);
2553 if (*p < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 ScreenLinesUC[idx] = 0;
2556#ifdef FEAT_ARABIC
2557 prev_c = u8c;
2558#endif
2559 }
2560 else
2561 {
2562#ifdef FEAT_ARABIC
2563 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2564 {
2565 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002566 int pc, pc1, nc;
2567 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 int firstbyte = *p;
2569
2570 /* The idea of what is the previous and next
2571 * character depends on 'rightleft'. */
2572 if (wp->w_p_rl)
2573 {
2574 pc = prev_c;
2575 pc1 = prev_c1;
2576 nc = utf_ptr2char(p + c_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002577 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002581 pc = utfc_ptr2char(p + c_len, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002583 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 prev_c = u8c;
2586
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 pc, pc1, nc);
2589 ScreenLines[idx] = firstbyte;
2590 }
2591 else
2592 prev_c = u8c;
2593#endif
2594 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar11936362007-09-17 20:39:42 +00002595#ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (u8c >= 0x10000)
2597 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2598 else
Bram Moolenaar11936362007-09-17 20:39:42 +00002599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 ScreenLinesUC[idx] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002601 for (i = 0; i < Screen_mco; ++i)
2602 {
2603 ScreenLinesC[i][idx] = u8cc[i];
2604 if (u8cc[i] == 0)
2605 break;
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 if (cells > 1)
2609 ScreenLines[idx + 1] = 0;
2610 }
Bram Moolenaar990bb662010-02-03 15:48:04 +01002611 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2612 /* double-byte single width character */
2613 ScreenLines2[idx] = p[1];
2614 else if (cells > 1)
2615 /* double-width character */
2616 ScreenLines[idx + 1] = p[1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 col += cells;
2618 idx += cells;
2619 p += c_len;
2620 }
2621 }
2622 else
2623#endif
2624 {
2625 len = (int)STRLEN(text);
2626 if (len > W_WIDTH(wp) - col)
2627 len = W_WIDTH(wp) - col;
2628 if (len > 0)
2629 {
2630#ifdef FEAT_RIGHTLEFT
2631 if (wp->w_p_rl)
2632 STRNCPY(current_ScreenLine, text, len);
2633 else
2634#endif
2635 STRNCPY(current_ScreenLine + col, text, len);
2636 col += len;
2637 }
2638 }
2639
2640 /* Fill the rest of the line with the fold filler */
2641#ifdef FEAT_RIGHTLEFT
2642 if (wp->w_p_rl)
2643 col -= txtcol;
2644#endif
2645 while (col < W_WIDTH(wp)
2646#ifdef FEAT_RIGHTLEFT
2647 - (wp->w_p_rl ? txtcol : 0)
2648#endif
2649 )
2650 {
2651#ifdef FEAT_MBYTE
2652 if (enc_utf8)
2653 {
2654 if (fill_fold >= 0x80)
2655 {
2656 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 ScreenLinesC[0][off + col] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 else
2660 ScreenLinesUC[off + col] = 0;
2661 }
2662#endif
2663 ScreenLines[off + col++] = fill_fold;
2664 }
2665
2666 if (text != buf)
2667 vim_free(text);
2668
2669 /*
2670 * 6. set highlighting for the Visual area an other text.
2671 * If all folded lines are in the Visual area, highlight the line.
2672 */
2673#ifdef FEAT_VISUAL
2674 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2675 {
2676 if (ltoreq(curwin->w_cursor, VIsual))
2677 {
2678 /* Visual is after curwin->w_cursor */
2679 top = &curwin->w_cursor;
2680 bot = &VIsual;
2681 }
2682 else
2683 {
2684 /* Visual is before curwin->w_cursor */
2685 top = &VIsual;
2686 bot = &curwin->w_cursor;
2687 }
2688 if (lnum >= top->lnum
2689 && lnume <= bot->lnum
2690 && (VIsual_mode != 'v'
2691 || ((lnum > top->lnum
2692 || (lnum == top->lnum
2693 && top->col == 0))
2694 && (lnume < bot->lnum
2695 || (lnume == bot->lnum
2696 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002697 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 if (VIsual_mode == Ctrl_V)
2700 {
2701 /* Visual block mode: highlight the chars part of the block */
2702 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)W_WIDTH(wp))
2703 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002704 if (wp->w_old_cursor_lcol != MAXCOL
2705 && wp->w_old_cursor_lcol + txtcol
2706 < (colnr_T)W_WIDTH(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 len = wp->w_old_cursor_lcol;
2708 else
2709 len = W_WIDTH(wp) - txtcol;
2710 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, hl_attr(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002711 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 }
2714 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 /* Set all attributes of the text */
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002717 RL_MEMSET(txtcol, hl_attr(HLF_V), W_WIDTH(wp) - txtcol);
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 }
2721#endif
2722
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002723#ifdef FEAT_SYN_HL
2724 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002725 if (wp->w_p_cuc)
2726 {
2727 txtcol += wp->w_virtcol;
2728 if (wp->w_p_wrap)
2729 txtcol -= wp->w_skipcol;
2730 else
2731 txtcol -= wp->w_leftcol;
2732 if (txtcol >= 0 && txtcol < W_WIDTH(wp))
2733 ScreenAttrs[off + txtcol] = hl_combine_attr(
2734 ScreenAttrs[off + txtcol], hl_attr(HLF_CUC));
2735 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 SCREEN_LINE(row + W_WINROW(wp), W_WINCOL(wp), (int)W_WIDTH(wp),
2739 (int)W_WIDTH(wp), FALSE);
2740
2741 /*
2742 * Update w_cline_height and w_cline_folded if the cursor line was
2743 * updated (saves a call to plines() later).
2744 */
2745 if (wp == curwin
2746 && lnum <= curwin->w_cursor.lnum
2747 && lnume >= curwin->w_cursor.lnum)
2748 {
2749 curwin->w_cline_row = row;
2750 curwin->w_cline_height = 1;
2751 curwin->w_cline_folded = TRUE;
2752 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2753 }
2754}
2755
2756/*
2757 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2758 */
2759 static void
2760copy_text_attr(off, buf, len, attr)
2761 int off;
2762 char_u *buf;
2763 int len;
2764 int attr;
2765{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002766 int i;
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 mch_memmove(ScreenLines + off, buf, (size_t)len);
2769# ifdef FEAT_MBYTE
2770 if (enc_utf8)
2771 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2772# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002773 for (i = 0; i < len; ++i)
2774 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775}
2776
2777/*
2778 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002779 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
2781 static void
2782fill_foldcolumn(p, wp, closed, lnum)
2783 char_u *p;
2784 win_T *wp;
2785 int closed; /* TRUE of FALSE */
2786 linenr_T lnum; /* current line number */
2787{
2788 int i = 0;
2789 int level;
2790 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002791 int empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
2793 /* Init to all spaces. */
2794 copy_spaces(p, (size_t)wp->w_p_fdc);
2795
2796 level = win_foldinfo.fi_level;
2797 if (level > 0)
2798 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002799 /* If there is only one column put more info in it. */
2800 empty = (wp->w_p_fdc == 1) ? 0 : 1;
2801
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 /* If the column is too narrow, we start at the lowest level that
2803 * fits and use numbers to indicated the depth. */
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002804 first_level = level - wp->w_p_fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 if (first_level < 1)
2806 first_level = 1;
2807
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002808 for (i = 0; i + empty < wp->w_p_fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
2810 if (win_foldinfo.fi_lnum == lnum
2811 && first_level + i >= win_foldinfo.fi_low_level)
2812 p[i] = '-';
2813 else if (first_level == 1)
2814 p[i] = '|';
2815 else if (first_level + i <= 9)
2816 p[i] = '0' + first_level + i;
2817 else
2818 p[i] = '>';
2819 if (first_level + i == level)
2820 break;
2821 }
2822 }
2823 if (closed)
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002824 p[i >= wp->w_p_fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825}
2826#endif /* FEAT_FOLDING */
2827
2828/*
2829 * Display line "lnum" of window 'wp' on the screen.
2830 * Start at row "startrow", stop when "endrow" is reached.
2831 * wp->w_virtcol needs to be valid.
2832 *
2833 * Return the number of last row the line occupies.
2834 */
2835 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00002836win_line(wp, lnum, startrow, endrow, nochange)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 win_T *wp;
2838 linenr_T lnum;
2839 int startrow;
2840 int endrow;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002841 int nochange UNUSED; /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 int col; /* visual column on screen */
2844 unsigned off; /* offset in ScreenLines/ScreenAttrs */
2845 int c = 0; /* init for GCC */
2846 long vcol = 0; /* virtual column (for tabs) */
2847 long vcol_prev = -1; /* "vcol" of previous character */
2848 char_u *line; /* current line */
2849 char_u *ptr; /* current position in "line" */
2850 int row; /* row in the window, excl w_winrow */
2851 int screen_row; /* row on the screen, incl w_winrow */
2852
2853 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
2854 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00002855 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 int c_extra = NUL; /* extra chars, all the same */
2857 int extra_attr = 0; /* attributes when n_extra != 0 */
2858 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
2859 displaying lcs_eol at end-of-line */
2860 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
2861 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
2862
2863 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
2864 int saved_n_extra = 0;
2865 char_u *saved_p_extra = NULL;
2866 int saved_c_extra = 0;
2867 int saved_char_attr = 0;
2868
2869 int n_attr = 0; /* chars with special attr */
2870 int saved_attr2 = 0; /* char_attr saved for n_attr */
2871 int n_attr3 = 0; /* chars with overruling special attr */
2872 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
2873
2874 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
2875
2876 int fromcol, tocol; /* start/end of inverting */
2877 int fromcol_prev = -2; /* start of inverting after cursor */
2878 int noinvcur = FALSE; /* don't invert the cursor */
2879#ifdef FEAT_VISUAL
2880 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00002881 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882#endif
2883 pos_T pos;
2884 long v;
2885
2886 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002887 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 int area_highlighting = FALSE; /* Visual or incsearch highlighting
2889 in this line */
2890 int attr = 0; /* attributes for area highlighting */
2891 int area_attr = 0; /* attributes desired by highlighting */
2892 int search_attr = 0; /* attributes desired by 'hlsearch' */
2893#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002894 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 int syntax_attr = 0; /* attributes desired by syntax */
2896 int has_syntax = FALSE; /* this buffer has syntax highl. */
2897 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00002898 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02002899 int draw_color_col = FALSE; /* highlight colorcolumn */
2900 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002901#endif
2902#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00002903 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002904# define SPWORDLEN 150
2905 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00002906 int nextlinecol = 0; /* column where nextline[] starts */
2907 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00002908 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00002909 int spell_attr = 0; /* attributes desired by spelling */
2910 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00002911 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
2912 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00002913 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00002914 static int cap_col = -1; /* column to check for Cap word */
2915 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00002916 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917#endif
2918 int extra_check; /* has syntax or linebreak */
2919#ifdef FEAT_MBYTE
2920 int multi_attr = 0; /* attributes desired by multibyte */
2921 int mb_l = 1; /* multi-byte byte length */
2922 int mb_c = 0; /* decoded multi-byte character */
2923 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002924 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
2926#ifdef FEAT_DIFF
2927 int filler_lines; /* nr of filler lines to be drawn */
2928 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002929 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int change_start = MAXCOL; /* first col of changed area */
2931 int change_end = -1; /* last col of changed area */
2932#endif
2933 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
2934#ifdef FEAT_LINEBREAK
2935 int need_showbreak = FALSE;
2936#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002937#if defined(FEAT_SIGNS) || (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
2938 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00002940 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941#endif
2942#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00002943 matchitem_T *cur; /* points to the match list */
2944 match_T *shl; /* points to search_hl or a match */
2945 int shl_flag; /* flag to indicate whether search_hl
2946 has been processed or not */
2947 int prevcol_hl_flag; /* flag to indicate whether prevcol
2948 equals startcol of search_hl or one
2949 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950#endif
2951#ifdef FEAT_ARABIC
2952 int prev_c = 0; /* previous Arabic character */
2953 int prev_c1 = 0; /* first composing char for prev_c */
2954#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00002955#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00002956 int did_line_attr = 0;
2957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /* draw_state: items that are drawn in sequence: */
2960#define WL_START 0 /* nothing done yet */
2961#ifdef FEAT_CMDWIN
2962# define WL_CMDLINE WL_START + 1 /* cmdline window column */
2963#else
2964# define WL_CMDLINE WL_START
2965#endif
2966#ifdef FEAT_FOLDING
2967# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
2968#else
2969# define WL_FOLD WL_CMDLINE
2970#endif
2971#ifdef FEAT_SIGNS
2972# define WL_SIGN WL_FOLD + 1 /* column for signs */
2973#else
2974# define WL_SIGN WL_FOLD /* column for signs */
2975#endif
2976#define WL_NR WL_SIGN + 1 /* line number */
2977#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
2978# define WL_SBR WL_NR + 1 /* 'showbreak' or 'diff' */
2979#else
2980# define WL_SBR WL_NR
2981#endif
2982#define WL_LINE WL_SBR + 1 /* text in the line */
2983 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00002984#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 int feedback_col = 0;
2986 int feedback_old_attr = -1;
2987#endif
2988
Bram Moolenaar860cae12010-06-05 23:22:07 +02002989#ifdef FEAT_CONCEAL
2990 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02002991 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02002992 int prev_syntax_id = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002993 int conceal_attr = hl_attr(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002994 int is_concealing = FALSE;
2995 int boguscols = 0; /* nonexistent columns added to force
2996 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002997 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02002998 int did_wcol = FALSE;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02002999# define VCOL_HLC (vcol - vcol_off)
3000#else
3001# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 if (startrow > endrow) /* past the end already! */
3005 return startrow;
3006
3007 row = startrow;
3008 screen_row = row + W_WINROW(wp);
3009
3010 /*
3011 * To speed up the loop below, set extra_check when there is linebreak,
3012 * trailing white space and/or syntax processing to be done.
3013 */
3014#ifdef FEAT_LINEBREAK
3015 extra_check = wp->w_p_lbr;
3016#else
3017 extra_check = 0;
3018#endif
3019#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003020 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 /* Prepare for syntax highlighting in this line. When there is an
3023 * error, stop syntax highlighting. */
3024 save_did_emsg = did_emsg;
3025 did_emsg = FALSE;
3026 syntax_start(wp, lnum);
3027 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003028 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else
3030 {
3031 did_emsg = save_did_emsg;
3032 has_syntax = TRUE;
3033 extra_check = TRUE;
3034 }
3035 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003036
3037 /* Check for columns to display for 'colorcolumn'. */
3038 color_cols = wp->w_p_cc_cols;
3039 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003040 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003041#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003042
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003043#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003044 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003045 && *wp->w_s->b_p_spl != NUL
3046 && wp->w_s->b_langp.ga_len > 0
3047 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003048 {
3049 /* Prepare for spell checking. */
3050 has_spell = TRUE;
3051 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003052
3053 /* Get the start of the next line, so that words that wrap to the next
3054 * line are found too: "et<line-break>al.".
3055 * Trick: skip a few chars for C/shell/Vim comments */
3056 nextline[SPWORDLEN] = NUL;
3057 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3058 {
3059 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3060 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3061 }
3062
3063 /* When a word wrapped from the previous line the start of the current
3064 * line is valid. */
3065 if (lnum == checked_lnum)
3066 cur_checked_col = checked_col;
3067 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003068
3069 /* When there was a sentence end in the previous line may require a
3070 * word starting with capital in this line. In line 1 always check
3071 * the first word. */
3072 if (lnum != capcol_lnum)
3073 cap_col = -1;
3074 if (lnum == 1)
3075 cap_col = 0;
3076 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#endif
3079
3080 /*
3081 * handle visual active in this window
3082 */
3083 fromcol = -10;
3084 tocol = MAXCOL;
3085#ifdef FEAT_VISUAL
3086 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3087 {
3088 /* Visual is after curwin->w_cursor */
3089 if (ltoreq(curwin->w_cursor, VIsual))
3090 {
3091 top = &curwin->w_cursor;
3092 bot = &VIsual;
3093 }
3094 else /* Visual is before curwin->w_cursor */
3095 {
3096 top = &VIsual;
3097 bot = &curwin->w_cursor;
3098 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003099 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (VIsual_mode == Ctrl_V) /* block mode */
3101 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003102 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
3104 fromcol = wp->w_old_cursor_fcol;
3105 tocol = wp->w_old_cursor_lcol;
3106 }
3107 }
3108 else /* non-block mode */
3109 {
3110 if (lnum > top->lnum && lnum <= bot->lnum)
3111 fromcol = 0;
3112 else if (lnum == top->lnum)
3113 {
3114 if (VIsual_mode == 'V') /* linewise */
3115 fromcol = 0;
3116 else
3117 {
3118 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3119 if (gchar_pos(top) == NUL)
3120 tocol = fromcol + 1;
3121 }
3122 }
3123 if (VIsual_mode != 'V' && lnum == bot->lnum)
3124 {
3125 if (*p_sel == 'e' && bot->col == 0
3126#ifdef FEAT_VIRTUALEDIT
3127 && bot->coladd == 0
3128#endif
3129 )
3130 {
3131 fromcol = -10;
3132 tocol = MAXCOL;
3133 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003134 else if (bot->col == MAXCOL)
3135 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 else
3137 {
3138 pos = *bot;
3139 if (*p_sel == 'e')
3140 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3141 else
3142 {
3143 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3144 ++tocol;
3145 }
3146 }
3147 }
3148 }
3149
3150#ifndef MSDOS
3151 /* Check if the character under the cursor should not be inverted */
3152 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3153# ifdef FEAT_GUI
3154 && !gui.in_use
3155# endif
3156 )
3157 noinvcur = TRUE;
3158#endif
3159
3160 /* if inverting in this line set area_highlighting */
3161 if (fromcol >= 0)
3162 {
3163 area_highlighting = TRUE;
3164 attr = hl_attr(HLF_V);
3165#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003166 if ((clip_star.available && !clip_star.owned
3167 && clip_isautosel_star())
3168 || (clip_plus.available && !clip_plus.owned
3169 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 attr = hl_attr(HLF_VNC);
3171#endif
3172 }
3173 }
3174
3175 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003176 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 */
3178 else
3179#endif /* FEAT_VISUAL */
3180 if (highlight_match
3181 && wp == curwin
3182 && lnum >= curwin->w_cursor.lnum
3183 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3184 {
3185 if (lnum == curwin->w_cursor.lnum)
3186 getvcol(curwin, &(curwin->w_cursor),
3187 (colnr_T *)&fromcol, NULL, NULL);
3188 else
3189 fromcol = 0;
3190 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3191 {
3192 pos.lnum = lnum;
3193 pos.col = search_match_endcol;
3194 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3195 }
3196 else
3197 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003198 /* do at least one character; happens when past end of line */
3199 if (fromcol == tocol)
3200 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 area_highlighting = TRUE;
3202 attr = hl_attr(HLF_I);
3203 }
3204
3205#ifdef FEAT_DIFF
3206 filler_lines = diff_check(wp, lnum);
3207 if (filler_lines < 0)
3208 {
3209 if (filler_lines == -1)
3210 {
3211 if (diff_find_change(wp, lnum, &change_start, &change_end))
3212 diff_hlf = HLF_ADD; /* added line */
3213 else if (change_start == 0)
3214 diff_hlf = HLF_TXD; /* changed text */
3215 else
3216 diff_hlf = HLF_CHD; /* changed line */
3217 }
3218 else
3219 diff_hlf = HLF_ADD; /* added line */
3220 filler_lines = 0;
3221 area_highlighting = TRUE;
3222 }
3223 if (lnum == wp->w_topline)
3224 filler_lines = wp->w_topfill;
3225 filler_todo = filler_lines;
3226#endif
3227
3228#ifdef LINE_ATTR
3229# ifdef FEAT_SIGNS
3230 /* If this line has a sign with line highlighting set line_attr. */
3231 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3232 if (v != 0)
3233 line_attr = sign_get_attr((int)v, TRUE);
3234# endif
3235# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3236 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003237 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 line_attr = hl_attr(HLF_L);
3239# endif
3240 if (line_attr != 0)
3241 area_highlighting = TRUE;
3242#endif
3243
3244 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3245 ptr = line;
3246
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003247#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003248 if (has_spell)
3249 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003250 /* For checking first word with a capital skip white space. */
3251 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003252 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003253
Bram Moolenaar30abd282005-06-22 22:35:10 +00003254 /* To be able to spell-check over line boundaries copy the end of the
3255 * current line into nextline[]. Above the start of the next line was
3256 * copied to nextline[SPWORDLEN]. */
3257 if (nextline[SPWORDLEN] == NUL)
3258 {
3259 /* No next line or it is empty. */
3260 nextlinecol = MAXCOL;
3261 nextline_idx = 0;
3262 }
3263 else
3264 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003265 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003266 if (v < SPWORDLEN)
3267 {
3268 /* Short line, use it completely and append the start of the
3269 * next line. */
3270 nextlinecol = 0;
3271 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003272 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003273 nextline_idx = v + 1;
3274 }
3275 else
3276 {
3277 /* Long line, use only the last SPWORDLEN bytes. */
3278 nextlinecol = v - SPWORDLEN;
3279 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3280 nextline_idx = SPWORDLEN + 1;
3281 }
3282 }
3283 }
3284#endif
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 /* find start of trailing whitespace */
3287 if (wp->w_p_list && lcs_trail)
3288 {
3289 trailcol = (colnr_T)STRLEN(ptr);
3290 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3291 --trailcol;
3292 trailcol += (colnr_T) (ptr - line);
3293 extra_check = TRUE;
3294 }
3295
3296 /*
3297 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3298 * first character to be displayed.
3299 */
3300 if (wp->w_p_wrap)
3301 v = wp->w_skipcol;
3302 else
3303 v = wp->w_leftcol;
3304 if (v > 0)
3305 {
3306#ifdef FEAT_MBYTE
3307 char_u *prev_ptr = ptr;
3308#endif
3309 while (vcol < v && *ptr != NUL)
3310 {
3311 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3312 vcol += c;
3313#ifdef FEAT_MBYTE
3314 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003316 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003319#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3320 /* When:
3321 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003322 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003323 * - 'virtualedit' is set, or
3324 * - the visual mode is active,
3325 * the end of the line may be before the start of the displayed part.
3326 */
3327 if (vcol < v && (
3328# ifdef FEAT_SYN_HL
3329 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003330 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003331# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3332 ||
3333# endif
3334# endif
3335# ifdef FEAT_VIRTUALEDIT
3336 virtual_active()
3337# ifdef FEAT_VISUAL
3338 ||
3339# endif
3340# endif
3341# ifdef FEAT_VISUAL
3342 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3343# endif
3344 ))
3345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#endif
3349
3350 /* Handle a character that's not completely on the screen: Put ptr at
3351 * that character but skip the first few screen characters. */
3352 if (vcol > v)
3353 {
3354 vcol -= c;
3355#ifdef FEAT_MBYTE
3356 ptr = prev_ptr;
3357#else
3358 --ptr;
3359#endif
3360 n_skip = v - vcol;
3361 }
3362
3363 /*
3364 * Adjust for when the inverted text is before the screen,
3365 * and when the start of the inverted text is before the screen.
3366 */
3367 if (tocol <= vcol)
3368 fromcol = 0;
3369 else if (fromcol >= 0 && fromcol < vcol)
3370 fromcol = vcol;
3371
3372#ifdef FEAT_LINEBREAK
3373 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3374 if (wp->w_p_wrap)
3375 need_showbreak = TRUE;
3376#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003377#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003378 /* When spell checking a word we need to figure out the start of the
3379 * word and if it's badly spelled or not. */
3380 if (has_spell)
3381 {
3382 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003383 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003384 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003385
3386 pos = wp->w_cursor;
3387 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003388 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003390
3391 /* spell_move_to() may call ml_get() and make "line" invalid */
3392 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3393 ptr = line + linecol;
3394
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003395 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003396 {
3397 /* no bad word found at line start, don't check until end of a
3398 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003399 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003400 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003401 }
3402 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003403 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003404 /* bad word found, use attributes until end of word */
3405 word_end = wp->w_cursor.col + len + 1;
3406
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003407 /* Turn index into actual attributes. */
3408 if (spell_hlf != HLF_COUNT)
3409 spell_attr = highlight_attr[spell_hlf];
3410 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003411 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003412
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003413# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003414 /* Need to restart syntax highlighting for this line. */
3415 if (has_syntax)
3416 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003417# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003418 }
3419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421
3422 /*
3423 * Correct highlighting for cursor that can't be disabled.
3424 * Avoids having to check this for each character.
3425 */
3426 if (fromcol >= 0)
3427 {
3428 if (noinvcur)
3429 {
3430 if ((colnr_T)fromcol == wp->w_virtcol)
3431 {
3432 /* highlighting starts at cursor, let it start just after the
3433 * cursor */
3434 fromcol_prev = fromcol;
3435 fromcol = -1;
3436 }
3437 else if ((colnr_T)fromcol < wp->w_virtcol)
3438 /* restart highlighting after the cursor */
3439 fromcol_prev = wp->w_virtcol;
3440 }
3441 if (fromcol >= tocol)
3442 fromcol = -1;
3443 }
3444
3445#ifdef FEAT_SEARCH_EXTRA
3446 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003447 * Handle highlighting the last used search pattern and matches.
3448 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003450 cur = wp->w_match_head;
3451 shl_flag = FALSE;
3452 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003454 if (shl_flag == FALSE)
3455 {
3456 shl = &search_hl;
3457 shl_flag = TRUE;
3458 }
3459 else
3460 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003461 shl->startcol = MAXCOL;
3462 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 shl->attr_cur = 0;
3464 if (shl->rm.regprog != NULL)
3465 {
3466 v = (long)(ptr - line);
3467 next_search_hl(wp, shl, lnum, (colnr_T)v);
3468
3469 /* Need to get the line again, a multi-line regexp may have made it
3470 * invalid. */
3471 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3472 ptr = line + v;
3473
3474 if (shl->lnum != 0 && shl->lnum <= lnum)
3475 {
3476 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003477 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003479 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3481 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003482 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003484 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003486 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003489 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003490 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else
3492#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003493 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003495 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 shl->attr_cur = shl->attr;
3498 search_attr = shl->attr;
3499 }
3500 area_highlighting = TRUE;
3501 }
3502 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003503 if (shl != &search_hl && cur != NULL)
3504 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506#endif
3507
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003508#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003509 /* Cursor line highlighting for 'cursorline' in the current window. Not
3510 * when Visual mode is active, because it's not clear what is selected
3511 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003512 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3513 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003514 {
3515 line_attr = hl_attr(HLF_CUL);
3516 area_highlighting = TRUE;
3517 }
3518#endif
3519
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003520 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 col = 0;
3522#ifdef FEAT_RIGHTLEFT
3523 if (wp->w_p_rl)
3524 {
3525 /* Rightleft window: process the text in the normal direction, but put
3526 * it in current_ScreenLine[] from right to left. Start at the
3527 * rightmost column of the window. */
3528 col = W_WIDTH(wp) - 1;
3529 off += col;
3530 }
3531#endif
3532
3533 /*
3534 * Repeat for the whole displayed line.
3535 */
3536 for (;;)
3537 {
3538 /* Skip this quickly when working on the text. */
3539 if (draw_state != WL_LINE)
3540 {
3541#ifdef FEAT_CMDWIN
3542 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3543 {
3544 draw_state = WL_CMDLINE;
3545 if (cmdwin_type != 0 && wp == curwin)
3546 {
3547 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003549 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 char_attr = hl_attr(HLF_AT);
3551 }
3552 }
3553#endif
3554
3555#ifdef FEAT_FOLDING
3556 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3557 {
3558 draw_state = WL_FOLD;
3559 if (wp->w_p_fdc > 0)
3560 {
3561 /* Draw the 'foldcolumn'. */
3562 fill_foldcolumn(extra, wp, FALSE, lnum);
3563 n_extra = wp->w_p_fdc;
3564 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003565 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 c_extra = NUL;
3567 char_attr = hl_attr(HLF_FC);
3568 }
3569 }
3570#endif
3571
3572#ifdef FEAT_SIGNS
3573 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3574 {
3575 draw_state = WL_SIGN;
3576 /* Show the sign column when there are any signs in this
3577 * buffer or when using Netbeans. */
3578 if (draw_signcolumn(wp)
3579# ifdef FEAT_DIFF
3580 && filler_todo <= 0
3581# endif
3582 )
3583 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003584 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003586 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587# endif
3588
3589 /* Draw two cells with the sign value or blank. */
3590 c_extra = ' ';
3591 char_attr = hl_attr(HLF_SC);
3592 n_extra = 2;
3593
3594 if (row == startrow)
3595 {
3596 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3597 SIGN_TEXT);
3598# ifdef FEAT_SIGN_ICONS
3599 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3600 SIGN_ICON);
3601 if (gui.in_use && icon_sign != 0)
3602 {
3603 /* Use the image in this position. */
3604 c_extra = SIGN_BYTE;
3605# ifdef FEAT_NETBEANS_INTG
3606 if (buf_signcount(wp->w_buffer, lnum) > 1)
3607 c_extra = MULTISIGN_BYTE;
3608# endif
3609 char_attr = icon_sign;
3610 }
3611 else
3612# endif
3613 if (text_sign != 0)
3614 {
3615 p_extra = sign_get_text(text_sign);
3616 if (p_extra != NULL)
3617 {
3618 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003619 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621 char_attr = sign_get_attr(text_sign, FALSE);
3622 }
3623 }
3624 }
3625 }
3626#endif
3627
3628 if (draw_state == WL_NR - 1 && n_extra == 0)
3629 {
3630 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003631 /* Display the absolute or relative line number. After the
3632 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3633 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 && (row == startrow
3635#ifdef FEAT_DIFF
3636 + filler_lines
3637#endif
3638 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3639 {
3640 /* Draw the line number (empty space after wrapping). */
3641 if (row == startrow
3642#ifdef FEAT_DIFF
3643 + filler_lines
3644#endif
3645 )
3646 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003647 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003648 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003649
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003650 if (wp->w_p_nu && !wp->w_p_rnu)
3651 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003652 num = (long)lnum;
3653 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003654 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003655 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003656 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003657 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003658 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003659 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003660 num = lnum;
3661 fmt = "%-*ld ";
3662 }
3663 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003664
Bram Moolenaar700e7342013-01-30 12:31:36 +01003665 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003666 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (wp->w_skipcol > 0)
3668 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3669 *p_extra = '-';
3670#ifdef FEAT_RIGHTLEFT
3671 if (wp->w_p_rl) /* reverse line numbers */
3672 rl_mirror(extra);
3673#endif
3674 p_extra = extra;
3675 c_extra = NUL;
3676 }
3677 else
3678 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003679 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003681#ifdef FEAT_SYN_HL
3682 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003683 * the current line differently.
3684 * TODO: Can we use CursorLine instead of CursorLineNr
3685 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003686 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003687 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003688 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
3692
3693#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3694 if (draw_state == WL_SBR - 1 && n_extra == 0)
3695 {
3696 draw_state = WL_SBR;
3697# ifdef FEAT_DIFF
3698 if (filler_todo > 0)
3699 {
3700 /* Draw "deleted" diff line(s). */
3701 if (char2cells(fill_diff) > 1)
3702 c_extra = '-';
3703 else
3704 c_extra = fill_diff;
3705# ifdef FEAT_RIGHTLEFT
3706 if (wp->w_p_rl)
3707 n_extra = col + 1;
3708 else
3709# endif
3710 n_extra = W_WIDTH(wp) - col;
3711 char_attr = hl_attr(HLF_DED);
3712 }
3713# endif
3714# ifdef FEAT_LINEBREAK
3715 if (*p_sbr != NUL && need_showbreak)
3716 {
3717 /* Draw 'showbreak' at the start of each broken line. */
3718 p_extra = p_sbr;
3719 c_extra = NUL;
3720 n_extra = (int)STRLEN(p_sbr);
3721 char_attr = hl_attr(HLF_AT);
3722 need_showbreak = FALSE;
3723 /* Correct end of highlighted area for 'showbreak',
3724 * required when 'linebreak' is also set. */
3725 if (tocol == vcol)
3726 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003727#ifdef FEAT_SYN_HL
3728 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003729 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003730 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733# endif
3734 }
3735#endif
3736
3737 if (draw_state == WL_LINE - 1 && n_extra == 0)
3738 {
3739 draw_state = WL_LINE;
3740 if (saved_n_extra)
3741 {
3742 /* Continue item from end of wrapped line. */
3743 n_extra = saved_n_extra;
3744 c_extra = saved_c_extra;
3745 p_extra = saved_p_extra;
3746 char_attr = saved_char_attr;
3747 }
3748 else
3749 char_attr = 0;
3750 }
3751 }
3752
3753 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003754 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003755 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef FEAT_DIFF
3757 && filler_todo <= 0
3758#endif
3759 )
3760 {
3761 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3762 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763 /* Pretend we have finished updating the window. Except when
3764 * 'cursorcolumn' is set. */
3765#ifdef FEAT_SYN_HL
3766 if (wp->w_p_cuc)
3767 row = wp->w_cline_row + wp->w_cline_height;
3768 else
3769#endif
3770 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 break;
3772 }
3773
3774 if (draw_state == WL_LINE && area_highlighting)
3775 {
3776 /* handle Visual or match highlighting in this line */
3777 if (vcol == fromcol
3778#ifdef FEAT_MBYTE
3779 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3780 && (*mb_ptr2cells)(ptr) > 1)
3781#endif
3782 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003783 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 && vcol < tocol))
3785 area_attr = attr; /* start highlighting */
3786 else if (area_attr != 0
3787 && (vcol == tocol
3788 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791#ifdef FEAT_SEARCH_EXTRA
3792 if (!n_extra)
3793 {
3794 /*
3795 * Check for start/end of search pattern match.
3796 * After end, check for start/end of next match.
3797 * When another match, have to check for start again.
3798 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003799 * Do this for 'search_hl' and the match list (ordered by
3800 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003802 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003803 cur = wp->w_match_head;
3804 shl_flag = FALSE;
3805 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003807 if (shl_flag == FALSE
3808 && ((cur != NULL
3809 && cur->priority > SEARCH_HL_PRIORITY)
3810 || cur == NULL))
3811 {
3812 shl = &search_hl;
3813 shl_flag = TRUE;
3814 }
3815 else
3816 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 while (shl->rm.regprog != NULL)
3818 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003819 if (shl->startcol != MAXCOL
3820 && v >= (long)shl->startcol
3821 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
3823 shl->attr_cur = shl->attr;
3824 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003825 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
3827 shl->attr_cur = 0;
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 next_search_hl(wp, shl, lnum, (colnr_T)v);
3830
3831 /* Need to get the line again, a multi-line regexp
3832 * may have made it invalid. */
3833 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3834 ptr = line + v;
3835
3836 if (shl->lnum == lnum)
3837 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003838 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003840 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003842 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003844 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 /* highlight empty match, try again after
3847 * it */
3848#ifdef FEAT_MBYTE
3849 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003850 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003851 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 else
3853#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003854 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856
3857 /* Loop to check if the match starts at the
3858 * current position */
3859 continue;
3860 }
3861 }
3862 break;
3863 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003864 if (shl != &search_hl && cur != NULL)
3865 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003867
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003868 /* Use attributes from match with highest priority among
3869 * 'search_hl' and the match list. */
3870 search_attr = search_hl.attr_cur;
3871 cur = wp->w_match_head;
3872 shl_flag = FALSE;
3873 while (cur != NULL || shl_flag == FALSE)
3874 {
3875 if (shl_flag == FALSE
3876 && ((cur != NULL
3877 && cur->priority > SEARCH_HL_PRIORITY)
3878 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003879 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 shl = &search_hl;
3881 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003882 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003883 else
3884 shl = &cur->hl;
3885 if (shl->attr_cur != 0)
3886 search_attr = shl->attr_cur;
3887 if (shl != &search_hl && cur != NULL)
3888 cur = cur->next;
3889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891#endif
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003894 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003896 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3897 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003899 if (diff_hlf == HLF_TXD && ptr - line > change_end
3900 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003902 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003905
3906 /* Decide which of the highlight attributes to use. */
3907 attr_pri = TRUE;
3908 if (area_attr != 0)
3909 char_attr = area_attr;
3910 else if (search_attr != 0)
3911 char_attr = search_attr;
3912#ifdef LINE_ATTR
3913 /* Use line_attr when not in the Visual or 'incsearch' area
3914 * (area_attr may be 0 when "noinvcur" is set). */
3915 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003916 || vcol < fromcol || vcol_prev < fromcol_prev
3917 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003918 char_attr = line_attr;
3919#endif
3920 else
3921 {
3922 attr_pri = FALSE;
3923#ifdef FEAT_SYN_HL
3924 if (has_syntax)
3925 char_attr = syntax_attr;
3926 else
3927#endif
3928 char_attr = 0;
3929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931
3932 /*
3933 * Get the next character to put on the screen.
3934 */
3935 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003936 * The "p_extra" points to the extra stuff that is inserted to
3937 * represent special characters (non-printable stuff) and other
3938 * things. When all characters are the same, c_extra is used.
3939 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3940 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3942 */
3943 if (n_extra > 0)
3944 {
3945 if (c_extra != NUL)
3946 {
3947 c = c_extra;
3948#ifdef FEAT_MBYTE
3949 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3950 if (enc_utf8 && (*mb_char2len)(c) > 1)
3951 {
3952 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003953 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003954 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 else
3957 mb_utf8 = FALSE;
3958#endif
3959 }
3960 else
3961 {
3962 c = *p_extra;
3963#ifdef FEAT_MBYTE
3964 if (has_mbyte)
3965 {
3966 mb_c = c;
3967 if (enc_utf8)
3968 {
3969 /* If the UTF-8 character is more than one byte:
3970 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003971 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 mb_utf8 = FALSE;
3973 if (mb_l > n_extra)
3974 mb_l = 1;
3975 else if (mb_l > 1)
3976 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003977 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003979 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982 else
3983 {
3984 /* if this is a DBCS character, put it in "mb_c" */
3985 mb_l = MB_BYTE2LEN(c);
3986 if (mb_l >= n_extra)
3987 mb_l = 1;
3988 else if (mb_l > 1)
3989 mb_c = (c << 8) + p_extra[1];
3990 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003991 if (mb_l == 0) /* at the NUL at end-of-line */
3992 mb_l = 1;
3993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 /* If a double-width char doesn't fit display a '>' in the
3995 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003996 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997# ifdef FEAT_RIGHTLEFT
3998 wp->w_p_rl ? (col <= 0) :
3999# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004000 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 && (*mb_char2cells)(mb_c) == 2)
4002 {
4003 c = '>';
4004 mb_c = c;
4005 mb_l = 1;
4006 mb_utf8 = FALSE;
4007 multi_attr = hl_attr(HLF_AT);
4008 /* put the pointer back to output the double-width
4009 * character at the start of the next line. */
4010 ++n_extra;
4011 --p_extra;
4012 }
4013 else
4014 {
4015 n_extra -= mb_l - 1;
4016 p_extra += mb_l - 1;
4017 }
4018 }
4019#endif
4020 ++p_extra;
4021 }
4022 --n_extra;
4023 }
4024 else
4025 {
4026 /*
4027 * Get a character from the line itself.
4028 */
4029 c = *ptr;
4030#ifdef FEAT_MBYTE
4031 if (has_mbyte)
4032 {
4033 mb_c = c;
4034 if (enc_utf8)
4035 {
4036 /* If the UTF-8 character is more than one byte: Decode it
4037 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004038 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 mb_utf8 = FALSE;
4040 if (mb_l > 1)
4041 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004042 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /* Overlong encoded ASCII or ASCII with composing char
4044 * is displayed normally, except a NUL. */
4045 if (mb_c < 0x80)
4046 c = mb_c;
4047 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004048
4049 /* At start of the line we can have a composing char.
4050 * Draw it as a space with a composing char. */
4051 if (utf_iscomposing(mb_c))
4052 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004053 int i;
4054
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004055 for (i = Screen_mco - 1; i > 0; --i)
4056 u8cc[i] = u8cc[i - 1];
4057 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004058 mb_c = ' ';
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061
4062 if ((mb_l == 1 && c >= 0x80)
4063 || (mb_l >= 1 && mb_c == 0)
4064 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004065# ifdef UNICODE16
4066 || mb_c >= 0x10000
4067# endif
4068 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 /*
4071 * Illegal UTF-8 byte: display as <xx>.
4072 * Non-BMP character : display as ? or fullwidth ?.
4073 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004074# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004079# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if (wp->w_p_rl) /* reverse */
4081 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004084# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 else if (utf_char2cells(mb_c) != 2)
4086 STRCPY(extra, "?");
4087 else
4088 /* 0xff1f in UTF-8: full-width '?' */
4089 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092 p_extra = extra;
4093 c = *p_extra;
4094 mb_c = mb_ptr2char_adv(&p_extra);
4095 mb_utf8 = (c >= 0x80);
4096 n_extra = (int)STRLEN(p_extra);
4097 c_extra = NUL;
4098 if (area_attr == 0 && search_attr == 0)
4099 {
4100 n_attr = n_extra + 1;
4101 extra_attr = hl_attr(HLF_8);
4102 saved_attr2 = char_attr; /* save current attr */
4103 }
4104 }
4105 else if (mb_l == 0) /* at the NUL at end-of-line */
4106 mb_l = 1;
4107#ifdef FEAT_ARABIC
4108 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4109 {
4110 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004111 int pc, pc1, nc;
4112 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 /* The idea of what is the previous and next
4115 * character depends on 'rightleft'. */
4116 if (wp->w_p_rl)
4117 {
4118 pc = prev_c;
4119 pc1 = prev_c1;
4120 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004121 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 else
4124 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004125 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004127 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 prev_c = mb_c;
4130
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004131 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 else
4134 prev_c = mb_c;
4135#endif
4136 }
4137 else /* enc_dbcs */
4138 {
4139 mb_l = MB_BYTE2LEN(c);
4140 if (mb_l == 0) /* at the NUL at end-of-line */
4141 mb_l = 1;
4142 else if (mb_l > 1)
4143 {
4144 /* We assume a second byte below 32 is illegal.
4145 * Hopefully this is OK for all double-byte encodings!
4146 */
4147 if (ptr[1] >= 32)
4148 mb_c = (c << 8) + ptr[1];
4149 else
4150 {
4151 if (ptr[1] == NUL)
4152 {
4153 /* head byte at end of line */
4154 mb_l = 1;
4155 transchar_nonprint(extra, c);
4156 }
4157 else
4158 {
4159 /* illegal tail byte */
4160 mb_l = 2;
4161 STRCPY(extra, "XX");
4162 }
4163 p_extra = extra;
4164 n_extra = (int)STRLEN(extra) - 1;
4165 c_extra = NUL;
4166 c = *p_extra++;
4167 if (area_attr == 0 && search_attr == 0)
4168 {
4169 n_attr = n_extra + 1;
4170 extra_attr = hl_attr(HLF_8);
4171 saved_attr2 = char_attr; /* save current attr */
4172 }
4173 mb_c = c;
4174 }
4175 }
4176 }
4177 /* If a double-width char doesn't fit display a '>' in the
4178 * last column; the character is displayed at the start of the
4179 * next line. */
4180 if ((
4181# ifdef FEAT_RIGHTLEFT
4182 wp->w_p_rl ? (col <= 0) :
4183# endif
4184 (col >= W_WIDTH(wp) - 1))
4185 && (*mb_char2cells)(mb_c) == 2)
4186 {
4187 c = '>';
4188 mb_c = c;
4189 mb_utf8 = FALSE;
4190 mb_l = 1;
4191 multi_attr = hl_attr(HLF_AT);
4192 /* Put pointer back so that the character will be
4193 * displayed at the start of the next line. */
4194 --ptr;
4195 }
4196 else if (*ptr != NUL)
4197 ptr += mb_l - 1;
4198
4199 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004200 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004201 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004202 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004205 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 c = ' ';
4207 if (area_attr == 0 && search_attr == 0)
4208 {
4209 n_attr = n_extra + 1;
4210 extra_attr = hl_attr(HLF_AT);
4211 saved_attr2 = char_attr; /* save current attr */
4212 }
4213 mb_c = c;
4214 mb_utf8 = FALSE;
4215 mb_l = 1;
4216 }
4217
4218 }
4219#endif
4220 ++ptr;
4221
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004222 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004223 if (wp->w_p_list && (c == 160
4224#ifdef FEAT_MBYTE
4225 || (mb_utf8 && mb_c == 160)
4226#endif
4227 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228 {
4229 c = lcs_nbsp;
4230 if (area_attr == 0 && search_attr == 0)
4231 {
4232 n_attr = 1;
4233 extra_attr = hl_attr(HLF_8);
4234 saved_attr2 = char_attr; /* save current attr */
4235 }
4236#ifdef FEAT_MBYTE
4237 mb_c = c;
4238 if (enc_utf8 && (*mb_char2len)(c) > 1)
4239 {
4240 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004241 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004242 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004243 }
4244 else
4245 mb_utf8 = FALSE;
4246#endif
4247 }
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (extra_check)
4250 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004251#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004252 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004253#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004254
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004255#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* Get syntax attribute, unless still at the start of the line
4257 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004258 v = (long)(ptr - line);
4259 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
4261 /* Get the syntax attribute for the character. If there
4262 * is an error, disable syntax highlighting. */
4263 save_did_emsg = did_emsg;
4264 did_emsg = FALSE;
4265
Bram Moolenaar217ad922005-03-20 22:37:15 +00004266 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004267# ifdef FEAT_SPELL
4268 has_spell ? &can_spell :
4269# endif
4270 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004273 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004274 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004275 has_syntax = FALSE;
4276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 else
4278 did_emsg = save_did_emsg;
4279
4280 /* Need to get the line again, a multi-line regexp may
4281 * have made it invalid. */
4282 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4283 ptr = line + v;
4284
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004285 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004287 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004288 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004289# ifdef FEAT_CONCEAL
4290 /* no concealing past the end of the line, it interferes
4291 * with line highlighting */
4292 if (c == NUL)
4293 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004294 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004295 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004298#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004299
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004300#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004301 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004302 * Only do this when there is no syntax highlighting, the
4303 * @Spell cluster is not used or the current syntax item
4304 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004305 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004306 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004307 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004308# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004309 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004310 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004311# endif
4312 if (c != 0 && (
4313# ifdef FEAT_SYN_HL
4314 !has_syntax ||
4315# endif
4316 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004317 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004318 char_u *prev_ptr, *p;
4319 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004320 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004321# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004322 if (has_mbyte)
4323 {
4324 prev_ptr = ptr - mb_l;
4325 v -= mb_l - 1;
4326 }
4327 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004328# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004329 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004330
4331 /* Use nextline[] if possible, it has the start of the
4332 * next line concatenated. */
4333 if ((prev_ptr - line) - nextlinecol >= 0)
4334 p = nextline + (prev_ptr - line) - nextlinecol;
4335 else
4336 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004337 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004338 len = spell_check(wp, p, &spell_hlf, &cap_col,
4339 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004340 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004341
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004342 /* In Insert mode only highlight a word that
4343 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004344 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004345 && (State & INSERT) != 0
4346 && wp->w_cursor.lnum == lnum
4347 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004348 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004349 && wp->w_cursor.col < (colnr_T)word_end)
4350 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004351 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004352 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004353 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004354
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004355 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004356 && (p - nextline) + len > nextline_idx)
4357 {
4358 /* Remember that the good word continues at the
4359 * start of the next line. */
4360 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004361 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004362 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004363
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004364 /* Turn index into actual attributes. */
4365 if (spell_hlf != HLF_COUNT)
4366 spell_attr = highlight_attr[spell_hlf];
4367
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004368 if (cap_col > 0)
4369 {
4370 if (p != prev_ptr
4371 && (p - nextline) + cap_col >= nextline_idx)
4372 {
4373 /* Remember that the word in the next line
4374 * must start with a capital. */
4375 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004376 cap_col = (int)((p - nextline) + cap_col
4377 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004378 }
4379 else
4380 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004381 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004382 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004383 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004384 }
4385 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004386 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004387 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004388 char_attr = hl_combine_attr(char_attr, spell_attr);
4389 else
4390 char_attr = hl_combine_attr(spell_attr, char_attr);
4391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#endif
4393#ifdef FEAT_LINEBREAK
4394 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004395 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
4397 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004398 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 n_extra = win_lbr_chartabsize(wp, ptr - (
4401# ifdef FEAT_MBYTE
4402 has_mbyte ? mb_l :
4403# endif
4404 1), (colnr_T)vcol, NULL) - 1;
4405 c_extra = ' ';
4406 if (vim_iswhite(c))
4407 c = ' ';
4408 }
4409#endif
4410
4411 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4412 {
4413 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004414 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 n_attr = 1;
4417 extra_attr = hl_attr(HLF_8);
4418 saved_attr2 = char_attr; /* save current attr */
4419 }
4420#ifdef FEAT_MBYTE
4421 mb_c = c;
4422 if (enc_utf8 && (*mb_char2len)(c) > 1)
4423 {
4424 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004425 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004426 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 else
4429 mb_utf8 = FALSE;
4430#endif
4431 }
4432 }
4433
4434 /*
4435 * Handling of non-printable characters.
4436 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004437 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 /*
4440 * when getting a character from the file, we may have to
4441 * turn it into something else on the way to putting it
4442 * into "ScreenLines".
4443 */
4444 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4445 {
4446 /* tab amount depends on current column */
4447 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004448 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4449#ifdef FEAT_CONCEAL
4450 /* Tab alignment should be identical regardless of
4451 * 'conceallevel' value. So tab compensates of all
4452 * previous concealed characters, and thus resets vcol_off
4453 * and boguscols accumulated so far in the line. Note that
4454 * the tab can be longer than 'tabstop' when there
4455 * are concealed characters. */
4456 n_extra += vcol_off;
4457 vcol -= vcol_off;
4458 vcol_off = 0;
4459 col -= boguscols;
4460 boguscols = 0;
4461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_MBYTE
4463 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4464#endif
4465 if (wp->w_p_list)
4466 {
4467 c = lcs_tab1;
4468 c_extra = lcs_tab2;
4469 n_attr = n_extra + 1;
4470 extra_attr = hl_attr(HLF_8);
4471 saved_attr2 = char_attr; /* save current attr */
4472#ifdef FEAT_MBYTE
4473 mb_c = c;
4474 if (enc_utf8 && (*mb_char2len)(c) > 1)
4475 {
4476 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004477 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004478 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480#endif
4481 }
4482 else
4483 {
4484 c_extra = ' ';
4485 c = ' ';
4486 }
4487 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004488 else if (c == NUL
4489 && ((wp->w_p_list && lcs_eol > 0)
4490 || ((fromcol >= 0 || fromcol_prev >= 0)
4491 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004492#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004493 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004494#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004495 && (
4496# ifdef FEAT_RIGHTLEFT
4497 wp->w_p_rl ? (col >= 0) :
4498# endif
4499 (col < W_WIDTH(wp)))
4500 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004501 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004502 && (colnr_T)vcol == wp->w_virtcol)))
4503 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004505 /* Display a '$' after the line or highlight an extra
4506 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4508 /* For a diff line the highlighting continues after the
4509 * "$". */
4510 if (
4511# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004512 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513# ifdef LINE_ATTR
4514 &&
4515# endif
4516# endif
4517# ifdef LINE_ATTR
4518 line_attr == 0
4519# endif
4520 )
4521#endif
4522 {
4523#ifdef FEAT_VIRTUALEDIT
4524 /* In virtualedit, visual selections may extend
4525 * beyond end of line. */
4526 if (area_highlighting && virtual_active()
4527 && tocol != MAXCOL && vcol < tocol)
4528 n_extra = 0;
4529 else
4530#endif
4531 {
4532 p_extra = at_end_str;
4533 n_extra = 1;
4534 c_extra = NUL;
4535 }
4536 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004537 if (wp->w_p_list)
4538 c = lcs_eol;
4539 else
4540 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 lcs_eol_one = -1;
4542 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004543 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 extra_attr = hl_attr(HLF_AT);
4546 n_attr = 1;
4547 }
4548#ifdef FEAT_MBYTE
4549 mb_c = c;
4550 if (enc_utf8 && (*mb_char2len)(c) > 1)
4551 {
4552 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004554 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 else
4557 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4558#endif
4559 }
4560 else if (c != NUL)
4561 {
4562 p_extra = transchar(c);
4563#ifdef FEAT_RIGHTLEFT
4564 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4565 rl_mirror(p_extra); /* reverse "<12>" */
4566#endif
4567 n_extra = byte2cells(c) - 1;
4568 c_extra = NUL;
4569 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004570 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {
4572 n_attr = n_extra + 1;
4573 extra_attr = hl_attr(HLF_8);
4574 saved_attr2 = char_attr; /* save current attr */
4575 }
4576#ifdef FEAT_MBYTE
4577 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4578#endif
4579 }
4580#ifdef FEAT_VIRTUALEDIT
4581 else if (VIsual_active
4582 && (VIsual_mode == Ctrl_V
4583 || VIsual_mode == 'v')
4584 && virtual_active()
4585 && tocol != MAXCOL
4586 && vcol < tocol
4587 && (
4588# ifdef FEAT_RIGHTLEFT
4589 wp->w_p_rl ? (col >= 0) :
4590# endif
4591 (col < W_WIDTH(wp))))
4592 {
4593 c = ' ';
4594 --ptr; /* put it back at the NUL */
4595 }
4596#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004597#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else if ((
4599# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004600 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 ) && (
4604# ifdef FEAT_RIGHTLEFT
4605 wp->w_p_rl ? (col >= 0) :
4606# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004607 (col
4608# ifdef FEAT_CONCEAL
4609 - boguscols
4610# endif
4611 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 /* Highlight until the right side of the window */
4614 c = ' ';
4615 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004616
4617 /* Remember we do the char for line highlighting. */
4618 ++did_line_attr;
4619
4620 /* don't do search HL for the rest of the line */
4621 if (line_attr != 0 && char_attr == search_attr && col > 0)
4622 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623# ifdef FEAT_DIFF
4624 if (diff_hlf == HLF_TXD)
4625 {
4626 diff_hlf = HLF_CHD;
4627 if (attr == 0 || char_attr != attr)
4628 char_attr = hl_attr(diff_hlf);
4629 }
4630# endif
4631 }
4632#endif
4633 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004634
4635#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004636 if ( wp->w_p_cole > 0
4637 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4638 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004639 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004640 && !(lnum_in_visual_area
4641 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004642 {
4643 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004644 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004645 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4646 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004647 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004648 /* First time at this concealed item: display one
4649 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004650 if (syn_get_sub_char() != NUL)
4651 c = syn_get_sub_char();
4652 else if (lcs_conceal != NUL)
4653 c = lcs_conceal;
4654 else
4655 c = ' ';
4656
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004657 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004658
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004659 if (n_extra > 0)
4660 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004661 vcol += n_extra;
4662 if (wp->w_p_wrap && n_extra > 0)
4663 {
4664# ifdef FEAT_RIGHTLEFT
4665 if (wp->w_p_rl)
4666 {
4667 col -= n_extra;
4668 boguscols -= n_extra;
4669 }
4670 else
4671# endif
4672 {
4673 boguscols += n_extra;
4674 col += n_extra;
4675 }
4676 }
4677 n_extra = 0;
4678 n_attr = 0;
4679 }
4680 else if (n_skip == 0)
4681 {
4682 is_concealing = TRUE;
4683 n_skip = 1;
4684 }
4685# ifdef FEAT_MBYTE
4686 mb_c = c;
4687 if (enc_utf8 && (*mb_char2len)(c) > 1)
4688 {
4689 mb_utf8 = TRUE;
4690 u8cc[0] = 0;
4691 c = 0xc0;
4692 }
4693 else
4694 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4695# endif
4696 }
4697 else
4698 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004699 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004700 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004701 }
4702#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004705#ifdef FEAT_CONCEAL
4706 /* In the cursor line and we may be concealing characters: correct
4707 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004708 if (!did_wcol && draw_state == WL_LINE
4709 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004710 && conceal_cursor_line(wp)
4711 && (int)wp->w_virtcol <= vcol + n_skip)
4712 {
4713 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004714 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004715 did_wcol = TRUE;
4716 }
4717#endif
4718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 /* Don't override visual selection highlighting. */
4720 if (n_attr > 0
4721 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004722 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 char_attr = extra_attr;
4724
Bram Moolenaar81695252004-12-29 20:58:21 +00004725#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /* XIM don't send preedit_start and preedit_end, but they send
4727 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4728 * im_is_preediting() here. */
4729 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004730 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 && (State & INSERT)
4732 && !p_imdisable
4733 && im_is_preediting()
4734 && draw_state == WL_LINE)
4735 {
4736 colnr_T tcol;
4737
4738 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004739 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
4741 tcol = preedit_end_col;
4742 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4743 {
4744 if (feedback_old_attr < 0)
4745 {
4746 feedback_col = 0;
4747 feedback_old_attr = char_attr;
4748 }
4749 char_attr = im_get_feedback_attr(feedback_col);
4750 if (char_attr < 0)
4751 char_attr = feedback_old_attr;
4752 feedback_col++;
4753 }
4754 else if (feedback_old_attr >= 0)
4755 {
4756 char_attr = feedback_old_attr;
4757 feedback_old_attr = -1;
4758 feedback_col = 0;
4759 }
4760 }
4761#endif
4762 /*
4763 * Handle the case where we are in column 0 but not on the first
4764 * character of the line and the user wants us to show us a
4765 * special character (via 'listchars' option "precedes:<char>".
4766 */
4767 if (lcs_prec_todo != NUL
4768 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4769#ifdef FEAT_DIFF
4770 && filler_todo <= 0
4771#endif
4772 && draw_state > WL_NR
4773 && c != NUL)
4774 {
4775 c = lcs_prec;
4776 lcs_prec_todo = NUL;
4777#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004778 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4779 {
4780 /* Double-width character being overwritten by the "precedes"
4781 * character, need to fill up half the character. */
4782 c_extra = MB_FILLER_CHAR;
4783 n_extra = 1;
4784 n_attr = 2;
4785 extra_attr = hl_attr(HLF_AT);
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 mb_c = c;
4788 if (enc_utf8 && (*mb_char2len)(c) > 1)
4789 {
4790 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004792 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794 else
4795 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4796#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004797 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 saved_attr3 = char_attr; /* save current attr */
4800 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4801 n_attr3 = 1;
4802 }
4803 }
4804
4805 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004806 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004808 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004809#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004810 || did_line_attr == 1
4811#endif
4812 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004814#ifdef FEAT_SEARCH_EXTRA
4815 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004816
4817 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004818 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004819 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004820#endif
4821
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004822 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 * highlight match at end of line. If it's beyond the last
4824 * char on the screen, just overwrite that one (tricky!) Not
4825 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004826#ifdef FEAT_SEARCH_EXTRA
4827 prevcol_hl_flag = FALSE;
4828 if (prevcol == (long)search_hl.startcol)
4829 prevcol_hl_flag = TRUE;
4830 else
4831 {
4832 cur = wp->w_match_head;
4833 while (cur != NULL)
4834 {
4835 if (prevcol == (long)cur->hl.startcol)
4836 {
4837 prevcol_hl_flag = TRUE;
4838 break;
4839 }
4840 cur = cur->next;
4841 }
4842 }
4843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004845 && ((area_attr != 0 && vcol == fromcol
4846#ifdef FEAT_VISUAL
4847 && (VIsual_mode != Ctrl_V
4848 || lnum == VIsual.lnum
4849 || lnum == curwin->w_cursor.lnum)
4850#endif
4851 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#ifdef FEAT_SEARCH_EXTRA
4853 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004854 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004855# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004856 && did_line_attr <= 1
4857# endif
4858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
4860 ))
4861 {
4862 int n = 0;
4863
4864#ifdef FEAT_RIGHTLEFT
4865 if (wp->w_p_rl)
4866 {
4867 if (col < 0)
4868 n = 1;
4869 }
4870 else
4871#endif
4872 {
4873 if (col >= W_WIDTH(wp))
4874 n = -1;
4875 }
4876 if (n != 0)
4877 {
4878 /* At the window boundary, highlight the last character
4879 * instead (better than nothing). */
4880 off += n;
4881 col += n;
4882 }
4883 else
4884 {
4885 /* Add a blank character to highlight. */
4886 ScreenLines[off] = ' ';
4887#ifdef FEAT_MBYTE
4888 if (enc_utf8)
4889 ScreenLinesUC[off] = 0;
4890#endif
4891 }
4892#ifdef FEAT_SEARCH_EXTRA
4893 if (area_attr == 0)
4894 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004895 /* Use attributes from match with highest priority among
4896 * 'search_hl' and the match list. */
4897 char_attr = search_hl.attr;
4898 cur = wp->w_match_head;
4899 shl_flag = FALSE;
4900 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004901 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004902 if (shl_flag == FALSE
4903 && ((cur != NULL
4904 && cur->priority > SEARCH_HL_PRIORITY)
4905 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004906 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004907 shl = &search_hl;
4908 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004909 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004910 else
4911 shl = &cur->hl;
4912 if ((ptr - line) - 1 == (long)shl->startcol)
4913 char_attr = shl->attr;
4914 if (shl != &search_hl && cur != NULL)
4915 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918#endif
4919 ScreenAttrs[off] = char_attr;
4920#ifdef FEAT_RIGHTLEFT
4921 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004924 --off;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
4927#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004930 ++off;
4931 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004932 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004933#ifdef FEAT_SYN_HL
4934 eol_hl_off = 1;
4935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
Bram Moolenaar91170f82006-05-05 21:15:17 +00004939 /*
4940 * At end of the text line.
4941 */
4942 if (c == NUL)
4943 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004944#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004945 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4946 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004947 {
4948 /* highlight last char after line */
4949 --col;
4950 --off;
4951 --vcol;
4952 }
4953
Bram Moolenaar1a384422010-07-14 19:53:30 +02004954 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004955 if (wp->w_p_wrap)
4956 v = wp->w_skipcol;
4957 else
4958 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004959
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004960 /* check if line ends before left margin */
4961 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004962 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004963#ifdef FEAT_CONCEAL
4964 /* Get rid of the boguscols now, we want to draw until the right
4965 * edge for 'cursorcolumn'. */
4966 col -= boguscols;
4967 boguscols = 0;
4968#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004969
4970 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004971 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004972
4973 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004974 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4975 && (int)wp->w_virtcol <
4976 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004977 && lnum != wp->w_cursor.lnum)
4978 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004979# ifdef FEAT_RIGHTLEFT
4980 && !wp->w_p_rl
4981# endif
4982 )
4983 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004984 int rightmost_vcol = 0;
4985 int i;
4986
4987 if (wp->w_p_cuc)
4988 rightmost_vcol = wp->w_virtcol;
4989 if (draw_color_col)
4990 /* determine rightmost colorcolumn to possibly draw */
4991 for (i = 0; color_cols[i] >= 0; ++i)
4992 if (rightmost_vcol < color_cols[i])
4993 rightmost_vcol = color_cols[i];
4994
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004995 while (col < W_WIDTH(wp))
4996 {
4997 ScreenLines[off] = ' ';
4998#ifdef FEAT_MBYTE
4999 if (enc_utf8)
5000 ScreenLinesUC[off] = 0;
5001#endif
5002 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005003 if (draw_color_col)
5004 draw_color_col = advance_color_col(VCOL_HLC,
5005 &color_cols);
5006
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005007 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005008 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005009 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005010 ScreenAttrs[off++] = hl_attr(HLF_MC);
5011 else
5012 ScreenAttrs[off++] = 0;
5013
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005014 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005015 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005016
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005017 ++vcol;
5018 }
5019 }
5020#endif
5021
Bram Moolenaar860cae12010-06-05 23:22:07 +02005022 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5023 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 row++;
5025
5026 /*
5027 * Update w_cline_height and w_cline_folded if the cursor line was
5028 * updated (saves a call to plines() later).
5029 */
5030 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5031 {
5032 curwin->w_cline_row = startrow;
5033 curwin->w_cline_height = row - startrow;
5034#ifdef FEAT_FOLDING
5035 curwin->w_cline_folded = FALSE;
5036#endif
5037 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5038 }
5039
5040 break;
5041 }
5042
5043 /* line continues beyond line end */
5044 if (lcs_ext
5045 && !wp->w_p_wrap
5046#ifdef FEAT_DIFF
5047 && filler_todo <= 0
5048#endif
5049 && (
5050#ifdef FEAT_RIGHTLEFT
5051 wp->w_p_rl ? col == 0 :
5052#endif
5053 col == W_WIDTH(wp) - 1)
5054 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005055 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5057 {
5058 c = lcs_ext;
5059 char_attr = hl_attr(HLF_AT);
5060#ifdef FEAT_MBYTE
5061 mb_c = c;
5062 if (enc_utf8 && (*mb_char2len)(c) > 1)
5063 {
5064 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005065 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005066 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068 else
5069 mb_utf8 = FALSE;
5070#endif
5071 }
5072
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005073#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005074 /* advance to the next 'colorcolumn' */
5075 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005076 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005077
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005078 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005079 * highlight the cursor position itself.
5080 * Also highlight the 'colorcolumn' if it is different than
5081 * 'cursorcolumn' */
5082 vcol_save_attr = -1;
5083 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005084 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005085 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005086 && lnum != wp->w_cursor.lnum)
5087 {
5088 vcol_save_attr = char_attr;
5089 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5090 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005091 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005092 {
5093 vcol_save_attr = char_attr;
5094 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5095 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005096 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005097#endif
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * Store character to be displayed.
5101 * Skip characters that are left of the screen for 'nowrap'.
5102 */
5103 vcol_prev = vcol;
5104 if (draw_state < WL_LINE || n_skip <= 0)
5105 {
5106 /*
5107 * Store the character.
5108 */
5109#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5110 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5111 {
5112 /* A double-wide character is: put first halve in left cell. */
5113 --off;
5114 --col;
5115 }
5116#endif
5117 ScreenLines[off] = c;
5118#ifdef FEAT_MBYTE
5119 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005120 {
5121 if ((mb_c & 0xff00) == 0x8e00)
5122 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 else if (enc_utf8)
5126 {
5127 if (mb_utf8)
5128 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005129 int i;
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005132 if ((c & 0xff) == 0)
5133 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005134 for (i = 0; i < Screen_mco; ++i)
5135 {
5136 ScreenLinesC[i][off] = u8cc[i];
5137 if (u8cc[i] == 0)
5138 break;
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 else
5142 ScreenLinesUC[off] = 0;
5143 }
5144 if (multi_attr)
5145 {
5146 ScreenAttrs[off] = multi_attr;
5147 multi_attr = 0;
5148 }
5149 else
5150#endif
5151 ScreenAttrs[off] = char_attr;
5152
5153#ifdef FEAT_MBYTE
5154 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5155 {
5156 /* Need to fill two screen columns. */
5157 ++off;
5158 ++col;
5159 if (enc_utf8)
5160 /* UTF-8: Put a 0 in the second screen char. */
5161 ScreenLines[off] = 0;
5162 else
5163 /* DBCS: Put second byte in the second screen char. */
5164 ScreenLines[off] = mb_c & 0xff;
5165 ++vcol;
5166 /* When "tocol" is halfway a character, set it to the end of
5167 * the character, otherwise highlighting won't stop. */
5168 if (tocol == vcol)
5169 ++tocol;
5170#ifdef FEAT_RIGHTLEFT
5171 if (wp->w_p_rl)
5172 {
5173 /* now it's time to backup one cell */
5174 --off;
5175 --col;
5176 }
5177#endif
5178 }
5179#endif
5180#ifdef FEAT_RIGHTLEFT
5181 if (wp->w_p_rl)
5182 {
5183 --off;
5184 --col;
5185 }
5186 else
5187#endif
5188 {
5189 ++off;
5190 ++col;
5191 }
5192 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005194 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005195 {
5196 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005197 ++vcol_off;
5198 if (n_extra > 0)
5199 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005200 if (wp->w_p_wrap)
5201 {
5202 /*
5203 * Special voodoo required if 'wrap' is on.
5204 *
5205 * Advance the column indicator to force the line
5206 * drawing to wrap early. This will make the line
5207 * take up the same screen space when parts are concealed,
5208 * so that cursor line computations aren't messed up.
5209 *
5210 * To avoid the fictitious advance of 'col' causing
5211 * trailing junk to be written out of the screen line
5212 * we are building, 'boguscols' keeps track of the number
5213 * of bad columns we have advanced.
5214 */
5215 if (n_extra > 0)
5216 {
5217 vcol += n_extra;
5218# ifdef FEAT_RIGHTLEFT
5219 if (wp->w_p_rl)
5220 {
5221 col -= n_extra;
5222 boguscols -= n_extra;
5223 }
5224 else
5225# endif
5226 {
5227 col += n_extra;
5228 boguscols += n_extra;
5229 }
5230 n_extra = 0;
5231 n_attr = 0;
5232 }
5233
5234
5235# ifdef FEAT_MBYTE
5236 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5237 {
5238 /* Need to fill two screen columns. */
5239# ifdef FEAT_RIGHTLEFT
5240 if (wp->w_p_rl)
5241 {
5242 --boguscols;
5243 --col;
5244 }
5245 else
5246# endif
5247 {
5248 ++boguscols;
5249 ++col;
5250 }
5251 }
5252# endif
5253
5254# ifdef FEAT_RIGHTLEFT
5255 if (wp->w_p_rl)
5256 {
5257 --boguscols;
5258 --col;
5259 }
5260 else
5261# endif
5262 {
5263 ++boguscols;
5264 ++col;
5265 }
5266 }
5267 else
5268 {
5269 if (n_extra > 0)
5270 {
5271 vcol += n_extra;
5272 n_extra = 0;
5273 n_attr = 0;
5274 }
5275 }
5276
5277 }
5278#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 else
5280 --n_skip;
5281
Bram Moolenaar64486672010-05-16 15:46:46 +02005282 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5283 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005284 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285#ifdef FEAT_DIFF
5286 && filler_todo <= 0
5287#endif
5288 )
5289 ++vcol;
5290
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005291#ifdef FEAT_SYN_HL
5292 if (vcol_save_attr >= 0)
5293 char_attr = vcol_save_attr;
5294#endif
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 /* restore attributes after "predeces" in 'listchars' */
5297 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5298 char_attr = saved_attr3;
5299
5300 /* restore attributes after last 'listchars' or 'number' char */
5301 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5302 char_attr = saved_attr2;
5303
5304 /*
5305 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005306 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 */
5308 if ((
5309#ifdef FEAT_RIGHTLEFT
5310 wp->w_p_rl ? (col < 0) :
5311#endif
5312 (col >= W_WIDTH(wp)))
5313 && (*ptr != NUL
5314#ifdef FEAT_DIFF
5315 || filler_todo > 0
5316#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005317 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5319 )
5320 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321#ifdef FEAT_CONCEAL
5322 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5323 (int)W_WIDTH(wp), wp->w_p_rl);
5324 boguscols = 0;
5325#else
5326 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5327 (int)W_WIDTH(wp), wp->w_p_rl);
5328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 ++row;
5330 ++screen_row;
5331
5332 /* When not wrapping and finished diff lines, or when displayed
5333 * '$' and highlighting until last column, break here. */
5334 if ((!wp->w_p_wrap
5335#ifdef FEAT_DIFF
5336 && filler_todo <= 0
5337#endif
5338 ) || lcs_eol_one == -1)
5339 break;
5340
5341 /* When the window is too narrow draw all "@" lines. */
5342 if (draw_state != WL_LINE
5343#ifdef FEAT_DIFF
5344 && filler_todo <= 0
5345#endif
5346 )
5347 {
5348 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5349#ifdef FEAT_VERTSPLIT
5350 draw_vsep_win(wp, row);
5351#endif
5352 row = endrow;
5353 }
5354
5355 /* When line got too long for screen break here. */
5356 if (row == endrow)
5357 {
5358 ++row;
5359 break;
5360 }
5361
5362 if (screen_cur_row == screen_row - 1
5363#ifdef FEAT_DIFF
5364 && filler_todo <= 0
5365#endif
5366 && W_WIDTH(wp) == Columns)
5367 {
5368 /* Remember that the line wraps, used for modeless copy. */
5369 LineWraps[screen_row - 1] = TRUE;
5370
5371 /*
5372 * Special trick to make copy/paste of wrapped lines work with
5373 * xterm/screen: write an extra character beyond the end of
5374 * the line. This will work with all terminal types
5375 * (regardless of the xn,am settings).
5376 * Only do this on a fast tty.
5377 * Only do this if the cursor is on the current line
5378 * (something has been written in it).
5379 * Don't do this for the GUI.
5380 * Don't do this for double-width characters.
5381 * Don't do this for a window not at the right screen border.
5382 */
5383 if (p_tf
5384#ifdef FEAT_GUI
5385 && !gui.in_use
5386#endif
5387#ifdef FEAT_MBYTE
5388 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005389 && ((*mb_off2cells)(LineOffset[screen_row],
5390 LineOffset[screen_row] + screen_Columns)
5391 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005393 + (int)Columns - 2,
5394 LineOffset[screen_row] + screen_Columns)
5395 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
5397 )
5398 {
5399 /* First make sure we are at the end of the screen line,
5400 * then output the same character again to let the
5401 * terminal know about the wrap. If the terminal doesn't
5402 * auto-wrap, we overwrite the character. */
5403 if (screen_cur_col != W_WIDTH(wp))
5404 screen_char(LineOffset[screen_row - 1]
5405 + (unsigned)Columns - 1,
5406 screen_row - 1, (int)(Columns - 1));
5407
5408#ifdef FEAT_MBYTE
5409 /* When there is a multi-byte character, just output a
5410 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005411 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5412 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 out_char(' ');
5414 else
5415#endif
5416 out_char(ScreenLines[LineOffset[screen_row - 1]
5417 + (Columns - 1)]);
5418 /* force a redraw of the first char on the next line */
5419 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5420 screen_start(); /* don't know where cursor is now */
5421 }
5422 }
5423
5424 col = 0;
5425 off = (unsigned)(current_ScreenLine - ScreenLines);
5426#ifdef FEAT_RIGHTLEFT
5427 if (wp->w_p_rl)
5428 {
5429 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5430 off += col;
5431 }
5432#endif
5433
5434 /* reset the drawing state for the start of a wrapped line */
5435 draw_state = WL_START;
5436 saved_n_extra = n_extra;
5437 saved_p_extra = p_extra;
5438 saved_c_extra = c_extra;
5439 saved_char_attr = char_attr;
5440 n_extra = 0;
5441 lcs_prec_todo = lcs_prec;
5442#ifdef FEAT_LINEBREAK
5443# ifdef FEAT_DIFF
5444 if (filler_todo <= 0)
5445# endif
5446 need_showbreak = TRUE;
5447#endif
5448#ifdef FEAT_DIFF
5449 --filler_todo;
5450 /* When the filler lines are actually below the last line of the
5451 * file, don't draw the line itself, break here. */
5452 if (filler_todo == 0 && wp->w_botfill)
5453 break;
5454#endif
5455 }
5456
5457 } /* for every character in the line */
5458
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005459#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005460 /* After an empty line check first word for capital. */
5461 if (*skipwhite(line) == NUL)
5462 {
5463 capcol_lnum = lnum + 1;
5464 cap_col = 0;
5465 }
5466#endif
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 return row;
5469}
5470
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005471#ifdef FEAT_MBYTE
5472static int comp_char_differs __ARGS((int, int));
5473
5474/*
5475 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005476 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005477 */
5478 static int
5479comp_char_differs(off_from, off_to)
5480 int off_from;
5481 int off_to;
5482{
5483 int i;
5484
5485 for (i = 0; i < Screen_mco; ++i)
5486 {
5487 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5488 return TRUE;
5489 if (ScreenLinesC[i][off_from] == 0)
5490 break;
5491 }
5492 return FALSE;
5493}
5494#endif
5495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496/*
5497 * Check whether the given character needs redrawing:
5498 * - the (first byte of the) character is different
5499 * - the attributes are different
5500 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005501 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 */
5503 static int
5504char_needs_redraw(off_from, off_to, cols)
5505 int off_from;
5506 int off_to;
5507 int cols;
5508{
5509 if (cols > 0
5510 && ((ScreenLines[off_from] != ScreenLines[off_to]
5511 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5512
5513#ifdef FEAT_MBYTE
5514 || (enc_dbcs != 0
5515 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5516 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5517 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5518 : (cols > 1 && ScreenLines[off_from + 1]
5519 != ScreenLines[off_to + 1])))
5520 || (enc_utf8
5521 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5522 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005523 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005524 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5525 && ScreenLines[off_from + 1]
5526 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527#endif
5528 ))
5529 return TRUE;
5530 return FALSE;
5531}
5532
5533/*
5534 * Move one "cooked" screen line to the screen, but only the characters that
5535 * have actually changed. Handle insert/delete character.
5536 * "coloff" gives the first column on the screen for this line.
5537 * "endcol" gives the columns where valid characters are.
5538 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5539 * needs to be cleared, negative otherwise.
5540 * "rlflag" is TRUE in a rightleft window:
5541 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5542 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5543 */
5544 static void
5545screen_line(row, coloff, endcol, clear_width
5546#ifdef FEAT_RIGHTLEFT
5547 , rlflag
5548#endif
5549 )
5550 int row;
5551 int coloff;
5552 int endcol;
5553 int clear_width;
5554#ifdef FEAT_RIGHTLEFT
5555 int rlflag;
5556#endif
5557{
5558 unsigned off_from;
5559 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005560#ifdef FEAT_MBYTE
5561 unsigned max_off_from;
5562 unsigned max_off_to;
5563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 int col = 0;
5565#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5566 int hl;
5567#endif
5568 int force = FALSE; /* force update rest of the line */
5569 int redraw_this /* bool: does character need redraw? */
5570#ifdef FEAT_GUI
5571 = TRUE /* For GUI when while-loop empty */
5572#endif
5573 ;
5574 int redraw_next; /* redraw_this for next character */
5575#ifdef FEAT_MBYTE
5576 int clear_next = FALSE;
5577 int char_cells; /* 1: normal char */
5578 /* 2: occupies two display cells */
5579# define CHAR_CELLS char_cells
5580#else
5581# define CHAR_CELLS 1
5582#endif
5583
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005584 /* Check for illegal row and col, just in case. */
5585 if (row >= Rows)
5586 row = Rows - 1;
5587 if (endcol > Columns)
5588 endcol = Columns;
5589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590# ifdef FEAT_CLIPBOARD
5591 clip_may_clear_selection(row, row);
5592# endif
5593
5594 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5595 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005596#ifdef FEAT_MBYTE
5597 max_off_from = off_from + screen_Columns;
5598 max_off_to = LineOffset[row] + screen_Columns;
5599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
5601#ifdef FEAT_RIGHTLEFT
5602 if (rlflag)
5603 {
5604 /* Clear rest first, because it's left of the text. */
5605 if (clear_width > 0)
5606 {
5607 while (col <= endcol && ScreenLines[off_to] == ' '
5608 && ScreenAttrs[off_to] == 0
5609# ifdef FEAT_MBYTE
5610 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5611# endif
5612 )
5613 {
5614 ++off_to;
5615 ++col;
5616 }
5617 if (col <= endcol)
5618 screen_fill(row, row + 1, col + coloff,
5619 endcol + coloff + 1, ' ', ' ', 0);
5620 }
5621 col = endcol + 1;
5622 off_to = LineOffset[row] + col + coloff;
5623 off_from += col;
5624 endcol = (clear_width > 0 ? clear_width : -clear_width);
5625 }
5626#endif /* FEAT_RIGHTLEFT */
5627
5628 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5629
5630 while (col < endcol)
5631 {
5632#ifdef FEAT_MBYTE
5633 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005634 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
5636 char_cells = 1;
5637#endif
5638
5639 redraw_this = redraw_next;
5640 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5641 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5642
5643#ifdef FEAT_GUI
5644 /* If the next character was bold, then redraw the current character to
5645 * remove any pixels that might have spilt over into us. This only
5646 * happens in the GUI.
5647 */
5648 if (redraw_next && gui.in_use)
5649 {
5650 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005651 if (hl > HL_ALL)
5652 hl = syn_attr2attr(hl);
5653 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 redraw_this = TRUE;
5655 }
5656#endif
5657
5658 if (redraw_this)
5659 {
5660 /*
5661 * Special handling when 'xs' termcap flag set (hpterm):
5662 * Attributes for characters are stored at the position where the
5663 * cursor is when writing the highlighting code. The
5664 * start-highlighting code must be written with the cursor on the
5665 * first highlighted character. The stop-highlighting code must
5666 * be written with the cursor just after the last highlighted
5667 * character.
5668 * Overwriting a character doesn't remove it's highlighting. Need
5669 * to clear the rest of the line, and force redrawing it
5670 * completely.
5671 */
5672 if ( p_wiv
5673 && !force
5674#ifdef FEAT_GUI
5675 && !gui.in_use
5676#endif
5677 && ScreenAttrs[off_to] != 0
5678 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5679 {
5680 /*
5681 * Need to remove highlighting attributes here.
5682 */
5683 windgoto(row, col + coloff);
5684 out_str(T_CE); /* clear rest of this screen line */
5685 screen_start(); /* don't know where cursor is now */
5686 force = TRUE; /* force redraw of rest of the line */
5687 redraw_next = TRUE; /* or else next char would miss out */
5688
5689 /*
5690 * If the previous character was highlighted, need to stop
5691 * highlighting at this character.
5692 */
5693 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5694 {
5695 screen_attr = ScreenAttrs[off_to - 1];
5696 term_windgoto(row, col + coloff);
5697 screen_stop_highlight();
5698 }
5699 else
5700 screen_attr = 0; /* highlighting has stopped */
5701 }
5702#ifdef FEAT_MBYTE
5703 if (enc_dbcs != 0)
5704 {
5705 /* Check if overwriting a double-byte with a single-byte or
5706 * the other way around requires another character to be
5707 * redrawn. For UTF-8 this isn't needed, because comparing
5708 * ScreenLinesUC[] is sufficient. */
5709 if (char_cells == 1
5710 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005711 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 /* Writing a single-cell character over a double-cell
5714 * character: need to redraw the next cell. */
5715 ScreenLines[off_to + 1] = 0;
5716 redraw_next = TRUE;
5717 }
5718 else if (char_cells == 2
5719 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005720 && (*mb_off2cells)(off_to, max_off_to) == 1
5721 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 /* Writing the second half of a double-cell character over
5724 * a double-cell character: need to redraw the second
5725 * cell. */
5726 ScreenLines[off_to + 2] = 0;
5727 redraw_next = TRUE;
5728 }
5729
5730 if (enc_dbcs == DBCS_JPNU)
5731 ScreenLines2[off_to] = ScreenLines2[off_from];
5732 }
5733 /* When writing a single-width character over a double-width
5734 * character and at the end of the redrawn text, need to clear out
5735 * the right halve of the old character.
5736 * Also required when writing the right halve of a double-width
5737 * char over the left halve of an existing one. */
5738 if (has_mbyte && col + char_cells == endcol
5739 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005740 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005742 && (*mb_off2cells)(off_to, max_off_to) == 1
5743 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 clear_next = TRUE;
5745#endif
5746
5747 ScreenLines[off_to] = ScreenLines[off_from];
5748#ifdef FEAT_MBYTE
5749 if (enc_utf8)
5750 {
5751 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5752 if (ScreenLinesUC[off_from] != 0)
5753 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005754 int i;
5755
5756 for (i = 0; i < Screen_mco; ++i)
5757 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 }
5760 if (char_cells == 2)
5761 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5762#endif
5763
5764#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005765 /* The bold trick makes a single column of pixels appear in the
5766 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * character should be redrawn too. This happens for our own GUI
5768 * and for some xterms. */
5769 if (
5770# ifdef FEAT_GUI
5771 gui.in_use
5772# endif
5773# if defined(FEAT_GUI) && defined(UNIX)
5774 ||
5775# endif
5776# ifdef UNIX
5777 term_is_xterm
5778# endif
5779 )
5780 {
5781 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005782 if (hl > HL_ALL)
5783 hl = syn_attr2attr(hl);
5784 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 redraw_next = TRUE;
5786 }
5787#endif
5788 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5789#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005790 /* For simplicity set the attributes of second half of a
5791 * double-wide character equal to the first half. */
5792 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005794
5795 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 else
5798#endif
5799 screen_char(off_to, row, col + coloff);
5800 }
5801 else if ( p_wiv
5802#ifdef FEAT_GUI
5803 && !gui.in_use
5804#endif
5805 && col + coloff > 0)
5806 {
5807 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5808 {
5809 /*
5810 * Don't output stop-highlight when moving the cursor, it will
5811 * stop the highlighting when it should continue.
5812 */
5813 screen_attr = 0;
5814 }
5815 else if (screen_attr != 0)
5816 screen_stop_highlight();
5817 }
5818
5819 off_to += CHAR_CELLS;
5820 off_from += CHAR_CELLS;
5821 col += CHAR_CELLS;
5822 }
5823
5824#ifdef FEAT_MBYTE
5825 if (clear_next)
5826 {
5827 /* Clear the second half of a double-wide character of which the left
5828 * half was overwritten with a single-wide character. */
5829 ScreenLines[off_to] = ' ';
5830 if (enc_utf8)
5831 ScreenLinesUC[off_to] = 0;
5832 screen_char(off_to, row, col + coloff);
5833 }
5834#endif
5835
5836 if (clear_width > 0
5837#ifdef FEAT_RIGHTLEFT
5838 && !rlflag
5839#endif
5840 )
5841 {
5842#ifdef FEAT_GUI
5843 int startCol = col;
5844#endif
5845
5846 /* blank out the rest of the line */
5847 while (col < clear_width && ScreenLines[off_to] == ' '
5848 && ScreenAttrs[off_to] == 0
5849#ifdef FEAT_MBYTE
5850 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5851#endif
5852 )
5853 {
5854 ++off_to;
5855 ++col;
5856 }
5857 if (col < clear_width)
5858 {
5859#ifdef FEAT_GUI
5860 /*
5861 * In the GUI, clearing the rest of the line may leave pixels
5862 * behind if the first character cleared was bold. Some bold
5863 * fonts spill over the left. In this case we redraw the previous
5864 * character too. If we didn't skip any blanks above, then we
5865 * only redraw if the character wasn't already redrawn anyway.
5866 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005867 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
5869 hl = ScreenAttrs[off_to];
5870 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005871 {
5872 int prev_cells = 1;
5873# ifdef FEAT_MBYTE
5874 if (enc_utf8)
5875 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5876 * that its width is 2. */
5877 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5878 else if (enc_dbcs != 0)
5879 {
5880 /* find previous character by counting from first
5881 * column and get its width. */
5882 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005883 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005884
5885 while (off < off_to)
5886 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005887 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005888 off += prev_cells;
5889 }
5890 }
5891
5892 if (enc_dbcs != 0 && prev_cells > 1)
5893 screen_char_2(off_to - prev_cells, row,
5894 col + coloff - prev_cells);
5895 else
5896# endif
5897 screen_char(off_to - prev_cells, row,
5898 col + coloff - prev_cells);
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901#endif
5902 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5903 ' ', ' ', 0);
5904#ifdef FEAT_VERTSPLIT
5905 off_to += clear_width - col;
5906 col = clear_width;
5907#endif
5908 }
5909 }
5910
5911 if (clear_width > 0)
5912 {
5913#ifdef FEAT_VERTSPLIT
5914 /* For a window that's left of another, draw the separator char. */
5915 if (col + coloff < Columns)
5916 {
5917 int c;
5918
5919 c = fillchar_vsep(&hl);
5920 if (ScreenLines[off_to] != c
5921# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005922 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5923 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924# endif
5925 || ScreenAttrs[off_to] != hl)
5926 {
5927 ScreenLines[off_to] = c;
5928 ScreenAttrs[off_to] = hl;
5929# ifdef FEAT_MBYTE
5930 if (enc_utf8)
5931 {
5932 if (c >= 0x80)
5933 {
5934 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005935 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 else
5938 ScreenLinesUC[off_to] = 0;
5939 }
5940# endif
5941 screen_char(off_to, row, col + coloff);
5942 }
5943 }
5944 else
5945#endif
5946 LineWraps[row] = FALSE;
5947 }
5948}
5949
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005950#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005952 * Mirror text "str" for right-left displaying.
5953 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005955 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956rl_mirror(str)
5957 char_u *str;
5958{
5959 char_u *p1, *p2;
5960 int t;
5961
5962 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5963 {
5964 t = *p1;
5965 *p1 = *p2;
5966 *p2 = t;
5967 }
5968}
5969#endif
5970
5971#if defined(FEAT_WINDOWS) || defined(PROTO)
5972/*
5973 * mark all status lines for redraw; used after first :cd
5974 */
5975 void
5976status_redraw_all()
5977{
5978 win_T *wp;
5979
5980 for (wp = firstwin; wp; wp = wp->w_next)
5981 if (wp->w_status_height)
5982 {
5983 wp->w_redr_status = TRUE;
5984 redraw_later(VALID);
5985 }
5986}
5987
5988/*
5989 * mark all status lines of the current buffer for redraw
5990 */
5991 void
5992status_redraw_curbuf()
5993{
5994 win_T *wp;
5995
5996 for (wp = firstwin; wp; wp = wp->w_next)
5997 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
5998 {
5999 wp->w_redr_status = TRUE;
6000 redraw_later(VALID);
6001 }
6002}
6003
6004/*
6005 * Redraw all status lines that need to be redrawn.
6006 */
6007 void
6008redraw_statuslines()
6009{
6010 win_T *wp;
6011
6012 for (wp = firstwin; wp; wp = wp->w_next)
6013 if (wp->w_redr_status)
6014 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006015 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006016 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018#endif
6019
6020#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6021/*
6022 * Redraw all status lines at the bottom of frame "frp".
6023 */
6024 void
6025win_redraw_last_status(frp)
6026 frame_T *frp;
6027{
6028 if (frp->fr_layout == FR_LEAF)
6029 frp->fr_win->w_redr_status = TRUE;
6030 else if (frp->fr_layout == FR_ROW)
6031 {
6032 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6033 win_redraw_last_status(frp);
6034 }
6035 else /* frp->fr_layout == FR_COL */
6036 {
6037 frp = frp->fr_child;
6038 while (frp->fr_next != NULL)
6039 frp = frp->fr_next;
6040 win_redraw_last_status(frp);
6041 }
6042}
6043#endif
6044
6045#ifdef FEAT_VERTSPLIT
6046/*
6047 * Draw the verticap separator right of window "wp" starting with line "row".
6048 */
6049 static void
6050draw_vsep_win(wp, row)
6051 win_T *wp;
6052 int row;
6053{
6054 int hl;
6055 int c;
6056
6057 if (wp->w_vsep_width)
6058 {
6059 /* draw the vertical separator right of this window */
6060 c = fillchar_vsep(&hl);
6061 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6062 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6063 c, ' ', hl);
6064 }
6065}
6066#endif
6067
6068#ifdef FEAT_WILDMENU
6069static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006070static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006073 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 */
6075 static int
6076status_match_len(xp, s)
6077 expand_T *xp;
6078 char_u *s;
6079{
6080 int len = 0;
6081
6082#ifdef FEAT_MENU
6083 int emenu = (xp->xp_context == EXPAND_MENUS
6084 || xp->xp_context == EXPAND_MENUNAMES);
6085
6086 /* Check for menu separators - replace with '|'. */
6087 if (emenu && menu_is_separator(s))
6088 return 1;
6089#endif
6090
6091 while (*s != NUL)
6092 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006093 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006094 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006095 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 }
6097
6098 return len;
6099}
6100
6101/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006102 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006103 * These are backslashes used for escaping. Do show backslashes in help tags.
6104 */
6105 static int
6106skip_status_match_char(xp, s)
6107 expand_T *xp;
6108 char_u *s;
6109{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006110 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006111#ifdef FEAT_MENU
6112 || ((xp->xp_context == EXPAND_MENUS
6113 || xp->xp_context == EXPAND_MENUNAMES)
6114 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6115#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006116 )
6117 {
6118#ifndef BACKSLASH_IN_FILENAME
6119 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6120 return 2;
6121#endif
6122 return 1;
6123 }
6124 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006125}
6126
6127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 * Show wildchar matches in the status line.
6129 * Show at least the "match" item.
6130 * We start at item 'first_match' in the list and show all matches that fit.
6131 *
6132 * If inversion is possible we use it. Else '=' characters are used.
6133 */
6134 void
6135win_redr_status_matches(xp, num_matches, matches, match, showtail)
6136 expand_T *xp;
6137 int num_matches;
6138 char_u **matches; /* list of matches */
6139 int match;
6140 int showtail;
6141{
6142#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6143 int row;
6144 char_u *buf;
6145 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006146 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 int fillchar;
6148 int attr;
6149 int i;
6150 int highlight = TRUE;
6151 char_u *selstart = NULL;
6152 int selstart_col = 0;
6153 char_u *selend = NULL;
6154 static int first_match = 0;
6155 int add_left = FALSE;
6156 char_u *s;
6157#ifdef FEAT_MENU
6158 int emenu;
6159#endif
6160#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6161 int l;
6162#endif
6163
6164 if (matches == NULL) /* interrupted completion? */
6165 return;
6166
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006167#ifdef FEAT_MBYTE
6168 if (has_mbyte)
6169 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6170 else
6171#endif
6172 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (buf == NULL)
6174 return;
6175
6176 if (match == -1) /* don't show match but original text */
6177 {
6178 match = 0;
6179 highlight = FALSE;
6180 }
6181 /* count 1 for the ending ">" */
6182 clen = status_match_len(xp, L_MATCH(match)) + 3;
6183 if (match == 0)
6184 first_match = 0;
6185 else if (match < first_match)
6186 {
6187 /* jumping left, as far as we can go */
6188 first_match = match;
6189 add_left = TRUE;
6190 }
6191 else
6192 {
6193 /* check if match fits on the screen */
6194 for (i = first_match; i < match; ++i)
6195 clen += status_match_len(xp, L_MATCH(i)) + 2;
6196 if (first_match > 0)
6197 clen += 2;
6198 /* jumping right, put match at the left */
6199 if ((long)clen > Columns)
6200 {
6201 first_match = match;
6202 /* if showing the last match, we can add some on the left */
6203 clen = 2;
6204 for (i = match; i < num_matches; ++i)
6205 {
6206 clen += status_match_len(xp, L_MATCH(i)) + 2;
6207 if ((long)clen >= Columns)
6208 break;
6209 }
6210 if (i == num_matches)
6211 add_left = TRUE;
6212 }
6213 }
6214 if (add_left)
6215 while (first_match > 0)
6216 {
6217 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6218 if ((long)clen >= Columns)
6219 break;
6220 --first_match;
6221 }
6222
6223 fillchar = fillchar_status(&attr, TRUE);
6224
6225 if (first_match == 0)
6226 {
6227 *buf = NUL;
6228 len = 0;
6229 }
6230 else
6231 {
6232 STRCPY(buf, "< ");
6233 len = 2;
6234 }
6235 clen = len;
6236
6237 i = first_match;
6238 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6239 {
6240 if (i == match)
6241 {
6242 selstart = buf + len;
6243 selstart_col = clen;
6244 }
6245
6246 s = L_MATCH(i);
6247 /* Check for menu separators - replace with '|' */
6248#ifdef FEAT_MENU
6249 emenu = (xp->xp_context == EXPAND_MENUS
6250 || xp->xp_context == EXPAND_MENUNAMES);
6251 if (emenu && menu_is_separator(s))
6252 {
6253 STRCPY(buf + len, transchar('|'));
6254 l = (int)STRLEN(buf + len);
6255 len += l;
6256 clen += l;
6257 }
6258 else
6259#endif
6260 for ( ; *s != NUL; ++s)
6261 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006262 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 clen += ptr2cells(s);
6264#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006265 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 STRNCPY(buf + len, s, l);
6268 s += l - 1;
6269 len += l;
6270 }
6271 else
6272#endif
6273 {
6274 STRCPY(buf + len, transchar_byte(*s));
6275 len += (int)STRLEN(buf + len);
6276 }
6277 }
6278 if (i == match)
6279 selend = buf + len;
6280
6281 *(buf + len++) = ' ';
6282 *(buf + len++) = ' ';
6283 clen += 2;
6284 if (++i == num_matches)
6285 break;
6286 }
6287
6288 if (i != num_matches)
6289 {
6290 *(buf + len++) = '>';
6291 ++clen;
6292 }
6293
6294 buf[len] = NUL;
6295
6296 row = cmdline_row - 1;
6297 if (row >= 0)
6298 {
6299 if (wild_menu_showing == 0)
6300 {
6301 if (msg_scrolled > 0)
6302 {
6303 /* Put the wildmenu just above the command line. If there is
6304 * no room, scroll the screen one line up. */
6305 if (cmdline_row == Rows - 1)
6306 {
6307 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6308 ++msg_scrolled;
6309 }
6310 else
6311 {
6312 ++cmdline_row;
6313 ++row;
6314 }
6315 wild_menu_showing = WM_SCROLLED;
6316 }
6317 else
6318 {
6319 /* Create status line if needed by setting 'laststatus' to 2.
6320 * Set 'winminheight' to zero to avoid that the window is
6321 * resized. */
6322 if (lastwin->w_status_height == 0)
6323 {
6324 save_p_ls = p_ls;
6325 save_p_wmh = p_wmh;
6326 p_ls = 2;
6327 p_wmh = 0;
6328 last_status(FALSE);
6329 }
6330 wild_menu_showing = WM_SHOWN;
6331 }
6332 }
6333
6334 screen_puts(buf, row, 0, attr);
6335 if (selstart != NULL && highlight)
6336 {
6337 *selend = NUL;
6338 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6339 }
6340
6341 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6342 }
6343
6344#ifdef FEAT_VERTSPLIT
6345 win_redraw_last_status(topframe);
6346#else
6347 lastwin->w_redr_status = TRUE;
6348#endif
6349 vim_free(buf);
6350}
6351#endif
6352
6353#if defined(FEAT_WINDOWS) || defined(PROTO)
6354/*
6355 * Redraw the status line of window wp.
6356 *
6357 * If inversion is possible we use it. Else '=' characters are used.
6358 */
6359 void
6360win_redr_status(wp)
6361 win_T *wp;
6362{
6363 int row;
6364 char_u *p;
6365 int len;
6366 int fillchar;
6367 int attr;
6368 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006369 static int busy = FALSE;
6370
6371 /* It's possible to get here recursively when 'statusline' (indirectly)
6372 * invokes ":redrawstatus". Simply ignore the call then. */
6373 if (busy)
6374 return;
6375 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377 wp->w_redr_status = FALSE;
6378 if (wp->w_status_height == 0)
6379 {
6380 /* no status line, can only be last window */
6381 redraw_cmdline = TRUE;
6382 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006383 else if (!redrawing()
6384#ifdef FEAT_INS_EXPAND
6385 /* don't update status line when popup menu is visible and may be
6386 * drawn over it */
6387 || pum_visible()
6388#endif
6389 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 /* Don't redraw right now, do it later. */
6392 wp->w_redr_status = TRUE;
6393 }
6394#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006395 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
6397 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006398 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400#endif
6401 else
6402 {
6403 fillchar = fillchar_status(&attr, wp == curwin);
6404
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006405 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 p = NameBuff;
6407 len = (int)STRLEN(p);
6408
6409 if (wp->w_buffer->b_help
6410#ifdef FEAT_QUICKFIX
6411 || wp->w_p_pvw
6412#endif
6413 || bufIsChanged(wp->w_buffer)
6414 || wp->w_buffer->b_p_ro)
6415 *(p + len++) = ' ';
6416 if (wp->w_buffer->b_help)
6417 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006418 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 len += (int)STRLEN(p + len);
6420 }
6421#ifdef FEAT_QUICKFIX
6422 if (wp->w_p_pvw)
6423 {
6424 STRCPY(p + len, _("[Preview]"));
6425 len += (int)STRLEN(p + len);
6426 }
6427#endif
6428 if (bufIsChanged(wp->w_buffer))
6429 {
6430 STRCPY(p + len, "[+]");
6431 len += 3;
6432 }
6433 if (wp->w_buffer->b_p_ro)
6434 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006435 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 len += 4;
6437 }
6438
6439#ifndef FEAT_VERTSPLIT
6440 this_ru_col = ru_col;
6441 if (this_ru_col < (Columns + 1) / 2)
6442 this_ru_col = (Columns + 1) / 2;
6443#else
6444 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6445 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6446 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6447 if (this_ru_col <= 1)
6448 {
6449 p = (char_u *)"<"; /* No room for file name! */
6450 len = 1;
6451 }
6452 else
6453#endif
6454#ifdef FEAT_MBYTE
6455 if (has_mbyte)
6456 {
6457 int clen = 0, i;
6458
6459 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006460 clen = mb_string2cells(p, -1);
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 /* Find first character that will fit.
6463 * Going from start to end is much faster for DBCS. */
6464 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006465 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 clen -= (*mb_ptr2cells)(p + i);
6467 len = clen;
6468 if (i > 0)
6469 {
6470 p = p + i - 1;
6471 *p = '<';
6472 ++len;
6473 }
6474
6475 }
6476 else
6477#endif
6478 if (len > this_ru_col - 1)
6479 {
6480 p += len - (this_ru_col - 1);
6481 *p = '<';
6482 len = this_ru_col - 1;
6483 }
6484
6485 row = W_WINROW(wp) + wp->w_height;
6486 screen_puts(p, row, W_WINCOL(wp), attr);
6487 screen_fill(row, row + 1, len + W_WINCOL(wp),
6488 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6489
6490 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6491 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6492 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6493 - 1 + W_WINCOL(wp)), attr);
6494
6495#ifdef FEAT_CMDL_INFO
6496 win_redr_ruler(wp, TRUE);
6497#endif
6498 }
6499
6500#ifdef FEAT_VERTSPLIT
6501 /*
6502 * May need to draw the character below the vertical separator.
6503 */
6504 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6505 {
6506 if (stl_connected(wp))
6507 fillchar = fillchar_status(&attr, wp == curwin);
6508 else
6509 fillchar = fillchar_vsep(&attr);
6510 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6511 attr);
6512 }
6513#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006514 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
Bram Moolenaar238a5642006-02-21 22:12:05 +00006517#ifdef FEAT_STL_OPT
6518/*
6519 * Redraw the status line according to 'statusline' and take care of any
6520 * errors encountered.
6521 */
6522 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006523redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006524 win_T *wp;
6525{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006526 static int entered = FALSE;
6527 int save_called_emsg = called_emsg;
6528
6529 /* When called recursively return. This can happen when the statusline
6530 * contains an expression that triggers a redraw. */
6531 if (entered)
6532 return;
6533 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006534
6535 called_emsg = FALSE;
6536 win_redr_custom(wp, FALSE);
6537 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006538 {
6539 /* When there is an error disable the statusline, otherwise the
6540 * display is messed up with errors and a redraw triggers the problem
6541 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006542 set_string_option_direct((char_u *)"statusline", -1,
6543 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006544 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006545 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006546 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006547 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006548}
6549#endif
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551# ifdef FEAT_VERTSPLIT
6552/*
6553 * Return TRUE if the status line of window "wp" is connected to the status
6554 * line of the window right of it. If not, then it's a vertical separator.
6555 * Only call if (wp->w_vsep_width != 0).
6556 */
6557 int
6558stl_connected(wp)
6559 win_T *wp;
6560{
6561 frame_T *fr;
6562
6563 fr = wp->w_frame;
6564 while (fr->fr_parent != NULL)
6565 {
6566 if (fr->fr_parent->fr_layout == FR_COL)
6567 {
6568 if (fr->fr_next != NULL)
6569 break;
6570 }
6571 else
6572 {
6573 if (fr->fr_next != NULL)
6574 return TRUE;
6575 }
6576 fr = fr->fr_parent;
6577 }
6578 return FALSE;
6579}
6580# endif
6581
6582#endif /* FEAT_WINDOWS */
6583
6584#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6585/*
6586 * Get the value to show for the language mappings, active 'keymap'.
6587 */
6588 int
6589get_keymap_str(wp, buf, len)
6590 win_T *wp;
6591 char_u *buf; /* buffer for the result */
6592 int len; /* length of buffer */
6593{
6594 char_u *p;
6595
6596 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6597 return FALSE;
6598
6599 {
6600#ifdef FEAT_EVAL
6601 buf_T *old_curbuf = curbuf;
6602 win_T *old_curwin = curwin;
6603 char_u *s;
6604
6605 curbuf = wp->w_buffer;
6606 curwin = wp;
6607 STRCPY(buf, "b:keymap_name"); /* must be writable */
6608 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006609 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 --emsg_skip;
6611 curbuf = old_curbuf;
6612 curwin = old_curwin;
6613 if (p == NULL || *p == NUL)
6614#endif
6615 {
6616#ifdef FEAT_KEYMAP
6617 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6618 p = wp->w_buffer->b_p_keymap;
6619 else
6620#endif
6621 p = (char_u *)"lang";
6622 }
6623 if ((int)(STRLEN(p) + 3) < len)
6624 sprintf((char *)buf, "<%s>", p);
6625 else
6626 buf[0] = NUL;
6627#ifdef FEAT_EVAL
6628 vim_free(s);
6629#endif
6630 }
6631 return buf[0] != NUL;
6632}
6633#endif
6634
6635#if defined(FEAT_STL_OPT) || defined(PROTO)
6636/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006637 * Redraw the status line or ruler of window "wp".
6638 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 */
6640 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006641win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006643 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 int attr;
6646 int curattr;
6647 int row;
6648 int col = 0;
6649 int maxwidth;
6650 int width;
6651 int n;
6652 int len;
6653 int fillchar;
6654 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006655 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006657 struct stl_hlrec hltab[STL_MAX_ITEM];
6658 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006659 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006660 win_T *ewp;
6661 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006664 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006666 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006667 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006668 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006669 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006670 attr = hl_attr(HLF_TPF);
6671 maxwidth = Columns;
6672# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006673 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006676 else
6677 {
6678 row = W_WINROW(wp) + wp->w_height;
6679 fillchar = fillchar_status(&attr, wp == curwin);
6680 maxwidth = W_WIDTH(wp);
6681
6682 if (draw_ruler)
6683 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006684 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006685 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006686 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006687 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006688 if (*++stl == '-')
6689 stl++;
6690 if (atoi((char *)stl))
6691 while (VIM_ISDIGIT(*stl))
6692 stl++;
6693 if (*stl++ != '(')
6694 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006695 }
6696#ifdef FEAT_VERTSPLIT
6697 col = ru_col - (Columns - W_WIDTH(wp));
6698 if (col < (W_WIDTH(wp) + 1) / 2)
6699 col = (W_WIDTH(wp) + 1) / 2;
6700#else
6701 col = ru_col;
6702 if (col > (Columns + 1) / 2)
6703 col = (Columns + 1) / 2;
6704#endif
6705 maxwidth = W_WIDTH(wp) - col;
6706#ifdef FEAT_WINDOWS
6707 if (!wp->w_status_height)
6708#endif
6709 {
6710 row = Rows - 1;
6711 --maxwidth; /* writing in last column may cause scrolling */
6712 fillchar = ' ';
6713 attr = 0;
6714 }
6715
6716# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006717 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006718# endif
6719 }
6720 else
6721 {
6722 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006723 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006724 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006725 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006726# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006727 use_sandbox = was_set_insecurely((char_u *)"statusline",
6728 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006729# endif
6730 }
6731
6732#ifdef FEAT_VERTSPLIT
6733 col += W_WINCOL(wp);
6734#endif
6735 }
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 if (maxwidth <= 0)
6738 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
Bram Moolenaar61452852011-02-01 18:01:11 +01006740 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6741 * the cursor away and back. */
6742 ewp = wp == NULL ? curwin : wp;
6743 p_crb_save = ewp->w_p_crb;
6744 ewp->w_p_crb = FALSE;
6745
Bram Moolenaar362f3562009-11-03 16:20:34 +00006746 /* Make a copy, because the statusline may include a function call that
6747 * might change the option value and free the memory. */
6748 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006749 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006750 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006751 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006752 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006753 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006755 /* Make all characters printable. */
6756 p = transstr(buf);
6757 if (p != NULL)
6758 {
6759 vim_strncpy(buf, p, sizeof(buf) - 1);
6760 vim_free(p);
6761 }
6762
6763 /* fill up with "fillchar" */
6764 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006765 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
6767#ifdef FEAT_MBYTE
6768 len += (*mb_char2bytes)(fillchar, buf + len);
6769#else
6770 buf[len++] = fillchar;
6771#endif
6772 ++width;
6773 }
6774 buf[len] = NUL;
6775
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006776 /*
6777 * Draw each snippet with the specified highlighting.
6778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 curattr = attr;
6780 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006781 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006783 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 screen_puts_len(p, len, row, col, curattr);
6785 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006786 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006788 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006790 else if (hltab[n].userhl < 0)
6791 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006793 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006794 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795#endif
6796 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006797 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006800
6801 if (wp == NULL)
6802 {
6803 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6804 col = 0;
6805 len = 0;
6806 p = buf;
6807 fillchar = 0;
6808 for (n = 0; tabtab[n].start != NULL; n++)
6809 {
6810 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6811 while (col < len)
6812 TabPageIdxs[col++] = fillchar;
6813 p = tabtab[n].start;
6814 fillchar = tabtab[n].userhl;
6815 }
6816 while (col < Columns)
6817 TabPageIdxs[col++] = fillchar;
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819}
6820
6821#endif /* FEAT_STL_OPT */
6822
6823/*
6824 * Output a single character directly to the screen and update ScreenLines.
6825 */
6826 void
6827screen_putchar(c, row, col, attr)
6828 int c;
6829 int row, col;
6830 int attr;
6831{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 char_u buf[MB_MAXBYTES + 1];
6833
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006834#ifdef FEAT_MBYTE
6835 if (has_mbyte)
6836 buf[(*mb_char2bytes)(c, buf)] = NUL;
6837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006839 {
6840 buf[0] = c;
6841 buf[1] = NUL;
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 screen_puts(buf, row, col, attr);
6844}
6845
6846/*
6847 * Get a single character directly from ScreenLines into "bytes[]".
6848 * Also return its attribute in *attrp;
6849 */
6850 void
6851screen_getbytes(row, col, bytes, attrp)
6852 int row, col;
6853 char_u *bytes;
6854 int *attrp;
6855{
6856 unsigned off;
6857
6858 /* safety check */
6859 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6860 {
6861 off = LineOffset[row] + col;
6862 *attrp = ScreenAttrs[off];
6863 bytes[0] = ScreenLines[off];
6864 bytes[1] = NUL;
6865
6866#ifdef FEAT_MBYTE
6867 if (enc_utf8 && ScreenLinesUC[off] != 0)
6868 bytes[utfc_char2bytes(off, bytes)] = NUL;
6869 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6870 {
6871 bytes[0] = ScreenLines[off];
6872 bytes[1] = ScreenLines2[off];
6873 bytes[2] = NUL;
6874 }
6875 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6876 {
6877 bytes[1] = ScreenLines[off + 1];
6878 bytes[2] = NUL;
6879 }
6880#endif
6881 }
6882}
6883
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006884#ifdef FEAT_MBYTE
6885static int screen_comp_differs __ARGS((int, int*));
6886
6887/*
6888 * Return TRUE if composing characters for screen posn "off" differs from
6889 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006890 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006891 */
6892 static int
6893screen_comp_differs(off, u8cc)
6894 int off;
6895 int *u8cc;
6896{
6897 int i;
6898
6899 for (i = 0; i < Screen_mco; ++i)
6900 {
6901 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6902 return TRUE;
6903 if (u8cc[i] == 0)
6904 break;
6905 }
6906 return FALSE;
6907}
6908#endif
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910/*
6911 * Put string '*text' on the screen at position 'row' and 'col', with
6912 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6913 * Note: only outputs within one row, message is truncated at screen boundary!
6914 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6915 */
6916 void
6917screen_puts(text, row, col, attr)
6918 char_u *text;
6919 int row;
6920 int col;
6921 int attr;
6922{
6923 screen_puts_len(text, -1, row, col, attr);
6924}
6925
6926/*
6927 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6928 * a NUL.
6929 */
6930 void
6931screen_puts_len(text, len, row, col, attr)
6932 char_u *text;
6933 int len;
6934 int row;
6935 int col;
6936 int attr;
6937{
6938 unsigned off;
6939 char_u *ptr = text;
6940 int c;
6941#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006942 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 int mbyte_blen = 1;
6944 int mbyte_cells = 1;
6945 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006946 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 int clear_next_cell = FALSE;
6948# ifdef FEAT_ARABIC
6949 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006950 int pc, nc, nc1;
6951 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# endif
6953#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006954#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6955 int force_redraw_this;
6956 int force_redraw_next = FALSE;
6957#endif
6958 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6961 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006962 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaarc236c162008-07-13 17:41:49 +00006964#ifdef FEAT_MBYTE
6965 /* When drawing over the right halve of a double-wide char clear out the
6966 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006967 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006968# ifdef FEAT_GUI
6969 && !gui.in_use
6970# endif
6971 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006972 {
6973 ScreenLines[off - 1] = ' ';
6974 ScreenAttrs[off - 1] = 0;
6975 if (enc_utf8)
6976 {
6977 ScreenLinesUC[off - 1] = 0;
6978 ScreenLinesC[0][off - 1] = 0;
6979 }
6980 /* redraw the previous cell, make it empty */
6981 screen_char(off - 1, row, col - 1);
6982 /* force the cell at "col" to be redrawn */
6983 force_redraw_next = TRUE;
6984 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00006985#endif
6986
Bram Moolenaar367329b2007-08-30 11:53:22 +00006987#ifdef FEAT_MBYTE
6988 max_off = LineOffset[row] + screen_Columns;
6989#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00006990 while (col < screen_Columns
6991 && (len < 0 || (int)(ptr - text) < len)
6992 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
6994 c = *ptr;
6995#ifdef FEAT_MBYTE
6996 /* check if this is the first byte of a multibyte */
6997 if (has_mbyte)
6998 {
6999 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007000 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007002 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7004 mbyte_cells = 1;
7005 else if (enc_dbcs != 0)
7006 mbyte_cells = mbyte_blen;
7007 else /* enc_utf8 */
7008 {
7009 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007010 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 (int)((text + len) - ptr));
7012 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007013 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007015# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 /* Non-BMP character: display as ? or fullwidth ?. */
7017 if (u8c >= 0x10000)
7018 {
7019 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7020 if (attr == 0)
7021 attr = hl_attr(HLF_8);
7022 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024# ifdef FEAT_ARABIC
7025 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7026 {
7027 /* Do Arabic shaping. */
7028 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7029 {
7030 /* Past end of string to be displayed. */
7031 nc = NUL;
7032 nc1 = NUL;
7033 }
7034 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007035 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007036 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7037 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007038 nc1 = pcc[0];
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 pc = prev_c;
7041 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007042 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 else
7045 prev_c = u8c;
7046# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007047 if (col + mbyte_cells > screen_Columns)
7048 {
7049 /* Only 1 cell left, but character requires 2 cells:
7050 * display a '>' in the last column to avoid wrapping. */
7051 c = '>';
7052 mbyte_cells = 1;
7053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 }
7056#endif
7057
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007058#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7059 force_redraw_this = force_redraw_next;
7060 force_redraw_next = FALSE;
7061#endif
7062
7063 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064#ifdef FEAT_MBYTE
7065 || (mbyte_cells == 2
7066 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7067 || (enc_dbcs == DBCS_JPNU
7068 && c == 0x8e
7069 && ScreenLines2[off] != ptr[1])
7070 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007071 && (ScreenLinesUC[off] !=
7072 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7073 || (ScreenLinesUC[off] != 0
7074 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075#endif
7076 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007077 || exmode_active;
7078
7079 if (need_redraw
7080#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7081 || force_redraw_this
7082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 )
7084 {
7085#if defined(FEAT_GUI) || defined(UNIX)
7086 /* The bold trick makes a single row of pixels appear in the next
7087 * character. When a bold character is removed, the next
7088 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007089 * and for some xterms. */
7090 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091# ifdef FEAT_GUI
7092 gui.in_use
7093# endif
7094# if defined(FEAT_GUI) && defined(UNIX)
7095 ||
7096# endif
7097# ifdef UNIX
7098 term_is_xterm
7099# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007100 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007102 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007104 if (n > HL_ALL)
7105 n = syn_attr2attr(n);
7106 if (n & HL_BOLD)
7107 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 }
7109#endif
7110#ifdef FEAT_MBYTE
7111 /* When at the end of the text and overwriting a two-cell
7112 * character with a one-cell character, need to clear the next
7113 * cell. Also when overwriting the left halve of a two-cell char
7114 * with the right halve of a two-cell char. Do this only once
7115 * (mb_off2cells() may return 2 on the right halve). */
7116 if (clear_next_cell)
7117 clear_next_cell = FALSE;
7118 else if (has_mbyte
7119 && (len < 0 ? ptr[mbyte_blen] == NUL
7120 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007121 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007123 && (*mb_off2cells)(off, max_off) == 1
7124 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 clear_next_cell = TRUE;
7126
7127 /* Make sure we never leave a second byte of a double-byte behind,
7128 * it confuses mb_off2cells(). */
7129 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007130 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007132 && (*mb_off2cells)(off, max_off) == 1
7133 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 ScreenLines[off + mbyte_blen] = 0;
7135#endif
7136 ScreenLines[off] = c;
7137 ScreenAttrs[off] = attr;
7138#ifdef FEAT_MBYTE
7139 if (enc_utf8)
7140 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007141 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 ScreenLinesUC[off] = 0;
7143 else
7144 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007145 int i;
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007148 for (i = 0; i < Screen_mco; ++i)
7149 {
7150 ScreenLinesC[i][off] = u8cc[i];
7151 if (u8cc[i] == 0)
7152 break;
7153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 }
7155 if (mbyte_cells == 2)
7156 {
7157 ScreenLines[off + 1] = 0;
7158 ScreenAttrs[off + 1] = attr;
7159 }
7160 screen_char(off, row, col);
7161 }
7162 else if (mbyte_cells == 2)
7163 {
7164 ScreenLines[off + 1] = ptr[1];
7165 ScreenAttrs[off + 1] = attr;
7166 screen_char_2(off, row, col);
7167 }
7168 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7169 {
7170 ScreenLines2[off] = ptr[1];
7171 screen_char(off, row, col);
7172 }
7173 else
7174#endif
7175 screen_char(off, row, col);
7176 }
7177#ifdef FEAT_MBYTE
7178 if (has_mbyte)
7179 {
7180 off += mbyte_cells;
7181 col += mbyte_cells;
7182 ptr += mbyte_blen;
7183 if (clear_next_cell)
7184 ptr = (char_u *)" ";
7185 }
7186 else
7187#endif
7188 {
7189 ++off;
7190 ++col;
7191 ++ptr;
7192 }
7193 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007194
7195#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7196 /* If we detected the next character needs to be redrawn, but the text
7197 * doesn't extend up to there, update the character here. */
7198 if (force_redraw_next && col < screen_Columns)
7199 {
7200# ifdef FEAT_MBYTE
7201 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7202 screen_char_2(off, row, col);
7203 else
7204# endif
7205 screen_char(off, row, col);
7206 }
7207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208}
7209
7210#ifdef FEAT_SEARCH_EXTRA
7211/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007212 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 */
7214 static void
7215start_search_hl()
7216{
7217 if (p_hls && !no_hlsearch)
7218 {
7219 last_pat_prog(&search_hl.rm);
7220 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007221# ifdef FEAT_RELTIME
7222 /* Set the time limit to 'redrawtime'. */
7223 profile_setlimit(p_rdt, &search_hl.tm);
7224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226}
7227
7228/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007229 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 */
7231 static void
7232end_search_hl()
7233{
7234 if (search_hl.rm.regprog != NULL)
7235 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007236 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 search_hl.rm.regprog = NULL;
7238 }
7239}
7240
7241/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007242 * Init for calling prepare_search_hl().
7243 */
7244 static void
7245init_search_hl(wp)
7246 win_T *wp;
7247{
7248 matchitem_T *cur;
7249
7250 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7251 * match */
7252 cur = wp->w_match_head;
7253 while (cur != NULL)
7254 {
7255 cur->hl.rm = cur->match;
7256 if (cur->hlg_id == 0)
7257 cur->hl.attr = 0;
7258 else
7259 cur->hl.attr = syn_id2attr(cur->hlg_id);
7260 cur->hl.buf = wp->w_buffer;
7261 cur->hl.lnum = 0;
7262 cur->hl.first_lnum = 0;
7263# ifdef FEAT_RELTIME
7264 /* Set the time limit to 'redrawtime'. */
7265 profile_setlimit(p_rdt, &(cur->hl.tm));
7266# endif
7267 cur = cur->next;
7268 }
7269 search_hl.buf = wp->w_buffer;
7270 search_hl.lnum = 0;
7271 search_hl.first_lnum = 0;
7272 /* time limit is set at the toplevel, for all windows */
7273}
7274
7275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 * Advance to the match in window "wp" line "lnum" or past it.
7277 */
7278 static void
7279prepare_search_hl(wp, lnum)
7280 win_T *wp;
7281 linenr_T lnum;
7282{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007283 matchitem_T *cur; /* points to the match list */
7284 match_T *shl; /* points to search_hl or a match */
7285 int shl_flag; /* flag to indicate whether search_hl
7286 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 int n;
7288
7289 /*
7290 * When using a multi-line pattern, start searching at the top
7291 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007292 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007294 cur = wp->w_match_head;
7295 shl_flag = FALSE;
7296 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007298 if (shl_flag == FALSE)
7299 {
7300 shl = &search_hl;
7301 shl_flag = TRUE;
7302 }
7303 else
7304 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 if (shl->rm.regprog != NULL
7306 && shl->lnum == 0
7307 && re_multiline(shl->rm.regprog))
7308 {
7309 if (shl->first_lnum == 0)
7310 {
7311# ifdef FEAT_FOLDING
7312 for (shl->first_lnum = lnum;
7313 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7314 if (hasFoldingWin(wp, shl->first_lnum - 1,
7315 NULL, NULL, TRUE, NULL))
7316 break;
7317# else
7318 shl->first_lnum = wp->w_topline;
7319# endif
7320 }
7321 n = 0;
7322 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7323 {
7324 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7325 if (shl->lnum != 0)
7326 {
7327 shl->first_lnum = shl->lnum
7328 + shl->rm.endpos[0].lnum
7329 - shl->rm.startpos[0].lnum;
7330 n = shl->rm.endpos[0].col;
7331 }
7332 else
7333 {
7334 ++shl->first_lnum;
7335 n = 0;
7336 }
7337 }
7338 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007339 if (shl != &search_hl && cur != NULL)
7340 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342}
7343
7344/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007345 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 * Uses shl->buf.
7347 * Sets shl->lnum and shl->rm contents.
7348 * Note: Assumes a previous match is always before "lnum", unless
7349 * shl->lnum is zero.
7350 * Careful: Any pointers for buffer lines will become invalid.
7351 */
7352 static void
7353next_search_hl(win, shl, lnum, mincol)
7354 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007355 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 linenr_T lnum;
7357 colnr_T mincol; /* minimal column for a match */
7358{
7359 linenr_T l;
7360 colnr_T matchcol;
7361 long nmatched;
7362
7363 if (shl->lnum != 0)
7364 {
7365 /* Check for three situations:
7366 * 1. If the "lnum" is below a previous match, start a new search.
7367 * 2. If the previous match includes "mincol", use it.
7368 * 3. Continue after the previous match.
7369 */
7370 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7371 if (lnum > l)
7372 shl->lnum = 0;
7373 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7374 return;
7375 }
7376
7377 /*
7378 * Repeat searching for a match until one is found that includes "mincol"
7379 * or none is found in this line.
7380 */
7381 called_emsg = FALSE;
7382 for (;;)
7383 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007384#ifdef FEAT_RELTIME
7385 /* Stop searching after passing the time limit. */
7386 if (profile_passed_limit(&(shl->tm)))
7387 {
7388 shl->lnum = 0; /* no match found in time */
7389 break;
7390 }
7391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 /* Three situations:
7393 * 1. No useful previous match: search from start of line.
7394 * 2. Not Vi compatible or empty match: continue at next character.
7395 * Break the loop if this is beyond the end of the line.
7396 * 3. Vi compatible searching: continue at end of previous match.
7397 */
7398 if (shl->lnum == 0)
7399 matchcol = 0;
7400 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7401 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007402 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007404 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007405
7406 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007407 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007408 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007410 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 shl->lnum = 0;
7412 break;
7413 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007414#ifdef FEAT_MBYTE
7415 if (has_mbyte)
7416 matchcol += mb_ptr2len(ml);
7417 else
7418#endif
7419 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 else
7422 matchcol = shl->rm.endpos[0].col;
7423
7424 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007425 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7426#ifdef FEAT_RELTIME
7427 &(shl->tm)
7428#else
7429 NULL
7430#endif
7431 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007432 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
7434 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007435 if (shl == &search_hl)
7436 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007437 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007438 vim_regfree(shl->rm.regprog);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007439 no_hlsearch = TRUE;
7440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007442 shl->lnum = 0;
7443 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 break;
7445 }
7446 if (nmatched == 0)
7447 {
7448 shl->lnum = 0; /* no match found */
7449 break;
7450 }
7451 if (shl->rm.startpos[0].lnum > 0
7452 || shl->rm.startpos[0].col >= mincol
7453 || nmatched > 1
7454 || shl->rm.endpos[0].col > mincol)
7455 {
7456 shl->lnum += shl->rm.startpos[0].lnum;
7457 break; /* useful match found */
7458 }
7459 }
7460}
7461#endif
7462
7463 static void
7464screen_start_highlight(attr)
7465 int attr;
7466{
7467 attrentry_T *aep = NULL;
7468
7469 screen_attr = attr;
7470 if (full_screen
7471#ifdef WIN3264
7472 && termcap_active
7473#endif
7474 )
7475 {
7476#ifdef FEAT_GUI
7477 if (gui.in_use)
7478 {
7479 char buf[20];
7480
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007481 /* The GUI handles this internally. */
7482 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 OUT_STR(buf);
7484 }
7485 else
7486#endif
7487 {
7488 if (attr > HL_ALL) /* special HL attr. */
7489 {
7490 if (t_colors > 1)
7491 aep = syn_cterm_attr2entry(attr);
7492 else
7493 aep = syn_term_attr2entry(attr);
7494 if (aep == NULL) /* did ":syntax clear" */
7495 attr = 0;
7496 else
7497 attr = aep->ae_attr;
7498 }
7499 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7500 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007501 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7502 && cterm_normal_fg_bold)
7503 /* If the Normal FG color has BOLD attribute and the new HL
7504 * has a FG color defined, clear BOLD. */
7505 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7507 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007508 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7509 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 out_str(T_US);
7511 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7512 out_str(T_CZH);
7513 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7514 out_str(T_MR);
7515
7516 /*
7517 * Output the color or start string after bold etc., in case the
7518 * bold etc. override the color setting.
7519 */
7520 if (aep != NULL)
7521 {
7522 if (t_colors > 1)
7523 {
7524 if (aep->ae_u.cterm.fg_color)
7525 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7526 if (aep->ae_u.cterm.bg_color)
7527 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7528 }
7529 else
7530 {
7531 if (aep->ae_u.term.start != NULL)
7532 out_str(aep->ae_u.term.start);
7533 }
7534 }
7535 }
7536 }
7537}
7538
7539 void
7540screen_stop_highlight()
7541{
7542 int do_ME = FALSE; /* output T_ME code */
7543
7544 if (screen_attr != 0
7545#ifdef WIN3264
7546 && termcap_active
7547#endif
7548 )
7549 {
7550#ifdef FEAT_GUI
7551 if (gui.in_use)
7552 {
7553 char buf[20];
7554
7555 /* use internal GUI code */
7556 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7557 OUT_STR(buf);
7558 }
7559 else
7560#endif
7561 {
7562 if (screen_attr > HL_ALL) /* special HL attr. */
7563 {
7564 attrentry_T *aep;
7565
7566 if (t_colors > 1)
7567 {
7568 /*
7569 * Assume that t_me restores the original colors!
7570 */
7571 aep = syn_cterm_attr2entry(screen_attr);
7572 if (aep != NULL && (aep->ae_u.cterm.fg_color
7573 || aep->ae_u.cterm.bg_color))
7574 do_ME = TRUE;
7575 }
7576 else
7577 {
7578 aep = syn_term_attr2entry(screen_attr);
7579 if (aep != NULL && aep->ae_u.term.stop != NULL)
7580 {
7581 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7582 do_ME = TRUE;
7583 else
7584 out_str(aep->ae_u.term.stop);
7585 }
7586 }
7587 if (aep == NULL) /* did ":syntax clear" */
7588 screen_attr = 0;
7589 else
7590 screen_attr = aep->ae_attr;
7591 }
7592
7593 /*
7594 * Often all ending-codes are equal to T_ME. Avoid outputting the
7595 * same sequence several times.
7596 */
7597 if (screen_attr & HL_STANDOUT)
7598 {
7599 if (STRCMP(T_SE, T_ME) == 0)
7600 do_ME = TRUE;
7601 else
7602 out_str(T_SE);
7603 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007604 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
7606 if (STRCMP(T_UE, T_ME) == 0)
7607 do_ME = TRUE;
7608 else
7609 out_str(T_UE);
7610 }
7611 if (screen_attr & HL_ITALIC)
7612 {
7613 if (STRCMP(T_CZR, T_ME) == 0)
7614 do_ME = TRUE;
7615 else
7616 out_str(T_CZR);
7617 }
7618 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7619 out_str(T_ME);
7620
7621 if (t_colors > 1)
7622 {
7623 /* set Normal cterm colors */
7624 if (cterm_normal_fg_color != 0)
7625 term_fg_color(cterm_normal_fg_color - 1);
7626 if (cterm_normal_bg_color != 0)
7627 term_bg_color(cterm_normal_bg_color - 1);
7628 if (cterm_normal_fg_bold)
7629 out_str(T_MD);
7630 }
7631 }
7632 }
7633 screen_attr = 0;
7634}
7635
7636/*
7637 * Reset the colors for a cterm. Used when leaving Vim.
7638 * The machine specific code may override this again.
7639 */
7640 void
7641reset_cterm_colors()
7642{
7643 if (t_colors > 1)
7644 {
7645 /* set Normal cterm colors */
7646 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7647 {
7648 out_str(T_OP);
7649 screen_attr = -1;
7650 }
7651 if (cterm_normal_fg_bold)
7652 {
7653 out_str(T_ME);
7654 screen_attr = -1;
7655 }
7656 }
7657}
7658
7659/*
7660 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7661 * using the attributes from ScreenAttrs["off"].
7662 */
7663 static void
7664screen_char(off, row, col)
7665 unsigned off;
7666 int row;
7667 int col;
7668{
7669 int attr;
7670
7671 /* Check for illegal values, just in case (could happen just after
7672 * resizing). */
7673 if (row >= screen_Rows || col >= screen_Columns)
7674 return;
7675
7676 /* Outputting the last character on the screen may scrollup the screen.
7677 * Don't to it! Mark the character invalid (update it when scrolled up) */
7678 if (row == screen_Rows - 1 && col == screen_Columns - 1
7679#ifdef FEAT_RIGHTLEFT
7680 /* account for first command-line character in rightleft mode */
7681 && !cmdmsg_rl
7682#endif
7683 )
7684 {
7685 ScreenAttrs[off] = (sattr_T)-1;
7686 return;
7687 }
7688
7689 /*
7690 * Stop highlighting first, so it's easier to move the cursor.
7691 */
7692#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7693 if (screen_char_attr != 0)
7694 attr = screen_char_attr;
7695 else
7696#endif
7697 attr = ScreenAttrs[off];
7698 if (screen_attr != attr)
7699 screen_stop_highlight();
7700
7701 windgoto(row, col);
7702
7703 if (screen_attr != attr)
7704 screen_start_highlight(attr);
7705
7706#ifdef FEAT_MBYTE
7707 if (enc_utf8 && ScreenLinesUC[off] != 0)
7708 {
7709 char_u buf[MB_MAXBYTES + 1];
7710
7711 /* Convert UTF-8 character to bytes and write it. */
7712
7713 buf[utfc_char2bytes(off, buf)] = NUL;
7714
7715 out_str(buf);
7716 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7717 ++screen_cur_col;
7718 }
7719 else
7720#endif
7721 {
7722#ifdef FEAT_MBYTE
7723 out_flush_check();
7724#endif
7725 out_char(ScreenLines[off]);
7726#ifdef FEAT_MBYTE
7727 /* double-byte character in single-width cell */
7728 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7729 out_char(ScreenLines2[off]);
7730#endif
7731 }
7732
7733 screen_cur_col++;
7734}
7735
7736#ifdef FEAT_MBYTE
7737
7738/*
7739 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7740 * on the screen at position 'row' and 'col'.
7741 * The attributes of the first byte is used for all. This is required to
7742 * output the two bytes of a double-byte character with nothing in between.
7743 */
7744 static void
7745screen_char_2(off, row, col)
7746 unsigned off;
7747 int row;
7748 int col;
7749{
7750 /* Check for illegal values (could be wrong when screen was resized). */
7751 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7752 return;
7753
7754 /* Outputting the last character on the screen may scrollup the screen.
7755 * Don't to it! Mark the character invalid (update it when scrolled up) */
7756 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7757 {
7758 ScreenAttrs[off] = (sattr_T)-1;
7759 return;
7760 }
7761
7762 /* Output the first byte normally (positions the cursor), then write the
7763 * second byte directly. */
7764 screen_char(off, row, col);
7765 out_char(ScreenLines[off + 1]);
7766 ++screen_cur_col;
7767}
7768#endif
7769
7770#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7771/*
7772 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7773 * This uses the contents of ScreenLines[] and doesn't change it.
7774 */
7775 void
7776screen_draw_rectangle(row, col, height, width, invert)
7777 int row;
7778 int col;
7779 int height;
7780 int width;
7781 int invert;
7782{
7783 int r, c;
7784 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007785#ifdef FEAT_MBYTE
7786 int max_off;
7787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007789 /* Can't use ScreenLines unless initialized */
7790 if (ScreenLines == NULL)
7791 return;
7792
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (invert)
7794 screen_char_attr = HL_INVERSE;
7795 for (r = row; r < row + height; ++r)
7796 {
7797 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007798#ifdef FEAT_MBYTE
7799 max_off = off + screen_Columns;
7800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 for (c = col; c < col + width; ++c)
7802 {
7803#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007804 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 screen_char_2(off + c, r, c);
7807 ++c;
7808 }
7809 else
7810#endif
7811 {
7812 screen_char(off + c, r, c);
7813#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007814 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 ++c;
7816#endif
7817 }
7818 }
7819 }
7820 screen_char_attr = 0;
7821}
7822#endif
7823
7824#ifdef FEAT_VERTSPLIT
7825/*
7826 * Redraw the characters for a vertically split window.
7827 */
7828 static void
7829redraw_block(row, end, wp)
7830 int row;
7831 int end;
7832 win_T *wp;
7833{
7834 int col;
7835 int width;
7836
7837# ifdef FEAT_CLIPBOARD
7838 clip_may_clear_selection(row, end - 1);
7839# endif
7840
7841 if (wp == NULL)
7842 {
7843 col = 0;
7844 width = Columns;
7845 }
7846 else
7847 {
7848 col = wp->w_wincol;
7849 width = wp->w_width;
7850 }
7851 screen_draw_rectangle(row, col, end - row, width, FALSE);
7852}
7853#endif
7854
7855/*
7856 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7857 * with character 'c1' in first column followed by 'c2' in the other columns.
7858 * Use attributes 'attr'.
7859 */
7860 void
7861screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7862 int start_row, end_row;
7863 int start_col, end_col;
7864 int c1, c2;
7865 int attr;
7866{
7867 int row;
7868 int col;
7869 int off;
7870 int end_off;
7871 int did_delete;
7872 int c;
7873 int norm_term;
7874#if defined(FEAT_GUI) || defined(UNIX)
7875 int force_next = FALSE;
7876#endif
7877
7878 if (end_row > screen_Rows) /* safety check */
7879 end_row = screen_Rows;
7880 if (end_col > screen_Columns) /* safety check */
7881 end_col = screen_Columns;
7882 if (ScreenLines == NULL
7883 || start_row >= end_row
7884 || start_col >= end_col) /* nothing to do */
7885 return;
7886
7887 /* it's a "normal" terminal when not in a GUI or cterm */
7888 norm_term = (
7889#ifdef FEAT_GUI
7890 !gui.in_use &&
7891#endif
7892 t_colors <= 1);
7893 for (row = start_row; row < end_row; ++row)
7894 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007895#ifdef FEAT_MBYTE
7896 if (has_mbyte
7897# ifdef FEAT_GUI
7898 && !gui.in_use
7899# endif
7900 )
7901 {
7902 /* When drawing over the right halve of a double-wide char clear
7903 * out the left halve. When drawing over the left halve of a
7904 * double wide-char clear out the right halve. Only needed in a
7905 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007906 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007907 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007908 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007909 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007910 }
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 /*
7913 * Try to use delete-line termcap code, when no attributes or in a
7914 * "normal" terminal, where a bold/italic space is just a
7915 * space.
7916 */
7917 did_delete = FALSE;
7918 if (c2 == ' '
7919 && end_col == Columns
7920 && can_clear(T_CE)
7921 && (attr == 0
7922 || (norm_term
7923 && attr <= HL_ALL
7924 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7925 {
7926 /*
7927 * check if we really need to clear something
7928 */
7929 col = start_col;
7930 if (c1 != ' ') /* don't clear first char */
7931 ++col;
7932
7933 off = LineOffset[row] + col;
7934 end_off = LineOffset[row] + end_col;
7935
7936 /* skip blanks (used often, keep it fast!) */
7937#ifdef FEAT_MBYTE
7938 if (enc_utf8)
7939 while (off < end_off && ScreenLines[off] == ' '
7940 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7941 ++off;
7942 else
7943#endif
7944 while (off < end_off && ScreenLines[off] == ' '
7945 && ScreenAttrs[off] == 0)
7946 ++off;
7947 if (off < end_off) /* something to be cleared */
7948 {
7949 col = off - LineOffset[row];
7950 screen_stop_highlight();
7951 term_windgoto(row, col);/* clear rest of this screen line */
7952 out_str(T_CE);
7953 screen_start(); /* don't know where cursor is now */
7954 col = end_col - col;
7955 while (col--) /* clear chars in ScreenLines */
7956 {
7957 ScreenLines[off] = ' ';
7958#ifdef FEAT_MBYTE
7959 if (enc_utf8)
7960 ScreenLinesUC[off] = 0;
7961#endif
7962 ScreenAttrs[off] = 0;
7963 ++off;
7964 }
7965 }
7966 did_delete = TRUE; /* the chars are cleared now */
7967 }
7968
7969 off = LineOffset[row] + start_col;
7970 c = c1;
7971 for (col = start_col; col < end_col; ++col)
7972 {
7973 if (ScreenLines[off] != c
7974#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007975 || (enc_utf8 && (int)ScreenLinesUC[off]
7976 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#endif
7978 || ScreenAttrs[off] != attr
7979#if defined(FEAT_GUI) || defined(UNIX)
7980 || force_next
7981#endif
7982 )
7983 {
7984#if defined(FEAT_GUI) || defined(UNIX)
7985 /* The bold trick may make a single row of pixels appear in
7986 * the next character. When a bold character is removed, the
7987 * next character should be redrawn too. This happens for our
7988 * own GUI and for some xterms. */
7989 if (
7990# ifdef FEAT_GUI
7991 gui.in_use
7992# endif
7993# if defined(FEAT_GUI) && defined(UNIX)
7994 ||
7995# endif
7996# ifdef UNIX
7997 term_is_xterm
7998# endif
7999 )
8000 {
8001 if (ScreenLines[off] != ' '
8002 && (ScreenAttrs[off] > HL_ALL
8003 || ScreenAttrs[off] & HL_BOLD))
8004 force_next = TRUE;
8005 else
8006 force_next = FALSE;
8007 }
8008#endif
8009 ScreenLines[off] = c;
8010#ifdef FEAT_MBYTE
8011 if (enc_utf8)
8012 {
8013 if (c >= 0x80)
8014 {
8015 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008016 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 }
8018 else
8019 ScreenLinesUC[off] = 0;
8020 }
8021#endif
8022 ScreenAttrs[off] = attr;
8023 if (!did_delete || c != ' ')
8024 screen_char(off, row, col);
8025 }
8026 ++off;
8027 if (col == start_col)
8028 {
8029 if (did_delete)
8030 break;
8031 c = c2;
8032 }
8033 }
8034 if (end_col == Columns)
8035 LineWraps[row] = FALSE;
8036 if (row == Rows - 1) /* overwritten the command line */
8037 {
8038 redraw_cmdline = TRUE;
8039 if (c1 == ' ' && c2 == ' ')
8040 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008041 if (start_col == 0)
8042 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 }
8045}
8046
8047/*
8048 * Check if there should be a delay. Used before clearing or redrawing the
8049 * screen or the command line.
8050 */
8051 void
8052check_for_delay(check_msg_scroll)
8053 int check_msg_scroll;
8054{
8055 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8056 && !did_wait_return
8057 && emsg_silent == 0)
8058 {
8059 out_flush();
8060 ui_delay(1000L, TRUE);
8061 emsg_on_display = FALSE;
8062 if (check_msg_scroll)
8063 msg_scroll = FALSE;
8064 }
8065}
8066
8067/*
8068 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008069 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 * Returns TRUE if there is a valid screen to write to.
8071 * Returns FALSE when starting up and screen not initialized yet.
8072 */
8073 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008074screen_valid(doclear)
8075 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008077 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 return (ScreenLines != NULL);
8079}
8080
8081/*
8082 * Resize the shell to Rows and Columns.
8083 * Allocate ScreenLines[] and associated items.
8084 *
8085 * There may be some time between setting Rows and Columns and (re)allocating
8086 * ScreenLines[]. This happens when starting up and when (manually) changing
8087 * the shell size. Always use screen_Rows and screen_Columns to access items
8088 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8089 * final size of the shell is needed.
8090 */
8091 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008092screenalloc(doclear)
8093 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 int new_row, old_row;
8096#ifdef FEAT_GUI
8097 int old_Rows;
8098#endif
8099 win_T *wp;
8100 int outofmem = FALSE;
8101 int len;
8102 schar_T *new_ScreenLines;
8103#ifdef FEAT_MBYTE
8104 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008105 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008107 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108#endif
8109 sattr_T *new_ScreenAttrs;
8110 unsigned *new_LineOffset;
8111 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008112#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008113 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008114 tabpage_T *tp;
8115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008117 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008118#ifdef FEAT_AUTOCMD
8119 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008121retry:
8122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 /*
8124 * Allocation of the screen buffers is done only when the size changes and
8125 * when Rows and Columns have been set and we have started doing full
8126 * screen stuff.
8127 */
8128 if ((ScreenLines != NULL
8129 && Rows == screen_Rows
8130 && Columns == screen_Columns
8131#ifdef FEAT_MBYTE
8132 && enc_utf8 == (ScreenLinesUC != NULL)
8133 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008134 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#endif
8136 )
8137 || Rows == 0
8138 || Columns == 0
8139 || (!full_screen && ScreenLines == NULL))
8140 return;
8141
8142 /*
8143 * It's possible that we produce an out-of-memory message below, which
8144 * will cause this function to be called again. To break the loop, just
8145 * return here.
8146 */
8147 if (entered)
8148 return;
8149 entered = TRUE;
8150
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008151 /*
8152 * Note that the window sizes are updated before reallocating the arrays,
8153 * thus we must not redraw here!
8154 */
8155 ++RedrawingDisabled;
8156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 win_new_shellsize(); /* fit the windows in the new sized shell */
8158
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 comp_col(); /* recompute columns for shown command and ruler */
8160
8161 /*
8162 * We're changing the size of the screen.
8163 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8164 * - Move lines from the old arrays into the new arrays, clear extra
8165 * lines (unless the screen is going to be cleared).
8166 * - Free the old arrays.
8167 *
8168 * If anything fails, make ScreenLines NULL, so we don't do anything!
8169 * Continuing with the old ScreenLines may result in a crash, because the
8170 * size is wrong.
8171 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008172 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008174#ifdef FEAT_AUTOCMD
8175 if (aucmd_win != NULL)
8176 win_free_lsize(aucmd_win);
8177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 new_ScreenLines = (schar_T *)lalloc((long_u)(
8180 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8181#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008182 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (enc_utf8)
8184 {
8185 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8186 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008187 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008188 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8190 }
8191 if (enc_dbcs == DBCS_JPNU)
8192 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8193 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8194#endif
8195 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8196 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8197 new_LineOffset = (unsigned *)lalloc((long_u)(
8198 Rows * sizeof(unsigned)), FALSE);
8199 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008200#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008201 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008204 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {
8206 if (win_alloc_lines(wp) == FAIL)
8207 {
8208 outofmem = TRUE;
8209#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008210 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211#endif
8212 }
8213 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008214#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008215 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8216 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008217 outofmem = TRUE;
8218#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008219#ifdef FEAT_WINDOWS
8220give_up:
8221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008223#ifdef FEAT_MBYTE
8224 for (i = 0; i < p_mco; ++i)
8225 if (new_ScreenLinesC[i] == NULL)
8226 break;
8227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 if (new_ScreenLines == NULL
8229#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008230 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8232#endif
8233 || new_ScreenAttrs == NULL
8234 || new_LineOffset == NULL
8235 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008236#ifdef FEAT_WINDOWS
8237 || new_TabPageIdxs == NULL
8238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 || outofmem)
8240 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008241 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008242 {
8243 /* guess the size */
8244 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8245
8246 /* Remember we did this to avoid getting outofmem messages over
8247 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008248 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 vim_free(new_ScreenLines);
8251 new_ScreenLines = NULL;
8252#ifdef FEAT_MBYTE
8253 vim_free(new_ScreenLinesUC);
8254 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008255 for (i = 0; i < p_mco; ++i)
8256 {
8257 vim_free(new_ScreenLinesC[i]);
8258 new_ScreenLinesC[i] = NULL;
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 vim_free(new_ScreenLines2);
8261 new_ScreenLines2 = NULL;
8262#endif
8263 vim_free(new_ScreenAttrs);
8264 new_ScreenAttrs = NULL;
8265 vim_free(new_LineOffset);
8266 new_LineOffset = NULL;
8267 vim_free(new_LineWraps);
8268 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008269#ifdef FEAT_WINDOWS
8270 vim_free(new_TabPageIdxs);
8271 new_TabPageIdxs = NULL;
8272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 }
8274 else
8275 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008276 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008277
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 for (new_row = 0; new_row < Rows; ++new_row)
8279 {
8280 new_LineOffset[new_row] = new_row * Columns;
8281 new_LineWraps[new_row] = FALSE;
8282
8283 /*
8284 * If the screen is not going to be cleared, copy as much as
8285 * possible from the old screen to the new one and clear the rest
8286 * (used when resizing the window at the "--more--" prompt or when
8287 * executing an external command, for the GUI).
8288 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008289 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
8291 (void)vim_memset(new_ScreenLines + new_row * Columns,
8292 ' ', (size_t)Columns * sizeof(schar_T));
8293#ifdef FEAT_MBYTE
8294 if (enc_utf8)
8295 {
8296 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8297 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008298 for (i = 0; i < p_mco; ++i)
8299 (void)vim_memset(new_ScreenLinesC[i]
8300 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 0, (size_t)Columns * sizeof(u8char_T));
8302 }
8303 if (enc_dbcs == DBCS_JPNU)
8304 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8305 0, (size_t)Columns * sizeof(schar_T));
8306#endif
8307 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8308 0, (size_t)Columns * sizeof(sattr_T));
8309 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008310 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
8312 if (screen_Columns < Columns)
8313 len = screen_Columns;
8314 else
8315 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008316#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008317 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008318 * may be invalid now. Also when p_mco changes. */
8319 if (!(enc_utf8 && ScreenLinesUC == NULL)
8320 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008321#endif
8322 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8323 ScreenLines + LineOffset[old_row],
8324 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008326 if (enc_utf8 && ScreenLinesUC != NULL
8327 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
8329 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8330 ScreenLinesUC + LineOffset[old_row],
8331 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008332 for (i = 0; i < p_mco; ++i)
8333 mch_memmove(new_ScreenLinesC[i]
8334 + new_LineOffset[new_row],
8335 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 (size_t)len * sizeof(u8char_T));
8337 }
8338 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8339 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8340 ScreenLines2 + LineOffset[old_row],
8341 (size_t)len * sizeof(schar_T));
8342#endif
8343 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8344 ScreenAttrs + LineOffset[old_row],
8345 (size_t)len * sizeof(sattr_T));
8346 }
8347 }
8348 }
8349 /* Use the last line of the screen for the current line. */
8350 current_ScreenLine = new_ScreenLines + Rows * Columns;
8351 }
8352
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008353 free_screenlines();
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 ScreenLines = new_ScreenLines;
8356#ifdef FEAT_MBYTE
8357 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008358 for (i = 0; i < p_mco; ++i)
8359 ScreenLinesC[i] = new_ScreenLinesC[i];
8360 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 ScreenLines2 = new_ScreenLines2;
8362#endif
8363 ScreenAttrs = new_ScreenAttrs;
8364 LineOffset = new_LineOffset;
8365 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008366#ifdef FEAT_WINDOWS
8367 TabPageIdxs = new_TabPageIdxs;
8368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
8370 /* It's important that screen_Rows and screen_Columns reflect the actual
8371 * size of ScreenLines[]. Set them before calling anything. */
8372#ifdef FEAT_GUI
8373 old_Rows = screen_Rows;
8374#endif
8375 screen_Rows = Rows;
8376 screen_Columns = Columns;
8377
8378 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008379 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 screenclear2();
8381
8382#ifdef FEAT_GUI
8383 else if (gui.in_use
8384 && !gui.starting
8385 && ScreenLines != NULL
8386 && old_Rows != Rows)
8387 {
8388 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8389 /*
8390 * Adjust the position of the cursor, for when executing an external
8391 * command.
8392 */
8393 if (msg_row >= Rows) /* Rows got smaller */
8394 msg_row = Rows - 1; /* put cursor at last row */
8395 else if (Rows > old_Rows) /* Rows got bigger */
8396 msg_row += Rows - old_Rows; /* put cursor in same place */
8397 if (msg_col >= Columns) /* Columns got smaller */
8398 msg_col = Columns - 1; /* put cursor at last column */
8399 }
8400#endif
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008403 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008404
8405#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008406 /*
8407 * Do not apply autocommands more than 3 times to avoid an endless loop
8408 * in case applying autocommands always changes Rows or Columns.
8409 */
8410 if (starting == 0 && ++retry_count <= 3)
8411 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008412 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008413 /* In rare cases, autocommands may have altered Rows or Columns,
8414 * jump back to check if we need to allocate the screen again. */
8415 goto retry;
8416 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418}
8419
8420 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008421free_screenlines()
8422{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008423#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008424 int i;
8425
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008426 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008427 for (i = 0; i < Screen_mco; ++i)
8428 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008429 vim_free(ScreenLines2);
8430#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008431 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008432 vim_free(ScreenAttrs);
8433 vim_free(LineOffset);
8434 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008435#ifdef FEAT_WINDOWS
8436 vim_free(TabPageIdxs);
8437#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008438}
8439
8440 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441screenclear()
8442{
8443 check_for_delay(FALSE);
8444 screenalloc(FALSE); /* allocate screen buffers if size changed */
8445 screenclear2(); /* clear the screen */
8446}
8447
8448 static void
8449screenclear2()
8450{
8451 int i;
8452
8453 if (starting == NO_SCREEN || ScreenLines == NULL
8454#ifdef FEAT_GUI
8455 || (gui.in_use && gui.starting)
8456#endif
8457 )
8458 return;
8459
8460#ifdef FEAT_GUI
8461 if (!gui.in_use)
8462#endif
8463 screen_attr = -1; /* force setting the Normal colors */
8464 screen_stop_highlight(); /* don't want highlighting here */
8465
8466#ifdef FEAT_CLIPBOARD
8467 /* disable selection without redrawing it */
8468 clip_scroll_selection(9999);
8469#endif
8470
8471 /* blank out ScreenLines */
8472 for (i = 0; i < Rows; ++i)
8473 {
8474 lineclear(LineOffset[i], (int)Columns);
8475 LineWraps[i] = FALSE;
8476 }
8477
8478 if (can_clear(T_CL))
8479 {
8480 out_str(T_CL); /* clear the display */
8481 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008482 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 else
8485 {
8486 /* can't clear the screen, mark all chars with invalid attributes */
8487 for (i = 0; i < Rows; ++i)
8488 lineinvalid(LineOffset[i], (int)Columns);
8489 clear_cmdline = TRUE;
8490 }
8491
8492 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8493
8494 win_rest_invalid(firstwin);
8495 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008496#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008497 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (must_redraw == CLEAR) /* no need to clear again */
8500 must_redraw = NOT_VALID;
8501 compute_cmdrow();
8502 msg_row = cmdline_row; /* put cursor on last line for messages */
8503 msg_col = 0;
8504 screen_start(); /* don't know where cursor is now */
8505 msg_scrolled = 0; /* can't scroll back */
8506 msg_didany = FALSE;
8507 msg_didout = FALSE;
8508}
8509
8510/*
8511 * Clear one line in ScreenLines.
8512 */
8513 static void
8514lineclear(off, width)
8515 unsigned off;
8516 int width;
8517{
8518 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8519#ifdef FEAT_MBYTE
8520 if (enc_utf8)
8521 (void)vim_memset(ScreenLinesUC + off, 0,
8522 (size_t)width * sizeof(u8char_T));
8523#endif
8524 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8525}
8526
8527/*
8528 * Mark one line in ScreenLines invalid by setting the attributes to an
8529 * invalid value.
8530 */
8531 static void
8532lineinvalid(off, width)
8533 unsigned off;
8534 int width;
8535{
8536 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8537}
8538
8539#ifdef FEAT_VERTSPLIT
8540/*
8541 * Copy part of a Screenline for vertically split window "wp".
8542 */
8543 static void
8544linecopy(to, from, wp)
8545 int to;
8546 int from;
8547 win_T *wp;
8548{
8549 unsigned off_to = LineOffset[to] + wp->w_wincol;
8550 unsigned off_from = LineOffset[from] + wp->w_wincol;
8551
8552 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8553 wp->w_width * sizeof(schar_T));
8554# ifdef FEAT_MBYTE
8555 if (enc_utf8)
8556 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008557 int i;
8558
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8560 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008561 for (i = 0; i < p_mco; ++i)
8562 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8563 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
8565 if (enc_dbcs == DBCS_JPNU)
8566 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8567 wp->w_width * sizeof(schar_T));
8568# endif
8569 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8570 wp->w_width * sizeof(sattr_T));
8571}
8572#endif
8573
8574/*
8575 * Return TRUE if clearing with term string "p" would work.
8576 * It can't work when the string is empty or it won't set the right background.
8577 */
8578 int
8579can_clear(p)
8580 char_u *p;
8581{
8582 return (*p != NUL && (t_colors <= 1
8583#ifdef FEAT_GUI
8584 || gui.in_use
8585#endif
8586 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8587}
8588
8589/*
8590 * Reset cursor position. Use whenever cursor was moved because of outputting
8591 * something directly to the screen (shell commands) or a terminal control
8592 * code.
8593 */
8594 void
8595screen_start()
8596{
8597 screen_cur_row = screen_cur_col = 9999;
8598}
8599
8600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 * Move the cursor to position "row","col" in the screen.
8602 * This tries to find the most efficient way to move, minimizing the number of
8603 * characters sent to the terminal.
8604 */
8605 void
8606windgoto(row, col)
8607 int row;
8608 int col;
8609{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008610 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 int i;
8612 int plan;
8613 int cost;
8614 int wouldbe_col;
8615 int noinvcurs;
8616 char_u *bs;
8617 int goto_cost;
8618 int attr;
8619
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008620#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8622
8623#define PLAN_LE 1
8624#define PLAN_CR 2
8625#define PLAN_NL 3
8626#define PLAN_WRITE 4
8627 /* Can't use ScreenLines unless initialized */
8628 if (ScreenLines == NULL)
8629 return;
8630
8631 if (col != screen_cur_col || row != screen_cur_row)
8632 {
8633 /* Check for valid position. */
8634 if (row < 0) /* window without text lines? */
8635 row = 0;
8636 if (row >= screen_Rows)
8637 row = screen_Rows - 1;
8638 if (col >= screen_Columns)
8639 col = screen_Columns - 1;
8640
8641 /* check if no cursor movement is allowed in highlight mode */
8642 if (screen_attr && *T_MS == NUL)
8643 noinvcurs = HIGHL_COST;
8644 else
8645 noinvcurs = 0;
8646 goto_cost = GOTO_COST + noinvcurs;
8647
8648 /*
8649 * Plan how to do the positioning:
8650 * 1. Use CR to move it to column 0, same row.
8651 * 2. Use T_LE to move it a few columns to the left.
8652 * 3. Use NL to move a few lines down, column 0.
8653 * 4. Move a few columns to the right with T_ND or by writing chars.
8654 *
8655 * Don't do this if the cursor went beyond the last column, the cursor
8656 * position is unknown then (some terminals wrap, some don't )
8657 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008658 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 * characters to move the cursor to the right.
8660 */
8661 if (row >= screen_cur_row && screen_cur_col < Columns)
8662 {
8663 /*
8664 * If the cursor is in the same row, bigger col, we can use CR
8665 * or T_LE.
8666 */
8667 bs = NULL; /* init for GCC */
8668 attr = screen_attr;
8669 if (row == screen_cur_row && col < screen_cur_col)
8670 {
8671 /* "le" is preferred over "bc", because "bc" is obsolete */
8672 if (*T_LE)
8673 bs = T_LE; /* "cursor left" */
8674 else
8675 bs = T_BC; /* "backspace character (old) */
8676 if (*bs)
8677 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8678 else
8679 cost = 999;
8680 if (col + 1 < cost) /* using CR is less characters */
8681 {
8682 plan = PLAN_CR;
8683 wouldbe_col = 0;
8684 cost = 1; /* CR is just one character */
8685 }
8686 else
8687 {
8688 plan = PLAN_LE;
8689 wouldbe_col = col;
8690 }
8691 if (noinvcurs) /* will stop highlighting */
8692 {
8693 cost += noinvcurs;
8694 attr = 0;
8695 }
8696 }
8697
8698 /*
8699 * If the cursor is above where we want to be, we can use CR LF.
8700 */
8701 else if (row > screen_cur_row)
8702 {
8703 plan = PLAN_NL;
8704 wouldbe_col = 0;
8705 cost = (row - screen_cur_row) * 2; /* CR LF */
8706 if (noinvcurs) /* will stop highlighting */
8707 {
8708 cost += noinvcurs;
8709 attr = 0;
8710 }
8711 }
8712
8713 /*
8714 * If the cursor is in the same row, smaller col, just use write.
8715 */
8716 else
8717 {
8718 plan = PLAN_WRITE;
8719 wouldbe_col = screen_cur_col;
8720 cost = 0;
8721 }
8722
8723 /*
8724 * Check if any characters that need to be written have the
8725 * correct attributes. Also avoid UTF-8 characters.
8726 */
8727 i = col - wouldbe_col;
8728 if (i > 0)
8729 cost += i;
8730 if (cost < goto_cost && i > 0)
8731 {
8732 /*
8733 * Check if the attributes are correct without additionally
8734 * stopping highlighting.
8735 */
8736 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8737 while (i && *p++ == attr)
8738 --i;
8739 if (i != 0)
8740 {
8741 /*
8742 * Try if it works when highlighting is stopped here.
8743 */
8744 if (*--p == 0)
8745 {
8746 cost += noinvcurs;
8747 while (i && *p++ == 0)
8748 --i;
8749 }
8750 if (i != 0)
8751 cost = 999; /* different attributes, don't do it */
8752 }
8753#ifdef FEAT_MBYTE
8754 if (enc_utf8)
8755 {
8756 /* Don't use an UTF-8 char for positioning, it's slow. */
8757 for (i = wouldbe_col; i < col; ++i)
8758 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8759 {
8760 cost = 999;
8761 break;
8762 }
8763 }
8764#endif
8765 }
8766
8767 /*
8768 * We can do it without term_windgoto()!
8769 */
8770 if (cost < goto_cost)
8771 {
8772 if (plan == PLAN_LE)
8773 {
8774 if (noinvcurs)
8775 screen_stop_highlight();
8776 while (screen_cur_col > col)
8777 {
8778 out_str(bs);
8779 --screen_cur_col;
8780 }
8781 }
8782 else if (plan == PLAN_CR)
8783 {
8784 if (noinvcurs)
8785 screen_stop_highlight();
8786 out_char('\r');
8787 screen_cur_col = 0;
8788 }
8789 else if (plan == PLAN_NL)
8790 {
8791 if (noinvcurs)
8792 screen_stop_highlight();
8793 while (screen_cur_row < row)
8794 {
8795 out_char('\n');
8796 ++screen_cur_row;
8797 }
8798 screen_cur_col = 0;
8799 }
8800
8801 i = col - screen_cur_col;
8802 if (i > 0)
8803 {
8804 /*
8805 * Use cursor-right if it's one character only. Avoids
8806 * removing a line of pixels from the last bold char, when
8807 * using the bold trick in the GUI.
8808 */
8809 if (T_ND[0] != NUL && T_ND[1] == NUL)
8810 {
8811 while (i-- > 0)
8812 out_char(*T_ND);
8813 }
8814 else
8815 {
8816 int off;
8817
8818 off = LineOffset[row] + screen_cur_col;
8819 while (i-- > 0)
8820 {
8821 if (ScreenAttrs[off] != screen_attr)
8822 screen_stop_highlight();
8823#ifdef FEAT_MBYTE
8824 out_flush_check();
8825#endif
8826 out_char(ScreenLines[off]);
8827#ifdef FEAT_MBYTE
8828 if (enc_dbcs == DBCS_JPNU
8829 && ScreenLines[off] == 0x8e)
8830 out_char(ScreenLines2[off]);
8831#endif
8832 ++off;
8833 }
8834 }
8835 }
8836 }
8837 }
8838 else
8839 cost = 999;
8840
8841 if (cost >= goto_cost)
8842 {
8843 if (noinvcurs)
8844 screen_stop_highlight();
8845 if (row == screen_cur_row && (col > screen_cur_col) &&
8846 *T_CRI != NUL)
8847 term_cursor_right(col - screen_cur_col);
8848 else
8849 term_windgoto(row, col);
8850 }
8851 screen_cur_row = row;
8852 screen_cur_col = col;
8853 }
8854}
8855
8856/*
8857 * Set cursor to its position in the current window.
8858 */
8859 void
8860setcursor()
8861{
8862 if (redrawing())
8863 {
8864 validate_cursor();
8865 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8866 W_WINCOL(curwin) + (
8867#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008868 /* With 'rightleft' set and the cursor on a double-wide
8869 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8871# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008872 (has_mbyte
8873 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8874 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875# endif
8876 1)) :
8877#endif
8878 curwin->w_wcol));
8879 }
8880}
8881
8882
8883/*
8884 * insert 'line_count' lines at 'row' in window 'wp'
8885 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8886 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8887 * scrolling.
8888 * Returns FAIL if the lines are not inserted, OK for success.
8889 */
8890 int
8891win_ins_lines(wp, row, line_count, invalid, mayclear)
8892 win_T *wp;
8893 int row;
8894 int line_count;
8895 int invalid;
8896 int mayclear;
8897{
8898 int did_delete;
8899 int nextrow;
8900 int lastrow;
8901 int retval;
8902
8903 if (invalid)
8904 wp->w_lines_valid = 0;
8905
8906 if (wp->w_height < 5)
8907 return FAIL;
8908
8909 if (line_count > wp->w_height - row)
8910 line_count = wp->w_height - row;
8911
8912 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8913 if (retval != MAYBE)
8914 return retval;
8915
8916 /*
8917 * If there is a next window or a status line, we first try to delete the
8918 * lines at the bottom to avoid messing what is after the window.
8919 * If this fails and there are following windows, don't do anything to avoid
8920 * messing up those windows, better just redraw.
8921 */
8922 did_delete = FALSE;
8923#ifdef FEAT_WINDOWS
8924 if (wp->w_next != NULL || wp->w_status_height)
8925 {
8926 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8927 line_count, (int)Rows, FALSE, NULL) == OK)
8928 did_delete = TRUE;
8929 else if (wp->w_next)
8930 return FAIL;
8931 }
8932#endif
8933 /*
8934 * if no lines deleted, blank the lines that will end up below the window
8935 */
8936 if (!did_delete)
8937 {
8938#ifdef FEAT_WINDOWS
8939 wp->w_redr_status = TRUE;
8940#endif
8941 redraw_cmdline = TRUE;
8942 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8943 lastrow = nextrow + line_count;
8944 if (lastrow > Rows)
8945 lastrow = Rows;
8946 screen_fill(nextrow - line_count, lastrow - line_count,
8947 W_WINCOL(wp), (int)W_ENDCOL(wp),
8948 ' ', ' ', 0);
8949 }
8950
8951 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8952 == FAIL)
8953 {
8954 /* deletion will have messed up other windows */
8955 if (did_delete)
8956 {
8957#ifdef FEAT_WINDOWS
8958 wp->w_redr_status = TRUE;
8959#endif
8960 win_rest_invalid(W_NEXT(wp));
8961 }
8962 return FAIL;
8963 }
8964
8965 return OK;
8966}
8967
8968/*
8969 * delete "line_count" window lines at "row" in window "wp"
8970 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8971 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8972 * scrolling
8973 * Return OK for success, FAIL if the lines are not deleted.
8974 */
8975 int
8976win_del_lines(wp, row, line_count, invalid, mayclear)
8977 win_T *wp;
8978 int row;
8979 int line_count;
8980 int invalid;
8981 int mayclear;
8982{
8983 int retval;
8984
8985 if (invalid)
8986 wp->w_lines_valid = 0;
8987
8988 if (line_count > wp->w_height - row)
8989 line_count = wp->w_height - row;
8990
8991 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
8992 if (retval != MAYBE)
8993 return retval;
8994
8995 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
8996 (int)Rows, FALSE, NULL) == FAIL)
8997 return FAIL;
8998
8999#ifdef FEAT_WINDOWS
9000 /*
9001 * If there are windows or status lines below, try to put them at the
9002 * correct place. If we can't do that, they have to be redrawn.
9003 */
9004 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9005 {
9006 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9007 line_count, (int)Rows, NULL) == FAIL)
9008 {
9009 wp->w_redr_status = TRUE;
9010 win_rest_invalid(wp->w_next);
9011 }
9012 }
9013 /*
9014 * If this is the last window and there is no status line, redraw the
9015 * command line later.
9016 */
9017 else
9018#endif
9019 redraw_cmdline = TRUE;
9020 return OK;
9021}
9022
9023/*
9024 * Common code for win_ins_lines() and win_del_lines().
9025 * Returns OK or FAIL when the work has been done.
9026 * Returns MAYBE when not finished yet.
9027 */
9028 static int
9029win_do_lines(wp, row, line_count, mayclear, del)
9030 win_T *wp;
9031 int row;
9032 int line_count;
9033 int mayclear;
9034 int del;
9035{
9036 int retval;
9037
9038 if (!redrawing() || line_count <= 0)
9039 return FAIL;
9040
9041 /* only a few lines left: redraw is faster */
9042 if (mayclear && Rows - line_count < 5
9043#ifdef FEAT_VERTSPLIT
9044 && wp->w_width == Columns
9045#endif
9046 )
9047 {
9048 screenclear(); /* will set wp->w_lines_valid to 0 */
9049 return FAIL;
9050 }
9051
9052 /*
9053 * Delete all remaining lines
9054 */
9055 if (row + line_count >= wp->w_height)
9056 {
9057 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9058 W_WINCOL(wp), (int)W_ENDCOL(wp),
9059 ' ', ' ', 0);
9060 return OK;
9061 }
9062
9063 /*
9064 * when scrolling, the message on the command line should be cleared,
9065 * otherwise it will stay there forever.
9066 */
9067 clear_cmdline = TRUE;
9068
9069 /*
9070 * If the terminal can set a scroll region, use that.
9071 * Always do this in a vertically split window. This will redraw from
9072 * ScreenLines[] when t_CV isn't defined. That's faster than using
9073 * win_line().
9074 * Don't use a scroll region when we are going to redraw the text, writing
9075 * a character in the lower right corner of the scroll region causes a
9076 * scroll-up in the DJGPP version.
9077 */
9078 if (scroll_region
9079#ifdef FEAT_VERTSPLIT
9080 || W_WIDTH(wp) != Columns
9081#endif
9082 )
9083 {
9084#ifdef FEAT_VERTSPLIT
9085 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9086#endif
9087 scroll_region_set(wp, row);
9088 if (del)
9089 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9090 wp->w_height - row, FALSE, wp);
9091 else
9092 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9093 wp->w_height - row, wp);
9094#ifdef FEAT_VERTSPLIT
9095 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9096#endif
9097 scroll_region_reset();
9098 return retval;
9099 }
9100
9101#ifdef FEAT_WINDOWS
9102 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9103 return FAIL;
9104#endif
9105
9106 return MAYBE;
9107}
9108
9109/*
9110 * window 'wp' and everything after it is messed up, mark it for redraw
9111 */
9112 static void
9113win_rest_invalid(wp)
9114 win_T *wp;
9115{
9116#ifdef FEAT_WINDOWS
9117 while (wp != NULL)
9118#else
9119 if (wp != NULL)
9120#endif
9121 {
9122 redraw_win_later(wp, NOT_VALID);
9123#ifdef FEAT_WINDOWS
9124 wp->w_redr_status = TRUE;
9125 wp = wp->w_next;
9126#endif
9127 }
9128 redraw_cmdline = TRUE;
9129}
9130
9131/*
9132 * The rest of the routines in this file perform screen manipulations. The
9133 * given operation is performed physically on the screen. The corresponding
9134 * change is also made to the internal screen image. In this way, the editor
9135 * anticipates the effect of editing changes on the appearance of the screen.
9136 * That way, when we call screenupdate a complete redraw isn't usually
9137 * necessary. Another advantage is that we can keep adding code to anticipate
9138 * screen changes, and in the meantime, everything still works.
9139 */
9140
9141/*
9142 * types for inserting or deleting lines
9143 */
9144#define USE_T_CAL 1
9145#define USE_T_CDL 2
9146#define USE_T_AL 3
9147#define USE_T_CE 4
9148#define USE_T_DL 5
9149#define USE_T_SR 6
9150#define USE_NL 7
9151#define USE_T_CD 8
9152#define USE_REDRAW 9
9153
9154/*
9155 * insert lines on the screen and update ScreenLines[]
9156 * 'end' is the line after the scrolled part. Normally it is Rows.
9157 * When scrolling region used 'off' is the offset from the top for the region.
9158 * 'row' and 'end' are relative to the start of the region.
9159 *
9160 * return FAIL for failure, OK for success.
9161 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009162 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163screen_ins_lines(off, row, line_count, end, wp)
9164 int off;
9165 int row;
9166 int line_count;
9167 int end;
9168 win_T *wp; /* NULL or window to use width from */
9169{
9170 int i;
9171 int j;
9172 unsigned temp;
9173 int cursor_row;
9174 int type;
9175 int result_empty;
9176 int can_ce = can_clear(T_CE);
9177
9178 /*
9179 * FAIL if
9180 * - there is no valid screen
9181 * - the screen has to be redrawn completely
9182 * - the line count is less than one
9183 * - the line count is more than 'ttyscroll'
9184 */
9185 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9186 return FAIL;
9187
9188 /*
9189 * There are seven ways to insert lines:
9190 * 0. When in a vertically split window and t_CV isn't set, redraw the
9191 * characters from ScreenLines[].
9192 * 1. Use T_CD (clear to end of display) if it exists and the result of
9193 * the insert is just empty lines
9194 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9195 * present or line_count > 1. It looks better if we do all the inserts
9196 * at once.
9197 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9198 * insert is just empty lines and T_CE is not present or line_count >
9199 * 1.
9200 * 4. Use T_AL (insert line) if it exists.
9201 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9202 * just empty lines.
9203 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9204 * just empty lines.
9205 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9206 * the 'da' flag is not set or we have clear line capability.
9207 * 8. redraw the characters from ScreenLines[].
9208 *
9209 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9210 * the scrollbar for the window. It does have insert line, use that if it
9211 * exists.
9212 */
9213 result_empty = (row + line_count >= end);
9214#ifdef FEAT_VERTSPLIT
9215 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9216 type = USE_REDRAW;
9217 else
9218#endif
9219 if (can_clear(T_CD) && result_empty)
9220 type = USE_T_CD;
9221 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9222 type = USE_T_CAL;
9223 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9224 type = USE_T_CDL;
9225 else if (*T_AL != NUL)
9226 type = USE_T_AL;
9227 else if (can_ce && result_empty)
9228 type = USE_T_CE;
9229 else if (*T_DL != NUL && result_empty)
9230 type = USE_T_DL;
9231 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9232 type = USE_T_SR;
9233 else
9234 return FAIL;
9235
9236 /*
9237 * For clearing the lines screen_del_lines() is used. This will also take
9238 * care of t_db if necessary.
9239 */
9240 if (type == USE_T_CD || type == USE_T_CDL ||
9241 type == USE_T_CE || type == USE_T_DL)
9242 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9243
9244 /*
9245 * If text is retained below the screen, first clear or delete as many
9246 * lines at the bottom of the window as are about to be inserted so that
9247 * the deleted lines won't later surface during a screen_del_lines.
9248 */
9249 if (*T_DB)
9250 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9251
9252#ifdef FEAT_CLIPBOARD
9253 /* Remove a modeless selection when inserting lines halfway the screen
9254 * or not the full width of the screen. */
9255 if (off + row > 0
9256# ifdef FEAT_VERTSPLIT
9257 || (wp != NULL && wp->w_width != Columns)
9258# endif
9259 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009260 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 else
9262 clip_scroll_selection(-line_count);
9263#endif
9264
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265#ifdef FEAT_GUI
9266 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9267 * scrolling is actually carried out. */
9268 gui_dont_update_cursor();
9269#endif
9270
9271 if (*T_CCS != NUL) /* cursor relative to region */
9272 cursor_row = row;
9273 else
9274 cursor_row = row + off;
9275
9276 /*
9277 * Shift LineOffset[] line_count down to reflect the inserted lines.
9278 * Clear the inserted lines in ScreenLines[].
9279 */
9280 row += off;
9281 end += off;
9282 for (i = 0; i < line_count; ++i)
9283 {
9284#ifdef FEAT_VERTSPLIT
9285 if (wp != NULL && wp->w_width != Columns)
9286 {
9287 /* need to copy part of a line */
9288 j = end - 1 - i;
9289 while ((j -= line_count) >= row)
9290 linecopy(j + line_count, j, wp);
9291 j += line_count;
9292 if (can_clear((char_u *)" "))
9293 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9294 else
9295 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9296 LineWraps[j] = FALSE;
9297 }
9298 else
9299#endif
9300 {
9301 j = end - 1 - i;
9302 temp = LineOffset[j];
9303 while ((j -= line_count) >= row)
9304 {
9305 LineOffset[j + line_count] = LineOffset[j];
9306 LineWraps[j + line_count] = LineWraps[j];
9307 }
9308 LineOffset[j + line_count] = temp;
9309 LineWraps[j + line_count] = FALSE;
9310 if (can_clear((char_u *)" "))
9311 lineclear(temp, (int)Columns);
9312 else
9313 lineinvalid(temp, (int)Columns);
9314 }
9315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
9317 screen_stop_highlight();
9318 windgoto(cursor_row, 0);
9319
9320#ifdef FEAT_VERTSPLIT
9321 /* redraw the characters */
9322 if (type == USE_REDRAW)
9323 redraw_block(row, end, wp);
9324 else
9325#endif
9326 if (type == USE_T_CAL)
9327 {
9328 term_append_lines(line_count);
9329 screen_start(); /* don't know where cursor is now */
9330 }
9331 else
9332 {
9333 for (i = 0; i < line_count; i++)
9334 {
9335 if (type == USE_T_AL)
9336 {
9337 if (i && cursor_row != 0)
9338 windgoto(cursor_row, 0);
9339 out_str(T_AL);
9340 }
9341 else /* type == USE_T_SR */
9342 out_str(T_SR);
9343 screen_start(); /* don't know where cursor is now */
9344 }
9345 }
9346
9347 /*
9348 * With scroll-reverse and 'da' flag set we need to clear the lines that
9349 * have been scrolled down into the region.
9350 */
9351 if (type == USE_T_SR && *T_DA)
9352 {
9353 for (i = 0; i < line_count; ++i)
9354 {
9355 windgoto(off + i, 0);
9356 out_str(T_CE);
9357 screen_start(); /* don't know where cursor is now */
9358 }
9359 }
9360
9361#ifdef FEAT_GUI
9362 gui_can_update_cursor();
9363 if (gui.in_use)
9364 out_flush(); /* always flush after a scroll */
9365#endif
9366 return OK;
9367}
9368
9369/*
9370 * delete lines on the screen and update ScreenLines[]
9371 * 'end' is the line after the scrolled part. Normally it is Rows.
9372 * When scrolling region used 'off' is the offset from the top for the region.
9373 * 'row' and 'end' are relative to the start of the region.
9374 *
9375 * Return OK for success, FAIL if the lines are not deleted.
9376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 int
9378screen_del_lines(off, row, line_count, end, force, wp)
9379 int off;
9380 int row;
9381 int line_count;
9382 int end;
9383 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009384 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 int j;
9387 int i;
9388 unsigned temp;
9389 int cursor_row;
9390 int cursor_end;
9391 int result_empty; /* result is empty until end of region */
9392 int can_delete; /* deleting line codes can be used */
9393 int type;
9394
9395 /*
9396 * FAIL if
9397 * - there is no valid screen
9398 * - the screen has to be redrawn completely
9399 * - the line count is less than one
9400 * - the line count is more than 'ttyscroll'
9401 */
9402 if (!screen_valid(TRUE) || line_count <= 0 ||
9403 (!force && line_count > p_ttyscroll))
9404 return FAIL;
9405
9406 /*
9407 * Check if the rest of the current region will become empty.
9408 */
9409 result_empty = row + line_count >= end;
9410
9411 /*
9412 * We can delete lines only when 'db' flag not set or when 'ce' option
9413 * available.
9414 */
9415 can_delete = (*T_DB == NUL || can_clear(T_CE));
9416
9417 /*
9418 * There are six ways to delete lines:
9419 * 0. When in a vertically split window and t_CV isn't set, redraw the
9420 * characters from ScreenLines[].
9421 * 1. Use T_CD if it exists and the result is empty.
9422 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9423 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9424 * none of the other ways work.
9425 * 4. Use T_CE (erase line) if the result is empty.
9426 * 5. Use T_DL (delete line) if it exists.
9427 * 6. redraw the characters from ScreenLines[].
9428 */
9429#ifdef FEAT_VERTSPLIT
9430 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9431 type = USE_REDRAW;
9432 else
9433#endif
9434 if (can_clear(T_CD) && result_empty)
9435 type = USE_T_CD;
9436#if defined(__BEOS__) && defined(BEOS_DR8)
9437 /*
9438 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9439 * its internal termcap... this works okay for tests which test *T_DB !=
9440 * NUL. It has the disadvantage that the user cannot use any :set t_*
9441 * command to get T_DB (back) to empty_option, only :set term=... will do
9442 * the trick...
9443 * Anyway, this hack will hopefully go away with the next OS release.
9444 * (Olaf Seibert)
9445 */
9446 else if (row == 0 && T_DB == empty_option
9447 && (line_count == 1 || *T_CDL == NUL))
9448#else
9449 else if (row == 0 && (
9450#ifndef AMIGA
9451 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9452 * up, so use delete-line command */
9453 line_count == 1 ||
9454#endif
9455 *T_CDL == NUL))
9456#endif
9457 type = USE_NL;
9458 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9459 type = USE_T_CDL;
9460 else if (can_clear(T_CE) && result_empty
9461#ifdef FEAT_VERTSPLIT
9462 && (wp == NULL || wp->w_width == Columns)
9463#endif
9464 )
9465 type = USE_T_CE;
9466 else if (*T_DL != NUL && can_delete)
9467 type = USE_T_DL;
9468 else if (*T_CDL != NUL && can_delete)
9469 type = USE_T_CDL;
9470 else
9471 return FAIL;
9472
9473#ifdef FEAT_CLIPBOARD
9474 /* Remove a modeless selection when deleting lines halfway the screen or
9475 * not the full width of the screen. */
9476 if (off + row > 0
9477# ifdef FEAT_VERTSPLIT
9478 || (wp != NULL && wp->w_width != Columns)
9479# endif
9480 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009481 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 else
9483 clip_scroll_selection(line_count);
9484#endif
9485
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486#ifdef FEAT_GUI
9487 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9488 * scrolling is actually carried out. */
9489 gui_dont_update_cursor();
9490#endif
9491
9492 if (*T_CCS != NUL) /* cursor relative to region */
9493 {
9494 cursor_row = row;
9495 cursor_end = end;
9496 }
9497 else
9498 {
9499 cursor_row = row + off;
9500 cursor_end = end + off;
9501 }
9502
9503 /*
9504 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9505 * Clear the inserted lines in ScreenLines[].
9506 */
9507 row += off;
9508 end += off;
9509 for (i = 0; i < line_count; ++i)
9510 {
9511#ifdef FEAT_VERTSPLIT
9512 if (wp != NULL && wp->w_width != Columns)
9513 {
9514 /* need to copy part of a line */
9515 j = row + i;
9516 while ((j += line_count) <= end - 1)
9517 linecopy(j - line_count, j, wp);
9518 j -= line_count;
9519 if (can_clear((char_u *)" "))
9520 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9521 else
9522 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9523 LineWraps[j] = FALSE;
9524 }
9525 else
9526#endif
9527 {
9528 /* whole width, moving the line pointers is faster */
9529 j = row + i;
9530 temp = LineOffset[j];
9531 while ((j += line_count) <= end - 1)
9532 {
9533 LineOffset[j - line_count] = LineOffset[j];
9534 LineWraps[j - line_count] = LineWraps[j];
9535 }
9536 LineOffset[j - line_count] = temp;
9537 LineWraps[j - line_count] = FALSE;
9538 if (can_clear((char_u *)" "))
9539 lineclear(temp, (int)Columns);
9540 else
9541 lineinvalid(temp, (int)Columns);
9542 }
9543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
9545 screen_stop_highlight();
9546
9547#ifdef FEAT_VERTSPLIT
9548 /* redraw the characters */
9549 if (type == USE_REDRAW)
9550 redraw_block(row, end, wp);
9551 else
9552#endif
9553 if (type == USE_T_CD) /* delete the lines */
9554 {
9555 windgoto(cursor_row, 0);
9556 out_str(T_CD);
9557 screen_start(); /* don't know where cursor is now */
9558 }
9559 else if (type == USE_T_CDL)
9560 {
9561 windgoto(cursor_row, 0);
9562 term_delete_lines(line_count);
9563 screen_start(); /* don't know where cursor is now */
9564 }
9565 /*
9566 * Deleting lines at top of the screen or scroll region: Just scroll
9567 * the whole screen (scroll region) up by outputting newlines on the
9568 * last line.
9569 */
9570 else if (type == USE_NL)
9571 {
9572 windgoto(cursor_end - 1, 0);
9573 for (i = line_count; --i >= 0; )
9574 out_char('\n'); /* cursor will remain on same line */
9575 }
9576 else
9577 {
9578 for (i = line_count; --i >= 0; )
9579 {
9580 if (type == USE_T_DL)
9581 {
9582 windgoto(cursor_row, 0);
9583 out_str(T_DL); /* delete a line */
9584 }
9585 else /* type == USE_T_CE */
9586 {
9587 windgoto(cursor_row + i, 0);
9588 out_str(T_CE); /* erase a line */
9589 }
9590 screen_start(); /* don't know where cursor is now */
9591 }
9592 }
9593
9594 /*
9595 * If the 'db' flag is set, we need to clear the lines that have been
9596 * scrolled up at the bottom of the region.
9597 */
9598 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9599 {
9600 for (i = line_count; i > 0; --i)
9601 {
9602 windgoto(cursor_end - i, 0);
9603 out_str(T_CE); /* erase a line */
9604 screen_start(); /* don't know where cursor is now */
9605 }
9606 }
9607
9608#ifdef FEAT_GUI
9609 gui_can_update_cursor();
9610 if (gui.in_use)
9611 out_flush(); /* always flush after a scroll */
9612#endif
9613
9614 return OK;
9615}
9616
9617/*
9618 * show the current mode and ruler
9619 *
9620 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9621 * If clear_cmdline is FALSE there may be a message there that needs to be
9622 * cleared only if a mode is shown.
9623 * Return the length of the message (0 if no message).
9624 */
9625 int
9626showmode()
9627{
9628 int need_clear;
9629 int length = 0;
9630 int do_mode;
9631 int attr;
9632 int nwr_save;
9633#ifdef FEAT_INS_EXPAND
9634 int sub_attr;
9635#endif
9636
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009637 do_mode = ((p_smd && msg_silent == 0)
9638 && ((State & INSERT)
9639 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640#ifdef FEAT_VISUAL
9641 || VIsual_active
9642#endif
9643 ));
9644 if (do_mode || Recording)
9645 {
9646 /*
9647 * Don't show mode right now, when not redrawing or inside a mapping.
9648 * Call char_avail() only when we are going to show something, because
9649 * it takes a bit of time.
9650 */
9651 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9652 {
9653 redraw_cmdline = TRUE; /* show mode later */
9654 return 0;
9655 }
9656
9657 nwr_save = need_wait_return;
9658
9659 /* wait a bit before overwriting an important message */
9660 check_for_delay(FALSE);
9661
9662 /* if the cmdline is more than one line high, erase top lines */
9663 need_clear = clear_cmdline;
9664 if (clear_cmdline && cmdline_row < Rows - 1)
9665 msg_clr_cmdline(); /* will reset clear_cmdline */
9666
9667 /* Position on the last line in the window, column 0 */
9668 msg_pos_mode();
9669 cursor_off();
9670 attr = hl_attr(HLF_CM); /* Highlight mode */
9671 if (do_mode)
9672 {
9673 MSG_PUTS_ATTR("--", attr);
9674#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009675 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009676# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009677 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009678# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009679 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009680# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009681 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009682# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 MSG_PUTS_ATTR(" IM", attr);
9684# else
9685 MSG_PUTS_ATTR(" XIM", attr);
9686# endif
9687#endif
9688#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9689 if (gui.in_use)
9690 {
9691 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009692 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694#endif
9695#ifdef FEAT_INS_EXPAND
9696 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9697 {
9698 /* These messages can get long, avoid a wrap in a narrow
9699 * window. Prefer showing edit_submode_extra. */
9700 length = (Rows - msg_row) * Columns - 3;
9701 if (edit_submode_extra != NULL)
9702 length -= vim_strsize(edit_submode_extra);
9703 if (length > 0)
9704 {
9705 if (edit_submode_pre != NULL)
9706 length -= vim_strsize(edit_submode_pre);
9707 if (length - vim_strsize(edit_submode) > 0)
9708 {
9709 if (edit_submode_pre != NULL)
9710 msg_puts_attr(edit_submode_pre, attr);
9711 msg_puts_attr(edit_submode, attr);
9712 }
9713 if (edit_submode_extra != NULL)
9714 {
9715 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9716 if ((int)edit_submode_highl < (int)HLF_COUNT)
9717 sub_attr = hl_attr(edit_submode_highl);
9718 else
9719 sub_attr = attr;
9720 msg_puts_attr(edit_submode_extra, sub_attr);
9721 }
9722 }
9723 length = 0;
9724 }
9725 else
9726#endif
9727 {
9728#ifdef FEAT_VREPLACE
9729 if (State & VREPLACE_FLAG)
9730 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9731 else
9732#endif
9733 if (State & REPLACE_FLAG)
9734 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9735 else if (State & INSERT)
9736 {
9737#ifdef FEAT_RIGHTLEFT
9738 if (p_ri)
9739 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9740#endif
9741 MSG_PUTS_ATTR(_(" INSERT"), attr);
9742 }
9743 else if (restart_edit == 'I')
9744 MSG_PUTS_ATTR(_(" (insert)"), attr);
9745 else if (restart_edit == 'R')
9746 MSG_PUTS_ATTR(_(" (replace)"), attr);
9747 else if (restart_edit == 'V')
9748 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9749#ifdef FEAT_RIGHTLEFT
9750 if (p_hkmap)
9751 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9752# ifdef FEAT_FKMAP
9753 if (p_fkmap)
9754 MSG_PUTS_ATTR(farsi_text_5, attr);
9755# endif
9756#endif
9757#ifdef FEAT_KEYMAP
9758 if (State & LANGMAP)
9759 {
9760# ifdef FEAT_ARABIC
9761 if (curwin->w_p_arab)
9762 MSG_PUTS_ATTR(_(" Arabic"), attr);
9763 else
9764# endif
9765 MSG_PUTS_ATTR(_(" (lang)"), attr);
9766 }
9767#endif
9768 if ((State & INSERT) && p_paste)
9769 MSG_PUTS_ATTR(_(" (paste)"), attr);
9770
9771#ifdef FEAT_VISUAL
9772 if (VIsual_active)
9773 {
9774 char *p;
9775
9776 /* Don't concatenate separate words to avoid translation
9777 * problems. */
9778 switch ((VIsual_select ? 4 : 0)
9779 + (VIsual_mode == Ctrl_V) * 2
9780 + (VIsual_mode == 'V'))
9781 {
9782 case 0: p = N_(" VISUAL"); break;
9783 case 1: p = N_(" VISUAL LINE"); break;
9784 case 2: p = N_(" VISUAL BLOCK"); break;
9785 case 4: p = N_(" SELECT"); break;
9786 case 5: p = N_(" SELECT LINE"); break;
9787 default: p = N_(" SELECT BLOCK"); break;
9788 }
9789 MSG_PUTS_ATTR(_(p), attr);
9790 }
9791#endif
9792 MSG_PUTS_ATTR(" --", attr);
9793 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009794
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 need_clear = TRUE;
9796 }
9797 if (Recording
9798#ifdef FEAT_INS_EXPAND
9799 && edit_submode == NULL /* otherwise it gets too long */
9800#endif
9801 )
9802 {
9803 MSG_PUTS_ATTR(_("recording"), attr);
9804 need_clear = TRUE;
9805 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009806
9807 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 if (need_clear || clear_cmdline)
9809 msg_clr_eos();
9810 msg_didout = FALSE; /* overwrite this message */
9811 length = msg_col;
9812 msg_col = 0;
9813 need_wait_return = nwr_save; /* never ask for hit-return for this */
9814 }
9815 else if (clear_cmdline && msg_silent == 0)
9816 /* Clear the whole command line. Will reset "clear_cmdline". */
9817 msg_clr_cmdline();
9818
9819#ifdef FEAT_CMDL_INFO
9820# ifdef FEAT_VISUAL
9821 /* In Visual mode the size of the selected area must be redrawn. */
9822 if (VIsual_active)
9823 clear_showcmd();
9824# endif
9825
9826 /* If the last window has no status line, the ruler is after the mode
9827 * message and must be redrawn */
9828 if (redrawing()
9829# ifdef FEAT_WINDOWS
9830 && lastwin->w_status_height == 0
9831# endif
9832 )
9833 win_redr_ruler(lastwin, TRUE);
9834#endif
9835 redraw_cmdline = FALSE;
9836 clear_cmdline = FALSE;
9837
9838 return length;
9839}
9840
9841/*
9842 * Position for a mode message.
9843 */
9844 static void
9845msg_pos_mode()
9846{
9847 msg_col = 0;
9848 msg_row = Rows - 1;
9849}
9850
9851/*
9852 * Delete mode message. Used when ESC is typed which is expected to end
9853 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009854 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 */
9856 void
9857unshowmode(force)
9858 int force;
9859{
9860 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009861 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 */
9863 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9864 redraw_cmdline = TRUE; /* delete mode later */
9865 else
9866 {
9867 msg_pos_mode();
9868 if (Recording)
9869 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9870 msg_clr_eos();
9871 }
9872}
9873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009874#if defined(FEAT_WINDOWS)
9875/*
9876 * Draw the tab pages line at the top of the Vim window.
9877 */
9878 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009879draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009880{
9881 int tabcount = 0;
9882 tabpage_T *tp;
9883 int tabwidth;
9884 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009885 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009886 int attr;
9887 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009888 win_T *cwp;
9889 int wincount;
9890 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009891 int c;
9892 int len;
9893 int attr_sel = hl_attr(HLF_TPS);
9894 int attr_nosel = hl_attr(HLF_TP);
9895 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009896 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009897 int room;
9898 int use_sep_chars = (t_colors < 8
9899#ifdef FEAT_GUI
9900 && !gui.in_use
9901#endif
9902 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009903
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009904 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009905
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009906#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009907 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009908 if (gui_use_tabline())
9909 {
9910 gui_update_tabline();
9911 return;
9912 }
9913#endif
9914
9915 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009916 return;
9917
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009918#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009919
9920 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9921 for (scol = 0; scol < Columns; ++scol)
9922 TabPageIdxs[scol] = 0;
9923
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009924 /* Use the 'tabline' option if it's set. */
9925 if (*p_tal != NUL)
9926 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009927 int save_called_emsg = called_emsg;
9928
9929 /* Check for an error. If there is one we would loop in redrawing the
9930 * screen. Avoid that by making 'tabline' empty. */
9931 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009932 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009933 if (called_emsg)
9934 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009935 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009936 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009937 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009938 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009939#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009940 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009941 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9942 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009943
Bram Moolenaar238a5642006-02-21 22:12:05 +00009944 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9945 if (tabwidth < 6)
9946 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009947
Bram Moolenaar238a5642006-02-21 22:12:05 +00009948 attr = attr_nosel;
9949 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009950 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009951 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9952 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009953 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009954 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009955
Bram Moolenaar238a5642006-02-21 22:12:05 +00009956 if (tp->tp_topframe == topframe)
9957 attr = attr_sel;
9958 if (use_sep_chars && col > 0)
9959 screen_putchar('|', 0, col++, attr);
9960
9961 if (tp->tp_topframe != topframe)
9962 attr = attr_nosel;
9963
9964 screen_putchar(' ', 0, col++, attr);
9965
9966 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009967 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009968 cwp = curwin;
9969 wp = firstwin;
9970 }
9971 else
9972 {
9973 cwp = tp->tp_curwin;
9974 wp = tp->tp_firstwin;
9975 }
9976
9977 modified = FALSE;
9978 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
9979 if (bufIsChanged(wp->w_buffer))
9980 modified = TRUE;
9981 if (modified || wincount > 1)
9982 {
9983 if (wincount > 1)
9984 {
9985 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009986 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009987 if (col + len >= Columns - 3)
9988 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +00009989 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009990#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +00009991 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009992#else
Bram Moolenaar238a5642006-02-21 22:12:05 +00009993 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009994#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00009995 );
9996 col += len;
9997 }
9998 if (modified)
9999 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10000 screen_putchar(' ', 0, col++, attr);
10001 }
10002
10003 room = scol - col + tabwidth - 1;
10004 if (room > 0)
10005 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010006 /* Get buffer name in NameBuff[] */
10007 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010008 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010009 len = vim_strsize(NameBuff);
10010 p = NameBuff;
10011#ifdef FEAT_MBYTE
10012 if (has_mbyte)
10013 while (len > room)
10014 {
10015 len -= ptr2cells(p);
10016 mb_ptr_adv(p);
10017 }
10018 else
10019#endif
10020 if (len > room)
10021 {
10022 p += len - room;
10023 len = room;
10024 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010025 if (len > Columns - col - 1)
10026 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010027
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010028 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010029 col += len;
10030 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010031 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010032
10033 /* Store the tab page number in TabPageIdxs[], so that
10034 * jump_to_mouse() knows where each one is. */
10035 ++tabcount;
10036 while (scol < col)
10037 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010038 }
10039
Bram Moolenaar238a5642006-02-21 22:12:05 +000010040 if (use_sep_chars)
10041 c = '_';
10042 else
10043 c = ' ';
10044 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010045
10046 /* Put an "X" for closing the current tab if there are several. */
10047 if (first_tabpage->tp_next != NULL)
10048 {
10049 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10050 TabPageIdxs[Columns - 1] = -999;
10051 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010052 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010053
10054 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10055 * set. */
10056 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010057}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010058
10059/*
10060 * Get buffer name for "buf" into NameBuff[].
10061 * Takes care of special buffer names and translates special characters.
10062 */
10063 void
10064get_trans_bufname(buf)
10065 buf_T *buf;
10066{
10067 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010068 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010069 else
10070 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10071 trans_characters(NameBuff, MAXPATHL);
10072}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010073#endif
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10076/*
10077 * Get the character to use in a status line. Get its attributes in "*attr".
10078 */
10079 static int
10080fillchar_status(attr, is_curwin)
10081 int *attr;
10082 int is_curwin;
10083{
10084 int fill;
10085 if (is_curwin)
10086 {
10087 *attr = hl_attr(HLF_S);
10088 fill = fill_stl;
10089 }
10090 else
10091 {
10092 *attr = hl_attr(HLF_SNC);
10093 fill = fill_stlnc;
10094 }
10095 /* Use fill when there is highlighting, and highlighting of current
10096 * window differs, or the fillchars differ, or this is not the
10097 * current window */
10098 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10099 || !is_curwin || firstwin == lastwin)
10100 || (fill_stl != fill_stlnc)))
10101 return fill;
10102 if (is_curwin)
10103 return '^';
10104 return '=';
10105}
10106#endif
10107
10108#ifdef FEAT_VERTSPLIT
10109/*
10110 * Get the character to use in a separator between vertically split windows.
10111 * Get its attributes in "*attr".
10112 */
10113 static int
10114fillchar_vsep(attr)
10115 int *attr;
10116{
10117 *attr = hl_attr(HLF_C);
10118 if (*attr == 0 && fill_vert == ' ')
10119 return '|';
10120 else
10121 return fill_vert;
10122}
10123#endif
10124
10125/*
10126 * Return TRUE if redrawing should currently be done.
10127 */
10128 int
10129redrawing()
10130{
10131 return (!RedrawingDisabled
10132 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10133}
10134
10135/*
10136 * Return TRUE if printing messages should currently be done.
10137 */
10138 int
10139messaging()
10140{
10141 return (!(p_lz && char_avail() && !KeyTyped));
10142}
10143
10144/*
10145 * Show current status info in ruler and various other places
10146 * If always is FALSE, only show ruler if position has changed.
10147 */
10148 void
10149showruler(always)
10150 int always;
10151{
10152 if (!always && !redrawing())
10153 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010154#ifdef FEAT_INS_EXPAND
10155 if (pum_visible())
10156 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010157# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010158 /* Don't redraw right now, do it later. */
10159 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010160# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010161 return;
10162 }
10163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010165 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010166 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010167 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
10170#endif
10171#ifdef FEAT_CMDL_INFO
10172 win_redr_ruler(curwin, always);
10173#endif
10174
10175#ifdef FEAT_TITLE
10176 if (need_maketitle
10177# ifdef FEAT_STL_OPT
10178 || (p_icon && (stl_syntax & STL_IN_ICON))
10179 || (p_title && (stl_syntax & STL_IN_TITLE))
10180# endif
10181 )
10182 maketitle();
10183#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010184#ifdef FEAT_WINDOWS
10185 /* Redraw the tab pages line if needed. */
10186 if (redraw_tabline)
10187 draw_tabline();
10188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189}
10190
10191#ifdef FEAT_CMDL_INFO
10192 static void
10193win_redr_ruler(wp, always)
10194 win_T *wp;
10195 int always;
10196{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010197#define RULER_BUF_LEN 70
10198 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 int row;
10200 int fillchar;
10201 int attr;
10202 int empty_line = FALSE;
10203 colnr_T virtcol;
10204 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010205 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 int o;
10207#ifdef FEAT_VERTSPLIT
10208 int this_ru_col;
10209 int off = 0;
10210 int width = Columns;
10211# define WITH_OFF(x) x
10212# define WITH_WIDTH(x) x
10213#else
10214# define WITH_OFF(x) 0
10215# define WITH_WIDTH(x) Columns
10216# define this_ru_col ru_col
10217#endif
10218
10219 /* If 'ruler' off or redrawing disabled, don't do anything */
10220 if (!p_ru)
10221 return;
10222
10223 /*
10224 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10225 * after deleting lines, before cursor.lnum is corrected.
10226 */
10227 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10228 return;
10229
10230#ifdef FEAT_INS_EXPAND
10231 /* Don't draw the ruler while doing insert-completion, it might overwrite
10232 * the (long) mode message. */
10233# ifdef FEAT_WINDOWS
10234 if (wp == lastwin && lastwin->w_status_height == 0)
10235# endif
10236 if (edit_submode != NULL)
10237 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010238 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10239 if (pum_visible())
10240 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#endif
10242
10243#ifdef FEAT_STL_OPT
10244 if (*p_ruf)
10245 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010246 int save_called_emsg = called_emsg;
10247
10248 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010250 if (called_emsg)
10251 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010252 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010253 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 return;
10255 }
10256#endif
10257
10258 /*
10259 * Check if not in Insert mode and the line is empty (will show "0-1").
10260 */
10261 if (!(State & INSERT)
10262 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10263 empty_line = TRUE;
10264
10265 /*
10266 * Only draw the ruler when something changed.
10267 */
10268 validate_virtcol_win(wp);
10269 if ( redraw_cmdline
10270 || always
10271 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10272 || wp->w_cursor.col != wp->w_ru_cursor.col
10273 || wp->w_virtcol != wp->w_ru_virtcol
10274#ifdef FEAT_VIRTUALEDIT
10275 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10276#endif
10277 || wp->w_topline != wp->w_ru_topline
10278 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10279#ifdef FEAT_DIFF
10280 || wp->w_topfill != wp->w_ru_topfill
10281#endif
10282 || empty_line != wp->w_ru_empty)
10283 {
10284 cursor_off();
10285#ifdef FEAT_WINDOWS
10286 if (wp->w_status_height)
10287 {
10288 row = W_WINROW(wp) + wp->w_height;
10289 fillchar = fillchar_status(&attr, wp == curwin);
10290# ifdef FEAT_VERTSPLIT
10291 off = W_WINCOL(wp);
10292 width = W_WIDTH(wp);
10293# endif
10294 }
10295 else
10296#endif
10297 {
10298 row = Rows - 1;
10299 fillchar = ' ';
10300 attr = 0;
10301#ifdef FEAT_VERTSPLIT
10302 width = Columns;
10303 off = 0;
10304#endif
10305 }
10306
10307 /* In list mode virtcol needs to be recomputed */
10308 virtcol = wp->w_virtcol;
10309 if (wp->w_p_list && lcs_tab1 == NUL)
10310 {
10311 wp->w_p_list = FALSE;
10312 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10313 wp->w_p_list = TRUE;
10314 }
10315
10316 /*
10317 * Some sprintfs return the length, some return a pointer.
10318 * To avoid portability problems we use strlen() here.
10319 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010320 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10322 ? 0L
10323 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010324 len = STRLEN(buffer);
10325 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10327 (int)virtcol + 1);
10328
10329 /*
10330 * Add a "50%" if there is room for it.
10331 * On the last line, don't print in the last column (scrolls the
10332 * screen up on some terminals).
10333 */
10334 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010335 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 o = i + vim_strsize(buffer + i + 1);
10337#ifdef FEAT_WINDOWS
10338 if (wp->w_status_height == 0) /* can't use last char of screen */
10339#endif
10340 ++o;
10341#ifdef FEAT_VERTSPLIT
10342 this_ru_col = ru_col - (Columns - width);
10343 if (this_ru_col < 0)
10344 this_ru_col = 0;
10345#endif
10346 /* Never use more than half the window/screen width, leave the other
10347 * half for the filename. */
10348 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10349 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10350 if (this_ru_col + o < WITH_WIDTH(width))
10351 {
10352 while (this_ru_col + o < WITH_WIDTH(width))
10353 {
10354#ifdef FEAT_MBYTE
10355 if (has_mbyte)
10356 i += (*mb_char2bytes)(fillchar, buffer + i);
10357 else
10358#endif
10359 buffer[i++] = fillchar;
10360 ++o;
10361 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010362 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 }
10364 /* Truncate at window boundary. */
10365#ifdef FEAT_MBYTE
10366 if (has_mbyte)
10367 {
10368 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010369 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 {
10371 o += (*mb_ptr2cells)(buffer + i);
10372 if (this_ru_col + o > WITH_WIDTH(width))
10373 {
10374 buffer[i] = NUL;
10375 break;
10376 }
10377 }
10378 }
10379 else
10380#endif
10381 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10382 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10383
10384 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10385 i = redraw_cmdline;
10386 screen_fill(row, row + 1,
10387 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10388 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10389 fillchar, fillchar, attr);
10390 /* don't redraw the cmdline because of showing the ruler */
10391 redraw_cmdline = i;
10392 wp->w_ru_cursor = wp->w_cursor;
10393 wp->w_ru_virtcol = wp->w_virtcol;
10394 wp->w_ru_empty = empty_line;
10395 wp->w_ru_topline = wp->w_topline;
10396 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10397#ifdef FEAT_DIFF
10398 wp->w_ru_topfill = wp->w_topfill;
10399#endif
10400 }
10401}
10402#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010403
10404#if defined(FEAT_LINEBREAK) || defined(PROTO)
10405/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010406 * Return the width of the 'number' and 'relativenumber' column.
10407 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010408 * Otherwise it depends on 'numberwidth' and the line count.
10409 */
10410 int
10411number_width(wp)
10412 win_T *wp;
10413{
10414 int n;
10415 linenr_T lnum;
10416
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010417 if (wp->w_p_rnu && !wp->w_p_nu)
10418 /* cursor line shows "0" */
10419 lnum = wp->w_height;
10420 else
10421 /* cursor line shows absolute line number */
10422 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010423
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010424 if (lnum == wp->w_nrwidth_line_count)
10425 return wp->w_nrwidth_width;
10426 wp->w_nrwidth_line_count = lnum;
10427
10428 n = 0;
10429 do
10430 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010431 lnum /= 10;
10432 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010433 } while (lnum > 0);
10434
10435 /* 'numberwidth' gives the minimal width plus one */
10436 if (n < wp->w_p_nuw - 1)
10437 n = wp->w_p_nuw - 1;
10438
10439 wp->w_nrwidth_width = n;
10440 return n;
10441}
10442#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010443
10444/*
10445 * Return the current cursor column. This is the actual position on the
10446 * screen. First column is 0.
10447 */
10448 int
10449screen_screencol()
10450{
10451 return screen_cur_col;
10452}
10453
10454/*
10455 * Return the current cursor row. This is the actual position on the screen.
10456 * First row is 0.
10457 */
10458 int
10459screen_screenrow()
10460{
10461 return screen_cur_row;
10462}