blob: a030207ae1eed769e50e4e16f45d65ae1ba4d276 [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)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003000# define FIX_FOR_BOGUSCOLS \
3001 { \
3002 n_extra += vcol_off; \
3003 vcol -= vcol_off; \
3004 vcol_off = 0; \
3005 col -= boguscols; \
3006 boguscols = 0; \
3007 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003008#else
3009# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 if (startrow > endrow) /* past the end already! */
3013 return startrow;
3014
3015 row = startrow;
3016 screen_row = row + W_WINROW(wp);
3017
3018 /*
3019 * To speed up the loop below, set extra_check when there is linebreak,
3020 * trailing white space and/or syntax processing to be done.
3021 */
3022#ifdef FEAT_LINEBREAK
3023 extra_check = wp->w_p_lbr;
3024#else
3025 extra_check = 0;
3026#endif
3027#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02003028 if (syntax_present(wp) && !wp->w_s->b_syn_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 /* Prepare for syntax highlighting in this line. When there is an
3031 * error, stop syntax highlighting. */
3032 save_did_emsg = did_emsg;
3033 did_emsg = FALSE;
3034 syntax_start(wp, lnum);
3035 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003036 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 else
3038 {
3039 did_emsg = save_did_emsg;
3040 has_syntax = TRUE;
3041 extra_check = TRUE;
3042 }
3043 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003044
3045 /* Check for columns to display for 'colorcolumn'. */
3046 color_cols = wp->w_p_cc_cols;
3047 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003048 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003049#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003050
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003051#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003052 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003053 && *wp->w_s->b_p_spl != NUL
3054 && wp->w_s->b_langp.ga_len > 0
3055 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003056 {
3057 /* Prepare for spell checking. */
3058 has_spell = TRUE;
3059 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003060
3061 /* Get the start of the next line, so that words that wrap to the next
3062 * line are found too: "et<line-break>al.".
3063 * Trick: skip a few chars for C/shell/Vim comments */
3064 nextline[SPWORDLEN] = NUL;
3065 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3066 {
3067 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3068 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3069 }
3070
3071 /* When a word wrapped from the previous line the start of the current
3072 * line is valid. */
3073 if (lnum == checked_lnum)
3074 cur_checked_col = checked_col;
3075 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003076
3077 /* When there was a sentence end in the previous line may require a
3078 * word starting with capital in this line. In line 1 always check
3079 * the first word. */
3080 if (lnum != capcol_lnum)
3081 cap_col = -1;
3082 if (lnum == 1)
3083 cap_col = 0;
3084 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#endif
3087
3088 /*
3089 * handle visual active in this window
3090 */
3091 fromcol = -10;
3092 tocol = MAXCOL;
3093#ifdef FEAT_VISUAL
3094 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3095 {
3096 /* Visual is after curwin->w_cursor */
3097 if (ltoreq(curwin->w_cursor, VIsual))
3098 {
3099 top = &curwin->w_cursor;
3100 bot = &VIsual;
3101 }
3102 else /* Visual is before curwin->w_cursor */
3103 {
3104 top = &VIsual;
3105 bot = &curwin->w_cursor;
3106 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003107 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (VIsual_mode == Ctrl_V) /* block mode */
3109 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003110 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 fromcol = wp->w_old_cursor_fcol;
3113 tocol = wp->w_old_cursor_lcol;
3114 }
3115 }
3116 else /* non-block mode */
3117 {
3118 if (lnum > top->lnum && lnum <= bot->lnum)
3119 fromcol = 0;
3120 else if (lnum == top->lnum)
3121 {
3122 if (VIsual_mode == 'V') /* linewise */
3123 fromcol = 0;
3124 else
3125 {
3126 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3127 if (gchar_pos(top) == NUL)
3128 tocol = fromcol + 1;
3129 }
3130 }
3131 if (VIsual_mode != 'V' && lnum == bot->lnum)
3132 {
3133 if (*p_sel == 'e' && bot->col == 0
3134#ifdef FEAT_VIRTUALEDIT
3135 && bot->coladd == 0
3136#endif
3137 )
3138 {
3139 fromcol = -10;
3140 tocol = MAXCOL;
3141 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003142 else if (bot->col == MAXCOL)
3143 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 else
3145 {
3146 pos = *bot;
3147 if (*p_sel == 'e')
3148 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3149 else
3150 {
3151 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3152 ++tocol;
3153 }
3154 }
3155 }
3156 }
3157
3158#ifndef MSDOS
3159 /* Check if the character under the cursor should not be inverted */
3160 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
3161# ifdef FEAT_GUI
3162 && !gui.in_use
3163# endif
3164 )
3165 noinvcur = TRUE;
3166#endif
3167
3168 /* if inverting in this line set area_highlighting */
3169 if (fromcol >= 0)
3170 {
3171 area_highlighting = TRUE;
3172 attr = hl_attr(HLF_V);
3173#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003174 if ((clip_star.available && !clip_star.owned
3175 && clip_isautosel_star())
3176 || (clip_plus.available && !clip_plus.owned
3177 && clip_isautosel_plus()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 attr = hl_attr(HLF_VNC);
3179#endif
3180 }
3181 }
3182
3183 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003184 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 */
3186 else
3187#endif /* FEAT_VISUAL */
3188 if (highlight_match
3189 && wp == curwin
3190 && lnum >= curwin->w_cursor.lnum
3191 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3192 {
3193 if (lnum == curwin->w_cursor.lnum)
3194 getvcol(curwin, &(curwin->w_cursor),
3195 (colnr_T *)&fromcol, NULL, NULL);
3196 else
3197 fromcol = 0;
3198 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3199 {
3200 pos.lnum = lnum;
3201 pos.col = search_match_endcol;
3202 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3203 }
3204 else
3205 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003206 /* do at least one character; happens when past end of line */
3207 if (fromcol == tocol)
3208 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 area_highlighting = TRUE;
3210 attr = hl_attr(HLF_I);
3211 }
3212
3213#ifdef FEAT_DIFF
3214 filler_lines = diff_check(wp, lnum);
3215 if (filler_lines < 0)
3216 {
3217 if (filler_lines == -1)
3218 {
3219 if (diff_find_change(wp, lnum, &change_start, &change_end))
3220 diff_hlf = HLF_ADD; /* added line */
3221 else if (change_start == 0)
3222 diff_hlf = HLF_TXD; /* changed text */
3223 else
3224 diff_hlf = HLF_CHD; /* changed line */
3225 }
3226 else
3227 diff_hlf = HLF_ADD; /* added line */
3228 filler_lines = 0;
3229 area_highlighting = TRUE;
3230 }
3231 if (lnum == wp->w_topline)
3232 filler_lines = wp->w_topfill;
3233 filler_todo = filler_lines;
3234#endif
3235
3236#ifdef LINE_ATTR
3237# ifdef FEAT_SIGNS
3238 /* If this line has a sign with line highlighting set line_attr. */
3239 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3240 if (v != 0)
3241 line_attr = sign_get_attr((int)v, TRUE);
3242# endif
3243# if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
3244 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003245 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 line_attr = hl_attr(HLF_L);
3247# endif
3248 if (line_attr != 0)
3249 area_highlighting = TRUE;
3250#endif
3251
3252 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3253 ptr = line;
3254
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003255#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003256 if (has_spell)
3257 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003258 /* For checking first word with a capital skip white space. */
3259 if (cap_col == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003260 cap_col = (int)(skipwhite(line) - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003261
Bram Moolenaar30abd282005-06-22 22:35:10 +00003262 /* To be able to spell-check over line boundaries copy the end of the
3263 * current line into nextline[]. Above the start of the next line was
3264 * copied to nextline[SPWORDLEN]. */
3265 if (nextline[SPWORDLEN] == NUL)
3266 {
3267 /* No next line or it is empty. */
3268 nextlinecol = MAXCOL;
3269 nextline_idx = 0;
3270 }
3271 else
3272 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003273 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003274 if (v < SPWORDLEN)
3275 {
3276 /* Short line, use it completely and append the start of the
3277 * next line. */
3278 nextlinecol = 0;
3279 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003280 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003281 nextline_idx = v + 1;
3282 }
3283 else
3284 {
3285 /* Long line, use only the last SPWORDLEN bytes. */
3286 nextlinecol = v - SPWORDLEN;
3287 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3288 nextline_idx = SPWORDLEN + 1;
3289 }
3290 }
3291 }
3292#endif
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /* find start of trailing whitespace */
3295 if (wp->w_p_list && lcs_trail)
3296 {
3297 trailcol = (colnr_T)STRLEN(ptr);
3298 while (trailcol > (colnr_T)0 && vim_iswhite(ptr[trailcol - 1]))
3299 --trailcol;
3300 trailcol += (colnr_T) (ptr - line);
3301 extra_check = TRUE;
3302 }
3303
3304 /*
3305 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3306 * first character to be displayed.
3307 */
3308 if (wp->w_p_wrap)
3309 v = wp->w_skipcol;
3310 else
3311 v = wp->w_leftcol;
3312 if (v > 0)
3313 {
3314#ifdef FEAT_MBYTE
3315 char_u *prev_ptr = ptr;
3316#endif
3317 while (vcol < v && *ptr != NUL)
3318 {
3319 c = win_lbr_chartabsize(wp, ptr, (colnr_T)vcol, NULL);
3320 vcol += c;
3321#ifdef FEAT_MBYTE
3322 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003324 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003327#if defined(FEAT_SYN_HL) || defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3328 /* When:
3329 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003330 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003331 * - 'virtualedit' is set, or
3332 * - the visual mode is active,
3333 * the end of the line may be before the start of the displayed part.
3334 */
3335 if (vcol < v && (
3336# ifdef FEAT_SYN_HL
3337 wp->w_p_cuc
Bram Moolenaar1a384422010-07-14 19:53:30 +02003338 || draw_color_col
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003339# if defined(FEAT_VIRTUALEDIT) || defined(FEAT_VISUAL)
3340 ||
3341# endif
3342# endif
3343# ifdef FEAT_VIRTUALEDIT
3344 virtual_active()
3345# ifdef FEAT_VISUAL
3346 ||
3347# endif
3348# endif
3349# ifdef FEAT_VISUAL
3350 (VIsual_active && wp->w_buffer == curwin->w_buffer)
3351# endif
3352 ))
3353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#endif
3357
3358 /* Handle a character that's not completely on the screen: Put ptr at
3359 * that character but skip the first few screen characters. */
3360 if (vcol > v)
3361 {
3362 vcol -= c;
3363#ifdef FEAT_MBYTE
3364 ptr = prev_ptr;
3365#else
3366 --ptr;
3367#endif
3368 n_skip = v - vcol;
3369 }
3370
3371 /*
3372 * Adjust for when the inverted text is before the screen,
3373 * and when the start of the inverted text is before the screen.
3374 */
3375 if (tocol <= vcol)
3376 fromcol = 0;
3377 else if (fromcol >= 0 && fromcol < vcol)
3378 fromcol = vcol;
3379
3380#ifdef FEAT_LINEBREAK
3381 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3382 if (wp->w_p_wrap)
3383 need_showbreak = TRUE;
3384#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003385#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003386 /* When spell checking a word we need to figure out the start of the
3387 * word and if it's badly spelled or not. */
3388 if (has_spell)
3389 {
3390 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003391 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003392 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003393
3394 pos = wp->w_cursor;
3395 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003396 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003397 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003398
3399 /* spell_move_to() may call ml_get() and make "line" invalid */
3400 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3401 ptr = line + linecol;
3402
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003403 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003404 {
3405 /* no bad word found at line start, don't check until end of a
3406 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003407 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003408 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003409 }
3410 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003411 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003412 /* bad word found, use attributes until end of word */
3413 word_end = wp->w_cursor.col + len + 1;
3414
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003415 /* Turn index into actual attributes. */
3416 if (spell_hlf != HLF_COUNT)
3417 spell_attr = highlight_attr[spell_hlf];
3418 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003419 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003420
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003421# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003422 /* Need to restart syntax highlighting for this line. */
3423 if (has_syntax)
3424 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003425# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003426 }
3427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
3429
3430 /*
3431 * Correct highlighting for cursor that can't be disabled.
3432 * Avoids having to check this for each character.
3433 */
3434 if (fromcol >= 0)
3435 {
3436 if (noinvcur)
3437 {
3438 if ((colnr_T)fromcol == wp->w_virtcol)
3439 {
3440 /* highlighting starts at cursor, let it start just after the
3441 * cursor */
3442 fromcol_prev = fromcol;
3443 fromcol = -1;
3444 }
3445 else if ((colnr_T)fromcol < wp->w_virtcol)
3446 /* restart highlighting after the cursor */
3447 fromcol_prev = wp->w_virtcol;
3448 }
3449 if (fromcol >= tocol)
3450 fromcol = -1;
3451 }
3452
3453#ifdef FEAT_SEARCH_EXTRA
3454 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003455 * Handle highlighting the last used search pattern and matches.
3456 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003458 cur = wp->w_match_head;
3459 shl_flag = FALSE;
3460 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003462 if (shl_flag == FALSE)
3463 {
3464 shl = &search_hl;
3465 shl_flag = TRUE;
3466 }
3467 else
3468 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003469 shl->startcol = MAXCOL;
3470 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 shl->attr_cur = 0;
3472 if (shl->rm.regprog != NULL)
3473 {
3474 v = (long)(ptr - line);
3475 next_search_hl(wp, shl, lnum, (colnr_T)v);
3476
3477 /* Need to get the line again, a multi-line regexp may have made it
3478 * invalid. */
3479 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3480 ptr = line + v;
3481
3482 if (shl->lnum != 0 && shl->lnum <= lnum)
3483 {
3484 if (shl->lnum == lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003485 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003487 shl->startcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3489 - shl->rm.startpos[0].lnum)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003490 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003492 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 /* Highlight one character for an empty match. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003494 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496#ifdef FEAT_MBYTE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003497 if (has_mbyte && line[shl->endcol] != NUL)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003498 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 else
3500#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003501 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003503 if ((long)shl->startcol < v) /* match at leftcol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 shl->attr_cur = shl->attr;
3506 search_attr = shl->attr;
3507 }
3508 area_highlighting = TRUE;
3509 }
3510 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003511 if (shl != &search_hl && cur != NULL)
3512 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
3514#endif
3515
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003516#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003517 /* Cursor line highlighting for 'cursorline' in the current window. Not
3518 * when Visual mode is active, because it's not clear what is selected
3519 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003520 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
3521 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003522 {
3523 line_attr = hl_attr(HLF_CUL);
3524 area_highlighting = TRUE;
3525 }
3526#endif
3527
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003528 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 col = 0;
3530#ifdef FEAT_RIGHTLEFT
3531 if (wp->w_p_rl)
3532 {
3533 /* Rightleft window: process the text in the normal direction, but put
3534 * it in current_ScreenLine[] from right to left. Start at the
3535 * rightmost column of the window. */
3536 col = W_WIDTH(wp) - 1;
3537 off += col;
3538 }
3539#endif
3540
3541 /*
3542 * Repeat for the whole displayed line.
3543 */
3544 for (;;)
3545 {
3546 /* Skip this quickly when working on the text. */
3547 if (draw_state != WL_LINE)
3548 {
3549#ifdef FEAT_CMDWIN
3550 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3551 {
3552 draw_state = WL_CMDLINE;
3553 if (cmdwin_type != 0 && wp == curwin)
3554 {
3555 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003557 c_extra = cmdwin_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 char_attr = hl_attr(HLF_AT);
3559 }
3560 }
3561#endif
3562
3563#ifdef FEAT_FOLDING
3564 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3565 {
3566 draw_state = WL_FOLD;
3567 if (wp->w_p_fdc > 0)
3568 {
3569 /* Draw the 'foldcolumn'. */
3570 fill_foldcolumn(extra, wp, FALSE, lnum);
3571 n_extra = wp->w_p_fdc;
3572 p_extra = extra;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003573 p_extra[n_extra] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 c_extra = NUL;
3575 char_attr = hl_attr(HLF_FC);
3576 }
3577 }
3578#endif
3579
3580#ifdef FEAT_SIGNS
3581 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3582 {
3583 draw_state = WL_SIGN;
3584 /* Show the sign column when there are any signs in this
3585 * buffer or when using Netbeans. */
3586 if (draw_signcolumn(wp)
3587# ifdef FEAT_DIFF
3588 && filler_todo <= 0
3589# endif
3590 )
3591 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003592 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003594 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595# endif
3596
3597 /* Draw two cells with the sign value or blank. */
3598 c_extra = ' ';
3599 char_attr = hl_attr(HLF_SC);
3600 n_extra = 2;
3601
3602 if (row == startrow)
3603 {
3604 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3605 SIGN_TEXT);
3606# ifdef FEAT_SIGN_ICONS
3607 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3608 SIGN_ICON);
3609 if (gui.in_use && icon_sign != 0)
3610 {
3611 /* Use the image in this position. */
3612 c_extra = SIGN_BYTE;
3613# ifdef FEAT_NETBEANS_INTG
3614 if (buf_signcount(wp->w_buffer, lnum) > 1)
3615 c_extra = MULTISIGN_BYTE;
3616# endif
3617 char_attr = icon_sign;
3618 }
3619 else
3620# endif
3621 if (text_sign != 0)
3622 {
3623 p_extra = sign_get_text(text_sign);
3624 if (p_extra != NULL)
3625 {
3626 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003627 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 char_attr = sign_get_attr(text_sign, FALSE);
3630 }
3631 }
3632 }
3633 }
3634#endif
3635
3636 if (draw_state == WL_NR - 1 && n_extra == 0)
3637 {
3638 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003639 /* Display the absolute or relative line number. After the
3640 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3641 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 && (row == startrow
3643#ifdef FEAT_DIFF
3644 + filler_lines
3645#endif
3646 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3647 {
3648 /* Draw the line number (empty space after wrapping). */
3649 if (row == startrow
3650#ifdef FEAT_DIFF
3651 + filler_lines
3652#endif
3653 )
3654 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003655 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003656 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003657
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003658 if (wp->w_p_nu && !wp->w_p_rnu)
3659 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003660 num = (long)lnum;
3661 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003662 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003663 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003664 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003665 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003666 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003667 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003668 num = lnum;
3669 fmt = "%-*ld ";
3670 }
3671 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003672
Bram Moolenaar700e7342013-01-30 12:31:36 +01003673 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003674 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (wp->w_skipcol > 0)
3676 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3677 *p_extra = '-';
3678#ifdef FEAT_RIGHTLEFT
3679 if (wp->w_p_rl) /* reverse line numbers */
3680 rl_mirror(extra);
3681#endif
3682 p_extra = extra;
3683 c_extra = NUL;
3684 }
3685 else
3686 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003687 n_extra = number_width(wp) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 char_attr = hl_attr(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003689#ifdef FEAT_SYN_HL
3690 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003691 * the current line differently.
3692 * TODO: Can we use CursorLine instead of CursorLineNr
3693 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003694 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003695 && lnum == wp->w_cursor.lnum)
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003696 char_attr = hl_attr(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 }
3700
3701#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3702 if (draw_state == WL_SBR - 1 && n_extra == 0)
3703 {
3704 draw_state = WL_SBR;
3705# ifdef FEAT_DIFF
3706 if (filler_todo > 0)
3707 {
3708 /* Draw "deleted" diff line(s). */
3709 if (char2cells(fill_diff) > 1)
3710 c_extra = '-';
3711 else
3712 c_extra = fill_diff;
3713# ifdef FEAT_RIGHTLEFT
3714 if (wp->w_p_rl)
3715 n_extra = col + 1;
3716 else
3717# endif
3718 n_extra = W_WIDTH(wp) - col;
3719 char_attr = hl_attr(HLF_DED);
3720 }
3721# endif
3722# ifdef FEAT_LINEBREAK
3723 if (*p_sbr != NUL && need_showbreak)
3724 {
3725 /* Draw 'showbreak' at the start of each broken line. */
3726 p_extra = p_sbr;
3727 c_extra = NUL;
3728 n_extra = (int)STRLEN(p_sbr);
3729 char_attr = hl_attr(HLF_AT);
3730 need_showbreak = FALSE;
3731 /* Correct end of highlighted area for 'showbreak',
3732 * required when 'linebreak' is also set. */
3733 if (tocol == vcol)
3734 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003735#ifdef FEAT_SYN_HL
3736 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003737 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003738 char_attr = hl_combine_attr(char_attr, HLF_CLN);
3739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741# endif
3742 }
3743#endif
3744
3745 if (draw_state == WL_LINE - 1 && n_extra == 0)
3746 {
3747 draw_state = WL_LINE;
3748 if (saved_n_extra)
3749 {
3750 /* Continue item from end of wrapped line. */
3751 n_extra = saved_n_extra;
3752 c_extra = saved_c_extra;
3753 p_extra = saved_p_extra;
3754 char_attr = saved_char_attr;
3755 }
3756 else
3757 char_attr = 0;
3758 }
3759 }
3760
3761 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01003762 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#ifdef FEAT_DIFF
3765 && filler_todo <= 0
3766#endif
3767 )
3768 {
3769 SCREEN_LINE(screen_row, W_WINCOL(wp), col, -(int)W_WIDTH(wp),
3770 wp->w_p_rl);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003771 /* Pretend we have finished updating the window. Except when
3772 * 'cursorcolumn' is set. */
3773#ifdef FEAT_SYN_HL
3774 if (wp->w_p_cuc)
3775 row = wp->w_cline_row + wp->w_cline_height;
3776 else
3777#endif
3778 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 break;
3780 }
3781
3782 if (draw_state == WL_LINE && area_highlighting)
3783 {
3784 /* handle Visual or match highlighting in this line */
3785 if (vcol == fromcol
3786#ifdef FEAT_MBYTE
3787 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
3788 && (*mb_ptr2cells)(ptr) > 1)
3789#endif
3790 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00003791 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 && vcol < tocol))
3793 area_attr = attr; /* start highlighting */
3794 else if (area_attr != 0
3795 && (vcol == tocol
3796 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799#ifdef FEAT_SEARCH_EXTRA
3800 if (!n_extra)
3801 {
3802 /*
3803 * Check for start/end of search pattern match.
3804 * After end, check for start/end of next match.
3805 * When another match, have to check for start again.
3806 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003807 * Do this for 'search_hl' and the match list (ordered by
3808 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003810 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003811 cur = wp->w_match_head;
3812 shl_flag = FALSE;
3813 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003815 if (shl_flag == FALSE
3816 && ((cur != NULL
3817 && cur->priority > SEARCH_HL_PRIORITY)
3818 || cur == NULL))
3819 {
3820 shl = &search_hl;
3821 shl_flag = TRUE;
3822 }
3823 else
3824 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 while (shl->rm.regprog != NULL)
3826 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003827 if (shl->startcol != MAXCOL
3828 && v >= (long)shl->startcol
3829 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 shl->attr_cur = shl->attr;
3832 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003833 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 {
3835 shl->attr_cur = 0;
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 next_search_hl(wp, shl, lnum, (colnr_T)v);
3838
3839 /* Need to get the line again, a multi-line regexp
3840 * may have made it invalid. */
3841 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3842 ptr = line + v;
3843
3844 if (shl->lnum == lnum)
3845 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003846 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003848 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003850 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003852 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
3854 /* highlight empty match, try again after
3855 * it */
3856#ifdef FEAT_MBYTE
3857 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003858 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003859 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 else
3861#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003862 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864
3865 /* Loop to check if the match starts at the
3866 * current position */
3867 continue;
3868 }
3869 }
3870 break;
3871 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003872 if (shl != &search_hl && cur != NULL)
3873 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003875
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003876 /* Use attributes from match with highest priority among
3877 * 'search_hl' and the match list. */
3878 search_attr = search_hl.attr_cur;
3879 cur = wp->w_match_head;
3880 shl_flag = FALSE;
3881 while (cur != NULL || shl_flag == FALSE)
3882 {
3883 if (shl_flag == FALSE
3884 && ((cur != NULL
3885 && cur->priority > SEARCH_HL_PRIORITY)
3886 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003887 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003888 shl = &search_hl;
3889 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003890 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003891 else
3892 shl = &cur->hl;
3893 if (shl->attr_cur != 0)
3894 search_attr = shl->attr_cur;
3895 if (shl != &search_hl && cur != NULL)
3896 cur = cur->next;
3897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 }
3899#endif
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003902 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003904 if (diff_hlf == HLF_CHD && ptr - line >= change_start
3905 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00003907 if (diff_hlf == HLF_TXD && ptr - line > change_end
3908 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003910 line_attr = hl_attr(diff_hlf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003913
3914 /* Decide which of the highlight attributes to use. */
3915 attr_pri = TRUE;
3916 if (area_attr != 0)
3917 char_attr = area_attr;
3918 else if (search_attr != 0)
3919 char_attr = search_attr;
3920#ifdef LINE_ATTR
3921 /* Use line_attr when not in the Visual or 'incsearch' area
3922 * (area_attr may be 0 when "noinvcur" is set). */
3923 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00003924 || vcol < fromcol || vcol_prev < fromcol_prev
3925 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003926 char_attr = line_attr;
3927#endif
3928 else
3929 {
3930 attr_pri = FALSE;
3931#ifdef FEAT_SYN_HL
3932 if (has_syntax)
3933 char_attr = syntax_attr;
3934 else
3935#endif
3936 char_attr = 0;
3937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939
3940 /*
3941 * Get the next character to put on the screen.
3942 */
3943 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00003944 * The "p_extra" points to the extra stuff that is inserted to
3945 * represent special characters (non-printable stuff) and other
3946 * things. When all characters are the same, c_extra is used.
3947 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
3948 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
3950 */
3951 if (n_extra > 0)
3952 {
3953 if (c_extra != NUL)
3954 {
3955 c = c_extra;
3956#ifdef FEAT_MBYTE
3957 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
3958 if (enc_utf8 && (*mb_char2len)(c) > 1)
3959 {
3960 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003961 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003962 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
3965 mb_utf8 = FALSE;
3966#endif
3967 }
3968 else
3969 {
3970 c = *p_extra;
3971#ifdef FEAT_MBYTE
3972 if (has_mbyte)
3973 {
3974 mb_c = c;
3975 if (enc_utf8)
3976 {
3977 /* If the UTF-8 character is more than one byte:
3978 * Decode it into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003979 mb_l = (*mb_ptr2len)(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 mb_utf8 = FALSE;
3981 if (mb_l > n_extra)
3982 mb_l = 1;
3983 else if (mb_l > 1)
3984 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003985 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990 else
3991 {
3992 /* if this is a DBCS character, put it in "mb_c" */
3993 mb_l = MB_BYTE2LEN(c);
3994 if (mb_l >= n_extra)
3995 mb_l = 1;
3996 else if (mb_l > 1)
3997 mb_c = (c << 8) + p_extra[1];
3998 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003999 if (mb_l == 0) /* at the NUL at end-of-line */
4000 mb_l = 1;
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 /* If a double-width char doesn't fit display a '>' in the
4003 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004004 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005# ifdef FEAT_RIGHTLEFT
4006 wp->w_p_rl ? (col <= 0) :
4007# endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004008 (col >= W_WIDTH(wp) - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 && (*mb_char2cells)(mb_c) == 2)
4010 {
4011 c = '>';
4012 mb_c = c;
4013 mb_l = 1;
4014 mb_utf8 = FALSE;
4015 multi_attr = hl_attr(HLF_AT);
4016 /* put the pointer back to output the double-width
4017 * character at the start of the next line. */
4018 ++n_extra;
4019 --p_extra;
4020 }
4021 else
4022 {
4023 n_extra -= mb_l - 1;
4024 p_extra += mb_l - 1;
4025 }
4026 }
4027#endif
4028 ++p_extra;
4029 }
4030 --n_extra;
4031 }
4032 else
4033 {
4034 /*
4035 * Get a character from the line itself.
4036 */
4037 c = *ptr;
4038#ifdef FEAT_MBYTE
4039 if (has_mbyte)
4040 {
4041 mb_c = c;
4042 if (enc_utf8)
4043 {
4044 /* If the UTF-8 character is more than one byte: Decode it
4045 * into "mb_c". */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004046 mb_l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 mb_utf8 = FALSE;
4048 if (mb_l > 1)
4049 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004050 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* Overlong encoded ASCII or ASCII with composing char
4052 * is displayed normally, except a NUL. */
4053 if (mb_c < 0x80)
4054 c = mb_c;
4055 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004056
4057 /* At start of the line we can have a composing char.
4058 * Draw it as a space with a composing char. */
4059 if (utf_iscomposing(mb_c))
4060 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004061 int i;
4062
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004063 for (i = Screen_mco - 1; i > 0; --i)
4064 u8cc[i] = u8cc[i - 1];
4065 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004066 mb_c = ' ';
4067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069
4070 if ((mb_l == 1 && c >= 0x80)
4071 || (mb_l >= 1 && mb_c == 0)
4072 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004073# ifdef UNICODE16
4074 || mb_c >= 0x10000
4075# endif
4076 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 /*
4079 * Illegal UTF-8 byte: display as <xx>.
4080 * Non-BMP character : display as ? or fullwidth ?.
4081 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004082# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
4086 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004087# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 if (wp->w_p_rl) /* reverse */
4089 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004092# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else if (utf_char2cells(mb_c) != 2)
4094 STRCPY(extra, "?");
4095 else
4096 /* 0xff1f in UTF-8: full-width '?' */
4097 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 p_extra = extra;
4101 c = *p_extra;
4102 mb_c = mb_ptr2char_adv(&p_extra);
4103 mb_utf8 = (c >= 0x80);
4104 n_extra = (int)STRLEN(p_extra);
4105 c_extra = NUL;
4106 if (area_attr == 0 && search_attr == 0)
4107 {
4108 n_attr = n_extra + 1;
4109 extra_attr = hl_attr(HLF_8);
4110 saved_attr2 = char_attr; /* save current attr */
4111 }
4112 }
4113 else if (mb_l == 0) /* at the NUL at end-of-line */
4114 mb_l = 1;
4115#ifdef FEAT_ARABIC
4116 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4117 {
4118 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004119 int pc, pc1, nc;
4120 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 /* The idea of what is the previous and next
4123 * character depends on 'rightleft'. */
4124 if (wp->w_p_rl)
4125 {
4126 pc = prev_c;
4127 pc1 = prev_c1;
4128 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004129 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 else
4132 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004133 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004135 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 prev_c = mb_c;
4138
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004139 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 else
4142 prev_c = mb_c;
4143#endif
4144 }
4145 else /* enc_dbcs */
4146 {
4147 mb_l = MB_BYTE2LEN(c);
4148 if (mb_l == 0) /* at the NUL at end-of-line */
4149 mb_l = 1;
4150 else if (mb_l > 1)
4151 {
4152 /* We assume a second byte below 32 is illegal.
4153 * Hopefully this is OK for all double-byte encodings!
4154 */
4155 if (ptr[1] >= 32)
4156 mb_c = (c << 8) + ptr[1];
4157 else
4158 {
4159 if (ptr[1] == NUL)
4160 {
4161 /* head byte at end of line */
4162 mb_l = 1;
4163 transchar_nonprint(extra, c);
4164 }
4165 else
4166 {
4167 /* illegal tail byte */
4168 mb_l = 2;
4169 STRCPY(extra, "XX");
4170 }
4171 p_extra = extra;
4172 n_extra = (int)STRLEN(extra) - 1;
4173 c_extra = NUL;
4174 c = *p_extra++;
4175 if (area_attr == 0 && search_attr == 0)
4176 {
4177 n_attr = n_extra + 1;
4178 extra_attr = hl_attr(HLF_8);
4179 saved_attr2 = char_attr; /* save current attr */
4180 }
4181 mb_c = c;
4182 }
4183 }
4184 }
4185 /* If a double-width char doesn't fit display a '>' in the
4186 * last column; the character is displayed at the start of the
4187 * next line. */
4188 if ((
4189# ifdef FEAT_RIGHTLEFT
4190 wp->w_p_rl ? (col <= 0) :
4191# endif
4192 (col >= W_WIDTH(wp) - 1))
4193 && (*mb_char2cells)(mb_c) == 2)
4194 {
4195 c = '>';
4196 mb_c = c;
4197 mb_utf8 = FALSE;
4198 mb_l = 1;
4199 multi_attr = hl_attr(HLF_AT);
4200 /* Put pointer back so that the character will be
4201 * displayed at the start of the next line. */
4202 --ptr;
4203 }
4204 else if (*ptr != NUL)
4205 ptr += mb_l - 1;
4206
4207 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004208 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004209 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004210 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004213 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 c = ' ';
4215 if (area_attr == 0 && search_attr == 0)
4216 {
4217 n_attr = n_extra + 1;
4218 extra_attr = hl_attr(HLF_AT);
4219 saved_attr2 = char_attr; /* save current attr */
4220 }
4221 mb_c = c;
4222 mb_utf8 = FALSE;
4223 mb_l = 1;
4224 }
4225
4226 }
4227#endif
4228 ++ptr;
4229
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004230 /* 'list' : change char 160 to lcs_nbsp. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004231 if (wp->w_p_list && (c == 160
4232#ifdef FEAT_MBYTE
4233 || (mb_utf8 && mb_c == 160)
4234#endif
4235 ) && lcs_nbsp)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004236 {
4237 c = lcs_nbsp;
4238 if (area_attr == 0 && search_attr == 0)
4239 {
4240 n_attr = 1;
4241 extra_attr = hl_attr(HLF_8);
4242 saved_attr2 = char_attr; /* save current attr */
4243 }
4244#ifdef FEAT_MBYTE
4245 mb_c = c;
4246 if (enc_utf8 && (*mb_char2len)(c) > 1)
4247 {
4248 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004250 c = 0xc0;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004251 }
4252 else
4253 mb_utf8 = FALSE;
4254#endif
4255 }
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (extra_check)
4258 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004259#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004260 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004261#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004262
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004263#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 /* Get syntax attribute, unless still at the start of the line
4265 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004266 v = (long)(ptr - line);
4267 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /* Get the syntax attribute for the character. If there
4270 * is an error, disable syntax highlighting. */
4271 save_did_emsg = did_emsg;
4272 did_emsg = FALSE;
4273
Bram Moolenaar217ad922005-03-20 22:37:15 +00004274 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004275# ifdef FEAT_SPELL
4276 has_spell ? &can_spell :
4277# endif
4278 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004281 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004282 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004283 has_syntax = FALSE;
4284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 else
4286 did_emsg = save_did_emsg;
4287
4288 /* Need to get the line again, a multi-line regexp may
4289 * have made it invalid. */
4290 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4291 ptr = line + v;
4292
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004293 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004295 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004296 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004297# ifdef FEAT_CONCEAL
4298 /* no concealing past the end of the line, it interferes
4299 * with line highlighting */
4300 if (c == NUL)
4301 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004302 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004303 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004306#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004307
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004308#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004309 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004310 * Only do this when there is no syntax highlighting, the
4311 * @Spell cluster is not used or the current syntax item
4312 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004313 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004314 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004315 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004316# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004317 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004318 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004319# endif
4320 if (c != 0 && (
4321# ifdef FEAT_SYN_HL
4322 !has_syntax ||
4323# endif
4324 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004325 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004326 char_u *prev_ptr, *p;
4327 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004328 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004329# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004330 if (has_mbyte)
4331 {
4332 prev_ptr = ptr - mb_l;
4333 v -= mb_l - 1;
4334 }
4335 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004336# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004337 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004338
4339 /* Use nextline[] if possible, it has the start of the
4340 * next line concatenated. */
4341 if ((prev_ptr - line) - nextlinecol >= 0)
4342 p = nextline + (prev_ptr - line) - nextlinecol;
4343 else
4344 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004345 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004346 len = spell_check(wp, p, &spell_hlf, &cap_col,
4347 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004348 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004349
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004350 /* In Insert mode only highlight a word that
4351 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004352 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004353 && (State & INSERT) != 0
4354 && wp->w_cursor.lnum == lnum
4355 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004356 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004357 && wp->w_cursor.col < (colnr_T)word_end)
4358 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004359 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004360 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004361 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004362
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004363 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004364 && (p - nextline) + len > nextline_idx)
4365 {
4366 /* Remember that the good word continues at the
4367 * start of the next line. */
4368 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004369 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004370 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004371
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004372 /* Turn index into actual attributes. */
4373 if (spell_hlf != HLF_COUNT)
4374 spell_attr = highlight_attr[spell_hlf];
4375
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004376 if (cap_col > 0)
4377 {
4378 if (p != prev_ptr
4379 && (p - nextline) + cap_col >= nextline_idx)
4380 {
4381 /* Remember that the word in the next line
4382 * must start with a capital. */
4383 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004384 cap_col = (int)((p - nextline) + cap_col
4385 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004386 }
4387 else
4388 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004389 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004390 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004391 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004392 }
4393 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004394 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004396 char_attr = hl_combine_attr(char_attr, spell_attr);
4397 else
4398 char_attr = hl_combine_attr(spell_attr, char_attr);
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
4401#ifdef FEAT_LINEBREAK
4402 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004403 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
4405 if (wp->w_p_lbr && vim_isbreak(c) && !vim_isbreak(*ptr)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004406 && !wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
4408 n_extra = win_lbr_chartabsize(wp, ptr - (
4409# ifdef FEAT_MBYTE
4410 has_mbyte ? mb_l :
4411# endif
4412 1), (colnr_T)vcol, NULL) - 1;
4413 c_extra = ' ';
4414 if (vim_iswhite(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004415 {
4416#ifdef FEAT_CONCEAL
4417 if (c == TAB)
4418 /* See "Tab alignment" below. */
4419 FIX_FOR_BOGUSCOLS;
4420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424#endif
4425
4426 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4427 {
4428 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004429 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 n_attr = 1;
4432 extra_attr = hl_attr(HLF_8);
4433 saved_attr2 = char_attr; /* save current attr */
4434 }
4435#ifdef FEAT_MBYTE
4436 mb_c = c;
4437 if (enc_utf8 && (*mb_char2len)(c) > 1)
4438 {
4439 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004440 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004441 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 else
4444 mb_utf8 = FALSE;
4445#endif
4446 }
4447 }
4448
4449 /*
4450 * Handling of non-printable characters.
4451 */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004452 if (!(chartab[c & 0xff] & CT_PRINT_CHAR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
4454 /*
4455 * when getting a character from the file, we may have to
4456 * turn it into something else on the way to putting it
4457 * into "ScreenLines".
4458 */
4459 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4460 {
4461 /* tab amount depends on current column */
4462 n_extra = (int)wp->w_buffer->b_p_ts
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004463 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4464#ifdef FEAT_CONCEAL
4465 /* Tab alignment should be identical regardless of
4466 * 'conceallevel' value. So tab compensates of all
4467 * previous concealed characters, and thus resets vcol_off
4468 * and boguscols accumulated so far in the line. Note that
4469 * the tab can be longer than 'tabstop' when there
4470 * are concealed characters. */
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004471 FIX_FOR_BOGUSCOLS;
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#ifdef FEAT_MBYTE
4474 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4475#endif
4476 if (wp->w_p_list)
4477 {
4478 c = lcs_tab1;
4479 c_extra = lcs_tab2;
4480 n_attr = n_extra + 1;
4481 extra_attr = hl_attr(HLF_8);
4482 saved_attr2 = char_attr; /* save current attr */
4483#ifdef FEAT_MBYTE
4484 mb_c = c;
4485 if (enc_utf8 && (*mb_char2len)(c) > 1)
4486 {
4487 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004488 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004489 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491#endif
4492 }
4493 else
4494 {
4495 c_extra = ' ';
4496 c = ' ';
4497 }
4498 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004499 else if (c == NUL
4500 && ((wp->w_p_list && lcs_eol > 0)
4501 || ((fromcol >= 0 || fromcol_prev >= 0)
4502 && tocol > vcol
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004503#ifdef FEAT_VISUAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004504 && VIsual_mode != Ctrl_V
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004505#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004506 && (
4507# ifdef FEAT_RIGHTLEFT
4508 wp->w_p_rl ? (col >= 0) :
4509# endif
4510 (col < W_WIDTH(wp)))
4511 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004512 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004513 && (colnr_T)vcol == wp->w_virtcol)))
4514 && lcs_eol_one >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004516 /* Display a '$' after the line or highlight an extra
4517 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4519 /* For a diff line the highlighting continues after the
4520 * "$". */
4521 if (
4522# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004523 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524# ifdef LINE_ATTR
4525 &&
4526# endif
4527# endif
4528# ifdef LINE_ATTR
4529 line_attr == 0
4530# endif
4531 )
4532#endif
4533 {
4534#ifdef FEAT_VIRTUALEDIT
4535 /* In virtualedit, visual selections may extend
4536 * beyond end of line. */
4537 if (area_highlighting && virtual_active()
4538 && tocol != MAXCOL && vcol < tocol)
4539 n_extra = 0;
4540 else
4541#endif
4542 {
4543 p_extra = at_end_str;
4544 n_extra = 1;
4545 c_extra = NUL;
4546 }
4547 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004548 if (wp->w_p_list)
4549 c = lcs_eol;
4550 else
4551 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 lcs_eol_one = -1;
4553 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004554 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 extra_attr = hl_attr(HLF_AT);
4557 n_attr = 1;
4558 }
4559#ifdef FEAT_MBYTE
4560 mb_c = c;
4561 if (enc_utf8 && (*mb_char2len)(c) > 1)
4562 {
4563 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004564 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004565 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 else
4568 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4569#endif
4570 }
4571 else if (c != NUL)
4572 {
4573 p_extra = transchar(c);
4574#ifdef FEAT_RIGHTLEFT
4575 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
4576 rl_mirror(p_extra); /* reverse "<12>" */
4577#endif
4578 n_extra = byte2cells(c) - 1;
4579 c_extra = NUL;
4580 c = *p_extra++;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004581 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
4583 n_attr = n_extra + 1;
4584 extra_attr = hl_attr(HLF_8);
4585 saved_attr2 = char_attr; /* save current attr */
4586 }
4587#ifdef FEAT_MBYTE
4588 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4589#endif
4590 }
4591#ifdef FEAT_VIRTUALEDIT
4592 else if (VIsual_active
4593 && (VIsual_mode == Ctrl_V
4594 || VIsual_mode == 'v')
4595 && virtual_active()
4596 && tocol != MAXCOL
4597 && vcol < tocol
4598 && (
4599# ifdef FEAT_RIGHTLEFT
4600 wp->w_p_rl ? (col >= 0) :
4601# endif
4602 (col < W_WIDTH(wp))))
4603 {
4604 c = ' ';
4605 --ptr; /* put it back at the NUL */
4606 }
4607#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004608#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 else if ((
4610# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004611 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 ) && (
4615# ifdef FEAT_RIGHTLEFT
4616 wp->w_p_rl ? (col >= 0) :
4617# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02004618 (col
4619# ifdef FEAT_CONCEAL
4620 - boguscols
4621# endif
4622 < W_WIDTH(wp))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 /* Highlight until the right side of the window */
4625 c = ' ';
4626 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004627
4628 /* Remember we do the char for line highlighting. */
4629 ++did_line_attr;
4630
4631 /* don't do search HL for the rest of the line */
4632 if (line_attr != 0 && char_attr == search_attr && col > 0)
4633 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634# ifdef FEAT_DIFF
4635 if (diff_hlf == HLF_TXD)
4636 {
4637 diff_hlf = HLF_CHD;
4638 if (attr == 0 || char_attr != attr)
4639 char_attr = hl_attr(diff_hlf);
4640 }
4641# endif
4642 }
4643#endif
4644 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02004645
4646#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004647 if ( wp->w_p_cole > 0
4648 && (wp != curwin || lnum != wp->w_cursor.lnum ||
4649 conceal_cursor_line(wp))
Bram Moolenaarf70e3d62010-07-24 13:15:07 +02004650 && (syntax_flags & HL_CONCEAL) != 0
Bram Moolenaare6dc5732010-07-24 23:52:26 +02004651 && !(lnum_in_visual_area
4652 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02004653 {
4654 char_attr = conceal_attr;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004655 if (prev_syntax_id != syntax_seqnr
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004656 && (syn_get_sub_char() != NUL || wp->w_p_cole == 1)
4657 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004658 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004659 /* First time at this concealed item: display one
4660 * character. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004661 if (syn_get_sub_char() != NUL)
4662 c = syn_get_sub_char();
4663 else if (lcs_conceal != NUL)
4664 c = lcs_conceal;
4665 else
4666 c = ' ';
4667
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004668 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004669
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004670 if (n_extra > 0)
4671 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004672 vcol += n_extra;
4673 if (wp->w_p_wrap && n_extra > 0)
4674 {
4675# ifdef FEAT_RIGHTLEFT
4676 if (wp->w_p_rl)
4677 {
4678 col -= n_extra;
4679 boguscols -= n_extra;
4680 }
4681 else
4682# endif
4683 {
4684 boguscols += n_extra;
4685 col += n_extra;
4686 }
4687 }
4688 n_extra = 0;
4689 n_attr = 0;
4690 }
4691 else if (n_skip == 0)
4692 {
4693 is_concealing = TRUE;
4694 n_skip = 1;
4695 }
4696# ifdef FEAT_MBYTE
4697 mb_c = c;
4698 if (enc_utf8 && (*mb_char2len)(c) > 1)
4699 {
4700 mb_utf8 = TRUE;
4701 u8cc[0] = 0;
4702 c = 0xc0;
4703 }
4704 else
4705 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4706# endif
4707 }
4708 else
4709 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004710 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02004711 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004712 }
4713#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004716#ifdef FEAT_CONCEAL
4717 /* In the cursor line and we may be concealing characters: correct
4718 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02004719 if (!did_wcol && draw_state == WL_LINE
4720 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004721 && conceal_cursor_line(wp)
4722 && (int)wp->w_virtcol <= vcol + n_skip)
4723 {
4724 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02004725 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02004726 did_wcol = TRUE;
4727 }
4728#endif
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 /* Don't override visual selection highlighting. */
4731 if (n_attr > 0
4732 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004733 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 char_attr = extra_attr;
4735
Bram Moolenaar81695252004-12-29 20:58:21 +00004736#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /* XIM don't send preedit_start and preedit_end, but they send
4738 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
4739 * im_is_preediting() here. */
4740 if (xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004741 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 && (State & INSERT)
4743 && !p_imdisable
4744 && im_is_preediting()
4745 && draw_state == WL_LINE)
4746 {
4747 colnr_T tcol;
4748
4749 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004750 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
4752 tcol = preedit_end_col;
4753 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
4754 {
4755 if (feedback_old_attr < 0)
4756 {
4757 feedback_col = 0;
4758 feedback_old_attr = char_attr;
4759 }
4760 char_attr = im_get_feedback_attr(feedback_col);
4761 if (char_attr < 0)
4762 char_attr = feedback_old_attr;
4763 feedback_col++;
4764 }
4765 else if (feedback_old_attr >= 0)
4766 {
4767 char_attr = feedback_old_attr;
4768 feedback_old_attr = -1;
4769 feedback_col = 0;
4770 }
4771 }
4772#endif
4773 /*
4774 * Handle the case where we are in column 0 but not on the first
4775 * character of the line and the user wants us to show us a
4776 * special character (via 'listchars' option "precedes:<char>".
4777 */
4778 if (lcs_prec_todo != NUL
4779 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
4780#ifdef FEAT_DIFF
4781 && filler_todo <= 0
4782#endif
4783 && draw_state > WL_NR
4784 && c != NUL)
4785 {
4786 c = lcs_prec;
4787 lcs_prec_todo = NUL;
4788#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02004789 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
4790 {
4791 /* Double-width character being overwritten by the "precedes"
4792 * character, need to fill up half the character. */
4793 c_extra = MB_FILLER_CHAR;
4794 n_extra = 1;
4795 n_attr = 2;
4796 extra_attr = hl_attr(HLF_AT);
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 mb_c = c;
4799 if (enc_utf8 && (*mb_char2len)(c) > 1)
4800 {
4801 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004802 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004803 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 else
4806 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4807#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004808 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 saved_attr3 = char_attr; /* save current attr */
4811 char_attr = hl_attr(HLF_AT); /* later copied to char_attr */
4812 n_attr3 = 1;
4813 }
4814 }
4815
4816 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00004817 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00004819 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004820#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004821 || did_line_attr == 1
4822#endif
4823 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00004825#ifdef FEAT_SEARCH_EXTRA
4826 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00004827
4828 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00004829 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00004830 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004831#endif
4832
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004833 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 * highlight match at end of line. If it's beyond the last
4835 * char on the screen, just overwrite that one (tricky!) Not
4836 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004837#ifdef FEAT_SEARCH_EXTRA
4838 prevcol_hl_flag = FALSE;
4839 if (prevcol == (long)search_hl.startcol)
4840 prevcol_hl_flag = TRUE;
4841 else
4842 {
4843 cur = wp->w_match_head;
4844 while (cur != NULL)
4845 {
4846 if (prevcol == (long)cur->hl.startcol)
4847 {
4848 prevcol_hl_flag = TRUE;
4849 break;
4850 }
4851 cur = cur->next;
4852 }
4853 }
4854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004856 && ((area_attr != 0 && vcol == fromcol
4857#ifdef FEAT_VISUAL
4858 && (VIsual_mode != Ctrl_V
4859 || lnum == VIsual.lnum
4860 || lnum == curwin->w_cursor.lnum)
4861#endif
4862 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#ifdef FEAT_SEARCH_EXTRA
4864 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004865 || (prevcol_hl_flag == TRUE
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00004866# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00004867 && did_line_attr <= 1
4868# endif
4869 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#endif
4871 ))
4872 {
4873 int n = 0;
4874
4875#ifdef FEAT_RIGHTLEFT
4876 if (wp->w_p_rl)
4877 {
4878 if (col < 0)
4879 n = 1;
4880 }
4881 else
4882#endif
4883 {
4884 if (col >= W_WIDTH(wp))
4885 n = -1;
4886 }
4887 if (n != 0)
4888 {
4889 /* At the window boundary, highlight the last character
4890 * instead (better than nothing). */
4891 off += n;
4892 col += n;
4893 }
4894 else
4895 {
4896 /* Add a blank character to highlight. */
4897 ScreenLines[off] = ' ';
4898#ifdef FEAT_MBYTE
4899 if (enc_utf8)
4900 ScreenLinesUC[off] = 0;
4901#endif
4902 }
4903#ifdef FEAT_SEARCH_EXTRA
4904 if (area_attr == 0)
4905 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004906 /* Use attributes from match with highest priority among
4907 * 'search_hl' and the match list. */
4908 char_attr = search_hl.attr;
4909 cur = wp->w_match_head;
4910 shl_flag = FALSE;
4911 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004912 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004913 if (shl_flag == FALSE
4914 && ((cur != NULL
4915 && cur->priority > SEARCH_HL_PRIORITY)
4916 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004917 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004918 shl = &search_hl;
4919 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004920 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004921 else
4922 shl = &cur->hl;
4923 if ((ptr - line) - 1 == (long)shl->startcol)
4924 char_attr = shl->attr;
4925 if (shl != &search_hl && cur != NULL)
4926 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929#endif
4930 ScreenAttrs[off] = char_attr;
4931#ifdef FEAT_RIGHTLEFT
4932 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00004933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004935 --off;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 else
4938#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00004939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00004941 ++off;
4942 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004943 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00004944#ifdef FEAT_SYN_HL
4945 eol_hl_off = 1;
4946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00004948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
Bram Moolenaar91170f82006-05-05 21:15:17 +00004950 /*
4951 * At end of the text line.
4952 */
4953 if (c == NUL)
4954 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004955#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004956 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
4957 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00004958 {
4959 /* highlight last char after line */
4960 --col;
4961 --off;
4962 --vcol;
4963 }
4964
Bram Moolenaar1a384422010-07-14 19:53:30 +02004965 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00004966 if (wp->w_p_wrap)
4967 v = wp->w_skipcol;
4968 else
4969 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02004970
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004971 /* check if line ends before left margin */
4972 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004973 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004974#ifdef FEAT_CONCEAL
4975 /* Get rid of the boguscols now, we want to draw until the right
4976 * edge for 'cursorcolumn'. */
4977 col -= boguscols;
4978 boguscols = 0;
4979#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02004980
4981 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004982 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02004983
4984 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02004985 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
4986 && (int)wp->w_virtcol <
4987 W_WIDTH(wp) * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02004988 && lnum != wp->w_cursor.lnum)
4989 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004990# ifdef FEAT_RIGHTLEFT
4991 && !wp->w_p_rl
4992# endif
4993 )
4994 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02004995 int rightmost_vcol = 0;
4996 int i;
4997
4998 if (wp->w_p_cuc)
4999 rightmost_vcol = wp->w_virtcol;
5000 if (draw_color_col)
5001 /* determine rightmost colorcolumn to possibly draw */
5002 for (i = 0; color_cols[i] >= 0; ++i)
5003 if (rightmost_vcol < color_cols[i])
5004 rightmost_vcol = color_cols[i];
5005
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005006 while (col < W_WIDTH(wp))
5007 {
5008 ScreenLines[off] = ' ';
5009#ifdef FEAT_MBYTE
5010 if (enc_utf8)
5011 ScreenLinesUC[off] = 0;
5012#endif
5013 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005014 if (draw_color_col)
5015 draw_color_col = advance_color_col(VCOL_HLC,
5016 &color_cols);
5017
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005018 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005019 ScreenAttrs[off++] = hl_attr(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005020 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005021 ScreenAttrs[off++] = hl_attr(HLF_MC);
5022 else
5023 ScreenAttrs[off++] = 0;
5024
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005025 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005026 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005027
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005028 ++vcol;
5029 }
5030 }
5031#endif
5032
Bram Moolenaar860cae12010-06-05 23:22:07 +02005033 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5034 (int)W_WIDTH(wp), wp->w_p_rl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 row++;
5036
5037 /*
5038 * Update w_cline_height and w_cline_folded if the cursor line was
5039 * updated (saves a call to plines() later).
5040 */
5041 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5042 {
5043 curwin->w_cline_row = startrow;
5044 curwin->w_cline_height = row - startrow;
5045#ifdef FEAT_FOLDING
5046 curwin->w_cline_folded = FALSE;
5047#endif
5048 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5049 }
5050
5051 break;
5052 }
5053
5054 /* line continues beyond line end */
5055 if (lcs_ext
5056 && !wp->w_p_wrap
5057#ifdef FEAT_DIFF
5058 && filler_todo <= 0
5059#endif
5060 && (
5061#ifdef FEAT_RIGHTLEFT
5062 wp->w_p_rl ? col == 0 :
5063#endif
5064 col == W_WIDTH(wp) - 1)
5065 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005066 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5068 {
5069 c = lcs_ext;
5070 char_attr = hl_attr(HLF_AT);
5071#ifdef FEAT_MBYTE
5072 mb_c = c;
5073 if (enc_utf8 && (*mb_char2len)(c) > 1)
5074 {
5075 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005076 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005077 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else
5080 mb_utf8 = FALSE;
5081#endif
5082 }
5083
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005084#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005085 /* advance to the next 'colorcolumn' */
5086 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005087 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005088
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005089 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005090 * highlight the cursor position itself.
5091 * Also highlight the 'colorcolumn' if it is different than
5092 * 'cursorcolumn' */
5093 vcol_save_attr = -1;
5094 if (draw_state == WL_LINE && !lnum_in_visual_area)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005095 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005096 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005097 && lnum != wp->w_cursor.lnum)
5098 {
5099 vcol_save_attr = char_attr;
5100 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_CUC));
5101 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005102 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005103 {
5104 vcol_save_attr = char_attr;
5105 char_attr = hl_combine_attr(char_attr, hl_attr(HLF_MC));
5106 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005107 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005108#endif
5109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 /*
5111 * Store character to be displayed.
5112 * Skip characters that are left of the screen for 'nowrap'.
5113 */
5114 vcol_prev = vcol;
5115 if (draw_state < WL_LINE || n_skip <= 0)
5116 {
5117 /*
5118 * Store the character.
5119 */
5120#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5121 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5122 {
5123 /* A double-wide character is: put first halve in left cell. */
5124 --off;
5125 --col;
5126 }
5127#endif
5128 ScreenLines[off] = c;
5129#ifdef FEAT_MBYTE
5130 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005131 {
5132 if ((mb_c & 0xff00) == 0x8e00)
5133 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 else if (enc_utf8)
5137 {
5138 if (mb_utf8)
5139 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005140 int i;
5141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005143 if ((c & 0xff) == 0)
5144 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005145 for (i = 0; i < Screen_mco; ++i)
5146 {
5147 ScreenLinesC[i][off] = u8cc[i];
5148 if (u8cc[i] == 0)
5149 break;
5150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152 else
5153 ScreenLinesUC[off] = 0;
5154 }
5155 if (multi_attr)
5156 {
5157 ScreenAttrs[off] = multi_attr;
5158 multi_attr = 0;
5159 }
5160 else
5161#endif
5162 ScreenAttrs[off] = char_attr;
5163
5164#ifdef FEAT_MBYTE
5165 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5166 {
5167 /* Need to fill two screen columns. */
5168 ++off;
5169 ++col;
5170 if (enc_utf8)
5171 /* UTF-8: Put a 0 in the second screen char. */
5172 ScreenLines[off] = 0;
5173 else
5174 /* DBCS: Put second byte in the second screen char. */
5175 ScreenLines[off] = mb_c & 0xff;
5176 ++vcol;
5177 /* When "tocol" is halfway a character, set it to the end of
5178 * the character, otherwise highlighting won't stop. */
5179 if (tocol == vcol)
5180 ++tocol;
5181#ifdef FEAT_RIGHTLEFT
5182 if (wp->w_p_rl)
5183 {
5184 /* now it's time to backup one cell */
5185 --off;
5186 --col;
5187 }
5188#endif
5189 }
5190#endif
5191#ifdef FEAT_RIGHTLEFT
5192 if (wp->w_p_rl)
5193 {
5194 --off;
5195 --col;
5196 }
5197 else
5198#endif
5199 {
5200 ++off;
5201 ++col;
5202 }
5203 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005204#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005205 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005206 {
5207 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005208 ++vcol_off;
5209 if (n_extra > 0)
5210 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005211 if (wp->w_p_wrap)
5212 {
5213 /*
5214 * Special voodoo required if 'wrap' is on.
5215 *
5216 * Advance the column indicator to force the line
5217 * drawing to wrap early. This will make the line
5218 * take up the same screen space when parts are concealed,
5219 * so that cursor line computations aren't messed up.
5220 *
5221 * To avoid the fictitious advance of 'col' causing
5222 * trailing junk to be written out of the screen line
5223 * we are building, 'boguscols' keeps track of the number
5224 * of bad columns we have advanced.
5225 */
5226 if (n_extra > 0)
5227 {
5228 vcol += n_extra;
5229# ifdef FEAT_RIGHTLEFT
5230 if (wp->w_p_rl)
5231 {
5232 col -= n_extra;
5233 boguscols -= n_extra;
5234 }
5235 else
5236# endif
5237 {
5238 col += n_extra;
5239 boguscols += n_extra;
5240 }
5241 n_extra = 0;
5242 n_attr = 0;
5243 }
5244
5245
5246# ifdef FEAT_MBYTE
5247 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5248 {
5249 /* Need to fill two screen columns. */
5250# ifdef FEAT_RIGHTLEFT
5251 if (wp->w_p_rl)
5252 {
5253 --boguscols;
5254 --col;
5255 }
5256 else
5257# endif
5258 {
5259 ++boguscols;
5260 ++col;
5261 }
5262 }
5263# endif
5264
5265# ifdef FEAT_RIGHTLEFT
5266 if (wp->w_p_rl)
5267 {
5268 --boguscols;
5269 --col;
5270 }
5271 else
5272# endif
5273 {
5274 ++boguscols;
5275 ++col;
5276 }
5277 }
5278 else
5279 {
5280 if (n_extra > 0)
5281 {
5282 vcol += n_extra;
5283 n_extra = 0;
5284 n_attr = 0;
5285 }
5286 }
5287
5288 }
5289#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 else
5291 --n_skip;
5292
Bram Moolenaar64486672010-05-16 15:46:46 +02005293 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5294 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005295 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#ifdef FEAT_DIFF
5297 && filler_todo <= 0
5298#endif
5299 )
5300 ++vcol;
5301
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005302#ifdef FEAT_SYN_HL
5303 if (vcol_save_attr >= 0)
5304 char_attr = vcol_save_attr;
5305#endif
5306
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 /* restore attributes after "predeces" in 'listchars' */
5308 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5309 char_attr = saved_attr3;
5310
5311 /* restore attributes after last 'listchars' or 'number' char */
5312 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5313 char_attr = saved_attr2;
5314
5315 /*
5316 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005317 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
5319 if ((
5320#ifdef FEAT_RIGHTLEFT
5321 wp->w_p_rl ? (col < 0) :
5322#endif
5323 (col >= W_WIDTH(wp)))
5324 && (*ptr != NUL
5325#ifdef FEAT_DIFF
5326 || filler_todo > 0
5327#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005328 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5330 )
5331 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332#ifdef FEAT_CONCEAL
5333 SCREEN_LINE(screen_row, W_WINCOL(wp), col - boguscols,
5334 (int)W_WIDTH(wp), wp->w_p_rl);
5335 boguscols = 0;
5336#else
5337 SCREEN_LINE(screen_row, W_WINCOL(wp), col,
5338 (int)W_WIDTH(wp), wp->w_p_rl);
5339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 ++row;
5341 ++screen_row;
5342
5343 /* When not wrapping and finished diff lines, or when displayed
5344 * '$' and highlighting until last column, break here. */
5345 if ((!wp->w_p_wrap
5346#ifdef FEAT_DIFF
5347 && filler_todo <= 0
5348#endif
5349 ) || lcs_eol_one == -1)
5350 break;
5351
5352 /* When the window is too narrow draw all "@" lines. */
5353 if (draw_state != WL_LINE
5354#ifdef FEAT_DIFF
5355 && filler_todo <= 0
5356#endif
5357 )
5358 {
5359 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
5360#ifdef FEAT_VERTSPLIT
5361 draw_vsep_win(wp, row);
5362#endif
5363 row = endrow;
5364 }
5365
5366 /* When line got too long for screen break here. */
5367 if (row == endrow)
5368 {
5369 ++row;
5370 break;
5371 }
5372
5373 if (screen_cur_row == screen_row - 1
5374#ifdef FEAT_DIFF
5375 && filler_todo <= 0
5376#endif
5377 && W_WIDTH(wp) == Columns)
5378 {
5379 /* Remember that the line wraps, used for modeless copy. */
5380 LineWraps[screen_row - 1] = TRUE;
5381
5382 /*
5383 * Special trick to make copy/paste of wrapped lines work with
5384 * xterm/screen: write an extra character beyond the end of
5385 * the line. This will work with all terminal types
5386 * (regardless of the xn,am settings).
5387 * Only do this on a fast tty.
5388 * Only do this if the cursor is on the current line
5389 * (something has been written in it).
5390 * Don't do this for the GUI.
5391 * Don't do this for double-width characters.
5392 * Don't do this for a window not at the right screen border.
5393 */
5394 if (p_tf
5395#ifdef FEAT_GUI
5396 && !gui.in_use
5397#endif
5398#ifdef FEAT_MBYTE
5399 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005400 && ((*mb_off2cells)(LineOffset[screen_row],
5401 LineOffset[screen_row] + screen_Columns)
5402 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005404 + (int)Columns - 2,
5405 LineOffset[screen_row] + screen_Columns)
5406 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408 )
5409 {
5410 /* First make sure we are at the end of the screen line,
5411 * then output the same character again to let the
5412 * terminal know about the wrap. If the terminal doesn't
5413 * auto-wrap, we overwrite the character. */
5414 if (screen_cur_col != W_WIDTH(wp))
5415 screen_char(LineOffset[screen_row - 1]
5416 + (unsigned)Columns - 1,
5417 screen_row - 1, (int)(Columns - 1));
5418
5419#ifdef FEAT_MBYTE
5420 /* When there is a multi-byte character, just output a
5421 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005422 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5423 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 out_char(' ');
5425 else
5426#endif
5427 out_char(ScreenLines[LineOffset[screen_row - 1]
5428 + (Columns - 1)]);
5429 /* force a redraw of the first char on the next line */
5430 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5431 screen_start(); /* don't know where cursor is now */
5432 }
5433 }
5434
5435 col = 0;
5436 off = (unsigned)(current_ScreenLine - ScreenLines);
5437#ifdef FEAT_RIGHTLEFT
5438 if (wp->w_p_rl)
5439 {
5440 col = W_WIDTH(wp) - 1; /* col is not used if breaking! */
5441 off += col;
5442 }
5443#endif
5444
5445 /* reset the drawing state for the start of a wrapped line */
5446 draw_state = WL_START;
5447 saved_n_extra = n_extra;
5448 saved_p_extra = p_extra;
5449 saved_c_extra = c_extra;
5450 saved_char_attr = char_attr;
5451 n_extra = 0;
5452 lcs_prec_todo = lcs_prec;
5453#ifdef FEAT_LINEBREAK
5454# ifdef FEAT_DIFF
5455 if (filler_todo <= 0)
5456# endif
5457 need_showbreak = TRUE;
5458#endif
5459#ifdef FEAT_DIFF
5460 --filler_todo;
5461 /* When the filler lines are actually below the last line of the
5462 * file, don't draw the line itself, break here. */
5463 if (filler_todo == 0 && wp->w_botfill)
5464 break;
5465#endif
5466 }
5467
5468 } /* for every character in the line */
5469
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005470#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005471 /* After an empty line check first word for capital. */
5472 if (*skipwhite(line) == NUL)
5473 {
5474 capcol_lnum = lnum + 1;
5475 cap_col = 0;
5476 }
5477#endif
5478
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 return row;
5480}
5481
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005482#ifdef FEAT_MBYTE
5483static int comp_char_differs __ARGS((int, int));
5484
5485/*
5486 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005487 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005488 */
5489 static int
5490comp_char_differs(off_from, off_to)
5491 int off_from;
5492 int off_to;
5493{
5494 int i;
5495
5496 for (i = 0; i < Screen_mco; ++i)
5497 {
5498 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5499 return TRUE;
5500 if (ScreenLinesC[i][off_from] == 0)
5501 break;
5502 }
5503 return FALSE;
5504}
5505#endif
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507/*
5508 * Check whether the given character needs redrawing:
5509 * - the (first byte of the) character is different
5510 * - the attributes are different
5511 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005512 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 */
5514 static int
5515char_needs_redraw(off_from, off_to, cols)
5516 int off_from;
5517 int off_to;
5518 int cols;
5519{
5520 if (cols > 0
5521 && ((ScreenLines[off_from] != ScreenLines[off_to]
5522 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
5523
5524#ifdef FEAT_MBYTE
5525 || (enc_dbcs != 0
5526 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
5527 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
5528 ? ScreenLines2[off_from] != ScreenLines2[off_to]
5529 : (cols > 1 && ScreenLines[off_from + 1]
5530 != ScreenLines[off_to + 1])))
5531 || (enc_utf8
5532 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
5533 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00005534 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02005535 || ((*mb_off2cells)(off_from, off_from + cols) > 1
5536 && ScreenLines[off_from + 1]
5537 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#endif
5539 ))
5540 return TRUE;
5541 return FALSE;
5542}
5543
5544/*
5545 * Move one "cooked" screen line to the screen, but only the characters that
5546 * have actually changed. Handle insert/delete character.
5547 * "coloff" gives the first column on the screen for this line.
5548 * "endcol" gives the columns where valid characters are.
5549 * "clear_width" is the width of the window. It's > 0 if the rest of the line
5550 * needs to be cleared, negative otherwise.
5551 * "rlflag" is TRUE in a rightleft window:
5552 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
5553 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
5554 */
5555 static void
5556screen_line(row, coloff, endcol, clear_width
5557#ifdef FEAT_RIGHTLEFT
5558 , rlflag
5559#endif
5560 )
5561 int row;
5562 int coloff;
5563 int endcol;
5564 int clear_width;
5565#ifdef FEAT_RIGHTLEFT
5566 int rlflag;
5567#endif
5568{
5569 unsigned off_from;
5570 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005571#ifdef FEAT_MBYTE
5572 unsigned max_off_from;
5573 unsigned max_off_to;
5574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 int col = 0;
5576#if defined(FEAT_GUI) || defined(UNIX) || defined(FEAT_VERTSPLIT)
5577 int hl;
5578#endif
5579 int force = FALSE; /* force update rest of the line */
5580 int redraw_this /* bool: does character need redraw? */
5581#ifdef FEAT_GUI
5582 = TRUE /* For GUI when while-loop empty */
5583#endif
5584 ;
5585 int redraw_next; /* redraw_this for next character */
5586#ifdef FEAT_MBYTE
5587 int clear_next = FALSE;
5588 int char_cells; /* 1: normal char */
5589 /* 2: occupies two display cells */
5590# define CHAR_CELLS char_cells
5591#else
5592# define CHAR_CELLS 1
5593#endif
5594
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01005595 /* Check for illegal row and col, just in case. */
5596 if (row >= Rows)
5597 row = Rows - 1;
5598 if (endcol > Columns)
5599 endcol = Columns;
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601# ifdef FEAT_CLIPBOARD
5602 clip_may_clear_selection(row, row);
5603# endif
5604
5605 off_from = (unsigned)(current_ScreenLine - ScreenLines);
5606 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00005607#ifdef FEAT_MBYTE
5608 max_off_from = off_from + screen_Columns;
5609 max_off_to = LineOffset[row] + screen_Columns;
5610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612#ifdef FEAT_RIGHTLEFT
5613 if (rlflag)
5614 {
5615 /* Clear rest first, because it's left of the text. */
5616 if (clear_width > 0)
5617 {
5618 while (col <= endcol && ScreenLines[off_to] == ' '
5619 && ScreenAttrs[off_to] == 0
5620# ifdef FEAT_MBYTE
5621 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5622# endif
5623 )
5624 {
5625 ++off_to;
5626 ++col;
5627 }
5628 if (col <= endcol)
5629 screen_fill(row, row + 1, col + coloff,
5630 endcol + coloff + 1, ' ', ' ', 0);
5631 }
5632 col = endcol + 1;
5633 off_to = LineOffset[row] + col + coloff;
5634 off_from += col;
5635 endcol = (clear_width > 0 ? clear_width : -clear_width);
5636 }
5637#endif /* FEAT_RIGHTLEFT */
5638
5639 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
5640
5641 while (col < endcol)
5642 {
5643#ifdef FEAT_MBYTE
5644 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00005645 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
5647 char_cells = 1;
5648#endif
5649
5650 redraw_this = redraw_next;
5651 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
5652 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
5653
5654#ifdef FEAT_GUI
5655 /* If the next character was bold, then redraw the current character to
5656 * remove any pixels that might have spilt over into us. This only
5657 * happens in the GUI.
5658 */
5659 if (redraw_next && gui.in_use)
5660 {
5661 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005662 if (hl > HL_ALL)
5663 hl = syn_attr2attr(hl);
5664 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 redraw_this = TRUE;
5666 }
5667#endif
5668
5669 if (redraw_this)
5670 {
5671 /*
5672 * Special handling when 'xs' termcap flag set (hpterm):
5673 * Attributes for characters are stored at the position where the
5674 * cursor is when writing the highlighting code. The
5675 * start-highlighting code must be written with the cursor on the
5676 * first highlighted character. The stop-highlighting code must
5677 * be written with the cursor just after the last highlighted
5678 * character.
5679 * Overwriting a character doesn't remove it's highlighting. Need
5680 * to clear the rest of the line, and force redrawing it
5681 * completely.
5682 */
5683 if ( p_wiv
5684 && !force
5685#ifdef FEAT_GUI
5686 && !gui.in_use
5687#endif
5688 && ScreenAttrs[off_to] != 0
5689 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
5690 {
5691 /*
5692 * Need to remove highlighting attributes here.
5693 */
5694 windgoto(row, col + coloff);
5695 out_str(T_CE); /* clear rest of this screen line */
5696 screen_start(); /* don't know where cursor is now */
5697 force = TRUE; /* force redraw of rest of the line */
5698 redraw_next = TRUE; /* or else next char would miss out */
5699
5700 /*
5701 * If the previous character was highlighted, need to stop
5702 * highlighting at this character.
5703 */
5704 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
5705 {
5706 screen_attr = ScreenAttrs[off_to - 1];
5707 term_windgoto(row, col + coloff);
5708 screen_stop_highlight();
5709 }
5710 else
5711 screen_attr = 0; /* highlighting has stopped */
5712 }
5713#ifdef FEAT_MBYTE
5714 if (enc_dbcs != 0)
5715 {
5716 /* Check if overwriting a double-byte with a single-byte or
5717 * the other way around requires another character to be
5718 * redrawn. For UTF-8 this isn't needed, because comparing
5719 * ScreenLinesUC[] is sufficient. */
5720 if (char_cells == 1
5721 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005722 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
5724 /* Writing a single-cell character over a double-cell
5725 * character: need to redraw the next cell. */
5726 ScreenLines[off_to + 1] = 0;
5727 redraw_next = TRUE;
5728 }
5729 else if (char_cells == 2
5730 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00005731 && (*mb_off2cells)(off_to, max_off_to) == 1
5732 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 /* Writing the second half of a double-cell character over
5735 * a double-cell character: need to redraw the second
5736 * cell. */
5737 ScreenLines[off_to + 2] = 0;
5738 redraw_next = TRUE;
5739 }
5740
5741 if (enc_dbcs == DBCS_JPNU)
5742 ScreenLines2[off_to] = ScreenLines2[off_from];
5743 }
5744 /* When writing a single-width character over a double-width
5745 * character and at the end of the redrawn text, need to clear out
5746 * the right halve of the old character.
5747 * Also required when writing the right halve of a double-width
5748 * char over the left halve of an existing one. */
5749 if (has_mbyte && col + char_cells == endcol
5750 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00005751 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00005753 && (*mb_off2cells)(off_to, max_off_to) == 1
5754 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 clear_next = TRUE;
5756#endif
5757
5758 ScreenLines[off_to] = ScreenLines[off_from];
5759#ifdef FEAT_MBYTE
5760 if (enc_utf8)
5761 {
5762 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
5763 if (ScreenLinesUC[off_from] != 0)
5764 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005765 int i;
5766
5767 for (i = 0; i < Screen_mco; ++i)
5768 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770 }
5771 if (char_cells == 2)
5772 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
5773#endif
5774
5775#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00005776 /* The bold trick makes a single column of pixels appear in the
5777 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 * character should be redrawn too. This happens for our own GUI
5779 * and for some xterms. */
5780 if (
5781# ifdef FEAT_GUI
5782 gui.in_use
5783# endif
5784# if defined(FEAT_GUI) && defined(UNIX)
5785 ||
5786# endif
5787# ifdef UNIX
5788 term_is_xterm
5789# endif
5790 )
5791 {
5792 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005793 if (hl > HL_ALL)
5794 hl = syn_attr2attr(hl);
5795 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 redraw_next = TRUE;
5797 }
5798#endif
5799 ScreenAttrs[off_to] = ScreenAttrs[off_from];
5800#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005801 /* For simplicity set the attributes of second half of a
5802 * double-wide character equal to the first half. */
5803 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005805
5806 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 else
5809#endif
5810 screen_char(off_to, row, col + coloff);
5811 }
5812 else if ( p_wiv
5813#ifdef FEAT_GUI
5814 && !gui.in_use
5815#endif
5816 && col + coloff > 0)
5817 {
5818 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
5819 {
5820 /*
5821 * Don't output stop-highlight when moving the cursor, it will
5822 * stop the highlighting when it should continue.
5823 */
5824 screen_attr = 0;
5825 }
5826 else if (screen_attr != 0)
5827 screen_stop_highlight();
5828 }
5829
5830 off_to += CHAR_CELLS;
5831 off_from += CHAR_CELLS;
5832 col += CHAR_CELLS;
5833 }
5834
5835#ifdef FEAT_MBYTE
5836 if (clear_next)
5837 {
5838 /* Clear the second half of a double-wide character of which the left
5839 * half was overwritten with a single-wide character. */
5840 ScreenLines[off_to] = ' ';
5841 if (enc_utf8)
5842 ScreenLinesUC[off_to] = 0;
5843 screen_char(off_to, row, col + coloff);
5844 }
5845#endif
5846
5847 if (clear_width > 0
5848#ifdef FEAT_RIGHTLEFT
5849 && !rlflag
5850#endif
5851 )
5852 {
5853#ifdef FEAT_GUI
5854 int startCol = col;
5855#endif
5856
5857 /* blank out the rest of the line */
5858 while (col < clear_width && ScreenLines[off_to] == ' '
5859 && ScreenAttrs[off_to] == 0
5860#ifdef FEAT_MBYTE
5861 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
5862#endif
5863 )
5864 {
5865 ++off_to;
5866 ++col;
5867 }
5868 if (col < clear_width)
5869 {
5870#ifdef FEAT_GUI
5871 /*
5872 * In the GUI, clearing the rest of the line may leave pixels
5873 * behind if the first character cleared was bold. Some bold
5874 * fonts spill over the left. In this case we redraw the previous
5875 * character too. If we didn't skip any blanks above, then we
5876 * only redraw if the character wasn't already redrawn anyway.
5877 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00005878 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
5880 hl = ScreenAttrs[off_to];
5881 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00005882 {
5883 int prev_cells = 1;
5884# ifdef FEAT_MBYTE
5885 if (enc_utf8)
5886 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
5887 * that its width is 2. */
5888 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
5889 else if (enc_dbcs != 0)
5890 {
5891 /* find previous character by counting from first
5892 * column and get its width. */
5893 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00005894 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00005895
5896 while (off < off_to)
5897 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00005898 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00005899 off += prev_cells;
5900 }
5901 }
5902
5903 if (enc_dbcs != 0 && prev_cells > 1)
5904 screen_char_2(off_to - prev_cells, row,
5905 col + coloff - prev_cells);
5906 else
5907# endif
5908 screen_char(off_to - prev_cells, row,
5909 col + coloff - prev_cells);
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912#endif
5913 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
5914 ' ', ' ', 0);
5915#ifdef FEAT_VERTSPLIT
5916 off_to += clear_width - col;
5917 col = clear_width;
5918#endif
5919 }
5920 }
5921
5922 if (clear_width > 0)
5923 {
5924#ifdef FEAT_VERTSPLIT
5925 /* For a window that's left of another, draw the separator char. */
5926 if (col + coloff < Columns)
5927 {
5928 int c;
5929
5930 c = fillchar_vsep(&hl);
5931 if (ScreenLines[off_to] != c
5932# ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005933 || (enc_utf8 && (int)ScreenLinesUC[off_to]
5934 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935# endif
5936 || ScreenAttrs[off_to] != hl)
5937 {
5938 ScreenLines[off_to] = c;
5939 ScreenAttrs[off_to] = hl;
5940# ifdef FEAT_MBYTE
5941 if (enc_utf8)
5942 {
5943 if (c >= 0x80)
5944 {
5945 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005946 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 }
5948 else
5949 ScreenLinesUC[off_to] = 0;
5950 }
5951# endif
5952 screen_char(off_to, row, col + coloff);
5953 }
5954 }
5955 else
5956#endif
5957 LineWraps[row] = FALSE;
5958 }
5959}
5960
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005961#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005963 * Mirror text "str" for right-left displaying.
5964 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005966 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967rl_mirror(str)
5968 char_u *str;
5969{
5970 char_u *p1, *p2;
5971 int t;
5972
5973 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
5974 {
5975 t = *p1;
5976 *p1 = *p2;
5977 *p2 = t;
5978 }
5979}
5980#endif
5981
5982#if defined(FEAT_WINDOWS) || defined(PROTO)
5983/*
5984 * mark all status lines for redraw; used after first :cd
5985 */
5986 void
5987status_redraw_all()
5988{
5989 win_T *wp;
5990
5991 for (wp = firstwin; wp; wp = wp->w_next)
5992 if (wp->w_status_height)
5993 {
5994 wp->w_redr_status = TRUE;
5995 redraw_later(VALID);
5996 }
5997}
5998
5999/*
6000 * mark all status lines of the current buffer for redraw
6001 */
6002 void
6003status_redraw_curbuf()
6004{
6005 win_T *wp;
6006
6007 for (wp = firstwin; wp; wp = wp->w_next)
6008 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6009 {
6010 wp->w_redr_status = TRUE;
6011 redraw_later(VALID);
6012 }
6013}
6014
6015/*
6016 * Redraw all status lines that need to be redrawn.
6017 */
6018 void
6019redraw_statuslines()
6020{
6021 win_T *wp;
6022
6023 for (wp = firstwin; wp; wp = wp->w_next)
6024 if (wp->w_redr_status)
6025 win_redr_status(wp);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006026 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006027 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028}
6029#endif
6030
6031#if (defined(FEAT_WILDMENU) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6032/*
6033 * Redraw all status lines at the bottom of frame "frp".
6034 */
6035 void
6036win_redraw_last_status(frp)
6037 frame_T *frp;
6038{
6039 if (frp->fr_layout == FR_LEAF)
6040 frp->fr_win->w_redr_status = TRUE;
6041 else if (frp->fr_layout == FR_ROW)
6042 {
6043 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6044 win_redraw_last_status(frp);
6045 }
6046 else /* frp->fr_layout == FR_COL */
6047 {
6048 frp = frp->fr_child;
6049 while (frp->fr_next != NULL)
6050 frp = frp->fr_next;
6051 win_redraw_last_status(frp);
6052 }
6053}
6054#endif
6055
6056#ifdef FEAT_VERTSPLIT
6057/*
6058 * Draw the verticap separator right of window "wp" starting with line "row".
6059 */
6060 static void
6061draw_vsep_win(wp, row)
6062 win_T *wp;
6063 int row;
6064{
6065 int hl;
6066 int c;
6067
6068 if (wp->w_vsep_width)
6069 {
6070 /* draw the vertical separator right of this window */
6071 c = fillchar_vsep(&hl);
6072 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6073 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6074 c, ' ', hl);
6075 }
6076}
6077#endif
6078
6079#ifdef FEAT_WILDMENU
6080static int status_match_len __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006081static int skip_status_match_char __ARGS((expand_T *xp, char_u *s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
6083/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006084 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
6086 static int
6087status_match_len(xp, s)
6088 expand_T *xp;
6089 char_u *s;
6090{
6091 int len = 0;
6092
6093#ifdef FEAT_MENU
6094 int emenu = (xp->xp_context == EXPAND_MENUS
6095 || xp->xp_context == EXPAND_MENUNAMES);
6096
6097 /* Check for menu separators - replace with '|'. */
6098 if (emenu && menu_is_separator(s))
6099 return 1;
6100#endif
6101
6102 while (*s != NUL)
6103 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006104 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006105 len += ptr2cells(s);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006106 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
6108
6109 return len;
6110}
6111
6112/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006113 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006114 * These are backslashes used for escaping. Do show backslashes in help tags.
6115 */
6116 static int
6117skip_status_match_char(xp, s)
6118 expand_T *xp;
6119 char_u *s;
6120{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006121 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006122#ifdef FEAT_MENU
6123 || ((xp->xp_context == EXPAND_MENUS
6124 || xp->xp_context == EXPAND_MENUNAMES)
6125 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6126#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006127 )
6128 {
6129#ifndef BACKSLASH_IN_FILENAME
6130 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6131 return 2;
6132#endif
6133 return 1;
6134 }
6135 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006136}
6137
6138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 * Show wildchar matches in the status line.
6140 * Show at least the "match" item.
6141 * We start at item 'first_match' in the list and show all matches that fit.
6142 *
6143 * If inversion is possible we use it. Else '=' characters are used.
6144 */
6145 void
6146win_redr_status_matches(xp, num_matches, matches, match, showtail)
6147 expand_T *xp;
6148 int num_matches;
6149 char_u **matches; /* list of matches */
6150 int match;
6151 int showtail;
6152{
6153#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6154 int row;
6155 char_u *buf;
6156 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006157 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 int fillchar;
6159 int attr;
6160 int i;
6161 int highlight = TRUE;
6162 char_u *selstart = NULL;
6163 int selstart_col = 0;
6164 char_u *selend = NULL;
6165 static int first_match = 0;
6166 int add_left = FALSE;
6167 char_u *s;
6168#ifdef FEAT_MENU
6169 int emenu;
6170#endif
6171#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6172 int l;
6173#endif
6174
6175 if (matches == NULL) /* interrupted completion? */
6176 return;
6177
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006178#ifdef FEAT_MBYTE
6179 if (has_mbyte)
6180 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6181 else
6182#endif
6183 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (buf == NULL)
6185 return;
6186
6187 if (match == -1) /* don't show match but original text */
6188 {
6189 match = 0;
6190 highlight = FALSE;
6191 }
6192 /* count 1 for the ending ">" */
6193 clen = status_match_len(xp, L_MATCH(match)) + 3;
6194 if (match == 0)
6195 first_match = 0;
6196 else if (match < first_match)
6197 {
6198 /* jumping left, as far as we can go */
6199 first_match = match;
6200 add_left = TRUE;
6201 }
6202 else
6203 {
6204 /* check if match fits on the screen */
6205 for (i = first_match; i < match; ++i)
6206 clen += status_match_len(xp, L_MATCH(i)) + 2;
6207 if (first_match > 0)
6208 clen += 2;
6209 /* jumping right, put match at the left */
6210 if ((long)clen > Columns)
6211 {
6212 first_match = match;
6213 /* if showing the last match, we can add some on the left */
6214 clen = 2;
6215 for (i = match; i < num_matches; ++i)
6216 {
6217 clen += status_match_len(xp, L_MATCH(i)) + 2;
6218 if ((long)clen >= Columns)
6219 break;
6220 }
6221 if (i == num_matches)
6222 add_left = TRUE;
6223 }
6224 }
6225 if (add_left)
6226 while (first_match > 0)
6227 {
6228 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6229 if ((long)clen >= Columns)
6230 break;
6231 --first_match;
6232 }
6233
6234 fillchar = fillchar_status(&attr, TRUE);
6235
6236 if (first_match == 0)
6237 {
6238 *buf = NUL;
6239 len = 0;
6240 }
6241 else
6242 {
6243 STRCPY(buf, "< ");
6244 len = 2;
6245 }
6246 clen = len;
6247
6248 i = first_match;
6249 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6250 {
6251 if (i == match)
6252 {
6253 selstart = buf + len;
6254 selstart_col = clen;
6255 }
6256
6257 s = L_MATCH(i);
6258 /* Check for menu separators - replace with '|' */
6259#ifdef FEAT_MENU
6260 emenu = (xp->xp_context == EXPAND_MENUS
6261 || xp->xp_context == EXPAND_MENUNAMES);
6262 if (emenu && menu_is_separator(s))
6263 {
6264 STRCPY(buf + len, transchar('|'));
6265 l = (int)STRLEN(buf + len);
6266 len += l;
6267 clen += l;
6268 }
6269 else
6270#endif
6271 for ( ; *s != NUL; ++s)
6272 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006273 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 clen += ptr2cells(s);
6275#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006276 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
6278 STRNCPY(buf + len, s, l);
6279 s += l - 1;
6280 len += l;
6281 }
6282 else
6283#endif
6284 {
6285 STRCPY(buf + len, transchar_byte(*s));
6286 len += (int)STRLEN(buf + len);
6287 }
6288 }
6289 if (i == match)
6290 selend = buf + len;
6291
6292 *(buf + len++) = ' ';
6293 *(buf + len++) = ' ';
6294 clen += 2;
6295 if (++i == num_matches)
6296 break;
6297 }
6298
6299 if (i != num_matches)
6300 {
6301 *(buf + len++) = '>';
6302 ++clen;
6303 }
6304
6305 buf[len] = NUL;
6306
6307 row = cmdline_row - 1;
6308 if (row >= 0)
6309 {
6310 if (wild_menu_showing == 0)
6311 {
6312 if (msg_scrolled > 0)
6313 {
6314 /* Put the wildmenu just above the command line. If there is
6315 * no room, scroll the screen one line up. */
6316 if (cmdline_row == Rows - 1)
6317 {
6318 screen_del_lines(0, 0, 1, (int)Rows, TRUE, NULL);
6319 ++msg_scrolled;
6320 }
6321 else
6322 {
6323 ++cmdline_row;
6324 ++row;
6325 }
6326 wild_menu_showing = WM_SCROLLED;
6327 }
6328 else
6329 {
6330 /* Create status line if needed by setting 'laststatus' to 2.
6331 * Set 'winminheight' to zero to avoid that the window is
6332 * resized. */
6333 if (lastwin->w_status_height == 0)
6334 {
6335 save_p_ls = p_ls;
6336 save_p_wmh = p_wmh;
6337 p_ls = 2;
6338 p_wmh = 0;
6339 last_status(FALSE);
6340 }
6341 wild_menu_showing = WM_SHOWN;
6342 }
6343 }
6344
6345 screen_puts(buf, row, 0, attr);
6346 if (selstart != NULL && highlight)
6347 {
6348 *selend = NUL;
6349 screen_puts(selstart, row, selstart_col, hl_attr(HLF_WM));
6350 }
6351
6352 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6353 }
6354
6355#ifdef FEAT_VERTSPLIT
6356 win_redraw_last_status(topframe);
6357#else
6358 lastwin->w_redr_status = TRUE;
6359#endif
6360 vim_free(buf);
6361}
6362#endif
6363
6364#if defined(FEAT_WINDOWS) || defined(PROTO)
6365/*
6366 * Redraw the status line of window wp.
6367 *
6368 * If inversion is possible we use it. Else '=' characters are used.
6369 */
6370 void
6371win_redr_status(wp)
6372 win_T *wp;
6373{
6374 int row;
6375 char_u *p;
6376 int len;
6377 int fillchar;
6378 int attr;
6379 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006380 static int busy = FALSE;
6381
6382 /* It's possible to get here recursively when 'statusline' (indirectly)
6383 * invokes ":redrawstatus". Simply ignore the call then. */
6384 if (busy)
6385 return;
6386 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 wp->w_redr_status = FALSE;
6389 if (wp->w_status_height == 0)
6390 {
6391 /* no status line, can only be last window */
6392 redraw_cmdline = TRUE;
6393 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006394 else if (!redrawing()
6395#ifdef FEAT_INS_EXPAND
6396 /* don't update status line when popup menu is visible and may be
6397 * drawn over it */
6398 || pum_visible()
6399#endif
6400 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 /* Don't redraw right now, do it later. */
6403 wp->w_redr_status = TRUE;
6404 }
6405#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006406 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006409 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 }
6411#endif
6412 else
6413 {
6414 fillchar = fillchar_status(&attr, wp == curwin);
6415
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006416 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 p = NameBuff;
6418 len = (int)STRLEN(p);
6419
6420 if (wp->w_buffer->b_help
6421#ifdef FEAT_QUICKFIX
6422 || wp->w_p_pvw
6423#endif
6424 || bufIsChanged(wp->w_buffer)
6425 || wp->w_buffer->b_p_ro)
6426 *(p + len++) = ' ';
6427 if (wp->w_buffer->b_help)
6428 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006429 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 len += (int)STRLEN(p + len);
6431 }
6432#ifdef FEAT_QUICKFIX
6433 if (wp->w_p_pvw)
6434 {
6435 STRCPY(p + len, _("[Preview]"));
6436 len += (int)STRLEN(p + len);
6437 }
6438#endif
6439 if (bufIsChanged(wp->w_buffer))
6440 {
6441 STRCPY(p + len, "[+]");
6442 len += 3;
6443 }
6444 if (wp->w_buffer->b_p_ro)
6445 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006446 STRCPY(p + len, _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 len += 4;
6448 }
6449
6450#ifndef FEAT_VERTSPLIT
6451 this_ru_col = ru_col;
6452 if (this_ru_col < (Columns + 1) / 2)
6453 this_ru_col = (Columns + 1) / 2;
6454#else
6455 this_ru_col = ru_col - (Columns - W_WIDTH(wp));
6456 if (this_ru_col < (W_WIDTH(wp) + 1) / 2)
6457 this_ru_col = (W_WIDTH(wp) + 1) / 2;
6458 if (this_ru_col <= 1)
6459 {
6460 p = (char_u *)"<"; /* No room for file name! */
6461 len = 1;
6462 }
6463 else
6464#endif
6465#ifdef FEAT_MBYTE
6466 if (has_mbyte)
6467 {
6468 int clen = 0, i;
6469
6470 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006471 clen = mb_string2cells(p, -1);
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 /* Find first character that will fit.
6474 * Going from start to end is much faster for DBCS. */
6475 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006476 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 clen -= (*mb_ptr2cells)(p + i);
6478 len = clen;
6479 if (i > 0)
6480 {
6481 p = p + i - 1;
6482 *p = '<';
6483 ++len;
6484 }
6485
6486 }
6487 else
6488#endif
6489 if (len > this_ru_col - 1)
6490 {
6491 p += len - (this_ru_col - 1);
6492 *p = '<';
6493 len = this_ru_col - 1;
6494 }
6495
6496 row = W_WINROW(wp) + wp->w_height;
6497 screen_puts(p, row, W_WINCOL(wp), attr);
6498 screen_fill(row, row + 1, len + W_WINCOL(wp),
6499 this_ru_col + W_WINCOL(wp), fillchar, fillchar, attr);
6500
6501 if (get_keymap_str(wp, NameBuff, MAXPATHL)
6502 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6503 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
6504 - 1 + W_WINCOL(wp)), attr);
6505
6506#ifdef FEAT_CMDL_INFO
6507 win_redr_ruler(wp, TRUE);
6508#endif
6509 }
6510
6511#ifdef FEAT_VERTSPLIT
6512 /*
6513 * May need to draw the character below the vertical separator.
6514 */
6515 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6516 {
6517 if (stl_connected(wp))
6518 fillchar = fillchar_status(&attr, wp == curwin);
6519 else
6520 fillchar = fillchar_vsep(&attr);
6521 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6522 attr);
6523 }
6524#endif
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006525 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
Bram Moolenaar238a5642006-02-21 22:12:05 +00006528#ifdef FEAT_STL_OPT
6529/*
6530 * Redraw the status line according to 'statusline' and take care of any
6531 * errors encountered.
6532 */
6533 static void
Bram Moolenaar362f3562009-11-03 16:20:34 +00006534redraw_custom_statusline(wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006535 win_T *wp;
6536{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006537 static int entered = FALSE;
6538 int save_called_emsg = called_emsg;
6539
6540 /* When called recursively return. This can happen when the statusline
6541 * contains an expression that triggers a redraw. */
6542 if (entered)
6543 return;
6544 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006545
6546 called_emsg = FALSE;
6547 win_redr_custom(wp, FALSE);
6548 if (called_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006549 {
6550 /* When there is an error disable the statusline, otherwise the
6551 * display is messed up with errors and a redraw triggers the problem
6552 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00006553 set_string_option_direct((char_u *)"statusline", -1,
6554 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006555 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006556 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00006557 called_emsg |= save_called_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00006558 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00006559}
6560#endif
6561
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562# ifdef FEAT_VERTSPLIT
6563/*
6564 * Return TRUE if the status line of window "wp" is connected to the status
6565 * line of the window right of it. If not, then it's a vertical separator.
6566 * Only call if (wp->w_vsep_width != 0).
6567 */
6568 int
6569stl_connected(wp)
6570 win_T *wp;
6571{
6572 frame_T *fr;
6573
6574 fr = wp->w_frame;
6575 while (fr->fr_parent != NULL)
6576 {
6577 if (fr->fr_parent->fr_layout == FR_COL)
6578 {
6579 if (fr->fr_next != NULL)
6580 break;
6581 }
6582 else
6583 {
6584 if (fr->fr_next != NULL)
6585 return TRUE;
6586 }
6587 fr = fr->fr_parent;
6588 }
6589 return FALSE;
6590}
6591# endif
6592
6593#endif /* FEAT_WINDOWS */
6594
6595#if defined(FEAT_WINDOWS) || defined(FEAT_STL_OPT) || defined(PROTO)
6596/*
6597 * Get the value to show for the language mappings, active 'keymap'.
6598 */
6599 int
6600get_keymap_str(wp, buf, len)
6601 win_T *wp;
6602 char_u *buf; /* buffer for the result */
6603 int len; /* length of buffer */
6604{
6605 char_u *p;
6606
6607 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
6608 return FALSE;
6609
6610 {
6611#ifdef FEAT_EVAL
6612 buf_T *old_curbuf = curbuf;
6613 win_T *old_curwin = curwin;
6614 char_u *s;
6615
6616 curbuf = wp->w_buffer;
6617 curwin = wp;
6618 STRCPY(buf, "b:keymap_name"); /* must be writable */
6619 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006620 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 --emsg_skip;
6622 curbuf = old_curbuf;
6623 curwin = old_curwin;
6624 if (p == NULL || *p == NUL)
6625#endif
6626 {
6627#ifdef FEAT_KEYMAP
6628 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
6629 p = wp->w_buffer->b_p_keymap;
6630 else
6631#endif
6632 p = (char_u *)"lang";
6633 }
6634 if ((int)(STRLEN(p) + 3) < len)
6635 sprintf((char *)buf, "<%s>", p);
6636 else
6637 buf[0] = NUL;
6638#ifdef FEAT_EVAL
6639 vim_free(s);
6640#endif
6641 }
6642 return buf[0] != NUL;
6643}
6644#endif
6645
6646#if defined(FEAT_STL_OPT) || defined(PROTO)
6647/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006648 * Redraw the status line or ruler of window "wp".
6649 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 */
6651 static void
Bram Moolenaar9372a112005-12-06 19:59:18 +00006652win_redr_custom(wp, draw_ruler)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 win_T *wp;
Bram Moolenaar9372a112005-12-06 19:59:18 +00006654 int draw_ruler; /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaar1d633412013-12-11 15:52:01 +01006656 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 int attr;
6658 int curattr;
6659 int row;
6660 int col = 0;
6661 int maxwidth;
6662 int width;
6663 int n;
6664 int len;
6665 int fillchar;
6666 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00006667 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006669 struct stl_hlrec hltab[STL_MAX_ITEM];
6670 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006671 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01006672 win_T *ewp;
6673 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
Bram Moolenaar1d633412013-12-11 15:52:01 +01006675 /* There is a tiny chance that this gets called recursively: When
6676 * redrawing a status line triggers redrawing the ruler or tabline.
6677 * Avoid trouble by not allowing recursion. */
6678 if (entered)
6679 return;
6680 entered = TRUE;
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006683 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006685 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006686 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006687 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00006688 fillchar = ' ';
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006689 attr = hl_attr(HLF_TPF);
6690 maxwidth = Columns;
6691# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006692 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006695 else
6696 {
6697 row = W_WINROW(wp) + wp->w_height;
6698 fillchar = fillchar_status(&attr, wp == curwin);
6699 maxwidth = W_WIDTH(wp);
6700
6701 if (draw_ruler)
6702 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006703 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006704 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006705 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006706 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00006707 if (*++stl == '-')
6708 stl++;
6709 if (atoi((char *)stl))
6710 while (VIM_ISDIGIT(*stl))
6711 stl++;
6712 if (*stl++ != '(')
6713 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006714 }
6715#ifdef FEAT_VERTSPLIT
6716 col = ru_col - (Columns - W_WIDTH(wp));
6717 if (col < (W_WIDTH(wp) + 1) / 2)
6718 col = (W_WIDTH(wp) + 1) / 2;
6719#else
6720 col = ru_col;
6721 if (col > (Columns + 1) / 2)
6722 col = (Columns + 1) / 2;
6723#endif
6724 maxwidth = W_WIDTH(wp) - col;
6725#ifdef FEAT_WINDOWS
6726 if (!wp->w_status_height)
6727#endif
6728 {
6729 row = Rows - 1;
6730 --maxwidth; /* writing in last column may cause scrolling */
6731 fillchar = ' ';
6732 attr = 0;
6733 }
6734
6735# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006736 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006737# endif
6738 }
6739 else
6740 {
6741 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00006742 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006743 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00006744 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006745# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006746 use_sandbox = was_set_insecurely((char_u *)"statusline",
6747 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006748# endif
6749 }
6750
6751#ifdef FEAT_VERTSPLIT
6752 col += W_WINCOL(wp);
6753#endif
6754 }
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01006757 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
Bram Moolenaar61452852011-02-01 18:01:11 +01006759 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
6760 * the cursor away and back. */
6761 ewp = wp == NULL ? curwin : wp;
6762 p_crb_save = ewp->w_p_crb;
6763 ewp->w_p_crb = FALSE;
6764
Bram Moolenaar362f3562009-11-03 16:20:34 +00006765 /* Make a copy, because the statusline may include a function call that
6766 * might change the option value and free the memory. */
6767 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006768 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00006769 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006770 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00006771 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01006772 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01006774 /* Make all characters printable. */
6775 p = transstr(buf);
6776 if (p != NULL)
6777 {
6778 vim_strncpy(buf, p, sizeof(buf) - 1);
6779 vim_free(p);
6780 }
6781
6782 /* fill up with "fillchar" */
6783 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006784 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {
6786#ifdef FEAT_MBYTE
6787 len += (*mb_char2bytes)(fillchar, buf + len);
6788#else
6789 buf[len++] = fillchar;
6790#endif
6791 ++width;
6792 }
6793 buf[len] = NUL;
6794
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006795 /*
6796 * Draw each snippet with the specified highlighting.
6797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 curattr = attr;
6799 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006800 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006802 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 screen_puts_len(p, len, row, col, curattr);
6804 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006805 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006807 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006809 else if (hltab[n].userhl < 0)
6810 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811#ifdef FEAT_WINDOWS
Bram Moolenaar238a5642006-02-21 22:12:05 +00006812 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006813 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814#endif
6815 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006816 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
6818 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006819
6820 if (wp == NULL)
6821 {
6822 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
6823 col = 0;
6824 len = 0;
6825 p = buf;
6826 fillchar = 0;
6827 for (n = 0; tabtab[n].start != NULL; n++)
6828 {
6829 len += vim_strnsize(p, (int)(tabtab[n].start - p));
6830 while (col < len)
6831 TabPageIdxs[col++] = fillchar;
6832 p = tabtab[n].start;
6833 fillchar = tabtab[n].userhl;
6834 }
6835 while (col < Columns)
6836 TabPageIdxs[col++] = fillchar;
6837 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01006838
6839theend:
6840 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841}
6842
6843#endif /* FEAT_STL_OPT */
6844
6845/*
6846 * Output a single character directly to the screen and update ScreenLines.
6847 */
6848 void
6849screen_putchar(c, row, col, attr)
6850 int c;
6851 int row, col;
6852 int attr;
6853{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 char_u buf[MB_MAXBYTES + 1];
6855
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006856#ifdef FEAT_MBYTE
6857 if (has_mbyte)
6858 buf[(*mb_char2bytes)(c, buf)] = NUL;
6859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006861 {
6862 buf[0] = c;
6863 buf[1] = NUL;
6864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 screen_puts(buf, row, col, attr);
6866}
6867
6868/*
6869 * Get a single character directly from ScreenLines into "bytes[]".
6870 * Also return its attribute in *attrp;
6871 */
6872 void
6873screen_getbytes(row, col, bytes, attrp)
6874 int row, col;
6875 char_u *bytes;
6876 int *attrp;
6877{
6878 unsigned off;
6879
6880 /* safety check */
6881 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
6882 {
6883 off = LineOffset[row] + col;
6884 *attrp = ScreenAttrs[off];
6885 bytes[0] = ScreenLines[off];
6886 bytes[1] = NUL;
6887
6888#ifdef FEAT_MBYTE
6889 if (enc_utf8 && ScreenLinesUC[off] != 0)
6890 bytes[utfc_char2bytes(off, bytes)] = NUL;
6891 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
6892 {
6893 bytes[0] = ScreenLines[off];
6894 bytes[1] = ScreenLines2[off];
6895 bytes[2] = NUL;
6896 }
6897 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
6898 {
6899 bytes[1] = ScreenLines[off + 1];
6900 bytes[2] = NUL;
6901 }
6902#endif
6903 }
6904}
6905
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006906#ifdef FEAT_MBYTE
6907static int screen_comp_differs __ARGS((int, int*));
6908
6909/*
6910 * Return TRUE if composing characters for screen posn "off" differs from
6911 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006912 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006913 */
6914 static int
6915screen_comp_differs(off, u8cc)
6916 int off;
6917 int *u8cc;
6918{
6919 int i;
6920
6921 for (i = 0; i < Screen_mco; ++i)
6922 {
6923 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
6924 return TRUE;
6925 if (u8cc[i] == 0)
6926 break;
6927 }
6928 return FALSE;
6929}
6930#endif
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * Put string '*text' on the screen at position 'row' and 'col', with
6934 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
6935 * Note: only outputs within one row, message is truncated at screen boundary!
6936 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
6937 */
6938 void
6939screen_puts(text, row, col, attr)
6940 char_u *text;
6941 int row;
6942 int col;
6943 int attr;
6944{
6945 screen_puts_len(text, -1, row, col, attr);
6946}
6947
6948/*
6949 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
6950 * a NUL.
6951 */
6952 void
6953screen_puts_len(text, len, row, col, attr)
6954 char_u *text;
6955 int len;
6956 int row;
6957 int col;
6958 int attr;
6959{
6960 unsigned off;
6961 char_u *ptr = text;
6962 int c;
6963#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00006964 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 int mbyte_blen = 1;
6966 int mbyte_cells = 1;
6967 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006968 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 int clear_next_cell = FALSE;
6970# ifdef FEAT_ARABIC
6971 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006972 int pc, nc, nc1;
6973 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# endif
6975#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006976#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
6977 int force_redraw_this;
6978 int force_redraw_next = FALSE;
6979#endif
6980 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
6983 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006984 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
Bram Moolenaarc236c162008-07-13 17:41:49 +00006986#ifdef FEAT_MBYTE
6987 /* When drawing over the right halve of a double-wide char clear out the
6988 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006989 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00006990# ifdef FEAT_GUI
6991 && !gui.in_use
6992# endif
6993 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006994 {
6995 ScreenLines[off - 1] = ' ';
6996 ScreenAttrs[off - 1] = 0;
6997 if (enc_utf8)
6998 {
6999 ScreenLinesUC[off - 1] = 0;
7000 ScreenLinesC[0][off - 1] = 0;
7001 }
7002 /* redraw the previous cell, make it empty */
7003 screen_char(off - 1, row, col - 1);
7004 /* force the cell at "col" to be redrawn */
7005 force_redraw_next = TRUE;
7006 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007007#endif
7008
Bram Moolenaar367329b2007-08-30 11:53:22 +00007009#ifdef FEAT_MBYTE
7010 max_off = LineOffset[row] + screen_Columns;
7011#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007012 while (col < screen_Columns
7013 && (len < 0 || (int)(ptr - text) < len)
7014 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
7016 c = *ptr;
7017#ifdef FEAT_MBYTE
7018 /* check if this is the first byte of a multibyte */
7019 if (has_mbyte)
7020 {
7021 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007022 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007024 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7026 mbyte_cells = 1;
7027 else if (enc_dbcs != 0)
7028 mbyte_cells = mbyte_blen;
7029 else /* enc_utf8 */
7030 {
7031 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007032 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 (int)((text + len) - ptr));
7034 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007035 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007037# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 /* Non-BMP character: display as ? or fullwidth ?. */
7039 if (u8c >= 0x10000)
7040 {
7041 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7042 if (attr == 0)
7043 attr = hl_attr(HLF_8);
7044 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046# ifdef FEAT_ARABIC
7047 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7048 {
7049 /* Do Arabic shaping. */
7050 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7051 {
7052 /* Past end of string to be displayed. */
7053 nc = NUL;
7054 nc1 = NUL;
7055 }
7056 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007057 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007058 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7059 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007060 nc1 = pcc[0];
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 pc = prev_c;
7063 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007064 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066 else
7067 prev_c = u8c;
7068# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007069 if (col + mbyte_cells > screen_Columns)
7070 {
7071 /* Only 1 cell left, but character requires 2 cells:
7072 * display a '>' in the last column to avoid wrapping. */
7073 c = '>';
7074 mbyte_cells = 1;
7075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 }
7077 }
7078#endif
7079
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007080#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7081 force_redraw_this = force_redraw_next;
7082 force_redraw_next = FALSE;
7083#endif
7084
7085 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086#ifdef FEAT_MBYTE
7087 || (mbyte_cells == 2
7088 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7089 || (enc_dbcs == DBCS_JPNU
7090 && c == 0x8e
7091 && ScreenLines2[off] != ptr[1])
7092 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007093 && (ScreenLinesUC[off] !=
7094 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7095 || (ScreenLinesUC[off] != 0
7096 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097#endif
7098 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007099 || exmode_active;
7100
7101 if (need_redraw
7102#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7103 || force_redraw_this
7104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 )
7106 {
7107#if defined(FEAT_GUI) || defined(UNIX)
7108 /* The bold trick makes a single row of pixels appear in the next
7109 * character. When a bold character is removed, the next
7110 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007111 * and for some xterms. */
7112 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113# ifdef FEAT_GUI
7114 gui.in_use
7115# endif
7116# if defined(FEAT_GUI) && defined(UNIX)
7117 ||
7118# endif
7119# ifdef UNIX
7120 term_is_xterm
7121# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007122 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007124 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007126 if (n > HL_ALL)
7127 n = syn_attr2attr(n);
7128 if (n & HL_BOLD)
7129 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
7131#endif
7132#ifdef FEAT_MBYTE
7133 /* When at the end of the text and overwriting a two-cell
7134 * character with a one-cell character, need to clear the next
7135 * cell. Also when overwriting the left halve of a two-cell char
7136 * with the right halve of a two-cell char. Do this only once
7137 * (mb_off2cells() may return 2 on the right halve). */
7138 if (clear_next_cell)
7139 clear_next_cell = FALSE;
7140 else if (has_mbyte
7141 && (len < 0 ? ptr[mbyte_blen] == NUL
7142 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007143 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007145 && (*mb_off2cells)(off, max_off) == 1
7146 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 clear_next_cell = TRUE;
7148
7149 /* Make sure we never leave a second byte of a double-byte behind,
7150 * it confuses mb_off2cells(). */
7151 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007152 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007154 && (*mb_off2cells)(off, max_off) == 1
7155 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 ScreenLines[off + mbyte_blen] = 0;
7157#endif
7158 ScreenLines[off] = c;
7159 ScreenAttrs[off] = attr;
7160#ifdef FEAT_MBYTE
7161 if (enc_utf8)
7162 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007163 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 ScreenLinesUC[off] = 0;
7165 else
7166 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007167 int i;
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007170 for (i = 0; i < Screen_mco; ++i)
7171 {
7172 ScreenLinesC[i][off] = u8cc[i];
7173 if (u8cc[i] == 0)
7174 break;
7175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
7177 if (mbyte_cells == 2)
7178 {
7179 ScreenLines[off + 1] = 0;
7180 ScreenAttrs[off + 1] = attr;
7181 }
7182 screen_char(off, row, col);
7183 }
7184 else if (mbyte_cells == 2)
7185 {
7186 ScreenLines[off + 1] = ptr[1];
7187 ScreenAttrs[off + 1] = attr;
7188 screen_char_2(off, row, col);
7189 }
7190 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7191 {
7192 ScreenLines2[off] = ptr[1];
7193 screen_char(off, row, col);
7194 }
7195 else
7196#endif
7197 screen_char(off, row, col);
7198 }
7199#ifdef FEAT_MBYTE
7200 if (has_mbyte)
7201 {
7202 off += mbyte_cells;
7203 col += mbyte_cells;
7204 ptr += mbyte_blen;
7205 if (clear_next_cell)
7206 ptr = (char_u *)" ";
7207 }
7208 else
7209#endif
7210 {
7211 ++off;
7212 ++col;
7213 ++ptr;
7214 }
7215 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007216
7217#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7218 /* If we detected the next character needs to be redrawn, but the text
7219 * doesn't extend up to there, update the character here. */
7220 if (force_redraw_next && col < screen_Columns)
7221 {
7222# ifdef FEAT_MBYTE
7223 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7224 screen_char_2(off, row, col);
7225 else
7226# endif
7227 screen_char(off, row, col);
7228 }
7229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230}
7231
7232#ifdef FEAT_SEARCH_EXTRA
7233/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007234 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 */
7236 static void
7237start_search_hl()
7238{
7239 if (p_hls && !no_hlsearch)
7240 {
7241 last_pat_prog(&search_hl.rm);
7242 search_hl.attr = hl_attr(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007243# ifdef FEAT_RELTIME
7244 /* Set the time limit to 'redrawtime'. */
7245 profile_setlimit(p_rdt, &search_hl.tm);
7246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 }
7248}
7249
7250/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007251 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 */
7253 static void
7254end_search_hl()
7255{
7256 if (search_hl.rm.regprog != NULL)
7257 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007258 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 search_hl.rm.regprog = NULL;
7260 }
7261}
7262
7263/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007264 * Init for calling prepare_search_hl().
7265 */
7266 static void
7267init_search_hl(wp)
7268 win_T *wp;
7269{
7270 matchitem_T *cur;
7271
7272 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7273 * match */
7274 cur = wp->w_match_head;
7275 while (cur != NULL)
7276 {
7277 cur->hl.rm = cur->match;
7278 if (cur->hlg_id == 0)
7279 cur->hl.attr = 0;
7280 else
7281 cur->hl.attr = syn_id2attr(cur->hlg_id);
7282 cur->hl.buf = wp->w_buffer;
7283 cur->hl.lnum = 0;
7284 cur->hl.first_lnum = 0;
7285# ifdef FEAT_RELTIME
7286 /* Set the time limit to 'redrawtime'. */
7287 profile_setlimit(p_rdt, &(cur->hl.tm));
7288# endif
7289 cur = cur->next;
7290 }
7291 search_hl.buf = wp->w_buffer;
7292 search_hl.lnum = 0;
7293 search_hl.first_lnum = 0;
7294 /* time limit is set at the toplevel, for all windows */
7295}
7296
7297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 * Advance to the match in window "wp" line "lnum" or past it.
7299 */
7300 static void
7301prepare_search_hl(wp, lnum)
7302 win_T *wp;
7303 linenr_T lnum;
7304{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007305 matchitem_T *cur; /* points to the match list */
7306 match_T *shl; /* points to search_hl or a match */
7307 int shl_flag; /* flag to indicate whether search_hl
7308 has been processed or not */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 int n;
7310
7311 /*
7312 * When using a multi-line pattern, start searching at the top
7313 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007314 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007316 cur = wp->w_match_head;
7317 shl_flag = FALSE;
7318 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007320 if (shl_flag == FALSE)
7321 {
7322 shl = &search_hl;
7323 shl_flag = TRUE;
7324 }
7325 else
7326 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if (shl->rm.regprog != NULL
7328 && shl->lnum == 0
7329 && re_multiline(shl->rm.regprog))
7330 {
7331 if (shl->first_lnum == 0)
7332 {
7333# ifdef FEAT_FOLDING
7334 for (shl->first_lnum = lnum;
7335 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7336 if (hasFoldingWin(wp, shl->first_lnum - 1,
7337 NULL, NULL, TRUE, NULL))
7338 break;
7339# else
7340 shl->first_lnum = wp->w_topline;
7341# endif
7342 }
7343 n = 0;
7344 while (shl->first_lnum < lnum && shl->rm.regprog != NULL)
7345 {
7346 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n);
7347 if (shl->lnum != 0)
7348 {
7349 shl->first_lnum = shl->lnum
7350 + shl->rm.endpos[0].lnum
7351 - shl->rm.startpos[0].lnum;
7352 n = shl->rm.endpos[0].col;
7353 }
7354 else
7355 {
7356 ++shl->first_lnum;
7357 n = 0;
7358 }
7359 }
7360 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007361 if (shl != &search_hl && cur != NULL)
7362 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
7364}
7365
7366/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007367 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 * Uses shl->buf.
7369 * Sets shl->lnum and shl->rm contents.
7370 * Note: Assumes a previous match is always before "lnum", unless
7371 * shl->lnum is zero.
7372 * Careful: Any pointers for buffer lines will become invalid.
7373 */
7374 static void
7375next_search_hl(win, shl, lnum, mincol)
7376 win_T *win;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007377 match_T *shl; /* points to search_hl or a match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 linenr_T lnum;
7379 colnr_T mincol; /* minimal column for a match */
7380{
7381 linenr_T l;
7382 colnr_T matchcol;
7383 long nmatched;
7384
7385 if (shl->lnum != 0)
7386 {
7387 /* Check for three situations:
7388 * 1. If the "lnum" is below a previous match, start a new search.
7389 * 2. If the previous match includes "mincol", use it.
7390 * 3. Continue after the previous match.
7391 */
7392 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7393 if (lnum > l)
7394 shl->lnum = 0;
7395 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7396 return;
7397 }
7398
7399 /*
7400 * Repeat searching for a match until one is found that includes "mincol"
7401 * or none is found in this line.
7402 */
7403 called_emsg = FALSE;
7404 for (;;)
7405 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007406#ifdef FEAT_RELTIME
7407 /* Stop searching after passing the time limit. */
7408 if (profile_passed_limit(&(shl->tm)))
7409 {
7410 shl->lnum = 0; /* no match found in time */
7411 break;
7412 }
7413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 /* Three situations:
7415 * 1. No useful previous match: search from start of line.
7416 * 2. Not Vi compatible or empty match: continue at next character.
7417 * Break the loop if this is beyond the end of the line.
7418 * 3. Vi compatible searching: continue at end of previous match.
7419 */
7420 if (shl->lnum == 0)
7421 matchcol = 0;
7422 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7423 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007424 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007426 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007427
7428 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007429 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007430 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007432 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 shl->lnum = 0;
7434 break;
7435 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007436#ifdef FEAT_MBYTE
7437 if (has_mbyte)
7438 matchcol += mb_ptr2len(ml);
7439 else
7440#endif
7441 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443 else
7444 matchcol = shl->rm.endpos[0].col;
7445
7446 shl->lnum = lnum;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007447 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum, matchcol,
7448#ifdef FEAT_RELTIME
7449 &(shl->tm)
7450#else
7451 NULL
7452#endif
7453 );
Bram Moolenaarc7040a52010-07-20 13:11:28 +02007454 if (called_emsg || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 {
7456 /* Error while handling regexp: stop using this regexp. */
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007457 if (shl == &search_hl)
7458 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007459 /* don't free regprog in the match list, it's a copy */
Bram Moolenaar473de612013-06-08 18:19:48 +02007460 vim_regfree(shl->rm.regprog);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007461 SET_NO_HLSEARCH(TRUE);
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 shl->rm.regprog = NULL;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007464 shl->lnum = 0;
7465 got_int = FALSE; /* avoid the "Type :quit to exit Vim" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 break;
7467 }
7468 if (nmatched == 0)
7469 {
7470 shl->lnum = 0; /* no match found */
7471 break;
7472 }
7473 if (shl->rm.startpos[0].lnum > 0
7474 || shl->rm.startpos[0].col >= mincol
7475 || nmatched > 1
7476 || shl->rm.endpos[0].col > mincol)
7477 {
7478 shl->lnum += shl->rm.startpos[0].lnum;
7479 break; /* useful match found */
7480 }
7481 }
7482}
7483#endif
7484
7485 static void
7486screen_start_highlight(attr)
7487 int attr;
7488{
7489 attrentry_T *aep = NULL;
7490
7491 screen_attr = attr;
7492 if (full_screen
7493#ifdef WIN3264
7494 && termcap_active
7495#endif
7496 )
7497 {
7498#ifdef FEAT_GUI
7499 if (gui.in_use)
7500 {
7501 char buf[20];
7502
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007503 /* The GUI handles this internally. */
7504 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 OUT_STR(buf);
7506 }
7507 else
7508#endif
7509 {
7510 if (attr > HL_ALL) /* special HL attr. */
7511 {
7512 if (t_colors > 1)
7513 aep = syn_cterm_attr2entry(attr);
7514 else
7515 aep = syn_term_attr2entry(attr);
7516 if (aep == NULL) /* did ":syntax clear" */
7517 attr = 0;
7518 else
7519 attr = aep->ae_attr;
7520 }
7521 if ((attr & HL_BOLD) && T_MD != NULL) /* bold */
7522 out_str(T_MD);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007523 else if (aep != NULL && t_colors > 1 && aep->ae_u.cterm.fg_color
7524 && cterm_normal_fg_bold)
7525 /* If the Normal FG color has BOLD attribute and the new HL
7526 * has a FG color defined, clear BOLD. */
7527 out_str(T_ME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 if ((attr & HL_STANDOUT) && T_SO != NULL) /* standout */
7529 out_str(T_SO);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007530 if ((attr & (HL_UNDERLINE | HL_UNDERCURL)) && T_US != NULL)
7531 /* underline or undercurl */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 out_str(T_US);
7533 if ((attr & HL_ITALIC) && T_CZH != NULL) /* italic */
7534 out_str(T_CZH);
7535 if ((attr & HL_INVERSE) && T_MR != NULL) /* inverse (reverse) */
7536 out_str(T_MR);
7537
7538 /*
7539 * Output the color or start string after bold etc., in case the
7540 * bold etc. override the color setting.
7541 */
7542 if (aep != NULL)
7543 {
7544 if (t_colors > 1)
7545 {
7546 if (aep->ae_u.cterm.fg_color)
7547 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7548 if (aep->ae_u.cterm.bg_color)
7549 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7550 }
7551 else
7552 {
7553 if (aep->ae_u.term.start != NULL)
7554 out_str(aep->ae_u.term.start);
7555 }
7556 }
7557 }
7558 }
7559}
7560
7561 void
7562screen_stop_highlight()
7563{
7564 int do_ME = FALSE; /* output T_ME code */
7565
7566 if (screen_attr != 0
7567#ifdef WIN3264
7568 && termcap_active
7569#endif
7570 )
7571 {
7572#ifdef FEAT_GUI
7573 if (gui.in_use)
7574 {
7575 char buf[20];
7576
7577 /* use internal GUI code */
7578 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7579 OUT_STR(buf);
7580 }
7581 else
7582#endif
7583 {
7584 if (screen_attr > HL_ALL) /* special HL attr. */
7585 {
7586 attrentry_T *aep;
7587
7588 if (t_colors > 1)
7589 {
7590 /*
7591 * Assume that t_me restores the original colors!
7592 */
7593 aep = syn_cterm_attr2entry(screen_attr);
7594 if (aep != NULL && (aep->ae_u.cterm.fg_color
7595 || aep->ae_u.cterm.bg_color))
7596 do_ME = TRUE;
7597 }
7598 else
7599 {
7600 aep = syn_term_attr2entry(screen_attr);
7601 if (aep != NULL && aep->ae_u.term.stop != NULL)
7602 {
7603 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7604 do_ME = TRUE;
7605 else
7606 out_str(aep->ae_u.term.stop);
7607 }
7608 }
7609 if (aep == NULL) /* did ":syntax clear" */
7610 screen_attr = 0;
7611 else
7612 screen_attr = aep->ae_attr;
7613 }
7614
7615 /*
7616 * Often all ending-codes are equal to T_ME. Avoid outputting the
7617 * same sequence several times.
7618 */
7619 if (screen_attr & HL_STANDOUT)
7620 {
7621 if (STRCMP(T_SE, T_ME) == 0)
7622 do_ME = TRUE;
7623 else
7624 out_str(T_SE);
7625 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00007626 if (screen_attr & (HL_UNDERLINE | HL_UNDERCURL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
7628 if (STRCMP(T_UE, T_ME) == 0)
7629 do_ME = TRUE;
7630 else
7631 out_str(T_UE);
7632 }
7633 if (screen_attr & HL_ITALIC)
7634 {
7635 if (STRCMP(T_CZR, T_ME) == 0)
7636 do_ME = TRUE;
7637 else
7638 out_str(T_CZR);
7639 }
7640 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7641 out_str(T_ME);
7642
7643 if (t_colors > 1)
7644 {
7645 /* set Normal cterm colors */
7646 if (cterm_normal_fg_color != 0)
7647 term_fg_color(cterm_normal_fg_color - 1);
7648 if (cterm_normal_bg_color != 0)
7649 term_bg_color(cterm_normal_bg_color - 1);
7650 if (cterm_normal_fg_bold)
7651 out_str(T_MD);
7652 }
7653 }
7654 }
7655 screen_attr = 0;
7656}
7657
7658/*
7659 * Reset the colors for a cterm. Used when leaving Vim.
7660 * The machine specific code may override this again.
7661 */
7662 void
7663reset_cterm_colors()
7664{
7665 if (t_colors > 1)
7666 {
7667 /* set Normal cterm colors */
7668 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
7669 {
7670 out_str(T_OP);
7671 screen_attr = -1;
7672 }
7673 if (cterm_normal_fg_bold)
7674 {
7675 out_str(T_ME);
7676 screen_attr = -1;
7677 }
7678 }
7679}
7680
7681/*
7682 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7683 * using the attributes from ScreenAttrs["off"].
7684 */
7685 static void
7686screen_char(off, row, col)
7687 unsigned off;
7688 int row;
7689 int col;
7690{
7691 int attr;
7692
7693 /* Check for illegal values, just in case (could happen just after
7694 * resizing). */
7695 if (row >= screen_Rows || col >= screen_Columns)
7696 return;
7697
7698 /* Outputting the last character on the screen may scrollup the screen.
7699 * Don't to it! Mark the character invalid (update it when scrolled up) */
7700 if (row == screen_Rows - 1 && col == screen_Columns - 1
7701#ifdef FEAT_RIGHTLEFT
7702 /* account for first command-line character in rightleft mode */
7703 && !cmdmsg_rl
7704#endif
7705 )
7706 {
7707 ScreenAttrs[off] = (sattr_T)-1;
7708 return;
7709 }
7710
7711 /*
7712 * Stop highlighting first, so it's easier to move the cursor.
7713 */
7714#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT)
7715 if (screen_char_attr != 0)
7716 attr = screen_char_attr;
7717 else
7718#endif
7719 attr = ScreenAttrs[off];
7720 if (screen_attr != attr)
7721 screen_stop_highlight();
7722
7723 windgoto(row, col);
7724
7725 if (screen_attr != attr)
7726 screen_start_highlight(attr);
7727
7728#ifdef FEAT_MBYTE
7729 if (enc_utf8 && ScreenLinesUC[off] != 0)
7730 {
7731 char_u buf[MB_MAXBYTES + 1];
7732
7733 /* Convert UTF-8 character to bytes and write it. */
7734
7735 buf[utfc_char2bytes(off, buf)] = NUL;
7736
7737 out_str(buf);
7738 if (utf_char2cells(ScreenLinesUC[off]) > 1)
7739 ++screen_cur_col;
7740 }
7741 else
7742#endif
7743 {
7744#ifdef FEAT_MBYTE
7745 out_flush_check();
7746#endif
7747 out_char(ScreenLines[off]);
7748#ifdef FEAT_MBYTE
7749 /* double-byte character in single-width cell */
7750 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7751 out_char(ScreenLines2[off]);
7752#endif
7753 }
7754
7755 screen_cur_col++;
7756}
7757
7758#ifdef FEAT_MBYTE
7759
7760/*
7761 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
7762 * on the screen at position 'row' and 'col'.
7763 * The attributes of the first byte is used for all. This is required to
7764 * output the two bytes of a double-byte character with nothing in between.
7765 */
7766 static void
7767screen_char_2(off, row, col)
7768 unsigned off;
7769 int row;
7770 int col;
7771{
7772 /* Check for illegal values (could be wrong when screen was resized). */
7773 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
7774 return;
7775
7776 /* Outputting the last character on the screen may scrollup the screen.
7777 * Don't to it! Mark the character invalid (update it when scrolled up) */
7778 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
7779 {
7780 ScreenAttrs[off] = (sattr_T)-1;
7781 return;
7782 }
7783
7784 /* Output the first byte normally (positions the cursor), then write the
7785 * second byte directly. */
7786 screen_char(off, row, col);
7787 out_char(ScreenLines[off + 1]);
7788 ++screen_cur_col;
7789}
7790#endif
7791
7792#if defined(FEAT_CLIPBOARD) || defined(FEAT_VERTSPLIT) || defined(PROTO)
7793/*
7794 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
7795 * This uses the contents of ScreenLines[] and doesn't change it.
7796 */
7797 void
7798screen_draw_rectangle(row, col, height, width, invert)
7799 int row;
7800 int col;
7801 int height;
7802 int width;
7803 int invert;
7804{
7805 int r, c;
7806 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007807#ifdef FEAT_MBYTE
7808 int max_off;
7809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00007811 /* Can't use ScreenLines unless initialized */
7812 if (ScreenLines == NULL)
7813 return;
7814
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (invert)
7816 screen_char_attr = HL_INVERSE;
7817 for (r = row; r < row + height; ++r)
7818 {
7819 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00007820#ifdef FEAT_MBYTE
7821 max_off = off + screen_Columns;
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 for (c = col; c < col + width; ++c)
7824 {
7825#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007826 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {
7828 screen_char_2(off + c, r, c);
7829 ++c;
7830 }
7831 else
7832#endif
7833 {
7834 screen_char(off + c, r, c);
7835#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007836 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 ++c;
7838#endif
7839 }
7840 }
7841 }
7842 screen_char_attr = 0;
7843}
7844#endif
7845
7846#ifdef FEAT_VERTSPLIT
7847/*
7848 * Redraw the characters for a vertically split window.
7849 */
7850 static void
7851redraw_block(row, end, wp)
7852 int row;
7853 int end;
7854 win_T *wp;
7855{
7856 int col;
7857 int width;
7858
7859# ifdef FEAT_CLIPBOARD
7860 clip_may_clear_selection(row, end - 1);
7861# endif
7862
7863 if (wp == NULL)
7864 {
7865 col = 0;
7866 width = Columns;
7867 }
7868 else
7869 {
7870 col = wp->w_wincol;
7871 width = wp->w_width;
7872 }
7873 screen_draw_rectangle(row, col, end - row, width, FALSE);
7874}
7875#endif
7876
7877/*
7878 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
7879 * with character 'c1' in first column followed by 'c2' in the other columns.
7880 * Use attributes 'attr'.
7881 */
7882 void
7883screen_fill(start_row, end_row, start_col, end_col, c1, c2, attr)
7884 int start_row, end_row;
7885 int start_col, end_col;
7886 int c1, c2;
7887 int attr;
7888{
7889 int row;
7890 int col;
7891 int off;
7892 int end_off;
7893 int did_delete;
7894 int c;
7895 int norm_term;
7896#if defined(FEAT_GUI) || defined(UNIX)
7897 int force_next = FALSE;
7898#endif
7899
7900 if (end_row > screen_Rows) /* safety check */
7901 end_row = screen_Rows;
7902 if (end_col > screen_Columns) /* safety check */
7903 end_col = screen_Columns;
7904 if (ScreenLines == NULL
7905 || start_row >= end_row
7906 || start_col >= end_col) /* nothing to do */
7907 return;
7908
7909 /* it's a "normal" terminal when not in a GUI or cterm */
7910 norm_term = (
7911#ifdef FEAT_GUI
7912 !gui.in_use &&
7913#endif
7914 t_colors <= 1);
7915 for (row = start_row; row < end_row; ++row)
7916 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00007917#ifdef FEAT_MBYTE
7918 if (has_mbyte
7919# ifdef FEAT_GUI
7920 && !gui.in_use
7921# endif
7922 )
7923 {
7924 /* When drawing over the right halve of a double-wide char clear
7925 * out the left halve. When drawing over the left halve of a
7926 * double wide-char clear out the right halve. Only needed in a
7927 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007928 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007929 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00007930 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00007931 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00007932 }
7933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 /*
7935 * Try to use delete-line termcap code, when no attributes or in a
7936 * "normal" terminal, where a bold/italic space is just a
7937 * space.
7938 */
7939 did_delete = FALSE;
7940 if (c2 == ' '
7941 && end_col == Columns
7942 && can_clear(T_CE)
7943 && (attr == 0
7944 || (norm_term
7945 && attr <= HL_ALL
7946 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
7947 {
7948 /*
7949 * check if we really need to clear something
7950 */
7951 col = start_col;
7952 if (c1 != ' ') /* don't clear first char */
7953 ++col;
7954
7955 off = LineOffset[row] + col;
7956 end_off = LineOffset[row] + end_col;
7957
7958 /* skip blanks (used often, keep it fast!) */
7959#ifdef FEAT_MBYTE
7960 if (enc_utf8)
7961 while (off < end_off && ScreenLines[off] == ' '
7962 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
7963 ++off;
7964 else
7965#endif
7966 while (off < end_off && ScreenLines[off] == ' '
7967 && ScreenAttrs[off] == 0)
7968 ++off;
7969 if (off < end_off) /* something to be cleared */
7970 {
7971 col = off - LineOffset[row];
7972 screen_stop_highlight();
7973 term_windgoto(row, col);/* clear rest of this screen line */
7974 out_str(T_CE);
7975 screen_start(); /* don't know where cursor is now */
7976 col = end_col - col;
7977 while (col--) /* clear chars in ScreenLines */
7978 {
7979 ScreenLines[off] = ' ';
7980#ifdef FEAT_MBYTE
7981 if (enc_utf8)
7982 ScreenLinesUC[off] = 0;
7983#endif
7984 ScreenAttrs[off] = 0;
7985 ++off;
7986 }
7987 }
7988 did_delete = TRUE; /* the chars are cleared now */
7989 }
7990
7991 off = LineOffset[row] + start_col;
7992 c = c1;
7993 for (col = start_col; col < end_col; ++col)
7994 {
7995 if (ScreenLines[off] != c
7996#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007997 || (enc_utf8 && (int)ScreenLinesUC[off]
7998 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999#endif
8000 || ScreenAttrs[off] != attr
8001#if defined(FEAT_GUI) || defined(UNIX)
8002 || force_next
8003#endif
8004 )
8005 {
8006#if defined(FEAT_GUI) || defined(UNIX)
8007 /* The bold trick may make a single row of pixels appear in
8008 * the next character. When a bold character is removed, the
8009 * next character should be redrawn too. This happens for our
8010 * own GUI and for some xterms. */
8011 if (
8012# ifdef FEAT_GUI
8013 gui.in_use
8014# endif
8015# if defined(FEAT_GUI) && defined(UNIX)
8016 ||
8017# endif
8018# ifdef UNIX
8019 term_is_xterm
8020# endif
8021 )
8022 {
8023 if (ScreenLines[off] != ' '
8024 && (ScreenAttrs[off] > HL_ALL
8025 || ScreenAttrs[off] & HL_BOLD))
8026 force_next = TRUE;
8027 else
8028 force_next = FALSE;
8029 }
8030#endif
8031 ScreenLines[off] = c;
8032#ifdef FEAT_MBYTE
8033 if (enc_utf8)
8034 {
8035 if (c >= 0x80)
8036 {
8037 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008038 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
8040 else
8041 ScreenLinesUC[off] = 0;
8042 }
8043#endif
8044 ScreenAttrs[off] = attr;
8045 if (!did_delete || c != ' ')
8046 screen_char(off, row, col);
8047 }
8048 ++off;
8049 if (col == start_col)
8050 {
8051 if (did_delete)
8052 break;
8053 c = c2;
8054 }
8055 }
8056 if (end_col == Columns)
8057 LineWraps[row] = FALSE;
8058 if (row == Rows - 1) /* overwritten the command line */
8059 {
8060 redraw_cmdline = TRUE;
8061 if (c1 == ' ' && c2 == ' ')
8062 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008063 if (start_col == 0)
8064 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066 }
8067}
8068
8069/*
8070 * Check if there should be a delay. Used before clearing or redrawing the
8071 * screen or the command line.
8072 */
8073 void
8074check_for_delay(check_msg_scroll)
8075 int check_msg_scroll;
8076{
8077 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8078 && !did_wait_return
8079 && emsg_silent == 0)
8080 {
8081 out_flush();
8082 ui_delay(1000L, TRUE);
8083 emsg_on_display = FALSE;
8084 if (check_msg_scroll)
8085 msg_scroll = FALSE;
8086 }
8087}
8088
8089/*
8090 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008091 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 * Returns TRUE if there is a valid screen to write to.
8093 * Returns FALSE when starting up and screen not initialized yet.
8094 */
8095 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008096screen_valid(doclear)
8097 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008099 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 return (ScreenLines != NULL);
8101}
8102
8103/*
8104 * Resize the shell to Rows and Columns.
8105 * Allocate ScreenLines[] and associated items.
8106 *
8107 * There may be some time between setting Rows and Columns and (re)allocating
8108 * ScreenLines[]. This happens when starting up and when (manually) changing
8109 * the shell size. Always use screen_Rows and screen_Columns to access items
8110 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8111 * final size of the shell is needed.
8112 */
8113 void
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008114screenalloc(doclear)
8115 int doclear;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 int new_row, old_row;
8118#ifdef FEAT_GUI
8119 int old_Rows;
8120#endif
8121 win_T *wp;
8122 int outofmem = FALSE;
8123 int len;
8124 schar_T *new_ScreenLines;
8125#ifdef FEAT_MBYTE
8126 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008127 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008129 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130#endif
8131 sattr_T *new_ScreenAttrs;
8132 unsigned *new_LineOffset;
8133 char_u *new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008134#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008135 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008136 tabpage_T *tp;
8137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008139 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008140#ifdef FEAT_AUTOCMD
8141 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008143retry:
8144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 /*
8146 * Allocation of the screen buffers is done only when the size changes and
8147 * when Rows and Columns have been set and we have started doing full
8148 * screen stuff.
8149 */
8150 if ((ScreenLines != NULL
8151 && Rows == screen_Rows
8152 && Columns == screen_Columns
8153#ifdef FEAT_MBYTE
8154 && enc_utf8 == (ScreenLinesUC != NULL)
8155 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008156 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157#endif
8158 )
8159 || Rows == 0
8160 || Columns == 0
8161 || (!full_screen && ScreenLines == NULL))
8162 return;
8163
8164 /*
8165 * It's possible that we produce an out-of-memory message below, which
8166 * will cause this function to be called again. To break the loop, just
8167 * return here.
8168 */
8169 if (entered)
8170 return;
8171 entered = TRUE;
8172
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008173 /*
8174 * Note that the window sizes are updated before reallocating the arrays,
8175 * thus we must not redraw here!
8176 */
8177 ++RedrawingDisabled;
8178
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 win_new_shellsize(); /* fit the windows in the new sized shell */
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 comp_col(); /* recompute columns for shown command and ruler */
8182
8183 /*
8184 * We're changing the size of the screen.
8185 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8186 * - Move lines from the old arrays into the new arrays, clear extra
8187 * lines (unless the screen is going to be cleared).
8188 * - Free the old arrays.
8189 *
8190 * If anything fails, make ScreenLines NULL, so we don't do anything!
8191 * Continuing with the old ScreenLines may result in a crash, because the
8192 * size is wrong.
8193 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008194 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008196#ifdef FEAT_AUTOCMD
8197 if (aucmd_win != NULL)
8198 win_free_lsize(aucmd_win);
8199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
8201 new_ScreenLines = (schar_T *)lalloc((long_u)(
8202 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8203#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008204 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 if (enc_utf8)
8206 {
8207 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8208 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008209 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008210 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8212 }
8213 if (enc_dbcs == DBCS_JPNU)
8214 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8215 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8216#endif
8217 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8218 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8219 new_LineOffset = (unsigned *)lalloc((long_u)(
8220 Rows * sizeof(unsigned)), FALSE);
8221 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008222#ifdef FEAT_WINDOWS
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008223 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008226 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
8228 if (win_alloc_lines(wp) == FAIL)
8229 {
8230 outofmem = TRUE;
8231#ifdef FEAT_WINDOWS
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008232 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233#endif
8234 }
8235 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008236#ifdef FEAT_AUTOCMD
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008237 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8238 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008239 outofmem = TRUE;
8240#endif
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008241#ifdef FEAT_WINDOWS
8242give_up:
8243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008245#ifdef FEAT_MBYTE
8246 for (i = 0; i < p_mco; ++i)
8247 if (new_ScreenLinesC[i] == NULL)
8248 break;
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (new_ScreenLines == NULL
8251#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008252 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8254#endif
8255 || new_ScreenAttrs == NULL
8256 || new_LineOffset == NULL
8257 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008258#ifdef FEAT_WINDOWS
8259 || new_TabPageIdxs == NULL
8260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 || outofmem)
8262 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008263 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008264 {
8265 /* guess the size */
8266 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8267
8268 /* Remember we did this to avoid getting outofmem messages over
8269 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008270 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 vim_free(new_ScreenLines);
8273 new_ScreenLines = NULL;
8274#ifdef FEAT_MBYTE
8275 vim_free(new_ScreenLinesUC);
8276 new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008277 for (i = 0; i < p_mco; ++i)
8278 {
8279 vim_free(new_ScreenLinesC[i]);
8280 new_ScreenLinesC[i] = NULL;
8281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 vim_free(new_ScreenLines2);
8283 new_ScreenLines2 = NULL;
8284#endif
8285 vim_free(new_ScreenAttrs);
8286 new_ScreenAttrs = NULL;
8287 vim_free(new_LineOffset);
8288 new_LineOffset = NULL;
8289 vim_free(new_LineWraps);
8290 new_LineWraps = NULL;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008291#ifdef FEAT_WINDOWS
8292 vim_free(new_TabPageIdxs);
8293 new_TabPageIdxs = NULL;
8294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
8296 else
8297 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008298 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 for (new_row = 0; new_row < Rows; ++new_row)
8301 {
8302 new_LineOffset[new_row] = new_row * Columns;
8303 new_LineWraps[new_row] = FALSE;
8304
8305 /*
8306 * If the screen is not going to be cleared, copy as much as
8307 * possible from the old screen to the new one and clear the rest
8308 * (used when resizing the window at the "--more--" prompt or when
8309 * executing an external command, for the GUI).
8310 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008311 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
8313 (void)vim_memset(new_ScreenLines + new_row * Columns,
8314 ' ', (size_t)Columns * sizeof(schar_T));
8315#ifdef FEAT_MBYTE
8316 if (enc_utf8)
8317 {
8318 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8319 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008320 for (i = 0; i < p_mco; ++i)
8321 (void)vim_memset(new_ScreenLinesC[i]
8322 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 0, (size_t)Columns * sizeof(u8char_T));
8324 }
8325 if (enc_dbcs == DBCS_JPNU)
8326 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8327 0, (size_t)Columns * sizeof(schar_T));
8328#endif
8329 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8330 0, (size_t)Columns * sizeof(sattr_T));
8331 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008332 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 if (screen_Columns < Columns)
8335 len = screen_Columns;
8336 else
8337 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008338#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008339 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008340 * may be invalid now. Also when p_mco changes. */
8341 if (!(enc_utf8 && ScreenLinesUC == NULL)
8342 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008343#endif
8344 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8345 ScreenLines + LineOffset[old_row],
8346 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008348 if (enc_utf8 && ScreenLinesUC != NULL
8349 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8352 ScreenLinesUC + LineOffset[old_row],
8353 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008354 for (i = 0; i < p_mco; ++i)
8355 mch_memmove(new_ScreenLinesC[i]
8356 + new_LineOffset[new_row],
8357 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 (size_t)len * sizeof(u8char_T));
8359 }
8360 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8361 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8362 ScreenLines2 + LineOffset[old_row],
8363 (size_t)len * sizeof(schar_T));
8364#endif
8365 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8366 ScreenAttrs + LineOffset[old_row],
8367 (size_t)len * sizeof(sattr_T));
8368 }
8369 }
8370 }
8371 /* Use the last line of the screen for the current line. */
8372 current_ScreenLine = new_ScreenLines + Rows * Columns;
8373 }
8374
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008375 free_screenlines();
8376
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 ScreenLines = new_ScreenLines;
8378#ifdef FEAT_MBYTE
8379 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008380 for (i = 0; i < p_mco; ++i)
8381 ScreenLinesC[i] = new_ScreenLinesC[i];
8382 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 ScreenLines2 = new_ScreenLines2;
8384#endif
8385 ScreenAttrs = new_ScreenAttrs;
8386 LineOffset = new_LineOffset;
8387 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008388#ifdef FEAT_WINDOWS
8389 TabPageIdxs = new_TabPageIdxs;
8390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 /* It's important that screen_Rows and screen_Columns reflect the actual
8393 * size of ScreenLines[]. Set them before calling anything. */
8394#ifdef FEAT_GUI
8395 old_Rows = screen_Rows;
8396#endif
8397 screen_Rows = Rows;
8398 screen_Columns = Columns;
8399
8400 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008401 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 screenclear2();
8403
8404#ifdef FEAT_GUI
8405 else if (gui.in_use
8406 && !gui.starting
8407 && ScreenLines != NULL
8408 && old_Rows != Rows)
8409 {
8410 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8411 /*
8412 * Adjust the position of the cursor, for when executing an external
8413 * command.
8414 */
8415 if (msg_row >= Rows) /* Rows got smaller */
8416 msg_row = Rows - 1; /* put cursor at last row */
8417 else if (Rows > old_Rows) /* Rows got bigger */
8418 msg_row += Rows - old_Rows; /* put cursor in same place */
8419 if (msg_col >= Columns) /* Columns got smaller */
8420 msg_col = Columns - 1; /* put cursor at last column */
8421 }
8422#endif
8423
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008425 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008426
8427#ifdef FEAT_AUTOCMD
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008428 /*
8429 * Do not apply autocommands more than 3 times to avoid an endless loop
8430 * in case applying autocommands always changes Rows or Columns.
8431 */
8432 if (starting == 0 && ++retry_count <= 3)
8433 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008434 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008435 /* In rare cases, autocommands may have altered Rows or Columns,
8436 * jump back to check if we need to allocate the screen again. */
8437 goto retry;
8438 }
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440}
8441
8442 void
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008443free_screenlines()
8444{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008445#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008446 int i;
8447
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008448 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008449 for (i = 0; i < Screen_mco; ++i)
8450 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008451 vim_free(ScreenLines2);
8452#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008453 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008454 vim_free(ScreenAttrs);
8455 vim_free(LineOffset);
8456 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00008457#ifdef FEAT_WINDOWS
8458 vim_free(TabPageIdxs);
8459#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008460}
8461
8462 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463screenclear()
8464{
8465 check_for_delay(FALSE);
8466 screenalloc(FALSE); /* allocate screen buffers if size changed */
8467 screenclear2(); /* clear the screen */
8468}
8469
8470 static void
8471screenclear2()
8472{
8473 int i;
8474
8475 if (starting == NO_SCREEN || ScreenLines == NULL
8476#ifdef FEAT_GUI
8477 || (gui.in_use && gui.starting)
8478#endif
8479 )
8480 return;
8481
8482#ifdef FEAT_GUI
8483 if (!gui.in_use)
8484#endif
8485 screen_attr = -1; /* force setting the Normal colors */
8486 screen_stop_highlight(); /* don't want highlighting here */
8487
8488#ifdef FEAT_CLIPBOARD
8489 /* disable selection without redrawing it */
8490 clip_scroll_selection(9999);
8491#endif
8492
8493 /* blank out ScreenLines */
8494 for (i = 0; i < Rows; ++i)
8495 {
8496 lineclear(LineOffset[i], (int)Columns);
8497 LineWraps[i] = FALSE;
8498 }
8499
8500 if (can_clear(T_CL))
8501 {
8502 out_str(T_CL); /* clear the display */
8503 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008504 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
8506 else
8507 {
8508 /* can't clear the screen, mark all chars with invalid attributes */
8509 for (i = 0; i < Rows; ++i)
8510 lineinvalid(LineOffset[i], (int)Columns);
8511 clear_cmdline = TRUE;
8512 }
8513
8514 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8515
8516 win_rest_invalid(firstwin);
8517 redraw_cmdline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008518#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008519 redraw_tabline = TRUE;
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00008520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (must_redraw == CLEAR) /* no need to clear again */
8522 must_redraw = NOT_VALID;
8523 compute_cmdrow();
8524 msg_row = cmdline_row; /* put cursor on last line for messages */
8525 msg_col = 0;
8526 screen_start(); /* don't know where cursor is now */
8527 msg_scrolled = 0; /* can't scroll back */
8528 msg_didany = FALSE;
8529 msg_didout = FALSE;
8530}
8531
8532/*
8533 * Clear one line in ScreenLines.
8534 */
8535 static void
8536lineclear(off, width)
8537 unsigned off;
8538 int width;
8539{
8540 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
8541#ifdef FEAT_MBYTE
8542 if (enc_utf8)
8543 (void)vim_memset(ScreenLinesUC + off, 0,
8544 (size_t)width * sizeof(u8char_T));
8545#endif
8546 (void)vim_memset(ScreenAttrs + off, 0, (size_t)width * sizeof(sattr_T));
8547}
8548
8549/*
8550 * Mark one line in ScreenLines invalid by setting the attributes to an
8551 * invalid value.
8552 */
8553 static void
8554lineinvalid(off, width)
8555 unsigned off;
8556 int width;
8557{
8558 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8559}
8560
8561#ifdef FEAT_VERTSPLIT
8562/*
8563 * Copy part of a Screenline for vertically split window "wp".
8564 */
8565 static void
8566linecopy(to, from, wp)
8567 int to;
8568 int from;
8569 win_T *wp;
8570{
8571 unsigned off_to = LineOffset[to] + wp->w_wincol;
8572 unsigned off_from = LineOffset[from] + wp->w_wincol;
8573
8574 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8575 wp->w_width * sizeof(schar_T));
8576# ifdef FEAT_MBYTE
8577 if (enc_utf8)
8578 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008579 int i;
8580
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8582 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008583 for (i = 0; i < p_mco; ++i)
8584 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8585 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 if (enc_dbcs == DBCS_JPNU)
8588 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8589 wp->w_width * sizeof(schar_T));
8590# endif
8591 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8592 wp->w_width * sizeof(sattr_T));
8593}
8594#endif
8595
8596/*
8597 * Return TRUE if clearing with term string "p" would work.
8598 * It can't work when the string is empty or it won't set the right background.
8599 */
8600 int
8601can_clear(p)
8602 char_u *p;
8603{
8604 return (*p != NUL && (t_colors <= 1
8605#ifdef FEAT_GUI
8606 || gui.in_use
8607#endif
8608 || cterm_normal_bg_color == 0 || *T_UT != NUL));
8609}
8610
8611/*
8612 * Reset cursor position. Use whenever cursor was moved because of outputting
8613 * something directly to the screen (shell commands) or a terminal control
8614 * code.
8615 */
8616 void
8617screen_start()
8618{
8619 screen_cur_row = screen_cur_col = 9999;
8620}
8621
8622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 * Move the cursor to position "row","col" in the screen.
8624 * This tries to find the most efficient way to move, minimizing the number of
8625 * characters sent to the terminal.
8626 */
8627 void
8628windgoto(row, col)
8629 int row;
8630 int col;
8631{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008632 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 int i;
8634 int plan;
8635 int cost;
8636 int wouldbe_col;
8637 int noinvcurs;
8638 char_u *bs;
8639 int goto_cost;
8640 int attr;
8641
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008642#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8644
8645#define PLAN_LE 1
8646#define PLAN_CR 2
8647#define PLAN_NL 3
8648#define PLAN_WRITE 4
8649 /* Can't use ScreenLines unless initialized */
8650 if (ScreenLines == NULL)
8651 return;
8652
8653 if (col != screen_cur_col || row != screen_cur_row)
8654 {
8655 /* Check for valid position. */
8656 if (row < 0) /* window without text lines? */
8657 row = 0;
8658 if (row >= screen_Rows)
8659 row = screen_Rows - 1;
8660 if (col >= screen_Columns)
8661 col = screen_Columns - 1;
8662
8663 /* check if no cursor movement is allowed in highlight mode */
8664 if (screen_attr && *T_MS == NUL)
8665 noinvcurs = HIGHL_COST;
8666 else
8667 noinvcurs = 0;
8668 goto_cost = GOTO_COST + noinvcurs;
8669
8670 /*
8671 * Plan how to do the positioning:
8672 * 1. Use CR to move it to column 0, same row.
8673 * 2. Use T_LE to move it a few columns to the left.
8674 * 3. Use NL to move a few lines down, column 0.
8675 * 4. Move a few columns to the right with T_ND or by writing chars.
8676 *
8677 * Don't do this if the cursor went beyond the last column, the cursor
8678 * position is unknown then (some terminals wrap, some don't )
8679 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008680 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 * characters to move the cursor to the right.
8682 */
8683 if (row >= screen_cur_row && screen_cur_col < Columns)
8684 {
8685 /*
8686 * If the cursor is in the same row, bigger col, we can use CR
8687 * or T_LE.
8688 */
8689 bs = NULL; /* init for GCC */
8690 attr = screen_attr;
8691 if (row == screen_cur_row && col < screen_cur_col)
8692 {
8693 /* "le" is preferred over "bc", because "bc" is obsolete */
8694 if (*T_LE)
8695 bs = T_LE; /* "cursor left" */
8696 else
8697 bs = T_BC; /* "backspace character (old) */
8698 if (*bs)
8699 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8700 else
8701 cost = 999;
8702 if (col + 1 < cost) /* using CR is less characters */
8703 {
8704 plan = PLAN_CR;
8705 wouldbe_col = 0;
8706 cost = 1; /* CR is just one character */
8707 }
8708 else
8709 {
8710 plan = PLAN_LE;
8711 wouldbe_col = col;
8712 }
8713 if (noinvcurs) /* will stop highlighting */
8714 {
8715 cost += noinvcurs;
8716 attr = 0;
8717 }
8718 }
8719
8720 /*
8721 * If the cursor is above where we want to be, we can use CR LF.
8722 */
8723 else if (row > screen_cur_row)
8724 {
8725 plan = PLAN_NL;
8726 wouldbe_col = 0;
8727 cost = (row - screen_cur_row) * 2; /* CR LF */
8728 if (noinvcurs) /* will stop highlighting */
8729 {
8730 cost += noinvcurs;
8731 attr = 0;
8732 }
8733 }
8734
8735 /*
8736 * If the cursor is in the same row, smaller col, just use write.
8737 */
8738 else
8739 {
8740 plan = PLAN_WRITE;
8741 wouldbe_col = screen_cur_col;
8742 cost = 0;
8743 }
8744
8745 /*
8746 * Check if any characters that need to be written have the
8747 * correct attributes. Also avoid UTF-8 characters.
8748 */
8749 i = col - wouldbe_col;
8750 if (i > 0)
8751 cost += i;
8752 if (cost < goto_cost && i > 0)
8753 {
8754 /*
8755 * Check if the attributes are correct without additionally
8756 * stopping highlighting.
8757 */
8758 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
8759 while (i && *p++ == attr)
8760 --i;
8761 if (i != 0)
8762 {
8763 /*
8764 * Try if it works when highlighting is stopped here.
8765 */
8766 if (*--p == 0)
8767 {
8768 cost += noinvcurs;
8769 while (i && *p++ == 0)
8770 --i;
8771 }
8772 if (i != 0)
8773 cost = 999; /* different attributes, don't do it */
8774 }
8775#ifdef FEAT_MBYTE
8776 if (enc_utf8)
8777 {
8778 /* Don't use an UTF-8 char for positioning, it's slow. */
8779 for (i = wouldbe_col; i < col; ++i)
8780 if (ScreenLinesUC[LineOffset[row] + i] != 0)
8781 {
8782 cost = 999;
8783 break;
8784 }
8785 }
8786#endif
8787 }
8788
8789 /*
8790 * We can do it without term_windgoto()!
8791 */
8792 if (cost < goto_cost)
8793 {
8794 if (plan == PLAN_LE)
8795 {
8796 if (noinvcurs)
8797 screen_stop_highlight();
8798 while (screen_cur_col > col)
8799 {
8800 out_str(bs);
8801 --screen_cur_col;
8802 }
8803 }
8804 else if (plan == PLAN_CR)
8805 {
8806 if (noinvcurs)
8807 screen_stop_highlight();
8808 out_char('\r');
8809 screen_cur_col = 0;
8810 }
8811 else if (plan == PLAN_NL)
8812 {
8813 if (noinvcurs)
8814 screen_stop_highlight();
8815 while (screen_cur_row < row)
8816 {
8817 out_char('\n');
8818 ++screen_cur_row;
8819 }
8820 screen_cur_col = 0;
8821 }
8822
8823 i = col - screen_cur_col;
8824 if (i > 0)
8825 {
8826 /*
8827 * Use cursor-right if it's one character only. Avoids
8828 * removing a line of pixels from the last bold char, when
8829 * using the bold trick in the GUI.
8830 */
8831 if (T_ND[0] != NUL && T_ND[1] == NUL)
8832 {
8833 while (i-- > 0)
8834 out_char(*T_ND);
8835 }
8836 else
8837 {
8838 int off;
8839
8840 off = LineOffset[row] + screen_cur_col;
8841 while (i-- > 0)
8842 {
8843 if (ScreenAttrs[off] != screen_attr)
8844 screen_stop_highlight();
8845#ifdef FEAT_MBYTE
8846 out_flush_check();
8847#endif
8848 out_char(ScreenLines[off]);
8849#ifdef FEAT_MBYTE
8850 if (enc_dbcs == DBCS_JPNU
8851 && ScreenLines[off] == 0x8e)
8852 out_char(ScreenLines2[off]);
8853#endif
8854 ++off;
8855 }
8856 }
8857 }
8858 }
8859 }
8860 else
8861 cost = 999;
8862
8863 if (cost >= goto_cost)
8864 {
8865 if (noinvcurs)
8866 screen_stop_highlight();
8867 if (row == screen_cur_row && (col > screen_cur_col) &&
8868 *T_CRI != NUL)
8869 term_cursor_right(col - screen_cur_col);
8870 else
8871 term_windgoto(row, col);
8872 }
8873 screen_cur_row = row;
8874 screen_cur_col = col;
8875 }
8876}
8877
8878/*
8879 * Set cursor to its position in the current window.
8880 */
8881 void
8882setcursor()
8883{
8884 if (redrawing())
8885 {
8886 validate_cursor();
8887 windgoto(W_WINROW(curwin) + curwin->w_wrow,
8888 W_WINCOL(curwin) + (
8889#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008890 /* With 'rightleft' set and the cursor on a double-wide
8891 * character, position it on the leftmost column. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 curwin->w_p_rl ? ((int)W_WIDTH(curwin) - curwin->w_wcol - (
8893# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00008894 (has_mbyte
8895 && (*mb_ptr2cells)(ml_get_cursor()) == 2
8896 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897# endif
8898 1)) :
8899#endif
8900 curwin->w_wcol));
8901 }
8902}
8903
8904
8905/*
8906 * insert 'line_count' lines at 'row' in window 'wp'
8907 * if 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
8908 * if 'mayclear' is TRUE the screen will be cleared if it is faster than
8909 * scrolling.
8910 * Returns FAIL if the lines are not inserted, OK for success.
8911 */
8912 int
8913win_ins_lines(wp, row, line_count, invalid, mayclear)
8914 win_T *wp;
8915 int row;
8916 int line_count;
8917 int invalid;
8918 int mayclear;
8919{
8920 int did_delete;
8921 int nextrow;
8922 int lastrow;
8923 int retval;
8924
8925 if (invalid)
8926 wp->w_lines_valid = 0;
8927
8928 if (wp->w_height < 5)
8929 return FAIL;
8930
8931 if (line_count > wp->w_height - row)
8932 line_count = wp->w_height - row;
8933
8934 retval = win_do_lines(wp, row, line_count, mayclear, FALSE);
8935 if (retval != MAYBE)
8936 return retval;
8937
8938 /*
8939 * If there is a next window or a status line, we first try to delete the
8940 * lines at the bottom to avoid messing what is after the window.
8941 * If this fails and there are following windows, don't do anything to avoid
8942 * messing up those windows, better just redraw.
8943 */
8944 did_delete = FALSE;
8945#ifdef FEAT_WINDOWS
8946 if (wp->w_next != NULL || wp->w_status_height)
8947 {
8948 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
8949 line_count, (int)Rows, FALSE, NULL) == OK)
8950 did_delete = TRUE;
8951 else if (wp->w_next)
8952 return FAIL;
8953 }
8954#endif
8955 /*
8956 * if no lines deleted, blank the lines that will end up below the window
8957 */
8958 if (!did_delete)
8959 {
8960#ifdef FEAT_WINDOWS
8961 wp->w_redr_status = TRUE;
8962#endif
8963 redraw_cmdline = TRUE;
8964 nextrow = W_WINROW(wp) + wp->w_height + W_STATUS_HEIGHT(wp);
8965 lastrow = nextrow + line_count;
8966 if (lastrow > Rows)
8967 lastrow = Rows;
8968 screen_fill(nextrow - line_count, lastrow - line_count,
8969 W_WINCOL(wp), (int)W_ENDCOL(wp),
8970 ' ', ' ', 0);
8971 }
8972
8973 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, NULL)
8974 == FAIL)
8975 {
8976 /* deletion will have messed up other windows */
8977 if (did_delete)
8978 {
8979#ifdef FEAT_WINDOWS
8980 wp->w_redr_status = TRUE;
8981#endif
8982 win_rest_invalid(W_NEXT(wp));
8983 }
8984 return FAIL;
8985 }
8986
8987 return OK;
8988}
8989
8990/*
8991 * delete "line_count" window lines at "row" in window "wp"
8992 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
8993 * If "mayclear" is TRUE the screen will be cleared if it is faster than
8994 * scrolling
8995 * Return OK for success, FAIL if the lines are not deleted.
8996 */
8997 int
8998win_del_lines(wp, row, line_count, invalid, mayclear)
8999 win_T *wp;
9000 int row;
9001 int line_count;
9002 int invalid;
9003 int mayclear;
9004{
9005 int retval;
9006
9007 if (invalid)
9008 wp->w_lines_valid = 0;
9009
9010 if (line_count > wp->w_height - row)
9011 line_count = wp->w_height - row;
9012
9013 retval = win_do_lines(wp, row, line_count, mayclear, TRUE);
9014 if (retval != MAYBE)
9015 return retval;
9016
9017 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
9018 (int)Rows, FALSE, NULL) == FAIL)
9019 return FAIL;
9020
9021#ifdef FEAT_WINDOWS
9022 /*
9023 * If there are windows or status lines below, try to put them at the
9024 * correct place. If we can't do that, they have to be redrawn.
9025 */
9026 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9027 {
9028 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
9029 line_count, (int)Rows, NULL) == FAIL)
9030 {
9031 wp->w_redr_status = TRUE;
9032 win_rest_invalid(wp->w_next);
9033 }
9034 }
9035 /*
9036 * If this is the last window and there is no status line, redraw the
9037 * command line later.
9038 */
9039 else
9040#endif
9041 redraw_cmdline = TRUE;
9042 return OK;
9043}
9044
9045/*
9046 * Common code for win_ins_lines() and win_del_lines().
9047 * Returns OK or FAIL when the work has been done.
9048 * Returns MAYBE when not finished yet.
9049 */
9050 static int
9051win_do_lines(wp, row, line_count, mayclear, del)
9052 win_T *wp;
9053 int row;
9054 int line_count;
9055 int mayclear;
9056 int del;
9057{
9058 int retval;
9059
9060 if (!redrawing() || line_count <= 0)
9061 return FAIL;
9062
9063 /* only a few lines left: redraw is faster */
9064 if (mayclear && Rows - line_count < 5
9065#ifdef FEAT_VERTSPLIT
9066 && wp->w_width == Columns
9067#endif
9068 )
9069 {
9070 screenclear(); /* will set wp->w_lines_valid to 0 */
9071 return FAIL;
9072 }
9073
9074 /*
9075 * Delete all remaining lines
9076 */
9077 if (row + line_count >= wp->w_height)
9078 {
9079 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
9080 W_WINCOL(wp), (int)W_ENDCOL(wp),
9081 ' ', ' ', 0);
9082 return OK;
9083 }
9084
9085 /*
9086 * when scrolling, the message on the command line should be cleared,
9087 * otherwise it will stay there forever.
9088 */
9089 clear_cmdline = TRUE;
9090
9091 /*
9092 * If the terminal can set a scroll region, use that.
9093 * Always do this in a vertically split window. This will redraw from
9094 * ScreenLines[] when t_CV isn't defined. That's faster than using
9095 * win_line().
9096 * Don't use a scroll region when we are going to redraw the text, writing
9097 * a character in the lower right corner of the scroll region causes a
9098 * scroll-up in the DJGPP version.
9099 */
9100 if (scroll_region
9101#ifdef FEAT_VERTSPLIT
9102 || W_WIDTH(wp) != Columns
9103#endif
9104 )
9105 {
9106#ifdef FEAT_VERTSPLIT
9107 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9108#endif
9109 scroll_region_set(wp, row);
9110 if (del)
9111 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
9112 wp->w_height - row, FALSE, wp);
9113 else
9114 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
9115 wp->w_height - row, wp);
9116#ifdef FEAT_VERTSPLIT
9117 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
9118#endif
9119 scroll_region_reset();
9120 return retval;
9121 }
9122
9123#ifdef FEAT_WINDOWS
9124 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9125 return FAIL;
9126#endif
9127
9128 return MAYBE;
9129}
9130
9131/*
9132 * window 'wp' and everything after it is messed up, mark it for redraw
9133 */
9134 static void
9135win_rest_invalid(wp)
9136 win_T *wp;
9137{
9138#ifdef FEAT_WINDOWS
9139 while (wp != NULL)
9140#else
9141 if (wp != NULL)
9142#endif
9143 {
9144 redraw_win_later(wp, NOT_VALID);
9145#ifdef FEAT_WINDOWS
9146 wp->w_redr_status = TRUE;
9147 wp = wp->w_next;
9148#endif
9149 }
9150 redraw_cmdline = TRUE;
9151}
9152
9153/*
9154 * The rest of the routines in this file perform screen manipulations. The
9155 * given operation is performed physically on the screen. The corresponding
9156 * change is also made to the internal screen image. In this way, the editor
9157 * anticipates the effect of editing changes on the appearance of the screen.
9158 * That way, when we call screenupdate a complete redraw isn't usually
9159 * necessary. Another advantage is that we can keep adding code to anticipate
9160 * screen changes, and in the meantime, everything still works.
9161 */
9162
9163/*
9164 * types for inserting or deleting lines
9165 */
9166#define USE_T_CAL 1
9167#define USE_T_CDL 2
9168#define USE_T_AL 3
9169#define USE_T_CE 4
9170#define USE_T_DL 5
9171#define USE_T_SR 6
9172#define USE_NL 7
9173#define USE_T_CD 8
9174#define USE_REDRAW 9
9175
9176/*
9177 * insert lines on the screen and update ScreenLines[]
9178 * 'end' is the line after the scrolled part. Normally it is Rows.
9179 * When scrolling region used 'off' is the offset from the top for the region.
9180 * 'row' and 'end' are relative to the start of the region.
9181 *
9182 * return FAIL for failure, OK for success.
9183 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009184 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185screen_ins_lines(off, row, line_count, end, wp)
9186 int off;
9187 int row;
9188 int line_count;
9189 int end;
9190 win_T *wp; /* NULL or window to use width from */
9191{
9192 int i;
9193 int j;
9194 unsigned temp;
9195 int cursor_row;
9196 int type;
9197 int result_empty;
9198 int can_ce = can_clear(T_CE);
9199
9200 /*
9201 * FAIL if
9202 * - there is no valid screen
9203 * - the screen has to be redrawn completely
9204 * - the line count is less than one
9205 * - the line count is more than 'ttyscroll'
9206 */
9207 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll)
9208 return FAIL;
9209
9210 /*
9211 * There are seven ways to insert lines:
9212 * 0. When in a vertically split window and t_CV isn't set, redraw the
9213 * characters from ScreenLines[].
9214 * 1. Use T_CD (clear to end of display) if it exists and the result of
9215 * the insert is just empty lines
9216 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9217 * present or line_count > 1. It looks better if we do all the inserts
9218 * at once.
9219 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9220 * insert is just empty lines and T_CE is not present or line_count >
9221 * 1.
9222 * 4. Use T_AL (insert line) if it exists.
9223 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9224 * just empty lines.
9225 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9226 * just empty lines.
9227 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9228 * the 'da' flag is not set or we have clear line capability.
9229 * 8. redraw the characters from ScreenLines[].
9230 *
9231 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9232 * the scrollbar for the window. It does have insert line, use that if it
9233 * exists.
9234 */
9235 result_empty = (row + line_count >= end);
9236#ifdef FEAT_VERTSPLIT
9237 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9238 type = USE_REDRAW;
9239 else
9240#endif
9241 if (can_clear(T_CD) && result_empty)
9242 type = USE_T_CD;
9243 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9244 type = USE_T_CAL;
9245 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9246 type = USE_T_CDL;
9247 else if (*T_AL != NUL)
9248 type = USE_T_AL;
9249 else if (can_ce && result_empty)
9250 type = USE_T_CE;
9251 else if (*T_DL != NUL && result_empty)
9252 type = USE_T_DL;
9253 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9254 type = USE_T_SR;
9255 else
9256 return FAIL;
9257
9258 /*
9259 * For clearing the lines screen_del_lines() is used. This will also take
9260 * care of t_db if necessary.
9261 */
9262 if (type == USE_T_CD || type == USE_T_CDL ||
9263 type == USE_T_CE || type == USE_T_DL)
9264 return screen_del_lines(off, row, line_count, end, FALSE, wp);
9265
9266 /*
9267 * If text is retained below the screen, first clear or delete as many
9268 * lines at the bottom of the window as are about to be inserted so that
9269 * the deleted lines won't later surface during a screen_del_lines.
9270 */
9271 if (*T_DB)
9272 screen_del_lines(off, end - line_count, line_count, end, FALSE, wp);
9273
9274#ifdef FEAT_CLIPBOARD
9275 /* Remove a modeless selection when inserting lines halfway the screen
9276 * or not the full width of the screen. */
9277 if (off + row > 0
9278# ifdef FEAT_VERTSPLIT
9279 || (wp != NULL && wp->w_width != Columns)
9280# endif
9281 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009282 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 else
9284 clip_scroll_selection(-line_count);
9285#endif
9286
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287#ifdef FEAT_GUI
9288 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9289 * scrolling is actually carried out. */
9290 gui_dont_update_cursor();
9291#endif
9292
9293 if (*T_CCS != NUL) /* cursor relative to region */
9294 cursor_row = row;
9295 else
9296 cursor_row = row + off;
9297
9298 /*
9299 * Shift LineOffset[] line_count down to reflect the inserted lines.
9300 * Clear the inserted lines in ScreenLines[].
9301 */
9302 row += off;
9303 end += off;
9304 for (i = 0; i < line_count; ++i)
9305 {
9306#ifdef FEAT_VERTSPLIT
9307 if (wp != NULL && wp->w_width != Columns)
9308 {
9309 /* need to copy part of a line */
9310 j = end - 1 - i;
9311 while ((j -= line_count) >= row)
9312 linecopy(j + line_count, j, wp);
9313 j += line_count;
9314 if (can_clear((char_u *)" "))
9315 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9316 else
9317 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9318 LineWraps[j] = FALSE;
9319 }
9320 else
9321#endif
9322 {
9323 j = end - 1 - i;
9324 temp = LineOffset[j];
9325 while ((j -= line_count) >= row)
9326 {
9327 LineOffset[j + line_count] = LineOffset[j];
9328 LineWraps[j + line_count] = LineWraps[j];
9329 }
9330 LineOffset[j + line_count] = temp;
9331 LineWraps[j + line_count] = FALSE;
9332 if (can_clear((char_u *)" "))
9333 lineclear(temp, (int)Columns);
9334 else
9335 lineinvalid(temp, (int)Columns);
9336 }
9337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338
9339 screen_stop_highlight();
9340 windgoto(cursor_row, 0);
9341
9342#ifdef FEAT_VERTSPLIT
9343 /* redraw the characters */
9344 if (type == USE_REDRAW)
9345 redraw_block(row, end, wp);
9346 else
9347#endif
9348 if (type == USE_T_CAL)
9349 {
9350 term_append_lines(line_count);
9351 screen_start(); /* don't know where cursor is now */
9352 }
9353 else
9354 {
9355 for (i = 0; i < line_count; i++)
9356 {
9357 if (type == USE_T_AL)
9358 {
9359 if (i && cursor_row != 0)
9360 windgoto(cursor_row, 0);
9361 out_str(T_AL);
9362 }
9363 else /* type == USE_T_SR */
9364 out_str(T_SR);
9365 screen_start(); /* don't know where cursor is now */
9366 }
9367 }
9368
9369 /*
9370 * With scroll-reverse and 'da' flag set we need to clear the lines that
9371 * have been scrolled down into the region.
9372 */
9373 if (type == USE_T_SR && *T_DA)
9374 {
9375 for (i = 0; i < line_count; ++i)
9376 {
9377 windgoto(off + i, 0);
9378 out_str(T_CE);
9379 screen_start(); /* don't know where cursor is now */
9380 }
9381 }
9382
9383#ifdef FEAT_GUI
9384 gui_can_update_cursor();
9385 if (gui.in_use)
9386 out_flush(); /* always flush after a scroll */
9387#endif
9388 return OK;
9389}
9390
9391/*
9392 * delete lines on the screen and update ScreenLines[]
9393 * 'end' is the line after the scrolled part. Normally it is Rows.
9394 * When scrolling region used 'off' is the offset from the top for the region.
9395 * 'row' and 'end' are relative to the start of the region.
9396 *
9397 * Return OK for success, FAIL if the lines are not deleted.
9398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 int
9400screen_del_lines(off, row, line_count, end, force, wp)
9401 int off;
9402 int row;
9403 int line_count;
9404 int end;
9405 int force; /* even when line_count > p_ttyscroll */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009406 win_T *wp UNUSED; /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407{
9408 int j;
9409 int i;
9410 unsigned temp;
9411 int cursor_row;
9412 int cursor_end;
9413 int result_empty; /* result is empty until end of region */
9414 int can_delete; /* deleting line codes can be used */
9415 int type;
9416
9417 /*
9418 * FAIL if
9419 * - there is no valid screen
9420 * - the screen has to be redrawn completely
9421 * - the line count is less than one
9422 * - the line count is more than 'ttyscroll'
9423 */
9424 if (!screen_valid(TRUE) || line_count <= 0 ||
9425 (!force && line_count > p_ttyscroll))
9426 return FAIL;
9427
9428 /*
9429 * Check if the rest of the current region will become empty.
9430 */
9431 result_empty = row + line_count >= end;
9432
9433 /*
9434 * We can delete lines only when 'db' flag not set or when 'ce' option
9435 * available.
9436 */
9437 can_delete = (*T_DB == NUL || can_clear(T_CE));
9438
9439 /*
9440 * There are six ways to delete lines:
9441 * 0. When in a vertically split window and t_CV isn't set, redraw the
9442 * characters from ScreenLines[].
9443 * 1. Use T_CD if it exists and the result is empty.
9444 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9445 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9446 * none of the other ways work.
9447 * 4. Use T_CE (erase line) if the result is empty.
9448 * 5. Use T_DL (delete line) if it exists.
9449 * 6. redraw the characters from ScreenLines[].
9450 */
9451#ifdef FEAT_VERTSPLIT
9452 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9453 type = USE_REDRAW;
9454 else
9455#endif
9456 if (can_clear(T_CD) && result_empty)
9457 type = USE_T_CD;
9458#if defined(__BEOS__) && defined(BEOS_DR8)
9459 /*
9460 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9461 * its internal termcap... this works okay for tests which test *T_DB !=
9462 * NUL. It has the disadvantage that the user cannot use any :set t_*
9463 * command to get T_DB (back) to empty_option, only :set term=... will do
9464 * the trick...
9465 * Anyway, this hack will hopefully go away with the next OS release.
9466 * (Olaf Seibert)
9467 */
9468 else if (row == 0 && T_DB == empty_option
9469 && (line_count == 1 || *T_CDL == NUL))
9470#else
9471 else if (row == 0 && (
9472#ifndef AMIGA
9473 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9474 * up, so use delete-line command */
9475 line_count == 1 ||
9476#endif
9477 *T_CDL == NUL))
9478#endif
9479 type = USE_NL;
9480 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9481 type = USE_T_CDL;
9482 else if (can_clear(T_CE) && result_empty
9483#ifdef FEAT_VERTSPLIT
9484 && (wp == NULL || wp->w_width == Columns)
9485#endif
9486 )
9487 type = USE_T_CE;
9488 else if (*T_DL != NUL && can_delete)
9489 type = USE_T_DL;
9490 else if (*T_CDL != NUL && can_delete)
9491 type = USE_T_CDL;
9492 else
9493 return FAIL;
9494
9495#ifdef FEAT_CLIPBOARD
9496 /* Remove a modeless selection when deleting lines halfway the screen or
9497 * not the full width of the screen. */
9498 if (off + row > 0
9499# ifdef FEAT_VERTSPLIT
9500 || (wp != NULL && wp->w_width != Columns)
9501# endif
9502 )
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009503 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 else
9505 clip_scroll_selection(line_count);
9506#endif
9507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#ifdef FEAT_GUI
9509 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9510 * scrolling is actually carried out. */
9511 gui_dont_update_cursor();
9512#endif
9513
9514 if (*T_CCS != NUL) /* cursor relative to region */
9515 {
9516 cursor_row = row;
9517 cursor_end = end;
9518 }
9519 else
9520 {
9521 cursor_row = row + off;
9522 cursor_end = end + off;
9523 }
9524
9525 /*
9526 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9527 * Clear the inserted lines in ScreenLines[].
9528 */
9529 row += off;
9530 end += off;
9531 for (i = 0; i < line_count; ++i)
9532 {
9533#ifdef FEAT_VERTSPLIT
9534 if (wp != NULL && wp->w_width != Columns)
9535 {
9536 /* need to copy part of a line */
9537 j = row + i;
9538 while ((j += line_count) <= end - 1)
9539 linecopy(j - line_count, j, wp);
9540 j -= line_count;
9541 if (can_clear((char_u *)" "))
9542 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width);
9543 else
9544 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9545 LineWraps[j] = FALSE;
9546 }
9547 else
9548#endif
9549 {
9550 /* whole width, moving the line pointers is faster */
9551 j = row + i;
9552 temp = LineOffset[j];
9553 while ((j += line_count) <= end - 1)
9554 {
9555 LineOffset[j - line_count] = LineOffset[j];
9556 LineWraps[j - line_count] = LineWraps[j];
9557 }
9558 LineOffset[j - line_count] = temp;
9559 LineWraps[j - line_count] = FALSE;
9560 if (can_clear((char_u *)" "))
9561 lineclear(temp, (int)Columns);
9562 else
9563 lineinvalid(temp, (int)Columns);
9564 }
9565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566
9567 screen_stop_highlight();
9568
9569#ifdef FEAT_VERTSPLIT
9570 /* redraw the characters */
9571 if (type == USE_REDRAW)
9572 redraw_block(row, end, wp);
9573 else
9574#endif
9575 if (type == USE_T_CD) /* delete the lines */
9576 {
9577 windgoto(cursor_row, 0);
9578 out_str(T_CD);
9579 screen_start(); /* don't know where cursor is now */
9580 }
9581 else if (type == USE_T_CDL)
9582 {
9583 windgoto(cursor_row, 0);
9584 term_delete_lines(line_count);
9585 screen_start(); /* don't know where cursor is now */
9586 }
9587 /*
9588 * Deleting lines at top of the screen or scroll region: Just scroll
9589 * the whole screen (scroll region) up by outputting newlines on the
9590 * last line.
9591 */
9592 else if (type == USE_NL)
9593 {
9594 windgoto(cursor_end - 1, 0);
9595 for (i = line_count; --i >= 0; )
9596 out_char('\n'); /* cursor will remain on same line */
9597 }
9598 else
9599 {
9600 for (i = line_count; --i >= 0; )
9601 {
9602 if (type == USE_T_DL)
9603 {
9604 windgoto(cursor_row, 0);
9605 out_str(T_DL); /* delete a line */
9606 }
9607 else /* type == USE_T_CE */
9608 {
9609 windgoto(cursor_row + i, 0);
9610 out_str(T_CE); /* erase a line */
9611 }
9612 screen_start(); /* don't know where cursor is now */
9613 }
9614 }
9615
9616 /*
9617 * If the 'db' flag is set, we need to clear the lines that have been
9618 * scrolled up at the bottom of the region.
9619 */
9620 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9621 {
9622 for (i = line_count; i > 0; --i)
9623 {
9624 windgoto(cursor_end - i, 0);
9625 out_str(T_CE); /* erase a line */
9626 screen_start(); /* don't know where cursor is now */
9627 }
9628 }
9629
9630#ifdef FEAT_GUI
9631 gui_can_update_cursor();
9632 if (gui.in_use)
9633 out_flush(); /* always flush after a scroll */
9634#endif
9635
9636 return OK;
9637}
9638
9639/*
9640 * show the current mode and ruler
9641 *
9642 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9643 * If clear_cmdline is FALSE there may be a message there that needs to be
9644 * cleared only if a mode is shown.
9645 * Return the length of the message (0 if no message).
9646 */
9647 int
9648showmode()
9649{
9650 int need_clear;
9651 int length = 0;
9652 int do_mode;
9653 int attr;
9654 int nwr_save;
9655#ifdef FEAT_INS_EXPAND
9656 int sub_attr;
9657#endif
9658
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009659 do_mode = ((p_smd && msg_silent == 0)
9660 && ((State & INSERT)
9661 || restart_edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662#ifdef FEAT_VISUAL
9663 || VIsual_active
9664#endif
9665 ));
9666 if (do_mode || Recording)
9667 {
9668 /*
9669 * Don't show mode right now, when not redrawing or inside a mapping.
9670 * Call char_avail() only when we are going to show something, because
9671 * it takes a bit of time.
9672 */
9673 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
9674 {
9675 redraw_cmdline = TRUE; /* show mode later */
9676 return 0;
9677 }
9678
9679 nwr_save = need_wait_return;
9680
9681 /* wait a bit before overwriting an important message */
9682 check_for_delay(FALSE);
9683
9684 /* if the cmdline is more than one line high, erase top lines */
9685 need_clear = clear_cmdline;
9686 if (clear_cmdline && cmdline_row < Rows - 1)
9687 msg_clr_cmdline(); /* will reset clear_cmdline */
9688
9689 /* Position on the last line in the window, column 0 */
9690 msg_pos_mode();
9691 cursor_off();
9692 attr = hl_attr(HLF_CM); /* Highlight mode */
9693 if (do_mode)
9694 {
9695 MSG_PUTS_ATTR("--", attr);
9696#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009697 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009698# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009699 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009700# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009701 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009702# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009703 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009704# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 MSG_PUTS_ATTR(" IM", attr);
9706# else
9707 MSG_PUTS_ATTR(" XIM", attr);
9708# endif
9709#endif
9710#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9711 if (gui.in_use)
9712 {
9713 if (hangul_input_state_get())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009714 MSG_PUTS_ATTR(" \307\321\261\333", attr); /* HANGUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
9716#endif
9717#ifdef FEAT_INS_EXPAND
9718 if (edit_submode != NULL) /* CTRL-X in Insert mode */
9719 {
9720 /* These messages can get long, avoid a wrap in a narrow
9721 * window. Prefer showing edit_submode_extra. */
9722 length = (Rows - msg_row) * Columns - 3;
9723 if (edit_submode_extra != NULL)
9724 length -= vim_strsize(edit_submode_extra);
9725 if (length > 0)
9726 {
9727 if (edit_submode_pre != NULL)
9728 length -= vim_strsize(edit_submode_pre);
9729 if (length - vim_strsize(edit_submode) > 0)
9730 {
9731 if (edit_submode_pre != NULL)
9732 msg_puts_attr(edit_submode_pre, attr);
9733 msg_puts_attr(edit_submode, attr);
9734 }
9735 if (edit_submode_extra != NULL)
9736 {
9737 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
9738 if ((int)edit_submode_highl < (int)HLF_COUNT)
9739 sub_attr = hl_attr(edit_submode_highl);
9740 else
9741 sub_attr = attr;
9742 msg_puts_attr(edit_submode_extra, sub_attr);
9743 }
9744 }
9745 length = 0;
9746 }
9747 else
9748#endif
9749 {
9750#ifdef FEAT_VREPLACE
9751 if (State & VREPLACE_FLAG)
9752 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
9753 else
9754#endif
9755 if (State & REPLACE_FLAG)
9756 MSG_PUTS_ATTR(_(" REPLACE"), attr);
9757 else if (State & INSERT)
9758 {
9759#ifdef FEAT_RIGHTLEFT
9760 if (p_ri)
9761 MSG_PUTS_ATTR(_(" REVERSE"), attr);
9762#endif
9763 MSG_PUTS_ATTR(_(" INSERT"), attr);
9764 }
9765 else if (restart_edit == 'I')
9766 MSG_PUTS_ATTR(_(" (insert)"), attr);
9767 else if (restart_edit == 'R')
9768 MSG_PUTS_ATTR(_(" (replace)"), attr);
9769 else if (restart_edit == 'V')
9770 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
9771#ifdef FEAT_RIGHTLEFT
9772 if (p_hkmap)
9773 MSG_PUTS_ATTR(_(" Hebrew"), attr);
9774# ifdef FEAT_FKMAP
9775 if (p_fkmap)
9776 MSG_PUTS_ATTR(farsi_text_5, attr);
9777# endif
9778#endif
9779#ifdef FEAT_KEYMAP
9780 if (State & LANGMAP)
9781 {
9782# ifdef FEAT_ARABIC
9783 if (curwin->w_p_arab)
9784 MSG_PUTS_ATTR(_(" Arabic"), attr);
9785 else
9786# endif
9787 MSG_PUTS_ATTR(_(" (lang)"), attr);
9788 }
9789#endif
9790 if ((State & INSERT) && p_paste)
9791 MSG_PUTS_ATTR(_(" (paste)"), attr);
9792
9793#ifdef FEAT_VISUAL
9794 if (VIsual_active)
9795 {
9796 char *p;
9797
9798 /* Don't concatenate separate words to avoid translation
9799 * problems. */
9800 switch ((VIsual_select ? 4 : 0)
9801 + (VIsual_mode == Ctrl_V) * 2
9802 + (VIsual_mode == 'V'))
9803 {
9804 case 0: p = N_(" VISUAL"); break;
9805 case 1: p = N_(" VISUAL LINE"); break;
9806 case 2: p = N_(" VISUAL BLOCK"); break;
9807 case 4: p = N_(" SELECT"); break;
9808 case 5: p = N_(" SELECT LINE"); break;
9809 default: p = N_(" SELECT BLOCK"); break;
9810 }
9811 MSG_PUTS_ATTR(_(p), attr);
9812 }
9813#endif
9814 MSG_PUTS_ATTR(" --", attr);
9815 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 need_clear = TRUE;
9818 }
9819 if (Recording
9820#ifdef FEAT_INS_EXPAND
9821 && edit_submode == NULL /* otherwise it gets too long */
9822#endif
9823 )
9824 {
9825 MSG_PUTS_ATTR(_("recording"), attr);
9826 need_clear = TRUE;
9827 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009828
9829 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 if (need_clear || clear_cmdline)
9831 msg_clr_eos();
9832 msg_didout = FALSE; /* overwrite this message */
9833 length = msg_col;
9834 msg_col = 0;
9835 need_wait_return = nwr_save; /* never ask for hit-return for this */
9836 }
9837 else if (clear_cmdline && msg_silent == 0)
9838 /* Clear the whole command line. Will reset "clear_cmdline". */
9839 msg_clr_cmdline();
9840
9841#ifdef FEAT_CMDL_INFO
9842# ifdef FEAT_VISUAL
9843 /* In Visual mode the size of the selected area must be redrawn. */
9844 if (VIsual_active)
9845 clear_showcmd();
9846# endif
9847
9848 /* If the last window has no status line, the ruler is after the mode
9849 * message and must be redrawn */
9850 if (redrawing()
9851# ifdef FEAT_WINDOWS
9852 && lastwin->w_status_height == 0
9853# endif
9854 )
9855 win_redr_ruler(lastwin, TRUE);
9856#endif
9857 redraw_cmdline = FALSE;
9858 clear_cmdline = FALSE;
9859
9860 return length;
9861}
9862
9863/*
9864 * Position for a mode message.
9865 */
9866 static void
9867msg_pos_mode()
9868{
9869 msg_col = 0;
9870 msg_row = Rows - 1;
9871}
9872
9873/*
9874 * Delete mode message. Used when ESC is typed which is expected to end
9875 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009876 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 */
9878 void
9879unshowmode(force)
9880 int force;
9881{
9882 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +01009883 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 */
9885 if (!redrawing() || (!force && char_avail() && !KeyTyped))
9886 redraw_cmdline = TRUE; /* delete mode later */
9887 else
9888 {
9889 msg_pos_mode();
9890 if (Recording)
9891 MSG_PUTS_ATTR(_("recording"), hl_attr(HLF_CM));
9892 msg_clr_eos();
9893 }
9894}
9895
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009896#if defined(FEAT_WINDOWS)
9897/*
9898 * Draw the tab pages line at the top of the Vim window.
9899 */
9900 static void
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009901draw_tabline()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009902{
9903 int tabcount = 0;
9904 tabpage_T *tp;
9905 int tabwidth;
9906 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009907 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009908 int attr;
9909 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009910 win_T *cwp;
9911 int wincount;
9912 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009913 int c;
9914 int len;
9915 int attr_sel = hl_attr(HLF_TPS);
9916 int attr_nosel = hl_attr(HLF_TP);
9917 int attr_fill = hl_attr(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009918 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009919 int room;
9920 int use_sep_chars = (t_colors < 8
9921#ifdef FEAT_GUI
9922 && !gui.in_use
9923#endif
9924 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009925
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009926 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009927
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009928#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +00009929 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00009930 if (gui_use_tabline())
9931 {
9932 gui_update_tabline();
9933 return;
9934 }
9935#endif
9936
9937 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009938 return;
9939
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009940#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009941
9942 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
9943 for (scol = 0; scol < Columns; ++scol)
9944 TabPageIdxs[scol] = 0;
9945
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009946 /* Use the 'tabline' option if it's set. */
9947 if (*p_tal != NUL)
9948 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009949 int save_called_emsg = called_emsg;
9950
9951 /* Check for an error. If there is one we would loop in redrawing the
9952 * screen. Avoid that by making 'tabline' empty. */
9953 called_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009954 win_redr_custom(NULL, FALSE);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009955 if (called_emsg)
9956 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00009957 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +00009958 called_emsg |= save_called_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009959 }
Bram Moolenaar238a5642006-02-21 22:12:05 +00009960 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009961#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009962 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009963 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
9964 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009965
Bram Moolenaar238a5642006-02-21 22:12:05 +00009966 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
9967 if (tabwidth < 6)
9968 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00009969
Bram Moolenaar238a5642006-02-21 22:12:05 +00009970 attr = attr_nosel;
9971 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009972 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00009973 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
9974 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009975 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009976 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009977
Bram Moolenaar238a5642006-02-21 22:12:05 +00009978 if (tp->tp_topframe == topframe)
9979 attr = attr_sel;
9980 if (use_sep_chars && col > 0)
9981 screen_putchar('|', 0, col++, attr);
9982
9983 if (tp->tp_topframe != topframe)
9984 attr = attr_nosel;
9985
9986 screen_putchar(' ', 0, col++, attr);
9987
9988 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00009989 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00009990 cwp = curwin;
9991 wp = firstwin;
9992 }
9993 else
9994 {
9995 cwp = tp->tp_curwin;
9996 wp = tp->tp_firstwin;
9997 }
9998
9999 modified = FALSE;
10000 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10001 if (bufIsChanged(wp->w_buffer))
10002 modified = TRUE;
10003 if (modified || wincount > 1)
10004 {
10005 if (wincount > 1)
10006 {
10007 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010008 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010009 if (col + len >= Columns - 3)
10010 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010011 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010012#if defined(FEAT_SYN_HL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010013 hl_combine_attr(attr, hl_attr(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010014#else
Bram Moolenaar238a5642006-02-21 22:12:05 +000010015 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010016#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010017 );
10018 col += len;
10019 }
10020 if (modified)
10021 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10022 screen_putchar(' ', 0, col++, attr);
10023 }
10024
10025 room = scol - col + tabwidth - 1;
10026 if (room > 0)
10027 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010028 /* Get buffer name in NameBuff[] */
10029 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010030 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010031 len = vim_strsize(NameBuff);
10032 p = NameBuff;
10033#ifdef FEAT_MBYTE
10034 if (has_mbyte)
10035 while (len > room)
10036 {
10037 len -= ptr2cells(p);
10038 mb_ptr_adv(p);
10039 }
10040 else
10041#endif
10042 if (len > room)
10043 {
10044 p += len - room;
10045 len = room;
10046 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010047 if (len > Columns - col - 1)
10048 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010049
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010050 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010051 col += len;
10052 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010053 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010054
10055 /* Store the tab page number in TabPageIdxs[], so that
10056 * jump_to_mouse() knows where each one is. */
10057 ++tabcount;
10058 while (scol < col)
10059 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010060 }
10061
Bram Moolenaar238a5642006-02-21 22:12:05 +000010062 if (use_sep_chars)
10063 c = '_';
10064 else
10065 c = ' ';
10066 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010067
10068 /* Put an "X" for closing the current tab if there are several. */
10069 if (first_tabpage->tp_next != NULL)
10070 {
10071 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10072 TabPageIdxs[Columns - 1] = -999;
10073 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010074 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010075
10076 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10077 * set. */
10078 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010079}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010080
10081/*
10082 * Get buffer name for "buf" into NameBuff[].
10083 * Takes care of special buffer names and translates special characters.
10084 */
10085 void
10086get_trans_bufname(buf)
10087 buf_T *buf;
10088{
10089 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010090 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010091 else
10092 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10093 trans_characters(NameBuff, MAXPATHL);
10094}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010095#endif
10096
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097#if defined(FEAT_WINDOWS) || defined(FEAT_WILDMENU) || defined(FEAT_STL_OPT)
10098/*
10099 * Get the character to use in a status line. Get its attributes in "*attr".
10100 */
10101 static int
10102fillchar_status(attr, is_curwin)
10103 int *attr;
10104 int is_curwin;
10105{
10106 int fill;
10107 if (is_curwin)
10108 {
10109 *attr = hl_attr(HLF_S);
10110 fill = fill_stl;
10111 }
10112 else
10113 {
10114 *attr = hl_attr(HLF_SNC);
10115 fill = fill_stlnc;
10116 }
10117 /* Use fill when there is highlighting, and highlighting of current
10118 * window differs, or the fillchars differ, or this is not the
10119 * current window */
10120 if (*attr != 0 && ((hl_attr(HLF_S) != hl_attr(HLF_SNC)
10121 || !is_curwin || firstwin == lastwin)
10122 || (fill_stl != fill_stlnc)))
10123 return fill;
10124 if (is_curwin)
10125 return '^';
10126 return '=';
10127}
10128#endif
10129
10130#ifdef FEAT_VERTSPLIT
10131/*
10132 * Get the character to use in a separator between vertically split windows.
10133 * Get its attributes in "*attr".
10134 */
10135 static int
10136fillchar_vsep(attr)
10137 int *attr;
10138{
10139 *attr = hl_attr(HLF_C);
10140 if (*attr == 0 && fill_vert == ' ')
10141 return '|';
10142 else
10143 return fill_vert;
10144}
10145#endif
10146
10147/*
10148 * Return TRUE if redrawing should currently be done.
10149 */
10150 int
10151redrawing()
10152{
10153 return (!RedrawingDisabled
10154 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10155}
10156
10157/*
10158 * Return TRUE if printing messages should currently be done.
10159 */
10160 int
10161messaging()
10162{
10163 return (!(p_lz && char_avail() && !KeyTyped));
10164}
10165
10166/*
10167 * Show current status info in ruler and various other places
10168 * If always is FALSE, only show ruler if position has changed.
10169 */
10170 void
10171showruler(always)
10172 int always;
10173{
10174 if (!always && !redrawing())
10175 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010176#ifdef FEAT_INS_EXPAND
10177 if (pum_visible())
10178 {
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010179# ifdef FEAT_WINDOWS
Bram Moolenaar9372a112005-12-06 19:59:18 +000010180 /* Don't redraw right now, do it later. */
10181 curwin->w_redr_status = TRUE;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +000010182# endif
Bram Moolenaar9372a112005-12-06 19:59:18 +000010183 return;
10184 }
10185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186#if defined(FEAT_STL_OPT) && defined(FEAT_WINDOWS)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010187 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010188 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010189 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 else
10192#endif
10193#ifdef FEAT_CMDL_INFO
10194 win_redr_ruler(curwin, always);
10195#endif
10196
10197#ifdef FEAT_TITLE
10198 if (need_maketitle
10199# ifdef FEAT_STL_OPT
10200 || (p_icon && (stl_syntax & STL_IN_ICON))
10201 || (p_title && (stl_syntax & STL_IN_TITLE))
10202# endif
10203 )
10204 maketitle();
10205#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010206#ifdef FEAT_WINDOWS
10207 /* Redraw the tab pages line if needed. */
10208 if (redraw_tabline)
10209 draw_tabline();
10210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211}
10212
10213#ifdef FEAT_CMDL_INFO
10214 static void
10215win_redr_ruler(wp, always)
10216 win_T *wp;
10217 int always;
10218{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010219#define RULER_BUF_LEN 70
10220 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 int row;
10222 int fillchar;
10223 int attr;
10224 int empty_line = FALSE;
10225 colnr_T virtcol;
10226 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010227 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 int o;
10229#ifdef FEAT_VERTSPLIT
10230 int this_ru_col;
10231 int off = 0;
10232 int width = Columns;
10233# define WITH_OFF(x) x
10234# define WITH_WIDTH(x) x
10235#else
10236# define WITH_OFF(x) 0
10237# define WITH_WIDTH(x) Columns
10238# define this_ru_col ru_col
10239#endif
10240
10241 /* If 'ruler' off or redrawing disabled, don't do anything */
10242 if (!p_ru)
10243 return;
10244
10245 /*
10246 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10247 * after deleting lines, before cursor.lnum is corrected.
10248 */
10249 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10250 return;
10251
10252#ifdef FEAT_INS_EXPAND
10253 /* Don't draw the ruler while doing insert-completion, it might overwrite
10254 * the (long) mode message. */
10255# ifdef FEAT_WINDOWS
10256 if (wp == lastwin && lastwin->w_status_height == 0)
10257# endif
10258 if (edit_submode != NULL)
10259 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010260 /* Don't draw the ruler when the popup menu is visible, it may overlap. */
10261 if (pum_visible())
10262 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263#endif
10264
10265#ifdef FEAT_STL_OPT
10266 if (*p_ruf)
10267 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010268 int save_called_emsg = called_emsg;
10269
10270 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010272 if (called_emsg)
10273 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010274 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010275 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 return;
10277 }
10278#endif
10279
10280 /*
10281 * Check if not in Insert mode and the line is empty (will show "0-1").
10282 */
10283 if (!(State & INSERT)
10284 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10285 empty_line = TRUE;
10286
10287 /*
10288 * Only draw the ruler when something changed.
10289 */
10290 validate_virtcol_win(wp);
10291 if ( redraw_cmdline
10292 || always
10293 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10294 || wp->w_cursor.col != wp->w_ru_cursor.col
10295 || wp->w_virtcol != wp->w_ru_virtcol
10296#ifdef FEAT_VIRTUALEDIT
10297 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
10298#endif
10299 || wp->w_topline != wp->w_ru_topline
10300 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10301#ifdef FEAT_DIFF
10302 || wp->w_topfill != wp->w_ru_topfill
10303#endif
10304 || empty_line != wp->w_ru_empty)
10305 {
10306 cursor_off();
10307#ifdef FEAT_WINDOWS
10308 if (wp->w_status_height)
10309 {
10310 row = W_WINROW(wp) + wp->w_height;
10311 fillchar = fillchar_status(&attr, wp == curwin);
10312# ifdef FEAT_VERTSPLIT
10313 off = W_WINCOL(wp);
10314 width = W_WIDTH(wp);
10315# endif
10316 }
10317 else
10318#endif
10319 {
10320 row = Rows - 1;
10321 fillchar = ' ';
10322 attr = 0;
10323#ifdef FEAT_VERTSPLIT
10324 width = Columns;
10325 off = 0;
10326#endif
10327 }
10328
10329 /* In list mode virtcol needs to be recomputed */
10330 virtcol = wp->w_virtcol;
10331 if (wp->w_p_list && lcs_tab1 == NUL)
10332 {
10333 wp->w_p_list = FALSE;
10334 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10335 wp->w_p_list = TRUE;
10336 }
10337
10338 /*
10339 * Some sprintfs return the length, some return a pointer.
10340 * To avoid portability problems we use strlen() here.
10341 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010342 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10344 ? 0L
10345 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010346 len = STRLEN(buffer);
10347 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10349 (int)virtcol + 1);
10350
10351 /*
10352 * Add a "50%" if there is room for it.
10353 * On the last line, don't print in the last column (scrolls the
10354 * screen up on some terminals).
10355 */
10356 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010357 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 o = i + vim_strsize(buffer + i + 1);
10359#ifdef FEAT_WINDOWS
10360 if (wp->w_status_height == 0) /* can't use last char of screen */
10361#endif
10362 ++o;
10363#ifdef FEAT_VERTSPLIT
10364 this_ru_col = ru_col - (Columns - width);
10365 if (this_ru_col < 0)
10366 this_ru_col = 0;
10367#endif
10368 /* Never use more than half the window/screen width, leave the other
10369 * half for the filename. */
10370 if (this_ru_col < (WITH_WIDTH(width) + 1) / 2)
10371 this_ru_col = (WITH_WIDTH(width) + 1) / 2;
10372 if (this_ru_col + o < WITH_WIDTH(width))
10373 {
10374 while (this_ru_col + o < WITH_WIDTH(width))
10375 {
10376#ifdef FEAT_MBYTE
10377 if (has_mbyte)
10378 i += (*mb_char2bytes)(fillchar, buffer + i);
10379 else
10380#endif
10381 buffer[i++] = fillchar;
10382 ++o;
10383 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010384 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 /* Truncate at window boundary. */
10387#ifdef FEAT_MBYTE
10388 if (has_mbyte)
10389 {
10390 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010391 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 {
10393 o += (*mb_ptr2cells)(buffer + i);
10394 if (this_ru_col + o > WITH_WIDTH(width))
10395 {
10396 buffer[i] = NUL;
10397 break;
10398 }
10399 }
10400 }
10401 else
10402#endif
10403 if (this_ru_col + (int)STRLEN(buffer) > WITH_WIDTH(width))
10404 buffer[WITH_WIDTH(width) - this_ru_col] = NUL;
10405
10406 screen_puts(buffer, row, this_ru_col + WITH_OFF(off), attr);
10407 i = redraw_cmdline;
10408 screen_fill(row, row + 1,
10409 this_ru_col + WITH_OFF(off) + (int)STRLEN(buffer),
10410 (int)(WITH_OFF(off) + WITH_WIDTH(width)),
10411 fillchar, fillchar, attr);
10412 /* don't redraw the cmdline because of showing the ruler */
10413 redraw_cmdline = i;
10414 wp->w_ru_cursor = wp->w_cursor;
10415 wp->w_ru_virtcol = wp->w_virtcol;
10416 wp->w_ru_empty = empty_line;
10417 wp->w_ru_topline = wp->w_topline;
10418 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10419#ifdef FEAT_DIFF
10420 wp->w_ru_topfill = wp->w_topfill;
10421#endif
10422 }
10423}
10424#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010425
10426#if defined(FEAT_LINEBREAK) || defined(PROTO)
10427/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010428 * Return the width of the 'number' and 'relativenumber' column.
10429 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010430 * Otherwise it depends on 'numberwidth' and the line count.
10431 */
10432 int
10433number_width(wp)
10434 win_T *wp;
10435{
10436 int n;
10437 linenr_T lnum;
10438
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010439 if (wp->w_p_rnu && !wp->w_p_nu)
10440 /* cursor line shows "0" */
10441 lnum = wp->w_height;
10442 else
10443 /* cursor line shows absolute line number */
10444 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010445
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010446 if (lnum == wp->w_nrwidth_line_count)
10447 return wp->w_nrwidth_width;
10448 wp->w_nrwidth_line_count = lnum;
10449
10450 n = 0;
10451 do
10452 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010453 lnum /= 10;
10454 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010455 } while (lnum > 0);
10456
10457 /* 'numberwidth' gives the minimal width plus one */
10458 if (n < wp->w_p_nuw - 1)
10459 n = wp->w_p_nuw - 1;
10460
10461 wp->w_nrwidth_width = n;
10462 return n;
10463}
10464#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010465
10466/*
10467 * Return the current cursor column. This is the actual position on the
10468 * screen. First column is 0.
10469 */
10470 int
10471screen_screencol()
10472{
10473 return screen_cur_col;
10474}
10475
10476/*
10477 * Return the current cursor row. This is the actual position on the screen.
10478 * First row is 0.
10479 */
10480 int
10481screen_screenrow()
10482{
10483 return screen_cur_row;
10484}